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,1606 @@
1
+ // Vectorized functions for fundamental operations
2
+
3
+ #pragma once
4
+
5
+ #include "ggml-impl.h"
6
+ #include "simd-mappings.h"
7
+ #include "ggml.h"
8
+ #include "ggml-cpu.h"
9
+
10
+ #if defined(GGML_USE_ACCELERATE)
11
+ #include <Accelerate/Accelerate.h>
12
+ #endif
13
+
14
+ // floating point type used to accumulate sums
15
+ typedef double ggml_float;
16
+
17
+ #define GGML_GELU_FP16
18
+ #define GGML_GELU_QUICK_FP16
19
+
20
+ #define GGML_SOFT_MAX_UNROLL 4
21
+ #define GGML_VEC_DOT_UNROLL 2
22
+ #define GGML_VEC_MAD_UNROLL 32
23
+
24
+ #ifdef __cplusplus
25
+ extern "C" {
26
+ #endif
27
+
28
+ //
29
+ // global data
30
+ //
31
+
32
+ // precomputed gelu table for f16 (128 KB)
33
+ extern ggml_fp16_t ggml_table_gelu_f16[1 << 16];
34
+
35
+ // precomputed quick gelu table for f16 (128 KB)
36
+ extern ggml_fp16_t ggml_table_gelu_quick_f16[1 << 16];
37
+
38
+ //
39
+ // fundamental operations
40
+ //
41
+
42
+ void ggml_vec_dot_f32(int n, float * GGML_RESTRICT s, size_t bs, const float * GGML_RESTRICT x, size_t bx, const float * GGML_RESTRICT y, size_t by, int nrc);
43
+ void ggml_vec_dot_bf16(int n, float * GGML_RESTRICT s, size_t bs, ggml_bf16_t * GGML_RESTRICT x, size_t bx, ggml_bf16_t * GGML_RESTRICT y, size_t by, int nrc);
44
+ void ggml_vec_dot_f16(int n, float * GGML_RESTRICT s, size_t bs, ggml_fp16_t * GGML_RESTRICT x, size_t bx, ggml_fp16_t * GGML_RESTRICT y, size_t by, int nrc);
45
+ // CrispASR patch (issue #38): F16 weight × F32 input → F32 dot, used by
46
+ // MUL_MAT when src0 is F16 and src1 is F32. Avoids saturating F32 → F16
47
+ // quantize on the input side. MUST RE-APPLY after every ggml bump.
48
+ void ggml_vec_dot_f16_f32(int n, float * GGML_RESTRICT s, size_t bs, ggml_fp16_t * GGML_RESTRICT x, size_t bx, float * GGML_RESTRICT y, size_t by, int nrc);
49
+
50
+ void ggml_vec_silu_f32(const int n, float * y, const float * x);
51
+ ggml_float ggml_vec_cvar_f32(const int n, float * y, const float * x, const float mean); //it will also center y ( y = y - mean )
52
+ ggml_float ggml_vec_soft_max_f32(const int n, float * y, const float * x, float max);
53
+ ggml_float ggml_vec_log_soft_max_f32(const int n, float * y, const float * x, float max);
54
+
55
+ inline static void ggml_vec_set_i8(const int n, int8_t * x, const int8_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
56
+ inline static void ggml_vec_set_i16(const int n, int16_t * x, const int16_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
57
+
58
+ inline static void ggml_vec_set_i32(const int n, int32_t * x, const int32_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
59
+ inline static void ggml_vec_cpy_i32(const int n, int32_t * y, const int32_t * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; }
60
+
61
+ inline static void ggml_vec_set_f16(const int n, ggml_fp16_t * x, const ggml_fp16_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
62
+ inline static void ggml_vec_set_bf16(const int n, ggml_bf16_t * x, const ggml_bf16_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
63
+
64
+ inline static void ggml_vec_add_f32 (const int n, float * z, const float * x, const float * y) {
65
+ int i = 0;
66
+ #if defined(__AVX2__)
67
+ for (; i + 7 < n; i += 8) {
68
+ __m256 vx = _mm256_loadu_ps(x + i);
69
+ __m256 vy = _mm256_loadu_ps(y + i);
70
+ __m256 vz = _mm256_add_ps(vx, vy);
71
+ _mm256_storeu_ps(z + i, vz);
72
+ }
73
+ #endif
74
+ for (; i < n; ++i) {
75
+ z[i] = x[i] + y[i];
76
+ }
77
+ }
78
+
79
+ inline static void ggml_vec_add_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
80
+ for (int i = 0; i < n; ++i) {
81
+ z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) + GGML_CPU_FP16_TO_FP32(y[i]));
82
+ }
83
+ }
84
+ inline static void ggml_vec_add1_f32(const int n, float * z, const float * x, const float v) { for (int i = 0; i < n; ++i) z[i] = x[i] + v; }
85
+ inline static void ggml_vec_acc_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] += x[i]; }
86
+ inline static void ggml_vec_acc1_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] += v; }
87
+ inline static void ggml_vec_sub_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i] - y[i]; }
88
+ inline static void ggml_vec_sub_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
89
+ for (int i = 0; i < n; ++i) {
90
+ z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) - GGML_CPU_FP16_TO_FP32(y[i]));
91
+ }
92
+ }
93
+ inline static void ggml_vec_set_f32 (const int n, float * x, const float v) { for (int i = 0; i < n; ++i) x[i] = v; }
94
+ inline static void ggml_vec_cpy_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; }
95
+ inline static void ggml_vec_neg_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = -x[i]; }
96
+ inline static void ggml_vec_neg_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
97
+ for (int i = 0; i < n; ++i) {
98
+ y[i] = GGML_CPU_FP32_TO_FP16(-GGML_CPU_FP16_TO_FP32(x[i]));
99
+ }
100
+ }
101
+
102
+ inline static void ggml_vec_mul_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]*y[i]; }
103
+ inline static void ggml_vec_mul_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
104
+ for (int i = 0; i < n; ++i) {
105
+ z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) * GGML_CPU_FP16_TO_FP32(y[i]));
106
+ }
107
+ }
108
+ inline static void ggml_vec_div_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]/y[i]; }
109
+ inline static void ggml_vec_div_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
110
+ for (int i = 0; i < n; ++i) {
111
+ z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) / GGML_CPU_FP16_TO_FP32(y[i]));
112
+ }
113
+ }
114
+
115
+ // compute GGML_VEC_DOT_UNROLL dot products at once
116
+ // xs - x row stride in bytes
117
+ inline static void ggml_vec_dot_f16_unroll(const int n, const int xs, float * GGML_RESTRICT s, void * GGML_RESTRICT xv, ggml_fp16_t * GGML_RESTRICT y) {
118
+ ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
119
+
120
+ ggml_fp16_t * GGML_RESTRICT x[GGML_VEC_DOT_UNROLL];
121
+
122
+ for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
123
+ x[i] = (ggml_fp16_t *) ((char *) xv + i*xs);
124
+ }
125
+
126
+ #if defined(GGML_SIMD)
127
+ #if defined(__ARM_FEATURE_SVE)
128
+
129
+ const int sve_register_length = svcntb() * 8;
130
+ const int ggml_f16_epr = sve_register_length / 16; // running when 16
131
+ const int ggml_f16_step = 8 * ggml_f16_epr; // choose 8 SVE registers
132
+
133
+ int np = (n & ~(ggml_f16_step - 1));
134
+
135
+ svfloat16_t sum_00 = svdup_n_f16(0.0f);
136
+ svfloat16_t sum_01 = svdup_n_f16(0.0f);
137
+ svfloat16_t sum_02 = svdup_n_f16(0.0f);
138
+ svfloat16_t sum_03 = svdup_n_f16(0.0f);
139
+
140
+ svfloat16_t sum_10 = svdup_n_f16(0.0f);
141
+ svfloat16_t sum_11 = svdup_n_f16(0.0f);
142
+ svfloat16_t sum_12 = svdup_n_f16(0.0f);
143
+ svfloat16_t sum_13 = svdup_n_f16(0.0f);
144
+
145
+ svfloat16_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8;
146
+ svfloat16_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8;
147
+
148
+ for (int i = 0; i < np; i += ggml_f16_step) {
149
+ ay1 = GGML_F16x_VEC_LOAD(y + i + 0 * ggml_f16_epr, 0); // 8 elements
150
+
151
+ ax1 = GGML_F16x_VEC_LOAD(x[0] + i + 0*ggml_f16_epr, 0); // 8 elements
152
+ sum_00 = GGML_F16x_VEC_FMA(sum_00, ax1, ay1); // sum_00 = sum_00+ax1*ay1
153
+ ax1 = GGML_F16x_VEC_LOAD(x[1] + i + 0*ggml_f16_epr, 0); // 8 elements
154
+ sum_10 = GGML_F16x_VEC_FMA(sum_10, ax1, ay1);
155
+
156
+ ay2 = GGML_F16x_VEC_LOAD(y + i + 1 * ggml_f16_epr, 1); // next 8 elements
157
+
158
+ ax2 = GGML_F16x_VEC_LOAD(x[0] + i + 1*ggml_f16_epr, 1); // next 8 elements
159
+ sum_01 = GGML_F16x_VEC_FMA(sum_01, ax2, ay2);
160
+ ax2 = GGML_F16x_VEC_LOAD(x[1] + i + 1*ggml_f16_epr, 1);
161
+ sum_11 = GGML_F16x_VEC_FMA(sum_11, ax2, ay2);
162
+
163
+ ay3 = GGML_F16x_VEC_LOAD(y + i + 2 * ggml_f16_epr, 2);
164
+
165
+ ax3 = GGML_F16x_VEC_LOAD(x[0] + i + 2*ggml_f16_epr, 2);
166
+ sum_02 = GGML_F16x_VEC_FMA(sum_02, ax3, ay3);
167
+ ax3 = GGML_F16x_VEC_LOAD(x[1] + i + 2*ggml_f16_epr, 2);
168
+ sum_12 = GGML_F16x_VEC_FMA(sum_12, ax3, ay3);
169
+
170
+ ay4 = GGML_F16x_VEC_LOAD(y + i + 3 * ggml_f16_epr, 3);
171
+
172
+ ax4 = GGML_F16x_VEC_LOAD(x[0] + i + 3*ggml_f16_epr, 3);
173
+ sum_03 = GGML_F16x_VEC_FMA(sum_03, ax4, ay4);
174
+ ax4 = GGML_F16x_VEC_LOAD(x[1] + i + 3*ggml_f16_epr, 3);
175
+ sum_13 = GGML_F16x_VEC_FMA(sum_13, ax4, ay4);
176
+
177
+ ay5 = GGML_F16x_VEC_LOAD(y + i + 4 * ggml_f16_epr, 4);
178
+
179
+ ax5 = GGML_F16x_VEC_LOAD(x[0] + i + 4*ggml_f16_epr, 4);
180
+
181
+ sum_00 = GGML_F16x_VEC_FMA(sum_00, ax5, ay5);
182
+ ax5 = GGML_F16x_VEC_LOAD(x[1] + i + 4*ggml_f16_epr, 4);
183
+ sum_10 = GGML_F16x_VEC_FMA(sum_10, ax5, ay5);
184
+
185
+ ay6 = GGML_F16x_VEC_LOAD(y + i + 5 * ggml_f16_epr, 5);
186
+
187
+ ax6 = GGML_F16x_VEC_LOAD(x[0] + i + 5*ggml_f16_epr, 5);
188
+
189
+ sum_01 = GGML_F16x_VEC_FMA(sum_01, ax6, ay6);
190
+ ax6 = GGML_F16x_VEC_LOAD(x[1] + i + 5*ggml_f16_epr, 5);
191
+ sum_11 = GGML_F16x_VEC_FMA(sum_11, ax6, ay6);
192
+
193
+ ay7 = GGML_F16x_VEC_LOAD(y + i + 6 * ggml_f16_epr, 6);
194
+
195
+ ax7 = GGML_F16x_VEC_LOAD(x[0] + i + 6*ggml_f16_epr, 6);
196
+
197
+ sum_02 = GGML_F16x_VEC_FMA(sum_02, ax7, ay7);
198
+ ax7 = GGML_F16x_VEC_LOAD(x[1] + i + 6*ggml_f16_epr, 6);
199
+ sum_12 = GGML_F16x_VEC_FMA(sum_12, ax7, ay7);
200
+
201
+ ay8 = GGML_F16x_VEC_LOAD(y + i + 7 * ggml_f16_epr, 7);
202
+
203
+ ax8 = GGML_F16x_VEC_LOAD(x[0] + i + 7*ggml_f16_epr, 7);
204
+
205
+ sum_03 = GGML_F16x_VEC_FMA(sum_03, ax8, ay8);
206
+ ax8 = GGML_F16x_VEC_LOAD(x[1] + i + 7*ggml_f16_epr, 7);
207
+ sum_13 = GGML_F16x_VEC_FMA(sum_13, ax8, ay8);
208
+ }
209
+
210
+ const int np2 = (n & ~(ggml_f16_epr - 1));
211
+ for (int k = np; k < np2; k += ggml_f16_epr) {
212
+ svfloat16_t ry = GGML_F16x_VEC_LOAD(y + k, 0);
213
+
214
+ svfloat16_t rx = GGML_F16x_VEC_LOAD(x[0] + k, 0);
215
+ sum_00 = GGML_F16x_VEC_FMA(sum_00, rx, ry);
216
+ rx = GGML_F16x_VEC_LOAD(x[1] + k, 0);
217
+ sum_10 = GGML_F16x_VEC_FMA(sum_10, rx, ry);
218
+ }
219
+
220
+ if (np2 < n) {
221
+ svbool_t pg = svwhilelt_b16(np2, n);
222
+ svfloat16_t hx_0 = svld1_f16(pg, (const __fp16 *)(x[0] + np2));
223
+ svfloat16_t hx_1 = svld1_f16(pg, (const __fp16 *)(x[1] + np2));
224
+ svfloat16_t hy = svld1_f16(pg, (const __fp16 *)(y + np2));
225
+
226
+ sum_00 = svmad_f16_x(pg, hx_0, hy, sum_00);
227
+ sum_10 = svmad_f16_x(pg, hx_1, hy, sum_10);
228
+ }
229
+ GGML_F16x_VEC_REDUCE(sumf[0], sum_00, sum_01, sum_02, sum_03);
230
+ GGML_F16x_VEC_REDUCE(sumf[1], sum_10, sum_11, sum_12, sum_13);
231
+ np = n;
232
+ #elif defined(__riscv_v_intrinsic)
233
+ #if defined(__riscv_zvfh)
234
+ size_t vl = __riscv_vsetvlmax_e32m4();
235
+
236
+ // initialize accumulators to all zeroes
237
+ vfloat32m4_t vsum0_0 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
238
+ vfloat32m4_t vsum0_1 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
239
+ vfloat32m4_t vsum1_0 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
240
+ vfloat32m4_t vsum1_1 = __riscv_vfmv_v_f_f32m4(0.0f, vl);
241
+
242
+ // calculate step size
243
+ const size_t epr = __riscv_vsetvlmax_e16m2();
244
+ const size_t step = epr * 2;
245
+ int np = (n & ~(step - 1));
246
+
247
+ // unroll by 2 along the row dimension
248
+ for (int i = 0; i < np; i += step) {
249
+ vfloat16m2_t ay0 = __riscv_vle16_v_f16m2((const _Float16 *)(y + i), epr);
250
+ vfloat16m2_t ax0_0 = __riscv_vle16_v_f16m2((const _Float16 *)(x[0] + i), epr);
251
+ vfloat16m2_t ax1_0 = __riscv_vle16_v_f16m2((const _Float16 *)(x[1] + i), epr);
252
+ vsum0_0 = __riscv_vfwmacc_vv_f32m4(vsum0_0, ax0_0, ay0, epr);
253
+ vsum1_0 = __riscv_vfwmacc_vv_f32m4(vsum1_0, ax1_0, ay0, epr);
254
+
255
+ vfloat16m2_t ay1 = __riscv_vle16_v_f16m2((const _Float16 *)(y + i + epr), epr);
256
+ vfloat16m2_t ax0_1 = __riscv_vle16_v_f16m2((const _Float16 *)(x[0] + i + epr), epr);
257
+ vfloat16m2_t ax1_1 = __riscv_vle16_v_f16m2((const _Float16 *)(x[1] + i + epr), epr);
258
+ vsum0_1 = __riscv_vfwmacc_vv_f32m4(vsum0_1, ax0_1, ay1, epr);
259
+ vsum1_1 = __riscv_vfwmacc_vv_f32m4(vsum1_1, ax1_1, ay1, epr);
260
+ }
261
+
262
+ vfloat32m4_t vsum0 = __riscv_vfadd_vv_f32m4(vsum0_0, vsum0_1, vl);
263
+ vfloat32m4_t vsum1 = __riscv_vfadd_vv_f32m4(vsum1_0, vsum1_1, vl);
264
+
265
+ // leftovers
266
+ for (int i = np; i < n; i += vl) {
267
+ vl = __riscv_vsetvl_e16m2(n - i);
268
+ vfloat16m2_t ay = __riscv_vle16_v_f16m2((const _Float16 *)(y + i), vl);
269
+ vfloat16m2_t ax0 = __riscv_vle16_v_f16m2((const _Float16 *)(x[0] + i), vl);
270
+ vfloat16m2_t ax1 = __riscv_vle16_v_f16m2((const _Float16 *)(x[1] + i), vl);
271
+
272
+ vsum0 = __riscv_vfwmacc_vv_f32m4(vsum0, ax0, ay, vl);
273
+ vsum1 = __riscv_vfwmacc_vv_f32m4(vsum1, ax1, ay, vl);
274
+ }
275
+
276
+ // reduce
277
+ vl = __riscv_vsetvlmax_e32m2();
278
+ vfloat32m2_t acc0_0 = __riscv_vfadd_vv_f32m2(__riscv_vget_v_f32m4_f32m2(vsum0, 0),
279
+ __riscv_vget_v_f32m4_f32m2(vsum0, 1), vl);
280
+ vl = __riscv_vsetvlmax_e32m1();
281
+ vfloat32m1_t acc0_1 = __riscv_vfadd_vv_f32m1(__riscv_vget_v_f32m2_f32m1(acc0_0, 0),
282
+ __riscv_vget_v_f32m2_f32m1(acc0_0, 1), vl);
283
+ vfloat32m1_t redsum0 = __riscv_vfredusum_vs_f32m1_f32m1(
284
+ acc0_1, __riscv_vfmv_v_f_f32m1(0.0f, 1), vl);
285
+
286
+ vl = __riscv_vsetvlmax_e32m2();
287
+ vfloat32m2_t acc1_0 = __riscv_vfadd_vv_f32m2(__riscv_vget_v_f32m4_f32m2(vsum1, 0),
288
+ __riscv_vget_v_f32m4_f32m2(vsum1, 1), vl);
289
+ vl = __riscv_vsetvlmax_e32m1();
290
+ vfloat32m1_t acc1_1 = __riscv_vfadd_vv_f32m1(__riscv_vget_v_f32m2_f32m1(acc1_0, 0),
291
+ __riscv_vget_v_f32m2_f32m1(acc1_0, 1), vl);
292
+ vfloat32m1_t redsum1 = __riscv_vfredusum_vs_f32m1_f32m1(
293
+ acc1_1, __riscv_vfmv_v_f_f32m1(0.0f, 1), vl);
294
+ sumf[0] = __riscv_vfmv_f_s_f32m1_f32(redsum0);
295
+ sumf[1] = __riscv_vfmv_f_s_f32m1_f32(redsum1);
296
+ np = n;
297
+ #else
298
+ const int np = 0;
299
+ #endif
300
+ #else
301
+ const int np = (n & ~(GGML_F16_STEP - 1));
302
+
303
+ GGML_F16_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_ARR] = { { GGML_F16_VEC_ZERO } };
304
+
305
+ GGML_F16_VEC ax[GGML_F16_ARR];
306
+ GGML_F16_VEC ay[GGML_F16_ARR];
307
+
308
+ for (int i = 0; i < np; i += GGML_F16_STEP) {
309
+ for (int j = 0; j < GGML_F16_ARR; j++) {
310
+ ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
311
+
312
+ for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
313
+ ax[j] = GGML_F16_VEC_LOAD(x[k] + i + j*GGML_F16_EPR, j);
314
+
315
+ sum[k][j] = GGML_F16_VEC_FMA(sum[k][j], ax[j], ay[j]);
316
+ }
317
+ }
318
+ }
319
+
320
+ // reduce sum0..sum3 to sum0
321
+ for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
322
+ GGML_F16_VEC_REDUCE(sumf[k], sum[k]);
323
+ }
324
+ #endif
325
+ #else
326
+ // scalar path
327
+ const int np = 0;
328
+ #endif
329
+ // scalar and leftovers
330
+ for (int i = np; i < n; ++i) {
331
+ for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
332
+ sumf[j] += (ggml_float)(GGML_CPU_FP16_TO_FP32(x[j][i])*GGML_CPU_FP16_TO_FP32(y[i]));
333
+ }
334
+ }
335
+
336
+ for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
337
+ s[i] = (float)sumf[i];
338
+ }
339
+ }
340
+
341
+ inline static void ggml_vec_mad_f32(const int n, float * GGML_RESTRICT y, const float * GGML_RESTRICT x, const float v) {
342
+ #if defined(GGML_SIMD)
343
+ #if defined(__ARM_FEATURE_SVE)
344
+
345
+ const int sve_register_length = ggml_cpu_get_sve_cnt() * 8;
346
+ const int ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16
347
+ const int ggml_f32_step = 8 * ggml_f32_epr; // choose 8 SVE registers
348
+ GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
349
+
350
+ const int np = (n & ~(ggml_f32_step - 1));
351
+ svfloat32_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8;
352
+ svfloat32_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8;
353
+ for (int i = 0; i < np; i += ggml_f32_step) {
354
+
355
+ ax1 = GGML_F32_VEC_LOAD(x + i);
356
+ ay1 = GGML_F32_VEC_LOAD(y + i);
357
+ ay1 = GGML_F32_VEC_FMA(ay1, ax1, vx);
358
+
359
+ GGML_F32_VEC_STORE(y + i, ay1);
360
+
361
+ ax2 = GGML_F32_VEC_LOAD(x + i + 1*ggml_f32_epr);
362
+ ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr);
363
+ ay2 = GGML_F32_VEC_FMA(ay2, ax2, vx);
364
+
365
+ GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2);
366
+
367
+ ax3 = GGML_F32_VEC_LOAD(x + i + 2*ggml_f32_epr);
368
+ ay3 = GGML_F32_VEC_LOAD(y + i + 2*ggml_f32_epr);
369
+ ay3 = GGML_F32_VEC_FMA(ay3, ax3, vx);
370
+
371
+ GGML_F32_VEC_STORE(y + i + 2*ggml_f32_epr, ay3);
372
+
373
+ ax4 = GGML_F32_VEC_LOAD(x + i + 3*ggml_f32_epr);
374
+ ay4 = GGML_F32_VEC_LOAD(y + i + 3*ggml_f32_epr);
375
+ ay4 = GGML_F32_VEC_FMA(ay4, ax4, vx);
376
+
377
+ GGML_F32_VEC_STORE(y + i + 3*ggml_f32_epr, ay4);
378
+
379
+ ax5 = GGML_F32_VEC_LOAD(x + i + 4*ggml_f32_epr);
380
+ ay5 = GGML_F32_VEC_LOAD(y + i + 4*ggml_f32_epr);
381
+ ay5 = GGML_F32_VEC_FMA(ay5, ax5, vx);
382
+
383
+ GGML_F32_VEC_STORE(y + i + 4*ggml_f32_epr, ay5);
384
+
385
+ ax6 = GGML_F32_VEC_LOAD(x + i + 5*ggml_f32_epr);
386
+ ay6 = GGML_F32_VEC_LOAD(y + i + 5*ggml_f32_epr);
387
+ ay6 = GGML_F32_VEC_FMA(ay6, ax6, vx);
388
+
389
+ GGML_F32_VEC_STORE(y + i + 5*ggml_f32_epr, ay6);
390
+
391
+ ax7 = GGML_F32_VEC_LOAD(x + i + 6*ggml_f32_epr);
392
+ ay7 = GGML_F32_VEC_LOAD(y + i + 6*ggml_f32_epr);
393
+ ay7 = GGML_F32_VEC_FMA(ay7, ax7, vx);
394
+
395
+ GGML_F32_VEC_STORE(y + i + 6*ggml_f32_epr, ay7);
396
+
397
+ ax8 = GGML_F32_VEC_LOAD(x + i + 7*ggml_f32_epr);
398
+ ay8 = GGML_F32_VEC_LOAD(y + i + 7*ggml_f32_epr);
399
+ ay8 = GGML_F32_VEC_FMA(ay8, ax8, vx);
400
+
401
+ GGML_F32_VEC_STORE(y + i + 7*ggml_f32_epr, ay8);
402
+ }
403
+ // leftovers
404
+ // Since 8 unrolls are done in above loop, leftovers lie in range [0, ggml_f32_step] which is handled in below loop
405
+ const int np2 = (n & ~(ggml_f32_epr - 1));
406
+ for (int i = np; i < np2; i += ggml_f32_epr) {
407
+ ax1 = GGML_F32_VEC_LOAD(x + i);
408
+ ay1 = GGML_F32_VEC_LOAD(y + i);
409
+ ay1 = GGML_F32_VEC_FMA(ay1, ax1, vx);
410
+
411
+ GGML_F32_VEC_STORE(y + i, ay1);
412
+ }
413
+ // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only
414
+ if (np2 < n) {
415
+ svbool_t pg =svwhilelt_b32(np2, n);
416
+ ax1 = svld1_f32(pg, x + np2);
417
+ ay1 = svld1_f32(pg, y + np2);
418
+ ay1 = svmad_f32_m(pg, ax1, vx, ay1);
419
+
420
+ svst1_f32(pg, y + np2, ay1);
421
+ }
422
+ #elif defined(__riscv_v_intrinsic)
423
+ for (int i = 0, avl; i < n; i += avl) {
424
+ avl = __riscv_vsetvl_e32m8(n - i);
425
+ vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[i], avl);
426
+ vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl);
427
+ vfloat32m8_t ny = __riscv_vfmadd_vf_f32m8(ax, v, ay, avl);
428
+ __riscv_vse32_v_f32m8(&y[i], ny, avl);
429
+ }
430
+ #else
431
+ const int np = (n & ~(GGML_F32_STEP - 1));
432
+
433
+ GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
434
+
435
+ GGML_F32_VEC ax[GGML_F32_ARR];
436
+ GGML_F32_VEC ay[GGML_F32_ARR];
437
+
438
+ for (int i = 0; i < np; i += GGML_F32_STEP) {
439
+ for (int j = 0; j < GGML_F32_ARR; j++) {
440
+ ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
441
+ ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
442
+ ay[j] = GGML_F32_VEC_FMA(ay[j], ax[j], vx);
443
+
444
+ GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
445
+ }
446
+ }
447
+
448
+ // leftovers
449
+ for (int i = np; i < n; ++i) {
450
+ y[i] += x[i]*v;
451
+ }
452
+ #endif
453
+ #else
454
+ // scalar
455
+ for (int i = 0; i < n; ++i) {
456
+ y[i] += x[i]*v;
457
+ }
458
+ #endif
459
+ }
460
+
461
+ inline static void ggml_vec_mad_f16(const int n, ggml_fp16_t * GGML_RESTRICT y, const ggml_fp16_t * GGML_RESTRICT x, const float v) {
462
+ #if defined(GGML_SIMD) && defined(__ARM_FEATURE_SVE)
463
+ const int sve_register_length = svcntb() * 8;
464
+ const int ggml_f16_epr = sve_register_length / 16;
465
+ const int ggml_f16_step = 8 * ggml_f16_epr;
466
+
467
+ GGML_F16x_VEC vx = GGML_F16x_VEC_SET1(v);
468
+
469
+ int np = (n & ~(ggml_f16_step - 1));
470
+
471
+ svfloat16_t ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8;
472
+ svfloat16_t ay1, ay2, ay3, ay4, ay5, ay6, ay7, ay8;
473
+ for (int i = 0; i < np; i += ggml_f16_step) {
474
+ ax1 = GGML_F16x_VEC_LOAD(x + i + 0 * ggml_f16_epr, 0);
475
+ ay1 = GGML_F16x_VEC_LOAD(y + i + 0 * ggml_f16_epr, 0);
476
+ ay1 = GGML_F16x_VEC_FMA(ay1, ax1, vx);
477
+
478
+ GGML_F16x_VEC_STORE(y + i + 0 * ggml_f16_epr, ay1, 0);
479
+
480
+ ax2 = GGML_F16x_VEC_LOAD(x + i + 1 * ggml_f16_epr, 1);
481
+ ay2 = GGML_F16x_VEC_LOAD(y + i + 1 * ggml_f16_epr, 1);
482
+ ay2 = GGML_F16x_VEC_FMA(ay2, ax2, vx);
483
+
484
+ GGML_F16x_VEC_STORE(y + i + 1 * ggml_f16_epr, ay2, 1);
485
+
486
+ ax3 = GGML_F16x_VEC_LOAD(x + i + 2 * ggml_f16_epr, 2);
487
+ ay3 = GGML_F16x_VEC_LOAD(y + i + 2 * ggml_f16_epr, 2);
488
+ ay3 = GGML_F16x_VEC_FMA(ay3, ax3, vx);
489
+
490
+ GGML_F16x_VEC_STORE(y + i + 2 * ggml_f16_epr, ay3, 2);
491
+
492
+ ax4 = GGML_F16x_VEC_LOAD(x + i + 3 * ggml_f16_epr, 3);
493
+ ay4 = GGML_F16x_VEC_LOAD(y + i + 3 * ggml_f16_epr, 3);
494
+ ay4 = GGML_F16x_VEC_FMA(ay4, ax4, vx);
495
+
496
+ GGML_F16x_VEC_STORE(y + i + 3 * ggml_f16_epr, ay4, 3);
497
+
498
+ ax5 = GGML_F16x_VEC_LOAD(x + i + 4 * ggml_f16_epr, 4);
499
+ ay5 = GGML_F16x_VEC_LOAD(y + i + 4 * ggml_f16_epr, 4);
500
+ ay5 = GGML_F16x_VEC_FMA(ay5, ax5, vx);
501
+
502
+ GGML_F16x_VEC_STORE(y + i + 4 * ggml_f16_epr, ay5, 4);
503
+
504
+ ax6 = GGML_F16x_VEC_LOAD(x + i + 5 * ggml_f16_epr, 5);
505
+ ay6 = GGML_F16x_VEC_LOAD(y + i + 5 * ggml_f16_epr, 5);
506
+ ay6 = GGML_F16x_VEC_FMA(ay6, ax6, vx);
507
+
508
+ GGML_F16x_VEC_STORE(y + i + 5 * ggml_f16_epr, ay6, 5);
509
+
510
+ ax7 = GGML_F16x_VEC_LOAD(x + i + 6 * ggml_f16_epr, 6);
511
+ ay7 = GGML_F16x_VEC_LOAD(y + i + 6 * ggml_f16_epr, 6);
512
+ ay7 = GGML_F16x_VEC_FMA(ay7, ax7, vx);
513
+
514
+ GGML_F16x_VEC_STORE(y + i + 6 * ggml_f16_epr, ay7, 6);
515
+
516
+ ax8 = GGML_F16x_VEC_LOAD(x + i + 7 * ggml_f16_epr, 7);
517
+ ay8 = GGML_F16x_VEC_LOAD(y + i + 7 * ggml_f16_epr, 7);
518
+ ay8 = GGML_F16x_VEC_FMA(ay8, ax8, vx);
519
+
520
+ GGML_F16x_VEC_STORE(y + i + 7 * ggml_f16_epr, ay8, 7);
521
+ }
522
+ const int np2 = (n & ~(ggml_f16_epr - 1));
523
+ for (int k = np; k < np2; k += ggml_f16_epr) {
524
+ svfloat16_t rx = GGML_F16x_VEC_LOAD(x + k, 0);
525
+ svfloat16_t ry = GGML_F16x_VEC_LOAD(y + k, 0);
526
+ ry = GGML_F16x_VEC_FMA(ry, rx, vx);
527
+
528
+ GGML_F16x_VEC_STORE(y + k, ry, 0);
529
+ }
530
+
531
+ if (np2 < n) {
532
+ svbool_t pg = svwhilelt_b16(np2, n);
533
+ svfloat16_t hx = svld1_f16(pg, (const __fp16 *)(x + np2));
534
+ svfloat16_t hy = svld1_f16(pg, (const __fp16 *)(y + np2));
535
+ hy = svmad_f16_x(pg, hx, vx, hy);
536
+ svst1_f16(pg, (__fp16 *)(y + np2), hy);
537
+ }
538
+ np = n;
539
+ #elif defined(__riscv_v_intrinsic) // implies __riscv_v_intrinsic
540
+ #if defined (__riscv_zvfh)
541
+ const ggml_fp16_t s = GGML_CPU_FP32_TO_FP16(v);
542
+ const _Float16 scale = *(const _Float16*)(&s);
543
+
544
+ // calculate step size
545
+ const int epr = __riscv_vsetvlmax_e16m4();
546
+ const int step = epr * 2;
547
+ int np = (n & ~(step - 1));
548
+
549
+ // unroll by 2
550
+ for (int i = 0; i < np; i += step) {
551
+ vfloat16m4_t ax0 = __riscv_vle16_v_f16m4((const _Float16*)x + i, epr);
552
+ vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, epr);
553
+ ay0 = __riscv_vfmacc_vf_f16m4(ay0, scale, ax0, epr);
554
+ __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, epr);
555
+ __asm__ __volatile__ ("" ::: "memory");
556
+
557
+ vfloat16m4_t ax1 = __riscv_vle16_v_f16m4((const _Float16*)x + i + epr, epr);
558
+ vfloat16m4_t ay1 = __riscv_vle16_v_f16m4((const _Float16*)y + i + epr, epr);
559
+ ay1 = __riscv_vfmacc_vf_f16m4(ay1, scale, ax1, epr);
560
+ __riscv_vse16_v_f16m4((_Float16*)y + i + epr, ay1, epr);
561
+ __asm__ __volatile__ ("" ::: "memory");
562
+ }
563
+
564
+ // leftovers
565
+ int vl;
566
+ for (int i = np; i < n; i += vl) {
567
+ vl = __riscv_vsetvl_e16m4(n - i);
568
+ vfloat16m4_t ax0 = __riscv_vle16_v_f16m4((const _Float16*)x + i, vl);
569
+ vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, vl);
570
+ ay0 = __riscv_vfmacc_vf_f16m4(ay0, scale, ax0, vl);
571
+ __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, vl);
572
+ }
573
+ np = n;
574
+ #else
575
+ // fall to scalar path
576
+ const int np = 0;
577
+ #endif
578
+ #elif defined(GGML_SIMD)
579
+ const int np = (n & ~(GGML_F16_STEP - 1));
580
+
581
+ GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
582
+
583
+ GGML_F16_VEC ax[GGML_F16_ARR];
584
+ GGML_F16_VEC ay[GGML_F16_ARR];
585
+
586
+ for (int i = 0; i < np; i += GGML_F16_STEP) {
587
+ for (int j = 0; j < GGML_F16_ARR; j++) {
588
+ ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
589
+ ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
590
+ ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx);
591
+
592
+ GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
593
+ }
594
+ }
595
+ #else
596
+ // scalar path
597
+ const int np = 0;
598
+ #endif
599
+
600
+ // scalar and leftovers
601
+ for (int i = np; i < n; ++i) {
602
+ y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i]) + GGML_CPU_FP16_TO_FP32(x[i])*v);
603
+ }
604
+ }
605
+
606
+ // xs and vs are byte strides of x and v
607
+ inline static void ggml_vec_mad_f32_unroll(const int n, const int xs, const int vs, float * GGML_RESTRICT y, const float * GGML_RESTRICT xv, const float * GGML_RESTRICT vv) {
608
+
609
+ const float * GGML_RESTRICT x[GGML_VEC_MAD_UNROLL];
610
+ const float * GGML_RESTRICT v[GGML_VEC_MAD_UNROLL];
611
+
612
+ for (int i = 0; i < GGML_VEC_MAD_UNROLL; ++i) {
613
+ x[i] = (const float *) ((const char *) xv + i*xs);
614
+ v[i] = (const float *) ((const char *) vv + i*vs);
615
+ }
616
+
617
+ #if defined(GGML_SIMD)
618
+ #if defined(__ARM_FEATURE_SVE)
619
+ // scalar Route to scalar implementation //TODO: Write SVE code
620
+ for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
621
+ for (int i = 0; i < n; ++i) {
622
+ y[i] += x[k][i]*v[k][0];
623
+ }
624
+ }
625
+ #elif defined(__riscv_v_intrinsic)
626
+ for (int i = 0, avl; i < n; i += avl) {
627
+ avl = __riscv_vsetvl_e32m8(n - i);
628
+ vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl);
629
+ for (int k = 0; k < GGML_VEC_MAD_UNROLL; k++) {
630
+ vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[k][i], avl);
631
+ ay = __riscv_vfmadd_vf_f32m8(ax, v[k][0], ay, avl);
632
+ }
633
+ __riscv_vse32_v_f32m8(&y[i], ay, avl);
634
+ }
635
+ #else
636
+ const int np = (n & ~(GGML_F32_STEP - 1));
637
+
638
+ GGML_F32_VEC vx[GGML_VEC_MAD_UNROLL];
639
+
640
+ for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
641
+ vx[k] = GGML_F32_VEC_SET1(v[k][0]);
642
+ }
643
+
644
+ GGML_F32_VEC ax[GGML_VEC_MAD_UNROLL][GGML_F32_ARR];
645
+ GGML_F32_VEC ay[GGML_F32_ARR];
646
+
647
+ for (int i = 0; i < np; i += GGML_F32_STEP) {
648
+ for (int j = 0; j < GGML_F32_ARR; j++) {
649
+ ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
650
+
651
+ for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
652
+ ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR);
653
+ ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]);
654
+ }
655
+
656
+ GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
657
+ }
658
+ }
659
+
660
+ // leftovers
661
+ for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
662
+ for (int i = np; i < n; ++i) {
663
+ y[i] += x[k][i]*v[k][0];
664
+ }
665
+ }
666
+ #endif
667
+ #else
668
+ // scalar
669
+ for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
670
+ for (int i = 0; i < n; ++i) {
671
+ y[i] += x[k][i]*v[k][0];
672
+ }
673
+ }
674
+ #endif
675
+ }
676
+
677
+ inline static void ggml_vec_mad1_f32(const int n, float * y, const float * x, const float s, const float b) {
678
+ #if defined(GGML_USE_ACCELERATE)
679
+ vDSP_vsmsa(x, 1, &s, &b, y, 1, n);
680
+ #elif defined(GGML_SIMD)
681
+ #if defined(__ARM_FEATURE_SVE)
682
+ // scalar ; TODO: Write SVE code
683
+ for (int i = 0; i < n; ++i) {
684
+ y[i] = x[i]*s + b;
685
+ }
686
+ #elif defined(__riscv_v_intrinsic)
687
+ for (int i = 0, avl; i < n; i += avl) {
688
+ avl = __riscv_vsetvl_e32m8(n - i);
689
+ vfloat32m8_t ax = __riscv_vle32_v_f32m8(&x[i], avl);
690
+ vfloat32m8_t vb = __riscv_vfmv_v_f_f32m8(b, avl);
691
+ vfloat32m8_t ny = __riscv_vfmadd_vf_f32m8(ax, s, vb, avl);
692
+ __riscv_vse32_v_f32m8(&y[i], ny, avl);
693
+ }
694
+ #else
695
+ const int np = (n & ~(GGML_F32_STEP - 1));
696
+
697
+ GGML_F32_VEC vs = GGML_F32_VEC_SET1(s);
698
+ GGML_F32_VEC vb = GGML_F32_VEC_SET1(b);
699
+
700
+ GGML_F32_VEC ay[GGML_F32_ARR];
701
+
702
+ for (int i = 0; i < np; i += GGML_F32_STEP) {
703
+ for (int j = 0; j < GGML_F32_ARR; j++) {
704
+ ay[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
705
+ ay[j] = GGML_F32_VEC_FMA(vb, ay[j], vs);
706
+
707
+ GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
708
+ }
709
+ }
710
+
711
+ // leftovers
712
+ for (int i = np; i < n; ++i) {
713
+ y[i] = x[i]*s + b;
714
+ }
715
+ #endif
716
+ #else
717
+ // scalar
718
+ for (int i = 0; i < n; ++i) {
719
+ y[i] = x[i]*s + b;
720
+ }
721
+ #endif
722
+ }
723
+
724
+ //inline static void ggml_vec_scale_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] *= v; }
725
+ inline static void ggml_vec_scale_f32(const int n, float * y, const float v) {
726
+ #if defined(GGML_USE_ACCELERATE)
727
+ vDSP_vsmul(y, 1, &v, y, 1, n);
728
+ #elif defined(GGML_SIMD)
729
+ #if defined(__ARM_FEATURE_SVE)
730
+ const int sve_register_length = ggml_cpu_get_sve_cnt() * 8;
731
+ const int ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16
732
+ const int ggml_f32_step = 2 * ggml_f32_epr;
733
+
734
+ GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
735
+ const int np = (n & ~(ggml_f32_step - 1));
736
+ svfloat32_t ay1;
737
+ svfloat32_t ay2;
738
+ for (int i = 0; i < np; i += ggml_f32_step) {
739
+ ay1 = GGML_F32_VEC_LOAD(y + i);
740
+ ay1 = GGML_F32_VEC_MUL(ay1, vx);
741
+ GGML_F32_VEC_STORE(y + i, ay1);
742
+
743
+ ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr);
744
+ ay2 = GGML_F32_VEC_MUL(ay2, vx);
745
+ GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2);
746
+ }
747
+ // leftovers
748
+ // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only
749
+ for (int i = np; i < n; i += ggml_f32_epr) {
750
+ svbool_t pg = svwhilelt_b32(i, n);
751
+ ay1 = svld1_f32(pg, y + i);
752
+ ay1 = svmul_f32_m(pg, ay1, vx);
753
+ svst1_f32(pg, y + i, ay1);
754
+ }
755
+ #elif defined(__riscv_v_intrinsic)
756
+ for (int i = 0, avl; i < n; i += avl) {
757
+ avl = __riscv_vsetvl_e32m8(n - i);
758
+ vfloat32m8_t ay = __riscv_vle32_v_f32m8(&y[i], avl);
759
+ vfloat32m8_t ny = __riscv_vfmul_vf_f32m8(ay, v, avl);
760
+ __riscv_vse32_v_f32m8(&y[i], ny, avl);
761
+ }
762
+ #else
763
+ const int np = (n & ~(GGML_F32_STEP - 1));
764
+
765
+ GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
766
+
767
+ GGML_F32_VEC ay[GGML_F32_ARR];
768
+
769
+ for (int i = 0; i < np; i += GGML_F32_STEP) {
770
+ for (int j = 0; j < GGML_F32_ARR; j++) {
771
+ ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
772
+ ay[j] = GGML_F32_VEC_MUL(ay[j], vx);
773
+
774
+ GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
775
+ }
776
+ }
777
+
778
+ // leftovers
779
+ for (int i = np; i < n; ++i) {
780
+ y[i] *= v;
781
+ }
782
+ #endif
783
+ #else
784
+ // scalar
785
+ for (int i = 0; i < n; ++i) {
786
+ y[i] *= v;
787
+ }
788
+ #endif
789
+ }
790
+
791
+ inline static void ggml_vec_scale_f16(const int n, ggml_fp16_t * y, const float v) {
792
+ #if defined(GGML_SIMD) && defined(__ARM_FEATURE_SVE)
793
+ const int sve_register_length = svcntb() * 8;
794
+ const int ggml_f16_epr = sve_register_length / 16;
795
+ const int ggml_f16_step = 2 * ggml_f16_epr;
796
+
797
+ GGML_F16x_VEC vx = GGML_F16x_VEC_SET1(v);
798
+ int np = (n & ~(ggml_f16_step - 1));
799
+ svfloat16_t ay1, ay2;
800
+
801
+ for (int i = 0; i < np; i += ggml_f16_step) {
802
+ ay1 = GGML_F16x_VEC_LOAD(y + i + 0*ggml_f16_epr, 0);
803
+ ay1 = GGML_F16x_VEC_MUL(ay1, vx);
804
+ GGML_F16x_VEC_STORE(y + i + 0*ggml_f16_epr, ay1, 0);
805
+
806
+ ay2 = GGML_F16x_VEC_LOAD(y + i + 1*ggml_f16_epr, 1);
807
+ ay2 = GGML_F16x_VEC_MUL(ay2, vx);
808
+ GGML_F16x_VEC_STORE(y + i + 1*ggml_f16_epr, ay2, 1);
809
+ }
810
+ // leftovers
811
+ // maximum number of leftover elements will be less that ggmlF_16x_epr. Apply predicated svmad on available elements only
812
+ if (np < n) {
813
+ svbool_t pg = svwhilelt_b16(np, n);
814
+ svfloat16_t hy = svld1_f16(pg, (__fp16 *)(y + np));
815
+ svfloat16_t out = svmul_f16_m(pg, hy, vx);
816
+ svst1_f16(pg, (__fp16 *)(y + np), out);
817
+ }
818
+ np = n;
819
+ #elif defined(__riscv_v_intrinsic)
820
+ #if defined(__riscv_zvfh)
821
+ const ggml_fp16_t s = GGML_CPU_FP32_TO_FP16(v);
822
+ const _Float16 scale = *(const _Float16*)(&s);
823
+
824
+ // calculate step size
825
+ const int epr = __riscv_vsetvlmax_e16m4();
826
+ const int step = epr * 2;
827
+ int np = (n & ~(step - 1));
828
+
829
+ // unroll by 2
830
+ for (int i = 0; i < np; i += step) {
831
+ vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, epr);
832
+ ay0 = __riscv_vfmul_vf_f16m4(ay0, scale, epr);
833
+ __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, epr);
834
+ __asm__ __volatile__ ("" ::: "memory");
835
+
836
+ vfloat16m4_t ay1 = __riscv_vle16_v_f16m4((const _Float16*)y + i + epr, epr);
837
+ ay1 = __riscv_vfmul_vf_f16m4(ay1, scale, epr);
838
+ __riscv_vse16_v_f16m4((_Float16*)y + i + epr, ay1, epr);
839
+ __asm__ __volatile__ ("" ::: "memory");
840
+ }
841
+
842
+ // leftovers
843
+ int vl;
844
+ for (int i = np; i < n; i += vl) {
845
+ vl = __riscv_vsetvl_e16m4(n - i);
846
+ vfloat16m4_t ay0 = __riscv_vle16_v_f16m4((const _Float16*)y + i, vl);
847
+ ay0 = __riscv_vfmul_vf_f16m4(ay0, scale, vl);
848
+ __riscv_vse16_v_f16m4((_Float16*)y + i, ay0, vl);
849
+ }
850
+ np = n;
851
+ #else
852
+ // fall to scalar path
853
+ const int np = 0;
854
+ #endif
855
+ #elif defined(GGML_SIMD)
856
+ const int np = (n & ~(GGML_F16_STEP - 1));
857
+
858
+ GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
859
+
860
+ GGML_F16_VEC ay[GGML_F16_ARR];
861
+
862
+ for (int i = 0; i < np; i += GGML_F16_STEP) {
863
+ for (int j = 0; j < GGML_F16_ARR; j++) {
864
+ ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
865
+ ay[j] = GGML_F16_VEC_MUL(ay[j], vx);
866
+
867
+ GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
868
+ }
869
+ }
870
+ #else
871
+ // scalar path
872
+ const int np = 0;
873
+ #endif
874
+ // scalar and leftovers
875
+ for (int i = np; i < n; ++i) {
876
+ y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i])*v);
877
+ }
878
+ }
879
+
880
+ inline static void ggml_vec_norm_f32 (const int n, float * s, const float * x) { ggml_vec_dot_f32(n, s, 0, x, 0, x, 0, 1); *s = sqrtf(*s); }
881
+ inline static void ggml_vec_sqr_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]*x[i]; }
882
+ inline static void ggml_vec_sqr_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
883
+ for (int i = 0; i < n; ++i) {
884
+ float v = GGML_CPU_FP16_TO_FP32(x[i]);
885
+ y[i] = GGML_CPU_FP32_TO_FP16(v*v);
886
+ }
887
+ }
888
+ inline static void ggml_vec_sqrt_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sqrtf(x[i]); }
889
+ inline static void ggml_vec_sqrt_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
890
+ for (int i = 0; i < n; ++i) {
891
+ y[i] = GGML_CPU_FP32_TO_FP16(sqrtf(GGML_CPU_FP16_TO_FP32(x[i])));
892
+ }
893
+ }
894
+ inline static void ggml_vec_log_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = logf(x[i]); }
895
+ inline static void ggml_vec_log_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
896
+ for (int i = 0; i < n; ++i) {
897
+ y[i] = GGML_CPU_FP32_TO_FP16(logf(GGML_CPU_FP16_TO_FP32(x[i])));
898
+ }
899
+ }
900
+ inline static void ggml_vec_sin_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sinf(x[i]); }
901
+ inline static void ggml_vec_sin_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
902
+ for (int i = 0; i < n; ++i) {
903
+ y[i] = GGML_CPU_FP32_TO_FP16(sinf(GGML_CPU_FP16_TO_FP32(x[i])));
904
+ }
905
+ }
906
+ inline static void ggml_vec_cos_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = cosf(x[i]); }
907
+ inline static void ggml_vec_cos_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
908
+ for (int i = 0; i < n; ++i) {
909
+ y[i] = GGML_CPU_FP32_TO_FP16(cosf(GGML_CPU_FP16_TO_FP32(x[i])));
910
+ }
911
+ }
912
+ inline static void ggml_vec_abs_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fabsf(x[i]); }
913
+ inline static void ggml_vec_abs_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
914
+ for (int i = 0; i < n; ++i) {
915
+ y[i] = GGML_CPU_FP32_TO_FP16(fabsf(GGML_CPU_FP16_TO_FP32(x[i])));
916
+ }
917
+ }
918
+ inline static void ggml_vec_sgn_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : ((x[i] < 0.f) ? -1.f : 0.f); }
919
+ inline static void ggml_vec_sgn_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
920
+ for (int i = 0; i < n; ++i) {
921
+ float v = GGML_CPU_FP16_TO_FP32(x[i]);
922
+ y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? 1.f : ((v < 0.f) ? -1.f : 0.f));
923
+ }
924
+ }
925
+ inline static void ggml_vec_step_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : 0.f; }
926
+ inline static void ggml_vec_step_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
927
+ for (int i = 0; i < n; ++i) {
928
+ y[i] = GGML_CPU_FP32_TO_FP16((GGML_CPU_FP16_TO_FP32(x[i]) > 0.f) ? 1.f : 0.f);
929
+ }
930
+ }
931
+ inline static void ggml_vec_tanh_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = tanhf(x[i]); }
932
+ inline static void ggml_vec_tanh_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
933
+ for (int i = 0; i < n; ++i) {
934
+ y[i] = GGML_CPU_FP32_TO_FP16(tanhf(GGML_CPU_FP16_TO_FP32(x[i])));
935
+ }
936
+ }
937
+ inline static void ggml_vec_elu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : expm1f(x[i]); }
938
+ inline static void ggml_vec_elu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
939
+ for (int i = 0; i < n; ++i) {
940
+ const float v = GGML_CPU_FP16_TO_FP32(x[i]);
941
+ y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v : expm1f(v));
942
+ }
943
+ }
944
+ inline static void ggml_vec_relu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : 0.f; }
945
+ inline static void ggml_vec_relu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
946
+ for (int i = 0; i < n; ++i) {
947
+ float v = GGML_CPU_FP16_TO_FP32(x[i]);
948
+ y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v : 0.f);
949
+ }
950
+ }
951
+ inline static void ggml_vec_leaky_relu_f32 (const int n, float * y, const float * x, const float ns) { for (int i = 0; i < n; ++i) y[i] = ((x[i] > 0.f) ? x[i] : 0.f) + ns * ((x[i] < 0.0f) ? x[i] : 0.f); }
952
+ inline static void ggml_vec_leaky_relu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const float ns) {
953
+ for (int i = 0; i < n; ++i) {
954
+ float v = GGML_CPU_FP16_TO_FP32(x[i]);
955
+ y[i] = GGML_CPU_FP32_TO_FP16(((v > 0.f) ? v : 0.f) + ns * ((v < 0.0f) ? v : 0.f));
956
+ }
957
+ }
958
+ inline static void ggml_vec_sigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = 1.f / (1.f + expf(-x[i])); }
959
+ inline static void ggml_vec_sigmoid_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
960
+ for (int i = 0; i < n; ++i) {
961
+ y[i] = GGML_CPU_FP32_TO_FP16(1.f / (1.f + expf(-GGML_CPU_FP16_TO_FP32(x[i]))));
962
+ }
963
+ }
964
+ // TODO: optimize performance
965
+ inline static void ggml_vec_hardswish_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); }
966
+ inline static void ggml_vec_hardswish_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
967
+ for (int i = 0; i < n; ++i) {
968
+ float v = GGML_CPU_FP16_TO_FP32(x[i]);
969
+ y[i] = GGML_CPU_FP32_TO_FP16(v * fminf(1.0f, fmaxf(0.0f, (v + 3.0f) / 6.0f)));
970
+ }
971
+ }
972
+ inline static void ggml_vec_hardsigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); }
973
+ inline static void ggml_vec_hardsigmoid_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
974
+ for (int i = 0; i < n; ++i) {
975
+ y[i] = GGML_CPU_FP32_TO_FP16(fminf(1.0f, fmaxf(0.0f, (GGML_CPU_FP16_TO_FP32(x[i]) + 3.0f) / 6.0f)));
976
+ }
977
+ }
978
+ inline static void ggml_vec_exp_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = expf(x[i]); }
979
+ inline static void ggml_vec_exp_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
980
+ for (int i = 0; i < n; ++i) {
981
+ y[i] = GGML_CPU_FP32_TO_FP16(expf(GGML_CPU_FP16_TO_FP32(x[i])));
982
+ }
983
+ }
984
+
985
+ static const float GELU_COEF_A = 0.044715f;
986
+ static const float GELU_QUICK_COEF = -1.702f;
987
+ static const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
988
+ static const float SQRT_2_INV = 0.70710678118654752440084436210484f;
989
+
990
+ inline static float ggml_gelu_f32(float x) {
991
+ return 0.5f*x*(1.0f + tanhf(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
992
+ }
993
+
994
+ inline static void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
995
+ const uint16_t * i16 = (const uint16_t *) x;
996
+ for (int i = 0; i < n; ++i) {
997
+ y[i] = ggml_table_gelu_f16[i16[i]];
998
+ }
999
+ }
1000
+
1001
+ inline static void ggml_vec_gelu_erf_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
1002
+ for (int i = 0; i < n; ++i) {
1003
+ float xi = GGML_CPU_FP16_TO_FP32(x[i]);
1004
+ float res = 0.5f*xi*(1.0f + erff(xi*SQRT_2_INV));
1005
+ y[i] = GGML_CPU_FP32_TO_FP16(res);
1006
+ }
1007
+ }
1008
+
1009
+ #ifdef GGML_GELU_FP16
1010
+ inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
1011
+ uint16_t t;
1012
+ for (int i = 0; i < n; ++i) {
1013
+ if (x[i] <= -10.0f) {
1014
+ y[i] = 0.0f;
1015
+ } else if (x[i] >= 10.0f) {
1016
+ y[i] = x[i];
1017
+ } else {
1018
+ ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
1019
+ memcpy(&t, &fp16, sizeof(uint16_t));
1020
+ y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[t]);
1021
+ }
1022
+ }
1023
+ }
1024
+ #else
1025
+ inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
1026
+ for (int i = 0; i < n; ++i) {
1027
+ y[i] = ggml_gelu_f32(x[i]);
1028
+ }
1029
+ }
1030
+ #endif
1031
+
1032
+ inline static void ggml_vec_gelu_erf_f32(const int n, float * y, const float * x) {
1033
+ for (int i = 0; i < n; ++i) {
1034
+ float xi = x[i];
1035
+ y[i] = 0.5f*xi*(1.0f + erff(xi*SQRT_2_INV));
1036
+ }
1037
+ }
1038
+
1039
+ inline static float ggml_gelu_quick_f32(float x) {
1040
+ return x*(1.0f/(1.0f+expf(GELU_QUICK_COEF*x)));
1041
+ }
1042
+
1043
+ inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
1044
+ const uint16_t * i16 = (const uint16_t *) x;
1045
+ for (int i = 0; i < n; ++i) {
1046
+ y[i] = ggml_table_gelu_quick_f16[i16[i]];
1047
+ }
1048
+ }
1049
+
1050
+ #ifdef GGML_GELU_QUICK_FP16
1051
+ inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
1052
+ uint16_t t;
1053
+ for (int i = 0; i < n; ++i) {
1054
+ ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
1055
+ memcpy(&t, &fp16, sizeof(uint16_t));
1056
+ y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]);
1057
+ }
1058
+ }
1059
+ #else
1060
+ inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
1061
+ for (int i = 0; i < n; ++i) {
1062
+ y[i] = ggml_gelu_quick_f32(x[i]);
1063
+ }
1064
+ }
1065
+ #endif
1066
+
1067
+ // Sigmoid Linear Unit (SiLU) function
1068
+ inline static float ggml_silu_f32(float x) {
1069
+ return x/(1.0f + expf(-x));
1070
+ }
1071
+ inline static ggml_fp16_t ggml_silu_f16(ggml_fp16_t x) {
1072
+ float v = GGML_CPU_FP16_TO_FP32(x);
1073
+ return GGML_CPU_FP32_TO_FP16(v/(1.0f + expf(-v)));
1074
+ }
1075
+
1076
+ #if __FINITE_MATH_ONLY__
1077
+ #error "some routines in ggml.c require non-finite math arithmetics -- pass -fno-finite-math-only to the compiler to fix"
1078
+ #error "ref: https://github.com/ggml-org/llama.cpp/pull/7154#issuecomment-2143844461"
1079
+ #endif
1080
+
1081
+ /* Below function was borrowed from the GitHub repository:
1082
+ https://github.com/openvinotoolkit/openvino/blob/master/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/common.hpp */
1083
+ #if defined(__ARM_FEATURE_SVE) && defined(__aarch64__)
1084
+ inline static svfloat32_t exp_ps_sve(svbool_t pg, svfloat32_t src) {
1085
+ // Constants
1086
+ const svfloat32_t log2_e = svdup_n_f32(1.4426950409f);
1087
+ const svfloat32_t ln2 = svdup_n_f32(0.6931473921f);
1088
+ const svfloat32_t half_ln2_sq = svdup_n_f32(0.2413862043f);
1089
+ const svuint32_t not_mask17 = svdup_n_u32(~((1u << 17) - 1));
1090
+ const svfloat32_t one = svdup_n_f32(1.0f);
1091
+ const svfloat32_t inactive1 = svdup_n_f32(0.0f);
1092
+ const svint32_t inactive2 = svdup_n_s32(0);
1093
+
1094
+ // Algorithm starts here
1095
+ svfloat32_t t0 = svmul_f32_m(pg, src, log2_e); // y = x * log2(e)
1096
+ svfloat32_t t1 = svrintm_f32_m(inactive1, pg, t0); // rount to int (float)
1097
+ svint32_t t2 = svcvt_s32_f32_m(inactive2, pg, t1); // n
1098
+
1099
+ t1 = svsub_f32_m(pg, t0, t1); // a = y - floor(y)
1100
+ t1 = svadd_f32_m(pg, t1, one); // b = a + 1
1101
+
1102
+ svuint32_t t3 = svlsr_n_u32_m(pg, svreinterpret_u32_f32(t1), 17); // v = b >> 17 (u32)
1103
+ svfloat32_t t4 = svexpa_f32(t3); // c = fexpa(v)
1104
+ t4 = svscale_f32_m(pg, t4, t2); // fexpa(v) * 2^(n)
1105
+
1106
+ // and_(t2.d, t1.d, not_mask17.d)
1107
+ svfloat32_t t5 = svreinterpret_f32_u32(svand_u32_m(pg, svreinterpret_u32_f32(t1), not_mask17));
1108
+ t5 = svsub_f32_m(pg, t1, t5); // z
1109
+ t0 = svmla_f32_m(pg, ln2, t5, half_ln2_sq); // ln2 + half_ln2_sq * z
1110
+ t0 = svmla_f32_m(pg, one, t5, t0); // 1 + (ln2 * z) + (half_ln2_sq * z * z)
1111
+ t0 = svmul_f32_m(pg, t0, t4); // Final result
1112
+
1113
+ return t0;
1114
+ }
1115
+ #endif
1116
+
1117
+ #if defined(__ARM_FEATURE_SVE) && defined(__aarch64__)
1118
+
1119
+ inline static svfloat32_t ggml_v_expf(svbool_t pg, svfloat32_t x) {
1120
+ const svfloat32_t r = svdup_n_f32_x(pg, 0x1.8p23f);
1121
+ const svfloat32_t z = svmla_n_f32_x(pg, r, x, 0x1.715476p+0f);
1122
+ const svfloat32_t n = svsub_f32_x(pg, z, r);
1123
+ const svfloat32_t b = svmls_n_f32_x(pg, svmls_n_f32_x(pg, x, n, 0x1.62e4p-1f), n, 0x1.7f7d1cp-20f);
1124
+ const svuint32_t e = svlsl_n_u32_x(pg, svreinterpret_u32_f32(z), 23);
1125
+ const svfloat32_t k = svreinterpret_f32_u32(svadd_u32_x(pg, e, svreinterpret_u32_f32(svdup_n_f32_x(pg, 1))));
1126
+ const svbool_t c = svacgt_n_f32(pg, n, 126);
1127
+ const svfloat32_t u = svmul_f32_x(pg, b, b);
1128
+ const svfloat32_t j = svmla_f32_x(pg,
1129
+ svmul_n_f32_x(pg, b, 0x1.ffffecp-1f),
1130
+ svmla_f32_x(pg, svmla_f32_x(pg, svdup_n_f32_x(pg, 0x1.fffdb6p-2f), svdup_n_f32_x(pg, 0x1.555e66p-3f), b),
1131
+ svmla_f32_x(pg, svdup_n_f32_x(pg, 0x1.573e2ep-5f), svdup_n_f32_x(pg, 0x1.0e4020p-7f), b), u), u);
1132
+ const svuint32_t d = svdup_n_u32_z(svcmple_n_f32(pg, n, 0.0), 0x82000000);
1133
+ const svfloat32_t s1 = svreinterpret_f32_u32(svadd_n_u32_x(pg, d, 0x7f000000));
1134
+ const svfloat32_t s2 = svreinterpret_f32_u32(svsub_u32_x(pg, e, d));
1135
+ return svsel_f32(svacgt_f32(pg, n, svdup_n_f32_x(pg, 192)), svmul_f32_x(pg, s1, s1),
1136
+ svsel_f32(c, svmul_f32_x(pg, svmla_f32_x(pg, s2, s2, j), s1), svmla_f32_x(pg, k, k, j)));
1137
+ }
1138
+
1139
+ // computes silu x/(1+exp(-x)) in single precision vector
1140
+ inline static svfloat32_t ggml_v_silu(svbool_t pg, svfloat32_t x) {
1141
+ const svfloat32_t one = svdup_n_f32_x(pg, 1.0f);
1142
+ const svfloat32_t zero = svdup_n_f32_x(pg, 0.0f);
1143
+ const svfloat32_t neg_x = svsub_f32_x(pg, zero, x);
1144
+ const svfloat32_t exp_neg_x = ggml_v_expf(pg, neg_x);
1145
+ const svfloat32_t one_plus_exp_neg_x = svadd_f32_x(pg, one, exp_neg_x);
1146
+ return svdiv_f32_x(pg, x, one_plus_exp_neg_x);
1147
+ }
1148
+
1149
+ #elif defined(__ARM_NEON) && defined(__aarch64__)
1150
+
1151
+ // adapted from arm limited optimized routine
1152
+ // the maximum error is 1.45358 plus 0.5 ulps
1153
+ // numbers above 88.38 will flush to infinity
1154
+ // numbers beneath -103.97 will flush to zero
1155
+ inline static float32x4_t ggml_v_expf(float32x4_t x) {
1156
+ const float32x4_t r = vdupq_n_f32(0x1.8p23f);
1157
+ const float32x4_t z = vfmaq_f32(r, x, vdupq_n_f32(0x1.715476p+0f));
1158
+ const float32x4_t n = vsubq_f32(z, r);
1159
+ const float32x4_t b = vfmsq_f32(vfmsq_f32(x, n, vdupq_n_f32(0x1.62e4p-1f)), n,
1160
+ vdupq_n_f32(0x1.7f7d1cp-20f));
1161
+ const uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_f32(z), 23);
1162
+ const float32x4_t k = vreinterpretq_f32_u32(vaddq_u32(e, vreinterpretq_u32_f32(vdupq_n_f32(1))));
1163
+ const uint32x4_t c = vcagtq_f32(n, vdupq_n_f32(126));
1164
+ const float32x4_t u = vmulq_f32(b, b);
1165
+ const float32x4_t j = vfmaq_f32(
1166
+ vmulq_f32(vdupq_n_f32(0x1.ffffecp-1f), b),
1167
+ vfmaq_f32(vfmaq_f32(vdupq_n_f32(0x1.fffdb6p-2f), vdupq_n_f32(0x1.555e66p-3f), b),
1168
+ vfmaq_f32(vdupq_n_f32(0x1.573e2ep-5f), vdupq_n_f32(0x1.0e4020p-7f), b), u), u);
1169
+ if (!vpaddd_u64(vreinterpretq_u64_u32(c)))
1170
+ return vfmaq_f32(k, j, k);
1171
+ const uint32x4_t d = vandq_u32(vclezq_f32(n), vdupq_n_u32(0x82000000));
1172
+ const float32x4_t s1 = vreinterpretq_f32_u32(vaddq_u32(d, vdupq_n_u32(0x7f000000)));
1173
+ const float32x4_t s2 = vreinterpretq_f32_u32(vsubq_u32(e, d));
1174
+ return vbslq_f32(vcagtq_f32(n, vdupq_n_f32(192)), vmulq_f32(s1, s1),
1175
+ vbslq_f32(c, vmulq_f32(vfmaq_f32(s2, s2, j), s1), vfmaq_f32(k, k, j)));
1176
+ }
1177
+
1178
+ // computes silu x/(1+exp(-x)) in single precision vector
1179
+ inline static float32x4_t ggml_v_silu(float32x4_t x) {
1180
+ const float32x4_t one = vdupq_n_f32(1.0f);
1181
+ const float32x4_t zero = vdupq_n_f32(0.0f);
1182
+ const float32x4_t neg_x = vsubq_f32(zero, x);
1183
+ const float32x4_t exp_neg_x = ggml_v_expf(neg_x);
1184
+ const float32x4_t one_plus_exp_neg_x = vaddq_f32(one, exp_neg_x);
1185
+ return vdivq_f32(x, one_plus_exp_neg_x);
1186
+ }
1187
+
1188
+ #elif defined(__AVX512F__) && defined(__AVX512DQ__)
1189
+
1190
+ // adapted from arm limited optimized routine
1191
+ // the maximum error is 1.45358 plus 0.5 ulps
1192
+ // numbers above 88.38 will flush to infinity
1193
+ // numbers beneath -103.97 will flush to zero
1194
+ inline static __m512 ggml_v_expf(__m512 x) {
1195
+ const __m512 r = _mm512_set1_ps(0x1.8p23f);
1196
+ const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
1197
+ const __m512 n = _mm512_sub_ps(z, r);
1198
+ const __m512 b =
1199
+ _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
1200
+ _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
1201
+ const __mmask16 d =
1202
+ _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ);
1203
+ const __m512 u = _mm512_mul_ps(b, b);
1204
+ const __m512 j = _mm512_fmadd_ps(
1205
+ _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
1206
+ _mm512_set1_ps(0x1.573e2ep-5f)),
1207
+ u,
1208
+ _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
1209
+ _mm512_set1_ps(0x1.fffdb6p-2f))),
1210
+ u,
1211
+ _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F)));
1212
+ const __m512 res = _mm512_scalef_ps(j, n);
1213
+ if (_mm512_kortestz(d, d))
1214
+ return res;
1215
+ const __m512 zero = _mm512_setzero_ps();
1216
+ const __m512 alt = _mm512_mask_blend_ps(
1217
+ _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero);
1218
+ return _mm512_mask_blend_ps(d, res, alt);
1219
+ }
1220
+
1221
+ // computes silu x/(1+exp(-x)) in single precision vector
1222
+ inline static __m512 ggml_v_silu(__m512 x) {
1223
+ const __m512 one = _mm512_set1_ps(1);
1224
+ const __m512 zero = _mm512_setzero_ps();
1225
+ const __m512 neg_x = _mm512_sub_ps(zero, x);
1226
+ const __m512 exp_neg_x = ggml_v_expf(neg_x);
1227
+ const __m512 one_plus_exp_neg_x = _mm512_add_ps(one, exp_neg_x);
1228
+ return _mm512_div_ps(x, one_plus_exp_neg_x);
1229
+ }
1230
+
1231
+ #elif defined(__AVX2__) && defined(__FMA__)
1232
+
1233
+ // adapted from arm limited optimized routine
1234
+ // the maximum error is 1.45358 plus 0.5 ulps
1235
+ // numbers above 88.38 will flush to infinity
1236
+ // numbers beneath -103.97 will flush to zero
1237
+ inline static __m256 ggml_v_expf(__m256 x) {
1238
+ const __m256 r = _mm256_set1_ps(0x1.8p23f);
1239
+ const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r);
1240
+ const __m256 n = _mm256_sub_ps(z, r);
1241
+ const __m256 b = _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.7f7d1cp-20f),
1242
+ _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x));
1243
+ const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23);
1244
+ const __m256 k = _mm256_castsi256_ps(
1245
+ _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1))));
1246
+ const __m256i c = _mm256_castps_si256(
1247
+ _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
1248
+ _mm256_set1_ps(126), _CMP_GT_OQ));
1249
+ const __m256 u = _mm256_mul_ps(b, b);
1250
+ const __m256 j = _mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b,
1251
+ _mm256_set1_ps(0x1.573e2ep-5f)), u,
1252
+ _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b,
1253
+ _mm256_set1_ps(0x1.fffdb6p-2f))),
1254
+ u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b));
1255
+ if (!_mm256_movemask_ps(_mm256_castsi256_ps(c)))
1256
+ return _mm256_fmadd_ps(j, k, k);
1257
+ const __m256i g = _mm256_and_si256(
1258
+ _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)),
1259
+ _mm256_set1_epi32(0x82000000u));
1260
+ const __m256 s1 =
1261
+ _mm256_castsi256_ps(_mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000u)));
1262
+ const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g));
1263
+ const __m256i d = _mm256_castps_si256(
1264
+ _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
1265
+ _mm256_set1_ps(192), _CMP_GT_OQ));
1266
+ return _mm256_or_ps(
1267
+ _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)),
1268
+ _mm256_andnot_ps(
1269
+ _mm256_castsi256_ps(d),
1270
+ _mm256_or_ps(
1271
+ _mm256_and_ps(_mm256_castsi256_ps(c),
1272
+ _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)),
1273
+ _mm256_andnot_ps(_mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k)))));
1274
+ }
1275
+
1276
+ // computes silu x/(1+exp(-x)) in single precision vector
1277
+ inline static __m256 ggml_v_silu(__m256 x) {
1278
+ const __m256 one = _mm256_set1_ps(1);
1279
+ const __m256 zero = _mm256_setzero_ps();
1280
+ const __m256 neg_x = _mm256_sub_ps(zero, x);
1281
+ const __m256 exp_neg_x = ggml_v_expf(neg_x);
1282
+ const __m256 one_plus_exp_neg_x = _mm256_add_ps(one, exp_neg_x);
1283
+ return _mm256_div_ps(x, one_plus_exp_neg_x);
1284
+ }
1285
+
1286
+ #elif defined(__SSE2__) // __AVX2__ / __ARM_NEON
1287
+
1288
+ #if defined(__FMA__)
1289
+ #define MADD128(x, y, z) _mm_fmadd_ps(x, y, z)
1290
+ #define NMADD128(x, y, z) _mm_fnmadd_ps(x, y, z)
1291
+ #else
1292
+ #define MADD128(x, y, z) _mm_add_ps(_mm_mul_ps(x, y), z)
1293
+ #define NMADD128(x, y, z) _mm_sub_ps(z, _mm_mul_ps(x, y))
1294
+ #endif
1295
+
1296
+ // adapted from arm limited optimized routine
1297
+ // the maximum error is 1.45358 plus 0.5 ulps
1298
+ // numbers above 88.38 will flush to infinity
1299
+ // numbers beneath -103.97 will flush to zero
1300
+ inline static __m128 ggml_v_expf(__m128 x) {
1301
+ const __m128 r = _mm_set1_ps(0x1.8p23f);
1302
+ const __m128 z = MADD128(x, _mm_set1_ps(0x1.715476p+0f), r);
1303
+ const __m128 n = _mm_sub_ps(z, r);
1304
+ const __m128 b =
1305
+ NMADD128(n, _mm_set1_ps(0x1.7f7d1cp-20f), NMADD128(n, _mm_set1_ps(0x1.62e4p-1f), x));
1306
+ const __m128i e = _mm_slli_epi32(_mm_castps_si128(z), 23);
1307
+ const __m128 k = _mm_castsi128_ps(_mm_add_epi32(e, _mm_castps_si128(_mm_set1_ps(1))));
1308
+ const __m128i c =
1309
+ _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(126)));
1310
+ const __m128 u = _mm_mul_ps(b, b);
1311
+ const __m128 j =
1312
+ MADD128(MADD128(MADD128(_mm_set1_ps(0x1.0e4020p-7f), b, _mm_set1_ps(0x1.573e2ep-5f)), u,
1313
+ MADD128(_mm_set1_ps(0x1.555e66p-3f), b, _mm_set1_ps(0x1.fffdb6p-2f))),
1314
+ u, _mm_mul_ps(_mm_set1_ps(0x1.ffffecp-1f), b));
1315
+ if (!_mm_movemask_epi8(c))
1316
+ return MADD128(j, k, k);
1317
+ const __m128i g = _mm_and_si128(_mm_castps_si128(_mm_cmple_ps(n, _mm_setzero_ps())),
1318
+ _mm_set1_epi32(0x82000000u));
1319
+ const __m128 s1 = _mm_castsi128_ps(_mm_add_epi32(g, _mm_set1_epi32(0x7f000000u)));
1320
+ const __m128 s2 = _mm_castsi128_ps(_mm_sub_epi32(e, g));
1321
+ const __m128i d =
1322
+ _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(192)));
1323
+ return _mm_or_ps(
1324
+ _mm_and_ps(_mm_castsi128_ps(d), _mm_mul_ps(s1, s1)),
1325
+ _mm_andnot_ps(_mm_castsi128_ps(d),
1326
+ _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(c), _mm_mul_ps(MADD128(s2, j, s2), s1)),
1327
+ _mm_andnot_ps(_mm_castsi128_ps(c), MADD128(k, j, k)))));
1328
+ }
1329
+
1330
+ // computes silu x/(1+exp(-x)) in single precision vector
1331
+ inline static __m128 ggml_v_silu(__m128 x) {
1332
+ const __m128 one = _mm_set1_ps(1);
1333
+ const __m128 zero = _mm_setzero_ps();
1334
+ const __m128 neg_x = _mm_sub_ps(zero, x);
1335
+ const __m128 exp_neg_x = ggml_v_expf(neg_x);
1336
+ const __m128 one_plus_exp_neg_x = _mm_add_ps(one, exp_neg_x);
1337
+ return _mm_div_ps(x, one_plus_exp_neg_x);
1338
+ }
1339
+
1340
+ #elif defined(__riscv_v_intrinsic)
1341
+
1342
+ // adapted from arm limited optimized routine
1343
+ // the maximum error is 1.45358 plus 0.5 ulps
1344
+ // numbers above 88.38 will flush to infinity
1345
+ // numbers beneath -103.97 will flush to zero
1346
+ inline static vfloat32m2_t ggml_v_expf_m2(vfloat32m2_t x, int vl) {
1347
+ const vfloat32m2_t r = __riscv_vfmv_v_f_f32m2(0x1.8p23f, vl);
1348
+ #ifdef __riscv_xtheadvector
1349
+ // workaround for compiler bug (gcc 14.3.0: Error: unrecognized opcode `th.vmv1r.v v2,v4')
1350
+ vfloat32m2_t z = __riscv_vfadd_vf_f32m2(r, 0.0f, vl);
1351
+ z = __riscv_vfmacc_vf_f32m2(z, 0x1.715476p+0f, x, vl);
1352
+ #else
1353
+ const vfloat32m2_t z = __riscv_vfmacc_vf_f32m2(r, 0x1.715476p+0f, x, vl);
1354
+ #endif
1355
+ const vfloat32m2_t n = __riscv_vfsub_vv_f32m2(z, r, vl);
1356
+ const vfloat32m2_t b = __riscv_vfnmsac_vf_f32m2(__riscv_vfnmsac_vf_f32m2(x, 0x1.62e4p-1f, n, vl),
1357
+ 0x1.7f7d1cp-20f, n, vl);
1358
+ const vuint32m2_t e = __riscv_vsll_vx_u32m2(__riscv_vreinterpret_v_f32m2_u32m2(z), 23, vl);
1359
+ const vfloat32m2_t k = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vadd_vx_u32m2(e, 0x3f800000, vl)); // 1.0f
1360
+ const vbool16_t c = __riscv_vmfgt_vf_f32m2_b16(__riscv_vfabs_v_f32m2(n, vl), 126.0f, vl);
1361
+ const vfloat32m2_t u = __riscv_vfmul_vv_f32m2(b, b, vl);
1362
+ const vfloat32m2_t j = __riscv_vfmacc_vv_f32m2(
1363
+ __riscv_vfmul_vf_f32m2(b, 0x1.ffffecp-1f, vl),
1364
+ __riscv_vfmacc_vv_f32m2(
1365
+ __riscv_vfmacc_vf_f32m2(__riscv_vfmv_v_f_f32m2(0x1.fffdb6p-2f, vl), 0x1.555e66p-3f, b, vl),
1366
+ __riscv_vfmacc_vf_f32m2(__riscv_vfmv_v_f_f32m2(0x1.573e2ep-5f, vl), 0x1.0e4020p-7f, b, vl),
1367
+ u, vl), u, vl);
1368
+ if (!__riscv_vcpop_m_b16(c, vl))
1369
+ return __riscv_vfmacc_vv_f32m2(k, j, k, vl);
1370
+ const vbool16_t dm = __riscv_vmfle_vf_f32m2_b16(n, 0.0f, vl);
1371
+ const vuint32m2_t d = __riscv_vmerge_vxm_u32m2(__riscv_vmv_v_x_u32m2(0, vl), 0x82000000, dm, vl);
1372
+ const vfloat32m2_t s1 = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vadd_vx_u32m2(d, 0x7f000000, vl));
1373
+ const vfloat32m2_t s2 = __riscv_vreinterpret_v_u32m2_f32m2(__riscv_vsub_vv_u32m2(e, d, vl));
1374
+ const vfloat32m2_t r1 = __riscv_vmerge_vvm_f32m2(
1375
+ __riscv_vfmacc_vv_f32m2(k, k, j, vl),
1376
+ __riscv_vfmul_vv_f32m2(__riscv_vfmacc_vv_f32m2(s2, s2, j, vl), s1, vl),
1377
+ c, vl);
1378
+ return __riscv_vmerge_vvm_f32m2(
1379
+ r1, __riscv_vfmul_vv_f32m2(s1, s1, vl),
1380
+ __riscv_vmfgt_vf_f32m2_b16(__riscv_vfabs_v_f32m2(n, vl), 192.0f, vl),
1381
+ vl);
1382
+ }
1383
+
1384
+ // computes silu x/(1+exp(-x)) in single precision vector
1385
+ inline static vfloat32m2_t ggml_v_silu_m2(vfloat32m2_t x, int vl) {
1386
+ const vfloat32m2_t neg_x = __riscv_vfneg_v_f32m2(x, vl);
1387
+ const vfloat32m2_t exp_neg_x = ggml_v_expf_m2(neg_x, vl);
1388
+ const vfloat32m2_t one_plus_exp_neg_x = __riscv_vfadd_vf_f32m2(exp_neg_x, 1.0f, vl);
1389
+ return __riscv_vfdiv_vv_f32m2(x, one_plus_exp_neg_x, vl);
1390
+ }
1391
+
1392
+ #endif // __ARM_NEON / __AVX2__ / __SSE2__ / __riscv_v_intrinsic
1393
+
1394
+ inline static void ggml_vec_silu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
1395
+ for (int i = 0; i < n; ++i) {
1396
+ y[i] = ggml_silu_f16(x[i]);
1397
+ }
1398
+ }
1399
+
1400
+ inline static float ggml_silu_backward_f32(float x, float dy) {
1401
+ const float s = 1.0f/(1.0f + expf(-x));
1402
+ return dy*s*(1.0f + x*(1.0f - s));
1403
+ }
1404
+
1405
+ inline static ggml_fp16_t ggml_silu_backward_f16(ggml_fp16_t x, ggml_fp16_t dy) {
1406
+ const float v = GGML_CPU_FP16_TO_FP32(x);
1407
+ const float s = 1.0f/(1.0f + expf(-v));
1408
+ return GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(dy)*s*(1.0f + v*(1.0f - s)));
1409
+ }
1410
+
1411
+ inline static void ggml_vec_silu_backward_f32(const int n, float * dx, const float * x, const float * dy) {
1412
+ for (int i = 0; i < n; ++i) {
1413
+ dx[i] = ggml_silu_backward_f32(x[i], dy[i]);
1414
+ }
1415
+ }
1416
+
1417
+ inline static void ggml_vec_silu_backward_f16(const int n, ggml_fp16_t * dx, const ggml_fp16_t * x, const ggml_fp16_t * dy) {
1418
+ for (int i = 0; i < n; ++i) {
1419
+ dx[i] = ggml_silu_backward_f16(x[i], dy[i]);
1420
+ }
1421
+ }
1422
+
1423
+ inline static void ggml_vec_reglu_f32 (const int n, float * y, const float * x, const float * g) {
1424
+ for (int i = 0; i < n; ++i) {
1425
+ y[i] = (x[i] > 0.f) ? x[i] * g[i] : 0.f;
1426
+ }
1427
+ }
1428
+
1429
+ inline static void ggml_vec_reglu_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
1430
+ for (int i = 0; i < n; ++i) {
1431
+ float v = GGML_CPU_FP16_TO_FP32(x[i]);
1432
+ y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v * GGML_CPU_FP16_TO_FP32(g[i]) : 0.f);
1433
+ }
1434
+ }
1435
+
1436
+ #ifdef GGML_GELU_FP16
1437
+ inline static void ggml_vec_geglu_f32(const int n, float * y, const float * x, const float * g) {
1438
+ uint16_t t;
1439
+ for (int i = 0; i < n; ++i) {
1440
+ if (x[i] <= -10.0f) {
1441
+ y[i] = 0.0f;
1442
+ } else if (x[i] >= 10.0f) {
1443
+ y[i] = x[i] * g[i];
1444
+ } else {
1445
+ ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
1446
+ memcpy(&t, &fp16, sizeof(uint16_t));
1447
+ y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[t]) * g[i];
1448
+ }
1449
+ }
1450
+ }
1451
+ #else
1452
+ inline static void ggml_vec_geglu_f32(const int n, float * y, const float * x, const float * g) {
1453
+ for (int i = 0; i < n; ++i) {
1454
+ y[i] = ggml_gelu_f32(x[i]) * g[i];
1455
+ }
1456
+ }
1457
+ #endif
1458
+
1459
+ inline static void ggml_vec_geglu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
1460
+ const uint16_t * i16 = (const uint16_t *) x;
1461
+ for (int i = 0; i < n; ++i) {
1462
+ float v = GGML_CPU_FP16_TO_FP32(g[i]);
1463
+ y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(ggml_table_gelu_f16[i16[i]]) * v);
1464
+ }
1465
+ }
1466
+
1467
+ void ggml_vec_swiglu_f32(const int n, float * y, const float * x, const float * g);
1468
+
1469
+ inline static void ggml_vec_swiglu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
1470
+ for (int i = 0; i < n; ++i) {
1471
+ float xi = GGML_CPU_FP16_TO_FP32(x[i]);
1472
+ float gi = GGML_CPU_FP16_TO_FP32(g[i]);
1473
+ y[i] = GGML_CPU_FP32_TO_FP16((xi/(1.0f + expf(-xi))) * gi);
1474
+ }
1475
+ }
1476
+
1477
+ inline static void ggml_vec_geglu_erf_f32(const int n, float * y, const float * x, const float * g) {
1478
+ for (int i = 0; i < n; ++i) {
1479
+ float xi = x[i];
1480
+ y[i] = 0.5f * xi * (1.0f + erff(xi*SQRT_2_INV)) * g[i];
1481
+ }
1482
+ }
1483
+
1484
+ inline static void ggml_vec_geglu_erf_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
1485
+ for (int i = 0; i < n; ++i) {
1486
+ float xi = GGML_CPU_FP16_TO_FP32(x[i]);
1487
+ float gi = GGML_CPU_FP16_TO_FP32(g[i]);
1488
+ y[i] = GGML_CPU_FP32_TO_FP16(0.5f * xi * (1.0f + erff(xi*SQRT_2_INV)) * gi);
1489
+ }
1490
+ }
1491
+
1492
+ #ifdef GGML_GELU_QUICK_FP16
1493
+ inline static void ggml_vec_geglu_quick_f32(const int n, float * y, const float * x, const float * g) {
1494
+ uint16_t t;
1495
+ for (int i = 0; i < n; ++i) {
1496
+ ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
1497
+ memcpy(&t, &fp16, sizeof(uint16_t));
1498
+ y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]) * g[i];
1499
+ }
1500
+ }
1501
+ #else
1502
+ inline static void ggml_vec_geglu_quick_f32(const int n, float * y, const float * x, const float * g) {
1503
+ for (int i = 0; i < n; ++i) {
1504
+ y[i] = ggml_gelu_quick_f32(x[i]) * g[i];
1505
+ }
1506
+ }
1507
+ #endif
1508
+
1509
+ inline static void ggml_vec_geglu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
1510
+ const uint16_t * i16 = (const uint16_t *) x;
1511
+ for (int i = 0; i < n; ++i) {
1512
+ float v = GGML_CPU_FP16_TO_FP32(g[i]);
1513
+ y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[i16[i]]) * v);
1514
+ }
1515
+ }
1516
+
1517
+ inline static void ggml_vec_siglu_f32(const int n, float * y, const float * x, const float * g) {
1518
+ for (int i = 0; i < n; ++i) {
1519
+ y[i] = (1.0f / (1.0f + expf(-x[i]))) * g[i];
1520
+ }
1521
+ }
1522
+
1523
+ inline static void ggml_vec_siglu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x, const ggml_fp16_t * g) {
1524
+ for (int i = 0; i < n; ++i) {
1525
+ float xi = GGML_CPU_FP16_TO_FP32(x[i]);
1526
+ float gi = GGML_CPU_FP16_TO_FP32(g[i]);
1527
+ y[i] = GGML_CPU_FP32_TO_FP16((1.0f / (1.0f + expf(-xi))) * gi);
1528
+ }
1529
+ }
1530
+
1531
+ inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) {
1532
+ #ifndef GGML_USE_ACCELERATE
1533
+ ggml_float sum = 0.0;
1534
+ for (int i = 0; i < n; ++i) {
1535
+ sum += (ggml_float)x[i];
1536
+ }
1537
+ *s = (float)sum;
1538
+ #else
1539
+ vDSP_sve(x, 1, s, n);
1540
+ #endif
1541
+ }
1542
+
1543
+ inline static void ggml_vec_cumsum_f32(const int n, float * y, const float * x) {
1544
+ for (int i = 0; i < n; ++i) {
1545
+ if (i == 0) {
1546
+ y[i] = x[i];
1547
+ } else {
1548
+ y[i] = y[i - 1] + x[i];
1549
+ }
1550
+ }
1551
+ }
1552
+
1553
+ inline static void ggml_vec_sum_f32_ggf(const int n, ggml_float * s, const float * x) {
1554
+ ggml_float sum = 0.0;
1555
+ for (int i = 0; i < n; ++i) {
1556
+ sum += (ggml_float)x[i];
1557
+ }
1558
+ *s = sum;
1559
+ }
1560
+
1561
+ inline static void ggml_vec_sum_f16_ggf(const int n, float * s, const ggml_fp16_t * x) {
1562
+ float sum = 0.0f;
1563
+ for (int i = 0; i < n; ++i) {
1564
+ sum += GGML_CPU_FP16_TO_FP32(x[i]);
1565
+ }
1566
+ *s = sum;
1567
+ }
1568
+
1569
+ inline static void ggml_vec_sum_bf16_ggf(const int n, float * s, const ggml_bf16_t * x) {
1570
+ float sum = 0.0f;
1571
+ for (int i = 0; i < n; ++i) {
1572
+ sum += GGML_BF16_TO_FP32(x[i]);
1573
+ }
1574
+ *s = sum;
1575
+ }
1576
+
1577
+ inline static void ggml_vec_max_f32(const int n, float * s, const float * x) {
1578
+ #ifndef GGML_USE_ACCELERATE
1579
+ float max = -INFINITY;
1580
+ for (int i = 0; i < n; ++i) {
1581
+ max = MAX(max, x[i]);
1582
+ }
1583
+ *s = max;
1584
+ #else
1585
+ vDSP_maxv(x, 1, s, n);
1586
+ #endif
1587
+ }
1588
+
1589
+ inline static void ggml_vec_norm_inv_f32(const int n, float * s, const float * x) {
1590
+ ggml_vec_norm_f32(n, s, x);
1591
+ *s = 1.f/(*s);
1592
+ }
1593
+
1594
+ inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) {
1595
+ float max = -INFINITY;
1596
+ int idx = 0;
1597
+ for (int i = 0; i < n; ++i) {
1598
+ max = MAX(max, x[i]);
1599
+ if (max == x[i]) { idx = i; }
1600
+ }
1601
+ *s = idx;
1602
+ }
1603
+
1604
+ #ifdef __cplusplus
1605
+ }
1606
+ #endif