@fugood/llama.node 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (286) hide show
  1. package/CMakeLists.txt +7 -0
  2. package/bin/darwin/arm64/llama-node.node +0 -0
  3. package/bin/darwin/x64/llama-node.node +0 -0
  4. package/bin/linux/arm64/llama-node.node +0 -0
  5. package/bin/linux/x64/llama-node.node +0 -0
  6. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  7. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  8. package/bin/win32/arm64/llama-node.node +0 -0
  9. package/bin/win32/arm64/node.lib +0 -0
  10. package/bin/win32/x64/llama-node.node +0 -0
  11. package/bin/win32/x64/node.lib +0 -0
  12. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  13. package/bin/win32-vulkan/arm64/node.lib +0 -0
  14. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/x64/node.lib +0 -0
  16. package/lib/binding.ts +18 -1
  17. package/package.json +1 -1
  18. package/src/DetokenizeWorker.cpp +1 -1
  19. package/src/EmbeddingWorker.cpp +17 -7
  20. package/src/EmbeddingWorker.h +2 -1
  21. package/src/LlamaCompletionWorker.cpp +8 -8
  22. package/src/LlamaCompletionWorker.h +2 -2
  23. package/src/LlamaContext.cpp +89 -27
  24. package/src/LlamaContext.h +2 -0
  25. package/src/TokenizeWorker.cpp +1 -1
  26. package/src/common.hpp +4 -4
  27. package/src/llama.cpp/.github/workflows/build.yml +240 -168
  28. package/src/llama.cpp/.github/workflows/docker.yml +8 -8
  29. package/src/llama.cpp/.github/workflows/python-lint.yml +8 -1
  30. package/src/llama.cpp/.github/workflows/server.yml +21 -14
  31. package/src/llama.cpp/CMakeLists.txt +14 -6
  32. package/src/llama.cpp/Sources/llama/llama.h +4 -0
  33. package/src/llama.cpp/cmake/arm64-apple-clang.cmake +16 -0
  34. package/src/llama.cpp/cmake/common.cmake +33 -0
  35. package/src/llama.cpp/cmake/x64-windows-llvm.cmake +11 -0
  36. package/src/llama.cpp/common/CMakeLists.txt +6 -4
  37. package/src/llama.cpp/common/arg.cpp +986 -770
  38. package/src/llama.cpp/common/arg.h +22 -22
  39. package/src/llama.cpp/common/common.cpp +212 -351
  40. package/src/llama.cpp/common/common.h +204 -117
  41. package/src/llama.cpp/common/json-schema-to-grammar.cpp +1 -1
  42. package/src/llama.cpp/common/log.cpp +50 -50
  43. package/src/llama.cpp/common/log.h +18 -18
  44. package/src/llama.cpp/common/ngram-cache.cpp +36 -36
  45. package/src/llama.cpp/common/ngram-cache.h +19 -19
  46. package/src/llama.cpp/common/sampling.cpp +163 -121
  47. package/src/llama.cpp/common/sampling.h +41 -20
  48. package/src/llama.cpp/common/speculative.cpp +274 -0
  49. package/src/llama.cpp/common/speculative.h +28 -0
  50. package/src/llama.cpp/docs/build.md +134 -161
  51. package/src/llama.cpp/examples/CMakeLists.txt +33 -14
  52. package/src/llama.cpp/examples/batched/CMakeLists.txt +1 -1
  53. package/src/llama.cpp/examples/batched/batched.cpp +19 -18
  54. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +1 -1
  55. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +10 -11
  56. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +1 -1
  57. package/src/llama.cpp/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +1 -1
  58. package/src/llama.cpp/examples/cvector-generator/CMakeLists.txt +1 -1
  59. package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +9 -9
  60. package/src/llama.cpp/examples/deprecation-warning/deprecation-warning.cpp +1 -1
  61. package/src/llama.cpp/examples/embedding/CMakeLists.txt +1 -1
  62. package/src/llama.cpp/examples/embedding/embedding.cpp +12 -12
  63. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +3 -2
  64. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +8 -8
  65. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +1 -1
  66. package/src/llama.cpp/examples/export-lora/export-lora.cpp +5 -5
  67. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +1 -1
  68. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +4 -7
  69. package/src/llama.cpp/examples/gen-docs/CMakeLists.txt +1 -1
  70. package/src/llama.cpp/examples/gen-docs/gen-docs.cpp +7 -7
  71. package/src/llama.cpp/examples/gguf/CMakeLists.txt +1 -1
  72. package/src/llama.cpp/examples/gguf-hash/CMakeLists.txt +8 -1
  73. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +1 -1
  74. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +2 -2
  75. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +1 -1
  76. package/src/llama.cpp/examples/gritlm/gritlm.cpp +18 -18
  77. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +1 -1
  78. package/src/llama.cpp/examples/imatrix/imatrix.cpp +31 -13
  79. package/src/llama.cpp/examples/infill/CMakeLists.txt +1 -1
  80. package/src/llama.cpp/examples/infill/infill.cpp +41 -87
  81. package/src/llama.cpp/examples/llama-bench/CMakeLists.txt +1 -1
  82. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +439 -459
  83. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +2 -0
  84. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +11 -14
  85. package/src/llama.cpp/examples/llava/CMakeLists.txt +10 -3
  86. package/src/llama.cpp/examples/llava/clip.cpp +263 -66
  87. package/src/llama.cpp/examples/llava/clip.h +8 -2
  88. package/src/llama.cpp/examples/llava/llava-cli.cpp +23 -23
  89. package/src/llama.cpp/examples/llava/llava.cpp +83 -22
  90. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +21 -21
  91. package/src/llama.cpp/examples/llava/qwen2vl-cli.cpp +581 -0
  92. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +1 -1
  93. package/src/llama.cpp/examples/lookahead/lookahead.cpp +26 -26
  94. package/src/llama.cpp/examples/lookup/CMakeLists.txt +4 -4
  95. package/src/llama.cpp/examples/lookup/lookup-create.cpp +7 -7
  96. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
  97. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +16 -15
  98. package/src/llama.cpp/examples/lookup/lookup.cpp +30 -30
  99. package/src/llama.cpp/examples/main/CMakeLists.txt +1 -1
  100. package/src/llama.cpp/examples/main/main.cpp +73 -114
  101. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +1 -1
  102. package/src/llama.cpp/examples/parallel/CMakeLists.txt +1 -1
  103. package/src/llama.cpp/examples/parallel/parallel.cpp +18 -19
  104. package/src/llama.cpp/examples/passkey/CMakeLists.txt +1 -1
  105. package/src/llama.cpp/examples/passkey/passkey.cpp +14 -14
  106. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +1 -1
  107. package/src/llama.cpp/examples/perplexity/perplexity.cpp +99 -120
  108. package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
  109. package/src/llama.cpp/examples/quantize/quantize.cpp +0 -3
  110. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +1 -1
  111. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +10 -9
  112. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +1 -1
  113. package/src/llama.cpp/examples/retrieval/retrieval.cpp +16 -16
  114. package/src/llama.cpp/examples/rpc/rpc-server.cpp +3 -1
  115. package/src/llama.cpp/examples/run/CMakeLists.txt +5 -0
  116. package/src/llama.cpp/examples/run/run.cpp +911 -0
  117. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +1 -1
  118. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +38 -21
  119. package/src/llama.cpp/examples/server/CMakeLists.txt +3 -16
  120. package/src/llama.cpp/examples/server/server.cpp +2073 -1339
  121. package/src/llama.cpp/examples/server/tests/requirements.txt +2 -2
  122. package/src/llama.cpp/examples/server/utils.hpp +354 -277
  123. package/src/llama.cpp/examples/simple/CMakeLists.txt +2 -2
  124. package/src/llama.cpp/examples/simple/simple.cpp +130 -94
  125. package/src/llama.cpp/examples/simple-chat/CMakeLists.txt +5 -0
  126. package/src/llama.cpp/examples/simple-chat/simple-chat.cpp +200 -0
  127. package/src/llama.cpp/examples/speculative/CMakeLists.txt +1 -1
  128. package/src/llama.cpp/examples/speculative/speculative.cpp +68 -64
  129. package/src/llama.cpp/examples/speculative-simple/CMakeLists.txt +5 -0
  130. package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +265 -0
  131. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +1 -1
  132. package/src/llama.cpp/examples/tokenize/tokenize.cpp +3 -3
  133. package/src/llama.cpp/examples/tts/CMakeLists.txt +5 -0
  134. package/src/llama.cpp/examples/tts/tts.cpp +932 -0
  135. package/src/llama.cpp/ggml/CMakeLists.txt +54 -36
  136. package/src/llama.cpp/ggml/include/ggml-backend.h +63 -34
  137. package/src/llama.cpp/ggml/include/ggml-blas.h +5 -3
  138. package/src/llama.cpp/ggml/include/ggml-cann.h +9 -7
  139. package/src/llama.cpp/ggml/include/ggml-cpp.h +38 -0
  140. package/src/llama.cpp/ggml/include/ggml-cpu.h +135 -0
  141. package/src/llama.cpp/ggml/include/ggml-cuda.h +12 -12
  142. package/src/llama.cpp/ggml/include/ggml-kompute.h +7 -3
  143. package/src/llama.cpp/ggml/include/ggml-metal.h +11 -7
  144. package/src/llama.cpp/ggml/include/ggml-opencl.h +26 -0
  145. package/src/llama.cpp/ggml/include/ggml-opt.h +216 -0
  146. package/src/llama.cpp/ggml/include/ggml-rpc.h +9 -5
  147. package/src/llama.cpp/ggml/include/ggml-sycl.h +18 -11
  148. package/src/llama.cpp/ggml/include/ggml-vulkan.h +10 -8
  149. package/src/llama.cpp/ggml/include/ggml.h +159 -417
  150. package/src/llama.cpp/ggml/src/CMakeLists.txt +121 -1155
  151. package/src/llama.cpp/ggml/src/ggml-alloc.c +23 -28
  152. package/src/llama.cpp/ggml/src/ggml-backend-impl.h +57 -36
  153. package/src/llama.cpp/ggml/src/ggml-backend-reg.cpp +552 -0
  154. package/src/llama.cpp/ggml/src/ggml-backend.cpp +306 -867
  155. package/src/llama.cpp/ggml/src/ggml-blas/CMakeLists.txt +87 -0
  156. package/src/llama.cpp/ggml/src/{ggml-blas.cpp → ggml-blas/ggml-blas.cpp} +216 -65
  157. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +76 -0
  158. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +456 -111
  159. package/src/llama.cpp/ggml/src/ggml-cann/common.h +6 -3
  160. package/src/llama.cpp/ggml/src/{ggml-cann.cpp → ggml-cann/ggml-cann.cpp} +343 -177
  161. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +2 -5
  162. package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +22 -9
  163. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +24 -13
  164. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +23 -13
  165. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +11 -0
  166. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +10 -0
  167. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +10 -0
  168. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +17 -0
  169. package/src/llama.cpp/ggml/src/ggml-common.h +42 -42
  170. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +336 -0
  171. package/src/llama.cpp/ggml/src/ggml-cpu/amx/amx.cpp +220 -0
  172. package/src/llama.cpp/ggml/src/ggml-cpu/amx/amx.h +8 -0
  173. package/src/llama.cpp/ggml/src/ggml-cpu/amx/common.h +91 -0
  174. package/src/llama.cpp/ggml/src/ggml-cpu/amx/mmq.cpp +2511 -0
  175. package/src/llama.cpp/ggml/src/ggml-cpu/amx/mmq.h +10 -0
  176. package/src/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +323 -0
  177. package/src/llama.cpp/ggml/src/{ggml-aarch64.c → ggml-cpu/ggml-cpu-aarch64.cpp} +1299 -246
  178. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.h +8 -0
  179. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-hbm.cpp +55 -0
  180. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-hbm.h +8 -0
  181. package/src/llama.cpp/ggml/src/{ggml-cpu-impl.h → ggml-cpu/ggml-cpu-impl.h} +14 -242
  182. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +10835 -0
  183. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.h +63 -0
  184. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-traits.cpp +36 -0
  185. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-traits.h +38 -0
  186. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +14123 -0
  187. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +628 -0
  188. package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.cpp +666 -0
  189. package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +152 -0
  190. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +8 -0
  191. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +104 -0
  192. package/src/llama.cpp/ggml/src/ggml-impl.h +393 -22
  193. package/src/llama.cpp/ggml/src/ggml-kompute/CMakeLists.txt +166 -0
  194. package/src/llama.cpp/ggml/src/{ggml-kompute.cpp → ggml-kompute/ggml-kompute.cpp} +360 -127
  195. package/src/llama.cpp/ggml/src/ggml-metal/CMakeLists.txt +105 -0
  196. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +288 -0
  197. package/src/llama.cpp/ggml/src/ggml-musa/CMakeLists.txt +107 -0
  198. package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +147 -0
  199. package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +4004 -0
  200. package/src/llama.cpp/ggml/src/ggml-opt.cpp +854 -0
  201. package/src/llama.cpp/ggml/src/ggml-quants.c +188 -10702
  202. package/src/llama.cpp/ggml/src/ggml-quants.h +78 -125
  203. package/src/llama.cpp/ggml/src/ggml-rpc/CMakeLists.txt +9 -0
  204. package/src/llama.cpp/ggml/src/{ggml-rpc.cpp → ggml-rpc/ggml-rpc.cpp} +478 -300
  205. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +84 -0
  206. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +3 -0
  207. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +36 -5
  208. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +259 -0
  209. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +3 -2
  210. package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +1 -1
  211. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +5 -5
  212. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +34 -35
  213. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +1030 -0
  214. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +76 -0
  215. package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +4 -4
  216. package/src/llama.cpp/ggml/src/{ggml-sycl.cpp → ggml-sycl/ggml-sycl.cpp} +3638 -4151
  217. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +3 -2
  218. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +6 -6
  219. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +75 -87
  220. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +7 -6
  221. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +56 -0
  222. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.hpp +11 -0
  223. package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +6 -0
  224. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +4 -3
  225. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +7 -7
  226. package/src/llama.cpp/ggml/src/ggml-sycl/tsembd.cpp +1 -0
  227. package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +4 -4
  228. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.cpp +141 -0
  229. package/src/llama.cpp/ggml/src/ggml-sycl/wkv6.hpp +10 -0
  230. package/src/llama.cpp/ggml/src/ggml-threading.cpp +12 -0
  231. package/src/llama.cpp/ggml/src/ggml-threading.h +14 -0
  232. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +92 -0
  233. package/src/llama.cpp/ggml/src/{ggml-vulkan.cpp → ggml-vulkan/ggml-vulkan.cpp} +2138 -887
  234. package/src/llama.cpp/ggml/src/{vulkan-shaders → ggml-vulkan/vulkan-shaders}/CMakeLists.txt +3 -1
  235. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +593 -0
  236. package/src/llama.cpp/ggml/src/ggml.c +4427 -20125
  237. package/src/llama.cpp/include/llama-cpp.h +25 -0
  238. package/src/llama.cpp/include/llama.h +93 -52
  239. package/src/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.inp +112 -0
  240. package/src/llama.cpp/models/ggml-vocab-roberta-bpe.gguf.out +46 -0
  241. package/src/llama.cpp/pocs/CMakeLists.txt +3 -1
  242. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +2 -2
  243. package/src/llama.cpp/pocs/vdot/q8dot.cpp +4 -3
  244. package/src/llama.cpp/pocs/vdot/vdot.cpp +8 -7
  245. package/src/llama.cpp/src/CMakeLists.txt +4 -8
  246. package/src/llama.cpp/src/llama-grammar.cpp +15 -15
  247. package/src/llama.cpp/src/llama-grammar.h +2 -5
  248. package/src/llama.cpp/src/llama-sampling.cpp +779 -194
  249. package/src/llama.cpp/src/llama-sampling.h +21 -2
  250. package/src/llama.cpp/src/llama-vocab.cpp +55 -10
  251. package/src/llama.cpp/src/llama-vocab.h +35 -11
  252. package/src/llama.cpp/src/llama.cpp +4317 -2979
  253. package/src/llama.cpp/src/unicode-data.cpp +2 -2
  254. package/src/llama.cpp/src/unicode.cpp +62 -51
  255. package/src/llama.cpp/src/unicode.h +9 -10
  256. package/src/llama.cpp/tests/CMakeLists.txt +48 -38
  257. package/src/llama.cpp/tests/test-arg-parser.cpp +15 -15
  258. package/src/llama.cpp/tests/test-backend-ops.cpp +324 -80
  259. package/src/llama.cpp/tests/test-barrier.cpp +1 -0
  260. package/src/llama.cpp/tests/test-chat-template.cpp +59 -9
  261. package/src/llama.cpp/tests/test-gguf.cpp +1303 -0
  262. package/src/llama.cpp/tests/test-grammar-integration.cpp +3 -6
  263. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -4
  264. package/src/llama.cpp/tests/test-llama-grammar.cpp +2 -4
  265. package/src/llama.cpp/tests/test-log.cpp +2 -2
  266. package/src/llama.cpp/tests/test-opt.cpp +853 -142
  267. package/src/llama.cpp/tests/test-quantize-fns.cpp +24 -21
  268. package/src/llama.cpp/tests/test-quantize-perf.cpp +16 -14
  269. package/src/llama.cpp/tests/test-rope.cpp +62 -20
  270. package/src/llama.cpp/tests/test-sampling.cpp +163 -138
  271. package/src/llama.cpp/tests/test-tokenizer-0.cpp +7 -7
  272. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +5 -5
  273. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +5 -5
  274. package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +0 -72
  275. package/src/llama.cpp/.github/workflows/nix-ci.yml +0 -79
  276. package/src/llama.cpp/.github/workflows/nix-flake-update.yml +0 -22
  277. package/src/llama.cpp/.github/workflows/nix-publish-flake.yml +0 -36
  278. package/src/llama.cpp/common/train.cpp +0 -1515
  279. package/src/llama.cpp/common/train.h +0 -233
  280. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +0 -5
  281. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +0 -1639
  282. package/src/llama.cpp/ggml/src/ggml-aarch64.h +0 -39
  283. package/src/llama.cpp/ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp +0 -600
  284. package/src/llama.cpp/tests/test-grad0.cpp +0 -1683
  285. /package/src/llama.cpp/ggml/{cmake → src/ggml-cpu/cmake}/FindSIMD.cmake +0 -0
  286. /package/src/llama.cpp/ggml/src/{llamafile → ggml-cpu/llamafile}/sgemm.h +0 -0
@@ -1,181 +1,892 @@
1
1
  #include "ggml.h"
2
+ #include "ggml-alloc.h"
3
+ #include "ggml-backend.h"
4
+ #include "ggml-cpu.h"
5
+ #include "ggml-opt.h"
2
6
 
3
7
  #include <cmath>
4
- #include <cstdio>
5
- #include <cstdlib>
6
- #include <cassert>
7
-
8
- #define MAX_NARGS 2
9
-
10
- #if defined(__GNUC__)
11
- #pragma GCC diagnostic ignored "-Wdouble-promotion"
12
- #endif
13
-
14
- //
15
- // logging
16
- //
17
- #define GGML_DEBUG 0
18
- #if (GGML_DEBUG >= 1)
19
- #define GGML_PRINT_DEBUG(...) printf(__VA_ARGS__)
20
- #else
21
- #define GGML_PRINT_DEBUG(...)
22
- #endif
23
-
24
- #if (GGML_DEBUG >= 5)
25
- #define GGML_PRINT_DEBUG_5(...) printf(__VA_ARGS__)
26
- #else
27
- #define GGML_PRINT_DEBUG_5(...)
28
- #endif
29
-
30
- #if (GGML_DEBUG >= 10)
31
- #define GGML_PRINT_DEBUG_10(...) printf(__VA_ARGS__)
32
- #else
33
- #define GGML_PRINT_DEBUG_10(...)
34
- #endif
35
-
36
- #define GGML_PRINT(...) printf(__VA_ARGS__)
37
-
38
-
39
- static float frand(void) {
40
- return (float)rand()/(float)RAND_MAX;
41
- }
42
-
43
- static struct ggml_tensor * get_random_tensor(
44
- struct ggml_context * ctx0, int ndims, int64_t ne[], float fmin, float fmax
45
- ) {
46
- struct ggml_tensor * result = ggml_new_tensor(ctx0, GGML_TYPE_F32, ndims, ne);
47
-
48
- switch (ndims) {
49
- case 1:
50
- for (int i0 = 0; i0 < ne[0]; i0++) {
51
- ((float *)result->data)[i0] = frand()*(fmax - fmin) + fmin;
8
+ #include <cinttypes>
9
+ #include <random>
10
+ #include <string>
11
+ #include <thread>
12
+ #include <vector>
13
+
14
+ static bool almost_equal(const double a, const double b, const double atol) {
15
+ return fabs(a - b) < atol;
16
+ }
17
+
18
+ constexpr int64_t ne_datapoint = 2;
19
+ constexpr int64_t ne_label = 1;
20
+ constexpr int64_t ndata = 6;
21
+
22
+ struct helper_ctx_data {
23
+ std::vector<ggml_opt_dataset_t> datasets_supervised;
24
+ std::vector<struct ggml_tensor *> data_batch;
25
+ std::vector<struct ggml_tensor *> labels_batch;
26
+
27
+ ggml_opt_dataset_t dataset_unsupervised;
28
+ struct ggml_context * ctx_static;
29
+ struct ggml_context * ctx_compute;
30
+ struct ggml_opt_params opt_params;
31
+ ggml_opt_context_t opt_ctx;
32
+ struct ggml_tensor * inputs;
33
+ struct ggml_tensor * weights;
34
+ struct ggml_tensor * outputs;
35
+ ggml_backend_buffer_t buf;
36
+ ggml_opt_result_t result;
37
+ ggml_opt_result_t result2;
38
+ };
39
+
40
+ // These default values make it easier to check optimization results vs. expected values.
41
+ static ggml_opt_optimizer_params helper_get_test_opt_pars(void * userdata) {
42
+ ggml_opt_optimizer_params result = ggml_opt_get_default_optimizer_params(userdata);
43
+ result.adamw.alpha = 1.0f;
44
+ result.adamw.beta1 = 0.0f;
45
+ result.adamw.beta2 = 0.0f;
46
+ result.adamw.eps = 0.0f;
47
+ return result;
48
+ }
49
+
50
+ static helper_ctx_data helper_get_ctx_data(
51
+ ggml_backend_sched_t backend_sched,
52
+ ggml_backend_t backend,
53
+ const bool init_opt_ctx = true,
54
+ const bool optimizer_defaults = true,
55
+ int64_t nbatch_logical = 1,
56
+ int64_t nbatch_physical = 1,
57
+ enum ggml_opt_loss_type loss_type = GGML_OPT_LOSS_TYPE_SUM) {
58
+ std::vector<ggml_opt_dataset_t> datasets(ndata);
59
+ for (int64_t ndata_shard = 1; ndata_shard <= ndata; ++ndata_shard) {
60
+ ggml_opt_dataset_t dataset = ggml_opt_dataset_init(ne_datapoint, ne_label, ndata, ndata_shard);
61
+
62
+ float * data = ggml_get_data_f32(ggml_opt_dataset_data( dataset));
63
+ float * labels = ggml_get_data_f32(ggml_opt_dataset_labels(dataset));
64
+
65
+ for (int64_t idata = 0; idata < ndata; ++idata) {
66
+ for (int64_t id = 0; id < ne_datapoint; ++id) {
67
+ data[ idata*ne_datapoint + id] = 16*idata + id;
52
68
  }
53
- break;
54
- case 2:
55
- for (int i1 = 0; i1 < ne[1]; i1++) {
56
- for (int i0 = 0; i0 < ne[0]; i0++) {
57
- ((float *)result->data)[i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
58
- }
69
+ for (int64_t il = 0; il < ne_label; ++il) {
70
+ labels[idata*ne_label + il] = 16*(16*idata + il);
59
71
  }
60
- break;
61
- case 3:
62
- for (int i2 = 0; i2 < ne[2]; i2++) {
63
- for (int i1 = 0; i1 < ne[1]; i1++) {
64
- for (int i0 = 0; i0 < ne[0]; i0++) {
65
- ((float *)result->data)[i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
72
+ }
73
+
74
+ datasets[ndata_shard-1] = dataset;
75
+ }
76
+
77
+ ggml_opt_dataset_t dataset_unsupervised = ggml_opt_dataset_init(1, 0, ndata, /*ndata_shard =*/ 1);
78
+
79
+ float * data = ggml_get_data_f32(ggml_opt_dataset_data(dataset_unsupervised));
80
+
81
+ for (int64_t idata = 0; idata < ndata; ++idata) {
82
+ data[idata] = idata;
83
+ }
84
+
85
+ struct ggml_context * ctx_static;
86
+ struct ggml_context * ctx_compute;
87
+ {
88
+ struct ggml_init_params params = {
89
+ /*.mem_size =*/ (2*ndata + 2)*ggml_tensor_overhead(),
90
+ /*.mem_buffer =*/ nullptr,
91
+ /*.no_alloc =*/ true,
92
+ };
93
+ ctx_static = ggml_init(params);
94
+ }
95
+ {
96
+ struct ggml_init_params params = {
97
+ /*.mem_size =*/ GGML_DEFAULT_GRAPH_SIZE*ggml_tensor_overhead() + 3*ggml_graph_overhead(),
98
+ /*.mem_buffer =*/ nullptr,
99
+ /*.no_alloc =*/ true,
100
+ };
101
+ ctx_compute = ggml_init(params);
102
+ }
103
+
104
+ std::vector<struct ggml_tensor *> data_batch(ndata);
105
+ std::vector<struct ggml_tensor *> labels_batch(ndata);
106
+ for (int64_t ndata_batch = 1; ndata_batch <= ndata; ++ndata_batch) {
107
+ data_batch[ndata_batch-1] = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, ndata_batch*ne_datapoint);
108
+ labels_batch[ndata_batch-1] = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, ndata_batch*ne_label);
109
+ }
110
+
111
+ struct ggml_tensor * inputs = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, nbatch_physical);
112
+ ggml_set_name(inputs, "inputs");
113
+
114
+ struct ggml_tensor * weights = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, 1);
115
+ ggml_set_name(weights, "weights");
116
+ ggml_set_param(ctx_static, weights);
117
+
118
+ struct ggml_tensor * intermediary = ggml_add(ctx_compute, inputs, weights);
119
+
120
+ struct ggml_tensor * outputs = ggml_scale(ctx_compute, intermediary, 1.0f);
121
+ ggml_set_name(outputs, "outputs");
122
+
123
+ ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx_static, backend);
124
+ const float w0 = float(ndata)/2;
125
+ ggml_backend_tensor_set(weights, &w0, 0, sizeof(float));
126
+
127
+ GGML_ASSERT(nbatch_logical % nbatch_physical == 0);
128
+ const int32_t opt_period = nbatch_logical / nbatch_physical;
129
+
130
+ struct ggml_opt_params opt_params = ggml_opt_default_params(backend_sched, ctx_compute, inputs, outputs, loss_type);
131
+ opt_params.opt_period = opt_period;
132
+ if (!optimizer_defaults) {
133
+ opt_params.get_opt_pars = helper_get_test_opt_pars;
134
+ }
135
+ ggml_opt_context_t opt_ctx = init_opt_ctx ? ggml_opt_init(opt_params) : nullptr;
136
+
137
+ ggml_opt_result_t result = ggml_opt_result_init();
138
+ ggml_opt_result_t result2 = ggml_opt_result_init();
139
+
140
+ return {datasets, data_batch, labels_batch, dataset_unsupervised, ctx_static, ctx_compute, opt_params, opt_ctx, inputs, weights, outputs, buf, result, result2};
141
+ }
142
+
143
+ static void helper_free_ctx_data(struct helper_ctx_data ctx_data) {
144
+ ggml_opt_result_free(ctx_data.result);
145
+ ggml_opt_result_free(ctx_data.result2);
146
+ ggml_opt_free(ctx_data.opt_ctx);
147
+ ggml_backend_buffer_free(ctx_data.buf);
148
+ ggml_free(ctx_data.ctx_static);
149
+ ggml_free(ctx_data.ctx_compute);
150
+ for (ggml_opt_dataset_t dataset : ctx_data.datasets_supervised) {
151
+ ggml_opt_dataset_free(dataset);
152
+ }
153
+ ggml_opt_dataset_free(ctx_data.dataset_unsupervised);
154
+ }
155
+
156
+ static void helper_after_test(
157
+ const char * func, const bool high_level, const std::string options,
158
+ const std::string subtest, const bool subtest_ok, int & ntest, int & npass) {
159
+ printf(" %s(high_level=%s%s, subtest=%s): ",
160
+ func, high_level ? "yes" : "no", options.c_str(), subtest.c_str());
161
+ if (subtest_ok) {
162
+ printf("\033[1;32mOK\033[0m\n");
163
+ npass++;
164
+ } else {
165
+ printf("\033[1;31mFAIL\033[0m\n");
166
+ }
167
+ ntest++;
168
+ }
169
+
170
+ static std::pair<int, int> test_dataset(ggml_backend_sched_t backend_sched, ggml_backend_t backend, const bool shuffle) {
171
+ int ntest = 0;
172
+ int npass = 0;
173
+
174
+ struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend);
175
+
176
+ for (int64_t ndata_shard = 1; ndata_shard <= ndata; ++ndata_shard) {
177
+ ggml_opt_dataset_t dataset = cd.datasets_supervised[ndata_shard-1];
178
+
179
+ if (shuffle) {
180
+ ggml_opt_dataset_shuffle(cd.opt_ctx, dataset, -1);
181
+ }
182
+
183
+ for (int64_t ndata_batch = 1; ndata_batch <= ndata; ++ndata_batch) {
184
+ if (ndata_batch % ndata_shard != 0) {
185
+ continue;
186
+ }
187
+ bool subtest_ok = true;
188
+
189
+ struct ggml_tensor * data_batch = cd.data_batch[ndata_batch-1];
190
+ struct ggml_tensor * labels_batch = cd.labels_batch[ndata_batch-1];
191
+
192
+ std::vector<float> data(ggml_nelements( data_batch));
193
+ std::vector<float> labels(ggml_nelements(labels_batch));
194
+
195
+ std::vector<int64_t> idata_shuffled;
196
+ const int64_t nbatches = ndata / ndata_batch;
197
+ for (int64_t ibatch = 0; ibatch < nbatches; ++ibatch) {
198
+ ggml_opt_dataset_get_batch(dataset, data_batch, labels_batch, ibatch);
199
+
200
+ ggml_backend_tensor_get( data_batch, data.data(), 0, ggml_nbytes( data_batch));
201
+ ggml_backend_tensor_get(labels_batch, labels.data(), 0, ggml_nbytes(labels_batch));
202
+
203
+ for (int64_t idata_batch = 0; idata_batch < ndata_batch; ++idata_batch) {
204
+ const int64_t idata = ibatch*ndata_batch + idata_batch;
205
+ const int64_t idata_found = data[idata_batch*ne_datapoint] / 16;
206
+ subtest_ok = subtest_ok && (shuffle || idata_found == idata);
207
+ idata_shuffled.push_back(idata_found);
208
+
209
+ for (int64_t id = 0; id < ne_datapoint; ++id) {
210
+ if (data[ idata_batch*ne_datapoint + id] != 16*idata_found + id) {
211
+ subtest_ok = false;
212
+ }
213
+ }
214
+ for (int64_t il = 0; il < ne_label; ++il) {
215
+ if (labels[idata_batch*ne_label + il] != 16*(16*idata_found + il)) {
216
+ subtest_ok = false;
217
+ }
66
218
  }
67
219
  }
68
220
  }
69
- break;
70
- case 4:
71
- for (int i3 = 0; i3 < ne[3]; i3++) {
72
- for (int i2 = 0; i2 < ne[2]; i2++) {
73
- for (int i1 = 0; i1 < ne[1]; i1++) {
74
- for (int i0 = 0; i0 < ne[0]; i0++) {
75
- ((float *)result->data)[i3*ne[2]*ne[1]*ne[0] + i2*ne[1]*ne[0] + i1*ne[0] + i0] = frand()*(fmax - fmin) + fmin;
76
- }
221
+
222
+ if (!shuffle || ndata % ndata_batch == 0) {
223
+ const int ndata_max = (ndata / ndata_batch) * ndata_batch;
224
+
225
+ for (int64_t idata = 0; subtest_ok && idata < ndata_max; ++idata) {
226
+ int ninstances = 0;
227
+ for (int64_t id : idata_shuffled) {
228
+ ninstances += id == idata;
77
229
  }
230
+ if (ninstances != 1) {
231
+ subtest_ok = false;
232
+ }
233
+ }
234
+ }
235
+
236
+ printf(" %s(shuffle=%s, ndata_shard=%" PRId64 ", ndata_batch=%" PRId64 "): ",
237
+ __func__, shuffle ? "yes" : "no", ndata_shard, ndata_batch);
238
+ if (subtest_ok) {
239
+ printf("\033[1;32mOK\033[0m\n");
240
+ npass++;
241
+ } else {
242
+ printf("\033[1;31mFAIL\033[0m\n");
243
+ }
244
+ ntest++;
245
+ }
246
+ }
247
+
248
+ helper_free_ctx_data(cd);
249
+
250
+ return std::make_pair(npass, ntest);
251
+ }
252
+
253
+ static std::pair<int, int> test_grad(ggml_backend_sched_t backend_sched, ggml_backend_t backend) {
254
+ int ntest = 0;
255
+ int npass = 0;
256
+
257
+ struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false,
258
+ /*nbatch_logical =*/ 999999, /*nbatch_physical =*/ 1);
259
+
260
+ std::vector<float> grad_history(ndata);
261
+ for (int64_t idata = 0; idata < ndata; ++idata) {
262
+ grad_history[idata] = NAN;
263
+ }
264
+
265
+ for (int idata = 0; idata < ndata; ++idata) {
266
+ const float idataf = idata;
267
+ ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs));
268
+ ggml_opt_forward_backward(cd.opt_ctx, cd.result);
269
+ ggml_backend_tensor_get(ggml_opt_grad_acc(cd.opt_ctx, cd.weights), grad_history.data() + idata, 0, sizeof(float));
270
+ }
271
+
272
+ {
273
+ bool subtest_ok = true;
274
+ for (int idata = 0; idata < ndata; ++idata) {
275
+ if (grad_history[idata] != idata + 1) {
276
+ subtest_ok = false;
277
+ }
278
+ }
279
+ printf(" %s(): ", __func__);
280
+ if (subtest_ok) {
281
+ printf("\033[1;32mOK\033[0m\n");
282
+ npass++;
283
+ } else {
284
+ printf("\033[1;31mFAIL\033[0m\n");
285
+ }
286
+ ntest++;
287
+ }
288
+
289
+ helper_free_ctx_data(cd);
290
+
291
+ return std::make_pair(npass, ntest);
292
+ }
293
+
294
+ static void helper_after_test_forward_backward(
295
+ const char * func, const bool high_level, const bool shuffle,
296
+ const std::string subtest, const bool subtest_ok, int & ntest, int & npass) {
297
+ std::string options = ", shuffle=";
298
+ options += shuffle ? "yes" : "no";
299
+ helper_after_test(func, high_level, options, subtest, subtest_ok, ntest, npass);
300
+ }
301
+
302
+ static std::pair<int, int> test_forward_backward(
303
+ ggml_backend_sched_t backend_sched, ggml_backend_t backend, const bool high_level, const bool shuffle) {
304
+ int ntest = 0;
305
+ int npass = 0;
306
+
307
+ struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false);
308
+ struct ggml_tensor * loss = ggml_opt_loss(cd.opt_ctx);
309
+
310
+ std::vector<float> loss_history(ndata);
311
+ for (int64_t idata = 0; idata < ndata; ++idata) {
312
+ loss_history[idata] = NAN;
313
+ }
314
+
315
+ {
316
+ int64_t ndata;
317
+ ggml_opt_result_ndata(cd.result, &ndata);
318
+ double loss;
319
+ double loss_unc;
320
+ ggml_opt_result_loss(cd.result, &loss, &loss_unc);
321
+ double accuracy;
322
+ double accuracy_unc;
323
+ ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc);
324
+ const bool subtest_ok = ndata == 0 && loss == 0.0 && std::isnan(loss_unc) && std::isnan(accuracy) && std::isnan(accuracy_unc);
325
+ helper_after_test_forward_backward(__func__, high_level, shuffle, "results_initial", subtest_ok, ntest, npass);
326
+ }
327
+
328
+ if (high_level) {
329
+ ggml_opt_dataset_t dataset = cd.dataset_unsupervised;
330
+ if (shuffle) {
331
+ ggml_opt_dataset_shuffle(cd.opt_ctx, dataset, -1);
332
+ }
333
+ ggml_opt_epoch(cd.opt_ctx, dataset, nullptr, cd.result, 0, nullptr, nullptr);
334
+ } else {
335
+ for (int idata = 0; idata < ndata; ++idata) {
336
+ const float idataf = idata;
337
+ ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs));
338
+ ggml_opt_forward(cd.opt_ctx, cd.result);
339
+ ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float));
340
+ }
341
+ }
342
+
343
+ {
344
+ float weights;
345
+ ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
346
+ const bool subtest_ok = weights == ndata/2;
347
+ helper_after_test_forward_backward(__func__, high_level, shuffle, "weights_after_forward", subtest_ok, ntest, npass);
348
+ }
349
+ {
350
+ int64_t ndata;
351
+ ggml_opt_result_ndata(cd.result, &ndata);
352
+ bool subtest_ok = ndata == 6;
353
+
354
+ double loss;
355
+ double loss_unc;
356
+ ggml_opt_result_loss(cd.result, &loss, &loss_unc);
357
+ subtest_ok = subtest_ok && loss == 33.0 && almost_equal(loss_unc, sqrt(3.5), 1e-10);
358
+
359
+ double accuracy;
360
+ double accuracy_unc;
361
+ ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc);
362
+ subtest_ok = subtest_ok && std::isnan(accuracy) && std::isnan(accuracy_unc);
363
+
364
+ helper_after_test_forward_backward(__func__, high_level, shuffle, "results_after_forward", subtest_ok, ntest, npass);
365
+ }
366
+
367
+ float w0;
368
+ ggml_backend_tensor_get(cd.weights, &w0, 0, sizeof(float));
369
+ for (int i = 0; i < 10; ++i) {
370
+ ggml_opt_forward_backward(cd.opt_ctx, nullptr);
371
+ }
372
+ ggml_backend_tensor_set(cd.weights, &w0, 0, sizeof(float));
373
+
374
+ ggml_opt_reset(cd.opt_ctx, /*optimizer =*/ false);
375
+ ggml_opt_result_reset(cd.result);
376
+
377
+ for (int64_t idata = 0; idata < ndata; ++idata) {
378
+ loss_history[idata] = NAN;
379
+ }
380
+
381
+ if (high_level) {
382
+ ggml_opt_dataset_t dataset = cd.dataset_unsupervised;
383
+ if (shuffle) {
384
+ ggml_opt_dataset_shuffle(cd.opt_ctx, dataset, -1);
385
+ }
386
+ ggml_opt_epoch(cd.opt_ctx, dataset, cd.result, nullptr, ndata, nullptr, nullptr);
387
+ } else {
388
+ for (int idata = 0; idata < ndata; ++idata) {
389
+ const float idataf = idata;
390
+ ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs));
391
+ ggml_opt_forward_backward(cd.opt_ctx, cd.result);
392
+ ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float));
393
+ }
394
+ }
395
+
396
+ {
397
+ float weights;
398
+ ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
399
+ const bool subtest_ok = weights == -ndata/2;
400
+ helper_after_test_forward_backward(__func__, high_level, shuffle, "weights_after_forward_backward", subtest_ok, ntest, npass);
401
+ }
402
+ {
403
+ int64_t ndata;
404
+ ggml_opt_result_ndata(cd.result, &ndata);
405
+ bool subtest_ok = ndata == 6;
406
+
407
+ double loss;
408
+ double loss_unc;
409
+ ggml_opt_result_loss(cd.result, &loss, &loss_unc);
410
+ subtest_ok = subtest_ok && loss == 18.0 && (shuffle || loss_unc == 0.0);
411
+
412
+ double accuracy;
413
+ double accuracy_unc;
414
+ ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc);
415
+ subtest_ok = subtest_ok && std::isnan(accuracy) && std::isnan(accuracy_unc);
416
+
417
+ helper_after_test_forward_backward(__func__, high_level, shuffle, "result_after_forward_backward", subtest_ok, ntest, npass);
418
+ }
419
+
420
+ helper_free_ctx_data(cd);
421
+
422
+ return std::make_pair(npass, ntest);
423
+ }
424
+
425
+ static std::pair<int, int> test_epoch_vs_fit(ggml_backend_sched_t backend_sched, ggml_backend_t backend) {
426
+ int ntest = 0;
427
+ int npass = 0;
428
+
429
+ float weights_epoch;
430
+ float weights_fit;
431
+
432
+ {
433
+ struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend, /*init_opt_ctx =*/ true);
434
+ ggml_opt_dataset_t dataset = cd.dataset_unsupervised;
435
+
436
+ ggml_opt_dataset_shuffle(cd.opt_ctx, dataset, -1);
437
+ ggml_opt_epoch(cd.opt_ctx, dataset, cd.result, nullptr, ndata, nullptr, nullptr);
438
+
439
+ ggml_backend_tensor_get(cd.weights, &weights_epoch, 0, ggml_nbytes(cd.weights));
440
+ helper_free_ctx_data(cd);
441
+ }
442
+ {
443
+ struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend, /*init_opt_ctx =*/ false);
444
+ ggml_opt_dataset_t dataset = cd.dataset_unsupervised;
445
+
446
+ ggml_opt_fit(backend_sched, cd.ctx_compute, cd.inputs, cd.outputs, dataset,
447
+ GGML_OPT_LOSS_TYPE_SUM, ggml_opt_get_default_optimizer_params, 1, 1, 0.0f, true);
448
+
449
+ ggml_backend_tensor_get(cd.weights, &weights_fit, 0, ggml_nbytes(cd.weights));
450
+ helper_free_ctx_data(cd);
451
+ }
452
+
453
+ const bool subtest_ok = weights_epoch == weights_fit;
454
+
455
+ printf(" %s(): ", __func__);
456
+ if (subtest_ok) {
457
+ printf("\033[1;32mOK\033[0m\n");
458
+ npass++;
459
+ } else {
460
+ printf("\033[1;31mFAIL\033[0m\n");
461
+ }
462
+ ntest++;
463
+
464
+ return std::make_pair(npass, ntest);
465
+ }
466
+
467
+ static void helper_after_test_idata_split(
468
+ const char * func, const bool high_level, const int epoch,
469
+ const std::string subtest, const bool subtest_ok, int & ntest, int & npass) {
470
+ std::string options = ", epoch=";
471
+ options += std::to_string(epoch);
472
+ helper_after_test(func, high_level, options, subtest, subtest_ok, ntest, npass);
473
+ }
474
+
475
+ static std::pair<int, int> test_idata_split(ggml_backend_sched_t backend_sched, ggml_backend_t backend, const bool high_level) {
476
+ int ntest = 0;
477
+ int npass = 0;
478
+
479
+ struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false);
480
+ struct ggml_tensor * loss = ggml_opt_loss(cd.opt_ctx);
481
+ const int idata_split = ndata * 2/3;
482
+
483
+ std::vector<float> loss_history(ndata);
484
+ for (int64_t idata = 0; idata < ndata; ++idata) {
485
+ loss_history[idata] = NAN;
486
+ }
487
+
488
+ for (int epoch = 1; epoch <= 4; ++epoch) {
489
+ if (high_level) {
490
+ ggml_opt_epoch(cd.opt_ctx, cd.dataset_unsupervised, cd.result, cd.result2, idata_split, nullptr, nullptr);
491
+ } else {
492
+ int idata = 0;
493
+ for (; idata < idata_split; ++idata) {
494
+ const float idataf = idata;
495
+ ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs));
496
+ ggml_opt_forward_backward(cd.opt_ctx, cd.result);
497
+ ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float));
498
+ }
499
+ for (; idata < ndata; ++idata) {
500
+ const float idataf = idata;
501
+ ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs));
502
+ ggml_opt_forward(cd.opt_ctx, cd.result2);
503
+ ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float));
504
+ }
505
+ }
506
+
507
+ {
508
+ float weights;
509
+ ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
510
+ const bool subtest_ok = weights == ndata/2 - epoch*idata_split;
511
+ helper_after_test_idata_split(__func__, high_level, epoch, "weights", subtest_ok, ntest, npass);
512
+ }
513
+ {
514
+ int64_t ndata_result;
515
+ ggml_opt_result_ndata(cd.result, &ndata_result);
516
+ bool subtest_ok = ndata_result == idata_split;
517
+
518
+ double loss;
519
+ double loss_unc;
520
+ ggml_opt_result_loss(cd.result, &loss, &loss_unc);
521
+ subtest_ok = subtest_ok && loss == 28.0 - epoch*16.0 && loss_unc == 0.0;
522
+
523
+ double accuracy;
524
+ double accuracy_unc;
525
+ ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc);
526
+ subtest_ok = subtest_ok && std::isnan(accuracy) && std::isnan(accuracy_unc);
527
+
528
+ helper_after_test_idata_split(__func__, high_level, epoch, "results_backward", subtest_ok, ntest, npass);
529
+ }
530
+ {
531
+ int64_t ndata_result;
532
+ ggml_opt_result_ndata(cd.result2, &ndata_result);
533
+ bool subtest_ok = ndata_result == ndata - idata_split;
534
+
535
+ double loss;
536
+ double loss_unc;
537
+ ggml_opt_result_loss(cd.result2, &loss, &loss_unc);
538
+ subtest_ok = subtest_ok && loss == 15.0 - epoch*8 && almost_equal(loss_unc, sqrt(0.5), 1e-10);
539
+
540
+ double accuracy;
541
+ double accuracy_unc;
542
+ ggml_opt_result_accuracy(cd.result2, &accuracy, &accuracy_unc);
543
+ subtest_ok = subtest_ok && std::isnan(accuracy) && std::isnan(accuracy_unc);
544
+
545
+ helper_after_test_idata_split(__func__, high_level, epoch, "results_forward", subtest_ok, ntest, npass);
546
+ }
547
+
548
+ ggml_opt_result_reset(cd.result);
549
+ ggml_opt_result_reset(cd.result2);
550
+ }
551
+
552
+ helper_free_ctx_data(cd);
553
+
554
+ return std::make_pair(npass, ntest);
555
+ }
556
+
557
+ static void helper_after_test_gradient_accumulation(
558
+ const char * func, const int nbatch_physical, const enum ggml_opt_loss_type loss_type, const int epoch,
559
+ const std::string subtest, const bool subtest_ok, int & ntest, int & npass) {
560
+ std::string options = ", nbatch_physical=";
561
+ options += std::to_string(nbatch_physical);
562
+ options += ", loss_type=";
563
+ options += loss_type == GGML_OPT_LOSS_TYPE_MEAN ? "mean" : "sum";
564
+ options += ", epoch=";
565
+ options += std::to_string(epoch);
566
+ helper_after_test(func, false, options, subtest, subtest_ok, ntest, npass);
567
+ }
568
+
569
+ static std::pair<int, int> test_gradient_accumulation(
570
+ ggml_backend_sched_t backend_sched, ggml_backend_t backend, const int32_t nbatch_physical, const enum ggml_opt_loss_type loss_type) {
571
+ int ntest = 0;
572
+ int npass = 0;
573
+
574
+ struct helper_ctx_data cd = helper_get_ctx_data(
575
+ backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false, /*nbatch_logical =*/ 6, nbatch_physical, loss_type);
576
+ struct ggml_tensor * loss = ggml_opt_loss(cd.opt_ctx);
577
+
578
+ std::vector<float> grad_history(ndata);
579
+ for (int64_t idata = 0; idata < ndata; ++idata) {
580
+ grad_history[idata] = NAN;
581
+ }
582
+
583
+ for (int epoch = 1; epoch <= 4; ++epoch) {
584
+ if (nbatch_physical == 1) {
585
+ for (int idata = 0; idata < ndata; ++idata) {
586
+ const float idataf = idata;
587
+ ggml_backend_tensor_set(cd.inputs, &idataf, 0, 1*sizeof(float));
588
+ ggml_opt_forward_backward(cd.opt_ctx, cd.result);
589
+ ggml_backend_tensor_get(ggml_opt_grad_acc(cd.opt_ctx, cd.weights), grad_history.data() + idata, 0, 1*sizeof(float));
590
+ }
591
+ } else if (nbatch_physical == 2) {
592
+ for (int idata = 0; idata < ndata; idata += 2) {
593
+ const float idataf[2] = {float(idata + 0), float(idata + 1)};
594
+ ggml_backend_tensor_set(cd.inputs, idataf, 0, 2*sizeof(float));
595
+ ggml_opt_forward_backward(cd.opt_ctx, cd.result);
596
+
597
+ grad_history[idata + 0] = 0.0f;
598
+ ggml_backend_tensor_get(ggml_opt_grad_acc(cd.opt_ctx, cd.weights), grad_history.data() + idata + 1, 0, 1*sizeof(float));
599
+ }
600
+ } else {
601
+ GGML_ASSERT(false);
602
+ }
603
+
604
+ {
605
+ GGML_ASSERT(ndata == 6);
606
+ constexpr double atol = 1e-6;
607
+ bool subtest_ok = true;
608
+ if (loss_type == GGML_OPT_LOSS_TYPE_SUM) {
609
+ if (nbatch_physical == 1) {
610
+ subtest_ok = subtest_ok && almost_equal(grad_history[0], 1.0, atol);
611
+ subtest_ok = subtest_ok && almost_equal(grad_history[2], 3.0, atol);
612
+ subtest_ok = subtest_ok && almost_equal(grad_history[4], 5.0, atol);
613
+ } else {
614
+ subtest_ok = subtest_ok && almost_equal(grad_history[0], 0.0, atol);
615
+ subtest_ok = subtest_ok && almost_equal(grad_history[2], 0.0, atol);
616
+ subtest_ok = subtest_ok && almost_equal(grad_history[4], 0.0, atol);
78
617
  }
618
+ subtest_ok = subtest_ok && almost_equal(grad_history[1], 2.0, atol);
619
+ subtest_ok = subtest_ok && almost_equal(grad_history[3], 4.0, atol);
620
+ subtest_ok = subtest_ok && almost_equal(grad_history[5], 0.0, atol);
621
+ } else if (loss_type == GGML_OPT_LOSS_TYPE_MEAN) {
622
+ if (nbatch_physical == 1) {
623
+ subtest_ok = subtest_ok && almost_equal(grad_history[0], 1.0/ndata, atol);
624
+ subtest_ok = subtest_ok && almost_equal(grad_history[2], 3.0/ndata, atol);
625
+ subtest_ok = subtest_ok && almost_equal(grad_history[4], 5.0/ndata, atol);
626
+ } else {
627
+ subtest_ok = subtest_ok && almost_equal(grad_history[0], 0.0/ndata, atol);
628
+ subtest_ok = subtest_ok && almost_equal(grad_history[2], 0.0/ndata, atol);
629
+ subtest_ok = subtest_ok && almost_equal(grad_history[4], 0.0/ndata, atol);
630
+ }
631
+ subtest_ok = subtest_ok && almost_equal(grad_history[1], 2.0/ndata, atol);
632
+ subtest_ok = subtest_ok && almost_equal(grad_history[3], 4.0/ndata, atol);
633
+ subtest_ok = subtest_ok && almost_equal(grad_history[5], 0.0/ndata, atol);
634
+ } else {
635
+ GGML_ASSERT(false);
636
+ }
637
+ helper_after_test_gradient_accumulation(__func__, nbatch_physical, loss_type, epoch, "grads", subtest_ok, ntest, npass);
638
+ }
639
+ {
640
+ float weights;
641
+ ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
642
+ const bool subtest_ok = weights == (ndata/2) - epoch;
643
+ helper_after_test_gradient_accumulation(__func__, nbatch_physical, loss_type, epoch, "weights", subtest_ok, ntest, npass);
644
+ }
645
+ {
646
+ int64_t ndata_result;
647
+ ggml_opt_result_ndata(cd.result, &ndata_result);
648
+ bool subtest_ok = ndata_result == ndata/nbatch_physical;
649
+
650
+ double loss;
651
+ ggml_opt_result_loss(cd.result, &loss, /*loss_unc =*/ nullptr);
652
+ if (loss_type == GGML_OPT_LOSS_TYPE_SUM) {
653
+ subtest_ok = subtest_ok && loss == (39.0 - epoch*6.0);
654
+ } else if (loss_type == GGML_OPT_LOSS_TYPE_MEAN) {
655
+ subtest_ok = subtest_ok && almost_equal(loss, (39.0 - epoch*6.0) / ndata, 1e-6);
656
+ } else {
657
+ GGML_ASSERT(false);
79
658
  }
80
- break;
81
- default:
82
- assert(false);
659
+
660
+ double accuracy;
661
+ double accuracy_unc;
662
+ ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc);
663
+ subtest_ok = subtest_ok && std::isnan(accuracy) && std::isnan(accuracy_unc);
664
+
665
+ helper_after_test_gradient_accumulation(__func__, nbatch_physical, loss_type, epoch, "results", subtest_ok, ntest, npass);
666
+ }
667
+
668
+ ggml_opt_result_reset(cd.result);
83
669
  }
84
670
 
671
+ helper_free_ctx_data(cd);
672
+
673
+ return std::make_pair(npass, ntest);
674
+ }
675
+
676
+ static ggml_opt_optimizer_params helper_get_regression_opt_pars(void * userdata) {
677
+ ggml_opt_optimizer_params result = ggml_opt_get_default_optimizer_params(userdata);
678
+ result.adamw.alpha = 0.1f;
85
679
  return result;
86
680
  }
87
681
 
88
- int main(void) {
89
- struct ggml_init_params params = {
90
- /* .mem_size = */ 1024*1024*1024,
91
- /* .mem_buffer = */ NULL,
92
- /* .no_alloc = */ false,
93
- };
682
+ static std::pair<int, int> test_regression(ggml_backend_sched_t backend_sched, ggml_backend_t backend) {
683
+ int ntest = 0;
684
+ int npass = 0;
94
685
 
95
- struct ggml_context * ctx = ggml_init(params);
686
+ // Test for simple regression with f(x) = a*x + b
96
687
 
97
- int64_t ne1[4] = {4, 128, 1, 1};
98
- int64_t ne2[4] = {4, 256, 1, 1};
99
- int64_t ne3[4] = {128, 256, 1, 1};
688
+ constexpr int64_t ndata_regression = 201;
689
+ constexpr float a_true = 1.2f;
690
+ constexpr float b_true = 3.4f;
100
691
 
101
- struct ggml_tensor * a = get_random_tensor(ctx, 2, ne1, -1, +1);
102
- struct ggml_tensor * b = get_random_tensor(ctx, 2, ne2, -1, +1);
103
- ggml_set_param(ctx, a);
104
- ggml_set_param(ctx, b);
692
+ std::mt19937 gen(12345);
693
+ std::normal_distribution<float> nd{0.0f, 0.1f};
105
694
 
106
- struct ggml_tensor * c = get_random_tensor(ctx, 2, ne3, -1, +1);
695
+ ggml_opt_dataset_t dataset = ggml_opt_dataset_init(1, 1, ndata_regression, ndata_regression);
107
696
 
108
- struct ggml_tensor * ab = ggml_mul_mat(ctx, a, b);
109
- struct ggml_tensor * d = ggml_sub(ctx, c, ab);
110
- struct ggml_tensor * e = ggml_sum(ctx, ggml_sqr(ctx, d));
697
+ float * data = ggml_get_data_f32(ggml_opt_dataset_data( dataset));
698
+ float * labels = ggml_get_data_f32(ggml_opt_dataset_labels(dataset));
111
699
 
112
- struct ggml_cgraph * ge = ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, true);
113
- ggml_build_forward_expand(ge, e);
114
- ggml_graph_reset(ge);
700
+ constexpr float x_min = -100.0f;
701
+ constexpr float x_max = 100.0f;
115
702
 
116
- ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1);
703
+ for (int64_t idata = 0; idata < ndata_regression; ++idata) {
704
+ const float x = x_min + (x_max - x_min) * idata/(ndata_regression-1);
705
+ const float y = a_true*x + b_true + nd(gen);
117
706
 
118
- const float fe = ggml_get_f32_1d(e, 0);
119
- printf("%s: e = %.4f\n", __func__, fe);
707
+ data[idata] = x;
708
+ labels[idata] = y;
709
+ }
120
710
 
121
- struct ggml_opt_params opt_params = ggml_opt_default_params(GGML_OPT_TYPE_ADAM);
711
+ struct ggml_context * ctx_static;
712
+ struct ggml_context * ctx_compute;
713
+ {
714
+ struct ggml_init_params params = {
715
+ /*.mem_size =*/ 3*ggml_tensor_overhead(),
716
+ /*.mem_buffer =*/ nullptr,
717
+ /*.no_alloc =*/ true,
718
+ };
719
+ ctx_static = ggml_init(params);
720
+ }
721
+ {
722
+ struct ggml_init_params params = {
723
+ /*.mem_size =*/ GGML_DEFAULT_GRAPH_SIZE*ggml_tensor_overhead() + 3*ggml_graph_overhead(),
724
+ /*.mem_buffer =*/ nullptr,
725
+ /*.no_alloc =*/ true,
726
+ };
727
+ ctx_compute = ggml_init(params);
728
+ }
122
729
 
123
- ggml_opt(ctx, opt_params, e);
730
+ // The first dimension is the dimension of the datapoints, the second dimension is the number of datapoints.
731
+ struct ggml_tensor * x = ggml_new_tensor_2d(ctx_static, GGML_TYPE_F32, 1, ndata_regression);
732
+ ggml_set_name(x, "x");
733
+
734
+ struct ggml_tensor * a = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, 1);
735
+ ggml_set_name(a, "a");
736
+ ggml_set_param(ctx_static, a);
737
+
738
+ struct ggml_tensor * b = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, 1);
739
+ ggml_set_name(b, "b");
740
+ ggml_set_param(ctx_static, b);
741
+
742
+ struct ggml_tensor * f = ggml_add(ctx_compute, ggml_mul(ctx_compute, x, a), b);
743
+ ggml_set_name(f, "f");
744
+ ggml_set_param(ctx_static, f);
745
+
746
+ ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors(ctx_static, backend);
747
+ const float a0 = 1.0f;
748
+ const float b0 = 3.0f;
749
+ ggml_backend_tensor_set(a, &a0, 0, sizeof(float));
750
+ ggml_backend_tensor_set(b, &b0, 0, sizeof(float));
751
+
752
+ ggml_opt_fit(backend_sched, ctx_compute, x, f, dataset, GGML_OPT_LOSS_TYPE_MEAN_SQUARED_ERROR,
753
+ helper_get_regression_opt_pars, 100, ndata_regression, 0.0f, true);
754
+
755
+ {
756
+ float a_fit;
757
+ ggml_backend_tensor_get(a, &a_fit, 0, sizeof(float));
758
+ float b_fit;
759
+ ggml_backend_tensor_get(b, &b_fit, 0, sizeof(float));
760
+ const bool subtest_ok = almost_equal(a_fit, a_true, 1e-2) && almost_equal(b_fit, b_true, 1e-2);
761
+ printf(" %s(subtest=weights): ", __func__);
762
+ if (subtest_ok) {
763
+ printf("\033[1;32mOK\033[0m\n");
764
+ npass++;
765
+ } else {
766
+ printf("\033[1;31mFAIL\033[0m\n");
767
+ }
768
+ ntest++;
769
+ }
124
770
 
125
- ggml_graph_reset(ge);
771
+ ggml_backend_buffer_free(buf);
772
+ ggml_free(ctx_static);
773
+ ggml_opt_dataset_free(dataset);
126
774
 
127
- ggml_graph_compute_with_ctx(ctx, ge, /*n_threads*/ 1);
775
+ return std::make_pair(npass, ntest);
776
+ }
128
777
 
129
- const float fe_opt = ggml_get_f32_1d(e, 0);
130
- printf("%s: original e = %.4f\n", __func__, fe);
131
- printf("%s: optimized e = %.4f\n", __func__, fe_opt);
778
+ static std::pair<int, int> test_backend(ggml_backend_sched_t backend_sched, ggml_backend_t backend) {
779
+ int npass = 0;
780
+ int ntest = 0;
132
781
 
133
- const bool success = (fe_opt <= fe);
134
- assert(success);
782
+ for (bool shuffle : {false, true}) {
783
+ std::pair<int, int> partial = test_dataset(backend_sched, backend, shuffle);
784
+ npass += partial.first;
785
+ ntest += partial.second;
786
+ }
787
+ {
788
+ std::pair<int, int> partial = test_grad(backend_sched, backend);
789
+ npass += partial.first;
790
+ ntest += partial.second;
791
+ }
792
+ for (bool high_level : {false, true}){
793
+ for (bool shuffle : {false, true}) {
794
+ if (!high_level && shuffle) {
795
+ continue;
796
+ }
135
797
 
136
- ggml_free(ctx);
137
- return success ? 0 : -1;
798
+ std::pair<int, int> partial = test_forward_backward(backend_sched, backend, high_level, shuffle);
799
+ npass += partial.first;
800
+ ntest += partial.second;
801
+ }
802
+ }
803
+ {
804
+ std::pair<int, int> partial = test_epoch_vs_fit(backend_sched, backend);
805
+ npass += partial.first;
806
+ ntest += partial.second;
807
+ }
808
+ for (bool high_level : {false, true}){
809
+ std::pair<int, int> partial = test_idata_split(backend_sched, backend, high_level);
810
+ npass += partial.first;
811
+ ntest += partial.second;
812
+ }
813
+ for (int32_t nbatch_physical : {2, 1}) {
814
+ for (enum ggml_opt_loss_type loss_type : {GGML_OPT_LOSS_TYPE_SUM, GGML_OPT_LOSS_TYPE_MEAN}) {
815
+ std::pair<int, int> partial = test_gradient_accumulation(backend_sched, backend, nbatch_physical, loss_type);
816
+ npass += partial.first;
817
+ ntest += partial.second;
818
+ }
819
+ }
820
+ {
821
+ std::pair<int, int> partial = test_regression(backend_sched, backend);
822
+ npass += partial.first;
823
+ ntest += partial.second;
824
+ }
825
+
826
+ return std::make_pair(npass, ntest);
138
827
  }
139
- // int64_t ne1[4] = {4, 128, 1, 1};
140
- // int64_t ne2[4] = {4, 256, 1, 1};;
141
- // int64_t ne3[4] = {128, 256, 1, 1};
142
- // main: original e = 25890.9375
143
- // main: optimized e = 10094.7031
144
828
 
145
- // int64_t ne1[4] = {8, 128, 1, 1};
146
- // int64_t ne2[4] = {8, 256, 1, 1};;
147
- // int64_t ne3[4] = {128, 256, 1, 1};
148
- // main: original e = 39429.5078
149
- // main: optimized e = 9275.8936
829
+ int main(void) {
830
+ const size_t dev_count = ggml_backend_dev_count();
831
+ printf("Testing %zu devices\n\n", dev_count);
832
+ size_t n_ok = 0;
833
+
834
+ std::vector<ggml_backend_dev_t> devs;
835
+ std::vector<ggml_backend_t> backends;
150
836
 
151
- // int64_t ne1[4] = {16, 128, 1, 1};
152
- // int64_t ne2[4] = {16, 256, 1, 1};;
153
- // int64_t ne3[4] = {128, 256, 1, 1};
154
- // main: original e = 68371.1328
155
- // main: optimized e = 7854.4502
837
+ for (size_t i = 0; i < dev_count; ++i) {
838
+ devs.push_back(ggml_backend_dev_get(i));
156
839
 
840
+ ggml_backend_t backend = ggml_backend_dev_init(devs[i], NULL);
841
+ GGML_ASSERT(backend != NULL);
157
842
 
158
- // int64_t ne1[4] = {32, 128, 1, 1};
159
- // int64_t ne2[4] = {32, 256, 1, 1};;
160
- // int64_t ne3[4] = {128, 256, 1, 1};
161
- // main: original e = 126061.1953
162
- // main: optimized e = 5451.0166
843
+ if (ggml_backend_is_cpu(backend)) {
844
+ ggml_backend_cpu_set_n_threads(backend, std::thread::hardware_concurrency() / 2);
845
+ }
846
+
847
+ backends.push_back(backend);
848
+ }
163
849
 
164
- // int64_t ne1[4] = {4, 1024, 1, 1};
165
- // int64_t ne2[4] = {4, 2048, 1, 1};;
166
- // int64_t ne3[4] = {1024, 2048, 1, 1};
167
- // main: original e = 1620817.8750
168
- // main: optimized e = 698387.6875
850
+ for (size_t i = 0; i < dev_count; ++i) {
851
+ // Put the backend to be tested in front so that it's prioritized:
852
+ std::vector<ggml_backend_t> backends_modded = {backends[i]};
853
+ backends_modded.insert(backends_modded.end(), backends.begin(), backends.end());
169
854
 
170
- // another run on M1
171
- // int64_t ne1[4] = {4, 1024, 1, 1};
172
- // int64_t ne2[4] = {4, 2048, 1, 1};;
173
- // int64_t ne3[4] = {1024, 2048, 1, 1};
174
- // main: original e = 1629595.6250
175
- // main: optimized e = 698169.1250
855
+ ggml_backend_sched_t backend_sched = ggml_backend_sched_new(
856
+ backends_modded.data(), nullptr, backends_modded.size(), GGML_DEFAULT_GRAPH_SIZE, false);
176
857
 
177
- // int64_t ne1[4] = {32, 1024, 1, 1};
178
- // int64_t ne2[4] = {32, 2048, 1, 1};;
179
- // int64_t ne3[4] = {1024, 2048, 1, 1};
180
- // main: original e = 8146770.5000
181
- // main: optimized e = 651119.1250
858
+ printf("Backend %zu/%zu: %s\n", i + 1, dev_count, ggml_backend_dev_name(devs[i]));
859
+ printf(" Device description: %s\n", ggml_backend_dev_description(devs[i]));
860
+ size_t free, total; // NOLINT
861
+ ggml_backend_dev_memory(devs[i], &free, &total);
862
+ printf(" Device memory: %zu MB (%zu MB free)\n", total / 1024 / 1024, free / 1024 / 1024);
863
+ printf("\n");
864
+
865
+ std::pair<int, int> result = test_backend(backend_sched, backends[i]);
866
+
867
+ printf(" %d/%d tests passed\n", result.first, result.second);
868
+ printf(" Backend %s: ", ggml_backend_name(backends[i]));
869
+ if (result.first == result.second) {
870
+ printf("\033[1;32mOK\033[0m\n");
871
+ n_ok++;
872
+ } else {
873
+ printf("\033[1;31mFAIL\033[0m\n");
874
+ }
875
+
876
+ printf("\n");
877
+
878
+ ggml_backend_sched_free(backend_sched);
879
+ }
880
+
881
+ for (ggml_backend_t backend : backends) {
882
+ ggml_backend_free(backend);
883
+ }
884
+
885
+ printf("%zu/%zu backends passed\n", n_ok, dev_count);
886
+ if (n_ok != dev_count) {
887
+ printf("\033[1;31mFAIL\033[0m\n");
888
+ return 1;
889
+ }
890
+ printf("\033[1;32mOK\033[0m\n");
891
+ return 0;
892
+ }