{"id":27925,"date":"2025-07-27T11:57:05","date_gmt":"2025-07-27T03:57:05","guid":{"rendered":"http:\/\/139.9.1.231\/?p=27925"},"modified":"2025-07-27T11:57:06","modified_gmt":"2025-07-27T03:57:06","slug":"llm-packed-samples-sorted-batching","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2025\/07\/27\/llm-packed-samples-sorted-batching\/","title":{"rendered":"\u9ad8\u6548 LLM \u8bad\u7ec3\u65b9\u6cd5\uff1aPacked samples\u548c sorted batching"},"content":{"rendered":"\n<ul class=\"has-light-gray-background-color has-background\"><li>\u8bba\u6587\uff1a<a href=\"https:\/\/arxiv.org\/pdf\/2401.18058\">LongAlign: A Recipe for Long Context Alignment of<\/a> Large Language Models<\/li><li>\u6587\u7ae0\uff1a<a href=\"https:\/\/huggingface.co\/blog\/sirluk\/llm-sequence-packing\">https:\/\/huggingface.co\/blog\/sirluk\/llm-sequence-packing<\/a><\/li><li>code\u5b9e\u73b0\uff1a<a href=\"https:\/\/github.com\/THUDM\/LongAlign\"><strong>https:\/\/github.com\/THUDM\/LongAlign<\/strong><\/a><\/li><\/ul>\n\n\n\n<p>\u00a0\u8981\u8ba9\u5927\u578b\u8bed\u8a00\u6a21\u578b\u66f4\u6709\u6548\u5730\u5904\u7406\u957f\u6587\u672c\u4e0a\u4e0b\u6587\uff0c\u9700\u8981\u5728\u76f8\u4f3c\u957f\u5ea6\u7684\u8f93\u5165\u5e8f\u5217\u4e0a\u8fdb\u884c\u6307\u4ee4\u5fae\u8c03\u3002LongAlign \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5927\u578b\u8bed\u8a00\u6a21\u578b\u6709\u6548\u5904\u7406\u957f\u8fbe 64k \u7684\u957f\u4e0a\u4e0b\u6587\uff0c\u5e76\u5c55\u73b0\u51fa\u5f3a\u5927\u7684\u957f\u6587\u672c\u7406\u89e3\u548c\u751f\u6210\u80fd\u529b\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-76.png\" alt=\"\" class=\"wp-image-27940\" width=\"392\" height=\"333\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-76.png 721w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-76-300x255.png 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/figure><\/div>\n\n\n\n<h3>LongAlign\u00a0\uff1a<\/h3>\n\n\n\n<h4>\u52a8\u673a\uff1a<\/h4>\n\n\n\n<ul><li>\u76ee\u524d\u7f3a\u4e4f\u7528\u4e8e\u6709\u76d1\u7763\u5fae\u8c03\uff08SFT\uff09\u7684\u957f\u6587\u672c\u6307\u4ee4\u8ddf\u968f\u6570\u636e\u96c6\uff0c\u66f4\u7f3a\u4e4f\u6784\u5efa\u6b64\u7c7b\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/li><li>\u957f\u4e0a\u4e0b\u6587\u6570\u636e\u7684\u957f\u5ea6\u5206\u5e03\u5dee\u5f02\u8f83\u5927\uff0c\u5728\u591aGPU\u73af\u5883\u4e2d\u4e25\u91cd\u964d\u4f4e\u4e86\u4f20\u7edf\u6279\u5904\u7406\u65b9\u6cd5\u7684\u8bad\u7ec3\u6548\u7387\u2014\u2014\u5904\u7406\u8f83\u77ed\u8f93\u5165\u7684GPU\u5fc5\u987b\u7b49\u5f85\u5904\u7406\u8f83\u957f\u8f93\u5165\u7684GPU\u5b8c\u6210\u4efb\u52a1\u540e\u624d\u80fd\u7ee7\u7eed\u8fd0\u884c\u3002<\/li><li>\u4e9f\u9700\u4e00\u4e2a\u5f3a\u5065\u7684\u57fa\u51c6\u8bc4\u4f30\u4f53\u7cfb\uff0c\u7528\u4e8e\u8861\u91cf\u5927\u578b\u8bed\u8a00\u6a21\u578b\u5728\u9762\u5bf9\u771f\u5b9e\u4e16\u754c\u957f\u6587\u672c\u67e5\u8be2\u65f6\u7684\u5904\u7406\u80fd\u529b\u3002<\/li><\/ul>\n\n\n\n<h4>\u8d21\u732e\uff1a<\/h4>\n\n\n\n<p><strong>LongAlign<\/strong> \u65b9\u6cd5\uff0c\u5206\u522b\u4ece\u6570\u636e\u6784\u5efa\u3001\u9ad8\u6548\u8bad\u7ec3\u548c\u8bc4\u4f30\u4e09\u4e2a\u65b9\u9762\u5165\u624b\uff1a<\/p>\n\n\n\n<p>\u5728\u6570\u636e\u65b9\u9762\uff0c\u4e3a\u6784\u5efa\u4e00\u4e2a\u591a\u6837\u5316\u7684\u957f\u6587\u672c\u6307\u4ee4\u8ddf\u968f\u6570\u636e\u96c6\uff0c\u4ece\u4e5d\u4e2a\u6765\u6e90\u6536\u96c6\u4e86\u957f\u6587\u672c\u5e8f\u5217\uff0c\u5e76\u4f7f\u7528 Self-Instruct\u751f\u6210\u4e86 1 \u4e07\u6761\u957f\u5ea6\u5728 8k \u5230 64k \u4e4b\u95f4\u7684\u6307\u4ee4\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u5728\u8bad\u7ec3\u65b9\u9762\uff0c\u4e3a\u5e94\u5bf9\u4e0d\u5747\u5300\u6279\u5904\u7406\u5bfc\u81f4\u7684\u6548\u7387\u95ee\u9898\uff0c\u91c7\u7528\u4e86<strong> packing \u7b56\u7565<\/strong>\uff0c\u5373\u5728<strong>\u5c06\u6570\u636e\u5206\u53d1\u5230 GPU \u4e4b\u524d\uff0c\u5c06\u591a\u4e2a\u5e8f\u5217\u6253\u5305\u4e3a\u63a5\u8fd1\u6700\u5927\u957f\u5ea6\u7684\u7ec4\u5408<\/strong>\u3002\u4f46\u6211\u4eec\u53d1\u73b0\u8fd9\u79cd\u6253\u5305\u8bad\u7ec3\u4e2d\u7684<strong>\u635f\u5931\u8ba1\u7b97\u5b58\u5728\u504f\u5dee\uff1a\u4e0d\u540c\u6570\u91cf\u5e8f\u5217\u7684\u6253\u5305\u5728\u6700\u7ec8\u635f\u5931\u8ba1\u7b97\u4e2d\u88ab\u8d4b\u4e88\u76f8\u540c\u6743\u91cd<\/strong>\u3002\u4e3a\u7f13\u89e3\u8fd9\u4e00\u95ee\u9898\uff0c\u6211\u4eec\u63d0\u51fa\u4e86<strong>\u201c\u635f\u5931\u52a0\u6743\u7b56\u7565\u201d<\/strong>\uff0c\u5bf9\u6bcf\u6761\u6587\u672c\u7684\u635f\u5931\u8fdb\u884c\u52a0\u6743\u5e73\u5747\uff0c\u4ee5\u5e73\u8861\u4e0d\u540c\u5e8f\u5217\u5bf9\u6574\u4f53\u635f\u5931\u7684\u8d21\u732e\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u5f15\u5165\u4e86<strong>\u201c\u6392\u5e8f\u6279\u5904\u7406\u201d\u65b9\u6cd5\uff0c\u5c06\u957f\u5ea6\u76f8\u8fd1\u7684\u5e8f\u5217\u5206\u7ec4\uff0c\u4ece\u800c\u51cf\u5c11\u6279\u5185\u7a7a\u95f2\u65f6\u95f4<\/strong>\u3002<\/p>\n\n\n\n<p>\u5728\u8bc4\u4f30\u65b9\u9762\uff0c\u5f00\u53d1\u4e86 <strong>LongBench-Chat<\/strong> \u57fa\u51c6\u6d4b\u8bd5\uff0c\u5b83\u5305\u542b\u957f\u5ea6\u4e3a 10k-100k \u7684\u5f00\u653e\u5f0f\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u7531\u535a\u58eb\u751f\u8fdb\u884c\u6807\u6ce8\u3002\u8bc4\u4f30\u5185\u5bb9\u6db5\u76d6\u63a8\u7406\u3001\u7f16\u7a0b\u3001\u6458\u8981\u4ee5\u53ca\u591a\u8bed\u79cd\u7ffb\u8bd1\u7b49\u591a\u79cd\u957f\u6587\u672c\u6307\u4ee4\u8ddf\u968f\u80fd\u529b\u3002\u4f7f\u7528 GPT-4\uff08OpenAI\uff0c2023b\uff09\u7ed3\u5408\u4eba\u5de5\u6807\u6ce8\u7ed3\u679c\u548c\u5c11\u91cf\u793a\u4f8b\uff0c\u5bf9\u6a21\u578b\u751f\u6210\u7684\u56de\u7b54\u8fdb\u884c\u8bc4\u5206\u3002<\/p>\n\n\n\n<h4>\u7ed3\u8bba\uff1a<\/h4>\n\n\n\n<p><strong>\u6570\u636e\u91cf\u4e0e\u591a\u6837\u6027\u7684\u5f71\u54cd<\/strong>\uff1a\u957f\u6587\u672c\u6307\u4ee4\u6570\u636e\u7684\u6570\u91cf\u548c\u591a\u6837\u6027\u90fd\u4f1a\u663e\u8457\u5f71\u54cd\u6a21\u578b\u5904\u7406\u957f\u4e0a\u4e0b\u6587\u7684\u80fd\u529b\uff0c\u6700\u7ec8\u6027\u80fd\u5dee\u5f02\u6700\u9ad8\u53ef\u8fbe 30%\u3002<\/p>\n\n\n\n<p><strong>\u957f\u6587\u672c\u6307\u4ee4\u6570\u636e\u7684\u76ca\u5904<\/strong>\uff1a\u589e\u52a0\u957f\u6587\u672c\u6307\u4ee4\u6570\u636e\u6709\u52a9\u4e8e\u63d0\u5347\u6a21\u578b\u5728\u957f\u4e0a\u4e0b\u6587\u4efb\u52a1\u4e2d\u7684\u8868\u73b0\uff0c\u540c\u65f6\u4e0d\u4f1a\u524a\u5f31\u5176\u5904\u7406\u77ed\u4e0a\u4e0b\u6587\u4efb\u52a1\u7684\u80fd\u529b\u3002<\/p>\n\n\n\n<p><strong>\u8bad\u7ec3\u7b56\u7565\u7684\u6709\u6548\u6027<\/strong>\uff1a<strong>\u91c7\u7528\u7684\u6253\u5305\u548c\u6392\u5e8f\u6279\u5904\u7406\u7b56\u7565\u53ef\u5c06\u8bad\u7ec3\u901f\u5ea6\u63d0\u5347\u8d85\u8fc7 100%\uff0c\u4e14\u4e0d\u5f71\u54cd\u6a21\u578b\u6027\u80fd<\/strong>\u3002\u6b64\u5916\uff0c\u63d0\u51fa\u7684<strong>\u635f\u5931\u52a0\u6743\u6280\u672f\u8fd8\u80fd\u5c06\u957f\u6587\u672c\u4efb\u52a1\u7684\u6027\u80fd\u63d0\u5347 10%\u3002<\/strong><\/p>\n\n\n\n<h4>\u6570\u636e\u96c6\u6784\u5efa\uff1a<\/h4>\n\n\n\n<p>\u6784\u5efa\u4e86\u4e00\u4e2a\u5305\u542b10,000\u6761\u957f\u5ea6\u57288k-64k\u4e4b\u95f4\u7684\u957f\u6587\u6307\u4ee4\u8ddf\u968f\u6570\u636e\u96c6\uff0c\u8fd9\u4e9b\u6570\u636e\u6765\u81ea\u4e8e9\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u6e90\uff0c\u5305\u62ec\u5b66\u672f\u8bba\u6587\u3001\u4e66\u7c4d\u3001\u767e\u79d1\u5168\u4e66\u7b49\uff0c\u8986\u76d6\u4e86\u591a\u6837\u5316\u7684\u4efb\u52a1\u7c7b\u578b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"706\" height=\"445\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-77.png\" alt=\"\" class=\"wp-image-27948\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-77.png 706w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-77-300x189.png 300w\" sizes=\"(max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<h4>\u9ad8\u6548\u8bad\u7ec3\u65b9\u6cd5\uff1a<\/h4>\n\n\n\n<p>\u4e3a\u4e86\u786e\u4fdd\u6a21\u578b\u5728\u6709\u76d1\u7763\u5fae\u8c03\uff08SFT\uff09\u540e\u4f9d\u7136\u5177\u5907\u5904\u7406\u957f\u6587\u672c\u548c\u77ed\u6587\u672c\uff08\u5373\u901a\u7528\u80fd\u529b\uff09\u7684\u80fd\u529b\uff0c\u5c06\u957f\u6587\u672c\u6307\u4ee4\u6570\u636e\u4e0e\u901a\u7528\u6307\u4ee4\u6570\u636e\u96c6\u6df7\u5408\u7528\u4e8e\u8bad\u7ec3\u3002\u8fd9\u79cd\u8bad\u7ec3\u7b56\u7565\u4f7f\u5f97\u5927\u91cf\u901a\u7528\u77ed\u6587\u672c\u6570\u636e\u4e0e\u76f8\u5bf9\u8f83\u5c11\u7684\u957f\u6307\u4ee4\u6570\u636e\u7ed3\u5408\uff0c\u4ece\u800c\u5f62\u6210\u4e86\u4e00\u4e2a\u201c\u957f\u5c3e\u201d\u5f0f\u7684\u6570\u636e\u957f\u5ea6\u5206\u5e03\u3002\u63a2\u7d22\u4e86\u4e24\u79cd\u8bad\u7ec3\u65b9\u6cd5\uff1a<strong>packing<\/strong> \u548c <strong>sorted batching<\/strong>\u3002<\/p>\n\n\n\n<h5>Packing\uff08\u6253\u5305\uff09<\/h5>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"328\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-78-1024x328.png\" alt=\"\" class=\"wp-image-27952\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-78-1024x328.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-78-300x96.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-78-768x246.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-78.png 1407w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u8be5\u65b9\u6cd5\u901a\u8fc7\u5c06\u4e0d\u540c\u957f\u5ea6\u7684\u6570\u636e\u62fc\u63a5\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5927\u957f\u5ea6\uff0c\u751f\u6210\u7684\u6253\u5305\u6570\u636e\u6574\u4f53\u957f\u5ea6\u63a5\u8fd1\u6700\u5927\u9650\u503c\u3002\u8fd9\u4e9b\u6253\u5305\u540e\u7684\u6570\u636e\u518d\u8fdb\u884c\u6279\u5904\u7406\u5e76\u5728\u591a GPU \u4e0a\u5904\u7406\uff0c\u6709\u6548\u51cf\u5c11\u4e86\u6bcf\u4e2a\u6279\u6b21\u4e2d\u7684\u7a7a\u8f6c\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u4e3a\u9632\u6b62\u540c\u4e00 pack \u4e2d\u7684\u4e0d\u540c\u5e8f\u5217\u5728\u81ea\u6ce8\u610f\u529b\u8ba1\u7b97\u4e2d\u53d1\u751f\u201c\u4ea4\u53c9\u6c61\u67d3\u201d\uff0c\u6211\u4eec<strong>\u4f20\u5165\u4e86\u6bcf\u4e2a\u5e8f\u5217\u7684\u8d77\u59cb\u4e0e\u7ed3\u675f\u4f4d\u7f6e\u5217\u8868<\/strong>\uff0c\u5e76\u4f7f\u7528\u4e86 FlashAttention 2 \u4e2d\u7684 <code>flash_attn_varlen_func<\/code> \u8be5\u65b9\u6cd5\u652f\u6301\u9ad8\u6548\u7684\u5757\u5bf9\u89d2\u6ce8\u610f\u529b\u8ba1\u7b97\uff0c\u8ba1\u7b97\u91cf\u4e0e IO \u65f6\u95f4\u5747\u4f18\u4e8e\u4f20\u7edf\u7684\u4e8c\u7ef4\u6ce8\u610f\u529b\u63a9\u7801\u3002<\/p>\n\n\n\n<h5>Packing \u7b56\u7565\u5b58\u5728\u7684\u504f\u5dee<\/h5>\n\n\n\n<p>\u4e0d\u8fc7\u6211\u4eec\u6ce8\u610f\u5230\uff0cpacking \u4f1a\u5e26\u6765\u5bf9<strong>\u957f\u5e8f\u5217<\/strong>\u548c<strong>\u76ee\u6807 token \u8f83\u591a\u7684\u5e8f\u5217<\/strong>\u7684<strong>\u504f\u5411<\/strong>\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a<strong>\u4e0d\u540c\u7684\u6253\u5305\u7ec4\uff08pack\uff09\u5728\u6700\u7ec8\u635f\u5931\u8ba1\u7b97\u4e2d\u88ab\u8d4b\u4e88\u76f8\u540c\u6743\u91cd\uff0c\u800c\u6bcf\u4e2a\u6253\u5305\u7ec4\u4e2d\u5305\u542b\u7684\u5e8f\u5217\u6570\u91cf\u548c\u6bcf\u4e2a\u5e8f\u5217\u7684\u76ee\u6807 token \u6570\u91cf\u5374\u4e0d\u540c\u3002<\/strong><\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5728\u5bf9\u6bcf\u4e2a\u6279\u6b21\u6c42\u5e73\u5747\u635f\u5931\u65f6\uff0c\u5305\u542b\u5e8f\u5217\u8f83\u5c11\uff08\u901a\u5e38\u662f\u8f83\u957f\u5e8f\u5217\uff09\u6216\u76ee\u6807 token \u8f83\u591a\u7684 pack\uff0c\u4f1a\u5bf9\u6700\u7ec8\u635f\u5931\u4ea7\u751f\u66f4\u5927\u5f71\u54cd\u3002<\/p>\n\n\n\n<p>\u5f62\u5f0f\u4e0a\uff0c\u8bbe\u5c06 M \u4e2a\u5e8f\u5217\u6253\u5305\u6210 K \u4e2a pack\uff0c\u7b2c i \u4e2a pack \u5305\u542b\u7d22\u5f15\u533a\u95f4\u4e3a [Pi\u22121,Pi)\u7684\u5e8f\u5217\uff0c\u5176\u4e2d P0=1,PK=M+1\u3002\u8bbe Li \u4e3a\u7b2c i\u4e2a\u5e8f\u5217\u5728\u5176 Ni\u200b \u4e2a\u76ee\u6807 token \u4e0a\u7684\u603b\u635f\u5931\u3002\u5982\u679c\u6211\u4eec\u5e0c\u671b<strong>\u5bf9\u6bcf\u4e2a\u5e8f\u5217\u8d4b\u4e88\u76f8\u7b49\u7684\u6743\u91cd[ SFT\u4e2d\u7b97loss ]<\/strong>\uff0c\u5219\u635f\u5931\u5e94\u5f53\u4e3a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-79.png\" alt=\"\" class=\"wp-image-27959\" width=\"126\" height=\"59\"\/><\/figure><\/div>\n\n\n\n<p>\u800c\u5728 packing \u60c5\u51b5\u4e0b\u8ba1\u7b97\u5f97\u5230\u7684\u635f\u5931\u4e3a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-80.png\" alt=\"\" class=\"wp-image-27961\" width=\"303\" height=\"56\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-80.png 603w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-80-300x56.png 300w\" sizes=\"(max-width: 303px) 100vw, 303px\" \/><\/figure><\/div>\n\n\n\n<p>(3)\u4e0e\u516c\u5f0f (2) \u76f8\u6bd4\uff0c\u5728 packing \u60c5\u51b5\u4e0b\uff0c\u76f8\u5f53\u4e8e\u4e3a\u7b2c j\u4e2a\u5e8f\u5217\u5206\u914d\u4e86\u4e00\u4e2a\u6743\u91cd\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-82.png\" alt=\"\" class=\"wp-image-27965\" width=\"104\" height=\"58\"\/><\/figure><\/div>\n\n\n\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u635f\u5931\u66f4\u504f\u5411\u4e8e<strong>\u76ee\u6807 token \u6570\u8f83\u591a\u7684\u5e8f\u5217<\/strong>\uff0c\u4ee5\u53ca\u4f4d\u4e8e<strong>pack \u8f83\u5c0f\u7684\u7ec4<\/strong>\u4e2d\u7684\u5e8f\u5217\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u79cd\u4e0d\u516c\u5e73\uff0c\u6211\u4eec\u63d0\u51fa\u5bf9\u7b2c i \u4e2a\u5e8f\u5217\u7684\u635f\u5931\u8fdb\u884c\u7f29\u653e\uff0c\u7f29\u653e\u56e0\u5b50\u4e3a\uff1a<em>K\/(N<sub>i<\/sub>M)<\/em>\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a pack \u4e2d\u7f29\u653e\u540e\u7684\u635f\u5931\u6c42\u548c\uff0c\u8fd9\u6837\u5f97\u5230\u7684\u603b\u635f\u5931\u5c06\u4e0e\u516c\u5f0f (2)\uff08\u5373\u5e73\u5747\u6bcf\u4e2a\u5e8f\u5217\u635f\u5931\uff09\u4fdd\u6301\u4e00\u81f4\uff0c\u4ece\u800c\u6d88\u9664\u4e86\u4e0d\u540c\u5e8f\u5217\u5728\u635f\u5931\u8ba1\u7b97\u4e2d\u6240\u53d7\u5230\u7684\u504f\u501a\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-81.png\" alt=\"\" class=\"wp-image-27963\" width=\"324\" height=\"71\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-81.png 666w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/07\/image-81-300x67.png 300w\" sizes=\"(max-width: 324px) 100vw, 324px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u635f\u5931\u52a0\u6743\u7b56\u7565<\/strong>\u5728\u4e0b\u6e38\u4efb\u52a1\u4e2d\u5e26\u6765\u4e86\u7ea6 <strong>10% \u7684\u6027\u80fd\u63d0\u5347<\/strong>\u3002<\/p>\n\n\n\n<h5>Sorted Batching\uff08\u6392\u5e8f\u6279\u5904\u7406\uff09<\/h5>\n\n\n\n<p>\u8fd8\u63d0\u51fa\u4e86\u4e00\u79cd\u9ad8\u6548\u7684 <strong>\u6392\u5e8f\u6279\u5904\u7406\u7b56\u7565<\/strong>\u3002\u4e3a\u786e\u4fdd\u6bcf\u4e2a batch \u4e2d\u7684\u5e8f\u5217\u957f\u5ea6\u76f8\u8fd1\uff0c\u6211\u4eec\u5148\u6309\u7167\u5e8f\u5217\u957f\u5ea6\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u5728\u6bcf\u8f6e\u8bad\u7ec3\u4e2d\u4ece\u4e2d<strong>\u968f\u673a\u9009\u53d6\u4e00\u6bb5\u8fde\u7eed\u7684\u6570\u636e\u7ec4\u6210\u4e00\u4e2a batch<\/strong>\uff0c\u4e14\u4e0d\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\uff0c\u8be5\u65b9\u6cd5\u4e0d\u53ef\u907f\u514d\u5730\u4f1a\u5f15\u5165 <strong>\u6279\u6b21\u95f4\u6570\u636e\u5206\u5e03\u7684\u4e0d\u5747\u8861<\/strong>\uff1a<strong>\u67d0\u4e9b batch \u53ef\u80fd\u5168\u90e8\u7531\u957f\u5e8f\u5217\u7ec4\u6210\uff0c\u53e6\u4e00\u4e9b\u5219\u5168\u662f\u77ed\u5e8f\u5217\u3002\u8fd9\u79cd\u504f\u5dee\u53ef\u80fd\u5bf9 SGD\uff08\u968f\u673a\u68af\u5ea6\u4e0b\u964d\uff09\u4f18\u5316\u8fc7\u7a0b\u9020\u6210\u4e25\u91cd\u5f71\u54cd\u3002<\/strong><\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u5c3d\u7ba1\u5982\u6b64\uff0c\u6211\u4eec\u5728\u5b9e\u9a8c\u4e2d\u53d1\u73b0\uff0c\u6392\u5e8f\u6279\u5904\u7406\u663e\u8457\u52a0\u5feb\u4e86\u8bad\u7ec3\u901f\u5ea6\uff0c\u4e14<strong>\u51e0\u4e4e\u4e0d\u4f1a\u5bf9\u6a21\u578b\u6027\u80fd\u4ea7\u751f\u8d1f\u9762\u5f71\u54cd<\/strong>\u3002\u8fd9\u53ef\u80fd<strong>\u5f97\u76ca\u4e8e\u6211\u4eec\u4f7f\u7528\u4e86\u8f83\u5927\u7684\u68af\u5ea6\u7d2f\u79ef\u6b65\u6570\uff08gradient accumulation steps\uff09\u548c\u4f18\u5316\u5668\u672c\u8eab\u8f83\u5f3a\u7684\u9002\u5e94\u80fd\u529b\u3002<\/strong><\/p>\n\n\n\n<h4>\u8bad\u7ec3\u65b9\u6cd5\u7ec6\u8282<\/h4>\n\n\n\n<p>\u8fd9\u91cc\u4ecb\u7ecd packing \u7b56\u7565\u4e0e\u635f\u5931\u52a0\u6743\u7684\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h5>Packing \u7b56\u7565\u5b9e\u73b0<\/h5>\n\n\n\n<p>\u5728\u6253\u5305\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u6570\u636e\u6279\u6b21\u4f1a\u4f20\u5165\u4e00\u4e2a\u7279\u6b8a\u7684\u4e00\u7ef4\u6ce8\u610f\u529b\u63a9\u7801\u3002\u5728\u8be5\u63a9\u7801\u4e2d\uff0c\u7b2c i\u4e2a\u5143\u7d20\u8868\u793a\u7b2c i \u4e2a\u5e8f\u5217\u5728\u8be5\u6279\u6b21\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002\u63a9\u7801\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e3a 0\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7b49\u4e8e <code>batch_size \u00d7 seq_len<\/code>\u3002<\/p>\n\n\n\n<p>\u5728\u6ce8\u610f\u529b\u8ba1\u7b97\u65f6\uff0c\u6211\u4eec<strong>\u4f7f\u7528 FlashAttention 2 \u63d0\u4f9b\u7684 <code>flash_attn_varlen_func<\/code> \u51fd\u6570\uff0c\u5e76\u5c06\u8be5\u63a9\u7801\u4f20\u5165\u5176\u53c2\u6570 <code>cu_seqlens_q<\/code> \u548c <code>cu_seqlens_k<\/code>\u3002\u8be5\u51fd\u6570\u4f1a\u6839\u636e\u63a9\u7801\u4e2d\u76f8\u90bb\u5143\u7d20\u8868\u793a\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\uff0c\u5728\u6bcf\u4e2a\u5e8f\u5217\u5185\u90e8\u8fdb\u884c\u6ce8\u610f\u529b\u8ba1\u7b97<\/strong>\u3002<strong>\u56e0\u6b64\uff0c\u6bcf\u4e2a\u5e8f\u5217\u7684 Query \u53ea\u80fd\u4e0e\u81ea\u8eab\u7684 Key \u8fdb\u884c\u6ce8\u610f\u529b\u64cd\u4f5c\uff0c\u5b9e\u73b0\u4e86\u201c\u5e8f\u5217\u5185\u72ec\u7acb\u6ce8\u610f\u201d\u3002<\/strong><\/p>\n\n\n\n<h5>\u635f\u5931\u52a0\u6743\u7b56\u7565\u5b9e\u73b0<\/h5>\n\n\n\n<p>\u5728\u5b9e\u73b0\u635f\u5931\u52a0\u6743\u7b56\u7565\u65f6\uff0c\u9996\u5148\u5bf9\u8bad\u7ec3\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff1a<strong>\u4e3a\u6bcf\u4e2a pack \u4e2d\u7684\u5e8f\u5217\u751f\u6210\u4e00\u4e2a\u52a0\u6743\u7684\u4e00\u7ef4\u63a9\u7801\u3002\u8be5\u63a9\u7801\u4e2d\uff0c\u5bf9\u5e94\u76ee\u6807 token \u7684\u4f4d\u7f6e\u6743\u91cd\u4e3a 1\/N<\/strong>\uff08\u5176\u4e2d N \u662f\u5f53\u524d\u5e8f\u5217\u7684\u76ee\u6807 token \u6570\uff09\uff0c<strong>\u5176\u4ed6\u4f4d\u7f6e\u4e3a 0\u3002<\/strong><\/p>\n\n\n\n<p><strong>\u8bad\u7ec3\u65f6\uff0c\u6839\u636e\u5f53\u524d\u914d\u7f6e\u52a8\u6001\u8bbe\u7f6e M \u548c K\uff0c\u8868\u793a\u5373\u5f53\u524d\u6279\u6b21\u4e2d\u5e8f\u5217\u7684\u6570\u91cf\u548c pack \u7684\u6570\u91cf\u3002\u7136\u540e\uff0c\u635f\u5931\u8ba1\u7b97\u65b9\u6cd5\u4e3a\uff1a\u5bf9\u6bcf\u4e2a token \u7684\u4ea4\u53c9\u71b5\u635f\u5931\u4e58\u4ee5\u6bd4\u4f8b\u7cfb\u6570 K\/(MN)\uff0c\u518d\u6c42\u548c\u5f97\u5230\u6700\u7ec8\u635f\u5931\u503c\u3002<\/strong><\/p>\n\n\n\n<h4>Packing \u52a0\u6743loss\u4ee3\u7801\u5b9e\u73b0\uff1a<\/h4>\n\n\n\n<p>SFT\u4e2d\u7b97loss\u901a\u5e38\u6765\u8bb2\u90fd\u662f<strong>\u6837\u672c\u5185\u4f5ctoken-level mean\uff0c\u6837\u672c\u95f4\u4f5csequence-level mean<\/strong>\uff0c\u4e5f\u5c31\u662f\u7b49\u5f0f\uff082\uff09\u7684\u8ba1\u7b97\u65b9\u5f0f\u3002\u5982\u679c\u4e0d\u540c\u6837\u672c\u95f4\u4f5ctoken-level mean\uff0c\u5219\u4f1a\u4f7ftarget token\u6570\u91cf\u591a\u7684\u6837\u672c\u66f4\u53d7\u91cd\u89c6\uff08\u76f8\u5f53\u4e8e\u88abupsample\uff09\uff0c\u4ece\u800c\u5f15\u5165\u4e0d\u540c\u6837\u672c\u95f4\u7684\u4e0d\u5e73\u8861\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### Support loss weighting for packing ###\r\n        loss = None\r\n        if labels is not None:\r\n            lm_logits = lm_logits.to(torch.float32)\r\n            # Shift so that tokens &lt; n predict n\r\n            shift_logits = lm_logits&#91;..., :-1, :].contiguous()\r\n            if isinstance(labels, tuple) or isinstance(labels, list):\r\n                labels, weights = labels\r\n            shift_labels = labels&#91;..., 1:].contiguous()\r\n            if self.pack_loss:\r\n                shift_weights = weights&#91;..., 1:].contiguous()\r\n                loss_fct = CrossEntropyLoss(ignore_index=-100, reduction='none')\r\n                loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))\r\n                loss = (loss * shift_weights).sum()\r\n            else:\r\n                loss_fct = CrossEntropyLoss(ignore_index=-100)\r\n                loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))\r\n\r\n            lm_logits = lm_logits.to(hidden_states.dtype)\r\n            loss = loss.to(hidden_states.dtype)\r\n        ### -------------------------------------- ###<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8bba\u6587\uff1aLongAlign: A Recipe for Long Context Alignment of La &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2025\/07\/27\/llm-packed-samples-sorted-batching\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u9ad8\u6548 LLM \u8bad\u7ec3\u65b9\u6cd5\uff1aPacked samples\u548c sorted batching<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[42,40,4,9,38,34],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/27925"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=27925"}],"version-history":[{"count":54,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/27925\/revisions"}],"predecessor-version":[{"id":28147,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/27925\/revisions\/28147"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=27925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=27925"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=27925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}