cohere-transcribe 0.1.0

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 (480) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +21 -0
  3. data/LICENSE.txt +201 -0
  4. data/NOTICE +5 -0
  5. data/README.md +265 -0
  6. data/THIRD_PARTY_NOTICES.md +67 -0
  7. data/exe/cohere-transcribe +8 -0
  8. data/exe/cohere-transcribe-doctor +8 -0
  9. data/ext/cohere_transcribe_native/CMakeLists.txt +262 -0
  10. data/ext/cohere_transcribe_native/README.md +26 -0
  11. data/ext/cohere_transcribe_native/audio_abi.cpp +1416 -0
  12. data/ext/cohere_transcribe_native/audio_exports.macos +5 -0
  13. data/ext/cohere_transcribe_native/audio_exports.map +10 -0
  14. data/ext/cohere_transcribe_native/cohere_abi.cpp +1209 -0
  15. data/ext/cohere_transcribe_native/exports.macos +40 -0
  16. data/ext/cohere_transcribe_native/exports.map +45 -0
  17. data/ext/cohere_transcribe_native/extconf.rb +89 -0
  18. data/ext/cohere_transcribe_native/test/abi_smoke.rb +232 -0
  19. data/ext/cohere_transcribe_native/test/audio_matrix_smoke.cpp +121 -0
  20. data/ext/cohere_transcribe_native/test/audio_reliability_smoke.cpp +198 -0
  21. data/ext/cohere_transcribe_native/test/native_abi_reliability_smoke.cpp +186 -0
  22. data/ext/cohere_transcribe_native/test/native_batch_projection_probe.rb +81 -0
  23. data/ext/cohere_transcribe_native/test/native_cancellation_smoke.cpp +194 -0
  24. data/lib/cohere/transcribe/alignment/ATTRIBUTION.md +23 -0
  25. data/lib/cohere/transcribe/alignment/LICENSE.ctc-forced-aligner +407 -0
  26. data/lib/cohere/transcribe/alignment/LICENSE.torchaudio +25 -0
  27. data/lib/cohere/transcribe/alignment/LICENSE.uroman +26 -0
  28. data/lib/cohere/transcribe/alignment/aligner.rb +476 -0
  29. data/lib/cohere/transcribe/alignment/ctc.rb +224 -0
  30. data/lib/cohere/transcribe/alignment/text.rb +237 -0
  31. data/lib/cohere/transcribe/alignment/uroman_data.rb +4950 -0
  32. data/lib/cohere/transcribe/api.rb +173 -0
  33. data/lib/cohere/transcribe/asr/batching.rb +472 -0
  34. data/lib/cohere/transcribe/asr/failure_policy.rb +112 -0
  35. data/lib/cohere/transcribe/asr/native.rb +676 -0
  36. data/lib/cohere/transcribe/audio/ATTRIBUTION.md +8 -0
  37. data/lib/cohere/transcribe/audio/LICENSE.auditok +21 -0
  38. data/lib/cohere/transcribe/audio/decoder.rb +315 -0
  39. data/lib/cohere/transcribe/audio/ffmpeg_native.rb +248 -0
  40. data/lib/cohere/transcribe/audio/segmentation.rb +260 -0
  41. data/lib/cohere/transcribe/cli.rb +727 -0
  42. data/lib/cohere/transcribe/configuration.rb +282 -0
  43. data/lib/cohere/transcribe/constants.rb +14 -0
  44. data/lib/cohere/transcribe/dense_converter.rb +548 -0
  45. data/lib/cohere/transcribe/doctor.rb +576 -0
  46. data/lib/cohere/transcribe/errors.rb +57 -0
  47. data/lib/cohere/transcribe/gguf_writer.rb +268 -0
  48. data/lib/cohere/transcribe/hub.rb +436 -0
  49. data/lib/cohere/transcribe/input.rb +110 -0
  50. data/lib/cohere/transcribe/licenses/crispasr.txt +21 -0
  51. data/lib/cohere/transcribe/loader.rb +128 -0
  52. data/lib/cohere/transcribe/model_identity.rb +440 -0
  53. data/lib/cohere/transcribe/output/publication.rb +1118 -0
  54. data/lib/cohere/transcribe/output/rendering.rb +105 -0
  55. data/lib/cohere/transcribe/output/timing.rb +86 -0
  56. data/lib/cohere/transcribe/python_text.rb +70 -0
  57. data/lib/cohere/transcribe/pytorch_checkpoint.rb +1180 -0
  58. data/lib/cohere/transcribe/runtime/engine.rb +1676 -0
  59. data/lib/cohere/transcribe/runtime/model_provider.rb +390 -0
  60. data/lib/cohere/transcribe/runtime/precision.rb +57 -0
  61. data/lib/cohere/transcribe/runtime/preparation.rb +215 -0
  62. data/lib/cohere/transcribe/runtime/resources.rb +165 -0
  63. data/lib/cohere/transcribe/runtime/word_pipeline.rb +364 -0
  64. data/lib/cohere/transcribe/safetensors.rb +579 -0
  65. data/lib/cohere/transcribe/state/checkpoint.rb +224 -0
  66. data/lib/cohere/transcribe/state/contracts.rb +141 -0
  67. data/lib/cohere/transcribe/state/io.rb +727 -0
  68. data/lib/cohere/transcribe/state/locking.rb +211 -0
  69. data/lib/cohere/transcribe/state/manifest.rb +155 -0
  70. data/lib/cohere/transcribe/state.rb +7 -0
  71. data/lib/cohere/transcribe/types.rb +535 -0
  72. data/lib/cohere/transcribe/vad/ATTRIBUTION.md +14 -0
  73. data/lib/cohere/transcribe/vad/LICENSE.faster-whisper +21 -0
  74. data/lib/cohere/transcribe/vad/LICENSE.silero-vad +21 -0
  75. data/lib/cohere/transcribe/vad/silero.rb +344 -0
  76. data/lib/cohere/transcribe/vad/silero_vad_v6.onnx +0 -0
  77. data/lib/cohere/transcribe/vad/timestamps.rb +219 -0
  78. data/lib/cohere/transcribe/version.rb +7 -0
  79. data/lib/cohere/transcribe.rb +26 -0
  80. data/sig/cohere/transcribe.rbs +250 -0
  81. data/vendor/crispasr/AUTHORS +510 -0
  82. data/vendor/crispasr/LICENSE +21 -0
  83. data/vendor/crispasr/UPSTREAM.md +9 -0
  84. data/vendor/crispasr/VERSION +1 -0
  85. data/vendor/crispasr/ggml/AUTHORS +335 -0
  86. data/vendor/crispasr/ggml/CMakeLists.txt +512 -0
  87. data/vendor/crispasr/ggml/LICENSE +21 -0
  88. data/vendor/crispasr/ggml/README.md +49 -0
  89. data/vendor/crispasr/ggml/cmake/FindNCCL.cmake +36 -0
  90. data/vendor/crispasr/ggml/cmake/GitVars.cmake +22 -0
  91. data/vendor/crispasr/ggml/cmake/common.cmake +50 -0
  92. data/vendor/crispasr/ggml/cmake/ggml-config.cmake.in +191 -0
  93. data/vendor/crispasr/ggml/ggml.pc.in +10 -0
  94. data/vendor/crispasr/ggml/include/ggml-alloc.h +85 -0
  95. data/vendor/crispasr/ggml/include/ggml-backend.h +431 -0
  96. data/vendor/crispasr/ggml/include/ggml-blas.h +25 -0
  97. data/vendor/crispasr/ggml/include/ggml-cann.h +123 -0
  98. data/vendor/crispasr/ggml/include/ggml-cpp.h +39 -0
  99. data/vendor/crispasr/ggml/include/ggml-cpu.h +151 -0
  100. data/vendor/crispasr/ggml/include/ggml-cuda.h +50 -0
  101. data/vendor/crispasr/ggml/include/ggml-hexagon.h +19 -0
  102. data/vendor/crispasr/ggml/include/ggml-metal.h +61 -0
  103. data/vendor/crispasr/ggml/include/ggml-opencl.h +26 -0
  104. data/vendor/crispasr/ggml/include/ggml-openvino.h +37 -0
  105. data/vendor/crispasr/ggml/include/ggml-opt.h +256 -0
  106. data/vendor/crispasr/ggml/include/ggml-rpc.h +35 -0
  107. data/vendor/crispasr/ggml/include/ggml-sycl.h +49 -0
  108. data/vendor/crispasr/ggml/include/ggml-virtgpu.h +14 -0
  109. data/vendor/crispasr/ggml/include/ggml-vulkan.h +29 -0
  110. data/vendor/crispasr/ggml/include/ggml-webgpu.h +19 -0
  111. data/vendor/crispasr/ggml/include/ggml-zdnn.h +17 -0
  112. data/vendor/crispasr/ggml/include/ggml-zendnn.h +22 -0
  113. data/vendor/crispasr/ggml/include/ggml.h +2887 -0
  114. data/vendor/crispasr/ggml/include/gguf.h +204 -0
  115. data/vendor/crispasr/ggml/src/CMakeLists.txt +493 -0
  116. data/vendor/crispasr/ggml/src/ggml-alloc.c +1323 -0
  117. data/vendor/crispasr/ggml/src/ggml-backend-dl.cpp +48 -0
  118. data/vendor/crispasr/ggml/src/ggml-backend-dl.h +44 -0
  119. data/vendor/crispasr/ggml/src/ggml-backend-impl.h +275 -0
  120. data/vendor/crispasr/ggml/src/ggml-backend-meta.cpp +2145 -0
  121. data/vendor/crispasr/ggml/src/ggml-backend-reg.cpp +586 -0
  122. data/vendor/crispasr/ggml/src/ggml-backend.cpp +2437 -0
  123. data/vendor/crispasr/ggml/src/ggml-common.h +1900 -0
  124. data/vendor/crispasr/ggml/src/ggml-cpu/CMakeLists.txt +718 -0
  125. data/vendor/crispasr/ggml/src/ggml-cpu/amx/amx.cpp +249 -0
  126. data/vendor/crispasr/ggml/src/ggml-cpu/amx/amx.h +8 -0
  127. data/vendor/crispasr/ggml/src/ggml-cpu/amx/common.h +115 -0
  128. data/vendor/crispasr/ggml/src/ggml-cpu/amx/mmq.cpp +2512 -0
  129. data/vendor/crispasr/ggml/src/ggml-cpu/amx/mmq.h +10 -0
  130. data/vendor/crispasr/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +98 -0
  131. data/vendor/crispasr/ggml/src/ggml-cpu/arch/arm/quants.c +4244 -0
  132. data/vendor/crispasr/ggml/src/ggml-cpu/arch/arm/repack.cpp +5156 -0
  133. data/vendor/crispasr/ggml/src/ggml-cpu/arch/loongarch/quants.c +2158 -0
  134. data/vendor/crispasr/ggml/src/ggml-cpu/arch/powerpc/cpu-feats.cpp +82 -0
  135. data/vendor/crispasr/ggml/src/ggml-cpu/arch/powerpc/quants.c +2304 -0
  136. data/vendor/crispasr/ggml/src/ggml-cpu/arch/riscv/cpu-feats.cpp +38 -0
  137. data/vendor/crispasr/ggml/src/ggml-cpu/arch/riscv/quants.c +4455 -0
  138. data/vendor/crispasr/ggml/src/ggml-cpu/arch/riscv/repack.cpp +1703 -0
  139. data/vendor/crispasr/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
  140. data/vendor/crispasr/ggml/src/ggml-cpu/arch/s390/quants.c +1465 -0
  141. data/vendor/crispasr/ggml/src/ggml-cpu/arch/wasm/quants.c +1220 -0
  142. data/vendor/crispasr/ggml/src/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
  143. data/vendor/crispasr/ggml/src/ggml-cpu/arch/x86/quants.c +3970 -0
  144. data/vendor/crispasr/ggml/src/ggml-cpu/arch/x86/repack.cpp +6407 -0
  145. data/vendor/crispasr/ggml/src/ggml-cpu/arch-fallback.h +349 -0
  146. data/vendor/crispasr/ggml/src/ggml-cpu/binary-ops.cpp +154 -0
  147. data/vendor/crispasr/ggml/src/ggml-cpu/binary-ops.h +16 -0
  148. data/vendor/crispasr/ggml/src/ggml-cpu/cmake/FindSIMD.cmake +100 -0
  149. data/vendor/crispasr/ggml/src/ggml-cpu/common.h +95 -0
  150. data/vendor/crispasr/ggml/src/ggml-cpu/ggml-cpu-impl.h +539 -0
  151. data/vendor/crispasr/ggml/src/ggml-cpu/ggml-cpu.c +3791 -0
  152. data/vendor/crispasr/ggml/src/ggml-cpu/ggml-cpu.cpp +703 -0
  153. data/vendor/crispasr/ggml/src/ggml-cpu/hbm.cpp +55 -0
  154. data/vendor/crispasr/ggml/src/ggml-cpu/hbm.h +8 -0
  155. data/vendor/crispasr/ggml/src/ggml-cpu/kleidiai/kernels.cpp +939 -0
  156. data/vendor/crispasr/ggml/src/ggml-cpu/kleidiai/kernels.h +90 -0
  157. data/vendor/crispasr/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +1513 -0
  158. data/vendor/crispasr/ggml/src/ggml-cpu/kleidiai/kleidiai.h +17 -0
  159. data/vendor/crispasr/ggml/src/ggml-cpu/llamafile/sgemm.cpp +4051 -0
  160. data/vendor/crispasr/ggml/src/ggml-cpu/llamafile/sgemm.h +25 -0
  161. data/vendor/crispasr/ggml/src/ggml-cpu/ops.cpp +11662 -0
  162. data/vendor/crispasr/ggml/src/ggml-cpu/ops.h +121 -0
  163. data/vendor/crispasr/ggml/src/ggml-cpu/quants.c +1288 -0
  164. data/vendor/crispasr/ggml/src/ggml-cpu/quants.h +103 -0
  165. data/vendor/crispasr/ggml/src/ggml-cpu/repack.cpp +4836 -0
  166. data/vendor/crispasr/ggml/src/ggml-cpu/repack.h +245 -0
  167. data/vendor/crispasr/ggml/src/ggml-cpu/simd-gemm.h +226 -0
  168. data/vendor/crispasr/ggml/src/ggml-cpu/simd-mappings.h +1329 -0
  169. data/vendor/crispasr/ggml/src/ggml-cpu/spacemit/ime.cpp +1025 -0
  170. data/vendor/crispasr/ggml/src/ggml-cpu/spacemit/ime.h +13 -0
  171. data/vendor/crispasr/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +3196 -0
  172. data/vendor/crispasr/ggml/src/ggml-cpu/spacemit/ime_kernels.h +26 -0
  173. data/vendor/crispasr/ggml/src/ggml-cpu/traits.cpp +36 -0
  174. data/vendor/crispasr/ggml/src/ggml-cpu/traits.h +38 -0
  175. data/vendor/crispasr/ggml/src/ggml-cpu/unary-ops.cpp +336 -0
  176. data/vendor/crispasr/ggml/src/ggml-cpu/unary-ops.h +35 -0
  177. data/vendor/crispasr/ggml/src/ggml-cpu/vec.cpp +681 -0
  178. data/vendor/crispasr/ggml/src/ggml-cpu/vec.h +1606 -0
  179. data/vendor/crispasr/ggml/src/ggml-cuda/CMakeLists.txt +272 -0
  180. data/vendor/crispasr/ggml/src/ggml-cuda/acc.cu +61 -0
  181. data/vendor/crispasr/ggml/src/ggml-cuda/acc.cuh +5 -0
  182. data/vendor/crispasr/ggml/src/ggml-cuda/add-id.cu +58 -0
  183. data/vendor/crispasr/ggml/src/ggml-cuda/add-id.cuh +3 -0
  184. data/vendor/crispasr/ggml/src/ggml-cuda/arange.cu +34 -0
  185. data/vendor/crispasr/ggml/src/ggml-cuda/arange.cuh +5 -0
  186. data/vendor/crispasr/ggml/src/ggml-cuda/argmax.cu +91 -0
  187. data/vendor/crispasr/ggml/src/ggml-cuda/argmax.cuh +3 -0
  188. data/vendor/crispasr/ggml/src/ggml-cuda/argsort.cu +265 -0
  189. data/vendor/crispasr/ggml/src/ggml-cuda/argsort.cuh +19 -0
  190. data/vendor/crispasr/ggml/src/ggml-cuda/binbcast.cu +534 -0
  191. data/vendor/crispasr/ggml/src/ggml-cuda/binbcast.cuh +12 -0
  192. data/vendor/crispasr/ggml/src/ggml-cuda/clamp.cu +45 -0
  193. data/vendor/crispasr/ggml/src/ggml-cuda/clamp.cuh +5 -0
  194. data/vendor/crispasr/ggml/src/ggml-cuda/col2im-1d.cu +81 -0
  195. data/vendor/crispasr/ggml/src/ggml-cuda/col2im-1d.cuh +3 -0
  196. data/vendor/crispasr/ggml/src/ggml-cuda/common.cuh +1489 -0
  197. data/vendor/crispasr/ggml/src/ggml-cuda/concat.cu +204 -0
  198. data/vendor/crispasr/ggml/src/ggml-cuda/concat.cuh +5 -0
  199. data/vendor/crispasr/ggml/src/ggml-cuda/conv-transpose-1d.cu +97 -0
  200. data/vendor/crispasr/ggml/src/ggml-cuda/conv-transpose-1d.cuh +5 -0
  201. data/vendor/crispasr/ggml/src/ggml-cuda/conv2d-dw.cu +161 -0
  202. data/vendor/crispasr/ggml/src/ggml-cuda/conv2d-dw.cuh +5 -0
  203. data/vendor/crispasr/ggml/src/ggml-cuda/conv2d-transpose.cu +115 -0
  204. data/vendor/crispasr/ggml/src/ggml-cuda/conv2d-transpose.cuh +5 -0
  205. data/vendor/crispasr/ggml/src/ggml-cuda/conv2d.cu +166 -0
  206. data/vendor/crispasr/ggml/src/ggml-cuda/conv2d.cuh +5 -0
  207. data/vendor/crispasr/ggml/src/ggml-cuda/convert.cu +892 -0
  208. data/vendor/crispasr/ggml/src/ggml-cuda/convert.cuh +66 -0
  209. data/vendor/crispasr/ggml/src/ggml-cuda/count-equal.cu +64 -0
  210. data/vendor/crispasr/ggml/src/ggml-cuda/count-equal.cuh +5 -0
  211. data/vendor/crispasr/ggml/src/ggml-cuda/cp-async.cuh +57 -0
  212. data/vendor/crispasr/ggml/src/ggml-cuda/cpy-utils.cuh +217 -0
  213. data/vendor/crispasr/ggml/src/ggml-cuda/cpy.cu +581 -0
  214. data/vendor/crispasr/ggml/src/ggml-cuda/cpy.cuh +7 -0
  215. data/vendor/crispasr/ggml/src/ggml-cuda/cross-entropy-loss.cu +177 -0
  216. data/vendor/crispasr/ggml/src/ggml-cuda/cross-entropy-loss.cuh +7 -0
  217. data/vendor/crispasr/ggml/src/ggml-cuda/cumsum.cu +307 -0
  218. data/vendor/crispasr/ggml/src/ggml-cuda/cumsum.cuh +5 -0
  219. data/vendor/crispasr/ggml/src/ggml-cuda/dequantize.cuh +99 -0
  220. data/vendor/crispasr/ggml/src/ggml-cuda/diag.cu +77 -0
  221. data/vendor/crispasr/ggml/src/ggml-cuda/diag.cuh +5 -0
  222. data/vendor/crispasr/ggml/src/ggml-cuda/diagmask.cu +40 -0
  223. data/vendor/crispasr/ggml/src/ggml-cuda/diagmask.cuh +5 -0
  224. data/vendor/crispasr/ggml/src/ggml-cuda/fattn-common.cuh +1212 -0
  225. data/vendor/crispasr/ggml/src/ggml-cuda/fattn-mma-f16.cuh +1860 -0
  226. data/vendor/crispasr/ggml/src/ggml-cuda/fattn-tile.cu +57 -0
  227. data/vendor/crispasr/ggml/src/ggml-cuda/fattn-tile.cuh +1309 -0
  228. data/vendor/crispasr/ggml/src/ggml-cuda/fattn-vec.cuh +600 -0
  229. data/vendor/crispasr/ggml/src/ggml-cuda/fattn-wmma-f16.cu +696 -0
  230. data/vendor/crispasr/ggml/src/ggml-cuda/fattn-wmma-f16.cuh +51 -0
  231. data/vendor/crispasr/ggml/src/ggml-cuda/fattn.cu +620 -0
  232. data/vendor/crispasr/ggml/src/ggml-cuda/fattn.cuh +5 -0
  233. data/vendor/crispasr/ggml/src/ggml-cuda/fill.cu +37 -0
  234. data/vendor/crispasr/ggml/src/ggml-cuda/fill.cuh +3 -0
  235. data/vendor/crispasr/ggml/src/ggml-cuda/gated_delta_net.cu +273 -0
  236. data/vendor/crispasr/ggml/src/ggml-cuda/gated_delta_net.cuh +4 -0
  237. data/vendor/crispasr/ggml/src/ggml-cuda/getrows.cu +332 -0
  238. data/vendor/crispasr/ggml/src/ggml-cuda/getrows.cuh +15 -0
  239. data/vendor/crispasr/ggml/src/ggml-cuda/ggml-cuda.cu +5580 -0
  240. data/vendor/crispasr/ggml/src/ggml-cuda/gla.cu +93 -0
  241. data/vendor/crispasr/ggml/src/ggml-cuda/gla.cuh +3 -0
  242. data/vendor/crispasr/ggml/src/ggml-cuda/im2col.cu +274 -0
  243. data/vendor/crispasr/ggml/src/ggml-cuda/im2col.cuh +6 -0
  244. data/vendor/crispasr/ggml/src/ggml-cuda/mean.cu +75 -0
  245. data/vendor/crispasr/ggml/src/ggml-cuda/mean.cuh +3 -0
  246. data/vendor/crispasr/ggml/src/ggml-cuda/mma.cuh +1333 -0
  247. data/vendor/crispasr/ggml/src/ggml-cuda/mmf.cu +191 -0
  248. data/vendor/crispasr/ggml/src/ggml-cuda/mmf.cuh +908 -0
  249. data/vendor/crispasr/ggml/src/ggml-cuda/mmid.cu +164 -0
  250. data/vendor/crispasr/ggml/src/ggml-cuda/mmid.cuh +5 -0
  251. data/vendor/crispasr/ggml/src/ggml-cuda/mmq.cu +372 -0
  252. data/vendor/crispasr/ggml/src/ggml-cuda/mmq.cuh +4175 -0
  253. data/vendor/crispasr/ggml/src/ggml-cuda/mmvf.cu +862 -0
  254. data/vendor/crispasr/ggml/src/ggml-cuda/mmvf.cuh +14 -0
  255. data/vendor/crispasr/ggml/src/ggml-cuda/mmvq.cu +1161 -0
  256. data/vendor/crispasr/ggml/src/ggml-cuda/mmvq.cuh +16 -0
  257. data/vendor/crispasr/ggml/src/ggml-cuda/norm.cu +756 -0
  258. data/vendor/crispasr/ggml/src/ggml-cuda/norm.cuh +20 -0
  259. data/vendor/crispasr/ggml/src/ggml-cuda/opt-step-adamw.cu +78 -0
  260. data/vendor/crispasr/ggml/src/ggml-cuda/opt-step-adamw.cuh +5 -0
  261. data/vendor/crispasr/ggml/src/ggml-cuda/opt-step-sgd.cu +49 -0
  262. data/vendor/crispasr/ggml/src/ggml-cuda/opt-step-sgd.cuh +5 -0
  263. data/vendor/crispasr/ggml/src/ggml-cuda/out-prod.cu +68 -0
  264. data/vendor/crispasr/ggml/src/ggml-cuda/out-prod.cuh +3 -0
  265. data/vendor/crispasr/ggml/src/ggml-cuda/pad.cu +106 -0
  266. data/vendor/crispasr/ggml/src/ggml-cuda/pad.cuh +5 -0
  267. data/vendor/crispasr/ggml/src/ggml-cuda/pad_reflect_1d.cu +91 -0
  268. data/vendor/crispasr/ggml/src/ggml-cuda/pad_reflect_1d.cuh +5 -0
  269. data/vendor/crispasr/ggml/src/ggml-cuda/pool2d.cu +94 -0
  270. data/vendor/crispasr/ggml/src/ggml-cuda/pool2d.cuh +5 -0
  271. data/vendor/crispasr/ggml/src/ggml-cuda/quantize.cu +443 -0
  272. data/vendor/crispasr/ggml/src/ggml-cuda/quantize.cuh +41 -0
  273. data/vendor/crispasr/ggml/src/ggml-cuda/reduce_rows.cuh +39 -0
  274. data/vendor/crispasr/ggml/src/ggml-cuda/roll.cu +67 -0
  275. data/vendor/crispasr/ggml/src/ggml-cuda/roll.cuh +5 -0
  276. data/vendor/crispasr/ggml/src/ggml-cuda/rope.cu +665 -0
  277. data/vendor/crispasr/ggml/src/ggml-cuda/rope.cuh +9 -0
  278. data/vendor/crispasr/ggml/src/ggml-cuda/scale.cu +34 -0
  279. data/vendor/crispasr/ggml/src/ggml-cuda/scale.cuh +5 -0
  280. data/vendor/crispasr/ggml/src/ggml-cuda/set-rows.cu +330 -0
  281. data/vendor/crispasr/ggml/src/ggml-cuda/set-rows.cuh +7 -0
  282. data/vendor/crispasr/ggml/src/ggml-cuda/set.cu +39 -0
  283. data/vendor/crispasr/ggml/src/ggml-cuda/set.cuh +7 -0
  284. data/vendor/crispasr/ggml/src/ggml-cuda/softcap.cu +34 -0
  285. data/vendor/crispasr/ggml/src/ggml-cuda/softcap.cuh +5 -0
  286. data/vendor/crispasr/ggml/src/ggml-cuda/softmax.cu +472 -0
  287. data/vendor/crispasr/ggml/src/ggml-cuda/softmax.cuh +7 -0
  288. data/vendor/crispasr/ggml/src/ggml-cuda/solve_tri.cu +275 -0
  289. data/vendor/crispasr/ggml/src/ggml-cuda/solve_tri.cuh +3 -0
  290. data/vendor/crispasr/ggml/src/ggml-cuda/ssm-conv.cu +197 -0
  291. data/vendor/crispasr/ggml/src/ggml-cuda/ssm-conv.cuh +3 -0
  292. data/vendor/crispasr/ggml/src/ggml-cuda/ssm-scan.cu +342 -0
  293. data/vendor/crispasr/ggml/src/ggml-cuda/ssm-scan.cuh +3 -0
  294. data/vendor/crispasr/ggml/src/ggml-cuda/sum.cu +41 -0
  295. data/vendor/crispasr/ggml/src/ggml-cuda/sum.cuh +5 -0
  296. data/vendor/crispasr/ggml/src/ggml-cuda/sumrows.cu +43 -0
  297. data/vendor/crispasr/ggml/src/ggml-cuda/sumrows.cuh +4 -0
  298. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_16.cu +5 -0
  299. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_32.cu +6 -0
  300. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_1-ncols2_8.cu +11 -0
  301. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_1.cu +10 -0
  302. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_2.cu +10 -0
  303. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_16-ncols2_4.cu +12 -0
  304. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_16.cu +5 -0
  305. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_32.cu +6 -0
  306. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_4.cu +12 -0
  307. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_2-ncols2_8.cu +11 -0
  308. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_1.cu +10 -0
  309. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_32-ncols2_2.cu +10 -0
  310. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_16.cu +5 -0
  311. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_2.cu +10 -0
  312. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_4.cu +12 -0
  313. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_4-ncols2_8.cu +11 -0
  314. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_64-ncols2_1.cu +10 -0
  315. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_1.cu +10 -0
  316. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_2.cu +10 -0
  317. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_4.cu +12 -0
  318. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-mma-f16-instance-ncols1_8-ncols2_8.cu +11 -0
  319. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq112-dv112.cu +5 -0
  320. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq128-dv128.cu +5 -0
  321. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq256-dv256.cu +5 -0
  322. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq320-dv256.cu +5 -0
  323. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq40-dv40.cu +5 -0
  324. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq512-dv512.cu +5 -0
  325. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq576-dv512.cu +5 -0
  326. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq64-dv64.cu +5 -0
  327. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq72-dv72.cu +5 -0
  328. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq80-dv80.cu +5 -0
  329. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-tile-instance-dkq96-dv96.cu +5 -0
  330. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-bf16.cu +7 -0
  331. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-f16.cu +7 -0
  332. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_0.cu +7 -0
  333. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q4_1.cu +7 -0
  334. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_0.cu +7 -0
  335. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q5_1.cu +7 -0
  336. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-bf16-q8_0.cu +7 -0
  337. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-bf16.cu +7 -0
  338. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-f16.cu +7 -0
  339. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q4_0.cu +7 -0
  340. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q4_1.cu +7 -0
  341. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q5_0.cu +7 -0
  342. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q5_1.cu +7 -0
  343. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-f16-q8_0.cu +7 -0
  344. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-bf16.cu +7 -0
  345. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-f16.cu +7 -0
  346. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q4_0.cu +7 -0
  347. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q4_1.cu +7 -0
  348. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q5_0.cu +7 -0
  349. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q5_1.cu +7 -0
  350. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_0-q8_0.cu +7 -0
  351. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-bf16.cu +7 -0
  352. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-f16.cu +7 -0
  353. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q4_0.cu +7 -0
  354. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q4_1.cu +7 -0
  355. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q5_0.cu +7 -0
  356. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q5_1.cu +7 -0
  357. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q4_1-q8_0.cu +7 -0
  358. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-bf16.cu +7 -0
  359. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-f16.cu +7 -0
  360. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q4_0.cu +7 -0
  361. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q4_1.cu +7 -0
  362. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q5_0.cu +7 -0
  363. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q5_1.cu +7 -0
  364. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_0-q8_0.cu +7 -0
  365. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-bf16.cu +7 -0
  366. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-f16.cu +7 -0
  367. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q4_0.cu +7 -0
  368. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q4_1.cu +7 -0
  369. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q5_0.cu +7 -0
  370. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q5_1.cu +7 -0
  371. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q5_1-q8_0.cu +7 -0
  372. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-bf16.cu +7 -0
  373. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-f16.cu +7 -0
  374. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q4_0.cu +7 -0
  375. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q4_1.cu +7 -0
  376. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q5_0.cu +7 -0
  377. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q5_1.cu +7 -0
  378. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/fattn-vec-instance-q8_0-q8_0.cu +7 -0
  379. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_1.cu +5 -0
  380. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_10.cu +5 -0
  381. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_11.cu +5 -0
  382. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_12.cu +5 -0
  383. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_13.cu +5 -0
  384. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_14.cu +5 -0
  385. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_15.cu +5 -0
  386. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_16.cu +5 -0
  387. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_2.cu +5 -0
  388. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_3.cu +5 -0
  389. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_4.cu +5 -0
  390. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_5.cu +5 -0
  391. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_6.cu +5 -0
  392. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_7.cu +5 -0
  393. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_8.cu +5 -0
  394. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmf-instance-ncols_9.cu +5 -0
  395. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq1_s.cu +5 -0
  396. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_s.cu +5 -0
  397. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xs.cu +5 -0
  398. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq2_xxs.cu +5 -0
  399. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_s.cu +5 -0
  400. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq3_xxs.cu +5 -0
  401. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_nl.cu +5 -0
  402. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-iq4_xs.cu +5 -0
  403. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-mxfp4.cu +5 -0
  404. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-nvfp4.cu +5 -0
  405. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q1_0.cu +5 -0
  406. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q2_k.cu +5 -0
  407. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q3_k.cu +5 -0
  408. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_0.cu +5 -0
  409. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_1.cu +5 -0
  410. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q4_k.cu +5 -0
  411. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_0.cu +5 -0
  412. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_1.cu +5 -0
  413. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q5_k.cu +5 -0
  414. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q6_k.cu +5 -0
  415. data/vendor/crispasr/ggml/src/ggml-cuda/template-instances/mmq-instance-q8_0.cu +5 -0
  416. data/vendor/crispasr/ggml/src/ggml-cuda/top-k.cu +95 -0
  417. data/vendor/crispasr/ggml/src/ggml-cuda/top-k.cuh +3 -0
  418. data/vendor/crispasr/ggml/src/ggml-cuda/topk-moe.cu +415 -0
  419. data/vendor/crispasr/ggml/src/ggml-cuda/topk-moe.cuh +27 -0
  420. data/vendor/crispasr/ggml/src/ggml-cuda/tri.cu +136 -0
  421. data/vendor/crispasr/ggml/src/ggml-cuda/tri.cuh +5 -0
  422. data/vendor/crispasr/ggml/src/ggml-cuda/tsembd.cu +47 -0
  423. data/vendor/crispasr/ggml/src/ggml-cuda/tsembd.cuh +5 -0
  424. data/vendor/crispasr/ggml/src/ggml-cuda/unary.cu +644 -0
  425. data/vendor/crispasr/ggml/src/ggml-cuda/unary.cuh +116 -0
  426. data/vendor/crispasr/ggml/src/ggml-cuda/upscale.cu +293 -0
  427. data/vendor/crispasr/ggml/src/ggml-cuda/upscale.cuh +5 -0
  428. data/vendor/crispasr/ggml/src/ggml-cuda/vecdotq.cuh +1317 -0
  429. data/vendor/crispasr/ggml/src/ggml-cuda/vendors/cuda.h +28 -0
  430. data/vendor/crispasr/ggml/src/ggml-cuda/vendors/hip.h +306 -0
  431. data/vendor/crispasr/ggml/src/ggml-cuda/vendors/musa.h +148 -0
  432. data/vendor/crispasr/ggml/src/ggml-cuda/wkv.cu +199 -0
  433. data/vendor/crispasr/ggml/src/ggml-cuda/wkv.cuh +7 -0
  434. data/vendor/crispasr/ggml/src/ggml-impl.h +783 -0
  435. data/vendor/crispasr/ggml/src/ggml-metal/CMakeLists.txt +124 -0
  436. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-common.cpp +457 -0
  437. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-common.h +52 -0
  438. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-context.h +41 -0
  439. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-context.m +778 -0
  440. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-device.cpp +2117 -0
  441. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-device.h +299 -0
  442. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-device.m +2093 -0
  443. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-impl.h +1267 -0
  444. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-ops.cpp +5023 -0
  445. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal-ops.h +111 -0
  446. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal.cpp +954 -0
  447. data/vendor/crispasr/ggml/src/ggml-metal/ggml-metal.metal +11756 -0
  448. data/vendor/crispasr/ggml/src/ggml-opt.cpp +1094 -0
  449. data/vendor/crispasr/ggml/src/ggml-quants.c +5491 -0
  450. data/vendor/crispasr/ggml/src/ggml-quants.h +112 -0
  451. data/vendor/crispasr/ggml/src/ggml-threading.cpp +12 -0
  452. data/vendor/crispasr/ggml/src/ggml-threading.h +14 -0
  453. data/vendor/crispasr/ggml/src/ggml.c +7925 -0
  454. data/vendor/crispasr/ggml/src/ggml.cpp +26 -0
  455. data/vendor/crispasr/ggml/src/gguf.cpp +1556 -0
  456. data/vendor/crispasr/src/cohere-arch.h +137 -0
  457. data/vendor/crispasr/src/cohere.cpp +5642 -0
  458. data/vendor/crispasr/src/cohere.h +327 -0
  459. data/vendor/crispasr/src/cohere_batch_planner.h +82 -0
  460. data/vendor/crispasr/src/cohere_chunking.h +64 -0
  461. data/vendor/crispasr/src/cohere_decoder_batch_layout.h +60 -0
  462. data/vendor/crispasr/src/cohere_encoder_padded_layout.h +27 -0
  463. data/vendor/crispasr/src/cohere_frontend.cpp +189 -0
  464. data/vendor/crispasr/src/cohere_frontend.h +31 -0
  465. data/vendor/crispasr/src/cohere_ragged_controller.h +149 -0
  466. data/vendor/crispasr/src/cohere_token_renderer.h +181 -0
  467. data/vendor/crispasr/src/core/attention.h +924 -0
  468. data/vendor/crispasr/src/core/audio_chunking.h +97 -0
  469. data/vendor/crispasr/src/core/beam_decode.h +486 -0
  470. data/vendor/crispasr/src/core/cpu_ops.h +135 -0
  471. data/vendor/crispasr/src/core/gguf_loader.cpp +1021 -0
  472. data/vendor/crispasr/src/core/gguf_loader.h +216 -0
  473. data/vendor/crispasr/src/core/gpu_backend_pref.h +119 -0
  474. data/vendor/crispasr/src/core/mel.cpp +519 -0
  475. data/vendor/crispasr/src/core/mel.h +265 -0
  476. data/vendor/crispasr/src/core/ngram_loop_fix.h +173 -0
  477. data/vendor/crispasr/src/core/repetition_loop_guard.h +54 -0
  478. data/vendor/crispasr/src/crispasr_imatrix.cpp +255 -0
  479. data/vendor/crispasr/src/crispasr_imatrix.h +38 -0
  480. metadata +596 -0
@@ -0,0 +1,1323 @@
1
+ #include "ggml-alloc.h"
2
+ #include "ggml-backend-impl.h"
3
+ #include "ggml.h"
4
+ #include "ggml-impl.h"
5
+
6
+ #include <assert.h>
7
+ #include <limits.h>
8
+ #include <stdarg.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+
13
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
14
+ #define MAX_FREE_BLOCKS 256
15
+
16
+ //#define GGML_ALLOCATOR_DEBUG
17
+
18
+ //#define AT_PRINTF(...) GGML_LOG_DEBUG(__VA_ARGS__)
19
+ #define AT_PRINTF(...)
20
+
21
+ // Per-node allocator trace. Gated by CRISPASR_GGML_ALLOC_TRACE=1. Emits one
22
+ // line per allocate/free decision in a diffable format. Use
23
+ // CRISPASR_GGML_ALLOC_TRACE_MAX_PASSES=N to cap (sched runs the allocator
24
+ // per backend split, so many small splits fire before the UNet one).
25
+ static int s_alloc_trace_passes_seen = 0;
26
+ static int s_alloc_trace_max_passes = -1; // -1 = uninitialized
27
+ static bool s_alloc_trace_active = false;
28
+
29
+ static bool ggml_alloc_trace_init_env(void) {
30
+ if (s_alloc_trace_max_passes == -1) {
31
+ const char * env = getenv("CRISPASR_GGML_ALLOC_TRACE");
32
+ if (env && *env) {
33
+ const char * max_env = getenv("CRISPASR_GGML_ALLOC_TRACE_MAX_PASSES");
34
+ s_alloc_trace_max_passes = max_env ? atoi(max_env) : INT_MAX;
35
+ } else {
36
+ s_alloc_trace_max_passes = 0;
37
+ }
38
+ }
39
+ return s_alloc_trace_max_passes > 0;
40
+ }
41
+
42
+ static void ggml_alloc_trace_pass_begin(int n_nodes, int n_leafs) {
43
+ int pass_idx = s_alloc_trace_passes_seen++;
44
+ s_alloc_trace_active = ggml_alloc_trace_init_env() && pass_idx < s_alloc_trace_max_passes;
45
+ if (s_alloc_trace_active) {
46
+ fprintf(stderr, "ggml-alloc: [TRACE] PASS pass=%d n_nodes=%d n_leafs=%d\n",
47
+ pass_idx, n_nodes, n_leafs);
48
+ }
49
+ }
50
+
51
+ static void ggml_alloc_trace_pass_end(void) {
52
+ if (s_alloc_trace_active) {
53
+ fprintf(stderr, "ggml-alloc: [TRACE] PASS_END pass=%d\n",
54
+ s_alloc_trace_passes_seen - 1);
55
+ s_alloc_trace_active = false;
56
+ }
57
+ }
58
+
59
+ // ops that return true for this function must not use restrict pointers for their backend implementations
60
+ bool ggml_op_can_inplace(enum ggml_op op) {
61
+ switch (op) {
62
+ case GGML_OP_FILL:
63
+ case GGML_OP_SCALE:
64
+ case GGML_OP_DIAG_MASK_ZERO:
65
+ case GGML_OP_DIAG_MASK_INF:
66
+ case GGML_OP_ADD:
67
+ case GGML_OP_ADD_ID:
68
+ case GGML_OP_ADD1:
69
+ case GGML_OP_SUB:
70
+ case GGML_OP_MUL:
71
+ case GGML_OP_DIV:
72
+ case GGML_OP_SQR:
73
+ case GGML_OP_SQRT:
74
+ case GGML_OP_LOG:
75
+ case GGML_OP_UNARY:
76
+ case GGML_OP_ROPE:
77
+ case GGML_OP_ROPE_BACK:
78
+ case GGML_OP_SILU_BACK:
79
+ case GGML_OP_RMS_NORM:
80
+ case GGML_OP_RMS_NORM_BACK:
81
+ case GGML_OP_SOFT_MAX:
82
+ case GGML_OP_SOFT_MAX_BACK:
83
+ return true;
84
+
85
+ default:
86
+ return false;
87
+ }
88
+ }
89
+
90
+ static size_t aligned_offset(const void * buffer, size_t offset, size_t alignment) {
91
+ assert(alignment && !(alignment & (alignment - 1))); // power of 2
92
+ size_t align = (alignment - (((uintptr_t)buffer + offset) % alignment)) % alignment;
93
+ return offset + align;
94
+ }
95
+
96
+ // tallocr
97
+
98
+ struct ggml_tallocr ggml_tallocr_new(ggml_backend_buffer_t buffer) {
99
+ void * base = ggml_backend_buffer_get_base(buffer);
100
+ size_t align = ggml_backend_buffer_get_alignment(buffer);
101
+
102
+ assert(align && !(align & (align - 1))); // power of 2
103
+
104
+ struct ggml_tallocr talloc = (struct ggml_tallocr) {
105
+ /*.buffer = */ buffer,
106
+ /*.base = */ base,
107
+ /*.alignment = */ align,
108
+ /*.offset = */ aligned_offset(base, 0, align),
109
+ };
110
+ return talloc;
111
+ }
112
+
113
+ enum ggml_status ggml_tallocr_alloc(struct ggml_tallocr * talloc, struct ggml_tensor * tensor) {
114
+ size_t size = ggml_backend_buffer_get_alloc_size(talloc->buffer, tensor);
115
+ size = GGML_PAD(size, talloc->alignment);
116
+
117
+ if (talloc->offset + size > ggml_backend_buffer_get_size(talloc->buffer)) {
118
+ GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %s (needed %zu, available %zu)\n",
119
+ __func__, tensor->name, size, ggml_backend_buffer_get_size(talloc->buffer) - talloc->offset);
120
+ GGML_ABORT("not enough space in the buffer");
121
+ }
122
+
123
+ void * addr = (char *)ggml_backend_buffer_get_base(talloc->buffer) + talloc->offset;
124
+ talloc->offset += size;
125
+
126
+ assert(((uintptr_t)addr % talloc->alignment) == 0);
127
+
128
+ return ggml_backend_tensor_alloc(talloc->buffer, tensor, addr);
129
+ }
130
+
131
+ // dynamic tensor allocator
132
+
133
+ #define GGML_VBUFFER_MAX_CHUNKS 16
134
+
135
+ // relative memory address within an allocation that can be split into multiple buffers (chunks)
136
+ struct buffer_address {
137
+ int chunk; // index of a backend buffer
138
+ size_t offset; // local memory offset within the buffer
139
+ };
140
+
141
+ static const struct buffer_address GGML_BUFFER_ADDRESS_INVALID = { -1, SIZE_MAX };
142
+
143
+ static bool ggml_buffer_address_less(struct buffer_address a, struct buffer_address b) {
144
+ return a.chunk != b.chunk ? a.chunk < b.chunk : a.offset < b.offset;
145
+ }
146
+
147
+ struct free_block {
148
+ size_t offset;
149
+ size_t size;
150
+ };
151
+
152
+ struct tallocr_chunk {
153
+ struct free_block free_blocks[MAX_FREE_BLOCKS];
154
+ int n_free_blocks;
155
+ size_t max_size;
156
+ };
157
+
158
+ struct ggml_dyn_tallocr {
159
+ size_t alignment;
160
+ size_t max_chunk_size;
161
+ struct tallocr_chunk * chunks[GGML_VBUFFER_MAX_CHUNKS];
162
+ int n_chunks;
163
+
164
+ #ifdef GGML_ALLOCATOR_DEBUG
165
+ struct {
166
+ const struct ggml_tensor * tensor;
167
+ struct buffer_address addr;
168
+ } allocated_tensors[1024];
169
+ #endif
170
+ };
171
+
172
+ static void ggml_dyn_tallocr_insert_block(struct tallocr_chunk * chunk, size_t offset, size_t size) {
173
+ GGML_ASSERT(chunk->n_free_blocks < MAX_FREE_BLOCKS && "out of free blocks");
174
+ // insert the new block in the correct position to keep the array sorted by address (to make merging blocks faster)
175
+ int insert_pos = 0;
176
+ while (insert_pos < chunk->n_free_blocks && chunk->free_blocks[insert_pos].offset < offset) {
177
+ insert_pos++;
178
+ }
179
+ // shift all blocks from insert_pos onward to make room for the new block
180
+ for (int i = chunk->n_free_blocks; i > insert_pos; i--) {
181
+ chunk->free_blocks[i] = chunk->free_blocks[i-1];
182
+ }
183
+ // insert the new block
184
+ chunk->free_blocks[insert_pos].offset = offset;
185
+ chunk->free_blocks[insert_pos].size = size;
186
+ chunk->n_free_blocks++;
187
+ }
188
+
189
+ static void ggml_dyn_tallocr_remove_block(struct tallocr_chunk * chunk, int idx) {
190
+ // shift all elements after idx by 1 to the left, overwriting the element at idx
191
+ for (int i = idx; i < chunk->n_free_blocks; i++) {
192
+ chunk->free_blocks[i] = chunk->free_blocks[i+1];
193
+ }
194
+ chunk->n_free_blocks--;
195
+ }
196
+
197
+ static int ggml_dyn_tallocr_new_chunk(struct ggml_dyn_tallocr * alloc, size_t min_size) {
198
+ if (alloc->n_chunks >= GGML_VBUFFER_MAX_CHUNKS) {
199
+ return -1;
200
+ }
201
+ struct tallocr_chunk * chunk = calloc(1, sizeof(struct tallocr_chunk));
202
+ chunk->n_free_blocks = 1;
203
+ chunk->free_blocks[0].offset = 0;
204
+ // available space in a chunk is limited to max_chunk_size, but can be higher if:
205
+ // 1. a single tensor exceeds the maximum, and cannot fit any other way
206
+ // 2. we are running out of chunks
207
+ // backends will either manage to allocate the larger size, or report an error.
208
+ chunk->free_blocks[0].size = MAX(min_size, alloc->max_chunk_size);
209
+ if (alloc->n_chunks == GGML_VBUFFER_MAX_CHUNKS - 1) {
210
+ chunk->free_blocks[0].size = SIZE_MAX/2;
211
+ }
212
+ alloc->chunks[alloc->n_chunks] = chunk;
213
+ alloc->n_chunks++;
214
+ return alloc->n_chunks - 1;
215
+ }
216
+
217
+ #ifdef GGML_ALLOCATOR_DEBUG
218
+ static void add_allocated_tensor(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, const struct ggml_tensor * tensor) {
219
+ for (int i = 0; i < 1024; i++) {
220
+ if (alloc->allocated_tensors[i].tensor == NULL) {
221
+ alloc->allocated_tensors[i].tensor = tensor;
222
+ alloc->allocated_tensors[i].addr = addr;
223
+ return;
224
+ }
225
+ }
226
+ GGML_ABORT("out of allocated_tensors");
227
+ }
228
+ static void remove_allocated_tensor(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, const struct ggml_tensor * tensor) {
229
+ for (int i = 0; i < 1024; i++) {
230
+ if (alloc->allocated_tensors[i].addr.chunk == addr.chunk && alloc->allocated_tensors[i].addr.offset == addr.offset) {
231
+ alloc->allocated_tensors[i].tensor = NULL;
232
+ return;
233
+ }
234
+ }
235
+ GGML_ABORT("tried to free tensor %s not found\n", tensor->name);
236
+ }
237
+ #endif
238
+
239
+ static struct buffer_address ggml_dyn_tallocr_alloc(struct ggml_dyn_tallocr * alloc, size_t size, const struct ggml_tensor * tensor) {
240
+ size = aligned_offset(NULL, size, alloc->alignment);
241
+
242
+ AT_PRINTF("%s: allocating %s (%zu bytes) - ", __func__, tensor->name, size);
243
+
244
+ int best_fit_chunk = -1;
245
+ int best_fit_block = -1;
246
+ size_t max_avail = 0;
247
+
248
+ // find the best fitting free block besides the last block, within any chunk
249
+ for (int c = 0; c < alloc->n_chunks; ++c) {
250
+ struct tallocr_chunk * chunk = alloc->chunks[c];
251
+ size_t best_fit_size = SIZE_MAX;
252
+ for (int i = 0; i < chunk->n_free_blocks - 1; i++) {
253
+ struct free_block * block = &chunk->free_blocks[i];
254
+ max_avail = MAX(max_avail, block->size);
255
+ if (block->size >= size && block->size <= best_fit_size) {
256
+ best_fit_chunk = c;
257
+ best_fit_block = i;
258
+ best_fit_size = block->size;
259
+ }
260
+ }
261
+ }
262
+
263
+ if (best_fit_block == -1) {
264
+ // no suitable block found, try the last block (this may grow a chunks size)
265
+ int64_t best_reuse = INT64_MIN;
266
+ for (int c = 0; c < alloc->n_chunks; ++c) {
267
+ struct tallocr_chunk * chunk = alloc->chunks[c];
268
+ if (chunk->n_free_blocks > 0) {
269
+ struct free_block * block = &chunk->free_blocks[chunk->n_free_blocks - 1];
270
+ max_avail = MAX(max_avail, block->size);
271
+ int64_t reuse_factor = chunk->max_size - block->offset - size;
272
+ // reuse_factor < 0 : amount of extra memory that needs to be allocated
273
+ // reuse_factor = 0 : allocated free space exactly matches tensor size
274
+ // reuse_factor > 0 : superfluous memory that will remain unused
275
+ bool better_reuse = best_reuse < 0 && reuse_factor > best_reuse;
276
+ bool better_fit = reuse_factor >= 0 && reuse_factor < best_reuse;
277
+ if (block->size >= size && (better_reuse || better_fit)) {
278
+ best_fit_chunk = c;
279
+ best_fit_block = chunk->n_free_blocks - 1;
280
+ best_reuse = reuse_factor;
281
+ }
282
+ }
283
+ }
284
+ }
285
+
286
+ if (best_fit_block == -1) {
287
+ // none of the existing chunks have enough space left
288
+ best_fit_chunk = ggml_dyn_tallocr_new_chunk(alloc, size);
289
+ best_fit_block = 0;
290
+ }
291
+ if (best_fit_chunk == -1) {
292
+ // since the last chunk always has virtually endless memory, this should never happen
293
+ GGML_LOG_ERROR("%s: not enough space in the buffer to allocate %zu bytes, largest block available %zu bytes\n",
294
+ __func__, size, max_avail);
295
+ GGML_ABORT("graph allocation: failed to reserve memory");
296
+ }
297
+
298
+ struct tallocr_chunk * chunk = alloc->chunks[best_fit_chunk];
299
+ struct free_block * block = &chunk->free_blocks[best_fit_block];
300
+ struct buffer_address addr = {.chunk = best_fit_chunk, .offset = block->offset };
301
+ block->offset += size;
302
+ block->size -= size;
303
+ if (block->size == 0) {
304
+ // remove block if empty
305
+ ggml_dyn_tallocr_remove_block(chunk, best_fit_block);
306
+ }
307
+
308
+ AT_PRINTF("block %d, offset %zu, chunk %d\n", best_fit_block, addr.offset, addr.chunk);
309
+
310
+ #ifdef GGML_ALLOCATOR_DEBUG
311
+ add_allocated_tensor(alloc, addr, tensor);
312
+ size_t cur_max = addr.offset + size;
313
+ if (cur_max > chunk->max_size) {
314
+ // sort allocated_tensors by chunk/offset
315
+ for (int i = 0; i < 1024; i++) {
316
+ for (int j = i + 1; j < 1024; j++) {
317
+ if (ggml_buffer_address_less(alloc->allocated_tensors[j].addr, alloc->allocated_tensors[i].addr)) {
318
+ const struct ggml_tensor * tmp_tensor = alloc->allocated_tensors[i].tensor;
319
+ struct buffer_address tmp_addr = alloc->allocated_tensors[i].addr;
320
+ alloc->allocated_tensors[i].tensor = alloc->allocated_tensors[j].tensor;
321
+ alloc->allocated_tensors[i].addr = alloc->allocated_tensors[j].addr;
322
+ alloc->allocated_tensors[j].tensor = tmp_tensor;
323
+ alloc->allocated_tensors[j].addr = tmp_addr;
324
+ }
325
+ }
326
+ }
327
+ GGML_LOG_DEBUG("max_size[%d] = %.2f MB: tensors: ", addr.chunk, cur_max / 1024.0 / 1024.0);
328
+ for (int i = 0; i < 1024; i++) {
329
+ if (alloc->allocated_tensors[i].tensor) {
330
+ GGML_LOG_DEBUG("%s [%d: %zx-%zx] (%.2f MB) ", alloc->allocated_tensors[i].tensor->name,
331
+ alloc->allocated_tensors[i].addr.chunk,
332
+ alloc->allocated_tensors[i].addr.offset,
333
+ alloc->allocated_tensors[i].addr.offset + ggml_nbytes(alloc->allocated_tensors[i].tensor),
334
+ ggml_nbytes(alloc->allocated_tensors[i].tensor) / 1024.0 / 1024.0);
335
+ }
336
+ }
337
+ GGML_LOG_DEBUG("\n");
338
+ }
339
+ #endif
340
+
341
+ chunk->max_size = MAX(chunk->max_size, addr.offset + size);
342
+
343
+ return addr;
344
+
345
+ GGML_UNUSED(tensor);
346
+ }
347
+
348
+ // this is a very naive implementation, but for our case the number of free blocks should be very small
349
+ static void ggml_dyn_tallocr_free_bytes(struct ggml_dyn_tallocr * alloc, struct buffer_address addr, size_t size) {
350
+ size = aligned_offset(NULL, size, alloc->alignment);
351
+
352
+ struct tallocr_chunk * chunk = alloc->chunks[addr.chunk];
353
+
354
+ // see if we can merge with an existing block
355
+ for (int i = 0; i < chunk->n_free_blocks; i++) {
356
+ struct free_block * block = &chunk->free_blocks[i];
357
+ // check if ptr is at the end of the block
358
+ if (block->offset + block->size == addr.offset) {
359
+ block->size += size;
360
+ // check if we can merge with the next block
361
+ if (i < chunk->n_free_blocks - 1) {
362
+ struct free_block * next = &chunk->free_blocks[i+1];
363
+ if (block->offset + block->size == next->offset) {
364
+ block->size += next->size;
365
+ ggml_dyn_tallocr_remove_block(chunk, i+1);
366
+ }
367
+ }
368
+ return;
369
+ }
370
+ // check if ptr is at the beginning of the block
371
+ if (addr.offset + size == block->offset) {
372
+ block->offset = addr.offset;
373
+ block->size += size;
374
+ // check if we can merge with the previous block
375
+ if (i > 0) {
376
+ struct free_block * prev = &chunk->free_blocks[i-1];
377
+ if (prev->offset + prev->size == block->offset) {
378
+ prev->size += block->size;
379
+ ggml_dyn_tallocr_remove_block(chunk, i);
380
+ }
381
+ }
382
+ return;
383
+ }
384
+ }
385
+ // otherwise, add a new block
386
+ ggml_dyn_tallocr_insert_block(chunk, addr.offset, size);
387
+ }
388
+
389
+ static void ggml_dyn_tallocr_reset(struct ggml_dyn_tallocr * alloc) {
390
+ for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS; i++) {
391
+ free(alloc->chunks[i]);
392
+ alloc->chunks[i] = NULL;
393
+ }
394
+ alloc->n_chunks = 0;
395
+
396
+ #ifdef GGML_ALLOCATOR_DEBUG
397
+ for (int i = 0; i < 1024; i++) {
398
+ alloc->allocated_tensors[i].tensor = NULL;
399
+ }
400
+ #endif
401
+ }
402
+
403
+ static struct ggml_dyn_tallocr * ggml_dyn_tallocr_new(size_t alignment, size_t max_buffer_size) {
404
+ struct ggml_dyn_tallocr * alloc = (struct ggml_dyn_tallocr *)malloc(sizeof(struct ggml_dyn_tallocr));
405
+
406
+ *alloc = (struct ggml_dyn_tallocr) {
407
+ /*.alignment = */ alignment,
408
+ /*.max_chunk_size = */ MIN(max_buffer_size, SIZE_MAX/2), // clamp to avoid overflows
409
+ /*.chunks = */ {NULL},
410
+ /*.n_chunks = */ 0,
411
+ #ifdef GGML_ALLOCATOR_DEBUG
412
+ /*.allocated_tensors = */ {{0}},
413
+ #endif
414
+ };
415
+
416
+ ggml_dyn_tallocr_reset(alloc);
417
+
418
+ return alloc;
419
+ }
420
+
421
+ static void ggml_dyn_tallocr_free(struct ggml_dyn_tallocr * alloc) {
422
+ for (int i = 0; i < alloc->n_chunks; ++i) {
423
+ free(alloc->chunks[i]);
424
+ }
425
+ free(alloc);
426
+ }
427
+
428
+ static size_t ggml_dyn_tallocr_max_size(struct ggml_dyn_tallocr * alloc, int chunk) {
429
+ return chunk < alloc->n_chunks ? alloc->chunks[chunk]->max_size : 0;
430
+ }
431
+
432
+
433
+ // virtual buffer with contiguous memory range, split into multiple backend buffers (chunks)
434
+
435
+ struct vbuffer {
436
+ ggml_backend_buffer_t chunks[GGML_VBUFFER_MAX_CHUNKS];
437
+ };
438
+
439
+ static void ggml_vbuffer_free(struct vbuffer * buf) {
440
+ if (buf == NULL) {
441
+ return;
442
+ }
443
+ for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS; ++i) {
444
+ ggml_backend_buffer_free(buf->chunks[i]);
445
+ }
446
+ free(buf);
447
+ }
448
+
449
+ static size_t ggml_vbuffer_chunk_size(struct vbuffer * buf, int chunk) {
450
+ return buf->chunks[chunk] ? ggml_backend_buffer_get_size(buf->chunks[chunk]) : 0;
451
+ }
452
+
453
+ static size_t ggml_vbuffer_size(struct vbuffer * buf) {
454
+ size_t size = 0;
455
+ for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS && buf->chunks[i]; ++i) {
456
+ size += ggml_backend_buffer_get_size(buf->chunks[i]);
457
+ }
458
+ return size;
459
+ }
460
+
461
+ static struct vbuffer * ggml_vbuffer_alloc(ggml_backend_buffer_type_t buft, const struct ggml_dyn_tallocr * talloc, enum ggml_backend_buffer_usage usage) {
462
+ struct vbuffer * buf = (struct vbuffer *)calloc(1, sizeof(struct vbuffer));
463
+ if (buf == NULL) {
464
+ return NULL;
465
+ }
466
+
467
+ for (int n = 0; n < talloc->n_chunks; n++) {
468
+ size_t chunk_size = talloc->chunks[n]->max_size;
469
+ buf->chunks[n] = ggml_backend_buft_alloc_buffer(buft, chunk_size);
470
+ if (buf->chunks[n] == NULL) {
471
+ ggml_vbuffer_free(buf);
472
+ return NULL;
473
+ }
474
+ ggml_backend_buffer_set_usage(buf->chunks[n], usage);
475
+ }
476
+ return buf;
477
+ }
478
+
479
+ static void ggml_vbuffer_tensor_alloc(struct vbuffer * buf, struct ggml_tensor * tensor, struct buffer_address buf_addr) {
480
+ void * base = ggml_backend_buffer_get_base(buf->chunks[buf_addr.chunk]);
481
+ void * addr = (char *)base + buf_addr.offset;
482
+ ggml_backend_tensor_alloc(buf->chunks[buf_addr.chunk], tensor, addr);
483
+ }
484
+
485
+ static void ggml_vbuffer_reset(struct vbuffer * buf) {
486
+ for (int i = 0; i < GGML_VBUFFER_MAX_CHUNKS && buf->chunks[i]; ++i) {
487
+ ggml_backend_buffer_reset(buf->chunks[i]);
488
+ }
489
+ }
490
+
491
+
492
+ /////////////////////////////////////
493
+
494
+ // graph allocator
495
+
496
+ struct hash_node {
497
+ int n_children;
498
+ int n_views;
499
+ int buffer_id;
500
+ struct buffer_address addr;
501
+ bool allocated;
502
+ };
503
+
504
+ struct tensor_alloc {
505
+ int buffer_id;
506
+ struct buffer_address addr;
507
+ size_t size_max; // 0 = pre-allocated, unused, or view
508
+ };
509
+
510
+ struct leaf_alloc {
511
+ struct tensor_alloc leaf;
512
+ };
513
+
514
+ struct node_alloc {
515
+ struct tensor_alloc dst;
516
+ struct tensor_alloc src[GGML_MAX_SRC];
517
+ };
518
+
519
+ struct ggml_gallocr {
520
+ ggml_backend_buffer_type_t * bufts; // [n_buffers]
521
+ struct vbuffer ** buffers; // [n_buffers]
522
+ struct ggml_dyn_tallocr ** buf_tallocs; // [n_buffers]
523
+ int n_buffers;
524
+
525
+ struct ggml_hash_set hash_set;
526
+ struct hash_node * hash_values; // [hash_set.size]
527
+
528
+ struct node_alloc * node_allocs; // [n_nodes]
529
+ int n_nodes;
530
+
531
+ struct leaf_alloc * leaf_allocs; // [n_leafs]
532
+ int n_leafs;
533
+ };
534
+
535
+ ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs) {
536
+ ggml_gallocr_t galloc = (ggml_gallocr_t)calloc(1, sizeof(struct ggml_gallocr));
537
+ GGML_ASSERT(galloc != NULL);
538
+
539
+ galloc->bufts = calloc(n_bufs, sizeof(ggml_backend_buffer_type_t));
540
+ GGML_ASSERT(galloc->bufts != NULL);
541
+
542
+ galloc->buffers = calloc(n_bufs, sizeof(struct vbuffer *));
543
+ GGML_ASSERT(galloc->buffers != NULL);
544
+
545
+ galloc->buf_tallocs = calloc(n_bufs, sizeof(struct ggml_dyn_tallocr *));
546
+ GGML_ASSERT(galloc->buf_tallocs != NULL);
547
+
548
+ for (int i = 0; i < n_bufs; i++) {
549
+ galloc->bufts[i] = bufts[i];
550
+ galloc->buffers[i] = NULL;
551
+
552
+ // check if the same buffer type is used multiple times and reuse the same allocator
553
+ for (int j = 0; j < i; j++) {
554
+ if (bufts[i] == bufts[j]) {
555
+ galloc->buf_tallocs[i] = galloc->buf_tallocs[j];
556
+ break;
557
+ }
558
+ }
559
+
560
+ if (galloc->buf_tallocs[i] == NULL) {
561
+ size_t alignment = ggml_backend_buft_get_alignment(bufts[i]);
562
+ size_t max_size = ggml_backend_buft_get_max_size(bufts[i]);
563
+ galloc->buf_tallocs[i] = ggml_dyn_tallocr_new(alignment, max_size);
564
+ }
565
+ }
566
+ galloc->n_buffers = n_bufs;
567
+
568
+ return galloc;
569
+ }
570
+
571
+ ggml_gallocr_t ggml_gallocr_new(ggml_backend_buffer_type_t buft) {
572
+ return ggml_gallocr_new_n(&buft, 1);
573
+ }
574
+
575
+ void ggml_gallocr_free(ggml_gallocr_t galloc) {
576
+ if (galloc == NULL) {
577
+ return;
578
+ }
579
+
580
+ for (int i = 0; i < galloc->n_buffers; i++) {
581
+ if (galloc->buffers != NULL) {
582
+ // skip if already freed
583
+ bool freed = false;
584
+ for (int j = 0; j < i; j++) {
585
+ if (galloc->buffers[j] == galloc->buffers[i]) {
586
+ freed = true;
587
+ break;
588
+ }
589
+ }
590
+ if (!freed) {
591
+ ggml_vbuffer_free(galloc->buffers[i]);
592
+ }
593
+ }
594
+ if (galloc->buf_tallocs != NULL) {
595
+ // skip if already freed
596
+ bool freed = false;
597
+ for (int j = 0; j < i; j++) {
598
+ if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
599
+ freed = true;
600
+ break;
601
+ }
602
+ }
603
+ if (!freed) {
604
+ ggml_dyn_tallocr_free(galloc->buf_tallocs[i]);
605
+ }
606
+ }
607
+ }
608
+
609
+ ggml_hash_set_free(&galloc->hash_set);
610
+ free(galloc->hash_values);
611
+ free(galloc->bufts);
612
+ free(galloc->buffers);
613
+ free(galloc->buf_tallocs);
614
+ free(galloc->node_allocs);
615
+ free(galloc->leaf_allocs);
616
+ free(galloc);
617
+ }
618
+
619
+ typedef struct ggml_gallocr * ggml_gallocr_t;
620
+
621
+ static struct hash_node * ggml_gallocr_hash_get(ggml_gallocr_t galloc, struct ggml_tensor * t) {
622
+ size_t i = ggml_hash_find_or_insert(&galloc->hash_set, t);
623
+ return &galloc->hash_values[i];
624
+ }
625
+
626
+ static bool ggml_gallocr_is_own(ggml_gallocr_t galloc, struct ggml_tensor * t) {
627
+ return ggml_gallocr_hash_get(galloc, t)->allocated;
628
+ }
629
+
630
+ static bool ggml_gallocr_is_allocated(ggml_gallocr_t galloc, struct ggml_tensor * t) {
631
+ return t->data != NULL // tensor data already set externally
632
+ || t->buffer // tensor on external buffer (but not yet allocated)
633
+ || ggml_gallocr_is_own(galloc, t); // tensor will be allocated by galloc
634
+ }
635
+
636
+ // free the extra space at the end if the new tensor is smaller
637
+ static void ggml_gallocr_free_extra_space(ggml_gallocr_t galloc, struct ggml_tensor * node, struct ggml_tensor * parent) {
638
+ struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
639
+ struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
640
+
641
+ size_t parent_size = ggml_backend_buft_get_alloc_size(galloc->bufts[p_hn->buffer_id], parent);
642
+ size_t node_size = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
643
+
644
+ GGML_ASSERT(parent_size >= node_size);
645
+
646
+ // note: we want after the freeing the chunks to continue to be aligned
647
+ struct ggml_dyn_tallocr * p_alloc = galloc->buf_tallocs[p_hn->buffer_id];
648
+ parent_size = aligned_offset(NULL, parent_size, p_alloc->alignment);
649
+ node_size = aligned_offset(NULL, node_size, p_alloc->alignment);
650
+
651
+ if (parent_size > node_size) {
652
+ struct buffer_address p_addr = p_hn->addr;
653
+ p_addr.offset += node_size;
654
+ size_t extra_size = parent_size - node_size;
655
+ AT_PRINTF("freeing extra %zu bytes from parent %s for %s\n", extra_size, parent->name, node->name);
656
+ ggml_dyn_tallocr_free_bytes(p_alloc, p_addr, extra_size);
657
+ }
658
+ }
659
+
660
+ static void ggml_gallocr_allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node, int buffer_id) {
661
+ GGML_ASSERT(buffer_id >= 0);
662
+ struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
663
+
664
+ if (!ggml_gallocr_is_allocated(galloc, node) && !ggml_impl_is_view(node)) {
665
+ hn->allocated = true;
666
+ assert(hn->addr.offset == 0);
667
+
668
+ // try to reuse a parent's buffer (inplace)
669
+ if (ggml_op_can_inplace(node->op)) {
670
+ for (int i = 0; i < GGML_MAX_SRC; i++) {
671
+ struct ggml_tensor * parent = node->src[i];
672
+ if (parent == NULL) {
673
+ continue;
674
+ }
675
+
676
+ // if the node's data is external, then we cannot re-use it
677
+ if (!ggml_gallocr_is_own(galloc, parent)) {
678
+ AT_PRINTF("not reusing parent %s for %s as %p is external\n", parent->name, node->name, parent->data);
679
+ continue;
680
+ }
681
+
682
+ // outputs cannot be reused
683
+ if (parent->flags & GGML_TENSOR_FLAG_OUTPUT || (parent->view_src != NULL && parent->view_src->flags & GGML_TENSOR_FLAG_OUTPUT)) {
684
+ AT_PRINTF("not reusing parent %s for %s as it is an output\n", parent->name, node->name);
685
+ continue;
686
+ }
687
+
688
+ if (!ggml_are_same_layout(node, parent)) {
689
+ AT_PRINTF("not reusing parent %s for %s as layouts are different\n", parent->name, node->name);
690
+ continue;
691
+ }
692
+
693
+ struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
694
+ if (p_hn->n_children == 1 && p_hn->n_views == 0) {
695
+ if (ggml_impl_is_view(parent)) {
696
+ struct ggml_tensor * view_src = parent->view_src;
697
+ struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src);
698
+ if (view_src_hn->n_views == 1 && view_src_hn->n_children == 0 && view_src->data == parent->data) {
699
+ AT_PRINTF("reusing view parent %s (%s) for %s\n", parent->name, view_src->name, node->name);
700
+ assert(view_src_hn->addr.chunk == p_hn->addr.chunk && view_src_hn->addr.offset == p_hn->addr.offset);
701
+ hn->buffer_id = p_hn->buffer_id;
702
+ hn->addr = p_hn->addr;
703
+ p_hn->allocated = false; // avoid freeing the parent
704
+ view_src_hn->allocated = false;
705
+ ggml_gallocr_free_extra_space(galloc, node, view_src);
706
+ if (s_alloc_trace_active) {
707
+ size_t size = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
708
+ fprintf(stderr,
709
+ "ggml-alloc: [TRACE] ALLOC_REUSE_VIEW node=%s op=%s buf=%d chunk=%d offset=%zu size=%zu inplace_from=%s view_src=%s\n",
710
+ node->name, ggml_op_name(node->op), hn->buffer_id,
711
+ hn->addr.chunk, hn->addr.offset, size,
712
+ parent->name, view_src->name);
713
+ }
714
+ return;
715
+ }
716
+ } else {
717
+ AT_PRINTF("reusing parent %s for %s\n", parent->name, node->name);
718
+ hn->buffer_id = p_hn->buffer_id;
719
+ hn->addr = p_hn->addr;
720
+ p_hn->allocated = false; // avoid freeing the parent
721
+ ggml_gallocr_free_extra_space(galloc, node, parent);
722
+ if (s_alloc_trace_active) {
723
+ size_t size = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
724
+ fprintf(stderr,
725
+ "ggml-alloc: [TRACE] ALLOC_REUSE node=%s op=%s buf=%d chunk=%d offset=%zu size=%zu inplace_from=%s view_src=-\n",
726
+ node->name, ggml_op_name(node->op), hn->buffer_id,
727
+ hn->addr.chunk, hn->addr.offset, size, parent->name);
728
+ }
729
+ return;
730
+ }
731
+ }
732
+ }
733
+ }
734
+ // allocate tensor from the buffer
735
+ struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
736
+ ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
737
+ size_t size = ggml_backend_buft_get_alloc_size(buft, node);
738
+ hn->buffer_id = buffer_id;
739
+ hn->addr = ggml_dyn_tallocr_alloc(alloc, size, node);
740
+ if (s_alloc_trace_active) {
741
+ fprintf(stderr,
742
+ "ggml-alloc: [TRACE] ALLOC node=%s op=%s buf=%d chunk=%d offset=%zu size=%zu inplace_from=- view_src=-\n",
743
+ node->name, ggml_op_name(node->op), buffer_id,
744
+ hn->addr.chunk, hn->addr.offset, size);
745
+ }
746
+ }
747
+ }
748
+
749
+ static void ggml_gallocr_free_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
750
+ // graph outputs are never freed
751
+ if (node->flags & GGML_TENSOR_FLAG_OUTPUT) {
752
+ AT_PRINTF("not freeing output %s\n", node->name);
753
+ if (s_alloc_trace_active) {
754
+ struct hash_node * hn_out = ggml_gallocr_hash_get(galloc, node);
755
+ size_t size = ggml_backend_buft_get_alloc_size(galloc->bufts[hn_out->buffer_id], node);
756
+ fprintf(stderr,
757
+ "ggml-alloc: [TRACE] FREE_SKIP_OUTPUT node=%s buf=%d chunk=%d offset=%zu size=%zu\n",
758
+ node->name, hn_out->buffer_id, hn_out->addr.chunk, hn_out->addr.offset, size);
759
+ }
760
+ return;
761
+ }
762
+
763
+ struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
764
+ int buffer_id = hn->buffer_id;
765
+ struct ggml_dyn_tallocr * alloc = galloc->buf_tallocs[buffer_id];
766
+ ggml_backend_buffer_type_t buft = galloc->bufts[buffer_id];
767
+ size_t size = ggml_backend_buft_get_alloc_size(buft, node);
768
+
769
+ AT_PRINTF("%s: freeing %s at {chunk=%d, offset=%zu} (%zu bytes) - n_free_blocks = %d\n",
770
+ __func__, node->name, hn->addr.chunk, hn->addr.offset, size, alloc->chunks[hn->addr.chunk]->n_free_blocks);
771
+ if (s_alloc_trace_active) {
772
+ fprintf(stderr,
773
+ "ggml-alloc: [TRACE] FREE node=%s buf=%d chunk=%d offset=%zu size=%zu\n",
774
+ node->name, buffer_id, hn->addr.chunk, hn->addr.offset, size);
775
+ }
776
+ #ifdef GGML_ALLOCATOR_DEBUG
777
+ remove_allocated_tensor(alloc, hn->addr, node);
778
+ #endif
779
+
780
+ ggml_dyn_tallocr_free_bytes(alloc, hn->addr, size);
781
+ hn->allocated = false;
782
+ }
783
+
784
+ static int get_node_buffer_id(const int * node_buffer_ids, int i) {
785
+ return node_buffer_ids ? node_buffer_ids[i] : 0;
786
+ }
787
+
788
+ static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
789
+ // clear hash tables
790
+ ggml_hash_set_reset(&galloc->hash_set);
791
+ memset(galloc->hash_values, 0, sizeof(struct hash_node) * galloc->hash_set.size);
792
+
793
+ ggml_alloc_trace_pass_begin(graph->n_nodes, graph->n_leafs);
794
+
795
+ // allocate leafs
796
+ // these may be tensors that the application is not using in the graph, but may still want to allocate for other purposes
797
+ for (int i = 0; i < graph->n_leafs; i++) {
798
+ struct ggml_tensor * leaf = graph->leafs[i];
799
+ ggml_gallocr_allocate_node(galloc, leaf, get_node_buffer_id(leaf_buffer_ids, i));
800
+ }
801
+
802
+ // count number of children and views
803
+ // allocate other graph inputs and leafs first to avoid overwriting them
804
+ for (int i = 0; i < graph->n_nodes; i++) {
805
+ struct ggml_tensor * node = graph->nodes[i];
806
+
807
+ // TODO: better way to add external dependencies
808
+ // GGML_OP_NONE does not appear normally in the graph nodes, but is used by ggml-backend to add dependencies to
809
+ // control when some tensors are allocated and freed. in this case, the dependencies are in `src`, but the node
810
+ // itself is never used and should not be considered a dependency
811
+ if (ggml_impl_is_view(node) && node->op != GGML_OP_NONE) {
812
+ struct ggml_tensor * view_src = node->view_src;
813
+ ggml_gallocr_hash_get(galloc, view_src)->n_views += 1;
814
+ }
815
+
816
+ if (node->flags & GGML_TENSOR_FLAG_INPUT) {
817
+ ggml_gallocr_allocate_node(galloc, graph->nodes[i], get_node_buffer_id(node_buffer_ids, i));
818
+ }
819
+
820
+ for (int j = 0; j < GGML_MAX_SRC; j++) {
821
+ struct ggml_tensor * src = node->src[j];
822
+ if (src == NULL) {
823
+ continue;
824
+ }
825
+
826
+ ggml_gallocr_hash_get(galloc, src)->n_children += 1;
827
+
828
+ // allocate explicit inputs
829
+ if (src->flags & GGML_TENSOR_FLAG_INPUT) {
830
+ ggml_gallocr_allocate_node(galloc, src, get_node_buffer_id(node_buffer_ids, i));
831
+ }
832
+ }
833
+ }
834
+
835
+ // allocate tensors
836
+ for (int i = 0; i < graph->n_nodes; i++) {
837
+ struct ggml_tensor * node = graph->nodes[i];
838
+ int buffer_id = get_node_buffer_id(node_buffer_ids, i);
839
+
840
+ // allocate parents (only leafs need to be allocated at this point)
841
+ for (int j = 0; j < GGML_MAX_SRC; j++) {
842
+ struct ggml_tensor * parent = node->src[j];
843
+ if (parent == NULL) {
844
+ continue;
845
+ }
846
+ ggml_gallocr_allocate_node(galloc, parent, buffer_id);
847
+ }
848
+
849
+ // allocate node
850
+ ggml_gallocr_allocate_node(galloc, node, buffer_id);
851
+
852
+ AT_PRINTF("exec: %s (%s) <= ", ggml_op_desc(node), node->name);
853
+ for (int j = 0; j < GGML_MAX_SRC; j++) {
854
+ struct ggml_tensor * parent = node->src[j];
855
+ if (parent == NULL) {
856
+ continue;
857
+ }
858
+ AT_PRINTF("%s", parent->name);
859
+ if (j < GGML_MAX_SRC - 1 && node->src[j + 1] != NULL) {
860
+ AT_PRINTF(", ");
861
+ }
862
+ }
863
+ AT_PRINTF("\n");
864
+
865
+ // update parents
866
+ for (int j = 0; j < GGML_MAX_SRC; j++) {
867
+ struct ggml_tensor * parent = node->src[j];
868
+ if (parent == NULL) {
869
+ continue;
870
+ }
871
+ struct hash_node * p_hn = ggml_gallocr_hash_get(galloc, parent);
872
+ p_hn->n_children -= 1;
873
+
874
+ AT_PRINTF("parent %s: %d children, %d views, allocated: %d\n",
875
+ parent->name, p_hn->n_children, p_hn->n_views, p_hn->allocated);
876
+
877
+ if (p_hn->n_children == 0 && p_hn->n_views == 0) {
878
+ if (ggml_impl_is_view(parent)) {
879
+ struct ggml_tensor * view_src = parent->view_src;
880
+ struct hash_node * view_src_hn = ggml_gallocr_hash_get(galloc, view_src);
881
+ view_src_hn->n_views -= 1;
882
+ AT_PRINTF("view_src %s: %d children, %d views\n",
883
+ view_src->name, view_src_hn->n_children, view_src_hn->n_views);
884
+ if (view_src_hn->n_views == 0 && view_src_hn->n_children == 0 && view_src_hn->allocated) {
885
+ ggml_gallocr_free_node(galloc, view_src);
886
+ }
887
+ }
888
+ else if (p_hn->allocated) {
889
+ ggml_gallocr_free_node(galloc, parent);
890
+ }
891
+ }
892
+ AT_PRINTF("\n");
893
+ }
894
+ }
895
+
896
+ ggml_alloc_trace_pass_end();
897
+ }
898
+
899
+ static bool ggml_gallocr_reserve_n_impl(
900
+ ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids, bool no_alloc) {
901
+ size_t min_hash_size = graph->n_nodes + graph->n_leafs;
902
+ // add 25% margin to avoid hash collisions
903
+ min_hash_size += min_hash_size / 4;
904
+
905
+ // initialize hash table
906
+ if (galloc->hash_set.size < min_hash_size) {
907
+ ggml_hash_set_free(&galloc->hash_set);
908
+ galloc->hash_set = ggml_hash_set_new(min_hash_size);
909
+ GGML_ASSERT(galloc->hash_set.keys != NULL);
910
+
911
+ free(galloc->hash_values);
912
+ galloc->hash_values = malloc(sizeof(struct hash_node) * galloc->hash_set.size);
913
+ GGML_ASSERT(galloc->hash_values != NULL);
914
+ }
915
+
916
+ // reset allocators
917
+ for (int i = 0; i < galloc->n_buffers; i++) {
918
+ ggml_dyn_tallocr_reset(galloc->buf_tallocs[i]);
919
+ }
920
+
921
+ // allocate in hash table
922
+ ggml_gallocr_alloc_graph_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids);
923
+
924
+ // set the node_allocs from the hash table
925
+ if (galloc->n_nodes < graph->n_nodes) {
926
+ free(galloc->node_allocs);
927
+ galloc->node_allocs = calloc(graph->n_nodes, sizeof(struct node_alloc));
928
+ GGML_ASSERT(galloc->node_allocs != NULL);
929
+ }
930
+ galloc->n_nodes = graph->n_nodes;
931
+ for (int i = 0; i < graph->n_nodes; i++) {
932
+ struct ggml_tensor * node = graph->nodes[i];
933
+ struct node_alloc * node_alloc = &galloc->node_allocs[i];
934
+ if (node->view_src || node->data) {
935
+ node_alloc->dst.buffer_id = -1;
936
+ node_alloc->dst.addr = GGML_BUFFER_ADDRESS_INVALID;
937
+ node_alloc->dst.size_max = 0;
938
+ } else {
939
+ struct hash_node * hn = ggml_gallocr_hash_get(galloc, node);
940
+ node_alloc->dst.buffer_id = hn->buffer_id;
941
+ node_alloc->dst.addr = hn->addr;
942
+ node_alloc->dst.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node);
943
+ }
944
+ for (int j = 0; j < GGML_MAX_SRC; j++) {
945
+ struct ggml_tensor * src = node->src[j];
946
+ if (!src || src->view_src || src->data) {
947
+ node_alloc->src[j].buffer_id = -1;
948
+ node_alloc->src[j].addr = GGML_BUFFER_ADDRESS_INVALID;
949
+ node_alloc->src[j].size_max = 0;
950
+ } else {
951
+ struct hash_node * hn = ggml_gallocr_hash_get(galloc, src);
952
+ node_alloc->src[j].buffer_id = hn->buffer_id;
953
+ node_alloc->src[j].addr = hn->addr;
954
+ node_alloc->src[j].size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], src);
955
+ }
956
+ }
957
+ }
958
+ if (galloc->n_leafs < graph->n_leafs) {
959
+ free(galloc->leaf_allocs);
960
+ galloc->leaf_allocs = calloc(graph->n_leafs, sizeof(galloc->leaf_allocs[0]));
961
+ GGML_ASSERT(galloc->leaf_allocs != NULL);
962
+ }
963
+ galloc->n_leafs = graph->n_leafs;
964
+ for (int i = 0; i < graph->n_leafs; i++) {
965
+ struct ggml_tensor * leaf = graph->leafs[i];
966
+ struct hash_node * hn = ggml_gallocr_hash_get(galloc, leaf);
967
+ if (leaf->view_src || leaf->data) {
968
+ galloc->leaf_allocs[i].leaf.buffer_id = -1;
969
+ galloc->leaf_allocs[i].leaf.addr = GGML_BUFFER_ADDRESS_INVALID;
970
+ galloc->leaf_allocs[i].leaf.size_max = 0;
971
+ } else {
972
+ galloc->leaf_allocs[i].leaf.buffer_id = hn->buffer_id;
973
+ galloc->leaf_allocs[i].leaf.addr = hn->addr;
974
+ galloc->leaf_allocs[i].leaf.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], leaf);
975
+ }
976
+ }
977
+
978
+ // reallocate buffers if needed
979
+ for (int i = 0; i < galloc->n_buffers; i++) {
980
+ // if the buffer type is used multiple times, we reuse the same buffer
981
+ for (int j = 0; j < i; j++) {
982
+ if (galloc->buf_tallocs[j] == galloc->buf_tallocs[i]) {
983
+ galloc->buffers[i] = galloc->buffers[j];
984
+ break;
985
+ }
986
+ }
987
+
988
+ // even if there are no tensors allocated in this buffer, we still need to allocate it to initialize views
989
+ bool realloc = galloc->buffers[i] == NULL;
990
+ size_t new_size = 0;
991
+ for (int c = 0; c < galloc->buf_tallocs[i]->n_chunks; c++) {
992
+ size_t cur_chunk_size = galloc->buffers[i] ? ggml_vbuffer_chunk_size(galloc->buffers[i], c) : 0;
993
+ size_t new_chunk_size = ggml_dyn_tallocr_max_size(galloc->buf_tallocs[i], c);
994
+ new_size += new_chunk_size;
995
+ if (new_chunk_size > cur_chunk_size) {
996
+ realloc = true;
997
+ }
998
+ }
999
+ if (realloc) {
1000
+ #ifndef NDEBUG
1001
+ {
1002
+ size_t cur_size = galloc->buffers[i] ? ggml_vbuffer_size(galloc->buffers[i]) : 0;
1003
+ if (cur_size > 0) {
1004
+ GGML_LOG_DEBUG("%s: reallocating %s buffer from size %.02f MiB to %.02f MiB\n",
1005
+ __func__, ggml_backend_buft_name(galloc->bufts[i]), cur_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
1006
+ }
1007
+ }
1008
+ #endif
1009
+ ggml_vbuffer_free(galloc->buffers[i]);
1010
+ if (no_alloc) {
1011
+ galloc->buffers[i] = NULL;
1012
+ } else {
1013
+ galloc->buffers[i] = ggml_vbuffer_alloc(galloc->bufts[i], galloc->buf_tallocs[i], GGML_BACKEND_BUFFER_USAGE_COMPUTE);
1014
+ if (galloc->buffers[i] == NULL) {
1015
+ GGML_LOG_ERROR("%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(galloc->bufts[i]), new_size);
1016
+ return false;
1017
+ }
1018
+ }
1019
+ }
1020
+ }
1021
+
1022
+ return true;
1023
+ }
1024
+
1025
+ void ggml_gallocr_reserve_n_size(
1026
+ ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids, size_t * sizes) {
1027
+ GGML_ASSERT(ggml_gallocr_reserve_n_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids, /*no_alloc =*/ true));
1028
+ for (int i = 0; i < galloc->n_buffers; i++) {
1029
+ sizes[i] = 0;
1030
+ for (int c = 0; c < galloc->buf_tallocs[i]->n_chunks; c++) {
1031
+ sizes[i] += galloc->buf_tallocs[i]->chunks[c]->max_size;
1032
+ }
1033
+ }
1034
+ }
1035
+
1036
+ bool ggml_gallocr_reserve_n(ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids) {
1037
+ return ggml_gallocr_reserve_n_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids, /*no_alloc =*/ false);
1038
+ }
1039
+
1040
+ bool ggml_gallocr_reserve(ggml_gallocr_t galloc, struct ggml_cgraph *graph) {
1041
+ return ggml_gallocr_reserve_n(galloc, graph, NULL, NULL);
1042
+ }
1043
+
1044
+ static void ggml_gallocr_init_tensor(ggml_gallocr_t galloc, struct ggml_tensor * tensor, struct tensor_alloc * tensor_alloc) {
1045
+ int buffer_id = tensor_alloc->buffer_id;
1046
+ assert(tensor->data || tensor->view_src || ggml_backend_buft_get_alloc_size(galloc->bufts[buffer_id], tensor) <= tensor_alloc->size_max);
1047
+
1048
+ if (tensor->view_src != NULL) {
1049
+ if (tensor->buffer == NULL) {
1050
+ assert(tensor_alloc->addr.offset == SIZE_MAX);
1051
+ if (tensor->view_src->buffer == NULL) {
1052
+ // this tensor was allocated without ggml-backend
1053
+ return;
1054
+ }
1055
+ ggml_backend_view_init(tensor);
1056
+ }
1057
+ } else {
1058
+ if (tensor->data == NULL) {
1059
+ assert(tensor_alloc->addr.offset != SIZE_MAX);
1060
+ assert(ggml_backend_buft_get_alloc_size(galloc->bufts[buffer_id], tensor) <= tensor_alloc->size_max);
1061
+ ggml_vbuffer_tensor_alloc(galloc->buffers[buffer_id], tensor, tensor_alloc->addr);
1062
+ } else {
1063
+ if (tensor->buffer == NULL) {
1064
+ // this tensor was allocated without ggml-backend
1065
+ return;
1066
+ }
1067
+ }
1068
+ }
1069
+ }
1070
+
1071
+ static bool ggml_gallocr_node_needs_realloc(ggml_gallocr_t galloc, struct ggml_tensor * node, struct tensor_alloc * talloc) {
1072
+ size_t node_size = 0;
1073
+ if (!node->data && !node->view_src) {
1074
+ // If we previously had data but don't now then reallocate
1075
+ if (talloc->buffer_id < 0) {
1076
+ return false;
1077
+ }
1078
+ node_size = ggml_backend_buft_get_alloc_size(galloc->bufts[talloc->buffer_id], node);
1079
+ }
1080
+ return talloc->size_max >= node_size;
1081
+ }
1082
+
1083
+ static bool ggml_gallocr_needs_realloc(ggml_gallocr_t galloc, struct ggml_cgraph * graph) {
1084
+ if (galloc->n_nodes != graph->n_nodes) {
1085
+ #ifndef NDEBUG
1086
+ GGML_LOG_DEBUG("%s: graph has different number of nodes\n", __func__);
1087
+ #endif
1088
+ return true;
1089
+ }
1090
+
1091
+ if (galloc->n_leafs != graph->n_leafs) {
1092
+ #ifndef NDEBUG
1093
+ GGML_LOG_DEBUG("%s: graph has different number of leafs\n", __func__);
1094
+ #endif
1095
+ return true;
1096
+ }
1097
+
1098
+ for (int i = 0; i < graph->n_nodes; i++) {
1099
+ struct ggml_tensor * node = graph->nodes[i];
1100
+ struct node_alloc * node_alloc = &galloc->node_allocs[i];
1101
+
1102
+ if (!ggml_gallocr_node_needs_realloc(galloc, node, &node_alloc->dst)) {
1103
+ #ifndef NDEBUG
1104
+ GGML_LOG_DEBUG("%s: node %s is not valid\n", __func__, node->name);
1105
+ #endif
1106
+ return true;
1107
+ }
1108
+
1109
+ for (int j = 0; j < GGML_MAX_SRC; j++) {
1110
+ struct ggml_tensor * src = node->src[j];
1111
+ if (src == NULL) {
1112
+ continue;
1113
+ }
1114
+ if (!ggml_gallocr_node_needs_realloc(galloc, src, &node_alloc->src[j])) {
1115
+ #ifndef NDEBUG
1116
+ GGML_LOG_DEBUG("%s: src %d (%s) of node %s is not valid\n", __func__, j, src->name, node->name);
1117
+ #endif
1118
+ return true;
1119
+ }
1120
+ }
1121
+ }
1122
+
1123
+ return false;
1124
+ }
1125
+
1126
+ bool ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, struct ggml_cgraph * graph) {
1127
+ if (ggml_gallocr_needs_realloc(galloc, graph)) {
1128
+ if (galloc->n_buffers == 1) {
1129
+ #ifndef NDEBUG
1130
+ GGML_LOG_DEBUG("%s: reallocating buffers automatically\n", __func__);
1131
+ #endif
1132
+ if (!ggml_gallocr_reserve(galloc, graph)) {
1133
+ return false;
1134
+ }
1135
+ } else {
1136
+ #ifndef NDEBUG
1137
+ GGML_LOG_DEBUG("%s: cannot reallocate multi buffer graph automatically, call reserve\n", __func__);
1138
+ #endif
1139
+ return false;
1140
+ }
1141
+ }
1142
+
1143
+ // reset buffers
1144
+ for (int i = 0; i < galloc->n_buffers; i++) {
1145
+ if (galloc->buffers[i] != NULL) {
1146
+ ggml_vbuffer_reset(galloc->buffers[i]);
1147
+ }
1148
+ }
1149
+
1150
+ // allocate the graph tensors from the previous assignments
1151
+ // leafs
1152
+ for (int i = 0; i < graph->n_leafs; i++) {
1153
+ struct ggml_tensor * leaf = graph->leafs[i];
1154
+ struct leaf_alloc * leaf_alloc = &galloc->leaf_allocs[i];
1155
+ ggml_gallocr_init_tensor(galloc, leaf, &leaf_alloc->leaf);
1156
+ }
1157
+ // nodes
1158
+ for (int i = 0; i < graph->n_nodes; i++) {
1159
+ struct ggml_tensor * node = graph->nodes[i];
1160
+ struct node_alloc * node_alloc = &galloc->node_allocs[i];
1161
+ for (int j = 0; j < GGML_MAX_SRC; j++) {
1162
+ struct ggml_tensor * src = node->src[j];
1163
+ if (src == NULL) {
1164
+ continue;
1165
+ }
1166
+ ggml_gallocr_init_tensor(galloc, src, &node_alloc->src[j]);
1167
+ }
1168
+ ggml_gallocr_init_tensor(galloc, node, &node_alloc->dst);
1169
+ }
1170
+
1171
+ return true;
1172
+ }
1173
+
1174
+ size_t ggml_gallocr_get_buffer_size(ggml_gallocr_t galloc, int buffer_id) {
1175
+ GGML_ASSERT(buffer_id >= 0 && buffer_id < galloc->n_buffers);
1176
+
1177
+ if (galloc->buffers[buffer_id] == NULL) {
1178
+ return 0;
1179
+ }
1180
+
1181
+ for (int i = 0; i < buffer_id; i++) {
1182
+ if (galloc->buffers[i] == galloc->buffers[buffer_id]) {
1183
+ // this buffer is the same as a previous one due to the same buffer type being used multiple times
1184
+ // only return the buffer size the first time it appears to avoid double counting
1185
+ return 0;
1186
+ }
1187
+ }
1188
+
1189
+ return ggml_vbuffer_size(galloc->buffers[buffer_id]);
1190
+ }
1191
+
1192
+ // utils
1193
+
1194
+ static void free_buffers(ggml_backend_buffer_t ** buffers, const size_t * n_buffers) {
1195
+ for (size_t i = 0; i < *n_buffers; i++) {
1196
+ ggml_backend_buffer_free((*buffers)[i]);
1197
+ }
1198
+ free(*buffers);
1199
+ }
1200
+
1201
+ static bool alloc_tensor_range(struct ggml_context * ctx,
1202
+ struct ggml_tensor * first, struct ggml_tensor * last,
1203
+ ggml_backend_buffer_type_t buft, size_t size,
1204
+ ggml_backend_buffer_t ** buffers, size_t * n_buffers) {
1205
+
1206
+ ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, size);
1207
+ if (buffer == NULL) {
1208
+ GGML_LOG_ERROR("%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(buft), size);
1209
+ free_buffers(buffers, n_buffers);
1210
+ return false;
1211
+ }
1212
+
1213
+ *buffers = realloc(*buffers, sizeof(ggml_backend_buffer_t) * (*n_buffers + 1));
1214
+ (*buffers)[(*n_buffers)++] = buffer;
1215
+
1216
+ struct ggml_tallocr tallocr = ggml_tallocr_new(buffer);
1217
+
1218
+ for (struct ggml_tensor * t = first; t != last; t = ggml_get_next_tensor(ctx, t)) {
1219
+ enum ggml_status status = GGML_STATUS_SUCCESS;
1220
+ if (t->data == NULL) {
1221
+ if (t->view_src == NULL) {
1222
+ status = ggml_tallocr_alloc(&tallocr, t);
1223
+ } else if (t->buffer == NULL) {
1224
+ status = ggml_backend_view_init(t);
1225
+ }
1226
+ } else {
1227
+ if (t->view_src != NULL && t->buffer == NULL) {
1228
+ // view of a pre-allocated tensor
1229
+ status = ggml_backend_view_init(t);
1230
+ }
1231
+ }
1232
+ if (status != GGML_STATUS_SUCCESS) {
1233
+ GGML_LOG_ERROR("%s: failed to initialize tensor %s\n", __func__, t->name);
1234
+ free_buffers(buffers, n_buffers);
1235
+ return false;
1236
+ }
1237
+ }
1238
+
1239
+ return true;
1240
+ }
1241
+
1242
+ static ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft_impl(
1243
+ struct ggml_context * ctx, ggml_backend_buffer_type_t buft, size_t * nbytes_total, bool no_alloc) {
1244
+ GGML_ASSERT(ggml_get_no_alloc(ctx) == true);
1245
+
1246
+ size_t alignment = ggml_backend_buft_get_alignment(buft);
1247
+ size_t max_size = ggml_backend_buft_get_max_size(buft);
1248
+
1249
+ ggml_backend_buffer_t * buffers = NULL;
1250
+ size_t n_buffers = 0;
1251
+ *nbytes_total = 0;
1252
+
1253
+ size_t cur_buf_size = 0;
1254
+ struct ggml_tensor * first = ggml_get_first_tensor(ctx);
1255
+ for (struct ggml_tensor * t = first; t != NULL; t = ggml_get_next_tensor(ctx, t)) {
1256
+ size_t this_size = 0;
1257
+ if (t->data == NULL && t->view_src == NULL) {
1258
+ this_size = GGML_PAD(ggml_backend_buft_get_alloc_size(buft, t), alignment);
1259
+ }
1260
+
1261
+ if (cur_buf_size > 0 && (cur_buf_size + this_size) > max_size) {
1262
+ // allocate tensors in the current buffer
1263
+ if (!no_alloc && !alloc_tensor_range(ctx, first, t, buft, cur_buf_size, &buffers, &n_buffers)) {
1264
+ return NULL;
1265
+ }
1266
+ first = t;
1267
+ *nbytes_total += cur_buf_size;
1268
+ cur_buf_size = this_size;
1269
+ } else {
1270
+ cur_buf_size += this_size;
1271
+ }
1272
+ }
1273
+
1274
+ // allocate remaining tensors
1275
+ if (cur_buf_size > 0) {
1276
+ *nbytes_total += cur_buf_size;
1277
+ if (!no_alloc && !alloc_tensor_range(ctx, first, NULL, buft, cur_buf_size, &buffers, &n_buffers)) {
1278
+ return NULL;
1279
+ }
1280
+ }
1281
+
1282
+ if (no_alloc) {
1283
+ return NULL;
1284
+ }
1285
+
1286
+ if (n_buffers == 0) {
1287
+ #ifndef NDEBUG
1288
+ GGML_LOG_DEBUG("%s: all tensors in the context are already allocated\n", __func__);
1289
+ #endif
1290
+ GGML_ASSERT(!buffers);
1291
+ return NULL;
1292
+ }
1293
+
1294
+ ggml_backend_buffer_t buffer;
1295
+ if (n_buffers == 1) {
1296
+ buffer = buffers[0];
1297
+ } else {
1298
+ buffer = ggml_backend_multi_buffer_alloc_buffer(buffers, n_buffers);
1299
+ }
1300
+ if (buffers) {
1301
+ free(buffers); // can be NULL if context is empty or no_alloc
1302
+ }
1303
+ return buffer;
1304
+ }
1305
+
1306
+ size_t ggml_backend_alloc_ctx_tensors_from_buft_size(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) {
1307
+ size_t nbytes_total = 0;
1308
+ ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft_impl(ctx, buft, &nbytes_total, /*no_alloc=*/ true);
1309
+ GGML_ASSERT(!buf);
1310
+ return nbytes_total;
1311
+ }
1312
+
1313
+ ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_context * ctx, ggml_backend_buffer_type_t buft) {
1314
+ size_t nbytes_total = 0;
1315
+ if (ggml_backend_buft_is_meta(buft)) {
1316
+ return ggml_backend_meta_alloc_ctx_tensors_from_buft(ctx, buft);
1317
+ }
1318
+ return ggml_backend_alloc_ctx_tensors_from_buft_impl(ctx, buft, &nbytes_total, /*no_alloc =*/ false);
1319
+ }
1320
+
1321
+ ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors(struct ggml_context * ctx, ggml_backend_t backend) {
1322
+ return ggml_backend_alloc_ctx_tensors_from_buft(ctx, ggml_backend_get_default_buffer_type(backend));
1323
+ }