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,778 @@
1
+ #import "ggml-metal-context.h"
2
+
3
+ #import "ggml-impl.h"
4
+ #import "ggml-backend-impl.h"
5
+
6
+ #import "ggml-metal-impl.h"
7
+ #import "ggml-metal-common.h"
8
+ #import "ggml-metal-ops.h"
9
+
10
+ #import <Foundation/Foundation.h>
11
+
12
+ #import <Metal/Metal.h>
13
+
14
+ #undef MIN
15
+ #undef MAX
16
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
17
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
18
+
19
+ // max number of MTLCommandBuffer used to submit a graph for processing
20
+ #define GGML_METAL_MAX_COMMAND_BUFFERS 8
21
+
22
+ struct ggml_metal_command_buffer {
23
+ id<MTLCommandBuffer> obj;
24
+ };
25
+
26
+ struct ggml_metal {
27
+ char name[128];
28
+
29
+ ggml_metal_device_t dev;
30
+ ggml_metal_library_t lib;
31
+
32
+ ggml_metal_event_t ev_cpy; // for async copies
33
+
34
+ dispatch_queue_t d_queue;
35
+
36
+ // additional, inference-time compiled pipelines
37
+ ggml_metal_pipelines_t pipelines_ext;
38
+
39
+ bool use_fusion;
40
+ bool use_concurrency;
41
+ bool use_graph_optimize;
42
+
43
+ int debug_graph;
44
+ int debug_fusion;
45
+
46
+ // how many times a given op was fused
47
+ uint64_t fuse_cnt[GGML_OP_COUNT];
48
+
49
+ // capture state
50
+ int capture_compute;
51
+ bool capture_started;
52
+
53
+ id<MTLCaptureScope> capture_scope;
54
+
55
+ // command buffer state
56
+ int n_cb; // number of extra threads used to submit the command buffers
57
+ int n_nodes_0; // number of nodes submitted by the main thread
58
+ int n_nodes_1; // remaining number of nodes submitted by the n_cb threads
59
+ int n_nodes_per_cb;
60
+
61
+ struct ggml_cgraph * gf;
62
+
63
+ // the callback given to the thread pool
64
+ void (^encode_async)(size_t ith);
65
+
66
+ // n_cb command buffers + 1 used by the main thread
67
+ struct ggml_metal_command_buffer cmd_bufs[GGML_METAL_MAX_COMMAND_BUFFERS + 1];
68
+
69
+ // extra command buffers for things like getting, setting and copying tensors
70
+ NSMutableArray * cmd_bufs_ext;
71
+
72
+ // the last command buffer queued into the Metal queue with operations relevant to the current Metal backend
73
+ id<MTLCommandBuffer> cmd_buf_last;
74
+
75
+ // abort ggml_metal_graph_compute if callback returns true
76
+ ggml_abort_callback abort_callback;
77
+ void * abort_callback_data;
78
+
79
+ // error state - set when a command buffer fails during synchronize
80
+ // once set, graph_compute will return GGML_STATUS_FAILED until the backend is recreated
81
+ bool has_error;
82
+ };
83
+
84
+ ggml_metal_t ggml_metal_init(ggml_metal_device_t dev) {
85
+ GGML_LOG_INFO("%s: allocating\n", __func__);
86
+
87
+ #if TARGET_OS_OSX && !GGML_METAL_NDEBUG
88
+ // Show all the Metal device instances in the system
89
+ NSArray * devices = MTLCopyAllDevices();
90
+ for (id<MTLDevice> device in devices) {
91
+ GGML_LOG_INFO("%s: found device: %s\n", __func__, [[device name] UTF8String]);
92
+ }
93
+ [devices release]; // since it was created by a *Copy* C method
94
+ #endif
95
+
96
+ // init context
97
+ ggml_metal_t res = calloc(1, sizeof(struct ggml_metal));
98
+
99
+ id<MTLDevice> device = ggml_metal_device_get_obj(dev);
100
+
101
+ GGML_LOG_INFO("%s: picking default device: %s\n", __func__, [[device name] UTF8String]);
102
+
103
+ // TODO: would it be better to have one queue for the backend and one queue for the device?
104
+ // the graph encoders and async ops would use the backend queue while the sync ops would use the device queue?
105
+ //res->queue = [device newCommandQueue]; [TAG_QUEUE_PER_BACKEND]
106
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(dev);
107
+ if (queue == nil) {
108
+ GGML_LOG_ERROR("%s: error: failed to create command queue\n", __func__);
109
+ return NULL;
110
+ }
111
+
112
+ res->dev = dev;
113
+ res->lib = ggml_metal_device_get_library(dev);
114
+ if (res->lib == NULL) {
115
+ GGML_LOG_WARN("%s: the device does not have a precompiled Metal library - this is unexpected\n", __func__);
116
+ GGML_LOG_WARN("%s: will try to compile it on the fly\n", __func__);
117
+
118
+ res->lib = ggml_metal_library_init(dev);
119
+ if (res->lib == NULL) {
120
+ GGML_LOG_ERROR("%s: error: failed to initialize the Metal library\n", __func__);
121
+
122
+ free(res);
123
+
124
+ return NULL;
125
+ }
126
+ }
127
+
128
+ res->ev_cpy = ggml_metal_device_event_init(dev);
129
+
130
+ const struct ggml_metal_device_props * props_dev = ggml_metal_device_get_props(dev);
131
+
132
+ snprintf(res->name, sizeof(res->name), "%s", props_dev->name);
133
+
134
+ res->d_queue = dispatch_queue_create("ggml-metal", DISPATCH_QUEUE_CONCURRENT);
135
+
136
+ res->use_fusion = getenv("GGML_METAL_FUSION_DISABLE") == nil;
137
+ res->use_concurrency = getenv("GGML_METAL_CONCURRENCY_DISABLE") == nil;
138
+
139
+ {
140
+ const char * val = getenv("GGML_METAL_GRAPH_DEBUG");
141
+ res->debug_graph = val ? atoi(val) : 0;
142
+ }
143
+
144
+ {
145
+ const char * val = getenv("GGML_METAL_FUSION_DEBUG");
146
+ res->debug_fusion = val ? atoi(val) : 0;
147
+ }
148
+
149
+ res->use_graph_optimize = true;
150
+
151
+ if (getenv("GGML_METAL_GRAPH_OPTIMIZE_DISABLE") != NULL) {
152
+ res->use_graph_optimize = false;
153
+ }
154
+
155
+ memset(res->fuse_cnt, 0, sizeof(res->fuse_cnt));
156
+
157
+ GGML_LOG_INFO("%s: use fusion = %s\n", __func__, res->use_fusion ? "true" : "false");
158
+ GGML_LOG_INFO("%s: use concurrency = %s\n", __func__, res->use_concurrency ? "true" : "false");
159
+ GGML_LOG_INFO("%s: use graph optimize = %s\n", __func__, res->use_graph_optimize ? "true" : "false");
160
+
161
+ res->capture_compute = 0;
162
+ res->capture_started = false;
163
+ res->capture_scope = nil;
164
+
165
+ {
166
+ const char * val = getenv("GGML_METAL_CAPTURE_COMPUTE");
167
+ if (val) {
168
+ res->capture_compute = atoi(val);
169
+ }
170
+ }
171
+
172
+ res->has_error = false;
173
+
174
+ res->gf = nil;
175
+ res->encode_async = nil;
176
+ for (int i = 0; i < GGML_METAL_MAX_COMMAND_BUFFERS; ++i) {
177
+ res->cmd_bufs[i].obj = nil;
178
+ }
179
+
180
+ res->cmd_bufs_ext = [[NSMutableArray alloc] init];
181
+
182
+ res->cmd_buf_last = nil;
183
+
184
+ res->pipelines_ext = ggml_metal_pipelines_init();
185
+
186
+ return res;
187
+ }
188
+
189
+ void ggml_metal_free(ggml_metal_t ctx) {
190
+ GGML_LOG_INFO("%s: deallocating\n", __func__);
191
+
192
+ for (int i = 0; i < GGML_METAL_MAX_COMMAND_BUFFERS; ++i) {
193
+ if (ctx->cmd_bufs[i].obj) {
194
+ [ctx->cmd_bufs[i].obj release];
195
+ }
196
+ }
197
+
198
+ for (int i = 0; i < (int) ctx->cmd_bufs_ext.count; ++i) {
199
+ if (ctx->cmd_bufs_ext[i]) {
200
+ [ctx->cmd_bufs_ext[i] release];
201
+ }
202
+ }
203
+
204
+ [ctx->cmd_bufs_ext removeAllObjects];
205
+ [ctx->cmd_bufs_ext release];
206
+
207
+ if (ctx->pipelines_ext) {
208
+ ggml_metal_pipelines_free(ctx->pipelines_ext);
209
+ ctx->pipelines_ext = nil;
210
+ }
211
+
212
+ if (ctx->debug_fusion > 0) {
213
+ GGML_LOG_DEBUG("%s: fusion stats:\n", __func__);
214
+ for (int i = 0; i < GGML_OP_COUNT; i++) {
215
+ if (ctx->fuse_cnt[i] == 0) {
216
+ continue;
217
+ }
218
+
219
+ // note: cannot use ggml_log here
220
+ GGML_LOG_DEBUG("%s: - %s: %" PRIu64 "\n", __func__, ggml_op_name((enum ggml_op) i), ctx->fuse_cnt[i]);
221
+ }
222
+ }
223
+
224
+ Block_release(ctx->encode_async);
225
+
226
+ //[ctx->queue release]; // [TAG_QUEUE_PER_BACKEND]
227
+
228
+ dispatch_release(ctx->d_queue);
229
+
230
+ ggml_metal_device_event_free(ctx->dev, ctx->ev_cpy);
231
+
232
+ free(ctx);
233
+ }
234
+
235
+ const char * ggml_metal_get_name(ggml_metal_t ctx) {
236
+ return ctx->name;
237
+ }
238
+
239
+ void ggml_metal_synchronize(ggml_metal_t ctx) {
240
+ // wait for any backend operations to finish
241
+ if (ctx->cmd_buf_last) {
242
+ [ctx->cmd_buf_last waitUntilCompleted];
243
+ ctx->cmd_buf_last = nil;
244
+ }
245
+
246
+ // check status of all command buffers
247
+ {
248
+ const int n_cb = ctx->n_cb;
249
+
250
+ for (int cb_idx = 0; cb_idx <= n_cb; ++cb_idx) {
251
+ id<MTLCommandBuffer> cmd_buf = ctx->cmd_bufs[cb_idx].obj;
252
+ if (!cmd_buf) {
253
+ continue;
254
+ }
255
+
256
+ MTLCommandBufferStatus status = [cmd_buf status];
257
+ if (status != MTLCommandBufferStatusCompleted) {
258
+ GGML_LOG_ERROR("%s: error: command buffer %d failed with status %d\n", __func__, cb_idx, (int) status);
259
+ if (status == MTLCommandBufferStatusError) {
260
+ GGML_LOG_ERROR("error: %s\n", [[cmd_buf error].localizedDescription UTF8String]);
261
+ }
262
+ ctx->has_error = true;
263
+ return;
264
+ }
265
+ }
266
+ }
267
+
268
+ // release any completed extra command buffers
269
+ if (ctx->cmd_bufs_ext.count > 0) {
270
+ for (size_t i = 0; i < ctx->cmd_bufs_ext.count; ++i) {
271
+ id<MTLCommandBuffer> cmd_buf = ctx->cmd_bufs_ext[i];
272
+
273
+ MTLCommandBufferStatus status = [cmd_buf status];
274
+ if (status != MTLCommandBufferStatusCompleted) {
275
+ GGML_LOG_ERROR("%s: error: command buffer %d failed with status %d\n", __func__, (int) i, (int) status);
276
+ if (status == MTLCommandBufferStatusError) {
277
+ GGML_LOG_ERROR("error: %s\n", [[cmd_buf error].localizedDescription UTF8String]);
278
+ }
279
+
280
+ // release this and all remaining command buffers before returning
281
+ for (size_t j = i; j < ctx->cmd_bufs_ext.count; ++j) {
282
+ [ctx->cmd_bufs_ext[j] release];
283
+ }
284
+ [ctx->cmd_bufs_ext removeAllObjects];
285
+
286
+ ctx->has_error = true;
287
+ return;
288
+ }
289
+
290
+ [cmd_buf release];
291
+ }
292
+
293
+ [ctx->cmd_bufs_ext removeAllObjects];
294
+ }
295
+ }
296
+
297
+ static struct ggml_metal_buffer_id ggml_metal_get_buffer_id(const struct ggml_tensor * t) {
298
+ if (!t) {
299
+ return (struct ggml_metal_buffer_id) { nil, 0 };
300
+ }
301
+
302
+ ggml_backend_buffer_t buffer = t->view_src ? t->view_src->buffer : t->buffer;
303
+
304
+ return ggml_metal_buffer_get_id(buffer->context, t);
305
+ }
306
+
307
+ void ggml_metal_set_tensor_async(ggml_metal_t ctx, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
308
+ @autoreleasepool {
309
+ // wrap the source data into a Metal buffer
310
+ id<MTLDevice> device = ggml_metal_device_get_obj(ctx->dev);
311
+ id<MTLBuffer> buf_src = [device newBufferWithBytes:data
312
+ length:size
313
+ options:MTLResourceStorageModeShared];
314
+
315
+ GGML_ASSERT(buf_src);
316
+
317
+ struct ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(tensor);
318
+ if (bid_dst.metal == nil) {
319
+ GGML_ABORT("%s: failed to find buffer for tensor '%s'\n", __func__, tensor->name);
320
+ }
321
+
322
+ bid_dst.offs += offset;
323
+
324
+ // queue the copy operation into the queue of the Metal context
325
+ // this will be queued at the end, after any currently ongoing GPU operations
326
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(ctx->dev);
327
+ id<MTLCommandBuffer> cmd_buf = [queue commandBuffer];
328
+ id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
329
+
330
+ [encoder copyFromBuffer:buf_src
331
+ sourceOffset:0
332
+ toBuffer:bid_dst.metal
333
+ destinationOffset:bid_dst.offs
334
+ size:size];
335
+
336
+ [encoder endEncoding];
337
+ [cmd_buf commit];
338
+ [buf_src release];
339
+
340
+ // do not wait here for completion
341
+ //[cmd_buf waitUntilCompleted];
342
+
343
+ // instead, remember a reference to the command buffer and wait for it later if needed
344
+ [ctx->cmd_bufs_ext addObject:cmd_buf];
345
+ ctx->cmd_buf_last = cmd_buf;
346
+
347
+ [cmd_buf retain];
348
+ }
349
+ }
350
+
351
+ void ggml_metal_get_tensor_async(ggml_metal_t ctx, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) {
352
+ @autoreleasepool {
353
+ id<MTLDevice> device = ggml_metal_device_get_obj(ctx->dev);
354
+ id<MTLBuffer> buf_dst = [device newBufferWithBytesNoCopy:data
355
+ length:size
356
+ options:MTLResourceStorageModeShared
357
+ deallocator:nil];
358
+
359
+ GGML_ASSERT(buf_dst);
360
+
361
+ struct ggml_metal_buffer_id bid_src = ggml_metal_get_buffer_id(tensor);
362
+ if (bid_src.metal == nil) {
363
+ GGML_ABORT("%s: failed to find buffer for tensor '%s'\n", __func__, tensor->name);
364
+ }
365
+
366
+ bid_src.offs += offset;
367
+
368
+ // queue the copy operation into the queue of the Metal context
369
+ // this will be queued at the end, after any currently ongoing GPU operations
370
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(ctx->dev);
371
+ id<MTLCommandBuffer> cmd_buf = [queue commandBuffer];
372
+ id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
373
+
374
+ [encoder copyFromBuffer:bid_src.metal
375
+ sourceOffset:bid_src.offs
376
+ toBuffer:buf_dst
377
+ destinationOffset:0
378
+ size:size];
379
+
380
+ [encoder endEncoding];
381
+ [cmd_buf commit];
382
+ [buf_dst release];
383
+
384
+ // do not wait here for completion
385
+ //[cmd_buf waitUntilCompleted];
386
+
387
+ // instead, remember a reference to the command buffer and wait for it later if needed
388
+ [ctx->cmd_bufs_ext addObject:cmd_buf];
389
+ ctx->cmd_buf_last = cmd_buf;
390
+
391
+ [cmd_buf retain];
392
+ }
393
+ }
394
+
395
+ bool ggml_metal_cpy_tensor_async(ggml_metal_t ctx_src, ggml_metal_t ctx_dst, const struct ggml_tensor * src, struct ggml_tensor * dst) {
396
+ @autoreleasepool {
397
+ struct ggml_metal_buffer_id bid_src = ggml_metal_get_buffer_id(src);
398
+ struct ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(dst);
399
+
400
+ if (bid_src.metal == nil || bid_dst.metal == nil) {
401
+ return false;
402
+ }
403
+
404
+ // queue the copy operation into the Metal context
405
+ // this will be queued at the end, after any currently ongoing GPU operations
406
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(ctx_src->dev);
407
+ id<MTLCommandBuffer> cmd_buf = [queue commandBuffer];
408
+ id<MTLBlitCommandEncoder> encoder = [cmd_buf blitCommandEncoder];
409
+
410
+ [encoder copyFromBuffer:bid_src.metal
411
+ sourceOffset:bid_src.offs
412
+ toBuffer:bid_dst.metal
413
+ destinationOffset:bid_dst.offs
414
+ size:ggml_nbytes(src)];
415
+
416
+ [encoder endEncoding];
417
+
418
+ ggml_metal_event_t ev_cpy = ggml_metal_get_ev_cpy(ctx_src);
419
+ ggml_metal_event_encode_signal(ev_cpy, cmd_buf);
420
+
421
+ [cmd_buf commit];
422
+
423
+ // do not wait here for completion
424
+ //[cmd_buf waitUntilCompleted];
425
+
426
+ // instead, remember a reference to the command buffer and wait for it later if needed
427
+ [ctx_src->cmd_bufs_ext addObject:cmd_buf];
428
+ ctx_src->cmd_buf_last = cmd_buf;
429
+
430
+ [cmd_buf retain];
431
+
432
+ ggml_metal_event_wait(ctx_dst, ev_cpy);
433
+
434
+ return true;
435
+ }
436
+ }
437
+
438
+ // CrispASR ICB-feasibility probe (§210): split each graph_compute into the
439
+ // host-side encode+commit time (what an MTLIndirectCommandBuffer replay would
440
+ // collapse to a single encode) vs the GPU execute+sync time (which ICB cannot
441
+ // remove). Gated by CRISPASR_METAL_PROFILE; when set, this forces a synchronous
442
+ // waitUntilCompleted after the encode so the two halves are cleanly attributable
443
+ // (it serializes the step — for measurement only, not a perf path).
444
+ static int g_crisp_metal_prof = -1; // -1 unread, 0 off, 1 on
445
+
446
+ enum ggml_status ggml_metal_graph_compute(ggml_metal_t ctx, struct ggml_cgraph * gf) {
447
+ if (ctx->has_error) {
448
+ GGML_LOG_ERROR("%s: backend is in error state from a previous command buffer failure - recreate the backend to recover\n", __func__);
449
+ return GGML_STATUS_FAILED;
450
+ }
451
+
452
+ if (g_crisp_metal_prof < 0) {
453
+ const char * e = getenv("CRISPASR_METAL_PROFILE");
454
+ g_crisp_metal_prof = (e && *e && *e != '0') ? 1 : 0;
455
+ }
456
+ const bool crisp_prof = g_crisp_metal_prof == 1;
457
+ const int64_t crisp_t0 = crisp_prof ? ggml_time_us() : 0;
458
+
459
+ // number of nodes encoded by the main thread (empirically determined)
460
+ const int n_main = MAX(64, 0.1*gf->n_nodes);
461
+
462
+ // number of threads in addition to the main thread
463
+ const int n_cb = ctx->n_cb;
464
+
465
+ // keep the memory wired
466
+ ggml_metal_device_rsets_keep_alive(ctx->dev);
467
+
468
+ // submit the ggml compute graph to the GPU by creating command buffers and encoding the ops in them
469
+ // the first n_nodes_0 are encoded and submitted for processing directly by the calling thread
470
+ // while these nodes are processing, we start n_cb threads to enqueue the rest of the nodes
471
+ // each thread creates it's own command buffer and enqueues the ops in parallel
472
+ //
473
+ // tests on M1 Pro and M2 Ultra using LLaMA models, show that optimal values for n_cb are 1 or 2
474
+
475
+ @autoreleasepool {
476
+ ctx->gf = gf;
477
+
478
+ ctx->n_nodes_0 = MIN(n_main, gf->n_nodes);
479
+ ctx->n_nodes_1 = gf->n_nodes - ctx->n_nodes_0;
480
+
481
+ ctx->n_nodes_per_cb = (ctx->n_nodes_1 + ctx->n_cb - 1) / ctx->n_cb;
482
+
483
+ if (ctx->capture_compute >= 0) {
484
+ ctx->capture_compute--;
485
+ }
486
+
487
+ const bool use_capture = ctx->capture_compute == 0;
488
+ if (use_capture) {
489
+ ctx->capture_compute = -1;
490
+
491
+ // make sure all previous computations have finished before starting the capture
492
+ if (ctx->cmd_buf_last) {
493
+ [ctx->cmd_buf_last waitUntilCompleted];
494
+ ctx->cmd_buf_last = nil;
495
+ }
496
+
497
+ if (!ctx->capture_started) {
498
+ NSString * path = [NSString stringWithFormat:@"/tmp/perf-metal-%d.gputrace", getpid()];
499
+
500
+ GGML_LOG_WARN("%s: capturing graph in %s\n", __func__, [path UTF8String]);
501
+
502
+ // create capture scope
503
+ id<MTLDevice> device = ggml_metal_device_get_obj(ctx->dev);
504
+ ctx->capture_scope = [[MTLCaptureManager sharedCaptureManager] newCaptureScopeWithDevice:device];
505
+
506
+ MTLCaptureDescriptor * descriptor = [MTLCaptureDescriptor new];
507
+ descriptor.captureObject = ctx->capture_scope;
508
+ descriptor.destination = MTLCaptureDestinationGPUTraceDocument;
509
+ descriptor.outputURL = [NSURL fileURLWithPath:path];
510
+
511
+ NSError * error = nil;
512
+ if (![[MTLCaptureManager sharedCaptureManager] startCaptureWithDescriptor:descriptor error:&error]) {
513
+ GGML_LOG_ERROR("%s: error: unable to start capture '%s'\n", __func__, [[error localizedDescription] UTF8String]);
514
+ } else {
515
+ [ctx->capture_scope beginScope];
516
+ ctx->capture_started = true;
517
+ }
518
+ }
519
+ }
520
+
521
+ // short-hand
522
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(ctx->dev);
523
+
524
+ // the main thread commits the first few commands immediately
525
+ // cmd_buf[n_cb]
526
+ {
527
+ id<MTLCommandBuffer> cmd_buf = [queue commandBufferWithUnretainedReferences];
528
+ [cmd_buf retain];
529
+
530
+ if (ctx->cmd_bufs[n_cb].obj) {
531
+ [ctx->cmd_bufs[n_cb].obj release];
532
+ }
533
+ ctx->cmd_bufs[n_cb].obj = cmd_buf;
534
+
535
+ [cmd_buf enqueue];
536
+
537
+ ctx->encode_async(n_cb);
538
+ }
539
+
540
+ // remember the command buffer for the next iteration
541
+ ctx->cmd_buf_last = ctx->cmd_bufs[n_cb].obj;
542
+
543
+ // prepare the rest of the command buffers asynchronously (optional)
544
+ // cmd_buf[0.. n_cb)
545
+ for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) {
546
+ id<MTLCommandBuffer> cmd_buf = [queue commandBufferWithUnretainedReferences];
547
+ [cmd_buf retain];
548
+
549
+ if (ctx->cmd_bufs[cb_idx].obj) {
550
+ [ctx->cmd_bufs[cb_idx].obj release];
551
+ }
552
+ ctx->cmd_bufs[cb_idx].obj = cmd_buf;
553
+
554
+ // always enqueue the first two command buffers
555
+ // enqueue all of the command buffers if we don't need to abort
556
+ if (cb_idx < 2 || ctx->abort_callback == NULL) {
557
+ [cmd_buf enqueue];
558
+
559
+ // update the pointer to the last queued command buffer
560
+ // this is needed to implement synchronize()
561
+ ctx->cmd_buf_last = cmd_buf;
562
+ }
563
+ }
564
+
565
+ dispatch_apply(n_cb, ctx->d_queue, ctx->encode_async);
566
+
567
+ // CrispASR ICB-feasibility probe (§210): attribute host-encode vs GPU.
568
+ // crisp_t1 = all ops encoded + all command buffers committed (this is the
569
+ // host work an ICB replay collapses). crisp_t2 = GPU finished. The split
570
+ // tells us whether the ~100 ms/step decode floor is host re-encode (ICB
571
+ // would remove it) or GPU execute+submit (ICB cannot).
572
+ if (crisp_prof) {
573
+ const int64_t crisp_t1 = ggml_time_us();
574
+ if (ctx->cmd_buf_last) {
575
+ [ctx->cmd_buf_last waitUntilCompleted];
576
+ }
577
+ const int64_t crisp_t2 = ggml_time_us();
578
+ fprintf(stderr, "[metal-prof] nodes=%4d n_cb=%d encode_us=%6lld gpu_us=%6lld total_us=%6lld\n",
579
+ gf->n_nodes, n_cb, (long long)(crisp_t1 - crisp_t0),
580
+ (long long)(crisp_t2 - crisp_t1), (long long)(crisp_t2 - crisp_t0));
581
+ }
582
+
583
+ // for debugging: block until graph is computed
584
+ //[ctx->cmd_buf_last waitUntilCompleted];
585
+
586
+ // enter here only when capturing in order to wait for all computation to finish
587
+ // otherwise, we leave the graph to compute asynchronously
588
+ if (use_capture && ctx->capture_started) {
589
+ // wait for completion and check status of each command buffer
590
+ // needed to detect if the device ran out-of-memory for example (#1881)
591
+ {
592
+ id<MTLCommandBuffer> cmd_buf = ctx->cmd_bufs[n_cb].obj;
593
+ [cmd_buf waitUntilCompleted];
594
+
595
+ MTLCommandBufferStatus status = [cmd_buf status];
596
+ if (status != MTLCommandBufferStatusCompleted) {
597
+ GGML_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, n_cb, status);
598
+ if (status == MTLCommandBufferStatusError) {
599
+ GGML_LOG_INFO("error: %s\n", [[cmd_buf error].localizedDescription UTF8String]);
600
+ }
601
+
602
+ return GGML_STATUS_FAILED;
603
+ }
604
+ }
605
+
606
+ for (int i = 0; i < n_cb; ++i) {
607
+ id<MTLCommandBuffer> cmd_buf = ctx->cmd_bufs[i].obj;
608
+ [cmd_buf waitUntilCompleted];
609
+
610
+ MTLCommandBufferStatus status = [cmd_buf status];
611
+ if (status != MTLCommandBufferStatusCompleted) {
612
+ GGML_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, i, status);
613
+ if (status == MTLCommandBufferStatusError) {
614
+ GGML_LOG_INFO("error: %s\n", [[cmd_buf error].localizedDescription UTF8String]);
615
+ }
616
+
617
+ return GGML_STATUS_FAILED;
618
+ }
619
+
620
+ id<MTLCommandBuffer> next_buffer = (i + 1 < n_cb ? ctx->cmd_bufs[i + 1].obj : nil);
621
+ if (!next_buffer) {
622
+ continue;
623
+ }
624
+
625
+ const bool next_queued = ([next_buffer status] != MTLCommandBufferStatusNotEnqueued);
626
+ if (next_queued) {
627
+ continue;
628
+ }
629
+
630
+ if (ctx->abort_callback && ctx->abort_callback(ctx->abort_callback_data)) {
631
+ GGML_LOG_INFO("%s: command buffer %d aborted", __func__, i);
632
+ return GGML_STATUS_ABORTED;
633
+ }
634
+
635
+ [next_buffer commit];
636
+ }
637
+
638
+ [ctx->capture_scope endScope];
639
+ [[MTLCaptureManager sharedCaptureManager] stopCapture];
640
+
641
+ ctx->capture_started = false;
642
+ }
643
+ }
644
+
645
+ return GGML_STATUS_SUCCESS;
646
+ }
647
+
648
+ void ggml_metal_graph_optimize(ggml_metal_t ctx, struct ggml_cgraph * gf) {
649
+ //const int64_t t_start = ggml_time_us();
650
+
651
+ if (ctx->use_graph_optimize) {
652
+ ggml_graph_optimize(gf);
653
+ }
654
+
655
+ //printf("%s: graph optimize took %.3f ms\n", __func__, (ggml_time_us() - t_start) / 1000.0);
656
+ }
657
+
658
+ void ggml_metal_event_record(ggml_metal_t ctx, ggml_metal_event_t ev) {
659
+ @autoreleasepool {
660
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(ctx->dev);
661
+ id<MTLCommandBuffer> cmd_buf = [queue commandBuffer];
662
+
663
+ ggml_metal_event_encode_signal(ev, cmd_buf);
664
+
665
+ [cmd_buf commit];
666
+
667
+ [ctx->cmd_bufs_ext addObject:cmd_buf];
668
+ ctx->cmd_buf_last = cmd_buf;
669
+
670
+ [cmd_buf retain];
671
+ }
672
+ }
673
+
674
+ void ggml_metal_event_wait(ggml_metal_t ctx, ggml_metal_event_t ev) {
675
+ @autoreleasepool {
676
+ id<MTLCommandQueue> queue = ggml_metal_device_get_queue(ctx->dev);
677
+ id<MTLCommandBuffer> cmd_buf = [queue commandBuffer];
678
+
679
+ ggml_metal_event_encode_wait(ev, cmd_buf);
680
+
681
+ [cmd_buf commit];
682
+
683
+ [ctx->cmd_bufs_ext addObject:cmd_buf];
684
+ ctx->cmd_buf_last = cmd_buf;
685
+
686
+ [cmd_buf retain];
687
+ }
688
+ }
689
+
690
+ ggml_metal_event_t ggml_metal_get_ev_cpy(ggml_metal_t ctx) {
691
+ return ctx->ev_cpy;
692
+ }
693
+
694
+ void ggml_metal_set_n_cb(ggml_metal_t ctx, int n_cb) {
695
+ // CrispASR debug (#83 r9 follow-up #5): override n_cb via env to test
696
+ // hypothesis that cross-cmd-buf concurrency violates mem_ranges tracking.
697
+ {
698
+ const char * env = getenv("CRISPASR_METAL_N_CB");
699
+ if (env && *env) {
700
+ n_cb = atoi(env);
701
+ }
702
+ }
703
+ if (ctx->n_cb != n_cb) {
704
+ ctx->n_cb = MIN(n_cb, GGML_METAL_MAX_COMMAND_BUFFERS);
705
+
706
+ if (ctx->n_cb > 2) {
707
+ GGML_LOG_WARN("%s: n_cb = %d, using n_cb > 2 is not recommended and can degrade the performance in some cases\n", __func__, n_cb);
708
+ }
709
+ }
710
+
711
+ if (ctx->encode_async) {
712
+ Block_release(ctx->encode_async);
713
+ }
714
+
715
+ ctx->encode_async = Block_copy(^(size_t iter) {
716
+ const int cb_idx = iter;
717
+ const int n_cb_l = ctx->n_cb;
718
+
719
+ const int n_nodes_0 = ctx->n_nodes_0;
720
+ const int n_nodes_1 = ctx->n_nodes_1;
721
+
722
+ const int n_nodes_per_cb = ctx->n_nodes_per_cb;
723
+
724
+ int idx_start = 0;
725
+ int idx_end = n_nodes_0;
726
+
727
+ if (cb_idx < n_cb_l) {
728
+ idx_start = n_nodes_0 + ( (cb_idx + 0) * n_nodes_per_cb);
729
+ idx_end = n_nodes_0 + (MIN((cb_idx == n_cb_l - 1) ? n_nodes_1 : (cb_idx + 1) * n_nodes_per_cb, n_nodes_1));
730
+ }
731
+
732
+ id<MTLCommandBuffer> cmd_buf = ctx->cmd_bufs[cb_idx].obj;
733
+
734
+ ggml_metal_op_t ctx_op = ggml_metal_op_init(
735
+ ctx->dev,
736
+ cmd_buf,
737
+ ctx->gf,
738
+ idx_start,
739
+ idx_end,
740
+ ctx->use_fusion,
741
+ ctx->use_concurrency,
742
+ ctx->capture_compute,
743
+ ctx->debug_graph,
744
+ ctx->debug_fusion);
745
+
746
+ for (int idx = 0; idx < ggml_metal_op_n_nodes(ctx_op); ++idx) {
747
+ const int res = ggml_metal_op_encode(ctx_op, idx);
748
+ if (res == 0) {
749
+ break;
750
+ }
751
+
752
+ idx += res - 1;
753
+ }
754
+
755
+ ggml_metal_op_free(ctx_op);
756
+
757
+ if (cb_idx < 2 || ctx->abort_callback == NULL) {
758
+ [cmd_buf commit];
759
+ }
760
+ });
761
+ }
762
+
763
+ void ggml_metal_set_abort_callback(ggml_metal_t ctx, ggml_abort_callback abort_callback, void * user_data) {
764
+ ctx->abort_callback = abort_callback;
765
+ ctx->abort_callback_data = user_data;
766
+ }
767
+
768
+ bool ggml_metal_supports_family(ggml_metal_t ctx, int family) {
769
+ GGML_ASSERT(ctx->dev != nil);
770
+
771
+ id<MTLDevice> device = ggml_metal_device_get_obj(ctx->dev);
772
+
773
+ return [device supportsFamily:(MTLGPUFamilyApple1 + family - 1)];
774
+ }
775
+
776
+ void ggml_metal_capture_next_compute(ggml_metal_t ctx) {
777
+ ctx->capture_compute = 1;
778
+ }