whispercpp 1.3.1 → 1.3.2

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 (797) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -3
  3. data/README.md +92 -31
  4. data/Rakefile +26 -7
  5. data/ext/.gitignore +5 -7
  6. data/ext/dependencies.rb +61 -0
  7. data/ext/extconf.rb +21 -198
  8. data/ext/options.rb +221 -0
  9. data/ext/ruby_whisper.c +159 -0
  10. data/ext/ruby_whisper.h +17 -2
  11. data/ext/ruby_whisper_context.c +641 -0
  12. data/ext/ruby_whisper_error.c +52 -0
  13. data/ext/ruby_whisper_model.c +232 -0
  14. data/ext/ruby_whisper_params.c +1301 -0
  15. data/ext/ruby_whisper_segment.c +143 -0
  16. data/ext/ruby_whisper_transcribe.cpp +87 -0
  17. data/ext/ruby_whisper_vad_params.c +288 -0
  18. data/ext/sources/.dockerignore +3 -0
  19. data/ext/sources/.github/workflows/bindings-ruby.yml +21 -0
  20. data/ext/sources/CMakeGraphVizOptions.cmake +8 -0
  21. data/ext/sources/CMakeLists.txt +251 -0
  22. data/ext/sources/bindings/javascript/CMakeLists.txt +41 -0
  23. data/ext/sources/bindings/javascript/emscripten.cpp +93 -0
  24. data/ext/sources/bindings/javascript/libwhisper.worker.js +1 -0
  25. data/ext/sources/bindings/javascript/package-tmpl.json +26 -0
  26. data/ext/sources/bindings/javascript/package.json +26 -0
  27. data/ext/sources/bindings/javascript/whisper.js +19 -0
  28. data/ext/sources/build-xcframework.sh +547 -0
  29. data/ext/sources/ci/run.sh +336 -0
  30. data/ext/sources/close-issue.yml +28 -0
  31. data/ext/sources/cmake/DefaultTargetOptions.cmake +16 -0
  32. data/ext/sources/cmake/FindFFmpeg.cmake +163 -0
  33. data/ext/sources/cmake/build-info.cmake +60 -0
  34. data/ext/sources/cmake/git-vars.cmake +22 -0
  35. data/ext/sources/cmake/whisper-config.cmake.in +65 -0
  36. data/ext/sources/cmake/whisper.pc.in +10 -0
  37. data/ext/sources/examples/CMakeLists.txt +124 -0
  38. data/ext/sources/examples/addon.node/CMakeLists.txt +31 -0
  39. data/ext/sources/examples/addon.node/__test__/whisper.spec.js +37 -0
  40. data/ext/sources/examples/addon.node/addon.cpp +438 -0
  41. data/ext/sources/examples/addon.node/index.js +54 -0
  42. data/ext/sources/examples/addon.node/package.json +16 -0
  43. data/ext/sources/examples/bench/CMakeLists.txt +8 -0
  44. data/ext/sources/examples/bench/bench.cpp +175 -0
  45. data/ext/sources/examples/bench.wasm/CMakeLists.txt +49 -0
  46. data/ext/sources/examples/bench.wasm/emscripten.cpp +87 -0
  47. data/ext/sources/examples/bench.wasm/index-tmpl.html +284 -0
  48. data/ext/sources/examples/cli/CMakeLists.txt +8 -0
  49. data/ext/sources/examples/cli/cli.cpp +1294 -0
  50. data/ext/sources/examples/coi-serviceworker.js +146 -0
  51. data/ext/sources/examples/command/CMakeLists.txt +10 -0
  52. data/ext/sources/examples/command/command.cpp +776 -0
  53. data/ext/sources/examples/command/commands.txt +9 -0
  54. data/ext/sources/examples/command.wasm/CMakeLists.txt +50 -0
  55. data/ext/sources/examples/command.wasm/emscripten.cpp +327 -0
  56. data/ext/sources/examples/command.wasm/index-tmpl.html +414 -0
  57. data/ext/sources/examples/common-ggml.cpp +238 -0
  58. data/ext/sources/examples/common-ggml.h +18 -0
  59. data/ext/sources/examples/common-sdl.cpp +227 -0
  60. data/ext/sources/examples/common-sdl.h +49 -0
  61. data/ext/sources/examples/common-whisper.cpp +168 -0
  62. data/ext/sources/examples/common-whisper.h +24 -0
  63. data/ext/sources/examples/common.cpp +675 -0
  64. data/ext/sources/examples/common.h +322 -0
  65. data/ext/sources/examples/deprecation-warning/CMakeLists.txt +6 -0
  66. data/ext/sources/examples/deprecation-warning/deprecation-warning.cpp +38 -0
  67. data/ext/sources/examples/ffmpeg-transcode.cpp +368 -0
  68. data/ext/sources/examples/generate-karaoke.sh +57 -0
  69. data/ext/sources/examples/grammar-parser.cpp +423 -0
  70. data/ext/sources/examples/grammar-parser.h +29 -0
  71. data/ext/sources/examples/helpers.js +191 -0
  72. data/ext/sources/examples/json.hpp +24596 -0
  73. data/ext/sources/examples/livestream.sh +112 -0
  74. data/ext/sources/examples/lsp/CMakeLists.txt +9 -0
  75. data/ext/sources/examples/lsp/lsp.cpp +467 -0
  76. data/ext/sources/examples/lsp/whisper.vim +362 -0
  77. data/ext/sources/examples/miniaudio.h +93468 -0
  78. data/ext/sources/examples/python/test_whisper_processor.py +7 -0
  79. data/ext/sources/examples/python/whisper_processor.py +54 -0
  80. data/ext/sources/examples/quantize/CMakeLists.txt +6 -0
  81. data/ext/sources/examples/quantize/quantize.cpp +223 -0
  82. data/ext/sources/examples/server/CMakeLists.txt +12 -0
  83. data/ext/sources/examples/server/bench.js +29 -0
  84. data/ext/sources/examples/server/httplib.h +10497 -0
  85. data/ext/sources/examples/server/server.cpp +1091 -0
  86. data/ext/sources/examples/server.py +115 -0
  87. data/ext/sources/examples/stb_vorbis.c +5584 -0
  88. data/ext/sources/examples/stream/CMakeLists.txt +10 -0
  89. data/ext/sources/examples/stream/stream.cpp +429 -0
  90. data/ext/sources/examples/stream.wasm/CMakeLists.txt +49 -0
  91. data/ext/sources/examples/stream.wasm/emscripten.cpp +216 -0
  92. data/ext/sources/examples/stream.wasm/index-tmpl.html +414 -0
  93. data/ext/sources/examples/sycl/CMakeLists.txt +9 -0
  94. data/ext/sources/examples/sycl/build.sh +22 -0
  95. data/ext/sources/examples/sycl/ls-sycl-device.cpp +11 -0
  96. data/ext/sources/examples/sycl/run-whisper.sh +17 -0
  97. data/ext/sources/examples/talk-llama/CMakeLists.txt +40 -0
  98. data/ext/sources/examples/talk-llama/eleven-labs.py +80 -0
  99. data/ext/sources/examples/talk-llama/llama-adapter.cpp +388 -0
  100. data/ext/sources/examples/talk-llama/llama-adapter.h +76 -0
  101. data/ext/sources/examples/talk-llama/llama-arch.cpp +1746 -0
  102. data/ext/sources/examples/talk-llama/llama-arch.h +437 -0
  103. data/ext/sources/examples/talk-llama/llama-batch.cpp +374 -0
  104. data/ext/sources/examples/talk-llama/llama-batch.h +89 -0
  105. data/ext/sources/examples/talk-llama/llama-chat.cpp +663 -0
  106. data/ext/sources/examples/talk-llama/llama-chat.h +58 -0
  107. data/ext/sources/examples/talk-llama/llama-context.cpp +2676 -0
  108. data/ext/sources/examples/talk-llama/llama-context.h +276 -0
  109. data/ext/sources/examples/talk-llama/llama-cparams.cpp +5 -0
  110. data/ext/sources/examples/talk-llama/llama-cparams.h +41 -0
  111. data/ext/sources/examples/talk-llama/llama-grammar.cpp +1229 -0
  112. data/ext/sources/examples/talk-llama/llama-grammar.h +173 -0
  113. data/ext/sources/examples/talk-llama/llama-graph.cpp +1618 -0
  114. data/ext/sources/examples/talk-llama/llama-graph.h +640 -0
  115. data/ext/sources/examples/talk-llama/llama-hparams.cpp +95 -0
  116. data/ext/sources/examples/talk-llama/llama-hparams.h +190 -0
  117. data/ext/sources/examples/talk-llama/llama-impl.cpp +167 -0
  118. data/ext/sources/examples/talk-llama/llama-impl.h +61 -0
  119. data/ext/sources/examples/talk-llama/llama-io.cpp +15 -0
  120. data/ext/sources/examples/talk-llama/llama-io.h +35 -0
  121. data/ext/sources/examples/talk-llama/llama-kv-cache.cpp +2739 -0
  122. data/ext/sources/examples/talk-llama/llama-kv-cache.h +502 -0
  123. data/ext/sources/examples/talk-llama/llama-kv-cells.h +379 -0
  124. data/ext/sources/examples/talk-llama/llama-memory.cpp +1 -0
  125. data/ext/sources/examples/talk-llama/llama-memory.h +32 -0
  126. data/ext/sources/examples/talk-llama/llama-mmap.cpp +600 -0
  127. data/ext/sources/examples/talk-llama/llama-mmap.h +68 -0
  128. data/ext/sources/examples/talk-llama/llama-model-loader.cpp +1138 -0
  129. data/ext/sources/examples/talk-llama/llama-model-loader.h +169 -0
  130. data/ext/sources/examples/talk-llama/llama-model-saver.cpp +281 -0
  131. data/ext/sources/examples/talk-llama/llama-model-saver.h +37 -0
  132. data/ext/sources/examples/talk-llama/llama-model.cpp +13814 -0
  133. data/ext/sources/examples/talk-llama/llama-model.h +425 -0
  134. data/ext/sources/examples/talk-llama/llama-quant.cpp +966 -0
  135. data/ext/sources/examples/talk-llama/llama-quant.h +1 -0
  136. data/ext/sources/examples/talk-llama/llama-sampling.cpp +2575 -0
  137. data/ext/sources/examples/talk-llama/llama-sampling.h +32 -0
  138. data/ext/sources/examples/talk-llama/llama-vocab.cpp +3340 -0
  139. data/ext/sources/examples/talk-llama/llama-vocab.h +131 -0
  140. data/ext/sources/examples/talk-llama/llama.cpp +354 -0
  141. data/ext/sources/examples/talk-llama/llama.h +1377 -0
  142. data/ext/sources/examples/talk-llama/prompts/talk-alpaca.txt +23 -0
  143. data/ext/sources/examples/talk-llama/speak +40 -0
  144. data/ext/sources/examples/talk-llama/speak.bat +1 -0
  145. data/ext/sources/examples/talk-llama/speak.ps1 +14 -0
  146. data/ext/sources/examples/talk-llama/talk-llama.cpp +808 -0
  147. data/ext/sources/examples/talk-llama/unicode-data.cpp +7034 -0
  148. data/ext/sources/examples/talk-llama/unicode-data.h +20 -0
  149. data/ext/sources/examples/talk-llama/unicode.cpp +849 -0
  150. data/ext/sources/examples/talk-llama/unicode.h +66 -0
  151. data/ext/sources/examples/vad-speech-segments/CMakeLists.txt +8 -0
  152. data/ext/sources/examples/vad-speech-segments/speech.cpp +143 -0
  153. data/ext/sources/examples/wchess/CMakeLists.txt +10 -0
  154. data/ext/sources/examples/wchess/libwchess/CMakeLists.txt +19 -0
  155. data/ext/sources/examples/wchess/libwchess/Chessboard.cpp +803 -0
  156. data/ext/sources/examples/wchess/libwchess/Chessboard.h +33 -0
  157. data/ext/sources/examples/wchess/libwchess/WChess.cpp +193 -0
  158. data/ext/sources/examples/wchess/libwchess/WChess.h +63 -0
  159. data/ext/sources/examples/wchess/libwchess/test-chessboard.cpp +117 -0
  160. data/ext/sources/examples/wchess/wchess.cmd/CMakeLists.txt +8 -0
  161. data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +249 -0
  162. data/ext/sources/examples/whisper.wasm/CMakeLists.txt +50 -0
  163. data/ext/sources/examples/whisper.wasm/emscripten.cpp +118 -0
  164. data/ext/sources/examples/whisper.wasm/index-tmpl.html +658 -0
  165. data/ext/sources/ggml/CMakeLists.txt +390 -0
  166. data/ext/sources/ggml/cmake/BuildTypes.cmake +54 -0
  167. data/ext/sources/ggml/cmake/GitVars.cmake +22 -0
  168. data/ext/sources/ggml/cmake/common.cmake +26 -0
  169. data/ext/sources/ggml/cmake/ggml-config.cmake.in +152 -0
  170. data/ext/{ggml → sources/ggml}/include/ggml-alloc.h +1 -1
  171. data/ext/{ggml → sources/ggml}/include/ggml-backend.h +9 -7
  172. data/ext/{ggml → sources/ggml}/include/ggml-cpp.h +2 -1
  173. data/ext/{ggml → sources/ggml}/include/ggml-cpu.h +9 -1
  174. data/ext/{ggml → sources/ggml}/include/ggml-metal.h +1 -1
  175. data/ext/{ggml → sources/ggml}/include/ggml-opt.h +49 -28
  176. data/ext/{ggml → sources/ggml}/include/ggml-rpc.h +6 -1
  177. data/ext/{ggml → sources/ggml}/include/ggml-vulkan.h +0 -2
  178. data/ext/{ggml → sources/ggml}/include/ggml.h +182 -265
  179. data/ext/sources/ggml/include/gguf.h +202 -0
  180. data/ext/sources/ggml/src/CMakeLists.txt +346 -0
  181. data/ext/{ggml → sources/ggml}/src/ggml-alloc.c +34 -29
  182. data/ext/sources/ggml/src/ggml-amx/CMakeLists.txt +107 -0
  183. data/ext/{ggml → sources/ggml}/src/ggml-backend-impl.h +1 -2
  184. data/ext/{ggml → sources/ggml}/src/ggml-backend-reg.cpp +87 -53
  185. data/ext/{ggml → sources/ggml}/src/ggml-backend.cpp +26 -14
  186. data/ext/sources/ggml/src/ggml-blas/CMakeLists.txt +87 -0
  187. data/ext/sources/ggml/src/ggml-cann/CMakeLists.txt +74 -0
  188. data/ext/sources/ggml/src/ggml-cann/Doxyfile +2579 -0
  189. data/ext/{ggml → sources/ggml}/src/ggml-cann/acl_tensor.cpp +10 -4
  190. data/ext/{ggml → sources/ggml}/src/ggml-cann/acl_tensor.h +5 -5
  191. data/ext/{ggml → sources/ggml}/src/ggml-cann/aclnn_ops.cpp +1272 -1506
  192. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.h +1125 -0
  193. data/ext/{ggml → sources/ggml}/src/ggml-cann/common.h +135 -1
  194. data/ext/{ggml → sources/ggml}/src/ggml-cann/ggml-cann.cpp +564 -146
  195. data/ext/sources/ggml/src/ggml-cann/kernels/CMakeLists.txt +30 -0
  196. data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/dup.cpp +3 -5
  197. data/ext/{ggml → sources/ggml}/src/ggml-common.h +12 -8
  198. data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +504 -0
  199. data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/amx.cpp +2 -1
  200. data/ext/sources/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
  201. data/ext/sources/ggml/src/ggml-cpu/binary-ops.h +16 -0
  202. data/ext/sources/ggml/src/ggml-cpu/cmake/FindSIMD.cmake +100 -0
  203. data/ext/sources/ggml/src/ggml-cpu/common.h +72 -0
  204. data/ext/{ggml → sources/ggml}/src/ggml-cpu/cpu-feats-x86.cpp +5 -1
  205. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +6431 -0
  206. data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-impl.h +163 -41
  207. data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-quants.c +4029 -1117
  208. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +3510 -0
  209. data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu.cpp +67 -18
  210. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.cpp +337 -0
  211. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.h +95 -0
  212. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +482 -0
  213. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.h +17 -0
  214. data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +3544 -0
  215. data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.h +14 -0
  216. data/ext/sources/ggml/src/ggml-cpu/ops.cpp +8903 -0
  217. data/ext/sources/ggml/src/ggml-cpu/ops.h +110 -0
  218. data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +892 -0
  219. data/ext/sources/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
  220. data/ext/sources/ggml/src/ggml-cpu/unary-ops.h +28 -0
  221. data/ext/sources/ggml/src/ggml-cpu/vec.cpp +252 -0
  222. data/ext/sources/ggml/src/ggml-cpu/vec.h +818 -0
  223. data/ext/sources/ggml/src/ggml-cuda/CMakeLists.txt +184 -0
  224. data/ext/sources/ggml/src/ggml-cuda/acc.cu +61 -0
  225. data/ext/sources/ggml/src/ggml-cuda/acc.cuh +5 -0
  226. data/ext/sources/ggml/src/ggml-cuda/arange.cu +34 -0
  227. data/ext/sources/ggml/src/ggml-cuda/arange.cuh +5 -0
  228. data/ext/sources/ggml/src/ggml-cuda/argmax.cu +91 -0
  229. data/ext/sources/ggml/src/ggml-cuda/argmax.cuh +3 -0
  230. data/ext/sources/ggml/src/ggml-cuda/argsort.cu +104 -0
  231. data/ext/sources/ggml/src/ggml-cuda/argsort.cuh +3 -0
  232. data/ext/sources/ggml/src/ggml-cuda/binbcast.cu +363 -0
  233. data/ext/sources/ggml/src/ggml-cuda/binbcast.cuh +9 -0
  234. data/ext/sources/ggml/src/ggml-cuda/clamp.cu +45 -0
  235. data/ext/sources/ggml/src/ggml-cuda/clamp.cuh +5 -0
  236. data/ext/sources/ggml/src/ggml-cuda/common.cuh +828 -0
  237. data/ext/sources/ggml/src/ggml-cuda/concat.cu +221 -0
  238. data/ext/sources/ggml/src/ggml-cuda/concat.cuh +5 -0
  239. data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cu +89 -0
  240. data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cuh +5 -0
  241. data/ext/sources/ggml/src/ggml-cuda/convert.cu +730 -0
  242. data/ext/sources/ggml/src/ggml-cuda/convert.cuh +26 -0
  243. data/ext/sources/ggml/src/ggml-cuda/count-equal.cu +64 -0
  244. data/ext/sources/ggml/src/ggml-cuda/count-equal.cuh +5 -0
  245. data/ext/sources/ggml/src/ggml-cuda/cp-async.cuh +57 -0
  246. data/ext/sources/ggml/src/ggml-cuda/cpy.cu +705 -0
  247. data/ext/sources/ggml/src/ggml-cuda/cpy.cuh +11 -0
  248. data/ext/sources/ggml/src/ggml-cuda/cross-entropy-loss.cu +189 -0
  249. data/ext/sources/ggml/src/ggml-cuda/cross-entropy-loss.cuh +7 -0
  250. data/ext/sources/ggml/src/ggml-cuda/dequantize.cuh +103 -0
  251. data/ext/sources/ggml/src/ggml-cuda/diagmask.cu +40 -0
  252. data/ext/sources/ggml/src/ggml-cuda/diagmask.cuh +5 -0
  253. data/ext/sources/ggml/src/ggml-cuda/fattn-common.cuh +881 -0
  254. data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +1471 -0
  255. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cu +357 -0
  256. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cuh +3 -0
  257. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cu +365 -0
  258. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cuh +3 -0
  259. data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f16.cuh +482 -0
  260. data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f32.cuh +472 -0
  261. data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cu +634 -0
  262. data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cuh +3 -0
  263. data/ext/sources/ggml/src/ggml-cuda/fattn.cu +346 -0
  264. data/ext/sources/ggml/src/ggml-cuda/fattn.cuh +3 -0
  265. data/ext/sources/ggml/src/ggml-cuda/getrows.cu +275 -0
  266. data/ext/sources/ggml/src/ggml-cuda/getrows.cuh +15 -0
  267. data/ext/sources/ggml/src/ggml-cuda/ggml-cuda.cu +3505 -0
  268. data/ext/sources/ggml/src/ggml-cuda/gla.cu +93 -0
  269. data/ext/sources/ggml/src/ggml-cuda/gla.cuh +3 -0
  270. data/ext/sources/ggml/src/ggml-cuda/im2col.cu +103 -0
  271. data/ext/sources/ggml/src/ggml-cuda/im2col.cuh +5 -0
  272. data/ext/sources/ggml/src/ggml-cuda/mma.cuh +396 -0
  273. data/ext/sources/ggml/src/ggml-cuda/mmq.cu +324 -0
  274. data/ext/sources/ggml/src/ggml-cuda/mmq.cuh +3217 -0
  275. data/ext/sources/ggml/src/ggml-cuda/mmv.cu +336 -0
  276. data/ext/sources/ggml/src/ggml-cuda/mmv.cuh +12 -0
  277. data/ext/sources/ggml/src/ggml-cuda/mmvq.cu +595 -0
  278. data/ext/sources/ggml/src/ggml-cuda/mmvq.cuh +12 -0
  279. data/ext/sources/ggml/src/ggml-cuda/norm.cu +458 -0
  280. data/ext/sources/ggml/src/ggml-cuda/norm.cuh +11 -0
  281. data/ext/sources/ggml/src/ggml-cuda/opt-step-adamw.cu +78 -0
  282. data/ext/sources/ggml/src/ggml-cuda/opt-step-adamw.cuh +5 -0
  283. data/ext/sources/ggml/src/ggml-cuda/out-prod.cu +68 -0
  284. data/ext/sources/ggml/src/ggml-cuda/out-prod.cuh +3 -0
  285. data/ext/sources/ggml/src/ggml-cuda/pad.cu +49 -0
  286. data/ext/sources/ggml/src/ggml-cuda/pad.cuh +5 -0
  287. data/ext/sources/ggml/src/ggml-cuda/pool2d.cu +94 -0
  288. data/ext/sources/ggml/src/ggml-cuda/pool2d.cuh +5 -0
  289. data/ext/sources/ggml/src/ggml-cuda/quantize.cu +190 -0
  290. data/ext/sources/ggml/src/ggml-cuda/quantize.cuh +27 -0
  291. data/ext/sources/ggml/src/ggml-cuda/rope.cu +456 -0
  292. data/ext/sources/ggml/src/ggml-cuda/rope.cuh +7 -0
  293. data/ext/sources/ggml/src/ggml-cuda/scale.cu +31 -0
  294. data/ext/sources/ggml/src/ggml-cuda/scale.cuh +5 -0
  295. data/ext/sources/ggml/src/ggml-cuda/softmax.cu +283 -0
  296. data/ext/sources/ggml/src/ggml-cuda/softmax.cuh +7 -0
  297. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cu +148 -0
  298. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cuh +3 -0
  299. data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cu +153 -0
  300. data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cuh +3 -0
  301. data/ext/sources/ggml/src/ggml-cuda/sum.cu +45 -0
  302. data/ext/sources/ggml/src/ggml-cuda/sum.cuh +5 -0
  303. data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +39 -0
  304. data/ext/sources/ggml/src/ggml-cuda/sumrows.cuh +5 -0
  305. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_16.cu +5 -0
  306. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_8.cu +10 -0
  307. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu +10 -0
  308. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu +10 -0
  309. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_4.cu +10 -0
  310. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_16.cu +5 -0
  311. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_4.cu +10 -0
  312. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_8.cu +10 -0
  313. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_1.cu +10 -0
  314. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu +10 -0
  315. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_16.cu +5 -0
  316. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu +10 -0
  317. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_4.cu +10 -0
  318. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_8.cu +10 -0
  319. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_64-ncols2_1.cu +10 -0
  320. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_1.cu +10 -0
  321. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu +10 -0
  322. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_4.cu +10 -0
  323. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_8.cu +10 -0
  324. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-f16.cu +5 -0
  325. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_0.cu +5 -0
  326. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_1.cu +5 -0
  327. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_0.cu +5 -0
  328. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_1.cu +5 -0
  329. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q8_0.cu +5 -0
  330. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-f16.cu +5 -0
  331. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_0.cu +5 -0
  332. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_1.cu +5 -0
  333. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_0.cu +5 -0
  334. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_1.cu +5 -0
  335. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q8_0.cu +5 -0
  336. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-f16.cu +5 -0
  337. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_0.cu +5 -0
  338. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_1.cu +5 -0
  339. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_0.cu +5 -0
  340. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_1.cu +5 -0
  341. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q8_0.cu +5 -0
  342. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-f16.cu +5 -0
  343. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_0.cu +5 -0
  344. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_1.cu +5 -0
  345. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_0.cu +5 -0
  346. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_1.cu +5 -0
  347. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q8_0.cu +5 -0
  348. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-f16.cu +5 -0
  349. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_0.cu +5 -0
  350. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_1.cu +5 -0
  351. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_0.cu +5 -0
  352. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_1.cu +5 -0
  353. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q8_0.cu +5 -0
  354. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-f16.cu +5 -0
  355. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_0.cu +5 -0
  356. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_1.cu +5 -0
  357. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_0.cu +5 -0
  358. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_1.cu +5 -0
  359. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q8_0.cu +5 -0
  360. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs256-f16-f16.cu +5 -0
  361. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-f16.cu +5 -0
  362. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_0.cu +5 -0
  363. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_1.cu +5 -0
  364. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_0.cu +5 -0
  365. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_1.cu +5 -0
  366. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q8_0.cu +5 -0
  367. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-f16.cu +5 -0
  368. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_0.cu +5 -0
  369. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_1.cu +5 -0
  370. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_0.cu +5 -0
  371. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_1.cu +5 -0
  372. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q8_0.cu +5 -0
  373. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-f16.cu +5 -0
  374. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_0.cu +5 -0
  375. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_1.cu +5 -0
  376. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_0.cu +5 -0
  377. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_1.cu +5 -0
  378. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q8_0.cu +5 -0
  379. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-f16.cu +5 -0
  380. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_0.cu +5 -0
  381. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_1.cu +5 -0
  382. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_0.cu +5 -0
  383. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_1.cu +5 -0
  384. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q8_0.cu +5 -0
  385. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-f16.cu +5 -0
  386. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_0.cu +5 -0
  387. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_1.cu +5 -0
  388. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_0.cu +5 -0
  389. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_1.cu +5 -0
  390. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q8_0.cu +5 -0
  391. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-f16.cu +5 -0
  392. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_0.cu +5 -0
  393. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_1.cu +5 -0
  394. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_0.cu +5 -0
  395. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_1.cu +5 -0
  396. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q8_0.cu +5 -0
  397. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-f16.cu +5 -0
  398. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_0.cu +5 -0
  399. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_1.cu +5 -0
  400. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_0.cu +5 -0
  401. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_1.cu +5 -0
  402. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q8_0.cu +5 -0
  403. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs256-f16-f16.cu +5 -0
  404. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-f16.cu +5 -0
  405. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_0.cu +5 -0
  406. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_1.cu +5 -0
  407. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_0.cu +5 -0
  408. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_1.cu +5 -0
  409. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q8_0.cu +5 -0
  410. data/ext/sources/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +78 -0
  411. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq1_s.cu +5 -0
  412. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_s.cu +5 -0
  413. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xs.cu +5 -0
  414. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xxs.cu +5 -0
  415. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_s.cu +5 -0
  416. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_xxs.cu +5 -0
  417. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_nl.cu +5 -0
  418. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_xs.cu +5 -0
  419. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q2_k.cu +5 -0
  420. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q3_k.cu +5 -0
  421. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_0.cu +5 -0
  422. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_1.cu +5 -0
  423. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_k.cu +5 -0
  424. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_0.cu +5 -0
  425. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_1.cu +5 -0
  426. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_k.cu +5 -0
  427. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q6_k.cu +5 -0
  428. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q8_0.cu +5 -0
  429. data/ext/sources/ggml/src/ggml-cuda/tsembd.cu +47 -0
  430. data/ext/sources/ggml/src/ggml-cuda/tsembd.cuh +5 -0
  431. data/ext/sources/ggml/src/ggml-cuda/unary.cu +289 -0
  432. data/ext/sources/ggml/src/ggml-cuda/unary.cuh +59 -0
  433. data/ext/sources/ggml/src/ggml-cuda/upscale.cu +51 -0
  434. data/ext/sources/ggml/src/ggml-cuda/upscale.cuh +5 -0
  435. data/ext/sources/ggml/src/ggml-cuda/vecdotq.cuh +1135 -0
  436. data/ext/{ggml → sources/ggml}/src/ggml-cuda/vendors/cuda.h +1 -0
  437. data/ext/{ggml → sources/ggml}/src/ggml-cuda/vendors/hip.h +57 -0
  438. data/ext/{ggml → sources/ggml}/src/ggml-cuda/vendors/musa.h +7 -1
  439. data/ext/sources/ggml/src/ggml-cuda/wkv.cu +199 -0
  440. data/ext/sources/ggml/src/ggml-cuda/wkv.cuh +7 -0
  441. data/ext/sources/ggml/src/ggml-hip/CMakeLists.txt +131 -0
  442. data/ext/{ggml → sources/ggml}/src/ggml-impl.h +64 -19
  443. data/ext/sources/ggml/src/ggml-kompute/CMakeLists.txt +166 -0
  444. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/common.comp +112 -0
  445. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_add.comp +58 -0
  446. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_addrow.comp +25 -0
  447. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f16.comp +52 -0
  448. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f32.comp +52 -0
  449. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f16.comp +52 -0
  450. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f32.comp +52 -0
  451. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_diagmask.comp +30 -0
  452. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_gelu.comp +22 -0
  453. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows.comp +17 -0
  454. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f16.comp +31 -0
  455. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f32.comp +31 -0
  456. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_0.comp +38 -0
  457. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_1.comp +39 -0
  458. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q6_k.comp +44 -0
  459. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul.comp +52 -0
  460. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_f16.comp +69 -0
  461. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_mat_f32.comp +51 -0
  462. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_0.comp +33 -0
  463. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_1.comp +35 -0
  464. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_k.comp +140 -0
  465. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q6_k.comp +106 -0
  466. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q8_0.comp +73 -0
  467. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n.comp +52 -0
  468. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n_pre.comp +28 -0
  469. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_norm.comp +84 -0
  470. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_relu.comp +21 -0
  471. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rmsnorm.comp +53 -0
  472. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f16.comp +52 -0
  473. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f32.comp +52 -0
  474. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f16.comp +52 -0
  475. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f32.comp +52 -0
  476. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale.comp +19 -0
  477. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale_8.comp +23 -0
  478. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_silu.comp +22 -0
  479. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_softmax.comp +72 -0
  480. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/rope_common.comp +71 -0
  481. data/ext/sources/ggml/src/ggml-metal/CMakeLists.txt +120 -0
  482. data/ext/sources/ggml/src/ggml-metal/ggml-metal-impl.h +622 -0
  483. data/ext/{ggml → sources/ggml}/src/ggml-metal/ggml-metal.m +2178 -1064
  484. data/ext/{ggml → sources/ggml}/src/ggml-metal/ggml-metal.metal +1575 -1218
  485. data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +113 -0
  486. data/ext/sources/ggml/src/ggml-musa/mudnn.cu +112 -0
  487. data/ext/sources/ggml/src/ggml-musa/mudnn.cuh +12 -0
  488. data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +96 -0
  489. data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +5124 -0
  490. data/ext/sources/ggml/src/ggml-opencl/kernels/add.cl +83 -0
  491. data/ext/sources/ggml/src/ggml-opencl/kernels/clamp.cl +20 -0
  492. data/ext/sources/ggml/src/ggml-opencl/kernels/cpy.cl +184 -0
  493. data/ext/sources/ggml/src/ggml-opencl/kernels/cvt.cl +118 -0
  494. data/ext/sources/ggml/src/ggml-opencl/kernels/diag_mask_inf.cl +58 -0
  495. data/ext/sources/ggml/src/ggml-opencl/kernels/embed_kernel.py +26 -0
  496. data/ext/sources/ggml/src/ggml-opencl/kernels/gelu.cl +62 -0
  497. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle.cl +268 -0
  498. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_general.cl +274 -0
  499. data/ext/sources/ggml/src/ggml-opencl/kernels/get_rows.cl +163 -0
  500. data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f16.cl +57 -0
  501. data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f32.cl +57 -0
  502. data/ext/sources/ggml/src/ggml-opencl/kernels/mul.cl +79 -0
  503. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mat_Ab_Bi_8x4.cl +139 -0
  504. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f16.cl +118 -0
  505. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32.cl +118 -0
  506. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_1row.cl +94 -0
  507. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_l4.cl +84 -0
  508. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f32_f32.cl +118 -0
  509. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32.cl +192 -0
  510. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_16x_flat.cl +307 -0
  511. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_8x_flat.cl +265 -0
  512. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_8x_flat.cl +272 -0
  513. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_v.cl +254 -0
  514. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q6_k.cl +190 -0
  515. data/ext/sources/ggml/src/ggml-opencl/kernels/norm.cl +81 -0
  516. data/ext/sources/ggml/src/ggml-opencl/kernels/relu.cl +16 -0
  517. data/ext/sources/ggml/src/ggml-opencl/kernels/rms_norm.cl +96 -0
  518. data/ext/sources/ggml/src/ggml-opencl/kernels/rope.cl +721 -0
  519. data/ext/sources/ggml/src/ggml-opencl/kernels/scale.cl +16 -0
  520. data/ext/sources/ggml/src/ggml-opencl/kernels/silu.cl +30 -0
  521. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +87 -0
  522. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +87 -0
  523. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f16.cl +86 -0
  524. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f32.cl +86 -0
  525. data/ext/sources/ggml/src/ggml-opencl/kernels/transpose.cl +84 -0
  526. data/ext/{ggml → sources/ggml}/src/ggml-opt.cpp +373 -190
  527. data/ext/{ggml → sources/ggml}/src/ggml-quants.c +114 -120
  528. data/ext/sources/ggml/src/ggml-rpc/CMakeLists.txt +9 -0
  529. data/ext/{ggml → sources/ggml}/src/ggml-rpc/ggml-rpc.cpp +480 -73
  530. data/ext/sources/ggml/src/ggml-sycl/CMakeLists.txt +189 -0
  531. data/ext/sources/ggml/src/ggml-sycl/backend.hpp +37 -0
  532. data/ext/sources/ggml/src/ggml-sycl/binbcast.cpp +345 -0
  533. data/ext/sources/ggml/src/ggml-sycl/binbcast.hpp +39 -0
  534. data/ext/{ggml → sources/ggml}/src/ggml-sycl/common.cpp +20 -32
  535. data/ext/sources/ggml/src/ggml-sycl/common.hpp +589 -0
  536. data/ext/{ggml → sources/ggml}/src/ggml-sycl/concat.cpp +32 -33
  537. data/ext/sources/ggml/src/ggml-sycl/concat.hpp +20 -0
  538. data/ext/{ggml → sources/ggml}/src/ggml-sycl/conv.cpp +4 -2
  539. data/ext/sources/ggml/src/ggml-sycl/conv.hpp +20 -0
  540. data/ext/{ggml → sources/ggml}/src/ggml-sycl/convert.cpp +104 -28
  541. data/ext/sources/ggml/src/ggml-sycl/convert.hpp +34 -0
  542. data/ext/sources/ggml/src/ggml-sycl/cpy.cpp +700 -0
  543. data/ext/sources/ggml/src/ggml-sycl/cpy.hpp +11 -0
  544. data/ext/sources/ggml/src/ggml-sycl/dequantize.hpp +791 -0
  545. data/ext/{ggml → sources/ggml}/src/ggml-sycl/dmmv.cpp +156 -17
  546. data/ext/sources/ggml/src/ggml-sycl/dmmv.hpp +27 -0
  547. data/ext/sources/ggml/src/ggml-sycl/dpct/helper.hpp +2957 -0
  548. data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +1511 -0
  549. data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +75 -0
  550. data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +99 -0
  551. data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +309 -0
  552. data/ext/sources/ggml/src/ggml-sycl/getrows.hpp +20 -0
  553. data/ext/{ggml → sources/ggml}/src/ggml-sycl/ggml-sycl.cpp +1004 -1240
  554. data/ext/sources/ggml/src/ggml-sycl/gla.cpp +106 -0
  555. data/ext/sources/ggml/src/ggml-sycl/gla.hpp +8 -0
  556. data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +136 -0
  557. data/ext/sources/ggml/src/ggml-sycl/im2col.hpp +21 -0
  558. data/ext/{ggml → sources/ggml}/src/ggml-sycl/mmq.cpp +0 -1
  559. data/ext/sources/ggml/src/ggml-sycl/mmq.hpp +33 -0
  560. data/ext/{ggml → sources/ggml}/src/ggml-sycl/mmvq.cpp +261 -166
  561. data/ext/sources/ggml/src/ggml-sycl/mmvq.hpp +27 -0
  562. data/ext/{ggml → sources/ggml}/src/ggml-sycl/norm.cpp +204 -81
  563. data/ext/sources/ggml/src/ggml-sycl/norm.hpp +26 -0
  564. data/ext/{ggml → sources/ggml}/src/ggml-sycl/outprod.cpp +8 -17
  565. data/ext/sources/ggml/src/ggml-sycl/outprod.hpp +10 -0
  566. data/ext/sources/ggml/src/ggml-sycl/presets.hpp +74 -0
  567. data/ext/sources/ggml/src/ggml-sycl/quants.hpp +83 -0
  568. data/ext/sources/ggml/src/ggml-sycl/rope.cpp +361 -0
  569. data/ext/sources/ggml/src/ggml-sycl/rope.hpp +20 -0
  570. data/ext/{ggml → sources/ggml}/src/ggml-sycl/softmax.cpp +35 -25
  571. data/ext/sources/ggml/src/ggml-sycl/softmax.hpp +20 -0
  572. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.cpp +13 -0
  573. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.hpp +23 -0
  574. data/ext/{ggml → sources/ggml}/src/ggml-sycl/tsembd.cpp +3 -3
  575. data/ext/sources/ggml/src/ggml-sycl/tsembd.hpp +20 -0
  576. data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +1215 -0
  577. data/ext/sources/ggml/src/ggml-sycl/wkv.cpp +293 -0
  578. data/ext/sources/ggml/src/ggml-sycl/wkv.hpp +10 -0
  579. data/ext/sources/ggml/src/ggml-vulkan/CMakeLists.txt +196 -0
  580. data/ext/sources/ggml/src/ggml-vulkan/cmake/host-toolchain.cmake.in +15 -0
  581. data/ext/{ggml → sources/ggml}/src/ggml-vulkan/ggml-vulkan.cpp +3130 -1087
  582. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +39 -0
  583. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/acc.comp +29 -0
  584. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/add.comp +29 -0
  585. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argmax.comp +51 -0
  586. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argsort.comp +69 -0
  587. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/clamp.comp +17 -0
  588. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/concat.comp +41 -0
  589. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/contig_copy.comp +49 -0
  590. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_dw.comp +105 -0
  591. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy.comp +23 -0
  592. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +51 -0
  593. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +242 -0
  594. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/cos.comp +17 -0
  595. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/count_equal.comp +31 -0
  596. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_f32.comp +20 -0
  597. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.comp +462 -0
  598. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.comp +699 -0
  599. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_head.comp +13 -0
  600. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_m.comp +42 -0
  601. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq1_s.comp +35 -0
  602. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_s.comp +44 -0
  603. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xs.comp +43 -0
  604. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xxs.comp +48 -0
  605. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_s.comp +39 -0
  606. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_xxs.comp +49 -0
  607. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq4_nl.comp +32 -0
  608. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq4_xs.comp +34 -0
  609. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q2_k.comp +34 -0
  610. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q3_k.comp +42 -0
  611. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_0.comp +30 -0
  612. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_1.comp +32 -0
  613. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_k.comp +68 -0
  614. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_0.comp +34 -0
  615. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_1.comp +35 -0
  616. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_k.comp +70 -0
  617. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q6_k.comp +33 -0
  618. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q8_0.comp +31 -0
  619. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/diag_mask_inf.comp +34 -0
  620. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/div.comp +27 -0
  621. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +337 -0
  622. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.comp +162 -0
  623. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +360 -0
  624. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +267 -0
  625. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_split_k_reduce.comp +59 -0
  626. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu.comp +25 -0
  627. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu_quick.comp +23 -0
  628. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.comp +64 -0
  629. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_head.comp +9 -0
  630. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_unary_head.comp +76 -0
  631. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows.comp +33 -0
  632. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows_quant.comp +41 -0
  633. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/group_norm.comp +66 -0
  634. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +100 -0
  635. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/l2_norm.comp +41 -0
  636. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/leaky_relu.comp +22 -0
  637. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul.comp +27 -0
  638. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_split_k_reduce.comp +48 -0
  639. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec.comp +169 -0
  640. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_base.comp +118 -0
  641. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq1_m.comp +82 -0
  642. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq1_s.comp +79 -0
  643. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_s.comp +90 -0
  644. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_xs.comp +87 -0
  645. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq2_xxs.comp +87 -0
  646. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq3_s.comp +90 -0
  647. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq3_xxs.comp +88 -0
  648. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_nc.comp +118 -0
  649. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_p021.comp +154 -0
  650. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q2_k.comp +130 -0
  651. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q3_k.comp +132 -0
  652. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q4_k.comp +136 -0
  653. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q5_k.comp +167 -0
  654. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q6_k.comp +130 -0
  655. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +868 -0
  656. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +441 -0
  657. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq.comp +442 -0
  658. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.comp +99 -0
  659. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/norm.comp +44 -0
  660. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/opt_step_adamw.comp +42 -0
  661. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/pad.comp +28 -0
  662. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/pool2d.comp +74 -0
  663. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/quantize_q8_1.comp +77 -0
  664. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/relu.comp +21 -0
  665. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/repeat.comp +26 -0
  666. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/repeat_back.comp +37 -0
  667. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm.comp +52 -0
  668. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm_back.comp +55 -0
  669. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.comp +58 -0
  670. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_multi.comp +60 -0
  671. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_neox.comp +43 -0
  672. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_norm.comp +43 -0
  673. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_vision.comp +47 -0
  674. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/scale.comp +24 -0
  675. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sigmoid.comp +20 -0
  676. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/silu.comp +22 -0
  677. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/silu_back.comp +26 -0
  678. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sin.comp +17 -0
  679. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max.comp +173 -0
  680. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_back.comp +50 -0
  681. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/square.comp +17 -0
  682. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sub.comp +29 -0
  683. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows.comp +37 -0
  684. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/tanh.comp +20 -0
  685. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_bfloat16_support.comp +7 -0
  686. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_coopmat2_support.comp +7 -0
  687. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_coopmat_support.comp +7 -0
  688. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/test_integer_dot_support.comp +7 -0
  689. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/timestep_embedding.comp +41 -0
  690. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +1373 -0
  691. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/upscale.comp +36 -0
  692. data/ext/{ggml → sources/ggml}/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +193 -35
  693. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/wkv6.comp +87 -0
  694. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/wkv7.comp +91 -0
  695. data/ext/{ggml → sources/ggml}/src/ggml.c +676 -1820
  696. data/ext/sources/ggml/src/gguf.cpp +1330 -0
  697. data/ext/{include → sources/include}/whisper.h +68 -2
  698. data/ext/sources/src/CMakeLists.txt +143 -0
  699. data/ext/{src → sources/src}/coreml/whisper-decoder-impl.h +27 -15
  700. data/ext/{src → sources/src}/coreml/whisper-decoder-impl.m +35 -10
  701. data/ext/{src → sources/src}/coreml/whisper-encoder-impl.h +21 -9
  702. data/ext/{src → sources/src}/coreml/whisper-encoder-impl.m +28 -3
  703. data/ext/sources/src/coreml/whisper-encoder.mm +73 -0
  704. data/ext/sources/src/whisper-arch.h +197 -0
  705. data/ext/{src → sources/src}/whisper.cpp +1905 -374
  706. data/ext/sources/tests/CMakeLists.txt +105 -0
  707. data/ext/sources/tests/earnings21/eval.mk +58 -0
  708. data/ext/sources/tests/earnings21/eval.py +68 -0
  709. data/ext/sources/tests/earnings21/normalizers/__init__.py +2 -0
  710. data/ext/sources/tests/earnings21/normalizers/basic.py +80 -0
  711. data/ext/sources/tests/earnings21/normalizers/english.json +1741 -0
  712. data/ext/sources/tests/earnings21/normalizers/english.py +550 -0
  713. data/ext/sources/tests/earnings21/requirements.txt +6 -0
  714. data/ext/sources/tests/en-0-ref.txt +1 -0
  715. data/ext/sources/tests/en-1-ref.txt +1 -0
  716. data/ext/sources/tests/en-2-ref.txt +1 -0
  717. data/ext/sources/tests/es-0-ref.txt +1 -0
  718. data/ext/sources/tests/librispeech/eval.mk +39 -0
  719. data/ext/sources/tests/librispeech/eval.py +47 -0
  720. data/ext/sources/tests/librispeech/normalizers/__init__.py +2 -0
  721. data/ext/sources/tests/librispeech/normalizers/basic.py +80 -0
  722. data/ext/sources/tests/librispeech/normalizers/english.json +1741 -0
  723. data/ext/sources/tests/librispeech/normalizers/english.py +550 -0
  724. data/ext/sources/tests/librispeech/requirements.txt +6 -0
  725. data/ext/sources/tests/run-tests.sh +130 -0
  726. data/ext/sources/tests/test-c.c +3 -0
  727. data/ext/sources/tests/test-vad-full.cpp +54 -0
  728. data/ext/sources/tests/test-vad.cpp +83 -0
  729. data/ext/sources/tests/test-whisper.js +58 -0
  730. data/extsources.rb +33 -5
  731. data/lib/whisper/model/uri.rb +149 -128
  732. data/sig/whisper.rbs +480 -0
  733. data/tests/helper.rb +28 -0
  734. data/tests/test_callback.rb +45 -3
  735. data/tests/test_error.rb +2 -2
  736. data/tests/test_model.rb +38 -0
  737. data/tests/test_package.rb +18 -3
  738. data/tests/test_params.rb +145 -8
  739. data/tests/test_segment.rb +10 -19
  740. data/tests/test_vad.rb +19 -0
  741. data/tests/test_vad_params.rb +103 -0
  742. data/tests/test_whisper.rb +37 -37
  743. data/whispercpp.gemspec +5 -4
  744. metadata +766 -111
  745. data/ext/cpu.mk +0 -9
  746. data/ext/examples/dr_wav.h +0 -8815
  747. data/ext/ggml/src/ggml-cann/aclnn_ops.h +0 -592
  748. data/ext/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +0 -4262
  749. data/ext/ggml/src/ggml-cpu/ggml-cpu.c +0 -14123
  750. data/ext/ggml/src/ggml-cpu/llamafile/sgemm.cpp +0 -1884
  751. data/ext/ggml/src/ggml-cpu/llamafile/sgemm.h +0 -14
  752. data/ext/ggml/src/ggml-metal/ggml-metal-impl.h +0 -288
  753. data/ext/ggml/src/ggml-sycl/element_wise.cpp +0 -1030
  754. data/ext/ggml/src/ggml-sycl/im2col.cpp +0 -126
  755. data/ext/ggml/src/ggml-sycl/rope.cpp +0 -276
  756. data/ext/ggml/src/ggml-sycl/wkv6.cpp +0 -141
  757. data/ext/metal-embed.mk +0 -17
  758. data/ext/metal.mk +0 -6
  759. data/ext/ruby_whisper.cpp +0 -1909
  760. data/ext/scripts/get-flags.mk +0 -38
  761. data/lib/whisper.rb +0 -2
  762. /data/ext/{ggml → sources/ggml}/include/ggml-blas.h +0 -0
  763. /data/ext/{ggml → sources/ggml}/include/ggml-cann.h +0 -0
  764. /data/ext/{ggml → sources/ggml}/include/ggml-cuda.h +0 -0
  765. /data/ext/{ggml → sources/ggml}/include/ggml-kompute.h +0 -0
  766. /data/ext/{ggml → sources/ggml}/include/ggml-opencl.h +0 -0
  767. /data/ext/{ggml → sources/ggml}/include/ggml-sycl.h +0 -0
  768. /data/ext/{ggml → sources/ggml}/src/ggml-amx/common.h +0 -0
  769. /data/ext/{ggml → sources/ggml}/src/ggml-amx/ggml-amx.cpp +0 -0
  770. /data/ext/{ggml → sources/ggml}/src/ggml-amx/mmq.cpp +0 -0
  771. /data/ext/{ggml → sources/ggml}/src/ggml-amx/mmq.h +0 -0
  772. /data/ext/{ggml → sources/ggml}/src/ggml-blas/ggml-blas.cpp +0 -0
  773. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/ascendc_kernels.h +0 -0
  774. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_f16.cpp +0 -0
  775. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_f32.cpp +0 -0
  776. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -0
  777. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -0
  778. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -0
  779. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -0
  780. /data/ext/{ggml → sources/ggml}/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -0
  781. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/amx.h +0 -0
  782. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/common.h +0 -0
  783. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/mmq.cpp +0 -0
  784. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/amx/mmq.h +0 -0
  785. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-aarch64.h +0 -0
  786. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-hbm.cpp +0 -0
  787. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-hbm.h +0 -0
  788. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-quants.h +0 -0
  789. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-traits.cpp +0 -0
  790. /data/ext/{ggml → sources/ggml}/src/ggml-cpu/ggml-cpu-traits.h +0 -0
  791. /data/ext/{ggml → sources/ggml}/src/ggml-kompute/ggml-kompute.cpp +0 -0
  792. /data/ext/{ggml → sources/ggml}/src/ggml-quants.h +0 -0
  793. /data/ext/{ggml → sources/ggml}/src/ggml-threading.cpp +0 -0
  794. /data/ext/{ggml → sources/ggml}/src/ggml-threading.h +0 -0
  795. /data/ext/{src → sources/src}/coreml/whisper-encoder.h +0 -0
  796. /data/ext/{src → sources/src}/openvino/whisper-openvino-encoder.cpp +0 -0
  797. /data/ext/{src → sources/src}/openvino/whisper-openvino-encoder.h +0 -0
@@ -0,0 +1,1618 @@
1
+ #include "llama-graph.h"
2
+
3
+ #include "llama-impl.h"
4
+ #include "llama-batch.h"
5
+ #include "llama-cparams.h"
6
+ #include "llama-kv-cache.h"
7
+
8
+ #include <cassert>
9
+ #include <cmath>
10
+ #include <cstring>
11
+
12
+ void llm_graph_input_embd::set_input(const llama_ubatch * ubatch) {
13
+ if (ubatch->token) {
14
+ const int64_t n_tokens = ubatch->n_tokens;
15
+
16
+ ggml_backend_tensor_set(tokens, ubatch->token, 0, n_tokens*ggml_element_size(tokens));
17
+ }
18
+
19
+ if (ubatch->embd) {
20
+ const int64_t n_embd = embd->ne[0];
21
+ const int64_t n_tokens = ubatch->n_tokens;
22
+
23
+ ggml_backend_tensor_set(embd, ubatch->embd, 0, n_tokens*n_embd*ggml_element_size(embd));
24
+ }
25
+ }
26
+
27
+ void llm_graph_input_pos::set_input(const llama_ubatch * ubatch) {
28
+ if (ubatch->pos && pos) {
29
+ const int64_t n_tokens = ubatch->n_tokens;
30
+
31
+ if (ubatch->token && n_pos_per_embd == 4) {
32
+ // in case we're using M-RoPE with text tokens, convert the 1D positions to 4D
33
+ // the 3 first dims are the same, and 4th dim is all 0
34
+ std::vector<llama_pos> pos_data(n_tokens*n_pos_per_embd);
35
+ // copy the first dimension
36
+ for (int i = 0; i < n_tokens; ++i) {
37
+ pos_data[ i] = ubatch->pos[i];
38
+ pos_data[ n_tokens + i] = ubatch->pos[i];
39
+ pos_data[2 * n_tokens + i] = ubatch->pos[i];
40
+ pos_data[3 * n_tokens + i] = 0; // 4th dim is 0
41
+ }
42
+ ggml_backend_tensor_set(pos, pos_data.data(), 0, pos_data.size()*ggml_element_size(pos));
43
+ } else {
44
+ ggml_backend_tensor_set(pos, ubatch->pos, 0, n_tokens*n_pos_per_embd*ggml_element_size(pos));
45
+ }
46
+ }
47
+ }
48
+
49
+ void llm_graph_input_attn_temp::set_input(const llama_ubatch * ubatch) {
50
+ if (ubatch->pos && attn_scale) {
51
+ const int64_t n_tokens = ubatch->n_tokens;
52
+
53
+ std::vector<float> attn_scale_data(n_tokens, 0.0f);
54
+ for (int i = 0; i < n_tokens; ++i) {
55
+ const float pos = ubatch->pos[i];
56
+ attn_scale_data[i] = std::log(
57
+ std::floor((pos + 1.0f) / n_attn_temp_floor_scale) + 1.0
58
+ ) * f_attn_temp_scale + 1.0;
59
+ }
60
+
61
+ ggml_backend_tensor_set(attn_scale, attn_scale_data.data(), 0, n_tokens*ggml_element_size(attn_scale));
62
+ }
63
+ }
64
+
65
+ void llm_graph_input_pos_bucket::set_input(const llama_ubatch * ubatch) {
66
+ if (pos_bucket) {
67
+ const int64_t n_tokens = ubatch->n_tokens;
68
+
69
+ GGML_ASSERT(ggml_backend_buffer_is_host(pos_bucket->buffer));
70
+ GGML_ASSERT(!ubatch->equal_seqs); // TODO: use ubatch->n_seqs instead of failing
71
+
72
+ int32_t * data = (int32_t *) pos_bucket->data;
73
+
74
+ for (int h = 0; h < 1; ++h) {
75
+ for (int j = 0; j < n_tokens; ++j) {
76
+ for (int i = 0; i < n_tokens; ++i) {
77
+ data[h*(n_tokens*n_tokens) + j*n_tokens + i] = llama_relative_position_bucket(ubatch->pos[i], ubatch->pos[j], hparams.n_rel_attn_bkts, true);
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+
84
+ void llm_graph_input_pos_bucket_kv::set_input(const llama_ubatch * ubatch) {
85
+ if (pos_bucket) {
86
+ kv_self->set_input_pos_bucket(pos_bucket, ubatch);
87
+ }
88
+ }
89
+
90
+ void llm_graph_input_out_ids::set_input(const llama_ubatch * ubatch) {
91
+ if (hparams.causal_attn || cparams.pooling_type == LLAMA_POOLING_TYPE_NONE) {
92
+ //GGML_ASSERT(out_ids && "every model that can must skip unused outputs");
93
+
94
+ if (!out_ids) {
95
+ LLAMA_LOG_WARN("%s: 'out_ids' is not created\n", __func__);
96
+ } else {
97
+ const int64_t n_tokens = ubatch->n_tokens;
98
+
99
+ GGML_ASSERT(ggml_backend_buffer_is_host(out_ids->buffer));
100
+ int32_t * data = (int32_t *) out_ids->data;
101
+
102
+ if (n_outputs == n_tokens) {
103
+ for (int i = 0; i < n_tokens; ++i) {
104
+ data[i] = i;
105
+ }
106
+ } else if (ubatch->output) {
107
+ int32_t n_outputs = 0;
108
+ for (int i = 0; i < n_tokens; ++i) {
109
+ if (ubatch->output[i]) {
110
+ data[n_outputs++] = i;
111
+ }
112
+ }
113
+ // the graph needs to have been passed the correct number of outputs
114
+ GGML_ASSERT(n_outputs == n_outputs);
115
+ } else if (n_outputs == 1) {
116
+ // only keep last output
117
+ data[0] = n_tokens - 1;
118
+ } else {
119
+ GGML_ASSERT(n_outputs == 0);
120
+ }
121
+ }
122
+ }
123
+ }
124
+
125
+ void llm_graph_input_mean::set_input(const llama_ubatch * ubatch) {
126
+ if (cparams.embeddings && cparams.pooling_type == LLAMA_POOLING_TYPE_MEAN) {
127
+ const int64_t n_tokens = ubatch->n_tokens;
128
+ const int64_t n_seq_tokens = ubatch->n_seq_tokens;
129
+ const int64_t n_seqs = ubatch->n_seqs;
130
+
131
+ GGML_ASSERT(mean);
132
+ GGML_ASSERT(ggml_backend_buffer_is_host(mean->buffer));
133
+
134
+ float * data = (float *) mean->data;
135
+ memset(mean->data, 0, n_tokens * n_tokens * ggml_element_size(mean));
136
+
137
+ std::vector<uint64_t> sum(n_tokens, 0);
138
+
139
+ for (int s = 0; s < n_seqs; ++s) {
140
+ const llama_seq_id seq_id = ubatch->seq_id[s][0];
141
+
142
+ // TODO: adapt limits to n_seqs when ubatch->equal_seqs is true
143
+ GGML_ASSERT(seq_id < n_tokens && "seq_id cannot be larger than n_tokens with pooling_type == MEAN");
144
+
145
+ sum[seq_id] += ubatch->n_seq_tokens;
146
+ }
147
+
148
+ std::vector<float> div(n_tokens, 0.0f);
149
+ for (int i = 0; i < n_tokens; ++i) {
150
+ const uint64_t s = sum[i];
151
+ if (s > 0) {
152
+ div[i] = 1.0f/float(s);
153
+ }
154
+ }
155
+
156
+ for (int s = 0; s < n_seqs; ++s) {
157
+ const llama_seq_id seq_id = ubatch->seq_id[s][0];
158
+
159
+ for (int i = 0; i < n_seq_tokens; ++i) {
160
+ data[seq_id*n_tokens + s*n_seq_tokens + i] = div[seq_id];
161
+ }
162
+ }
163
+ }
164
+ }
165
+
166
+ void llm_graph_input_cls::set_input(const llama_ubatch * ubatch) {
167
+ if (cparams.embeddings && (
168
+ cparams.pooling_type == LLAMA_POOLING_TYPE_CLS ||
169
+ cparams.pooling_type == LLAMA_POOLING_TYPE_RANK)) {
170
+ const int64_t n_tokens = ubatch->n_tokens;
171
+ const int64_t n_seq_tokens = ubatch->n_seq_tokens;
172
+ const int64_t n_seqs = ubatch->n_seqs;
173
+
174
+ GGML_ASSERT(cls);
175
+ GGML_ASSERT(ggml_backend_buffer_is_host(cls->buffer));
176
+
177
+ uint32_t * data = (uint32_t *) cls->data;
178
+ memset(cls->data, 0, n_tokens * ggml_element_size(cls));
179
+
180
+ for (int s = 0; s < n_seqs; ++s) {
181
+ const llama_seq_id seq_id = ubatch->seq_id[s][0];
182
+
183
+ // TODO: adapt limits to n_seqs when ubatch->equal_seqs is true
184
+ GGML_ASSERT(seq_id < n_tokens && "seq_id cannot be larger than n_tokens with pooling_type == CLS or RANK");
185
+
186
+ for (int i = 0; i < n_seq_tokens; ++i) {
187
+ const llama_pos pos = ubatch->pos[s*n_seq_tokens + i];
188
+
189
+ if (pos == 0) {
190
+ data[seq_id] = s*n_seq_tokens + i;
191
+ }
192
+ }
193
+ }
194
+ }
195
+
196
+ if (cparams.embeddings && cparams.pooling_type == LLAMA_POOLING_TYPE_LAST) {
197
+ const int64_t n_tokens = ubatch->n_tokens;
198
+ const int64_t n_seq_tokens = ubatch->n_seq_tokens;
199
+ const int64_t n_seqs = ubatch->n_seqs;
200
+
201
+ GGML_ASSERT(cls);
202
+ GGML_ASSERT(ggml_backend_buffer_is_host(cls->buffer));
203
+
204
+ uint32_t * data = (uint32_t *) cls->data;
205
+ memset(cls->data, 0, n_tokens * ggml_element_size(cls));
206
+
207
+ std::vector<int> last_pos(n_tokens, -1);
208
+ std::vector<int> last_row(n_tokens, -1);
209
+
210
+ for (int s = 0; s < n_seqs; ++s) {
211
+ const llama_seq_id seq_id = ubatch->seq_id[s][0];
212
+
213
+ // TODO: adapt limits to n_seqs when ubatch->equal_seqs is true
214
+ GGML_ASSERT(seq_id < n_tokens && "seq_id cannot be larger than n_tokens with pooling_type == LAST");
215
+
216
+ for (int i = 0; i < n_seq_tokens; ++i) {
217
+ const llama_pos pos = ubatch->pos[s*n_seq_tokens + i];
218
+
219
+ if (pos >= last_pos[seq_id]) {
220
+ last_pos[seq_id] = pos;
221
+ last_row[seq_id] = s*n_seq_tokens + i;
222
+ }
223
+ }
224
+ }
225
+
226
+ for (int i = 0; i < n_tokens; ++i) {
227
+ if (last_row[i] >= 0) {
228
+ data[i] = last_row[i];
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ void llm_graph_input_s_copy::set_input(const llama_ubatch * ubatch) {
235
+ GGML_UNUSED(ubatch);
236
+
237
+ const int64_t n_kv = kv_self->n;
238
+
239
+ if (s_copy) {
240
+ GGML_ASSERT(ggml_backend_buffer_is_host(s_copy->buffer));
241
+ int32_t * data = (int32_t *) s_copy->data;
242
+
243
+ // assuming copy destinations ALWAYS happen ONLY on the cells between head and head+n
244
+ for (uint32_t i = 0; i < n_kv; ++i) {
245
+ data[i] = kv_self->s_copy(i);
246
+ }
247
+ }
248
+ }
249
+
250
+ void llm_graph_input_s_mask::set_input(const llama_ubatch * ubatch) {
251
+ GGML_UNUSED(ubatch);
252
+
253
+ const int64_t n_kv = kv_self->n;
254
+
255
+ if (s_mask) {
256
+ GGML_ASSERT(ggml_backend_buffer_is_host(s_mask->buffer));
257
+ float * data = (float *) s_mask->data;
258
+
259
+ // clear unused states
260
+ for (int i = 0; i < n_kv; ++i) {
261
+ data[i] = kv_self->s_mask(i);
262
+ }
263
+ }
264
+ }
265
+
266
+ void llm_graph_input_cross_embd::set_input(const llama_ubatch * ubatch) {
267
+ GGML_UNUSED(ubatch);
268
+
269
+ if (cross_embd && !cross->v_embd.empty()) {
270
+ assert(cross_embd->type == GGML_TYPE_F32);
271
+
272
+ ggml_backend_tensor_set(cross_embd, cross->v_embd.data(), 0, ggml_nbytes(cross_embd));
273
+ }
274
+ }
275
+
276
+ void llm_graph_input_attn_no_cache::set_input(const llama_ubatch * ubatch) {
277
+ if (kq_mask) {
278
+ if (cparams.causal_attn) {
279
+ const int64_t n_kv = ubatch->n_tokens;
280
+ const int64_t n_tokens = ubatch->n_tokens;
281
+ const int64_t n_seq_tokens = ubatch->n_seq_tokens;
282
+ const int64_t n_seqs = ubatch->n_seqs;
283
+
284
+ GGML_ASSERT(ggml_backend_buffer_is_host(kq_mask->buffer));
285
+ float * data = (float *) kq_mask->data;
286
+
287
+ for (int h = 0; h < 1; ++h) {
288
+ for (int s1 = 0; s1 < n_seqs; ++s1) {
289
+ const llama_seq_id seq_id = ubatch->seq_id[s1][0];
290
+
291
+ for (int j = 0; j < n_seq_tokens; ++j) {
292
+ const int32_t tj = s1*n_seq_tokens + j;
293
+
294
+ for (int s0 = 0; s0 < n_seqs; ++s0) {
295
+ for (int i = 0; i < n_seq_tokens; ++i) {
296
+ const int32_t ti = s0*n_seq_tokens + i;
297
+ float f = -INFINITY;
298
+
299
+ for (int s = 0; s < ubatch->n_seq_id[s0]; ++s) {
300
+ if (ubatch->seq_id[s0][s] == seq_id && ubatch->pos[ti] <= ubatch->pos[tj]) {
301
+ if (hparams.use_alibi) {
302
+ f = -std::abs(ubatch->pos[ti] - ubatch->pos[tj]);
303
+ } else {
304
+ f = 0.0f;
305
+ }
306
+ break;
307
+ }
308
+ }
309
+
310
+ data[h*(n_kv*n_tokens) + tj*n_kv + ti] = f;
311
+ }
312
+ }
313
+ }
314
+ }
315
+ }
316
+ } else {
317
+ const int64_t n_tokens = ubatch->n_tokens;
318
+ const int64_t n_seq_tokens = ubatch->n_seq_tokens;
319
+ const int64_t n_seqs = ubatch->n_seqs;
320
+ const int64_t n_stride = ubatch->n_tokens;
321
+
322
+ GGML_ASSERT(ggml_backend_buffer_is_host(kq_mask->buffer));
323
+
324
+ float * data = (float *) kq_mask->data;
325
+
326
+ for (int h = 0; h < 1; ++h) {
327
+ for (int s1 = 0; s1 < n_seqs; ++s1) {
328
+ const llama_seq_id seq_id = ubatch->seq_id[s1][0];
329
+
330
+ for (int j = 0; j < n_seq_tokens; ++j) {
331
+ const int32_t tj = s1*n_seq_tokens + j;
332
+
333
+ for (int s0 = 0; s0 < n_seqs; ++s0) {
334
+ for (int i = 0; i < n_seq_tokens; ++i) {
335
+ const int32_t ti = s0*n_seq_tokens + i;
336
+ float f = -INFINITY;
337
+
338
+ for (int s = 0; s < ubatch->n_seq_id[s0]; ++s) {
339
+ if (ubatch->seq_id[s0][s] == seq_id) {
340
+ if (hparams.use_alibi) {
341
+ f = -std::abs(ubatch->pos[ti] - ubatch->pos[tj]);
342
+ } else {
343
+ f = 0.0f;
344
+ }
345
+ break;
346
+ }
347
+ }
348
+
349
+ data[h*(n_tokens*n_tokens) + tj*n_stride + ti] = f;
350
+ }
351
+ }
352
+
353
+ for (int i = n_tokens; i < n_stride; ++i) {
354
+ data[h*(n_tokens*n_tokens) + tj*n_stride + i] = -INFINITY;
355
+ }
356
+ }
357
+ }
358
+ }
359
+ }
360
+ }
361
+ }
362
+
363
+ void llm_graph_input_attn_kv_unified::set_input(const llama_ubatch * ubatch) {
364
+ if (self_kq_mask) {
365
+ kv_self->set_input_kq_mask(self_kq_mask, ubatch, cparams.causal_attn);
366
+ }
367
+ }
368
+
369
+ void llm_graph_input_attn_kv_unified_iswa::set_input(const llama_ubatch * ubatch) {
370
+ if (self_kq_mask) {
371
+ kv_self->get_kv_base()->set_input_kq_mask(self_kq_mask, ubatch, cparams.causal_attn);
372
+ }
373
+
374
+ if (self_kq_mask_swa) {
375
+ kv_self->get_kv_swa()->set_input_kq_mask(self_kq_mask_swa, ubatch, cparams.causal_attn);
376
+ }
377
+ }
378
+
379
+ void llm_graph_input_attn_cross::set_input(const llama_ubatch * ubatch) {
380
+ if (cross_kq_mask) {
381
+ const int64_t n_enc = cross_kq_mask->ne[0];
382
+ const int64_t n_tokens = ubatch->n_tokens;
383
+
384
+ GGML_ASSERT(ggml_backend_buffer_is_host(cross_kq_mask->buffer));
385
+ GGML_ASSERT(!ubatch->equal_seqs); // TODO: use ubatch->n_seqs instead of failing
386
+
387
+ float * data = (float *) cross_kq_mask->data;
388
+
389
+ for (int h = 0; h < 1; ++h) {
390
+ for (int j = 0; j < n_tokens; ++j) {
391
+ for (int i = 0; i < n_enc; ++i) {
392
+ float f = -INFINITY;
393
+ for (int s = 0; s < ubatch->n_seq_id[j]; ++s) {
394
+ const llama_seq_id seq_id = ubatch->seq_id[j][s];
395
+ if (cross->seq_ids_enc[i].find(seq_id) != cross->seq_ids_enc[i].end()) {
396
+ f = 0.0f;
397
+ }
398
+ }
399
+ data[h*(n_enc*n_tokens) + j*n_enc + i] = f;
400
+ }
401
+ }
402
+
403
+ for (int i = n_tokens; i < GGML_PAD(n_tokens, GGML_KQ_MASK_PAD); ++i) {
404
+ for (int j = 0; j < n_enc; ++j) {
405
+ data[h*(n_enc*n_tokens) + i*n_enc + j] = -INFINITY;
406
+ }
407
+ }
408
+ }
409
+ }
410
+ }
411
+
412
+ //
413
+ // llm_graph_context
414
+ //
415
+
416
+ llm_graph_context::llm_graph_context(const llm_graph_params & params) :
417
+ arch (params.arch),
418
+ hparams (params.hparams),
419
+ cparams (params.cparams),
420
+ ubatch (params.ubatch),
421
+ n_embd (hparams.n_embd),
422
+ n_layer (hparams.n_layer),
423
+ n_rot (hparams.n_rot),
424
+ n_ctx (cparams.n_ctx),
425
+ n_head (hparams.n_head()),
426
+ n_head_kv (hparams.n_head_kv()),
427
+ n_embd_head_k (hparams.n_embd_head_k),
428
+ n_embd_k_gqa (hparams.n_embd_k_gqa()),
429
+ n_embd_head_v (hparams.n_embd_head_v),
430
+ n_embd_v_gqa (hparams.n_embd_v_gqa()),
431
+ n_expert (hparams.n_expert),
432
+ n_expert_used (cparams.warmup ? hparams.n_expert : hparams.n_expert_used),
433
+ freq_base (cparams.rope_freq_base),
434
+ freq_scale (cparams.rope_freq_scale),
435
+ ext_factor (cparams.yarn_ext_factor),
436
+ attn_factor (cparams.yarn_attn_factor),
437
+ beta_fast (cparams.yarn_beta_fast),
438
+ beta_slow (cparams.yarn_beta_slow),
439
+ norm_eps (hparams.f_norm_eps),
440
+ norm_rms_eps (hparams.f_norm_rms_eps),
441
+ n_tokens (ubatch.n_tokens),
442
+ n_outputs (params.n_outputs),
443
+ n_ctx_orig (cparams.n_ctx_orig_yarn),
444
+ pooling_type (cparams.pooling_type),
445
+ rope_type (hparams.rope_type),
446
+ ctx0 (params.ctx),
447
+ sched (params.sched),
448
+ backend_cpu (params.backend_cpu),
449
+ cvec (params.cvec),
450
+ loras (params.loras),
451
+ memory (params.memory),
452
+ cross (params.cross),
453
+ cb_func (params.cb),
454
+ res (std::make_unique<llm_graph_result>()) {
455
+ }
456
+
457
+ int64_t llm_graph_context::n_pos_per_embd() const {
458
+ return arch == LLM_ARCH_QWEN2VL ? 4 : 1;
459
+ }
460
+
461
+ void llm_graph_context::cb(ggml_tensor * cur, const char * name, int il) const {
462
+ if (cb_func) {
463
+ cb_func(ubatch, cur, name, il);
464
+ }
465
+ }
466
+
467
+ ggml_tensor * llm_graph_context::build_cvec(
468
+ ggml_tensor * cur,
469
+ int il) const {
470
+ return cvec->apply_to(ctx0, cur, il);
471
+ }
472
+
473
+ ggml_tensor * llm_graph_context::build_lora_mm(
474
+ ggml_tensor * w,
475
+ ggml_tensor * cur) const {
476
+ ggml_tensor * res = ggml_mul_mat(ctx0, w, cur);
477
+
478
+ for (const auto & lora : *loras) {
479
+ llama_adapter_lora_weight * lw = lora.first->get_weight(w);
480
+ if (lw == nullptr) {
481
+ continue;
482
+ }
483
+
484
+ const float adapter_scale = lora.second;
485
+ const float scale = lw->get_scale(lora.first->alpha, adapter_scale);
486
+
487
+ ggml_tensor * ab_cur = ggml_mul_mat(
488
+ ctx0, lw->b,
489
+ ggml_mul_mat(ctx0, lw->a, cur)
490
+ );
491
+
492
+ ab_cur = ggml_scale(ctx0, ab_cur, scale);
493
+ res = ggml_add(ctx0, res, ab_cur);
494
+ }
495
+
496
+ return res;
497
+ }
498
+
499
+ ggml_tensor * llm_graph_context::build_lora_mm_id(
500
+ ggml_tensor * w, // ggml_tensor * as
501
+ ggml_tensor * cur, // ggml_tensor * b
502
+ ggml_tensor * ids) const {
503
+ ggml_tensor * res = ggml_mul_mat_id(ctx0, w, cur, ids);
504
+ for (const auto & lora : *loras) {
505
+ llama_adapter_lora_weight * lw = lora.first->get_weight(w);
506
+ if (lw == nullptr) {
507
+ continue;
508
+ }
509
+
510
+ const float alpha = lora.first->alpha;
511
+ const float rank = (float) lw->b->ne[0];
512
+ const float scale = alpha ? lora.second * alpha / rank : lora.second;
513
+
514
+ ggml_tensor * ab_cur = ggml_mul_mat_id(
515
+ ctx0, lw->b,
516
+ ggml_mul_mat_id(ctx0, lw->a, cur, ids),
517
+ ids
518
+ );
519
+
520
+ ab_cur = ggml_scale(ctx0, ab_cur, scale);
521
+ res = ggml_add(ctx0, res, ab_cur);
522
+ }
523
+
524
+ return res;
525
+ }
526
+
527
+ ggml_tensor * llm_graph_context::build_norm(
528
+ ggml_tensor * cur,
529
+ ggml_tensor * mw,
530
+ ggml_tensor * mb,
531
+ llm_norm_type type,
532
+ int il) const {
533
+ switch (type) {
534
+ case LLM_NORM: cur = ggml_norm (ctx0, cur, hparams.f_norm_eps); break;
535
+ case LLM_NORM_RMS: cur = ggml_rms_norm(ctx0, cur, hparams.f_norm_rms_eps); break;
536
+ case LLM_NORM_GROUP:
537
+ {
538
+ cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], 1, cur->ne[1]);
539
+ cur = ggml_group_norm(ctx0, cur, hparams.n_norm_groups, hparams.f_norm_group_eps);
540
+ cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], cur->ne[2]);
541
+ } break;
542
+ }
543
+
544
+ if (mw || mb) {
545
+ cb(cur, "norm", il);
546
+ }
547
+
548
+ if (mw) {
549
+ cur = ggml_mul(ctx0, cur, mw);
550
+ if (mb) {
551
+ cb(cur, "norm_w", il);
552
+ }
553
+ }
554
+
555
+ if (mb) {
556
+ cur = ggml_add(ctx0, cur, mb);
557
+ }
558
+
559
+ return cur;
560
+ }
561
+
562
+ ggml_tensor * llm_graph_context::build_ffn(
563
+ ggml_tensor * cur,
564
+ ggml_tensor * up,
565
+ ggml_tensor * up_b,
566
+ ggml_tensor * up_s,
567
+ ggml_tensor * gate,
568
+ ggml_tensor * gate_b,
569
+ ggml_tensor * gate_s,
570
+ ggml_tensor * down,
571
+ ggml_tensor * down_b,
572
+ ggml_tensor * down_s,
573
+ ggml_tensor * act_scales,
574
+ llm_ffn_op_type type_op,
575
+ llm_ffn_gate_type type_gate,
576
+ int il) const {
577
+ ggml_tensor * tmp = up ? build_lora_mm(up, cur) : cur;
578
+ cb(tmp, "ffn_up", il);
579
+
580
+ if (up_b) {
581
+ tmp = ggml_add(ctx0, tmp, up_b);
582
+ cb(tmp, "ffn_up_b", il);
583
+ }
584
+
585
+ if (up_s) {
586
+ tmp = ggml_mul(ctx0, tmp, up_s);
587
+ cb(tmp, "ffn_up_s", il);
588
+ }
589
+
590
+ if (gate) {
591
+ switch (type_gate) {
592
+ case LLM_FFN_SEQ:
593
+ {
594
+ cur = build_lora_mm(gate, tmp);
595
+ cb(cur, "ffn_gate", il);
596
+ } break;
597
+ case LLM_FFN_PAR:
598
+ {
599
+ cur = build_lora_mm(gate, cur);
600
+ cb(cur, "ffn_gate", il);
601
+ } break;
602
+ }
603
+
604
+ if (gate_b) {
605
+ cur = ggml_add(ctx0, cur, gate_b);
606
+ cb(cur, "ffn_gate_b", il);
607
+ }
608
+
609
+ if (gate_s) {
610
+ cur = ggml_mul(ctx0, cur, gate_s);
611
+ cb(cur, "ffn_gate_s", il);
612
+ }
613
+
614
+ } else {
615
+ cur = tmp;
616
+ }
617
+
618
+ switch (type_op) {
619
+ case LLM_FFN_SILU:
620
+ {
621
+ cur = ggml_silu(ctx0, cur);
622
+ cb(cur, "ffn_silu", il);
623
+ } break;
624
+ case LLM_FFN_GELU:
625
+ {
626
+ cur = ggml_gelu(ctx0, cur);
627
+ cb(cur, "ffn_gelu", il);
628
+ if (act_scales != NULL) {
629
+ cur = ggml_div(ctx0, cur, act_scales);
630
+ cb(cur, "ffn_act", il);
631
+ }
632
+ } break;
633
+ case LLM_FFN_RELU:
634
+ {
635
+ cur = ggml_relu(ctx0, cur);
636
+ cb(cur, "ffn_relu", il);
637
+ } break;
638
+ case LLM_FFN_RELU_SQR:
639
+ {
640
+ cur = ggml_relu(ctx0, cur);
641
+ cb(cur, "ffn_relu", il);
642
+
643
+ cur = ggml_sqr(ctx0, cur);
644
+ cb(cur, "ffn_sqr(relu)", il);
645
+ } break;
646
+ case LLM_FFN_SWIGLU:
647
+ {
648
+ // Project to 4h. If using swiglu double the output width, see https://arxiv.org/pdf/2002.05202.pdf
649
+ int64_t split_point = cur->ne[0] / 2;
650
+ ggml_tensor * x0 = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, split_point, cur->ne[1], cur->nb[1], 0));
651
+ ggml_tensor * x1 = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, split_point, cur->ne[1], cur->nb[1], split_point * ggml_element_size(cur)));
652
+
653
+ x0 = ggml_silu(ctx0, x0);
654
+ cb(cur, "ffn_silu", il);
655
+
656
+ cur = ggml_mul(ctx0, x0, x1);
657
+ cb(cur, "ffn_mul", il);
658
+ } break;
659
+ }
660
+
661
+ if (gate && type_gate == LLM_FFN_PAR) {
662
+ cur = ggml_mul(ctx0, cur, tmp);
663
+ cb(cur, "ffn_gate_par", il);
664
+ }
665
+
666
+ if (down) {
667
+ cur = build_lora_mm(down, cur);
668
+ if (arch == LLM_ARCH_GLM4) {
669
+ // GLM4 seems to have numerical issues with half-precision accumulators
670
+ ggml_mul_mat_set_prec(cur, GGML_PREC_F32);
671
+ }
672
+ }
673
+
674
+ if (down_b) {
675
+ cb(cur, "ffn_down", il);
676
+ }
677
+
678
+ if (down_b) {
679
+ cur = ggml_add(ctx0, cur, down_b);
680
+ }
681
+
682
+ if (down_s) {
683
+ cur = ggml_mul(ctx0, cur, down_s);
684
+ cb(cur, "ffn_down_s", il);
685
+ }
686
+
687
+ return cur;
688
+ }
689
+
690
+ ggml_tensor * llm_graph_context::build_moe_ffn(
691
+ ggml_tensor * cur,
692
+ ggml_tensor * gate_inp,
693
+ ggml_tensor * up_exps,
694
+ ggml_tensor * gate_exps,
695
+ ggml_tensor * down_exps,
696
+ ggml_tensor * exp_probs_b,
697
+ int64_t n_expert,
698
+ int64_t n_expert_used,
699
+ llm_ffn_op_type type_op,
700
+ bool norm_w,
701
+ bool scale_w,
702
+ float w_scale,
703
+ llama_expert_gating_func_type gating_op,
704
+ int il) const {
705
+ const int64_t n_embd = cur->ne[0];
706
+ const int64_t n_tokens = cur->ne[1];
707
+ const bool weight_before_ffn = arch == LLM_ARCH_LLAMA4; // for llama4, we apply the sigmoid-ed weights before the FFN
708
+
709
+ ggml_tensor * logits = build_lora_mm(gate_inp, cur); // [n_expert, n_tokens]
710
+ cb(logits, "ffn_moe_logits", il);
711
+
712
+ ggml_tensor * probs = nullptr;
713
+ switch (gating_op) {
714
+ case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX:
715
+ {
716
+ probs = ggml_soft_max(ctx0, logits); // [n_expert, n_tokens]
717
+ } break;
718
+ case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID:
719
+ {
720
+ probs = ggml_sigmoid(ctx0, logits); // [n_expert, n_tokens]
721
+ } break;
722
+ default:
723
+ GGML_ABORT("fatal error");
724
+ }
725
+ cb(probs, "ffn_moe_probs", il);
726
+
727
+ // add experts selection bias - introduced in DeepSeek V3
728
+ // leave probs unbiased as it's later used to get expert weights
729
+ ggml_tensor * selection_probs = probs;
730
+ if (exp_probs_b != nullptr) {
731
+ selection_probs = ggml_add(ctx0, probs, exp_probs_b);
732
+ cb(selection_probs, "ffn_moe_probs_biased", il);
733
+ }
734
+
735
+ // llama4 doesn't have exp_probs_b, and sigmoid is only used after top_k
736
+ // see: https://github.com/meta-llama/llama-models/blob/699a02993512fb36936b1b0741e13c06790bcf98/models/llama4/moe.py#L183-L198
737
+ if (arch == LLM_ARCH_LLAMA4) {
738
+ selection_probs = logits;
739
+ }
740
+
741
+ // select experts
742
+ ggml_tensor * selected_experts = ggml_top_k(ctx0, selection_probs, n_expert_used); // [n_expert_used, n_tokens]
743
+ cb(selected_experts->src[0], "ffn_moe_argsort", il);
744
+ cb(selected_experts, "ffn_moe_topk", il);
745
+
746
+ ggml_tensor * weights = ggml_get_rows(ctx0,
747
+ ggml_reshape_3d(ctx0, probs, 1, n_expert, n_tokens), selected_experts); // [1, n_expert_used, n_tokens]
748
+ cb(weights, "ffn_moe_weights", il);
749
+
750
+ if (norm_w) {
751
+ weights = ggml_reshape_2d(ctx0, weights, n_expert_used, n_tokens);
752
+
753
+ ggml_tensor * weights_sum = ggml_sum_rows(ctx0, weights); // [1, n_tokens]
754
+ cb(weights_sum, "ffn_moe_weights_sum", il);
755
+
756
+ weights = ggml_div(ctx0, weights, weights_sum); // [n_expert_used, n_tokens]
757
+ cb(weights, "ffn_moe_weights_norm", il);
758
+
759
+ weights = ggml_reshape_3d(ctx0, weights, 1, n_expert_used, n_tokens);
760
+ }
761
+ if (scale_w) {
762
+ weights = ggml_scale(ctx0, weights, w_scale);
763
+ cb(weights, "ffn_moe_weights_scaled", il);
764
+ }
765
+
766
+ cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
767
+
768
+ if (weight_before_ffn) {
769
+ // TODO: this is a workaround as we don't yet have a repeat op that takes custom dim (ggml_repeat_4d)
770
+ ggml_tensor * repeated = ggml_new_tensor_3d(ctx0, cur->type, n_embd, n_expert_used, n_tokens);
771
+ repeated = ggml_repeat(ctx0, cur, repeated); // [n_embd, n_expert_used, n_tokens]
772
+ cur = ggml_mul(ctx0, repeated, weights);
773
+ cb(cur, "ffn_moe_weighted", il);
774
+ }
775
+
776
+ ggml_tensor * up = build_lora_mm_id(up_exps, cur, selected_experts); // [n_ff, n_expert_used, n_tokens]
777
+ cb(up, "ffn_moe_up", il);
778
+
779
+ ggml_tensor * experts = nullptr;
780
+ if (gate_exps) {
781
+ cur = build_lora_mm_id(gate_exps, cur, selected_experts); // [n_ff, n_expert_used, n_tokens]
782
+ cb(cur, "ffn_moe_gate", il);
783
+ } else {
784
+ cur = up;
785
+ }
786
+
787
+ switch (type_op) {
788
+ case LLM_FFN_SILU:
789
+ {
790
+ cur = ggml_silu(ctx0, cur);
791
+ cb(cur, "ffn_moe_silu", il);
792
+ } break;
793
+ case LLM_FFN_GELU:
794
+ {
795
+ cur = ggml_gelu(ctx0, cur);
796
+ cb(cur, "ffn_moe_gelu", il);
797
+ } break;
798
+ default:
799
+ GGML_ABORT("fatal error");
800
+ }
801
+
802
+ if (gate_exps) {
803
+ cur = ggml_mul(ctx0, cur, up); // [n_ff, n_expert_used, n_tokens]
804
+ cb(cur, "ffn_moe_gate_par", il);
805
+ }
806
+
807
+ experts = build_lora_mm_id(down_exps, cur, selected_experts); // [n_embd, n_expert_used, n_tokens]
808
+ cb(experts, "ffn_moe_down", il);
809
+
810
+ if (!weight_before_ffn) {
811
+ experts = ggml_mul(ctx0, experts, weights);
812
+ cb(cur, "ffn_moe_weighted", il);
813
+ }
814
+
815
+ // aggregate experts
816
+ ggml_tensor * moe_out = nullptr;
817
+ for (int i = 0; i < n_expert_used; ++i) {
818
+ ggml_tensor * cur_expert = ggml_view_2d(ctx0, experts, n_embd, n_tokens,
819
+ experts->nb[2], i*experts->nb[1]);
820
+
821
+ if (i == 0) {
822
+ moe_out = cur_expert;
823
+ } else {
824
+ moe_out = ggml_add(ctx0, moe_out, cur_expert);
825
+ }
826
+ }
827
+
828
+ if (n_expert_used == 1) {
829
+ // avoid returning a non-contiguous tensor
830
+ moe_out = ggml_cont(ctx0, moe_out);
831
+ }
832
+
833
+ cb(moe_out, "ffn_moe_out", il);
834
+
835
+ return moe_out;
836
+ }
837
+
838
+ // input embeddings with optional lora
839
+ ggml_tensor * llm_graph_context::build_inp_embd(ggml_tensor * tok_embd) const {
840
+ const int64_t n_embd = hparams.n_embd;
841
+
842
+ auto inp = std::make_unique<llm_graph_input_embd>();
843
+
844
+ ggml_tensor * cur = nullptr;
845
+
846
+ if (ubatch.token) {
847
+ inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
848
+ //cb(inp->tokens, "inp_tokens", -1);
849
+ ggml_set_input(inp->tokens);
850
+ res->t_tokens = inp->tokens;
851
+
852
+ cur = ggml_get_rows(ctx0, tok_embd, inp->tokens);
853
+
854
+ // apply lora for embedding tokens if needed
855
+ for (const auto & lora : *loras) {
856
+ llama_adapter_lora_weight * lw = lora.first->get_weight(tok_embd);
857
+ if (lw == nullptr) {
858
+ continue;
859
+ }
860
+
861
+ const float adapter_scale = lora.second;
862
+ const float scale = lw->get_scale(lora.first->alpha, adapter_scale);
863
+
864
+ ggml_tensor * inpL_delta = ggml_scale(ctx0, ggml_mul_mat(
865
+ ctx0, lw->b, // non-transposed lora_b
866
+ ggml_get_rows(ctx0, lw->a, inp->tokens)
867
+ ), scale);
868
+
869
+ cur = ggml_add(ctx0, cur, inpL_delta);
870
+ }
871
+ } else {
872
+ inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, ubatch.n_tokens);
873
+ ggml_set_input(inp->embd);
874
+
875
+ cur = inp->embd;
876
+ }
877
+
878
+ // For Granite architecture
879
+ if (hparams.f_embedding_scale != 0.0f) {
880
+ cur = ggml_scale(ctx0, cur, hparams.f_embedding_scale);
881
+ }
882
+
883
+ cb(cur, "inp_embd", -1);
884
+
885
+ res->add_input(std::move(inp));
886
+
887
+ return cur;
888
+ }
889
+
890
+ ggml_tensor * llm_graph_context::build_inp_pos() const {
891
+ auto inp = std::make_unique<llm_graph_input_pos>(n_pos_per_embd());
892
+
893
+ auto & cur = inp->pos;
894
+
895
+ cur = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens*n_pos_per_embd());
896
+ ggml_set_input(cur);
897
+
898
+ res->add_input(std::move(inp));
899
+
900
+ return cur;
901
+ }
902
+
903
+ ggml_tensor * llm_graph_context::build_inp_attn_scale() const {
904
+ auto inp = std::make_unique<llm_graph_input_attn_temp>(hparams.n_attn_temp_floor_scale, hparams.f_attn_temp_scale);
905
+
906
+ auto & cur = inp->attn_scale;
907
+
908
+ // this need to be 1x1xN for broadcasting
909
+ cur = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, 1, 1, n_tokens);
910
+ ggml_set_input(cur);
911
+
912
+ res->add_input(std::move(inp));
913
+
914
+ return cur;
915
+ }
916
+
917
+ ggml_tensor * llm_graph_context::build_inp_out_ids() const {
918
+ auto inp = std::make_unique<llm_graph_input_out_ids>(hparams, cparams, n_outputs);
919
+
920
+ auto & cur = inp->out_ids;
921
+
922
+ cur = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_outputs);
923
+ ggml_set_input(cur);
924
+
925
+ res->add_input(std::move(inp));
926
+
927
+ return cur;
928
+ }
929
+
930
+ ggml_tensor * llm_graph_context::build_inp_mean() const {
931
+ auto inp = std::make_unique<llm_graph_input_mean>(cparams);
932
+
933
+ auto & cur = inp->mean;
934
+
935
+ cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_tokens, n_tokens);
936
+ ggml_set_input(cur);
937
+
938
+ res->add_input(std::move(inp));
939
+
940
+ return cur;
941
+ }
942
+
943
+ ggml_tensor * llm_graph_context::build_inp_cls() const {
944
+ auto inp = std::make_unique<llm_graph_input_cls>(cparams);
945
+
946
+ auto & cur = inp->cls;
947
+
948
+ cur = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
949
+ ggml_set_input(cur);
950
+
951
+ res->add_input(std::move(inp));
952
+
953
+ return cur;
954
+ }
955
+
956
+ ggml_tensor * llm_graph_context::build_inp_s_copy() const {
957
+ const llama_kv_cache_recurrent * kv_self = static_cast<const llama_kv_cache_recurrent *>(memory);
958
+
959
+ auto inp = std::make_unique<llm_graph_input_s_copy>(kv_self);
960
+
961
+ const auto n_kv = kv_self->n;
962
+
963
+ auto & cur = inp->s_copy;
964
+
965
+ cur = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_kv);
966
+ ggml_set_input(cur);
967
+
968
+ res->add_input(std::move(inp));
969
+
970
+ return cur;
971
+ }
972
+
973
+ ggml_tensor * llm_graph_context::build_inp_s_mask() const {
974
+ const llama_kv_cache_recurrent * kv_self = static_cast<const llama_kv_cache_recurrent *>(memory);
975
+
976
+ auto inp = std::make_unique<llm_graph_input_s_mask>(kv_self);
977
+
978
+ const auto n_kv = kv_self->n;
979
+
980
+ auto & cur = inp->s_mask;
981
+
982
+ cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, 1, n_kv);
983
+ ggml_set_input(cur);
984
+
985
+ res->add_input(std::move(inp));
986
+
987
+ return cur;
988
+ }
989
+
990
+ ggml_tensor * llm_graph_context::build_inp_cross_embd() const {
991
+ auto inp = std::make_unique<llm_graph_input_cross_embd>(cross);
992
+
993
+ auto & cur = inp->cross_embd;
994
+
995
+ // if we have the output embeddings from the encoder, use them directly
996
+ // TODO: needs more work to be correct, for now just use the tensor shape
997
+ //if (cross->t_embd) {
998
+ // cur = ggml_view_tensor(ctx0, cross->t_embd);
999
+
1000
+ // return cur;
1001
+ //}
1002
+
1003
+ const auto n_embd = !cross->v_embd.empty() ? cross->n_embd : hparams.n_embd;
1004
+ const auto n_enc = !cross->v_embd.empty() ? cross->n_enc : hparams.n_ctx_train;
1005
+
1006
+ cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, n_enc);
1007
+ ggml_set_input(cur);
1008
+
1009
+ res->add_input(std::move(inp));
1010
+
1011
+ return cur;
1012
+ }
1013
+
1014
+ ggml_tensor * llm_graph_context::build_inp_pos_bucket_enc() const {
1015
+ auto inp = std::make_unique<llm_graph_input_pos_bucket>(hparams);
1016
+
1017
+ auto & cur = inp->pos_bucket;
1018
+
1019
+ cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, n_tokens, n_tokens);
1020
+ ggml_set_input(cur);
1021
+
1022
+ res->add_input(std::move(inp));
1023
+
1024
+ return cur;
1025
+ }
1026
+
1027
+ ggml_tensor * llm_graph_context::build_inp_pos_bucket_dec() const {
1028
+ const llama_kv_cache_unified * kv_self = static_cast<const llama_kv_cache_unified *>(memory);
1029
+
1030
+ auto inp = std::make_unique<llm_graph_input_pos_bucket_kv>(hparams, kv_self);
1031
+
1032
+ const auto n_kv = kv_self->get_n();
1033
+
1034
+ auto & cur = inp->pos_bucket;
1035
+
1036
+ cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, n_kv, n_tokens);
1037
+ ggml_set_input(cur);
1038
+
1039
+ res->add_input(std::move(inp));
1040
+
1041
+ return cur;
1042
+ }
1043
+
1044
+ ggml_tensor * llm_graph_context::build_pos_bias(ggml_tensor * pos_bucket, ggml_tensor * attn_rel_b) const {
1045
+ ggml_tensor * pos_bucket_1d = ggml_reshape_1d(ctx0, pos_bucket, pos_bucket->ne[0] * pos_bucket->ne[1]);
1046
+ cb(pos_bucket_1d, "pos_bucket_1d", -1);
1047
+
1048
+ ggml_tensor * pos_bias = ggml_get_rows(ctx0, attn_rel_b, pos_bucket_1d);
1049
+
1050
+ pos_bias = ggml_reshape_3d(ctx0, pos_bias, pos_bias->ne[0], pos_bucket->ne[0], pos_bucket->ne[1]);
1051
+ pos_bias = ggml_permute (ctx0, pos_bias, 2, 0, 1, 3);
1052
+ pos_bias = ggml_cont (ctx0, pos_bias);
1053
+
1054
+ cb(pos_bias, "pos_bias", -1);
1055
+
1056
+ return pos_bias;
1057
+ }
1058
+
1059
+ ggml_tensor * llm_graph_context::build_attn_mha(
1060
+ ggml_cgraph * gf,
1061
+ ggml_tensor * q,
1062
+ ggml_tensor * k,
1063
+ ggml_tensor * v,
1064
+ ggml_tensor * kq_b,
1065
+ ggml_tensor * kq_mask,
1066
+ ggml_tensor * v_mla,
1067
+ float kq_scale) const {
1068
+ const bool v_trans = v->nb[1] > v->nb[2];
1069
+
1070
+ q = ggml_permute(ctx0, q, 0, 2, 1, 3);
1071
+ k = ggml_permute(ctx0, k, 0, 2, 1, 3);
1072
+ v = ggml_permute(ctx0, v, 0, 2, 1, 3);
1073
+
1074
+ const auto n_tokens = q->ne[1];
1075
+ const auto n_head = q->ne[2];
1076
+ const auto n_kv = k->ne[1];
1077
+
1078
+ ggml_tensor * cur;
1079
+
1080
+ // TODO: replace hardcoded padding with ggml-provided padding
1081
+ if (cparams.flash_attn && (n_kv % 256 == 0) && kq_b == nullptr) {
1082
+ GGML_ASSERT(kq_b == nullptr && "Flash attention does not support KQ bias yet");
1083
+
1084
+ if (v_trans) {
1085
+ v = ggml_transpose(ctx0, v);
1086
+ }
1087
+
1088
+ // this can happen when KV cache is not used (e.g. an embedding model with non-causal attn)
1089
+ if (k->type == GGML_TYPE_F32) {
1090
+ k = ggml_cast(ctx0, k, GGML_TYPE_F16);
1091
+ }
1092
+
1093
+ if (v->type == GGML_TYPE_F32) {
1094
+ v = ggml_cast(ctx0, v, GGML_TYPE_F16);
1095
+ }
1096
+
1097
+ cur = ggml_flash_attn_ext(ctx0, q, k, v, kq_mask, kq_scale, hparams.f_max_alibi_bias,
1098
+ hparams.attn_soft_cap ? hparams.f_attn_logit_softcapping : 0.0f);
1099
+
1100
+ ggml_flash_attn_ext_set_prec(cur, GGML_PREC_F32);
1101
+
1102
+ if (v_mla) {
1103
+ #if 0
1104
+ // v_mla can be applied as a matrix-vector multiplication with broadcasting across dimension 3 == n_tokens.
1105
+ // However, the code is optimized for dimensions 0 and 1 being large, so this is ineffient.
1106
+ cur = ggml_reshape_4d(ctx0, cur, v_mla->ne[0], 1, n_head, n_tokens);
1107
+ cur = ggml_mul_mat(ctx0, v_mla, cur);
1108
+ #else
1109
+ // It's preferable to do the calculation as a matrix-matrix multiplication with n_tokens in dimension 1.
1110
+ // The permutations are noops and only change how the tensor data is interpreted.
1111
+ cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);
1112
+ cur = ggml_mul_mat(ctx0, v_mla, cur);
1113
+ cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);
1114
+ cur = ggml_cont(ctx0, cur); // Needed because ggml_reshape_2d expects contiguous inputs.
1115
+ #endif
1116
+ }
1117
+
1118
+ cur = ggml_reshape_2d(ctx0, cur, cur->ne[0]*n_head, n_tokens);
1119
+ } else {
1120
+ ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
1121
+
1122
+ // note: this op tends to require high floating point range
1123
+ // while for some models F16 is enough, for others it is not, so we default to F32 here
1124
+ ggml_mul_mat_set_prec(kq, GGML_PREC_F32);
1125
+
1126
+ if (arch == LLM_ARCH_GROK) {
1127
+ // need to do the following:
1128
+ // multiply by attn_output_multiplyer of 0.08838834764831845
1129
+ // and then :
1130
+ // kq = 30 * tanh(kq / 30)
1131
+ // before the softmax below
1132
+
1133
+ kq = ggml_tanh(ctx0, ggml_scale(ctx0, kq, 0.08838834764831845f/30.0f));
1134
+ kq = ggml_scale(ctx0, kq, 30);
1135
+ }
1136
+
1137
+ if (hparams.attn_soft_cap) {
1138
+ kq = ggml_scale(ctx0, kq, 1.0f / hparams.f_attn_logit_softcapping);
1139
+ kq = ggml_tanh (ctx0, kq);
1140
+ kq = ggml_scale(ctx0, kq, hparams.f_attn_logit_softcapping);
1141
+ }
1142
+
1143
+ if (kq_b) {
1144
+ kq = ggml_add(ctx0, kq, kq_b);
1145
+ }
1146
+
1147
+ kq = ggml_soft_max_ext(ctx0, kq, kq_mask, kq_scale, hparams.f_max_alibi_bias);
1148
+
1149
+ if (!v_trans) {
1150
+ // note: avoid this branch
1151
+ v = ggml_cont(ctx0, ggml_transpose(ctx0, v));
1152
+ }
1153
+
1154
+ ggml_tensor * kqv = ggml_mul_mat(ctx0, v, kq);
1155
+
1156
+ // for MLA with the absorption optimization, we need to "decompress" from MQA back to MHA
1157
+ if (v_mla) {
1158
+ kqv = ggml_mul_mat(ctx0, v_mla, kqv);
1159
+ }
1160
+
1161
+ cur = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
1162
+
1163
+ cur = ggml_cont_2d(ctx0, cur, cur->ne[0]*n_head, n_tokens);
1164
+
1165
+ if (!cparams.offload_kqv) {
1166
+ // all nodes between the KV store and the attention output are run on the CPU
1167
+ ggml_backend_sched_set_tensor_backend(sched, cur, backend_cpu);
1168
+ }
1169
+ }
1170
+
1171
+ ggml_build_forward_expand(gf, cur);
1172
+
1173
+ return cur;
1174
+ }
1175
+
1176
+ llm_graph_input_attn_no_cache * llm_graph_context::build_attn_inp_no_cache() const {
1177
+ auto inp = std::make_unique<llm_graph_input_attn_no_cache>(hparams, cparams);
1178
+
1179
+ // note: there is no KV cache, so the number of KV values is equal to the number of tokens in the batch
1180
+ inp->kq_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_tokens, GGML_PAD(n_tokens, GGML_KQ_MASK_PAD));
1181
+ //cb(inp_kq_mask, "KQ_mask", -1);
1182
+ ggml_set_input(inp->kq_mask);
1183
+
1184
+ inp->kq_mask_cnv = cparams.flash_attn ? ggml_cast(ctx0, inp->kq_mask, GGML_TYPE_F16) : inp->kq_mask;
1185
+
1186
+ return (llm_graph_input_attn_no_cache *) res->add_input(std::move(inp));
1187
+ }
1188
+
1189
+ ggml_tensor * llm_graph_context::build_attn(
1190
+ llm_graph_input_attn_no_cache * inp,
1191
+ ggml_cgraph * gf,
1192
+ ggml_tensor * wo,
1193
+ ggml_tensor * wo_b,
1194
+ ggml_tensor * q_cur,
1195
+ ggml_tensor * k_cur,
1196
+ ggml_tensor * v_cur,
1197
+ ggml_tensor * kq_b,
1198
+ ggml_tensor * v_mla,
1199
+ float kq_scale,
1200
+ int il) const {
1201
+ GGML_UNUSED(n_tokens);
1202
+
1203
+ // these nodes are added to the graph together so that they are not reordered
1204
+ // by doing so, the number of splits in the graph is reduced
1205
+ ggml_build_forward_expand(gf, q_cur);
1206
+ ggml_build_forward_expand(gf, k_cur);
1207
+ ggml_build_forward_expand(gf, v_cur);
1208
+
1209
+ const auto & kq_mask = inp->get_kq_mask();
1210
+
1211
+ ggml_tensor * q = q_cur;
1212
+ ggml_tensor * k = k_cur;
1213
+ ggml_tensor * v = v_cur;
1214
+
1215
+ ggml_tensor * cur = build_attn_mha(gf, q, k, v, kq_b, kq_mask, v_mla, kq_scale);
1216
+ cb(cur, "kqv_out", il);
1217
+
1218
+ if (wo) {
1219
+ cur = build_lora_mm(wo, cur);
1220
+ }
1221
+
1222
+ if (wo_b) {
1223
+ //cb(cur, "kqv_wo", il);
1224
+ }
1225
+
1226
+ if (wo_b) {
1227
+ cur = ggml_add(ctx0, cur, wo_b);
1228
+ }
1229
+
1230
+ return cur;
1231
+ }
1232
+
1233
+ llm_graph_input_attn_kv_unified * llm_graph_context::build_attn_inp_kv_unified() const {
1234
+ const llama_kv_cache_unified * kv_self = static_cast<const llama_kv_cache_unified *>(memory);
1235
+
1236
+ auto inp = std::make_unique<llm_graph_input_attn_kv_unified>(hparams, cparams, kv_self);
1237
+
1238
+ {
1239
+ GGML_ASSERT(hparams.swa_type == LLAMA_SWA_TYPE_NONE && "Use llama_kv_cache_unified_iswa for SWA");
1240
+
1241
+ const auto n_kv = kv_self->get_n();
1242
+
1243
+ inp->self_kq_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_kv, GGML_PAD(n_tokens, GGML_KQ_MASK_PAD));
1244
+ //cb(inp->self_kq_mask, "KQ_mask", -1);
1245
+ ggml_set_input(inp->self_kq_mask);
1246
+
1247
+ inp->self_kq_mask_cnv = cparams.flash_attn ? ggml_cast(ctx0, inp->self_kq_mask, GGML_TYPE_F16) : inp->self_kq_mask;
1248
+ }
1249
+
1250
+ return (llm_graph_input_attn_kv_unified *) res->add_input(std::move(inp));
1251
+ }
1252
+
1253
+ ggml_tensor * llm_graph_context::build_attn(
1254
+ llm_graph_input_attn_kv_unified * inp,
1255
+ ggml_cgraph * gf,
1256
+ ggml_tensor * wo,
1257
+ ggml_tensor * wo_b,
1258
+ ggml_tensor * q_cur,
1259
+ ggml_tensor * k_cur,
1260
+ ggml_tensor * v_cur,
1261
+ ggml_tensor * kq_b,
1262
+ ggml_tensor * v_mla,
1263
+ float kq_scale,
1264
+ int il) const {
1265
+ // these nodes are added to the graph together so that they are not reordered
1266
+ // by doing so, the number of splits in the graph is reduced
1267
+ ggml_build_forward_expand(gf, q_cur);
1268
+ ggml_build_forward_expand(gf, k_cur);
1269
+ ggml_build_forward_expand(gf, v_cur);
1270
+
1271
+ const llama_kv_cache_unified * kv_self = static_cast<const llama_kv_cache_unified *>(memory);
1272
+
1273
+ // store to KV cache
1274
+ {
1275
+ ggml_build_forward_expand(gf, kv_self->cpy_k(ctx0, k_cur, il));
1276
+ ggml_build_forward_expand(gf, kv_self->cpy_v(ctx0, v_cur, il));
1277
+ }
1278
+
1279
+ const auto & kq_mask = inp->get_kq_mask();
1280
+
1281
+ ggml_tensor * q = q_cur;
1282
+ ggml_tensor * k = kv_self->get_k(ctx0, il);
1283
+ ggml_tensor * v = kv_self->get_v(ctx0, il);
1284
+
1285
+ ggml_tensor * cur = build_attn_mha(gf, q, k, v, kq_b, kq_mask, v_mla, kq_scale);
1286
+ cb(cur, "kqv_out", il);
1287
+
1288
+ if (wo) {
1289
+ cur = build_lora_mm(wo, cur);
1290
+ if (arch == LLM_ARCH_GLM4) {
1291
+ // GLM4 seems to have numerical issues with half-precision accumulators
1292
+ ggml_mul_mat_set_prec(cur, GGML_PREC_F32);
1293
+ }
1294
+ }
1295
+
1296
+ if (wo_b) {
1297
+ cur = ggml_add(ctx0, cur, wo_b);
1298
+ }
1299
+
1300
+ return cur;
1301
+ }
1302
+
1303
+ llm_graph_input_attn_kv_unified_iswa * llm_graph_context::build_attn_inp_kv_unified_iswa() const {
1304
+ const llama_kv_cache_unified_iswa * kv_self = static_cast<const llama_kv_cache_unified_iswa *>(memory);
1305
+
1306
+ auto inp = std::make_unique<llm_graph_input_attn_kv_unified_iswa>(hparams, cparams, kv_self);
1307
+
1308
+ {
1309
+ const auto n_kv = kv_self->get_kv_base()->get_n();
1310
+
1311
+ inp->self_kq_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_kv, GGML_PAD(n_tokens, GGML_KQ_MASK_PAD));
1312
+ //cb(inp->self_kq_mask, "KQ_mask", -1);
1313
+ ggml_set_input(inp->self_kq_mask);
1314
+
1315
+ inp->self_kq_mask_cnv = cparams.flash_attn ? ggml_cast(ctx0, inp->self_kq_mask, GGML_TYPE_F16) : inp->self_kq_mask;
1316
+ }
1317
+
1318
+ {
1319
+ GGML_ASSERT(hparams.swa_type != LLAMA_SWA_TYPE_NONE && "Use llama_kv_cache_unified for non-SWA");
1320
+
1321
+ const auto n_kv = kv_self->get_kv_swa()->get_n();
1322
+
1323
+ inp->self_kq_mask_swa = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_kv, GGML_PAD(n_tokens, GGML_KQ_MASK_PAD));
1324
+ //cb(inp->self_kq_mask_swa, "KQ_mask_swa", -1);
1325
+ ggml_set_input(inp->self_kq_mask_swa);
1326
+
1327
+ inp->self_kq_mask_swa_cnv = cparams.flash_attn ? ggml_cast(ctx0, inp->self_kq_mask_swa, GGML_TYPE_F16) : inp->self_kq_mask_swa;
1328
+ }
1329
+
1330
+ return (llm_graph_input_attn_kv_unified_iswa *) res->add_input(std::move(inp));
1331
+ }
1332
+
1333
+ ggml_tensor * llm_graph_context::build_attn(
1334
+ llm_graph_input_attn_kv_unified_iswa * inp,
1335
+ ggml_cgraph * gf,
1336
+ ggml_tensor * wo,
1337
+ ggml_tensor * wo_b,
1338
+ ggml_tensor * q_cur,
1339
+ ggml_tensor * k_cur,
1340
+ ggml_tensor * v_cur,
1341
+ ggml_tensor * kq_b,
1342
+ ggml_tensor * v_mla,
1343
+ float kq_scale,
1344
+ int il) const {
1345
+ // these nodes are added to the graph together so that they are not reordered
1346
+ // by doing so, the number of splits in the graph is reduced
1347
+ ggml_build_forward_expand(gf, q_cur);
1348
+ ggml_build_forward_expand(gf, k_cur);
1349
+ ggml_build_forward_expand(gf, v_cur);
1350
+
1351
+ const bool is_swa = hparams.is_swa(il);
1352
+
1353
+ const llama_kv_cache_unified_iswa * kv_self = static_cast<const llama_kv_cache_unified_iswa *>(memory);
1354
+
1355
+ const auto * kv = is_swa ? kv_self->get_kv_swa() : kv_self->get_kv_base();
1356
+
1357
+ // store to KV cache
1358
+ {
1359
+ ggml_build_forward_expand(gf, kv->cpy_k(ctx0, k_cur, il));
1360
+ ggml_build_forward_expand(gf, kv->cpy_v(ctx0, v_cur, il));
1361
+ }
1362
+
1363
+ const auto & kq_mask = is_swa ? inp->get_kq_mask_swa() : inp->get_kq_mask();
1364
+
1365
+ ggml_tensor * q = q_cur;
1366
+ ggml_tensor * k = kv->get_k(ctx0, il);
1367
+ ggml_tensor * v = kv->get_v(ctx0, il);
1368
+
1369
+ ggml_tensor * cur = build_attn_mha(gf, q, k, v, kq_b, kq_mask, v_mla, kq_scale);
1370
+ cb(cur, "kqv_out", il);
1371
+
1372
+ if (wo) {
1373
+ cur = build_lora_mm(wo, cur);
1374
+ }
1375
+
1376
+ if (wo_b) {
1377
+ //cb(cur, "kqv_wo", il);
1378
+ }
1379
+
1380
+ if (wo_b) {
1381
+ cur = ggml_add(ctx0, cur, wo_b);
1382
+ }
1383
+
1384
+ return cur;
1385
+ }
1386
+
1387
+ llm_graph_input_attn_cross * llm_graph_context::build_attn_inp_cross() const {
1388
+ auto inp = std::make_unique<llm_graph_input_attn_cross>(cross);
1389
+
1390
+ const int32_t n_enc = !cross->v_embd.empty() ? cross->n_enc : hparams.n_ctx_train;
1391
+
1392
+ inp->cross_kq_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_enc, GGML_PAD(n_tokens, GGML_KQ_MASK_PAD));
1393
+ ggml_set_input(inp->cross_kq_mask);
1394
+
1395
+ inp->cross_kq_mask_cnv = cparams.flash_attn ? ggml_cast(ctx0, inp->cross_kq_mask, GGML_TYPE_F16) : inp->cross_kq_mask;
1396
+
1397
+ return (llm_graph_input_attn_cross *) res->add_input(std::move(inp));
1398
+ }
1399
+
1400
+ ggml_tensor * llm_graph_context::build_attn(
1401
+ llm_graph_input_attn_cross * inp,
1402
+ ggml_cgraph * gf,
1403
+ ggml_tensor * wo,
1404
+ ggml_tensor * wo_b,
1405
+ ggml_tensor * q_cur,
1406
+ ggml_tensor * k_cur,
1407
+ ggml_tensor * v_cur,
1408
+ ggml_tensor * kq_b,
1409
+ ggml_tensor * v_mla,
1410
+ float kq_scale,
1411
+ int il) const {
1412
+ // these nodes are added to the graph together so that they are not reordered
1413
+ // by doing so, the number of splits in the graph is reduced
1414
+ ggml_build_forward_expand(gf, q_cur);
1415
+ ggml_build_forward_expand(gf, k_cur);
1416
+ ggml_build_forward_expand(gf, v_cur);
1417
+
1418
+ const auto & kq_mask = inp->get_kq_mask_cross();
1419
+
1420
+ ggml_tensor * q = q_cur;
1421
+ ggml_tensor * k = k_cur;
1422
+ ggml_tensor * v = v_cur;
1423
+
1424
+ ggml_tensor * cur = build_attn_mha(gf, q, k, v, kq_b, kq_mask, v_mla, kq_scale);
1425
+ cb(cur, "kqv_out", il);
1426
+
1427
+ if (wo) {
1428
+ cur = build_lora_mm(wo, cur);
1429
+ }
1430
+
1431
+ if (wo_b) {
1432
+ //cb(cur, "kqv_wo", il);
1433
+ }
1434
+
1435
+ if (wo_b) {
1436
+ cur = ggml_add(ctx0, cur, wo_b);
1437
+ }
1438
+
1439
+ return cur;
1440
+ }
1441
+
1442
+ ggml_tensor * llm_graph_context::build_copy_mask_state(
1443
+ ggml_cgraph * gf,
1444
+ ggml_tensor * s,
1445
+ ggml_tensor * state_copy,
1446
+ ggml_tensor * state_mask,
1447
+ int32_t n_state,
1448
+ int32_t n_seqs) const {
1449
+ const llama_kv_cache_recurrent * kv_self = static_cast<const llama_kv_cache_recurrent *>(memory);
1450
+
1451
+ const auto n_kv = kv_self->n;
1452
+ const auto kv_head = kv_self->head;
1453
+
1454
+ ggml_tensor * states = ggml_reshape_2d(ctx0, s, n_state, kv_self->size);
1455
+
1456
+ // copy states
1457
+ // NOTE: assuming the copy destinations are ALL contained between kv_head and kv_head + n_kv
1458
+ // this shrinks the tensors's ne[1] to n_kv
1459
+ states = ggml_get_rows(ctx0, states, state_copy);
1460
+
1461
+ // clear states of sequences which are starting at the beginning of this batch
1462
+ // FIXME: zero-out NANs?
1463
+ states = ggml_mul(ctx0, states, state_mask);
1464
+
1465
+ // copy states which won't be changed further (between n_seqs and n_kv)
1466
+ ggml_build_forward_expand(gf,
1467
+ ggml_cpy(ctx0,
1468
+ ggml_view_1d(ctx0, states, n_state*(n_kv - n_seqs), (n_seqs )*n_state*ggml_element_size(states)),
1469
+ ggml_view_1d(ctx0, s, n_state*(n_kv - n_seqs), (kv_head + n_seqs)*n_state*ggml_element_size(s))));
1470
+
1471
+ // the part of the states that will be used and modified
1472
+ return ggml_view_2d(ctx0, states, n_state, n_seqs, states->nb[1], 0);
1473
+ }
1474
+
1475
+ ggml_tensor * llm_graph_context::build_rwkv_token_shift_load(
1476
+ ggml_cgraph * gf,
1477
+ ggml_tensor * state_copy,
1478
+ ggml_tensor * state_mask,
1479
+ const llama_ubatch & ubatch,
1480
+ int il) const {
1481
+ const llama_kv_cache_recurrent * kv_self = static_cast<const llama_kv_cache_recurrent *>(memory);
1482
+
1483
+ const auto token_shift_count = hparams.token_shift_count;
1484
+
1485
+ const int64_t n_seqs = ubatch.n_seqs;
1486
+
1487
+ ggml_tensor * token_shift_all = kv_self->k_l[il];
1488
+
1489
+ ggml_tensor * token_shift = build_copy_mask_state(
1490
+ gf, token_shift_all, state_copy, state_mask,
1491
+ hparams.n_embd_k_s(), n_seqs);
1492
+
1493
+ token_shift = ggml_reshape_3d(ctx0, token_shift, hparams.n_embd, token_shift_count, n_seqs);
1494
+
1495
+ return token_shift;
1496
+ }
1497
+
1498
+ ggml_tensor * llm_graph_context::build_rwkv_token_shift_store(
1499
+ ggml_tensor * token_shift,
1500
+ const llama_ubatch & ubatch,
1501
+ int il) const {
1502
+ const llama_kv_cache_recurrent * kv_self = static_cast<const llama_kv_cache_recurrent *>(memory);
1503
+
1504
+ const auto token_shift_count = hparams.token_shift_count;
1505
+ const auto n_embd = hparams.n_embd;
1506
+
1507
+ const int64_t n_seqs = ubatch.n_seqs;
1508
+
1509
+ const auto kv_head = kv_self->head;
1510
+
1511
+ return ggml_cpy(
1512
+ ctx0,
1513
+ ggml_view_1d(ctx0, token_shift, n_embd * n_seqs * token_shift_count, 0),
1514
+ ggml_view_1d(ctx0, kv_self->k_l[il], hparams.n_embd_k_s() * n_seqs, hparams.n_embd_k_s() * kv_head * ggml_element_size(kv_self->k_l[il]))
1515
+ );
1516
+ }
1517
+
1518
+ void llm_graph_context::build_pooling(
1519
+ ggml_cgraph * gf,
1520
+ ggml_tensor * cls,
1521
+ ggml_tensor * cls_b,
1522
+ ggml_tensor * cls_out,
1523
+ ggml_tensor * cls_out_b) const {
1524
+ if (!cparams.embeddings) {
1525
+ return;
1526
+ }
1527
+
1528
+ ggml_tensor * inp = res->t_embd;
1529
+
1530
+ //// find result_norm tensor for input
1531
+ //for (int i = ggml_graph_n_nodes(gf) - 1; i >= 0; --i) {
1532
+ // inp = ggml_graph_node(gf, i);
1533
+ // if (strcmp(inp->name, "result_norm") == 0 || strcmp(inp->name, "result_embd") == 0) {
1534
+ // break;
1535
+ // }
1536
+
1537
+ // inp = nullptr;
1538
+ //}
1539
+
1540
+ GGML_ASSERT(inp != nullptr && "missing result_norm/result_embd tensor");
1541
+
1542
+ ggml_tensor * cur;
1543
+
1544
+ switch (pooling_type) {
1545
+ case LLAMA_POOLING_TYPE_NONE:
1546
+ {
1547
+ cur = inp;
1548
+ } break;
1549
+ case LLAMA_POOLING_TYPE_MEAN:
1550
+ {
1551
+ ggml_tensor * inp_mean = build_inp_mean();
1552
+ cur = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, inp)), inp_mean);
1553
+ } break;
1554
+ case LLAMA_POOLING_TYPE_CLS:
1555
+ case LLAMA_POOLING_TYPE_LAST:
1556
+ {
1557
+ ggml_tensor * inp_cls = build_inp_cls();
1558
+ cur = ggml_get_rows(ctx0, inp, inp_cls);
1559
+ } break;
1560
+ case LLAMA_POOLING_TYPE_RANK:
1561
+ {
1562
+ ggml_tensor * inp_cls = build_inp_cls();
1563
+ inp = ggml_get_rows(ctx0, inp, inp_cls);
1564
+
1565
+ // classification head
1566
+ // https://github.com/huggingface/transformers/blob/5af7d41e49bbfc8319f462eb45253dcb3863dfb7/src/transformers/models/roberta/modeling_roberta.py#L1566
1567
+ GGML_ASSERT(cls != nullptr);
1568
+ GGML_ASSERT(cls_b != nullptr);
1569
+
1570
+ cur = ggml_add (ctx0, ggml_mul_mat(ctx0, cls, inp), cls_b);
1571
+ cur = ggml_tanh(ctx0, cur);
1572
+
1573
+ // some models don't have `cls_out`, for example: https://huggingface.co/jinaai/jina-reranker-v1-tiny-en
1574
+ // https://huggingface.co/jinaai/jina-reranker-v1-tiny-en/blob/cb5347e43979c3084a890e3f99491952603ae1b7/modeling_bert.py#L884-L896
1575
+ if (cls_out) {
1576
+ GGML_ASSERT(cls_out_b != nullptr);
1577
+
1578
+ cur = ggml_add (ctx0, ggml_mul_mat(ctx0, cls_out, cur), cls_out_b);
1579
+ }
1580
+ } break;
1581
+ default:
1582
+ {
1583
+ GGML_ABORT("unknown pooling type");
1584
+ }
1585
+ }
1586
+
1587
+ cb(cur, "result_embd_pooled", -1);
1588
+ res->t_embd_pooled = cur;
1589
+
1590
+ ggml_build_forward_expand(gf, cur);
1591
+ }
1592
+
1593
+ int32_t llama_relative_position_bucket(llama_pos x, llama_pos y, uint64_t n_buckets, bool bidirectional) {
1594
+ // TODO move to hparams if a T5 variant appears that uses a different value
1595
+ const int64_t max_distance = 128;
1596
+
1597
+ if (bidirectional) {
1598
+ n_buckets >>= 1;
1599
+ }
1600
+
1601
+ const int64_t max_exact = n_buckets >> 1;
1602
+
1603
+ int32_t relative_position = x - y;
1604
+ int32_t relative_bucket = 0;
1605
+
1606
+ if (bidirectional) {
1607
+ relative_bucket += (relative_position > 0) * n_buckets;
1608
+ relative_position = abs(relative_position);
1609
+ } else {
1610
+ relative_position = -std::min<int32_t>(relative_position, 0);
1611
+ }
1612
+
1613
+ int32_t relative_position_if_large = floorf(max_exact + logf(1.0 * relative_position / max_exact) * (n_buckets - max_exact) / log(1.0 * max_distance / max_exact));
1614
+ relative_position_if_large = std::min<int32_t>(relative_position_if_large, n_buckets - 1);
1615
+ relative_bucket += (relative_position < max_exact ? relative_position : relative_position_if_large);
1616
+
1617
+ return relative_bucket;
1618
+ }