@fugood/llama.node 0.2.2 → 0.3.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 (320) hide show
  1. package/CMakeLists.txt +5 -2
  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 +8 -1
  17. package/package.json +1 -1
  18. package/patches/llama.patch +12 -12
  19. package/src/DetokenizeWorker.cpp +1 -1
  20. package/src/LlamaContext.cpp +33 -1
  21. package/src/LlamaContext.h +1 -0
  22. package/src/LoadSessionWorker.cpp +1 -0
  23. package/src/llama.cpp/.github/workflows/bench.yml +310 -0
  24. package/src/llama.cpp/.github/workflows/build.yml +1315 -0
  25. package/src/llama.cpp/.github/workflows/close-issue.yml +23 -0
  26. package/src/llama.cpp/.github/workflows/docker.yml +116 -0
  27. package/src/llama.cpp/.github/workflows/editorconfig.yml +27 -0
  28. package/src/llama.cpp/.github/workflows/gguf-publish.yml +44 -0
  29. package/src/llama.cpp/.github/workflows/labeler.yml +17 -0
  30. package/src/llama.cpp/.github/workflows/nix-ci-aarch64.yml +65 -0
  31. package/src/llama.cpp/.github/workflows/nix-ci.yml +72 -0
  32. package/src/llama.cpp/.github/workflows/nix-flake-update.yml +22 -0
  33. package/src/llama.cpp/.github/workflows/nix-publish-flake.yml +36 -0
  34. package/src/llama.cpp/.github/workflows/python-check-requirements.yml +35 -0
  35. package/src/llama.cpp/.github/workflows/python-lint.yml +23 -0
  36. package/src/llama.cpp/.github/workflows/python-type-check.yml +38 -0
  37. package/src/llama.cpp/.github/workflows/server.yml +183 -0
  38. package/src/llama.cpp/CMakeLists.txt +91 -1245
  39. package/src/llama.cpp/cmake/arm64-windows-llvm.cmake +1 -1
  40. package/src/llama.cpp/cmake/build-info.cmake +58 -0
  41. package/src/llama.cpp/cmake/git-vars.cmake +22 -0
  42. package/src/llama.cpp/common/CMakeLists.txt +4 -3
  43. package/src/llama.cpp/common/build-info.cpp.in +4 -0
  44. package/src/llama.cpp/common/common.cpp +1116 -877
  45. package/src/llama.cpp/common/common.h +191 -77
  46. package/src/llama.cpp/common/grammar-parser.cpp +118 -31
  47. package/src/llama.cpp/common/json-schema-to-grammar.cpp +346 -65
  48. package/src/llama.cpp/common/log.h +1 -1
  49. package/src/llama.cpp/common/ngram-cache.h +10 -3
  50. package/src/llama.cpp/common/sampling.cpp +19 -10
  51. package/src/llama.cpp/docs/build.md +353 -0
  52. package/src/llama.cpp/examples/CMakeLists.txt +22 -22
  53. package/src/llama.cpp/examples/baby-llama/CMakeLists.txt +1 -1
  54. package/src/llama.cpp/examples/baby-llama/baby-llama.cpp +6 -6
  55. package/src/llama.cpp/examples/batched/CMakeLists.txt +1 -1
  56. package/src/llama.cpp/examples/batched/batched.cpp +52 -55
  57. package/src/llama.cpp/examples/batched-bench/CMakeLists.txt +1 -1
  58. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +20 -72
  59. package/src/llama.cpp/examples/benchmark/CMakeLists.txt +1 -1
  60. package/src/llama.cpp/examples/chat-13B.bat +57 -0
  61. package/src/llama.cpp/examples/convert-llama2c-to-ggml/CMakeLists.txt +1 -1
  62. package/src/llama.cpp/examples/{finetune → cvector-generator}/CMakeLists.txt +2 -2
  63. package/src/llama.cpp/examples/cvector-generator/completions.txt +582 -0
  64. package/src/llama.cpp/examples/cvector-generator/cvector-generator.cpp +503 -0
  65. package/src/llama.cpp/examples/cvector-generator/mean.hpp +48 -0
  66. package/src/llama.cpp/examples/cvector-generator/negative.txt +4 -0
  67. package/src/llama.cpp/examples/cvector-generator/pca.hpp +325 -0
  68. package/src/llama.cpp/examples/cvector-generator/positive.txt +4 -0
  69. package/src/llama.cpp/examples/deprecation-warning/deprecation-warning.cpp +35 -0
  70. package/src/llama.cpp/examples/embedding/CMakeLists.txt +1 -1
  71. package/src/llama.cpp/examples/embedding/embedding.cpp +94 -46
  72. package/src/llama.cpp/examples/eval-callback/CMakeLists.txt +2 -2
  73. package/src/llama.cpp/examples/eval-callback/eval-callback.cpp +4 -6
  74. package/src/llama.cpp/examples/export-lora/CMakeLists.txt +1 -1
  75. package/src/llama.cpp/examples/export-lora/export-lora.cpp +344 -386
  76. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +2 -2
  77. package/src/llama.cpp/examples/gbnf-validator/gbnf-validator.cpp +30 -25
  78. package/src/llama.cpp/examples/gguf/CMakeLists.txt +1 -1
  79. package/src/llama.cpp/examples/gguf/gguf.cpp +5 -0
  80. package/src/llama.cpp/examples/gguf-hash/CMakeLists.txt +15 -0
  81. package/src/llama.cpp/examples/gguf-hash/deps/rotate-bits/rotate-bits.h +46 -0
  82. package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.c +295 -0
  83. package/src/llama.cpp/examples/gguf-hash/deps/sha1/sha1.h +52 -0
  84. package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.c +221 -0
  85. package/src/llama.cpp/examples/gguf-hash/deps/sha256/sha256.h +24 -0
  86. package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.c +42 -0
  87. package/src/llama.cpp/examples/gguf-hash/deps/xxhash/xxhash.h +7093 -0
  88. package/src/llama.cpp/examples/gguf-hash/gguf-hash.cpp +693 -0
  89. package/src/llama.cpp/examples/gguf-split/CMakeLists.txt +1 -1
  90. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +3 -3
  91. package/src/llama.cpp/examples/gritlm/CMakeLists.txt +1 -1
  92. package/src/llama.cpp/examples/gritlm/gritlm.cpp +6 -2
  93. package/src/llama.cpp/examples/imatrix/CMakeLists.txt +1 -1
  94. package/src/llama.cpp/examples/imatrix/imatrix.cpp +137 -176
  95. package/src/llama.cpp/examples/infill/CMakeLists.txt +1 -1
  96. package/src/llama.cpp/examples/infill/infill.cpp +38 -153
  97. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +175 -94
  98. package/src/llama.cpp/examples/llama.android/app/build.gradle.kts +65 -0
  99. package/src/llama.cpp/examples/llama.android/build.gradle.kts +6 -0
  100. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +68 -0
  101. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/CMakeLists.txt +11 -7
  102. package/src/llama.cpp/examples/llama.android/llama/src/main/cpp/llama-android.cpp +2 -2
  103. package/src/llama.cpp/examples/llama.android/settings.gradle.kts +18 -0
  104. package/src/llama.cpp/examples/llava/CMakeLists.txt +6 -5
  105. package/src/llama.cpp/examples/llava/android/build_64.sh +8 -0
  106. package/src/llama.cpp/examples/llava/clip.cpp +23 -14
  107. package/src/llama.cpp/examples/llava/llava-cli.cpp +8 -6
  108. package/src/llama.cpp/examples/llava/requirements.txt +3 -2
  109. package/src/llama.cpp/examples/lookahead/CMakeLists.txt +1 -1
  110. package/src/llama.cpp/examples/lookahead/lookahead.cpp +2 -1
  111. package/src/llama.cpp/examples/lookup/CMakeLists.txt +4 -4
  112. package/src/llama.cpp/examples/lookup/lookup-create.cpp +2 -0
  113. package/src/llama.cpp/examples/lookup/lookup-merge.cpp +4 -4
  114. package/src/llama.cpp/examples/lookup/lookup-stats.cpp +2 -2
  115. package/src/llama.cpp/examples/lookup/lookup.cpp +1 -1
  116. package/src/llama.cpp/examples/main/CMakeLists.txt +1 -1
  117. package/src/llama.cpp/examples/main/main.cpp +98 -75
  118. package/src/llama.cpp/examples/main-cmake-pkg/CMakeLists.txt +4 -5
  119. package/src/llama.cpp/examples/parallel/CMakeLists.txt +1 -1
  120. package/src/llama.cpp/examples/parallel/parallel.cpp +2 -1
  121. package/src/llama.cpp/examples/passkey/CMakeLists.txt +1 -1
  122. package/src/llama.cpp/examples/passkey/passkey.cpp +23 -43
  123. package/src/llama.cpp/examples/perplexity/CMakeLists.txt +1 -1
  124. package/src/llama.cpp/examples/perplexity/perplexity.cpp +13 -10
  125. package/src/llama.cpp/examples/quantize/CMakeLists.txt +1 -1
  126. package/src/llama.cpp/examples/quantize/quantize.cpp +37 -34
  127. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +1 -1
  128. package/src/llama.cpp/examples/quantize-stats/quantize-stats.cpp +1 -1
  129. package/src/llama.cpp/examples/retrieval/CMakeLists.txt +1 -1
  130. package/src/llama.cpp/examples/retrieval/retrieval.cpp +26 -77
  131. package/src/llama.cpp/examples/save-load-state/CMakeLists.txt +1 -1
  132. package/src/llama.cpp/examples/save-load-state/save-load-state.cpp +14 -7
  133. package/src/llama.cpp/examples/server/CMakeLists.txt +26 -2
  134. package/src/llama.cpp/examples/server/server.cpp +274 -671
  135. package/src/llama.cpp/examples/server/tests/requirements.txt +2 -2
  136. package/src/llama.cpp/examples/server/utils.hpp +28 -29
  137. package/src/llama.cpp/examples/simple/CMakeLists.txt +1 -1
  138. package/src/llama.cpp/examples/simple/simple.cpp +21 -29
  139. package/src/llama.cpp/examples/speculative/CMakeLists.txt +1 -1
  140. package/src/llama.cpp/examples/speculative/speculative.cpp +2 -1
  141. package/src/llama.cpp/examples/sycl/CMakeLists.txt +1 -1
  142. package/src/llama.cpp/examples/sycl/build.sh +23 -0
  143. package/src/llama.cpp/examples/sycl/run-llama2.sh +36 -0
  144. package/src/llama.cpp/examples/sycl/win-build-sycl.bat +33 -0
  145. package/src/llama.cpp/examples/sycl/win-run-llama2.bat +9 -0
  146. package/src/llama.cpp/examples/tokenize/CMakeLists.txt +1 -1
  147. package/src/llama.cpp/examples/tokenize/tokenize.cpp +16 -2
  148. package/src/llama.cpp/ggml/CMakeLists.txt +253 -0
  149. package/src/llama.cpp/{cmake → ggml/cmake}/FindSIMD.cmake +6 -6
  150. package/src/llama.cpp/{ggml-backend.h → ggml/include/ggml-backend.h} +22 -17
  151. package/src/llama.cpp/ggml/include/ggml-blas.h +23 -0
  152. package/src/llama.cpp/ggml/include/ggml-cann.h +125 -0
  153. package/src/llama.cpp/{ggml-cuda.h → ggml/include/ggml-cuda.h} +3 -0
  154. package/src/llama.cpp/{ggml-metal.h → ggml/include/ggml-metal.h} +1 -2
  155. package/src/llama.cpp/{ggml-sycl.h → ggml/include/ggml-sycl.h} +3 -10
  156. package/src/llama.cpp/{ggml.h → ggml/include/ggml.h} +80 -85
  157. package/src/llama.cpp/ggml/src/CMakeLists.txt +1329 -0
  158. package/src/llama.cpp/ggml/src/ggml-aarch64.c +2193 -0
  159. package/src/llama.cpp/ggml/src/ggml-aarch64.h +39 -0
  160. package/src/llama.cpp/{ggml-alloc.c → ggml/src/ggml-alloc.c} +100 -49
  161. package/src/llama.cpp/{ggml-backend-impl.h → ggml/src/ggml-backend-impl.h} +20 -8
  162. package/src/llama.cpp/{ggml-backend.c → ggml/src/ggml-backend.c} +307 -167
  163. package/src/llama.cpp/ggml/src/ggml-blas.cpp +367 -0
  164. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +198 -0
  165. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +230 -0
  166. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +2944 -0
  167. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +592 -0
  168. package/src/llama.cpp/ggml/src/ggml-cann/common.h +282 -0
  169. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +32 -0
  170. package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +17 -0
  171. package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +223 -0
  172. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +186 -0
  173. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +180 -0
  174. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +193 -0
  175. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +191 -0
  176. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +208 -0
  177. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +206 -0
  178. package/src/llama.cpp/ggml/src/ggml-cann.cpp +2023 -0
  179. package/src/llama.cpp/{ggml-common.h → ggml/src/ggml-common.h} +41 -7
  180. package/src/llama.cpp/{ggml-impl.h → ggml/src/ggml-impl.h} +113 -9
  181. package/src/llama.cpp/{ggml-kompute.cpp → ggml/src/ggml-kompute.cpp} +33 -18
  182. package/src/llama.cpp/{ggml-quants.c → ggml/src/ggml-quants.c} +1460 -940
  183. package/src/llama.cpp/{ggml-quants.h → ggml/src/ggml-quants.h} +19 -20
  184. package/src/llama.cpp/{ggml-rpc.cpp → ggml/src/ggml-rpc.cpp} +95 -72
  185. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +27 -0
  186. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +53 -0
  187. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +355 -0
  188. package/src/llama.cpp/ggml/src/ggml-sycl/concat.cpp +195 -0
  189. package/src/llama.cpp/ggml/src/ggml-sycl/concat.hpp +21 -0
  190. package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +547 -0
  191. package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +27 -0
  192. package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +698 -0
  193. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +1023 -0
  194. package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.hpp +27 -0
  195. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +3011 -0
  196. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.cpp +3031 -0
  197. package/src/llama.cpp/ggml/src/ggml-sycl/mmq.hpp +33 -0
  198. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +1027 -0
  199. package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.hpp +27 -0
  200. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +374 -0
  201. package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +35 -0
  202. package/src/llama.cpp/ggml/src/ggml-sycl/presets.hpp +66 -0
  203. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +275 -0
  204. package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +22 -0
  205. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.cpp +251 -0
  206. package/src/llama.cpp/ggml/src/ggml-sycl/softmax.hpp +24 -0
  207. package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +1140 -0
  208. package/src/llama.cpp/ggml/src/ggml-sycl.cpp +5314 -0
  209. package/src/llama.cpp/{ggml-vulkan.cpp → ggml/src/ggml-vulkan.cpp} +1781 -1868
  210. package/src/llama.cpp/{ggml.c → ggml/src/ggml.c} +1245 -2087
  211. package/src/llama.cpp/{sgemm.cpp → ggml/src/llamafile/sgemm.cpp} +21 -24
  212. package/src/llama.cpp/{sgemm.h → ggml/src/llamafile/sgemm.h} +1 -1
  213. package/src/llama.cpp/ggml/src/vulkan-shaders/CMakeLists.txt +5 -0
  214. package/src/llama.cpp/ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp +552 -0
  215. package/src/llama.cpp/{llama.h → include/llama.h} +175 -100
  216. package/src/llama.cpp/models/.editorconfig +1 -0
  217. package/src/llama.cpp/models/ggml-vocab-aquila.gguf +0 -0
  218. package/src/llama.cpp/models/ggml-vocab-baichuan.gguf +0 -0
  219. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf +0 -0
  220. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.inp +112 -0
  221. package/src/llama.cpp/models/ggml-vocab-bert-bge.gguf.out +46 -0
  222. package/src/llama.cpp/models/ggml-vocab-command-r.gguf +0 -0
  223. package/src/llama.cpp/models/ggml-vocab-command-r.gguf.inp +112 -0
  224. package/src/llama.cpp/models/ggml-vocab-command-r.gguf.out +46 -0
  225. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf +0 -0
  226. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.inp +112 -0
  227. package/src/llama.cpp/models/ggml-vocab-deepseek-coder.gguf.out +46 -0
  228. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf +0 -0
  229. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.inp +112 -0
  230. package/src/llama.cpp/models/ggml-vocab-deepseek-llm.gguf.out +46 -0
  231. package/src/llama.cpp/models/ggml-vocab-falcon.gguf +0 -0
  232. package/src/llama.cpp/models/ggml-vocab-falcon.gguf.inp +112 -0
  233. package/src/llama.cpp/models/ggml-vocab-falcon.gguf.out +46 -0
  234. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf +0 -0
  235. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.inp +112 -0
  236. package/src/llama.cpp/models/ggml-vocab-gpt-2.gguf.out +46 -0
  237. package/src/llama.cpp/models/ggml-vocab-gpt-neox.gguf +0 -0
  238. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf +0 -0
  239. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.inp +112 -0
  240. package/src/llama.cpp/models/ggml-vocab-llama-bpe.gguf.out +46 -0
  241. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf +0 -0
  242. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.inp +112 -0
  243. package/src/llama.cpp/models/ggml-vocab-llama-spm.gguf.out +46 -0
  244. package/src/llama.cpp/models/ggml-vocab-mpt.gguf +0 -0
  245. package/src/llama.cpp/models/ggml-vocab-mpt.gguf.inp +112 -0
  246. package/src/llama.cpp/models/ggml-vocab-mpt.gguf.out +46 -0
  247. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf +0 -0
  248. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.inp +112 -0
  249. package/src/llama.cpp/models/ggml-vocab-phi-3.gguf.out +46 -0
  250. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf +0 -0
  251. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.inp +112 -0
  252. package/src/llama.cpp/models/ggml-vocab-qwen2.gguf.out +46 -0
  253. package/src/llama.cpp/models/ggml-vocab-refact.gguf +0 -0
  254. package/src/llama.cpp/models/ggml-vocab-refact.gguf.inp +112 -0
  255. package/src/llama.cpp/models/ggml-vocab-refact.gguf.out +46 -0
  256. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf +0 -0
  257. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.inp +112 -0
  258. package/src/llama.cpp/models/ggml-vocab-starcoder.gguf.out +46 -0
  259. package/src/llama.cpp/pocs/vdot/CMakeLists.txt +2 -2
  260. package/src/llama.cpp/requirements/requirements-all.txt +12 -0
  261. package/src/llama.cpp/requirements/requirements-compare-llama-bench.txt +2 -0
  262. package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf.txt +3 -0
  263. package/src/llama.cpp/requirements/requirements-convert_hf_to_gguf_update.txt +3 -0
  264. package/src/llama.cpp/requirements/{requirements-convert.txt → requirements-convert_legacy_llama.txt} +1 -1
  265. package/src/llama.cpp/requirements/requirements-convert_llama_ggml_to_gguf.txt +1 -0
  266. package/src/llama.cpp/requirements/requirements-convert_lora_to_gguf.txt +2 -0
  267. package/src/llama.cpp/requirements/requirements-pydantic.txt +3 -0
  268. package/src/llama.cpp/requirements/requirements-test-tokenizer-random.txt +1 -0
  269. package/src/llama.cpp/requirements.txt +5 -4
  270. package/src/llama.cpp/scripts/build-info.sh +30 -0
  271. package/src/llama.cpp/scripts/install-oneapi.bat +19 -0
  272. package/src/llama.cpp/src/CMakeLists.txt +33 -0
  273. package/src/llama.cpp/src/llama-grammar.cpp +539 -0
  274. package/src/llama.cpp/src/llama-grammar.h +39 -0
  275. package/src/llama.cpp/src/llama-impl.h +26 -0
  276. package/src/llama.cpp/src/llama-sampling.cpp +635 -0
  277. package/src/llama.cpp/src/llama-sampling.h +56 -0
  278. package/src/llama.cpp/src/llama-vocab.cpp +1721 -0
  279. package/src/llama.cpp/src/llama-vocab.h +130 -0
  280. package/src/llama.cpp/{llama.cpp → src/llama.cpp} +5979 -5260
  281. package/src/llama.cpp/{unicode-data.cpp → src/unicode-data.cpp} +851 -802
  282. package/src/llama.cpp/{unicode.cpp → src/unicode.cpp} +52 -30
  283. package/src/llama.cpp/{unicode.h → src/unicode.h} +5 -1
  284. package/src/llama.cpp/tests/CMakeLists.txt +19 -20
  285. package/src/llama.cpp/tests/test-backend-ops.cpp +245 -67
  286. package/src/llama.cpp/tests/test-chat-template.cpp +57 -3
  287. package/src/llama.cpp/tests/test-double-float.cpp +2 -2
  288. package/src/llama.cpp/tests/test-grad0.cpp +2 -2
  289. package/src/llama.cpp/tests/test-grammar-integration.cpp +978 -31
  290. package/src/llama.cpp/tests/test-grammar-parser.cpp +423 -158
  291. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +508 -135
  292. package/src/llama.cpp/tests/test-llama-grammar.cpp +15 -9
  293. package/src/llama.cpp/tests/test-quantize-fns.cpp +1 -1
  294. package/src/llama.cpp/tests/test-quantize-perf.cpp +1 -1
  295. package/src/llama.cpp/tests/test-rope.cpp +3 -4
  296. package/src/llama.cpp/tests/test-sampling.cpp +5 -5
  297. package/src/llama.cpp/tests/test-tokenizer-0.cpp +6 -6
  298. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +20 -15
  299. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +22 -11
  300. package/bin/darwin/arm64/default.metallib +0 -0
  301. package/bin/darwin/x64/default.metallib +0 -0
  302. package/src/llama.cpp/examples/beam-search/CMakeLists.txt +0 -5
  303. package/src/llama.cpp/examples/beam-search/beam-search.cpp +0 -188
  304. package/src/llama.cpp/examples/finetune/finetune.cpp +0 -1862
  305. package/src/llama.cpp/examples/llama.android/llama/CMakeLists.txt +0 -55
  306. package/src/llama.cpp/examples/train-text-from-scratch/CMakeLists.txt +0 -5
  307. package/src/llama.cpp/examples/train-text-from-scratch/train-text-from-scratch.cpp +0 -1253
  308. package/src/llama.cpp/ggml-opencl.cpp +0 -2305
  309. package/src/llama.cpp/ggml-opencl.h +0 -36
  310. package/src/llama.cpp/ggml-sycl.cpp +0 -17340
  311. package/src/llama.cpp/ggml-vulkan-shaders.hpp +0 -81211
  312. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf-update.txt +0 -2
  313. package/src/llama.cpp/requirements/requirements-convert-hf-to-gguf.txt +0 -2
  314. package/src/llama.cpp/requirements/requirements-convert-llama-ggml-to-gguf.txt +0 -1
  315. package/src/llama.cpp/scripts/gen-build-info-cpp.cmake +0 -24
  316. /package/src/llama.cpp/{ggml-alloc.h → ggml/include/ggml-alloc.h} +0 -0
  317. /package/src/llama.cpp/{ggml-kompute.h → ggml/include/ggml-kompute.h} +0 -0
  318. /package/src/llama.cpp/{ggml-rpc.h → ggml/include/ggml-rpc.h} +0 -0
  319. /package/src/llama.cpp/{ggml-vulkan.h → ggml/include/ggml-vulkan.h} +0 -0
  320. /package/src/llama.cpp/{unicode-data.h → src/unicode-data.h} +0 -0
@@ -7,28 +7,79 @@
7
7
 
8
8
  #include <cassert>
9
9
 
10
- int main()
11
- {
12
- grammar_parser::parse_state parsed_grammar;
10
+ static const char * type_str(llama_gretype type) {
11
+ switch (type) {
12
+ case LLAMA_GRETYPE_CHAR: return "LLAMA_GRETYPE_CHAR";
13
+ case LLAMA_GRETYPE_CHAR_NOT: return "LLAMA_GRETYPE_CHAR_NOT";
14
+ case LLAMA_GRETYPE_CHAR_ALT: return "LLAMA_GRETYPE_CHAR_ALT";
15
+ case LLAMA_GRETYPE_CHAR_RNG_UPPER: return "LLAMA_GRETYPE_CHAR_RNG_UPPER";
16
+ case LLAMA_GRETYPE_RULE_REF: return "LLAMA_GRETYPE_RULE_REF";
17
+ case LLAMA_GRETYPE_ALT: return "LLAMA_GRETYPE_ALT";
18
+ case LLAMA_GRETYPE_END: return "LLAMA_GRETYPE_END";
19
+ default: return "?";
20
+ }
21
+ }
13
22
 
14
- const char *grammar_bytes = R"""(root ::= (expr "=" term "\n")+
15
- expr ::= term ([-+*/] term)*
16
- term ::= [0-9]+)""";
23
+ static void verify_parsing(const char *grammar_bytes, const std::vector<std::pair<std::string, uint32_t>> expected, const std::vector<llama_grammar_element> &expected_rules) {
24
+ uint32_t index = 0;
25
+ grammar_parser::parse_state parsed_grammar = grammar_parser::parse(grammar_bytes);
17
26
 
18
- parsed_grammar = grammar_parser::parse(grammar_bytes);
27
+ std::map<uint32_t, std::string> symbol_names;
28
+ for (auto it = parsed_grammar.symbol_ids.begin(); it != parsed_grammar.symbol_ids.end(); ++it) {
29
+ symbol_names[it->second] = it->first;
30
+ }
19
31
 
20
- std::vector<std::pair<std::string, uint32_t>> expected = {
21
- {"expr", 2},
22
- {"expr_5", 5},
23
- {"expr_6", 6},
24
- {"root", 0},
25
- {"root_1", 1},
26
- {"root_4", 4},
27
- {"term", 3},
28
- {"term_7", 7},
32
+ auto print_all = [&]() {
33
+ fprintf(stderr, " verify_parsing(R\"\"\"(%s)\"\"\", {\n", grammar_bytes);
34
+ for (auto it = parsed_grammar.symbol_ids.begin(); it != parsed_grammar.symbol_ids.end(); ++it) {
35
+ fprintf(stderr, " {\"%s\", %u},\n", it->first.c_str(), it->second);
36
+ }
37
+ fprintf(stderr, " }, {\n");
38
+ for (size_t i_rule = 0; i_rule < parsed_grammar.rules.size(); i_rule++) {
39
+ fprintf(stderr, " // %s (index %zu)\n", symbol_names[i_rule].c_str(), i_rule);
40
+ auto & rule = parsed_grammar.rules[i_rule];
41
+ for (uint32_t i = 0; i < rule.size(); i++) {
42
+ std::string rule_str;
43
+ fprintf(stderr, " {%s, ", type_str(rule[i].type));
44
+ if (rule[i].type == LLAMA_GRETYPE_CHAR || rule[i].type == LLAMA_GRETYPE_CHAR_ALT ||
45
+ rule[i].type == LLAMA_GRETYPE_CHAR_NOT || rule[i].type == LLAMA_GRETYPE_CHAR_RNG_UPPER) {
46
+ char c = rule[i].value;
47
+ if (c == '\n') {
48
+ fprintf(stderr, "'\\n'");
49
+ } else if (c == '\t') {
50
+ fprintf(stderr, "'\\t'");
51
+ } else if (c == '\r') {
52
+ fprintf(stderr, "'\\r'");
53
+ } else if (c == '\0') {
54
+ fprintf(stderr, "'\\0'");
55
+ } else {
56
+ fprintf(stderr, "'%c'", c);
57
+ }
58
+ } else if (rule[i].type == LLAMA_GRETYPE_RULE_REF) {
59
+ fprintf(stderr, "/* %s */ %u", symbol_names[rule[i].value].c_str(), rule[i].value);
60
+ } else {
61
+ fprintf(stderr, "%u", rule[i].value);
62
+ }
63
+ fprintf(stderr, "},\n");
64
+ }
65
+ }
66
+ fprintf(stderr, " });\n");
29
67
  };
30
68
 
31
- uint32_t index = 0;
69
+ if (getenv("TEST_GRAMMAR_PARSER_PRINT_ALL")) {
70
+ print_all();
71
+ fprintf(stderr, "\n");
72
+ return;
73
+ }
74
+
75
+ fprintf(stderr, "Testing grammar:%s\n", grammar_bytes);
76
+
77
+ if (parsed_grammar.symbol_ids.size() != expected.size()) {
78
+ fprintf(stderr, "Code to update expectation (set TEST_GRAMMAR_PARSER_PRINT_ALL=1 to print all):\n");
79
+ print_all();
80
+ assert(parsed_grammar.symbol_ids.size() == expected.size());
81
+ }
82
+
32
83
  for (auto it = parsed_grammar.symbol_ids.begin(); it != parsed_grammar.symbol_ids.end(); ++it)
33
84
  {
34
85
  std::string key = it->first;
@@ -38,51 +89,18 @@ term ::= [0-9]+)""";
38
89
  // pretty print error message before asserting
39
90
  if (expected_pair.first != key || expected_pair.second != value)
40
91
  {
92
+ fprintf(stderr, "index: %u\n", index);
41
93
  fprintf(stderr, "expected_pair: %s, %u\n", expected_pair.first.c_str(), expected_pair.second);
42
94
  fprintf(stderr, "actual_pair: %s, %u\n", key.c_str(), value);
43
95
  fprintf(stderr, "expected_pair != actual_pair\n");
96
+ fprintf(stderr, "Code to update expectation (set TEST_GRAMMAR_PARSER_PRINT_ALL=1 to print all):\n");
97
+ print_all();
44
98
  }
45
99
 
46
100
  assert(expected_pair.first == key && expected_pair.second == value);
47
101
 
48
102
  index++;
49
103
  }
50
- std::vector<llama_grammar_element> expected_rules = {
51
- {LLAMA_GRETYPE_RULE_REF, 4},
52
- {LLAMA_GRETYPE_END, 0},
53
- {LLAMA_GRETYPE_RULE_REF, 2},
54
- {LLAMA_GRETYPE_CHAR, 61},
55
- {LLAMA_GRETYPE_RULE_REF, 3},
56
- {LLAMA_GRETYPE_CHAR, 10},
57
- {LLAMA_GRETYPE_END, 0},
58
- {LLAMA_GRETYPE_RULE_REF, 3},
59
- {LLAMA_GRETYPE_RULE_REF, 6},
60
- {LLAMA_GRETYPE_END, 0},
61
- {LLAMA_GRETYPE_RULE_REF, 7},
62
- {LLAMA_GRETYPE_END, 0},
63
- {LLAMA_GRETYPE_RULE_REF, 1},
64
- {LLAMA_GRETYPE_RULE_REF, 4},
65
- {LLAMA_GRETYPE_ALT, 0},
66
- {LLAMA_GRETYPE_RULE_REF, 1},
67
- {LLAMA_GRETYPE_END, 0},
68
- {LLAMA_GRETYPE_CHAR, 45},
69
- {LLAMA_GRETYPE_CHAR_ALT, 43},
70
- {LLAMA_GRETYPE_CHAR_ALT, 42},
71
- {LLAMA_GRETYPE_CHAR_ALT, 47},
72
- {LLAMA_GRETYPE_RULE_REF, 3},
73
- {LLAMA_GRETYPE_END, 0},
74
- {LLAMA_GRETYPE_RULE_REF, 5},
75
- {LLAMA_GRETYPE_RULE_REF, 6},
76
- {LLAMA_GRETYPE_ALT, 0},
77
- {LLAMA_GRETYPE_END, 0},
78
- {LLAMA_GRETYPE_CHAR, 48},
79
- {LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
80
- {LLAMA_GRETYPE_RULE_REF, 7},
81
- {LLAMA_GRETYPE_ALT, 0},
82
- {LLAMA_GRETYPE_CHAR, 48},
83
- {LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
84
- {LLAMA_GRETYPE_END, 0},
85
- };
86
104
 
87
105
  index = 0;
88
106
  for (auto rule : parsed_grammar.rules)
@@ -97,28 +115,306 @@ term ::= [0-9]+)""";
97
115
  if (expected_element.type != element.type || expected_element.value != element.value)
98
116
  {
99
117
  fprintf(stderr, "index: %u\n", index);
100
- fprintf(stderr, "expected_element: %d, %u\n", expected_element.type, expected_element.value);
101
- fprintf(stderr, "actual_element: %d, %u\n", element.type, element.value);
118
+ fprintf(stderr, "expected_element: %s, %u\n", type_str(expected_element.type), expected_element.value);
119
+ fprintf(stderr, "actual_element: %s, %u\n", type_str(element.type), element.value);
102
120
  fprintf(stderr, "expected_element != actual_element\n");
121
+ fprintf(stderr, "all elements:\n");
122
+ fprintf(stderr, "Code to update expectation (set TEST_GRAMMAR_PARSER_PRINT_ALL=1 to print all):\n");
123
+ print_all();
103
124
  }
104
125
 
105
126
  assert(expected_element.type == element.type && expected_element.value == element.value);
106
127
  index++;
107
128
  }
108
129
  }
130
+ }
131
+
132
+ static void verify_failure(const char *grammar_bytes) {
133
+ fprintf(stderr, "Testing expected failure:%s\n", grammar_bytes);
134
+ auto result = grammar_parser::parse(grammar_bytes);
135
+ assert(result.rules.empty() && "should have failed");
136
+ }
137
+
138
+ int main()
139
+ {
140
+ verify_failure(R"""(
141
+ root ::= "a"{,}"
142
+ )""");
143
+
144
+ verify_failure(R"""(
145
+ root ::= "a"{,10}"
146
+ )""");
109
147
 
110
- const char *longer_grammar_bytes = R"""(
111
- root ::= (expr "=" ws term "\n")+
112
- expr ::= term ([-+*/] term)*
113
- term ::= ident | num | "(" ws expr ")" ws
114
- ident ::= [a-z] [a-z0-9_]* ws
115
- num ::= [0-9]+ ws
116
- ws ::= [ \t\n]*
117
- )""";
148
+ verify_parsing(R"""(
149
+ root ::= "a"
150
+ )""", {
151
+ {"root", 0},
152
+ }, {
153
+ // root (index 0)
154
+ {LLAMA_GRETYPE_CHAR, 'a'},
155
+ {LLAMA_GRETYPE_END, 0},
156
+ });
157
+
158
+ verify_parsing(R"""(
159
+ root ::= "a" | [bdx-z] | [^1-3]
160
+ )""", {
161
+ {"root", 0},
162
+ }, {
163
+ // root (index 0)
164
+ {LLAMA_GRETYPE_CHAR, 'a'},
165
+ {LLAMA_GRETYPE_ALT, 0},
166
+ {LLAMA_GRETYPE_CHAR, 'b'},
167
+ {LLAMA_GRETYPE_CHAR_ALT, 'd'},
168
+ {LLAMA_GRETYPE_CHAR_ALT, 'x'},
169
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, 'z'},
170
+ {LLAMA_GRETYPE_ALT, 0},
171
+ {LLAMA_GRETYPE_CHAR_NOT, '1'},
172
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, '3'},
173
+ {LLAMA_GRETYPE_END, 0},
174
+ });
175
+
176
+ verify_parsing(R"""(
177
+ root ::= a+
178
+ a ::= "a"
179
+ )""", {
180
+ {"a", 1},
181
+ {"root", 0},
182
+ {"root_2", 2},
183
+ }, {
184
+ // root (index 0)
185
+ {LLAMA_GRETYPE_RULE_REF, /* a */ 1},
186
+ {LLAMA_GRETYPE_RULE_REF, /* root_2 */ 2},
187
+ {LLAMA_GRETYPE_END, 0},
188
+ // a (index 1)
189
+ {LLAMA_GRETYPE_CHAR, 'a'},
190
+ {LLAMA_GRETYPE_END, 0},
191
+ // root_2 (index 2)
192
+ {LLAMA_GRETYPE_RULE_REF, /* a */ 1},
193
+ {LLAMA_GRETYPE_RULE_REF, /* root_2 */ 2},
194
+ {LLAMA_GRETYPE_ALT, 0},
195
+ {LLAMA_GRETYPE_END, 0},
196
+ });
197
+
198
+ verify_parsing(R"""(
199
+ root ::= "a"+
200
+ )""", {
201
+ {"root", 0},
202
+ {"root_1", 1},
203
+ }, {
204
+ // root (index 0)
205
+ {LLAMA_GRETYPE_CHAR, 'a'},
206
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
207
+ {LLAMA_GRETYPE_END, 0},
208
+ // root_1 (index 1)
209
+ {LLAMA_GRETYPE_CHAR, 'a'},
210
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
211
+ {LLAMA_GRETYPE_ALT, 0},
212
+ {LLAMA_GRETYPE_END, 0},
213
+ });
214
+
215
+ verify_parsing(R"""(
216
+ root ::= a?
217
+ a ::= "a"
218
+ )""", {
219
+ {"a", 1},
220
+ {"root", 0},
221
+ {"root_2", 2},
222
+ }, {
223
+ // root (index 0)
224
+ {LLAMA_GRETYPE_RULE_REF, /* root_2 */ 2},
225
+ {LLAMA_GRETYPE_END, 0},
226
+ // a (index 1)
227
+ {LLAMA_GRETYPE_CHAR, 'a'},
228
+ {LLAMA_GRETYPE_END, 0},
229
+ // root_2 (index 2)
230
+ {LLAMA_GRETYPE_RULE_REF, /* a */ 1},
231
+ {LLAMA_GRETYPE_ALT, 0},
232
+ {LLAMA_GRETYPE_END, 0},
233
+ });
118
234
 
119
- parsed_grammar = grammar_parser::parse(longer_grammar_bytes);
235
+ verify_parsing(R"""(
236
+ root ::= "a"?
237
+ )""", {
238
+ {"root", 0},
239
+ {"root_1", 1},
240
+ }, {
241
+ // root (index 0)
242
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
243
+ {LLAMA_GRETYPE_END, 0},
244
+ // root_1 (index 1)
245
+ {LLAMA_GRETYPE_CHAR, 'a'},
246
+ {LLAMA_GRETYPE_ALT, 0},
247
+ {LLAMA_GRETYPE_END, 0},
248
+ });
249
+
250
+ verify_parsing(R"""(
251
+ root ::= a*
252
+ a ::= "a"
253
+ )""", {
254
+ {"a", 1},
255
+ {"root", 0},
256
+ {"root_2", 2},
257
+ }, {
258
+ // root (index 0)
259
+ {LLAMA_GRETYPE_RULE_REF, /* root_2 */ 2},
260
+ {LLAMA_GRETYPE_END, 0},
261
+ // a (index 1)
262
+ {LLAMA_GRETYPE_CHAR, 'a'},
263
+ {LLAMA_GRETYPE_END, 0},
264
+ // root_2 (index 2)
265
+ {LLAMA_GRETYPE_RULE_REF, /* a */ 1},
266
+ {LLAMA_GRETYPE_RULE_REF, /* root_2 */ 2},
267
+ {LLAMA_GRETYPE_ALT, 0},
268
+ {LLAMA_GRETYPE_END, 0},
269
+ });
270
+
271
+ verify_parsing(R"""(
272
+ root ::= "a"*
273
+ )""", {
274
+ {"root", 0},
275
+ {"root_1", 1},
276
+ }, {
277
+ // root (index 0)
278
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
279
+ {LLAMA_GRETYPE_END, 0},
280
+ // root_1 (index 1)
281
+ {LLAMA_GRETYPE_CHAR, 'a'},
282
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
283
+ {LLAMA_GRETYPE_ALT, 0},
284
+ {LLAMA_GRETYPE_END, 0},
285
+ });
120
286
 
121
- expected = {
287
+ verify_parsing(R"""(
288
+ root ::= "a"{2}
289
+ )""", {
290
+ {"root", 0},
291
+ }, {
292
+ // root (index 0)
293
+ {LLAMA_GRETYPE_CHAR, 'a'},
294
+ {LLAMA_GRETYPE_CHAR, 'a'},
295
+ {LLAMA_GRETYPE_END, 0},
296
+ });
297
+
298
+ verify_parsing(R"""(
299
+ root ::= "a"{2,}
300
+ )""", {
301
+ {"root", 0},
302
+ {"root_1", 1},
303
+ }, {
304
+ // root (index 0)
305
+ {LLAMA_GRETYPE_CHAR, 'a'},
306
+ {LLAMA_GRETYPE_CHAR, 'a'},
307
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
308
+ {LLAMA_GRETYPE_END, 0},
309
+ // root_1 (index 1)
310
+ {LLAMA_GRETYPE_CHAR, 'a'},
311
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
312
+ {LLAMA_GRETYPE_ALT, 0},
313
+ {LLAMA_GRETYPE_END, 0},
314
+ });
315
+
316
+ verify_parsing(R"""(
317
+ root ::= "a"{ 4}
318
+ )""", {
319
+ {"root", 0},
320
+ }, {
321
+ // root (index 0)
322
+ {LLAMA_GRETYPE_CHAR, 'a'},
323
+ {LLAMA_GRETYPE_CHAR, 'a'},
324
+ {LLAMA_GRETYPE_CHAR, 'a'},
325
+ {LLAMA_GRETYPE_CHAR, 'a'},
326
+ {LLAMA_GRETYPE_END, 0},
327
+ });
328
+
329
+ verify_parsing(R"""(
330
+ root ::= "a"{2,4}
331
+ )""", {
332
+ {"root", 0},
333
+ {"root_1", 1},
334
+ {"root_2", 2},
335
+ }, {
336
+ // root (index 0)
337
+ {LLAMA_GRETYPE_CHAR, 'a'},
338
+ {LLAMA_GRETYPE_CHAR, 'a'},
339
+ {LLAMA_GRETYPE_RULE_REF, /* root_2 */ 2},
340
+ {LLAMA_GRETYPE_END, 0},
341
+ // root_1 (index 1)
342
+ {LLAMA_GRETYPE_CHAR, 'a'},
343
+ {LLAMA_GRETYPE_ALT, 0},
344
+ {LLAMA_GRETYPE_END, 0},
345
+ // root_2 (index 2)
346
+ {LLAMA_GRETYPE_CHAR, 'a'},
347
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
348
+ {LLAMA_GRETYPE_ALT, 0},
349
+ {LLAMA_GRETYPE_END, 0},
350
+ });
351
+
352
+ verify_parsing(R"""(
353
+ root ::= (expr "=" term "\n")+
354
+ expr ::= term ([-+*/] term)*
355
+ term ::= [0-9]+
356
+ )""", {
357
+ {"expr", 2},
358
+ {"expr_5", 5},
359
+ {"expr_6", 6},
360
+ {"root", 0},
361
+ {"root_1", 1},
362
+ {"root_4", 4},
363
+ {"term", 3},
364
+ {"term_7", 7},
365
+ }, {
366
+ // root (index 0)
367
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
368
+ {LLAMA_GRETYPE_RULE_REF, /* root_4 */ 4},
369
+ {LLAMA_GRETYPE_END, 0},
370
+ // root_1 (index 1)
371
+ {LLAMA_GRETYPE_RULE_REF, /* expr */ 2},
372
+ {LLAMA_GRETYPE_CHAR, '='},
373
+ {LLAMA_GRETYPE_RULE_REF, /* term */ 3},
374
+ {LLAMA_GRETYPE_CHAR, '\n'},
375
+ {LLAMA_GRETYPE_END, 0},
376
+ // expr (index 2)
377
+ {LLAMA_GRETYPE_RULE_REF, /* term */ 3},
378
+ {LLAMA_GRETYPE_RULE_REF, /* expr_6 */ 6},
379
+ {LLAMA_GRETYPE_END, 0},
380
+ // term (index 3)
381
+ {LLAMA_GRETYPE_CHAR, '0'},
382
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, '9'},
383
+ {LLAMA_GRETYPE_RULE_REF, /* term_7 */ 7},
384
+ {LLAMA_GRETYPE_END, 0},
385
+ // root_4 (index 4)
386
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
387
+ {LLAMA_GRETYPE_RULE_REF, /* root_4 */ 4},
388
+ {LLAMA_GRETYPE_ALT, 0},
389
+ {LLAMA_GRETYPE_END, 0},
390
+ // expr_5 (index 5)
391
+ {LLAMA_GRETYPE_CHAR, '-'},
392
+ {LLAMA_GRETYPE_CHAR_ALT, '+'},
393
+ {LLAMA_GRETYPE_CHAR_ALT, '*'},
394
+ {LLAMA_GRETYPE_CHAR_ALT, '/'},
395
+ {LLAMA_GRETYPE_RULE_REF, /* term */ 3},
396
+ {LLAMA_GRETYPE_END, 0},
397
+ // expr_6 (index 6)
398
+ {LLAMA_GRETYPE_RULE_REF, /* expr_5 */ 5},
399
+ {LLAMA_GRETYPE_RULE_REF, /* expr_6 */ 6},
400
+ {LLAMA_GRETYPE_ALT, 0},
401
+ {LLAMA_GRETYPE_END, 0},
402
+ // term_7 (index 7)
403
+ {LLAMA_GRETYPE_CHAR, '0'},
404
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, '9'},
405
+ {LLAMA_GRETYPE_RULE_REF, /* term_7 */ 7},
406
+ {LLAMA_GRETYPE_ALT, 0},
407
+ {LLAMA_GRETYPE_END, 0},
408
+ });
409
+
410
+ verify_parsing(R"""(
411
+ root ::= (expr "=" ws term "\n")+
412
+ expr ::= term ([-+*/] term)*
413
+ term ::= ident | num | "(" ws expr ")" ws
414
+ ident ::= [a-z] [a-z0-9_]* ws
415
+ num ::= [0-9]+ ws
416
+ ws ::= [ \t\n]*
417
+ )""", {
122
418
  {"expr", 2},
123
419
  {"expr_6", 6},
124
420
  {"expr_7", 7},
@@ -132,119 +428,88 @@ term ::= [0-9]+)""";
132
428
  {"term", 4},
133
429
  {"ws", 3},
134
430
  {"ws_12", 12},
135
- };
136
-
137
- index = 0;
138
- for (auto it = parsed_grammar.symbol_ids.begin(); it != parsed_grammar.symbol_ids.end(); ++it)
139
- {
140
- std::string key = it->first;
141
- uint32_t value = it->second;
142
- std::pair<std::string, uint32_t> expected_pair = expected[index];
143
-
144
- // pretty print error message before asserting
145
- if (expected_pair.first != key || expected_pair.second != value)
146
- {
147
- fprintf(stderr, "expected_pair: %s, %u\n", expected_pair.first.c_str(), expected_pair.second);
148
- fprintf(stderr, "actual_pair: %s, %u\n", key.c_str(), value);
149
- fprintf(stderr, "expected_pair != actual_pair\n");
150
- }
151
-
152
- assert(expected_pair.first == key && expected_pair.second == value);
153
-
154
- index++;
155
- }
156
- expected_rules = {
157
- {LLAMA_GRETYPE_RULE_REF, 5},
431
+ }, {
432
+ // root (index 0)
433
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
434
+ {LLAMA_GRETYPE_RULE_REF, /* root_5 */ 5},
158
435
  {LLAMA_GRETYPE_END, 0},
159
- {LLAMA_GRETYPE_RULE_REF, 2},
160
- {LLAMA_GRETYPE_CHAR, 61},
161
- {LLAMA_GRETYPE_RULE_REF, 3},
162
- {LLAMA_GRETYPE_RULE_REF, 4},
163
- {LLAMA_GRETYPE_CHAR, 10},
436
+ // root_1 (index 1)
437
+ {LLAMA_GRETYPE_RULE_REF, /* expr */ 2},
438
+ {LLAMA_GRETYPE_CHAR, '='},
439
+ {LLAMA_GRETYPE_RULE_REF, /* ws */ 3},
440
+ {LLAMA_GRETYPE_RULE_REF, /* term */ 4},
441
+ {LLAMA_GRETYPE_CHAR, '\n'},
164
442
  {LLAMA_GRETYPE_END, 0},
165
- {LLAMA_GRETYPE_RULE_REF, 4},
166
- {LLAMA_GRETYPE_RULE_REF, 7},
443
+ // expr (index 2)
444
+ {LLAMA_GRETYPE_RULE_REF, /* term */ 4},
445
+ {LLAMA_GRETYPE_RULE_REF, /* expr_7 */ 7},
167
446
  {LLAMA_GRETYPE_END, 0},
168
- {LLAMA_GRETYPE_RULE_REF, 12},
447
+ // ws (index 3)
448
+ {LLAMA_GRETYPE_RULE_REF, /* ws_12 */ 12},
169
449
  {LLAMA_GRETYPE_END, 0},
170
- {LLAMA_GRETYPE_RULE_REF, 8},
450
+ // term (index 4)
451
+ {LLAMA_GRETYPE_RULE_REF, /* ident */ 8},
171
452
  {LLAMA_GRETYPE_ALT, 0},
172
- {LLAMA_GRETYPE_RULE_REF, 9},
453
+ {LLAMA_GRETYPE_RULE_REF, /* num */ 9},
173
454
  {LLAMA_GRETYPE_ALT, 0},
174
- {LLAMA_GRETYPE_CHAR, 40},
175
- {LLAMA_GRETYPE_RULE_REF, 3},
176
- {LLAMA_GRETYPE_RULE_REF, 2},
177
- {LLAMA_GRETYPE_CHAR, 41},
178
- {LLAMA_GRETYPE_RULE_REF, 3},
455
+ {LLAMA_GRETYPE_CHAR, '('},
456
+ {LLAMA_GRETYPE_RULE_REF, /* ws */ 3},
457
+ {LLAMA_GRETYPE_RULE_REF, /* expr */ 2},
458
+ {LLAMA_GRETYPE_CHAR, ')'},
459
+ {LLAMA_GRETYPE_RULE_REF, /* ws */ 3},
179
460
  {LLAMA_GRETYPE_END, 0},
180
- {LLAMA_GRETYPE_RULE_REF, 1},
181
- {LLAMA_GRETYPE_RULE_REF, 5},
461
+ // root_5 (index 5)
462
+ {LLAMA_GRETYPE_RULE_REF, /* root_1 */ 1},
463
+ {LLAMA_GRETYPE_RULE_REF, /* root_5 */ 5},
182
464
  {LLAMA_GRETYPE_ALT, 0},
183
- {LLAMA_GRETYPE_RULE_REF, 1},
184
465
  {LLAMA_GRETYPE_END, 0},
185
- {LLAMA_GRETYPE_CHAR, 45},
186
- {LLAMA_GRETYPE_CHAR_ALT, 43},
187
- {LLAMA_GRETYPE_CHAR_ALT, 42},
188
- {LLAMA_GRETYPE_CHAR_ALT, 47},
189
- {LLAMA_GRETYPE_RULE_REF, 4},
466
+ // expr_6 (index 6)
467
+ {LLAMA_GRETYPE_CHAR, '-'},
468
+ {LLAMA_GRETYPE_CHAR_ALT, '+'},
469
+ {LLAMA_GRETYPE_CHAR_ALT, '*'},
470
+ {LLAMA_GRETYPE_CHAR_ALT, '/'},
471
+ {LLAMA_GRETYPE_RULE_REF, /* term */ 4},
190
472
  {LLAMA_GRETYPE_END, 0},
191
- {LLAMA_GRETYPE_RULE_REF, 6},
192
- {LLAMA_GRETYPE_RULE_REF, 7},
473
+ // expr_7 (index 7)
474
+ {LLAMA_GRETYPE_RULE_REF, /* expr_6 */ 6},
475
+ {LLAMA_GRETYPE_RULE_REF, /* expr_7 */ 7},
193
476
  {LLAMA_GRETYPE_ALT, 0},
194
477
  {LLAMA_GRETYPE_END, 0},
195
- {LLAMA_GRETYPE_CHAR, 97},
196
- {LLAMA_GRETYPE_CHAR_RNG_UPPER, 122},
197
- {LLAMA_GRETYPE_RULE_REF, 10},
198
- {LLAMA_GRETYPE_RULE_REF, 3},
478
+ // ident (index 8)
479
+ {LLAMA_GRETYPE_CHAR, 'a'},
480
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, 'z'},
481
+ {LLAMA_GRETYPE_RULE_REF, /* ident_10 */ 10},
482
+ {LLAMA_GRETYPE_RULE_REF, /* ws */ 3},
199
483
  {LLAMA_GRETYPE_END, 0},
200
- {LLAMA_GRETYPE_RULE_REF, 11},
201
- {LLAMA_GRETYPE_RULE_REF, 3},
484
+ // num (index 9)
485
+ {LLAMA_GRETYPE_CHAR, '0'},
486
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, '9'},
487
+ {LLAMA_GRETYPE_RULE_REF, /* num_11 */ 11},
488
+ {LLAMA_GRETYPE_RULE_REF, /* ws */ 3},
202
489
  {LLAMA_GRETYPE_END, 0},
203
- {LLAMA_GRETYPE_CHAR, 97},
204
- {LLAMA_GRETYPE_CHAR_RNG_UPPER, 122},
205
- {LLAMA_GRETYPE_CHAR_ALT, 48},
206
- {LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
207
- {LLAMA_GRETYPE_CHAR_ALT, 95},
208
- {LLAMA_GRETYPE_RULE_REF, 10},
490
+ // ident_10 (index 10)
491
+ {LLAMA_GRETYPE_CHAR, 'a'},
492
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, 'z'},
493
+ {LLAMA_GRETYPE_CHAR_ALT, '0'},
494
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, '9'},
495
+ {LLAMA_GRETYPE_CHAR_ALT, '_'},
496
+ {LLAMA_GRETYPE_RULE_REF, /* ident_10 */ 10},
209
497
  {LLAMA_GRETYPE_ALT, 0},
210
498
  {LLAMA_GRETYPE_END, 0},
211
- {LLAMA_GRETYPE_CHAR, 48},
212
- {LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
213
- {LLAMA_GRETYPE_RULE_REF, 11},
499
+ // num_11 (index 11)
500
+ {LLAMA_GRETYPE_CHAR, '0'},
501
+ {LLAMA_GRETYPE_CHAR_RNG_UPPER, '9'},
502
+ {LLAMA_GRETYPE_RULE_REF, /* num_11 */ 11},
214
503
  {LLAMA_GRETYPE_ALT, 0},
215
- {LLAMA_GRETYPE_CHAR, 48},
216
- {LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
217
504
  {LLAMA_GRETYPE_END, 0},
218
- {LLAMA_GRETYPE_CHAR, 32},
219
- {LLAMA_GRETYPE_CHAR_ALT, 9},
220
- {LLAMA_GRETYPE_CHAR_ALT, 10},
221
- {LLAMA_GRETYPE_RULE_REF, 12},
505
+ // ws_12 (index 12)
506
+ {LLAMA_GRETYPE_CHAR, ' '},
507
+ {LLAMA_GRETYPE_CHAR_ALT, '\t'},
508
+ {LLAMA_GRETYPE_CHAR_ALT, '\n'},
509
+ {LLAMA_GRETYPE_RULE_REF, /* ws_12 */ 12},
222
510
  {LLAMA_GRETYPE_ALT, 0},
223
511
  {LLAMA_GRETYPE_END, 0},
224
- };
225
-
226
- index = 0;
227
- for (auto rule : parsed_grammar.rules)
228
- {
229
- // compare rule to expected rule
230
- for (uint32_t i = 0; i < rule.size(); i++)
231
- {
232
- llama_grammar_element element = rule[i];
233
- llama_grammar_element expected_element = expected_rules[index];
234
-
235
- // pretty print error message before asserting
236
- if (expected_element.type != element.type || expected_element.value != element.value)
237
- {
238
- fprintf(stderr, "index: %u\n", index);
239
- fprintf(stderr, "expected_element: %d, %u\n", expected_element.type, expected_element.value);
240
- fprintf(stderr, "actual_element: %d, %u\n", element.type, element.value);
241
- fprintf(stderr, "expected_element != actual_element\n");
242
- }
243
-
244
- assert(expected_element.type == element.type && expected_element.value == element.value);
245
- index++;
246
- }
247
- }
512
+ });
248
513
 
249
514
  return 0;
250
515
  }