{"id":35035,"date":"2024-12-17T09:59:51","date_gmt":"2024-12-17T01:59:51","guid":{"rendered":"https:\/\/17aitech.com\/?p=35035"},"modified":"2024-12-17T09:59:51","modified_gmt":"2024-12-17T01:59:51","slug":"%e6%96%b0pytorch-api%ef%bc%9a%e5%87%a0%e8%a1%8c%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0%e4%b8%8d%e5%90%8c%e6%b3%a8%e6%84%8f%e5%8a%9b%e5%8f%98%e4%bd%93%ef%bc%8c%e5%85%bc%e5%85%b7flashattention%e6%80%a7","status":"publish","type":"post","link":"https:\/\/17aitech.com\/?p=35035","title":{"rendered":"\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027"},"content":{"rendered":"<p>\u6587\u7ae0\u6765\u6e90\u4e8e\u4e92\u8054\u7f51:<a href=\"https:\/\/www.jiqizhixin.com\/articles\/2024-08-11-3\" target=\"_blank\">\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027<\/a><\/p>\n<blockquote data-author-name=\"\" data-content-utf8-length=\"26\" data-source-title=\"\" data-type=\"2\" data-url=\"\">\n<section>\n<p>\u7528 FlexAttention \u5c1d\u8bd5\u4e00\u79cd\u65b0\u7684\u6ce8\u610f\u529b\u6a21\u5f0f\u3002<\/p>\n<\/section>\n<\/blockquote>\n<section><\/section>\n<section>\u7406\u8bba\u4e0a\uff0c\u6ce8\u610f\u529b\u673a\u5236\u5c31\u662f\u4f60\u6240\u9700\u8981\u7684\u4e00\u5207\u3002\u7136\u800c\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4f18\u5316\u50cf FlashAttention \u8fd9\u6837\u7684\u6ce8\u610f\u529b\u673a\u5236\u7684\u5b9e\u73b0\u3002<\/section>\n<section><\/section>\n<section>\u5c3d\u7ba1\u8fd9\u4e9b\u878d\u5408\u7684\u6ce8\u610f\u529b\u673a\u5236\u5927\u5927\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u4e14\u652f\u6301\u957f\u4e0a\u4e0b\u6587\uff0c\u4f46\u8fd9\u79cd\u6548\u7387\u7684\u63d0\u5347\u4e5f\u4f34\u968f\u7740\u7075\u6d3b\u6027\u7684\u4e27\u5931\u3002\u5bf9\u4e8e\u673a\u5668\u5b66\u4e60\u7814\u7a76\u4eba\u5458\u6765\u8bf4\uff0c\u8fd9\u5c31\u50cf\u662f\u4e00\u79cd\u300c\u8f6f\u4ef6\u5f69\u7968\u300d\u2014\u2014 \u5982\u679c\u4f60\u7684\u6ce8\u610f\u529b\u53d8\u4f53\u4e0d\u9002\u5408\u73b0\u6709\u7684\u4f18\u5316\u5185\u6838\uff0c\u4f60\u5c06\u9762\u4e34\u8fd0\u884c\u7f13\u6162\u548c CUDA \u5185\u5b58\u4e0d\u8db3\u7684\u56f0\u5883\u3002\u00a0<\/section>\n<section><\/section>\n<section>\u4e00\u4e9b\u6ce8\u610f\u529b\u53d8\u4f53\u5305\u62ec\u56e0\u679c\u6ce8\u610f\u529b\u3001\u76f8\u5bf9\u4f4d\u7f6e\u5d4c\u5165\u3001Alibi\u3001\u6ed1\u52a8\u7a97\u53e3\u6ce8\u610f\u529b\u3001PrefixLM\u3001\u6587\u6863\u63a9\u7801\u3001\u4e0d\u89c4\u5219\u5f20\u91cf\u3001PagedAttention \u7b49\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u4eba\u4eec\u901a\u5e38\u5e0c\u671b\u5c06\u8fd9\u4e9b\u53d8\u4f53\u7ec4\u5408\u5728\u4e00\u8d77\uff01\u6bd4\u5982\u6ed1\u52a8\u7a97\u53e3\u6ce8\u610f\u529b + \u6587\u6863\u63a9\u7801 + \u56e0\u679c\u6ce8\u610f\u529b + \u4e0a\u4e0b\u6587\u5e76\u884c\uff0c\u53c8\u6bd4\u5982 PagedAttention + \u6ed1\u52a8\u7a97\u53e3\u7684\u7ec4\u5408\u3002<\/section>\n<section><\/section>\n<section>\u4e0b\u56fe\u5de6\u4fa7\u4ee3\u8868\u4e86\u5f53\u4eca\u7684\u73b0\u72b6 \u2014\u2014 \u4e00\u4e9b\u63a9\u7801 + \u504f\u7f6e + \u8bbe\u7f6e\u7684\u7ec4\u5408\u5df2\u7ecf\u6709\u73b0\u6210\u7684\u5185\u6838\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u5404\u79cd\u9009\u9879\u7684\u6dfb\u52a0\u4f1a\u5bfc\u81f4\u8bbe\u7f6e\u5448\u6307\u6570\u7ea7\u589e\u957f\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4f1a\u652f\u6301\u65b0\u7684\u6ce8\u610f\u529b\u53d8\u4f53\u3002\u00a0<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png\"><\/a><\/section>\n<section>\u4e3a\u4e86\u5f7b\u5e95\u5730\u89e3\u51b3\u8fd9\u4e2a\u8d85\u7acb\u65b9\u4f53\u95ee\u9898\uff0cPyTorch \u56e2\u961f\u5f15\u5165\u4e86 FlexAttention\uff0c\u4e00\u4e2a\u65b0\u7684 PyTorch API\u3002<\/section>\n<section><\/section>\n<ol>\n<li>\n<section>FlexAttention \u662f\u4e00\u4e2a\u7075\u6d3b\u7684 API\uff0c\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u51e0\u884c\u60ef\u7528\u7684 PyTorch \u4ee3\u7801\u5c31\u80fd\u5b9e\u73b0\u591a\u4e2a\u6ce8\u610f\u529b\u53d8\u4f53\u3002<\/section>\n<\/li>\n<li>\n<section>\u56e2\u961f\u4eba\u5458\u901a\u8fc7 torch.compile \u5c06\u5176\u964d\u4f4e\u5230\u4e00\u4e2a\u878d\u5408\u7684 FlashAttention \u5185\u6838\u4e2d \uff0c\u751f\u6210\u4e86\u4e00\u4e2a\u4e0d\u4f1a\u5360\u7528\u989d\u5916\u5185\u5b58\u4e14\u6027\u80fd\u53ef\u4e0e\u624b\u5199\u5185\u6838\u76f8\u5ab2\u7f8e\u7684 FlashAttention \u5185\u6838\u3002<\/section>\n<\/li>\n<li>\n<section>\u5229\u7528 PyTorch \u7684\u81ea\u52a8\u6c42\u5bfc\u673a\u5236\u81ea\u52a8\u751f\u6210\u53cd\u5411\u4f20\u64ad\u3002<\/section>\n<\/li>\n<li>\n<section>\u6700\u540e\uff0cPyTorch \u56e2\u961f\u8fd8\u53ef\u4ee5\u5229\u7528\u6ce8\u610f\u529b\u63a9\u7801\u4e2d\u7684\u7a00\u758f\u6027\uff0c\u4ece\u800c\u663e\u8457\u6539\u5584\u6807\u51c6\u6ce8\u610f\u529b\u5b9e\u73b0\u3002<\/section>\n<\/li>\n<\/ol>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-62232c9254dcfb85ac7fffc2bceb96aa.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-62232c9254dcfb85ac7fffc2bceb96aa.png\"><\/a><\/section>\n<section>FlashAttention 1-3 \u7248\u672c\u7684\u53c2\u4e0e\u8005 Tri Dao \u5bf9\u8fd9\u9879\u7814\u7a76\u8fdb\u884c\u4e86\u8f6c\u53d1\u5e76\u8bc4\u8bba\uff1a\u8fd9\u9879\u7814\u7a76\u4f7f\u5f97\u5f88\u591a\u6280\u672f\u90fd\u878d\u5408\u5728\u4e00\u8d77\u4e86\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-f090ce2bc98d6b3e1f64d673839df22d.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-f090ce2bc98d6b3e1f64d673839df22d.png\"><\/a><\/section>\n<section><strong>FlexAttention<\/strong><\/section>\n<section><\/section>\n<section>\u7ecf\u5178\u7684\u6ce8\u610f\u529b\u65b9\u7a0b\u5f0f\u5982\u4e0b\uff1a<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-04af646e432c2ba3d2e1235523472d59.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-04af646e432c2ba3d2e1235523472d59.png\"><\/a><\/section>\n<section>\u4ee3\u7801\u5f62\u5f0f\uff1a<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-78df1b440f1908284866944a0c893f3c.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-78df1b440f1908284866944a0c893f3c.png\"><\/a><\/section>\n<section>FlexAttention \u5f62\u5f0f\u5982\u4e0b\uff0c\u5176\u901a\u8fc7\u63a5\u53d7\u7528\u6237\u5b9a\u4e49\u7684\u51fd\u6570 score_mod \u6765\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-d16e6ef5955a1050a3517c00d7778a20.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-d16e6ef5955a1050a3517c00d7778a20.png\"><\/a>\u4ee3\u7801\u5f62\u5f0f\uff1a<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-3b8e077f02df2d7a5f7b00cd65fca7ed.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-3b8e077f02df2d7a5f7b00cd65fca7ed.png\"><\/a><\/section>\n<section>\u6b64\u51fd\u6570\u5141\u8bb8\u7528\u6237\u5728 softmax \u4e4b\u524d\u4fee\u6539\u6ce8\u610f\u529b\u5206\u6570\u3002\u7814\u7a76\u4eba\u5458\u53d1\u73b0\uff0c\u8be5\u51fd\u6570\u6700\u7ec8\u8db3\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u7528\u6237\u5bf9\u6ce8\u610f\u529b\u53d8\u4f53\u7684\u9700\u6c42\u3002<\/section>\n<section><\/section>\n<section>\u5177\u4f53\u800c\u8a00\uff0cscore_mod \u5982\u4e0b\uff1a<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-61b9f25ce752ecf10a6e2c65ec89d9e8.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-61b9f25ce752ecf10a6e2c65ec89d9e8.png\"><\/a><\/section>\n<section>\u8981\u5e94\u7528\u6b64\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5176\u5b9e\u73b0\u4e3a\uff1a<\/section>\n<section>\n<pre data-lang=\"cs\"><code>for b in range (batch_size):<\/code>\r\n<code>    for h in range (num_heads):<\/code>\r\n<code>        for q_idx in range (sequence_length):<\/code>\r\n<code>            for kv_idx in range (sequence_length):<\/code>\r\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 modified_scores [b, h, q_idx, kv_idx] = score_mod (scores [b, h, q_idx, kv_idx], b, h, q_idx, kv_idx)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>\u6700\u7ec8\u7684 API \u5177\u6709\u4ee4\u4eba\u60ca\u8bb6\u7684\u8868\u8fbe\u80fd\u529b\u3002<\/section>\n<section><\/section>\n<section><strong>Score Mod \u793a\u4f8b<\/strong><\/section>\n<section><\/section>\n<section><strong>\u5168\u6ce8\u610f\u529b<\/strong><\/section>\n<section><\/section>\n<section>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cscore_mod \u65e0\u64cd\u4f5c\uff0c\u5b83\u63a5\u53d7\u5206\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u539f\u6837\u8fd4\u56de\u5b83\u4eec\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-4427de43c5efcc1bb5640bdc7a75396c.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-4427de43c5efcc1bb5640bdc7a75396c.png\"><\/a><\/section>\n<section>\u7136\u540e\u7aef\u5230\u7aef\u7684\u4f7f\u7528\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-baef9b850b0d35c5a4f4de8637ffdf73.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-baef9b850b0d35c5a4f4de8637ffdf73.png\"><\/a><\/section>\n<section><strong>\u76f8\u5bf9\u4f4d\u7f6e\u7f16\u7801<\/strong><\/section>\n<section><\/section>\n<section>\u4e00\u79cd\u5e38\u89c1\u7684\u6ce8\u610f\u529b\u53d8\u4f53\u662f\u76f8\u5bf9\u4f4d\u7f6e\u7f16\u7801\u3002\u76f8\u5bf9\u4f4d\u7f6e\u7f16\u7801\u4e0d\u662f\u5bf9\u67e5\u8be2\u548c\u952e\u4e2d\u7684\u7edd\u5bf9\u8ddd\u79bb\u8fdb\u884c\u7f16\u7801\uff0c\u800c\u662f\u6839\u636e\u67e5\u8be2\u548c\u952e\u4e4b\u95f4\u7684\u8ddd\u79bb\u8c03\u6574\u5206\u6570\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-93523c65fdee56ab719d4c9ead67da24.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-93523c65fdee56ab719d4c9ead67da24.png\"><\/a><\/section>\n<section>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0e\u5178\u578b\u5b9e\u73b0\u4e0d\u540c\uff0c\u8fd9\u4e0d\u9700\u8981\u5177\u4f53\u5316\u00a0SxS \u5f20\u91cf\u3002\u76f8\u53cd\uff0cFlexAttention \u4f1a\u5728\u5185\u6838\u4e2d\u52a8\u6001\u8ba1\u7b97\u504f\u5dee\u503c\uff0c\u4ece\u800c\u663e\u8457\u63d0\u9ad8\u5185\u5b58\u548c\u6027\u80fd\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-50b5be9d7df1de1dcddde01082e14d30.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-50b5be9d7df1de1dcddde01082e14d30.png\"><\/a><\/section>\n<section><strong>Soft-capping<\/strong><\/section>\n<section><\/section>\n<section>Soft-capping \u662f Gemma 2 \u548c Grok-1 \u4f7f\u7528\u7684\u4e00\u79cd\u6280\u672f\uff0c\u5728 FlexAttention \u4e2d\uff0c\u5b83\u7684\u5f62\u5f0f\u662f\u8fd9\u6837\u7684\uff1a<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-a4ef1073647a716ca84cc784262dab48.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-a4ef1073647a716ca84cc784262dab48.png\"><\/a><\/section>\n<section><strong>Causal Mask<\/strong><\/section>\n<section><\/section>\n<section>\u5c3d\u7ba1\u53cc\u5411\u6ce8\u610f\u529b\u5f88\u7b80\u5355\uff0c\u4f46\u5728\u8bba\u6587\u300aAttention is All You Need\u300b\uff0c\u4ee5\u53ca\u5176\u4ed6\u7684 LLM \u4e2d\uff0c\u5b83\u4eec\u7684\u8bbe\u7f6e\u90fd\u662f\u4ec5\u89e3\u7801\u5668\u7684\u6ce8\u610f\u529b\uff0c\u5176\u4e2d\u6bcf\u4e2a token \u53ea\u80fd\u5173\u6ce8\u5b83\u4e4b\u524d\u7684 token\u3002\u5982\u679c\u7528\u6237\u4f7f\u7528 score_mod API \uff0c\u53ef\u4ee5\u5c06\u5176\u8868\u793a\u4e3a\uff1a<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-a1350d01f41c6f78dbd77fc941cdc911.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-a1350d01f41c6f78dbd77fc941cdc911.png\"><\/a><\/section>\n<section><strong>Sliding Window + Causal<\/strong><\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-ce9f4e4e464225dcae2db0c7977717ff.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-ce9f4e4e464225dcae2db0c7977717ff.png\"><\/a><\/section>\n<section><em><sup>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u56fe\u6e90\uff1ahttps:\/\/arxiv.org\/abs\/2310.06825<\/sup><\/em><\/section>\n<section><\/section>\n<section>Mistral \u4e00\u76f4\u5728\u63a8\u5e7f\u6ed1\u52a8\u7a97\u53e3\u6ce8\u610f\u529b\uff08\u4e5f\u79f0\u4e3a\u5c40\u90e8\u6ce8\u610f\u529b\uff09\uff0c\u5b83\u5141\u8bb8\u67e5\u8be2 token \u4ec5\u5173\u6ce8\u6700\u8fd1\u7684 1024 \u4e2a token\uff0c\u901a\u5e38\u4e0e\u56e0\u679c\u6ce8\u610f\u529b\u4e00\u8d77\u4f7f\u7528\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-4c7ad85598ea8ff2422ae571cca332b5.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-4c7ad85598ea8ff2422ae571cca332b5.png\"><\/a><\/section>\n<section>\u7814\u7a76\u8005\u5bf9\u5e26\u6709\u6ed1\u52a8\u7a97\u53e3\u63a9\u7801\u7684 F.scaled_dot_product_attention \u4ee5\u53ca\u5e26\u6709\u56e0\u679c\u63a9\u7801\u7684 FA2 \u8fdb\u884c\u57fa\u51c6\u6d4b\u8bd5\u3002\u7ed3\u679c\u8868\u660e\uff0cFlexAttention \u4e0d\u4ec5\u660e\u663e\u5feb\u4e8e F.scaled_dot_product_attention\uff0c\u4e5f\u660e\u663e\u5feb\u4e8e\u5e26\u6709\u56e0\u679c\u63a9\u7801\u7684 FA2\u3002<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-701ac4a61930ee6707170f99112de305.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-701ac4a61930ee6707170f99112de305.png\"><\/a><\/section>\n<section><strong>\u6027\u80fd<\/strong><\/section>\n<section><\/section>\n<section>\u603b\u4f53\u800c\u8a00\uff0cFlexAttention \u7684\u6027\u80fd\u51e0\u4e4e\u4e0e\u624b\u5199\u7684 Triton \u5185\u6838\u4e00\u6837\u597d\u3002\u7136\u800c\uff0c\u7531\u4e8e FlexAttention \u5177\u6709\u901a\u7528\u6027\uff0c\u56e0\u6b64\u4f1a\u906d\u53d7\u8f7b\u5fae\u7684\u6027\u80fd\u635f\u5931\u3002\u4f8b\u5982\uff0c\u7528\u6237\u5fc5\u987b\u627f\u53d7\u4e00\u4e9b\u989d\u5916\u7684\u5ef6\u8fdf\u3002<\/section>\n<section><\/section>\n<section>FlexAttention \u5728\u524d\u5411\u4f20\u64ad\u4e2d\u5b9e\u73b0\u4e86 FlashAttention2 \u6027\u80fd\u7684 90%\uff0c\u5728\u53cd\u5411\u4f20\u64ad\u4e2d\u5b9e\u73b0\u4e86 85%\u3002FlexAttention \u76ee\u524d\u6b63\u5728\u4f7f\u7528\u4e00\u79cd\u786e\u5b9a\u6027\u7b97\u6cd5\uff0c\u8be5\u7b97\u6cd5\u6bd4 FAv2 \u91cd\u65b0\u8ba1\u7b97\u4e86\u66f4\u591a\u7684\u4e2d\u95f4\u4f53\uff0c\u7814\u7a76\u8005\u8ba1\u5212\u6539\u8fdb FlexAttention \u7684\u53cd\u5411\u7b97\u6cd5\uff0c\u6765\u7f29\u5c0f\u8fd9\u4e00\u5dee\u8ddd\uff01<\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-cc28532c2c58d98ef317d8e3c294873d.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-cc28532c2c58d98ef317d8e3c294873d.png\"><\/a><\/section>\n<section><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-40e9ec77bfa710a6eb4e960bffb33ff6.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-40e9ec77bfa710a6eb4e960bffb33ff6.png\"><\/a><\/section>\n<section><em><sup>\u53c2\u8003\u94fe\u63a5\uff1ahttps:\/\/pytorch.org\/blog\/flexattention\/<\/sup><\/em><\/section>\n<p>\u6587\u7ae0\u6765\u6e90\u4e8e\u4e92\u8054\u7f51:<a href=\"https:\/\/www.jiqizhixin.com\/articles\/2024-08-11-3\" target=\"_blank\">\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6765\u6e90\u4e8e\u4e92\u8054\u7f51:\u65b0PyTorch AP [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[27],"tags":[55],"class_list":["post-35035","post","type-post","status-publish","format-standard","hentry","category-news","tag-55"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027 - \u4e00\u8d77AI\u6280\u672f<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/17aitech.com\/?p=35035\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/17aitech.com\/?p=35035\",\"url\":\"https:\/\/17aitech.com\/?p=35035\",\"name\":\"\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027 - \u4e00\u8d77AI\u6280\u672f\",\"isPartOf\":{\"@id\":\"https:\/\/17aitech.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/17aitech.com\/?p=35035#primaryimage\"},\"image\":{\"@id\":\"https:\/\/17aitech.com\/?p=35035#primaryimage\"},\"thumbnailUrl\":\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png\",\"datePublished\":\"2024-12-17T01:59:51+00:00\",\"author\":{\"@id\":\"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d\"},\"breadcrumb\":{\"@id\":\"https:\/\/17aitech.com\/?p=35035#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/17aitech.com\/?p=35035\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/17aitech.com\/?p=35035#primaryimage\",\"url\":\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png\",\"contentUrl\":\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/17aitech.com\/?p=35035#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/17aitech.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/17aitech.com\/#website\",\"url\":\"https:\/\/17aitech.com\/\",\"name\":\"\u4e00\u8d77AI\u6280\u672f\",\"description\":\"\u8ba9AI\u77e5\u8bc6\u89e6\u624b\u53ef\u53ca\",\"alternateName\":\"\u4e00\u8d77AI\u6280\u672f\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/17aitech.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d\",\"name\":\"AI\u5c0f\u52a9\u624b\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/17aitech.com\/#\/schema\/person\/image\/\",\"url\":\"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png\",\"contentUrl\":\"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png\",\"caption\":\"AI\u5c0f\u52a9\u624b\"},\"description\":\"\u8fd9\u4e2a\u4eba\u5f88\u61d2\uff0c\u4ec0\u4e48\u90fd\u6ca1\u6709\u7559\u4e0b\uff5e\",\"url\":\"https:\/\/17aitech.com\/?page_id=33738&user=3\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027 - \u4e00\u8d77AI\u6280\u672f","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/17aitech.com\/?p=35035","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/17aitech.com\/?p=35035","url":"https:\/\/17aitech.com\/?p=35035","name":"\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027 - \u4e00\u8d77AI\u6280\u672f","isPartOf":{"@id":"https:\/\/17aitech.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/17aitech.com\/?p=35035#primaryimage"},"image":{"@id":"https:\/\/17aitech.com\/?p=35035#primaryimage"},"thumbnailUrl":"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png","datePublished":"2024-12-17T01:59:51+00:00","author":{"@id":"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d"},"breadcrumb":{"@id":"https:\/\/17aitech.com\/?p=35035#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/17aitech.com\/?p=35035"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/17aitech.com\/?p=35035#primaryimage","url":"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png","contentUrl":"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/08\/frc-0fe35a3170ee0c0f82c7ae22868e3fcd.png"},{"@type":"BreadcrumbList","@id":"https:\/\/17aitech.com\/?p=35035#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/17aitech.com\/"},{"@type":"ListItem","position":2,"name":"\u65b0PyTorch API\uff1a\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u4e0d\u540c\u6ce8\u610f\u529b\u53d8\u4f53\uff0c\u517c\u5177FlashAttention\u6027\u80fd\u548cPyTorch\u7075\u6d3b\u6027"}]},{"@type":"WebSite","@id":"https:\/\/17aitech.com\/#website","url":"https:\/\/17aitech.com\/","name":"\u4e00\u8d77AI\u6280\u672f","description":"\u8ba9AI\u77e5\u8bc6\u89e6\u624b\u53ef\u53ca","alternateName":"\u4e00\u8d77AI\u6280\u672f","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/17aitech.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d","name":"AI\u5c0f\u52a9\u624b","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/17aitech.com\/#\/schema\/person\/image\/","url":"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png","contentUrl":"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png","caption":"AI\u5c0f\u52a9\u624b"},"description":"\u8fd9\u4e2a\u4eba\u5f88\u61d2\uff0c\u4ec0\u4e48\u90fd\u6ca1\u6709\u7559\u4e0b\uff5e","url":"https:\/\/17aitech.com\/?page_id=33738&user=3"}]}},"_links":{"self":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/posts\/35035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35035"}],"version-history":[{"count":0,"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/posts\/35035\/revisions"}],"wp:attachment":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}