flash-attn-npu 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of flash-attn-npu might be problematic. Click here for more details.

Files changed (281) hide show
  1. flash_attn_npu-0.1.0/AUTHORS +1 -0
  2. flash_attn_npu-0.1.0/LICENSE +29 -0
  3. flash_attn_npu-0.1.0/MANIFEST.in +7 -0
  4. flash_attn_npu-0.1.0/PKG-INFO +308 -0
  5. flash_attn_npu-0.1.0/README.md +281 -0
  6. flash_attn_npu-0.1.0/csrc/catlass/.clang-format +57 -0
  7. flash_attn_npu-0.1.0/csrc/catlass/.git +1 -0
  8. flash_attn_npu-0.1.0/csrc/catlass/.gitcode/ISSUE_TEMPLATE/bug-report.yml +47 -0
  9. flash_attn_npu-0.1.0/csrc/catlass/.gitcode/ISSUE_TEMPLATE/documentation.yml +28 -0
  10. flash_attn_npu-0.1.0/csrc/catlass/.gitcode/ISSUE_TEMPLATE/feature-request.yml +33 -0
  11. flash_attn_npu-0.1.0/csrc/catlass/.gitcode/ISSUE_TEMPLATE/question.yml +22 -0
  12. flash_attn_npu-0.1.0/csrc/catlass/.gitcode/PULL_REQUEST_TEMPLATE.zh-CN.md +22 -0
  13. flash_attn_npu-0.1.0/csrc/catlass/.gitignore +49 -0
  14. flash_attn_npu-0.1.0/csrc/catlass/CHANGELOG.md +123 -0
  15. flash_attn_npu-0.1.0/csrc/catlass/CMakeLists.txt +53 -0
  16. flash_attn_npu-0.1.0/csrc/catlass/CONTRIBUTING.md +55 -0
  17. flash_attn_npu-0.1.0/csrc/catlass/LICENSE +38 -0
  18. flash_attn_npu-0.1.0/csrc/catlass/OAT.xml +62 -0
  19. flash_attn_npu-0.1.0/csrc/catlass/OWNERS +25 -0
  20. flash_attn_npu-0.1.0/csrc/catlass/README.md +176 -0
  21. flash_attn_npu-0.1.0/csrc/catlass/SECURITYNOTE.md +77 -0
  22. flash_attn_npu-0.1.0/csrc/catlass/Third_Party_Open_Source_Software_Notice +0 -0
  23. flash_attn_npu-0.1.0/csrc/catlass/cmake/funcs.cmake +88 -0
  24. flash_attn_npu-0.1.0/csrc/catlass/cmake/modules/CMakeASCENDCompiler.cmake.in +22 -0
  25. flash_attn_npu-0.1.0/csrc/catlass/cmake/modules/CMakeASCENDInformation.cmake +91 -0
  26. flash_attn_npu-0.1.0/csrc/catlass/cmake/modules/CMakeDetermineASCENDCompiler.cmake +71 -0
  27. flash_attn_npu-0.1.0/csrc/catlass/cmake/modules/CMakeTestASCENDCompiler.cmake +11 -0
  28. flash_attn_npu-0.1.0/csrc/catlass/docs/advanced_collections.md +8 -0
  29. flash_attn_npu-0.1.0/csrc/catlass/docs/contents/advanced/api.md +236 -0
  30. flash_attn_npu-0.1.0/csrc/catlass/docs/contents/advanced/catlass_optimize_guidance.md +162 -0
  31. flash_attn_npu-0.1.0/csrc/catlass/docs/contents/advanced/dispatch_policies.md +110 -0
  32. flash_attn_npu-0.1.0/csrc/catlass/docs/contents/advanced/matmul_template_summary.md +543 -0
  33. flash_attn_npu-0.1.0/csrc/catlass/docs/contents/advanced/swizzle_explanation.md +39 -0
  34. flash_attn_npu-0.1.0/csrc/catlass/docs/contents/example_design/10_grouped_matmul_slice_m_per_token_dequant.md +140 -0
  35. flash_attn_npu-0.1.0/csrc/catlass/docs/contents/example_design/34_single_splitk_matmul.md +259 -0
  36. flash_attn_npu-0.1.0/csrc/catlass/docs/dev_guide.md +169 -0
  37. flash_attn_npu-0.1.0/csrc/catlass/docs/dir_structure.md +25 -0
  38. flash_attn_npu-0.1.0/csrc/catlass/docs/evaluation_collections.md +20 -0
  39. flash_attn_npu-0.1.0/csrc/catlass/docs/example_design_collections.md +5 -0
  40. flash_attn_npu-0.1.0/csrc/catlass/docs/images/api_custom.png +0 -0
  41. flash_attn_npu-0.1.0/csrc/catlass/docs/images/api_level.png +0 -0
  42. flash_attn_npu-0.1.0/csrc/catlass/docs/images/catlass_optimize_guidance/swizzle_case.png +0 -0
  43. flash_attn_npu-0.1.0/csrc/catlass/docs/images/homepage_perf_gmm.png +0 -0
  44. flash_attn_npu-0.1.0/csrc/catlass/docs/images/homepage_perf_mm.png +0 -0
  45. flash_attn_npu-0.1.0/csrc/catlass/docs/images/split_k_matmul.png +0 -0
  46. flash_attn_npu-0.1.0/csrc/catlass/docs/images/swizzle10.png +0 -0
  47. flash_attn_npu-0.1.0/csrc/catlass/docs/images/swizzle30.png +0 -0
  48. flash_attn_npu-0.1.0/csrc/catlass/docs/images/swizzle31.png +0 -0
  49. flash_attn_npu-0.1.0/csrc/catlass/docs/quickstart.md +101 -0
  50. flash_attn_npu-0.1.0/csrc/catlass/docs/summary.md +29 -0
  51. flash_attn_npu-0.1.0/csrc/catlass/docs/tla/01_layout.md +168 -0
  52. flash_attn_npu-0.1.0/csrc/catlass/docs/tla/02_tensor.md +54 -0
  53. flash_attn_npu-0.1.0/csrc/catlass/docs/tools/ascendc_dump.md +83 -0
  54. flash_attn_npu-0.1.0/csrc/catlass/docs/tools/msdebug.md +367 -0
  55. flash_attn_npu-0.1.0/csrc/catlass/docs/tools/performance_tools.md +185 -0
  56. flash_attn_npu-0.1.0/csrc/catlass/docs/tools/print.md +104 -0
  57. flash_attn_npu-0.1.0/csrc/catlass/docs/tutorials.md +957 -0
  58. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/arch/arch.hpp +40 -0
  59. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/arch/cross_core_sync.hpp +120 -0
  60. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/arch/local_tensor_buffer.hpp +233 -0
  61. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/arch/resource.hpp +43 -0
  62. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/catlass.hpp +46 -0
  63. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/block/block_conv.hpp +59 -0
  64. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/block/block_conv2d_pingpong.hpp +457 -0
  65. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/block/block_conv3d_pingpong_bias.hpp +865 -0
  66. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/block/block_swizzle.hpp +237 -0
  67. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/device/device_conv.hpp +110 -0
  68. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/dispatch_policy.hpp +43 -0
  69. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/kernel/basic_conv2d.hpp +191 -0
  70. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/kernel/conv3d_bias.hpp +187 -0
  71. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/tile/copy_gm_to_l1.hpp +117 -0
  72. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/tile/copy_l0c_to_gm.hpp +121 -0
  73. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/tile/copy_l1_to_l0a.hpp +82 -0
  74. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/tile/copy_l1_to_l0b.hpp +69 -0
  75. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv/tile/tile_copy.hpp +53 -0
  76. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/conv_coord.hpp +1189 -0
  77. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/coord.hpp +346 -0
  78. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/debug.hpp +61 -0
  79. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/detail/alignment.hpp +81 -0
  80. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/detail/callback.hpp +61 -0
  81. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/detail/dependent_false.hpp +20 -0
  82. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/detail/kernel_adapter.hpp +56 -0
  83. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/detail/macros.hpp +26 -0
  84. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue.hpp +43 -0
  85. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_elemwise_no_source.hpp +185 -0
  86. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_elemwise_one_source.hpp +183 -0
  87. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_fa_rescale_o.hpp +339 -0
  88. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_fa_softmax.hpp +405 -0
  89. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_gemm.hpp +214 -0
  90. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_gemv.hpp +217 -0
  91. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_mla_fd_rescale_o.hpp +308 -0
  92. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_mla_rescale_o.hpp +479 -0
  93. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_mla_softmax.hpp +443 -0
  94. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_mla_tp1_rescale_o.hpp +341 -0
  95. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_mla_tp1_softmax.hpp +508 -0
  96. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_online_softmax_no_mask.hpp +877 -0
  97. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_per_token_dequant.hpp +644 -0
  98. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_rescale_o_no_split_row.hpp +308 -0
  99. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/block/block_epilogue_w4a4_per_token_per_channel_dequant.hpp +260 -0
  100. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/dispatch_policy.hpp +107 -0
  101. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/copy_gm_to_ub.hpp +187 -0
  102. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/copy_ub_to_gm.hpp +143 -0
  103. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_broadcast_add.hpp +82 -0
  104. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_broadcast_inplace_by_column.hpp +68 -0
  105. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_broadcast_inplace_by_row.hpp +58 -0
  106. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_broadcast_mul.hpp +137 -0
  107. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_broadcast_one_blk.hpp +59 -0
  108. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_cast.hpp +48 -0
  109. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_copy.hpp +138 -0
  110. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_elemwise_add.hpp +49 -0
  111. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_elemwise_gelu.hpp +55 -0
  112. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_elemwise_mul.hpp +48 -0
  113. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_elemwise_muls.hpp +42 -0
  114. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_elemwise_silu.hpp +50 -0
  115. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/epilogue/tile/tile_swizzle.hpp +92 -0
  116. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad.hpp +96 -0
  117. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_dynamic_aiv.hpp +513 -0
  118. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_dynamic_common.hpp +356 -0
  119. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_dynamic_single_core_splitk.hpp +391 -0
  120. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_dynamic_small.hpp +284 -0
  121. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_dynamic_streamk.hpp +372 -0
  122. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_fa_pv.hpp +194 -0
  123. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_fa_qk.hpp +200 -0
  124. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_fai_pv_head_tail.hpp +315 -0
  125. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_fai_pv_normal.hpp +306 -0
  126. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_fai_qk_head_tail.hpp +305 -0
  127. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_fai_qk_normal.hpp +337 -0
  128. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_gemm.hpp +364 -0
  129. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_mla_pv.hpp +207 -0
  130. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_mla_pv_tp1_spec.hpp +221 -0
  131. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_mla_qk.hpp +241 -0
  132. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_mla_qk_tp1_spec.hpp +259 -0
  133. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_pingpong.hpp +360 -0
  134. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_pingpong_bias.hpp +419 -0
  135. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_pingpong_full_loadA.hpp +345 -0
  136. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_pingpong_slice_k_with_prologue.hpp +805 -0
  137. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_pingpong_with_prologue.hpp +535 -0
  138. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_preload.hpp +399 -0
  139. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_preload_async.hpp +447 -0
  140. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_preload_async_with_callback.hpp +448 -0
  141. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_single_core_splitk.hpp +393 -0
  142. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_small.hpp +320 -0
  143. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_streamk.hpp +410 -0
  144. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_mmad_w4a4_per_token_per_channel_dequant.hpp +501 -0
  145. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_scheduler_iterateK.hpp +25 -0
  146. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_scheduler_l2_misplace_core.hpp +252 -0
  147. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/block/block_swizzle.hpp +995 -0
  148. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/device/device_gemm.hpp +111 -0
  149. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/dispatch_policy.hpp +303 -0
  150. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/gemm_type.hpp +29 -0
  151. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/helper.hpp +320 -0
  152. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/basic_matmul.hpp +149 -0
  153. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/basic_matmul_preload.hpp +165 -0
  154. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/batched_matmul.hpp +181 -0
  155. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_aiv_matmul.hpp +162 -0
  156. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_common_matmul.hpp +146 -0
  157. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_local_padding_c_padding_common_matmul.hpp +418 -0
  158. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_padding_common_matmul.hpp +277 -0
  159. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_padding_multi_core_splitk_matmul.hpp +278 -0
  160. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_padding_streamk_matmul.hpp +512 -0
  161. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_single_core_splitk_matmul.hpp +1018 -0
  162. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/dynamic_small_matmul.hpp +121 -0
  163. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/fp8_matmul.hpp +279 -0
  164. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/gemm.hpp +450 -0
  165. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/group_gemm.hpp +533 -0
  166. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/grouped_matmul.hpp +220 -0
  167. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/grouped_matmul_slice_k.hpp +292 -0
  168. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/grouped_matmul_slice_k_per_token_dequant.hpp +347 -0
  169. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/grouped_matmul_slice_m.hpp +216 -0
  170. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/grouped_matmul_slice_m_per_token_dequant.hpp +350 -0
  171. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/grouped_matmul_slice_m_per_token_dequant_multistage_workspace.hpp +392 -0
  172. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/matmul_activation.hpp +215 -0
  173. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/matmul_bias.hpp +166 -0
  174. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/matmul_epilogue.hpp +208 -0
  175. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/matmul_full_loadA.hpp +164 -0
  176. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/optimized_matmul.hpp +412 -0
  177. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/padding_matmul.hpp +1221 -0
  178. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/padding_splitk_matmul.hpp +367 -0
  179. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/quant_matmul.hpp +251 -0
  180. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/quant_matmul_multistage_workspace.hpp +340 -0
  181. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/single_core_slicek_matmul.hpp +430 -0
  182. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/small_matmul.hpp +147 -0
  183. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/splitk_matmul.hpp +366 -0
  184. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/streamk_matmul.hpp +448 -0
  185. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/w4a4_matmul_per_token_per_channel_dequant.hpp +386 -0
  186. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/w4a8_matmul.hpp +216 -0
  187. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/kernel/w8a16_matmul.hpp +217 -0
  188. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/cast_fp8_to_fp16.hpp +422 -0
  189. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/cast_int4_to_int8.hpp +184 -0
  190. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/cast_int8_to_fp16.hpp +190 -0
  191. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_gm_to_l1.hpp +1472 -0
  192. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_gm_to_ub.hpp +60 -0
  193. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_l0c_to_gm.hpp +482 -0
  194. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_l1_to_bt.hpp +61 -0
  195. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_l1_to_fp.hpp +61 -0
  196. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_l1_to_l0a.hpp +414 -0
  197. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_l1_to_l0b.hpp +514 -0
  198. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/copy_ub_to_gm.hpp +60 -0
  199. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/tile_copy.hpp +274 -0
  200. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/tile_mmad.hpp +109 -0
  201. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/tile_muls.hpp +52 -0
  202. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm/tile/tile_traits.hpp +48 -0
  203. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemm_coord.hpp +158 -0
  204. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/block/block_gemv.hpp +29 -0
  205. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/block/block_gemv_aic.hpp +365 -0
  206. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/block/block_gemv_aiv.hpp +233 -0
  207. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/device/device_gemv.hpp +110 -0
  208. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/helper.hpp +130 -0
  209. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/kernel/kernel_gemv_aic.hpp +290 -0
  210. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/kernel/kernel_gemv_aiv.hpp +204 -0
  211. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/tile/matrix_copy_gm_to_ub.hpp +210 -0
  212. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/tile/tile_copy.hpp +92 -0
  213. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/tile/tile_vmad.hpp +472 -0
  214. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/tile/tile_vmuls.hpp +56 -0
  215. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/tile/vec_copy_gm_to_ub.hpp +54 -0
  216. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv/tile/vec_copy_ub_to_gm.hpp +103 -0
  217. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/gemv_coord.hpp +100 -0
  218. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/layout/layout.hpp +19 -0
  219. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/layout/matrix.hpp +1612 -0
  220. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/layout/tensor.hpp +273 -0
  221. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/layout/vector.hpp +134 -0
  222. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/matrix_coord.hpp +103 -0
  223. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/numeric_size.hpp +35 -0
  224. flash_attn_npu-0.1.0/csrc/catlass/include/catlass/status.hpp +20 -0
  225. flash_attn_npu-0.1.0/csrc/catlass/scripts/build.sh +201 -0
  226. flash_attn_npu-0.1.0/csrc/catlass/tools/CMakeLists.txt +14 -0
  227. flash_attn_npu-0.1.0/csrc/catlass/tools/library/CMakeLists.txt +80 -0
  228. flash_attn_npu-0.1.0/csrc/catlass/tools/library/include/catlass/library/manifest.h +38 -0
  229. flash_attn_npu-0.1.0/csrc/catlass/tools/library/include/catlass/library/operation.h +255 -0
  230. flash_attn_npu-0.1.0/csrc/catlass/tools/library/scripts/code_generator.py +57 -0
  231. flash_attn_npu-0.1.0/csrc/catlass/tools/library/scripts/gemm_operation.py +753 -0
  232. flash_attn_npu-0.1.0/csrc/catlass/tools/library/scripts/library.py +124 -0
  233. flash_attn_npu-0.1.0/csrc/catlass/tools/library/scripts/manifest.py +205 -0
  234. flash_attn_npu-0.1.0/csrc/catlass/tools/library/scripts/search_space.py +669 -0
  235. flash_attn_npu-0.1.0/csrc/catlass/tools/library/scripts/search_space_config.py +51 -0
  236. flash_attn_npu-0.1.0/csrc/catlass/tools/library/scripts/utils.py +75 -0
  237. flash_attn_npu-0.1.0/csrc/catlass/tools/library/src/gemm_operation.h +305 -0
  238. flash_attn_npu-0.1.0/csrc/catlass/tools/library/src/library_utils.h +95 -0
  239. flash_attn_npu-0.1.0/csrc/catlass/tools/library/src/manifest.cpp +37 -0
  240. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/CMakeLists.txt +58 -0
  241. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/README.md +230 -0
  242. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/dfx_kernel/CMakeLists.txt +15 -0
  243. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/dfx_kernel/clear_l2_cache.cpp +60 -0
  244. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/catlass_tuner.h +46 -0
  245. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/command_line_parser.h +123 -0
  246. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/device_memory_manager.h +122 -0
  247. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/gemm_op_config.h +196 -0
  248. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/library_helper.h +36 -0
  249. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/log.h +44 -0
  250. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/m_t_var.h +121 -0
  251. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/metric.h +150 -0
  252. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/metrics.h +45 -0
  253. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/op_config.h +143 -0
  254. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/op_launcher.h +45 -0
  255. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/include/profiler.h +85 -0
  256. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/catlass_tuner.cpp +221 -0
  257. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/command_line_parser.cpp +267 -0
  258. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/device_memory_manager.cpp +288 -0
  259. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/gemm_op_config.cpp +413 -0
  260. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/library_helper.cpp +205 -0
  261. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/main.cpp +26 -0
  262. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/metric.cpp +132 -0
  263. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/metrics.cpp +289 -0
  264. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/op_config.cpp +111 -0
  265. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/op_launcher.cpp +65 -0
  266. flash_attn_npu-0.1.0/csrc/catlass/tools/tuner/src/profiler.cpp +407 -0
  267. flash_attn_npu-0.1.0/flash_attn_npu/__init__.py +11 -0
  268. flash_attn_npu-0.1.0/flash_attn_npu/flash_attn_interface.py +1599 -0
  269. flash_attn_npu-0.1.0/flash_attn_npu.egg-info/PKG-INFO +308 -0
  270. flash_attn_npu-0.1.0/flash_attn_npu.egg-info/SOURCES.txt +281 -0
  271. flash_attn_npu-0.1.0/flash_attn_npu.egg-info/dependency_links.txt +1 -0
  272. flash_attn_npu-0.1.0/flash_attn_npu.egg-info/requires.txt +2 -0
  273. flash_attn_npu-0.1.0/flash_attn_npu.egg-info/top_level.txt +4 -0
  274. flash_attn_npu-0.1.0/flash_attn_npu_v3/__init__.py +5 -0
  275. flash_attn_npu-0.1.0/flash_attn_npu_v3/flash_attn_interface.py +1146 -0
  276. flash_attn_npu-0.1.0/pyproject.toml +13 -0
  277. flash_attn_npu-0.1.0/setup.cfg +4 -0
  278. flash_attn_npu-0.1.0/setup.py +282 -0
  279. flash_attn_npu-0.1.0/tests/test_flash_attn_npu.py +397 -0
  280. flash_attn_npu-0.1.0/tests/test_flash_attn_npu_bwd.py +243 -0
  281. flash_attn_npu-0.1.0/tests/test_flash_attn_npu_v3.py +326 -0
@@ -0,0 +1 @@
1
+ Minghua Shen, shenmh6@mail.sysu.edu.cn
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2022, the respective contributors, as shown by the AUTHORS file.
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ * Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ * Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,7 @@
1
+ include csrc/catlass/**/*.h
2
+ include csrc/catlass/**/*.hpp
3
+ include csrc/catlass/**/*.cpp
4
+ recursive-include csrc/catlass *
5
+ prune csrc/catlass/tests
6
+ prune csrc/catlass/examples
7
+ prune csrc/catlass/benchmarks
@@ -0,0 +1,308 @@
1
+ Metadata-Version: 2.4
2
+ Name: flash_attn_npu
3
+ Version: 0.1.0
4
+ Summary: High-performance FlashAttention implementation for Ascend NPU
5
+ Home-page: https://github.com/MinghuasLab/flash-attention-npu
6
+ Author: Minghua Shen
7
+ Author-email: shenmh6@mail.sysu.edu.cn
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: BSD License
10
+ Classifier: Operating System :: Unix
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ License-File: AUTHORS
15
+ Requires-Dist: torch
16
+ Requires-Dist: einops
17
+ Dynamic: author
18
+ Dynamic: author-email
19
+ Dynamic: classifier
20
+ Dynamic: description
21
+ Dynamic: description-content-type
22
+ Dynamic: home-page
23
+ Dynamic: license-file
24
+ Dynamic: requires-dist
25
+ Dynamic: requires-python
26
+ Dynamic: summary
27
+
28
+ # FlashAttention for Ascend NPU
29
+
30
+ <div align="center">
31
+ <a href="#"><img src="https://img.shields.io/badge/English-README.md-blue?style=flat-square" alt="English"></a> <a href="README.zh.md"><img src="https://img.shields.io/badge/中文-README.zh.md-green?style=flat-square" alt="中文"></a>
32
+ </div>
33
+
34
+ ## Introduction
35
+
36
+ FlashAttention significantly improves training and inference efficiency for modern large language models through tiling and memory-aware algorithms. The current mainstream implementation is [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention), which is primarily designed for NVIDIA GPU architectures. During Ascend platform migration, we found the lack of an API-compatible implementation with [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention) increased adaptation complexity. To address this gap, this repository implements FlashAttention algorithms adapted for Ascend NPU by following the core design of [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention) and building upon the [CANN/CATLASS](https://gitcode.com/cann/catlass) framework and its sample code. We provide an API consistent with [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention) to facilitate model migration and enable future attention algorithm optimizations for Ascend NPU.
37
+
38
+ **This project is under active development, and discussions and contributions are highly welcome.**
39
+
40
+ ## Getting Started
41
+
42
+ ### Prerequisites
43
+
44
+ - Hardware: Ascend 910B / 910C NPU
45
+ - OS: Linux
46
+ - Software:
47
+ - CANN >= 8.5.0
48
+ - PyTorch >= 2.1.0
49
+ - torch_npu >= 2.1.0 (same version with PyTorch)
50
+ - Python Dependencies
51
+ ```bash
52
+ pip install packaging psutil ninja
53
+ ```
54
+
55
+ ### Installation
56
+
57
+ 1. Set environment variables:
58
+ ```bash
59
+ source /usr/local/Ascend/cann/set_env.sh
60
+ ```
61
+
62
+ 2. Clone the repository:
63
+ ```bash
64
+ git clone https://github.com/MinghuasLab/flash-attention-npu.git
65
+ cd flash-attention-npu
66
+ git submodule update --init --recursive
67
+ ```
68
+
69
+ 3. Build and install:
70
+
71
+ ```bash
72
+ python setup.py install
73
+ ```
74
+
75
+ Build specific version:
76
+
77
+ ```bash
78
+ # Build v2 only
79
+ FLASH_ATTN_BUILD_VERSION=v2 python setup.py install
80
+
81
+ # Build v3 only
82
+ FLASH_ATTN_BUILD_VERSION=v3 python setup.py install
83
+ ```
84
+
85
+ ## Testing
86
+
87
+ Run test scripts:
88
+
89
+ ```bash
90
+ # Test FlashAttention v2
91
+ pytest -q -s tests/test_flash_attn_npu.py
92
+
93
+ # Test FlashAttention v3
94
+ pytest -q -s tests/test_flash_attn_npu_v3.py
95
+ ```
96
+
97
+ ## Usage
98
+
99
+ ### FlashAttention v2
100
+
101
+ #### flash_attn_with_kvcache
102
+
103
+ ```python
104
+ def flash_attn_with_kvcache(
105
+ q,
106
+ k_cache,
107
+ v_cache,
108
+ k=None,
109
+ v=None,
110
+ rotary_cos=None,
111
+ rotary_sin=None,
112
+ cache_seqlens: Optional[Union[(int, torch.Tensor)]] = None,
113
+ cache_batch_idx: Optional[torch.Tensor] = None,
114
+ block_table: Optional[torch.Tensor] = None,
115
+ softmax_scale=None,
116
+ causal=False,
117
+ window_size=(-1, -1), # -1 means infinite context window
118
+ rotary_interleaved=True,
119
+ alibi_slopes=None,
120
+ ):
121
+ """
122
+ If k and v are not None, k_cache and v_cache will be updated *in-place* with the new values
123
+ from k and v. This is useful for incremental decoding: you can pass in the cached key/value
124
+ from the previous step, update them with the new key/value from the current step, and in
125
+ the same kernel perform attention with the updated cache.
126
+
127
+ If you pass in k / v, you must make sure that the cache is large enough to hold the new values.
128
+ For example, the KV cache can be pre-allocated with the max sequence length, and you can use
129
+ cache_seqlens to keep track of the current sequence length for each sequence in the batch.
130
+
131
+ If rotary_cos and rotary_sin are passed in, rotary positional embedding will be applied.
132
+ key @k will be rotated by rotary_cos and rotary_sin at positions cache_seqlens, cache_seqlens + 1, etc.
133
+ If causal or local (i.e., window_size != (-1, -1)), query @q will be rotated at positions
134
+ cache_seqlens, cache_seqlens + 1, etc.
135
+ If neither causal nor local, query @q will be rotated only at position cache_seqlens
136
+ (i.e., we assume that all tokens in @q are at position cache_seqlens).
137
+
138
+ Multi-query and grouped-query attention (MQA/GQA) are supported by passing in fewer KV heads
139
+ than Q heads. Q head count must be divisible by KV head count.
140
+ For example, if Q has 6 heads and K, V have 2 heads, then Q heads 0, 1, 2 will attend to
141
+ K, V head 0, and Q heads 3, 4, 5 will attend to K, V head 1.
142
+
143
+ If causal=True, the causal mask is aligned to the bottom-right corner of the attention matrix.
144
+ For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = mask) is:
145
+ 1 1 1 1 0
146
+ 1 1 1 1 1
147
+ If seqlen_q = 5 and seqlen_k = 2, the causal mask is:
148
+ 0 0
149
+ 0 0
150
+ 0 0
151
+ 1 0
152
+ 1 1
153
+ If a row of the mask is all zeros, the output will be zero.
154
+
155
+ If window_size != (-1, -1), implements sliding window local attention.
156
+ Query at position i will only attend to keys in [i + seqlen_k - seqlen_q - window_size[0],
157
+ i + seqlen_k - seqlen_q + window_size[1]].
158
+
159
+ Warning: Does not support backward pass.
160
+
161
+ Arguments:
162
+ q: (batch_size, seqlen, nheads, headdim)
163
+ k_cache: If no block_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim);
164
+ if block_table (i.e., paged KV cache), shape (num_blocks, page_block_size, nheads_k, headdim)
165
+ page_block_size must be a multiple of 256.
166
+ v_cache: If no block_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim);
167
+ if block_table (i.e., paged KV cache), shape (num_blocks, page_block_size, nheads_k, headdim)
168
+ k [optional]: (batch_size, seqlen_new, nheads_k, headdim). If not None, we concatenate k to k_cache
169
+ starting at the position specified by cache_seqlens.
170
+ v [optional]: (batch_size, seqlen_new, nheads_k, headdim). Similar to k.
171
+ rotary_cos [optional]: (seqlen_ro, rotary_dim / 2). If not None, we apply rotary positional
172
+ embedding to k and q. Only applies if k and v are passed in. rotary_dim must be divisible by 16.
173
+ rotary_sin [optional]: (seqlen_ro, rotary_dim / 2). Same as rotary_cos.
174
+ cache_seqlens: int or (batch_size,), dtype torch.int32. The sequence length of the KV cache.
175
+ block_table [optional]: (batch_size, max_num_blocks_per_seq), dtype torch.int32.
176
+ cache_batch_idx: (batch_size,), dtype torch.int32. Indices to index into the KV cache.
177
+ If None, we assume that the batch indices are [0, 1, 2, ..., batch_size - 1].
178
+ If indices are not unique and k and v are provided, the updated values in the cache
179
+ might be from any of the duplicate indices.
180
+ softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim).
181
+ causal: bool. Whether to apply causal attention mask (e.g., for autoregressive modeling).
182
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
183
+ rotary_interleaved: bool. Only applies if rotary_cos and rotary_sin are passed in.
184
+ If True, rotary positional embedding combines dimensions 0 & 1, 2 & 3, etc.
185
+ If False, rotary positional embedding combines dimensions 0 & rotary_dim / 2,
186
+ 1 & rotary_dim / 2 + 1 (i.e., GPT-NeoX style).
187
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32.
188
+ Add bias to the attention scores of query i and key j of (-alibi_slope * |i + seqlen_k - seqlen_q - j|).
189
+
190
+ Returns:
191
+ out: (batch_size, seqlen, nheads, headdim).
192
+ """
193
+ ```
194
+
195
+ ### FlashAttention v3
196
+
197
+ #### flash_attn_with_kvcache
198
+
199
+ ```python
200
+ def flash_attn_with_kvcache(
201
+ q,
202
+ k_cache,
203
+ v_cache,
204
+ k=None,
205
+ v=None,
206
+ qv=None,
207
+ rotary_cos=None,
208
+ rotary_sin=None,
209
+ cache_seqlens: Optional[Union[(int, torch.Tensor)]] = None,
210
+ cache_batch_idx: Optional[torch.Tensor] = None,
211
+ cache_leftpad: Optional[torch.Tensor] = None,
212
+ page_table: Optional[torch.Tensor] = None,
213
+ cu_seqlens_q: Optional[torch.Tensor] = None,
214
+ cu_seqlens_k_new: Optional[torch.Tensor] = None,
215
+ max_seqlen_q: Optional[int] = None,
216
+ rotary_seqlens: Optional[torch.Tensor] = None,
217
+ q_descale: Optional[torch.Tensor] = None,
218
+ k_descale: Optional[torch.Tensor] = None,
219
+ v_descale: Optional[torch.Tensor] = None,
220
+ softmax_scale=None,
221
+ causal=False,
222
+ window_size=(-1, -1),
223
+ attention_chunk=0,
224
+ softcap=0.0,
225
+ rotary_interleaved=True,
226
+ scheduler_metadata=None,
227
+ num_splits=0,
228
+ pack_gqa=None,
229
+ sm_margin=0,
230
+ return_softmax_lse=False,
231
+ ):
232
+ """
233
+ v3 version of the KV cache interface, with more features compared to v2.
234
+
235
+ If k and v are not None, k_cache and v_cache will be updated *in-place* with the new values
236
+ from k and v. This is useful for incremental decoding.
237
+
238
+ Multi-query and grouped-query attention (MQA/GQA) are supported.
239
+
240
+ If causal=True, the causal mask is aligned to the bottom-right corner of the attention matrix.
241
+
242
+ If window_size != (-1, -1), implements sliding window local attention.
243
+
244
+ Warning: Does not support backward pass.
245
+
246
+ Arguments:
247
+ q: (batch_size, seqlen, nheads, headdim)
248
+ k_cache: If no page_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim);
249
+ if page_table (i.e., paged KV cache), shape (num_blocks, page_block_size, nheads_k, headdim)
250
+ page_block_size can be any value (e.g., 1, 2, 3, 64, etc).
251
+ v_cache: If no page_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim_v);
252
+ if page_table, shape (num_blocks, page_block_size, nheads_k, headdim_v).
253
+ k [optional]: (batch_size, seqlen_new, nheads_k, headdim). If not None, concatenate k to k_cache
254
+ starting at the position specified by cache_seqlens.
255
+ v [optional]: (batch_size, seqlen_new, nheads_k, headdim_v). Similar to k.
256
+ qv [optional]: (batch_size, seqlen, nheads, headdim_v).
257
+ rotary_cos [optional]: (seqlen_ro, rotary_dim / 2). Cosine values for rotary positional embedding.
258
+ rotary_sin [optional]: (seqlen_ro, rotary_dim / 2). Sine values for rotary positional embedding.
259
+ cache_seqlens: int or (batch_size,), dtype torch.int32. The sequence length of the KV cache.
260
+ cache_batch_idx: (batch_size,), dtype torch.int32. Indices to index into the KV cache.
261
+ cache_leftpad: (batch_size,), dtype torch.int32. KV cache starting index.
262
+ page_table [optional]: (batch_size, max_num_blocks_per_seq), dtype torch.int32.
263
+ cu_seqlens_q [optional]: Cumulative sequence lengths of queries in ragged mode.
264
+ cu_seqlens_k_new [optional]: Cumulative sequence lengths of new keys in ragged mode.
265
+ max_seqlen_q [optional]: Maximum query sequence length in ragged mode.
266
+ rotary_seqlens [optional]: Sequence lengths for rotary positional embedding.
267
+ q_descale, k_descale, v_descale: Optional dequantization scales for FP8 quantization.
268
+ softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim).
269
+ causal: bool. Whether to apply causal attention mask.
270
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
271
+ attention_chunk: int. Attention chunk size.
272
+ softcap: float. Activates softcapping attention if > 0.
273
+ rotary_interleaved: bool. Rotary positional embedding mode.
274
+ scheduler_metadata: Optional scheduler metadata.
275
+ num_splits: int. If > 1, split key/value along the sequence dimension into this many chunks.
276
+ If num_splits == 1, no splitting. If num_splits == 0, automatically selected.
277
+ pack_gqa: bool. Whether to pack GQA for better performance.
278
+ sm_margin: int. SM margin for tuning.
279
+ return_softmax_lse: bool. Whether to return logsumexp of attention scores.
280
+
281
+ Returns:
282
+ out: (batch_size, seqlen, nheads, headdim).
283
+ softmax_lse [optional]: (batch_size, nheads, seqlen). The logsumexp of each row of QK^T * scaling.
284
+ """
285
+ ```
286
+
287
+ ## Features
288
+
289
+ #### flash_attn_with_kvcache
290
+ | Feature | v2 | v3 |
291
+ |---------|----|----|
292
+ | FP16 (float16) | ✅ | ✅ |
293
+ | BF16 (bfloat16) | ✅ | ✅ |
294
+ | Causal Attention | ✅ | ✅ |
295
+ | Sliding Window Attention | - | - |
296
+ | MQA/GQA | ✅ | ✅ |
297
+ | Paged KV Cache | ✅ | ✅ |
298
+ | Rotary Positional Embedding (RoPE) | - | - |
299
+ | ALiBi | - | - |
300
+ | Softcapping | - | - |
301
+ | FP8 Quantization | - | - |
302
+ | Variable-length Sequences | ✅ | ✅ |
303
+
304
+
305
+
306
+ ## License
307
+
308
+ This project is licensed under the BSD 3-Clause License. See the [LICENSE](./LICENSE) file for details.
@@ -0,0 +1,281 @@
1
+ # FlashAttention for Ascend NPU
2
+
3
+ <div align="center">
4
+ <a href="#"><img src="https://img.shields.io/badge/English-README.md-blue?style=flat-square" alt="English"></a> <a href="README.zh.md"><img src="https://img.shields.io/badge/中文-README.zh.md-green?style=flat-square" alt="中文"></a>
5
+ </div>
6
+
7
+ ## Introduction
8
+
9
+ FlashAttention significantly improves training and inference efficiency for modern large language models through tiling and memory-aware algorithms. The current mainstream implementation is [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention), which is primarily designed for NVIDIA GPU architectures. During Ascend platform migration, we found the lack of an API-compatible implementation with [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention) increased adaptation complexity. To address this gap, this repository implements FlashAttention algorithms adapted for Ascend NPU by following the core design of [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention) and building upon the [CANN/CATLASS](https://gitcode.com/cann/catlass) framework and its sample code. We provide an API consistent with [Dao-AILab/flash-attention](https://github.com/Dao-AILab/flash-attention) to facilitate model migration and enable future attention algorithm optimizations for Ascend NPU.
10
+
11
+ **This project is under active development, and discussions and contributions are highly welcome.**
12
+
13
+ ## Getting Started
14
+
15
+ ### Prerequisites
16
+
17
+ - Hardware: Ascend 910B / 910C NPU
18
+ - OS: Linux
19
+ - Software:
20
+ - CANN >= 8.5.0
21
+ - PyTorch >= 2.1.0
22
+ - torch_npu >= 2.1.0 (same version with PyTorch)
23
+ - Python Dependencies
24
+ ```bash
25
+ pip install packaging psutil ninja
26
+ ```
27
+
28
+ ### Installation
29
+
30
+ 1. Set environment variables:
31
+ ```bash
32
+ source /usr/local/Ascend/cann/set_env.sh
33
+ ```
34
+
35
+ 2. Clone the repository:
36
+ ```bash
37
+ git clone https://github.com/MinghuasLab/flash-attention-npu.git
38
+ cd flash-attention-npu
39
+ git submodule update --init --recursive
40
+ ```
41
+
42
+ 3. Build and install:
43
+
44
+ ```bash
45
+ python setup.py install
46
+ ```
47
+
48
+ Build specific version:
49
+
50
+ ```bash
51
+ # Build v2 only
52
+ FLASH_ATTN_BUILD_VERSION=v2 python setup.py install
53
+
54
+ # Build v3 only
55
+ FLASH_ATTN_BUILD_VERSION=v3 python setup.py install
56
+ ```
57
+
58
+ ## Testing
59
+
60
+ Run test scripts:
61
+
62
+ ```bash
63
+ # Test FlashAttention v2
64
+ pytest -q -s tests/test_flash_attn_npu.py
65
+
66
+ # Test FlashAttention v3
67
+ pytest -q -s tests/test_flash_attn_npu_v3.py
68
+ ```
69
+
70
+ ## Usage
71
+
72
+ ### FlashAttention v2
73
+
74
+ #### flash_attn_with_kvcache
75
+
76
+ ```python
77
+ def flash_attn_with_kvcache(
78
+ q,
79
+ k_cache,
80
+ v_cache,
81
+ k=None,
82
+ v=None,
83
+ rotary_cos=None,
84
+ rotary_sin=None,
85
+ cache_seqlens: Optional[Union[(int, torch.Tensor)]] = None,
86
+ cache_batch_idx: Optional[torch.Tensor] = None,
87
+ block_table: Optional[torch.Tensor] = None,
88
+ softmax_scale=None,
89
+ causal=False,
90
+ window_size=(-1, -1), # -1 means infinite context window
91
+ rotary_interleaved=True,
92
+ alibi_slopes=None,
93
+ ):
94
+ """
95
+ If k and v are not None, k_cache and v_cache will be updated *in-place* with the new values
96
+ from k and v. This is useful for incremental decoding: you can pass in the cached key/value
97
+ from the previous step, update them with the new key/value from the current step, and in
98
+ the same kernel perform attention with the updated cache.
99
+
100
+ If you pass in k / v, you must make sure that the cache is large enough to hold the new values.
101
+ For example, the KV cache can be pre-allocated with the max sequence length, and you can use
102
+ cache_seqlens to keep track of the current sequence length for each sequence in the batch.
103
+
104
+ If rotary_cos and rotary_sin are passed in, rotary positional embedding will be applied.
105
+ key @k will be rotated by rotary_cos and rotary_sin at positions cache_seqlens, cache_seqlens + 1, etc.
106
+ If causal or local (i.e., window_size != (-1, -1)), query @q will be rotated at positions
107
+ cache_seqlens, cache_seqlens + 1, etc.
108
+ If neither causal nor local, query @q will be rotated only at position cache_seqlens
109
+ (i.e., we assume that all tokens in @q are at position cache_seqlens).
110
+
111
+ Multi-query and grouped-query attention (MQA/GQA) are supported by passing in fewer KV heads
112
+ than Q heads. Q head count must be divisible by KV head count.
113
+ For example, if Q has 6 heads and K, V have 2 heads, then Q heads 0, 1, 2 will attend to
114
+ K, V head 0, and Q heads 3, 4, 5 will attend to K, V head 1.
115
+
116
+ If causal=True, the causal mask is aligned to the bottom-right corner of the attention matrix.
117
+ For example, if seqlen_q = 2 and seqlen_k = 5, the causal mask (1 = keep, 0 = mask) is:
118
+ 1 1 1 1 0
119
+ 1 1 1 1 1
120
+ If seqlen_q = 5 and seqlen_k = 2, the causal mask is:
121
+ 0 0
122
+ 0 0
123
+ 0 0
124
+ 1 0
125
+ 1 1
126
+ If a row of the mask is all zeros, the output will be zero.
127
+
128
+ If window_size != (-1, -1), implements sliding window local attention.
129
+ Query at position i will only attend to keys in [i + seqlen_k - seqlen_q - window_size[0],
130
+ i + seqlen_k - seqlen_q + window_size[1]].
131
+
132
+ Warning: Does not support backward pass.
133
+
134
+ Arguments:
135
+ q: (batch_size, seqlen, nheads, headdim)
136
+ k_cache: If no block_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim);
137
+ if block_table (i.e., paged KV cache), shape (num_blocks, page_block_size, nheads_k, headdim)
138
+ page_block_size must be a multiple of 256.
139
+ v_cache: If no block_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim);
140
+ if block_table (i.e., paged KV cache), shape (num_blocks, page_block_size, nheads_k, headdim)
141
+ k [optional]: (batch_size, seqlen_new, nheads_k, headdim). If not None, we concatenate k to k_cache
142
+ starting at the position specified by cache_seqlens.
143
+ v [optional]: (batch_size, seqlen_new, nheads_k, headdim). Similar to k.
144
+ rotary_cos [optional]: (seqlen_ro, rotary_dim / 2). If not None, we apply rotary positional
145
+ embedding to k and q. Only applies if k and v are passed in. rotary_dim must be divisible by 16.
146
+ rotary_sin [optional]: (seqlen_ro, rotary_dim / 2). Same as rotary_cos.
147
+ cache_seqlens: int or (batch_size,), dtype torch.int32. The sequence length of the KV cache.
148
+ block_table [optional]: (batch_size, max_num_blocks_per_seq), dtype torch.int32.
149
+ cache_batch_idx: (batch_size,), dtype torch.int32. Indices to index into the KV cache.
150
+ If None, we assume that the batch indices are [0, 1, 2, ..., batch_size - 1].
151
+ If indices are not unique and k and v are provided, the updated values in the cache
152
+ might be from any of the duplicate indices.
153
+ softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim).
154
+ causal: bool. Whether to apply causal attention mask (e.g., for autoregressive modeling).
155
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
156
+ rotary_interleaved: bool. Only applies if rotary_cos and rotary_sin are passed in.
157
+ If True, rotary positional embedding combines dimensions 0 & 1, 2 & 3, etc.
158
+ If False, rotary positional embedding combines dimensions 0 & rotary_dim / 2,
159
+ 1 & rotary_dim / 2 + 1 (i.e., GPT-NeoX style).
160
+ alibi_slopes: (nheads,) or (batch_size, nheads), fp32.
161
+ Add bias to the attention scores of query i and key j of (-alibi_slope * |i + seqlen_k - seqlen_q - j|).
162
+
163
+ Returns:
164
+ out: (batch_size, seqlen, nheads, headdim).
165
+ """
166
+ ```
167
+
168
+ ### FlashAttention v3
169
+
170
+ #### flash_attn_with_kvcache
171
+
172
+ ```python
173
+ def flash_attn_with_kvcache(
174
+ q,
175
+ k_cache,
176
+ v_cache,
177
+ k=None,
178
+ v=None,
179
+ qv=None,
180
+ rotary_cos=None,
181
+ rotary_sin=None,
182
+ cache_seqlens: Optional[Union[(int, torch.Tensor)]] = None,
183
+ cache_batch_idx: Optional[torch.Tensor] = None,
184
+ cache_leftpad: Optional[torch.Tensor] = None,
185
+ page_table: Optional[torch.Tensor] = None,
186
+ cu_seqlens_q: Optional[torch.Tensor] = None,
187
+ cu_seqlens_k_new: Optional[torch.Tensor] = None,
188
+ max_seqlen_q: Optional[int] = None,
189
+ rotary_seqlens: Optional[torch.Tensor] = None,
190
+ q_descale: Optional[torch.Tensor] = None,
191
+ k_descale: Optional[torch.Tensor] = None,
192
+ v_descale: Optional[torch.Tensor] = None,
193
+ softmax_scale=None,
194
+ causal=False,
195
+ window_size=(-1, -1),
196
+ attention_chunk=0,
197
+ softcap=0.0,
198
+ rotary_interleaved=True,
199
+ scheduler_metadata=None,
200
+ num_splits=0,
201
+ pack_gqa=None,
202
+ sm_margin=0,
203
+ return_softmax_lse=False,
204
+ ):
205
+ """
206
+ v3 version of the KV cache interface, with more features compared to v2.
207
+
208
+ If k and v are not None, k_cache and v_cache will be updated *in-place* with the new values
209
+ from k and v. This is useful for incremental decoding.
210
+
211
+ Multi-query and grouped-query attention (MQA/GQA) are supported.
212
+
213
+ If causal=True, the causal mask is aligned to the bottom-right corner of the attention matrix.
214
+
215
+ If window_size != (-1, -1), implements sliding window local attention.
216
+
217
+ Warning: Does not support backward pass.
218
+
219
+ Arguments:
220
+ q: (batch_size, seqlen, nheads, headdim)
221
+ k_cache: If no page_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim);
222
+ if page_table (i.e., paged KV cache), shape (num_blocks, page_block_size, nheads_k, headdim)
223
+ page_block_size can be any value (e.g., 1, 2, 3, 64, etc).
224
+ v_cache: If no page_table, shape (batch_size_cache, seqlen_cache, nheads_k, headdim_v);
225
+ if page_table, shape (num_blocks, page_block_size, nheads_k, headdim_v).
226
+ k [optional]: (batch_size, seqlen_new, nheads_k, headdim). If not None, concatenate k to k_cache
227
+ starting at the position specified by cache_seqlens.
228
+ v [optional]: (batch_size, seqlen_new, nheads_k, headdim_v). Similar to k.
229
+ qv [optional]: (batch_size, seqlen, nheads, headdim_v).
230
+ rotary_cos [optional]: (seqlen_ro, rotary_dim / 2). Cosine values for rotary positional embedding.
231
+ rotary_sin [optional]: (seqlen_ro, rotary_dim / 2). Sine values for rotary positional embedding.
232
+ cache_seqlens: int or (batch_size,), dtype torch.int32. The sequence length of the KV cache.
233
+ cache_batch_idx: (batch_size,), dtype torch.int32. Indices to index into the KV cache.
234
+ cache_leftpad: (batch_size,), dtype torch.int32. KV cache starting index.
235
+ page_table [optional]: (batch_size, max_num_blocks_per_seq), dtype torch.int32.
236
+ cu_seqlens_q [optional]: Cumulative sequence lengths of queries in ragged mode.
237
+ cu_seqlens_k_new [optional]: Cumulative sequence lengths of new keys in ragged mode.
238
+ max_seqlen_q [optional]: Maximum query sequence length in ragged mode.
239
+ rotary_seqlens [optional]: Sequence lengths for rotary positional embedding.
240
+ q_descale, k_descale, v_descale: Optional dequantization scales for FP8 quantization.
241
+ softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(headdim).
242
+ causal: bool. Whether to apply causal attention mask.
243
+ window_size: (left, right). If not (-1, -1), implements sliding window local attention.
244
+ attention_chunk: int. Attention chunk size.
245
+ softcap: float. Activates softcapping attention if > 0.
246
+ rotary_interleaved: bool. Rotary positional embedding mode.
247
+ scheduler_metadata: Optional scheduler metadata.
248
+ num_splits: int. If > 1, split key/value along the sequence dimension into this many chunks.
249
+ If num_splits == 1, no splitting. If num_splits == 0, automatically selected.
250
+ pack_gqa: bool. Whether to pack GQA for better performance.
251
+ sm_margin: int. SM margin for tuning.
252
+ return_softmax_lse: bool. Whether to return logsumexp of attention scores.
253
+
254
+ Returns:
255
+ out: (batch_size, seqlen, nheads, headdim).
256
+ softmax_lse [optional]: (batch_size, nheads, seqlen). The logsumexp of each row of QK^T * scaling.
257
+ """
258
+ ```
259
+
260
+ ## Features
261
+
262
+ #### flash_attn_with_kvcache
263
+ | Feature | v2 | v3 |
264
+ |---------|----|----|
265
+ | FP16 (float16) | ✅ | ✅ |
266
+ | BF16 (bfloat16) | ✅ | ✅ |
267
+ | Causal Attention | ✅ | ✅ |
268
+ | Sliding Window Attention | - | - |
269
+ | MQA/GQA | ✅ | ✅ |
270
+ | Paged KV Cache | ✅ | ✅ |
271
+ | Rotary Positional Embedding (RoPE) | - | - |
272
+ | ALiBi | - | - |
273
+ | Softcapping | - | - |
274
+ | FP8 Quantization | - | - |
275
+ | Variable-length Sequences | ✅ | ✅ |
276
+
277
+
278
+
279
+ ## License
280
+
281
+ This project is licensed under the BSD 3-Clause License. See the [LICENSE](./LICENSE) file for details.
@@ -0,0 +1,57 @@
1
+ BasedOnStyle: LLVM
2
+ ColumnLimit: 120
3
+ IndentWidth: 4
4
+ TabWidth: 4
5
+ UseTab: Never
6
+ PointerAlignment: Right
7
+ SpaceBeforeParens: ControlStatements
8
+ SortIncludes: true
9
+ IncludeBlocks: Regroup
10
+ IncludeCategories:
11
+ - Regex: "^<[^/]+>$"
12
+ Priority: 1
13
+ - Regex: '^<[^/]+\.h(>|$)'
14
+ Priority: 2
15
+ - Regex: "^<.*/.*>"
16
+ Priority: 3
17
+ - Regex: '^"(catlass|tla)/.*"'
18
+ Priority: 4
19
+ - Regex: '^".*"'
20
+ Priority: 5
21
+ - Regex: ".*"
22
+ Priority: 6
23
+ AccessModifierOffset: -4
24
+ AlignAfterOpenBracket: BlockIndent
25
+ AllowAllArgumentsOnNextLine: false
26
+ AllowAllParametersOfDeclarationOnNextLine: false
27
+ AllowShortFunctionsOnASingleLine: false
28
+ AlwaysBreakTemplateDeclarations: Yes
29
+ BinPackParameters: false
30
+ BreakBeforeBinaryOperators: NonAssignment
31
+ BreakBeforeTernaryOperators: true
32
+ BreakConstructorInitializers: BeforeComma
33
+ PenaltyBreakAssignment: 100
34
+ PenaltyBreakBeforeFirstCallParameter: 100
35
+ PenaltyBreakComment: 60
36
+ PenaltyBreakFirstLessLess: 120
37
+ PenaltyBreakString: 1000
38
+ PenaltyExcessCharacter: 100
39
+ DerivePointerAlignment: false
40
+ SpaceInEmptyParentheses: false
41
+ SpacesInAngles: false
42
+ SpacesInConditionalStatement: false
43
+ SpacesInContainerLiterals: false
44
+ SpacesInCStyleCastParentheses: false
45
+ SpacesInParentheses: false
46
+ SpacesInSquareBrackets: false
47
+ AlwaysBreakAfterReturnType: None
48
+ AllowShortEnumsOnASingleLine: false
49
+ AllowShortBlocksOnASingleLine: false
50
+ BreakBeforeBraces: Custom
51
+ BraceWrapping:
52
+ AfterClass: false
53
+ AfterStruct: false
54
+ AfterUnion: false
55
+ AfterFunction: true
56
+ BeforeCatch: false
57
+ BeforeElse: false