whispercpp 1.3.6 → 1.3.7

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.
Files changed (828) hide show
  1. checksums.yaml +4 -4
  2. data/.document +3 -0
  3. data/.rdoc_options +2 -0
  4. data/README.md +38 -5
  5. data/Rakefile +18 -3
  6. data/ext/dependencies.rb +10 -4
  7. data/ext/dependencies_for_windows.rb +17 -0
  8. data/ext/extconf.rb +20 -8
  9. data/ext/options.rb +54 -14
  10. data/ext/options_for_windows.rb +51 -0
  11. data/ext/ruby_whisper.c +36 -42
  12. data/ext/ruby_whisper.h +135 -0
  13. data/ext/ruby_whisper_context.c +107 -28
  14. data/ext/ruby_whisper_log_queue.c +180 -0
  15. data/ext/ruby_whisper_log_settable.h +47 -0
  16. data/ext/ruby_whisper_parakeet.c +49 -0
  17. data/ext/ruby_whisper_parakeet_context.c +304 -0
  18. data/ext/ruby_whisper_parakeet_context_params.c +117 -0
  19. data/ext/ruby_whisper_parakeet_model.c +84 -0
  20. data/ext/ruby_whisper_parakeet_params.c +548 -0
  21. data/ext/ruby_whisper_parakeet_segment.c +157 -0
  22. data/ext/ruby_whisper_parakeet_token.c +188 -0
  23. data/ext/ruby_whisper_parakeet_transcribe.cpp +58 -0
  24. data/ext/ruby_whisper_params.c +256 -65
  25. data/ext/ruby_whisper_segment.c +6 -6
  26. data/ext/ruby_whisper_transcribe.cpp +42 -15
  27. data/ext/sources/CMakeLists.txt +41 -3
  28. data/ext/sources/CMakePresets.json +95 -0
  29. data/ext/sources/cmake/parakeet-config.cmake.in +30 -0
  30. data/ext/sources/cmake/parakeet.pc.in +10 -0
  31. data/ext/sources/cmake/whisper.pc.in +1 -1
  32. data/ext/sources/examples/CMakeLists.txt +4 -2
  33. data/ext/sources/examples/bench/bench.cpp +1 -1
  34. data/ext/sources/examples/cli/cli.cpp +43 -9
  35. data/ext/sources/examples/common-ggml.cpp +2 -0
  36. data/ext/sources/examples/common-whisper.cpp +139 -67
  37. data/ext/sources/examples/common-whisper.h +11 -0
  38. data/ext/sources/examples/ffmpeg-transcode.cpp +211 -341
  39. data/ext/sources/examples/parakeet-cli/CMakeLists.txt +8 -0
  40. data/ext/sources/examples/parakeet-cli/parakeet-cli.cpp +243 -0
  41. data/ext/sources/examples/parakeet-quantize/CMakeLists.txt +7 -0
  42. data/ext/sources/examples/parakeet-quantize/parakeet-quantize.cpp +230 -0
  43. data/ext/sources/examples/server/server.cpp +199 -163
  44. data/ext/sources/ggml/CMakeLists.txt +21 -13
  45. data/ext/sources/ggml/cmake/FindNCCL.cmake +36 -0
  46. data/ext/sources/ggml/cmake/ggml-config.cmake.in +12 -2
  47. data/ext/sources/ggml/include/ggml-alloc.h +1 -0
  48. data/ext/sources/ggml/include/ggml-backend.h +72 -10
  49. data/ext/sources/ggml/include/ggml-cuda.h +3 -0
  50. data/ext/sources/ggml/include/ggml-rpc.h +3 -3
  51. data/ext/sources/ggml/include/ggml.h +101 -9
  52. data/ext/sources/ggml/include/gguf.h +10 -2
  53. data/ext/sources/ggml/src/CMakeLists.txt +22 -5
  54. data/ext/sources/ggml/src/ggml-alloc.c +5 -1
  55. data/ext/sources/ggml/src/ggml-backend-impl.h +22 -2
  56. data/ext/sources/ggml/src/ggml-backend-meta.cpp +2263 -0
  57. data/ext/sources/ggml/src/ggml-backend-reg.cpp +12 -0
  58. data/ext/sources/ggml/src/ggml-backend.cpp +110 -9
  59. data/ext/sources/ggml/src/ggml-blas/ggml-blas.cpp +4 -0
  60. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.cpp +672 -257
  61. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.h +71 -0
  62. data/ext/sources/ggml/src/ggml-cann/common.h +20 -10
  63. data/ext/sources/ggml/src/ggml-cann/ggml-cann.cpp +211 -30
  64. data/ext/sources/ggml/src/ggml-common.h +11 -0
  65. data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +58 -29
  66. data/ext/sources/ggml/src/ggml-cpu/amx/amx.cpp +2 -0
  67. data/ext/sources/ggml/src/ggml-cpu/amx/mmq.cpp +16 -16
  68. data/ext/sources/ggml/src/ggml-cpu/arch/arm/quants.c +116 -7
  69. data/ext/sources/ggml/src/ggml-cpu/arch/arm/repack.cpp +65 -0
  70. data/ext/sources/ggml/src/ggml-cpu/arch/loongarch/quants.c +151 -1
  71. data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/quants.c +0 -1
  72. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/quants.c +4279 -1292
  73. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/repack.cpp +5 -35
  74. data/ext/sources/ggml/src/ggml-cpu/arch/s390/quants.c +0 -1
  75. data/ext/sources/ggml/src/ggml-cpu/arch/wasm/quants.c +72 -1
  76. data/ext/sources/ggml/src/ggml-cpu/arch/x86/quants.c +177 -27
  77. data/ext/sources/ggml/src/ggml-cpu/arch/x86/repack.cpp +1 -1
  78. data/ext/sources/ggml/src/ggml-cpu/arch-fallback.h +5 -0
  79. data/ext/sources/ggml/src/ggml-cpu/cmake/FindSMTIME.cmake +32 -0
  80. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-impl.h +10 -0
  81. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +95 -5
  82. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.cpp +2 -0
  83. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +146 -134
  84. data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +88 -70
  85. data/ext/sources/ggml/src/ggml-cpu/ops.cpp +372 -73
  86. data/ext/sources/ggml/src/ggml-cpu/ops.h +3 -0
  87. data/ext/sources/ggml/src/ggml-cpu/quants.c +55 -0
  88. data/ext/sources/ggml/src/ggml-cpu/quants.h +3 -0
  89. data/ext/sources/ggml/src/ggml-cpu/repack.cpp +3 -0
  90. data/ext/sources/ggml/src/ggml-cpu/simd-gemm.h +90 -0
  91. data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +3 -16
  92. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.cpp +1402 -687
  93. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.h +8 -0
  94. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +597 -2766
  95. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime2_kernels.cpp +5768 -0
  96. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_env.cpp +320 -0
  97. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_env.h +55 -0
  98. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_kernels.h +182 -19
  99. data/ext/sources/ggml/src/ggml-cpu/spacemit/repack.cpp +1795 -0
  100. data/ext/sources/ggml/src/ggml-cpu/spacemit/repack.h +14 -0
  101. data/ext/sources/ggml/src/ggml-cpu/spacemit/rvv_kernels.cpp +3178 -0
  102. data/ext/sources/ggml/src/ggml-cpu/spacemit/rvv_kernels.h +95 -0
  103. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_barrier.h +34 -0
  104. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_mem_pool.cpp +760 -0
  105. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_mem_pool.h +32 -0
  106. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_tcm.h +409 -0
  107. data/ext/sources/ggml/src/ggml-cpu/vec.cpp +37 -53
  108. data/ext/sources/ggml/src/ggml-cpu/vec.h +225 -240
  109. data/ext/sources/ggml/src/ggml-cuda/CMakeLists.txt +17 -7
  110. data/ext/sources/ggml/src/ggml-cuda/allreduce.cu +971 -0
  111. data/ext/sources/ggml/src/ggml-cuda/allreduce.cuh +29 -0
  112. data/ext/sources/ggml/src/ggml-cuda/argsort.cu +62 -26
  113. data/ext/sources/ggml/src/ggml-cuda/binbcast.cu +44 -18
  114. data/ext/sources/ggml/src/ggml-cuda/binbcast.cuh +1 -0
  115. data/ext/sources/ggml/src/ggml-cuda/common.cuh +242 -28
  116. data/ext/sources/ggml/src/ggml-cuda/concat.cu +120 -114
  117. data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cu +45 -21
  118. data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cuh +1 -0
  119. data/ext/sources/ggml/src/ggml-cuda/convert.cu +53 -0
  120. data/ext/sources/ggml/src/ggml-cuda/convert.cuh +10 -0
  121. data/ext/sources/ggml/src/ggml-cuda/cpy.cu +14 -6
  122. data/ext/sources/ggml/src/ggml-cuda/dequantize.cuh +22 -0
  123. data/ext/sources/ggml/src/ggml-cuda/fattn-common.cuh +278 -44
  124. data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +331 -130
  125. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cu +12 -0
  126. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cuh +126 -27
  127. data/ext/sources/ggml/src/ggml-cuda/fattn-vec.cuh +40 -15
  128. data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cu +18 -9
  129. data/ext/sources/ggml/src/ggml-cuda/fattn.cu +152 -49
  130. data/ext/sources/ggml/src/ggml-cuda/fattn.cuh +2 -0
  131. data/ext/sources/ggml/src/ggml-cuda/fwht.cu +101 -0
  132. data/ext/sources/ggml/src/ggml-cuda/fwht.cuh +4 -0
  133. data/ext/sources/ggml/src/ggml-cuda/gated_delta_net.cu +84 -35
  134. data/ext/sources/ggml/src/ggml-cuda/getrows.cu +34 -12
  135. data/ext/sources/ggml/src/ggml-cuda/ggml-cuda.cu +1069 -609
  136. data/ext/sources/ggml/src/ggml-cuda/im2col.cu +32 -29
  137. data/ext/sources/ggml/src/ggml-cuda/mean.cu +4 -2
  138. data/ext/sources/ggml/src/ggml-cuda/mma.cuh +242 -195
  139. data/ext/sources/ggml/src/ggml-cuda/mmf.cuh +3 -3
  140. data/ext/sources/ggml/src/ggml-cuda/mmq.cu +18 -12
  141. data/ext/sources/ggml/src/ggml-cuda/mmq.cuh +502 -423
  142. data/ext/sources/ggml/src/ggml-cuda/mmvf.cu +19 -12
  143. data/ext/sources/ggml/src/ggml-cuda/mmvq.cu +485 -57
  144. data/ext/sources/ggml/src/ggml-cuda/mmvq.cuh +6 -1
  145. data/ext/sources/ggml/src/ggml-cuda/norm.cu +36 -10
  146. data/ext/sources/ggml/src/ggml-cuda/out-prod.cu +23 -7
  147. data/ext/sources/ggml/src/ggml-cuda/quantize.cu +133 -26
  148. data/ext/sources/ggml/src/ggml-cuda/quantize.cuh +1 -1
  149. data/ext/sources/ggml/src/ggml-cuda/reduce_rows.cuh +5 -1
  150. data/ext/sources/ggml/src/ggml-cuda/rope.cu +11 -4
  151. data/ext/sources/ggml/src/ggml-cuda/scale.cu +4 -1
  152. data/ext/sources/ggml/src/ggml-cuda/set-rows.cu +14 -6
  153. data/ext/sources/ggml/src/ggml-cuda/snake.cu +72 -0
  154. data/ext/sources/ggml/src/ggml-cuda/snake.cuh +8 -0
  155. data/ext/sources/ggml/src/ggml-cuda/softcap.cu +4 -1
  156. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cu +45 -13
  157. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cuh +1 -1
  158. data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cu +40 -18
  159. data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +8 -4
  160. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_16.cu +1 -0
  161. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_32.cu +1 -0
  162. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_8.cu +2 -0
  163. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_4.cu +1 -0
  164. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_16.cu +1 -0
  165. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_32.cu +1 -0
  166. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_4.cu +1 -0
  167. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_8.cu +2 -0
  168. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_16.cu +1 -0
  169. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_4.cu +1 -0
  170. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_8.cu +2 -0
  171. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_4.cu +1 -0
  172. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_8.cu +2 -0
  173. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq192-dv128.cu +5 -0
  174. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq320-dv256.cu +5 -0
  175. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq512-dv512.cu +5 -0
  176. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-bf16.cu +7 -0
  177. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-f16.cu +7 -0
  178. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_0.cu +7 -0
  179. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_1.cu +7 -0
  180. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_0.cu +7 -0
  181. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_1.cu +7 -0
  182. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q8_0.cu +7 -0
  183. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-bf16.cu +7 -0
  184. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-bf16.cu +7 -0
  185. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-bf16.cu +7 -0
  186. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-bf16.cu +7 -0
  187. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-bf16.cu +7 -0
  188. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-bf16.cu +7 -0
  189. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-nvfp4.cu +5 -0
  190. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q1_0.cu +5 -0
  191. data/ext/sources/ggml/src/ggml-cuda/top-k.cu +5 -4
  192. data/ext/sources/ggml/src/ggml-cuda/topk-moe.cu +26 -23
  193. data/ext/sources/ggml/src/ggml-cuda/unary.cu +31 -2
  194. data/ext/sources/ggml/src/ggml-cuda/unary.cuh +2 -0
  195. data/ext/sources/ggml/src/ggml-cuda/vecdotq.cuh +80 -0
  196. data/ext/sources/ggml/src/ggml-cuda/vendors/cuda.h +7 -2
  197. data/ext/sources/ggml/src/ggml-cuda/vendors/hip.h +22 -4
  198. data/ext/sources/ggml/src/ggml-cuda/vendors/musa.h +3 -0
  199. data/ext/sources/ggml/src/ggml-hexagon/CMakeLists.txt +2 -1
  200. data/ext/sources/ggml/src/ggml-hexagon/ggml-hexagon.cpp +1428 -743
  201. data/ext/sources/ggml/src/ggml-hexagon/htp/CMakeLists.txt +45 -7
  202. data/ext/sources/ggml/src/ggml-hexagon/htp/act-ops.c +53 -84
  203. data/ext/sources/ggml/src/ggml-hexagon/htp/argsort-ops.c +25 -12
  204. data/ext/sources/ggml/src/ggml-hexagon/htp/binary-ops.c +165 -184
  205. data/ext/sources/ggml/src/ggml-hexagon/htp/cmake-toolchain.cmake +5 -5
  206. data/ext/sources/ggml/src/ggml-hexagon/htp/concat-ops.c +277 -0
  207. data/ext/sources/ggml/src/ggml-hexagon/htp/cpy-ops.c +170 -127
  208. data/ext/sources/ggml/src/ggml-hexagon/htp/cumsum-ops.c +270 -0
  209. data/ext/sources/ggml/src/ggml-hexagon/htp/diag-ops.c +216 -0
  210. data/ext/sources/ggml/src/ggml-hexagon/htp/fill-ops.c +123 -0
  211. data/ext/sources/ggml/src/ggml-hexagon/htp/flash-attn-ops.c +125 -97
  212. data/ext/sources/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.c +1148 -0
  213. data/ext/sources/ggml/src/ggml-hexagon/htp/get-rows-ops.c +148 -42
  214. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dma.c +2 -2
  215. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dma.h +252 -62
  216. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dump.h +9 -0
  217. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-utils.h +87 -1
  218. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-flash-attn-ops.c +1878 -0
  219. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-matmul-ops.c +2066 -0
  220. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-ops.c +6 -0
  221. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-ops.h +88 -0
  222. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-profile.h +34 -0
  223. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-queue.c +158 -0
  224. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-queue.h +134 -0
  225. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-utils.h +200 -0
  226. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-ctx.h +96 -13
  227. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-ops.h +182 -57
  228. data/ext/sources/ggml/src/ggml-hexagon/htp/htp_iface.idl +9 -3
  229. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-base.h +71 -3
  230. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-copy.h +27 -10
  231. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-div.h +63 -23
  232. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-exp.h +9 -8
  233. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-flash-attn.h +47 -0
  234. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-log.h +65 -0
  235. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-pow.h +42 -0
  236. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-repl.h +74 -0
  237. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sigmoid.h +1 -0
  238. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sin-cos.h +90 -0
  239. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-utils.h +5 -8
  240. data/ext/sources/ggml/src/ggml-hexagon/htp/main.c +529 -815
  241. data/ext/sources/ggml/src/ggml-hexagon/htp/matmul-ops.c +2522 -234
  242. data/ext/sources/ggml/src/ggml-hexagon/htp/pad-ops.c +547 -0
  243. data/ext/sources/ggml/src/ggml-hexagon/htp/repeat-ops.c +148 -0
  244. data/ext/sources/ggml/src/ggml-hexagon/htp/rope-ops.c +291 -95
  245. data/ext/sources/ggml/src/ggml-hexagon/htp/set-rows-ops.c +59 -37
  246. data/ext/sources/ggml/src/ggml-hexagon/htp/softmax-ops.c +121 -133
  247. data/ext/sources/ggml/src/ggml-hexagon/htp/solve-tri-ops.c +267 -0
  248. data/ext/sources/ggml/src/ggml-hexagon/htp/ssm-conv.c +244 -151
  249. data/ext/sources/ggml/src/ggml-hexagon/htp/sum-rows-ops.c +6 -6
  250. data/ext/sources/ggml/src/ggml-hexagon/htp/unary-ops.c +719 -45
  251. data/ext/sources/ggml/src/ggml-hexagon/htp/vtcm-utils.h +16 -0
  252. data/ext/sources/ggml/src/ggml-hexagon/htp-opnode.h +272 -0
  253. data/ext/sources/ggml/src/ggml-hexagon/libggml-htp.inf +3 -1
  254. data/ext/sources/ggml/src/ggml-hip/CMakeLists.txt +22 -9
  255. data/ext/sources/ggml/src/ggml-impl.h +6 -1
  256. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.cpp +138 -13
  257. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.h +32 -1
  258. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.m +164 -28
  259. data/ext/sources/ggml/src/ggml-metal/ggml-metal-impl.h +80 -0
  260. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.cpp +190 -19
  261. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.h +2 -0
  262. data/ext/sources/ggml/src/ggml-metal/ggml-metal.cpp +39 -26
  263. data/ext/sources/ggml/src/ggml-metal/ggml-metal.metal +823 -322
  264. data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +5 -6
  265. data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +54 -5
  266. data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +12248 -5907
  267. data/ext/sources/ggml/src/ggml-opencl/kernels/concat.cl +67 -0
  268. data/ext/sources/ggml/src/ggml-opencl/kernels/cpy.cl +59 -0
  269. data/ext/sources/ggml/src/ggml-opencl/kernels/cvt.cl +1819 -112
  270. data/ext/sources/ggml/src/ggml-opencl/kernels/gated_delta_net.cl +249 -0
  271. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32_ns.cl +306 -0
  272. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_0_f32_ns.cl +256 -0
  273. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_1_f32_ns.cl +258 -0
  274. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_k_f32_ns.cl +283 -0
  275. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_0_f32_ns.cl +260 -0
  276. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_1_f32_ns.cl +262 -0
  277. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_k_f32_ns.cl +288 -0
  278. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q6_k_f32_ns.cl +267 -0
  279. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_iq4_nl_f32.cl +150 -0
  280. data/ext/sources/ggml/src/ggml-opencl/kernels/{mul_mat_Ab_Bi_8x4.cl → gemm_noshuffle_q4_0_f32.cl} +1 -1
  281. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q4_k_f32.cl +172 -0
  282. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_0_f32.cl +131 -0
  283. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_1_f32.cl +134 -0
  284. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_k_f32.cl +176 -0
  285. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q6_k_f32.cl +140 -0
  286. data/ext/sources/ggml/src/ggml-opencl/kernels/{mul_mm_q8_0_f32_8x4.cl → gemm_noshuffle_q8_0_f32.cl} +1 -1
  287. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_xmem_f16_f32_os8.cl +233 -0
  288. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_mxfp4_f32_ns.cl +165 -0
  289. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_0_f32_ns.cl +120 -0
  290. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_1_f32_ns.cl +123 -0
  291. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_k_f32_ns.cl +155 -0
  292. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_0_f32_ns.cl +123 -0
  293. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_1_f32_ns.cl +125 -0
  294. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_k_f32_ns.cl +160 -0
  295. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q6_k_f32_ns.cl +141 -0
  296. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_iq4_nl_f32.cl +302 -0
  297. data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle_general.cl → gemv_noshuffle_q4_0_f32.cl} +5 -5
  298. data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle.cl → gemv_noshuffle_q4_0_f32_spec.cl} +5 -5
  299. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q4_k_f32.cl +318 -0
  300. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_0_f32.cl +291 -0
  301. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_1_f32.cl +294 -0
  302. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_k_f32.cl +326 -0
  303. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q6_k_f32.cl +293 -0
  304. data/ext/sources/ggml/src/ggml-opencl/kernels/get_rows.cl +15 -9
  305. data/ext/sources/ggml/src/ggml-opencl/kernels/moe_reorder_b.cl +30 -0
  306. data/ext/sources/ggml/src/ggml-opencl/kernels/moe_sort_by_expert.cl +82 -0
  307. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_iq4_nl_f32_l4_lm.cl +171 -0
  308. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q4_k_f32_l4_lm.cl +179 -0
  309. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_0_f32_l4_lm.cl +173 -0
  310. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_1_f32_l4_lm.cl +175 -0
  311. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_k_f32_l4_lm.cl +192 -0
  312. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_iq4_nl_f32.cl +164 -0
  313. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_iq4_nl_f32_flat.cl +202 -0
  314. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_k_f32_flat.cl +196 -0
  315. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_0_f32.cl +241 -0
  316. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_0_f32_flat.cl +243 -0
  317. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_1_f32.cl +243 -0
  318. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_1_f32_flat.cl +247 -0
  319. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_k_f32.cl +187 -0
  320. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_k_f32_flat.cl +203 -0
  321. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q6_k_f32_flat.cl +48 -64
  322. data/ext/sources/ggml/src/ggml-openvino/ggml-decoder.cpp +15 -5
  323. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino-extra.cpp +18 -11
  324. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino.cpp +35 -13
  325. data/ext/sources/ggml/src/ggml-openvino/ggml-quants.cpp +264 -192
  326. data/ext/sources/ggml/src/ggml-openvino/openvino/op/rope.cpp +33 -7
  327. data/ext/sources/ggml/src/ggml-openvino/openvino/op/unary_gelu.cpp +25 -0
  328. data/ext/sources/ggml/src/ggml-openvino/openvino/op_table.cpp +1 -0
  329. data/ext/sources/ggml/src/ggml-openvino/openvino/op_table.h +1 -0
  330. data/ext/sources/ggml/src/ggml-openvino/openvino/rt_info/weightless_caching_attributes.hpp +41 -0
  331. data/ext/sources/ggml/src/ggml-openvino/openvino/translate_session.cpp +27 -3
  332. data/ext/sources/ggml/src/ggml-openvino/openvino/utils.cpp +67 -36
  333. data/ext/sources/ggml/src/ggml-openvino/openvino/utils.h +1 -0
  334. data/ext/sources/ggml/src/ggml-openvino/utils.cpp +101 -44
  335. data/ext/sources/ggml/src/ggml-openvino/utils.h +23 -3
  336. data/ext/sources/ggml/src/ggml-opt.cpp +1 -0
  337. data/ext/sources/ggml/src/ggml-quants.c +289 -114
  338. data/ext/sources/ggml/src/ggml-quants.h +3 -0
  339. data/ext/sources/ggml/src/ggml-rpc/CMakeLists.txt +24 -0
  340. data/ext/sources/ggml/src/ggml-rpc/ggml-rpc.cpp +167 -311
  341. data/ext/sources/ggml/src/ggml-rpc/transport.cpp +683 -0
  342. data/ext/sources/ggml/src/ggml-rpc/transport.h +34 -0
  343. data/ext/sources/ggml/src/ggml-sycl/CMakeLists.txt +50 -4
  344. data/ext/sources/ggml/src/ggml-sycl/add-id.cpp +1 -1
  345. data/ext/sources/ggml/src/ggml-sycl/backend.hpp +3 -1
  346. data/ext/sources/ggml/src/ggml-sycl/common.cpp +74 -2
  347. data/ext/sources/ggml/src/ggml-sycl/common.hpp +41 -1
  348. data/ext/sources/ggml/src/ggml-sycl/convert.cpp +115 -13
  349. data/ext/sources/ggml/src/ggml-sycl/convert.hpp +9 -0
  350. data/ext/sources/ggml/src/ggml-sycl/cumsum.cpp +148 -0
  351. data/ext/sources/ggml/src/ggml-sycl/cumsum.hpp +5 -0
  352. data/ext/sources/ggml/src/ggml-sycl/dequantize.hpp +663 -0
  353. data/ext/sources/ggml/src/ggml-sycl/diag.cpp +67 -0
  354. data/ext/sources/ggml/src/ggml-sycl/diag.hpp +5 -0
  355. data/ext/sources/ggml/src/ggml-sycl/dmmv.cpp +586 -6
  356. data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +1 -90
  357. data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +0 -2
  358. data/ext/sources/ggml/src/ggml-sycl/fattn-buffers.cpp +56 -0
  359. data/ext/sources/ggml/src/ggml-sycl/fattn-buffers.hpp +63 -0
  360. data/ext/sources/ggml/src/ggml-sycl/fattn-common.hpp +7 -5
  361. data/ext/sources/ggml/src/ggml-sycl/fattn-tile.cpp +4 -0
  362. data/ext/sources/ggml/src/ggml-sycl/fattn-tile.hpp +76 -168
  363. data/ext/sources/ggml/src/ggml-sycl/fattn-vec.hpp +7 -0
  364. data/ext/sources/ggml/src/ggml-sycl/fattn.cpp +3 -1
  365. data/ext/sources/ggml/src/ggml-sycl/fill.cpp +55 -0
  366. data/ext/sources/ggml/src/ggml-sycl/fill.hpp +5 -0
  367. data/ext/sources/ggml/src/ggml-sycl/gated_delta_net.cpp +69 -31
  368. data/ext/sources/ggml/src/ggml-sycl/gated_delta_net.hpp +1 -0
  369. data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +3 -0
  370. data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +79 -3
  371. data/ext/sources/ggml/src/ggml-sycl/ggml-sycl.cpp +823 -190
  372. data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +353 -89
  373. data/ext/sources/ggml/src/ggml-sycl/im2col.hpp +5 -3
  374. data/ext/sources/ggml/src/ggml-sycl/mmvq.cpp +1344 -26
  375. data/ext/sources/ggml/src/ggml-sycl/mmvq.hpp +16 -0
  376. data/ext/sources/ggml/src/ggml-sycl/pad.cpp +27 -27
  377. data/ext/sources/ggml/src/ggml-sycl/quants.hpp +71 -0
  378. data/ext/sources/ggml/src/ggml-sycl/set_rows.cpp +7 -1
  379. data/ext/sources/ggml/src/ggml-sycl/solve_tri.cpp +172 -0
  380. data/ext/sources/ggml/src/ggml-sycl/solve_tri.hpp +8 -0
  381. data/ext/sources/ggml/src/ggml-sycl/ssm_conv.cpp +6 -1
  382. data/ext/sources/ggml/src/ggml-sycl/ssm_scan.cpp +156 -0
  383. data/ext/sources/ggml/src/ggml-sycl/ssm_scan.hpp +5 -0
  384. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.cpp +62 -10
  385. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.hpp +18 -6
  386. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq512-dv512.cpp +6 -0
  387. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-f16.cpp +1 -0
  388. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q4_0.cpp +1 -0
  389. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q4_1.cpp +1 -0
  390. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q5_0.cpp +1 -0
  391. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q5_1.cpp +1 -0
  392. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q8_0.cpp +1 -0
  393. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-f16.cpp +1 -0
  394. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q4_0.cpp +1 -0
  395. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q4_1.cpp +1 -0
  396. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q5_0.cpp +1 -0
  397. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q5_1.cpp +1 -0
  398. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q8_0.cpp +1 -0
  399. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-f16.cpp +1 -0
  400. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q4_0.cpp +1 -0
  401. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q4_1.cpp +1 -0
  402. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q5_0.cpp +1 -0
  403. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q5_1.cpp +1 -0
  404. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q8_0.cpp +1 -0
  405. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-f16.cpp +1 -0
  406. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q4_0.cpp +1 -0
  407. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q4_1.cpp +1 -0
  408. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q5_0.cpp +1 -0
  409. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q5_1.cpp +1 -0
  410. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q8_0.cpp +1 -0
  411. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-f16.cpp +1 -0
  412. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q4_0.cpp +1 -0
  413. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q4_1.cpp +1 -0
  414. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q5_0.cpp +1 -0
  415. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q5_1.cpp +1 -0
  416. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q8_0.cpp +1 -0
  417. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-f16.cpp +1 -0
  418. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q4_0.cpp +1 -0
  419. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q4_1.cpp +1 -0
  420. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q5_0.cpp +1 -0
  421. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q5_1.cpp +1 -0
  422. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q8_0.cpp +1 -0
  423. data/ext/sources/ggml/src/ggml-sycl/type.hpp +112 -0
  424. data/ext/sources/ggml/src/ggml-sycl/upscale.cpp +410 -0
  425. data/ext/sources/ggml/src/ggml-sycl/upscale.hpp +9 -0
  426. data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +215 -53
  427. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-buffer.cpp +4 -0
  428. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-device.cpp +2 -0
  429. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend.cpp +2 -0
  430. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-shm.cpp +1 -0
  431. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu.cpp +1 -0
  432. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu.h +0 -2
  433. data/ext/sources/ggml/src/ggml-vulkan/CMakeLists.txt +11 -0
  434. data/ext/sources/ggml/src/ggml-vulkan/ggml-vulkan.cpp +2060 -535
  435. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +4 -0
  436. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/contig_copy.comp +6 -2
  437. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp +146 -13
  438. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy.comp +3 -1
  439. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +1 -1
  440. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +25 -1
  441. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.glsl +88 -0
  442. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.glsl +643 -1
  443. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_nvfp4.comp +32 -0
  444. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q1_0.comp +29 -0
  445. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/diag.comp +0 -1
  446. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dot_product_funcs.glsl +27 -0
  447. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/exp.comp +0 -1
  448. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/feature-tests/coopmat2_decode_vector.comp +7 -0
  449. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +197 -48
  450. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl +60 -59
  451. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +115 -113
  452. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +122 -31
  453. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_dequant.glsl +131 -0
  454. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_mmq_funcs.glsl +203 -0
  455. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/fwht.comp +115 -0
  456. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net.comp +125 -64
  457. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.glsl +0 -1
  458. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_head.glsl +10 -1
  459. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_main.glsl +16 -6
  460. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +76 -54
  461. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col_3d.comp +0 -1
  462. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/log.comp +0 -1
  463. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec.comp +122 -27
  464. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iface.glsl +6 -6
  465. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q2_k.comp +1 -1
  466. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q4_k.comp +1 -1
  467. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q5_k.comp +1 -1
  468. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq.comp +1 -0
  469. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq_funcs.glsl +88 -55
  470. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +11 -17
  471. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +43 -10
  472. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.glsl +159 -125
  473. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.glsl +8 -8
  474. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_shmem_types.glsl +24 -9
  475. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/multi_add.comp +0 -1
  476. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_funcs.glsl +5 -2
  477. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.glsl +0 -1
  478. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_params.glsl +3 -2
  479. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/snake.comp +49 -0
  480. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/ssm_conv.comp +11 -1
  481. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/tri.comp +0 -1
  482. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/types.glsl +79 -2
  483. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +171 -147
  484. data/ext/sources/ggml/src/ggml-webgpu/CMakeLists.txt +5 -2
  485. data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp +2202 -283
  486. data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu.cpp +2610 -1403
  487. data/ext/sources/ggml/src/ggml-webgpu/pre_wgsl.hpp +37 -7
  488. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/add_id.wgsl +64 -0
  489. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/binary.wgsl +8 -7
  490. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/common_decls.tmpl +76 -95
  491. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/concat.wgsl +19 -1
  492. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/conv2d.wgsl +165 -0
  493. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{cpy.tmpl.wgsl → cpy.wgsl} +25 -50
  494. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn.wgsl +107 -184
  495. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_quant_staging.tmpl +124 -0
  496. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_tile.wgsl +397 -0
  497. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_blk.wgsl +101 -0
  498. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_reduce.wgsl +84 -0
  499. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_split.wgsl +619 -0
  500. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/gated_delta_net.wgsl +149 -0
  501. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/get_rows.wgsl +183 -78
  502. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/glu.wgsl +155 -0
  503. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/im2col.wgsl +101 -0
  504. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_decls.tmpl +655 -495
  505. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id.wgsl +195 -0
  506. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id_gather.wgsl +52 -0
  507. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id_vec.wgsl +154 -0
  508. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_reg_tile.wgsl +8 -6
  509. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_subgroup_matrix.wgsl +5 -1
  510. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec.wgsl +80 -409
  511. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec_acc.tmpl +1432 -0
  512. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec_q_acc.tmpl +303 -0
  513. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/quant_inner_loops.tmpl +21 -0
  514. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/quantize_q8.wgsl +173 -0
  515. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm_mul.wgsl +152 -0
  516. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{rope.tmpl.wgsl → rope.wgsl} +71 -142
  517. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/row_norm.wgsl +153 -0
  518. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/scale.wgsl +6 -4
  519. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set.wgsl +109 -0
  520. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.wgsl +2 -3
  521. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows_quant.wgsl +224 -0
  522. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{soft_max.tmpl.wgsl → soft_max.wgsl} +106 -206
  523. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/solve_tri.wgsl +121 -0
  524. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/ssm_conv.wgsl +65 -0
  525. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/ssm_scan.wgsl +193 -0
  526. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/unary.wgsl +68 -48
  527. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/upscale.wgsl +240 -0
  528. data/ext/sources/ggml/src/ggml-zdnn/ggml-zdnn.cpp +18 -14
  529. data/ext/sources/ggml/src/ggml-zendnn/CMakeLists.txt +1 -1
  530. data/ext/sources/ggml/src/ggml-zendnn/ggml-zendnn.cpp +244 -10
  531. data/ext/sources/ggml/src/ggml.c +110 -28
  532. data/ext/sources/ggml/src/gguf.cpp +173 -28
  533. data/ext/sources/include/parakeet.h +342 -0
  534. data/ext/sources/include/whisper.h +10 -0
  535. data/ext/sources/media/matmul.png +0 -0
  536. data/ext/sources/src/CMakeLists.txt +23 -0
  537. data/ext/sources/src/parakeet-arch.h +188 -0
  538. data/ext/sources/src/parakeet.cpp +3838 -0
  539. data/ext/sources/src/whisper.cpp +56 -12
  540. data/extsources.rb +26 -10
  541. data/lib/whisper/log_settable.rb +36 -0
  542. data/lib/whisper/model/uri.rb +13 -1
  543. data/lib/whisper/output.rb +74 -0
  544. data/sig/whisper.rbs +411 -62
  545. data/test/helper.rb +2 -0
  546. data/test/jfk_reader/jfk_reader.c +50 -7
  547. data/test/test_callback.rb +1 -0
  548. data/test/test_package.rb +6 -5
  549. data/test/test_parakeet.rb +28 -0
  550. data/test/test_parakeet_callback.rb +107 -0
  551. data/test/test_parakeet_context.rb +116 -0
  552. data/test/test_parakeet_context_params.rb +24 -0
  553. data/test/test_parakeet_model.rb +21 -0
  554. data/test/test_parakeet_params.rb +78 -0
  555. data/test/test_parakeet_segment.rb +42 -0
  556. data/test/test_parakeet_token.rb +73 -0
  557. data/test/test_params.rb +2 -0
  558. data/test/test_vad_segment.rb +1 -1
  559. data/test/test_whisper.rb +24 -6
  560. data/whispercpp.gemspec +2 -2
  561. metadata +215 -281
  562. data/ext/sources/bindings/javascript/CMakeLists.txt +0 -41
  563. data/ext/sources/bindings/javascript/emscripten.cpp +0 -93
  564. data/ext/sources/bindings/javascript/libwhisper.worker.js +0 -1
  565. data/ext/sources/bindings/javascript/package.json +0 -26
  566. data/ext/sources/bindings/javascript/whisper.js +0 -19
  567. data/ext/sources/examples/addon.node/CMakeLists.txt +0 -31
  568. data/ext/sources/examples/addon.node/__test__/whisper.spec.js +0 -133
  569. data/ext/sources/examples/addon.node/addon.cpp +0 -557
  570. data/ext/sources/examples/addon.node/index.js +0 -59
  571. data/ext/sources/examples/addon.node/package.json +0 -16
  572. data/ext/sources/examples/addon.node/vad-example.js +0 -132
  573. data/ext/sources/examples/bench.wasm/CMakeLists.txt +0 -49
  574. data/ext/sources/examples/bench.wasm/emscripten.cpp +0 -87
  575. data/ext/sources/examples/bench.wasm/index-tmpl.html +0 -285
  576. data/ext/sources/examples/coi-serviceworker.js +0 -146
  577. data/ext/sources/examples/command/CMakeLists.txt +0 -10
  578. data/ext/sources/examples/command/command.cpp +0 -802
  579. data/ext/sources/examples/command/commands.txt +0 -9
  580. data/ext/sources/examples/command.wasm/CMakeLists.txt +0 -50
  581. data/ext/sources/examples/command.wasm/emscripten.cpp +0 -327
  582. data/ext/sources/examples/command.wasm/index-tmpl.html +0 -415
  583. data/ext/sources/examples/generate-karaoke.sh +0 -57
  584. data/ext/sources/examples/helpers.js +0 -191
  585. data/ext/sources/examples/livestream.sh +0 -112
  586. data/ext/sources/examples/lsp/CMakeLists.txt +0 -10
  587. data/ext/sources/examples/lsp/lsp.cpp +0 -471
  588. data/ext/sources/examples/lsp/whisper.vim +0 -362
  589. data/ext/sources/examples/python/test_whisper_processor.py +0 -7
  590. data/ext/sources/examples/python/whisper_processor.py +0 -54
  591. data/ext/sources/examples/server/bench.js +0 -29
  592. data/ext/sources/examples/server.py +0 -120
  593. data/ext/sources/examples/stream/CMakeLists.txt +0 -10
  594. data/ext/sources/examples/stream/stream.cpp +0 -437
  595. data/ext/sources/examples/stream.wasm/CMakeLists.txt +0 -49
  596. data/ext/sources/examples/stream.wasm/emscripten.cpp +0 -216
  597. data/ext/sources/examples/stream.wasm/index-tmpl.html +0 -491
  598. data/ext/sources/examples/sycl/CMakeLists.txt +0 -9
  599. data/ext/sources/examples/sycl/build.sh +0 -22
  600. data/ext/sources/examples/sycl/ls-sycl-device.cpp +0 -11
  601. data/ext/sources/examples/sycl/run-whisper.sh +0 -17
  602. data/ext/sources/examples/talk-llama/CMakeLists.txt +0 -48
  603. data/ext/sources/examples/talk-llama/eleven-labs.py +0 -80
  604. data/ext/sources/examples/talk-llama/llama-adapter.cpp +0 -488
  605. data/ext/sources/examples/talk-llama/llama-adapter.h +0 -89
  606. data/ext/sources/examples/talk-llama/llama-arch.cpp +0 -2877
  607. data/ext/sources/examples/talk-llama/llama-arch.h +0 -628
  608. data/ext/sources/examples/talk-llama/llama-batch.cpp +0 -919
  609. data/ext/sources/examples/talk-llama/llama-batch.h +0 -173
  610. data/ext/sources/examples/talk-llama/llama-chat.cpp +0 -896
  611. data/ext/sources/examples/talk-llama/llama-chat.h +0 -71
  612. data/ext/sources/examples/talk-llama/llama-context.cpp +0 -3633
  613. data/ext/sources/examples/talk-llama/llama-context.h +0 -359
  614. data/ext/sources/examples/talk-llama/llama-cparams.cpp +0 -5
  615. data/ext/sources/examples/talk-llama/llama-cparams.h +0 -47
  616. data/ext/sources/examples/talk-llama/llama-ext.h +0 -12
  617. data/ext/sources/examples/talk-llama/llama-grammar.cpp +0 -1464
  618. data/ext/sources/examples/talk-llama/llama-grammar.h +0 -194
  619. data/ext/sources/examples/talk-llama/llama-graph.cpp +0 -2735
  620. data/ext/sources/examples/talk-llama/llama-graph.h +0 -1031
  621. data/ext/sources/examples/talk-llama/llama-hparams.cpp +0 -258
  622. data/ext/sources/examples/talk-llama/llama-hparams.h +0 -353
  623. data/ext/sources/examples/talk-llama/llama-impl.cpp +0 -171
  624. data/ext/sources/examples/talk-llama/llama-impl.h +0 -75
  625. data/ext/sources/examples/talk-llama/llama-io.cpp +0 -15
  626. data/ext/sources/examples/talk-llama/llama-io.h +0 -35
  627. data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.cpp +0 -330
  628. data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.h +0 -137
  629. data/ext/sources/examples/talk-llama/llama-kv-cache.cpp +0 -2285
  630. data/ext/sources/examples/talk-llama/llama-kv-cache.h +0 -389
  631. data/ext/sources/examples/talk-llama/llama-kv-cells.h +0 -533
  632. data/ext/sources/examples/talk-llama/llama-memory-hybrid-iswa.cpp +0 -275
  633. data/ext/sources/examples/talk-llama/llama-memory-hybrid-iswa.h +0 -140
  634. data/ext/sources/examples/talk-llama/llama-memory-hybrid.cpp +0 -268
  635. data/ext/sources/examples/talk-llama/llama-memory-hybrid.h +0 -139
  636. data/ext/sources/examples/talk-llama/llama-memory-recurrent.cpp +0 -1165
  637. data/ext/sources/examples/talk-llama/llama-memory-recurrent.h +0 -182
  638. data/ext/sources/examples/talk-llama/llama-memory.cpp +0 -59
  639. data/ext/sources/examples/talk-llama/llama-memory.h +0 -122
  640. data/ext/sources/examples/talk-llama/llama-mmap.cpp +0 -752
  641. data/ext/sources/examples/talk-llama/llama-mmap.h +0 -73
  642. data/ext/sources/examples/talk-llama/llama-model-loader.cpp +0 -1655
  643. data/ext/sources/examples/talk-llama/llama-model-loader.h +0 -206
  644. data/ext/sources/examples/talk-llama/llama-model-saver.cpp +0 -299
  645. data/ext/sources/examples/talk-llama/llama-model-saver.h +0 -40
  646. data/ext/sources/examples/talk-llama/llama-model.cpp +0 -9056
  647. data/ext/sources/examples/talk-llama/llama-model.h +0 -597
  648. data/ext/sources/examples/talk-llama/llama-quant.cpp +0 -1304
  649. data/ext/sources/examples/talk-llama/llama-quant.h +0 -1
  650. data/ext/sources/examples/talk-llama/llama-sampler.cpp +0 -3885
  651. data/ext/sources/examples/talk-llama/llama-sampler.h +0 -42
  652. data/ext/sources/examples/talk-llama/llama-vocab.cpp +0 -3970
  653. data/ext/sources/examples/talk-llama/llama-vocab.h +0 -187
  654. data/ext/sources/examples/talk-llama/llama.cpp +0 -1194
  655. data/ext/sources/examples/talk-llama/llama.h +0 -1573
  656. data/ext/sources/examples/talk-llama/models/afmoe.cpp +0 -190
  657. data/ext/sources/examples/talk-llama/models/apertus.cpp +0 -125
  658. data/ext/sources/examples/talk-llama/models/arcee.cpp +0 -135
  659. data/ext/sources/examples/talk-llama/models/arctic.cpp +0 -137
  660. data/ext/sources/examples/talk-llama/models/arwkv7.cpp +0 -86
  661. data/ext/sources/examples/talk-llama/models/baichuan.cpp +0 -123
  662. data/ext/sources/examples/talk-llama/models/bailingmoe.cpp +0 -143
  663. data/ext/sources/examples/talk-llama/models/bailingmoe2.cpp +0 -133
  664. data/ext/sources/examples/talk-llama/models/bert.cpp +0 -184
  665. data/ext/sources/examples/talk-llama/models/bitnet.cpp +0 -145
  666. data/ext/sources/examples/talk-llama/models/bloom.cpp +0 -101
  667. data/ext/sources/examples/talk-llama/models/chameleon.cpp +0 -178
  668. data/ext/sources/examples/talk-llama/models/chatglm.cpp +0 -132
  669. data/ext/sources/examples/talk-llama/models/codeshell.cpp +0 -111
  670. data/ext/sources/examples/talk-llama/models/cogvlm.cpp +0 -102
  671. data/ext/sources/examples/talk-llama/models/cohere2-iswa.cpp +0 -134
  672. data/ext/sources/examples/talk-llama/models/command-r.cpp +0 -122
  673. data/ext/sources/examples/talk-llama/models/dbrx.cpp +0 -122
  674. data/ext/sources/examples/talk-llama/models/deci.cpp +0 -135
  675. data/ext/sources/examples/talk-llama/models/deepseek.cpp +0 -142
  676. data/ext/sources/examples/talk-llama/models/deepseek2.cpp +0 -262
  677. data/ext/sources/examples/talk-llama/models/delta-net-base.cpp +0 -445
  678. data/ext/sources/examples/talk-llama/models/dots1.cpp +0 -132
  679. data/ext/sources/examples/talk-llama/models/dream.cpp +0 -105
  680. data/ext/sources/examples/talk-llama/models/ernie4-5-moe.cpp +0 -148
  681. data/ext/sources/examples/talk-llama/models/ernie4-5.cpp +0 -110
  682. data/ext/sources/examples/talk-llama/models/eurobert.cpp +0 -97
  683. data/ext/sources/examples/talk-llama/models/exaone-moe.cpp +0 -145
  684. data/ext/sources/examples/talk-llama/models/exaone.cpp +0 -114
  685. data/ext/sources/examples/talk-llama/models/exaone4.cpp +0 -123
  686. data/ext/sources/examples/talk-llama/models/falcon-h1.cpp +0 -111
  687. data/ext/sources/examples/talk-llama/models/falcon.cpp +0 -120
  688. data/ext/sources/examples/talk-llama/models/gemma-embedding.cpp +0 -116
  689. data/ext/sources/examples/talk-llama/models/gemma.cpp +0 -112
  690. data/ext/sources/examples/talk-llama/models/gemma2-iswa.cpp +0 -128
  691. data/ext/sources/examples/talk-llama/models/gemma3.cpp +0 -155
  692. data/ext/sources/examples/talk-llama/models/gemma3n-iswa.cpp +0 -384
  693. data/ext/sources/examples/talk-llama/models/glm4-moe.cpp +0 -170
  694. data/ext/sources/examples/talk-llama/models/glm4.cpp +0 -157
  695. data/ext/sources/examples/talk-llama/models/gpt2.cpp +0 -105
  696. data/ext/sources/examples/talk-llama/models/gptneox.cpp +0 -144
  697. data/ext/sources/examples/talk-llama/models/granite-hybrid.cpp +0 -195
  698. data/ext/sources/examples/talk-llama/models/granite.cpp +0 -210
  699. data/ext/sources/examples/talk-llama/models/grok.cpp +0 -159
  700. data/ext/sources/examples/talk-llama/models/grovemoe.cpp +0 -139
  701. data/ext/sources/examples/talk-llama/models/hunyuan-dense.cpp +0 -132
  702. data/ext/sources/examples/talk-llama/models/hunyuan-moe.cpp +0 -153
  703. data/ext/sources/examples/talk-llama/models/internlm2.cpp +0 -120
  704. data/ext/sources/examples/talk-llama/models/jais.cpp +0 -86
  705. data/ext/sources/examples/talk-llama/models/jais2.cpp +0 -123
  706. data/ext/sources/examples/talk-llama/models/jamba.cpp +0 -106
  707. data/ext/sources/examples/talk-llama/models/kimi-linear.cpp +0 -381
  708. data/ext/sources/examples/talk-llama/models/lfm2.cpp +0 -196
  709. data/ext/sources/examples/talk-llama/models/llada-moe.cpp +0 -122
  710. data/ext/sources/examples/talk-llama/models/llada.cpp +0 -99
  711. data/ext/sources/examples/talk-llama/models/llama-iswa.cpp +0 -178
  712. data/ext/sources/examples/talk-llama/models/llama.cpp +0 -175
  713. data/ext/sources/examples/talk-llama/models/maincoder.cpp +0 -117
  714. data/ext/sources/examples/talk-llama/models/mamba-base.cpp +0 -289
  715. data/ext/sources/examples/talk-llama/models/mamba.cpp +0 -54
  716. data/ext/sources/examples/talk-llama/models/mimo2-iswa.cpp +0 -129
  717. data/ext/sources/examples/talk-llama/models/minicpm3.cpp +0 -200
  718. data/ext/sources/examples/talk-llama/models/minimax-m2.cpp +0 -123
  719. data/ext/sources/examples/talk-llama/models/mistral3.cpp +0 -160
  720. data/ext/sources/examples/talk-llama/models/models.h +0 -704
  721. data/ext/sources/examples/talk-llama/models/modern-bert.cpp +0 -109
  722. data/ext/sources/examples/talk-llama/models/mpt.cpp +0 -126
  723. data/ext/sources/examples/talk-llama/models/nemotron-h.cpp +0 -162
  724. data/ext/sources/examples/talk-llama/models/nemotron.cpp +0 -122
  725. data/ext/sources/examples/talk-llama/models/neo-bert.cpp +0 -104
  726. data/ext/sources/examples/talk-llama/models/olmo.cpp +0 -121
  727. data/ext/sources/examples/talk-llama/models/olmo2.cpp +0 -150
  728. data/ext/sources/examples/talk-llama/models/olmoe.cpp +0 -124
  729. data/ext/sources/examples/talk-llama/models/openai-moe-iswa.cpp +0 -127
  730. data/ext/sources/examples/talk-llama/models/openelm.cpp +0 -124
  731. data/ext/sources/examples/talk-llama/models/orion.cpp +0 -123
  732. data/ext/sources/examples/talk-llama/models/paddleocr.cpp +0 -122
  733. data/ext/sources/examples/talk-llama/models/pangu-embedded.cpp +0 -121
  734. data/ext/sources/examples/talk-llama/models/phi2.cpp +0 -121
  735. data/ext/sources/examples/talk-llama/models/phi3.cpp +0 -152
  736. data/ext/sources/examples/talk-llama/models/plamo.cpp +0 -110
  737. data/ext/sources/examples/talk-llama/models/plamo2.cpp +0 -320
  738. data/ext/sources/examples/talk-llama/models/plamo3.cpp +0 -128
  739. data/ext/sources/examples/talk-llama/models/plm.cpp +0 -169
  740. data/ext/sources/examples/talk-llama/models/qwen.cpp +0 -108
  741. data/ext/sources/examples/talk-llama/models/qwen2.cpp +0 -126
  742. data/ext/sources/examples/talk-llama/models/qwen2moe.cpp +0 -151
  743. data/ext/sources/examples/talk-llama/models/qwen2vl.cpp +0 -117
  744. data/ext/sources/examples/talk-llama/models/qwen3.cpp +0 -120
  745. data/ext/sources/examples/talk-llama/models/qwen35.cpp +0 -381
  746. data/ext/sources/examples/talk-llama/models/qwen35moe.cpp +0 -422
  747. data/ext/sources/examples/talk-llama/models/qwen3moe.cpp +0 -131
  748. data/ext/sources/examples/talk-llama/models/qwen3next.cpp +0 -525
  749. data/ext/sources/examples/talk-llama/models/qwen3vl-moe.cpp +0 -140
  750. data/ext/sources/examples/talk-llama/models/qwen3vl.cpp +0 -132
  751. data/ext/sources/examples/talk-llama/models/refact.cpp +0 -94
  752. data/ext/sources/examples/talk-llama/models/rnd1.cpp +0 -126
  753. data/ext/sources/examples/talk-llama/models/rwkv6-base.cpp +0 -164
  754. data/ext/sources/examples/talk-llama/models/rwkv6.cpp +0 -94
  755. data/ext/sources/examples/talk-llama/models/rwkv6qwen2.cpp +0 -86
  756. data/ext/sources/examples/talk-llama/models/rwkv7-base.cpp +0 -137
  757. data/ext/sources/examples/talk-llama/models/rwkv7.cpp +0 -90
  758. data/ext/sources/examples/talk-llama/models/seed-oss.cpp +0 -124
  759. data/ext/sources/examples/talk-llama/models/smallthinker.cpp +0 -126
  760. data/ext/sources/examples/talk-llama/models/smollm3.cpp +0 -128
  761. data/ext/sources/examples/talk-llama/models/stablelm.cpp +0 -146
  762. data/ext/sources/examples/talk-llama/models/starcoder.cpp +0 -100
  763. data/ext/sources/examples/talk-llama/models/starcoder2.cpp +0 -121
  764. data/ext/sources/examples/talk-llama/models/step35-iswa.cpp +0 -165
  765. data/ext/sources/examples/talk-llama/models/t5-dec.cpp +0 -166
  766. data/ext/sources/examples/talk-llama/models/t5-enc.cpp +0 -96
  767. data/ext/sources/examples/talk-llama/models/wavtokenizer-dec.cpp +0 -149
  768. data/ext/sources/examples/talk-llama/models/xverse.cpp +0 -108
  769. data/ext/sources/examples/talk-llama/prompts/talk-alpaca.txt +0 -23
  770. data/ext/sources/examples/talk-llama/speak +0 -40
  771. data/ext/sources/examples/talk-llama/speak.bat +0 -1
  772. data/ext/sources/examples/talk-llama/speak.ps1 +0 -14
  773. data/ext/sources/examples/talk-llama/talk-llama.cpp +0 -813
  774. data/ext/sources/examples/talk-llama/unicode-data.cpp +0 -7034
  775. data/ext/sources/examples/talk-llama/unicode-data.h +0 -20
  776. data/ext/sources/examples/talk-llama/unicode.cpp +0 -1103
  777. data/ext/sources/examples/talk-llama/unicode.h +0 -111
  778. data/ext/sources/examples/wchess/CMakeLists.txt +0 -10
  779. data/ext/sources/examples/wchess/libwchess/CMakeLists.txt +0 -19
  780. data/ext/sources/examples/wchess/libwchess/Chessboard.cpp +0 -803
  781. data/ext/sources/examples/wchess/libwchess/Chessboard.h +0 -33
  782. data/ext/sources/examples/wchess/libwchess/WChess.cpp +0 -193
  783. data/ext/sources/examples/wchess/libwchess/WChess.h +0 -63
  784. data/ext/sources/examples/wchess/libwchess/test-chessboard.cpp +0 -117
  785. data/ext/sources/examples/wchess/wchess.cmd/CMakeLists.txt +0 -8
  786. data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +0 -253
  787. data/ext/sources/examples/whisper.wasm/CMakeLists.txt +0 -50
  788. data/ext/sources/examples/whisper.wasm/emscripten.cpp +0 -118
  789. data/ext/sources/examples/whisper.wasm/index-tmpl.html +0 -659
  790. data/ext/sources/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +0 -99
  791. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-msg.h +0 -155
  792. data/ext/sources/ggml/src/ggml-hexagon/op-desc.h +0 -153
  793. data/ext/sources/ggml/src/ggml-opencl/kernels/embed_kernel.py +0 -26
  794. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/eliminate_zp.cpp +0 -123
  795. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/eliminate_zp.h +0 -17
  796. data/ext/sources/ggml/src/ggml-virtgpu/regenerate_remoting.py +0 -333
  797. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rte.glsl +0 -5
  798. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +0 -182
  799. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/glu.tmpl.wgsl +0 -323
  800. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.wgsl +0 -718
  801. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm.wgsl +0 -123
  802. data/ext/sources/tests/CMakeLists.txt +0 -112
  803. data/ext/sources/tests/earnings21/eval.mk +0 -58
  804. data/ext/sources/tests/earnings21/eval.py +0 -68
  805. data/ext/sources/tests/earnings21/normalizers/__init__.py +0 -2
  806. data/ext/sources/tests/earnings21/normalizers/basic.py +0 -80
  807. data/ext/sources/tests/earnings21/normalizers/english.json +0 -1741
  808. data/ext/sources/tests/earnings21/normalizers/english.py +0 -550
  809. data/ext/sources/tests/earnings21/requirements.txt +0 -6
  810. data/ext/sources/tests/en-0-ref.txt +0 -1
  811. data/ext/sources/tests/en-1-ref.txt +0 -1
  812. data/ext/sources/tests/en-2-ref.txt +0 -1
  813. data/ext/sources/tests/es-0-ref.txt +0 -1
  814. data/ext/sources/tests/librispeech/eval.mk +0 -39
  815. data/ext/sources/tests/librispeech/eval.py +0 -47
  816. data/ext/sources/tests/librispeech/normalizers/__init__.py +0 -2
  817. data/ext/sources/tests/librispeech/normalizers/basic.py +0 -80
  818. data/ext/sources/tests/librispeech/normalizers/english.json +0 -1741
  819. data/ext/sources/tests/librispeech/normalizers/english.py +0 -550
  820. data/ext/sources/tests/librispeech/requirements.txt +0 -6
  821. data/ext/sources/tests/run-tests.sh +0 -130
  822. data/ext/sources/tests/test-c.c +0 -3
  823. data/ext/sources/tests/test-vad-full.cpp +0 -56
  824. data/ext/sources/tests/test-vad.cpp +0 -83
  825. data/ext/sources/tests/test-whisper.js +0 -58
  826. data/lib/whisper/context.rb +0 -15
  827. data/lib/whisper/segment.rb +0 -58
  828. /data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle_general_q8_0_f32.cl → gemv_noshuffle_q8_0_f32.cl} +0 -0
@@ -0,0 +1,1878 @@
1
+ // HMX-accelerated Flash Attention for prefill (neq1 >= 32).
2
+ // Ported from htp-ops-lib/src/dsp/ops/flash_attn.c, adapted to the htp/ codebase.
3
+
4
+ #pragma clang diagnostic ignored "-Wunused-variable"
5
+ #pragma clang diagnostic ignored "-Wunused-function"
6
+ #pragma clang diagnostic ignored "-Wunused-but-set-variable"
7
+
8
+ #include <assert.h>
9
+ #include <HAP_compute_res.h>
10
+ #include <HAP_farf.h>
11
+ #include <math.h>
12
+ #include <stdbool.h>
13
+ #include <stddef.h>
14
+ #include <stdint.h>
15
+ #include <string.h>
16
+
17
+ #define GGML_COMMON_DECL_C
18
+ #include "ggml-common.h"
19
+ #include "hex-dma.h"
20
+ #include "hex-fastdiv.h"
21
+ #include "hmx-profile.h"
22
+ #include "hmx-queue.h"
23
+ #include "hmx-utils.h"
24
+ #include "htp-ctx.h"
25
+ #include "htp-ops.h"
26
+ #include "hvx-dump.h"
27
+ #include "hvx-copy.h"
28
+ #include "hvx-reduce.h"
29
+ #include "hvx-utils.h"
30
+ #include "hvx-flash-attn.h"
31
+ #include "vtcm-utils.h"
32
+ #include "worker-pool.h"
33
+
34
+ // ============================================================================
35
+ // Constants
36
+ // ============================================================================
37
+
38
+ // Tile constants from hmx-utils.h
39
+ // HMX_FP16_TILE_N_ROWS = 32
40
+ // HMX_FP16_TILE_N_COLS = 32
41
+ // HMX_FP16_TILE_N_ELMS = 1024
42
+ // HMX_FP16_TILE_SIZE = 2048
43
+
44
+ // ============================================================================
45
+ // Dynamic block size computation (GQA-aware)
46
+ // ============================================================================
47
+
48
+ // Exact VTCM usage for a given (gqa_factor, DK, DV, Br, Bc) configuration.
49
+ // g_br = hex_align_up(gqa_factor * Br, 32) replaces Br for all Q/O/S/P/D dimensions.
50
+ // Layout: Q + O_ping + O_pong + K_dma*2 + V_dma*2 + K_tile + V_tile + S + P + D + vectors + scales
51
+ // Mask is DMA'd into a VTCM buffer (Br rows per KV block) to avoid DDR reads in softmax.
52
+ static size_t hmx_fa_compute_vtcm_usage(size_t gqa_factor, size_t DK, size_t DV, size_t Br, size_t Bc, size_t n_threads, bool use_pipeline) {
53
+ const size_t g_br = hex_align_up(gqa_factor * Br, HMX_FP16_TILE_N_ROWS);
54
+ const size_t q_tile_size = hex_align_up(g_br * DK * sizeof(__fp16), 4096); // Q: [g_br, DK]
55
+ const size_t o_tile_size = hex_align_up(g_br * DV * sizeof(__fp16), 4096); // O: [g_br, DV] x2 ping-pong
56
+ const size_t k_dma_size = hex_align_up(Bc * hex_round_up(DK * sizeof(__fp16), 128), 4096); // K DMA: [Bc, DK] x2 double-buf
57
+ const size_t v_dma_size = hex_align_up(Bc * hex_round_up(DV * sizeof(__fp16), 128), 4096); // V DMA: [Bc, DV] x2 double-buf
58
+ const size_t k_tile_size = hex_align_up(Bc * DK * sizeof(__fp16), 4096); // K tiles: [Bc, DK] interleaved
59
+ const size_t v_tile_size = hex_align_up(Bc * DV * sizeof(__fp16), 4096); // V tiles: [Bc, DV] interleaved
60
+ const size_t s_tile_size = hex_align_up(g_br * Bc * sizeof(__fp16), 4096); // S/P:[g_br, Bc]
61
+ const size_t d_tile_size = hex_align_up(g_br * g_br * sizeof(__fp16), 4096); // D: [g_br, g_br]
62
+ const size_t col_vec_size = hex_align_up(g_br * sizeof(__fp16), 256); // m, l, etc.
63
+ const size_t row_vec_size = hex_align_up(Bc * sizeof(__fp16), 256);
64
+ const size_t m_line_size = hex_align_up(Bc * sizeof(__fp16), 128);
65
+ const size_t m_buf_size = hex_align_up(Br * m_line_size, 4096);
66
+ const size_t slopes_size = hex_align_up(g_br * sizeof(__fp16), 128);
67
+
68
+ return q_tile_size * 1 // Q tiles
69
+ + o_tile_size * 2 // O ping-pong
70
+ + k_dma_size * 2 // K DMA x2
71
+ + v_dma_size * 2 // V DMA x2
72
+ + k_tile_size * 1 // K tiles
73
+ + v_tile_size * (use_pipeline ? 2 : 1) // V tiles (double-buffered if pipelining)
74
+ + s_tile_size * 2 // S + P
75
+ + d_tile_size * 1 // D (diagonal matrix)
76
+ + col_vec_size * 4 // m_vec, l_vec, s_rowmax, p_rowsum
77
+ + row_vec_size * 2 * n_threads // per-thread softmax row scratch
78
+ + m_buf_size * 1 // mask VTCM buffer [Br rows]
79
+ + slopes_size // Slopes
80
+ + 256 * 2; // HMX scales (id + qk)
81
+ }
82
+
83
+ // ============================================================================
84
+ // FP16 exp2 polynomial (ported from htp-ops-lib/include/dsp/hvx_math.h)
85
+ // ============================================================================
86
+ // 5th-order Horner polynomial for exp2(x) in qf16/hf16 domain. Input must be
87
+ // ≤ 0 (safe softmax invariant — overflow handling omitted). ~18 ALU ops per
88
+ // 64 fp16 lanes, fully parallel across HVX threads (no scatter/gather engine).
89
+ // Replaces the F32 round-trip (qf16→f32→exp→f32→f16, ~44 ops for 2×32 lanes).
90
+ static inline HVX_Vector hvx_exp2_hf(HVX_Vector x_v) {
91
+ const HVX_Vector zero_v = Q6_V_vzero();
92
+ const HVX_Vector half_hf_v = Q6_Vh_vsplat_R(0x3800); // fp16 0.5
93
+
94
+ // k = round_toward_neg_inf(x); f = (float)k; frac = x - f
95
+ HVX_Vector x_minus_half = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vsub_VhfVhf(x_v, half_hf_v));
96
+ HVX_Vector k_v = Q6_Vh_equals_Vhf(x_minus_half); // truncate to int16
97
+ HVX_Vector f_v = Q6_Vhf_equals_Vh(k_v); // back to fp16
98
+
99
+ HVX_Vector x_qf16 = Q6_Vqf16_vsub_VhfVhf(x_v, f_v); // fractional part in qf16
100
+
101
+ // Horner: y = ((((E5*x + E4)*x + E3)*x + E2)*x + E1)*x + E0
102
+ HVX_Vector y = Q6_Vqf16_vmpy_Vqf16Vqf16(Q6_Vh_vsplat_R(0x5082), x_qf16); // E5*x
103
+ y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x157d)); // + E4
104
+ y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
105
+ y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x20ed)); // + E3
106
+ y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
107
+ y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x2b1b)); // + E2
108
+ y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
109
+ y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x33b0)); // + E1
110
+ y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16);
111
+ y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x398c)); // + E0
112
+ y = Q6_Vqf16_vmpy_Vqf16Vqf16(y, x_qf16); // y = y * x
113
+ y = Q6_Vqf16_vadd_Vqf16Vhf(y, Q6_Vh_vsplat_R(0x3c00)); // + 1.0
114
+
115
+ // Combine polynomial (mantissa) with integer part (exponent): result = y * 2^k
116
+ y = Q6_Vhf_equals_Vqf16(y);
117
+ HVX_Vector y_exp = Q6_Vuh_vlsr_VuhR(Q6_Vh_vasl_VhR(y, 1), 11);
118
+ y_exp = Q6_Vh_vadd_VhVh(k_v, y_exp);
119
+ HVX_VectorPred q_underflow = Q6_Q_vcmp_gt_VhVh(zero_v, y_exp);
120
+ y = Q6_Vh_vaslacc_VhVhR(y, k_v, 10);
121
+ return Q6_V_vmux_QVV(q_underflow, zero_v, y);
122
+ }
123
+
124
+ #define FA_MIN_KV_BLOCKS 3
125
+
126
+ // Cost-based (Br, Bc) search for flash attention with pipeline constraint.
127
+ //
128
+ // VTCM model (same as before):
129
+ // overhead + g_br * per_gbr + g_br² * per_gbr2 + Bc * per_bc + g_br * Bc * per_gbr_bc
130
+ //
131
+ // Cost model (minimization objective):
132
+ // Q * (c_q_fixed + K * c_iter_fixed), where Q = ceil(qo/Br), K = ceil(kv/Bc)
133
+ static int hmx_fa_find_chunk_size(size_t * Br_out,
134
+ size_t * Bc_out,
135
+ size_t gqa_factor,
136
+ size_t DK,
137
+ size_t DV,
138
+ size_t qo_len,
139
+ size_t kv_len,
140
+ size_t vtcm_budget,
141
+ size_t n_threads) {
142
+ const size_t T = HMX_FP16_TILE_N_ROWS; // 32
143
+ const size_t br_unit = hmx_ceil_div(T, gqa_factor);
144
+ // Bc must be a multiple of 64 so that n_tiles_per_bc is even. The softmax
145
+ // P-tile write uses a dual-tile pattern (vshuff + two stores 16 slots apart)
146
+ // that would race across r0 blocks if the last dual-tile is half-occupied.
147
+ // See .cursor/todos/hmx-flash-attn-bc-search-space.md for the perf trade-off.
148
+ const size_t bc_unit = HMX_FP16_TILE_N_COLS * 2; // 64
149
+ const size_t fp16 = sizeof(__fp16);
150
+ const bool can_pipeline = (kv_len >= FA_MIN_KV_BLOCKS * bc_unit && n_threads >= 2);
151
+
152
+ // Approximate per-unit VTCM costs (without per-buffer alignment padding).
153
+ const size_t per_gbr = (DK + 2 * DV) * fp16 + 4 * fp16; // Q + O×2 + 4 col vectors
154
+ const size_t per_gbr2 = fp16; // D diagonal matrix
155
+ const size_t per_bc =
156
+ 3 * DK * fp16 + (can_pipeline ? 4 : 3) * DV * fp16 + 2 * n_threads * fp16; // K/V DMA x2 + tiles + row bufs
157
+ const size_t per_gbr_bc = 2 * fp16; // S + P
158
+
159
+ const size_t overhead = 256 * 2 + 13 * 4096;
160
+
161
+ if (vtcm_budget <= overhead) {
162
+ return -1;
163
+ }
164
+ const size_t usable = vtcm_budget - overhead;
165
+
166
+ // Br_max: largest Br aligned to br_unit that does not exceed qo_len.
167
+ const size_t Br_max = qo_len >= br_unit ? hex_align_down(qo_len, br_unit) : br_unit;
168
+
169
+ // Pipeline constraint: cap Bc so n_kv_blocks >= FA_MIN_KV_BLOCKS.
170
+ // Only relax when kv_len is too short to form enough blocks.
171
+ const size_t Bc_limit = can_pipeline ? hex_align_down(kv_len / FA_MIN_KV_BLOCKS, bc_unit) :
172
+ (kv_len >= bc_unit ? hex_align_down(kv_len, bc_unit) : bc_unit);
173
+ // Cost coefficients calibrated from profiling
174
+ const size_t c_q_fixed = 1400; // per-Q-block: q_load + epilogue o_update + o_norm + o_store
175
+ const size_t c_iter_fixed = 200; // per-KV-iter: HMX queue push/pop + DMA pop + barriers
176
+
177
+ size_t best_cost = SIZE_MAX, best_mn = 0;
178
+ size_t best_Br = 0, best_Bc = 0;
179
+
180
+ for (size_t Br = Br_max; Br >= br_unit; Br -= br_unit) {
181
+ const size_t g_br = hex_align_up(gqa_factor * Br, T);
182
+
183
+ // g_br-dependent VTCM cost: g_br * per_gbr + g_br² * per_gbr2
184
+ const size_t gbr_cost = g_br * per_gbr + g_br * g_br * per_gbr2;
185
+ if (gbr_cost >= usable) {
186
+ if (Br == br_unit) {
187
+ break;
188
+ }
189
+ continue;
190
+ }
191
+
192
+ // Analytically solve for max Bc:
193
+ // remain >= Bc * (per_bc + g_br * per_gbr_bc + Br * fp16_mask)
194
+ // The Br * fp16 term accounts for the VTCM mask buffer [Br × Bc].
195
+ const size_t remain = usable - gbr_cost;
196
+ const size_t bc_denom = per_bc + g_br * per_gbr_bc + Br * fp16;
197
+ size_t Bc = hex_smin(hex_align_down(remain / bc_denom, bc_unit), Bc_limit);
198
+ if (Bc < bc_unit) {
199
+ if (Br == br_unit) {
200
+ break;
201
+ }
202
+ continue;
203
+ }
204
+
205
+ // Exact VTCM verification (alignment padding may push over budget)
206
+ while (Bc >= bc_unit && hmx_fa_compute_vtcm_usage(gqa_factor, DK, DV, Br, Bc, n_threads, can_pipeline) > vtcm_budget) {
207
+ Bc -= bc_unit;
208
+ }
209
+ if (Bc < bc_unit) {
210
+ if (Br == br_unit) {
211
+ break;
212
+ }
213
+ continue;
214
+ }
215
+
216
+ const size_t q_blocks = (qo_len + Br - 1) / Br;
217
+ const size_t kv_blocks = (kv_len + Bc - 1) / Bc;
218
+ const size_t cost = q_blocks * (c_q_fixed + kv_blocks * c_iter_fixed);
219
+ const size_t mn = Br * Bc;
220
+
221
+ if (cost < best_cost || (cost == best_cost && mn > best_mn)) {
222
+ best_cost = cost;
223
+ best_mn = mn;
224
+ best_Br = Br;
225
+ best_Bc = Bc;
226
+ }
227
+
228
+ if (Br == br_unit) {
229
+ break;
230
+ }
231
+ }
232
+
233
+ if (best_Br == 0) {
234
+ return -1;
235
+ }
236
+
237
+ *Br_out = best_Br;
238
+ *Bc_out = best_Bc;
239
+ return 0;
240
+ }
241
+
242
+ // ============================================================================
243
+ // Tile interleave / extract helpers
244
+ // ============================================================================
245
+
246
+ // transpose scatter offsets moved to hmx-utils.h as hmx_transpose_scatter_offsets
247
+
248
+ // Scatter offsets for diagonal tile: entry[2i] = i*136, entry[2i+1] = i*136+6
249
+ // 136 = 4 * 32 + 8 = byte offset to diagonal in a 32x32 fp16 interleaved tile
250
+ static const int16_t d_tile_scatter_offsets[64] __attribute__((aligned(128))) = {
251
+ 0 * 136, 0 * 136 + 6,
252
+ 1 * 136, 1 * 136 + 6,
253
+ 2 * 136, 2 * 136 + 6,
254
+ 3 * 136, 3 * 136 + 6,
255
+ 4 * 136, 4 * 136 + 6,
256
+ 5 * 136, 5 * 136 + 6,
257
+ 6 * 136, 6 * 136 + 6,
258
+ 7 * 136, 7 * 136 + 6,
259
+ 8 * 136, 8 * 136 + 6,
260
+ 9 * 136, 9 * 136 + 6,
261
+ 10 * 136, 10 * 136 + 6,
262
+ 11 * 136, 11 * 136 + 6,
263
+ 12 * 136, 12 * 136 + 6,
264
+ 13 * 136, 13 * 136 + 6,
265
+ 14 * 136, 14 * 136 + 6,
266
+ 15 * 136, 15 * 136 + 6,
267
+ 0, 0,
268
+ 0, 0,
269
+ 0, 0,
270
+ 0, 0,
271
+ 0, 0,
272
+ 0, 0,
273
+ 0, 0,
274
+ 0, 0,
275
+ 0, 0,
276
+ 0, 0,
277
+ 0, 0,
278
+ 0, 0,
279
+ 0, 0,
280
+ 0, 0,
281
+ 0, 0,
282
+ 0, 0,
283
+ };
284
+
285
+ // hmx_interleave_rows_to_tiles and hmx_interleave_cols_to_tiles are in hmx-utils.h
286
+
287
+ // ============================================================================
288
+ // HMX Flash Attention context (GQA-merged)
289
+ // ============================================================================
290
+
291
+ struct hmx_fa_context {
292
+ const struct htp_ops_context * octx;
293
+ bool use_pipeline; // true when n_kv_blocks >= FA_MIN_KV_BLOCKS && n_threads >= 2
294
+ uint32_t n_threads;
295
+
296
+ // Op parameters
297
+ float scale;
298
+ float max_bias;
299
+ float logit_softcap;
300
+ uint32_t n_head_log2;
301
+ float m0, m1;
302
+
303
+ // Dimensions
304
+ uint32_t DK, DV;
305
+ uint32_t n_kv; // kv_len
306
+ uint32_t n_kv_heads; // number of KV heads
307
+ uint32_t n_heads; // number of Q heads
308
+ uint32_t G; // GQA factor = n_heads / n_kv_heads
309
+ struct fastdiv_values div_G;
310
+ uint32_t n_kv_blocks;
311
+ uint32_t neq1; // Q token count
312
+
313
+ // Types
314
+ bool is_q_fp32;
315
+ bool is_dst_fp32;
316
+
317
+ // Dynamic block sizes
318
+ uint32_t Br; // Q tokens per block (before GQA expansion)
319
+ uint32_t Bc;
320
+ uint32_t g_br; // hex_align_up(G * Br, 32) - actual tile row dim
321
+
322
+ // VTCM buffers (allocated by vtcm_seq_alloc)
323
+ __fp16 * vtcm_q_tiles; // Q tile format [g_br, D]
324
+ __fp16 * vtcm_o_tiles[2]; // O ping-pong [g_br, D]
325
+ __fp16 * vtcm_k_fp16[2]; // K DMA double-buffer [Bc, D]
326
+ __fp16 * vtcm_v_fp16[2]; // V DMA double-buffer [Bc, D]
327
+ __fp16 * vtcm_k_tiles; // K tiles (transposed)
328
+ __fp16 * vtcm_v_tiles[2]; // V tiles (column-major, double-buffered)
329
+ __fp16 * vtcm_s_tiles; // S = QK^T [g_br, Bc]
330
+ __fp16 * vtcm_p_tiles; // P = softmax(S) [g_br, Bc]
331
+ __fp16 * vtcm_d_tiles; // Diagonal rescale [g_br, g_br]
332
+ HVX_Vector * vtcm_m_vec; // Row max [g_br]
333
+ HVX_Vector * vtcm_l_vec; // Row sum [g_br]
334
+ HVX_Vector * vtcm_s_rowmax; // Softmax intermediate [g_br]
335
+ HVX_Vector * vtcm_p_rowsum; // Softmax intermediate [g_br]
336
+ HVX_Vector * vtcm_row_bufs; // Per-thread softmax row scratch [n_threads][2][Bc/64]
337
+ uint8_t * vtcm_hmx_scales_id; // HMX output scales (identity)
338
+ uint8_t * vtcm_hmx_scales_qk; // HMX output scales (qk_scale)
339
+ __fp16 * vtcm_mask_buf; // VTCM mask buffer [Br × m_line], DMA'd per KV block
340
+ __fp16 * vtcm_slopes; // ALiBi slopes [g_br]
341
+ size_t row_buf_stride; // HVX vectors per row buffer (Bc/64)
342
+ size_t mask_buf_row_stride; // elements (__fp16) per row in mask buffer
343
+ bool mask_broadcast; // true when mask->ne[2] == 1 (head-independent, single 2D DMA)
344
+ };
345
+
346
+ // ============================================================================
347
+ // Multi-thread K interleave phase
348
+ // ============================================================================
349
+
350
+ typedef struct {
351
+ struct hmx_fa_context * factx;
352
+ int kv_rows;
353
+ size_t src_stride;
354
+ size_t buf_idx;
355
+ } fa_k_int_args_t;
356
+
357
+ static void fa_k_interleave_thread(unsigned int n, unsigned int i, void * data) {
358
+ fa_k_int_args_t * args = (fa_k_int_args_t *) data;
359
+ struct hmx_fa_context * factx = args->factx;
360
+
361
+ const int total_rows = args->kv_rows;
362
+ const int rows_per_t = hex_align_up(hmx_ceil_div(total_rows, n), 2); // ensure even (row pairs)
363
+ const int start = i * rows_per_t;
364
+ const int end = hex_smin(start + rows_per_t, total_rows);
365
+
366
+ if (start >= total_rows) {
367
+ return;
368
+ }
369
+
370
+ hmx_interleave_rows_to_tiles(factx->vtcm_k_tiles, factx->vtcm_k_fp16[args->buf_idx], total_rows, (int) factx->DK,
371
+ (int) args->src_stride, start, end);
372
+ }
373
+
374
+ static void fa_phase_k_interleave(struct hmx_fa_context * factx, int kv_rows, size_t src_stride, size_t buf_idx) {
375
+ worker_pool_context_t wp = factx->octx->ctx->worker_pool;
376
+ fa_k_int_args_t args = { factx, kv_rows, src_stride, buf_idx };
377
+ if (factx->n_threads > 1 && kv_rows >= (int) (factx->n_threads * 2)) {
378
+ worker_pool_run_func(wp, fa_k_interleave_thread, &args, factx->n_threads);
379
+ } else {
380
+ fa_k_interleave_thread(1, 0, &args);
381
+ }
382
+ }
383
+
384
+ // ============================================================================
385
+ // Multi-thread V interleave phase
386
+ // ============================================================================
387
+
388
+ typedef struct {
389
+ struct hmx_fa_context * factx;
390
+ int kv_rows;
391
+ size_t src_stride;
392
+ size_t buf_idx;
393
+ size_t n_col_tiles;
394
+ } fa_v_int_args_t;
395
+
396
+ static void fa_v_interleave_thread(unsigned int n, unsigned int i, void * data) {
397
+ fa_v_int_args_t * args = (fa_v_int_args_t *) data;
398
+ struct hmx_fa_context * factx = args->factx;
399
+
400
+ const int total_rows = args->kv_rows;
401
+ const int rows_per_t = hex_align_up(hmx_ceil_div(total_rows, n), 2);
402
+ const int start = i * rows_per_t;
403
+ const int end = hex_smin(start + rows_per_t, total_rows);
404
+
405
+ if (start >= total_rows) {
406
+ return;
407
+ }
408
+
409
+ __fp16 * v_tiles_dest = factx->use_pipeline ? factx->vtcm_v_tiles[args->buf_idx] : factx->vtcm_v_tiles[0];
410
+
411
+ hmx_interleave_cols_to_tiles(v_tiles_dest, factx->vtcm_v_fp16[args->buf_idx], total_rows, (int) factx->DV,
412
+ (int) args->src_stride, (int) args->n_col_tiles, start, end);
413
+ }
414
+
415
+ static void fa_phase_v_interleave(struct hmx_fa_context * factx,
416
+ int kv_rows,
417
+ size_t src_stride,
418
+ size_t buf_idx,
419
+ size_t n_col_tiles) {
420
+ worker_pool_context_t wp = factx->octx->ctx->worker_pool;
421
+ fa_v_int_args_t args = { factx, kv_rows, src_stride, buf_idx, n_col_tiles };
422
+ if (factx->n_threads > 1 && kv_rows >= (int) (factx->n_threads * 2)) {
423
+ worker_pool_run_func(wp, fa_v_interleave_thread, &args, factx->n_threads);
424
+ } else {
425
+ fa_v_interleave_thread(1, 0, &args);
426
+ }
427
+ }
428
+
429
+ // ============================================================================
430
+ // Multi-thread Q load phase: read Q[G × neq1, DK] from DDR, convert F32→F16
431
+ // (or deal F16 pairs), and write interleaved into vtcm_q_tiles.
432
+ // Each thread owns a disjoint range of row pairs; writes target distinct tile
433
+ // slots (r0 selects tile row, r1 selects intra-tile slot), so there is no
434
+ // write conflict. Padding fill (when n_rows_g < g_br) is done single-threaded
435
+ // by the caller before dispatching.
436
+ // ============================================================================
437
+
438
+ typedef struct {
439
+ struct hmx_fa_context * factx;
440
+ const struct htp_tensor * q;
441
+ uint32_t q_start;
442
+ uint32_t kv_head;
443
+ uint32_t ib3;
444
+ size_t n_rows_g;
445
+ } fa_q_load_args_t;
446
+
447
+ static void fa_q_load_thread(unsigned int n, unsigned int i, void * data) {
448
+ fa_q_load_args_t * args = (fa_q_load_args_t *) data;
449
+ struct hmx_fa_context * factx = args->factx;
450
+
451
+ const size_t n_rows_g = args->n_rows_g;
452
+ const size_t G = factx->G;
453
+ const size_t DK = factx->DK;
454
+
455
+ // Partition row pairs across threads. Keep each thread's start even so r/r+1
456
+ // are always in the same thread's range.
457
+ const size_t rows_per_t = hex_align_up(hmx_ceil_div(n_rows_g, n), 2);
458
+ const size_t start = (size_t) i * rows_per_t;
459
+ const size_t end = hex_smin(start + rows_per_t, n_rows_g);
460
+
461
+ if (start >= n_rows_g) {
462
+ return;
463
+ }
464
+
465
+ const struct htp_tensor * q = args->q;
466
+ const uint32_t q_start = args->q_start;
467
+ const uint32_t kv_head = args->kv_head;
468
+ const uint32_t ib3 = args->ib3;
469
+
470
+ for (size_t r = start; r < end; r += 2) {
471
+ const bool next_row_valid = (r + 1) < n_rows_g;
472
+
473
+ const size_t q_idx0 = fastdiv(r + 0, &factx->div_G);
474
+ const size_t h_idx0 = fastmodulo(r + 0, G, &factx->div_G);
475
+ const size_t q_idx1 = fastdiv(r + 1, &factx->div_G);
476
+ const size_t h_idx1 = fastmodulo(r + 1, G, &factx->div_G);
477
+
478
+ const uint8_t * q_ptr0 = (const uint8_t *) q->data + (q_start + q_idx0) * q->nb[1] +
479
+ (kv_head * G + h_idx0) * q->nb[2] + ib3 * q->nb[3];
480
+ const uint8_t * q_ptr1 = next_row_valid ? ((const uint8_t *) q->data + (q_start + q_idx1) * q->nb[1] +
481
+ (kv_head * G + h_idx1) * q->nb[2] + ib3 * q->nb[3]) :
482
+ NULL;
483
+
484
+ size_t r0 = r / HMX_FP16_TILE_N_ROWS;
485
+ size_t r1 = r % HMX_FP16_TILE_N_ROWS;
486
+ __fp16 * out_base = factx->vtcm_q_tiles + r0 * HMX_FP16_TILE_N_ROWS * DK;
487
+
488
+ if (factx->is_q_fp32) {
489
+ const HVX_Vector * pv_in0 = (const HVX_Vector *) q_ptr0;
490
+ const HVX_Vector * pv_in1 = q_ptr1 ? (const HVX_Vector *) q_ptr1 : NULL;
491
+
492
+ for (uint32_t d = 0; d < DK / 32; ++d) {
493
+ HVX_Vector v0 = pv_in0[d];
494
+ HVX_Vector v1 = pv_in1 ? pv_in1[d] : Q6_V_vzero();
495
+ HVX_Vector v_hf = hvx_vec_f32_to_f16_shuff(v0, v1);
496
+
497
+ HVX_Vector * out_tile = (HVX_Vector *) (out_base + d * HMX_FP16_TILE_N_ELMS);
498
+ out_tile[r1 / 2] = v_hf;
499
+ }
500
+ } else {
501
+ const HVX_Vector * pv_in0 = (const HVX_Vector *) q_ptr0;
502
+ const HVX_Vector * pv_in1 = q_ptr1 ? (const HVX_Vector *) q_ptr1 : NULL;
503
+
504
+ for (uint32_t d = 0; d < DK / 64; ++d) {
505
+ HVX_Vector v0 = pv_in0[d];
506
+ HVX_Vector v1 = pv_in1 ? pv_in1[d] : Q6_V_vzero();
507
+ HVX_VectorPair vp = Q6_W_vshuff_VVR(v1, v0, -2);
508
+
509
+ __fp16 * out_dual_tile = out_base + d * HMX_FP16_TILE_N_ELMS * 2;
510
+ HVX_Vector * pv_out0 = ((HVX_Vector *) out_dual_tile) + r1 / 2;
511
+ HVX_Vector * pv_out1 = pv_out0 + 16;
512
+
513
+ *pv_out0 = Q6_V_lo_W(vp);
514
+ *pv_out1 = Q6_V_hi_W(vp);
515
+ }
516
+ }
517
+ }
518
+ }
519
+
520
+ static void fa_phase_q_load(struct hmx_fa_context * factx,
521
+ const struct htp_tensor * q,
522
+ uint32_t q_start,
523
+ uint32_t kv_head,
524
+ uint32_t ib3,
525
+ size_t n_rows_g) {
526
+ worker_pool_context_t wp = factx->octx->ctx->worker_pool;
527
+ fa_q_load_args_t args = { factx, q, q_start, kv_head, ib3, n_rows_g };
528
+ // Require >= 2 row pairs per thread so partitioning is worthwhile.
529
+ if (factx->n_threads > 1 && n_rows_g >= (size_t) (factx->n_threads * 2)) {
530
+ worker_pool_run_func(wp, fa_q_load_thread, &args, factx->n_threads);
531
+ } else {
532
+ fa_q_load_thread(1, 0, &args);
533
+ }
534
+ }
535
+
536
+ // ============================================================================
537
+ // Multi-thread O store phase: read O tiles from VTCM, convert F16->F32 (or
538
+ // deal F16 pairs), and write to strided DDR dst tensor. Each thread owns a
539
+ // disjoint row range; writes target distinct dst rows (different q_idx/h_idx
540
+ // pairs produced by r/G and r%G), so there is no write conflict.
541
+ // ============================================================================
542
+
543
+ typedef struct {
544
+ struct hmx_fa_context * factx;
545
+ const struct htp_tensor * dst;
546
+ const __fp16 * o_tile_src;
547
+ uint32_t q_start;
548
+ uint32_t kv_head;
549
+ uint32_t ib3;
550
+ size_t n_rows_g;
551
+ } fa_o_store_args_t;
552
+
553
+ static void fa_o_store_thread(unsigned int n, unsigned int i, void * data) {
554
+ fa_o_store_args_t * args = (fa_o_store_args_t *) data;
555
+ struct hmx_fa_context * factx = args->factx;
556
+
557
+ const size_t n_rows_g = args->n_rows_g;
558
+ const size_t G = factx->G;
559
+ const size_t DV = factx->DV;
560
+
561
+ const size_t rows_per_t = hmx_ceil_div(n_rows_g, n);
562
+ const size_t start = (size_t) i * rows_per_t;
563
+ const size_t end = hex_smin(start + rows_per_t, n_rows_g);
564
+
565
+ if (start >= n_rows_g) {
566
+ return;
567
+ }
568
+
569
+ const struct htp_tensor * dst = args->dst;
570
+ const __fp16 * o_tile_src = args->o_tile_src;
571
+ const uint32_t q_start = args->q_start;
572
+ const uint32_t kv_head = args->kv_head;
573
+ const uint32_t ib3 = args->ib3;
574
+
575
+ for (size_t r = start; r < end; ++r) {
576
+ const size_t q_idx = fastdiv(r, &factx->div_G);
577
+ const size_t h_idx = fastmodulo(r, G, &factx->div_G);
578
+
579
+ // FIX(dst-indexing): ggml_flash_attn_ext() creates dst as permute(0,2,1,3) ->
580
+ // [DV, n_heads, n_tokens, n_seq], so head stride is nb[1] and token stride is nb[2].
581
+ uint8_t * dst_row = (uint8_t *) dst->data + (kv_head * G + h_idx) * dst->nb[1] +
582
+ (q_start + q_idx) * dst->nb[2] + ib3 * dst->nb[3];
583
+
584
+ size_t r0 = r / HMX_FP16_TILE_N_ROWS;
585
+ size_t r1 = r % HMX_FP16_TILE_N_ROWS;
586
+ const __fp16 * tile_row_base = o_tile_src + r0 * HMX_FP16_TILE_N_ROWS * DV;
587
+
588
+ if (factx->is_dst_fp32) {
589
+ float * out = (float *) dst_row;
590
+ for (uint32_t d = 0; d < DV / 32; ++d) {
591
+ const HVX_Vector * in_tile = (const HVX_Vector *) (tile_row_base + d * HMX_FP16_TILE_N_ELMS);
592
+ HVX_VectorPair vp = hvx_vec_f16_to_f32_shuff(in_tile[r1 / 2]);
593
+ if (r1 % 2 == 0) {
594
+ *(HVX_UVector *) (out + d * 32) = Q6_V_lo_W(vp);
595
+ } else {
596
+ *(HVX_UVector *) (out + d * 32) = Q6_V_hi_W(vp);
597
+ }
598
+ }
599
+ } else {
600
+ __fp16 * out = (__fp16 *) dst_row;
601
+ for (uint32_t d = 0; d < DV / 64; ++d) {
602
+ const __fp16 * in_dual_tile = tile_row_base + d * HMX_FP16_TILE_N_ELMS * 2;
603
+ const HVX_Vector * pv_in0 = ((const HVX_Vector *) in_dual_tile) + r1 / 2;
604
+ const HVX_Vector * pv_in1 = pv_in0 + 16;
605
+ HVX_VectorPair vp = Q6_W_vdeal_VVR(*pv_in1, *pv_in0, -2);
606
+ if (r1 % 2 == 0) {
607
+ *(HVX_UVector *) (out + d * 64) = Q6_V_lo_W(vp);
608
+ } else {
609
+ *(HVX_UVector *) (out + d * 64) = Q6_V_hi_W(vp);
610
+ }
611
+ }
612
+ }
613
+ }
614
+ }
615
+
616
+ static void fa_phase_o_store(struct hmx_fa_context * factx,
617
+ const struct htp_tensor * dst,
618
+ const __fp16 * o_tile_src,
619
+ uint32_t q_start,
620
+ uint32_t kv_head,
621
+ uint32_t ib3,
622
+ size_t n_rows_g) {
623
+ worker_pool_context_t wp = factx->octx->ctx->worker_pool;
624
+ fa_o_store_args_t args = { factx, dst, o_tile_src, q_start, kv_head, ib3, n_rows_g };
625
+ if (factx->n_threads > 1 && n_rows_g >= (size_t) (factx->n_threads * 2)) {
626
+ worker_pool_run_func(wp, fa_o_store_thread, &args, factx->n_threads);
627
+ } else {
628
+ fa_o_store_thread(1, 0, &args);
629
+ }
630
+ }
631
+
632
+ // ============================================================================
633
+ // Multi-thread softmax phase + serial m/l update + build_D
634
+ // ============================================================================
635
+
636
+ typedef struct {
637
+ struct hmx_fa_context * factx;
638
+ size_t kv_rows;
639
+ size_t n_rows_g;
640
+ size_t n_col_tiles;
641
+ size_t n_tiles_per_bc;
642
+ size_t n_row_tiles;
643
+ size_t n_row_tiles_g_br;
644
+ uint32_t Bc;
645
+ uint32_t G;
646
+ uint32_t kv_head;
647
+ uint32_t kv_start;
648
+ uint32_t q_start;
649
+ uint32_t ib3;
650
+ bool has_alibi; // true when max_bias != 0 (need slope * mask + add)
651
+
652
+ // ALiBi per-head slopes (indexed by GQA-merged row: slope[r] for r in [0, n_rows_g))
653
+ // slope[r] = 1.0 when max_bias == 0 (no ALiBi)
654
+ // Pointer into hmx_fa_context.vtcm_slopes (sized to g_br)
655
+ __fp16 * slopes;
656
+
657
+ // Mask info (preloaded before softmax)
658
+ const struct htp_tensor * mask;
659
+ const __fp16 * mask_vtcm; // VTCM mask buffer base (NULL = DDR fallback)
660
+ size_t mask_vtcm_row_stride; // elements (__fp16) per row in VTCM mask buffer
661
+ } fa_softmax_args_t;
662
+
663
+ static void fa_softmax_thread(unsigned int n, unsigned int i, void * data) {
664
+ fa_softmax_args_t * args = (fa_softmax_args_t *) data;
665
+ struct hmx_fa_context * factx = args->factx;
666
+
667
+ const size_t n_rows_g = args->n_rows_g;
668
+ const size_t kv_rows = args->kv_rows;
669
+ const size_t Bc = args->Bc;
670
+ const size_t G = args->G;
671
+ const size_t n_tiles_per_bc = args->n_tiles_per_bc;
672
+ const size_t n_row_vec_cnt = hmx_ceil_div(n_rows_g, 64);
673
+
674
+ // Partition r_vec_idx across threads
675
+ const size_t vecs_per_t = hmx_ceil_div(n_row_vec_cnt, n);
676
+ const size_t vec_start = i * vecs_per_t;
677
+ const size_t vec_end = hex_smin(vec_start + vecs_per_t, n_row_vec_cnt);
678
+
679
+ if (vec_start >= n_row_vec_cnt) {
680
+ return;
681
+ }
682
+
683
+ // Per-thread row scratch: thread i uses bufs at offset i * 2 * stride
684
+ const size_t row_buf_stride = factx->row_buf_stride;
685
+ HVX_Vector * my_row_buf0 = factx->vtcm_row_bufs + i * 2 * row_buf_stride;
686
+ HVX_Vector * my_row_buf1 = my_row_buf0 + row_buf_stride;
687
+
688
+ const HVX_Vector v_neg_inf = Q6_Vh_vsplat_R(0xfbff);
689
+
690
+ // Per-row accumulators: each fp16 lane in a 64-lane vector holds one row's scalar.
691
+ // CONTRACT: lane bits must be IEEE fp16 (hf), never qf16 — qf16 uses a different
692
+ // bit layout, so a later hf-domain read would silently produce wrong values.
693
+ // Convert first via Q6_Vhf_equals_Vqf16(). For reference: vtcm_m_vec/vtcm_s_rowmax
694
+ // are hf; vtcm_l_vec is qf16 — don't mix them up.
695
+
696
+ for (size_t r_vec_idx = vec_start; r_vec_idx < vec_end; ++r_vec_idx) {
697
+ HVX_Vector rowmax_acc_v = v_neg_inf;
698
+ HVX_Vector rowsum_acc_v = Q6_V_vzero();
699
+ HVX_Vector m_prev_v = factx->vtcm_m_vec[r_vec_idx];
700
+
701
+ for (int r_vec_off = 0; r_vec_off < 64; r_vec_off += 2) {
702
+ int r = r_vec_idx * 64 + r_vec_off;
703
+ if (r >= (int) hex_align_up(n_rows_g, 2)) {
704
+ break;
705
+ }
706
+
707
+ int r0 = r / HMX_FP16_TILE_N_ROWS;
708
+ int r1 = r % HMX_FP16_TILE_N_ROWS;
709
+
710
+ const __fp16 * s_ld_base = factx->vtcm_s_tiles + r0 * HMX_FP16_TILE_N_ROWS * Bc;
711
+ __fp16 * p_st_base = factx->vtcm_p_tiles + r0 * HMX_FP16_TILE_N_ROWS * Bc;
712
+
713
+ // Decode 2 rows from S tiles into per-thread row buffers
714
+ HVX_Vector * pv_row_buf0 = my_row_buf0;
715
+ HVX_Vector * pv_row_buf1 = my_row_buf1;
716
+ for (size_t c = 0; c < kv_rows; c += 64) {
717
+ const __fp16 * in_dual_tile = s_ld_base + (c / 64) * HMX_FP16_TILE_N_ELMS * 2;
718
+ const HVX_Vector * pv_s_in0 = ((const HVX_Vector *) in_dual_tile) + r1 / 2;
719
+ const HVX_Vector * pv_s_in1 = pv_s_in0 + 16;
720
+
721
+ HVX_VectorPair vp_s_dual_row = Q6_W_vdeal_VVR(*pv_s_in1, *pv_s_in0, -2);
722
+ *pv_row_buf0++ = Q6_V_lo_W(vp_s_dual_row);
723
+ *pv_row_buf1++ = Q6_V_hi_W(vp_s_dual_row);
724
+ }
725
+
726
+ // Apply softcap if enabled (in F32 precision)
727
+ if (factx->logit_softcap != 0.0f) {
728
+ // When EXP2_HF is on, fold log2(e) into v_cap so the output lands in
729
+ // log2(e)-scaled space for the downstream exp2. log2(e) is kept OUT
730
+ // of qk_scale in this configuration (see scale setup) so tanh sees
731
+ // the physical QK/(√d·c) argument.
732
+ float cap = factx->logit_softcap;
733
+ #ifdef HMX_FA_USE_EXP2_HF
734
+ cap *= 1.44269504f; // log2(e)
735
+ #endif
736
+ const HVX_Vector v_cap = hvx_vec_splat_f32(cap);
737
+ for (size_t c = 0; c < kv_rows; c += 64) {
738
+ size_t ci = c / 64;
739
+
740
+ HVX_VectorPair r0_f32 = hvx_vec_f16_to_f32(my_row_buf0[ci]);
741
+ HVX_Vector t0_lo = hvx_vec_tanh_f32(Q6_V_lo_W(r0_f32));
742
+ HVX_Vector t0_hi = hvx_vec_tanh_f32(Q6_V_hi_W(r0_f32));
743
+ t0_lo = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(t0_lo, v_cap));
744
+ t0_hi = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(t0_hi, v_cap));
745
+ my_row_buf0[ci] = hvx_vec_f32_to_f16(t0_lo, t0_hi);
746
+
747
+ HVX_VectorPair r1_f32 = hvx_vec_f16_to_f32(my_row_buf1[ci]);
748
+ HVX_Vector t1_lo = hvx_vec_tanh_f32(Q6_V_lo_W(r1_f32));
749
+ HVX_Vector t1_hi = hvx_vec_tanh_f32(Q6_V_hi_W(r1_f32));
750
+ t1_lo = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(t1_lo, v_cap));
751
+ t1_hi = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(t1_hi, v_cap));
752
+ my_row_buf1[ci] = hvx_vec_f32_to_f16(t1_lo, t1_hi);
753
+ }
754
+ }
755
+
756
+ // Apply mask & compute rowmax(S)
757
+ //
758
+ // Optimizations over baseline:
759
+ // A. No-ALiBi fast path: when max_bias==0 (slope≡1.0), skip the
760
+ // slope multiplication — still add mask (additive bias) but
761
+ // avoid the mul_f16_f16. Saves 2 ops/dual-row vs ALiBi path.
762
+ // B. GQA mask row dedup: G consecutive Q rows share one mask row
763
+ // (qi = r / G). Reuse mask vector when qi is unchanged between
764
+ // row0 and row1 (saves ~75% of VTCM loads for G=4).
765
+
766
+ // ALiBi slopes — only needed when has_alibi (scheme A)
767
+ HVX_Vector v_slope0, v_slope1;
768
+ if (args->has_alibi) {
769
+ HVX_Vector v_s = hvx_vmemu(args->slopes + r);
770
+ v_slope0 = hvx_vec_repl_f16(v_s);
771
+ v_slope1 = (r + 1 < (int) n_rows_g) ? hvx_vec_repl_f16(Q6_V_vror_VR(v_s, 2)) : Q6_V_vzero();
772
+ }
773
+
774
+ const HVX_Vector v_threshold = Q6_Vh_vsplat_R(0xcc00); // fp16 -16.0 (hoisted outside for-c)
775
+
776
+ HVX_Vector v_s_rowmax0 = v_neg_inf;
777
+ HVX_Vector v_s_rowmax1 = v_neg_inf;
778
+ for (size_t c = 0; c < kv_rows; c += 64) {
779
+ size_t ci = c / 64;
780
+ const size_t ne = hex_smin(kv_rows - c, 64);
781
+ HVX_VectorPred q_tail_keep = Q6_Q_vsetq2_R(ne * sizeof(__fp16));
782
+
783
+ if (args->mask) {
784
+ HVX_Vector v_mask0, v_mask1;
785
+
786
+ if (args->mask_vtcm) {
787
+ // Read mask from VTCM buffer (DMA'd per KV block).
788
+ // GQA dedup (scheme B): skip load when qi unchanged.
789
+ const size_t qi0 = fastdiv(r + 0, &factx->div_G);
790
+ v_mask0 = *(const HVX_UVector *) (args->mask_vtcm + qi0 * args->mask_vtcm_row_stride + c);
791
+ v_mask1 = v_neg_inf;
792
+ if (r + 1 < (int) n_rows_g) {
793
+ const size_t qi1 = fastdiv(r + 1, &factx->div_G);
794
+ if (qi1 == qi0) {
795
+ v_mask1 = v_mask0; // scheme B: reuse — same mask row
796
+ } else {
797
+ v_mask1 = *(const HVX_UVector *) (args->mask_vtcm + qi1 * args->mask_vtcm_row_stride + c);
798
+ }
799
+ }
800
+ } else {
801
+ // Fallback: read mask directly from DDR (when mask->ne[2] > 1).
802
+ const struct htp_tensor * mask = args->mask;
803
+ const size_t q_idx0 = args->q_start + fastdiv(r + 0, &factx->div_G);
804
+ const size_t h_idx0 = args->kv_head * G + fastmodulo(r + 0, G, &factx->div_G);
805
+ const uint32_t im2_0 = h_idx0 % mask->ne[2];
806
+ const uint32_t im3_0 = args->ib3 % mask->ne[3];
807
+
808
+ const __fp16 * m0_ptr = (const __fp16 *) ((const uint8_t *) mask->data + q_idx0 * mask->nb[1] +
809
+ im2_0 * mask->nb[2] + im3_0 * mask->nb[3]) + args->kv_start + c;
810
+ v_mask0 = *(const HVX_UVector *) m0_ptr;
811
+ v_mask1 = v_neg_inf;
812
+
813
+ if (r + 1 < (int) n_rows_g) {
814
+ const size_t q_idx1 = args->q_start + fastdiv(r + 1, &factx->div_G);
815
+ if (q_idx1 == q_idx0) {
816
+ // scheme B: same mask row in DDR path
817
+ v_mask1 = v_mask0;
818
+ } else {
819
+ const size_t h_idx1 = args->kv_head * G + fastmodulo(r + 1, G, &factx->div_G);
820
+ const uint32_t im2_1 = h_idx1 % mask->ne[2];
821
+ const uint32_t im3_1 = args->ib3 % mask->ne[3];
822
+ const __fp16 * m1_ptr = (const __fp16 *) ((const uint8_t *) mask->data + q_idx1 * mask->nb[1] +
823
+ im2_1 * mask->nb[2] + im3_1 * mask->nb[3]) + args->kv_start + c;
824
+ v_mask1 = *(const HVX_UVector *) m1_ptr;
825
+ }
826
+ }
827
+ }
828
+
829
+ // Threshold: mask values below -16.0 are treated as -inf (causal mask).
830
+ HVX_VectorPred q_keep0 = Q6_Q_and_QQ(Q6_Q_vcmp_gt_VhfVhf(v_mask0, v_threshold), q_tail_keep);
831
+ HVX_VectorPred q_keep1 = Q6_Q_and_QQ(Q6_Q_vcmp_gt_VhfVhf(v_mask1, v_threshold), q_tail_keep);
832
+
833
+ if (args->has_alibi) {
834
+ // ALiBi path: S += slope * mask (full mul + add)
835
+ HVX_Vector v_sm0 = hvx_vec_mul_f16_f16(v_mask0, v_slope0);
836
+ HVX_Vector v_sm1 = hvx_vec_mul_f16_f16(v_mask1, v_slope1);
837
+ my_row_buf0[ci] = Q6_V_vmux_QVV(q_keep0, hvx_vec_add_f16_f16(my_row_buf0[ci], v_sm0), v_neg_inf);
838
+ my_row_buf1[ci] = Q6_V_vmux_QVV(q_keep1, hvx_vec_add_f16_f16(my_row_buf1[ci], v_sm1), v_neg_inf);
839
+ } else {
840
+ // No-ALiBi fast path (scheme A): slope≡1.0, skip the mul
841
+ // but still add mask (additive positional bias). vmux
842
+ // clamps mask < -16 to -inf as a numerical safeguard.
843
+ my_row_buf0[ci] = Q6_V_vmux_QVV(q_keep0, hvx_vec_add_f16_f16(my_row_buf0[ci], v_mask0), v_neg_inf);
844
+ my_row_buf1[ci] = Q6_V_vmux_QVV(q_keep1, hvx_vec_add_f16_f16(my_row_buf1[ci], v_mask1), v_neg_inf);
845
+ }
846
+ } else {
847
+ if (ne < 64) {
848
+ my_row_buf0[ci] = Q6_V_vmux_QVV(q_tail_keep, my_row_buf0[ci], v_neg_inf);
849
+ my_row_buf1[ci] = Q6_V_vmux_QVV(q_tail_keep, my_row_buf1[ci], v_neg_inf);
850
+ }
851
+ }
852
+
853
+ v_s_rowmax0 = Q6_Vhf_vmax_VhfVhf(v_s_rowmax0, my_row_buf0[ci]);
854
+ v_s_rowmax1 = Q6_Vhf_vmax_VhfVhf(v_s_rowmax1, my_row_buf1[ci]);
855
+ }
856
+
857
+ v_s_rowmax0 = hvx_vec_reduce_max_f16(v_s_rowmax0);
858
+ v_s_rowmax1 = hvx_vec_reduce_max_f16(v_s_rowmax1);
859
+
860
+ // Splat m_prev[r], m_prev[r+1] from the per-row accumulator.
861
+ // vror brings the target lane to lane 0, then vdelta replicates it
862
+ // across all lanes — stays in the vector domain (no store/reload).
863
+ HVX_Vector v_m_prev0 = hvx_vec_repl_f16(Q6_V_vror_VR(m_prev_v, r_vec_off * 2));
864
+ HVX_Vector v_m_prev1 = hvx_vec_repl_f16(Q6_V_vror_VR(m_prev_v, (r_vec_off + 1) * 2));
865
+
866
+ // HVX max — both operands are splats, so result is splat of m_new.
867
+ HVX_Vector v_dup_m0 = Q6_Vhf_vmax_VhfVhf(v_m_prev0, v_s_rowmax0);
868
+ HVX_Vector v_dup_m1 = Q6_Vhf_vmax_VhfVhf(v_m_prev1, v_s_rowmax1);
869
+
870
+ // Insert row r, r+1 rowmax into rowmax_acc_v via 2-byte-wide vmux.
871
+ // Byte ranges: lane0 = [r_vec_off*2 .. r_vec_off*2+1], lane1 shifted by 2.
872
+ // vsetq2 handles the n=128 corner case when r_vec_off reaches 62.
873
+ {
874
+ HVX_VectorPred p_start = Q6_Q_vsetq_R(r_vec_off * 2);
875
+ HVX_VectorPred p_mid = Q6_Q_vsetq_R((r_vec_off + 1) * 2);
876
+ HVX_VectorPred p_end = Q6_Q_vsetq2_R((r_vec_off + 2) * 2);
877
+ HVX_VectorPred p_lane0 = Q6_Q_and_QQn(p_mid, p_start);
878
+ HVX_VectorPred p_lane1 = Q6_Q_and_QQn(p_end, p_mid);
879
+ rowmax_acc_v = Q6_V_vmux_QVV(p_lane0, v_dup_m0, rowmax_acc_v);
880
+ rowmax_acc_v = Q6_V_vmux_QVV(p_lane1, v_dup_m1, rowmax_acc_v);
881
+ }
882
+
883
+ // Compute P = exp(S - m_new), using HVX exp
884
+ const HVX_Vector v_zero = Q6_V_vzero();
885
+ HVX_Vector v_p_rowsum0 = v_zero;
886
+ HVX_Vector v_p_rowsum1 = v_zero;
887
+
888
+ #ifdef HMX_FA_USE_EXP2_HF
889
+ // FP16 exp2 polynomial path (matches htp-ops-lib flash_attn.c):
890
+ // P = exp2(S - m_new)
891
+ for (size_t c = 0; c < kv_rows; c += 64) {
892
+ size_t ci = c / 64;
893
+ HVX_Vector v_s_minus_m0 = Q6_Vqf16_vsub_VhfVhf(my_row_buf0[ci], v_dup_m0);
894
+ HVX_Vector v_s_minus_m1 = Q6_Vqf16_vsub_VhfVhf(my_row_buf1[ci], v_dup_m1);
895
+
896
+ HVX_Vector v_p_row0_hf = hvx_exp2_hf(Q6_Vhf_equals_Vqf16(v_s_minus_m0));
897
+ HVX_Vector v_p_row1_hf = hvx_exp2_hf(Q6_Vhf_equals_Vqf16(v_s_minus_m1));
898
+ #else
899
+ // F32 exp path: qf16 → f32 → exp → f32 → f16. Higher precision,
900
+ for (size_t c = 0; c < kv_rows; c += 64) {
901
+ size_t ci = c / 64;
902
+ HVX_Vector v_s_minus_m0 = Q6_Vqf16_vsub_VhfVhf(my_row_buf0[ci], v_dup_m0);
903
+ HVX_Vector v_s_minus_m1 = Q6_Vqf16_vsub_VhfVhf(my_row_buf1[ci], v_dup_m1);
904
+
905
+ HVX_VectorPair vp0 = hvx_vec_f16_to_f32_shuff(Q6_Vhf_equals_Vqf16(v_s_minus_m0));
906
+ HVX_Vector p0_lo = hvx_vec_exp_f32(Q6_V_lo_W(vp0));
907
+ HVX_Vector p0_hi = hvx_vec_exp_f32(Q6_V_hi_W(vp0));
908
+ HVX_Vector v_p_row0_hf = hvx_vec_f32_to_f16_shuff(p0_lo, p0_hi);
909
+
910
+ HVX_VectorPair vp1 = hvx_vec_f16_to_f32_shuff(Q6_Vhf_equals_Vqf16(v_s_minus_m1));
911
+ HVX_Vector p1_lo = hvx_vec_exp_f32(Q6_V_lo_W(vp1));
912
+ HVX_Vector p1_hi = hvx_vec_exp_f32(Q6_V_hi_W(vp1));
913
+ HVX_Vector v_p_row1_hf = hvx_vec_f32_to_f16_shuff(p1_lo, p1_hi);
914
+ #endif
915
+ // Write P to tile format. Dual-tile pattern assumes Bc is a
916
+ // multiple of 64 (enforced by bc_unit=64 in hmx_fa_find_chunk_size),
917
+ // so both tile halves are always in the current r0 block.
918
+ __fp16 * out_dual_tile = p_st_base + (c / 64) * HMX_FP16_TILE_N_ELMS * 2;
919
+ HVX_Vector * pv_p_out0 = ((HVX_Vector *) out_dual_tile) + r1 / 2;
920
+ HVX_Vector * pv_p_out1 = pv_p_out0 + 16;
921
+
922
+ HVX_VectorPair vp_p_dual = Q6_W_vshuff_VVR(v_p_row1_hf, v_p_row0_hf, -2);
923
+ *pv_p_out0 = Q6_V_lo_W(vp_p_dual);
924
+ *pv_p_out1 = Q6_V_hi_W(vp_p_dual);
925
+
926
+ HVX_VectorPair vp_p0 = hvx_vec_f16_to_f32_shuff(v_p_row0_hf);
927
+ HVX_VectorPair vp_p1 = hvx_vec_f16_to_f32_shuff(v_p_row1_hf);
928
+
929
+ v_p_rowsum0 = Q6_Vqf32_vadd_Vqf32Vqf32(v_p_rowsum0, Q6_Vqf32_vadd_VsfVsf(Q6_V_lo_W(vp_p0), Q6_V_hi_W(vp_p0)));
930
+ v_p_rowsum1 = Q6_Vqf32_vadd_Vqf32Vqf32(v_p_rowsum1, Q6_Vqf32_vadd_VsfVsf(Q6_V_lo_W(vp_p1), Q6_V_hi_W(vp_p1)));
931
+ }
932
+
933
+ HVX_Vector rowsum0_sf = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(v_p_rowsum0));
934
+ HVX_Vector rowsum1_sf = hvx_vec_reduce_sum_f32(Q6_Vsf_equals_Vqf32(v_p_rowsum1));
935
+ {
936
+ // Both inputs are f32 splats, so the f32->f16 output is an fp16 splat.
937
+ HVX_Vector rv0_v = hvx_vec_f32_to_f16(rowsum0_sf, rowsum0_sf);
938
+ HVX_Vector rv1_v = hvx_vec_f32_to_f16(rowsum1_sf, rowsum1_sf);
939
+
940
+ HVX_VectorPred p_start = Q6_Q_vsetq_R(r_vec_off * 2);
941
+ HVX_VectorPred p_mid = Q6_Q_vsetq_R((r_vec_off + 1) * 2);
942
+ HVX_VectorPred p_end = Q6_Q_vsetq2_R((r_vec_off + 2) * 2);
943
+ HVX_VectorPred p_lane0 = Q6_Q_and_QQn(p_mid, p_start);
944
+ HVX_VectorPred p_lane1 = Q6_Q_and_QQn(p_end, p_mid);
945
+ rowsum_acc_v = Q6_V_vmux_QVV(p_lane0, rv0_v, rowsum_acc_v);
946
+ rowsum_acc_v = Q6_V_vmux_QVV(p_lane1, rv1_v, rowsum_acc_v);
947
+ }
948
+ }
949
+
950
+ factx->vtcm_s_rowmax[r_vec_idx] = rowmax_acc_v;
951
+ factx->vtcm_p_rowsum[r_vec_idx] = rowsum_acc_v;
952
+ }
953
+ }
954
+
955
+ // Serial m/l update + build_D. Must run after softmax barrier (s_rowmax written by all threads).
956
+ //
957
+ // noinline: function boundary acts as a hard compiler barrier so the (size_t)addr scatter
958
+ // intrinsics inside cannot be hoisted past the call site. Mirrors the structural protection
959
+ // matmul gets for free via worker_pool function-pointer dispatch. Without this, the compiler
960
+ // can reorder the scatter past the subsequent hmx_queue_push and the HMX-queue worker thread
961
+ // reads stale VTCM (PPL → ~vocab-size).
962
+ static __attribute__((noinline)) void fa_ml_update_and_build_d(struct hmx_fa_context * factx,
963
+ size_t n_rows_g,
964
+ size_t n_row_tiles,
965
+ size_t n_row_tiles_g_br) {
966
+ // Reuse s_rowmax buffer for exp(m_diff) — safe because softmax is fully complete
967
+ HVX_Vector * const mvec_exp_m_diff = factx->vtcm_s_rowmax;
968
+
969
+ const size_t n_row_vec_cnt = hmx_ceil_div(n_rows_g, 64);
970
+ for (size_t i = 0; i < n_row_vec_cnt; ++i) {
971
+ HVX_Vector v_m_prev = factx->vtcm_m_vec[i];
972
+ HVX_Vector v_m_curr = Q6_Vhf_vmax_VhfVhf(v_m_prev, factx->vtcm_s_rowmax[i]);
973
+ HVX_Vector v_m_diff = Q6_Vqf16_vsub_VhfVhf(v_m_prev, v_m_curr);
974
+
975
+ #ifdef HMX_FA_USE_EXP2_HF
976
+ // Base-2 path: must match P = exp2(S - m_new) in fa_softmax_thread.
977
+ HVX_Vector v_exp_m_diff = hvx_exp2_hf(Q6_Vhf_equals_Vqf16(v_m_diff));
978
+ #else
979
+ HVX_VectorPair vp_diff = hvx_vec_f16_to_f32_shuff(Q6_Vhf_equals_Vqf16(v_m_diff));
980
+ HVX_Vector exp_lo = hvx_vec_exp_f32(Q6_V_lo_W(vp_diff));
981
+ HVX_Vector exp_hi = hvx_vec_exp_f32(Q6_V_hi_W(vp_diff));
982
+ HVX_Vector v_exp_m_diff = hvx_vec_f32_to_f16_shuff(exp_lo, exp_hi);
983
+ #endif
984
+
985
+ HVX_Vector v_l_curr = Q6_Vqf16_vmpy_Vqf16Vhf(factx->vtcm_l_vec[i], v_exp_m_diff);
986
+ v_l_curr = Q6_Vqf16_vadd_Vqf16Vhf(v_l_curr, factx->vtcm_p_rowsum[i]);
987
+
988
+ factx->vtcm_m_vec[i] = v_m_curr;
989
+ factx->vtcm_l_vec[i] = v_l_curr;
990
+ mvec_exp_m_diff[i] = v_exp_m_diff;
991
+ }
992
+
993
+ // Build diagonal tile D = diag(exp(m_diff))
994
+ const HVX_Vector v_offsets = *(const HVX_Vector *) d_tile_scatter_offsets;
995
+ const HVX_VectorPred q_32_mask = Q6_Q_vsetq_R(32 * sizeof(__fp16));
996
+ for (size_t i = 0; i < n_row_tiles; ++i) {
997
+ const HVX_Vector v_content = Q6_V_vror_VR(mvec_exp_m_diff[i / 2], (i % 2) * 64);
998
+ __fp16 * out_base = factx->vtcm_d_tiles + i * (n_row_tiles_g_br + 1) * HMX_FP16_TILE_N_ELMS;
999
+ Q6_vscatter_QRMVhV(q_32_mask, (size_t) out_base, HMX_FP16_TILE_SIZE - 1, v_offsets, v_content);
1000
+ // Compiler barrier — Q6_vscatter takes (size_t)addr; without this the
1001
+ // compiler may not recognize the volatile read below as aliasing and
1002
+ // could reorder it before the scatter, defeating the HW drain.
1003
+ __asm__ __volatile__("" ::: "memory");
1004
+ // Per-tile drain: scatter regions are disjoint (stride > tile size),
1005
+ // so a single drain at tile 0 does NOT retire later tiles' entries.
1006
+ (void) *(volatile HVX_Vector *) out_base;
1007
+ }
1008
+ }
1009
+
1010
+ // Build D = diag(1/l) tile for the final O = D @ O normalization.
1011
+ //
1012
+ // noinline: same rationale as fa_ml_update_and_build_d — keeps Q6_vscatter from
1013
+ // being hoisted past the subsequent hmx_queue_push at the o_norm call site.
1014
+ static __attribute__((noinline)) void fa_build_d_diag_inv_l(struct hmx_fa_context * factx,
1015
+ size_t n_row_tiles,
1016
+ size_t n_row_tiles_g_br) {
1017
+ const HVX_Vector v_offsets = *(const HVX_Vector *) d_tile_scatter_offsets;
1018
+ const HVX_VectorPred q_32_mask = Q6_Q_vsetq_R(32 * sizeof(__fp16));
1019
+ const HVX_Vector one = hvx_vec_splat_f32(1.0f);
1020
+
1021
+ HVX_Vector v_content = Q6_V_vzero();
1022
+ for (size_t i = 0; i < n_row_tiles; ++i) {
1023
+ if ((i % 2) == 0) {
1024
+ HVX_Vector v_l_hf = Q6_Vhf_equals_Vqf16(factx->vtcm_l_vec[i / 2]);
1025
+ HVX_VectorPair vp_l = hvx_vec_f16_to_f32_shuff(v_l_hf);
1026
+ HVX_Vector inv_lo = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(one, hvx_vec_inverse_f32(Q6_V_lo_W(vp_l))));
1027
+ HVX_Vector inv_hi = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(one, hvx_vec_inverse_f32(Q6_V_hi_W(vp_l))));
1028
+ v_content = hvx_vec_f32_to_f16_shuff(inv_lo, inv_hi);
1029
+ } else {
1030
+ v_content = Q6_V_vror_VR(v_content, 64);
1031
+ }
1032
+
1033
+ __fp16 * out_base = factx->vtcm_d_tiles + i * (n_row_tiles_g_br + 1) * HMX_FP16_TILE_N_ELMS;
1034
+ Q6_vscatter_QRMVhV(q_32_mask, (size_t) out_base, HMX_FP16_TILE_SIZE - 1, v_offsets, v_content);
1035
+ // Compiler barrier — see fa_ml_update_and_build_d for rationale.
1036
+ __asm__ __volatile__("" ::: "memory");
1037
+ (void) *(volatile HVX_Vector *) out_base;
1038
+ }
1039
+ }
1040
+
1041
+ // Combined: multi-thread softmax -> barrier -> serial m/l update + build_D
1042
+ static void fa_phase_softmax_and_build_d(struct hmx_fa_context * factx,
1043
+ fa_softmax_args_t * sargs,
1044
+ size_t n_row_tiles,
1045
+ size_t n_row_tiles_g_br) {
1046
+ worker_pool_context_t wp = factx->octx->ctx->worker_pool;
1047
+ const size_t n_row_vec_cnt = hmx_ceil_div(sargs->n_rows_g, 64);
1048
+
1049
+ if (factx->n_threads > 1 && n_row_vec_cnt >= 2) {
1050
+ uint32_t n_use = (uint32_t) hex_smin((size_t) factx->n_threads, n_row_vec_cnt);
1051
+ worker_pool_run_func(wp, fa_softmax_thread, sargs, n_use);
1052
+ } else {
1053
+ fa_softmax_thread(1, 0, sargs);
1054
+ }
1055
+ // barrier implicit in worker_pool_run_func return
1056
+
1057
+ fa_ml_update_and_build_d(factx, sargs->n_rows_g, n_row_tiles, n_row_tiles_g_br);
1058
+ }
1059
+
1060
+ // ============================================================================
1061
+ // HMX job structs and worker functions
1062
+ // ============================================================================
1063
+
1064
+ typedef struct {
1065
+ const __fp16 * q_tiles;
1066
+ const __fp16 * k_tiles;
1067
+ __fp16 * s_tiles;
1068
+ size_t n_row_tiles;
1069
+ size_t n_col_tiles;
1070
+ size_t n_dot_tiles; // DK / 32
1071
+ size_t n_tiles_per_bc;
1072
+ uint8_t * hmx_scales;
1073
+ } hmx_fa_qk_job_t;
1074
+
1075
+ static void hmx_fa_qk_dot_worker(void * data) {
1076
+ hmx_fa_qk_job_t * job = (hmx_fa_qk_job_t *) data;
1077
+ const size_t n_row_tiles = job->n_row_tiles;
1078
+ const size_t n_col_tiles = job->n_col_tiles;
1079
+ const size_t n_dot_tiles = job->n_dot_tiles;
1080
+ const size_t n_tiles_per_bc = job->n_tiles_per_bc;
1081
+ const __fp16 * restrict q_tiles = job->q_tiles;
1082
+ const __fp16 * restrict k_tiles = job->k_tiles;
1083
+ __fp16 * restrict s_tiles = job->s_tiles;
1084
+ __builtin_assume(n_row_tiles > 0);
1085
+ __builtin_assume(n_col_tiles > 0);
1086
+ __builtin_assume(n_dot_tiles > 0);
1087
+
1088
+ Q6_bias_mxmem2_A((void *) job->hmx_scales);
1089
+ for (size_t r = 0; r < n_row_tiles; ++r) {
1090
+ for (size_t c = 0; c < n_col_tiles; ++c) {
1091
+ const __fp16 * row_tiles = q_tiles + r * HMX_FP16_TILE_N_ROWS * n_dot_tiles * HMX_FP16_TILE_N_COLS;
1092
+ const __fp16 * col_tiles = k_tiles + c * HMX_FP16_TILE_N_COLS * n_dot_tiles * HMX_FP16_TILE_N_COLS;
1093
+ __fp16 * out_tile = s_tiles + (r * n_tiles_per_bc + c) * HMX_FP16_TILE_N_ELMS;
1094
+
1095
+ for (size_t k = 0; k < n_dot_tiles; ++k) {
1096
+ Q6_activation_hf_mxmem_RR((unsigned int) row_tiles, 2047);
1097
+ Q6_weight_hf_mxmem_RR((unsigned int) col_tiles, 2047);
1098
+ row_tiles += HMX_FP16_TILE_N_ELMS;
1099
+ col_tiles += HMX_FP16_TILE_N_ELMS;
1100
+ }
1101
+ Q6_mxmem_AR_after_hf(out_tile, 0);
1102
+ }
1103
+ }
1104
+ }
1105
+
1106
+ typedef struct {
1107
+ __fp16 * o_curr;
1108
+ const __fp16 * o_prev;
1109
+ const __fp16 * p_tiles;
1110
+ const __fp16 * v_tiles;
1111
+ const __fp16 * d_tiles;
1112
+ uint8_t * hmx_scales;
1113
+ size_t n_row_tiles;
1114
+ size_t n_col_tiles;
1115
+ size_t n_row_tiles_g_br;
1116
+ size_t n_tiles_per_bc;
1117
+ size_t DV;
1118
+ } hmx_fa_o_update_job_t;
1119
+
1120
+ static void hmx_fa_o_update_worker(void * data) {
1121
+ hmx_fa_o_update_job_t * job = (hmx_fa_o_update_job_t *) data;
1122
+ const size_t n_row_tiles = job->n_row_tiles;
1123
+ const size_t n_col_tiles = job->n_col_tiles;
1124
+ const size_t n_row_tiles_g_br = job->n_row_tiles_g_br;
1125
+ const size_t n_tiles_per_bc = job->n_tiles_per_bc;
1126
+ const size_t DV_tiles = job->DV / 32;
1127
+ const __fp16 * restrict d_tiles = job->d_tiles;
1128
+ const __fp16 * restrict p_tiles = job->p_tiles;
1129
+ const __fp16 * restrict v_tiles = job->v_tiles;
1130
+ const __fp16 * restrict o_prev = job->o_prev;
1131
+ __fp16 * restrict o_curr = job->o_curr;
1132
+ __builtin_assume(n_row_tiles > 0);
1133
+ __builtin_assume(n_col_tiles > 0);
1134
+ __builtin_assume(DV_tiles > 0);
1135
+
1136
+ Q6_bias_mxmem2_A((void *) job->hmx_scales);
1137
+ for (size_t r = 0; r < n_row_tiles; ++r) {
1138
+ for (size_t c = 0; c < DV_tiles; ++c) {
1139
+ // D[r,r] @ O_prev[r,c] — only the diagonal tile
1140
+ const __fp16 * d_diag = d_tiles + r * (n_row_tiles_g_br + 1) * HMX_FP16_TILE_N_ELMS;
1141
+ const __fp16 * o_rc = o_prev + (c * n_row_tiles_g_br + r) * HMX_FP16_TILE_N_ELMS;
1142
+ Q6_activation_hf_mxmem_RR((unsigned int) d_diag, 2047);
1143
+ Q6_weight_hf_mxmem_RR((unsigned int) o_rc, 2047);
1144
+
1145
+ // P @ V (accumulate on same accumulator)
1146
+ const __fp16 * p_tile_in = p_tiles + (r * n_tiles_per_bc) * HMX_FP16_TILE_N_ELMS;
1147
+ const __fp16 * v_tile_in = v_tiles + (c * n_tiles_per_bc) * HMX_FP16_TILE_N_ELMS;
1148
+ for (size_t k = 0; k < n_col_tiles; ++k) {
1149
+ Q6_activation_hf_mxmem_RR((unsigned int) p_tile_in, 2047);
1150
+ Q6_weight_hf_mxmem_RR((unsigned int) v_tile_in, 2047);
1151
+ p_tile_in += HMX_FP16_TILE_N_ELMS;
1152
+ v_tile_in += HMX_FP16_TILE_N_ELMS;
1153
+ }
1154
+
1155
+ __fp16 * o_tile_out = o_curr + (c * n_row_tiles_g_br + r) * HMX_FP16_TILE_N_ELMS;
1156
+ Q6_mxmem_AR_after_hf(o_tile_out, 0);
1157
+ }
1158
+ }
1159
+ }
1160
+
1161
+ typedef struct {
1162
+ __fp16 * o_curr; // output (row-major tile layout)
1163
+ const __fp16 * o_prev; // input (column-major tile layout)
1164
+ const __fp16 * d_tiles; // diag(1/l) tiles
1165
+ uint8_t * hmx_scales;
1166
+ size_t n_row_tiles;
1167
+ size_t n_row_tiles_g_br;
1168
+ size_t DV;
1169
+ } hmx_fa_o_norm_job_t;
1170
+
1171
+ static void hmx_fa_o_norm_worker(void * data) {
1172
+ hmx_fa_o_norm_job_t * job = (hmx_fa_o_norm_job_t *) data;
1173
+ const size_t n_row_tiles = job->n_row_tiles;
1174
+ const size_t n_row_tiles_g_br = job->n_row_tiles_g_br;
1175
+ const size_t DV_tiles = job->DV / 32;
1176
+ const __fp16 * restrict d_tiles = job->d_tiles;
1177
+ const __fp16 * restrict o_prev = job->o_prev;
1178
+ __fp16 * restrict o_curr = job->o_curr;
1179
+ __builtin_assume(n_row_tiles > 0);
1180
+ __builtin_assume(DV_tiles > 0);
1181
+
1182
+ Q6_bias_mxmem2_A((void *) job->hmx_scales);
1183
+ for (size_t r = 0; r < n_row_tiles; ++r) {
1184
+ for (size_t c = 0; c < DV_tiles; ++c) {
1185
+ const __fp16 * d_diag = d_tiles + r * (n_row_tiles_g_br + 1) * HMX_FP16_TILE_N_ELMS;
1186
+ const __fp16 * o_rc = o_prev + (c * n_row_tiles_g_br + r) * HMX_FP16_TILE_N_ELMS;
1187
+ __fp16 * o_out = o_curr + (r * DV_tiles + c) * HMX_FP16_TILE_N_ELMS;
1188
+
1189
+ Q6_activation_hf_mxmem_RR((unsigned int) d_diag, 2047);
1190
+ Q6_weight_hf_mxmem_RR((unsigned int) o_rc, 2047);
1191
+ Q6_mxmem_AR_after_hf(o_out, 0);
1192
+ }
1193
+ }
1194
+ }
1195
+
1196
+ // Populate per-GQA-row ALiBi slopes for a given KV head.
1197
+ // Row r in the GQA-merged block maps to Q head h = kv_head * G + r % G.
1198
+ // slope(h) = m0^(h+1) when h < n_head_log2, else m1^(2*(h-n_head_log2)+1).
1199
+ // When max_bias == 0, all slopes are 1.0 (no ALiBi).
1200
+ static __attribute__((noinline)) void fa_compute_slopes(
1201
+ const struct hmx_fa_context * factx,
1202
+ uint32_t kv_head,
1203
+ size_t n_rows_g) {
1204
+ __fp16 * slopes = factx->vtcm_slopes;
1205
+ if (factx->max_bias == 0.0f) {
1206
+ hvx_splat_f16_a(slopes, 1.0f, n_rows_g);
1207
+ return;
1208
+ }
1209
+
1210
+ const uint32_t G = factx->G;
1211
+ const uint32_t n_head_log2 = factx->n_head_log2;
1212
+ const float m0 = factx->m0;
1213
+ const float m1 = factx->m1;
1214
+
1215
+ __fp16 temp_slopes[512] __attribute__((aligned(128)));
1216
+ if (G <= 32) {
1217
+ // Fast path: Compute G unique slope values in vector registers
1218
+ HVX_Vector v_val = hvx_alibi_slopes(kv_head, G, n_head_log2, m0, m1);
1219
+
1220
+ __fp16 temp_slopes_aligned[64] __attribute__((aligned(128)));
1221
+ hvx_vmem(temp_slopes_aligned) = hvx_vec_f32_to_f16(v_val, Q6_V_vzero());
1222
+
1223
+ for (uint32_t i = 0; i < G; ++i) {
1224
+ temp_slopes[i] = temp_slopes_aligned[i];
1225
+ }
1226
+ } else {
1227
+ // Fallback path: G > 32 (rare configurations)
1228
+ for (uint32_t i = 0; i < G; ++i) {
1229
+ temp_slopes[i] = (__fp16)alibi_slope(kv_head * G + i, n_head_log2, m0, m1);
1230
+ }
1231
+ }
1232
+
1233
+ // Allocate stack buffer to avoid scalar writes to VTCM (which generates L2 misses)
1234
+ __fp16 local_slopes[n_rows_g] __attribute__((aligned(128)));
1235
+ for (size_t r = 0; r < n_rows_g; ++r) {
1236
+ local_slopes[r] = temp_slopes[fastmodulo(r, G, &factx->div_G)];
1237
+ }
1238
+
1239
+ // Copy to VTCM slopes using HVX block copy (both are aligned to 128 bytes)
1240
+ hvx_copy_f16_aa((uint8_t *)slopes, (const uint8_t *)local_slopes, n_rows_g);
1241
+ }
1242
+
1243
+ // ============================================================================
1244
+ // Core HMX flash attention algorithm (GQA-merged)
1245
+ // ============================================================================
1246
+
1247
+ int hmx_flash_attn_ext(struct htp_ops_context * octx) {
1248
+ const struct htp_tensor * q = octx->src[0];
1249
+ const struct htp_tensor * k = octx->src[1];
1250
+ const struct htp_tensor * v = octx->src[2];
1251
+ const struct htp_tensor * mask = (octx->src[3] && octx->src[3]->data) ? octx->src[3] : NULL;
1252
+ const struct htp_tensor * dst = octx->dst;
1253
+
1254
+ struct htp_context * const ctx = octx->ctx;
1255
+
1256
+ if (!ctx->hmx_enabled) {
1257
+ return HTP_STATUS_NO_SUPPORT;
1258
+ }
1259
+
1260
+ // Dimensions
1261
+ const uint32_t neq0 = q->ne[0]; // head_dim (DK)
1262
+ const uint32_t neq1 = q->ne[1]; // n_tokens
1263
+ const uint32_t neq2 = q->ne[2]; // n_heads
1264
+ const uint32_t neq3 = q->ne[3]; // n_seqs
1265
+
1266
+ const uint32_t nek0 = k->ne[0]; // head_dim
1267
+ const uint32_t nek1 = k->ne[1]; // kv_len
1268
+
1269
+ const uint32_t nev0 = v->ne[0]; // head_dim (DV)
1270
+
1271
+ const uint32_t DK = neq0;
1272
+ const uint32_t DV = nev0;
1273
+
1274
+ // HMX requires head_dim to be multiple of 32
1275
+ if (DK % 32 != 0 || DV % 32 != 0) {
1276
+ return HTP_STATUS_NO_SUPPORT;
1277
+ }
1278
+
1279
+ // GQA factor
1280
+ const uint32_t n_kv_heads = k->ne[2];
1281
+ const uint32_t G = neq2 / n_kv_heads;
1282
+
1283
+ // Thread count for multi-thread HVX phases
1284
+ const uint32_t n_threads_init = octx->n_threads;
1285
+
1286
+ // Compute dynamic block sizes (GQA-aware, accounting for per-thread row bufs)
1287
+ size_t Br, Bc;
1288
+ const size_t vtcm_budget = ctx->vtcm_size;
1289
+ if (hmx_fa_find_chunk_size(&Br, &Bc, G, DK, DV, neq1, nek1, vtcm_budget, n_threads_init) != 0) {
1290
+ return HTP_STATUS_VTCM_TOO_SMALL;
1291
+ }
1292
+
1293
+ const size_t g_br = hex_align_up(G * Br, HMX_FP16_TILE_N_ROWS);
1294
+
1295
+ const uint32_t n_kv_blocks = (nek1 + Bc - 1) / Bc;
1296
+ const bool use_pipeline = (n_kv_blocks >= FA_MIN_KV_BLOCKS && n_threads_init >= 2);
1297
+
1298
+ // Bypass thread pool dispatch for small prompts/non-pipelined prefill by setting n_threads = 1
1299
+ const uint32_t n_threads = use_pipeline ? n_threads_init : 1;
1300
+
1301
+ FARF(HIGH, "hmx-fa: neq1=%u nek1=%u DK=%u DV=%u G=%u Br=%zu Bc=%zu g_br=%zu n_kv_blocks=%u pipeline=%d vtcm=%zu",
1302
+ neq1, nek1, DK, DV, G, Br, Bc, g_br, n_kv_blocks, use_pipeline, vtcm_budget);
1303
+
1304
+ // ======== Build context ========
1305
+ struct hmx_fa_context factx;
1306
+ memset(&factx, 0, sizeof(factx));
1307
+ factx.octx = octx;
1308
+ factx.n_threads = n_threads;
1309
+ factx.DK = DK;
1310
+ factx.DV = DV;
1311
+ factx.n_kv = nek1;
1312
+ factx.n_kv_heads = n_kv_heads;
1313
+ factx.n_heads = neq2;
1314
+ factx.G = G;
1315
+ factx.div_G = init_fastdiv_values(G);
1316
+ factx.neq1 = neq1;
1317
+ factx.Br = (uint32_t) Br;
1318
+ factx.Bc = (uint32_t) Bc;
1319
+ factx.g_br = (uint32_t) g_br;
1320
+ factx.n_kv_blocks = n_kv_blocks;
1321
+ factx.is_q_fp32 = (q->type == HTP_TYPE_F32);
1322
+ factx.is_dst_fp32 = (dst->type == HTP_TYPE_F32);
1323
+ factx.use_pipeline = use_pipeline;
1324
+ factx.mask_broadcast = (mask != NULL && mask->ne[2] == 1);
1325
+
1326
+ // Extract op parameters (mutable during softcap adjustment, then stored as const in factx)
1327
+ float scale = 1.0f, max_bias = 0.0f, logit_softcap = 0.0f;
1328
+ memcpy(&scale, (float *) octx->op_params + 0, sizeof(float));
1329
+ memcpy(&max_bias, (float *) octx->op_params + 1, sizeof(float));
1330
+ memcpy(&logit_softcap, (float *) octx->op_params + 2, sizeof(float));
1331
+
1332
+ if (logit_softcap != 0.0f) {
1333
+ scale /= logit_softcap;
1334
+ }
1335
+
1336
+ #ifdef HMX_FA_USE_EXP2_HF
1337
+ // Pre-bake log2(e) into qk_scale so HMX-produced S tiles are in log2(e)-scaled
1338
+ // space. Then exp2(S - m) in the softmax equals base-e exp((S - m) / log2(e)),
1339
+ // preserving ggml's base-e softmax semantics. Matches htp-ops-lib flash_attn.c.
1340
+ //
1341
+ // When softcap is active we cannot pre-bake log2(e) here — it would land inside
1342
+ // the tanh argument and shift the softcap knee from x≈c to x≈c/log2(e), giving
1343
+ // numerically wrong softcapped values. Instead fold log2(e) into the post-tanh
1344
+ // multiplier (see softcap block: v_cap absorbs log2(e)).
1345
+ if (logit_softcap == 0.0f) {
1346
+ scale *= 1.44269504f; // log2(e)
1347
+ }
1348
+ #endif
1349
+
1350
+ factx.scale = scale;
1351
+ factx.max_bias = max_bias;
1352
+ factx.logit_softcap = logit_softcap;
1353
+
1354
+ factx.n_head_log2 = 1u << (uint32_t) floor(log2(neq2));
1355
+ factx.m0 = powf(2.0f, -(max_bias) / factx.n_head_log2);
1356
+ factx.m1 = powf(2.0f, -(max_bias / 2.0f) / factx.n_head_log2);
1357
+
1358
+ // ======== VTCM allocation (GQA-aware) ========
1359
+ const size_t size_k_row = DK * sizeof(__fp16);
1360
+ const size_t size_v_row = DV * sizeof(__fp16);
1361
+ const size_t size_k_row_padded = hex_round_up(size_k_row, 128);
1362
+ const size_t size_v_row_padded = hex_round_up(size_v_row, 128);
1363
+
1364
+ const size_t q_tile_bytes = hex_align_up(g_br * DK * sizeof(__fp16), 4096);
1365
+ const size_t o_tile_bytes = hex_align_up(g_br * DV * sizeof(__fp16), 4096);
1366
+ const size_t k_dma_bytes = hex_align_up(Bc * size_k_row_padded, 4096);
1367
+ const size_t v_dma_bytes = hex_align_up(Bc * size_v_row_padded, 4096);
1368
+ const size_t k_tile_bytes = hex_align_up(Bc * DK * sizeof(__fp16), 4096);
1369
+ const size_t v_tile_bytes = hex_align_up(Bc * DV * sizeof(__fp16), 4096);
1370
+ const size_t s_tile_bytes = hex_align_up(g_br * Bc * sizeof(__fp16), 4096);
1371
+ const size_t d_tile_bytes = hex_align_up(g_br * g_br * sizeof(__fp16), 4096);
1372
+ const size_t col_vec_bytes = hex_align_up(g_br * sizeof(__fp16), 256);
1373
+ const size_t row_vec_bytes = hex_align_up(Bc * sizeof(__fp16), 256);
1374
+ const size_t m_line_bytes = hex_align_up(Bc * sizeof(__fp16), 128);
1375
+ const size_t m_buf_bytes = hex_align_up(Br * m_line_bytes, 4096);
1376
+ const size_t slopes_bytes = hex_align_up(g_br * sizeof(__fp16), 128);
1377
+
1378
+ uint8_t * vtcm_cur = ctx->vtcm_base;
1379
+
1380
+ factx.vtcm_q_tiles = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, q_tile_bytes);
1381
+ factx.vtcm_o_tiles[0] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, o_tile_bytes);
1382
+ factx.vtcm_o_tiles[1] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, o_tile_bytes);
1383
+ factx.vtcm_k_fp16[0] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, k_dma_bytes);
1384
+ factx.vtcm_k_fp16[1] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, k_dma_bytes);
1385
+ factx.vtcm_v_fp16[0] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, v_dma_bytes);
1386
+ factx.vtcm_v_fp16[1] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, v_dma_bytes);
1387
+ factx.vtcm_k_tiles = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, k_tile_bytes);
1388
+ factx.vtcm_v_tiles[0] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, v_tile_bytes);
1389
+ if (use_pipeline) {
1390
+ factx.vtcm_v_tiles[1] = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, v_tile_bytes);
1391
+ } else {
1392
+ factx.vtcm_v_tiles[1] = NULL;
1393
+ }
1394
+ factx.vtcm_s_tiles = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, s_tile_bytes);
1395
+ factx.vtcm_p_tiles = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, s_tile_bytes);
1396
+ factx.vtcm_d_tiles = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, d_tile_bytes);
1397
+ factx.vtcm_m_vec = (HVX_Vector *) vtcm_seq_alloc(&vtcm_cur, col_vec_bytes);
1398
+ factx.vtcm_l_vec = (HVX_Vector *) vtcm_seq_alloc(&vtcm_cur, col_vec_bytes);
1399
+ factx.vtcm_s_rowmax = (HVX_Vector *) vtcm_seq_alloc(&vtcm_cur, col_vec_bytes);
1400
+ factx.vtcm_p_rowsum = (HVX_Vector *) vtcm_seq_alloc(&vtcm_cur, col_vec_bytes);
1401
+ factx.vtcm_row_bufs = (HVX_Vector *) vtcm_seq_alloc(&vtcm_cur, row_vec_bytes * 2 * n_threads);
1402
+ factx.row_buf_stride = row_vec_bytes / sizeof(HVX_Vector);
1403
+ factx.vtcm_hmx_scales_id = vtcm_seq_alloc(&vtcm_cur, 256);
1404
+ factx.vtcm_hmx_scales_qk = vtcm_seq_alloc(&vtcm_cur, 256);
1405
+ factx.vtcm_mask_buf = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, m_buf_bytes);
1406
+ factx.mask_buf_row_stride = m_line_bytes / sizeof(__fp16);
1407
+ factx.vtcm_slopes = (__fp16 *) vtcm_seq_alloc(&vtcm_cur, slopes_bytes);
1408
+
1409
+ if ((size_t) (vtcm_cur - ctx->vtcm_base) > ctx->vtcm_size) {
1410
+ return HTP_STATUS_VTCM_TOO_SMALL;
1411
+ }
1412
+
1413
+ // ======== Initialize HMX output scales ========
1414
+ // Identity scale (1.0) for O updates and normalization
1415
+ hmx_init_column_scales(factx.vtcm_hmx_scales_id, Q6_V_vsplat_R(0x3c00)); // 1.0
1416
+
1417
+ // QK scale embedded in HMX output
1418
+ hmx_init_column_scales(factx.vtcm_hmx_scales_qk, hvx_vec_splat_f16(factx.scale));
1419
+
1420
+ // ======== Skip compute if profiling ========
1421
+ if (octx->flags & HTP_OPFLAGS_SKIP_COMPUTE) {
1422
+ return HTP_STATUS_OK;
1423
+ }
1424
+
1425
+ // Profiling timers
1426
+ TIMER_DEFINE(total);
1427
+ TIMER_DEFINE(q_load);
1428
+ TIMER_DEFINE(kv_dma);
1429
+ TIMER_DEFINE(k_interleave);
1430
+ TIMER_DEFINE(v_interleave);
1431
+ TIMER_DEFINE(qk_dot);
1432
+ TIMER_DEFINE(softmax);
1433
+ TIMER_DEFINE(o_update);
1434
+ TIMER_DEFINE(o_norm);
1435
+ TIMER_DEFINE(o_store);
1436
+
1437
+ TIMER_START(total);
1438
+
1439
+ // ======== DMA setup ========
1440
+ dma_queue * const dma = ctx->dma[0];
1441
+
1442
+ // Padded row sizes for DMA (defined in outer scope)
1443
+
1444
+ const size_t n_row_tiles_g_br = g_br / HMX_FP16_TILE_N_ROWS;
1445
+ const size_t n_tiles_per_bc = Bc / HMX_FP16_TILE_N_COLS;
1446
+
1447
+ // Q/O element size for Q load and O store
1448
+ const size_t qo_element_size = factx.is_q_fp32 ? sizeof(float) : sizeof(__fp16);
1449
+
1450
+ // ======== HMX lock strategy ========
1451
+ // Pipeline: queue thread auto-acquires HMX lock on first push; released by suspend.
1452
+ // Fallback: main thread holds the lock (original behavior).
1453
+ if (!factx.use_pipeline) {
1454
+ HAP_compute_res_hmx_lock(ctx->vtcm_rctx);
1455
+ }
1456
+
1457
+ // ======== Reusable job descriptors for pipeline ========
1458
+ hmx_fa_qk_job_t qk_job;
1459
+ hmx_fa_o_update_job_t ou_job;
1460
+ hmx_fa_o_norm_job_t on_job;
1461
+
1462
+ // ======== Main loop: per batch, per KV head, per Q block ========
1463
+ for (uint32_t ib3 = 0; ib3 < neq3; ++ib3) {
1464
+ for (uint32_t kv_head = 0; kv_head < n_kv_heads; ++kv_head) {
1465
+ const uint32_t ik2 = kv_head;
1466
+ const uint32_t ik3 = ib3 / (neq3 / k->ne[3]);
1467
+ const uint32_t iv2 = kv_head;
1468
+ const uint32_t iv3 = ib3 / (neq3 / v->ne[3]);
1469
+
1470
+ for (uint32_t q_start = 0; q_start < neq1; q_start += Br) {
1471
+ const uint32_t n_q_rows = hex_smin(Br, neq1 - q_start);
1472
+ const size_t n_rows_g = n_q_rows * G;
1473
+ const size_t g_br_actual = hex_align_up(n_rows_g, HMX_FP16_TILE_N_ROWS);
1474
+ const size_t n_row_tiles = g_br_actual / HMX_FP16_TILE_N_ROWS;
1475
+
1476
+ // ---- Load Q block [g_br, D] -> tiles, interleaving G heads ----
1477
+ TIMER_START(q_load);
1478
+ if (n_rows_g < g_br) {
1479
+ hvx_splat_u8_a(factx.vtcm_q_tiles, 0, q_tile_bytes);
1480
+ }
1481
+ fa_phase_q_load(&factx, q, q_start, kv_head, ib3, n_rows_g);
1482
+ TIMER_STOP(q_load);
1483
+
1484
+ // ---- Initialize per-block state ----
1485
+ hvx_splat_u8_a(factx.vtcm_l_vec, 0, col_vec_bytes);
1486
+ hvx_splat_u8_a(factx.vtcm_d_tiles, 0, d_tile_bytes);
1487
+ hvx_splat_u16_a(factx.vtcm_m_vec, 0xfbff, col_vec_bytes/2);
1488
+
1489
+ __fp16 * o_tile_prev = factx.vtcm_o_tiles[0];
1490
+ __fp16 * o_tile_curr = factx.vtcm_o_tiles[1];
1491
+ hvx_splat_u8_a(o_tile_prev, 0, o_tile_bytes);
1492
+
1493
+ // ---- KV block loop with DMA double-buffering ----
1494
+ size_t buf_idx = 0;
1495
+
1496
+ fa_compute_slopes(&factx, kv_head, n_rows_g);
1497
+
1498
+ // Prefetch first KV block
1499
+ if (factx.n_kv_blocks > 0) {
1500
+ const uint32_t kv_rows0 = hex_smin(Bc, nek1);
1501
+
1502
+ const uint8_t * k_src = (const uint8_t *) k->data + ik2 * k->nb[2] + ik3 * k->nb[3];
1503
+ dma_queue_push(dma, dma_make_ptr(factx.vtcm_k_fp16[0], k_src), size_k_row_padded, k->nb[1],
1504
+ size_k_row, kv_rows0);
1505
+
1506
+ const uint8_t * v_src = (const uint8_t *) v->data + iv2 * v->nb[2] + iv3 * v->nb[3];
1507
+ dma_queue_push(dma, dma_make_ptr(factx.vtcm_v_fp16[0], v_src), size_v_row_padded, v->nb[1],
1508
+ size_v_row, kv_rows0);
1509
+ }
1510
+
1511
+ // Mask DMA: single 2D transfer of n_q_rows unique mask rows into VTCM buffer.
1512
+ // Only when mask is head-broadcast (ne[2]==1); otherwise softmax reads DDR directly.
1513
+ #define MASK_DMA_PUSH(kv_start_val, kv_rows_val, has_mask_dma_var) \
1514
+ do { \
1515
+ has_mask_dma_var = false; \
1516
+ if (mask && factx.mask_broadcast) { \
1517
+ const uint32_t _im3 = ib3 % mask->ne[3]; \
1518
+ const uint8_t * _ms = (const uint8_t *) mask->data + q_start * mask->nb[1] + _im3 * mask->nb[3] + \
1519
+ (kv_start_val) * sizeof(__fp16); \
1520
+ dma_queue_push(dma, dma_make_ptr(factx.vtcm_mask_buf, _ms), m_line_bytes, mask->nb[1], \
1521
+ (kv_rows_val) * sizeof(__fp16), n_q_rows); \
1522
+ has_mask_dma_var = true; \
1523
+ } \
1524
+ } while (0)
1525
+
1526
+ #define MASK_DMA_POP(has_mask_dma_var) \
1527
+ do { \
1528
+ if (has_mask_dma_var) { \
1529
+ dma_queue_pop(dma); \
1530
+ } \
1531
+ } while (0)
1532
+
1533
+ #define DMA_PREFETCH_KV(blk_val) \
1534
+ do { \
1535
+ if ((blk_val) < factx.n_kv_blocks) { \
1536
+ const uint32_t _ns = (blk_val) * Bc; \
1537
+ const uint32_t _nr = hex_smin(Bc, nek1 - _ns); \
1538
+ size_t _nb = 1 - buf_idx; \
1539
+ const uint8_t * _ks = (const uint8_t *) k->data + _ns * k->nb[1] + ik2 * k->nb[2] + ik3 * k->nb[3]; \
1540
+ dma_queue_push(dma, dma_make_ptr(factx.vtcm_k_fp16[_nb], _ks), size_k_row_padded, k->nb[1], size_k_row, _nr); \
1541
+ const uint8_t * _vs = (const uint8_t *) v->data + _ns * v->nb[1] + iv2 * v->nb[2] + iv3 * v->nb[3]; \
1542
+ dma_queue_push(dma, dma_make_ptr(factx.vtcm_v_fp16[_nb], _vs), size_v_row_padded, v->nb[1], size_v_row, _nr); \
1543
+ } \
1544
+ } while (0)
1545
+
1546
+ const size_t k_src_stride = size_k_row_padded / sizeof(__fp16);
1547
+ const size_t v_src_stride = size_v_row_padded / sizeof(__fp16);
1548
+
1549
+ if (factx.use_pipeline) {
1550
+ // ==================================================================
1551
+ // Pipeline path: HVX phases ‖ HMX queue worker
1552
+ // ==================================================================
1553
+ struct hmx_queue * hmx_q = ctx->hmx_queue;
1554
+
1555
+ for (uint32_t kv_blk = 0; kv_blk < factx.n_kv_blocks; ++kv_blk) {
1556
+ const uint32_t kv_start = kv_blk * Bc;
1557
+ const uint32_t kv_rows = hex_smin(Bc, nek1 - kv_start);
1558
+ const size_t n_col_tiles = hmx_ceil_div(kv_rows, HMX_FP16_TILE_N_COLS);
1559
+
1560
+ // Wait for current KV DMA
1561
+ TIMER_START(kv_dma);
1562
+ dma_queue_pop(dma); // K
1563
+ dma_queue_pop(dma); // V
1564
+ TIMER_STOP(kv_dma);
1565
+
1566
+ // Push mask DMA for this block (single 2D DMA when broadcast)
1567
+ bool has_mask_dma = false;
1568
+ MASK_DMA_PUSH(kv_start, kv_rows, has_mask_dma);
1569
+
1570
+ // ---- Phase 1: K_int(blk) ‖ O_update(blk-1) ----
1571
+ if (kv_blk > 0) {
1572
+ // Submit O_update for previous block (HMX worker)
1573
+ ou_job.o_curr = o_tile_curr;
1574
+ ou_job.o_prev = o_tile_prev;
1575
+ ou_job.p_tiles = factx.vtcm_p_tiles;
1576
+ ou_job.v_tiles = factx.vtcm_v_tiles[1 - buf_idx];
1577
+ ou_job.d_tiles = factx.vtcm_d_tiles;
1578
+ ou_job.hmx_scales = factx.vtcm_hmx_scales_id;
1579
+ ou_job.n_row_tiles = n_row_tiles;
1580
+ ou_job.n_col_tiles = hmx_ceil_div(hex_smin(Bc, nek1 - (kv_blk - 1) * Bc), HMX_FP16_TILE_N_COLS);
1581
+ ou_job.n_row_tiles_g_br = n_row_tiles_g_br;
1582
+ ou_job.n_tiles_per_bc = n_tiles_per_bc;
1583
+ ou_job.DV = DV;
1584
+ hmx_queue_push(hmx_q, hmx_queue_make_desc(hmx_fa_o_update_worker, &ou_job));
1585
+ }
1586
+
1587
+ TIMER_START(k_interleave);
1588
+ fa_phase_k_interleave(&factx, kv_rows, k_src_stride, buf_idx);
1589
+ TIMER_STOP(k_interleave);
1590
+
1591
+ // ---- Phase 2: qk_dot(blk) on HMX ‖ V_int(blk) + DMA prefetch on HVX ----
1592
+ qk_job.q_tiles = factx.vtcm_q_tiles;
1593
+ qk_job.k_tiles = factx.vtcm_k_tiles;
1594
+ qk_job.s_tiles = factx.vtcm_s_tiles;
1595
+ qk_job.n_row_tiles = n_row_tiles;
1596
+ qk_job.n_col_tiles = n_col_tiles;
1597
+ qk_job.n_dot_tiles = DK / 32;
1598
+ qk_job.n_tiles_per_bc = n_tiles_per_bc;
1599
+ qk_job.hmx_scales = factx.vtcm_hmx_scales_qk;
1600
+ TIMER_START(qk_dot);
1601
+ hmx_queue_push(hmx_q, hmx_queue_make_desc(hmx_fa_qk_dot_worker, &qk_job));
1602
+
1603
+ // DMA push next block (non-blocking, before worker_pool)
1604
+ DMA_PREFETCH_KV(kv_blk + 1);
1605
+
1606
+ TIMER_START(v_interleave);
1607
+ fa_phase_v_interleave(&factx, kv_rows, v_src_stride, buf_idx, n_tiles_per_bc);
1608
+ TIMER_STOP(v_interleave);
1609
+
1610
+ // Pop and swap previous block's output update (deferred HMX pop)
1611
+ if (kv_blk > 0) {
1612
+ hmx_queue_pop(hmx_q);
1613
+ hex_swap_ptr((void **) &o_tile_curr, (void **) &o_tile_prev);
1614
+ }
1615
+
1616
+ // Pop current block's dot product job
1617
+ hmx_queue_pop(hmx_q);
1618
+ TIMER_STOP(qk_dot);
1619
+
1620
+ // ---- Phase 3: softmax(blk) + build_D(blk) | HMX idle ----
1621
+ // Pop mask DMA before softmax (ensures VTCM buffer is ready)
1622
+ MASK_DMA_POP(has_mask_dma);
1623
+
1624
+ fa_softmax_args_t sargs;
1625
+ memset(&sargs, 0, sizeof(sargs));
1626
+ sargs.factx = &factx;
1627
+ sargs.kv_rows = kv_rows;
1628
+ sargs.n_rows_g = n_rows_g;
1629
+ sargs.n_col_tiles = n_col_tiles;
1630
+ sargs.n_tiles_per_bc = n_tiles_per_bc;
1631
+ sargs.n_row_tiles = n_row_tiles;
1632
+ sargs.n_row_tiles_g_br = n_row_tiles_g_br;
1633
+ sargs.Bc = Bc;
1634
+ sargs.G = G;
1635
+ sargs.kv_head = kv_head;
1636
+ sargs.kv_start = kv_start;
1637
+ sargs.q_start = q_start;
1638
+ sargs.ib3 = ib3;
1639
+ sargs.has_alibi = (factx.max_bias != 0.0f);
1640
+ sargs.mask = mask;
1641
+ sargs.mask_vtcm = has_mask_dma ? (const __fp16 *) factx.vtcm_mask_buf : NULL;
1642
+ sargs.mask_vtcm_row_stride = factx.mask_buf_row_stride;
1643
+ sargs.slopes = factx.vtcm_slopes;
1644
+
1645
+ TIMER_START(softmax);
1646
+ fa_phase_softmax_and_build_d(&factx, &sargs, n_row_tiles, n_row_tiles_g_br);
1647
+ TIMER_STOP(softmax);
1648
+
1649
+ buf_idx = 1 - buf_idx;
1650
+ } // end KV block loop (pipeline)
1651
+
1652
+ // Epilogue: O_update for last block
1653
+ if (factx.n_kv_blocks > 0) {
1654
+ const uint32_t last_blk = factx.n_kv_blocks - 1;
1655
+ const size_t last_cols = hmx_ceil_div(hex_smin(Bc, nek1 - last_blk * Bc), HMX_FP16_TILE_N_COLS);
1656
+ ou_job.o_curr = o_tile_curr;
1657
+ ou_job.o_prev = o_tile_prev;
1658
+ ou_job.p_tiles = factx.vtcm_p_tiles;
1659
+ ou_job.v_tiles = factx.vtcm_v_tiles[1 - buf_idx];
1660
+ ou_job.d_tiles = factx.vtcm_d_tiles;
1661
+ ou_job.hmx_scales = factx.vtcm_hmx_scales_id;
1662
+ ou_job.n_row_tiles = n_row_tiles;
1663
+ ou_job.n_col_tiles = last_cols;
1664
+ ou_job.n_row_tiles_g_br = n_row_tiles_g_br;
1665
+ ou_job.n_tiles_per_bc = n_tiles_per_bc;
1666
+ ou_job.DV = DV;
1667
+
1668
+ TIMER_START(o_update);
1669
+ hmx_queue_push(hmx_q, hmx_queue_make_desc(hmx_fa_o_update_worker, &ou_job));
1670
+ hmx_queue_pop(hmx_q);
1671
+ TIMER_STOP(o_update);
1672
+
1673
+ hex_swap_ptr((void **) &o_tile_curr, (void **) &o_tile_prev);
1674
+ }
1675
+
1676
+ } else {
1677
+ // ==================================================================
1678
+ // Fallback path: sequential with multi-thread HVX phases
1679
+ // Main thread holds HMX lock, runs HMX inline.
1680
+ // ==================================================================
1681
+
1682
+ for (uint32_t kv_blk = 0; kv_blk < factx.n_kv_blocks; ++kv_blk) {
1683
+ const uint32_t kv_start = kv_blk * Bc;
1684
+ const uint32_t kv_rows = hex_smin(Bc, nek1 - kv_start);
1685
+ const size_t n_col_tiles = hmx_ceil_div(kv_rows, HMX_FP16_TILE_N_COLS);
1686
+
1687
+ TIMER_START(kv_dma);
1688
+ dma_queue_pop(dma); // K
1689
+ dma_queue_pop(dma); // V
1690
+ TIMER_STOP(kv_dma);
1691
+
1692
+ bool has_mask_dma = false;
1693
+ MASK_DMA_PUSH(kv_start, kv_rows, has_mask_dma);
1694
+ DMA_PREFETCH_KV(kv_blk + 1);
1695
+
1696
+ // K interleave (multi-thread HVX)
1697
+ TIMER_START(k_interleave);
1698
+ fa_phase_k_interleave(&factx, kv_rows, k_src_stride, buf_idx);
1699
+ TIMER_STOP(k_interleave);
1700
+
1701
+ // QK dot (inline HMX on main thread)
1702
+ TIMER_START(qk_dot);
1703
+ {
1704
+ const size_t n_dot_tiles = (size_t) (DK / 32);
1705
+ const __fp16 * restrict q_base = factx.vtcm_q_tiles;
1706
+ const __fp16 * restrict k_base = factx.vtcm_k_tiles;
1707
+ __fp16 * restrict s_base = factx.vtcm_s_tiles;
1708
+ __builtin_assume(n_row_tiles > 0);
1709
+ __builtin_assume(n_col_tiles > 0);
1710
+ __builtin_assume(n_dot_tiles > 0);
1711
+
1712
+ Q6_bias_mxmem2_A((void *) factx.vtcm_hmx_scales_qk);
1713
+ for (size_t r = 0; r < n_row_tiles; ++r) {
1714
+ for (size_t c = 0; c < n_col_tiles; ++c) {
1715
+ const __fp16 * row_tiles = q_base + r * HMX_FP16_TILE_N_ROWS * DK;
1716
+ const __fp16 * col_tiles = k_base + c * HMX_FP16_TILE_N_COLS * DK;
1717
+ __fp16 * out_tile = s_base + (r * n_tiles_per_bc + c) * HMX_FP16_TILE_N_ELMS;
1718
+ for (size_t k = 0; k < n_dot_tiles; ++k) {
1719
+ Q6_activation_hf_mxmem_RR((unsigned int) row_tiles, 2047);
1720
+ Q6_weight_hf_mxmem_RR((unsigned int) col_tiles, 2047);
1721
+ row_tiles += HMX_FP16_TILE_N_ELMS;
1722
+ col_tiles += HMX_FP16_TILE_N_ELMS;
1723
+ }
1724
+ Q6_mxmem_AR_after_hf(out_tile, 0);
1725
+ }
1726
+ }
1727
+ }
1728
+ TIMER_STOP(qk_dot);
1729
+
1730
+ // Pop mask DMA
1731
+ MASK_DMA_POP(has_mask_dma);
1732
+
1733
+ // Softmax + build_D (multi-thread HVX + serial m/l update)
1734
+ fa_softmax_args_t sargs;
1735
+ memset(&sargs, 0, sizeof(sargs));
1736
+ sargs.factx = &factx;
1737
+ sargs.kv_rows = kv_rows;
1738
+ sargs.n_rows_g = n_rows_g;
1739
+ sargs.n_col_tiles = n_col_tiles;
1740
+ sargs.n_tiles_per_bc = n_tiles_per_bc;
1741
+ sargs.n_row_tiles = n_row_tiles;
1742
+ sargs.n_row_tiles_g_br = n_row_tiles_g_br;
1743
+ sargs.Bc = Bc;
1744
+ sargs.G = G;
1745
+ sargs.kv_head = kv_head;
1746
+ sargs.kv_start = kv_start;
1747
+ sargs.q_start = q_start;
1748
+ sargs.ib3 = ib3;
1749
+ sargs.has_alibi = (factx.max_bias != 0.0f);
1750
+ sargs.mask = mask;
1751
+ sargs.mask_vtcm = has_mask_dma ? (const __fp16 *) factx.vtcm_mask_buf : NULL;
1752
+ sargs.mask_vtcm_row_stride = factx.mask_buf_row_stride;
1753
+ sargs.slopes = factx.vtcm_slopes;
1754
+
1755
+ TIMER_START(softmax);
1756
+ fa_phase_softmax_and_build_d(&factx, &sargs, n_row_tiles, n_row_tiles_g_br);
1757
+ TIMER_STOP(softmax);
1758
+
1759
+ // V interleave (multi-thread HVX)
1760
+ TIMER_START(v_interleave);
1761
+ // FIX(v-stride): use n_tiles_per_bc (block-invariant) as V tile layout
1762
+ // stride to match o_update's v_tile access. Using per-block n_col_tiles
1763
+ // misplaces DV_tile 1..3 in the last partial KV block.
1764
+ fa_phase_v_interleave(&factx, kv_rows, v_src_stride, buf_idx, n_tiles_per_bc);
1765
+ TIMER_STOP(v_interleave);
1766
+
1767
+ // O update (inline HMX on main thread)
1768
+ TIMER_START(o_update);
1769
+ {
1770
+ const size_t DV_tiles = (size_t) (DV / 32);
1771
+ const __fp16 * restrict d_base = factx.vtcm_d_tiles;
1772
+ const __fp16 * restrict p_base = factx.vtcm_p_tiles;
1773
+ const __fp16 * restrict v_base = factx.vtcm_v_tiles[0];
1774
+ const __fp16 * restrict op_base = o_tile_prev;
1775
+ __fp16 * restrict oc_base = o_tile_curr;
1776
+ __builtin_assume(n_row_tiles > 0);
1777
+ __builtin_assume(n_col_tiles > 0);
1778
+ __builtin_assume(DV_tiles > 0);
1779
+
1780
+ Q6_bias_mxmem2_A((void *) factx.vtcm_hmx_scales_id);
1781
+ for (size_t r = 0; r < n_row_tiles; ++r) {
1782
+ for (size_t c = 0; c < DV_tiles; ++c) {
1783
+ const __fp16 * d_diag = d_base + r * (n_row_tiles_g_br + 1) * HMX_FP16_TILE_N_ELMS;
1784
+ const __fp16 * o_rc = op_base + (c * n_row_tiles_g_br + r) * HMX_FP16_TILE_N_ELMS;
1785
+ Q6_activation_hf_mxmem_RR((unsigned int) d_diag, 2047);
1786
+ Q6_weight_hf_mxmem_RR((unsigned int) o_rc, 2047);
1787
+
1788
+ const __fp16 * p_tile_in = p_base + (r * n_tiles_per_bc) * HMX_FP16_TILE_N_ELMS;
1789
+ const __fp16 * v_tile_in = v_base + (c * n_tiles_per_bc) * HMX_FP16_TILE_N_ELMS;
1790
+ for (size_t k = 0; k < n_col_tiles; ++k) {
1791
+ Q6_activation_hf_mxmem_RR((unsigned int) p_tile_in, 2047);
1792
+ Q6_weight_hf_mxmem_RR((unsigned int) v_tile_in, 2047);
1793
+ p_tile_in += HMX_FP16_TILE_N_ELMS;
1794
+ v_tile_in += HMX_FP16_TILE_N_ELMS;
1795
+ }
1796
+
1797
+ __fp16 * o_tile_out = oc_base + (c * n_row_tiles_g_br + r) * HMX_FP16_TILE_N_ELMS;
1798
+ Q6_mxmem_AR_after_hf(o_tile_out, 0);
1799
+ }
1800
+ }
1801
+ hex_swap_ptr((void **) &o_tile_curr, (void **) &o_tile_prev);
1802
+ }
1803
+ TIMER_STOP(o_update);
1804
+
1805
+ buf_idx = 1 - buf_idx;
1806
+ } // end KV block loop (fallback)
1807
+ }
1808
+
1809
+ // ---- Final normalization: O = diag(1/l) @ O ----
1810
+ TIMER_START(o_norm);
1811
+ {
1812
+ fa_build_d_diag_inv_l(&factx, n_row_tiles, n_row_tiles_g_br);
1813
+
1814
+ // HMX: O_final = diag(1/l) @ O_prev
1815
+ if (factx.use_pipeline) {
1816
+ on_job.o_curr = o_tile_curr;
1817
+ on_job.o_prev = o_tile_prev;
1818
+ on_job.d_tiles = factx.vtcm_d_tiles;
1819
+ on_job.hmx_scales = factx.vtcm_hmx_scales_id;
1820
+ on_job.n_row_tiles = n_row_tiles;
1821
+ on_job.n_row_tiles_g_br = n_row_tiles_g_br;
1822
+ on_job.DV = DV;
1823
+ hmx_queue_push(ctx->hmx_queue, hmx_queue_make_desc(hmx_fa_o_norm_worker, &on_job));
1824
+ hmx_queue_pop(ctx->hmx_queue);
1825
+ } else {
1826
+ const size_t DV_tiles = (size_t) (DV / 32);
1827
+ const __fp16 * restrict d_base = factx.vtcm_d_tiles;
1828
+ const __fp16 * restrict op_base = o_tile_prev;
1829
+ __fp16 * restrict oc_base = o_tile_curr;
1830
+ __builtin_assume(n_row_tiles > 0);
1831
+ __builtin_assume(DV_tiles > 0);
1832
+
1833
+ Q6_bias_mxmem2_A((void *) factx.vtcm_hmx_scales_id);
1834
+ for (size_t r = 0; r < n_row_tiles; ++r) {
1835
+ for (size_t c = 0; c < DV_tiles; ++c) {
1836
+ const __fp16 * d_diag = d_base + r * (n_row_tiles_g_br + 1) * HMX_FP16_TILE_N_ELMS;
1837
+ const __fp16 * o_rc = op_base + (c * n_row_tiles_g_br + r) * HMX_FP16_TILE_N_ELMS;
1838
+ __fp16 * o_out = oc_base + (r * DV_tiles + c) * HMX_FP16_TILE_N_ELMS;
1839
+
1840
+ Q6_activation_hf_mxmem_RR((unsigned int) d_diag, 2047);
1841
+ Q6_weight_hf_mxmem_RR((unsigned int) o_rc, 2047);
1842
+ Q6_mxmem_AR_after_hf(o_out, 0);
1843
+ }
1844
+ }
1845
+ }
1846
+ }
1847
+ TIMER_STOP(o_norm);
1848
+
1849
+ // ---- Store O block ----
1850
+ TIMER_START(o_store);
1851
+ fa_phase_o_store(&factx, dst, o_tile_curr, q_start, kv_head, ib3, n_rows_g);
1852
+ TIMER_STOP(o_store);
1853
+
1854
+ #undef MASK_DMA_PUSH
1855
+ #undef MASK_DMA_POP
1856
+ #undef DMA_PREFETCH_KV
1857
+
1858
+ } // end Q block loop
1859
+ } // end KV head loop
1860
+ } // end batch loop
1861
+
1862
+ if (factx.use_pipeline) {
1863
+ hmx_queue_suspend(ctx->hmx_queue);
1864
+ } else {
1865
+ HAP_compute_res_hmx_unlock(ctx->vtcm_rctx);
1866
+ }
1867
+
1868
+ TIMER_STOP(total);
1869
+
1870
+ #if defined(ENABLE_PROFILE_TIMERS)
1871
+ FARF(HIGH, "hmx-fa: %lld us, q_load=%lld kv_dma=%lld k_interleave=%lld v_interleave=%lld", TIMER_US(total),
1872
+ TIMER_US(q_load), TIMER_US(kv_dma), TIMER_US(k_interleave), TIMER_US(v_interleave));
1873
+ FARF(HIGH, " qk_dot=%lld softmax=%lld o_update=%lld o_norm=%lld o_store=%lld", TIMER_US(qk_dot), TIMER_US(softmax),
1874
+ TIMER_US(o_update), TIMER_US(o_norm), TIMER_US(o_store));
1875
+ #endif
1876
+
1877
+ return HTP_STATUS_OK;
1878
+ }