whispercpp 1.3.6 → 1.3.8

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 (965) hide show
  1. checksums.yaml +4 -4
  2. data/.document +3 -0
  3. data/.rdoc_options +2 -0
  4. data/README.md +43 -9
  5. data/Rakefile +18 -3
  6. data/ext/dependencies.rb +10 -4
  7. data/ext/dependencies_for_windows.rb +17 -0
  8. data/ext/extconf.rb +20 -8
  9. data/ext/options.rb +54 -14
  10. data/ext/options_for_windows.rb +51 -0
  11. data/ext/ruby_whisper.c +35 -42
  12. data/ext/ruby_whisper.h +141 -0
  13. data/ext/ruby_whisper_context.c +157 -29
  14. data/ext/ruby_whisper_log_queue.c +180 -0
  15. data/ext/ruby_whisper_log_settable.h +46 -0
  16. data/ext/ruby_whisper_parakeet.c +49 -0
  17. data/ext/ruby_whisper_parakeet_context.c +304 -0
  18. data/ext/ruby_whisper_parakeet_context_params.c +117 -0
  19. data/ext/ruby_whisper_parakeet_model.c +84 -0
  20. data/ext/ruby_whisper_parakeet_params.c +548 -0
  21. data/ext/ruby_whisper_parakeet_segment.c +157 -0
  22. data/ext/ruby_whisper_parakeet_token.c +188 -0
  23. data/ext/ruby_whisper_parakeet_transcribe.cpp +58 -0
  24. data/ext/ruby_whisper_params.c +265 -73
  25. data/ext/ruby_whisper_segment.c +6 -6
  26. data/ext/ruby_whisper_transcribe.cpp +23 -15
  27. data/ext/ruby_whisper_vad_context.c +30 -10
  28. data/ext/ruby_whisper_vad_context_detect.cpp +8 -9
  29. data/ext/ruby_whisper_vad_params.c +4 -4
  30. data/ext/ruby_whisper_vad_segment.c +2 -2
  31. data/ext/sources/CMakeLists.txt +42 -3
  32. data/ext/sources/CMakePresets.json +95 -0
  33. data/ext/sources/cmake/parakeet-config.cmake.in +30 -0
  34. data/ext/sources/cmake/parakeet.pc.in +10 -0
  35. data/ext/sources/cmake/whisper.pc.in +2 -2
  36. data/ext/sources/examples/CMakeLists.txt +4 -2
  37. data/ext/sources/examples/bench/bench.cpp +1 -1
  38. data/ext/sources/examples/cli/cli.cpp +52 -10
  39. data/ext/sources/examples/common-ggml.cpp +4 -0
  40. data/ext/sources/examples/common-whisper.cpp +139 -67
  41. data/ext/sources/examples/common-whisper.h +11 -0
  42. data/ext/sources/examples/ffmpeg-transcode.cpp +211 -341
  43. data/ext/sources/examples/parakeet-cli/CMakeLists.txt +8 -0
  44. data/ext/sources/examples/parakeet-cli/parakeet-cli.cpp +243 -0
  45. data/ext/sources/examples/parakeet-quantize/CMakeLists.txt +7 -0
  46. data/ext/sources/examples/parakeet-quantize/parakeet-quantize.cpp +230 -0
  47. data/ext/sources/examples/server/server.cpp +199 -163
  48. data/ext/sources/examples/vad-speech-segments/speech.cpp +3 -2
  49. data/ext/sources/ggml/CMakeLists.txt +21 -14
  50. data/ext/sources/ggml/cmake/FindNCCL.cmake +36 -0
  51. data/ext/sources/ggml/cmake/ggml-config.cmake.in +12 -2
  52. data/ext/sources/ggml/include/ggml-alloc.h +1 -0
  53. data/ext/sources/ggml/include/ggml-backend.h +72 -10
  54. data/ext/sources/ggml/include/ggml-cuda.h +2 -2
  55. data/ext/sources/ggml/include/ggml-rpc.h +3 -3
  56. data/ext/sources/ggml/include/ggml-sycl.h +8 -0
  57. data/ext/sources/ggml/include/ggml.h +103 -9
  58. data/ext/sources/ggml/include/gguf.h +10 -2
  59. data/ext/sources/ggml/src/CMakeLists.txt +30 -6
  60. data/ext/sources/ggml/src/ggml-alloc.c +5 -1
  61. data/ext/sources/ggml/src/ggml-backend-impl.h +22 -2
  62. data/ext/sources/ggml/src/ggml-backend-meta.cpp +2266 -0
  63. data/ext/sources/ggml/src/ggml-backend-reg.cpp +12 -0
  64. data/ext/sources/ggml/src/ggml-backend.cpp +110 -9
  65. data/ext/sources/ggml/src/ggml-blas/ggml-blas.cpp +4 -0
  66. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.cpp +672 -257
  67. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.h +71 -0
  68. data/ext/sources/ggml/src/ggml-cann/common.h +20 -10
  69. data/ext/sources/ggml/src/ggml-cann/ggml-cann.cpp +211 -30
  70. data/ext/sources/ggml/src/ggml-common.h +24 -2
  71. data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +59 -30
  72. data/ext/sources/ggml/src/ggml-cpu/amx/amx.cpp +2 -0
  73. data/ext/sources/ggml/src/ggml-cpu/amx/mmq.cpp +21 -22
  74. data/ext/sources/ggml/src/ggml-cpu/arch/arm/quants.c +194 -11
  75. data/ext/sources/ggml/src/ggml-cpu/arch/arm/repack.cpp +65 -0
  76. data/ext/sources/ggml/src/ggml-cpu/arch/loongarch/quants.c +151 -1
  77. data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/quants.c +0 -1
  78. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/quants.c +4279 -1292
  79. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/repack.cpp +5 -35
  80. data/ext/sources/ggml/src/ggml-cpu/arch/s390/quants.c +0 -1
  81. data/ext/sources/ggml/src/ggml-cpu/arch/wasm/quants.c +72 -1
  82. data/ext/sources/ggml/src/ggml-cpu/arch/x86/quants.c +319 -31
  83. data/ext/sources/ggml/src/ggml-cpu/arch/x86/repack.cpp +1 -1
  84. data/ext/sources/ggml/src/ggml-cpu/arch-fallback.h +12 -2
  85. data/ext/sources/ggml/src/ggml-cpu/cmake/FindSMTIME.cmake +32 -0
  86. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-impl.h +10 -0
  87. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +109 -5
  88. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.cpp +2 -0
  89. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +146 -134
  90. data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +107 -82
  91. data/ext/sources/ggml/src/ggml-cpu/ops.cpp +501 -119
  92. data/ext/sources/ggml/src/ggml-cpu/ops.h +3 -0
  93. data/ext/sources/ggml/src/ggml-cpu/quants.c +106 -0
  94. data/ext/sources/ggml/src/ggml-cpu/quants.h +6 -0
  95. data/ext/sources/ggml/src/ggml-cpu/repack.cpp +3 -0
  96. data/ext/sources/ggml/src/ggml-cpu/simd-gemm.h +91 -1
  97. data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +14 -16
  98. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.cpp +1402 -687
  99. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.h +8 -0
  100. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +597 -2766
  101. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime2_kernels.cpp +5768 -0
  102. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_env.cpp +320 -0
  103. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_env.h +55 -0
  104. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_kernels.h +182 -19
  105. data/ext/sources/ggml/src/ggml-cpu/spacemit/repack.cpp +1795 -0
  106. data/ext/sources/ggml/src/ggml-cpu/spacemit/repack.h +14 -0
  107. data/ext/sources/ggml/src/ggml-cpu/spacemit/rvv_kernels.cpp +3178 -0
  108. data/ext/sources/ggml/src/ggml-cpu/spacemit/rvv_kernels.h +95 -0
  109. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_barrier.h +34 -0
  110. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_mem_pool.cpp +760 -0
  111. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_mem_pool.h +32 -0
  112. data/ext/sources/ggml/src/ggml-cpu/spacemit/spine_tcm.h +409 -0
  113. data/ext/sources/ggml/src/ggml-cpu/vec.cpp +39 -55
  114. data/ext/sources/ggml/src/ggml-cpu/vec.h +225 -240
  115. data/ext/sources/ggml/src/ggml-cuda/CMakeLists.txt +17 -7
  116. data/ext/sources/ggml/src/ggml-cuda/allreduce.cu +971 -0
  117. data/ext/sources/ggml/src/ggml-cuda/allreduce.cuh +29 -0
  118. data/ext/sources/ggml/src/ggml-cuda/argsort.cu +62 -26
  119. data/ext/sources/ggml/src/ggml-cuda/binbcast.cu +134 -64
  120. data/ext/sources/ggml/src/ggml-cuda/binbcast.cuh +1 -0
  121. data/ext/sources/ggml/src/ggml-cuda/col2im-1d.cu +81 -0
  122. data/ext/sources/ggml/src/ggml-cuda/col2im-1d.cuh +3 -0
  123. data/ext/sources/ggml/src/ggml-cuda/common.cuh +246 -28
  124. data/ext/sources/ggml/src/ggml-cuda/concat.cu +134 -116
  125. data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cu +14 -12
  126. data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cu +45 -21
  127. data/ext/sources/ggml/src/ggml-cuda/conv2d-transpose.cuh +1 -0
  128. data/ext/sources/ggml/src/ggml-cuda/convert.cu +139 -34
  129. data/ext/sources/ggml/src/ggml-cuda/convert.cuh +10 -0
  130. data/ext/sources/ggml/src/ggml-cuda/cpy.cu +88 -29
  131. data/ext/sources/ggml/src/ggml-cuda/dequantize.cuh +22 -0
  132. data/ext/sources/ggml/src/ggml-cuda/fattn-common.cuh +287 -49
  133. data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +335 -130
  134. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cu +12 -0
  135. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cuh +127 -24
  136. data/ext/sources/ggml/src/ggml-cuda/fattn-vec.cuh +40 -15
  137. data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cu +18 -9
  138. data/ext/sources/ggml/src/ggml-cuda/fattn.cu +169 -60
  139. data/ext/sources/ggml/src/ggml-cuda/fattn.cuh +2 -0
  140. data/ext/sources/ggml/src/ggml-cuda/fwht.cu +101 -0
  141. data/ext/sources/ggml/src/ggml-cuda/fwht.cuh +4 -0
  142. data/ext/sources/ggml/src/ggml-cuda/gated_delta_net.cu +109 -45
  143. data/ext/sources/ggml/src/ggml-cuda/gated_delta_net.cuh +10 -0
  144. data/ext/sources/ggml/src/ggml-cuda/getrows.cu +48 -23
  145. data/ext/sources/ggml/src/ggml-cuda/ggml-cuda.cu +2034 -2104
  146. data/ext/sources/ggml/src/ggml-cuda/im2col.cu +32 -29
  147. data/ext/sources/ggml/src/ggml-cuda/mean.cu +4 -2
  148. data/ext/sources/ggml/src/ggml-cuda/mma.cuh +242 -195
  149. data/ext/sources/ggml/src/ggml-cuda/mmf.cuh +3 -3
  150. data/ext/sources/ggml/src/ggml-cuda/mmq.cu +25 -12
  151. data/ext/sources/ggml/src/ggml-cuda/mmq.cuh +502 -423
  152. data/ext/sources/ggml/src/ggml-cuda/mmvf.cu +19 -12
  153. data/ext/sources/ggml/src/ggml-cuda/mmvq.cu +562 -97
  154. data/ext/sources/ggml/src/ggml-cuda/mmvq.cuh +6 -1
  155. data/ext/sources/ggml/src/ggml-cuda/norm.cu +36 -10
  156. data/ext/sources/ggml/src/ggml-cuda/out-prod.cu +66 -7
  157. data/ext/sources/ggml/src/ggml-cuda/quantize.cu +133 -26
  158. data/ext/sources/ggml/src/ggml-cuda/quantize.cuh +1 -1
  159. data/ext/sources/ggml/src/ggml-cuda/reduce_rows.cuh +5 -1
  160. data/ext/sources/ggml/src/ggml-cuda/rope.cu +11 -4
  161. data/ext/sources/ggml/src/ggml-cuda/scale.cu +4 -1
  162. data/ext/sources/ggml/src/ggml-cuda/set-rows.cu +78 -10
  163. data/ext/sources/ggml/src/ggml-cuda/snake.cu +72 -0
  164. data/ext/sources/ggml/src/ggml-cuda/snake.cuh +8 -0
  165. data/ext/sources/ggml/src/ggml-cuda/softcap.cu +4 -1
  166. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cu +45 -13
  167. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cuh +1 -1
  168. data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cu +40 -18
  169. data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +8 -4
  170. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_16.cu +1 -0
  171. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_32.cu +1 -0
  172. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_8.cu +2 -0
  173. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu +1 -0
  174. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_4.cu +1 -0
  175. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_16.cu +1 -0
  176. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_32.cu +1 -0
  177. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_4.cu +1 -0
  178. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_8.cu +2 -0
  179. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu +1 -0
  180. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_16.cu +1 -0
  181. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu +1 -0
  182. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_4.cu +1 -0
  183. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_8.cu +2 -0
  184. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu +1 -0
  185. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_4.cu +1 -0
  186. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_8.cu +2 -0
  187. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq192-dv128.cu +5 -0
  188. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq320-dv256.cu +5 -0
  189. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq512-dv512.cu +5 -0
  190. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-bf16.cu +7 -0
  191. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-f16.cu +7 -0
  192. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_0.cu +7 -0
  193. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_1.cu +7 -0
  194. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_0.cu +7 -0
  195. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_1.cu +7 -0
  196. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q8_0.cu +7 -0
  197. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-bf16.cu +7 -0
  198. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-bf16.cu +7 -0
  199. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-bf16.cu +7 -0
  200. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-bf16.cu +7 -0
  201. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-bf16.cu +7 -0
  202. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-bf16.cu +7 -0
  203. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-nvfp4.cu +5 -0
  204. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-q1_0.cu +5 -0
  205. data/ext/sources/ggml/src/ggml-cuda/top-k.cu +5 -4
  206. data/ext/sources/ggml/src/ggml-cuda/topk-moe.cu +33 -24
  207. data/ext/sources/ggml/src/ggml-cuda/unary.cu +31 -2
  208. data/ext/sources/ggml/src/ggml-cuda/unary.cuh +2 -0
  209. data/ext/sources/ggml/src/ggml-cuda/vecdotq.cuh +80 -0
  210. data/ext/sources/ggml/src/ggml-cuda/vendors/cuda.h +7 -2
  211. data/ext/sources/ggml/src/ggml-cuda/vendors/hip.h +23 -4
  212. data/ext/sources/ggml/src/ggml-cuda/vendors/musa.h +4 -0
  213. data/ext/sources/ggml/src/ggml-hexagon/CMakeLists.txt +1 -5
  214. data/ext/sources/ggml/src/ggml-hexagon/ggml-hexagon.cpp +2788 -1762
  215. data/ext/sources/ggml/src/ggml-hexagon/htp/CMakeLists.txt +13 -4
  216. data/ext/sources/ggml/src/ggml-hexagon/htp/act-ops.c +53 -84
  217. data/ext/sources/ggml/src/ggml-hexagon/htp/argsort-ops.c +25 -12
  218. data/ext/sources/ggml/src/ggml-hexagon/htp/binary-ops.c +165 -184
  219. data/ext/sources/ggml/src/ggml-hexagon/htp/cmake-toolchain.cmake +17 -19
  220. data/ext/sources/ggml/src/ggml-hexagon/htp/concat-ops.c +277 -0
  221. data/ext/sources/ggml/src/ggml-hexagon/htp/cpy-ops.c +170 -127
  222. data/ext/sources/ggml/src/ggml-hexagon/htp/cumsum-ops.c +270 -0
  223. data/ext/sources/ggml/src/ggml-hexagon/htp/diag-ops.c +216 -0
  224. data/ext/sources/ggml/src/ggml-hexagon/htp/fill-ops.c +123 -0
  225. data/ext/sources/ggml/src/ggml-hexagon/htp/flash-attn-ops.c +1774 -396
  226. data/ext/sources/ggml/src/ggml-hexagon/htp/flash-attn-ops.h +303 -0
  227. data/ext/sources/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.c +1148 -0
  228. data/ext/sources/ggml/src/ggml-hexagon/htp/get-rows-ops.c +148 -42
  229. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-common.h +80 -0
  230. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dma.c +2 -2
  231. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dma.h +255 -62
  232. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-dump.h +9 -0
  233. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-profile.h +64 -0
  234. data/ext/sources/ggml/src/ggml-hexagon/htp/hex-utils.h +25 -21
  235. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-fa-kernels.h +555 -0
  236. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-mm-kernels-tiled.h +1303 -0
  237. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-queue.c +167 -0
  238. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-queue.h +157 -0
  239. data/ext/sources/ggml/src/ggml-hexagon/htp/hmx-utils.h +222 -0
  240. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-ctx.h +104 -13
  241. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-ops.h +222 -57
  242. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-vtcm.h +19 -0
  243. data/ext/sources/ggml/src/ggml-hexagon/htp/htp_iface.idl +10 -3
  244. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-base.h +78 -26
  245. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-copy.h +27 -10
  246. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-div.h +63 -23
  247. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-exp.h +48 -8
  248. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-fa-kernels.h +232 -0
  249. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-flash-attn.h +47 -0
  250. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-log.h +65 -0
  251. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-mm-kernels-flat.h +1511 -0
  252. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-mm-kernels-tiled.h +1200 -0
  253. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-pow.h +42 -0
  254. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-repl.h +74 -0
  255. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sigmoid.h +40 -0
  256. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-sin-cos.h +90 -0
  257. data/ext/sources/ggml/src/ggml-hexagon/htp/hvx-utils.h +5 -8
  258. data/ext/sources/ggml/src/ggml-hexagon/htp/main.c +625 -816
  259. data/ext/sources/ggml/src/ggml-hexagon/htp/matmul-ops.c +3052 -2166
  260. data/ext/sources/ggml/src/ggml-hexagon/htp/matmul-ops.h +650 -0
  261. data/ext/sources/ggml/src/ggml-hexagon/htp/pad-ops.c +547 -0
  262. data/ext/sources/ggml/src/ggml-hexagon/htp/repeat-ops.c +148 -0
  263. data/ext/sources/ggml/src/ggml-hexagon/htp/rope-ops.c +337 -106
  264. data/ext/sources/ggml/src/ggml-hexagon/htp/set-rows-ops.c +59 -37
  265. data/ext/sources/ggml/src/ggml-hexagon/htp/softmax-ops.c +121 -133
  266. data/ext/sources/ggml/src/ggml-hexagon/htp/solve-tri-ops.c +267 -0
  267. data/ext/sources/ggml/src/ggml-hexagon/htp/ssm-conv.c +245 -151
  268. data/ext/sources/ggml/src/ggml-hexagon/htp/sum-rows-ops.c +6 -6
  269. data/ext/sources/ggml/src/ggml-hexagon/htp/unary-ops.c +719 -45
  270. data/ext/sources/ggml/src/ggml-hexagon/htp/worker-pool.c +15 -3
  271. data/ext/sources/ggml/src/ggml-hexagon/htp/worker-pool.h +8 -0
  272. data/ext/sources/ggml/src/ggml-hexagon/htp-opnode.h +390 -0
  273. data/ext/sources/ggml/src/ggml-hexagon/libggml-htp.inf +3 -5
  274. data/ext/sources/ggml/src/ggml-hip/CMakeLists.txt +27 -9
  275. data/ext/sources/ggml/src/ggml-impl.h +6 -1
  276. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.cpp +207 -18
  277. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.h +36 -2
  278. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.m +186 -29
  279. data/ext/sources/ggml/src/ggml-metal/ggml-metal-impl.h +118 -0
  280. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.cpp +322 -21
  281. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.h +4 -0
  282. data/ext/sources/ggml/src/ggml-metal/ggml-metal.cpp +39 -26
  283. data/ext/sources/ggml/src/ggml-metal/ggml-metal.metal +1226 -467
  284. data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +5 -6
  285. data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +67 -5
  286. data/ext/sources/ggml/src/ggml-opencl/fa_tune.h +92 -0
  287. data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +16290 -6246
  288. data/ext/sources/ggml/src/ggml-opencl/kernels/concat.cl +67 -0
  289. data/ext/sources/ggml/src/ggml-opencl/kernels/cpy.cl +59 -0
  290. data/ext/sources/ggml/src/ggml-opencl/kernels/cvt.cl +1997 -92
  291. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f16.cl +81 -41
  292. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32.cl +88 -39
  293. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32_f16.cl +1995 -96
  294. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32_q4_0.cl +1615 -0
  295. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32_q8_0.cl +1486 -0
  296. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_pre_f16.cl +156 -0
  297. data/ext/sources/ggml/src/ggml-opencl/kernels/gated_delta_net.cl +249 -0
  298. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32_ns.cl +374 -0
  299. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_0_f32_ns.cl +324 -0
  300. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_1_f32_ns.cl +326 -0
  301. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q4_k_f32_ns.cl +348 -0
  302. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_0_f32_ns.cl +328 -0
  303. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_1_f32_ns.cl +330 -0
  304. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q5_k_f32_ns.cl +356 -0
  305. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_moe_q6_k_f32_ns.cl +335 -0
  306. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_iq4_nl_f32.cl +150 -0
  307. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q1_0_f32.cl +94 -0
  308. data/ext/sources/ggml/src/ggml-opencl/kernels/{mul_mat_Ab_Bi_8x4.cl → gemm_noshuffle_q4_0_f32.cl} +1 -1
  309. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q4_k_f32.cl +172 -0
  310. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_0_f32.cl +131 -0
  311. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_1_f32.cl +134 -0
  312. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q5_k_f32.cl +176 -0
  313. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_noshuffle_q6_k_f32.cl +140 -0
  314. data/ext/sources/ggml/src/ggml-opencl/kernels/{mul_mm_q8_0_f32_8x4.cl → gemm_noshuffle_q8_0_f32.cl} +1 -1
  315. data/ext/sources/ggml/src/ggml-opencl/kernels/gemm_xmem_f16_f32_os8.cl +233 -0
  316. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_mxfp4_f32_ns.cl +165 -0
  317. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_0_f32_ns.cl +120 -0
  318. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_1_f32_ns.cl +123 -0
  319. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q4_k_f32_ns.cl +155 -0
  320. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_0_f32_ns.cl +123 -0
  321. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_1_f32_ns.cl +125 -0
  322. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q5_k_f32_ns.cl +160 -0
  323. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_moe_q6_k_f32_ns.cl +141 -0
  324. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_iq4_nl_f32.cl +302 -0
  325. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q1_0_f32.cl +121 -0
  326. data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle_general.cl → gemv_noshuffle_q4_0_f32.cl} +5 -5
  327. data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle.cl → gemv_noshuffle_q4_0_f32_spec.cl} +5 -5
  328. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q4_k_f32.cl +318 -0
  329. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_0_f32.cl +291 -0
  330. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_1_f32.cl +294 -0
  331. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q5_k_f32.cl +326 -0
  332. data/ext/sources/ggml/src/ggml-opencl/kernels/gemv_noshuffle_q6_k_f32.cl +293 -0
  333. data/ext/sources/ggml/src/ggml-opencl/kernels/{gemv_noshuffle_general_q8_0_f32.cl → gemv_noshuffle_q8_0_f32.cl} +1 -1
  334. data/ext/sources/ggml/src/ggml-opencl/kernels/get_rows.cl +15 -9
  335. data/ext/sources/ggml/src/ggml-opencl/kernels/moe_reorder_b.cl +30 -0
  336. data/ext/sources/ggml/src/ggml-opencl/kernels/moe_sort_by_expert.cl +82 -0
  337. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_iq4_nl_f32_l4_lm.cl +171 -0
  338. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q1_0_f32_l4_lm.cl +156 -0
  339. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q4_k_f32_l4_lm.cl +179 -0
  340. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_0_f32_l4_lm.cl +173 -0
  341. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_1_f32_l4_lm.cl +175 -0
  342. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_q5_k_f32_l4_lm.cl +192 -0
  343. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_l4.cl +1149 -0
  344. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_iq4_nl_f32.cl +164 -0
  345. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_iq4_nl_f32_flat.cl +202 -0
  346. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q1_0_f32.cl +141 -0
  347. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q1_0_f32_flat.cl +190 -0
  348. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q4_k_f32_flat.cl +196 -0
  349. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_0_f32.cl +241 -0
  350. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_0_f32_flat.cl +243 -0
  351. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_1_f32.cl +243 -0
  352. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_1_f32_flat.cl +247 -0
  353. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_k_f32.cl +187 -0
  354. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q5_k_f32_flat.cl +203 -0
  355. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q6_k_f32_flat.cl +48 -64
  356. data/ext/sources/ggml/src/ggml-opencl/kernels/norm.cl +5 -2
  357. data/ext/sources/ggml/src/ggml-opencl/kernels/set_rows.cl +500 -0
  358. data/ext/sources/ggml/src/ggml-opencl/libdl.h +79 -0
  359. data/ext/sources/ggml/src/ggml-openvino/.clang-format +0 -5
  360. data/ext/sources/ggml/src/ggml-openvino/CMakeLists.txt +2 -4
  361. data/ext/sources/ggml/src/ggml-openvino/ggml-decoder.cpp +740 -127
  362. data/ext/sources/ggml/src/ggml-openvino/ggml-decoder.h +76 -23
  363. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino-extra.cpp +75 -14
  364. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino-extra.h +29 -8
  365. data/ext/sources/ggml/src/ggml-openvino/ggml-openvino.cpp +339 -69
  366. data/ext/sources/ggml/src/ggml-openvino/ggml-quants.cpp +330 -192
  367. data/ext/sources/ggml/src/ggml-openvino/ggml-quants.h +10 -4
  368. data/ext/sources/ggml/src/ggml-openvino/openvino/decoder.h +56 -16
  369. data/ext/sources/ggml/src/ggml-openvino/openvino/frontend.h +1 -1
  370. data/ext/sources/ggml/src/ggml-openvino/openvino/input_model.h +4 -4
  371. data/ext/sources/ggml/src/ggml-openvino/openvino/node_context.h +94 -37
  372. data/ext/sources/ggml/src/ggml-openvino/openvino/op/add_id.cpp +76 -0
  373. data/ext/sources/ggml/src/ggml-openvino/openvino/op/argsort.cpp +47 -0
  374. data/ext/sources/ggml/src/ggml-openvino/openvino/op/clamp.cpp +33 -0
  375. data/ext/sources/ggml/src/ggml-openvino/openvino/op/concat.cpp +48 -0
  376. data/ext/sources/ggml/src/ggml-openvino/openvino/op/cont.cpp +8 -16
  377. data/ext/sources/ggml/src/ggml-openvino/openvino/op/cpy.cpp +14 -1
  378. data/ext/sources/ggml/src/ggml-openvino/openvino/op/div.cpp +146 -0
  379. data/ext/sources/ggml/src/ggml-openvino/openvino/op/flash_attn_ext.cpp +108 -21
  380. data/ext/sources/ggml/src/ggml-openvino/openvino/op/gated_delta_net.cpp +282 -0
  381. data/ext/sources/ggml/src/ggml-openvino/openvino/op/gated_delta_net.hpp +65 -0
  382. data/ext/sources/ggml/src/ggml-openvino/openvino/op/get_rows.cpp +2 -9
  383. data/ext/sources/ggml/src/ggml-openvino/openvino/op/glu_geglu.cpp +21 -7
  384. data/ext/sources/ggml/src/ggml-openvino/openvino/op/glu_swiglu.cpp +41 -8
  385. data/ext/sources/ggml/src/ggml-openvino/openvino/op/im2col.cpp +120 -0
  386. data/ext/sources/ggml/src/ggml-openvino/openvino/op/l2_norm.cpp +44 -0
  387. data/ext/sources/ggml/src/ggml-openvino/openvino/op/mul_mat_id.cpp +226 -0
  388. data/ext/sources/ggml/src/ggml-openvino/openvino/op/mulmat.cpp +19 -9
  389. data/ext/sources/ggml/src/ggml-openvino/openvino/op/norm.cpp +58 -0
  390. data/ext/sources/ggml/src/ggml-openvino/openvino/op/pad.cpp +95 -0
  391. data/ext/sources/ggml/src/ggml-openvino/openvino/op/permute.cpp +58 -13
  392. data/ext/sources/ggml/src/ggml-openvino/openvino/op/repeat.cpp +74 -0
  393. data/ext/sources/ggml/src/ggml-openvino/openvino/op/reshape.cpp +13 -6
  394. data/ext/sources/ggml/src/ggml-openvino/openvino/op/rms_norm.cpp +1 -1
  395. data/ext/sources/ggml/src/ggml-openvino/openvino/op/rope.cpp +161 -39
  396. data/ext/sources/ggml/src/ggml-openvino/openvino/op/set_rows.cpp +3 -3
  397. data/ext/sources/ggml/src/ggml-openvino/openvino/op/softmax.cpp +126 -49
  398. data/ext/sources/ggml/src/ggml-openvino/openvino/op/ssm_conv.cpp +59 -0
  399. data/ext/sources/ggml/src/ggml-openvino/openvino/op/sum_rows.cpp +27 -0
  400. data/ext/sources/ggml/src/ggml-openvino/openvino/op/transpose.cpp +32 -1
  401. data/ext/sources/ggml/src/ggml-openvino/openvino/op/unary_silu.cpp +1 -1
  402. data/ext/sources/ggml/src/ggml-openvino/openvino/op/unary_softplus.cpp +38 -0
  403. data/ext/sources/ggml/src/ggml-openvino/openvino/op/view.cpp +90 -25
  404. data/ext/sources/ggml/src/ggml-openvino/openvino/op_table.cpp +41 -22
  405. data/ext/sources/ggml/src/ggml-openvino/openvino/op_table.h +18 -4
  406. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/mark_decompression_convert_constant_folding.h +1 -1
  407. data/ext/sources/ggml/src/ggml-openvino/openvino/rt_info/weightless_caching_attributes.hpp +41 -0
  408. data/ext/sources/ggml/src/ggml-openvino/openvino/translate_session.cpp +70 -43
  409. data/ext/sources/ggml/src/ggml-openvino/openvino/translate_session.h +5 -4
  410. data/ext/sources/ggml/src/ggml-openvino/openvino/utils.cpp +612 -36
  411. data/ext/sources/ggml/src/ggml-openvino/openvino/utils.h +29 -26
  412. data/ext/sources/ggml/src/ggml-openvino/utils.cpp +460 -114
  413. data/ext/sources/ggml/src/ggml-openvino/utils.h +32 -9
  414. data/ext/sources/ggml/src/ggml-opt.cpp +1 -0
  415. data/ext/sources/ggml/src/ggml-quants.c +365 -114
  416. data/ext/sources/ggml/src/ggml-quants.h +6 -0
  417. data/ext/sources/ggml/src/ggml-rpc/CMakeLists.txt +24 -0
  418. data/ext/sources/ggml/src/ggml-rpc/ggml-rpc.cpp +167 -311
  419. data/ext/sources/ggml/src/ggml-rpc/transport.cpp +683 -0
  420. data/ext/sources/ggml/src/ggml-rpc/transport.h +34 -0
  421. data/ext/sources/ggml/src/ggml-sycl/CMakeLists.txt +50 -4
  422. data/ext/sources/ggml/src/ggml-sycl/add-id.cpp +1 -1
  423. data/ext/sources/ggml/src/ggml-sycl/backend.hpp +5 -1
  424. data/ext/sources/ggml/src/ggml-sycl/binbcast.cpp +12 -0
  425. data/ext/sources/ggml/src/ggml-sycl/col2im-1d.cpp +102 -0
  426. data/ext/sources/ggml/src/ggml-sycl/col2im-1d.hpp +8 -0
  427. data/ext/sources/ggml/src/ggml-sycl/common.cpp +72 -2
  428. data/ext/sources/ggml/src/ggml-sycl/common.hpp +59 -2
  429. data/ext/sources/ggml/src/ggml-sycl/concat.cpp +21 -1
  430. data/ext/sources/ggml/src/ggml-sycl/conv2d-dw.cpp +158 -0
  431. data/ext/sources/ggml/src/ggml-sycl/conv2d-dw.hpp +10 -0
  432. data/ext/sources/ggml/src/ggml-sycl/conv2d-transpose.cpp +125 -0
  433. data/ext/sources/ggml/src/ggml-sycl/conv2d-transpose.hpp +10 -0
  434. data/ext/sources/ggml/src/ggml-sycl/conv2d.cpp +150 -0
  435. data/ext/sources/ggml/src/ggml-sycl/conv2d.hpp +10 -0
  436. data/ext/sources/ggml/src/ggml-sycl/conv3d.cpp +224 -0
  437. data/ext/sources/ggml/src/ggml-sycl/conv3d.hpp +8 -0
  438. data/ext/sources/ggml/src/ggml-sycl/convert.cpp +121 -13
  439. data/ext/sources/ggml/src/ggml-sycl/convert.hpp +9 -0
  440. data/ext/sources/ggml/src/ggml-sycl/cpy.cpp +706 -0
  441. data/ext/sources/ggml/src/ggml-sycl/cpy.hpp +281 -0
  442. data/ext/sources/ggml/src/ggml-sycl/cross_entropy_loss.cpp +255 -0
  443. data/ext/sources/ggml/src/ggml-sycl/cross_entropy_loss.hpp +7 -0
  444. data/ext/sources/ggml/src/ggml-sycl/cumsum.cpp +148 -0
  445. data/ext/sources/ggml/src/ggml-sycl/cumsum.hpp +5 -0
  446. data/ext/sources/ggml/src/ggml-sycl/dequantize.hpp +678 -0
  447. data/ext/sources/ggml/src/ggml-sycl/diag.cpp +67 -0
  448. data/ext/sources/ggml/src/ggml-sycl/diag.hpp +5 -0
  449. data/ext/sources/ggml/src/ggml-sycl/dmmv.cpp +997 -244
  450. data/ext/sources/ggml/src/ggml-sycl/dpct/helper.hpp +15 -7
  451. data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +215 -204
  452. data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +2 -2
  453. data/ext/sources/ggml/src/ggml-sycl/fattn-buffers.cpp +56 -0
  454. data/ext/sources/ggml/src/ggml-sycl/fattn-buffers.hpp +63 -0
  455. data/ext/sources/ggml/src/ggml-sycl/fattn-common.hpp +7 -5
  456. data/ext/sources/ggml/src/ggml-sycl/fattn-tile.cpp +4 -0
  457. data/ext/sources/ggml/src/ggml-sycl/fattn-tile.hpp +76 -168
  458. data/ext/sources/ggml/src/ggml-sycl/fattn-vec.hpp +7 -0
  459. data/ext/sources/ggml/src/ggml-sycl/fattn.cpp +3 -1
  460. data/ext/sources/ggml/src/ggml-sycl/fill.cpp +55 -0
  461. data/ext/sources/ggml/src/ggml-sycl/fill.hpp +5 -0
  462. data/ext/sources/ggml/src/ggml-sycl/gated_delta_net.cpp +69 -31
  463. data/ext/sources/ggml/src/ggml-sycl/gated_delta_net.hpp +1 -0
  464. data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +3 -0
  465. data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +79 -3
  466. data/ext/sources/ggml/src/ggml-sycl/ggml-sycl.cpp +1758 -455
  467. data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +353 -89
  468. data/ext/sources/ggml/src/ggml-sycl/im2col.hpp +5 -3
  469. data/ext/sources/ggml/src/ggml-sycl/mmvq.cpp +1542 -39
  470. data/ext/sources/ggml/src/ggml-sycl/mmvq.hpp +33 -0
  471. data/ext/sources/ggml/src/ggml-sycl/norm.cpp +103 -49
  472. data/ext/sources/ggml/src/ggml-sycl/outprod.cpp +45 -9
  473. data/ext/sources/ggml/src/ggml-sycl/pad.cpp +27 -27
  474. data/ext/sources/ggml/src/ggml-sycl/pool.cpp +185 -0
  475. data/ext/sources/ggml/src/ggml-sycl/pool.hpp +22 -0
  476. data/ext/sources/ggml/src/ggml-sycl/presets.hpp +3 -1
  477. data/ext/sources/ggml/src/ggml-sycl/quants.hpp +71 -0
  478. data/ext/sources/ggml/src/ggml-sycl/set_rows.cpp +17 -3
  479. data/ext/sources/ggml/src/ggml-sycl/softmax.cpp +9 -10
  480. data/ext/sources/ggml/src/ggml-sycl/solve_tri.cpp +172 -0
  481. data/ext/sources/ggml/src/ggml-sycl/solve_tri.hpp +8 -0
  482. data/ext/sources/ggml/src/ggml-sycl/ssm_conv.cpp +6 -1
  483. data/ext/sources/ggml/src/ggml-sycl/ssm_scan.cpp +156 -0
  484. data/ext/sources/ggml/src/ggml-sycl/ssm_scan.hpp +5 -0
  485. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.cpp +62 -10
  486. data/ext/sources/ggml/src/ggml-sycl/sycl_hw.hpp +18 -6
  487. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-tile-instance-dkq512-dv512.cpp +6 -0
  488. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-f16.cpp +1 -0
  489. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q4_0.cpp +1 -0
  490. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q4_1.cpp +1 -0
  491. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q5_0.cpp +1 -0
  492. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q5_1.cpp +1 -0
  493. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-f16-q8_0.cpp +1 -0
  494. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-f16.cpp +1 -0
  495. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q4_0.cpp +1 -0
  496. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q4_1.cpp +1 -0
  497. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q5_0.cpp +1 -0
  498. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q5_1.cpp +1 -0
  499. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_0-q8_0.cpp +1 -0
  500. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-f16.cpp +1 -0
  501. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q4_0.cpp +1 -0
  502. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q4_1.cpp +1 -0
  503. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q5_0.cpp +1 -0
  504. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q5_1.cpp +1 -0
  505. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q4_1-q8_0.cpp +1 -0
  506. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-f16.cpp +1 -0
  507. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q4_0.cpp +1 -0
  508. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q4_1.cpp +1 -0
  509. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q5_0.cpp +1 -0
  510. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q5_1.cpp +1 -0
  511. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_0-q8_0.cpp +1 -0
  512. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-f16.cpp +1 -0
  513. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q4_0.cpp +1 -0
  514. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q4_1.cpp +1 -0
  515. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q5_0.cpp +1 -0
  516. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q5_1.cpp +1 -0
  517. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q5_1-q8_0.cpp +1 -0
  518. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-f16.cpp +1 -0
  519. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q4_0.cpp +1 -0
  520. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q4_1.cpp +1 -0
  521. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q5_0.cpp +1 -0
  522. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q5_1.cpp +1 -0
  523. data/ext/sources/ggml/src/ggml-sycl/template-instances/fattn-vec-instance-q8_0-q8_0.cpp +1 -0
  524. data/ext/sources/ggml/src/ggml-sycl/type.hpp +112 -0
  525. data/ext/sources/ggml/src/ggml-sycl/upscale.cpp +410 -0
  526. data/ext/sources/ggml/src/ggml-sycl/upscale.hpp +9 -0
  527. data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +242 -45
  528. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-buffer.cpp +4 -0
  529. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend-device.cpp +2 -0
  530. data/ext/sources/ggml/src/ggml-virtgpu/ggml-backend.cpp +2 -0
  531. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu-shm.cpp +1 -0
  532. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu.cpp +1 -0
  533. data/ext/sources/ggml/src/ggml-virtgpu/virtgpu.h +0 -2
  534. data/ext/sources/ggml/src/ggml-vulkan/CMakeLists.txt +16 -0
  535. data/ext/sources/ggml/src/ggml-vulkan/ggml-vulkan.cpp +2843 -700
  536. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +4 -0
  537. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/col2im_1d.comp +61 -0
  538. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/contig_copy.comp +6 -2
  539. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp +146 -13
  540. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv3d_mm.comp +431 -0
  541. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy.comp +3 -1
  542. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +1 -1
  543. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +25 -1
  544. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.glsl +88 -0
  545. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.glsl +643 -1
  546. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_nvfp4.comp +32 -0
  547. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q1_0.comp +29 -0
  548. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/diag.comp +3 -4
  549. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dot_product_funcs.glsl +27 -0
  550. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/feature-tests/coopmat2_decode_vector.comp +7 -0
  551. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +198 -48
  552. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.glsl +60 -59
  553. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +116 -113
  554. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +122 -31
  555. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_dequant.glsl +131 -0
  556. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_mmq_funcs.glsl +203 -0
  557. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/fwht.comp +115 -0
  558. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net.comp +125 -64
  559. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.glsl +0 -1
  560. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_unary_head.glsl +21 -19
  561. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows_back.comp +25 -0
  562. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_head.glsl +29 -1
  563. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_main.glsl +17 -11
  564. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +76 -54
  565. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col_3d.comp +0 -1
  566. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/l2_norm.comp +4 -7
  567. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/log.comp +0 -1
  568. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec.comp +122 -27
  569. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iface.glsl +6 -6
  570. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q2_k.comp +1 -1
  571. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q4_k.comp +1 -1
  572. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q5_k.comp +1 -1
  573. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq.comp +22 -24
  574. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq_funcs.glsl +88 -55
  575. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +42 -40
  576. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +49 -15
  577. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.glsl +222 -171
  578. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.glsl +8 -8
  579. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_shmem_types.glsl +24 -9
  580. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/multi_add.comp +0 -1
  581. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/norm.comp +10 -10
  582. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/repeat_back.comp +3 -3
  583. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp +3 -3
  584. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_funcs.glsl +5 -2
  585. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.glsl +0 -1
  586. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_params.glsl +3 -2
  587. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/snake.comp +49 -0
  588. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/ssm_conv.comp +11 -1
  589. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/tri.comp +3 -4
  590. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/types.glsl +79 -2
  591. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp +168 -0
  592. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +282 -211
  593. data/ext/sources/ggml/src/ggml-webgpu/CMakeLists.txt +5 -2
  594. data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp +2209 -283
  595. data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu.cpp +2618 -1416
  596. data/ext/sources/ggml/src/ggml-webgpu/pre_wgsl.hpp +37 -7
  597. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/add_id.wgsl +64 -0
  598. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/binary.wgsl +8 -7
  599. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/common_decls.tmpl +90 -95
  600. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/concat.wgsl +19 -1
  601. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/conv2d.wgsl +165 -0
  602. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{cpy.tmpl.wgsl → cpy.wgsl} +25 -50
  603. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn.wgsl +107 -184
  604. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_quant_staging.tmpl +124 -0
  605. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_tile.wgsl +397 -0
  606. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_blk.wgsl +101 -0
  607. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_reduce.wgsl +84 -0
  608. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn_vec_split.wgsl +619 -0
  609. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/gated_delta_net.wgsl +149 -0
  610. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/get_rows.wgsl +204 -78
  611. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/glu.wgsl +155 -0
  612. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/im2col.wgsl +101 -0
  613. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_decls.tmpl +805 -526
  614. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id.wgsl +195 -0
  615. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id_gather.wgsl +52 -0
  616. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_id_vec.wgsl +154 -0
  617. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_reg_tile.wgsl +8 -6
  618. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_subgroup_matrix.wgsl +5 -1
  619. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec.wgsl +90 -413
  620. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec_acc.tmpl +1553 -0
  621. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec_q_acc.tmpl +297 -0
  622. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/quant_inner_loops.tmpl +21 -0
  623. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/quantize_q8.wgsl +178 -0
  624. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm_mul.wgsl +152 -0
  625. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{rope.tmpl.wgsl → rope.wgsl} +71 -142
  626. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/row_norm.wgsl +153 -0
  627. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/scale.wgsl +6 -4
  628. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set.wgsl +109 -0
  629. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.wgsl +2 -3
  630. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows_quant.wgsl +224 -0
  631. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/{soft_max.tmpl.wgsl → soft_max.wgsl} +106 -206
  632. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/solve_tri.wgsl +121 -0
  633. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/ssm_conv.wgsl +65 -0
  634. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/ssm_scan.wgsl +193 -0
  635. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/unary.wgsl +68 -48
  636. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/upscale.wgsl +240 -0
  637. data/ext/sources/ggml/src/ggml-zdnn/ggml-zdnn.cpp +18 -14
  638. data/ext/sources/ggml/src/ggml-zendnn/CMakeLists.txt +1 -1
  639. data/ext/sources/ggml/src/ggml-zendnn/ggml-zendnn.cpp +244 -10
  640. data/ext/sources/ggml/src/ggml.c +146 -42
  641. data/ext/sources/ggml/src/gguf.cpp +173 -28
  642. data/ext/sources/include/parakeet.h +342 -0
  643. data/ext/sources/include/whisper.h +31 -0
  644. data/ext/sources/media/matmul.png +0 -0
  645. data/ext/sources/src/CMakeLists.txt +23 -0
  646. data/ext/sources/src/parakeet-arch.h +188 -0
  647. data/ext/sources/src/parakeet.cpp +3838 -0
  648. data/ext/sources/src/whisper.cpp +220 -26
  649. data/extsources.rb +26 -10
  650. data/lib/whisper/log_settable.rb +33 -0
  651. data/lib/whisper/model/uri.rb +13 -8
  652. data/lib/whisper/output.rb +74 -0
  653. data/sig/whisper.rbs +417 -62
  654. data/test/helper.rb +2 -0
  655. data/test/jfk_reader/jfk_reader.c +50 -7
  656. data/test/test_callback.rb +1 -0
  657. data/test/test_package.rb +6 -5
  658. data/test/test_parakeet.rb +28 -0
  659. data/test/test_parakeet_callback.rb +107 -0
  660. data/test/test_parakeet_context.rb +116 -0
  661. data/test/test_parakeet_context_params.rb +24 -0
  662. data/test/test_parakeet_model.rb +21 -0
  663. data/test/test_parakeet_params.rb +78 -0
  664. data/test/test_parakeet_segment.rb +42 -0
  665. data/test/test_parakeet_token.rb +73 -0
  666. data/test/test_params.rb +2 -0
  667. data/test/test_vad.rb +9 -0
  668. data/test/test_vad_context.rb +2 -2
  669. data/test/test_vad_segment.rb +1 -1
  670. data/test/test_whisper.rb +24 -6
  671. data/whispercpp.gemspec +2 -2
  672. metadata +263 -304
  673. data/ext/sources/bindings/javascript/CMakeLists.txt +0 -41
  674. data/ext/sources/bindings/javascript/emscripten.cpp +0 -93
  675. data/ext/sources/bindings/javascript/libwhisper.worker.js +0 -1
  676. data/ext/sources/bindings/javascript/package.json +0 -26
  677. data/ext/sources/bindings/javascript/whisper.js +0 -19
  678. data/ext/sources/examples/addon.node/CMakeLists.txt +0 -31
  679. data/ext/sources/examples/addon.node/__test__/whisper.spec.js +0 -133
  680. data/ext/sources/examples/addon.node/addon.cpp +0 -557
  681. data/ext/sources/examples/addon.node/index.js +0 -59
  682. data/ext/sources/examples/addon.node/package.json +0 -16
  683. data/ext/sources/examples/addon.node/vad-example.js +0 -132
  684. data/ext/sources/examples/bench.wasm/CMakeLists.txt +0 -49
  685. data/ext/sources/examples/bench.wasm/emscripten.cpp +0 -87
  686. data/ext/sources/examples/bench.wasm/index-tmpl.html +0 -285
  687. data/ext/sources/examples/coi-serviceworker.js +0 -146
  688. data/ext/sources/examples/command/CMakeLists.txt +0 -10
  689. data/ext/sources/examples/command/command.cpp +0 -802
  690. data/ext/sources/examples/command/commands.txt +0 -9
  691. data/ext/sources/examples/command.wasm/CMakeLists.txt +0 -50
  692. data/ext/sources/examples/command.wasm/emscripten.cpp +0 -327
  693. data/ext/sources/examples/command.wasm/index-tmpl.html +0 -415
  694. data/ext/sources/examples/generate-karaoke.sh +0 -57
  695. data/ext/sources/examples/helpers.js +0 -191
  696. data/ext/sources/examples/livestream.sh +0 -112
  697. data/ext/sources/examples/lsp/CMakeLists.txt +0 -10
  698. data/ext/sources/examples/lsp/lsp.cpp +0 -471
  699. data/ext/sources/examples/lsp/whisper.vim +0 -362
  700. data/ext/sources/examples/python/test_whisper_processor.py +0 -7
  701. data/ext/sources/examples/python/whisper_processor.py +0 -54
  702. data/ext/sources/examples/server/bench.js +0 -29
  703. data/ext/sources/examples/server.py +0 -120
  704. data/ext/sources/examples/stream/CMakeLists.txt +0 -10
  705. data/ext/sources/examples/stream/stream.cpp +0 -437
  706. data/ext/sources/examples/stream.wasm/CMakeLists.txt +0 -49
  707. data/ext/sources/examples/stream.wasm/emscripten.cpp +0 -216
  708. data/ext/sources/examples/stream.wasm/index-tmpl.html +0 -491
  709. data/ext/sources/examples/sycl/CMakeLists.txt +0 -9
  710. data/ext/sources/examples/sycl/build.sh +0 -22
  711. data/ext/sources/examples/sycl/ls-sycl-device.cpp +0 -11
  712. data/ext/sources/examples/sycl/run-whisper.sh +0 -17
  713. data/ext/sources/examples/talk-llama/CMakeLists.txt +0 -48
  714. data/ext/sources/examples/talk-llama/eleven-labs.py +0 -80
  715. data/ext/sources/examples/talk-llama/llama-adapter.cpp +0 -488
  716. data/ext/sources/examples/talk-llama/llama-adapter.h +0 -89
  717. data/ext/sources/examples/talk-llama/llama-arch.cpp +0 -2877
  718. data/ext/sources/examples/talk-llama/llama-arch.h +0 -628
  719. data/ext/sources/examples/talk-llama/llama-batch.cpp +0 -919
  720. data/ext/sources/examples/talk-llama/llama-batch.h +0 -173
  721. data/ext/sources/examples/talk-llama/llama-chat.cpp +0 -896
  722. data/ext/sources/examples/talk-llama/llama-chat.h +0 -71
  723. data/ext/sources/examples/talk-llama/llama-context.cpp +0 -3633
  724. data/ext/sources/examples/talk-llama/llama-context.h +0 -359
  725. data/ext/sources/examples/talk-llama/llama-cparams.cpp +0 -5
  726. data/ext/sources/examples/talk-llama/llama-cparams.h +0 -47
  727. data/ext/sources/examples/talk-llama/llama-ext.h +0 -12
  728. data/ext/sources/examples/talk-llama/llama-grammar.cpp +0 -1464
  729. data/ext/sources/examples/talk-llama/llama-grammar.h +0 -194
  730. data/ext/sources/examples/talk-llama/llama-graph.cpp +0 -2735
  731. data/ext/sources/examples/talk-llama/llama-graph.h +0 -1031
  732. data/ext/sources/examples/talk-llama/llama-hparams.cpp +0 -258
  733. data/ext/sources/examples/talk-llama/llama-hparams.h +0 -353
  734. data/ext/sources/examples/talk-llama/llama-impl.cpp +0 -171
  735. data/ext/sources/examples/talk-llama/llama-impl.h +0 -75
  736. data/ext/sources/examples/talk-llama/llama-io.cpp +0 -15
  737. data/ext/sources/examples/talk-llama/llama-io.h +0 -35
  738. data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.cpp +0 -330
  739. data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.h +0 -137
  740. data/ext/sources/examples/talk-llama/llama-kv-cache.cpp +0 -2285
  741. data/ext/sources/examples/talk-llama/llama-kv-cache.h +0 -389
  742. data/ext/sources/examples/talk-llama/llama-kv-cells.h +0 -533
  743. data/ext/sources/examples/talk-llama/llama-memory-hybrid-iswa.cpp +0 -275
  744. data/ext/sources/examples/talk-llama/llama-memory-hybrid-iswa.h +0 -140
  745. data/ext/sources/examples/talk-llama/llama-memory-hybrid.cpp +0 -268
  746. data/ext/sources/examples/talk-llama/llama-memory-hybrid.h +0 -139
  747. data/ext/sources/examples/talk-llama/llama-memory-recurrent.cpp +0 -1165
  748. data/ext/sources/examples/talk-llama/llama-memory-recurrent.h +0 -182
  749. data/ext/sources/examples/talk-llama/llama-memory.cpp +0 -59
  750. data/ext/sources/examples/talk-llama/llama-memory.h +0 -122
  751. data/ext/sources/examples/talk-llama/llama-mmap.cpp +0 -752
  752. data/ext/sources/examples/talk-llama/llama-mmap.h +0 -73
  753. data/ext/sources/examples/talk-llama/llama-model-loader.cpp +0 -1655
  754. data/ext/sources/examples/talk-llama/llama-model-loader.h +0 -206
  755. data/ext/sources/examples/talk-llama/llama-model-saver.cpp +0 -299
  756. data/ext/sources/examples/talk-llama/llama-model-saver.h +0 -40
  757. data/ext/sources/examples/talk-llama/llama-model.cpp +0 -9056
  758. data/ext/sources/examples/talk-llama/llama-model.h +0 -597
  759. data/ext/sources/examples/talk-llama/llama-quant.cpp +0 -1304
  760. data/ext/sources/examples/talk-llama/llama-quant.h +0 -1
  761. data/ext/sources/examples/talk-llama/llama-sampler.cpp +0 -3885
  762. data/ext/sources/examples/talk-llama/llama-sampler.h +0 -42
  763. data/ext/sources/examples/talk-llama/llama-vocab.cpp +0 -3970
  764. data/ext/sources/examples/talk-llama/llama-vocab.h +0 -187
  765. data/ext/sources/examples/talk-llama/llama.cpp +0 -1194
  766. data/ext/sources/examples/talk-llama/llama.h +0 -1573
  767. data/ext/sources/examples/talk-llama/models/afmoe.cpp +0 -190
  768. data/ext/sources/examples/talk-llama/models/apertus.cpp +0 -125
  769. data/ext/sources/examples/talk-llama/models/arcee.cpp +0 -135
  770. data/ext/sources/examples/talk-llama/models/arctic.cpp +0 -137
  771. data/ext/sources/examples/talk-llama/models/arwkv7.cpp +0 -86
  772. data/ext/sources/examples/talk-llama/models/baichuan.cpp +0 -123
  773. data/ext/sources/examples/talk-llama/models/bailingmoe.cpp +0 -143
  774. data/ext/sources/examples/talk-llama/models/bailingmoe2.cpp +0 -133
  775. data/ext/sources/examples/talk-llama/models/bert.cpp +0 -184
  776. data/ext/sources/examples/talk-llama/models/bitnet.cpp +0 -145
  777. data/ext/sources/examples/talk-llama/models/bloom.cpp +0 -101
  778. data/ext/sources/examples/talk-llama/models/chameleon.cpp +0 -178
  779. data/ext/sources/examples/talk-llama/models/chatglm.cpp +0 -132
  780. data/ext/sources/examples/talk-llama/models/codeshell.cpp +0 -111
  781. data/ext/sources/examples/talk-llama/models/cogvlm.cpp +0 -102
  782. data/ext/sources/examples/talk-llama/models/cohere2-iswa.cpp +0 -134
  783. data/ext/sources/examples/talk-llama/models/command-r.cpp +0 -122
  784. data/ext/sources/examples/talk-llama/models/dbrx.cpp +0 -122
  785. data/ext/sources/examples/talk-llama/models/deci.cpp +0 -135
  786. data/ext/sources/examples/talk-llama/models/deepseek.cpp +0 -142
  787. data/ext/sources/examples/talk-llama/models/deepseek2.cpp +0 -262
  788. data/ext/sources/examples/talk-llama/models/delta-net-base.cpp +0 -445
  789. data/ext/sources/examples/talk-llama/models/dots1.cpp +0 -132
  790. data/ext/sources/examples/talk-llama/models/dream.cpp +0 -105
  791. data/ext/sources/examples/talk-llama/models/ernie4-5-moe.cpp +0 -148
  792. data/ext/sources/examples/talk-llama/models/ernie4-5.cpp +0 -110
  793. data/ext/sources/examples/talk-llama/models/eurobert.cpp +0 -97
  794. data/ext/sources/examples/talk-llama/models/exaone-moe.cpp +0 -145
  795. data/ext/sources/examples/talk-llama/models/exaone.cpp +0 -114
  796. data/ext/sources/examples/talk-llama/models/exaone4.cpp +0 -123
  797. data/ext/sources/examples/talk-llama/models/falcon-h1.cpp +0 -111
  798. data/ext/sources/examples/talk-llama/models/falcon.cpp +0 -120
  799. data/ext/sources/examples/talk-llama/models/gemma-embedding.cpp +0 -116
  800. data/ext/sources/examples/talk-llama/models/gemma.cpp +0 -112
  801. data/ext/sources/examples/talk-llama/models/gemma2-iswa.cpp +0 -128
  802. data/ext/sources/examples/talk-llama/models/gemma3.cpp +0 -155
  803. data/ext/sources/examples/talk-llama/models/gemma3n-iswa.cpp +0 -384
  804. data/ext/sources/examples/talk-llama/models/glm4-moe.cpp +0 -170
  805. data/ext/sources/examples/talk-llama/models/glm4.cpp +0 -157
  806. data/ext/sources/examples/talk-llama/models/gpt2.cpp +0 -105
  807. data/ext/sources/examples/talk-llama/models/gptneox.cpp +0 -144
  808. data/ext/sources/examples/talk-llama/models/granite-hybrid.cpp +0 -195
  809. data/ext/sources/examples/talk-llama/models/granite.cpp +0 -210
  810. data/ext/sources/examples/talk-llama/models/grok.cpp +0 -159
  811. data/ext/sources/examples/talk-llama/models/grovemoe.cpp +0 -139
  812. data/ext/sources/examples/talk-llama/models/hunyuan-dense.cpp +0 -132
  813. data/ext/sources/examples/talk-llama/models/hunyuan-moe.cpp +0 -153
  814. data/ext/sources/examples/talk-llama/models/internlm2.cpp +0 -120
  815. data/ext/sources/examples/talk-llama/models/jais.cpp +0 -86
  816. data/ext/sources/examples/talk-llama/models/jais2.cpp +0 -123
  817. data/ext/sources/examples/talk-llama/models/jamba.cpp +0 -106
  818. data/ext/sources/examples/talk-llama/models/kimi-linear.cpp +0 -381
  819. data/ext/sources/examples/talk-llama/models/lfm2.cpp +0 -196
  820. data/ext/sources/examples/talk-llama/models/llada-moe.cpp +0 -122
  821. data/ext/sources/examples/talk-llama/models/llada.cpp +0 -99
  822. data/ext/sources/examples/talk-llama/models/llama-iswa.cpp +0 -178
  823. data/ext/sources/examples/talk-llama/models/llama.cpp +0 -175
  824. data/ext/sources/examples/talk-llama/models/maincoder.cpp +0 -117
  825. data/ext/sources/examples/talk-llama/models/mamba-base.cpp +0 -289
  826. data/ext/sources/examples/talk-llama/models/mamba.cpp +0 -54
  827. data/ext/sources/examples/talk-llama/models/mimo2-iswa.cpp +0 -129
  828. data/ext/sources/examples/talk-llama/models/minicpm3.cpp +0 -200
  829. data/ext/sources/examples/talk-llama/models/minimax-m2.cpp +0 -123
  830. data/ext/sources/examples/talk-llama/models/mistral3.cpp +0 -160
  831. data/ext/sources/examples/talk-llama/models/models.h +0 -704
  832. data/ext/sources/examples/talk-llama/models/modern-bert.cpp +0 -109
  833. data/ext/sources/examples/talk-llama/models/mpt.cpp +0 -126
  834. data/ext/sources/examples/talk-llama/models/nemotron-h.cpp +0 -162
  835. data/ext/sources/examples/talk-llama/models/nemotron.cpp +0 -122
  836. data/ext/sources/examples/talk-llama/models/neo-bert.cpp +0 -104
  837. data/ext/sources/examples/talk-llama/models/olmo.cpp +0 -121
  838. data/ext/sources/examples/talk-llama/models/olmo2.cpp +0 -150
  839. data/ext/sources/examples/talk-llama/models/olmoe.cpp +0 -124
  840. data/ext/sources/examples/talk-llama/models/openai-moe-iswa.cpp +0 -127
  841. data/ext/sources/examples/talk-llama/models/openelm.cpp +0 -124
  842. data/ext/sources/examples/talk-llama/models/orion.cpp +0 -123
  843. data/ext/sources/examples/talk-llama/models/paddleocr.cpp +0 -122
  844. data/ext/sources/examples/talk-llama/models/pangu-embedded.cpp +0 -121
  845. data/ext/sources/examples/talk-llama/models/phi2.cpp +0 -121
  846. data/ext/sources/examples/talk-llama/models/phi3.cpp +0 -152
  847. data/ext/sources/examples/talk-llama/models/plamo.cpp +0 -110
  848. data/ext/sources/examples/talk-llama/models/plamo2.cpp +0 -320
  849. data/ext/sources/examples/talk-llama/models/plamo3.cpp +0 -128
  850. data/ext/sources/examples/talk-llama/models/plm.cpp +0 -169
  851. data/ext/sources/examples/talk-llama/models/qwen.cpp +0 -108
  852. data/ext/sources/examples/talk-llama/models/qwen2.cpp +0 -126
  853. data/ext/sources/examples/talk-llama/models/qwen2moe.cpp +0 -151
  854. data/ext/sources/examples/talk-llama/models/qwen2vl.cpp +0 -117
  855. data/ext/sources/examples/talk-llama/models/qwen3.cpp +0 -120
  856. data/ext/sources/examples/talk-llama/models/qwen35.cpp +0 -381
  857. data/ext/sources/examples/talk-llama/models/qwen35moe.cpp +0 -422
  858. data/ext/sources/examples/talk-llama/models/qwen3moe.cpp +0 -131
  859. data/ext/sources/examples/talk-llama/models/qwen3next.cpp +0 -525
  860. data/ext/sources/examples/talk-llama/models/qwen3vl-moe.cpp +0 -140
  861. data/ext/sources/examples/talk-llama/models/qwen3vl.cpp +0 -132
  862. data/ext/sources/examples/talk-llama/models/refact.cpp +0 -94
  863. data/ext/sources/examples/talk-llama/models/rnd1.cpp +0 -126
  864. data/ext/sources/examples/talk-llama/models/rwkv6-base.cpp +0 -164
  865. data/ext/sources/examples/talk-llama/models/rwkv6.cpp +0 -94
  866. data/ext/sources/examples/talk-llama/models/rwkv6qwen2.cpp +0 -86
  867. data/ext/sources/examples/talk-llama/models/rwkv7-base.cpp +0 -137
  868. data/ext/sources/examples/talk-llama/models/rwkv7.cpp +0 -90
  869. data/ext/sources/examples/talk-llama/models/seed-oss.cpp +0 -124
  870. data/ext/sources/examples/talk-llama/models/smallthinker.cpp +0 -126
  871. data/ext/sources/examples/talk-llama/models/smollm3.cpp +0 -128
  872. data/ext/sources/examples/talk-llama/models/stablelm.cpp +0 -146
  873. data/ext/sources/examples/talk-llama/models/starcoder.cpp +0 -100
  874. data/ext/sources/examples/talk-llama/models/starcoder2.cpp +0 -121
  875. data/ext/sources/examples/talk-llama/models/step35-iswa.cpp +0 -165
  876. data/ext/sources/examples/talk-llama/models/t5-dec.cpp +0 -166
  877. data/ext/sources/examples/talk-llama/models/t5-enc.cpp +0 -96
  878. data/ext/sources/examples/talk-llama/models/wavtokenizer-dec.cpp +0 -149
  879. data/ext/sources/examples/talk-llama/models/xverse.cpp +0 -108
  880. data/ext/sources/examples/talk-llama/prompts/talk-alpaca.txt +0 -23
  881. data/ext/sources/examples/talk-llama/speak +0 -40
  882. data/ext/sources/examples/talk-llama/speak.bat +0 -1
  883. data/ext/sources/examples/talk-llama/speak.ps1 +0 -14
  884. data/ext/sources/examples/talk-llama/talk-llama.cpp +0 -813
  885. data/ext/sources/examples/talk-llama/unicode-data.cpp +0 -7034
  886. data/ext/sources/examples/talk-llama/unicode-data.h +0 -20
  887. data/ext/sources/examples/talk-llama/unicode.cpp +0 -1103
  888. data/ext/sources/examples/talk-llama/unicode.h +0 -111
  889. data/ext/sources/examples/wchess/CMakeLists.txt +0 -10
  890. data/ext/sources/examples/wchess/libwchess/CMakeLists.txt +0 -19
  891. data/ext/sources/examples/wchess/libwchess/Chessboard.cpp +0 -803
  892. data/ext/sources/examples/wchess/libwchess/Chessboard.h +0 -33
  893. data/ext/sources/examples/wchess/libwchess/WChess.cpp +0 -193
  894. data/ext/sources/examples/wchess/libwchess/WChess.h +0 -63
  895. data/ext/sources/examples/wchess/libwchess/test-chessboard.cpp +0 -117
  896. data/ext/sources/examples/wchess/wchess.cmd/CMakeLists.txt +0 -8
  897. data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +0 -253
  898. data/ext/sources/examples/whisper.wasm/CMakeLists.txt +0 -50
  899. data/ext/sources/examples/whisper.wasm/emscripten.cpp +0 -118
  900. data/ext/sources/examples/whisper.wasm/index-tmpl.html +0 -659
  901. data/ext/sources/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +0 -99
  902. data/ext/sources/ggml/src/ggml-hexagon/htp/htp-msg.h +0 -155
  903. data/ext/sources/ggml/src/ggml-hexagon/op-desc.h +0 -153
  904. data/ext/sources/ggml/src/ggml-opencl/kernels/embed_kernel.py +0 -26
  905. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/eliminate_zp.cpp +0 -123
  906. data/ext/sources/ggml/src/ggml-openvino/openvino/pass/eliminate_zp.h +0 -17
  907. data/ext/sources/ggml/src/ggml-virtgpu/regenerate_remoting.py +0 -333
  908. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/abs.comp +0 -21
  909. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/ceil.comp +0 -22
  910. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/clamp.comp +0 -17
  911. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/cos.comp +0 -17
  912. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/elu.comp +0 -27
  913. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/exp.comp +0 -21
  914. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/floor.comp +0 -22
  915. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu.comp +0 -25
  916. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu_erf.comp +0 -39
  917. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu_quick.comp +0 -23
  918. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/hardsigmoid.comp +0 -22
  919. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/hardswish.comp +0 -22
  920. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/leaky_relu.comp +0 -22
  921. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/neg.comp +0 -20
  922. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/relu.comp +0 -21
  923. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/round.comp +0 -29
  924. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rte.glsl +0 -5
  925. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sgn.comp +0 -21
  926. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sigmoid.comp +0 -20
  927. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/silu.comp +0 -22
  928. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sin.comp +0 -17
  929. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/softplus.comp +0 -23
  930. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sqrt.comp +0 -17
  931. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/square.comp +0 -17
  932. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/step.comp +0 -22
  933. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/tanh.comp +0 -20
  934. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/trunc.comp +0 -22
  935. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/xielu.comp +0 -35
  936. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +0 -182
  937. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/glu.tmpl.wgsl +0 -323
  938. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.wgsl +0 -718
  939. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm.wgsl +0 -123
  940. data/ext/sources/tests/CMakeLists.txt +0 -112
  941. data/ext/sources/tests/earnings21/eval.mk +0 -58
  942. data/ext/sources/tests/earnings21/eval.py +0 -68
  943. data/ext/sources/tests/earnings21/normalizers/__init__.py +0 -2
  944. data/ext/sources/tests/earnings21/normalizers/basic.py +0 -80
  945. data/ext/sources/tests/earnings21/normalizers/english.json +0 -1741
  946. data/ext/sources/tests/earnings21/normalizers/english.py +0 -550
  947. data/ext/sources/tests/earnings21/requirements.txt +0 -6
  948. data/ext/sources/tests/en-0-ref.txt +0 -1
  949. data/ext/sources/tests/en-1-ref.txt +0 -1
  950. data/ext/sources/tests/en-2-ref.txt +0 -1
  951. data/ext/sources/tests/es-0-ref.txt +0 -1
  952. data/ext/sources/tests/librispeech/eval.mk +0 -39
  953. data/ext/sources/tests/librispeech/eval.py +0 -47
  954. data/ext/sources/tests/librispeech/normalizers/__init__.py +0 -2
  955. data/ext/sources/tests/librispeech/normalizers/basic.py +0 -80
  956. data/ext/sources/tests/librispeech/normalizers/english.json +0 -1741
  957. data/ext/sources/tests/librispeech/normalizers/english.py +0 -550
  958. data/ext/sources/tests/librispeech/requirements.txt +0 -6
  959. data/ext/sources/tests/run-tests.sh +0 -130
  960. data/ext/sources/tests/test-c.c +0 -3
  961. data/ext/sources/tests/test-vad-full.cpp +0 -56
  962. data/ext/sources/tests/test-vad.cpp +0 -83
  963. data/ext/sources/tests/test-whisper.js +0 -58
  964. data/lib/whisper/context.rb +0 -15
  965. data/lib/whisper/segment.rb +0 -58
@@ -1,2285 +0,0 @@
1
- #include "llama-kv-cache.h"
2
-
3
- #include "llama-impl.h"
4
- #include "llama-io.h"
5
- #include "llama-model.h"
6
- #include "llama-context.h"
7
-
8
- #include <algorithm>
9
- #include <cassert>
10
- #include <cmath>
11
- #include <cstring>
12
- #include <limits>
13
- #include <map>
14
- #include <stdexcept>
15
-
16
- //
17
- // llama_kv_cache
18
- //
19
-
20
- llama_kv_cache::llama_kv_cache(
21
- const llama_model & model,
22
- ggml_type type_k,
23
- ggml_type type_v,
24
- bool v_trans,
25
- bool offload,
26
- bool unified,
27
- uint32_t kv_size,
28
- uint32_t n_seq_max,
29
- uint32_t n_pad,
30
- uint32_t n_swa,
31
- llama_swa_type swa_type,
32
- const layer_filter_cb & filter,
33
- const layer_reuse_cb & reuse) :
34
- model(model), hparams(model.hparams), v_trans(v_trans),
35
- n_seq_max(n_seq_max), n_stream(unified ? 1 : n_seq_max), n_pad(n_pad), n_swa(n_swa), swa_type(swa_type) {
36
-
37
- GGML_ASSERT(kv_size % n_pad == 0);
38
-
39
- const uint32_t n_layer_kv = hparams.n_layer_kv();
40
-
41
- // define a comparator for the buft -> ctx map to ensure that the order is well-defined:
42
- struct ggml_backend_buft_comparator {
43
- bool operator()(const ggml_backend_buffer_type_t & lhs, const ggml_backend_buffer_type_t & rhs) const {
44
- return strcmp(ggml_backend_buft_name(lhs), ggml_backend_buft_name(rhs)) < 0;
45
- }
46
- };
47
- std::map<ggml_backend_buffer_type_t, ggml_context_ptr, ggml_backend_buft_comparator> ctx_map;
48
-
49
- // create a context for each buffer type
50
- auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
51
- auto it = ctx_map.find(buft);
52
- if (it == ctx_map.end()) {
53
- ggml_init_params params = {
54
- /*.mem_size =*/ size_t(2u*(1 + n_stream)*n_layer_kv*ggml_tensor_overhead()),
55
- /*.mem_buffer =*/ NULL,
56
- /*.no_alloc =*/ true,
57
- };
58
-
59
- ggml_context * ctx = ggml_init(params);
60
- if (!ctx) {
61
- return nullptr;
62
- }
63
-
64
- ctx_map.emplace(buft, ctx);
65
-
66
- return ctx;
67
- }
68
-
69
- return it->second.get();
70
- };
71
-
72
- GGML_ASSERT(n_stream == 1 || n_stream == n_seq_max);
73
-
74
- v_heads.resize(n_stream);
75
- for (uint32_t s = 0; s < n_stream; ++s) {
76
- v_heads[s] = 0;
77
- }
78
-
79
- v_cells.resize(n_stream);
80
- for (uint32_t s = 0; s < n_stream; ++s) {
81
- v_cells[s].resize(kv_size);
82
- }
83
-
84
- // by default, all sequence ids are mapped to the 0th stream
85
- seq_to_stream.resize(LLAMA_MAX_SEQ, 0);
86
-
87
- if (n_stream > 1) {
88
- seq_to_stream.resize(n_stream, 0);
89
- for (uint32_t s = 0; s < n_stream; ++s) {
90
- seq_to_stream[s] = s;
91
- }
92
- }
93
-
94
- // [TAG_V_CACHE_VARIABLE]
95
- if (v_trans && hparams.is_n_embd_v_gqa_variable()) {
96
- LLAMA_LOG_WARN("%s: the V embeddings have different sizes across layers and FA is not enabled - padding V cache to %d\n",
97
- __func__, hparams.n_embd_v_gqa_max());
98
- }
99
-
100
- const bool is_mla = hparams.is_mla();
101
-
102
- for (uint32_t il = 0; il < hparams.n_layer; il++) {
103
- if (!hparams.has_kv(il)) {
104
- LLAMA_LOG_DEBUG("%s: layer %3d: does not have KV cache\n", __func__, il);
105
- continue;
106
- }
107
-
108
- if (filter && !filter(il)) {
109
- LLAMA_LOG_DEBUG("%s: layer %3d: filtered\n", __func__, il);
110
- continue;
111
- }
112
-
113
- // [TAG_V_CACHE_VARIABLE]
114
- const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
115
- const uint32_t n_embd_v_gqa = !v_trans ? hparams.n_embd_v_gqa(il) : hparams.n_embd_v_gqa_max();
116
-
117
- const char * dev_name = "CPU";
118
-
119
- ggml_backend_buffer_type_t buft = ggml_backend_cpu_buffer_type();
120
-
121
- if (offload) {
122
- auto * dev = model.dev_layer(il);
123
- buft = ggml_backend_dev_buffer_type(dev);
124
-
125
- dev_name = ggml_backend_dev_name(dev);
126
- }
127
-
128
- LLAMA_LOG_DEBUG("%s: layer %3d: dev = %s\n", __func__, il, dev_name);
129
-
130
- ggml_context * ctx = ctx_for_buft(buft);
131
- if (!ctx) {
132
- throw std::runtime_error("failed to create ggml context for kv cache");
133
- }
134
-
135
- const bool has_k = true;
136
- const bool has_v = !is_mla;
137
-
138
- ggml_tensor * k = has_k ? ggml_new_tensor_3d(ctx, type_k, n_embd_k_gqa, kv_size, n_stream) : nullptr;
139
- ggml_tensor * v = has_v ? ggml_new_tensor_3d(ctx, type_v, n_embd_v_gqa, kv_size, n_stream) : nullptr;
140
-
141
- has_k && ggml_format_name(k, "cache_k_l%d", il);
142
- has_v && ggml_format_name(v, "cache_v_l%d", il);
143
-
144
- std::vector<ggml_tensor *> k_stream;
145
- std::vector<ggml_tensor *> v_stream;
146
-
147
- for (uint32_t s = 0; s < n_stream; ++s) {
148
- k_stream.push_back(has_k ? ggml_view_2d(ctx, k, n_embd_k_gqa, kv_size, k->nb[1], s*k->nb[2]) : nullptr);
149
- v_stream.push_back(has_v ? ggml_view_2d(ctx, v, n_embd_v_gqa, kv_size, v->nb[1], s*v->nb[2]) : nullptr);
150
- }
151
-
152
- map_layer_ids[il] = layers.size();
153
-
154
- layers.push_back({ il, k, v, k_stream, v_stream, });
155
- }
156
-
157
- if (reuse) {
158
- LLAMA_LOG_DEBUG("%s: reusing layers:\n", __func__);
159
-
160
- for (uint32_t il = 0; il < hparams.n_layer; il++) {
161
- const int32_t il_reuse = reuse(il);
162
-
163
- if (il_reuse < 0) {
164
- LLAMA_LOG_DEBUG("%s: - layer %3d: no reuse\n", __func__, il);
165
- continue;
166
- }
167
-
168
- if (filter && !filter(il)) {
169
- LLAMA_LOG_DEBUG("%s: - layer %3d: filtered\n", __func__, il);
170
- continue;
171
- }
172
-
173
- GGML_ASSERT(map_layer_ids.find(il_reuse) != map_layer_ids.end());
174
-
175
- map_layer_ids[il] = map_layer_ids[il_reuse];
176
-
177
- LLAMA_LOG_DEBUG("%s: - layer %3d: reuse layer %d, is_swa = %d\n", __func__, il, il_reuse, hparams.is_swa(il));
178
- }
179
- }
180
-
181
- // allocate tensors and initialize the buffers to avoid NaNs in the padding
182
- for (auto & [buft, ctx] : ctx_map) {
183
- ggml_backend_buffer_t buf;
184
- if (model.hparams.no_alloc) {
185
- buf = ggml_backend_buft_alloc_buffer(buft, /*size =*/ 0); // dummy buffer
186
- for (ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != nullptr; t = ggml_get_next_tensor(ctx.get(), t)) {
187
- t->buffer = buf; // set dummy buffer for KV cache so that the backend scheduler won't try to allocate it
188
- }
189
- } else {
190
- buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx.get(), buft); // real buffer
191
- }
192
- if (!buf) {
193
- throw std::runtime_error("failed to allocate buffer for kv cache");
194
- }
195
-
196
- LLAMA_LOG_INFO("%s: %10s KV buffer size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf), ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
197
-
198
- ggml_backend_buffer_clear(buf, 0);
199
- ctxs_bufs.emplace_back(std::move(ctx), buf);
200
- }
201
-
202
- {
203
- const size_t memory_size_k = size_k_bytes();
204
- const size_t memory_size_v = size_v_bytes();
205
-
206
- LLAMA_LOG_INFO("%s: size = %7.2f MiB (%6u cells, %3d layers, %2u/%u seqs), K (%s): %7.2f MiB, V (%s): %7.2f MiB\n", __func__,
207
- (float)(memory_size_k + memory_size_v) / (1024.0f * 1024.0f), kv_size, (int) layers.size(), n_seq_max, n_stream,
208
- ggml_type_name(type_k), (float)memory_size_k / (1024.0f * 1024.0f),
209
- ggml_type_name(type_v), (float)memory_size_v / (1024.0f * 1024.0f));
210
- }
211
-
212
- const char * LLAMA_KV_CACHE_DEBUG = getenv("LLAMA_KV_CACHE_DEBUG");
213
- debug = LLAMA_KV_CACHE_DEBUG ? atoi(LLAMA_KV_CACHE_DEBUG) : 0;
214
- }
215
-
216
- void llama_kv_cache::clear(bool data) {
217
- for (uint32_t s = 0; s < n_stream; ++s) {
218
- v_cells[s].reset();
219
- v_heads[s] = 0;
220
- }
221
-
222
- if (data) {
223
- for (auto & [_, buf] : ctxs_bufs) {
224
- ggml_backend_buffer_clear(buf.get(), 0);
225
- }
226
- }
227
- }
228
-
229
- bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
230
- GGML_ASSERT(seq_id == -1 || (seq_id >= 0 && (size_t) seq_id < seq_to_stream.size()));
231
-
232
- if (p0 < 0) {
233
- p0 = 0;
234
- }
235
-
236
- if (p1 < 0) {
237
- p1 = std::numeric_limits<llama_pos>::max();
238
- }
239
-
240
- if (seq_id >= 0) {
241
- auto & cells = v_cells[seq_to_stream[seq_id]];
242
- auto & head = v_heads[seq_to_stream[seq_id]];
243
-
244
- uint32_t new_head = cells.size();
245
-
246
- for (uint32_t i = 0; i < cells.size(); ++i) {
247
- if (!cells.pos_in(i, p0, p1)) {
248
- continue;
249
- }
250
-
251
- if (cells.seq_has(i, seq_id) && cells.seq_rm(i, seq_id)) {
252
- if (new_head == cells.size()) {
253
- new_head = i;
254
- }
255
- }
256
- }
257
-
258
- // If we freed up a slot, set head to it so searching can start there.
259
- if (new_head != cells.size() && new_head < head) {
260
- head = new_head;
261
- }
262
- } else {
263
- // match any sequence
264
- for (uint32_t s = 0; s < n_stream; ++s) {
265
- auto & cells = v_cells[s];
266
- auto & head = v_heads[s];
267
-
268
- uint32_t new_head = cells.size();
269
-
270
- for (uint32_t i = 0; i < cells.size(); ++i) {
271
- if (!cells.pos_in(i, p0, p1)) {
272
- continue;
273
- }
274
-
275
- cells.rm(i);
276
-
277
- if (new_head == cells.size()) {
278
- new_head = i;
279
- }
280
- }
281
-
282
- // If we freed up a slot, set head to it so searching can start there.
283
- if (new_head != cells.size() && new_head < head) {
284
- head = new_head;
285
- }
286
- }
287
- }
288
-
289
- return true;
290
- }
291
-
292
- void llama_kv_cache::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
293
- GGML_ASSERT(seq_id_src >= 0 && (size_t) seq_id_src < seq_to_stream.size());
294
- GGML_ASSERT(seq_id_dst >= 0 && (size_t) seq_id_dst < seq_to_stream.size());
295
-
296
- const auto s0 = seq_to_stream[seq_id_src];
297
- const auto s1 = seq_to_stream[seq_id_dst];
298
-
299
- if (s0 == s1) {
300
- // since both sequences are in the same stream, no data copy is necessary
301
- // we just have to update the cells meta data
302
-
303
- auto & cells = v_cells[s0];
304
-
305
- if (seq_id_src == seq_id_dst) {
306
- return;
307
- }
308
-
309
- if (p0 < 0) {
310
- p0 = 0;
311
- }
312
-
313
- if (p1 < 0) {
314
- p1 = std::numeric_limits<llama_pos>::max();
315
- }
316
-
317
- for (uint32_t i = 0; i < cells.size(); ++i) {
318
- if (!cells.pos_in(i, p0, p1)) {
319
- continue;
320
- }
321
-
322
- if (cells.seq_has(i, seq_id_src)) {
323
- cells.seq_add(i, seq_id_dst);
324
- }
325
- }
326
-
327
- return;
328
- }
329
-
330
- // cross-stream sequence copies require to copy the actual buffer data
331
-
332
- bool is_full = true;
333
-
334
- if (p0 > 0 && p0 + 1 < (int) get_size()) {
335
- is_full = false;
336
- }
337
-
338
- if (p1 > 0 && p1 + 1 < (int) get_size()) {
339
- is_full = false;
340
- }
341
-
342
- GGML_ASSERT(is_full && "seq_cp() is only supported for full KV buffers");
343
-
344
- // enqueue the copy operation - the buffer copy will be performed during the next update
345
- sc_info.ssrc.push_back(s0);
346
- sc_info.sdst.push_back(s1);
347
-
348
- v_cells[s1].reset();
349
- for (uint32_t i = 0; i < v_cells[s0].size(); ++i) {
350
- if (v_cells[s0].seq_has(i, seq_id_src)) {
351
- llama_pos pos = v_cells[s0].pos_get(i);
352
- llama_pos shift = v_cells[s0].get_shift(i);
353
-
354
- llama_kv_cell_ext ext = v_cells[s0].ext_get(i);
355
-
356
- if (shift != 0) {
357
- pos -= shift;
358
- assert(pos >= 0);
359
- }
360
-
361
- v_cells[s1].pos_set(i, pos);
362
- v_cells[s1].seq_add(i, seq_id_dst);
363
-
364
- if (shift != 0) {
365
- v_cells[s1].pos_add(i, shift);
366
- }
367
-
368
- v_cells[s1].ext_set(i, ext);
369
- }
370
- }
371
-
372
- v_heads[s1] = v_heads[s0];
373
-
374
- //for (uint32_t s = 0; s < n_stream; ++s) {
375
- // LLAMA_LOG_WARN("%s: seq %d: min = %d, max = %d\n", __func__, s, v_cells[s].seq_pos_min(s), v_cells[s].seq_pos_max(s));
376
- //}
377
- }
378
-
379
- void llama_kv_cache::seq_keep(llama_seq_id seq_id) {
380
- GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
381
-
382
- auto & cells = v_cells[seq_to_stream[seq_id]];
383
- auto & head = v_heads[seq_to_stream[seq_id]];
384
-
385
- uint32_t new_head = cells.size();
386
-
387
- for (uint32_t i = 0; i < cells.size(); ++i) {
388
- if (cells.seq_keep(i, seq_id)) {
389
- if (new_head == cells.size()) {
390
- new_head = i;
391
- }
392
- }
393
- }
394
-
395
- // If we freed up a slot, set head to it so searching can start there.
396
- if (new_head != cells.size() && new_head < head) {
397
- head = new_head;
398
- }
399
- }
400
-
401
- void llama_kv_cache::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {
402
- GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
403
- GGML_ASSERT(hparams.n_pos_per_embd() == 1 && "seq_add() is only supported for n_pos_per_embd() == 1");
404
-
405
- auto & cells = v_cells[seq_to_stream[seq_id]];
406
- auto & head = v_heads[seq_to_stream[seq_id]];
407
-
408
- if (shift == 0) {
409
- return;
410
- }
411
-
412
- uint32_t new_head = cells.size();
413
-
414
- if (p0 < 0) {
415
- p0 = 0;
416
- }
417
-
418
- if (p1 < 0) {
419
- p1 = std::numeric_limits<llama_pos>::max();
420
- }
421
-
422
- // If there is no range then return early to avoid looping over all cells.
423
- if (p0 == p1) {
424
- return;
425
- }
426
-
427
- for (uint32_t i = 0; i < cells.size(); ++i) {
428
- if (!cells.pos_in(i, p0, p1)) {
429
- continue;
430
- }
431
-
432
- if (cells.seq_has(i, seq_id)) {
433
- if (cells.pos_add(i, shift)) {
434
- if (new_head == cells.size()) {
435
- new_head = i;
436
- }
437
- }
438
- }
439
- }
440
-
441
- // If we freed up a slot, set head to it so searching can start there.
442
- // Otherwise we just start the next search from the beginning.
443
- head = new_head != cells.size() ? new_head : 0;
444
- }
445
-
446
- void llama_kv_cache::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
447
- GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
448
- GGML_ASSERT(hparams.n_pos_per_embd() == 1 && "seq_div() is only supported for n_pos_per_embd() == 1");
449
-
450
- auto & cells = v_cells[seq_to_stream[seq_id]];
451
-
452
- if (d == 1) {
453
- return;
454
- }
455
-
456
- if (p0 < 0) {
457
- p0 = 0;
458
- }
459
-
460
- if (p1 < 0) {
461
- p1 = std::numeric_limits<llama_pos>::max();
462
- }
463
-
464
- // If there is no range then return early to avoid looping over the cache.
465
- if (p0 == p1) {
466
- return;
467
- }
468
-
469
- for (uint32_t i = 0; i < cells.size(); ++i) {
470
- if (!cells.pos_in(i, p0, p1)) {
471
- continue;
472
- }
473
-
474
- if (cells.seq_has(i, seq_id)) {
475
- cells.pos_div(i, d);
476
- }
477
- }
478
- }
479
-
480
- llama_pos llama_kv_cache::seq_pos_min(llama_seq_id seq_id) const {
481
- GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
482
-
483
- const auto & cells = v_cells[seq_to_stream[seq_id]];
484
-
485
- return cells.seq_pos_min(seq_id);
486
- }
487
-
488
- llama_pos llama_kv_cache::seq_pos_max(llama_seq_id seq_id) const {
489
- GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
490
-
491
- const auto & cells = v_cells[seq_to_stream[seq_id]];
492
-
493
- return cells.seq_pos_max(seq_id);
494
- }
495
-
496
- std::map<ggml_backend_buffer_type_t, size_t> llama_kv_cache::memory_breakdown() const {
497
- std::map<ggml_backend_buffer_type_t, size_t> ret;
498
- for (const auto & [ctx, buf] : ctxs_bufs) {
499
- ggml_backend_buffer_type_t buft = ggml_backend_buffer_get_type(buf.get());
500
-
501
- if (hparams.no_alloc) {
502
- GGML_ASSERT(ggml_backend_buffer_get_base(buf.get()) == nullptr);
503
- ret[buft] += ggml_backend_alloc_ctx_tensors_from_buft_size(ctx.get(), buft);
504
- } else {
505
- // GGML_ASSERT(ggml_backend_buffer_get_base(buf.get()) != nullptr); // multi_buffer does not have a defined base
506
- ret[buft] += ggml_backend_buffer_get_size(buf.get());
507
- }
508
- }
509
-
510
- return ret;
511
- }
512
-
513
- llama_memory_context_ptr llama_kv_cache::init_batch(
514
- llama_batch_allocr & balloc,
515
- uint32_t n_ubatch,
516
- bool embd_all) {
517
- GGML_UNUSED(embd_all);
518
-
519
- do {
520
- balloc.split_reset();
521
-
522
- std::vector<llama_ubatch> ubatches;
523
- while (true) {
524
- auto ubatch = n_stream == 1 ? balloc.split_simple(n_ubatch) : balloc.split_equal(n_ubatch, true);
525
-
526
- if (ubatch.n_tokens == 0) {
527
- break;
528
- }
529
-
530
- ubatches.push_back(std::move(ubatch)); // NOLINT
531
- }
532
-
533
- if (balloc.get_n_used() < balloc.get_n_tokens()) {
534
- // failed to find a suitable split
535
- break;
536
- }
537
-
538
- auto sinfos = prepare(ubatches);
539
- if (sinfos.empty()) {
540
- break;
541
- }
542
-
543
- return std::make_unique<llama_kv_cache_context>(
544
- this, std::move(sinfos), std::move(ubatches));
545
- } while (false);
546
-
547
- return std::make_unique<llama_kv_cache_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
548
- }
549
-
550
- llama_memory_context_ptr llama_kv_cache::init_full() {
551
- return std::make_unique<llama_kv_cache_context>(this);
552
- }
553
-
554
- llama_memory_context_ptr llama_kv_cache::init_update(llama_context * lctx, bool optimize) {
555
- GGML_UNUSED(optimize);
556
-
557
- bool do_shift = get_has_shift();
558
-
559
- return std::make_unique<llama_kv_cache_context>(this, lctx, do_shift, std::move(sc_info));
560
- }
561
-
562
- llama_kv_cache::slot_info_vec_t llama_kv_cache::prepare(const std::vector<llama_ubatch> & ubatches) {
563
- llama_kv_cache::slot_info_vec_t res;
564
-
565
- struct state_t {
566
- slot_info sinfo; // slot info for the ubatch
567
-
568
- std::vector<uint32_t> v_heads_old; // old positions of the heads, before placing the ubatch
569
-
570
- std::vector<llama_kv_cells> v_cells; // copy of the old cells, before placing the ubatch
571
- };
572
-
573
- // remember the old state of the cells so we can restore it in the end
574
- std::vector<state_t> states;
575
-
576
- bool success = true;
577
-
578
- for (const auto & ubatch : ubatches) {
579
- // only find a suitable slot for the ubatch. don't modify the cells yet
580
- const auto sinfo_new = find_slot(ubatch, false);
581
- if (sinfo_new.empty()) {
582
- success = false;
583
- break;
584
- }
585
-
586
- // remember the position that we found
587
- res.push_back(sinfo_new);
588
-
589
- // store the old state of the cells in the recovery stack
590
- {
591
- state_t state = { sinfo_new, v_heads, {} };
592
-
593
- for (uint32_t s = 0; s < sinfo_new.n_stream(); ++s) {
594
- auto & cells = v_cells[sinfo_new.strm[s]];
595
-
596
- state.v_cells.push_back(cells.cp(sinfo_new.idxs[s]));
597
- }
598
-
599
- states.push_back(std::move(state));
600
- }
601
-
602
- // now emplace the ubatch
603
- apply_ubatch(sinfo_new, ubatch);
604
- }
605
-
606
- GGML_ASSERT(!states.empty() || !success);
607
-
608
- // iterate backwards and restore the cells to their original state
609
- for (auto it = states.rbegin(); it != states.rend(); ++it) {
610
- const auto & sinfo = it->sinfo;
611
-
612
- for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
613
- auto & cells = v_cells[sinfo.strm[s]];
614
- auto & head = v_heads[sinfo.strm[s]];
615
-
616
- cells.set(sinfo.idxs[s], it->v_cells[s]);
617
- head = it->v_heads_old[s];
618
- }
619
- }
620
-
621
- if (!success) {
622
- return {};
623
- }
624
-
625
- return res;
626
- }
627
-
628
- bool llama_kv_cache::update(llama_context * lctx, bool do_shift, const stream_copy_info & sc_info) {
629
- bool updated = false;
630
-
631
- auto * sched = lctx->get_sched();
632
-
633
- if (!sc_info.empty()) {
634
- assert(n_stream > 1 && "stream copy should never happen with a single stream");
635
-
636
- llama_synchronize(lctx);
637
-
638
- const size_t n_copy = sc_info.ssrc.size();
639
-
640
- for (size_t i = 0; i < n_copy; ++i) {
641
- const auto ssrc = sc_info.ssrc[i];
642
- const auto sdst = sc_info.sdst[i];
643
-
644
- assert(ssrc < n_stream);
645
- assert(sdst < n_stream);
646
-
647
- LLAMA_LOG_DEBUG("%s: copying KV buffer: stream %d to stream %d\n", __func__, ssrc, sdst);
648
-
649
- assert(ssrc != sdst);
650
-
651
- for (uint32_t il = 0; il < layers.size(); ++il) {
652
- const auto & layer = layers[il];
653
-
654
- ggml_backend_tensor_copy(layer.k_stream[ssrc], layer.k_stream[sdst]);
655
-
656
- if (layer.v_stream[ssrc]) {
657
- ggml_backend_tensor_copy(layer.v_stream[ssrc], layer.v_stream[sdst]);
658
- }
659
- }
660
- }
661
- }
662
-
663
- if (do_shift) {
664
- if (!get_can_shift()) {
665
- GGML_ABORT("The current KV cache / model configuration does not support K-shift");
666
- }
667
-
668
- LLAMA_LOG_DEBUG("%s: applying K-shift\n", __func__);
669
-
670
- // apply K-shift if needed
671
- if (hparams.rope_type != LLAMA_ROPE_TYPE_NONE) {
672
- ggml_backend_sched_reset(sched);
673
-
674
- auto * res = lctx->get_gf_res_reserve();
675
-
676
- res->reset();
677
-
678
- auto * gf = build_graph_shift(res, lctx);
679
- if (!ggml_backend_sched_alloc_graph(sched, gf)) {
680
- LLAMA_LOG_ERROR("%s: failed to allocate compute graph for K-shift\n", __func__);
681
- return updated;
682
- }
683
-
684
- res->set_inputs(nullptr);
685
-
686
- if (lctx->graph_compute(gf, false) != GGML_STATUS_SUCCESS) {
687
- LLAMA_LOG_ERROR("%s: failed to compute K-shift\n", __func__);
688
- return updated;
689
- }
690
-
691
- updated = true;
692
- }
693
-
694
- for (uint32_t s = 0; s < n_stream; ++s) {
695
- auto & cells = v_cells[s];
696
-
697
- cells.reset_shift();
698
- }
699
- }
700
-
701
- return updated;
702
- }
703
-
704
- llama_kv_cache::slot_info llama_kv_cache::find_slot(const llama_ubatch & ubatch, bool cont) const {
705
-
706
- if (debug > 0) {
707
- for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
708
- const auto seq_id = ubatch.seq_id_unq[s];
709
- const auto stream_id = seq_to_stream[seq_id];
710
- const auto & cells = v_cells[stream_id];
711
- const uint32_t head_cur = v_heads[stream_id];
712
-
713
- LLAMA_LOG_DEBUG("%s: stream[%d], n = %5d, used = %5d, head = %5d, size = %5d, n_swa = %5d\n",
714
- __func__, stream_id, cells.used_max_p1(), cells.get_used(), head_cur, get_size(), n_swa);
715
-
716
- if ((debug == 2 && n_swa > 0) || debug > 2) {
717
- std::string ss;
718
- for (uint32_t i = 0; i < cells.size(); ++i) {
719
- if (cells.is_empty(i)) {
720
- ss += '.';
721
- } else {
722
- assert(cells.seq_count(i) >= 1);
723
-
724
- if (cells.seq_count(i) == 1) {
725
- ss += std::to_string(cells.seq_get(i));
726
- } else {
727
- ss += 'M';
728
- }
729
- }
730
- if (i%256 == 255) {
731
- ss += " *";
732
- ss += '\n';
733
- }
734
- }
735
- LLAMA_LOG_DEBUG("\n%s\n", ss.c_str());
736
- }
737
-
738
- if ((debug == 2 && n_swa > 0) || debug > 2) {
739
- std::string ss;
740
- for (uint32_t i = 0; i < cells.size(); ++i) {
741
- std::string cur;
742
- if (cells.is_empty(i)) {
743
- cur = '.';
744
- } else {
745
- cur = std::to_string(cells.pos_get(i));
746
- }
747
- const int n = cur.size();
748
- for (int j = 0; j < 5 - n; ++j) {
749
- cur += ' ';
750
- }
751
- ss += cur;
752
- if (i%256 == 255) {
753
- ss += " *";
754
- }
755
- if (i%64 == 63) {
756
- ss += '\n';
757
- }
758
- }
759
- LLAMA_LOG_DEBUG("\n%s\n", ss.c_str());
760
- }
761
-
762
- for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
763
- if (cells.seq_pos_min(s) < 0) {
764
- continue;
765
- }
766
-
767
- LLAMA_LOG_DEBUG("%s: stream[%d] min[%d] = %5d, max[%d] = %5d\n", __func__, stream_id, s, cells.seq_pos_min(s), s, cells.seq_pos_max(s));
768
- }
769
- }
770
- }
771
-
772
- uint32_t n_tokens = ubatch.n_tokens;
773
- uint32_t n_seqs = 1;
774
-
775
- if (n_stream > 1) {
776
- GGML_ASSERT(n_tokens % ubatch.n_seqs_unq == 0);
777
-
778
- n_seqs = ubatch.n_seqs_unq;
779
- n_tokens = n_tokens / n_seqs;
780
- }
781
-
782
- slot_info res = {
783
- /*.s0 =*/ LLAMA_MAX_SEQ,
784
- /*.s1 =*/ 0,
785
- /*.strm =*/ { },
786
- /*.idxs =*/ { },
787
- };
788
-
789
- res.resize(n_seqs);
790
-
791
- for (uint32_t s = 0; s < n_seqs; ++s) {
792
- const auto seq_id = ubatch.seq_id_unq[s];
793
-
794
- if (n_stream > 1) {
795
- GGML_ASSERT(ubatch.n_seq_id[s*n_tokens] == 1);
796
- GGML_ASSERT(ubatch.seq_id [s*n_tokens][0] == seq_id);
797
- }
798
-
799
- res.s0 = std::min<uint32_t>(res.s0, seq_to_stream[seq_id]);
800
- res.s1 = std::max<uint32_t>(res.s1, seq_to_stream[seq_id]);
801
-
802
- res.strm[s] = seq_to_stream[seq_id];
803
- res.idxs[s].reserve(n_tokens);
804
-
805
- const auto & cells = v_cells[seq_to_stream[seq_id]];
806
-
807
- uint32_t head_cur = v_heads[seq_to_stream[seq_id]];
808
-
809
- // if we have enough unused cells before the current head ->
810
- // better to start searching from the beginning of the cache, hoping to fill it
811
- if (head_cur > cells.get_used() + 2*n_tokens) {
812
- head_cur = 0;
813
- }
814
-
815
- if (n_tokens > cells.size()) {
816
- LLAMA_LOG_ERROR("%s: n_tokens = %d > size = %u\n", __func__, n_tokens, cells.size());
817
- return { };
818
- }
819
-
820
- uint32_t n_tested = 0;
821
-
822
- // for continuous slots, we test that all tokens in the ubatch fit, starting from the current head
823
- // for non-continuous slots, we test the tokens one by one
824
- const uint32_t n_test = cont ? n_tokens : 1;
825
-
826
- while (true) {
827
- if (head_cur + n_test > cells.size()) {
828
- n_tested += cells.size() - head_cur;
829
- head_cur = 0;
830
- continue;
831
- }
832
-
833
- for (uint32_t i = 0; i < n_test; i++) {
834
- const auto idx = head_cur;
835
-
836
- head_cur++;
837
- n_tested++;
838
-
839
- //const llama_pos pos = ubatch.pos[i];
840
- //const llama_seq_id seq_id = ubatch.seq_id[i][0];
841
-
842
- // can we use this cell? either:
843
- // - the cell is empty
844
- // - the cell is occupied only by one sequence:
845
- // - (disabled) mask causally, if the sequence is the same as the one we are inserting
846
- // - mask SWA, using current max pos for that sequence in the cache
847
- // always insert in the cell with minimum pos
848
- bool can_use = cells.is_empty(idx);
849
-
850
- if (!can_use && cells.seq_count(idx) == 1) {
851
- const llama_pos pos_cell = cells.pos_get(idx);
852
-
853
- // (disabled) causal mask
854
- // note: it's better to purge any "future" tokens beforehand
855
- //if (cells.seq_has(idx, seq_id)) {
856
- // can_use = pos_cell >= pos;
857
- //}
858
-
859
- if (!can_use) {
860
- const llama_seq_id seq_id_cell = cells.seq_get(idx);
861
-
862
- // SWA mask
863
- if (llama_hparams::is_masked_swa(n_swa, swa_type, pos_cell, cells.seq_pos_max(seq_id_cell) + 1)) {
864
- can_use = true;
865
- }
866
- }
867
- }
868
-
869
- if (can_use) {
870
- res.idxs[s].push_back(idx);
871
- } else {
872
- if (cont) {
873
- break;
874
- }
875
- }
876
- }
877
-
878
- if (res.idxs[s].size() == n_tokens) {
879
- break;
880
- }
881
-
882
- if (cont) {
883
- res.idxs[s].clear();
884
- }
885
-
886
- if (n_tested >= cells.size()) {
887
- //LLAMA_LOG_ERROR("%s: failed to find a slot for %d tokens\n", __func__, n_tokens);
888
- return { };
889
- }
890
- }
891
-
892
- // we didn't find a suitable slot - return empty result
893
- if (res.idxs[s].size() < n_tokens) {
894
- return { };
895
- }
896
- }
897
-
898
- assert(res.s1 >= res.s0);
899
-
900
- return res;
901
- }
902
-
903
- void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & ubatch) {
904
- // keep track of the max sequence position that we would overwrite with this ubatch
905
- // for non-SWA cache, this would be always empty
906
- llama_seq_id seq_pos_max_rm[LLAMA_MAX_SEQ];
907
- for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
908
- seq_pos_max_rm[s] = -1;
909
- }
910
-
911
- assert(ubatch.n_tokens == sinfo.n_stream()*sinfo.size());
912
-
913
- for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
914
- for (uint32_t ii = 0; ii < sinfo.size(); ++ii) {
915
- const uint32_t i = s*sinfo.size() + ii;
916
-
917
- auto & cells = v_cells[sinfo.strm[s]];
918
-
919
- const auto idx = sinfo.idxs[s][ii];
920
-
921
- if (!cells.is_empty(idx)) {
922
- assert(cells.seq_count(idx) == 1);
923
-
924
- const llama_seq_id seq_id = cells.seq_get(idx);
925
- const llama_pos pos = cells.pos_get(idx);
926
-
927
- seq_pos_max_rm[seq_id] = std::max(seq_pos_max_rm[seq_id], pos);
928
-
929
- cells.rm(idx);
930
- }
931
-
932
- cells.pos_set(idx, ubatch.pos[i]);
933
-
934
- if (ubatch.is_pos_2d()) {
935
- llama_kv_cell_ext ext {
936
- /*.x =*/ ubatch.pos[i + ubatch.n_tokens*2],
937
- /*.y =*/ ubatch.pos[i + ubatch.n_tokens],
938
- };
939
- cells.ext_set(idx, ext);
940
- }
941
-
942
- for (int32_t s = 0; s < ubatch.n_seq_id[i]; s++) {
943
- cells.seq_add(idx, ubatch.seq_id[i][s]);
944
- }
945
- }
946
- }
947
-
948
- // note: we want to preserve the invariant that all positions between [pos_min, pos_max] for each sequence
949
- // will be present in the cache. so we have to purge any position which is less than those we would overwrite
950
- // ref: https://github.com/ggml-org/llama.cpp/pull/13746#issuecomment-2916057092
951
- for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
952
- if (seq_pos_max_rm[s] == -1) {
953
- continue;
954
- }
955
-
956
- GGML_ASSERT(s < seq_to_stream.size());
957
-
958
- auto & cells = v_cells[seq_to_stream[s]];
959
-
960
- if (cells.seq_pos_min(s) <= seq_pos_max_rm[s]) {
961
- LLAMA_LOG_DEBUG("%s: purging positions [%d, %d] of sequence %d from KV cache\n",
962
- __func__, cells.seq_pos_min(s), seq_pos_max_rm[s], s);
963
-
964
- seq_rm(s, cells.seq_pos_min(s), seq_pos_max_rm[s] + 1);
965
- }
966
- }
967
-
968
- // move the head at the end of the slot
969
- for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
970
- auto & head = v_heads[sinfo.strm[s]];
971
-
972
- head = sinfo.idxs[s].back() + 1;
973
- }
974
- }
975
-
976
- bool llama_kv_cache::get_can_shift() const {
977
- // Step35 uses per-layer RoPE dims; K-shift assumes a single global n_rot.
978
- if (model.arch == LLM_ARCH_STEP35) {
979
- return false;
980
- }
981
- if (hparams.n_pos_per_embd() > 1) {
982
- return false;
983
- }
984
- return true;
985
- }
986
-
987
- uint32_t llama_kv_cache::get_size() const {
988
- const auto & cells = v_cells[seq_to_stream[0]];
989
-
990
- return cells.size();
991
- }
992
-
993
- uint32_t llama_kv_cache::get_n_stream() const {
994
- return n_stream;
995
- }
996
-
997
- bool llama_kv_cache::get_has_shift() const {
998
- bool result = false;
999
-
1000
- for (uint32_t s = 0; s < n_stream; ++s) {
1001
- result |= v_cells[s].get_has_shift();
1002
- }
1003
-
1004
- return result;
1005
- }
1006
-
1007
- uint32_t llama_kv_cache::get_n_kv(const slot_info & sinfo) const {
1008
- uint32_t result = 0;
1009
-
1010
- // pad the n_kv value so that the graph remains constant across batches and can be reused
1011
- // note: this also helps some backends with performance (f.ex https://github.com/ggml-org/llama.cpp/pull/16812#issuecomment-3455112220)
1012
- const uint32_t n_pad_cur = std::max(n_pad, 256u);
1013
-
1014
- for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
1015
- const auto & cells = v_cells[sinfo.strm[s]];
1016
-
1017
- result = std::max(std::min(cells.size(), std::max(n_pad_cur, GGML_PAD(cells.used_max_p1(), n_pad_cur))), result);
1018
- }
1019
-
1020
- return result;
1021
- }
1022
-
1023
- ggml_tensor * llama_kv_cache::get_k(ggml_context * ctx, int32_t il, uint32_t n_kv, const slot_info & sinfo) const {
1024
- const int32_t ikv = map_layer_ids.at(il);
1025
-
1026
- auto * k = layers[ikv].k;
1027
-
1028
- const uint64_t kv_size = get_size();
1029
- const uint64_t n_embd_k_gqa = k->ne[0];
1030
-
1031
- assert(n_embd_k_gqa == hparams.n_embd_k_gqa(il));
1032
-
1033
- const uint32_t ns = sinfo.s1 - sinfo.s0 + 1;
1034
-
1035
- return ggml_view_4d(ctx, k,
1036
- hparams.n_embd_head_k(il), hparams.n_head_kv(il), n_kv, ns,
1037
- ggml_row_size(k->type, hparams.n_embd_head_k(il)),
1038
- ggml_row_size(k->type, n_embd_k_gqa),
1039
- ggml_row_size(k->type, n_embd_k_gqa*kv_size),
1040
- ggml_row_size(k->type, n_embd_k_gqa*kv_size)*sinfo.s0);
1041
- }
1042
-
1043
- ggml_tensor * llama_kv_cache::get_v(ggml_context * ctx, int32_t il, uint32_t n_kv, const slot_info & sinfo) const {
1044
- const int32_t ikv = map_layer_ids.at(il);
1045
-
1046
- auto * v = layers[ikv].v;
1047
-
1048
- const uint64_t kv_size = get_size();
1049
- const uint64_t n_embd_v_gqa = v->ne[0];
1050
-
1051
- // [TAG_V_CACHE_VARIABLE]
1052
- assert(n_embd_v_gqa >= hparams.n_embd_v_gqa(il));
1053
-
1054
- const uint32_t ns = sinfo.s1 - sinfo.s0 + 1;
1055
-
1056
- if (!v_trans) {
1057
- // note: v->nb[1] <= v->nb[2]
1058
- return ggml_view_4d(ctx, v,
1059
- hparams.n_embd_head_v(il), hparams.n_head_kv(il), n_kv, ns,
1060
- ggml_row_size(v->type, hparams.n_embd_head_v(il)), // v->nb[1]
1061
- ggml_row_size(v->type, n_embd_v_gqa), // v->nb[2]
1062
- ggml_row_size(v->type, n_embd_v_gqa*kv_size), // v->nb[3]
1063
- ggml_row_size(v->type, n_embd_v_gqa*kv_size)*sinfo.s0);
1064
- }
1065
-
1066
- // note: v->nb[1] > v->nb[2]
1067
- return ggml_view_4d(ctx, v,
1068
- n_kv, hparams.n_head_kv(il), hparams.n_embd_head_v(il), ns,
1069
- ggml_row_size(v->type, kv_size*hparams.n_embd_head_v(il)), // v->nb[1]
1070
- ggml_row_size(v->type, kv_size), // v->nb[2]
1071
- ggml_row_size(v->type, kv_size*n_embd_v_gqa), // v->nb[3]
1072
- ggml_row_size(v->type, kv_size*n_embd_v_gqa)*sinfo.s0);
1073
- }
1074
-
1075
- ggml_tensor * llama_kv_cache::cpy_k(ggml_context * ctx, ggml_tensor * k_cur, ggml_tensor * k_idxs, int32_t il, const slot_info & sinfo) const {
1076
- GGML_UNUSED(sinfo);
1077
-
1078
- const int32_t ikv = map_layer_ids.at(il);
1079
-
1080
- ggml_tensor * k = layers[ikv].k;
1081
-
1082
- const int64_t n_embd_head = k_cur->ne[0];
1083
- const int64_t n_head = k_cur->ne[1];
1084
- const int64_t n_tokens = k_cur->ne[2];
1085
-
1086
- const int64_t n_embd_gqa = n_embd_head*n_head;
1087
-
1088
- // we can merge dims 0 and 1
1089
- // TODO: add ggml helper function for this?
1090
- GGML_ASSERT(ggml_row_size(k_cur->type, n_embd_head) == k_cur->nb[1]);
1091
-
1092
- k_cur = ggml_view_2d(ctx, k_cur, n_embd_gqa, n_tokens, k_cur->nb[2], 0);
1093
-
1094
- const int64_t n_stream = k->ne[2];
1095
-
1096
- if (n_stream > 1) {
1097
- const int64_t kv_size = get_size();
1098
-
1099
- assert(n_embd_gqa == k->ne[0]);
1100
- assert(kv_size == k->ne[1]);
1101
-
1102
- // merge the buffer across all streams because the idxs are global
1103
- k = ggml_reshape_2d(ctx, k, n_embd_gqa, kv_size*n_stream);
1104
- }
1105
-
1106
- // store the current K values into the cache
1107
- return ggml_set_rows(ctx, k, k_cur, k_idxs);
1108
- }
1109
-
1110
- ggml_tensor * llama_kv_cache::cpy_v(ggml_context * ctx, ggml_tensor * v_cur, ggml_tensor * v_idxs, int32_t il, const slot_info & sinfo) const {
1111
- GGML_UNUSED(sinfo);
1112
-
1113
- const int32_t ikv = map_layer_ids.at(il);
1114
-
1115
- auto * v = layers[ikv].v;
1116
-
1117
- const int64_t n_embd_head = v_cur->ne[0];
1118
- const int64_t n_head = v_cur->ne[1];
1119
- const int64_t n_tokens = v_cur->ne[2];
1120
-
1121
- const int64_t n_embd_gqa = n_embd_head*n_head;
1122
-
1123
- // we can merge dims 0 and 1
1124
- GGML_ASSERT(ggml_row_size(v_cur->type, n_embd_head) == v_cur->nb[1]);
1125
-
1126
- const int64_t n_stream = v->ne[2];
1127
-
1128
- // take this branch when FA is enabled (the V cache is not transposed)
1129
- if (!v_trans) {
1130
- v_cur = ggml_view_2d(ctx, v_cur, n_embd_gqa, n_tokens, v_cur->nb[2], 0);
1131
-
1132
- if (n_stream > 1) {
1133
- const int64_t kv_size = get_size();
1134
-
1135
- assert(n_embd_gqa == v->ne[0]);
1136
- assert(kv_size == v->ne[1]);
1137
-
1138
- // merge the buffer across all streams because the idxs are global
1139
- v = ggml_reshape_2d(ctx, v, n_embd_gqa, kv_size*n_stream);
1140
- }
1141
-
1142
- return ggml_set_rows(ctx, v, v_cur, v_idxs);
1143
- }
1144
-
1145
- if (ggml_row_size(v_cur->type, n_embd_gqa) == v_cur->nb[2]) {
1146
- // we can merge dims 0, 1 and 2
1147
- v_cur = ggml_reshape_2d(ctx, v_cur, n_embd_gqa, n_tokens);
1148
- } else {
1149
- // otherwise -> make a copy to get contiguous data
1150
- v_cur = ggml_cont_2d (ctx, v_cur, n_embd_gqa, n_tokens);
1151
- }
1152
-
1153
- // [TAG_V_CACHE_VARIABLE]
1154
- if (n_embd_gqa < v->ne[0]) {
1155
- v_cur = ggml_pad(ctx, v_cur, v->ne[0] - n_embd_gqa, 0, 0, 0);
1156
- }
1157
-
1158
- // in this branch the v_idxs are constructed in such a way that each row is a single head element
1159
- ggml_tensor * v_view = ggml_reshape_2d(ctx, v, 1, ggml_nelements(v));
1160
-
1161
- v_cur = ggml_reshape_2d(ctx, v_cur, 1, ggml_nelements(v_cur));
1162
-
1163
- return ggml_set_rows(ctx, v_view, v_cur, v_idxs);
1164
- }
1165
-
1166
- ggml_tensor * llama_kv_cache::build_input_k_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
1167
- const uint32_t n_tokens = ubatch.n_tokens;
1168
-
1169
- ggml_tensor * k_idxs = ggml_new_tensor_1d(ctx, GGML_TYPE_I64, n_tokens);
1170
-
1171
- ggml_set_input(k_idxs);
1172
-
1173
- return k_idxs;
1174
- }
1175
-
1176
- ggml_tensor * llama_kv_cache::build_input_v_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
1177
- const uint32_t n_tokens = ubatch.n_tokens;
1178
-
1179
- ggml_tensor * v_idxs;
1180
-
1181
- if (!v_trans) {
1182
- v_idxs = ggml_new_tensor_1d(ctx, GGML_TYPE_I64, n_tokens);
1183
- } else {
1184
- v_idxs = ggml_new_tensor_1d(ctx, GGML_TYPE_I64, n_tokens*hparams.n_embd_v_gqa_max());
1185
- }
1186
-
1187
- ggml_set_input(v_idxs);
1188
-
1189
- return v_idxs;
1190
- }
1191
-
1192
- void llama_kv_cache::set_input_k_idxs(ggml_tensor * dst, const llama_ubatch * ubatch, const slot_info & sinfo) const {
1193
- const uint32_t n_tokens = ubatch->n_tokens;
1194
- GGML_ASSERT(n_tokens == (int64_t) sinfo.size()*sinfo.n_stream());
1195
-
1196
- GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1197
- int64_t * data = (int64_t *) dst->data;
1198
-
1199
- for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
1200
- const int64_t offs = sinfo.strm[s]*get_size();
1201
-
1202
- for (uint32_t i = 0; i < sinfo.size(); ++i) {
1203
- data[s*sinfo.size() + i] = offs + sinfo.idxs[s][i];
1204
- }
1205
- }
1206
- }
1207
-
1208
- void llama_kv_cache::set_input_v_idxs(ggml_tensor * dst, const llama_ubatch * ubatch, const slot_info & sinfo) const {
1209
- const uint32_t n_tokens = ubatch->n_tokens;
1210
- GGML_ASSERT(n_tokens == (int64_t) sinfo.size()*sinfo.n_stream());
1211
-
1212
- GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1213
- int64_t * data = (int64_t *) dst->data;
1214
-
1215
- if (!v_trans) {
1216
- for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
1217
- const int64_t offs = sinfo.strm[s]*get_size();
1218
-
1219
- for (uint32_t i = 0; i < sinfo.size(); ++i) {
1220
- data[s*sinfo.size() + i] = offs + sinfo.idxs[s][i];
1221
- }
1222
- }
1223
- } else {
1224
- // note: the V cache is transposed when not using flash attention
1225
- const int64_t kv_size = get_size();
1226
-
1227
- const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa_max();
1228
-
1229
- for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
1230
- const int64_t offs = sinfo.strm[s]*kv_size*n_embd_v_gqa;
1231
-
1232
- for (uint32_t i = 0; i < sinfo.size(); ++i) {
1233
- for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
1234
- data[s*sinfo.size()*n_embd_v_gqa + i*n_embd_v_gqa + j] = offs + j*kv_size + sinfo.idxs[s][i];
1235
- }
1236
- }
1237
- }
1238
- }
1239
- }
1240
-
1241
- void llama_kv_cache::set_input_k_shift(ggml_tensor * dst) const {
1242
- GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1243
-
1244
- int32_t * data = (int32_t *) dst->data;
1245
-
1246
- for (uint32_t s = 0; s < n_stream; ++s) {
1247
- const auto & cells = v_cells[s];
1248
-
1249
- for (uint32_t i = 0; i < cells.size(); ++i) {
1250
- data[s*cells.size() + i] = cells.is_empty(i) ? 0 : cells.get_shift(i);
1251
- }
1252
- }
1253
- }
1254
-
1255
- struct args_set_input_kq_mask {
1256
- const llama_hparams & hparams;
1257
- const llama_ubatch * ubatch;
1258
-
1259
- const std::vector<llama_kv_cells> & v_cells;
1260
- const std::vector<uint32_t> & seq_to_stream;
1261
-
1262
- uint32_t n_swa;
1263
- llama_swa_type swa_type;
1264
-
1265
- int64_t n_kv;
1266
- int64_t n_stream;
1267
- int64_t n_tps;
1268
- };
1269
-
1270
- template<bool causal, bool swa, bool is_2d, bool alibi>
1271
- static void set_input_kq_mask_impl(const args_set_input_kq_mask & args, float * data) {
1272
- //const auto & hparams = args.hparams;
1273
- const auto & ubatch = args.ubatch;
1274
-
1275
- const auto & v_cells = args.v_cells;
1276
- const auto & seq_to_stream = args.seq_to_stream;
1277
-
1278
- const uint32_t n_swa = args.n_swa;
1279
- const llama_swa_type swa_type = args.swa_type;
1280
-
1281
- const int64_t n_kv = args.n_kv;
1282
- const int64_t n_stream = args.n_stream;
1283
- const int64_t n_tps = args.n_tps;
1284
-
1285
- // the min position in the batch for each sequence
1286
- llama_pos seq_pos_min[LLAMA_MAX_SEQ];
1287
- std::fill(seq_pos_min, seq_pos_min + LLAMA_MAX_SEQ, INT32_MAX);
1288
-
1289
- for (uint32_t i = 0; i < ubatch->n_tokens; ++i) {
1290
- const llama_seq_id seq_id = ubatch->seq_id[i][0];
1291
-
1292
- seq_pos_min[seq_id] = std::min(seq_pos_min[seq_id], ubatch->pos[i]);
1293
- }
1294
-
1295
- for (uint32_t s = 0; s < n_stream; ++s) {
1296
- // bookkeeping of the KQ mask cells that could change for other tokens of the same sequence
1297
- std::unordered_map<llama_seq_id, uint32_t> seq_srct;
1298
- std::unordered_map<llama_seq_id, std::vector<uint32_t>> seq_idxs;
1299
-
1300
- for (uint32_t ii = 0; ii < n_tps; ++ii) {
1301
- const uint32_t i = s*n_tps + ii;
1302
-
1303
- const llama_seq_id seq_id = ubatch->seq_id[i][0];
1304
-
1305
- const auto & cells = v_cells.at(seq_to_stream[seq_id]);
1306
-
1307
- llama_pos p0 = -1;
1308
- const llama_pos p1 = ubatch->pos[i];
1309
-
1310
- // for M-RoPE
1311
- const llama_pos p1_x = is_2d ? ubatch->pos[i + ubatch->n_tokens*2] : 0;
1312
- const llama_pos p1_y = is_2d ? ubatch->pos[i + ubatch->n_tokens] : 0;
1313
-
1314
- const uint64_t idst = n_kv*i;
1315
-
1316
- // for tokens of the same sequence, the mask is mostly the same, so we can reuse it
1317
- // the only cells that could change are the ones that are with similar positions as the
1318
- // ones in the batch (i.e. due to causal masking, SWA, etc.)
1319
- // keep track of those cells and shortcut the loop to save time
1320
- // note: this optimization is not compatible with Alibi position encoding
1321
- // ref: https://github.com/ggml-org/llama.cpp/pull/18842
1322
- bool prev = false;
1323
-
1324
- auto & idxs = seq_idxs[seq_id];
1325
-
1326
- if (!alibi) {
1327
- if (seq_srct.find(seq_id) != seq_srct.end()) {
1328
- const uint32_t srct = seq_srct[seq_id];
1329
-
1330
- const uint64_t idst_prev = n_kv*srct;
1331
-
1332
- std::copy(data + idst_prev, data + idst_prev + n_kv, data + idst);
1333
-
1334
- prev = true;
1335
- } else {
1336
- idxs.clear();
1337
- idxs.reserve(ubatch->n_tokens + n_swa + 32);
1338
-
1339
- seq_srct[seq_id] = i;
1340
- }
1341
- }
1342
-
1343
- for (uint32_t jj = 0; jj < n_kv; ++jj) {
1344
- uint32_t j = jj;
1345
-
1346
- // we have an exiting mask for this sequence -> update just seq_idxs
1347
- if (!alibi) {
1348
- if (prev) {
1349
- if (jj >= idxs.size()) {
1350
- break;
1351
- }
1352
-
1353
- j = idxs[jj];
1354
- }
1355
- }
1356
-
1357
- if (cells.is_empty(j)) {
1358
- goto skip;
1359
- }
1360
-
1361
- // mask the token if not the same sequence
1362
- if (!cells.seq_has(j, seq_id)) {
1363
- goto skip;
1364
- }
1365
-
1366
- p0 = cells.pos_get(j);
1367
-
1368
- if (!alibi) {
1369
- if (!prev) {
1370
- // record all cells for which: p0 >= seq_pos_min[seq_id] - n_swa - 32
1371
- if (p0 + (int32_t) (n_swa + 32) >= seq_pos_min[seq_id]) {
1372
- idxs.push_back(j);
1373
- }
1374
- }
1375
- }
1376
-
1377
- if (causal) {
1378
- // mask future tokens
1379
- if (p0 > p1) {
1380
- goto skip;
1381
- }
1382
-
1383
- // M-RoPE causal mask
1384
- if (is_2d) {
1385
- if (p0 == p1) {
1386
- const auto & p0_ext = cells.ext_get(j);
1387
-
1388
- if (p0_ext.is_2d_gt(p1_x, p1_y)) {
1389
- goto skip;
1390
- }
1391
- }
1392
- }
1393
- }
1394
-
1395
- // apply SWA if any
1396
- if (swa) {
1397
- if (llama_hparams::is_masked_swa(n_swa, swa_type, p0, p1)) {
1398
- goto skip;
1399
- }
1400
- }
1401
-
1402
- if (alibi) {
1403
- data[idst + j] = -std::abs(p0 - p1);
1404
- } else {
1405
- data[idst + j] = 0.0f;
1406
- }
1407
-
1408
- continue;
1409
- skip:
1410
- data[idst + j] = -INFINITY;
1411
- }
1412
- }
1413
- }
1414
- }
1415
-
1416
- template<bool causal, bool swa, bool is_2d>
1417
- static void set_input_kq_mask_impl(const args_set_input_kq_mask & args, float * data) {
1418
- const bool alibi = args.hparams.use_alibi;
1419
- if (alibi) {
1420
- set_input_kq_mask_impl<causal, swa, is_2d, true> (args, data);
1421
- } else {
1422
- set_input_kq_mask_impl<causal, swa, is_2d, false>(args, data);
1423
- }
1424
- }
1425
-
1426
- template<bool causal, bool swa>
1427
- static void set_input_kq_mask_impl(const args_set_input_kq_mask & args, float * data) {
1428
- const bool is_2d = args.ubatch->is_pos_2d();
1429
- if (is_2d) {
1430
- set_input_kq_mask_impl<causal, swa, true> (args, data);
1431
- } else {
1432
- set_input_kq_mask_impl<causal, swa, false>(args, data);
1433
- }
1434
- }
1435
-
1436
- template<bool causal>
1437
- static void set_input_kq_mask_impl(const args_set_input_kq_mask & args, float * data) {
1438
- const bool swa = args.swa_type != LLAMA_SWA_TYPE_NONE;
1439
- if (swa) {
1440
- set_input_kq_mask_impl<causal, true> (args, data);
1441
- } else {
1442
- set_input_kq_mask_impl<causal, false>(args, data);
1443
- }
1444
- }
1445
-
1446
- void llama_kv_cache::set_input_kq_mask(ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const {
1447
- const uint32_t n_tokens = ubatch->n_tokens;
1448
-
1449
- GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1450
- float * data = (float *) dst->data;
1451
-
1452
- const int64_t n_kv = dst->ne[0];
1453
- const int64_t n_stream = dst->ne[3]; // num streams in the current ubatch
1454
-
1455
- GGML_ASSERT(n_tokens%n_stream == 0);
1456
-
1457
- // n_tps == n_tokens_per_stream
1458
- const int64_t n_tps = n_tokens/n_stream;
1459
-
1460
- //const int64_t t_start = ggml_time_us();
1461
-
1462
- const args_set_input_kq_mask args = {
1463
- /*.hparams =*/ hparams,
1464
- /*.ubatch =*/ ubatch,
1465
- /*.v_cells =*/ v_cells,
1466
- /*.seq_to_stream =*/ seq_to_stream,
1467
- /*.n_swa =*/ n_swa,
1468
- /*.swa_type =*/ swa_type,
1469
- /*.n_kv =*/ n_kv,
1470
- /*.n_stream =*/ n_stream,
1471
- /*.n_tps =*/ n_tps,
1472
- };
1473
-
1474
- if (causal_attn) {
1475
- set_input_kq_mask_impl<true> (args, data);
1476
- } else {
1477
- set_input_kq_mask_impl<false>(args, data);
1478
- }
1479
-
1480
- //const int64_t t_end = ggml_time_us();
1481
-
1482
- //LLAMA_LOG_ERROR("%s: kq mask time: %0.3f ms\n", __func__, (t_end - t_start)/1000.0);
1483
- }
1484
-
1485
- void llama_kv_cache::set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const {
1486
- const int64_t n_tokens = ubatch->n_tokens;
1487
-
1488
- GGML_ASSERT(n_stream == 1 && "TODO: support multiple streams");
1489
- const auto & cells = v_cells[0];
1490
-
1491
- GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1492
- GGML_ASSERT(!ubatch->equal_seqs()); // TODO: use ubatch->n_seqs instead of failing
1493
-
1494
- int32_t * data = (int32_t *) dst->data;
1495
-
1496
- const int32_t n_kv = dst->ne[0];
1497
-
1498
- for (int h = 0; h < 1; ++h) {
1499
- for (int i = 0; i < n_tokens; ++i) {
1500
- for (int j = 0; j < n_kv; ++j) {
1501
- // the position when the cells is empty is irrelevant - it will be masked out later in the attention
1502
- const llama_pos p0 = cells.is_empty(j) ? -1 : cells.pos_get(j);
1503
-
1504
- data[h*(n_kv*n_tokens) + i*n_kv + j] = llama_relative_position_bucket(p0, ubatch->pos[i], hparams.n_rel_attn_bkts, false);
1505
- }
1506
- }
1507
- }
1508
- }
1509
-
1510
- size_t llama_kv_cache::total_size() const {
1511
- size_t size = 0;
1512
-
1513
- for (const auto & [_, buf] : ctxs_bufs) {
1514
- size += ggml_backend_buffer_get_size(buf.get());
1515
- }
1516
-
1517
- return size;
1518
- }
1519
-
1520
- size_t llama_kv_cache::size_k_bytes() const {
1521
- size_t size_k_bytes = 0;
1522
-
1523
- for (const auto & layer : layers) {
1524
- size_k_bytes += ggml_nbytes(layer.k);
1525
- }
1526
-
1527
- return size_k_bytes;
1528
- }
1529
-
1530
- size_t llama_kv_cache::size_v_bytes() const {
1531
- size_t size_v_bytes = 0;
1532
-
1533
- for (const auto & layer : layers) {
1534
- size_v_bytes += layer.v ? ggml_nbytes(layer.v) : 0;
1535
- }
1536
-
1537
- return size_v_bytes;
1538
- }
1539
-
1540
- ggml_tensor * llama_kv_cache::build_rope_shift(
1541
- const llama_cparams & cparams,
1542
- ggml_context * ctx,
1543
- ggml_tensor * cur,
1544
- ggml_tensor * shift,
1545
- ggml_tensor * factors,
1546
- float freq_base,
1547
- float freq_scale,
1548
- uint32_t il) const {
1549
- const auto & n_ctx_orig = cparams.n_ctx_orig_yarn;
1550
-
1551
- const auto & yarn_ext_factor = cparams.yarn_ext_factor;
1552
- const auto & yarn_beta_fast = cparams.yarn_beta_fast;
1553
- const auto & yarn_beta_slow = cparams.yarn_beta_slow;
1554
- const auto & yarn_attn_factor = cparams.yarn_attn_factor;
1555
-
1556
- const auto & n_rot = hparams.n_rot(il);
1557
- const auto & rope_type = hparams.rope_type == LLAMA_ROPE_TYPE_MROPE || hparams.rope_type == LLAMA_ROPE_TYPE_IMROPE
1558
- // @ngxson : this is a workaround
1559
- // for M-RoPE, we want to rotate the whole vector when doing KV shift
1560
- // a normal RoPE should work, we just need to use the correct ordering
1561
- // ref: https://github.com/ggml-org/llama.cpp/pull/13870
1562
- ? LLAMA_ROPE_TYPE_NEOX
1563
- : hparams.rope_type;
1564
-
1565
- ggml_tensor * tmp;
1566
-
1567
- if (ggml_is_quantized(cur->type)) {
1568
- // dequantize to f32 -> RoPE -> quantize back
1569
- tmp = ggml_cast(ctx, cur, GGML_TYPE_F32);
1570
-
1571
- tmp = ggml_rope_ext(ctx, tmp,
1572
- shift, factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
1573
- yarn_ext_factor, yarn_attn_factor, yarn_beta_fast, yarn_beta_slow);
1574
-
1575
- tmp = ggml_cpy(ctx, tmp, cur);
1576
- } else {
1577
- // we rotate only the first n_rot dimensions
1578
- tmp = ggml_rope_ext_inplace(ctx, cur,
1579
- shift, factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
1580
- yarn_ext_factor, yarn_attn_factor, yarn_beta_fast, yarn_beta_slow);
1581
- }
1582
-
1583
- return tmp;
1584
- }
1585
-
1586
- class llm_graph_input_k_shift : public llm_graph_input_i {
1587
- public:
1588
- llm_graph_input_k_shift(const llama_kv_cache * kv_self) : kv_self(kv_self) {}
1589
- virtual ~llm_graph_input_k_shift() = default;
1590
-
1591
- void set_input(const llama_ubatch * ubatch) override;
1592
-
1593
- ggml_tensor * k_shift; // I32 [kv_size*n_stream]
1594
-
1595
- const llama_kv_cache * kv_self;
1596
- };
1597
-
1598
- void llm_graph_input_k_shift::set_input(const llama_ubatch * ubatch) {
1599
- GGML_UNUSED(ubatch);
1600
-
1601
- if (k_shift) {
1602
- kv_self->set_input_k_shift(k_shift);
1603
- }
1604
- }
1605
-
1606
- ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_context * lctx) const {
1607
- auto * ctx = res->get_ctx();
1608
- auto * gf = res->get_gf();
1609
-
1610
- auto inp = std::make_unique<llm_graph_input_k_shift>(this);
1611
-
1612
- inp->k_shift = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, (int64_t) get_size()*n_stream);
1613
- ggml_set_input(inp->k_shift);
1614
-
1615
- const auto & cparams = lctx->get_cparams();
1616
-
1617
- for (const auto & layer : layers) {
1618
- const uint32_t il = layer.il;
1619
-
1620
- const int64_t n_head_kv = hparams.n_head_kv(il);
1621
- const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
1622
-
1623
- const auto n_rot = hparams.n_rot(il);
1624
- const auto n_embd_head_k = hparams.n_embd_head_k(il);
1625
- const auto n_embd_nope = hparams.n_lora_kv > 0 ? n_embd_head_k - n_rot : 0;
1626
-
1627
- const float freq_base_l = model.get_rope_freq_base (cparams, il);
1628
- const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
1629
-
1630
- ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
1631
-
1632
- ggml_tensor * k =
1633
- ggml_view_3d(ctx, layer.k,
1634
- n_rot, n_head_kv, get_size()*n_stream,
1635
- ggml_row_size(layer.k->type, n_embd_head_k),
1636
- ggml_row_size(layer.k->type, n_embd_k_gqa),
1637
- ggml_row_size(layer.k->type, n_embd_nope));
1638
-
1639
- ggml_tensor * cur = build_rope_shift(cparams, ctx, k, inp->k_shift, rope_factors, freq_base_l, freq_scale_l, il);
1640
-
1641
- ggml_build_forward_expand(gf, cur);
1642
- }
1643
-
1644
- res->add_input(std::move(inp));
1645
-
1646
- return gf;
1647
- }
1648
-
1649
- void llama_kv_cache::state_write(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) const {
1650
- GGML_UNUSED(flags);
1651
-
1652
- io.write(&n_stream, sizeof(n_stream));
1653
-
1654
- for (uint32_t s = 0; s < n_stream; ++s) {
1655
- cell_ranges_t cr { s, {} };
1656
-
1657
- uint32_t cell_count = 0;
1658
-
1659
- const auto & cells = v_cells[s];
1660
-
1661
- // Count the number of cells with the specified seq_id
1662
- // Find all the ranges of cells with this seq id (or all, when -1)
1663
- uint32_t cell_range_begin = cells.size();
1664
-
1665
- for (uint32_t i = 0; i < cells.size(); ++i) {
1666
- if (!cells.is_empty(i) && (seq_id == -1 || cells.seq_has(i, seq_id))) {
1667
- ++cell_count;
1668
- if (cell_range_begin == cells.size()) {
1669
- cell_range_begin = i;
1670
- }
1671
- } else {
1672
- if (cell_range_begin != cells.size()) {
1673
- cr.data.emplace_back(cell_range_begin, i);
1674
- cell_range_begin = cells.size();
1675
- }
1676
- }
1677
- }
1678
-
1679
- if (cell_range_begin != cells.size()) {
1680
- cr.data.emplace_back(cell_range_begin, cells.size());
1681
- }
1682
-
1683
- // DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count
1684
- uint32_t cell_count_check = 0;
1685
- for (const auto & range : cr.data) {
1686
- cell_count_check += range.second - range.first;
1687
- }
1688
- GGML_ASSERT(cell_count == cell_count_check);
1689
-
1690
- io.write(&cell_count, sizeof(cell_count));
1691
-
1692
- // skip empty streams
1693
- if (cell_count == 0) {
1694
- continue;
1695
- }
1696
-
1697
- state_write_meta(io, cr, seq_id);
1698
- state_write_data(io, cr);
1699
- }
1700
- }
1701
-
1702
- void llama_kv_cache::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) {
1703
- GGML_UNUSED(flags);
1704
-
1705
- GGML_ASSERT(seq_id == -1 || (seq_id >= 0 && (size_t) seq_id < seq_to_stream.size()));
1706
-
1707
- uint32_t n_stream_cur;
1708
- io.read_to(&n_stream_cur, sizeof(n_stream_cur));
1709
- if (n_stream_cur != n_stream) {
1710
- throw std::runtime_error("n_stream mismatch");
1711
- }
1712
-
1713
- for (uint32_t s = 0; s < n_stream; ++s) {
1714
- uint32_t cell_count;
1715
- io.read_to(&cell_count, sizeof(cell_count));
1716
-
1717
- if (cell_count == 0) {
1718
- continue;
1719
- }
1720
-
1721
- const uint32_t strm = seq_id == -1 ? s : seq_to_stream[seq_id];
1722
-
1723
- slot_info sinfo;
1724
-
1725
- bool res = true;
1726
- res = res && state_read_meta(io, strm, cell_count, sinfo, seq_id);
1727
- res = res && state_read_data(io, strm, cell_count, sinfo);
1728
-
1729
- if (!res) {
1730
- if (seq_id == -1) {
1731
- clear(true);
1732
- } else {
1733
- seq_rm(seq_id, -1, -1);
1734
- }
1735
- throw std::runtime_error("failed to restore kv cache");
1736
- }
1737
- }
1738
- }
1739
-
1740
- void llama_kv_cache::state_write_meta(llama_io_write_i & io, const cell_ranges_t & cr, llama_seq_id seq_id) const {
1741
- const auto & cells = v_cells[cr.strm];
1742
-
1743
- for (const auto & range : cr.data) {
1744
- for (uint32_t i = range.first; i < range.second; ++i) {
1745
- std::vector<llama_seq_id> seq_ids;
1746
-
1747
- for (llama_seq_id cur = 0; cur < (int) n_seq_max; ++cur) {
1748
- if (cur == seq_id || seq_id == -1) {
1749
- if (cells.seq_has(i, cur)) {
1750
- seq_ids.push_back(cur);
1751
- }
1752
- }
1753
- }
1754
-
1755
- const llama_pos pos = cells.pos_get(i);
1756
- const uint32_t n_seq_id = seq_ids.size();
1757
-
1758
- io.write(&pos, sizeof(pos));
1759
- io.write(&n_seq_id, sizeof(n_seq_id));
1760
-
1761
- if (hparams.n_pos_per_embd() > 1) {
1762
- const llama_kv_cell_ext ext = cells.ext_get(i);
1763
- io.write(&ext, sizeof(ext));
1764
- }
1765
-
1766
- for (const auto & seq_id : seq_ids) {
1767
- io.write(&seq_id, sizeof(seq_id));
1768
- }
1769
- }
1770
- }
1771
- }
1772
-
1773
- void llama_kv_cache::state_write_data(llama_io_write_i & io, const cell_ranges_t & cr) const {
1774
- const auto & cells = v_cells[cr.strm];
1775
-
1776
- const uint32_t v_trans = this->v_trans ? 1 : 0;
1777
- const uint32_t n_layer = layers.size();
1778
-
1779
- io.write(&v_trans, sizeof(v_trans));
1780
- io.write(&n_layer, sizeof(n_layer));
1781
-
1782
- // Iterate and write all the keys first, each row is a cell
1783
- // Get whole range at a time
1784
- for (const auto & layer : layers) {
1785
- const uint32_t il = layer.il;
1786
-
1787
- const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
1788
-
1789
- auto * k = layer.k_stream[cr.strm];
1790
-
1791
- // Write key type
1792
- const int32_t k_type_i = (int32_t) k->type;
1793
- io.write(&k_type_i, sizeof(k_type_i));
1794
-
1795
- // Write row size of key
1796
- const uint64_t k_size_row = ggml_row_size(k->type, n_embd_k_gqa);
1797
- io.write(&k_size_row, sizeof(k_size_row));
1798
-
1799
- // Read each range of cells of k_size length and write out
1800
- for (const auto & range : cr.data) {
1801
- const size_t range_size = range.second - range.first;
1802
- const size_t buf_size = range_size * k_size_row;
1803
- io.write_tensor(k, range.first * k_size_row, buf_size);
1804
- }
1805
- }
1806
-
1807
- if (!v_trans) {
1808
- for (const auto & layer : layers) {
1809
- const uint32_t il = layer.il;
1810
-
1811
- const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
1812
-
1813
- auto * v = layer.v_stream[cr.strm];
1814
- if (!v) {
1815
- continue;
1816
- }
1817
-
1818
- // Write value type
1819
- const int32_t v_type_i = (int32_t) v->type;
1820
- io.write(&v_type_i, sizeof(v_type_i));
1821
-
1822
- // Write row size of value
1823
- const uint64_t v_size_row = ggml_row_size(v->type, n_embd_v_gqa);
1824
- io.write(&v_size_row, sizeof(v_size_row));
1825
-
1826
- // Read each range of cells of v_size length and write out
1827
- for (const auto & range : cr.data) {
1828
- const size_t range_size = range.second - range.first;
1829
- const size_t buf_size = range_size * v_size_row;
1830
- io.write_tensor(v, range.first * v_size_row, buf_size);
1831
- }
1832
- }
1833
- } else {
1834
- // When v is transposed, we also need the element size and get the element ranges from each row
1835
- const uint32_t kv_size = cells.size();
1836
-
1837
- for (const auto & layer : layers) {
1838
- const uint32_t il = layer.il;
1839
-
1840
- const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
1841
-
1842
- auto * v = layer.v_stream[cr.strm];
1843
- if (!v) {
1844
- continue;
1845
- }
1846
-
1847
- // Write value type
1848
- const int32_t v_type_i = (int32_t) v->type;
1849
- io.write(&v_type_i, sizeof(v_type_i));
1850
-
1851
- // Write element size
1852
- const uint32_t v_size_el = ggml_type_size(v->type);
1853
- io.write(&v_size_el, sizeof(v_size_el));
1854
-
1855
- // Write GQA embedding size
1856
- io.write(&n_embd_v_gqa, sizeof(n_embd_v_gqa));
1857
-
1858
- // For each row, we get the element values of each cell
1859
- for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
1860
- // Read each range of cells of v_size_el length and write out
1861
- for (const auto & range : cr.data) {
1862
- const size_t range_size = range.second - range.first;
1863
- const size_t src_offset = (range.first + j * kv_size) * v_size_el;
1864
- const size_t buf_size = range_size * v_size_el;
1865
- io.write_tensor(v, src_offset, buf_size);
1866
- }
1867
- }
1868
- }
1869
- }
1870
- }
1871
-
1872
- bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, slot_info & sinfo, llama_seq_id dest_seq_id) {
1873
- auto & cells = v_cells[strm];
1874
- auto & head = v_heads[strm];
1875
-
1876
- if (dest_seq_id != -1) {
1877
- // single sequence
1878
- seq_rm(dest_seq_id, -1, -1);
1879
-
1880
- llama_batch_allocr balloc(hparams.n_pos_per_embd());
1881
-
1882
- llama_ubatch ubatch = balloc.ubatch_reserve(cell_count, 1);
1883
-
1884
- ubatch.seq_id_unq[0] = dest_seq_id;
1885
-
1886
- for (uint32_t i = 0; i < cell_count; ++i) {
1887
- llama_pos pos;
1888
- uint32_t n_seq_id;
1889
-
1890
- io.read_to(&pos, sizeof(pos));
1891
- io.read_to(&n_seq_id, sizeof(n_seq_id));
1892
-
1893
- if (n_seq_id != 1) {
1894
- LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__);
1895
- return false;
1896
- }
1897
-
1898
- if (hparams.n_pos_per_embd() > 1) {
1899
- llama_kv_cell_ext ext;
1900
- io.read_to(&ext, sizeof(ext));
1901
-
1902
- ubatch.pos[i + ubatch.n_tokens] = ext.y;
1903
- ubatch.pos[i + ubatch.n_tokens*2] = ext.x;
1904
- }
1905
-
1906
- // read the sequence id, but directly discard it - we will use dest_seq_id instead
1907
- {
1908
- llama_seq_id seq_id;
1909
- io.read_to(&seq_id, sizeof(seq_id));
1910
- }
1911
-
1912
- ubatch.pos[i] = pos;
1913
- ubatch.n_seq_id[i] = n_seq_id;
1914
- ubatch.seq_id[i] = &dest_seq_id;
1915
- }
1916
-
1917
- sinfo = find_slot(ubatch, false);
1918
- if (sinfo.empty()) {
1919
- LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
1920
- return false;
1921
- }
1922
-
1923
- // TODO: we cannot yet restore llama_kv_cell_ext as the apply_ubatch() does not support it yet
1924
- // see: https://github.com/ggml-org/llama.cpp/pull/16825#issuecomment-3460868350
1925
- apply_ubatch(sinfo, ubatch);
1926
-
1927
- LLAMA_LOG_DEBUG("%s: cell_count = %d, dest_seq_id = %d\n", __func__, cell_count, dest_seq_id);
1928
-
1929
- // DEBUG CHECK: verify that all cells were allocated and have correct seq_id and pos values
1930
- GGML_ASSERT(sinfo.n_stream() == 1);
1931
- GGML_ASSERT(sinfo.idxs[0].size() == cell_count);
1932
- for (uint32_t i = 0; i < cell_count; ++i) {
1933
- const uint32_t idx = sinfo.idxs[0][i];
1934
- GGML_ASSERT(cells.pos_get(idx) == ubatch.pos[i]);
1935
- GGML_ASSERT(cells.seq_has(idx, dest_seq_id));
1936
- }
1937
- } else {
1938
- // whole KV cache restore
1939
-
1940
- if (cell_count > cells.size()) {
1941
- LLAMA_LOG_ERROR("%s: not enough cells in kv cache\n", __func__);
1942
- return false;
1943
- }
1944
-
1945
- clear(true);
1946
-
1947
- for (uint32_t i = 0; i < cell_count; ++i) {
1948
- llama_pos pos;
1949
- uint32_t n_seq_id;
1950
-
1951
- io.read_to(&pos, sizeof(pos));
1952
- io.read_to(&n_seq_id, sizeof(n_seq_id));
1953
-
1954
- cells.pos_set(i, pos);
1955
-
1956
- if (hparams.n_pos_per_embd() > 1) {
1957
- llama_kv_cell_ext ext;
1958
- io.read_to(&ext, sizeof(ext));
1959
- cells.ext_set(i, ext);
1960
- }
1961
-
1962
- for (uint32_t j = 0; j < n_seq_id; ++j) {
1963
- llama_seq_id seq_id;
1964
- io.read_to(&seq_id, sizeof(seq_id));
1965
-
1966
- if (seq_id < 0 || (uint32_t) seq_id >= n_seq_max) {
1967
- LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, %u)\n", __func__, seq_id, n_seq_max);
1968
- return false;
1969
- }
1970
-
1971
- cells.seq_add(i, seq_id);
1972
- }
1973
- }
1974
-
1975
- // Create contiguous slot_info for whole cache restore
1976
- sinfo.s0 = strm;
1977
- sinfo.s1 = strm;
1978
- sinfo.resize(1);
1979
- sinfo.strm[0] = strm;
1980
- sinfo.idxs[0].resize(cell_count);
1981
- for (uint32_t i = 0; i < cell_count; ++i) {
1982
- sinfo.idxs[0][i] = i;
1983
- }
1984
-
1985
- head = 0;
1986
- }
1987
-
1988
- return true;
1989
- }
1990
-
1991
- bool llama_kv_cache::state_read_data(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, const slot_info & sinfo) {
1992
- auto & cells = v_cells[strm];
1993
-
1994
- uint32_t v_trans;
1995
- uint32_t n_layer;
1996
-
1997
- io.read_to(&v_trans, sizeof(v_trans));
1998
- io.read_to(&n_layer, sizeof(n_layer));
1999
-
2000
- if (n_layer != layers.size()) {
2001
- LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, (uint32_t) layers.size());
2002
- return false;
2003
- }
2004
-
2005
- if (cell_count > cells.size()) {
2006
- LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, cells.size());
2007
- return false;
2008
- }
2009
-
2010
- if (this->v_trans != (bool) v_trans) {
2011
- LLAMA_LOG_ERROR("%s: incompatible V transposition\n", __func__);
2012
- return false;
2013
- }
2014
-
2015
- // For each layer, read the keys for each cell, one row is one cell, read as one contiguous block
2016
- for (const auto & layer : layers) {
2017
- const uint32_t il = layer.il;
2018
-
2019
- const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
2020
-
2021
- auto * k = layer.k_stream[strm];
2022
-
2023
- // Read type of key
2024
- int32_t k_type_i_ref;
2025
- io.read_to(&k_type_i_ref, sizeof(k_type_i_ref));
2026
- const int32_t k_type_i = (int32_t) k->type;
2027
- if (k_type_i != k_type_i_ref) {
2028
- LLAMA_LOG_ERROR("%s: mismatched key type (%d != %d, layer %d)\n", __func__, k_type_i, k_type_i_ref, il);
2029
- return false;
2030
- }
2031
-
2032
- // Read row size of key
2033
- uint64_t k_size_row_ref;
2034
- io.read_to(&k_size_row_ref, sizeof(k_size_row_ref));
2035
- const size_t k_size_row = ggml_row_size(k->type, n_embd_k_gqa);
2036
- if (k_size_row != k_size_row_ref) {
2037
- LLAMA_LOG_ERROR("%s: mismatched key row size (%zu != %zu, layer %d)\n", __func__, k_size_row, (size_t) k_size_row_ref, il);
2038
- return false;
2039
- }
2040
-
2041
- if (cell_count) {
2042
- if (sinfo.is_contiguous()) {
2043
- // Fast path: contiguous cells, single memcpy
2044
- ggml_backend_tensor_set(k, io.read(cell_count * k_size_row), sinfo.head() * k_size_row, cell_count * k_size_row);
2045
- } else {
2046
- // Slow path: scatter to non-contiguous positions
2047
- const void * src = io.read(cell_count * k_size_row);
2048
- for (uint32_t i = 0; i < cell_count; ++i) {
2049
- const size_t dst_offset = sinfo.idxs[0][i] * k_size_row;
2050
- ggml_backend_tensor_set(k, (const char*)src + i * k_size_row, dst_offset, k_size_row);
2051
- }
2052
- }
2053
- }
2054
- }
2055
-
2056
- if (!this->v_trans) {
2057
- for (const auto & layer : layers) {
2058
- const uint32_t il = layer.il;
2059
-
2060
- const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
2061
-
2062
- auto * v = layer.v_stream[strm];
2063
- if (!v) {
2064
- continue;
2065
- }
2066
-
2067
- // Read type of value
2068
- int32_t v_type_i_ref;
2069
- io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
2070
- const int32_t v_type_i = (int32_t) v->type;
2071
- if (v_type_i != v_type_i_ref) {
2072
- LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
2073
- return false;
2074
- }
2075
-
2076
- // Read row size of value
2077
- uint64_t v_size_row_ref;
2078
- io.read_to(&v_size_row_ref, sizeof(v_size_row_ref));
2079
- const size_t v_size_row = ggml_row_size(v->type, n_embd_v_gqa);
2080
- if (v_size_row != v_size_row_ref) {
2081
- LLAMA_LOG_ERROR("%s: mismatched value row size (%zu != %zu, layer %d)\n", __func__, v_size_row, (size_t) v_size_row_ref, il);
2082
- return false;
2083
- }
2084
-
2085
- if (cell_count) {
2086
- if (sinfo.is_contiguous()) {
2087
- // Fast path: contiguous cells, single memcpy
2088
- ggml_backend_tensor_set(v, io.read(cell_count * v_size_row), sinfo.head() * v_size_row, cell_count * v_size_row);
2089
- } else {
2090
- // Slow path: scatter to non-contiguous positions
2091
- const void * src = io.read(cell_count * v_size_row);
2092
- for (uint32_t i = 0; i < cell_count; ++i) {
2093
- const size_t dst_offset = sinfo.idxs[0][i] * v_size_row;
2094
- ggml_backend_tensor_set(v, (const char*)src + i * v_size_row, dst_offset, v_size_row);
2095
- }
2096
- }
2097
- }
2098
- }
2099
- } else {
2100
- // For each layer, read the values for each cell (transposed)
2101
- for (const auto & layer : layers) {
2102
- const uint32_t il = layer.il;
2103
-
2104
- const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
2105
-
2106
- auto * v = layer.v_stream[strm];
2107
- if (!v) {
2108
- continue;
2109
- }
2110
-
2111
- // Read type of value
2112
- int32_t v_type_i_ref;
2113
- io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
2114
- const int32_t v_type_i = (int32_t) v->type;
2115
- if (v_type_i != v_type_i_ref) {
2116
- LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
2117
- return false;
2118
- }
2119
-
2120
- // Read element size of value
2121
- uint32_t v_size_el_ref;
2122
- io.read_to(&v_size_el_ref, sizeof(v_size_el_ref));
2123
- const size_t v_size_el = ggml_type_size(v->type);
2124
- if (v_size_el != v_size_el_ref) {
2125
- LLAMA_LOG_ERROR("%s: mismatched value element size (%zu != %zu, layer %d)\n", __func__, v_size_el, (size_t) v_size_el_ref, il);
2126
- return false;
2127
- }
2128
-
2129
- // Read GQA embedding size
2130
- uint32_t n_embd_v_gqa_ref;
2131
- io.read_to(&n_embd_v_gqa_ref, sizeof(n_embd_v_gqa_ref));
2132
- if (n_embd_v_gqa != n_embd_v_gqa_ref) {
2133
- LLAMA_LOG_ERROR("%s: mismatched GQA embedding size (%u != %u, layer %d)\n", __func__, n_embd_v_gqa, n_embd_v_gqa_ref, il);
2134
- return false;
2135
- }
2136
-
2137
- if (cell_count) {
2138
- if (sinfo.is_contiguous()) {
2139
- // Fast path: contiguous cells
2140
- const uint32_t h = sinfo.head();
2141
- for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
2142
- const size_t dst_offset = (h + j * cells.size()) * v_size_el;
2143
- ggml_backend_tensor_set(v, io.read(cell_count * v_size_el), dst_offset, cell_count * v_size_el);
2144
- }
2145
- } else {
2146
- // Slow path: scatter to non-contiguous positions
2147
- for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
2148
- const void * src = io.read(cell_count * v_size_el);
2149
- for (uint32_t i = 0; i < cell_count; ++i) {
2150
- const size_t dst_offset = (sinfo.idxs[0][i] + j * cells.size()) * v_size_el;
2151
- ggml_backend_tensor_set(v, (const char*)src + i * v_size_el, dst_offset, v_size_el);
2152
- }
2153
- }
2154
- }
2155
- }
2156
- }
2157
- }
2158
-
2159
- return true;
2160
- }
2161
-
2162
- //
2163
- // llama_kv_cache_context
2164
- //
2165
-
2166
- llama_kv_cache_context::llama_kv_cache_context(llama_memory_status status) : status(status) {}
2167
-
2168
- llama_kv_cache_context::llama_kv_cache_context(
2169
- llama_kv_cache * kv) : status(LLAMA_MEMORY_STATUS_SUCCESS), kv(kv) {
2170
- n_kv = kv->get_size();
2171
-
2172
- const uint32_t n_stream = kv->get_n_stream();
2173
-
2174
- // create a dummy slot info - the actual data is irrelevant. we just need to build the graph
2175
- sinfos.resize(1);
2176
- sinfos[0].s0 = 0;
2177
- sinfos[0].s1 = n_stream - 1;
2178
- sinfos[0].idxs.resize(n_stream);
2179
- for (uint32_t s = 0; s < n_stream; ++s) {
2180
- sinfos[0].strm.push_back(s);
2181
- sinfos[0].idxs[s].resize(1, 0);
2182
- }
2183
- }
2184
-
2185
- llama_kv_cache_context::llama_kv_cache_context(
2186
- llama_kv_cache * kv,
2187
- llama_context * lctx,
2188
- bool do_shift,
2189
- stream_copy_info sc_info) : status(LLAMA_MEMORY_STATUS_SUCCESS), kv(kv), lctx(lctx), do_shift(do_shift), sc_info(std::move(sc_info)) {
2190
- if (!do_shift && this->sc_info.empty()) {
2191
- status = LLAMA_MEMORY_STATUS_NO_UPDATE;
2192
- }
2193
- }
2194
-
2195
- llama_kv_cache_context::llama_kv_cache_context(
2196
- llama_kv_cache * kv,
2197
- llama_kv_cache::slot_info_vec_t sinfos,
2198
- std::vector<llama_ubatch> ubatches) : status(LLAMA_MEMORY_STATUS_SUCCESS), kv(kv), sinfos(std::move(sinfos)), ubatches(std::move(ubatches)) {
2199
- }
2200
-
2201
- llama_kv_cache_context::~llama_kv_cache_context() = default;
2202
-
2203
- bool llama_kv_cache_context::next() {
2204
- assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
2205
-
2206
- if (++i_cur >= ubatches.size()) {
2207
- return false;
2208
- }
2209
-
2210
- return true;
2211
- }
2212
-
2213
- bool llama_kv_cache_context::apply() {
2214
- assert(!llama_memory_status_is_fail(status));
2215
-
2216
- // no ubatches -> this is a KV cache update
2217
- if (ubatches.empty()) {
2218
- kv->update(lctx, do_shift, sc_info);
2219
-
2220
- return true;
2221
- }
2222
-
2223
- kv->apply_ubatch(sinfos[i_cur], ubatches[i_cur]);
2224
- n_kv = kv->get_n_kv(sinfos[i_cur]);
2225
-
2226
- return true;
2227
- }
2228
-
2229
- llama_memory_status llama_kv_cache_context::get_status() const {
2230
- return status;
2231
- }
2232
-
2233
- const llama_ubatch & llama_kv_cache_context::get_ubatch() const {
2234
- assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
2235
-
2236
- return ubatches[i_cur];
2237
- }
2238
-
2239
- uint32_t llama_kv_cache_context::get_n_kv() const {
2240
- return n_kv;
2241
- }
2242
-
2243
- ggml_tensor * llama_kv_cache_context::get_k(ggml_context * ctx, int32_t il) const {
2244
- return kv->get_k(ctx, il, n_kv, sinfos[i_cur]);
2245
- }
2246
-
2247
- ggml_tensor * llama_kv_cache_context::get_v(ggml_context * ctx, int32_t il) const {
2248
- return kv->get_v(ctx, il, n_kv, sinfos[i_cur]);
2249
- }
2250
-
2251
- ggml_tensor * llama_kv_cache_context::cpy_k(ggml_context * ctx, ggml_tensor * k_cur, ggml_tensor * k_idxs, int32_t il) const {
2252
- return kv->cpy_k(ctx, k_cur, k_idxs, il, sinfos[i_cur]);
2253
- }
2254
-
2255
- ggml_tensor * llama_kv_cache_context::cpy_v(ggml_context * ctx, ggml_tensor * v_cur, ggml_tensor * v_idxs, int32_t il) const {
2256
- return kv->cpy_v(ctx, v_cur, v_idxs, il, sinfos[i_cur]);
2257
- }
2258
-
2259
- ggml_tensor * llama_kv_cache_context::build_input_k_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
2260
- return kv->build_input_k_idxs(ctx, ubatch);
2261
- }
2262
-
2263
- ggml_tensor * llama_kv_cache_context::build_input_v_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
2264
- return kv->build_input_v_idxs(ctx, ubatch);
2265
- }
2266
-
2267
- void llama_kv_cache_context::set_input_k_shift(ggml_tensor * dst) const {
2268
- kv->set_input_k_shift(dst);
2269
- }
2270
-
2271
- void llama_kv_cache_context::set_input_k_idxs(ggml_tensor * dst, const llama_ubatch * ubatch) const {
2272
- kv->set_input_k_idxs(dst, ubatch, sinfos[i_cur]);
2273
- }
2274
-
2275
- void llama_kv_cache_context::set_input_v_idxs(ggml_tensor * dst, const llama_ubatch * ubatch) const {
2276
- kv->set_input_v_idxs(dst, ubatch, sinfos[i_cur]);
2277
- }
2278
-
2279
- void llama_kv_cache_context::set_input_kq_mask(ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const {
2280
- kv->set_input_kq_mask(dst, ubatch, causal_attn);
2281
- }
2282
-
2283
- void llama_kv_cache_context::set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const {
2284
- kv->set_input_pos_bucket(dst, ubatch);
2285
- }