whispercpp 1.3.5 → 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 (1017) hide show
  1. checksums.yaml +4 -4
  2. data/.document +3 -0
  3. data/.rdoc_options +2 -0
  4. data/LICENSE +1 -1
  5. data/README.md +133 -3
  6. data/Rakefile +18 -3
  7. data/ext/dependencies.rb +10 -4
  8. data/ext/dependencies_for_windows.rb +17 -0
  9. data/ext/extconf.rb +20 -7
  10. data/ext/options.rb +54 -14
  11. data/ext/options_for_windows.rb +51 -0
  12. data/ext/ruby_whisper.c +56 -46
  13. data/ext/ruby_whisper.h +165 -2
  14. data/ext/ruby_whisper_context.c +297 -126
  15. data/ext/ruby_whisper_context_params.c +163 -0
  16. data/ext/ruby_whisper_log_queue.c +180 -0
  17. data/ext/ruby_whisper_log_settable.h +47 -0
  18. data/ext/ruby_whisper_model.c +0 -1
  19. data/ext/ruby_whisper_parakeet.c +49 -0
  20. data/ext/ruby_whisper_parakeet_context.c +304 -0
  21. data/ext/ruby_whisper_parakeet_context_params.c +117 -0
  22. data/ext/ruby_whisper_parakeet_model.c +84 -0
  23. data/ext/ruby_whisper_parakeet_params.c +548 -0
  24. data/ext/ruby_whisper_parakeet_segment.c +157 -0
  25. data/ext/ruby_whisper_parakeet_token.c +188 -0
  26. data/ext/ruby_whisper_parakeet_transcribe.cpp +58 -0
  27. data/ext/ruby_whisper_params.c +256 -66
  28. data/ext/ruby_whisper_segment.c +6 -7
  29. data/ext/ruby_whisper_token.c +29 -9
  30. data/ext/ruby_whisper_transcribe.cpp +46 -16
  31. data/ext/ruby_whisper_vad_context.c +48 -1
  32. data/ext/ruby_whisper_vad_context_detect.cpp +6 -5
  33. data/ext/ruby_whisper_vad_params.c +0 -1
  34. data/ext/ruby_whisper_vad_segment.c +0 -1
  35. data/ext/ruby_whisper_vad_segments.c +0 -1
  36. data/ext/sources/CMakeLists.txt +41 -3
  37. data/ext/sources/CMakePresets.json +95 -0
  38. data/ext/sources/cmake/parakeet-config.cmake.in +30 -0
  39. data/ext/sources/cmake/parakeet.pc.in +10 -0
  40. data/ext/sources/cmake/whisper-config.cmake.in +5 -40
  41. data/ext/sources/cmake/whisper.pc.in +1 -1
  42. data/ext/sources/examples/CMakeLists.txt +4 -2
  43. data/ext/sources/examples/bench/bench.cpp +24 -19
  44. data/ext/sources/examples/cli/cli.cpp +51 -9
  45. data/ext/sources/examples/common-ggml.cpp +4 -0
  46. data/ext/sources/examples/common-whisper.cpp +139 -67
  47. data/ext/sources/examples/common-whisper.h +11 -0
  48. data/ext/sources/examples/ffmpeg-transcode.cpp +211 -341
  49. data/ext/sources/examples/miniaudio.h +4507 -2131
  50. data/ext/sources/examples/parakeet-cli/CMakeLists.txt +8 -0
  51. data/ext/sources/examples/parakeet-cli/parakeet-cli.cpp +243 -0
  52. data/ext/sources/examples/parakeet-quantize/CMakeLists.txt +7 -0
  53. data/ext/sources/examples/parakeet-quantize/parakeet-quantize.cpp +230 -0
  54. data/ext/sources/examples/server/server.cpp +213 -163
  55. data/ext/sources/ggml/CMakeLists.txt +29 -15
  56. data/ext/sources/ggml/cmake/FindNCCL.cmake +36 -0
  57. data/ext/sources/ggml/cmake/ggml-config.cmake.in +12 -2
  58. data/ext/sources/ggml/include/ggml-alloc.h +1 -0
  59. data/ext/sources/ggml/include/ggml-backend.h +73 -11
  60. data/ext/sources/ggml/include/ggml-cann.h +1 -1
  61. data/ext/sources/ggml/include/ggml-cpu.h +5 -0
  62. data/ext/sources/ggml/include/ggml-cuda.h +3 -0
  63. data/ext/sources/ggml/include/ggml-openvino.h +37 -0
  64. data/ext/sources/ggml/include/ggml-opt.h +1 -1
  65. data/ext/sources/ggml/include/ggml-rpc.h +8 -3
  66. data/ext/sources/ggml/include/ggml-virtgpu.h +14 -0
  67. data/ext/sources/ggml/include/ggml.h +155 -16
  68. data/ext/sources/ggml/include/gguf.h +10 -2
  69. data/ext/sources/ggml/src/CMakeLists.txt +25 -5
  70. data/ext/sources/ggml/src/ggml-alloc.c +9 -10
  71. data/ext/sources/ggml/src/ggml-backend-dl.cpp +48 -0
  72. data/ext/sources/ggml/src/ggml-backend-dl.h +45 -0
  73. data/ext/sources/ggml/src/ggml-backend-impl.h +22 -2
  74. data/ext/sources/ggml/src/ggml-backend-meta.cpp +2263 -0
  75. data/ext/sources/ggml/src/ggml-backend-reg.cpp +40 -86
  76. data/ext/sources/ggml/src/ggml-backend.cpp +114 -10
  77. data/ext/sources/ggml/src/ggml-blas/CMakeLists.txt +1 -1
  78. data/ext/sources/ggml/src/ggml-blas/ggml-blas.cpp +10 -2
  79. data/ext/sources/ggml/src/ggml-cann/acl_tensor.cpp +1 -1
  80. data/ext/sources/ggml/src/ggml-cann/acl_tensor.h +1 -1
  81. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.cpp +1016 -442
  82. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.h +111 -85
  83. data/ext/sources/ggml/src/ggml-cann/common.h +23 -14
  84. data/ext/sources/ggml/src/ggml-cann/ggml-cann.cpp +255 -92
  85. data/ext/sources/ggml/src/ggml-common.h +22 -0
  86. data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +68 -34
  87. data/ext/sources/ggml/src/ggml-cpu/amx/amx.cpp +44 -19
  88. data/ext/sources/ggml/src/ggml-cpu/amx/common.h +34 -10
  89. data/ext/sources/ggml/src/ggml-cpu/amx/mmq.cpp +101 -101
  90. data/ext/sources/ggml/src/ggml-cpu/arch/arm/quants.c +194 -1
  91. data/ext/sources/ggml/src/ggml-cpu/arch/arm/repack.cpp +2874 -613
  92. data/ext/sources/ggml/src/ggml-cpu/arch/loongarch/quants.c +151 -1
  93. data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/quants.c +0 -1
  94. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/quants.c +5480 -840
  95. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/repack.cpp +1361 -0
  96. data/ext/sources/ggml/src/ggml-cpu/arch/s390/quants.c +8 -11
  97. data/ext/sources/ggml/src/ggml-cpu/arch/wasm/quants.c +72 -1
  98. data/ext/sources/ggml/src/ggml-cpu/arch/x86/quants.c +186 -36
  99. data/ext/sources/ggml/src/ggml-cpu/arch/x86/repack.cpp +119 -19
  100. data/ext/sources/ggml/src/ggml-cpu/arch-fallback.h +112 -26
  101. data/ext/sources/ggml/src/ggml-cpu/binary-ops.cpp +2 -6
  102. data/ext/sources/ggml/src/ggml-cpu/cmake/FindSMTIME.cmake +32 -0
  103. data/ext/sources/ggml/src/ggml-cpu/common.h +8 -0
  104. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-impl.h +13 -0
  105. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +153 -16
  106. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.cpp +17 -0
  107. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.cpp +21 -20
  108. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +976 -251
  109. data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +671 -266
  110. data/ext/sources/ggml/src/ggml-cpu/ops.cpp +1277 -263
  111. data/ext/sources/ggml/src/ggml-cpu/ops.h +4 -0
  112. data/ext/sources/ggml/src/ggml-cpu/quants.c +95 -0
  113. data/ext/sources/ggml/src/ggml-cpu/quants.h +6 -0
  114. data/ext/sources/ggml/src/ggml-cpu/repack.cpp +2893 -679
  115. data/ext/sources/ggml/src/ggml-cpu/repack.h +119 -8
  116. data/ext/sources/ggml/src/ggml-cpu/simd-gemm.h +226 -0
  117. data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +114 -19
  118. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.cpp +1402 -687
  119. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.h +8 -0
  120. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +597 -2766
  121. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime2_kernels.cpp +5768 -0
  122. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_env.cpp +320 -0
  123. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_env.h +55 -0
  124. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_kernels.h +182 -19
  125. data/ext/sources/ggml/src/ggml-cpu/spacemit/repack.cpp +1795 -0
  126. data/ext/sources/ggml/src/ggml-cpu/spacemit/repack.h +14 -0
  127. data/ext/sources/ggml/src/ggml-cpu/spacemit/rvv_kernels.cpp +3178 -0
  128. data/ext/sources/ggml/src/ggml-cpu/spacemit/rvv_kernels.h +95 -0
  129. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_barrier.h +34 -0
  130. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_mem_pool.cpp +760 -0
  131. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_mem_pool.h +32 -0
  132. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_tcm.h +409 -0
  133. data/ext/sources/ggml/src/ggml-cpu/unary-ops.cpp +1 -1
  134. data/ext/sources/ggml/src/ggml-cpu/vec.cpp +54 -53
  135. data/ext/sources/ggml/src/ggml-cpu/vec.h +225 -240
  136. data/ext/sources/ggml/src/ggml-cuda/CMakeLists.txt +18 -8
  137. data/ext/sources/ggml/src/ggml-cuda/allreduce.cu +971 -0
  138. data/ext/sources/ggml/src/ggml-cuda/allreduce.cuh +29 -0
  139. data/ext/sources/ggml/src/ggml-cuda/argsort.cu +73 -28
  140. data/ext/sources/ggml/src/ggml-cuda/binbcast.cu +69 -41
  141. data/ext/sources/ggml/src/ggml-cuda/binbcast.cuh +1 -0
  142. data/ext/sources/ggml/src/ggml-cuda/common.cuh +359 -29
  143. data/ext/sources/ggml/src/ggml-cuda/concat.cu +120 -114
  144. data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cu +45 -21
  145. data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cuh +1 -0
  146. data/ext/sources/ggml/src/ggml-cuda/convert.cu +94 -27
  147. data/ext/sources/ggml/src/ggml-cuda/convert.cuh +10 -0
  148. data/ext/sources/ggml/src/ggml-cuda/cpy.cu +20 -9
  149. data/ext/sources/ggml/src/ggml-cuda/dequantize.cuh +22 -0
  150. data/ext/sources/ggml/src/ggml-cuda/fattn-common.cuh +333 -85
  151. data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +632 -190
  152. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cu +12 -0
  153. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cuh +162 -49
  154. data/ext/sources/ggml/src/ggml-cuda/fattn-vec.cuh +43 -18
  155. data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cu +44 -14
  156. data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cuh +1 -1
  157. data/ext/sources/ggml/src/ggml-cuda/fattn.cu +241 -23
  158. data/ext/sources/ggml/src/ggml-cuda/fattn.cuh +2 -0
  159. data/ext/sources/ggml/src/ggml-cuda/fwht.cu +101 -0
  160. data/ext/sources/ggml/src/ggml-cuda/fwht.cuh +4 -0
  161. data/ext/sources/ggml/src/ggml-cuda/gated_delta_net.cu +312 -0
  162. data/ext/sources/ggml/src/ggml-cuda/gated_delta_net.cuh +4 -0
  163. data/ext/sources/ggml/src/ggml-cuda/getrows.cu +34 -12
  164. data/ext/sources/ggml/src/ggml-cuda/ggml-cuda.cu +1454 -599
  165. data/ext/sources/ggml/src/ggml-cuda/im2col.cu +32 -29
  166. data/ext/sources/ggml/src/ggml-cuda/mean.cu +13 -10
  167. data/ext/sources/ggml/src/ggml-cuda/mma.cuh +397 -183
  168. data/ext/sources/ggml/src/ggml-cuda/mmf.cu +30 -10
  169. data/ext/sources/ggml/src/ggml-cuda/mmf.cuh +161 -88
  170. data/ext/sources/ggml/src/ggml-cuda/mmq.cu +18 -12
  171. data/ext/sources/ggml/src/ggml-cuda/mmq.cuh +522 -431
  172. data/ext/sources/ggml/src/ggml-cuda/mmvf.cu +139 -72
  173. data/ext/sources/ggml/src/ggml-cuda/mmvf.cuh +2 -0
  174. data/ext/sources/ggml/src/ggml-cuda/mmvq.cu +608 -88
  175. data/ext/sources/ggml/src/ggml-cuda/mmvq.cuh +6 -0
  176. data/ext/sources/ggml/src/ggml-cuda/norm.cu +47 -79
  177. data/ext/sources/ggml/src/ggml-cuda/out-prod.cu +23 -7
  178. data/ext/sources/ggml/src/ggml-cuda/pad.cu +13 -10
  179. data/ext/sources/ggml/src/ggml-cuda/quantize.cu +134 -27
  180. data/ext/sources/ggml/src/ggml-cuda/quantize.cuh +1 -1
  181. data/ext/sources/ggml/src/ggml-cuda/reduce_rows.cuh +7 -17
  182. data/ext/sources/ggml/src/ggml-cuda/rope.cu +244 -137
  183. data/ext/sources/ggml/src/ggml-cuda/scale.cu +4 -1
  184. data/ext/sources/ggml/src/ggml-cuda/set-rows.cu +14 -6
  185. data/ext/sources/ggml/src/ggml-cuda/snake.cu +72 -0
  186. data/ext/sources/ggml/src/ggml-cuda/snake.cuh +8 -0
  187. data/ext/sources/ggml/src/ggml-cuda/softcap.cu +4 -1
  188. data/ext/sources/ggml/src/ggml-cuda/softmax.cu +8 -83
  189. data/ext/sources/ggml/src/ggml-cuda/solve_tri.cu +1 -1
  190. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cu +96 -40
  191. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cuh +1 -1
  192. data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cu +40 -18
  193. data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +8 -4
  194. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_16.cu +1 -0
  195. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_32.cu +6 -0
  196. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_8.cu +2 -0
  197. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_4.cu +2 -0
  198. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_16.cu +1 -0
  199. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_32.cu +6 -0
  200. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_4.cu +2 -0
  201. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_8.cu +2 -0
  202. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_16.cu +1 -0
  203. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_4.cu +2 -0
  204. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_8.cu +2 -0
  205. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_4.cu +2 -0
  206. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_8.cu +2 -0
  207. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq192-dv128.cu +5 -0
  208. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq320-dv256.cu +5 -0
  209. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq512-dv512.cu +5 -0
  210. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-bf16.cu +7 -0
  211. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-f16.cu +7 -0
  212. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_0.cu +7 -0
  213. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_1.cu +7 -0
  214. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_0.cu +7 -0
  215. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_1.cu +7 -0
  216. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q8_0.cu +7 -0
  217. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-bf16.cu +7 -0
  218. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-bf16.cu +7 -0
  219. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-bf16.cu +7 -0
  220. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-bf16.cu +7 -0
  221. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-bf16.cu +7 -0
  222. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-bf16.cu +7 -0
  223. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-nvfp4.cu +5 -0
  224. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q1_0.cu +5 -0
  225. data/ext/sources/ggml/src/ggml-cuda/top-k.cu +5 -5
  226. data/ext/sources/ggml/src/ggml-cuda/topk-moe.cu +202 -135
  227. data/ext/sources/ggml/src/ggml-cuda/topk-moe.cuh +20 -14
  228. data/ext/sources/ggml/src/ggml-cuda/unary.cu +86 -2
  229. data/ext/sources/ggml/src/ggml-cuda/unary.cuh +4 -0
  230. data/ext/sources/ggml/src/ggml-cuda/vecdotq.cuh +111 -17
  231. data/ext/sources/ggml/src/ggml-cuda/vendors/cuda.h +7 -2
  232. data/ext/sources/ggml/src/ggml-cuda/vendors/hip.h +30 -2
  233. data/ext/sources/ggml/src/ggml-cuda/vendors/musa.h +3 -0
  234. data/ext/sources/ggml/src/ggml-hexagon/CMakeLists.txt +84 -46
  235. data/ext/sources/ggml/src/ggml-hexagon/ggml-hexagon.cpp +1612 -753
  236. data/ext/sources/ggml/src/ggml-hexagon/htp/CMakeLists.txt +51 -11
  237. data/ext/sources/ggml/src/ggml-hexagon/htp/act-ops.c +361 -261
  238. data/ext/sources/ggml/src/ggml-hexagon/htp/argsort-ops.c +294 -0
  239. data/ext/sources/ggml/src/ggml-hexagon/htp/binary-ops.c +753 -241
  240. data/ext/sources/ggml/src/ggml-hexagon/htp/cmake-toolchain.cmake +5 -5
  241. data/ext/sources/ggml/src/ggml-hexagon/htp/concat-ops.c +277 -0
  242. data/ext/sources/ggml/src/ggml-hexagon/htp/cpy-ops.c +295 -0
  243. data/ext/sources/ggml/src/ggml-hexagon/htp/cumsum-ops.c +270 -0
  244. data/ext/sources/ggml/src/ggml-hexagon/htp/diag-ops.c +216 -0
  245. data/ext/sources/ggml/src/ggml-hexagon/htp/fill-ops.c +123 -0
  246. data/ext/sources/ggml/src/ggml-hexagon/htp/flash-attn-ops.c +471 -296
  247. data/ext/sources/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.c +1148 -0
  248. data/ext/sources/ggml/src/ggml-hexagon/htp/get-rows-ops.c +159 -53
  249. data/ext/sources/ggml/src/ggml-hexagon/htp/{htp-dma.c → hex-dma.c} +3 -3
  250. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dma.h +372 -0
  251. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dump.h +86 -0
  252. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-fastdiv.h +37 -0
  253. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-utils.h +137 -0
  254. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-flash-attn-ops.c +1878 -0
  255. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-matmul-ops.c +2066 -0
  256. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-ops.c +6 -0
  257. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-ops.h +88 -0
  258. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-profile.h +34 -0
  259. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-queue.c +158 -0
  260. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-queue.h +134 -0
  261. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-utils.h +200 -0
  262. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-ctx.h +97 -14
  263. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-ops.h +163 -67
  264. data/ext/sources/ggml/src/ggml-hexagon/htp/htp_iface.idl +9 -3
  265. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-arith.h +443 -0
  266. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-base.h +308 -0
  267. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-copy.h +262 -0
  268. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-div.h +291 -0
  269. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-dump.h +129 -0
  270. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-exp.h +216 -0
  271. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-flash-attn.h +47 -0
  272. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-floor.h +100 -0
  273. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-inverse.h +210 -0
  274. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-log.h +65 -0
  275. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-pow.h +42 -0
  276. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-reduce.h +296 -0
  277. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-repl.h +74 -0
  278. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-scale.h +133 -0
  279. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sigmoid.h +142 -0
  280. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sin-cos.h +90 -0
  281. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sqrt.h +126 -0
  282. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-types.h +36 -0
  283. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-utils.h +18 -1348
  284. data/ext/sources/ggml/src/ggml-hexagon/htp/main.c +547 -635
  285. data/ext/sources/ggml/src/ggml-hexagon/htp/matmul-ops.c +3556 -1101
  286. data/ext/sources/ggml/src/ggml-hexagon/htp/pad-ops.c +547 -0
  287. data/ext/sources/ggml/src/ggml-hexagon/htp/repeat-ops.c +148 -0
  288. data/ext/sources/ggml/src/ggml-hexagon/htp/rope-ops.c +475 -269
  289. data/ext/sources/ggml/src/ggml-hexagon/htp/set-rows-ops.c +94 -72
  290. data/ext/sources/ggml/src/ggml-hexagon/htp/softmax-ops.c +222 -217
  291. data/ext/sources/ggml/src/ggml-hexagon/htp/solve-tri-ops.c +267 -0
  292. data/ext/sources/ggml/src/ggml-hexagon/htp/ssm-conv.c +432 -0
  293. data/ext/sources/ggml/src/ggml-hexagon/htp/sum-rows-ops.c +128 -0
  294. data/ext/sources/ggml/src/ggml-hexagon/htp/unary-ops.c +886 -117
  295. data/ext/sources/ggml/src/ggml-hexagon/htp/vtcm-utils.h +16 -0
  296. data/ext/sources/ggml/src/ggml-hexagon/htp/worker-pool.c +1 -5
  297. data/ext/sources/ggml/src/ggml-hexagon/htp-drv.cpp +418 -0
  298. data/ext/sources/ggml/src/ggml-hexagon/htp-drv.h +121 -0
  299. data/ext/sources/ggml/src/ggml-hexagon/htp-opnode.h +272 -0
  300. data/ext/sources/ggml/src/ggml-hexagon/libdl.h +79 -0
  301. data/ext/sources/ggml/src/ggml-hexagon/libggml-htp.inf +40 -0
  302. data/ext/sources/ggml/src/ggml-hip/CMakeLists.txt +28 -9
  303. data/ext/sources/ggml/src/ggml-impl.h +68 -1
  304. data/ext/sources/ggml/src/ggml-metal/CMakeLists.txt +10 -10
  305. data/ext/sources/ggml/src/ggml-metal/ggml-metal-common.cpp +13 -2
  306. data/ext/sources/ggml/src/ggml-metal/ggml-metal-context.h +8 -0
  307. data/ext/sources/ggml/src/ggml-metal/ggml-metal-context.m +147 -17
  308. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.cpp +409 -83
  309. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.h +54 -5
  310. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.m +254 -52
  311. data/ext/sources/ggml/src/ggml-metal/ggml-metal-impl.h +254 -23
  312. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.cpp +756 -285
  313. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.h +7 -4
  314. data/ext/sources/ggml/src/ggml-metal/ggml-metal.cpp +359 -133
  315. data/ext/sources/ggml/src/ggml-metal/ggml-metal.metal +1867 -1123
  316. data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +5 -6
  317. data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +71 -4
  318. data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +14127 -5314
  319. data/ext/sources/ggml/src/ggml-opencl/kernels/concat.cl +97 -88
  320. data/ext/sources/ggml/src/ggml-opencl/kernels/cpy.cl +104 -0
  321. data/ext/sources/ggml/src/ggml-opencl/kernels/cumsum.cl +139 -0
  322. data/ext/sources/ggml/src/ggml-opencl/kernels/cvt.cl +1978 -67
  323. data/ext/sources/ggml/src/ggml-opencl/kernels/diag.cl +27 -0
  324. data/ext/sources/ggml/src/ggml-opencl/kernels/exp.cl +125 -0
  325. data/ext/sources/ggml/src/ggml-opencl/kernels/expm1.cl +87 -56
  326. data/ext/sources/ggml/src/ggml-opencl/kernels/gated_delta_net.cl +249 -0
  327. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32_ns.cl +306 -0
  328. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_0_f32_ns.cl +256 -0
  329. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_1_f32_ns.cl +258 -0
  330. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_k_f32_ns.cl +283 -0
  331. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_0_f32_ns.cl +260 -0
  332. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_1_f32_ns.cl +262 -0
  333. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_k_f32_ns.cl +288 -0
  334. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q6_k_f32_ns.cl +267 -0
  335. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_iq4_nl_f32.cl +150 -0
  336. data/ext/sources/ggml/src/ggml-opencl/kernels/{mul_mat_Ab_Bi_8x4.cl → gemm_noshuffle_q4_0_f32.cl} +1 -1
  337. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q4_1_f32.cl +132 -0
  338. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q4_k_f32.cl +172 -0
  339. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_0_f32.cl +131 -0
  340. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_1_f32.cl +134 -0
  341. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_k_f32.cl +176 -0
  342. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q6_k_f32.cl +140 -0
  343. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q8_0_f32.cl +129 -0
  344. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_xmem_f16_f32_os8.cl +233 -0
  345. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_mxfp4_f32_ns.cl +165 -0
  346. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_0_f32_ns.cl +120 -0
  347. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_1_f32_ns.cl +123 -0
  348. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_k_f32_ns.cl +155 -0
  349. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_0_f32_ns.cl +123 -0
  350. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_1_f32_ns.cl +125 -0
  351. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_k_f32_ns.cl +160 -0
  352. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q6_k_f32_ns.cl +141 -0
  353. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_iq4_nl_f32.cl +302 -0
  354. data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle_general.cl → gemv_noshuffle_q4_0_f32.cl} +5 -5
  355. data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle.cl → gemv_noshuffle_q4_0_f32_spec.cl} +5 -5
  356. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q4_1_f32.cl +283 -0
  357. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q4_k_f32.cl +318 -0
  358. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_0_f32.cl +291 -0
  359. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_1_f32.cl +294 -0
  360. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_k_f32.cl +326 -0
  361. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q6_k_f32.cl +293 -0
  362. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q8_0_f32.cl +195 -0
  363. data/ext/sources/ggml/src/ggml-opencl/kernels/get_rows.cl +15 -9
  364. data/ext/sources/ggml/src/ggml-opencl/kernels/l2_norm.cl +71 -0
  365. data/ext/sources/ggml/src/ggml-opencl/kernels/mean.cl +114 -13
  366. data/ext/sources/ggml/src/ggml-opencl/kernels/moe_reorder_b.cl +30 -0
  367. data/ext/sources/ggml/src/ggml-opencl/kernels/moe_sort_by_expert.cl +82 -0
  368. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_iq4_nl_f32_l4_lm.cl +171 -0
  369. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q4_0_f32_l4_lm.cl +163 -0
  370. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q4_1_f32_l4_lm.cl +165 -0
  371. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q4_k_f32_l4_lm.cl +179 -0
  372. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_0_f32_l4_lm.cl +173 -0
  373. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_1_f32_l4_lm.cl +175 -0
  374. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_k_f32_l4_lm.cl +192 -0
  375. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q6_k_f32_l4_lm.cl +158 -0
  376. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_iq4_nl_f32.cl +164 -0
  377. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_iq4_nl_f32_flat.cl +202 -0
  378. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_1_f32.cl +219 -0
  379. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_1_f32_flat.cl +229 -0
  380. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_k_f32.cl +180 -0
  381. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_k_f32_flat.cl +196 -0
  382. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_0_f32.cl +241 -0
  383. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_0_f32_flat.cl +243 -0
  384. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_1_f32.cl +243 -0
  385. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_1_f32_flat.cl +247 -0
  386. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_k_f32.cl +187 -0
  387. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_k_f32_flat.cl +203 -0
  388. data/ext/sources/ggml/src/ggml-opencl/kernels/{mul_mv_q6_k.cl → mul_mv_q6_k_f32.cl} +4 -0
  389. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q6_k_f32_flat.cl +178 -0
  390. data/ext/sources/ggml/src/ggml-opencl/kernels/neg.cl +125 -0
  391. data/ext/sources/ggml/src/ggml-opencl/kernels/repeat.cl +31 -32
  392. data/ext/sources/ggml/src/ggml-opencl/kernels/scale.cl +14 -4
  393. data/ext/sources/ggml/src/ggml-opencl/kernels/softplus.cl +88 -60
  394. data/ext/sources/ggml/src/ggml-opencl/kernels/solve_tri.cl +51 -0
  395. data/ext/sources/ggml/src/ggml-opencl/kernels/sum_rows.cl +114 -13
  396. data/ext/sources/ggml/src/ggml-opencl/kernels/tanh.cl +94 -48
  397. data/ext/sources/ggml/src/ggml-opencl/kernels/transpose.cl +26 -0
  398. data/ext/sources/ggml/src/ggml-opencl/kernels/tri.cl +32 -0
  399. data/ext/sources/ggml/src/ggml-openvino/.clang-format +154 -0
  400. data/ext/sources/ggml/src/ggml-openvino/CMakeLists.txt +22 -0
  401. data/ext/sources/ggml/src/ggml-openvino/ggml-decoder.cpp +985 -0
  402. data/ext/sources/ggml/src/ggml-openvino/ggml-decoder.h +294 -0
  403. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino-extra.cpp +380 -0
  404. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino-extra.h +182 -0
  405. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino.cpp +1132 -0
  406. data/ext/sources/ggml/src/ggml-openvino/ggml-quants.cpp +956 -0
  407. data/ext/sources/ggml/src/ggml-openvino/ggml-quants.h +153 -0
  408. data/ext/sources/ggml/src/ggml-openvino/openvino/decoder.h +74 -0
  409. data/ext/sources/ggml/src/ggml-openvino/openvino/frontend.cpp +27 -0
  410. data/ext/sources/ggml/src/ggml-openvino/openvino/frontend.h +23 -0
  411. data/ext/sources/ggml/src/ggml-openvino/openvino/input_model.cpp +17 -0
  412. data/ext/sources/ggml/src/ggml-openvino/openvino/input_model.h +29 -0
  413. data/ext/sources/ggml/src/ggml-openvino/openvino/node_context.h +112 -0
  414. data/ext/sources/ggml/src/ggml-openvino/openvino/op/cont.cpp +48 -0
  415. data/ext/sources/ggml/src/ggml-openvino/openvino/op/cpy.cpp +21 -0
  416. data/ext/sources/ggml/src/ggml-openvino/openvino/op/flash_attn_ext.cpp +90 -0
  417. data/ext/sources/ggml/src/ggml-openvino/openvino/op/get_rows.cpp +69 -0
  418. data/ext/sources/ggml/src/ggml-openvino/openvino/op/glu_geglu.cpp +61 -0
  419. data/ext/sources/ggml/src/ggml-openvino/openvino/op/glu_swiglu.cpp +62 -0
  420. data/ext/sources/ggml/src/ggml-openvino/openvino/op/mulmat.cpp +90 -0
  421. data/ext/sources/ggml/src/ggml-openvino/openvino/op/permute.cpp +102 -0
  422. data/ext/sources/ggml/src/ggml-openvino/openvino/op/reshape.cpp +83 -0
  423. data/ext/sources/ggml/src/ggml-openvino/openvino/op/rms_norm.cpp +46 -0
  424. data/ext/sources/ggml/src/ggml-openvino/openvino/op/rope.cpp +149 -0
  425. data/ext/sources/ggml/src/ggml-openvino/openvino/op/scale.cpp +41 -0
  426. data/ext/sources/ggml/src/ggml-openvino/openvino/op/set_rows.cpp +76 -0
  427. data/ext/sources/ggml/src/ggml-openvino/openvino/op/softmax.cpp +89 -0
  428. data/ext/sources/ggml/src/ggml-openvino/openvino/op/transpose.cpp +23 -0
  429. data/ext/sources/ggml/src/ggml-openvino/openvino/op/unary_gelu.cpp +25 -0
  430. data/ext/sources/ggml/src/ggml-openvino/openvino/op/unary_silu.cpp +27 -0
  431. data/ext/sources/ggml/src/ggml-openvino/openvino/op/view.cpp +53 -0
  432. data/ext/sources/ggml/src/ggml-openvino/openvino/op_table.cpp +47 -0
  433. data/ext/sources/ggml/src/ggml-openvino/openvino/op_table.h +40 -0
  434. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/fuse_to_sdpa.cpp +60 -0
  435. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/fuse_to_sdpa.h +17 -0
  436. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/mark_decompression_convert_constant_folding.h +29 -0
  437. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/squeeze_matmul.cpp +58 -0
  438. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/squeeze_matmul.h +17 -0
  439. data/ext/sources/ggml/src/ggml-openvino/openvino/rt_info/weightless_caching_attributes.hpp +41 -0
  440. data/ext/sources/ggml/src/ggml-openvino/openvino/translate_session.cpp +317 -0
  441. data/ext/sources/ggml/src/ggml-openvino/openvino/translate_session.h +28 -0
  442. data/ext/sources/ggml/src/ggml-openvino/openvino/utils.cpp +257 -0
  443. data/ext/sources/ggml/src/ggml-openvino/openvino/utils.h +86 -0
  444. data/ext/sources/ggml/src/ggml-openvino/utils.cpp +880 -0
  445. data/ext/sources/ggml/src/ggml-openvino/utils.h +143 -0
  446. data/ext/sources/ggml/src/ggml-opt.cpp +1 -0
  447. data/ext/sources/ggml/src/ggml-quants.c +385 -119
  448. data/ext/sources/ggml/src/ggml-quants.h +6 -0
  449. data/ext/sources/ggml/src/ggml-rpc/CMakeLists.txt +24 -0
  450. data/ext/sources/ggml/src/ggml-rpc/ggml-rpc.cpp +167 -311
  451. data/ext/sources/ggml/src/ggml-rpc/transport.cpp +683 -0
  452. data/ext/sources/ggml/src/ggml-rpc/transport.h +34 -0
  453. data/ext/sources/ggml/src/ggml-sycl/CMakeLists.txt +64 -91
  454. data/ext/sources/ggml/src/ggml-sycl/add-id.cpp +5 -1
  455. data/ext/sources/ggml/src/ggml-sycl/backend.hpp +4 -1
  456. data/ext/sources/ggml/src/ggml-sycl/binbcast.cpp +21 -20
  457. data/ext/sources/ggml/src/ggml-sycl/common.cpp +74 -2
  458. data/ext/sources/ggml/src/ggml-sycl/common.hpp +356 -11
  459. data/ext/sources/ggml/src/ggml-sycl/convert.cpp +184 -14
  460. data/ext/sources/ggml/src/ggml-sycl/convert.hpp +31 -1
  461. data/ext/sources/ggml/src/ggml-sycl/count-equal.cpp +1 -1
  462. data/ext/sources/ggml/src/ggml-sycl/cumsum.cpp +148 -0
  463. data/ext/sources/ggml/src/ggml-sycl/cumsum.hpp +5 -0
  464. data/ext/sources/ggml/src/ggml-sycl/dequantize.hpp +663 -0
  465. data/ext/sources/ggml/src/ggml-sycl/diag.cpp +67 -0
  466. data/ext/sources/ggml/src/ggml-sycl/diag.hpp +5 -0
  467. data/ext/sources/ggml/src/ggml-sycl/dmmv.cpp +586 -6
  468. data/ext/sources/ggml/src/ggml-sycl/dpct/helper.hpp +791 -47
  469. data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +77 -156
  470. data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +2 -2
  471. data/ext/sources/ggml/src/ggml-sycl/fattn-buffers.cpp +56 -0
  472. data/ext/sources/ggml/src/ggml-sycl/fattn-buffers.hpp +63 -0
  473. data/ext/sources/ggml/src/ggml-sycl/fattn-common.hpp +1181 -0
  474. data/ext/sources/ggml/src/ggml-sycl/fattn-tile.cpp +59 -0
  475. data/ext/sources/ggml/src/ggml-sycl/fattn-tile.hpp +1246 -0
  476. data/ext/sources/ggml/src/ggml-sycl/fattn-vec.hpp +674 -0
  477. data/ext/sources/ggml/src/ggml-sycl/fattn.cpp +227 -0
  478. data/ext/sources/ggml/src/ggml-sycl/fattn.hpp +22 -0
  479. data/ext/sources/ggml/src/ggml-sycl/fill.cpp +55 -0
  480. data/ext/sources/ggml/src/ggml-sycl/fill.hpp +5 -0
  481. data/ext/sources/ggml/src/ggml-sycl/gated_delta_net.cpp +347 -0
  482. data/ext/sources/ggml/src/ggml-sycl/gated_delta_net.hpp +9 -0
  483. data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +3 -0
  484. data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +79 -3
  485. data/ext/sources/ggml/src/ggml-sycl/ggml-sycl.cpp +1134 -236
  486. data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +353 -89
  487. data/ext/sources/ggml/src/ggml-sycl/im2col.hpp +5 -3
  488. data/ext/sources/ggml/src/ggml-sycl/mmvq.cpp +1344 -26
  489. data/ext/sources/ggml/src/ggml-sycl/mmvq.hpp +16 -0
  490. data/ext/sources/ggml/src/ggml-sycl/norm.cpp +65 -66
  491. data/ext/sources/ggml/src/ggml-sycl/outprod.cpp +3 -3
  492. data/ext/sources/ggml/src/ggml-sycl/pad.cpp +27 -27
  493. data/ext/sources/ggml/src/ggml-sycl/presets.hpp +3 -0
  494. data/ext/sources/ggml/src/ggml-sycl/quants.hpp +72 -1
  495. data/ext/sources/ggml/src/ggml-sycl/rope.cpp +450 -287
  496. data/ext/sources/ggml/src/ggml-sycl/rope.hpp +6 -0
  497. data/ext/sources/ggml/src/ggml-sycl/set_rows.cpp +7 -1
  498. data/ext/sources/ggml/src/ggml-sycl/softmax.cpp +6 -6
  499. data/ext/sources/ggml/src/ggml-sycl/solve_tri.cpp +172 -0
  500. data/ext/sources/ggml/src/ggml-sycl/solve_tri.hpp +8 -0
  501. data/ext/sources/ggml/src/ggml-sycl/ssm_conv.cpp +6 -1
  502. data/ext/sources/ggml/src/ggml-sycl/ssm_scan.cpp +156 -0
  503. data/ext/sources/ggml/src/ggml-sycl/ssm_scan.hpp +5 -0
  504. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.cpp +62 -10
  505. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.hpp +18 -6
  506. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq112-dv112.cpp +5 -0
  507. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq128-dv128.cpp +5 -0
  508. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq256-dv256.cpp +5 -0
  509. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq40-dv40.cpp +5 -0
  510. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq512-dv512.cpp +6 -0
  511. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq576-dv512.cpp +5 -0
  512. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq64-dv64.cpp +5 -0
  513. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq72-dv72.cpp +5 -0
  514. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq80-dv80.cpp +5 -0
  515. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq96-dv96.cpp +5 -0
  516. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-f16.cpp +8 -0
  517. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q4_0.cpp +8 -0
  518. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q4_1.cpp +8 -0
  519. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q5_0.cpp +8 -0
  520. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q5_1.cpp +8 -0
  521. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q8_0.cpp +8 -0
  522. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-f16.cpp +8 -0
  523. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q4_0.cpp +8 -0
  524. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q4_1.cpp +8 -0
  525. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q5_0.cpp +8 -0
  526. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q5_1.cpp +8 -0
  527. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q8_0.cpp +8 -0
  528. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-f16.cpp +8 -0
  529. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q4_0.cpp +8 -0
  530. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q4_1.cpp +8 -0
  531. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q5_0.cpp +8 -0
  532. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q5_1.cpp +8 -0
  533. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q8_0.cpp +8 -0
  534. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-f16.cpp +8 -0
  535. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q4_0.cpp +8 -0
  536. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q4_1.cpp +8 -0
  537. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q5_0.cpp +8 -0
  538. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q5_1.cpp +8 -0
  539. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q8_0.cpp +8 -0
  540. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-f16.cpp +8 -0
  541. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q4_0.cpp +8 -0
  542. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q4_1.cpp +8 -0
  543. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q5_0.cpp +8 -0
  544. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q5_1.cpp +8 -0
  545. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q8_0.cpp +8 -0
  546. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-f16.cpp +8 -0
  547. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q4_0.cpp +8 -0
  548. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q4_1.cpp +8 -0
  549. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q5_0.cpp +8 -0
  550. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q5_1.cpp +8 -0
  551. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q8_0.cpp +8 -0
  552. data/ext/sources/ggml/src/ggml-sycl/type.hpp +112 -0
  553. data/ext/sources/ggml/src/ggml-sycl/upscale.cpp +410 -0
  554. data/ext/sources/ggml/src/ggml-sycl/upscale.hpp +9 -0
  555. data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +228 -53
  556. data/ext/sources/ggml/src/ggml-sycl/wkv.cpp +1 -1
  557. data/ext/sources/ggml/src/ggml-virtgpu/CMakeLists.txt +70 -0
  558. data/ext/sources/ggml/src/ggml-virtgpu/apir_cs_ggml-rpc-front.cpp +87 -0
  559. data/ext/sources/ggml/src/ggml-virtgpu/backend/CMakeLists.txt +21 -0
  560. data/ext/sources/ggml/src/ggml-virtgpu/backend/apir_cs_ggml-rpc-back.cpp +115 -0
  561. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-convert.h +13 -0
  562. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-dispatched-backend.cpp +102 -0
  563. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-dispatched-buffer-type.cpp +105 -0
  564. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-dispatched-buffer.cpp +179 -0
  565. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-dispatched-device.cpp +148 -0
  566. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-dispatched.cpp +51 -0
  567. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-dispatched.gen.h +73 -0
  568. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-dispatched.h +27 -0
  569. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend-virgl-apir.h +32 -0
  570. data/ext/sources/ggml/src/ggml-virtgpu/backend/backend.cpp +144 -0
  571. data/ext/sources/ggml/src/ggml-virtgpu/backend/shared/api_remoting.h +95 -0
  572. data/ext/sources/ggml/src/ggml-virtgpu/backend/shared/apir_backend.gen.h +94 -0
  573. data/ext/sources/ggml/src/ggml-virtgpu/backend/shared/apir_backend.h +50 -0
  574. data/ext/sources/ggml/src/ggml-virtgpu/backend/shared/apir_cs.h +378 -0
  575. data/ext/sources/ggml/src/ggml-virtgpu/backend/shared/apir_cs_ggml.h +232 -0
  576. data/ext/sources/ggml/src/ggml-virtgpu/backend/shared/apir_cs_rpc.h +58 -0
  577. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-buffer-type.cpp +81 -0
  578. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-buffer.cpp +123 -0
  579. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-device.cpp +160 -0
  580. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-reg.cpp +213 -0
  581. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend.cpp +71 -0
  582. data/ext/sources/ggml/src/ggml-virtgpu/ggml-remoting.h +71 -0
  583. data/ext/sources/ggml/src/ggml-virtgpu/ggmlremoting_functions.yaml +166 -0
  584. data/ext/sources/ggml/src/ggml-virtgpu/include/apir_hw.h +9 -0
  585. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-apir.h +15 -0
  586. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-forward-backend.cpp +58 -0
  587. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-forward-buffer-type.cpp +110 -0
  588. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-forward-buffer.cpp +173 -0
  589. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-forward-device.cpp +192 -0
  590. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-forward-impl.h +36 -0
  591. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-forward.gen.h +53 -0
  592. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-shm.cpp +99 -0
  593. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-shm.h +23 -0
  594. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-utils.cpp +179 -0
  595. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-utils.h +86 -0
  596. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu.cpp +545 -0
  597. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu.h +115 -0
  598. data/ext/sources/ggml/src/ggml-vulkan/CMakeLists.txt +12 -1
  599. data/ext/sources/ggml/src/ggml-vulkan/ggml-vulkan.cpp +3250 -940
  600. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +4 -0
  601. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/acc.comp +16 -8
  602. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/contig_copy.comp +6 -2
  603. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp +146 -13
  604. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy.comp +3 -1
  605. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +1 -1
  606. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +25 -1
  607. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.glsl +88 -0
  608. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.glsl +643 -1
  609. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_nvfp4.comp +32 -0
  610. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q1_0.comp +29 -0
  611. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/diag.comp +0 -1
  612. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dot_product_funcs.glsl +27 -0
  613. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/elu.comp +27 -0
  614. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/exp.comp +0 -1
  615. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/feature-tests/coopmat2_decode_vector.comp +7 -0
  616. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +533 -180
  617. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl +113 -68
  618. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +412 -222
  619. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +222 -83
  620. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_dequant.glsl +131 -0
  621. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_mask_opt.comp +162 -0
  622. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_mmq_funcs.glsl +203 -0
  623. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_split_k_reduce.comp +9 -8
  624. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/fwht.comp +115 -0
  625. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net.comp +189 -0
  626. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.glsl +0 -1
  627. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_head.glsl +10 -1
  628. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_main.glsl +16 -6
  629. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +76 -54
  630. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col_3d.comp +0 -1
  631. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/l2_norm.comp +12 -9
  632. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/log.comp +0 -1
  633. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec.comp +122 -27
  634. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_base.glsl +20 -17
  635. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iface.glsl +6 -6
  636. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q2_k.comp +1 -1
  637. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q4_k.comp +1 -1
  638. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q5_k.comp +1 -1
  639. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq.comp +1 -0
  640. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq_funcs.glsl +88 -55
  641. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +22 -20
  642. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +51 -14
  643. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.glsl +159 -125
  644. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_id_funcs.glsl +3 -1
  645. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq.comp +5 -3
  646. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.glsl +8 -8
  647. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_shmem_types.glsl +24 -9
  648. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/multi_add.comp +0 -1
  649. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm.comp +2 -3
  650. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_funcs.glsl +39 -63
  651. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.glsl +0 -1
  652. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_multi.comp +7 -4
  653. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_neox.comp +7 -4
  654. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_norm.comp +7 -4
  655. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_params.glsl +13 -7
  656. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_vision.comp +7 -4
  657. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sgn.comp +21 -0
  658. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/snake.comp +49 -0
  659. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/ssm_conv.comp +27 -11
  660. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/tri.comp +0 -1
  661. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/types.glsl +79 -2
  662. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +193 -149
  663. data/ext/sources/ggml/src/ggml-webgpu/CMakeLists.txt +5 -2
  664. data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp +3221 -97
  665. data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu.cpp +3493 -1997
  666. data/ext/sources/ggml/src/ggml-webgpu/pre_wgsl.hpp +37 -7
  667. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/add_id.wgsl +64 -0
  668. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/argmax.wgsl +72 -0
  669. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/argsort.wgsl +106 -0
  670. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/argsort_merge.wgsl +134 -0
  671. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/binary.wgsl +142 -0
  672. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/common_decls.tmpl +115 -141
  673. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/concat.wgsl +93 -0
  674. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/conv2d.wgsl +165 -0
  675. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{cpy.tmpl.wgsl → cpy.wgsl} +25 -44
  676. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/cumsum.wgsl +66 -0
  677. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn.wgsl +198 -230
  678. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_quant_staging.tmpl +124 -0
  679. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_tile.wgsl +397 -0
  680. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_blk.wgsl +101 -0
  681. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_reduce.wgsl +84 -0
  682. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_split.wgsl +619 -0
  683. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/gated_delta_net.wgsl +149 -0
  684. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{get_rows.tmpl.wgsl → get_rows.wgsl} +234 -335
  685. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/glu.wgsl +155 -0
  686. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/im2col.wgsl +101 -0
  687. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_decls.tmpl +871 -42
  688. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id.wgsl +195 -0
  689. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id_gather.wgsl +52 -0
  690. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id_vec.wgsl +154 -0
  691. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_reg_tile.wgsl +149 -0
  692. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{mul_mat_subgroup_matrix.tmpl.wgsl → mul_mat_subgroup_matrix.wgsl} +36 -138
  693. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec.wgsl +151 -0
  694. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec_acc.tmpl +1432 -0
  695. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec_q_acc.tmpl +303 -0
  696. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/pad.wgsl +86 -0
  697. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/quant_inner_loops.tmpl +21 -0
  698. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/quantize_q8.wgsl +173 -0
  699. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/repeat.wgsl +67 -0
  700. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm_mul.wgsl +152 -0
  701. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{rope.tmpl.wgsl → rope.wgsl} +71 -142
  702. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/row_norm.wgsl +153 -0
  703. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{scale.tmpl.wgsl → scale.wgsl} +15 -40
  704. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set.wgsl +109 -0
  705. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.wgsl +39 -12
  706. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows_quant.wgsl +224 -0
  707. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{soft_max.tmpl.wgsl → soft_max.wgsl} +106 -206
  708. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/solve_tri.wgsl +121 -0
  709. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/ssm_conv.wgsl +65 -0
  710. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/ssm_scan.wgsl +193 -0
  711. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/sum_rows.wgsl +55 -0
  712. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/unary.wgsl +213 -0
  713. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/upscale.wgsl +240 -0
  714. data/ext/sources/ggml/src/ggml-zdnn/ggml-zdnn.cpp +24 -15
  715. data/ext/sources/ggml/src/ggml-zendnn/CMakeLists.txt +31 -32
  716. data/ext/sources/ggml/src/ggml-zendnn/ggml-zendnn.cpp +253 -16
  717. data/ext/sources/ggml/src/ggml.c +268 -52
  718. data/ext/sources/ggml/src/gguf.cpp +377 -47
  719. data/ext/sources/include/parakeet.h +342 -0
  720. data/ext/sources/include/whisper.h +10 -0
  721. data/ext/sources/media/matmul.png +0 -0
  722. data/ext/sources/src/CMakeLists.txt +23 -0
  723. data/ext/sources/src/parakeet-arch.h +188 -0
  724. data/ext/sources/src/parakeet.cpp +3838 -0
  725. data/ext/sources/src/whisper.cpp +62 -40
  726. data/extsources.rb +26 -10
  727. data/lib/whisper/log_settable.rb +36 -0
  728. data/lib/whisper/model/uri.rb +13 -1
  729. data/lib/whisper/output.rb +74 -0
  730. data/sig/whisper.rbs +445 -55
  731. data/test/helper.rb +2 -0
  732. data/test/jfk_reader/jfk_reader.c +50 -7
  733. data/test/test_callback.rb +1 -0
  734. data/test/test_context_params.rb +82 -0
  735. data/test/test_package.rb +6 -5
  736. data/test/test_parakeet.rb +28 -0
  737. data/test/test_parakeet_callback.rb +107 -0
  738. data/test/test_parakeet_context.rb +116 -0
  739. data/test/test_parakeet_context_params.rb +24 -0
  740. data/test/test_parakeet_model.rb +21 -0
  741. data/test/test_parakeet_params.rb +78 -0
  742. data/test/test_parakeet_segment.rb +42 -0
  743. data/test/test_parakeet_token.rb +73 -0
  744. data/test/test_params.rb +2 -0
  745. data/test/test_token.rb +11 -0
  746. data/test/test_vad_context.rb +58 -8
  747. data/test/test_vad_segment.rb +1 -1
  748. data/test/test_whisper.rb +44 -6
  749. data/whispercpp.gemspec +2 -2
  750. metadata +426 -280
  751. data/ext/sources/bindings/javascript/CMakeLists.txt +0 -41
  752. data/ext/sources/bindings/javascript/emscripten.cpp +0 -93
  753. data/ext/sources/bindings/javascript/libwhisper.worker.js +0 -1
  754. data/ext/sources/bindings/javascript/package.json +0 -26
  755. data/ext/sources/bindings/javascript/whisper.js +0 -19
  756. data/ext/sources/examples/addon.node/CMakeLists.txt +0 -31
  757. data/ext/sources/examples/addon.node/__test__/whisper.spec.js +0 -133
  758. data/ext/sources/examples/addon.node/addon.cpp +0 -557
  759. data/ext/sources/examples/addon.node/index.js +0 -59
  760. data/ext/sources/examples/addon.node/package.json +0 -16
  761. data/ext/sources/examples/addon.node/vad-example.js +0 -132
  762. data/ext/sources/examples/bench.wasm/CMakeLists.txt +0 -49
  763. data/ext/sources/examples/bench.wasm/emscripten.cpp +0 -87
  764. data/ext/sources/examples/bench.wasm/index-tmpl.html +0 -285
  765. data/ext/sources/examples/coi-serviceworker.js +0 -146
  766. data/ext/sources/examples/command/CMakeLists.txt +0 -10
  767. data/ext/sources/examples/command/command.cpp +0 -802
  768. data/ext/sources/examples/command/commands.txt +0 -9
  769. data/ext/sources/examples/command.wasm/CMakeLists.txt +0 -50
  770. data/ext/sources/examples/command.wasm/emscripten.cpp +0 -327
  771. data/ext/sources/examples/command.wasm/index-tmpl.html +0 -415
  772. data/ext/sources/examples/generate-karaoke.sh +0 -57
  773. data/ext/sources/examples/helpers.js +0 -191
  774. data/ext/sources/examples/livestream.sh +0 -112
  775. data/ext/sources/examples/lsp/CMakeLists.txt +0 -10
  776. data/ext/sources/examples/lsp/lsp.cpp +0 -471
  777. data/ext/sources/examples/lsp/whisper.vim +0 -362
  778. data/ext/sources/examples/python/test_whisper_processor.py +0 -7
  779. data/ext/sources/examples/python/whisper_processor.py +0 -54
  780. data/ext/sources/examples/server/bench.js +0 -29
  781. data/ext/sources/examples/server.py +0 -120
  782. data/ext/sources/examples/stream/CMakeLists.txt +0 -10
  783. data/ext/sources/examples/stream/stream.cpp +0 -437
  784. data/ext/sources/examples/stream.wasm/CMakeLists.txt +0 -49
  785. data/ext/sources/examples/stream.wasm/emscripten.cpp +0 -216
  786. data/ext/sources/examples/stream.wasm/index-tmpl.html +0 -491
  787. data/ext/sources/examples/sycl/CMakeLists.txt +0 -9
  788. data/ext/sources/examples/sycl/build.sh +0 -22
  789. data/ext/sources/examples/sycl/ls-sycl-device.cpp +0 -11
  790. data/ext/sources/examples/sycl/run-whisper.sh +0 -17
  791. data/ext/sources/examples/talk-llama/CMakeLists.txt +0 -47
  792. data/ext/sources/examples/talk-llama/eleven-labs.py +0 -80
  793. data/ext/sources/examples/talk-llama/llama-adapter.cpp +0 -494
  794. data/ext/sources/examples/talk-llama/llama-adapter.h +0 -88
  795. data/ext/sources/examples/talk-llama/llama-arch.cpp +0 -2559
  796. data/ext/sources/examples/talk-llama/llama-arch.h +0 -586
  797. data/ext/sources/examples/talk-llama/llama-batch.cpp +0 -917
  798. data/ext/sources/examples/talk-llama/llama-batch.h +0 -173
  799. data/ext/sources/examples/talk-llama/llama-chat.cpp +0 -876
  800. data/ext/sources/examples/talk-llama/llama-chat.h +0 -70
  801. data/ext/sources/examples/talk-llama/llama-context.cpp +0 -3645
  802. data/ext/sources/examples/talk-llama/llama-context.h +0 -360
  803. data/ext/sources/examples/talk-llama/llama-cparams.cpp +0 -5
  804. data/ext/sources/examples/talk-llama/llama-cparams.h +0 -42
  805. data/ext/sources/examples/talk-llama/llama-grammar.cpp +0 -1464
  806. data/ext/sources/examples/talk-llama/llama-grammar.h +0 -194
  807. data/ext/sources/examples/talk-llama/llama-graph.cpp +0 -2282
  808. data/ext/sources/examples/talk-llama/llama-graph.h +0 -910
  809. data/ext/sources/examples/talk-llama/llama-hparams.cpp +0 -241
  810. data/ext/sources/examples/talk-llama/llama-hparams.h +0 -284
  811. data/ext/sources/examples/talk-llama/llama-impl.cpp +0 -171
  812. data/ext/sources/examples/talk-llama/llama-impl.h +0 -63
  813. data/ext/sources/examples/talk-llama/llama-io.cpp +0 -15
  814. data/ext/sources/examples/talk-llama/llama-io.h +0 -35
  815. data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.cpp +0 -328
  816. data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.h +0 -137
  817. data/ext/sources/examples/talk-llama/llama-kv-cache.cpp +0 -2100
  818. data/ext/sources/examples/talk-llama/llama-kv-cache.h +0 -390
  819. data/ext/sources/examples/talk-llama/llama-kv-cells.h +0 -533
  820. data/ext/sources/examples/talk-llama/llama-memory-hybrid.cpp +0 -268
  821. data/ext/sources/examples/talk-llama/llama-memory-hybrid.h +0 -139
  822. data/ext/sources/examples/talk-llama/llama-memory-recurrent.cpp +0 -1167
  823. data/ext/sources/examples/talk-llama/llama-memory-recurrent.h +0 -182
  824. data/ext/sources/examples/talk-llama/llama-memory.cpp +0 -59
  825. data/ext/sources/examples/talk-llama/llama-memory.h +0 -122
  826. data/ext/sources/examples/talk-llama/llama-mmap.cpp +0 -735
  827. data/ext/sources/examples/talk-llama/llama-mmap.h +0 -73
  828. data/ext/sources/examples/talk-llama/llama-model-loader.cpp +0 -1247
  829. data/ext/sources/examples/talk-llama/llama-model-loader.h +0 -176
  830. data/ext/sources/examples/talk-llama/llama-model-saver.cpp +0 -285
  831. data/ext/sources/examples/talk-llama/llama-model-saver.h +0 -37
  832. data/ext/sources/examples/talk-llama/llama-model.cpp +0 -8338
  833. data/ext/sources/examples/talk-llama/llama-model.h +0 -544
  834. data/ext/sources/examples/talk-llama/llama-quant.cpp +0 -1072
  835. data/ext/sources/examples/talk-llama/llama-quant.h +0 -1
  836. data/ext/sources/examples/talk-llama/llama-sampling.cpp +0 -3771
  837. data/ext/sources/examples/talk-llama/llama-sampling.h +0 -44
  838. data/ext/sources/examples/talk-llama/llama-vocab.cpp +0 -3900
  839. data/ext/sources/examples/talk-llama/llama-vocab.h +0 -182
  840. data/ext/sources/examples/talk-llama/llama.cpp +0 -1140
  841. data/ext/sources/examples/talk-llama/llama.h +0 -1540
  842. data/ext/sources/examples/talk-llama/models/afmoe.cpp +0 -191
  843. data/ext/sources/examples/talk-llama/models/apertus.cpp +0 -125
  844. data/ext/sources/examples/talk-llama/models/arcee.cpp +0 -135
  845. data/ext/sources/examples/talk-llama/models/arctic.cpp +0 -138
  846. data/ext/sources/examples/talk-llama/models/arwkv7.cpp +0 -86
  847. data/ext/sources/examples/talk-llama/models/baichuan.cpp +0 -122
  848. data/ext/sources/examples/talk-llama/models/bailingmoe.cpp +0 -144
  849. data/ext/sources/examples/talk-llama/models/bailingmoe2.cpp +0 -135
  850. data/ext/sources/examples/talk-llama/models/bert.cpp +0 -178
  851. data/ext/sources/examples/talk-llama/models/bitnet.cpp +0 -160
  852. data/ext/sources/examples/talk-llama/models/bloom.cpp +0 -101
  853. data/ext/sources/examples/talk-llama/models/chameleon.cpp +0 -178
  854. data/ext/sources/examples/talk-llama/models/chatglm.cpp +0 -132
  855. data/ext/sources/examples/talk-llama/models/codeshell.cpp +0 -111
  856. data/ext/sources/examples/talk-llama/models/cogvlm.cpp +0 -102
  857. data/ext/sources/examples/talk-llama/models/cohere2-iswa.cpp +0 -134
  858. data/ext/sources/examples/talk-llama/models/command-r.cpp +0 -122
  859. data/ext/sources/examples/talk-llama/models/dbrx.cpp +0 -123
  860. data/ext/sources/examples/talk-llama/models/deci.cpp +0 -135
  861. data/ext/sources/examples/talk-llama/models/deepseek.cpp +0 -144
  862. data/ext/sources/examples/talk-llama/models/deepseek2.cpp +0 -259
  863. data/ext/sources/examples/talk-llama/models/dots1.cpp +0 -134
  864. data/ext/sources/examples/talk-llama/models/dream.cpp +0 -105
  865. data/ext/sources/examples/talk-llama/models/ernie4-5-moe.cpp +0 -150
  866. data/ext/sources/examples/talk-llama/models/ernie4-5.cpp +0 -110
  867. data/ext/sources/examples/talk-llama/models/exaone.cpp +0 -114
  868. data/ext/sources/examples/talk-llama/models/exaone4.cpp +0 -123
  869. data/ext/sources/examples/talk-llama/models/falcon-h1.cpp +0 -113
  870. data/ext/sources/examples/talk-llama/models/falcon.cpp +0 -120
  871. data/ext/sources/examples/talk-llama/models/gemma-embedding.cpp +0 -116
  872. data/ext/sources/examples/talk-llama/models/gemma.cpp +0 -112
  873. data/ext/sources/examples/talk-llama/models/gemma2-iswa.cpp +0 -128
  874. data/ext/sources/examples/talk-llama/models/gemma3.cpp +0 -155
  875. data/ext/sources/examples/talk-llama/models/gemma3n-iswa.cpp +0 -384
  876. data/ext/sources/examples/talk-llama/models/glm4-moe.cpp +0 -170
  877. data/ext/sources/examples/talk-llama/models/glm4.cpp +0 -150
  878. data/ext/sources/examples/talk-llama/models/gpt2.cpp +0 -105
  879. data/ext/sources/examples/talk-llama/models/gptneox.cpp +0 -144
  880. data/ext/sources/examples/talk-llama/models/granite-hybrid.cpp +0 -196
  881. data/ext/sources/examples/talk-llama/models/granite.cpp +0 -211
  882. data/ext/sources/examples/talk-llama/models/graph-context-mamba.cpp +0 -283
  883. data/ext/sources/examples/talk-llama/models/grok.cpp +0 -159
  884. data/ext/sources/examples/talk-llama/models/grovemoe.cpp +0 -141
  885. data/ext/sources/examples/talk-llama/models/hunyuan-dense.cpp +0 -132
  886. data/ext/sources/examples/talk-llama/models/hunyuan-moe.cpp +0 -154
  887. data/ext/sources/examples/talk-llama/models/internlm2.cpp +0 -120
  888. data/ext/sources/examples/talk-llama/models/jais.cpp +0 -86
  889. data/ext/sources/examples/talk-llama/models/jamba.cpp +0 -106
  890. data/ext/sources/examples/talk-llama/models/lfm2.cpp +0 -175
  891. data/ext/sources/examples/talk-llama/models/llada-moe.cpp +0 -122
  892. data/ext/sources/examples/talk-llama/models/llada.cpp +0 -99
  893. data/ext/sources/examples/talk-llama/models/llama-iswa.cpp +0 -178
  894. data/ext/sources/examples/talk-llama/models/llama.cpp +0 -168
  895. data/ext/sources/examples/talk-llama/models/maincoder.cpp +0 -117
  896. data/ext/sources/examples/talk-llama/models/mamba.cpp +0 -55
  897. data/ext/sources/examples/talk-llama/models/mimo2-iswa.cpp +0 -123
  898. data/ext/sources/examples/talk-llama/models/minicpm3.cpp +0 -199
  899. data/ext/sources/examples/talk-llama/models/minimax-m2.cpp +0 -124
  900. data/ext/sources/examples/talk-llama/models/mistral3.cpp +0 -160
  901. data/ext/sources/examples/talk-llama/models/models.h +0 -569
  902. data/ext/sources/examples/talk-llama/models/modern-bert.cpp +0 -116
  903. data/ext/sources/examples/talk-llama/models/mpt.cpp +0 -126
  904. data/ext/sources/examples/talk-llama/models/nemotron-h.cpp +0 -150
  905. data/ext/sources/examples/talk-llama/models/nemotron.cpp +0 -122
  906. data/ext/sources/examples/talk-llama/models/neo-bert.cpp +0 -104
  907. data/ext/sources/examples/talk-llama/models/olmo.cpp +0 -121
  908. data/ext/sources/examples/talk-llama/models/olmo2.cpp +0 -150
  909. data/ext/sources/examples/talk-llama/models/olmoe.cpp +0 -124
  910. data/ext/sources/examples/talk-llama/models/openai-moe-iswa.cpp +0 -127
  911. data/ext/sources/examples/talk-llama/models/openelm.cpp +0 -124
  912. data/ext/sources/examples/talk-llama/models/orion.cpp +0 -123
  913. data/ext/sources/examples/talk-llama/models/pangu-embedded.cpp +0 -121
  914. data/ext/sources/examples/talk-llama/models/phi2.cpp +0 -121
  915. data/ext/sources/examples/talk-llama/models/phi3.cpp +0 -152
  916. data/ext/sources/examples/talk-llama/models/plamo.cpp +0 -110
  917. data/ext/sources/examples/talk-llama/models/plamo2.cpp +0 -316
  918. data/ext/sources/examples/talk-llama/models/plamo3.cpp +0 -128
  919. data/ext/sources/examples/talk-llama/models/plm.cpp +0 -168
  920. data/ext/sources/examples/talk-llama/models/qwen.cpp +0 -108
  921. data/ext/sources/examples/talk-llama/models/qwen2.cpp +0 -126
  922. data/ext/sources/examples/talk-llama/models/qwen2moe.cpp +0 -151
  923. data/ext/sources/examples/talk-llama/models/qwen2vl.cpp +0 -117
  924. data/ext/sources/examples/talk-llama/models/qwen3.cpp +0 -117
  925. data/ext/sources/examples/talk-llama/models/qwen3moe.cpp +0 -124
  926. data/ext/sources/examples/talk-llama/models/qwen3next.cpp +0 -873
  927. data/ext/sources/examples/talk-llama/models/qwen3vl-moe.cpp +0 -149
  928. data/ext/sources/examples/talk-llama/models/qwen3vl.cpp +0 -141
  929. data/ext/sources/examples/talk-llama/models/refact.cpp +0 -94
  930. data/ext/sources/examples/talk-llama/models/rnd1.cpp +0 -126
  931. data/ext/sources/examples/talk-llama/models/rwkv6-base.cpp +0 -162
  932. data/ext/sources/examples/talk-llama/models/rwkv6.cpp +0 -94
  933. data/ext/sources/examples/talk-llama/models/rwkv6qwen2.cpp +0 -86
  934. data/ext/sources/examples/talk-llama/models/rwkv7-base.cpp +0 -135
  935. data/ext/sources/examples/talk-llama/models/rwkv7.cpp +0 -90
  936. data/ext/sources/examples/talk-llama/models/seed-oss.cpp +0 -124
  937. data/ext/sources/examples/talk-llama/models/smallthinker.cpp +0 -126
  938. data/ext/sources/examples/talk-llama/models/smollm3.cpp +0 -128
  939. data/ext/sources/examples/talk-llama/models/stablelm.cpp +0 -146
  940. data/ext/sources/examples/talk-llama/models/starcoder.cpp +0 -100
  941. data/ext/sources/examples/talk-llama/models/starcoder2.cpp +0 -121
  942. data/ext/sources/examples/talk-llama/models/t5-dec.cpp +0 -166
  943. data/ext/sources/examples/talk-llama/models/t5-enc.cpp +0 -96
  944. data/ext/sources/examples/talk-llama/models/wavtokenizer-dec.cpp +0 -149
  945. data/ext/sources/examples/talk-llama/models/xverse.cpp +0 -108
  946. data/ext/sources/examples/talk-llama/prompts/talk-alpaca.txt +0 -23
  947. data/ext/sources/examples/talk-llama/speak +0 -40
  948. data/ext/sources/examples/talk-llama/speak.bat +0 -1
  949. data/ext/sources/examples/talk-llama/speak.ps1 +0 -14
  950. data/ext/sources/examples/talk-llama/talk-llama.cpp +0 -813
  951. data/ext/sources/examples/talk-llama/unicode-data.cpp +0 -7034
  952. data/ext/sources/examples/talk-llama/unicode-data.h +0 -20
  953. data/ext/sources/examples/talk-llama/unicode.cpp +0 -1147
  954. data/ext/sources/examples/talk-llama/unicode.h +0 -111
  955. data/ext/sources/examples/wchess/CMakeLists.txt +0 -10
  956. data/ext/sources/examples/wchess/libwchess/CMakeLists.txt +0 -19
  957. data/ext/sources/examples/wchess/libwchess/Chessboard.cpp +0 -803
  958. data/ext/sources/examples/wchess/libwchess/Chessboard.h +0 -33
  959. data/ext/sources/examples/wchess/libwchess/WChess.cpp +0 -193
  960. data/ext/sources/examples/wchess/libwchess/WChess.h +0 -63
  961. data/ext/sources/examples/wchess/libwchess/test-chessboard.cpp +0 -117
  962. data/ext/sources/examples/wchess/wchess.cmd/CMakeLists.txt +0 -8
  963. data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +0 -253
  964. data/ext/sources/examples/whisper.wasm/CMakeLists.txt +0 -50
  965. data/ext/sources/examples/whisper.wasm/emscripten.cpp +0 -118
  966. data/ext/sources/examples/whisper.wasm/index-tmpl.html +0 -659
  967. data/ext/sources/ggml/cmake/BuildTypes.cmake +0 -54
  968. data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm-ppc.h +0 -333
  969. data/ext/sources/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +0 -99
  970. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-dma.h +0 -157
  971. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-msg.h +0 -165
  972. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-exp.c +0 -94
  973. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-inverse.c +0 -72
  974. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sigmoid.c +0 -49
  975. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-utils.c +0 -1020
  976. data/ext/sources/ggml/src/ggml-hexagon/htp/ops-utils.h +0 -149
  977. data/ext/sources/ggml/src/ggml-hexagon/htp-utils.c +0 -454
  978. data/ext/sources/ggml/src/ggml-hexagon/htp-utils.h +0 -221
  979. data/ext/sources/ggml/src/ggml-hexagon/op-desc.h +0 -153
  980. data/ext/sources/ggml/src/ggml-opencl/kernels/embed_kernel.py +0 -26
  981. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rte.glsl +0 -5
  982. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/bin_op.tmpl.wgsl +0 -188
  983. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/binary_head.tmpl +0 -45
  984. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +0 -147
  985. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/glu.tmpl.wgsl +0 -323
  986. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.tmpl.wgsl +0 -907
  987. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_reg_tile.tmpl.wgsl +0 -247
  988. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec.tmpl.wgsl +0 -267
  989. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm.wgsl +0 -123
  990. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.tmpl.wgsl +0 -112
  991. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/unary_op.wgsl +0 -483
  992. data/ext/sources/tests/CMakeLists.txt +0 -112
  993. data/ext/sources/tests/earnings21/eval.mk +0 -58
  994. data/ext/sources/tests/earnings21/eval.py +0 -68
  995. data/ext/sources/tests/earnings21/normalizers/__init__.py +0 -2
  996. data/ext/sources/tests/earnings21/normalizers/basic.py +0 -80
  997. data/ext/sources/tests/earnings21/normalizers/english.json +0 -1741
  998. data/ext/sources/tests/earnings21/normalizers/english.py +0 -550
  999. data/ext/sources/tests/earnings21/requirements.txt +0 -6
  1000. data/ext/sources/tests/en-0-ref.txt +0 -1
  1001. data/ext/sources/tests/en-1-ref.txt +0 -1
  1002. data/ext/sources/tests/en-2-ref.txt +0 -1
  1003. data/ext/sources/tests/es-0-ref.txt +0 -1
  1004. data/ext/sources/tests/librispeech/eval.mk +0 -39
  1005. data/ext/sources/tests/librispeech/eval.py +0 -47
  1006. data/ext/sources/tests/librispeech/normalizers/__init__.py +0 -2
  1007. data/ext/sources/tests/librispeech/normalizers/basic.py +0 -80
  1008. data/ext/sources/tests/librispeech/normalizers/english.json +0 -1741
  1009. data/ext/sources/tests/librispeech/normalizers/english.py +0 -550
  1010. data/ext/sources/tests/librispeech/requirements.txt +0 -6
  1011. data/ext/sources/tests/run-tests.sh +0 -130
  1012. data/ext/sources/tests/test-c.c +0 -3
  1013. data/ext/sources/tests/test-vad-full.cpp +0 -56
  1014. data/ext/sources/tests/test-vad.cpp +0 -83
  1015. data/ext/sources/tests/test-whisper.js +0 -58
  1016. data/lib/whisper/context.rb +0 -15
  1017. data/lib/whisper/segment.rb +0 -58
@@ -0,0 +1,2066 @@
1
+ #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
2
+ #pragma clang diagnostic ignored "-Wunused-function"
3
+ #pragma clang diagnostic ignored "-Wunused-variable"
4
+ #pragma clang diagnostic ignored "-Wunused-but-set-variable"
5
+
6
+ #include <assert.h>
7
+ #include <stdbool.h>
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+ #include <string.h>
11
+
12
+ #include <HAP_farf.h>
13
+ #include <HAP_compute_res.h>
14
+
15
+ #define GGML_COMMON_DECL_C
16
+ #include "ggml-common.h"
17
+
18
+ #include "hex-dma.h"
19
+ #include "hex-fastdiv.h"
20
+ #include "worker-pool.h"
21
+
22
+ #include "hvx-utils.h"
23
+ #include "hvx-dump.h"
24
+ #include "htp-ctx.h"
25
+ #include "htp-ops.h"
26
+
27
+ #include "hmx-ops.h"
28
+ #include "hmx-utils.h"
29
+ #include "hmx-queue.h"
30
+ #include "hmx-profile.h"
31
+
32
+ #include "vtcm-utils.h"
33
+
34
+ static const __fp16 q4_0_to_fp16_lut[64] __attribute__((aligned(VLEN))) = {
35
+ -8, 0, -7, 0, -6, 0, -5, 0, -4, 0, -3, 0, -2, 0, -1, 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0,
36
+ };
37
+
38
+ static const __fp16 q4_1_to_fp16_lut[64] __attribute__((aligned(VLEN))) = {
39
+ 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0,
40
+ };
41
+
42
+ // MXFP4 dequantization LUT: maps 4-bit index to fp16 mantissa value
43
+ // kvalues: 0, 0.5, 1, 1.5, 2, 3, 4, 6, 0, -0.5, -1, -1.5, -2, -3, -4, -6
44
+ static const __fp16 mxfp4_to_fp16_lut[64] __attribute__((aligned(VLEN))) = {
45
+ 0, 0, 0.5, 0, 1, 0, 1.5, 0, 2, 0, 3, 0, 4, 0, 6, 0, 0, 0, -0.5, 0, -1, 0, -1.5, 0, -2, 0, -3, 0, -4, 0, -6, 0,
46
+ };
47
+
48
+ static const __fp16 iq4_nl_to_fp16_lut[64] __attribute__((aligned(VLEN))) = {
49
+ -127, 0, -104, 0, -83, 0, -65, 0, -49, 0, -35, 0, -22, 0, -10, 0,
50
+ 1, 0, 13, 0, 25, 0, 38, 0, 53, 0, 69, 0, 89, 0, 113, 0,
51
+ };
52
+
53
+ // Scales per x4x2 logical block: 8 × sizeof(__fp16) = 16 bytes
54
+ #define HMX_X4X2_SCALES_PER_BLK 8
55
+ #define HMX_X4X2_DBLK_SIZE 16 // 8 * 2 bytes (fp16 scales for Q4_0/Q8_0/IQ4_NL)
56
+ #define HMX_X4X2_MXFP4_EBLK_SIZE 8 // 8 * 1 byte (E8M0 scales for MXFP4)
57
+
58
+ // Compute the byte stride of one row in x4x2 format.
59
+ // Numerically equals ggml_row_size(type, k) when k is 256-aligned, because
60
+ // x4x2 packing has the same density as block_q4_0 / block_q8_0.
61
+ // Layout per row: [quants: nb*128 (Q4) or nb*256 (Q8)][scales: nb*16 bytes]
62
+ // Total per row = nb * (128+16) = 144*nb (Q4) or nb * (256+16) = 272*nb (Q8).
63
+ // Callers must ensure k is a multiple of 256 (enforced by proc_hmx_matmul_req).
64
+ static inline size_t get_x4x2_row_stride(int weight_type, int k) {
65
+ int nb = (k + QK_Q4_0x4x2 - 1) / QK_Q4_0x4x2;
66
+ switch (weight_type) {
67
+ case HTP_TYPE_Q4_0:
68
+ case HTP_TYPE_IQ4_NL:
69
+ return (size_t) nb * (QK_Q4_0x4x2 / 2 + HMX_X4X2_DBLK_SIZE); // 144 * nb
70
+ case HTP_TYPE_Q4_1:
71
+ return (size_t) nb * (QK_Q4_0x4x2 / 2 + 32); // 160 * nb
72
+ case HTP_TYPE_Q8_0:
73
+ return (size_t) nb * (QK_Q8_0x4x2 + HMX_X4X2_DBLK_SIZE); // 272 * nb
74
+ case HTP_TYPE_MXFP4:
75
+ return (size_t) nb * (QK_MXFP4x4x2 / 2 + HMX_X4X2_MXFP4_EBLK_SIZE); // 136 * nb
76
+ case HTP_TYPE_F16:
77
+ return (size_t) k * sizeof(__fp16);
78
+ case HTP_TYPE_F32:
79
+ return (size_t) k * sizeof(float);
80
+ default:
81
+ return 0;
82
+ }
83
+ }
84
+
85
+ // --- Overflow-safe arithmetic for VTCM budget calculation ---
86
+
87
+ static inline bool hmx_mul_overflow(size_t a, size_t b, size_t *out) {
88
+ if (a != 0 && b > SIZE_MAX / a) return true;
89
+ *out = a * b;
90
+ return false;
91
+ }
92
+
93
+ static inline bool hmx_add_overflow(size_t a, size_t b, size_t *out) {
94
+ if (a > SIZE_MAX - b) return true;
95
+ *out = a + b;
96
+ return false;
97
+ }
98
+
99
+ // Search for optimal (mc, nc) chunk sizes within VTCM budget.
100
+ //
101
+ // VTCM model: nc * per_n_cost + mc * per_m_cost + mc * nc * per_mn_cost + overhead
102
+ //
103
+ // Minimize ceil(m/mc) * m_block_cost + ceil(n/nc) * n_block_cost.
104
+ // All matmul paths repeat weight processing per M-block and activation loading
105
+ // per N-block, so discrete block counts drive total overhead.
106
+ // Tie-break: when cost is equal, prefer larger mc * nc.
107
+ //
108
+ // Caller-provided coefficients:
109
+ // m_block_cost: penalty per extra M-block (weight redundancy, scales with n).
110
+ // n_block_cost: penalty per extra N-block (activation redundancy, scales with m).
111
+ //
112
+ // Algorithm: nc sweeps from n_max down by 32, analytically solving for mc_max.
113
+ // Returns 0 on success, -1 if VTCM is insufficient.
114
+ static int hmx_compute_chunks(size_t vtcm_total,
115
+ size_t overhead,
116
+ size_t per_n_cost,
117
+ size_t per_m_cost,
118
+ size_t per_mn_cost,
119
+ int m,
120
+ int n,
121
+ size_t m_block_cost,
122
+ size_t n_block_cost,
123
+ size_t * m_chunk_out,
124
+ size_t * n_chunk_out,
125
+ size_t * total_out) {
126
+ if (m <= 0 || n <= 0) return -1;
127
+ if (vtcm_total <= overhead) return -1;
128
+ if (per_n_cost == 0 || per_m_cost == 0 || per_mn_cost == 0) return -1;
129
+
130
+ const size_t usable = vtcm_total - overhead;
131
+
132
+ size_t best_cost = SIZE_MAX;
133
+ size_t best_mn = 0;
134
+ size_t best_m = 0, best_n = 0;
135
+
136
+ const size_t n_max = hex_align_down((size_t)n, HMX_FP16_TILE_N_COLS);
137
+ for (size_t nc = n_max; nc >= HMX_FP16_TILE_N_COLS; nc -= HMX_FP16_TILE_N_COLS) {
138
+ size_t n_fixed = 0, ncmn = 0, mc_denom = 0;
139
+ if (hmx_mul_overflow(nc, per_n_cost, &n_fixed)) continue;
140
+ if (n_fixed >= usable) goto next_nc;
141
+
142
+ if (hmx_mul_overflow(nc, per_mn_cost, &ncmn)) goto next_nc;
143
+ if (hmx_add_overflow(per_m_cost, ncmn, &mc_denom) || mc_denom == 0) goto next_nc;
144
+
145
+ {
146
+ size_t remain = usable - n_fixed;
147
+ size_t mc = remain / mc_denom;
148
+ mc = hex_align_down(mc, HMX_FP16_TILE_N_ROWS);
149
+ mc = hex_smin(mc, (size_t)m);
150
+
151
+ if (mc == 0) {
152
+ goto next_nc;
153
+ }
154
+
155
+ size_t mblocks = ((size_t) m + mc - 1) / mc;
156
+ size_t nblocks = ((size_t) n + nc - 1) / nc;
157
+ size_t cost = mblocks * m_block_cost + nblocks * n_block_cost;
158
+ size_t mn = mc * nc;
159
+ if (cost < best_cost || (cost == best_cost && mn > best_mn)) {
160
+ best_cost = cost;
161
+ best_mn = mn;
162
+ best_m = mc;
163
+ best_n = nc;
164
+ }
165
+ }
166
+
167
+ next_nc:
168
+ if (nc == HMX_FP16_TILE_N_COLS) break; // avoid size_t underflow
169
+ }
170
+
171
+ if (best_m == 0 || best_n == 0) return -1;
172
+
173
+ // Compute exact total (with overflow checks)
174
+ size_t t0 = 0, t1 = 0, t2 = 0, mn = 0, total = 0;
175
+ if (hmx_mul_overflow(best_n, per_n_cost, &t0)) return -1;
176
+ if (hmx_mul_overflow(best_m, per_m_cost, &t1)) return -1;
177
+ if (hmx_mul_overflow(best_m, best_n, &mn)) return -1;
178
+ if (hmx_mul_overflow(mn, per_mn_cost, &t2)) return -1;
179
+ if (hmx_add_overflow(t0, t1, &total)) return -1;
180
+ if (hmx_add_overflow(total, t2, &total)) return -1;
181
+ if (hmx_add_overflow(total, overhead, &total)) return -1;
182
+
183
+ *m_chunk_out = best_m;
184
+ *n_chunk_out = best_n;
185
+ *total_out = total;
186
+ return 0;
187
+ }
188
+
189
+ // --- x4x2 format dequantizers ---
190
+
191
+ // Dequantize one x4x2 Q4_0 group (32 elements from 32 packed bytes) -> 32 FP16 in first 64 bytes.
192
+ // In x4x2, sub-blocks 0..3 use lower nibbles, sub-blocks 4..7 use upper nibbles
193
+ // of the same 32 packed bytes.
194
+ static inline HVX_Vector dequantize_x4x2_q4_0_group_hvx(const uint8_t *packed_32, bool upper_nibbles, const __fp16 *scale, const HVX_Vector vlut_cvt) {
195
+ (void)vlut_cvt;
196
+ HVX_Vector vq = hvx_vmemu(packed_32);
197
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
198
+ const HVX_Vector i8 = Q6_Vb_vsplat_R(8);
199
+ HVX_Vector v_scales = hvx_vec_repl_f16(hvx_vmemu(scale));
200
+
201
+ HVX_Vector v_quants = Q6_Vub_vlsr_VubR(vq, 4 * upper_nibbles);
202
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
203
+
204
+ HVX_Vector v_int8 = Q6_Vb_vsub_VbVb(v_quants, i8);
205
+ HVX_Vector v0 = Q6_V_lo_W(Q6_Wh_vunpack_Vb(v_int8));
206
+ HVX_Vector v_hf = Q6_Vhf_equals_Vh(v0);
207
+
208
+ return Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_hf, v_scales));
209
+ }
210
+
211
+ // Batch-dequantize 4 contiguous x4x2 Q4_0 groups (4x32 = 128 packed bytes) using
212
+ // full HVX vector width.
213
+ // Output: vector_x2 each hold 32 FP16 values in the first 64 bytes.
214
+ static inline HVX_Vector_x2 dequantize_x4x2_q4_0_x4groups_hvx(
215
+ const uint8_t *packed_128, bool upper_nibbles,
216
+ const __fp16 *scales_4, const HVX_Vector vlut_cvt) {
217
+ (void)vlut_cvt;
218
+ HVX_Vector vq = hvx_vmemu(packed_128);
219
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
220
+ const HVX_Vector i8 = Q6_Vb_vsplat_R(8);
221
+ HVX_Vector v_quants = Q6_Vub_vlsr_VubR(vq, 4 * upper_nibbles);
222
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
223
+
224
+ HVX_Vector v_int8 = Q6_Vb_vsub_VbVb(v_quants, i8);
225
+
226
+ HVX_VectorPair vp_int16 = Q6_Wh_vunpack_Vb(v_int8);
227
+ HVX_Vector v_lo = Q6_V_lo_W(vp_int16);
228
+ HVX_Vector v_hi = Q6_V_hi_W(vp_int16);
229
+
230
+ v_lo = Q6_Vhf_equals_Vh(v_lo);
231
+ v_hi = Q6_Vhf_equals_Vh(v_hi);
232
+
233
+ HVX_Vector vscale = hvx_vmemu(scales_4);
234
+ HVX_Vector v_sc01 = hvx_vec_repl_2x_f16(vscale);
235
+ HVX_Vector v_sc23 = hvx_vec_repl_2x_f16(Q6_V_vror_VR(vscale, 4));
236
+
237
+ v_lo = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_lo, v_sc01));
238
+ v_hi = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_hi, v_sc23));
239
+
240
+ HVX_Vector_x2 r = { v_lo, v_hi };
241
+ return r;
242
+ }
243
+
244
+ static inline HVX_Vector dequantize_x4x2_q4_1_group_hvx(const uint8_t *packed_32, bool upper_nibbles, const __fp16 *scale_offset, const HVX_Vector vlut_cvt) {
245
+ (void)vlut_cvt;
246
+ HVX_Vector vq = hvx_vmemu(packed_32);
247
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
248
+ HVX_Vector v_dm = hvx_vmemu(scale_offset);
249
+ HVX_Vector v_scales = hvx_vec_repl_f16(v_dm);
250
+ HVX_Vector v_offsets = hvx_vec_repl_f16(Q6_V_vror_VR(v_dm, 2));
251
+
252
+ HVX_Vector v_quants = Q6_Vub_vlsr_VubR(vq, 4 * upper_nibbles);
253
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
254
+
255
+ HVX_Vector v0 = Q6_V_lo_W(Q6_Wh_vunpack_Vb(v_quants));
256
+ HVX_Vector v_hf = Q6_Vhf_equals_Vh(v0);
257
+
258
+ return Q6_Vhf_equals_Vqf16(Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(v_hf, v_scales), v_offsets));
259
+ }
260
+
261
+ static inline HVX_Vector_x2 dequantize_x4x2_q4_1_x4groups_hvx(
262
+ const uint8_t *packed_128, bool upper_nibbles,
263
+ const __fp16 *scales_offsets_4, const HVX_Vector vlut_cvt) {
264
+ (void)vlut_cvt;
265
+ HVX_Vector vq = hvx_vmemu(packed_128);
266
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
267
+ HVX_Vector v_quants = Q6_Vub_vlsr_VubR(vq, 4 * upper_nibbles);
268
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
269
+
270
+ HVX_VectorPair vp_int16 = Q6_Wh_vunpack_Vb(v_quants);
271
+ HVX_Vector v_lo = Q6_V_lo_W(vp_int16);
272
+ HVX_Vector v_hi = Q6_V_hi_W(vp_int16);
273
+
274
+ v_lo = Q6_Vhf_equals_Vh(v_lo);
275
+ v_hi = Q6_Vhf_equals_Vh(v_hi);
276
+
277
+ HVX_Vector vscale_offset = hvx_vmemu(scales_offsets_4);
278
+ HVX_VectorPair dm_deal = Q6_W_vdeal_VVR(vscale_offset, vscale_offset, -2);
279
+ HVX_Vector vd = Q6_V_lo_W(dm_deal);
280
+ HVX_Vector vm = Q6_V_hi_W(dm_deal);
281
+
282
+ HVX_Vector v_sc01 = hvx_vec_repl_2x_f16(vd);
283
+ HVX_Vector v_sc23 = hvx_vec_repl_2x_f16(Q6_V_vror_VR(vd, 4));
284
+
285
+ HVX_Vector v_os01 = hvx_vec_repl_2x_f16(vm);
286
+ HVX_Vector v_os23 = hvx_vec_repl_2x_f16(Q6_V_vror_VR(vm, 4));
287
+
288
+ v_lo = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(v_lo, v_sc01), v_os01));
289
+ v_hi = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vadd_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(v_hi, v_sc23), v_os23));
290
+
291
+ HVX_Vector_x2 r = { v_lo, v_hi };
292
+ return r;
293
+ }
294
+
295
+ // LUT-based dequantizers for non-linear IQ4_NL format.
296
+ static inline HVX_Vector dequantize_x4x2_iq4_nl_group_hvx(const uint8_t *packed_32, bool upper_nibbles, const __fp16 *scale, const HVX_Vector vlut_cvt) {
297
+ HVX_Vector vq = hvx_vmemu(packed_32);
298
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
299
+ HVX_Vector v_scales = hvx_vec_repl_f16(hvx_vmemu(scale));
300
+ HVX_Vector v_quants = Q6_Vub_vlsr_VubR(vq, 4 * upper_nibbles);
301
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
302
+ v_quants = Q6_Vb_vshuff_Vb(v_quants);
303
+ HVX_VectorPair vp = Q6_Wh_vlut16_VbVhR(v_quants, vlut_cvt, 0);
304
+ HVX_Vector v_hf = Q6_V_lo_W(vp);
305
+
306
+ return Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_hf, v_scales));
307
+ }
308
+
309
+ static inline HVX_Vector_x2 dequantize_x4x2_iq4_nl_x4groups_hvx(
310
+ const uint8_t *packed_128, bool upper_nibbles,
311
+ const __fp16 *scales_4, const HVX_Vector vlut_cvt) {
312
+ HVX_Vector vq = hvx_vmemu(packed_128);
313
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
314
+ HVX_Vector v_quants = Q6_Vub_vlsr_VubR(vq, 4 * upper_nibbles);
315
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
316
+
317
+ v_quants = Q6_Vb_vshuff_Vb(v_quants);
318
+
319
+ HVX_VectorPair vp = Q6_Wh_vlut16_VbVhR(v_quants, vlut_cvt, 0);
320
+ HVX_Vector v_lo = Q6_V_lo_W(vp);
321
+ HVX_Vector v_hi = Q6_V_hi_W(vp);
322
+
323
+ HVX_Vector vscale = hvx_vmemu(scales_4);
324
+ HVX_Vector v_sc01 = hvx_vec_repl_2x_f16(vscale);
325
+ HVX_Vector v_sc23 = hvx_vec_repl_2x_f16(Q6_V_vror_VR(vscale, 4));
326
+
327
+ v_lo = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_lo, v_sc01));
328
+ v_hi = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_hi, v_sc23));
329
+
330
+ HVX_Vector_x2 r = { v_lo, v_hi };
331
+ return r;
332
+ }
333
+
334
+ // Dequantize one x4x2 Q8_0 group (32 int8 quants) -> 32 FP16 in first 64 bytes.
335
+ static inline HVX_Vector dequantize_x4x2_q8_0_group_hvx(const int8_t *quants_32, const __fp16 *scale) {
336
+ HVX_Vector vq = hvx_vmemu(quants_32);
337
+ HVX_Vector v_scales = hvx_vec_repl_f16(hvx_vmemu(scale));
338
+ HVX_Vector v0 = Q6_V_lo_W(Q6_Wh_vunpack_Vb(vq));
339
+ HVX_Vector v_hf = Q6_Vhf_equals_Vh(v0);
340
+ return Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_hf, v_scales));
341
+ }
342
+
343
+ // --- MXFP4 E8M0 scale conversion and dequantization ---
344
+ //
345
+ // HVX batch-convert 8 E8M0 bytes (one x4x2 block's scales) to __fp16[8] on stack.
346
+ // Scalar loads from the stack array execute on the scalar pipeline, in parallel
347
+ // with HVX vlut16/vmpy/vscatter — freeing HVX slots in the hot loop.
348
+ // Arithmetic: fp16_bits = clamp(e - 112, 0, 30) << 10
349
+ // e=0..112 -> 0 (underflow), e=113..142 -> valid fp16, e>=143 -> clamped to 2^15.
350
+
351
+ typedef struct {
352
+ __fp16 v[8] __attribute__((aligned(16)));
353
+ } mxfp4_scales_t;
354
+
355
+ static inline mxfp4_scales_t mxfp4_convert_scales(const uint8_t * e8m0_8) {
356
+ mxfp4_scales_t s;
357
+ HVX_Vector v = hvx_vmemu(e8m0_8);
358
+ HVX_Vector vh = Q6_V_lo_W(Q6_Wuh_vunpack_Vub(v));
359
+ vh = Q6_Vh_vsub_VhVh(vh, Q6_Vh_vsplat_R(112));
360
+ vh = Q6_Vh_vmax_VhVh(vh, Q6_V_vzero());
361
+ vh = Q6_Vh_vmin_VhVh(vh, Q6_Vh_vsplat_R(30));
362
+ vh = Q6_Vh_vasl_VhR(vh, 10);
363
+ hvx_vec_store_u(s.v, 16, vh);
364
+ return s;
365
+ }
366
+
367
+ static inline HVX_Vector mxfp4_extract_splat(mxfp4_scales_t scales, int idx) {
368
+ return hvx_vec_splat_f16(scales.v[idx]);
369
+ }
370
+
371
+ // Dequantize one x4x2 MXFP4 group (32 elements from 32 packed bytes) -> 32 FP16.
372
+ static inline HVX_Vector dequantize_x4x2_mxfp4_group_hvx(const uint8_t * packed_32,
373
+ bool upper_nibbles,
374
+ int sub_blk,
375
+ const HVX_Vector vlut_cvt,
376
+ mxfp4_scales_t scales) {
377
+ HVX_Vector vq = hvx_vmemu(packed_32);
378
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
379
+ HVX_Vector v_quants = upper_nibbles ? Q6_Vub_vlsr_VubR(vq, 4) : vq;
380
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
381
+
382
+ HVX_Vector v_sc = mxfp4_extract_splat(scales, sub_blk);
383
+
384
+ v_quants = Q6_Vb_vshuff_Vb(v_quants);
385
+ HVX_VectorPair vp = Q6_Wh_vlut16_VbVhR(v_quants, vlut_cvt, 0);
386
+ HVX_Vector v_hf = Q6_V_lo_W(vp);
387
+
388
+ return Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_hf, v_sc));
389
+ }
390
+
391
+ // Batch-dequantize 4 contiguous x4x2 MXFP4 groups (4x32 = 128 packed bytes).
392
+ static inline HVX_Vector_x4 dequantize_x4x2_mxfp4_x4groups_hvx(const uint8_t * packed_128,
393
+ bool upper_nibbles,
394
+ int sub_blk_base,
395
+ const HVX_Vector vlut_cvt,
396
+ mxfp4_scales_t scales) {
397
+ HVX_Vector vq = hvx_vmemu(packed_128);
398
+ const HVX_Vector mask_h4 = Q6_Vb_vsplat_R(0x0F);
399
+ HVX_Vector v_quants = upper_nibbles ? Q6_Vub_vlsr_VubR(vq, 4) : vq;
400
+ v_quants = Q6_V_vand_VV(v_quants, mask_h4);
401
+
402
+ v_quants = Q6_Vb_vshuff_Vb(v_quants);
403
+
404
+ HVX_VectorPair vp = Q6_Wh_vlut16_VbVhR(v_quants, vlut_cvt, 0);
405
+ HVX_Vector v_lo = Q6_V_lo_W(vp);
406
+ HVX_Vector v_hi = Q6_V_hi_W(vp);
407
+
408
+ HVX_VectorPred q64 = Q6_Q_vsetq_R(64);
409
+ HVX_Vector v_sc01 = Q6_V_vmux_QVV(q64, mxfp4_extract_splat(scales, sub_blk_base + 0),
410
+ mxfp4_extract_splat(scales, sub_blk_base + 1));
411
+ HVX_Vector v_sc23 = Q6_V_vmux_QVV(q64, mxfp4_extract_splat(scales, sub_blk_base + 2),
412
+ mxfp4_extract_splat(scales, sub_blk_base + 3));
413
+
414
+ v_lo = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_lo, v_sc01));
415
+ v_hi = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(v_hi, v_sc23));
416
+
417
+ HVX_Vector_x4 r = { v_lo, Q6_V_vror_VR(v_lo, 64), v_hi, Q6_V_vror_VR(v_hi, 64) };
418
+ return r;
419
+ }
420
+
421
+ typedef struct {
422
+ __fp16 *dst;
423
+ const uint8_t *src;
424
+ int n_cols;
425
+ int k_block;
426
+ size_t row_stride;
427
+ int weight_type;
428
+ int n_tot_tiles;
429
+ int n_tiles_per_task;
430
+ int n_tasks;
431
+ int n_k_tiles;
432
+ struct fastdiv_values n_k_tiles_div;
433
+ } x4x2_dequantize_state_t;
434
+
435
+ // Dequantize a tile range from x4x2 weight data (already in VTCM) to tile-major FP16.
436
+ // Input: vtcm_src has n_cols rows of x4x2 data, each row_stride bytes.
437
+ // Output: vtcm_dst in tile-major FP16 layout.
438
+
439
+ #define DEFINE_DEQUANTIZE_Q4_TASK(suffix, lut_name, helper_prefix, dblk_size, scale_step) \
440
+ static void dequantize_x4x2_weight_to_fp16_tiles_task_##suffix( \
441
+ const x4x2_dequantize_state_t *state, \
442
+ int start_tile, int end_tile) { \
443
+ \
444
+ const int n_k_tiles = state->n_k_tiles; \
445
+ const int qrow_size = (unsigned)state->k_block / 2; \
446
+ const struct fastdiv_values n_k_tiles_div = state->n_k_tiles_div; \
447
+ const HVX_Vector vlut_cvt = hvx_vmem(lut_name); \
448
+ \
449
+ const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets); \
450
+ const HVX_Vector v_scat_step = Q6_V_vsplat_R(4); \
451
+ const HVX_VectorPred q_mask64 = Q6_Q_vsetq_R(64); \
452
+ \
453
+ unsigned ct = fastdiv((unsigned)start_tile, &n_k_tiles_div); \
454
+ unsigned kt = fastmodulo((unsigned)start_tile, n_k_tiles, &n_k_tiles_div); \
455
+ \
456
+ for (unsigned t = start_tile; t < (unsigned)end_tile; ) { \
457
+ if (kt >= (unsigned)n_k_tiles) { kt = 0; ct++; } \
458
+ \
459
+ if ((kt % 4 == 0) && (t + 4 <= (unsigned)end_tile) && (fastdiv(t + 3, &n_k_tiles_div) == ct)) { \
460
+ unsigned blk_idx = ((kt * 32) / QK_Q4_0x4x2); \
461
+ unsigned sub_blk_base = ((kt * 32) % QK_Q4_0x4x2) / 32; \
462
+ bool upper = (sub_blk_base >= 4); \
463
+ unsigned packed_off = blk_idx * (QK_Q4_0x4x2 / 2); \
464
+ unsigned scale_off = qrow_size + blk_idx * (dblk_size) + sub_blk_base * (scale_step); \
465
+ \
466
+ __fp16 *tile_bases[4]; \
467
+ for (unsigned g = 0; g < 4; g++) { \
468
+ tile_bases[g] = state->dst + (t + g) * HMX_FP16_TILE_N_ELMS; \
469
+ } \
470
+ \
471
+ HVX_Vector v_off = v_scat_base; \
472
+ unsigned row_offset = ct * HMX_FP16_TILE_N_COLS * state->row_stride; \
473
+ \
474
+ for (int r = 0; r < HMX_FP16_TILE_N_ROWS; r += 2) { \
475
+ const uint8_t *r0 = state->src + row_offset; row_offset += state->row_stride; \
476
+ const uint8_t *r1 = state->src + row_offset; row_offset += state->row_stride; \
477
+ \
478
+ HVX_Vector_x2 dv0 = dequantize_x4x2_##helper_prefix##_x4groups_hvx( \
479
+ r0 + packed_off, upper, (const __fp16 *)(r0 + scale_off), vlut_cvt); \
480
+ Q6_vscatter_RMVwV((size_t)tile_bases[0], 2 * HMX_FP16_TILE_SIZE - 1, v_off, dv0.v[0]); \
481
+ Q6_vscatter_RMVwV((size_t)tile_bases[2], 2 * HMX_FP16_TILE_SIZE - 1, v_off, dv0.v[1]); \
482
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step); \
483
+ \
484
+ HVX_Vector_x2 dv1 = dequantize_x4x2_##helper_prefix##_x4groups_hvx( \
485
+ r1 + packed_off, upper, (const __fp16 *)(r1 + scale_off), vlut_cvt); \
486
+ Q6_vscatter_RMVwV((size_t)tile_bases[0], 2 * HMX_FP16_TILE_SIZE - 1, v_off, dv1.v[0]); \
487
+ Q6_vscatter_RMVwV((size_t)tile_bases[2], 2 * HMX_FP16_TILE_SIZE - 1, v_off, dv1.v[1]); \
488
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step); \
489
+ } \
490
+ \
491
+ for (int g = 0; g < 4; g++) { (void) *(volatile HVX_Vector *)(tile_bases[g]); } \
492
+ t += 4; kt += 4; \
493
+ continue; \
494
+ } \
495
+ \
496
+ __fp16 *tile_base = state->dst + t * HMX_FP16_TILE_N_ELMS; \
497
+ { \
498
+ unsigned blk_idx = (kt * 32) / QK_Q4_0x4x2; \
499
+ unsigned sub_blk = ((kt * 32) % QK_Q4_0x4x2) / 32; \
500
+ bool upper = (sub_blk >= 4); \
501
+ unsigned byte_off = blk_idx * (QK_Q4_0x4x2 / 2) + (upper ? (sub_blk - 4) : sub_blk) * 32; \
502
+ unsigned scale_off = qrow_size + blk_idx * (dblk_size) + sub_blk * (scale_step); \
503
+ \
504
+ HVX_Vector v_off = v_scat_base; \
505
+ unsigned row_offset = ct * HMX_FP16_TILE_N_COLS * state->row_stride; \
506
+ unsigned row1 = ct * HMX_FP16_TILE_N_COLS + 1; \
507
+ \
508
+ for (int r = 0; r < HMX_FP16_TILE_N_ROWS; r += 2, row1 += 2) { \
509
+ const uint8_t *r0 = state->src + row_offset; row_offset += state->row_stride; \
510
+ const uint8_t *r1 = state->src + row_offset; row_offset += state->row_stride; \
511
+ \
512
+ HVX_Vector v0 = dequantize_x4x2_##helper_prefix##_group_hvx( \
513
+ r0 + byte_off, upper, (const __fp16 *)(r0 + scale_off), vlut_cvt); \
514
+ HVX_Vector v1 = (row1 < (unsigned)state->n_cols) \
515
+ ? dequantize_x4x2_##helper_prefix##_group_hvx( \
516
+ r1 + byte_off, upper, (const __fp16 *)(r1 + scale_off), vlut_cvt) \
517
+ : Q6_V_vzero(); \
518
+ \
519
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v0); \
520
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step); \
521
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v1); \
522
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step); \
523
+ } \
524
+ (void) *(volatile HVX_Vector *)(tile_base); \
525
+ } \
526
+ ++t; ++kt; \
527
+ } \
528
+ \
529
+ if (start_tile < end_tile) { \
530
+ (void) *(volatile HVX_Vector *)(state->dst + (end_tile - 1) * HMX_FP16_TILE_N_ELMS); \
531
+ } \
532
+ } \
533
+ \
534
+ static void dequantize_x4x2_worker_loop_##suffix(unsigned int n, unsigned int i, void *data) { \
535
+ x4x2_dequantize_state_t *state = (x4x2_dequantize_state_t *)data; \
536
+ for (unsigned int task_id = i; task_id < (unsigned int)state->n_tasks; task_id += n) { \
537
+ int start = task_id * state->n_tiles_per_task; \
538
+ int end = hex_smin(start + state->n_tiles_per_task, state->n_tot_tiles); \
539
+ dequantize_x4x2_weight_to_fp16_tiles_task_##suffix(state, start, end); \
540
+ } \
541
+ }
542
+
543
+ DEFINE_DEQUANTIZE_Q4_TASK(q4_0, q4_0_to_fp16_lut, q4_0, HMX_X4X2_DBLK_SIZE, (int)sizeof(__fp16))
544
+ DEFINE_DEQUANTIZE_Q4_TASK(q4_1, q4_1_to_fp16_lut, q4_1, 32, 4)
545
+ DEFINE_DEQUANTIZE_Q4_TASK(iq4_nl, iq4_nl_to_fp16_lut, iq4_nl, HMX_X4X2_DBLK_SIZE, (int)sizeof(__fp16))
546
+
547
+ static void dequantize_x4x2_weight_to_fp16_tiles_task_mxfp4(
548
+ const x4x2_dequantize_state_t *state,
549
+ int start_tile, int end_tile) {
550
+
551
+ const int n_k_tiles = state->n_k_tiles;
552
+ const int qrow_size = (unsigned)state->k_block / 2;
553
+ const struct fastdiv_values n_k_tiles_div = state->n_k_tiles_div;
554
+ const HVX_Vector vlut_cvt = hvx_vmem(mxfp4_to_fp16_lut);
555
+
556
+ const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets);
557
+ const HVX_Vector v_scat_step = Q6_V_vsplat_R(4);
558
+ const HVX_VectorPred q_mask64 = Q6_Q_vsetq_R(64);
559
+
560
+ unsigned ct = fastdiv((unsigned)start_tile, &n_k_tiles_div);
561
+ unsigned kt = fastmodulo((unsigned)start_tile, n_k_tiles, &n_k_tiles_div);
562
+
563
+ for (unsigned t = start_tile; t < (unsigned)end_tile; ) {
564
+ if (kt >= (unsigned)n_k_tiles) { kt = 0; ct++; }
565
+
566
+ // Batch-4 fast path for MXFP4
567
+ if ((kt % 4 == 0) && (t + 4 <= (unsigned)end_tile) && (fastdiv(t + 3, &n_k_tiles_div) == ct)) {
568
+ int blk_idx = (kt * 32) / QK_MXFP4x4x2;
569
+ int sub_blk_base = ((kt * 32) % QK_MXFP4x4x2) / 32;
570
+ bool upper = (sub_blk_base >= 4);
571
+ int packed_off = blk_idx * (QK_MXFP4x4x2 / 2);
572
+ int e8m0_blk_off = qrow_size + blk_idx * HMX_X4X2_MXFP4_EBLK_SIZE;
573
+
574
+ __fp16 * tile_bases[4];
575
+ for (int g = 0; g < 4; g++) {
576
+ tile_bases[g] = state->dst + (t + g) * HMX_FP16_TILE_N_ELMS;
577
+ }
578
+
579
+ HVX_Vector v_off = v_scat_base;
580
+ for (int r = 0; r < HMX_FP16_TILE_N_ROWS; r += 2) {
581
+ int row0 = ct * HMX_FP16_TILE_N_COLS + r;
582
+ int row1 = row0 + 1;
583
+ const uint8_t * r0 = state->src + row0 * state->row_stride;
584
+ const uint8_t * r1 = state->src + row1 * state->row_stride;
585
+
586
+ mxfp4_scales_t r0_e8 = mxfp4_convert_scales(r0 + e8m0_blk_off);
587
+
588
+ HVX_Vector_x4 dv0, dv1;
589
+ dv0 = dequantize_x4x2_mxfp4_x4groups_hvx(r0 + packed_off, upper, sub_blk_base, vlut_cvt, r0_e8);
590
+ if (row1 < state->n_cols) {
591
+ mxfp4_scales_t r1_e8 = mxfp4_convert_scales(r1 + e8m0_blk_off);
592
+ dv1 = dequantize_x4x2_mxfp4_x4groups_hvx(r1 + packed_off, upper, sub_blk_base, vlut_cvt, r1_e8);
593
+ } else {
594
+ dv1.v[0] = dv1.v[1] = dv1.v[2] = dv1.v[3] = Q6_V_vzero();
595
+ }
596
+
597
+ for (int g = 0; g < 4; g++) {
598
+ Q6_vscatter_QRMVwV(q_mask64, (size_t) tile_bases[g], HMX_FP16_TILE_SIZE - 1, v_off, dv0.v[g]);
599
+ }
600
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
601
+ for (int g = 0; g < 4; g++) {
602
+ Q6_vscatter_QRMVwV(q_mask64, (size_t) tile_bases[g], HMX_FP16_TILE_SIZE - 1, v_off, dv1.v[g]);
603
+ }
604
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
605
+ }
606
+
607
+ for (int g = 0; g < 4; g++) {
608
+ (void) *(volatile HVX_Vector *) (tile_bases[g]);
609
+ }
610
+
611
+ t += 4; kt += 4;
612
+ continue;
613
+ }
614
+
615
+ // Single-tile fallback
616
+ __fp16 *tile_base = state->dst + t * HMX_FP16_TILE_N_ELMS;
617
+ {
618
+ int blk_idx = (kt * 32) / QK_MXFP4x4x2;
619
+ int sub_blk = ((kt * 32) % QK_MXFP4x4x2) / 32;
620
+ bool upper = (sub_blk >= 4);
621
+ int byte_off = blk_idx * (QK_MXFP4x4x2 / 2) + (upper ? (sub_blk - 4) : sub_blk) * 32;
622
+ int e8m0_blk_off = qrow_size + blk_idx * HMX_X4X2_MXFP4_EBLK_SIZE;
623
+
624
+ HVX_Vector v_off = v_scat_base;
625
+ for (int r = 0; r < HMX_FP16_TILE_N_ROWS; r += 2) {
626
+ int row0 = ct * HMX_FP16_TILE_N_COLS + r;
627
+ int row1 = row0 + 1;
628
+
629
+ const uint8_t * r0 = state->src + row0 * state->row_stride;
630
+ const uint8_t * r1 = state->src + row1 * state->row_stride;
631
+
632
+ mxfp4_scales_t r0_e8 = mxfp4_convert_scales(r0 + e8m0_blk_off);
633
+
634
+ HVX_Vector v0 = dequantize_x4x2_mxfp4_group_hvx(r0 + byte_off, upper, sub_blk, vlut_cvt, r0_e8);
635
+ HVX_Vector v1;
636
+ if (row1 < state->n_cols) {
637
+ mxfp4_scales_t r1_e8 = mxfp4_convert_scales(r1 + e8m0_blk_off);
638
+ v1 = dequantize_x4x2_mxfp4_group_hvx(r1 + byte_off, upper, sub_blk, vlut_cvt, r1_e8);
639
+ } else {
640
+ v1 = Q6_V_vzero();
641
+ }
642
+
643
+ Q6_vscatter_QRMVwV(q_mask64, (size_t) tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v0);
644
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
645
+ Q6_vscatter_QRMVwV(q_mask64, (size_t) tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v1);
646
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
647
+ }
648
+ (void) *(volatile HVX_Vector *) (tile_base);
649
+ }
650
+ ++t; ++kt;
651
+ }
652
+
653
+ if (start_tile < end_tile) {
654
+ (void) *(volatile HVX_Vector *)(state->dst + (end_tile - 1) * HMX_FP16_TILE_N_ELMS);
655
+ }
656
+ }
657
+
658
+ static void dequantize_x4x2_worker_loop_mxfp4(unsigned int n, unsigned int i, void *data) {
659
+ x4x2_dequantize_state_t *state = (x4x2_dequantize_state_t *)data;
660
+ for (unsigned int task_id = i; task_id < (unsigned int)state->n_tasks; task_id += n) {
661
+ int start = task_id * state->n_tiles_per_task;
662
+ int end = hex_smin(start + state->n_tiles_per_task, state->n_tot_tiles);
663
+ dequantize_x4x2_weight_to_fp16_tiles_task_mxfp4(state, start, end);
664
+ }
665
+ }
666
+
667
+ static void dequantize_x4x2_weight_to_fp16_tiles_task_q8_0(
668
+ const x4x2_dequantize_state_t *state,
669
+ int start_tile, int end_tile) {
670
+
671
+ const int n_k_tiles = state->n_k_tiles;
672
+ const int qrow_size = state->k_block;
673
+ const struct fastdiv_values n_k_tiles_div = state->n_k_tiles_div;
674
+
675
+ const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets);
676
+ const HVX_Vector v_scat_step = Q6_V_vsplat_R(4);
677
+ const HVX_VectorPred q_mask64 = Q6_Q_vsetq_R(64);
678
+
679
+ unsigned ct = fastdiv((unsigned)start_tile, &n_k_tiles_div);
680
+ unsigned kt = fastmodulo((unsigned)start_tile, n_k_tiles, &n_k_tiles_div);
681
+
682
+ for (unsigned t = start_tile; t < (unsigned)end_tile; ) {
683
+ if (kt >= (unsigned)n_k_tiles) { kt = 0; ct++; }
684
+
685
+ __fp16 *tile_base = state->dst + t * HMX_FP16_TILE_N_ELMS;
686
+ {
687
+ int blk_idx = (kt * 32) / QK_Q8_0x4x2;
688
+ int sub_blk = ((kt * 32) % QK_Q8_0x4x2) / 32;
689
+ int byte_off = blk_idx * QK_Q8_0x4x2 + sub_blk * 32;
690
+ int scale_off = qrow_size + blk_idx * HMX_X4X2_DBLK_SIZE + sub_blk * (int)sizeof(__fp16);
691
+
692
+ HVX_Vector v_off = v_scat_base;
693
+ for (int r = 0; r < HMX_FP16_TILE_N_ROWS; r += 2) {
694
+ int row0 = ct * HMX_FP16_TILE_N_COLS + r;
695
+ int row1 = row0 + 1;
696
+
697
+ const uint8_t *r0 = state->src + row0 * state->row_stride;
698
+ const uint8_t *r1 = state->src + row1 * state->row_stride;
699
+
700
+ HVX_Vector v0 = dequantize_x4x2_q8_0_group_hvx((const int8_t *)(r0 + byte_off), (const __fp16 *)(r0 + scale_off));
701
+ HVX_Vector v1 = (row1 < state->n_cols) ? dequantize_x4x2_q8_0_group_hvx((const int8_t *)(r1 + byte_off), (const __fp16 *)(r1 + scale_off)) : Q6_V_vzero();
702
+
703
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v0);
704
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
705
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v1);
706
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
707
+ }
708
+ (void) *(volatile HVX_Vector *)(tile_base);
709
+ }
710
+ ++t; ++kt;
711
+ }
712
+
713
+ if (start_tile < end_tile) {
714
+ (void) *(volatile HVX_Vector *)(state->dst + (end_tile - 1) * HMX_FP16_TILE_N_ELMS);
715
+ }
716
+ }
717
+
718
+ static void dequantize_x4x2_worker_loop_q8_0(unsigned int n, unsigned int i, void *data) {
719
+ x4x2_dequantize_state_t *state = (x4x2_dequantize_state_t *)data;
720
+ for (unsigned int task_id = i; task_id < (unsigned int)state->n_tasks; task_id += n) {
721
+ int start = task_id * state->n_tiles_per_task;
722
+ int end = hex_smin(start + state->n_tiles_per_task, state->n_tot_tiles);
723
+ dequantize_x4x2_weight_to_fp16_tiles_task_q8_0(state, start, end);
724
+ }
725
+ }
726
+
727
+ static void convert_f16_weight_to_fp16_tiles_task(
728
+ const x4x2_dequantize_state_t *state,
729
+ int start_tile, int end_tile) {
730
+
731
+ const int n_k_tiles = state->n_k_tiles;
732
+ const struct fastdiv_values n_k_tiles_div = state->n_k_tiles_div;
733
+
734
+ const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets);
735
+ const HVX_Vector v_scat_step = Q6_V_vsplat_R(4);
736
+ const HVX_VectorPred q_mask64 = Q6_Q_vsetq_R(64);
737
+
738
+ unsigned ct = fastdiv((unsigned)start_tile, &n_k_tiles_div);
739
+ unsigned kt = fastmodulo((unsigned)start_tile, n_k_tiles, &n_k_tiles_div);
740
+
741
+ for (unsigned t = start_tile; t < (unsigned)end_tile; ) {
742
+ if (kt >= (unsigned)n_k_tiles) { kt = 0; ct++; }
743
+
744
+ __fp16 *tile_base = state->dst + t * HMX_FP16_TILE_N_ELMS;
745
+ {
746
+ int byte_off = kt * 32 * sizeof(__fp16);
747
+
748
+ HVX_Vector v_off = v_scat_base;
749
+ for (int r = 0; r < HMX_FP16_TILE_N_ROWS; r += 2) {
750
+ int row0 = ct * HMX_FP16_TILE_N_COLS + r;
751
+ int row1 = row0 + 1;
752
+
753
+ const uint8_t *r0 = state->src + row0 * state->row_stride;
754
+ const uint8_t *r1 = state->src + row1 * state->row_stride;
755
+
756
+ HVX_Vector v0 = hvx_vmemu((const __fp16 *)(r0 + byte_off));
757
+ HVX_Vector v1 = (row1 < state->n_cols) ? hvx_vmemu((const __fp16 *)(r1 + byte_off)) : Q6_V_vzero();
758
+
759
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v0);
760
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
761
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v1);
762
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
763
+ }
764
+ (void) *(volatile HVX_Vector *)(tile_base);
765
+ }
766
+ ++t; ++kt;
767
+ }
768
+
769
+ if (start_tile < end_tile) {
770
+ (void) *(volatile HVX_Vector *)(state->dst + (end_tile - 1) * HMX_FP16_TILE_N_ELMS);
771
+ }
772
+ }
773
+
774
+ static void convert_f16_worker_loop(unsigned int n, unsigned int i, void *data) {
775
+ x4x2_dequantize_state_t *state = (x4x2_dequantize_state_t *)data;
776
+ for (unsigned int task_id = i; task_id < (unsigned int)state->n_tasks; task_id += n) {
777
+ int start = task_id * state->n_tiles_per_task;
778
+ int end = hex_smin(start + state->n_tiles_per_task, state->n_tot_tiles);
779
+ convert_f16_weight_to_fp16_tiles_task(state, start, end);
780
+ }
781
+ }
782
+
783
+ static void quantize_f32_weight_to_fp16_tiles_task(
784
+ const x4x2_dequantize_state_t *state,
785
+ int start_tile, int end_tile) {
786
+
787
+ const int n_k_tiles = state->n_k_tiles;
788
+ const struct fastdiv_values n_k_tiles_div = state->n_k_tiles_div;
789
+
790
+ const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets);
791
+ const HVX_Vector v_scat_step = Q6_V_vsplat_R(4);
792
+ const HVX_VectorPred q_mask64 = Q6_Q_vsetq_R(64);
793
+
794
+ unsigned ct = fastdiv((unsigned)start_tile, &n_k_tiles_div);
795
+ unsigned kt = fastmodulo((unsigned)start_tile, n_k_tiles, &n_k_tiles_div);
796
+
797
+ for (unsigned t = start_tile; t < (unsigned)end_tile; ) {
798
+ if (kt >= (unsigned)n_k_tiles) { kt = 0; ct++; }
799
+
800
+ __fp16 *tile_base = state->dst + t * HMX_FP16_TILE_N_ELMS;
801
+ {
802
+ int byte_off = kt * 32 * sizeof(float);
803
+
804
+ HVX_Vector v_off = v_scat_base;
805
+ for (int r = 0; r < HMX_FP16_TILE_N_ROWS; r += 2) {
806
+ int row0 = ct * HMX_FP16_TILE_N_COLS + r;
807
+ int row1 = row0 + 1;
808
+
809
+ const uint8_t *r0 = state->src + row0 * state->row_stride;
810
+ const uint8_t *r1 = state->src + row1 * state->row_stride;
811
+
812
+ HVX_Vector v0_f32 = hvx_vmemu((const float *)(r0 + byte_off));
813
+ HVX_Vector v1_f32 = (row1 < state->n_cols) ? hvx_vmemu((const float *)(r1 + byte_off)) : Q6_V_vzero();
814
+
815
+ HVX_Vector v_out = hvx_vec_f32_to_f16(v0_f32, v1_f32);
816
+
817
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v_out);
818
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
819
+
820
+ HVX_Vector v_out_hi = Q6_V_vror_VR(v_out, 64);
821
+ Q6_vscatter_QRMVwV(q_mask64, (size_t)tile_base, HMX_FP16_TILE_SIZE - 1, v_off, v_out_hi);
822
+ v_off = Q6_Vw_vadd_VwVw(v_off, v_scat_step);
823
+ }
824
+ (void) *(volatile HVX_Vector *)(tile_base);
825
+ }
826
+ ++t; ++kt;
827
+ }
828
+
829
+ if (start_tile < end_tile) {
830
+ (void) *(volatile HVX_Vector *)(state->dst + (end_tile - 1) * HMX_FP16_TILE_N_ELMS);
831
+ }
832
+ }
833
+
834
+ static void quantize_f32_worker_loop(unsigned int n, unsigned int i, void *data) {
835
+ x4x2_dequantize_state_t *state = (x4x2_dequantize_state_t *)data;
836
+ for (unsigned int task_id = i; task_id < (unsigned int)state->n_tasks; task_id += n) {
837
+ int start = task_id * state->n_tiles_per_task;
838
+ int end = hex_smin(start + state->n_tiles_per_task, state->n_tot_tiles);
839
+ quantize_f32_weight_to_fp16_tiles_task(state, start, end);
840
+ }
841
+ }
842
+
843
+
844
+ static void dequantize_x4x2_weight_chunk_to_fp16_tiles(
845
+ struct htp_context *ctx, __fp16 *vtcm_dst,
846
+ const void *vtcm_src, int n_cols, int k_block,
847
+ size_t row_stride, int weight_type,
848
+ int n_k_tiles, struct fastdiv_values n_k_tiles_div,
849
+ worker_callback_t dequant_worker_fn, int n_threads) {
850
+
851
+ assert(n_cols % HMX_FP16_TILE_N_COLS == 0);
852
+ assert(k_block % HMX_FP16_TILE_N_COLS == 0);
853
+
854
+ size_t n_col_tiles = n_cols / HMX_FP16_TILE_N_COLS;
855
+ size_t n_tot_tiles = n_col_tiles * n_k_tiles;
856
+
857
+ size_t n_tiles_per_task = (n_threads == 1) ? n_tot_tiles : hmx_ceil_div(n_tot_tiles, n_threads);
858
+
859
+ x4x2_dequantize_state_t state;
860
+ state.n_tasks = (n_tot_tiles + n_tiles_per_task - 1) / n_tiles_per_task;
861
+ state.n_tot_tiles = n_tot_tiles;
862
+ state.n_tiles_per_task = n_tiles_per_task;
863
+ state.dst = vtcm_dst;
864
+ state.src = (const uint8_t *)vtcm_src;
865
+ state.n_cols = n_cols;
866
+ state.k_block = k_block;
867
+ state.row_stride = row_stride;
868
+ state.weight_type = weight_type;
869
+ state.n_k_tiles = n_k_tiles;
870
+ state.n_k_tiles_div = n_k_tiles_div;
871
+
872
+ if (state.n_tasks == 1 || n_threads == 1) {
873
+ dequant_worker_fn(1, 0, &state);
874
+ } else {
875
+ worker_pool_run_func(ctx->worker_pool, dequant_worker_fn, &state, n_threads);
876
+ }
877
+ }
878
+
879
+ // --- End x4x2 dequantizers ---
880
+
881
+ #pragma clang diagnostic ignored "-Wbackend-plugin" // spurios warning for hmx intrinsics
882
+
883
+ // requires external HMX lock
884
+ static void core_dot_chunk_fp16(__fp16 *restrict output, const __fp16 *restrict activation, const __fp16 *restrict weight, const __fp16 *restrict scales,
885
+ int n_row_tiles, int n_col_tiles, int n_dot_tiles) {
886
+ __builtin_assume(n_row_tiles > 0);
887
+ __builtin_assume(n_col_tiles > 0);
888
+ __builtin_assume(n_dot_tiles > 0);
889
+
890
+ Q6_bias_mxmem2_A((void *)scales);
891
+ for (int r = 0; r < n_row_tiles; ++r) {
892
+ for (size_t c = 0; c < n_col_tiles; ++c) {
893
+ Q6_mxclracc_hf();
894
+
895
+ const __fp16 *row_tiles = activation + r * n_dot_tiles * HMX_FP16_TILE_N_ELMS;
896
+ const __fp16 *col_tiles = weight + c * n_dot_tiles * HMX_FP16_TILE_N_ELMS;
897
+
898
+ for (int k = 0, k_block; k < n_dot_tiles; k += k_block) {
899
+ k_block = hex_smin(n_dot_tiles - k, 32);
900
+ const uint32_t range = 2048u * (uint32_t)k_block - 1;
901
+ Q6_activation_hf_mxmem_RR_deep((unsigned int)row_tiles, range);
902
+ Q6_weight_hf_mxmem_RR((unsigned int)col_tiles, range);
903
+ row_tiles += k_block * HMX_FP16_TILE_N_ELMS;
904
+ col_tiles += k_block * HMX_FP16_TILE_N_ELMS;
905
+ }
906
+
907
+ __fp16 *out_tile = output + (r * n_col_tiles + c) * HMX_FP16_TILE_N_ELMS;
908
+ Q6_mxmem_AR_after_hf(out_tile, 0);
909
+ }
910
+ }
911
+ }
912
+
913
+ // --- Async HMX matmul job (for pipeline overlap) ---
914
+
915
+ typedef struct {
916
+ __fp16 * output;
917
+ const __fp16 * activation;
918
+ const __fp16 * weight;
919
+ const __fp16 * scales;
920
+ uint32_t n_row_tiles;
921
+ uint32_t n_col_tiles;
922
+ uint32_t n_dot_tiles;
923
+ } hmx_matmul_job_t;
924
+
925
+ static void hmx_matmul_worker_fn(void * data) {
926
+ hmx_matmul_job_t * job = (hmx_matmul_job_t *) data;
927
+ FARF(HIGH, "hmx-mm-job: n_row_tiles %u n_col_tiles %u n_dot_tiles %u", job->n_row_tiles, job->n_col_tiles, job->n_dot_tiles);
928
+ core_dot_chunk_fp16(job->output, job->activation, job->weight, job->scales, job->n_row_tiles, job->n_col_tiles, job->n_dot_tiles);
929
+ }
930
+
931
+ static inline void hmx_matmul_job_init(hmx_matmul_job_t * job,
932
+ __fp16 * output,
933
+ const __fp16 * activation,
934
+ const __fp16 * weight,
935
+ const __fp16 * scales,
936
+ int n_row_tiles,
937
+ int n_col_tiles,
938
+ int n_dot_tiles) {
939
+ job->output = output;
940
+ job->activation = activation;
941
+ job->weight = weight;
942
+ job->scales = scales;
943
+ job->n_row_tiles = n_row_tiles;
944
+ job->n_col_tiles = n_col_tiles;
945
+ job->n_dot_tiles = n_dot_tiles;
946
+ }
947
+
948
+ // output : fp16 -> f32p
949
+
950
+ static void transfer_output_chunk_fp16_to_fp32(float *restrict dst, const __fp16 *restrict vtcm_src, int n_rows, int n_cols, int n) {
951
+ assert(n_cols % HMX_FP16_TILE_N_COLS == 0);
952
+ const size_t tile_row_stride = (n_cols / HMX_FP16_TILE_N_COLS) * HMX_FP16_TILE_N_ELMS;
953
+
954
+ const HVX_Vector one = hvx_vec_splat_f16(1.0);
955
+
956
+ for (size_t r = 0; r < n_rows; r += 2) {
957
+ const size_t r0 = r / HMX_FP16_TILE_N_ROWS;
958
+ const size_t r1 = (r % HMX_FP16_TILE_N_ROWS) / 2; // index of the row pair within the tile
959
+ const __fp16 *row_base = vtcm_src + r0 * tile_row_stride;
960
+ float *output_row_base = dst + r * n; // global memory row base for row r (and r+1)
961
+
962
+ #pragma unroll(4)
963
+ for (size_t c = 0; c < n_cols; c += HMX_FP16_TILE_N_COLS) {
964
+ const size_t c0 = c / HMX_FP16_TILE_N_COLS;
965
+ const __fp16 *tile = row_base + c0 * HMX_FP16_TILE_N_ELMS;
966
+ HVX_Vector v = ((const HVX_Vector *) tile)[r1];
967
+ HVX_VectorPair vp = Q6_Wqf32_vmpy_VhfVhf(v, one);
968
+
969
+ volatile HVX_Vector *pv_out0 = (volatile HVX_Vector *) (output_row_base + c + 0);
970
+ volatile HVX_Vector *pv_out1 = (volatile HVX_Vector *) (output_row_base + c + n); // next row in global memory
971
+
972
+ *pv_out0 = Q6_Vsf_equals_Vqf32(Q6_V_lo_W(vp));
973
+ if (r + 1 < n_rows) {
974
+ *pv_out1 = Q6_Vsf_equals_Vqf32(Q6_V_hi_W(vp));
975
+ }
976
+ }
977
+ }
978
+ }
979
+
980
+ typedef struct {
981
+ const __fp16 *vtcm_src;
982
+ float *dst;
983
+ int n_tasks;
984
+ int n_tot_chunks;
985
+ int n_chunks_per_task;
986
+ int n_cols;
987
+ int n; // DDR row stride (total output columns)
988
+ } output_transfer_task_state_t;
989
+
990
+ static void transfer_output_chunk_worker_fn(unsigned int n, unsigned int i, void *data) {
991
+ output_transfer_task_state_t *st = (output_transfer_task_state_t *) data;
992
+
993
+ for (unsigned int task_id = i; task_id < (unsigned int)st->n_tasks; task_id += n) {
994
+ int chunk_idx = task_id * st->n_chunks_per_task;
995
+ size_t chunk_size = hex_smin(st->n_tot_chunks - chunk_idx, st->n_chunks_per_task);
996
+
997
+ float *dst = st->dst + chunk_idx * st->n;
998
+ const __fp16 *vtcm_src = st->vtcm_src + chunk_idx * st->n_cols;
999
+ transfer_output_chunk_fp16_to_fp32(dst, vtcm_src, chunk_size, st->n_cols, st->n);
1000
+ }
1001
+ }
1002
+
1003
+ static void transfer_output_chunk_threaded(struct htp_context *ctx, float *dst, const __fp16 *vtcm_src,
1004
+ int n_rows, int n_cols, int n, int n_threads) {
1005
+ assert(n_cols % HMX_FP16_TILE_N_COLS == 0);
1006
+
1007
+ size_t n_tot_chunks = n_rows;
1008
+ size_t n_chunks_per_task = (n_threads == 1) ? n_tot_chunks : HMX_FP16_TILE_N_ROWS; // must be multiple of HMX_FP16_TILE_N_ROWS (32)
1009
+
1010
+ output_transfer_task_state_t state;
1011
+ state.n_tasks = (n_tot_chunks + n_chunks_per_task - 1) / n_chunks_per_task;
1012
+ state.n_tot_chunks = n_tot_chunks;
1013
+ state.n_chunks_per_task = n_chunks_per_task;
1014
+ state.dst = dst;
1015
+ state.vtcm_src = vtcm_src;
1016
+ state.n_cols = n_cols;
1017
+ state.n = n;
1018
+
1019
+ if (state.n_tasks == 1 || n_threads == 1) {
1020
+ transfer_output_chunk_worker_fn(1, 0, &state);
1021
+ } else {
1022
+ worker_pool_run_func(ctx->worker_pool, transfer_output_chunk_worker_fn, &state, n_threads);
1023
+ }
1024
+ }
1025
+
1026
+ // activations : fp32 -> fp16
1027
+
1028
+ static void transfer_activation_chunk_fp32_to_fp16(__fp16 *restrict vtcm_dst, const float *restrict src, int n_rows, int k_block, int k_stride) {
1029
+ const int n_rows_padded = hex_align_up(n_rows, HMX_FP16_TILE_N_ROWS);
1030
+ const int n_rows_tiled = (n_rows / HMX_FP16_TILE_N_ROWS) * HMX_FP16_TILE_N_ROWS;
1031
+
1032
+ int r = 0;
1033
+
1034
+ #pragma unroll(2)
1035
+ for (r = 0; r < n_rows_tiled; r += 2) {
1036
+ int r0 = r / HMX_FP16_TILE_N_ROWS; // tile row index
1037
+ int r1 = r % HMX_FP16_TILE_N_ROWS; // intra-tile row idx
1038
+
1039
+ const HVX_Vector *pv_in0 = (const HVX_Vector *) (src + (r + 0) * k_stride);
1040
+ const HVX_Vector *pv_in1 = (const HVX_Vector *) (src + (r + 1) * k_stride);
1041
+ for (int c = 0; c < k_block; c += 32) {
1042
+ HVX_Vector v0 = *pv_in0++;
1043
+ HVX_Vector v1 = *pv_in1++;
1044
+
1045
+ HVX_Vector v_out = hvx_vec_f32_to_f16_shuff(v0, v1);
1046
+
1047
+ // compute output position
1048
+ int c0 = c / HMX_FP16_TILE_N_COLS; // tile column index
1049
+ int tile_idx = r0 * (k_block / HMX_FP16_TILE_N_COLS) + c0;
1050
+
1051
+ HVX_Vector *tile = (HVX_Vector *) (vtcm_dst + tile_idx * HMX_FP16_TILE_N_ELMS);
1052
+ tile[r1 / 2] = v_out;
1053
+ }
1054
+ }
1055
+
1056
+ for (; r < n_rows_padded; r += 2) {
1057
+ int r0 = r / HMX_FP16_TILE_N_ROWS; // tile row index
1058
+ int r1 = r % HMX_FP16_TILE_N_ROWS; // intra-tile row idx
1059
+
1060
+ const bool row0_valid = r < n_rows;
1061
+ const bool row1_valid = (r + 1) < n_rows;
1062
+
1063
+ const HVX_Vector *pv_in0 = row0_valid ? (const HVX_Vector *) (src + (r + 0) * k_stride) : NULL;
1064
+ const HVX_Vector *pv_in1 = row1_valid ? (const HVX_Vector *) (src + (r + 1) * k_stride) : NULL;
1065
+ for (int c = 0; c < k_block; c += 32) {
1066
+ HVX_Vector v0 = row0_valid ? *pv_in0++ : Q6_V_vzero();
1067
+ HVX_Vector v1 = row1_valid ? *pv_in1++ : Q6_V_vzero();
1068
+
1069
+ HVX_Vector v_out = hvx_vec_f32_to_f16_shuff(v0, v1);
1070
+
1071
+ // compute output position
1072
+ int c0 = c / HMX_FP16_TILE_N_COLS; // tile column index
1073
+ int tile_idx = r0 * (k_block / HMX_FP16_TILE_N_COLS) + c0;
1074
+
1075
+ HVX_Vector *tile = (HVX_Vector *) (vtcm_dst + tile_idx * HMX_FP16_TILE_N_ELMS);
1076
+ tile[r1 / 2] = v_out;
1077
+ }
1078
+ }
1079
+ }
1080
+
1081
+ typedef struct {
1082
+ __fp16 *dst;
1083
+ const float *src;
1084
+ int n_tasks;
1085
+ int n_tot_chunks;
1086
+ int n_chunks_per_task;
1087
+ int k_block;
1088
+ int k_stride;
1089
+ } activation_transfer_task_state_t;
1090
+
1091
+ static void transfer_activation_chunk_worker_fn(unsigned int n, unsigned int i, void *data) {
1092
+ activation_transfer_task_state_t *st = (activation_transfer_task_state_t *) data;
1093
+
1094
+ for (unsigned int task_id = i; task_id < (unsigned int)st->n_tasks; task_id += n) {
1095
+ // one chunk: one row
1096
+ int chunk_idx = task_id * st->n_chunks_per_task;
1097
+ size_t chunk_size = hex_smin(st->n_tot_chunks - chunk_idx, st->n_chunks_per_task);
1098
+
1099
+ __fp16 *dst = st->dst + chunk_idx * st->k_block;
1100
+ const float *src = st->src + chunk_idx * st->k_stride;
1101
+ transfer_activation_chunk_fp32_to_fp16(dst, src, chunk_size, st->k_block, st->k_stride);
1102
+ }
1103
+ }
1104
+
1105
+ static void transfer_activation_chunk_threaded(struct htp_context *ctx, __fp16 *dst, const float *src, int n_rows, int k_block, int k_stride, int n_threads) {
1106
+ assert(k_block % HMX_FP16_TILE_N_COLS == 0 && k_stride % HMX_FP16_TILE_N_COLS == 0);
1107
+ assert(VLEN == 32 * sizeof(float));
1108
+
1109
+ size_t n_tot_chunks = n_rows;
1110
+ size_t n_chunks_per_task = (n_threads == 1) ? n_tot_chunks : 32; // must be multiple of 32 to ensure correct destination address
1111
+
1112
+ activation_transfer_task_state_t state;
1113
+ state.n_tasks = (n_tot_chunks + n_chunks_per_task - 1) / n_chunks_per_task;
1114
+ state.n_tot_chunks = n_tot_chunks;
1115
+ state.n_chunks_per_task = n_chunks_per_task;
1116
+ state.dst = dst;
1117
+ state.src = src;
1118
+ state.k_block = k_block;
1119
+ state.k_stride = k_stride;
1120
+
1121
+ if (state.n_tasks == 1 || n_threads == 1) {
1122
+ transfer_activation_chunk_worker_fn(1, 0, &state);
1123
+ } else {
1124
+ worker_pool_run_func(ctx->worker_pool, transfer_activation_chunk_worker_fn, &state, n_threads);
1125
+ }
1126
+ }
1127
+
1128
+ // C += AB
1129
+ static void core_mma_chunk_fp16(__fp16 *restrict c, const __fp16 *restrict a, const __fp16 *restrict b,
1130
+ const __fp16 *restrict col_scales, const __fp16 *restrict eye_tile,
1131
+ int n_row_tiles, int n_col_tiles, int n_dot_tiles, bool zero_init) {
1132
+ __builtin_assume(n_row_tiles > 0);
1133
+ __builtin_assume(n_col_tiles > 0);
1134
+ __builtin_assume(n_dot_tiles > 0);
1135
+
1136
+ Q6_bias_mxmem2_A((void *)col_scales);
1137
+
1138
+ const size_t dot_tile_stride = n_dot_tiles * HMX_FP16_TILE_N_ELMS;
1139
+ for (size_t i = 0; i < n_row_tiles; ++i) {
1140
+ const __fp16 *row_base = a + i * dot_tile_stride;
1141
+ __fp16 *res_base = c + i * n_col_tiles * HMX_FP16_TILE_N_ELMS;
1142
+ for (size_t j = 0; j < n_col_tiles; ++j) {
1143
+ Q6_mxclracc_hf();
1144
+
1145
+ const __fp16 *col_tiles = b + j * dot_tile_stride;
1146
+ const __fp16 *row_tiles = row_base;
1147
+ __fp16 *accum_tile = res_base + j * HMX_FP16_TILE_N_ELMS;
1148
+ if (!zero_init) {
1149
+ Q6_activation_hf_mxmem_RR((unsigned int)accum_tile, 2047);
1150
+ Q6_weight_hf_mxmem_RR((unsigned int)eye_tile, 2047);
1151
+ }
1152
+
1153
+ for (int k = 0, k_block; k < n_dot_tiles; k += k_block) {
1154
+ k_block = hex_smin(n_dot_tiles - k, 32);
1155
+ const uint32_t range = 2048u * (uint32_t)k_block - 1;
1156
+ Q6_activation_hf_mxmem_RR_deep((unsigned int)row_tiles, range);
1157
+ Q6_weight_hf_mxmem_RR((unsigned int)col_tiles, range);
1158
+ row_tiles += k_block * HMX_FP16_TILE_N_ELMS;
1159
+ col_tiles += k_block * HMX_FP16_TILE_N_ELMS;
1160
+ }
1161
+
1162
+ Q6_mxmem_AR_after_hf(accum_tile, 0);
1163
+ }
1164
+ }
1165
+ }
1166
+
1167
+ int hmx_matmul_2d_f32(struct htp_context *ctx, float *restrict dst, const float *restrict activation,
1168
+ const uint8_t *restrict permuted_weight, int m, int k, int n,
1169
+ int act_stride, int weight_stride, int weight_type) {
1170
+ if (k % 32 != 0 || n % 32 != 0) { return -1; }
1171
+
1172
+ if (!hex_is_aligned(dst, VLEN) || !hex_is_aligned(activation, VLEN) || !hex_is_aligned(permuted_weight, VLEN)) {
1173
+ return -1;
1174
+ }
1175
+
1176
+ size_t row_stride = get_x4x2_row_stride(weight_type, k);
1177
+ if (row_stride == 0) {
1178
+ return -1;
1179
+ }
1180
+
1181
+ worker_callback_t dequant_worker_fn = NULL;
1182
+ switch (weight_type) {
1183
+ case HTP_TYPE_Q4_0: dequant_worker_fn = dequantize_x4x2_worker_loop_q4_0; break;
1184
+ case HTP_TYPE_IQ4_NL: dequant_worker_fn = dequantize_x4x2_worker_loop_iq4_nl; break;
1185
+ case HTP_TYPE_Q4_1: dequant_worker_fn = dequantize_x4x2_worker_loop_q4_1; break;
1186
+ case HTP_TYPE_MXFP4: dequant_worker_fn = dequantize_x4x2_worker_loop_mxfp4; break;
1187
+ case HTP_TYPE_Q8_0: dequant_worker_fn = dequantize_x4x2_worker_loop_q8_0; break;
1188
+ case HTP_TYPE_F16: dequant_worker_fn = convert_f16_worker_loop; break;
1189
+ case HTP_TYPE_F32: dequant_worker_fn = quantize_f32_worker_loop; break;
1190
+ default:
1191
+ return -1;
1192
+ }
1193
+
1194
+ const int n_k_tiles = k / HMX_FP16_TILE_N_COLS;
1195
+ const struct fastdiv_values n_k_tiles_div = init_fastdiv_values(n_k_tiles);
1196
+
1197
+ // --- Dynamic Mode Configuration ---
1198
+ const bool use_pipeline = (m > 32);
1199
+ const int num_threads = (m <= 32) ? 1 : ctx->n_threads;
1200
+
1201
+ // --- Dynamic VTCM layout ---
1202
+ const size_t vec_dot_size = k * sizeof(__fp16);
1203
+ const size_t vtcm_budget = ctx->vtcm_size;
1204
+ size_t vtcm_used = 0;
1205
+
1206
+ // Pipeline = 4-stage DMA→dequant→HMX→store with HMX worker overlap.
1207
+ const size_t size_per_n = row_stride + (use_pipeline ? 2 * vec_dot_size : vec_dot_size); // Q + S0 + S1 (dequant bufs)
1208
+ const size_t size_per_mn = (use_pipeline ? 2 : 1) * sizeof(__fp16); // O x 2 (output double buffer)
1209
+
1210
+ size_t m_chunk_n_rows = 0, n_chunk_n_cols = 0;
1211
+ if (hmx_compute_chunks(vtcm_budget, /*overhead=*/256, size_per_n, /*per_m=*/vec_dot_size, size_per_mn,
1212
+ hex_align_up(m, HMX_FP16_TILE_N_ROWS), n,
1213
+ /*m_block_cost=*/(size_t) n * 3,
1214
+ /*n_block_cost=*/(size_t) m * 2, &m_chunk_n_rows, &n_chunk_n_cols, &vtcm_used)) {
1215
+ FARF(HIGH, "hmx-mm-2d: VTCM too small : m %d k %d n %d budget %zu", m, k, n, vtcm_budget);
1216
+ return -1;
1217
+ }
1218
+
1219
+ const size_t weight_area_size = hex_align_up(n_chunk_n_cols * row_stride, HMX_FP16_TILE_SIZE);
1220
+ const size_t act_area_size = hex_align_up(m_chunk_n_rows * vec_dot_size, HMX_FP16_TILE_SIZE);
1221
+ const size_t output_area_size = hex_align_up(m_chunk_n_rows * n_chunk_n_cols * sizeof(__fp16), HMX_FP16_TILE_SIZE);
1222
+
1223
+ size_t scratch0_size, scratch1_size, scratch2_size;
1224
+ scratch0_size = hex_align_up(n_chunk_n_cols * vec_dot_size, HMX_FP16_TILE_SIZE); // dequant buf 0
1225
+ scratch1_size = use_pipeline ? scratch0_size : 0; // dequant buf 1
1226
+ scratch2_size = use_pipeline ? output_area_size : 0; // output buf 1
1227
+
1228
+ uint8_t *vtcm_ptr = (uint8_t *) ctx->vtcm_base;
1229
+ __fp16 *vtcm_weight = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, weight_area_size);
1230
+ __fp16 *vtcm_activation = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, act_area_size);
1231
+ __fp16 *vtcm_output = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, output_area_size);
1232
+ void *vtcm_scratch0 = vtcm_seq_alloc(&vtcm_ptr, scratch0_size);
1233
+ void *vtcm_scratch1 = scratch1_size ? vtcm_seq_alloc(&vtcm_ptr, scratch1_size) : NULL;
1234
+ void *vtcm_scratch2 = scratch2_size ? vtcm_seq_alloc(&vtcm_ptr, scratch2_size) : NULL;
1235
+ __fp16 *vtcm_scales = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, 256);
1236
+
1237
+ vtcm_used = vtcm_ptr - (uint8_t *) ctx->vtcm_base;
1238
+ if (vtcm_used > vtcm_budget) {
1239
+ FARF(ERROR, "hmx-mm-2d: VTCM overflow: used %zu budget %zu", vtcm_used, vtcm_budget);
1240
+ return -1;
1241
+ }
1242
+
1243
+ hmx_init_column_scales(vtcm_scales, Q6_V_vsplat_R(0x3c00)); // scale: 1.0, bias: 0.0 in FP16
1244
+
1245
+ FARF(HIGH, "hmx-mm-2d: standard : m %d k %d n %d wtype %d mc %zu nc %zu vtcm %zu/%zu",
1246
+ m, k, n, weight_type, m_chunk_n_rows, n_chunk_n_cols, vtcm_used, vtcm_budget);
1247
+
1248
+ TIMER_DEFINE(activation_load);
1249
+ TIMER_DEFINE(weight_load);
1250
+ TIMER_DEFINE(hmx_core);
1251
+ TIMER_DEFINE(output_store);
1252
+
1253
+ TIMER_DEFINE(total);
1254
+ TIMER_START(total);
1255
+
1256
+ int n_chunk_cnt = hmx_ceil_div(n, n_chunk_n_cols);
1257
+
1258
+ if (use_pipeline) {
1259
+ // --- Asynchronous Pipelined Loop (Current implementation) ---
1260
+ hmx_matmul_job_t job_slots[2]; // persistent double-buffered job descriptors
1261
+
1262
+ for (size_t mr = 0; mr < m; mr += m_chunk_n_rows) {
1263
+ const size_t n_rows = hex_smin(m - mr, m_chunk_n_rows);
1264
+
1265
+ void *vtcm_qweight = vtcm_weight;
1266
+ void *vtcm_weight_bufs[2] = { vtcm_scratch0, vtcm_scratch1 };
1267
+ void *vtcm_output_bufs[2] = { vtcm_output, vtcm_scratch2 };
1268
+
1269
+ // prologue: A0
1270
+ const size_t n_cols_A0 = hex_smin(n - 0 * n_chunk_n_cols, n_chunk_n_cols);
1271
+ {
1272
+ const uint8_t *qweight_chunk_A0 = permuted_weight;
1273
+ dma_queue_push(ctx->dma[0], dma_make_ptr(vtcm_qweight, qweight_chunk_A0), row_stride, weight_stride, row_stride, n_cols_A0);
1274
+ }
1275
+
1276
+ {
1277
+ const float *activation_chunk = activation + mr * act_stride;
1278
+ transfer_activation_chunk_threaded(ctx, vtcm_activation, activation_chunk, n_rows, k, act_stride, num_threads);
1279
+ }
1280
+
1281
+ // prologue: B0, A1, submit C0 (async), B1 (overlaps C0)
1282
+ {
1283
+ // B0: wait for DMA, dequant weight chunk 0
1284
+ dma_queue_pop(ctx->dma[0]);
1285
+ dequantize_x4x2_weight_chunk_to_fp16_tiles(ctx, vtcm_weight_bufs[0], vtcm_qweight, n_cols_A0, k, row_stride, weight_type, n_k_tiles, n_k_tiles_div, dequant_worker_fn, num_threads);
1286
+
1287
+ // A1: issue DMA for weight chunk 1
1288
+ const size_t n_cols_A1 = hex_smin(n - 1 * n_chunk_n_cols, n_chunk_n_cols);
1289
+ if (1 < n_chunk_cnt) {
1290
+ const uint8_t *qweight_chunk_A1 = permuted_weight + n_chunk_n_cols * weight_stride;
1291
+ dma_queue_push(ctx->dma[0], dma_make_ptr(vtcm_qweight, qweight_chunk_A1), row_stride, weight_stride, row_stride, n_cols_A1);
1292
+ }
1293
+
1294
+ // submit C0 (non-blocking — HMX worker executes in parallel)
1295
+ hmx_matmul_job_init(&job_slots[0], (__fp16 *) vtcm_output_bufs[0], (__fp16 *) vtcm_activation,
1296
+ (__fp16 *) vtcm_weight_bufs[0], vtcm_scales,
1297
+ hmx_ceil_div(n_rows, HMX_FP16_TILE_N_ROWS),
1298
+ hmx_ceil_div(n_cols_A0, HMX_FP16_TILE_N_COLS), k / HMX_FP16_TILE_N_ROWS);
1299
+ hmx_queue_push(ctx->hmx_queue, hmx_queue_make_desc(hmx_matmul_worker_fn, &job_slots[0]));
1300
+
1301
+ // B1: DMA pop + dequant (runs in parallel with C0 on HMX worker)
1302
+ if (1 < n_chunk_cnt) {
1303
+ dma_queue_pop(ctx->dma[0]);
1304
+ dequantize_x4x2_weight_chunk_to_fp16_tiles(ctx, vtcm_weight_bufs[1], vtcm_qweight, n_cols_A1, k, row_stride, weight_type, n_k_tiles, n_k_tiles_div, dequant_worker_fn, num_threads);
1305
+ }
1306
+ }
1307
+
1308
+ // main loop: wait C_i → submit C_{i+1} → D_i + B_{i+2} (parallel with C_{i+1})
1309
+ for (int i = 0; i < n_chunk_cnt; ++i) {
1310
+ const size_t nc = i * n_chunk_n_cols;
1311
+ const size_t nc_p1 = nc + 1 * n_chunk_n_cols;
1312
+ const size_t nc_p2 = nc + 2 * n_chunk_n_cols;
1313
+
1314
+ const size_t n_cols = hex_smin(n - nc, n_chunk_n_cols);
1315
+ const size_t n_cols_p1 = hex_smin(n - nc_p1, n_chunk_n_cols);
1316
+ const size_t n_cols_p2 = hex_smin(n - nc_p2, n_chunk_n_cols);
1317
+
1318
+ // issue A_{i+2}: DMA push (non-blocking)
1319
+ if (i + 2 < n_chunk_cnt) {
1320
+ const uint8_t *qweight_chunk_p2 = permuted_weight + nc_p2 * weight_stride;
1321
+ dma_queue_push(ctx->dma[0], dma_make_ptr(vtcm_qweight, qweight_chunk_p2), row_stride, weight_stride, row_stride, n_cols_p2);
1322
+ }
1323
+
1324
+ // wait C_i: block until prologue/previous C completes
1325
+ hmx_queue_pop(ctx->hmx_queue);
1326
+
1327
+ // submit C_{i+1} (non-blocking, overlaps with D_i + B_{i+2} below)
1328
+ if (i + 1 < n_chunk_cnt) {
1329
+ hmx_matmul_job_init(&job_slots[(i + 1) % 2], (__fp16 *) vtcm_output_bufs[(i + 1) % 2],
1330
+ (__fp16 *) vtcm_activation, (__fp16 *) vtcm_weight_bufs[(i + 1) % 2],
1331
+ vtcm_scales, hmx_ceil_div(n_rows, HMX_FP16_TILE_N_ROWS),
1332
+ hmx_ceil_div(n_cols_p1, HMX_FP16_TILE_N_COLS), k / HMX_FP16_TILE_N_ROWS);
1333
+ hmx_queue_push(ctx->hmx_queue, hmx_queue_make_desc(hmx_matmul_worker_fn, &job_slots[(i + 1) % 2]));
1334
+ }
1335
+
1336
+ // D_i: store output (multi-thread HVX, parallel with C_{i+1})
1337
+ float *output_chunk = dst + (mr * n + nc);
1338
+ transfer_output_chunk_threaded(ctx, output_chunk, vtcm_output_bufs[i % 2], n_rows, n_cols, n, num_threads);
1339
+
1340
+ // B_{i+2}: DMA pop + dequant (multi-thread HVX, parallel with C_{i+1})
1341
+ if (i + 2 < n_chunk_cnt) {
1342
+ dma_queue_pop(ctx->dma[0]);
1343
+ dequantize_x4x2_weight_chunk_to_fp16_tiles(ctx, vtcm_weight_bufs[(i + 2) % 2], vtcm_qweight, n_cols_p2, k, row_stride, weight_type, n_k_tiles, n_k_tiles_div, dequant_worker_fn, num_threads);
1344
+ }
1345
+ }
1346
+ }
1347
+ hmx_queue_suspend(ctx->hmx_queue);
1348
+ } else {
1349
+ // --- Synchronous Loop (Optimized for small/non-pipelined cases) ---
1350
+ HAP_compute_res_hmx_lock(ctx->vtcm_rctx);
1351
+
1352
+ for (size_t mr = 0; mr < m; mr += m_chunk_n_rows) {
1353
+ const size_t n_rows = hex_smin(m - mr, m_chunk_n_rows);
1354
+ const size_t n_row_tiles = hmx_ceil_div(n_rows, HMX_FP16_TILE_N_ROWS);
1355
+
1356
+ // Load Activation
1357
+ const float *activation_chunk = activation + mr * act_stride;
1358
+ transfer_activation_chunk_threaded(ctx, vtcm_activation, activation_chunk, n_rows, k, act_stride, num_threads);
1359
+
1360
+ for (size_t nc = 0; nc < n; nc += n_chunk_n_cols) {
1361
+ const size_t n_cols = hex_smin(n - nc, n_chunk_n_cols);
1362
+ const size_t n_col_tiles = hmx_ceil_div(n_cols, HMX_FP16_TILE_N_COLS);
1363
+
1364
+ // A: DMA Load Weight
1365
+ const uint8_t *qweight_chunk = permuted_weight + nc * weight_stride;
1366
+ dma_queue_push(ctx->dma[0], dma_make_ptr(vtcm_weight, qweight_chunk), row_stride, weight_stride, row_stride, n_cols);
1367
+ dma_queue_pop(ctx->dma[0]);
1368
+
1369
+ // B: Dequantize / Convert Weight
1370
+ dequantize_x4x2_weight_chunk_to_fp16_tiles(ctx, vtcm_scratch0, vtcm_weight, n_cols, k, row_stride, weight_type, n_k_tiles, n_k_tiles_div, dequant_worker_fn, num_threads);
1371
+
1372
+ // C: HMX Compute (Synchronous)
1373
+ core_dot_chunk_fp16(vtcm_output, vtcm_activation, vtcm_scratch0, vtcm_scales, n_row_tiles, n_col_tiles, k / HMX_FP16_TILE_N_ROWS);
1374
+
1375
+ // D: Output Store
1376
+ float *output_chunk = dst + (mr * n + nc);
1377
+ transfer_output_chunk_threaded(ctx, output_chunk, vtcm_output, n_rows, n_cols, n, num_threads);
1378
+ }
1379
+ }
1380
+ HAP_compute_res_hmx_unlock(ctx->vtcm_rctx);
1381
+ }
1382
+
1383
+ TIMER_STOP(total);
1384
+
1385
+ #if defined(ENABLE_PROFILE_TIMERS)
1386
+ FARF(HIGH, "hex-mm-2d: %lld us : m %d k %d n %d", TIMER_US(total), m, k, n);
1387
+ if (!use_pipeline) {
1388
+ FARF(HIGH, " activation_load: %lld us, weight_load: %lld us, hmx_core: %lld us, output_store: %lld us",
1389
+ TIMER_US(activation_load), TIMER_US(weight_load), TIMER_US(hmx_core), TIMER_US(output_store));
1390
+ size_t weight_size = (size_t)n * row_stride;
1391
+ float bandwidth = 1e-3f * weight_size / (float)TIMER_US(weight_load);
1392
+ FARF(HIGH, " weight load bandwidth: %.2f GB/s", bandwidth);
1393
+ }
1394
+ #endif
1395
+
1396
+ return 0;
1397
+ }
1398
+
1399
+ //
1400
+
1401
+ static inline int hmx_matmul_batch_r2(const hmx_matmul_f16_f32_batched_params_t *params) {
1402
+ return params->ne02 > 0 ? params->ne12 / params->ne02 : 1;
1403
+ }
1404
+
1405
+ static inline int hmx_matmul_batch_r3(const hmx_matmul_f16_f32_batched_params_t *params) {
1406
+ return params->ne03 > 0 ? params->ne13 / params->ne03 : 1;
1407
+ }
1408
+
1409
+ static inline const __fp16 *hmx_matmul_weight_batch_ptr(const hmx_matmul_f16_f32_batched_params_t *params,
1410
+ int dst_b2, int dst_b3) {
1411
+ const int r2 = hmx_matmul_batch_r2(params);
1412
+ const int r3 = hmx_matmul_batch_r3(params);
1413
+ return (const __fp16 *) ((const uint8_t *) params->permuted_weight +
1414
+ (size_t) (dst_b2 / r2) * params->src0_nb2 +
1415
+ (size_t) (dst_b3 / r3) * params->src0_nb3);
1416
+ }
1417
+
1418
+ static inline const float *hmx_matmul_activation_batch_ptr(const hmx_matmul_f16_f32_batched_params_t *params,
1419
+ int dst_b2, int dst_b3) {
1420
+ return (const float *) ((const uint8_t *) params->activation +
1421
+ (size_t) dst_b2 * params->src1_nb2 +
1422
+ (size_t) dst_b3 * params->src1_nb3);
1423
+ }
1424
+
1425
+ static inline float *hmx_matmul_dst_batch_ptr(const hmx_matmul_f16_f32_batched_params_t *params,
1426
+ int dst_b2, int dst_b3) {
1427
+ return (float *) ((uint8_t *) params->dst +
1428
+ (size_t) dst_b2 * params->dst_nb2 +
1429
+ (size_t) dst_b3 * params->dst_nb3);
1430
+ }
1431
+
1432
+ static int hmx_matmul_f16_f32_batched_legacy(struct htp_context *ctx,
1433
+ const hmx_matmul_f16_f32_batched_params_t *params) {
1434
+ int ret = 0;
1435
+ for (int b3 = 0; b3 < params->ne13 && ret == 0; ++b3) {
1436
+ for (int b2 = 0; b2 < params->ne12 && ret == 0; ++b2) {
1437
+ ret = hmx_matmul_f16_f32(ctx, hmx_matmul_dst_batch_ptr(params, b2, b3),
1438
+ hmx_matmul_activation_batch_ptr(params, b2, b3),
1439
+ hmx_matmul_weight_batch_ptr(params, b2, b3),
1440
+ params->m, params->k, params->n,
1441
+ params->act_stride, params->weight_stride);
1442
+ }
1443
+ }
1444
+ return ret;
1445
+ }
1446
+
1447
+ int hmx_matmul_f16_f32_batched(struct htp_context *ctx, const hmx_matmul_f16_f32_batched_params_t *params) {
1448
+ if (!ctx || !params || !params->dst || !params->activation || !params->permuted_weight) { return -1; }
1449
+ if (!params->m || !params->k || !params->n) { return -1; }
1450
+ if (params->act_stride < params->k || params->weight_stride < params->k || params->dst_stride < params->n) { return -1; }
1451
+ if (params->ne02 <= 0 || params->ne03 <= 0 || params->ne12 <= 0 || params->ne13 <= 0) { return -1; }
1452
+ if (params->ne12 % params->ne02 != 0 || params->ne13 % params->ne03 != 0) { return -1; }
1453
+ if (params->k % 32 != 0 || params->n % 32 != 0) { return -1; }
1454
+
1455
+ if (!hex_is_aligned(params->dst, VLEN) ||
1456
+ !hex_is_aligned(params->activation, VLEN) ||
1457
+ !hex_is_aligned(params->permuted_weight, VLEN)) {
1458
+ return -1;
1459
+ }
1460
+
1461
+ const int group_size = hmx_matmul_batch_r2(params);
1462
+
1463
+ if (group_size <= 1) {
1464
+ FARF(HIGH, "%s: no dim2 GQA reuse (group=%d), using legacy batched loop", __func__, group_size);
1465
+ return hmx_matmul_f16_f32_batched_legacy(ctx, params);
1466
+ }
1467
+
1468
+ // Grouped path: reuse interleaved weight across all q_heads sharing a
1469
+ // kv_head. Each q_head gets its own activation buffer in VTCM (so
1470
+ // activation is loaded once per m_chunk and reused across all n_chunks),
1471
+ // and each q_head is computed individually to avoid tile-major packing
1472
+ // issues. m_chunk_n_rows is always a multiple of 32 (from
1473
+ // hmx_compute_chunks), so per-head tile arrays don't overlap.
1474
+ const size_t vtcm_budget = ctx->vtcm_size;
1475
+ const size_t vec_dot_size = params->k * sizeof(__fp16);
1476
+
1477
+ // When the activation has a large stride (e.g. permuted Q tensor with
1478
+ // act_stride >> k), HVX vector loads from strided DDR thrash L2 cache.
1479
+ // Allocate an F32 scratch buffer in VTCM and use 2D DMA to gather
1480
+ // strided rows into a contiguous block before the F32->F16 conversion.
1481
+ const bool use_dma_activation = (params->act_stride > params->k);
1482
+ const size_t f32_scratch_per_m = use_dma_activation ? (size_t) params->k * sizeof(float) : 0;
1483
+
1484
+ size_t m_chunk_n_rows = 0, n_chunk_n_cols = 0, vtcm_used = 0;
1485
+ // FP16 weight: interleave and activation load have similar per-element cost.
1486
+ if (hmx_compute_chunks(vtcm_budget, /*overhead=*/256,
1487
+ /*per_n=*/3 * vec_dot_size,
1488
+ /*per_m=*/group_size * vec_dot_size + f32_scratch_per_m,
1489
+ /*per_mn=*/sizeof(__fp16),
1490
+ hex_align_up(params->m, HMX_FP16_TILE_N_ROWS), params->n,
1491
+ /*m_block_cost=*/(size_t) params->n,
1492
+ /*n_block_cost=*/(size_t) params->m, &m_chunk_n_rows, &n_chunk_n_cols, &vtcm_used) != 0) {
1493
+ FARF(HIGH, "%s: grouped path does not fit VTCM, falling back to legacy batched loop", __func__);
1494
+ return hmx_matmul_f16_f32_batched_legacy(ctx, params);
1495
+ }
1496
+
1497
+ const size_t act_head_stride = m_chunk_n_rows * (size_t) params->k; // fp16 elements between heads
1498
+ const size_t weight_area_size = hex_align_up(n_chunk_n_cols * vec_dot_size, HMX_FP16_TILE_SIZE);
1499
+ const size_t activation_area_size = hex_align_up(group_size * m_chunk_n_rows * vec_dot_size, HMX_FP16_TILE_SIZE);
1500
+ const size_t output_area_size = hex_align_up(m_chunk_n_rows * n_chunk_n_cols * sizeof(__fp16), HMX_FP16_TILE_SIZE);
1501
+ const size_t scratch_area_size = hex_align_up(n_chunk_n_cols * vec_dot_size, HMX_FP16_TILE_SIZE);
1502
+ const size_t f32_scratch_size = use_dma_activation
1503
+ ? hex_align_up(m_chunk_n_rows * (size_t) params->k * sizeof(float), HMX_FP16_TILE_SIZE) : 0;
1504
+
1505
+ uint8_t *vtcm_ptr = (uint8_t *) ctx->vtcm_base;
1506
+ __fp16 *vtcm_weight = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, weight_area_size);
1507
+ __fp16 *vtcm_activation = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, activation_area_size);
1508
+ __fp16 *vtcm_output = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, output_area_size);
1509
+ void *vtcm_scratch0 = vtcm_seq_alloc(&vtcm_ptr, scratch_area_size);
1510
+ void *vtcm_scratch1 = vtcm_seq_alloc(&vtcm_ptr, scratch_area_size);
1511
+ __fp16 *vtcm_scales = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, 256);
1512
+ float *vtcm_f32_act = use_dma_activation ? (float *) vtcm_seq_alloc(&vtcm_ptr, f32_scratch_size) : NULL;
1513
+
1514
+ if ((size_t) (vtcm_ptr - (uint8_t *) ctx->vtcm_base) > vtcm_budget) {
1515
+ FARF(HIGH, "%s: grouped layout overflowed VTCM, falling back to legacy batched loop", __func__);
1516
+ return hmx_matmul_f16_f32_batched_legacy(ctx, params);
1517
+ }
1518
+
1519
+ hmx_init_column_scales(vtcm_scales, Q6_V_vsplat_R(0x3c00)); // scale: 1.0, bias: 0.0 in FP16
1520
+
1521
+ FARF(HIGH, "%s: grouped path m=%d k=%d n=%d group=%d streams=%d mc=%zu nc=%zu vtcm=%zu/%zu",
1522
+ __func__, params->m, params->k, params->n, group_size, params->ne13,
1523
+ m_chunk_n_rows, n_chunk_n_cols,
1524
+ (size_t) (vtcm_ptr - (uint8_t *) ctx->vtcm_base), vtcm_budget);
1525
+
1526
+ TIMER_DEFINE(activation_load);
1527
+ TIMER_DEFINE(weight_load);
1528
+ TIMER_DEFINE(hmx_core);
1529
+ TIMER_DEFINE(output_store);
1530
+ TIMER_DEFINE(total);
1531
+
1532
+ TIMER_START(total);
1533
+
1534
+ const size_t fp16_row_bytes = (size_t) params->k * sizeof(__fp16);
1535
+ const size_t weight_row_bytes = (size_t) params->weight_stride * sizeof(__fp16);
1536
+
1537
+ HAP_compute_res_hmx_lock(ctx->vtcm_rctx);
1538
+
1539
+ for (int b3 = 0; b3 < params->ne13; ++b3) {
1540
+ for (int b2_base = 0; b2_base < params->ne12; b2_base += group_size) {
1541
+ const __fp16 *weight_group = hmx_matmul_weight_batch_ptr(params, b2_base, b3);
1542
+
1543
+ for (size_t mr = 0; mr < (size_t) params->m; mr += m_chunk_n_rows) {
1544
+ const size_t n_rows = hex_smin((size_t) params->m - mr, m_chunk_n_rows);
1545
+ const size_t n_row_tiles = hmx_ceil_div((int) n_rows, HMX_FP16_TILE_N_ROWS);
1546
+
1547
+ // Pre-load activations for all heads in the group (once per m_chunk).
1548
+ // When the source is strided (permuted Q), use 2D DMA to gather
1549
+ // contiguous rows into a VTCM scratch buffer first, then HVX
1550
+ // converts from the contiguous VTCM buffer. This avoids L2 cache
1551
+ // thrashing from HVX loads at large strides.
1552
+ TIMER_START(activation_load);
1553
+ for (int g = 0; g < group_size; ++g) {
1554
+ const float *activation_chunk = hmx_matmul_activation_batch_ptr(params, b2_base + g, b3) + mr * params->act_stride;
1555
+ __fp16 *vtcm_act_g = vtcm_activation + (size_t) g * act_head_stride;
1556
+ if (use_dma_activation) {
1557
+ const size_t row_bytes = (size_t) params->k * sizeof(float);
1558
+ const size_t stride_bytes = (size_t) params->act_stride * sizeof(float);
1559
+ dma_queue_push(ctx->dma[0],
1560
+ dma_make_ptr(vtcm_f32_act, activation_chunk),
1561
+ row_bytes, stride_bytes, row_bytes, n_rows);
1562
+ dma_queue_pop(ctx->dma[0]);
1563
+ transfer_activation_chunk_threaded(ctx, vtcm_act_g,
1564
+ vtcm_f32_act, (int) n_rows,
1565
+ params->k, params->k, ctx->n_threads);
1566
+ } else {
1567
+ transfer_activation_chunk_threaded(ctx, vtcm_act_g,
1568
+ activation_chunk, (int) n_rows,
1569
+ params->k, params->act_stride, ctx->n_threads);
1570
+ }
1571
+ }
1572
+ TIMER_STOP(activation_load);
1573
+
1574
+ void *buf_curr = vtcm_scratch0;
1575
+ void *buf_next = vtcm_scratch1;
1576
+
1577
+ {
1578
+ const size_t n_cols_first = hex_smin((size_t) params->n, n_chunk_n_cols);
1579
+ dma_queue_push(ctx->dma[0], dma_make_ptr(buf_curr, weight_group),
1580
+ fp16_row_bytes, weight_row_bytes, fp16_row_bytes, n_cols_first);
1581
+ }
1582
+
1583
+ for (size_t nc = 0; nc < (size_t) params->n; nc += n_chunk_n_cols) {
1584
+ const size_t n_cols = hex_smin((size_t) params->n - nc, n_chunk_n_cols);
1585
+ const size_t n_col_tiles = hmx_ceil_div((int) n_cols, HMX_FP16_TILE_N_COLS);
1586
+
1587
+ TIMER_START(weight_load);
1588
+ {
1589
+ dma_queue_pop(ctx->dma[0]);
1590
+
1591
+ const size_t nc_next = nc + n_chunk_n_cols;
1592
+ if (nc_next < (size_t) params->n) {
1593
+ const size_t n_cols_next = hex_smin((size_t) params->n - nc_next, n_chunk_n_cols);
1594
+ const __fp16 *next_weight_chunk = weight_group + nc_next * params->weight_stride;
1595
+
1596
+ dma_queue_push(ctx->dma[0], dma_make_ptr(buf_next, next_weight_chunk),
1597
+ fp16_row_bytes, weight_row_bytes, fp16_row_bytes, n_cols_next);
1598
+ }
1599
+
1600
+ hmx_interleave_rows_to_tiles(vtcm_weight, (const __fp16 *) buf_curr, n_cols, params->k, params->k,
1601
+ 0, n_cols);
1602
+ hex_swap_ptr(&buf_curr, &buf_next);
1603
+ }
1604
+ TIMER_STOP(weight_load);
1605
+
1606
+ // Reuse the interleaved weight for every q_head in this GQA group
1607
+ for (int g = 0; g < group_size; ++g) {
1608
+ TIMER_START(hmx_core);
1609
+ {
1610
+ const __fp16 * vtcm_act_g = vtcm_activation + (size_t) g * act_head_stride;
1611
+ core_dot_chunk_fp16(vtcm_output, vtcm_act_g, vtcm_weight, vtcm_scales, n_row_tiles, n_col_tiles,
1612
+ params->k / 32);
1613
+ }
1614
+ TIMER_STOP(hmx_core);
1615
+
1616
+ TIMER_START(output_store);
1617
+ {
1618
+ float *output = hmx_matmul_dst_batch_ptr(params, b2_base + g, b3) + mr * params->dst_stride + nc;
1619
+ transfer_output_chunk_threaded(ctx, output, vtcm_output, (int) n_rows, (int) n_cols, params->dst_stride, ctx->n_threads);
1620
+ }
1621
+ TIMER_STOP(output_store);
1622
+ }
1623
+ }
1624
+ }
1625
+ }
1626
+ }
1627
+
1628
+ HAP_compute_res_hmx_unlock(ctx->vtcm_rctx);
1629
+
1630
+ TIMER_STOP(total);
1631
+
1632
+ #if defined(ENABLE_PROFILE_TIMERS)
1633
+ FARF(HIGH, "%s: %lld us, m=%d k=%d n=%d group=%d", __func__, TIMER_US(total),
1634
+ params->m, params->k, params->n, group_size);
1635
+ FARF(HIGH, " activation_load: %lld us, weight_load: %lld us, hmx_core: %lld us, output_store: %lld us",
1636
+ TIMER_US(activation_load), TIMER_US(weight_load), TIMER_US(hmx_core), TIMER_US(output_store));
1637
+ #endif
1638
+
1639
+ return 0;
1640
+ }
1641
+
1642
+ int hmx_matmul_f16_f32(struct htp_context *ctx, float *restrict dst, const float *restrict activation,
1643
+ const __fp16 *restrict permuted_weight, int m, int k, int n,
1644
+ int act_stride, int weight_stride) {
1645
+ if (!dst || !activation || !permuted_weight || !m || !n || !k) { return -1; }
1646
+ return hmx_matmul_2d_f32(ctx, dst, activation, (const uint8_t *)permuted_weight, m, k, n,
1647
+ act_stride, weight_stride * (int)sizeof(__fp16), HTP_TYPE_F16);
1648
+ }
1649
+
1650
+ struct mmid_row_mapping {
1651
+ uint32_t i1;
1652
+ uint32_t i2;
1653
+ };
1654
+
1655
+ typedef struct {
1656
+ __fp16 *dst;
1657
+ const float *src;
1658
+ int n_tasks;
1659
+ int n_tot_chunks;
1660
+ int n_chunks_per_task;
1661
+ int k_block;
1662
+ const struct mmid_row_mapping *matrix_rows;
1663
+ int cur_a;
1664
+ int mapping_stride;
1665
+ int ne11;
1666
+ struct fastdiv_values ne11_div;
1667
+ size_t nb11;
1668
+ size_t nb12;
1669
+ int start_row;
1670
+ int cne1;
1671
+ } activation_transfer_gathered_task_state_t;
1672
+
1673
+ typedef struct {
1674
+ const __fp16 *vtcm_src;
1675
+ float *dst;
1676
+ int n_tasks;
1677
+ int n_tot_chunks;
1678
+ int n_chunks_per_task;
1679
+ int n_cols;
1680
+ const struct mmid_row_mapping *matrix_rows;
1681
+ int cur_a;
1682
+ int mapping_stride;
1683
+ size_t dst_nb1;
1684
+ size_t dst_nb2;
1685
+ int start_row;
1686
+ int cne1;
1687
+ } output_transfer_scattered_task_state_t;
1688
+
1689
+ static void transfer_activation_chunk_fp32_to_fp16_gathered(
1690
+ __fp16 *restrict vtcm_dst,
1691
+ const float *restrict src,
1692
+ int start_row,
1693
+ int n_rows,
1694
+ int k_block,
1695
+ const struct mmid_row_mapping *matrix_rows,
1696
+ int cur_a,
1697
+ int mapping_stride,
1698
+ int ne11,
1699
+ const struct fastdiv_values * ne11_div,
1700
+ size_t nb11,
1701
+ size_t nb12,
1702
+ int cne1) {
1703
+ const int n_rows_padded = hex_align_up(n_rows, HMX_FP16_TILE_N_ROWS);
1704
+ const int n_rows_tiled = (n_rows / HMX_FP16_TILE_N_ROWS) * HMX_FP16_TILE_N_ROWS;
1705
+
1706
+ int r = 0;
1707
+
1708
+ #pragma unroll(2)
1709
+ for (r = 0; r < n_rows_tiled; r += 2) {
1710
+ int r0 = r / HMX_FP16_TILE_N_ROWS; // tile row index
1711
+ int r1 = r % HMX_FP16_TILE_N_ROWS; // intra-tile row idx
1712
+
1713
+ int r_idx0 = start_row + r + 0;
1714
+ int r_idx1 = start_row + r + 1;
1715
+
1716
+ struct mmid_row_mapping mapping0 = matrix_rows[cur_a * mapping_stride + r_idx0];
1717
+ struct mmid_row_mapping mapping1 = matrix_rows[cur_a * mapping_stride + r_idx1];
1718
+
1719
+ int i11_0 = fastmodulo(mapping0.i1, ne11, ne11_div);
1720
+ int i11_1 = fastmodulo(mapping1.i1, ne11, ne11_div);
1721
+
1722
+ const float *row0_ptr = (const float *) ((const uint8_t *) src + i11_0 * nb11 + mapping0.i2 * nb12);
1723
+ const float *row1_ptr = (const float *) ((const uint8_t *) src + i11_1 * nb11 + mapping1.i2 * nb12);
1724
+
1725
+ const HVX_Vector *pv_in0 = (const HVX_Vector *) row0_ptr;
1726
+ const HVX_Vector *pv_in1 = (const HVX_Vector *) row1_ptr;
1727
+
1728
+ for (int c = 0; c < k_block; c += 32) {
1729
+ HVX_Vector v0 = *pv_in0++;
1730
+ HVX_Vector v1 = *pv_in1++;
1731
+
1732
+ HVX_Vector v_out = hvx_vec_f32_to_f16_shuff(v0, v1);
1733
+
1734
+ int c0 = c / HMX_FP16_TILE_N_COLS; // tile column index
1735
+ int tile_idx = r0 * (k_block / HMX_FP16_TILE_N_COLS) + c0;
1736
+
1737
+ HVX_Vector *tile = (HVX_Vector *) (vtcm_dst + tile_idx * HMX_FP16_TILE_N_ELMS);
1738
+ tile[r1 / 2] = v_out;
1739
+ }
1740
+ }
1741
+
1742
+ for (; r < n_rows_padded; r += 2) {
1743
+ int r0 = r / HMX_FP16_TILE_N_ROWS; // tile row index
1744
+ int r1 = r % HMX_FP16_TILE_N_ROWS; // intra-tile row idx
1745
+
1746
+ const bool row0_valid = (start_row + r + 0) < cne1;
1747
+ const bool row1_valid = (start_row + r + 1) < cne1;
1748
+
1749
+ const float *row0_ptr = NULL;
1750
+ const float *row1_ptr = NULL;
1751
+
1752
+ if (row0_valid) {
1753
+ struct mmid_row_mapping mapping0 = matrix_rows[cur_a * mapping_stride + (start_row + r + 0)];
1754
+ int i11_0 = fastmodulo(mapping0.i1, ne11, ne11_div);
1755
+ row0_ptr = (const float *) ((const uint8_t *) src + i11_0 * nb11 + mapping0.i2 * nb12);
1756
+ }
1757
+ if (row1_valid) {
1758
+ struct mmid_row_mapping mapping1 = matrix_rows[cur_a * mapping_stride + (start_row + r + 1)];
1759
+ int i11_1 = fastmodulo(mapping1.i1, ne11, ne11_div);
1760
+ row1_ptr = (const float *) ((const uint8_t *) src + i11_1 * nb11 + mapping1.i2 * nb12);
1761
+ }
1762
+
1763
+ const HVX_Vector *pv_in0 = (const HVX_Vector *) row0_ptr;
1764
+ const HVX_Vector *pv_in1 = (const HVX_Vector *) row1_ptr;
1765
+
1766
+ for (int c = 0; c < k_block; c += 32) {
1767
+ HVX_Vector v0 = row0_valid ? *pv_in0++ : Q6_V_vzero();
1768
+ HVX_Vector v1 = row1_valid ? *pv_in1++ : Q6_V_vzero();
1769
+
1770
+ HVX_Vector v_out = hvx_vec_f32_to_f16_shuff(v0, v1);
1771
+
1772
+ int c0 = c / HMX_FP16_TILE_N_COLS; // tile column index
1773
+ int tile_idx = r0 * (k_block / HMX_FP16_TILE_N_COLS) + c0;
1774
+
1775
+ HVX_Vector *tile = (HVX_Vector *) (vtcm_dst + tile_idx * HMX_FP16_TILE_N_ELMS);
1776
+ tile[r1 / 2] = v_out;
1777
+ }
1778
+ }
1779
+ }
1780
+
1781
+ static void transfer_activation_chunk_gathered_worker_fn(unsigned int n, unsigned int i, void *data) {
1782
+ activation_transfer_gathered_task_state_t *st = data;
1783
+ int chunk_idx = i;
1784
+ int chunk_size = st->n_chunks_per_task;
1785
+ int start_row = st->start_row + chunk_idx * chunk_size;
1786
+ int n_rows = hex_smin(st->cne1 - start_row, chunk_size);
1787
+ if (n_rows > 0) {
1788
+ __fp16 *dst = st->dst + (size_t)(start_row - st->start_row) * st->k_block;
1789
+ transfer_activation_chunk_fp32_to_fp16_gathered(
1790
+ dst, st->src, start_row, n_rows, st->k_block,
1791
+ st->matrix_rows, st->cur_a, st->mapping_stride,
1792
+ st->ne11, &st->ne11_div, st->nb11, st->nb12, st->cne1);
1793
+ }
1794
+ }
1795
+
1796
+ static void transfer_activation_chunk_gathered_threaded(
1797
+ struct htp_context *ctx,
1798
+ __fp16 *dst,
1799
+ const float *src,
1800
+ int start_row,
1801
+ int n_rows,
1802
+ int k_block,
1803
+ const struct mmid_row_mapping *matrix_rows,
1804
+ int cur_a,
1805
+ int mapping_stride,
1806
+ int ne11,
1807
+ size_t nb11,
1808
+ size_t nb12,
1809
+ int cne1,
1810
+ int n_threads) {
1811
+ if (n_rows <= 0) return;
1812
+ int chunks_per_thread = hmx_ceil_div(n_rows, n_threads);
1813
+ chunks_per_thread = hex_align_up(chunks_per_thread, HMX_FP16_TILE_N_ROWS);
1814
+
1815
+ int actual_threads = hmx_ceil_div(n_rows, chunks_per_thread);
1816
+
1817
+ activation_transfer_gathered_task_state_t state = {
1818
+ .dst = dst,
1819
+ .src = src,
1820
+ .n_tasks = actual_threads,
1821
+ .n_tot_chunks = n_rows,
1822
+ .n_chunks_per_task = chunks_per_thread,
1823
+ .k_block = k_block,
1824
+ .matrix_rows = matrix_rows,
1825
+ .cur_a = cur_a,
1826
+ .mapping_stride = mapping_stride,
1827
+ .ne11 = ne11,
1828
+ .ne11_div = init_fastdiv_values(ne11),
1829
+ .nb11 = nb11,
1830
+ .nb12 = nb12,
1831
+ .start_row = start_row,
1832
+ .cne1 = cne1,
1833
+ };
1834
+
1835
+ if (actual_threads <= 1) {
1836
+ transfer_activation_chunk_gathered_worker_fn(1, 0, &state);
1837
+ } else {
1838
+ worker_pool_run_func(ctx->worker_pool, transfer_activation_chunk_gathered_worker_fn, &state, actual_threads);
1839
+ }
1840
+ }
1841
+
1842
+ static void transfer_output_chunk_fp16_to_fp32_scattered(
1843
+ float *restrict dst,
1844
+ const __fp16 *restrict vtcm_src,
1845
+ int start_row,
1846
+ int n_rows,
1847
+ int n_cols,
1848
+ const struct mmid_row_mapping *matrix_rows,
1849
+ int cur_a,
1850
+ int mapping_stride,
1851
+ size_t dst_nb1,
1852
+ size_t dst_nb2,
1853
+ int cne1) {
1854
+ assert(n_cols % HMX_FP16_TILE_N_COLS == 0);
1855
+ const size_t tile_row_stride = (n_cols / HMX_FP16_TILE_N_COLS) * HMX_FP16_TILE_N_ELMS;
1856
+
1857
+ const HVX_Vector one = hvx_vec_splat_f16(1.0);
1858
+
1859
+ for (size_t r = 0; r < n_rows; r += 2) {
1860
+ const size_t r0 = r / HMX_FP16_TILE_N_ROWS;
1861
+ const size_t r1 = (r % HMX_FP16_TILE_N_ROWS) / 2; // index of the row pair within the tile
1862
+ const __fp16 *row_base = vtcm_src + r0 * tile_row_stride;
1863
+
1864
+ int r_idx0 = start_row + (int)r + 0;
1865
+ int r_idx1 = start_row + (int)r + 1;
1866
+
1867
+ if (r_idx0 >= cne1) break;
1868
+
1869
+ struct mmid_row_mapping mapping0 = matrix_rows[cur_a * mapping_stride + r_idx0];
1870
+ float *output_row0 = (float *) ((uint8_t *) dst + mapping0.i1 * dst_nb1 + mapping0.i2 * dst_nb2);
1871
+
1872
+ float *output_row1 = NULL;
1873
+ if (r_idx1 < cne1) {
1874
+ struct mmid_row_mapping mapping1 = matrix_rows[cur_a * mapping_stride + r_idx1];
1875
+ output_row1 = (float *) ((uint8_t *) dst + mapping1.i1 * dst_nb1 + mapping1.i2 * dst_nb2);
1876
+ }
1877
+
1878
+ #pragma unroll(4)
1879
+ for (size_t c = 0; c < (size_t)n_cols; c += HMX_FP16_TILE_N_COLS) {
1880
+ const size_t c0 = c / HMX_FP16_TILE_N_COLS;
1881
+ const __fp16 *tile = row_base + c0 * HMX_FP16_TILE_N_ELMS;
1882
+ HVX_Vector v = ((const HVX_Vector *) tile)[r1];
1883
+ HVX_VectorPair vp = Q6_Wqf32_vmpy_VhfVhf(v, one);
1884
+
1885
+ volatile HVX_Vector *pv_out0 = (volatile HVX_Vector *) (output_row0 + c);
1886
+ volatile HVX_Vector *pv_out1 = output_row1 ? (volatile HVX_Vector *) (output_row1 + c) : NULL;
1887
+
1888
+ *pv_out0 = Q6_Vsf_equals_Vqf32(Q6_V_lo_W(vp));
1889
+ if (pv_out1) {
1890
+ *pv_out1 = Q6_Vsf_equals_Vqf32(Q6_V_hi_W(vp));
1891
+ }
1892
+ }
1893
+ }
1894
+ }
1895
+
1896
+ static void transfer_output_chunk_scattered_worker_fn(unsigned int n, unsigned int i, void *data) {
1897
+ output_transfer_scattered_task_state_t *st = data;
1898
+ int chunk_idx = i;
1899
+ int chunk_size = st->n_chunks_per_task;
1900
+ int start_row = st->start_row + chunk_idx * chunk_size;
1901
+ int n_rows = hex_smin(st->cne1 - start_row, chunk_size);
1902
+ if (n_rows > 0) {
1903
+ const __fp16 *src = st->vtcm_src + (size_t)(start_row - st->start_row) * st->n_cols;
1904
+ transfer_output_chunk_fp16_to_fp32_scattered(
1905
+ st->dst, src, start_row, n_rows, st->n_cols,
1906
+ st->matrix_rows, st->cur_a, st->mapping_stride,
1907
+ st->dst_nb1, st->dst_nb2, st->cne1);
1908
+ }
1909
+ }
1910
+
1911
+ static void transfer_output_chunk_scattered_threaded(
1912
+ struct htp_context *ctx,
1913
+ float *dst,
1914
+ const __fp16 *vtcm_src,
1915
+ int start_row,
1916
+ int n_rows,
1917
+ int n_cols,
1918
+ const struct mmid_row_mapping *matrix_rows,
1919
+ int cur_a,
1920
+ int mapping_stride,
1921
+ size_t dst_nb1,
1922
+ size_t dst_nb2,
1923
+ int cne1,
1924
+ int n_threads) {
1925
+ if (n_rows <= 0) return;
1926
+ int chunks_per_thread = hmx_ceil_div(n_rows, n_threads);
1927
+ chunks_per_thread = hex_align_up(chunks_per_thread, HMX_FP16_TILE_N_ROWS);
1928
+
1929
+ int actual_threads = hmx_ceil_div(n_rows, chunks_per_thread);
1930
+
1931
+ output_transfer_scattered_task_state_t state = {
1932
+ .vtcm_src = vtcm_src,
1933
+ .dst = dst,
1934
+ .n_tasks = actual_threads,
1935
+ .n_tot_chunks = n_rows,
1936
+ .n_chunks_per_task = chunks_per_thread,
1937
+ .n_cols = n_cols,
1938
+ .matrix_rows = matrix_rows,
1939
+ .cur_a = cur_a,
1940
+ .mapping_stride = mapping_stride,
1941
+ .dst_nb1 = dst_nb1,
1942
+ .dst_nb2 = dst_nb2,
1943
+ .start_row = start_row,
1944
+ .cne1 = cne1,
1945
+ };
1946
+
1947
+ if (actual_threads <= 1) {
1948
+ transfer_output_chunk_scattered_worker_fn(1, 0, &state);
1949
+ } else {
1950
+ worker_pool_run_func(ctx->worker_pool, transfer_output_chunk_scattered_worker_fn, &state, actual_threads);
1951
+ }
1952
+ }
1953
+
1954
+ int hmx_matmul_id_2d_f32(struct htp_context *ctx,
1955
+ float *restrict dst,
1956
+ const float *activation,
1957
+ const uint8_t *permuted_weight,
1958
+ int m, int k, int n,
1959
+ int ne11,
1960
+ size_t act_nb1, size_t act_nb2,
1961
+ size_t dst_nb1, size_t dst_nb2,
1962
+ int weight_stride,
1963
+ int weight_type,
1964
+ const struct mmid_row_mapping *matrix_rows,
1965
+ int cur_a,
1966
+ int mapping_stride) {
1967
+ const int cne1 = m;
1968
+ const int m_padded = hex_align_up(m, 32);
1969
+
1970
+ if (k % 32 != 0 || n % 32 != 0) { return -1; }
1971
+
1972
+ if (!hex_is_aligned(dst, VLEN) || !hex_is_aligned(activation, VLEN) || !hex_is_aligned(permuted_weight, VLEN)) {
1973
+ return -1;
1974
+ }
1975
+
1976
+ size_t row_stride = get_x4x2_row_stride(weight_type, k);
1977
+ if (row_stride == 0) {
1978
+ return -1;
1979
+ }
1980
+
1981
+ worker_callback_t dequant_worker_fn = NULL;
1982
+ switch (weight_type) {
1983
+ case HTP_TYPE_Q4_0: dequant_worker_fn = dequantize_x4x2_worker_loop_q4_0; break;
1984
+ case HTP_TYPE_IQ4_NL: dequant_worker_fn = dequantize_x4x2_worker_loop_iq4_nl; break;
1985
+ case HTP_TYPE_Q4_1: dequant_worker_fn = dequantize_x4x2_worker_loop_q4_1; break;
1986
+ case HTP_TYPE_MXFP4: dequant_worker_fn = dequantize_x4x2_worker_loop_mxfp4; break;
1987
+ case HTP_TYPE_Q8_0: dequant_worker_fn = dequantize_x4x2_worker_loop_q8_0; break;
1988
+ case HTP_TYPE_F16: dequant_worker_fn = convert_f16_worker_loop; break;
1989
+ case HTP_TYPE_F32: dequant_worker_fn = quantize_f32_worker_loop; break;
1990
+ default:
1991
+ return -1;
1992
+ }
1993
+
1994
+ const int n_k_tiles = k / HMX_FP16_TILE_N_COLS;
1995
+ const struct fastdiv_values n_k_tiles_div = init_fastdiv_values(n_k_tiles);
1996
+
1997
+ const int num_threads = ctx->n_threads;
1998
+
1999
+ const size_t vec_dot_size = k * sizeof(__fp16);
2000
+ const size_t vtcm_budget = ctx->vtcm_size;
2001
+ size_t vtcm_used = 0;
2002
+
2003
+ const size_t size_per_n = row_stride + vec_dot_size;
2004
+ const size_t size_per_mn = sizeof(__fp16);
2005
+
2006
+ size_t m_chunk_n_rows = 0, n_chunk_n_cols = 0;
2007
+ if (hmx_compute_chunks(vtcm_budget, /*overhead=*/256, size_per_n, /*per_m=*/vec_dot_size, size_per_mn,
2008
+ m_padded, n,
2009
+ /*m_block_cost=*/(size_t) n * 3,
2010
+ /*n_block_cost=*/(size_t) m_padded * 2, &m_chunk_n_rows, &n_chunk_n_cols, &vtcm_used)) {
2011
+ FARF(HIGH, "hmx-mm-id-2d: VTCM too small : m %d k %d n %d budget %zu", m_padded, k, n, vtcm_budget);
2012
+ return -1;
2013
+ }
2014
+
2015
+ const size_t weight_area_size = hex_align_up(n_chunk_n_cols * row_stride, HMX_FP16_TILE_SIZE);
2016
+ const size_t act_area_size = hex_align_up(m_chunk_n_rows * vec_dot_size, HMX_FP16_TILE_SIZE);
2017
+ const size_t output_area_size = hex_align_up(m_chunk_n_rows * n_chunk_n_cols * sizeof(__fp16), HMX_FP16_TILE_SIZE);
2018
+
2019
+ size_t scratch0_size = hex_align_up(n_chunk_n_cols * vec_dot_size, HMX_FP16_TILE_SIZE);
2020
+
2021
+ uint8_t *vtcm_ptr = (uint8_t *) ctx->vtcm_base;
2022
+ __fp16 *vtcm_weight = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, weight_area_size);
2023
+ __fp16 *vtcm_activation = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, act_area_size);
2024
+ __fp16 *vtcm_output = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, output_area_size);
2025
+ void *vtcm_scratch0 = vtcm_seq_alloc(&vtcm_ptr, scratch0_size);
2026
+ __fp16 *vtcm_scales = (__fp16 *) vtcm_seq_alloc(&vtcm_ptr, 256);
2027
+
2028
+ vtcm_used = vtcm_ptr - (uint8_t *) ctx->vtcm_base;
2029
+ if (vtcm_used > vtcm_budget) {
2030
+ FARF(ERROR, "hmx-mm-id-2d: VTCM overflow: used %zu budget %zu", vtcm_used, vtcm_budget);
2031
+ return -1;
2032
+ }
2033
+
2034
+ hmx_init_column_scales(vtcm_scales, Q6_V_vsplat_R(0x3c00));
2035
+
2036
+ HAP_compute_res_hmx_lock(ctx->vtcm_rctx);
2037
+
2038
+ for (size_t mr = 0; mr < (size_t) m_padded; mr += m_chunk_n_rows) {
2039
+ const size_t n_rows = hex_smin(m_padded - mr, m_chunk_n_rows);
2040
+ const size_t n_row_tiles = hmx_ceil_div(n_rows, HMX_FP16_TILE_N_ROWS);
2041
+
2042
+ transfer_activation_chunk_gathered_threaded(
2043
+ ctx, vtcm_activation, activation, (int) mr, (int) n_rows, k,
2044
+ matrix_rows, cur_a, mapping_stride, ne11, act_nb1, act_nb2, cne1, num_threads);
2045
+
2046
+ for (size_t nc = 0; nc < (size_t) n; nc += n_chunk_n_cols) {
2047
+ const size_t n_cols = hex_smin((size_t) n - nc, n_chunk_n_cols);
2048
+ const size_t n_col_tiles = hmx_ceil_div(n_cols, HMX_FP16_TILE_N_COLS);
2049
+
2050
+ const uint8_t *qweight_chunk = permuted_weight + nc * weight_stride;
2051
+ dma_queue_push(ctx->dma[0], dma_make_ptr(vtcm_weight, qweight_chunk), row_stride, weight_stride, row_stride, n_cols);
2052
+ dma_queue_pop(ctx->dma[0]);
2053
+
2054
+ dequantize_x4x2_weight_chunk_to_fp16_tiles(ctx, vtcm_scratch0, vtcm_weight, n_cols, k, row_stride, weight_type, n_k_tiles, n_k_tiles_div, dequant_worker_fn, num_threads);
2055
+
2056
+ core_dot_chunk_fp16(vtcm_output, vtcm_activation, vtcm_scratch0, vtcm_scales, n_row_tiles, n_col_tiles, k / HMX_FP16_TILE_N_ROWS);
2057
+
2058
+ transfer_output_chunk_scattered_threaded(
2059
+ ctx, dst, vtcm_output, (int) mr, (int) n_rows, (int) n_cols,
2060
+ matrix_rows, cur_a, mapping_stride, dst_nb1, dst_nb2, cne1, num_threads);
2061
+ }
2062
+ }
2063
+
2064
+ HAP_compute_res_hmx_unlock(ctx->vtcm_rctx);
2065
+ return 0;
2066
+ }