whispercpp 1.3.3 → 1.3.4

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 (586) hide show
  1. checksums.yaml +4 -4
  2. data/ext/ruby_whisper_params.c +55 -25
  3. data/ext/sources/CMakeLists.txt +1 -1
  4. data/ext/sources/bindings/javascript/package.json +1 -1
  5. data/ext/sources/build-xcframework.sh +24 -0
  6. data/ext/sources/examples/CMakeLists.txt +1 -0
  7. data/ext/sources/examples/addon.node/addon.cpp +19 -19
  8. data/ext/sources/examples/addon.node/index.js +7 -5
  9. data/ext/sources/examples/bench/bench.cpp +26 -16
  10. data/ext/sources/examples/bench.wasm/index-tmpl.html +10 -9
  11. data/ext/sources/examples/cli/cli.cpp +4 -2
  12. data/ext/sources/examples/command/command.cpp +26 -24
  13. data/ext/sources/examples/command.wasm/index-tmpl.html +5 -4
  14. data/ext/sources/examples/common-ggml.cpp +2 -0
  15. data/ext/sources/examples/lsp/lsp.cpp +19 -17
  16. data/ext/sources/examples/server/server.cpp +24 -13
  17. data/ext/sources/examples/server.py +6 -1
  18. data/ext/sources/examples/stream/stream.cpp +4 -2
  19. data/ext/sources/examples/stream.wasm/emscripten.cpp +6 -6
  20. data/ext/sources/examples/stream.wasm/index-tmpl.html +82 -5
  21. data/ext/sources/examples/talk-llama/CMakeLists.txt +2 -2
  22. data/ext/sources/examples/talk-llama/llama-adapter.cpp +101 -4
  23. data/ext/sources/examples/talk-llama/llama-adapter.h +6 -0
  24. data/ext/sources/examples/talk-llama/llama-arch.cpp +588 -15
  25. data/ext/sources/examples/talk-llama/llama-arch.h +58 -1
  26. data/ext/sources/examples/talk-llama/llama-batch.cpp +103 -71
  27. data/ext/sources/examples/talk-llama/llama-batch.h +31 -18
  28. data/ext/sources/examples/talk-llama/llama-chat.cpp +120 -5
  29. data/ext/sources/examples/talk-llama/llama-chat.h +7 -0
  30. data/ext/sources/examples/talk-llama/llama-context.cpp +460 -357
  31. data/ext/sources/examples/talk-llama/llama-context.h +44 -29
  32. data/ext/sources/examples/talk-llama/llama-cparams.h +4 -4
  33. data/ext/sources/examples/talk-llama/llama-graph.cpp +543 -271
  34. data/ext/sources/examples/talk-llama/llama-graph.h +278 -168
  35. data/ext/sources/examples/talk-llama/llama-hparams.cpp +118 -4
  36. data/ext/sources/examples/talk-llama/llama-hparams.h +61 -15
  37. data/ext/sources/examples/talk-llama/llama-impl.h +2 -0
  38. data/ext/sources/examples/talk-llama/llama-kv-cache-iswa.cpp +326 -0
  39. data/ext/sources/examples/talk-llama/{llama-kv-cache-unified-iswa.h → llama-kv-cache-iswa.h} +38 -29
  40. data/ext/sources/examples/talk-llama/llama-kv-cache.cpp +2020 -0
  41. data/ext/sources/examples/talk-llama/llama-kv-cache.h +358 -27
  42. data/ext/sources/examples/talk-llama/llama-kv-cells.h +80 -28
  43. data/ext/sources/examples/talk-llama/llama-memory-hybrid.cpp +56 -36
  44. data/ext/sources/examples/talk-llama/llama-memory-hybrid.h +30 -29
  45. data/ext/sources/examples/talk-llama/llama-memory-recurrent.cpp +48 -19
  46. data/ext/sources/examples/talk-llama/llama-memory-recurrent.h +13 -14
  47. data/ext/sources/examples/talk-llama/llama-memory.h +16 -10
  48. data/ext/sources/examples/talk-llama/llama-model-loader.cpp +2 -0
  49. data/ext/sources/examples/talk-llama/llama-model-loader.h +3 -2
  50. data/ext/sources/examples/talk-llama/llama-model.cpp +7165 -2336
  51. data/ext/sources/examples/talk-llama/llama-model.h +60 -9
  52. data/ext/sources/examples/talk-llama/llama-quant.cpp +48 -10
  53. data/ext/sources/examples/talk-llama/llama-sampling.cpp +226 -126
  54. data/ext/sources/examples/talk-llama/llama-vocab.cpp +440 -13
  55. data/ext/sources/examples/talk-llama/llama-vocab.h +45 -0
  56. data/ext/sources/examples/talk-llama/llama.cpp +65 -10
  57. data/ext/sources/examples/talk-llama/llama.h +95 -177
  58. data/ext/sources/examples/talk-llama/talk-llama.cpp +9 -6
  59. data/ext/sources/examples/talk-llama/unicode.cpp +207 -0
  60. data/ext/sources/examples/talk-llama/unicode.h +45 -0
  61. data/ext/sources/examples/wchess/wchess.cmd/wchess.cmd.cpp +4 -2
  62. data/ext/sources/examples/whisper.wasm/index-tmpl.html +17 -16
  63. data/ext/sources/ggml/CMakeLists.txt +59 -31
  64. data/ext/sources/ggml/cmake/ggml-config.cmake.in +132 -93
  65. data/ext/sources/ggml/include/ggml-backend.h +17 -1
  66. data/ext/sources/ggml/include/ggml-cpu.h +1 -1
  67. data/ext/sources/ggml/include/ggml-metal.h +1 -6
  68. data/ext/sources/ggml/include/ggml-opt.h +25 -6
  69. data/ext/sources/ggml/include/ggml-webgpu.h +19 -0
  70. data/ext/sources/ggml/include/ggml-zdnn.h +17 -0
  71. data/ext/sources/ggml/include/ggml.h +221 -16
  72. data/ext/sources/ggml/src/CMakeLists.txt +17 -2
  73. data/ext/sources/ggml/src/ggml-alloc.c +265 -141
  74. data/ext/sources/ggml/src/ggml-backend-impl.h +4 -1
  75. data/ext/sources/ggml/src/ggml-backend-reg.cpp +30 -13
  76. data/ext/sources/ggml/src/ggml-backend.cpp +221 -38
  77. data/ext/sources/ggml/src/ggml-blas/CMakeLists.txt +1 -1
  78. data/ext/sources/ggml/src/ggml-blas/ggml-blas.cpp +5 -4
  79. data/ext/sources/ggml/src/ggml-cann/CMakeLists.txt +14 -0
  80. data/ext/sources/ggml/src/ggml-cann/acl_tensor.cpp +3 -1
  81. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.cpp +903 -717
  82. data/ext/sources/ggml/src/ggml-cann/aclnn_ops.h +143 -25
  83. data/ext/sources/ggml/src/ggml-cann/common.h +143 -1
  84. data/ext/sources/ggml/src/ggml-cann/ggml-cann.cpp +488 -69
  85. data/ext/sources/ggml/src/ggml-common.h +17 -0
  86. data/ext/sources/ggml/src/ggml-cpu/CMakeLists.txt +40 -18
  87. data/ext/sources/ggml/src/ggml-cpu/amx/amx.cpp +4 -2
  88. data/ext/sources/ggml/src/ggml-cpu/arch/arm/quants.c +132 -596
  89. data/ext/sources/ggml/src/ggml-cpu/arch/arm/repack.cpp +14 -286
  90. data/ext/sources/ggml/src/ggml-cpu/arch/loongarch/quants.c +103 -582
  91. data/ext/sources/ggml/src/ggml-cpu/arch/powerpc/quants.c +162 -589
  92. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/quants.c +265 -437
  93. data/ext/sources/ggml/src/ggml-cpu/arch/riscv/repack.cpp +3 -58
  94. data/ext/sources/ggml/src/ggml-cpu/arch/s390/quants.c +521 -353
  95. data/ext/sources/ggml/src/ggml-cpu/arch/wasm/quants.c +54 -314
  96. data/ext/sources/ggml/src/ggml-cpu/arch/x86/quants.c +184 -675
  97. data/ext/sources/ggml/src/ggml-cpu/arch/x86/repack.cpp +4679 -1657
  98. data/ext/sources/ggml/src/ggml-cpu/arch-fallback.h +32 -2
  99. data/ext/sources/ggml/src/ggml-cpu/common.h +14 -0
  100. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu-impl.h +13 -6
  101. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.c +70 -42
  102. data/ext/sources/ggml/src/ggml-cpu/ggml-cpu.cpp +35 -28
  103. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.cpp +152 -18
  104. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kernels.h +7 -1
  105. data/ext/sources/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +227 -97
  106. data/ext/sources/ggml/src/ggml-cpu/llamafile/sgemm.cpp +474 -1116
  107. data/ext/sources/ggml/src/ggml-cpu/ops.cpp +1587 -1177
  108. data/ext/sources/ggml/src/ggml-cpu/ops.h +5 -8
  109. data/ext/sources/ggml/src/ggml-cpu/quants.c +35 -0
  110. data/ext/sources/ggml/src/ggml-cpu/quants.h +8 -0
  111. data/ext/sources/ggml/src/ggml-cpu/repack.cpp +458 -47
  112. data/ext/sources/ggml/src/ggml-cpu/repack.h +22 -0
  113. data/ext/sources/ggml/src/ggml-cpu/simd-mappings.h +89 -60
  114. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.cpp +1024 -0
  115. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime.h +13 -0
  116. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +3196 -0
  117. data/ext/sources/ggml/src/ggml-cpu/spacemit/ime_kernels.h +26 -0
  118. data/ext/sources/ggml/src/ggml-cpu/traits.cpp +2 -2
  119. data/ext/sources/ggml/src/ggml-cpu/traits.h +1 -1
  120. data/ext/sources/ggml/src/ggml-cpu/vec.cpp +170 -26
  121. data/ext/sources/ggml/src/ggml-cpu/vec.h +506 -63
  122. data/ext/sources/ggml/src/ggml-cuda/CMakeLists.txt +20 -16
  123. data/ext/sources/ggml/src/ggml-cuda/add-id.cu +58 -0
  124. data/ext/sources/ggml/src/ggml-cuda/add-id.cuh +3 -0
  125. data/ext/sources/ggml/src/ggml-cuda/binbcast.cu +330 -191
  126. data/ext/sources/ggml/src/ggml-cuda/binbcast.cuh +2 -0
  127. data/ext/sources/ggml/src/ggml-cuda/common.cuh +250 -63
  128. data/ext/sources/ggml/src/ggml-cuda/conv-transpose-1d.cu +1 -4
  129. data/ext/sources/ggml/src/ggml-cuda/conv2d.cu +166 -0
  130. data/ext/sources/ggml/src/ggml-cuda/conv2d.cuh +5 -0
  131. data/ext/sources/ggml/src/ggml-cuda/convert.cu +95 -22
  132. data/ext/sources/ggml/src/ggml-cuda/convert.cuh +15 -0
  133. data/ext/sources/ggml/src/ggml-cuda/cpy-utils.cuh +217 -0
  134. data/ext/sources/ggml/src/ggml-cuda/cpy.cu +64 -307
  135. data/ext/sources/ggml/src/ggml-cuda/cross-entropy-loss.cu +2 -14
  136. data/ext/sources/ggml/src/ggml-cuda/dequantize.cuh +14 -40
  137. data/ext/sources/ggml/src/ggml-cuda/fattn-common.cuh +498 -367
  138. data/ext/sources/ggml/src/ggml-cuda/fattn-mma-f16.cuh +137 -91
  139. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cu +755 -0
  140. data/ext/sources/ggml/src/ggml-cuda/fattn-tile.cuh +3 -0
  141. data/ext/sources/ggml/src/ggml-cuda/fattn-vec.cuh +593 -0
  142. data/ext/sources/ggml/src/ggml-cuda/fattn-wmma-f16.cu +86 -50
  143. data/ext/sources/ggml/src/ggml-cuda/fattn.cu +185 -198
  144. data/ext/sources/ggml/src/ggml-cuda/fattn.cuh +2 -0
  145. data/ext/sources/ggml/src/ggml-cuda/getrows.cu +50 -39
  146. data/ext/sources/ggml/src/ggml-cuda/ggml-cuda.cu +379 -107
  147. data/ext/sources/ggml/src/ggml-cuda/im2col.cu +196 -35
  148. data/ext/sources/ggml/src/ggml-cuda/im2col.cuh +1 -0
  149. data/ext/sources/ggml/src/ggml-cuda/mean.cu +56 -2
  150. data/ext/sources/ggml/src/ggml-cuda/mma.cuh +198 -45
  151. data/ext/sources/ggml/src/ggml-cuda/mmf.cu +123 -0
  152. data/ext/sources/ggml/src/ggml-cuda/mmf.cuh +496 -0
  153. data/ext/sources/ggml/src/ggml-cuda/mmq.cu +206 -57
  154. data/ext/sources/ggml/src/ggml-cuda/mmq.cuh +1262 -721
  155. data/ext/sources/ggml/src/ggml-cuda/{mmv.cu → mmvf.cu} +53 -53
  156. data/ext/sources/ggml/src/ggml-cuda/{mmv.cuh → mmvf.cuh} +3 -3
  157. data/ext/sources/ggml/src/ggml-cuda/mmvq.cu +64 -73
  158. data/ext/sources/ggml/src/ggml-cuda/norm.cu +284 -12
  159. data/ext/sources/ggml/src/ggml-cuda/norm.cuh +7 -0
  160. data/ext/sources/ggml/src/ggml-cuda/opt-step-sgd.cu +49 -0
  161. data/ext/sources/ggml/src/ggml-cuda/opt-step-sgd.cuh +5 -0
  162. data/ext/sources/ggml/src/ggml-cuda/pad.cu +46 -23
  163. data/ext/sources/ggml/src/ggml-cuda/pad_reflect_1d.cu +91 -0
  164. data/ext/sources/ggml/src/ggml-cuda/pad_reflect_1d.cuh +5 -0
  165. data/ext/sources/ggml/src/ggml-cuda/quantize.cu +12 -10
  166. data/ext/sources/ggml/src/ggml-cuda/reduce_rows.cuh +53 -0
  167. data/ext/sources/ggml/src/ggml-cuda/roll.cu +67 -0
  168. data/ext/sources/ggml/src/ggml-cuda/roll.cuh +5 -0
  169. data/ext/sources/ggml/src/ggml-cuda/rope.cu +21 -27
  170. data/ext/sources/ggml/src/ggml-cuda/scale.cu +14 -11
  171. data/ext/sources/ggml/src/ggml-cuda/set-rows.cu +276 -0
  172. data/ext/sources/ggml/src/ggml-cuda/set-rows.cuh +7 -0
  173. data/ext/sources/ggml/src/ggml-cuda/softcap.cu +34 -0
  174. data/ext/sources/ggml/src/ggml-cuda/softcap.cuh +5 -0
  175. data/ext/sources/ggml/src/ggml-cuda/softmax.cu +126 -59
  176. data/ext/sources/ggml/src/ggml-cuda/ssm-conv.cu +10 -2
  177. data/ext/sources/ggml/src/ggml-cuda/ssm-scan.cu +322 -100
  178. data/ext/sources/ggml/src/ggml-cuda/sum.cu +6 -10
  179. data/ext/sources/ggml/src/ggml-cuda/sumrows.cu +21 -4
  180. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-f16.cu +7 -0
  181. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q4_0.cu +7 -0
  182. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q4_1.cu +7 -0
  183. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q5_0.cu +7 -0
  184. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q5_1.cu +7 -0
  185. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q8_0.cu +7 -0
  186. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-f16.cu +7 -0
  187. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q4_0.cu +7 -0
  188. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q4_1.cu +7 -0
  189. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q5_0.cu +7 -0
  190. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q5_1.cu +7 -0
  191. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q8_0.cu +7 -0
  192. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-f16.cu +7 -0
  193. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q4_0.cu +7 -0
  194. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q4_1.cu +7 -0
  195. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q5_0.cu +7 -0
  196. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q5_1.cu +7 -0
  197. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q8_0.cu +7 -0
  198. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-f16.cu +7 -0
  199. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q4_0.cu +7 -0
  200. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q4_1.cu +7 -0
  201. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q5_0.cu +7 -0
  202. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q5_1.cu +7 -0
  203. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q8_0.cu +7 -0
  204. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-f16.cu +7 -0
  205. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q4_0.cu +7 -0
  206. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q4_1.cu +7 -0
  207. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q5_0.cu +7 -0
  208. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q5_1.cu +7 -0
  209. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q8_0.cu +7 -0
  210. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-f16.cu +7 -0
  211. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q4_0.cu +7 -0
  212. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q4_1.cu +7 -0
  213. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q5_0.cu +7 -0
  214. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q5_1.cu +7 -0
  215. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q8_0.cu +7 -0
  216. data/ext/sources/ggml/src/ggml-cuda/template-instances/generate_cu_files.py +21 -18
  217. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_1.cu +5 -0
  218. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_10.cu +5 -0
  219. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_11.cu +5 -0
  220. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_12.cu +5 -0
  221. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_13.cu +5 -0
  222. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_14.cu +5 -0
  223. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_15.cu +5 -0
  224. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_16.cu +5 -0
  225. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_2.cu +5 -0
  226. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_3.cu +5 -0
  227. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_4.cu +5 -0
  228. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_5.cu +5 -0
  229. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_6.cu +5 -0
  230. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_7.cu +5 -0
  231. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_8.cu +5 -0
  232. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_9.cu +5 -0
  233. data/ext/sources/ggml/src/ggml-cuda/template-instances/mmq-instance-mxfp4.cu +5 -0
  234. data/ext/sources/ggml/src/ggml-cuda/topk-moe.cu +259 -0
  235. data/ext/sources/ggml/src/ggml-cuda/topk-moe.cuh +14 -0
  236. data/ext/sources/ggml/src/ggml-cuda/tsembd.cu +3 -3
  237. data/ext/sources/ggml/src/ggml-cuda/unary.cu +90 -0
  238. data/ext/sources/ggml/src/ggml-cuda/unary.cuh +8 -0
  239. data/ext/sources/ggml/src/ggml-cuda/upscale.cu +92 -6
  240. data/ext/sources/ggml/src/ggml-cuda/vecdotq.cuh +110 -22
  241. data/ext/sources/ggml/src/ggml-cuda/vendors/cuda.h +4 -0
  242. data/ext/sources/ggml/src/ggml-cuda/vendors/hip.h +58 -36
  243. data/ext/sources/ggml/src/ggml-cuda/vendors/musa.h +4 -3
  244. data/ext/sources/ggml/src/ggml-hip/CMakeLists.txt +10 -2
  245. data/ext/sources/ggml/src/ggml-impl.h +119 -9
  246. data/ext/sources/ggml/src/ggml-metal/CMakeLists.txt +10 -7
  247. data/ext/sources/ggml/src/ggml-metal/ggml-metal-common.cpp +446 -0
  248. data/ext/sources/ggml/src/ggml-metal/ggml-metal-common.h +52 -0
  249. data/ext/sources/ggml/src/ggml-metal/ggml-metal-context.h +33 -0
  250. data/ext/sources/ggml/src/ggml-metal/ggml-metal-context.m +600 -0
  251. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.cpp +1376 -0
  252. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.h +226 -0
  253. data/ext/sources/ggml/src/ggml-metal/ggml-metal-device.m +1308 -0
  254. data/ext/sources/ggml/src/ggml-metal/ggml-metal-impl.h +136 -63
  255. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.cpp +3158 -0
  256. data/ext/sources/ggml/src/ggml-metal/ggml-metal-ops.h +82 -0
  257. data/ext/sources/ggml/src/ggml-metal/ggml-metal.cpp +718 -0
  258. data/ext/sources/ggml/src/ggml-metal/ggml-metal.metal +2854 -1503
  259. data/ext/sources/ggml/src/ggml-musa/CMakeLists.txt +18 -8
  260. data/ext/sources/ggml/src/ggml-opencl/CMakeLists.txt +18 -0
  261. data/ext/sources/ggml/src/ggml-opencl/ggml-opencl.cpp +2510 -242
  262. data/ext/sources/ggml/src/ggml-opencl/kernels/add.cl +107 -0
  263. data/ext/sources/ggml/src/ggml-opencl/kernels/add_id.cl +42 -0
  264. data/ext/sources/ggml/src/ggml-opencl/kernels/conv2d.cl +185 -0
  265. data/ext/sources/ggml/src/ggml-opencl/kernels/conv2d_f16_f32.cl +176 -0
  266. data/ext/sources/ggml/src/ggml-opencl/kernels/cvt.cl +84 -0
  267. data/ext/sources/ggml/src/ggml-opencl/kernels/div.cl +66 -0
  268. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f16.cl +370 -0
  269. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32.cl +370 -0
  270. data/ext/sources/ggml/src/ggml-opencl/kernels/flash_attn_f32_f16.cl +373 -0
  271. data/ext/sources/ggml/src/ggml-opencl/kernels/gelu.cl +27 -0
  272. data/ext/sources/ggml/src/ggml-opencl/kernels/glu.cl +177 -0
  273. data/ext/sources/ggml/src/ggml-opencl/kernels/group_norm.cl +49 -0
  274. data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f16.cl +1 -1
  275. data/ext/sources/ggml/src/ggml-opencl/kernels/im2col_f32.cl +1 -1
  276. data/ext/sources/ggml/src/ggml-opencl/kernels/mul.cl +73 -0
  277. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mat_f16_f32.cl +130 -0
  278. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_l4_lm.cl +132 -0
  279. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mm_f32_f32_l4_lm.cl +133 -0
  280. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32.cl +189 -0
  281. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32_flat.cl +176 -0
  282. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32.cl +140 -0
  283. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32_flat.cl +222 -0
  284. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32.cl +144 -0
  285. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32_flat.cl +167 -0
  286. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32.cl +125 -0
  287. data/ext/sources/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32_flat.cl +202 -0
  288. data/ext/sources/ggml/src/ggml-opencl/kernels/norm.cl +80 -0
  289. data/ext/sources/ggml/src/ggml-opencl/kernels/rms_norm.cl +79 -0
  290. data/ext/sources/ggml/src/ggml-opencl/kernels/scale.cl +3 -2
  291. data/ext/sources/ggml/src/ggml-opencl/kernels/set_rows.cl +189 -0
  292. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +34 -13
  293. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +34 -13
  294. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f16.cl +34 -13
  295. data/ext/sources/ggml/src/ggml-opencl/kernels/softmax_f32.cl +34 -13
  296. data/ext/sources/ggml/src/ggml-opencl/kernels/sub.cl +66 -0
  297. data/ext/sources/ggml/src/ggml-opencl/kernels/transpose.cl +20 -0
  298. data/ext/sources/ggml/src/ggml-opencl/kernels/tsembd.cl +2 -2
  299. data/ext/sources/ggml/src/ggml-opencl/kernels/upscale.cl +2 -3
  300. data/ext/sources/ggml/src/ggml-opt.cpp +97 -41
  301. data/ext/sources/ggml/src/ggml-quants.c +111 -16
  302. data/ext/sources/ggml/src/ggml-quants.h +6 -0
  303. data/ext/sources/ggml/src/ggml-rpc/ggml-rpc.cpp +67 -47
  304. data/ext/sources/ggml/src/ggml-sycl/backend.hpp +2 -0
  305. data/ext/sources/ggml/src/ggml-sycl/binbcast.cpp +15 -5
  306. data/ext/sources/ggml/src/ggml-sycl/binbcast.hpp +6 -0
  307. data/ext/sources/ggml/src/ggml-sycl/concat.cpp +25 -16
  308. data/ext/sources/ggml/src/ggml-sycl/conv.cpp +10 -4
  309. data/ext/sources/ggml/src/ggml-sycl/convert.cpp +166 -99
  310. data/ext/sources/ggml/src/ggml-sycl/cpy.cpp +72 -306
  311. data/ext/sources/ggml/src/ggml-sycl/cpy.hpp +213 -1
  312. data/ext/sources/ggml/src/ggml-sycl/dmmv.cpp +67 -49
  313. data/ext/sources/ggml/src/ggml-sycl/dpct/helper.hpp +1 -31
  314. data/ext/sources/ggml/src/ggml-sycl/element_wise.cpp +79 -29
  315. data/ext/sources/ggml/src/ggml-sycl/element_wise.hpp +2 -0
  316. data/ext/sources/ggml/src/ggml-sycl/gemm.hpp +14 -26
  317. data/ext/sources/ggml/src/ggml-sycl/getrows.cpp +9 -6
  318. data/ext/sources/ggml/src/ggml-sycl/ggml-sycl.cpp +328 -323
  319. data/ext/sources/ggml/src/ggml-sycl/gla.cpp +2 -2
  320. data/ext/sources/ggml/src/ggml-sycl/im2col.cpp +2 -2
  321. data/ext/sources/ggml/src/ggml-sycl/mmq.cpp +80 -60
  322. data/ext/sources/ggml/src/ggml-sycl/mmvq.cpp +201 -132
  323. data/ext/sources/ggml/src/ggml-sycl/norm.cpp +74 -55
  324. data/ext/sources/ggml/src/ggml-sycl/quantize.hpp +133 -0
  325. data/ext/sources/ggml/src/ggml-sycl/quants.hpp +8 -9
  326. data/ext/sources/ggml/src/ggml-sycl/rope.cpp +35 -42
  327. data/ext/sources/ggml/src/ggml-sycl/set_rows.cpp +234 -0
  328. data/ext/sources/ggml/src/ggml-sycl/set_rows.hpp +8 -0
  329. data/ext/sources/ggml/src/ggml-sycl/softmax.cpp +3 -3
  330. data/ext/sources/ggml/src/ggml-sycl/tsembd.cpp +12 -6
  331. data/ext/sources/ggml/src/ggml-sycl/vecdotq.hpp +2 -6
  332. data/ext/sources/ggml/src/ggml-sycl/wkv.cpp +16 -12
  333. data/ext/sources/ggml/src/ggml-vulkan/ggml-vulkan.cpp +3492 -883
  334. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/add.comp +41 -1
  335. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/add_id.comp +42 -0
  336. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argmax.comp +13 -4
  337. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/argsort.comp +39 -29
  338. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/conv2d_mm.comp +349 -0
  339. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_from_quant.comp +2 -2
  340. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/copy_to_quant.comp +66 -12
  341. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs.comp +154 -0
  342. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_funcs_cm2.comp +21 -0
  343. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_s.comp +1 -1
  344. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq2_xxs.comp +2 -1
  345. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_s.comp +6 -5
  346. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_iq3_xxs.comp +4 -2
  347. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_mxfp4.comp +32 -0
  348. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q2_k.comp +1 -1
  349. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q3_k.comp +1 -1
  350. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q4_k.comp +1 -1
  351. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q5_k.comp +1 -1
  352. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/dequant_q6_k.comp +1 -1
  353. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/exp.comp +21 -0
  354. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn.comp +69 -24
  355. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_base.comp +60 -20
  356. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm1.comp +98 -42
  357. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_cm2.comp +64 -27
  358. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/flash_attn_split_k_reduce.comp +74 -13
  359. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/geglu_erf.comp +27 -0
  360. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/geglu_quick.comp +11 -0
  361. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/gelu_erf.comp +39 -0
  362. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/generic_binary_head.comp +4 -17
  363. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows.comp +19 -10
  364. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/get_rows_quant.comp +25 -15
  365. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/glu_head.comp +4 -0
  366. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/hardsigmoid.comp +22 -0
  367. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/hardswish.comp +22 -0
  368. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp +18 -14
  369. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/im2col_3d.comp +126 -0
  370. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_base.comp +65 -1
  371. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_nc.comp +11 -7
  372. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vecq.comp +140 -0
  373. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm.comp +144 -531
  374. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_cm2.comp +206 -38
  375. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mm_funcs.comp +556 -0
  376. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq.comp +12 -5
  377. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/mul_mmq_funcs.comp +15 -9
  378. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/multi_add.comp +111 -0
  379. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/opt_step_sgd.comp +22 -0
  380. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/pad.comp +24 -3
  381. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/quantize_q8_1.comp +53 -3
  382. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm.comp +55 -11
  383. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rms_norm_partials.comp +65 -0
  384. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/roll.comp +46 -0
  385. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_head.comp +1 -4
  386. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_multi.comp +7 -9
  387. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_neox.comp +7 -9
  388. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rope_norm.comp +7 -9
  389. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/rte.comp +5 -0
  390. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/scale.comp +1 -1
  391. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max.comp +29 -7
  392. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/soft_max_back.comp +4 -0
  393. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sqrt.comp +17 -0
  394. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/sum_rows.comp +38 -5
  395. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/swiglu_oai.comp +14 -0
  396. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/timestep_embedding.comp +4 -3
  397. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/types.comp +101 -9
  398. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/upscale.comp +69 -5
  399. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/utils.comp +25 -0
  400. data/ext/sources/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +335 -77
  401. data/ext/sources/ggml/src/ggml-webgpu/CMakeLists.txt +54 -0
  402. data/ext/sources/ggml/src/ggml-webgpu/ggml-webgpu.cpp +1558 -0
  403. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/add.tmpl.wgsl +44 -0
  404. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/add_in_place.tmpl.wgsl +41 -0
  405. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/binary_head.tmpl +45 -0
  406. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/common_decls.tmpl +930 -0
  407. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/cpy.wgsl +60 -0
  408. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +124 -0
  409. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/get_rows.tmpl.wgsl +874 -0
  410. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/memset.wgsl +40 -0
  411. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul.tmpl.wgsl +44 -0
  412. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_in_place.tmpl.wgsl +41 -0
  413. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.tmpl.wgsl +907 -0
  414. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm.wgsl +57 -0
  415. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm_in_place.wgsl +48 -0
  416. data/ext/sources/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.wgsl +81 -0
  417. data/ext/sources/ggml/src/ggml-zdnn/CMakeLists.txt +36 -0
  418. data/ext/sources/ggml/src/ggml-zdnn/common.hpp +59 -0
  419. data/ext/sources/ggml/src/ggml-zdnn/ggml-zdnn.cpp +628 -0
  420. data/ext/sources/ggml/src/ggml-zdnn/mmf.cpp +80 -0
  421. data/ext/sources/ggml/src/ggml-zdnn/mmf.hpp +12 -0
  422. data/ext/sources/ggml/src/ggml-zdnn/utils.cpp +79 -0
  423. data/ext/sources/ggml/src/ggml-zdnn/utils.hpp +19 -0
  424. data/ext/sources/ggml/src/ggml.c +478 -98
  425. data/ext/sources/ggml/src/gguf.cpp +8 -1
  426. data/ext/sources/src/whisper.cpp +23 -46
  427. data/ext/sources/tests/CMakeLists.txt +8 -1
  428. data/ext/sources/tests/test-vad-full.cpp +3 -3
  429. data/ext/sources/tests/test-vad.cpp +2 -2
  430. data/lib/whisper/model/uri.rb +1 -1
  431. data/sig/whisper.rbs +7 -0
  432. data/test/test_params.rb +8 -0
  433. data/test/test_whisper.rb +1 -1
  434. data/whispercpp.gemspec +1 -1
  435. metadata +164 -157
  436. data/ext/sources/examples/talk-llama/llama-kv-cache-unified-iswa.cpp +0 -279
  437. data/ext/sources/examples/talk-llama/llama-kv-cache-unified.cpp +0 -1841
  438. data/ext/sources/examples/talk-llama/llama-kv-cache-unified.h +0 -303
  439. data/ext/sources/ggml/include/ggml-kompute.h +0 -50
  440. data/ext/sources/ggml/src/ggml-amx/CMakeLists.txt +0 -107
  441. data/ext/sources/ggml/src/ggml-amx/common.h +0 -94
  442. data/ext/sources/ggml/src/ggml-amx/ggml-amx.cpp +0 -446
  443. data/ext/sources/ggml/src/ggml-amx/mmq.cpp +0 -2510
  444. data/ext/sources/ggml/src/ggml-amx/mmq.h +0 -17
  445. data/ext/sources/ggml/src/ggml-cann/kernels/CMakeLists.txt +0 -30
  446. data/ext/sources/ggml/src/ggml-cann/kernels/ascendc_kernels.h +0 -19
  447. data/ext/sources/ggml/src/ggml-cann/kernels/dup.cpp +0 -234
  448. data/ext/sources/ggml/src/ggml-cann/kernels/get_row_f16.cpp +0 -197
  449. data/ext/sources/ggml/src/ggml-cann/kernels/get_row_f32.cpp +0 -190
  450. data/ext/sources/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -204
  451. data/ext/sources/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -191
  452. data/ext/sources/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -218
  453. data/ext/sources/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -216
  454. data/ext/sources/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -295
  455. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cu +0 -357
  456. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f16.cuh +0 -3
  457. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cu +0 -365
  458. data/ext/sources/ggml/src/ggml-cuda/fattn-tile-f32.cuh +0 -3
  459. data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f16.cuh +0 -482
  460. data/ext/sources/ggml/src/ggml-cuda/fattn-vec-f32.cuh +0 -472
  461. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-f16.cu +0 -5
  462. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_0.cu +0 -5
  463. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q4_1.cu +0 -5
  464. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_0.cu +0 -5
  465. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q5_1.cu +0 -5
  466. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-f16-q8_0.cu +0 -5
  467. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-f16.cu +0 -5
  468. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_0.cu +0 -5
  469. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q4_1.cu +0 -5
  470. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_0.cu +0 -5
  471. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q5_1.cu +0 -5
  472. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_0-q8_0.cu +0 -5
  473. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-f16.cu +0 -5
  474. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_0.cu +0 -5
  475. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q4_1.cu +0 -5
  476. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_0.cu +0 -5
  477. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q5_1.cu +0 -5
  478. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q4_1-q8_0.cu +0 -5
  479. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-f16.cu +0 -5
  480. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_0.cu +0 -5
  481. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q4_1.cu +0 -5
  482. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_0.cu +0 -5
  483. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q5_1.cu +0 -5
  484. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_0-q8_0.cu +0 -5
  485. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-f16.cu +0 -5
  486. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_0.cu +0 -5
  487. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q4_1.cu +0 -5
  488. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_0.cu +0 -5
  489. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q5_1.cu +0 -5
  490. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q5_1-q8_0.cu +0 -5
  491. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-f16.cu +0 -5
  492. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_0.cu +0 -5
  493. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q4_1.cu +0 -5
  494. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_0.cu +0 -5
  495. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q5_1.cu +0 -5
  496. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs128-q8_0-q8_0.cu +0 -5
  497. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs256-f16-f16.cu +0 -5
  498. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-f16.cu +0 -5
  499. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_0.cu +0 -5
  500. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q4_1.cu +0 -5
  501. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_0.cu +0 -5
  502. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q5_1.cu +0 -5
  503. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f16-instance-hs64-f16-q8_0.cu +0 -5
  504. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-f16.cu +0 -5
  505. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_0.cu +0 -5
  506. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q4_1.cu +0 -5
  507. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_0.cu +0 -5
  508. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q5_1.cu +0 -5
  509. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-f16-q8_0.cu +0 -5
  510. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-f16.cu +0 -5
  511. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_0.cu +0 -5
  512. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q4_1.cu +0 -5
  513. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_0.cu +0 -5
  514. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q5_1.cu +0 -5
  515. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_0-q8_0.cu +0 -5
  516. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-f16.cu +0 -5
  517. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_0.cu +0 -5
  518. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q4_1.cu +0 -5
  519. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_0.cu +0 -5
  520. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q5_1.cu +0 -5
  521. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q4_1-q8_0.cu +0 -5
  522. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-f16.cu +0 -5
  523. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_0.cu +0 -5
  524. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q4_1.cu +0 -5
  525. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_0.cu +0 -5
  526. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q5_1.cu +0 -5
  527. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_0-q8_0.cu +0 -5
  528. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-f16.cu +0 -5
  529. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_0.cu +0 -5
  530. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q4_1.cu +0 -5
  531. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_0.cu +0 -5
  532. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q5_1.cu +0 -5
  533. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q5_1-q8_0.cu +0 -5
  534. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-f16.cu +0 -5
  535. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_0.cu +0 -5
  536. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q4_1.cu +0 -5
  537. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_0.cu +0 -5
  538. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q5_1.cu +0 -5
  539. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs128-q8_0-q8_0.cu +0 -5
  540. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs256-f16-f16.cu +0 -5
  541. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-f16.cu +0 -5
  542. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_0.cu +0 -5
  543. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q4_1.cu +0 -5
  544. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_0.cu +0 -5
  545. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q5_1.cu +0 -5
  546. data/ext/sources/ggml/src/ggml-cuda/template-instances/fattn-vec-f32-instance-hs64-f16-q8_0.cu +0 -5
  547. data/ext/sources/ggml/src/ggml-kompute/CMakeLists.txt +0 -166
  548. data/ext/sources/ggml/src/ggml-kompute/ggml-kompute.cpp +0 -2251
  549. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/common.comp +0 -112
  550. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_add.comp +0 -58
  551. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_addrow.comp +0 -25
  552. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f16.comp +0 -52
  553. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f16_f32.comp +0 -52
  554. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f16.comp +0 -52
  555. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_cpy_f32_f32.comp +0 -52
  556. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_diagmask.comp +0 -30
  557. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_gelu.comp +0 -22
  558. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows.comp +0 -17
  559. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f16.comp +0 -31
  560. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_f32.comp +0 -31
  561. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_0.comp +0 -38
  562. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q4_1.comp +0 -39
  563. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_getrows_q6_k.comp +0 -44
  564. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul.comp +0 -52
  565. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_f16.comp +0 -69
  566. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_mat_f32.comp +0 -51
  567. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_0.comp +0 -33
  568. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_1.comp +0 -35
  569. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q4_k.comp +0 -140
  570. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q6_k.comp +0 -106
  571. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mat_q8_0.comp +0 -73
  572. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n.comp +0 -52
  573. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_mul_mv_q_n_pre.comp +0 -28
  574. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_norm.comp +0 -84
  575. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_relu.comp +0 -21
  576. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rmsnorm.comp +0 -53
  577. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f16.comp +0 -52
  578. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_neox_f32.comp +0 -52
  579. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f16.comp +0 -52
  580. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_rope_norm_f32.comp +0 -52
  581. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale.comp +0 -19
  582. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_scale_8.comp +0 -23
  583. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_silu.comp +0 -22
  584. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/op_softmax.comp +0 -72
  585. data/ext/sources/ggml/src/ggml-kompute/kompute-shaders/rope_common.comp +0 -71
  586. data/ext/sources/ggml/src/ggml-metal/ggml-metal.m +0 -6280
@@ -0,0 +1,2020 @@
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 <limits>
12
+ #include <map>
13
+ #include <stdexcept>
14
+
15
+ //
16
+ // llama_kv_cache
17
+ //
18
+
19
+ llama_kv_cache::llama_kv_cache(
20
+ const llama_model & model,
21
+ ggml_type type_k,
22
+ ggml_type type_v,
23
+ bool v_trans,
24
+ bool offload,
25
+ bool unified,
26
+ uint32_t kv_size,
27
+ uint32_t n_seq_max,
28
+ uint32_t n_pad,
29
+ uint32_t n_swa,
30
+ llama_swa_type swa_type,
31
+ const layer_filter_cb & filter,
32
+ const layer_reuse_cb & reuse) :
33
+ model(model), hparams(model.hparams), v_trans(v_trans),
34
+ 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) {
35
+
36
+ GGML_ASSERT(kv_size % n_pad == 0);
37
+
38
+ const uint32_t n_layer_kv = hparams.n_layer_kv();
39
+
40
+ // create a context for each buffer type
41
+ std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
42
+ auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
43
+ auto it = ctx_map.find(buft);
44
+ if (it == ctx_map.end()) {
45
+ ggml_init_params params = {
46
+ /*.mem_size =*/ size_t(2u*(1 + n_stream)*n_layer_kv*ggml_tensor_overhead()),
47
+ /*.mem_buffer =*/ NULL,
48
+ /*.no_alloc =*/ true,
49
+ };
50
+
51
+ ggml_context * ctx = ggml_init(params);
52
+ if (!ctx) {
53
+ return nullptr;
54
+ }
55
+
56
+ ctx_map[buft] = ctx;
57
+ ctxs.emplace_back(ctx);
58
+
59
+ return ctx;
60
+ }
61
+
62
+ return it->second;
63
+ };
64
+
65
+ GGML_ASSERT(n_stream == 1 || n_stream == n_seq_max);
66
+
67
+ v_heads.resize(n_stream);
68
+ for (uint32_t s = 0; s < n_stream; ++s) {
69
+ v_heads[s] = 0;
70
+ }
71
+
72
+ v_cells.resize(n_stream);
73
+ for (uint32_t s = 0; s < n_stream; ++s) {
74
+ v_cells[s].resize(kv_size);
75
+ }
76
+
77
+ // by default, all sequence ids are mapped to the 0th stream
78
+ seq_to_stream.resize(LLAMA_MAX_SEQ, 0);
79
+
80
+ if (n_stream > 1) {
81
+ seq_to_stream.resize(n_stream, 0);
82
+ for (uint32_t s = 0; s < n_stream; ++s) {
83
+ seq_to_stream[s] = s;
84
+ }
85
+ }
86
+
87
+ // [TAG_V_CACHE_VARIABLE]
88
+ if (v_trans && hparams.is_n_embd_v_gqa_variable()) {
89
+ LLAMA_LOG_WARN("%s: the V embeddings have different sizes across layers and FA is not enabled - padding V cache to %d\n",
90
+ __func__, hparams.n_embd_v_gqa_max());
91
+ }
92
+
93
+ for (uint32_t il = 0; il < hparams.n_layer; il++) {
94
+ if (!hparams.has_kv(il)) {
95
+ LLAMA_LOG_DEBUG("%s: layer %3d: does not have KV cache\n", __func__, il);
96
+ continue;
97
+ }
98
+
99
+ if (filter && !filter(il)) {
100
+ LLAMA_LOG_DEBUG("%s: layer %3d: filtered\n", __func__, il);
101
+ continue;
102
+ }
103
+
104
+ // [TAG_V_CACHE_VARIABLE]
105
+ const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
106
+ const uint32_t n_embd_v_gqa = !v_trans ? hparams.n_embd_v_gqa(il) : hparams.n_embd_v_gqa_max();
107
+
108
+ const char * dev_name = "CPU";
109
+
110
+ ggml_backend_buffer_type_t buft = ggml_backend_cpu_buffer_type();
111
+
112
+ if (offload) {
113
+ auto * dev = model.dev_layer(il);
114
+ buft = ggml_backend_dev_buffer_type(dev);
115
+
116
+ dev_name = ggml_backend_dev_name(dev);
117
+ }
118
+
119
+ LLAMA_LOG_DEBUG("%s: layer %3d: dev = %s\n", __func__, il, dev_name);
120
+
121
+ ggml_context * ctx = ctx_for_buft(buft);
122
+ if (!ctx) {
123
+ throw std::runtime_error("failed to create ggml context for kv cache");
124
+ }
125
+
126
+ ggml_tensor * k;
127
+ ggml_tensor * v;
128
+
129
+ k = ggml_new_tensor_3d(ctx, type_k, n_embd_k_gqa, kv_size, n_stream);
130
+ v = ggml_new_tensor_3d(ctx, type_v, n_embd_v_gqa, kv_size, n_stream);
131
+
132
+ ggml_format_name(k, "cache_k_l%d", il);
133
+ ggml_format_name(v, "cache_v_l%d", il);
134
+
135
+ std::vector<ggml_tensor *> k_stream;
136
+ std::vector<ggml_tensor *> v_stream;
137
+
138
+ for (uint32_t s = 0; s < n_stream; ++s) {
139
+ k_stream.push_back(ggml_view_2d(ctx, k, n_embd_k_gqa, kv_size, k->nb[1], s*k->nb[2]));
140
+ v_stream.push_back(ggml_view_2d(ctx, v, n_embd_v_gqa, kv_size, v->nb[1], s*v->nb[2]));
141
+ }
142
+
143
+ map_layer_ids[il] = layers.size();
144
+
145
+ layers.push_back({ il, k, v, k_stream, v_stream, });
146
+ }
147
+
148
+ if (reuse) {
149
+ LLAMA_LOG_DEBUG("%s: reusing layers:\n", __func__);
150
+
151
+ for (uint32_t il = 0; il < hparams.n_layer; il++) {
152
+ const int32_t il_reuse = reuse(il);
153
+
154
+ if (il_reuse < 0) {
155
+ LLAMA_LOG_DEBUG("%s: - layer %3d: no reuse\n", __func__, il);
156
+ continue;
157
+ }
158
+
159
+ if (filter && !filter(il)) {
160
+ LLAMA_LOG_DEBUG("%s: - layer %3d: filtered\n", __func__, il);
161
+ continue;
162
+ }
163
+
164
+ GGML_ASSERT(map_layer_ids.find(il_reuse) != map_layer_ids.end());
165
+
166
+ map_layer_ids[il] = map_layer_ids[il_reuse];
167
+
168
+ LLAMA_LOG_DEBUG("%s: - layer %3d: reuse layer %d, is_swa = %d\n", __func__, il, il_reuse, hparams.is_swa(il));
169
+ }
170
+ }
171
+
172
+ // allocate tensors and initialize the buffers to avoid NaNs in the padding
173
+ for (auto it : ctx_map) {
174
+ auto * buft = it.first;
175
+ auto * ctx = it.second;
176
+
177
+ ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
178
+ if (!buf) {
179
+ throw std::runtime_error("failed to allocate buffer for kv cache");
180
+ }
181
+
182
+ 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);
183
+
184
+ ggml_backend_buffer_clear(buf, 0);
185
+ bufs.emplace_back(buf);
186
+ }
187
+
188
+ {
189
+ const size_t memory_size_k = size_k_bytes();
190
+ const size_t memory_size_v = size_v_bytes();
191
+
192
+ 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__,
193
+ (float)(memory_size_k + memory_size_v) / (1024.0f * 1024.0f), kv_size, (int) layers.size(), n_seq_max, n_stream,
194
+ ggml_type_name(type_k), (float)memory_size_k / (1024.0f * 1024.0f),
195
+ ggml_type_name(type_v), (float)memory_size_v / (1024.0f * 1024.0f));
196
+ }
197
+
198
+ const char * LLAMA_KV_CACHE_DEBUG = getenv("LLAMA_KV_CACHE_DEBUG");
199
+ debug = LLAMA_KV_CACHE_DEBUG ? atoi(LLAMA_KV_CACHE_DEBUG) : 0;
200
+ }
201
+
202
+ void llama_kv_cache::clear(bool data) {
203
+ for (uint32_t s = 0; s < n_stream; ++s) {
204
+ v_cells[s].reset();
205
+ v_heads[s] = 0;
206
+ }
207
+
208
+ if (data) {
209
+ for (auto & buf : bufs) {
210
+ ggml_backend_buffer_clear(buf.get(), 0);
211
+ }
212
+ }
213
+ }
214
+
215
+ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
216
+ GGML_ASSERT(seq_id == -1 || (seq_id >= 0 && (size_t) seq_id < seq_to_stream.size()));
217
+
218
+ if (p0 < 0) {
219
+ p0 = 0;
220
+ }
221
+
222
+ if (p1 < 0) {
223
+ p1 = std::numeric_limits<llama_pos>::max();
224
+ }
225
+
226
+ if (seq_id >= 0) {
227
+ auto & cells = v_cells[seq_to_stream[seq_id]];
228
+ auto & head = v_heads[seq_to_stream[seq_id]];
229
+
230
+ uint32_t new_head = cells.size();
231
+
232
+ for (uint32_t i = 0; i < cells.size(); ++i) {
233
+ if (!cells.pos_in(i, p0, p1)) {
234
+ continue;
235
+ }
236
+
237
+ if (cells.seq_has(i, seq_id) && cells.seq_rm(i, seq_id)) {
238
+ if (new_head == cells.size()) {
239
+ new_head = i;
240
+ }
241
+ }
242
+ }
243
+
244
+ // If we freed up a slot, set head to it so searching can start there.
245
+ if (new_head != cells.size() && new_head < head) {
246
+ head = new_head;
247
+ }
248
+ } else {
249
+ // match any sequence
250
+ for (uint32_t s = 0; s < n_stream; ++s) {
251
+ auto & cells = v_cells[s];
252
+ auto & head = v_heads[s];
253
+
254
+ uint32_t new_head = cells.size();
255
+
256
+ for (uint32_t i = 0; i < cells.size(); ++i) {
257
+ if (!cells.pos_in(i, p0, p1)) {
258
+ continue;
259
+ }
260
+
261
+ cells.rm(i);
262
+
263
+ if (new_head == cells.size()) {
264
+ new_head = i;
265
+ }
266
+ }
267
+
268
+ // If we freed up a slot, set head to it so searching can start there.
269
+ if (new_head != cells.size() && new_head < head) {
270
+ head = new_head;
271
+ }
272
+ }
273
+ }
274
+
275
+ return true;
276
+ }
277
+
278
+ void llama_kv_cache::seq_cp(llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) {
279
+ GGML_ASSERT(seq_id_src >= 0 && (size_t) seq_id_src < seq_to_stream.size());
280
+ GGML_ASSERT(seq_id_dst >= 0 && (size_t) seq_id_dst < seq_to_stream.size());
281
+
282
+ const auto s0 = seq_to_stream[seq_id_src];
283
+ const auto s1 = seq_to_stream[seq_id_dst];
284
+
285
+ if (s0 == s1) {
286
+ // since both sequences are in the same stream, no data copy is necessary
287
+ // we just have to update the cells meta data
288
+
289
+ auto & cells = v_cells[s0];
290
+
291
+ if (seq_id_src == seq_id_dst) {
292
+ return;
293
+ }
294
+
295
+ if (p0 < 0) {
296
+ p0 = 0;
297
+ }
298
+
299
+ if (p1 < 0) {
300
+ p1 = std::numeric_limits<llama_pos>::max();
301
+ }
302
+
303
+ for (uint32_t i = 0; i < cells.size(); ++i) {
304
+ if (!cells.pos_in(i, p0, p1)) {
305
+ continue;
306
+ }
307
+
308
+ if (cells.seq_has(i, seq_id_src)) {
309
+ cells.seq_add(i, seq_id_dst);
310
+ }
311
+ }
312
+
313
+ return;
314
+ }
315
+
316
+ // cross-stream sequence copies require to copy the actual buffer data
317
+
318
+ bool is_full = true;
319
+
320
+ if (p0 > 0 && p0 + 1 < (int) get_size()) {
321
+ is_full = false;
322
+ }
323
+
324
+ if (p1 > 0 && p1 + 1 < (int) get_size()) {
325
+ is_full = false;
326
+ }
327
+
328
+ GGML_ASSERT(is_full && "seq_cp() is only supported for full KV buffers");
329
+
330
+ // enqueue the copy operation - the buffer copy will be performed during the next update
331
+ sc_info.ssrc.push_back(s0);
332
+ sc_info.sdst.push_back(s1);
333
+
334
+ v_cells[s1].reset();
335
+ for (uint32_t i = 0; i < v_cells[s0].size(); ++i) {
336
+ if (v_cells[s0].seq_has(i, seq_id_src)) {
337
+ llama_pos pos = v_cells[s0].pos_get(i);
338
+ llama_pos shift = v_cells[s0].get_shift(i);
339
+
340
+ if (shift != 0) {
341
+ pos -= shift;
342
+ assert(pos >= 0);
343
+ }
344
+
345
+ v_cells[s1].pos_set(i, pos);
346
+ v_cells[s1].seq_add(i, seq_id_dst);
347
+
348
+ if (shift != 0) {
349
+ v_cells[s1].pos_add(i, shift);
350
+ }
351
+ }
352
+ }
353
+
354
+ v_heads[s1] = v_heads[s0];
355
+
356
+ //for (uint32_t s = 0; s < n_stream; ++s) {
357
+ // 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));
358
+ //}
359
+ }
360
+
361
+ void llama_kv_cache::seq_keep(llama_seq_id seq_id) {
362
+ GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
363
+
364
+ auto & cells = v_cells[seq_to_stream[seq_id]];
365
+ auto & head = v_heads[seq_to_stream[seq_id]];
366
+
367
+ uint32_t new_head = cells.size();
368
+
369
+ for (uint32_t i = 0; i < cells.size(); ++i) {
370
+ if (cells.seq_keep(i, seq_id)) {
371
+ if (new_head == cells.size()) {
372
+ new_head = i;
373
+ }
374
+ }
375
+ }
376
+
377
+ // If we freed up a slot, set head to it so searching can start there.
378
+ if (new_head != cells.size() && new_head < head) {
379
+ head = new_head;
380
+ }
381
+ }
382
+
383
+ void llama_kv_cache::seq_add(llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) {
384
+ GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
385
+
386
+ auto & cells = v_cells[seq_to_stream[seq_id]];
387
+ auto & head = v_heads[seq_to_stream[seq_id]];
388
+
389
+ if (shift == 0) {
390
+ return;
391
+ }
392
+
393
+ uint32_t new_head = cells.size();
394
+
395
+ if (p0 < 0) {
396
+ p0 = 0;
397
+ }
398
+
399
+ if (p1 < 0) {
400
+ p1 = std::numeric_limits<llama_pos>::max();
401
+ }
402
+
403
+ // If there is no range then return early to avoid looping over all cells.
404
+ if (p0 == p1) {
405
+ return;
406
+ }
407
+
408
+ for (uint32_t i = 0; i < cells.size(); ++i) {
409
+ if (!cells.pos_in(i, p0, p1)) {
410
+ continue;
411
+ }
412
+
413
+ if (cells.seq_has(i, seq_id)) {
414
+ if (cells.pos_add(i, shift)) {
415
+ if (new_head == cells.size()) {
416
+ new_head = i;
417
+ }
418
+ }
419
+ }
420
+ }
421
+
422
+ // If we freed up a slot, set head to it so searching can start there.
423
+ // Otherwise we just start the next search from the beginning.
424
+ head = new_head != cells.size() ? new_head : 0;
425
+ }
426
+
427
+ void llama_kv_cache::seq_div(llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) {
428
+ GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
429
+
430
+ auto & cells = v_cells[seq_to_stream[seq_id]];
431
+
432
+ if (d == 1) {
433
+ return;
434
+ }
435
+
436
+ if (p0 < 0) {
437
+ p0 = 0;
438
+ }
439
+
440
+ if (p1 < 0) {
441
+ p1 = std::numeric_limits<llama_pos>::max();
442
+ }
443
+
444
+ // If there is no range then return early to avoid looping over the cache.
445
+ if (p0 == p1) {
446
+ return;
447
+ }
448
+
449
+ for (uint32_t i = 0; i < cells.size(); ++i) {
450
+ if (!cells.pos_in(i, p0, p1)) {
451
+ continue;
452
+ }
453
+
454
+ if (cells.seq_has(i, seq_id)) {
455
+ cells.pos_div(i, d);
456
+ }
457
+ }
458
+ }
459
+
460
+ llama_pos llama_kv_cache::seq_pos_min(llama_seq_id seq_id) const {
461
+ GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
462
+
463
+ const auto & cells = v_cells[seq_to_stream[seq_id]];
464
+
465
+ return cells.seq_pos_min(seq_id);
466
+ }
467
+
468
+ llama_pos llama_kv_cache::seq_pos_max(llama_seq_id seq_id) const {
469
+ GGML_ASSERT(seq_id >= 0 && (size_t) seq_id < seq_to_stream.size());
470
+
471
+ const auto & cells = v_cells[seq_to_stream[seq_id]];
472
+
473
+ return cells.seq_pos_max(seq_id);
474
+ }
475
+
476
+ std::map<ggml_backend_buffer_type_t, size_t> llama_kv_cache::memory_breakdown() const {
477
+ std::map<ggml_backend_buffer_type_t, size_t> ret;
478
+ for (const ggml_backend_buffer_ptr & buf_ptr : bufs) {
479
+ ret[ggml_backend_buffer_get_type(buf_ptr.get())] += ggml_backend_buffer_get_size(buf_ptr.get());
480
+ }
481
+ return ret;
482
+ }
483
+
484
+ llama_memory_context_ptr llama_kv_cache::init_batch(
485
+ llama_batch_allocr & balloc,
486
+ uint32_t n_ubatch,
487
+ bool embd_all) {
488
+ GGML_UNUSED(embd_all);
489
+
490
+ do {
491
+ balloc.split_reset();
492
+
493
+ std::vector<llama_ubatch> ubatches;
494
+ while (true) {
495
+ auto ubatch = n_stream == 1 ? balloc.split_simple(n_ubatch) : balloc.split_equal(n_ubatch, true);
496
+
497
+ if (ubatch.n_tokens == 0) {
498
+ break;
499
+ }
500
+
501
+ ubatches.push_back(std::move(ubatch)); // NOLINT
502
+ }
503
+
504
+ if (balloc.get_n_used() < balloc.get_n_tokens()) {
505
+ // failed to find a suitable split
506
+ break;
507
+ }
508
+
509
+ auto sinfos = prepare(ubatches);
510
+ if (sinfos.empty()) {
511
+ break;
512
+ }
513
+
514
+ return std::make_unique<llama_kv_cache_context>(
515
+ this, std::move(sinfos), std::move(ubatches));
516
+ } while (false);
517
+
518
+ return std::make_unique<llama_kv_cache_context>(LLAMA_MEMORY_STATUS_FAILED_PREPARE);
519
+ }
520
+
521
+ llama_memory_context_ptr llama_kv_cache::init_full() {
522
+ return std::make_unique<llama_kv_cache_context>(this);
523
+ }
524
+
525
+ llama_memory_context_ptr llama_kv_cache::init_update(llama_context * lctx, bool optimize) {
526
+ GGML_UNUSED(optimize);
527
+
528
+ bool do_shift = get_has_shift();
529
+
530
+ return std::make_unique<llama_kv_cache_context>(this, lctx, do_shift, std::move(sc_info));
531
+ }
532
+
533
+ llama_kv_cache::slot_info_vec_t llama_kv_cache::prepare(const std::vector<llama_ubatch> & ubatches) {
534
+ llama_kv_cache::slot_info_vec_t res;
535
+
536
+ struct state_t {
537
+ slot_info sinfo; // slot info for the ubatch
538
+
539
+ std::vector<uint32_t> v_heads_old; // old positions of the heads, before placing the ubatch
540
+
541
+ std::vector<llama_kv_cells> v_cells; // copy of the old cells, before placing the ubatch
542
+ };
543
+
544
+ // remember the old state of the cells so we can restore it in the end
545
+ std::vector<state_t> states;
546
+
547
+ bool success = true;
548
+
549
+ for (const auto & ubatch : ubatches) {
550
+ // only find a suitable slot for the ubatch. don't modify the cells yet
551
+ const auto sinfo_new = find_slot(ubatch, false);
552
+ if (sinfo_new.empty()) {
553
+ success = false;
554
+ break;
555
+ }
556
+
557
+ // remeber the position that we found
558
+ res.push_back(sinfo_new);
559
+
560
+ // store the old state of the cells in the recovery stack
561
+ {
562
+ state_t state = { sinfo_new, v_heads, {} };
563
+
564
+ for (uint32_t s = 0; s < sinfo_new.n_stream(); ++s) {
565
+ auto & cells = v_cells[sinfo_new.strm[s]];
566
+
567
+ state.v_cells.push_back(cells.cp(sinfo_new.idxs[s]));
568
+ }
569
+
570
+ states.push_back(std::move(state));
571
+ }
572
+
573
+ // now emplace the ubatch
574
+ apply_ubatch(sinfo_new, ubatch);
575
+ }
576
+
577
+ GGML_ASSERT(!states.empty() || !success);
578
+
579
+ // iterate backwards and restore the cells to their original state
580
+ for (auto it = states.rbegin(); it != states.rend(); ++it) {
581
+ const auto & sinfo = it->sinfo;
582
+
583
+ for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
584
+ auto & cells = v_cells[sinfo.strm[s]];
585
+ auto & head = v_heads[sinfo.strm[s]];
586
+
587
+ cells.set(sinfo.idxs[s], it->v_cells[s]);
588
+ head = it->v_heads_old[s];
589
+ }
590
+ }
591
+
592
+ if (!success) {
593
+ return {};
594
+ }
595
+
596
+ return res;
597
+ }
598
+
599
+ bool llama_kv_cache::update(llama_context * lctx, bool do_shift, const stream_copy_info & sc_info) {
600
+ bool updated = false;
601
+
602
+ auto * sched = lctx->get_sched();
603
+
604
+ if (!sc_info.empty()) {
605
+ assert(n_stream > 1 && "stream copy should never happen with a single stream");
606
+
607
+ llama_synchronize(lctx);
608
+
609
+ const size_t n_copy = sc_info.ssrc.size();
610
+
611
+ for (size_t i = 0; i < n_copy; ++i) {
612
+ const auto ssrc = sc_info.ssrc[i];
613
+ const auto sdst = sc_info.sdst[i];
614
+
615
+ assert(ssrc < n_stream);
616
+ assert(sdst < n_stream);
617
+
618
+ LLAMA_LOG_DEBUG("%s: copying KV buffer: stream %d to stream %d\n", __func__, ssrc, sdst);
619
+
620
+ assert(ssrc != sdst);
621
+
622
+ for (uint32_t il = 0; il < layers.size(); ++il) {
623
+ const auto & layer = layers[il];
624
+
625
+ ggml_backend_tensor_copy(layer.k_stream[ssrc], layer.k_stream[sdst]);
626
+ ggml_backend_tensor_copy(layer.v_stream[ssrc], layer.v_stream[sdst]);
627
+ }
628
+ }
629
+ }
630
+
631
+ if (do_shift) {
632
+ if (!get_can_shift()) {
633
+ GGML_ABORT("The current KV cache / model configuration does not support K-shift");
634
+ }
635
+
636
+ LLAMA_LOG_DEBUG("%s: applying K-shift\n", __func__);
637
+
638
+ // apply K-shift if needed
639
+ if (hparams.rope_type != LLAMA_ROPE_TYPE_NONE) {
640
+ ggml_backend_sched_reset(sched);
641
+
642
+ auto * res = lctx->get_gf_res_reserve();
643
+
644
+ res->reset();
645
+
646
+ auto * gf = build_graph_shift(res, lctx);
647
+ if (!ggml_backend_sched_alloc_graph(sched, gf)) {
648
+ LLAMA_LOG_ERROR("%s: failed to allocate compute graph for K-shift\n", __func__);
649
+ return updated;
650
+ }
651
+
652
+ res->set_inputs(nullptr);
653
+
654
+ if (lctx->graph_compute(gf, false) != GGML_STATUS_SUCCESS) {
655
+ LLAMA_LOG_ERROR("%s: failed to compute K-shift\n", __func__);
656
+ return updated;
657
+ }
658
+
659
+ updated = true;
660
+ }
661
+
662
+ for (uint32_t s = 0; s < n_stream; ++s) {
663
+ auto & cells = v_cells[s];
664
+
665
+ cells.reset_shift();
666
+ }
667
+ }
668
+
669
+ return updated;
670
+ }
671
+
672
+ llama_kv_cache::slot_info llama_kv_cache::find_slot(const llama_ubatch & ubatch, bool cont) const {
673
+
674
+ if (debug > 0) {
675
+ for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
676
+ const auto seq_id = ubatch.seq_id_unq[s];
677
+ const auto stream_id = seq_to_stream[seq_id];
678
+ const auto & cells = v_cells[stream_id];
679
+ const uint32_t head_cur = v_heads[stream_id];
680
+
681
+ LLAMA_LOG_DEBUG("%s: stream[%d], n = %5d, used = %5d, head = %5d, size = %5d, n_swa = %5d\n",
682
+ __func__, stream_id, cells.used_max_p1(), cells.get_used(), head_cur, get_size(), n_swa);
683
+
684
+ if ((debug == 2 && n_swa > 0) || debug > 2) {
685
+ std::string ss;
686
+ for (uint32_t i = 0; i < cells.size(); ++i) {
687
+ if (cells.is_empty(i)) {
688
+ ss += '.';
689
+ } else {
690
+ assert(cells.seq_count(i) >= 1);
691
+
692
+ if (cells.seq_count(i) == 1) {
693
+ ss += std::to_string(cells.seq_get(i));
694
+ } else {
695
+ ss += 'M';
696
+ }
697
+ }
698
+ if (i%256 == 255) {
699
+ ss += " *";
700
+ ss += '\n';
701
+ }
702
+ }
703
+ LLAMA_LOG_DEBUG("\n%s\n", ss.c_str());
704
+ }
705
+
706
+ if ((debug == 2 && n_swa > 0) || debug > 2) {
707
+ std::string ss;
708
+ for (uint32_t i = 0; i < cells.size(); ++i) {
709
+ std::string cur;
710
+ if (cells.is_empty(i)) {
711
+ cur = '.';
712
+ } else {
713
+ cur = std::to_string(cells.pos_get(i));
714
+ }
715
+ const int n = cur.size();
716
+ for (int j = 0; j < 5 - n; ++j) {
717
+ cur += ' ';
718
+ }
719
+ ss += cur;
720
+ if (i%256 == 255) {
721
+ ss += " *";
722
+ }
723
+ if (i%64 == 63) {
724
+ ss += '\n';
725
+ }
726
+ }
727
+ LLAMA_LOG_DEBUG("\n%s\n", ss.c_str());
728
+ }
729
+
730
+ for (int s = 0; s < LLAMA_MAX_SEQ; ++s) {
731
+ if (cells.seq_pos_min(s) < 0) {
732
+ continue;
733
+ }
734
+
735
+ 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));
736
+ }
737
+ }
738
+ }
739
+
740
+ uint32_t n_tokens = ubatch.n_tokens;
741
+ uint32_t n_seqs = 1;
742
+
743
+ if (n_stream > 1) {
744
+ GGML_ASSERT(n_tokens % ubatch.n_seqs_unq == 0);
745
+
746
+ n_seqs = ubatch.n_seqs_unq;
747
+ n_tokens = n_tokens / n_seqs;
748
+ }
749
+
750
+ slot_info res = {
751
+ /*.s0 =*/ LLAMA_MAX_SEQ,
752
+ /*.s1 =*/ 0,
753
+ /*.strm =*/ { },
754
+ /*.idxs =*/ { },
755
+ };
756
+
757
+ res.resize(n_seqs);
758
+
759
+ for (uint32_t s = 0; s < n_seqs; ++s) {
760
+ const auto seq_id = ubatch.seq_id_unq[s];
761
+
762
+ if (n_stream > 1) {
763
+ GGML_ASSERT(ubatch.n_seq_id[s*n_tokens] == 1);
764
+ GGML_ASSERT(ubatch.seq_id [s*n_tokens][0] == seq_id);
765
+ }
766
+
767
+ res.s0 = std::min<uint32_t>(res.s0, seq_to_stream[seq_id]);
768
+ res.s1 = std::max<uint32_t>(res.s1, seq_to_stream[seq_id]);
769
+
770
+ res.strm[s] = seq_to_stream[seq_id];
771
+ res.idxs[s].reserve(n_tokens);
772
+
773
+ const auto & cells = v_cells[seq_to_stream[seq_id]];
774
+
775
+ uint32_t head_cur = v_heads[seq_to_stream[seq_id]];
776
+
777
+ // if we have enough unused cells before the current head ->
778
+ // better to start searching from the beginning of the cache, hoping to fill it
779
+ if (head_cur > cells.get_used() + 2*n_tokens) {
780
+ head_cur = 0;
781
+ }
782
+
783
+ if (n_tokens > cells.size()) {
784
+ LLAMA_LOG_ERROR("%s: n_tokens = %d > size = %u\n", __func__, n_tokens, cells.size());
785
+ return { };
786
+ }
787
+
788
+ uint32_t n_tested = 0;
789
+
790
+ // for continuous slots, we test that all tokens in the ubatch fit, starting from the current head
791
+ // for non-continuous slots, we test the tokens one by one
792
+ const uint32_t n_test = cont ? n_tokens : 1;
793
+
794
+ while (true) {
795
+ if (head_cur + n_test > cells.size()) {
796
+ n_tested += cells.size() - head_cur;
797
+ head_cur = 0;
798
+ continue;
799
+ }
800
+
801
+ for (uint32_t i = 0; i < n_test; i++) {
802
+ const auto idx = head_cur;
803
+
804
+ head_cur++;
805
+ n_tested++;
806
+
807
+ //const llama_pos pos = ubatch.pos[i];
808
+ //const llama_seq_id seq_id = ubatch.seq_id[i][0];
809
+
810
+ // can we use this cell? either:
811
+ // - the cell is empty
812
+ // - the cell is occupied only by one sequence:
813
+ // - (disabled) mask causally, if the sequence is the same as the one we are inserting
814
+ // - mask SWA, using current max pos for that sequence in the cache
815
+ // always insert in the cell with minimum pos
816
+ bool can_use = cells.is_empty(idx);
817
+
818
+ if (!can_use && cells.seq_count(idx) == 1) {
819
+ const llama_pos pos_cell = cells.pos_get(idx);
820
+
821
+ // (disabled) causal mask
822
+ // note: it's better to purge any "future" tokens beforehand
823
+ //if (cells.seq_has(idx, seq_id)) {
824
+ // can_use = pos_cell >= pos;
825
+ //}
826
+
827
+ if (!can_use) {
828
+ const llama_seq_id seq_id_cell = cells.seq_get(idx);
829
+
830
+ // SWA mask
831
+ if (is_masked_swa(pos_cell, cells.seq_pos_max(seq_id_cell) + 1)) {
832
+ can_use = true;
833
+ }
834
+ }
835
+ }
836
+
837
+ if (can_use) {
838
+ res.idxs[s].push_back(idx);
839
+ } else {
840
+ if (cont) {
841
+ break;
842
+ }
843
+ }
844
+ }
845
+
846
+ if (res.idxs[s].size() == n_tokens) {
847
+ break;
848
+ }
849
+
850
+ if (cont) {
851
+ res.idxs[s].clear();
852
+ }
853
+
854
+ if (n_tested >= cells.size()) {
855
+ //LLAMA_LOG_ERROR("%s: failed to find a slot for %d tokens\n", __func__, n_tokens);
856
+ return { };
857
+ }
858
+ }
859
+
860
+ // we didn't find a suitable slot - return empty result
861
+ if (res.idxs[s].size() < n_tokens) {
862
+ return { };
863
+ }
864
+ }
865
+
866
+ assert(res.s1 >= res.s0);
867
+
868
+ return res;
869
+ }
870
+
871
+ void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & ubatch) {
872
+ // keep track of the max sequence position that we would overwrite with this ubatch
873
+ // for non-SWA cache, this would be always empty
874
+ llama_seq_id seq_pos_max_rm[LLAMA_MAX_SEQ];
875
+ for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
876
+ seq_pos_max_rm[s] = -1;
877
+ }
878
+
879
+ assert(ubatch.n_tokens == sinfo.n_stream()*sinfo.size());
880
+
881
+ for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
882
+ for (uint32_t ii = 0; ii < sinfo.size(); ++ii) {
883
+ const uint32_t i = s*sinfo.size() + ii;
884
+
885
+ auto & cells = v_cells[sinfo.strm[s]];
886
+
887
+ const auto idx = sinfo.idxs[s][ii];
888
+
889
+ if (!cells.is_empty(idx)) {
890
+ assert(cells.seq_count(idx) == 1);
891
+
892
+ const llama_seq_id seq_id = cells.seq_get(idx);
893
+ const llama_pos pos = cells.pos_get(idx);
894
+
895
+ seq_pos_max_rm[seq_id] = std::max(seq_pos_max_rm[seq_id], pos);
896
+
897
+ cells.rm(idx);
898
+ }
899
+
900
+ cells.pos_set(idx, ubatch.pos[i]);
901
+
902
+ for (int32_t s = 0; s < ubatch.n_seq_id[i]; s++) {
903
+ cells.seq_add(idx, ubatch.seq_id[i][s]);
904
+ }
905
+ }
906
+ }
907
+
908
+ // note: we want to preserve the invariant that all positions between [pos_min, pos_max] for each sequence
909
+ // will be present in the cache. so we have to purge any position which is less than those we would overwrite
910
+ // ref: https://github.com/ggml-org/llama.cpp/pull/13746#issuecomment-2916057092
911
+ for (uint32_t s = 0; s < LLAMA_MAX_SEQ; ++s) {
912
+ if (seq_pos_max_rm[s] == -1) {
913
+ continue;
914
+ }
915
+
916
+ GGML_ASSERT(s < seq_to_stream.size());
917
+
918
+ auto & cells = v_cells[seq_to_stream[s]];
919
+
920
+ if (cells.seq_pos_min(s) <= seq_pos_max_rm[s]) {
921
+ LLAMA_LOG_DEBUG("%s: purging positions [%d, %d] of sequence %d from KV cache\n",
922
+ __func__, cells.seq_pos_min(s), seq_pos_max_rm[s], s);
923
+
924
+ seq_rm(s, cells.seq_pos_min(s), seq_pos_max_rm[s] + 1);
925
+ }
926
+ }
927
+
928
+ // move the head at the end of the slot
929
+ for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
930
+ auto & head = v_heads[sinfo.strm[s]];
931
+
932
+ head = sinfo.idxs[s].back() + 1;
933
+ }
934
+ }
935
+
936
+ bool llama_kv_cache::get_can_shift() const {
937
+ return true;
938
+ }
939
+
940
+ uint32_t llama_kv_cache::get_size() const {
941
+ const auto & cells = v_cells[seq_to_stream[0]];
942
+
943
+ return cells.size();
944
+ }
945
+
946
+ uint32_t llama_kv_cache::get_n_stream() const {
947
+ return n_stream;
948
+ }
949
+
950
+ bool llama_kv_cache::get_has_shift() const {
951
+ bool result = false;
952
+
953
+ for (uint32_t s = 0; s < n_stream; ++s) {
954
+ result |= v_cells[s].get_has_shift();
955
+ }
956
+
957
+ return result;
958
+ }
959
+
960
+ uint32_t llama_kv_cache::get_n_kv(const slot_info & sinfo) const {
961
+ uint32_t result = 0;
962
+
963
+ for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
964
+ const auto & cells = v_cells[sinfo.strm[s]];
965
+
966
+ result = std::max(std::min(cells.size(), std::max(n_pad, GGML_PAD(cells.used_max_p1(), n_pad))), result);
967
+ }
968
+
969
+ return result;
970
+ }
971
+
972
+ ggml_tensor * llama_kv_cache::get_k(ggml_context * ctx, int32_t il, uint32_t n_kv, const slot_info & sinfo) const {
973
+ const int32_t ikv = map_layer_ids.at(il);
974
+
975
+ auto * k = layers[ikv].k;
976
+
977
+ const uint64_t kv_size = get_size();
978
+ const uint64_t n_embd_k_gqa = k->ne[0];
979
+
980
+ assert(n_embd_k_gqa == hparams.n_embd_k_gqa(il));
981
+
982
+ const uint32_t ns = sinfo.s1 - sinfo.s0 + 1;
983
+
984
+ return ggml_view_4d(ctx, k,
985
+ hparams.n_embd_head_k, hparams.n_head_kv(il), n_kv, ns,
986
+ ggml_row_size(k->type, hparams.n_embd_head_k),
987
+ ggml_row_size(k->type, n_embd_k_gqa),
988
+ ggml_row_size(k->type, n_embd_k_gqa*kv_size),
989
+ ggml_row_size(k->type, n_embd_k_gqa*kv_size)*sinfo.s0);
990
+ }
991
+
992
+ ggml_tensor * llama_kv_cache::get_v(ggml_context * ctx, int32_t il, uint32_t n_kv, const slot_info & sinfo) const {
993
+ const int32_t ikv = map_layer_ids.at(il);
994
+
995
+ auto * v = layers[ikv].v;
996
+
997
+ const uint64_t kv_size = get_size();
998
+ const uint64_t n_embd_v_gqa = v->ne[0];
999
+
1000
+ // [TAG_V_CACHE_VARIABLE]
1001
+ assert(n_embd_v_gqa >= hparams.n_embd_v_gqa(il));
1002
+
1003
+ const uint32_t ns = sinfo.s1 - sinfo.s0 + 1;
1004
+
1005
+ if (!v_trans) {
1006
+ // note: v->nb[1] <= v->nb[2]
1007
+ return ggml_view_4d(ctx, v,
1008
+ hparams.n_embd_head_v, hparams.n_head_kv(il), n_kv, ns,
1009
+ ggml_row_size(v->type, hparams.n_embd_head_v), // v->nb[1]
1010
+ ggml_row_size(v->type, n_embd_v_gqa), // v->nb[2]
1011
+ ggml_row_size(v->type, n_embd_v_gqa*kv_size), // v->nb[3]
1012
+ ggml_row_size(v->type, n_embd_v_gqa*kv_size)*sinfo.s0);
1013
+ }
1014
+
1015
+ // note: v->nb[1] > v->nb[2]
1016
+ return ggml_view_4d(ctx, v,
1017
+ n_kv, hparams.n_head_kv(il), hparams.n_embd_head_v, ns,
1018
+ ggml_row_size(v->type, kv_size*hparams.n_embd_head_v), // v->nb[1]
1019
+ ggml_row_size(v->type, kv_size), // v->nb[2]
1020
+ ggml_row_size(v->type, kv_size*n_embd_v_gqa), // v->nb[3]
1021
+ ggml_row_size(v->type, kv_size*n_embd_v_gqa)*sinfo.s0);
1022
+ }
1023
+
1024
+ 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 {
1025
+ GGML_UNUSED(sinfo);
1026
+
1027
+ const int32_t ikv = map_layer_ids.at(il);
1028
+
1029
+ ggml_tensor * k = layers[ikv].k;
1030
+
1031
+ const int64_t n_embd_head = k_cur->ne[0];
1032
+ const int64_t n_head = k_cur->ne[1];
1033
+ const int64_t n_tokens = k_cur->ne[2];
1034
+
1035
+ const int64_t n_embd_gqa = n_embd_head*n_head;
1036
+
1037
+ // we can merge dims 0 and 1
1038
+ // TODO: add ggml helper function for this?
1039
+ GGML_ASSERT(ggml_row_size(k_cur->type, n_embd_head) == k_cur->nb[1]);
1040
+
1041
+ k_cur = ggml_view_2d(ctx, k_cur, n_embd_gqa, n_tokens, k_cur->nb[2], 0);
1042
+
1043
+ const int64_t n_stream = k->ne[2];
1044
+
1045
+ if (n_stream > 1) {
1046
+ const int64_t kv_size = get_size();
1047
+
1048
+ assert(n_embd_gqa == k->ne[0]);
1049
+ assert(kv_size == k->ne[1]);
1050
+
1051
+ // merge the buffer across all streams because the idxs are global
1052
+ k = ggml_reshape_2d(ctx, k, n_embd_gqa, kv_size*n_stream);
1053
+ }
1054
+
1055
+ // store the current K values into the cache
1056
+ return ggml_set_rows(ctx, k, k_cur, k_idxs);
1057
+ }
1058
+
1059
+ 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 {
1060
+ GGML_UNUSED(sinfo);
1061
+
1062
+ const int32_t ikv = map_layer_ids.at(il);
1063
+
1064
+ auto * v = layers[ikv].v;
1065
+
1066
+ const int64_t n_embd_head = v_cur->ne[0];
1067
+ const int64_t n_head = v_cur->ne[1];
1068
+ const int64_t n_tokens = v_cur->ne[2];
1069
+
1070
+ const int64_t n_embd_gqa = n_embd_head*n_head;
1071
+
1072
+ // we can merge dims 0 and 1
1073
+ GGML_ASSERT(ggml_row_size(v_cur->type, n_embd_head) == v_cur->nb[1]);
1074
+
1075
+ const int64_t n_stream = v->ne[2];
1076
+
1077
+ // take this branch when FA is enabled (the V cache is not transposed)
1078
+ if (!v_trans) {
1079
+ v_cur = ggml_view_2d(ctx, v_cur, n_embd_gqa, n_tokens, v_cur->nb[2], 0);
1080
+
1081
+ if (n_stream > 1) {
1082
+ const int64_t kv_size = get_size();
1083
+
1084
+ assert(n_embd_gqa == v->ne[0]);
1085
+ assert(kv_size == v->ne[1]);
1086
+
1087
+ // merge the buffer across all streams because the idxs are global
1088
+ v = ggml_reshape_2d(ctx, v, n_embd_gqa, kv_size*n_stream);
1089
+ }
1090
+
1091
+ return ggml_set_rows(ctx, v, v_cur, v_idxs);
1092
+ }
1093
+
1094
+ if (ggml_row_size(v_cur->type, n_embd_gqa) == v_cur->nb[2]) {
1095
+ // we can merge dims 0, 1 and 2
1096
+ v_cur = ggml_reshape_2d(ctx, v_cur, n_embd_gqa, n_tokens);
1097
+ } else {
1098
+ // otherwise -> make a copy to get contiguous data
1099
+ v_cur = ggml_cont_2d (ctx, v_cur, n_embd_gqa, n_tokens);
1100
+ }
1101
+
1102
+ // [TAG_V_CACHE_VARIABLE]
1103
+ if (n_embd_gqa < v->ne[0]) {
1104
+ v_cur = ggml_pad(ctx, v_cur, v->ne[0] - n_embd_gqa, 0, 0, 0);
1105
+ }
1106
+
1107
+ // in this branch the v_idxs are constructed in such a way that each row is a single head element
1108
+ ggml_tensor * v_view = ggml_reshape_2d(ctx, v, 1, ggml_nelements(v));
1109
+
1110
+ v_cur = ggml_reshape_2d(ctx, v_cur, 1, ggml_nelements(v_cur));
1111
+
1112
+ return ggml_set_rows(ctx, v_view, v_cur, v_idxs);
1113
+ }
1114
+
1115
+ ggml_tensor * llama_kv_cache::build_input_k_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
1116
+ const uint32_t n_tokens = ubatch.n_tokens;
1117
+
1118
+ ggml_tensor * k_idxs = ggml_new_tensor_1d(ctx, GGML_TYPE_I64, n_tokens);
1119
+
1120
+ ggml_set_input(k_idxs);
1121
+
1122
+ return k_idxs;
1123
+ }
1124
+
1125
+ ggml_tensor * llama_kv_cache::build_input_v_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
1126
+ const uint32_t n_tokens = ubatch.n_tokens;
1127
+
1128
+ ggml_tensor * v_idxs;
1129
+
1130
+ if (!v_trans) {
1131
+ v_idxs = ggml_new_tensor_1d(ctx, GGML_TYPE_I64, n_tokens);
1132
+ } else {
1133
+ v_idxs = ggml_new_tensor_1d(ctx, GGML_TYPE_I64, n_tokens*hparams.n_embd_v_gqa_max());
1134
+ }
1135
+
1136
+ ggml_set_input(v_idxs);
1137
+
1138
+ return v_idxs;
1139
+ }
1140
+
1141
+ void llama_kv_cache::set_input_k_idxs(ggml_tensor * dst, const llama_ubatch * ubatch, const slot_info & sinfo) const {
1142
+ const uint32_t n_tokens = ubatch->n_tokens;
1143
+ GGML_ASSERT(n_tokens == (int64_t) sinfo.size()*sinfo.n_stream());
1144
+
1145
+ GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1146
+ int64_t * data = (int64_t *) dst->data;
1147
+
1148
+ for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
1149
+ const int64_t offs = sinfo.strm[s]*get_size();
1150
+
1151
+ for (uint32_t i = 0; i < sinfo.size(); ++i) {
1152
+ data[s*sinfo.size() + i] = offs + sinfo.idxs[s][i];
1153
+ }
1154
+ }
1155
+ }
1156
+
1157
+ void llama_kv_cache::set_input_v_idxs(ggml_tensor * dst, const llama_ubatch * ubatch, const slot_info & sinfo) const {
1158
+ const uint32_t n_tokens = ubatch->n_tokens;
1159
+ GGML_ASSERT(n_tokens == (int64_t) sinfo.size()*sinfo.n_stream());
1160
+
1161
+ GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1162
+ int64_t * data = (int64_t *) dst->data;
1163
+
1164
+ if (!v_trans) {
1165
+ for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
1166
+ const int64_t offs = sinfo.strm[s]*get_size();
1167
+
1168
+ for (uint32_t i = 0; i < sinfo.size(); ++i) {
1169
+ data[s*sinfo.size() + i] = offs + sinfo.idxs[s][i];
1170
+ }
1171
+ }
1172
+ } else {
1173
+ // note: the V cache is transposed when not using flash attention
1174
+ const int64_t kv_size = get_size();
1175
+
1176
+ const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa_max();
1177
+
1178
+ for (uint32_t s = 0; s < sinfo.n_stream(); ++s) {
1179
+ const int64_t offs = sinfo.strm[s]*kv_size*n_embd_v_gqa;
1180
+
1181
+ for (uint32_t i = 0; i < sinfo.size(); ++i) {
1182
+ for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
1183
+ data[s*sinfo.size()*n_embd_v_gqa + i*n_embd_v_gqa + j] = offs + j*kv_size + sinfo.idxs[s][i];
1184
+ }
1185
+ }
1186
+ }
1187
+ }
1188
+ }
1189
+
1190
+ void llama_kv_cache::set_input_k_shift(ggml_tensor * dst) const {
1191
+ GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1192
+
1193
+ int32_t * data = (int32_t *) dst->data;
1194
+
1195
+ for (uint32_t s = 0; s < n_stream; ++s) {
1196
+ const auto & cells = v_cells[s];
1197
+
1198
+ for (uint32_t i = 0; i < cells.size(); ++i) {
1199
+ data[s*cells.size() + i] = cells.is_empty(i) ? 0 : cells.get_shift(i);
1200
+ }
1201
+ }
1202
+ }
1203
+
1204
+ void llama_kv_cache::set_input_kq_mask(ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const {
1205
+ const uint32_t n_tokens = ubatch->n_tokens;
1206
+
1207
+ GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1208
+ float * data = (float *) dst->data;
1209
+
1210
+ const int64_t n_kv = dst->ne[0];
1211
+ const int64_t n_stream = dst->ne[3]; // num streams in the current ubatch
1212
+
1213
+ GGML_ASSERT(n_tokens%n_stream == 0);
1214
+
1215
+ // n_tps == n_tokens_per_stream
1216
+ const int64_t n_tps = n_tokens/n_stream;
1217
+ const int64_t n_tps_pad = GGML_PAD(n_tps, GGML_KQ_MASK_PAD);
1218
+
1219
+ std::fill(data, data + ggml_nelements(dst), -INFINITY);
1220
+
1221
+ // Use only the previous KV cells of the correct sequence for each token of the ubatch.
1222
+ // It's assumed that if a token in the batch has multiple sequences, they are equivalent.
1223
+ // Example with a cache of 10 tokens, 2 tokens populated in cache and 3 tokens in batch:
1224
+ // Causal mask:
1225
+ // xxx-------
1226
+ // xxxx------
1227
+ // xxxxx-----
1228
+ // Non-causal mask:
1229
+ // xxxxx-----
1230
+ // xxxxx-----
1231
+ // xxxxx-----
1232
+ // To visualize the mask, see https://github.com/ggml-org/llama.cpp/pull/12615
1233
+ // TODO: optimize this section
1234
+ for (uint32_t h = 0; h < 1; ++h) {
1235
+ for (uint32_t s = 0; s < n_stream; ++s) {
1236
+ for (uint32_t ii = 0; ii < n_tps; ++ii) {
1237
+ const uint32_t i = s*n_tps + ii;
1238
+
1239
+ const llama_seq_id seq_id = ubatch->seq_id[i][0];
1240
+
1241
+ const auto & cells = v_cells[seq_to_stream[seq_id]];
1242
+
1243
+ const llama_pos p1 = ubatch->pos[i];
1244
+
1245
+ const uint64_t idst = n_kv*(h*n_stream*n_tps_pad + s*n_tps_pad + ii);
1246
+
1247
+ for (uint32_t j = 0; j < n_kv; ++j) {
1248
+ if (cells.is_empty(j)) {
1249
+ continue;
1250
+ }
1251
+
1252
+ // mask the token if not the same sequence
1253
+ if (!cells.seq_has(j, seq_id)) {
1254
+ continue;
1255
+ }
1256
+
1257
+ const llama_pos p0 = cells.pos_get(j);
1258
+
1259
+ // mask future tokens
1260
+ if (causal_attn && p0 > p1) {
1261
+ continue;
1262
+ }
1263
+
1264
+ // apply SWA if any
1265
+ if (is_masked_swa(p0, p1)) {
1266
+ continue;
1267
+ }
1268
+
1269
+ data[idst + j] = hparams.use_alibi ? -std::abs(p0 - p1) : 0.0f;
1270
+ }
1271
+ }
1272
+ }
1273
+ }
1274
+ }
1275
+
1276
+ void llama_kv_cache::set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const {
1277
+ const int64_t n_tokens = ubatch->n_tokens;
1278
+
1279
+ GGML_ASSERT(n_stream == 1 && "TODO: support multiple streams");
1280
+ const auto & cells = v_cells[0];
1281
+
1282
+ GGML_ASSERT(ggml_backend_buffer_is_host(dst->buffer));
1283
+ GGML_ASSERT(!ubatch->equal_seqs()); // TODO: use ubatch->n_seqs instead of failing
1284
+
1285
+ int32_t * data = (int32_t *) dst->data;
1286
+
1287
+ const int32_t n_kv = dst->ne[0];
1288
+
1289
+ for (int h = 0; h < 1; ++h) {
1290
+ for (int i = 0; i < n_tokens; ++i) {
1291
+ for (int j = 0; j < n_kv; ++j) {
1292
+ // the position when the cells is empty is irrelevant - it will be masked out later in the attention
1293
+ const llama_pos p0 = cells.is_empty(j) ? -1 : cells.pos_get(j);
1294
+
1295
+ data[h*(n_kv*n_tokens) + i*n_kv + j] = llama_relative_position_bucket(p0, ubatch->pos[i], hparams.n_rel_attn_bkts, false);
1296
+ }
1297
+ }
1298
+ }
1299
+ }
1300
+
1301
+ size_t llama_kv_cache::total_size() const {
1302
+ size_t size = 0;
1303
+
1304
+ for (const auto & buf : bufs) {
1305
+ size += ggml_backend_buffer_get_size(buf.get());
1306
+ }
1307
+
1308
+ return size;
1309
+ }
1310
+
1311
+ size_t llama_kv_cache::size_k_bytes() const {
1312
+ size_t size_k_bytes = 0;
1313
+
1314
+ for (const auto & layer : layers) {
1315
+ size_k_bytes += ggml_nbytes(layer.k);
1316
+ }
1317
+
1318
+ return size_k_bytes;
1319
+ }
1320
+
1321
+ size_t llama_kv_cache::size_v_bytes() const {
1322
+ size_t size_v_bytes = 0;
1323
+
1324
+ for (const auto & layer : layers) {
1325
+ size_v_bytes += ggml_nbytes(layer.v);
1326
+ }
1327
+
1328
+ return size_v_bytes;
1329
+ }
1330
+
1331
+ ggml_tensor * llama_kv_cache::build_rope_shift(
1332
+ const llama_cparams & cparams,
1333
+ ggml_context * ctx,
1334
+ ggml_tensor * cur,
1335
+ ggml_tensor * shift,
1336
+ ggml_tensor * factors,
1337
+ float freq_base,
1338
+ float freq_scale) const {
1339
+ const auto & n_ctx_orig = cparams.n_ctx_orig_yarn;
1340
+
1341
+ const auto & yarn_ext_factor = cparams.yarn_ext_factor;
1342
+ const auto & yarn_beta_fast = cparams.yarn_beta_fast;
1343
+ const auto & yarn_beta_slow = cparams.yarn_beta_slow;
1344
+
1345
+ const auto & n_rot = hparams.n_rot;
1346
+ const auto & rope_type = hparams.rope_type == LLAMA_ROPE_TYPE_MROPE
1347
+ // @ngxson : this is a workaround
1348
+ // for M-RoPE, we want to rotate the whole vector when doing KV shift
1349
+ // a normal RoPE should work, we just need to use the correct ordering
1350
+ // ref: https://github.com/ggml-org/llama.cpp/pull/13870
1351
+ ? LLAMA_ROPE_TYPE_NEOX
1352
+ : hparams.rope_type;
1353
+
1354
+ // See llm_build_deepseek2() for why attn_factor has to be scaled for YaRN RoPE to work correctly.
1355
+ // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
1356
+ const float yarn_attn_factor = model.arch == LLM_ARCH_DEEPSEEK2
1357
+ ? 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale))
1358
+ : cparams.yarn_attn_factor;
1359
+
1360
+ ggml_tensor * tmp;
1361
+
1362
+ if (ggml_is_quantized(cur->type)) {
1363
+ // dequantize to f32 -> RoPE -> quantize back
1364
+ tmp = ggml_cast(ctx, cur, GGML_TYPE_F32);
1365
+
1366
+ tmp = ggml_rope_ext(ctx, tmp,
1367
+ shift, factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
1368
+ yarn_ext_factor, yarn_attn_factor, yarn_beta_fast, yarn_beta_slow);
1369
+
1370
+ tmp = ggml_cpy(ctx, tmp, cur);
1371
+ } else {
1372
+ // we rotate only the first n_rot dimensions
1373
+ tmp = ggml_rope_ext_inplace(ctx, cur,
1374
+ shift, factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
1375
+ yarn_ext_factor, yarn_attn_factor, yarn_beta_fast, yarn_beta_slow);
1376
+ }
1377
+
1378
+ return tmp;
1379
+ }
1380
+
1381
+ class llm_graph_input_k_shift : public llm_graph_input_i {
1382
+ public:
1383
+ llm_graph_input_k_shift(const llama_kv_cache * kv_self) : kv_self(kv_self) {}
1384
+ virtual ~llm_graph_input_k_shift() = default;
1385
+
1386
+ void set_input(const llama_ubatch * ubatch) override;
1387
+
1388
+ ggml_tensor * k_shift; // I32 [kv_size*n_stream]
1389
+
1390
+ const llama_kv_cache * kv_self;
1391
+ };
1392
+
1393
+ void llm_graph_input_k_shift::set_input(const llama_ubatch * ubatch) {
1394
+ GGML_UNUSED(ubatch);
1395
+
1396
+ if (k_shift) {
1397
+ kv_self->set_input_k_shift(k_shift);
1398
+ }
1399
+ }
1400
+
1401
+ ggml_cgraph * llama_kv_cache::build_graph_shift(llm_graph_result * res, llama_context * lctx) const {
1402
+ auto * ctx = res->get_ctx();
1403
+ auto * gf = res->get_gf();
1404
+
1405
+ const auto & n_embd_head_k = hparams.n_embd_head_k;
1406
+ //const auto & n_embd_head_v = hparams.n_embd_head_v;
1407
+
1408
+ auto inp = std::make_unique<llm_graph_input_k_shift>(this);
1409
+
1410
+ inp->k_shift = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, (int64_t) get_size()*n_stream);
1411
+ ggml_set_input(inp->k_shift);
1412
+
1413
+ const auto & cparams = lctx->get_cparams();
1414
+
1415
+ for (const auto & layer : layers) {
1416
+ const uint32_t il = layer.il;
1417
+
1418
+ const int64_t n_head_kv = hparams.n_head_kv(il);
1419
+ const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
1420
+
1421
+ const float freq_base_l = model.get_rope_freq_base (cparams, il);
1422
+ const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
1423
+
1424
+ ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
1425
+
1426
+ ggml_tensor * k =
1427
+ ggml_view_3d(ctx, layer.k,
1428
+ n_embd_head_k, n_head_kv, get_size()*n_stream,
1429
+ ggml_row_size(layer.k->type, n_embd_head_k),
1430
+ ggml_row_size(layer.k->type, n_embd_k_gqa),
1431
+ 0);
1432
+
1433
+ ggml_tensor * cur = build_rope_shift(cparams, ctx, k, inp->k_shift, rope_factors, freq_base_l, freq_scale_l);
1434
+
1435
+ ggml_build_forward_expand(gf, cur);
1436
+ }
1437
+
1438
+ res->add_input(std::move(inp));
1439
+
1440
+ return gf;
1441
+ }
1442
+
1443
+ bool llama_kv_cache::is_masked_swa(llama_pos p0, llama_pos p1) const {
1444
+ return llama_hparams::is_masked_swa(n_swa, swa_type, p0, p1);
1445
+ }
1446
+
1447
+ void llama_kv_cache::state_write(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) const {
1448
+ GGML_UNUSED(flags);
1449
+
1450
+ io.write(&n_stream, sizeof(n_stream));
1451
+
1452
+ for (uint32_t s = 0; s < n_stream; ++s) {
1453
+ cell_ranges_t cr { s, {} };
1454
+
1455
+ uint32_t cell_count = 0;
1456
+
1457
+ const auto & cells = v_cells[s];
1458
+
1459
+ // Count the number of cells with the specified seq_id
1460
+ // Find all the ranges of cells with this seq id (or all, when -1)
1461
+ uint32_t cell_range_begin = cells.size();
1462
+
1463
+ for (uint32_t i = 0; i < cells.size(); ++i) {
1464
+ if (!cells.is_empty(i) && (seq_id == -1 || cells.seq_has(i, seq_id))) {
1465
+ ++cell_count;
1466
+ if (cell_range_begin == cells.size()) {
1467
+ cell_range_begin = i;
1468
+ }
1469
+ } else {
1470
+ if (cell_range_begin != cells.size()) {
1471
+ cr.data.emplace_back(cell_range_begin, i);
1472
+ cell_range_begin = cells.size();
1473
+ }
1474
+ }
1475
+ }
1476
+
1477
+ if (cell_range_begin != cells.size()) {
1478
+ cr.data.emplace_back(cell_range_begin, cells.size());
1479
+ }
1480
+
1481
+ // DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count
1482
+ uint32_t cell_count_check = 0;
1483
+ for (const auto & range : cr.data) {
1484
+ cell_count_check += range.second - range.first;
1485
+ }
1486
+ GGML_ASSERT(cell_count == cell_count_check);
1487
+
1488
+ io.write(&cell_count, sizeof(cell_count));
1489
+
1490
+ // skip empty streams
1491
+ if (cell_count == 0) {
1492
+ continue;
1493
+ }
1494
+
1495
+ state_write_meta(io, cr, seq_id);
1496
+ state_write_data(io, cr);
1497
+ }
1498
+ }
1499
+
1500
+ void llama_kv_cache::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) {
1501
+ GGML_UNUSED(flags);
1502
+
1503
+ GGML_ASSERT(seq_id == -1 || (seq_id >= 0 && (size_t) seq_id < seq_to_stream.size()));
1504
+
1505
+ uint32_t n_stream_cur;
1506
+ io.read_to(&n_stream_cur, sizeof(n_stream_cur));
1507
+ if (n_stream_cur != n_stream) {
1508
+ throw std::runtime_error("n_stream mismatch");
1509
+ }
1510
+
1511
+ for (uint32_t s = 0; s < n_stream; ++s) {
1512
+ uint32_t cell_count;
1513
+ io.read_to(&cell_count, sizeof(cell_count));
1514
+
1515
+ if (cell_count == 0) {
1516
+ continue;
1517
+ }
1518
+
1519
+ const uint32_t strm = seq_id == -1 ? s : seq_to_stream[seq_id];
1520
+
1521
+ bool res = true;
1522
+ res = res && state_read_meta(io, strm, cell_count, seq_id);
1523
+ res = res && state_read_data(io, strm, cell_count);
1524
+
1525
+ if (!res) {
1526
+ if (seq_id == -1) {
1527
+ clear(true);
1528
+ } else {
1529
+ seq_rm(seq_id, -1, -1);
1530
+ }
1531
+ throw std::runtime_error("failed to restore kv cache");
1532
+ }
1533
+ }
1534
+ }
1535
+
1536
+ void llama_kv_cache::state_write_meta(llama_io_write_i & io, const cell_ranges_t & cr, llama_seq_id seq_id) const {
1537
+ const auto & cells = v_cells[cr.strm];
1538
+
1539
+ for (const auto & range : cr.data) {
1540
+ for (uint32_t i = range.first; i < range.second; ++i) {
1541
+ std::vector<llama_seq_id> seq_ids;
1542
+
1543
+ for (llama_seq_id cur = 0; cur < (int) n_seq_max; ++cur) {
1544
+ if (cur == seq_id || seq_id == -1) {
1545
+ if (cells.seq_has(i, cur)) {
1546
+ seq_ids.push_back(cur);
1547
+ }
1548
+ }
1549
+ }
1550
+
1551
+ const llama_pos pos = cells.pos_get(i);
1552
+ const uint32_t n_seq_id = seq_ids.size();
1553
+
1554
+ io.write(&pos, sizeof(pos));
1555
+ io.write(&n_seq_id, sizeof(n_seq_id));
1556
+
1557
+ for (const auto & seq_id : seq_ids) {
1558
+ io.write(&seq_id, sizeof(seq_id));
1559
+ }
1560
+ }
1561
+ }
1562
+ }
1563
+
1564
+ void llama_kv_cache::state_write_data(llama_io_write_i & io, const cell_ranges_t & cr) const {
1565
+ const auto & cells = v_cells[cr.strm];
1566
+
1567
+ const uint32_t v_trans = this->v_trans ? 1 : 0;
1568
+ const uint32_t n_layer = layers.size();
1569
+
1570
+ io.write(&v_trans, sizeof(v_trans));
1571
+ io.write(&n_layer, sizeof(n_layer));
1572
+
1573
+ std::vector<uint8_t> tmp_buf;
1574
+
1575
+ // Iterate and write all the keys first, each row is a cell
1576
+ // Get whole range at a time
1577
+ for (const auto & layer : layers) {
1578
+ const uint32_t il = layer.il;
1579
+
1580
+ const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
1581
+
1582
+ auto * k = layer.k_stream[cr.strm];
1583
+
1584
+ // Write key type
1585
+ const int32_t k_type_i = (int32_t) k->type;
1586
+ io.write(&k_type_i, sizeof(k_type_i));
1587
+
1588
+ // Write row size of key
1589
+ const uint64_t k_size_row = ggml_row_size(k->type, n_embd_k_gqa);
1590
+ io.write(&k_size_row, sizeof(k_size_row));
1591
+
1592
+ // Read each range of cells of k_size length each into tmp_buf and write out
1593
+ for (const auto & range : cr.data) {
1594
+ const size_t range_size = range.second - range.first;
1595
+ const size_t buf_size = range_size * k_size_row;
1596
+ io.write_tensor(k, range.first * k_size_row, buf_size);
1597
+ }
1598
+ }
1599
+
1600
+ if (!v_trans) {
1601
+ for (const auto & layer : layers) {
1602
+ const uint32_t il = layer.il;
1603
+
1604
+ const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
1605
+
1606
+ auto * v = layer.v_stream[cr.strm];
1607
+
1608
+ // Write value type
1609
+ const int32_t v_type_i = (int32_t) v->type;
1610
+ io.write(&v_type_i, sizeof(v_type_i));
1611
+
1612
+ // Write row size of value
1613
+ const uint64_t v_size_row = ggml_row_size(v->type, n_embd_v_gqa);
1614
+ io.write(&v_size_row, sizeof(v_size_row));
1615
+
1616
+ // Read each range of cells of v_size length each into tmp_buf and write out
1617
+ for (const auto & range : cr.data) {
1618
+ const size_t range_size = range.second - range.first;
1619
+ const size_t buf_size = range_size * v_size_row;
1620
+ io.write_tensor(v, range.first * v_size_row, buf_size);
1621
+ }
1622
+ }
1623
+ } else {
1624
+ // When v is transposed, we also need the element size and get the element ranges from each row
1625
+ const uint32_t kv_size = cells.size();
1626
+
1627
+ for (const auto & layer : layers) {
1628
+ const uint32_t il = layer.il;
1629
+
1630
+ const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
1631
+
1632
+ auto * v = layer.v_stream[cr.strm];
1633
+
1634
+ // Write value type
1635
+ const int32_t v_type_i = (int32_t) v->type;
1636
+ io.write(&v_type_i, sizeof(v_type_i));
1637
+
1638
+ // Write element size
1639
+ const uint32_t v_size_el = ggml_type_size(v->type);
1640
+ io.write(&v_size_el, sizeof(v_size_el));
1641
+
1642
+ // Write GQA embedding size
1643
+ io.write(&n_embd_v_gqa, sizeof(n_embd_v_gqa));
1644
+
1645
+ // For each row, we get the element values of each cell
1646
+ for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
1647
+ // Read each range of cells of v_size_el length each into tmp_buf and write out
1648
+ for (const auto & range : cr.data) {
1649
+ const size_t range_size = range.second - range.first;
1650
+ const size_t src_offset = (range.first + j * kv_size) * v_size_el;
1651
+ const size_t buf_size = range_size * v_size_el;
1652
+ io.write_tensor(v, src_offset, buf_size);
1653
+ }
1654
+ }
1655
+ }
1656
+ }
1657
+ }
1658
+
1659
+ bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, llama_seq_id dest_seq_id) {
1660
+ auto & cells = v_cells[strm];
1661
+ auto & head = v_heads[strm];
1662
+
1663
+ if (dest_seq_id != -1) {
1664
+ // single sequence
1665
+ seq_rm(dest_seq_id, -1, -1);
1666
+
1667
+ llama_batch_allocr balloc(hparams.n_pos_per_embd());
1668
+
1669
+ llama_ubatch ubatch = balloc.ubatch_reserve(cell_count, 1);
1670
+
1671
+ ubatch.seq_id_unq[0] = dest_seq_id;
1672
+
1673
+ for (uint32_t i = 0; i < cell_count; ++i) {
1674
+ llama_pos pos;
1675
+ uint32_t n_seq_id;
1676
+
1677
+ io.read_to(&pos, sizeof(pos));
1678
+ io.read_to(&n_seq_id, sizeof(n_seq_id));
1679
+
1680
+ if (n_seq_id != 1) {
1681
+ LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__);
1682
+ return false;
1683
+ }
1684
+
1685
+ // read the sequence id, but directly discard it - we will use dest_seq_id instead
1686
+ {
1687
+ llama_seq_id seq_id;
1688
+ io.read_to(&seq_id, sizeof(seq_id));
1689
+ }
1690
+
1691
+ ubatch.pos[i] = pos;
1692
+ ubatch.n_seq_id[i] = n_seq_id;
1693
+ ubatch.seq_id[i] = &dest_seq_id;
1694
+ }
1695
+
1696
+ const auto sinfo = find_slot(ubatch, true);
1697
+ if (sinfo.empty()) {
1698
+ LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
1699
+ return false;
1700
+ }
1701
+
1702
+ apply_ubatch(sinfo, ubatch);
1703
+
1704
+ const auto head_cur = sinfo.head();
1705
+
1706
+ // keep the head at the old position because we will read the KV data into it in state_read_data()
1707
+ head = head_cur;
1708
+
1709
+ LLAMA_LOG_DEBUG("%s: head_cur = %d, head = %d, cell_count = %d, dest_seq_id = %d\n", __func__, head_cur, head, cell_count, dest_seq_id);
1710
+
1711
+ // DEBUG CHECK: head_cur should be our first cell, head_cur + cell_count - 1 should be our last cell (verify seq_id and pos values)
1712
+ // Assume that this is one contiguous block of cells
1713
+ GGML_ASSERT(head_cur + cell_count <= cells.size());
1714
+ GGML_ASSERT(cells.pos_get(head_cur) == ubatch.pos[0]);
1715
+ GGML_ASSERT(cells.pos_get(head_cur + cell_count - 1) == ubatch.pos[cell_count - 1]);
1716
+ GGML_ASSERT(cells.seq_has(head_cur, dest_seq_id));
1717
+ GGML_ASSERT(cells.seq_has(head_cur + cell_count - 1, dest_seq_id));
1718
+ } else {
1719
+ // whole KV cache restore
1720
+
1721
+ if (cell_count > cells.size()) {
1722
+ LLAMA_LOG_ERROR("%s: not enough cells in kv cache\n", __func__);
1723
+ return false;
1724
+ }
1725
+
1726
+ clear(true);
1727
+
1728
+ for (uint32_t i = 0; i < cell_count; ++i) {
1729
+ llama_pos pos;
1730
+ uint32_t n_seq_id;
1731
+
1732
+ io.read_to(&pos, sizeof(pos));
1733
+ io.read_to(&n_seq_id, sizeof(n_seq_id));
1734
+
1735
+ cells.pos_set(i, pos);
1736
+
1737
+ for (uint32_t j = 0; j < n_seq_id; ++j) {
1738
+ llama_seq_id seq_id;
1739
+ io.read_to(&seq_id, sizeof(seq_id));
1740
+
1741
+ if (seq_id < 0 || (uint32_t) seq_id >= n_seq_max) {
1742
+ LLAMA_LOG_ERROR("%s: invalid seq_id, %d is out of range [0, %u)\n", __func__, seq_id, n_seq_max);
1743
+ return false;
1744
+ }
1745
+
1746
+ cells.seq_add(i, seq_id);
1747
+ }
1748
+ }
1749
+
1750
+ head = 0;
1751
+ }
1752
+
1753
+ return true;
1754
+ }
1755
+
1756
+ bool llama_kv_cache::state_read_data(llama_io_read_i & io, uint32_t strm, uint32_t cell_count) {
1757
+ auto & cells = v_cells[strm];
1758
+ auto & head = v_heads[strm];
1759
+
1760
+ uint32_t v_trans;
1761
+ uint32_t n_layer;
1762
+
1763
+ io.read_to(&v_trans, sizeof(v_trans));
1764
+ io.read_to(&n_layer, sizeof(n_layer));
1765
+
1766
+ if (n_layer != layers.size()) {
1767
+ LLAMA_LOG_ERROR("%s: mismatched layer count (%u instead of %u)\n", __func__, n_layer, (uint32_t) layers.size());
1768
+ return false;
1769
+ }
1770
+
1771
+ if (cell_count > cells.size()) {
1772
+ LLAMA_LOG_ERROR("%s: not enough cells in kv cache to restore state (%u > %u)\n", __func__, cell_count, cells.size());
1773
+ return false;
1774
+ }
1775
+
1776
+ if (this->v_trans != (bool) v_trans) {
1777
+ LLAMA_LOG_ERROR("%s: incompatible V transposition\n", __func__);
1778
+ return false;
1779
+ }
1780
+
1781
+ // For each layer, read the keys for each cell, one row is one cell, read as one contiguous block
1782
+ for (const auto & layer : layers) {
1783
+ const uint32_t il = layer.il;
1784
+
1785
+ const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il);
1786
+
1787
+ auto * k = layer.k_stream[strm];
1788
+
1789
+ // Read type of key
1790
+ int32_t k_type_i_ref;
1791
+ io.read_to(&k_type_i_ref, sizeof(k_type_i_ref));
1792
+ const int32_t k_type_i = (int32_t) k->type;
1793
+ if (k_type_i != k_type_i_ref) {
1794
+ LLAMA_LOG_ERROR("%s: mismatched key type (%d != %d, layer %d)\n", __func__, k_type_i, k_type_i_ref, il);
1795
+ return false;
1796
+ }
1797
+
1798
+ // Read row size of key
1799
+ uint64_t k_size_row_ref;
1800
+ io.read_to(&k_size_row_ref, sizeof(k_size_row_ref));
1801
+ const size_t k_size_row = ggml_row_size(k->type, n_embd_k_gqa);
1802
+ if (k_size_row != k_size_row_ref) {
1803
+ LLAMA_LOG_ERROR("%s: mismatched key row size (%zu != %zu, layer %d)\n", __func__, k_size_row, (size_t) k_size_row_ref, il);
1804
+ return false;
1805
+ }
1806
+
1807
+ if (cell_count) {
1808
+ // Read and set the keys for the whole cell range
1809
+ ggml_backend_tensor_set(k, io.read(cell_count * k_size_row), head * k_size_row, cell_count * k_size_row);
1810
+ }
1811
+ }
1812
+
1813
+ if (!this->v_trans) {
1814
+ for (const auto & layer : layers) {
1815
+ const uint32_t il = layer.il;
1816
+
1817
+ const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
1818
+
1819
+ auto * v = layer.v_stream[strm];
1820
+
1821
+ // Read type of value
1822
+ int32_t v_type_i_ref;
1823
+ io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
1824
+ const int32_t v_type_i = (int32_t) v->type;
1825
+ if (v_type_i != v_type_i_ref) {
1826
+ LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
1827
+ return false;
1828
+ }
1829
+
1830
+ // Read row size of value
1831
+ uint64_t v_size_row_ref;
1832
+ io.read_to(&v_size_row_ref, sizeof(v_size_row_ref));
1833
+ const size_t v_size_row = ggml_row_size(v->type, n_embd_v_gqa);
1834
+ if (v_size_row != v_size_row_ref) {
1835
+ LLAMA_LOG_ERROR("%s: mismatched value row size (%zu != %zu, layer %d)\n", __func__, v_size_row, (size_t) v_size_row_ref, il);
1836
+ return false;
1837
+ }
1838
+
1839
+ if (cell_count) {
1840
+ // Read and set the values for the whole cell range
1841
+ ggml_backend_tensor_set(v, io.read(cell_count * v_size_row), head * v_size_row, cell_count * v_size_row);
1842
+ }
1843
+ }
1844
+ } else {
1845
+ // For each layer, read the values for each cell (transposed)
1846
+ for (const auto & layer : layers) {
1847
+ const uint32_t il = layer.il;
1848
+
1849
+ const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il);
1850
+
1851
+ auto * v = layer.v_stream[strm];
1852
+
1853
+ // Read type of value
1854
+ int32_t v_type_i_ref;
1855
+ io.read_to(&v_type_i_ref, sizeof(v_type_i_ref));
1856
+ const int32_t v_type_i = (int32_t) v->type;
1857
+ if (v_type_i != v_type_i_ref) {
1858
+ LLAMA_LOG_ERROR("%s: mismatched value type (%d != %d, layer %d)\n", __func__, v_type_i, v_type_i_ref, il);
1859
+ return false;
1860
+ }
1861
+
1862
+ // Read element size of value
1863
+ uint32_t v_size_el_ref;
1864
+ io.read_to(&v_size_el_ref, sizeof(v_size_el_ref));
1865
+ const size_t v_size_el = ggml_type_size(v->type);
1866
+ if (v_size_el != v_size_el_ref) {
1867
+ LLAMA_LOG_ERROR("%s: mismatched value element size (%zu != %zu, layer %d)\n", __func__, v_size_el, (size_t) v_size_el_ref, il);
1868
+ return false;
1869
+ }
1870
+
1871
+ // Read GQA embedding size
1872
+ uint32_t n_embd_v_gqa_ref;
1873
+ io.read_to(&n_embd_v_gqa_ref, sizeof(n_embd_v_gqa_ref));
1874
+ if (n_embd_v_gqa != n_embd_v_gqa_ref) {
1875
+ LLAMA_LOG_ERROR("%s: mismatched GQA embedding size (%u != %u, layer %d)\n", __func__, n_embd_v_gqa, n_embd_v_gqa_ref, il);
1876
+ return false;
1877
+ }
1878
+
1879
+ if (cell_count) {
1880
+ // For each row in the transposed matrix, read the values for the whole cell range
1881
+ for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
1882
+ const size_t dst_offset = (head + j * cells.size()) * v_size_el;
1883
+ ggml_backend_tensor_set(v, io.read(cell_count * v_size_el), dst_offset, cell_count * v_size_el);
1884
+ }
1885
+ }
1886
+ }
1887
+ }
1888
+
1889
+ return true;
1890
+ }
1891
+
1892
+ //
1893
+ // llama_kv_cache_context
1894
+ //
1895
+
1896
+ llama_kv_cache_context::llama_kv_cache_context(llama_memory_status status) : status(status) {}
1897
+
1898
+ llama_kv_cache_context::llama_kv_cache_context(
1899
+ llama_kv_cache * kv) : status(LLAMA_MEMORY_STATUS_SUCCESS), kv(kv) {
1900
+ n_kv = kv->get_size();
1901
+
1902
+ const uint32_t n_stream = kv->get_n_stream();
1903
+
1904
+ // create a dummy slot info - the actual data is irrelevant. we just need to build the graph
1905
+ sinfos.resize(1);
1906
+ sinfos[0].s0 = 0;
1907
+ sinfos[0].s1 = n_stream - 1;
1908
+ sinfos[0].idxs.resize(n_stream);
1909
+ for (uint32_t s = 0; s < n_stream; ++s) {
1910
+ sinfos[0].strm.push_back(s);
1911
+ sinfos[0].idxs[s].resize(1, 0);
1912
+ }
1913
+ }
1914
+
1915
+ llama_kv_cache_context::llama_kv_cache_context(
1916
+ llama_kv_cache * kv,
1917
+ llama_context * lctx,
1918
+ bool do_shift,
1919
+ stream_copy_info sc_info) : status(LLAMA_MEMORY_STATUS_SUCCESS), kv(kv), lctx(lctx), do_shift(do_shift), sc_info(std::move(sc_info)) {
1920
+ if (!do_shift && this->sc_info.empty()) {
1921
+ status = LLAMA_MEMORY_STATUS_NO_UPDATE;
1922
+ }
1923
+ }
1924
+
1925
+ llama_kv_cache_context::llama_kv_cache_context(
1926
+ llama_kv_cache * kv,
1927
+ llama_kv_cache::slot_info_vec_t sinfos,
1928
+ std::vector<llama_ubatch> ubatches) : status(LLAMA_MEMORY_STATUS_SUCCESS), kv(kv), sinfos(std::move(sinfos)), ubatches(std::move(ubatches)) {
1929
+ }
1930
+
1931
+ llama_kv_cache_context::~llama_kv_cache_context() = default;
1932
+
1933
+ bool llama_kv_cache_context::next() {
1934
+ assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
1935
+
1936
+ if (++i_cur >= ubatches.size()) {
1937
+ return false;
1938
+ }
1939
+
1940
+ return true;
1941
+ }
1942
+
1943
+ bool llama_kv_cache_context::apply() {
1944
+ assert(!llama_memory_status_is_fail(status));
1945
+
1946
+ // no ubatches -> this is a KV cache update
1947
+ if (ubatches.empty()) {
1948
+ kv->update(lctx, do_shift, sc_info);
1949
+
1950
+ return true;
1951
+ }
1952
+
1953
+ kv->apply_ubatch(sinfos[i_cur], ubatches[i_cur]);
1954
+ n_kv = kv->get_n_kv(sinfos[i_cur]);
1955
+
1956
+ return true;
1957
+ }
1958
+
1959
+ llama_memory_status llama_kv_cache_context::get_status() const {
1960
+ return status;
1961
+ }
1962
+
1963
+ const llama_ubatch & llama_kv_cache_context::get_ubatch() const {
1964
+ assert(status == LLAMA_MEMORY_STATUS_SUCCESS);
1965
+
1966
+ return ubatches[i_cur];
1967
+ }
1968
+
1969
+ uint32_t llama_kv_cache_context::get_n_kv() const {
1970
+ return n_kv;
1971
+ }
1972
+
1973
+ ggml_tensor * llama_kv_cache_context::get_k(ggml_context * ctx, int32_t il) const {
1974
+ return kv->get_k(ctx, il, n_kv, sinfos[i_cur]);
1975
+ }
1976
+
1977
+ ggml_tensor * llama_kv_cache_context::get_v(ggml_context * ctx, int32_t il) const {
1978
+ return kv->get_v(ctx, il, n_kv, sinfos[i_cur]);
1979
+ }
1980
+
1981
+ ggml_tensor * llama_kv_cache_context::cpy_k(ggml_context * ctx, ggml_tensor * k_cur, ggml_tensor * k_idxs, int32_t il) const {
1982
+ return kv->cpy_k(ctx, k_cur, k_idxs, il, sinfos[i_cur]);
1983
+ }
1984
+
1985
+ ggml_tensor * llama_kv_cache_context::cpy_v(ggml_context * ctx, ggml_tensor * v_cur, ggml_tensor * v_idxs, int32_t il) const {
1986
+ return kv->cpy_v(ctx, v_cur, v_idxs, il, sinfos[i_cur]);
1987
+ }
1988
+
1989
+ ggml_tensor * llama_kv_cache_context::build_input_k_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
1990
+ return kv->build_input_k_idxs(ctx, ubatch);
1991
+ }
1992
+
1993
+ ggml_tensor * llama_kv_cache_context::build_input_v_idxs(ggml_context * ctx, const llama_ubatch & ubatch) const {
1994
+ return kv->build_input_v_idxs(ctx, ubatch);
1995
+ }
1996
+
1997
+ void llama_kv_cache_context::set_input_k_shift(ggml_tensor * dst) const {
1998
+ kv->set_input_k_shift(dst);
1999
+ }
2000
+
2001
+ void llama_kv_cache_context::set_input_k_idxs(ggml_tensor * dst, const llama_ubatch * ubatch) const {
2002
+ kv->set_input_k_idxs(dst, ubatch, sinfos[i_cur]);
2003
+ }
2004
+
2005
+ void llama_kv_cache_context::set_input_v_idxs(ggml_tensor * dst, const llama_ubatch * ubatch) const {
2006
+ kv->set_input_v_idxs(dst, ubatch, sinfos[i_cur]);
2007
+ }
2008
+
2009
+ void llama_kv_cache_context::set_input_kq_mask(ggml_tensor * dst, const llama_ubatch * ubatch, bool causal_attn) const {
2010
+ kv->set_input_kq_mask(dst, ubatch, causal_attn);
2011
+ }
2012
+
2013
+ void llama_kv_cache_context::set_input_pos_bucket(ggml_tensor * dst, const llama_ubatch * ubatch) const {
2014
+ kv->set_input_pos_bucket(dst, ubatch);
2015
+ }
2016
+
2017
+ uint32_t llama_kv_cache::get_padding(const llama_cparams & cparams) {
2018
+ // the FA kernels require padding to avoid extra runtime boundary checks
2019
+ return cparams.flash_attn ? 256u : 32u;
2020
+ }