@fugood/llama.node 0.3.15 → 0.3.17

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 (203) hide show
  1. package/CMakeLists.txt +3 -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-cuda/arm64/llama-node.node +0 -0
  7. package/bin/linux-cuda/x64/llama-node.node +0 -0
  8. package/bin/linux-vulkan/arm64/llama-node.node +0 -0
  9. package/bin/linux-vulkan/x64/llama-node.node +0 -0
  10. package/bin/win32/arm64/llama-node.node +0 -0
  11. package/bin/win32/arm64/node.lib +0 -0
  12. package/bin/win32/x64/llama-node.node +0 -0
  13. package/bin/win32/x64/node.lib +0 -0
  14. package/bin/win32-vulkan/arm64/llama-node.node +0 -0
  15. package/bin/win32-vulkan/arm64/node.lib +0 -0
  16. package/bin/win32-vulkan/x64/llama-node.node +0 -0
  17. package/bin/win32-vulkan/x64/node.lib +0 -0
  18. package/lib/binding.ts +5 -0
  19. package/package.json +1 -1
  20. package/src/LlamaCompletionWorker.cpp +8 -0
  21. package/src/LlamaCompletionWorker.h +1 -0
  22. package/src/LlamaContext.cpp +3 -2
  23. package/src/llama.cpp/.github/workflows/build-linux-cross.yml +124 -0
  24. package/src/llama.cpp/.github/workflows/build.yml +70 -27
  25. package/src/llama.cpp/.github/workflows/docker.yml +6 -6
  26. package/src/llama.cpp/.github/workflows/server.yml +7 -11
  27. package/src/llama.cpp/CMakeLists.txt +23 -1
  28. package/src/llama.cpp/common/CMakeLists.txt +6 -3
  29. package/src/llama.cpp/common/arg.cpp +809 -105
  30. package/src/llama.cpp/common/arg.h +9 -0
  31. package/src/llama.cpp/common/chat.cpp +1 -1
  32. package/src/llama.cpp/common/common.cpp +31 -521
  33. package/src/llama.cpp/common/common.h +17 -36
  34. package/src/llama.cpp/common/json-schema-to-grammar.cpp +3 -0
  35. package/src/llama.cpp/common/llguidance.cpp +30 -47
  36. package/src/llama.cpp/common/minja/chat-template.hpp +15 -7
  37. package/src/llama.cpp/common/minja/minja.hpp +119 -93
  38. package/src/llama.cpp/common/sampling.cpp +3 -0
  39. package/src/llama.cpp/docs/build.md +122 -7
  40. package/src/llama.cpp/examples/CMakeLists.txt +0 -9
  41. package/src/llama.cpp/examples/batched/batched.cpp +1 -1
  42. package/src/llama.cpp/examples/batched-bench/batched-bench.cpp +1 -1
  43. package/src/llama.cpp/examples/embedding/embedding.cpp +7 -1
  44. package/src/llama.cpp/examples/export-lora/export-lora.cpp +1 -1
  45. package/src/llama.cpp/examples/gguf-split/gguf-split.cpp +15 -16
  46. package/src/llama.cpp/examples/gritlm/gritlm.cpp +1 -1
  47. package/src/llama.cpp/examples/llama-bench/llama-bench.cpp +210 -8
  48. package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
  49. package/src/llama.cpp/examples/llava/CMakeLists.txt +39 -24
  50. package/src/llama.cpp/examples/llava/clip-impl.h +345 -0
  51. package/src/llama.cpp/examples/llava/clip.cpp +2152 -1803
  52. package/src/llama.cpp/examples/llava/clip.h +39 -22
  53. package/src/llama.cpp/examples/llava/deprecation-warning.cpp +22 -0
  54. package/src/llama.cpp/examples/llava/llava.cpp +64 -52
  55. package/src/llama.cpp/examples/llava/mtmd-cli.cpp +344 -0
  56. package/src/llama.cpp/examples/llava/mtmd.cpp +708 -0
  57. package/src/llama.cpp/examples/llava/mtmd.h +168 -0
  58. package/src/llama.cpp/examples/llava/{qwen2vl-cli.cpp → qwen2vl-test.cpp} +83 -31
  59. package/src/llama.cpp/examples/main/main.cpp +16 -5
  60. package/src/llama.cpp/examples/parallel/parallel.cpp +3 -1
  61. package/src/llama.cpp/examples/passkey/passkey.cpp +1 -1
  62. package/src/llama.cpp/examples/perplexity/perplexity.cpp +17 -3
  63. package/src/llama.cpp/examples/quantize/quantize.cpp +115 -2
  64. package/src/llama.cpp/examples/rpc/CMakeLists.txt +4 -2
  65. package/src/llama.cpp/examples/rpc/rpc-server.cpp +163 -8
  66. package/src/llama.cpp/examples/run/CMakeLists.txt +12 -1
  67. package/src/llama.cpp/examples/run/run.cpp +14 -28
  68. package/src/llama.cpp/examples/server/httplib.h +313 -247
  69. package/src/llama.cpp/examples/server/server.cpp +243 -139
  70. package/src/llama.cpp/examples/server/utils.hpp +51 -2
  71. package/src/llama.cpp/examples/speculative/speculative.cpp +1 -1
  72. package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +1 -1
  73. package/src/llama.cpp/examples/sycl/build.sh +2 -2
  74. package/src/llama.cpp/examples/sycl/win-build-sycl.bat +2 -2
  75. package/src/llama.cpp/examples/tts/tts.cpp +14 -9
  76. package/src/llama.cpp/ggml/CMakeLists.txt +8 -2
  77. package/src/llama.cpp/ggml/cmake/GitVars.cmake +22 -0
  78. package/src/llama.cpp/ggml/include/ggml-cpu.h +5 -0
  79. package/src/llama.cpp/ggml/include/ggml-rpc.h +6 -1
  80. package/src/llama.cpp/ggml/include/ggml.h +66 -99
  81. package/src/llama.cpp/ggml/src/CMakeLists.txt +15 -8
  82. package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +0 -2
  83. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +8 -4
  84. package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +5 -5
  85. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +692 -1534
  86. package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +613 -122
  87. package/src/llama.cpp/ggml/src/ggml-cann/common.h +135 -1
  88. package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +507 -137
  89. package/src/llama.cpp/ggml/src/ggml-common.h +12 -6
  90. package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +48 -22
  91. package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
  92. package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
  93. package/src/llama.cpp/ggml/src/ggml-cpu/common.h +72 -0
  94. package/src/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +1 -1
  95. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +2413 -228
  96. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +2 -21
  97. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +754 -404
  98. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +1004 -13516
  99. package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +2 -0
  100. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +2 -7
  101. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +0 -1
  102. package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +3 -4
  103. package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +533 -88
  104. package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +8809 -0
  105. package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +110 -0
  106. package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +892 -0
  107. package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
  108. package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.h +28 -0
  109. package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +258 -0
  110. package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +802 -0
  111. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +7 -0
  112. package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +1 -0
  113. package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +0 -4
  114. package/src/llama.cpp/ggml/src/ggml-impl.h +52 -18
  115. package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +70 -3
  116. package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +67 -119
  117. package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +1023 -260
  118. package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +293 -40
  119. package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +127 -33
  120. package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +1 -0
  121. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +350 -0
  122. package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.hpp +39 -0
  123. package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +0 -35
  124. package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +29 -293
  125. package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +79 -90
  126. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +967 -438
  127. package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +22 -23
  128. package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +12 -43
  129. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +24 -20
  130. package/src/llama.cpp/ggml/src/ggml-sycl/getrows.hpp +1 -4
  131. package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +210 -286
  132. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +84 -74
  133. package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +1 -3
  134. package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +37 -49
  135. package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +7 -22
  136. package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +4 -14
  137. package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +204 -118
  138. package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +1 -3
  139. package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +23 -0
  140. package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +692 -126
  141. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +12 -0
  142. package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +21 -10
  143. package/src/llama.cpp/ggml/src/ggml.c +141 -245
  144. package/src/llama.cpp/ggml/src/gguf.cpp +1 -0
  145. package/src/llama.cpp/include/llama.h +30 -11
  146. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.inp +112 -0
  147. package/src/llama.cpp/models/ggml-vocab-llama4.gguf.out +46 -0
  148. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +112 -0
  149. package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.out +46 -0
  150. package/src/llama.cpp/requirements/requirements-all.txt +2 -0
  151. package/src/llama.cpp/requirements/requirements-gguf_editor_gui.txt +3 -0
  152. package/src/llama.cpp/src/CMakeLists.txt +3 -2
  153. package/src/llama.cpp/src/llama-adapter.cpp +37 -1
  154. package/src/llama.cpp/src/llama-arch.cpp +161 -17
  155. package/src/llama.cpp/src/llama-arch.h +16 -0
  156. package/src/llama.cpp/src/llama-chat.cpp +82 -17
  157. package/src/llama.cpp/src/llama-chat.h +6 -2
  158. package/src/llama.cpp/src/llama-context.cpp +108 -92
  159. package/src/llama.cpp/src/llama-context.h +1 -2
  160. package/src/llama.cpp/src/llama-graph.cpp +189 -119
  161. package/src/llama.cpp/src/llama-graph.h +26 -6
  162. package/src/llama.cpp/src/llama-hparams.h +13 -0
  163. package/src/llama.cpp/src/llama-kv-cache.cpp +70 -123
  164. package/src/llama.cpp/src/llama-kv-cache.h +41 -115
  165. package/src/llama.cpp/src/llama-memory.h +1 -1
  166. package/src/llama.cpp/src/llama-mmap.cpp +1 -1
  167. package/src/llama.cpp/src/llama-model-loader.cpp +10 -5
  168. package/src/llama.cpp/src/llama-model-loader.h +5 -3
  169. package/src/llama.cpp/src/llama-model.cpp +1544 -291
  170. package/src/llama.cpp/src/llama-model.h +13 -1
  171. package/src/llama.cpp/src/llama-quant.cpp +29 -8
  172. package/src/llama.cpp/src/llama-sampling.cpp +7 -1
  173. package/src/llama.cpp/src/llama-vocab.cpp +44 -6
  174. package/src/llama.cpp/src/llama.cpp +1 -1
  175. package/src/llama.cpp/tests/CMakeLists.txt +43 -30
  176. package/src/llama.cpp/tests/test-arg-parser.cpp +51 -4
  177. package/src/llama.cpp/tests/test-backend-ops.cpp +139 -57
  178. package/src/llama.cpp/tests/test-chat-template.cpp +34 -13
  179. package/src/llama.cpp/tests/test-chat.cpp +12 -2
  180. package/src/llama.cpp/{examples/gbnf-validator/gbnf-validator.cpp → tests/test-gbnf-validator.cpp} +2 -2
  181. package/src/llama.cpp/tests/test-grammar-integration.cpp +3 -2
  182. package/src/llama.cpp/tests/test-grammar-llguidance.cpp +63 -2
  183. package/src/llama.cpp/tests/test-grammar-parser.cpp +3 -1
  184. package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -1
  185. package/src/llama.cpp/tests/test-llama-grammar.cpp +2 -1
  186. package/src/llama.cpp/{examples/quantize-stats/quantize-stats.cpp → tests/test-quantize-stats.cpp} +3 -1
  187. package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +2 -1
  188. package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +2 -1
  189. package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +0 -5
  190. package/src/llama.cpp/examples/llava/gemma3-cli.cpp +0 -341
  191. package/src/llama.cpp/examples/llava/llava-cli.cpp +0 -332
  192. package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +0 -354
  193. package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +0 -6
  194. package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +0 -30
  195. package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +0 -19
  196. package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +0 -234
  197. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +0 -197
  198. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +0 -190
  199. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -204
  200. package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -191
  201. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -218
  202. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -216
  203. package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -295
@@ -0,0 +1,345 @@
1
+ #include "ggml.h"
2
+ #include "gguf.h"
3
+ #include "clip.h"
4
+
5
+ #include <climits>
6
+ #include <cstdarg>
7
+ #include <string>
8
+ #include <map>
9
+ #include <sstream>
10
+ #include <vector>
11
+ #include <memory>
12
+
13
+ // Internal header for clip.cpp
14
+
15
+ #define KEY_FTYPE "general.file_type"
16
+ #define KEY_NAME "general.name"
17
+ #define KEY_DESCRIPTION "general.description"
18
+ #define KEY_MINICPMV_VERSION "clip.minicpmv_version"
19
+ #define KEY_USE_GELU "clip.use_gelu"
20
+ #define KEY_USE_SILU "clip.use_silu"
21
+ #define KEY_N_EMBD "clip.vision.embedding_length"
22
+ #define KEY_N_FF "clip.vision.feed_forward_length"
23
+ #define KEY_N_BLOCK "clip.vision.block_count"
24
+ #define KEY_N_HEAD "clip.vision.attention.head_count"
25
+ #define KEY_LAYER_NORM_EPS "clip.vision.attention.layer_norm_epsilon"
26
+ #define KEY_PROJ_DIM "clip.vision.projection_dim"
27
+ #define KEY_IMAGE_SIZE "clip.vision.image_size"
28
+ #define KEY_PATCH_SIZE "clip.vision.patch_size"
29
+ #define KEY_IMAGE_MEAN "clip.vision.image_mean"
30
+ #define KEY_IMAGE_STD "clip.vision.image_std"
31
+ #define KEY_FEATURE_LAYER "clip.vision.feature_layer"
32
+ #define KEY_PROJ_SCALE_FACTOR "clip.vision.projector.scale_factor"
33
+ #define KEY_PROJ_TYPE "clip.projector_type"
34
+
35
+ #define KEY_USE_GLU_MLP "clip.use_glu_mlp" // for qwen2.5vl
36
+ #define KEY_USE_RMS_NORM "clip.use_rms_norm" // for qwen2.5vl
37
+
38
+ #define KEY_MM_PATCH_MERGE_TYPE "clip.vision.mm_patch_merge_type"
39
+ #define KEY_IMAGE_GRID_PINPOINTS "clip.vision.image_grid_pinpoints"
40
+ #define KEY_IMAGE_CROP_RESOLUTION "clip.vision.image_crop_resolution"
41
+ #define KEY_WIN_ATTN_PATTERN "clip.vision.n_wa_pattern"
42
+ #define KEY_ATTN_WINDOW_SIZE "clip.vision.window_size"
43
+
44
+
45
+ //
46
+ // tensor name constants
47
+ //
48
+
49
+ #define TN_POS_EMBD "%s.position_embd.weight"
50
+ #define TN_CLASS_EMBD "v.class_embd"
51
+ #define TN_PATCH_EMBD "v.patch_embd.weight" // not rename tensor with ".0" postfix for backwrad compat
52
+ #define TN_PATCH_EMBD_1 "v.patch_embd.weight.1"
53
+ #define TN_PATCH_BIAS "v.patch_embd.bias"
54
+ #define TN_ATTN_K "%s.blk.%d.attn_k.%s"
55
+ #define TN_ATTN_Q "%s.blk.%d.attn_q.%s"
56
+ #define TN_ATTN_V "%s.blk.%d.attn_v.%s"
57
+ #define TN_ATTN_OUTPUT "%s.blk.%d.attn_out.%s"
58
+ #define TN_FFN_DOWN "%s.blk.%d.ffn_down.%s"
59
+ #define TN_FFN_GATE "%s.blk.%d.ffn_gate.%s"
60
+ #define TN_FFN_UP "%s.blk.%d.ffn_up.%s"
61
+ #define TN_FFN_GATE "%s.blk.%d.ffn_gate.%s"
62
+ #define TN_LN_1 "%s.blk.%d.ln1.%s"
63
+ #define TN_LN_2 "%s.blk.%d.ln2.%s"
64
+ #define TN_LN_PRE "%s.pre_ln.%s"
65
+ #define TN_LN_POST "%s.post_ln.%s"
66
+ #define TN_LLAVA_PROJ "mm.%d.%s"
67
+ #define TN_MVLM_PROJ_MLP "mm.model.mlp.%d.%s"
68
+ #define TN_MVLM_PROJ_BLOCK "mm.model.mb_block.%d.block.%d.%s"
69
+ #define TN_MVLM_PROJ_PEG "mm.model.peg.%d.%s"
70
+ #define TN_IMAGE_NEWLINE "model.image_newline"
71
+ #define TN_MM_INP_PROJ "mm.input_projection.weight" // gemma3
72
+ #define TN_MM_SOFT_EMB_N "mm.soft_emb_norm.weight" // gemma3
73
+ #define TN_MM_PROJECTOR "mm.model.fc.weight" // idefics3
74
+ #define TN_TOK_IMG_BREAK "v.token_embd.img_break" // pixtral
75
+
76
+ // mimicpmv
77
+ #define TN_MINICPMV_POS_EMBD_K "resampler.pos_embed_k"
78
+ #define TN_MINICPMV_QUERY "resampler.query"
79
+ #define TN_MINICPMV_PROJ "resampler.proj.weight"
80
+ #define TN_MINICPMV_KV_PROJ "resampler.kv.weight"
81
+ #define TN_MINICPMV_ATTN "resampler.attn.%s.%s"
82
+ #define TN_MINICPMV_LN "resampler.ln_%s.%s"
83
+
84
+ #define TN_GLM_ADAPER_CONV "adapter.conv.%s"
85
+ #define TN_GLM_ADAPTER_LINEAR "adapter.linear.linear.%s"
86
+ #define TN_GLM_ADAPTER_NORM_1 "adapter.linear.norm1.%s"
87
+ #define TN_GLM_ADAPTER_D_H_2_4H "adapter.linear.dense_h_to_4h.%s"
88
+ #define TN_GLM_ADAPTER_GATE "adapter.linear.gate.%s"
89
+ #define TN_GLM_ADAPTER_D_4H_2_H "adapter.linear.dense_4h_to_h.%s"
90
+
91
+ enum projector_type {
92
+ PROJECTOR_TYPE_MLP,
93
+ PROJECTOR_TYPE_MLP_NORM,
94
+ PROJECTOR_TYPE_LDP,
95
+ PROJECTOR_TYPE_LDPV2,
96
+ PROJECTOR_TYPE_MINICPMV,
97
+ PROJECTOR_TYPE_GLM_EDGE,
98
+ PROJECTOR_TYPE_QWEN2VL,
99
+ PROJECTOR_TYPE_GEMMA3,
100
+ PROJECTOR_TYPE_IDEFICS3,
101
+ PROJECTOR_TYPE_PIXTRAL,
102
+ PROJECTOR_TYPE_QWEN25VL,
103
+ PROJECTOR_TYPE_UNKNOWN,
104
+ };
105
+
106
+ static std::map<projector_type, std::string> PROJECTOR_TYPE_NAMES = {
107
+ { PROJECTOR_TYPE_MLP, "mlp" },
108
+ { PROJECTOR_TYPE_LDP, "ldp" },
109
+ { PROJECTOR_TYPE_LDPV2, "ldpv2"},
110
+ { PROJECTOR_TYPE_MINICPMV, "resampler"},
111
+ { PROJECTOR_TYPE_GLM_EDGE, "adapter"},
112
+ { PROJECTOR_TYPE_QWEN2VL, "qwen2vl_merger"},
113
+ { PROJECTOR_TYPE_QWEN25VL, "qwen2.5vl_merger"},
114
+ { PROJECTOR_TYPE_GEMMA3, "gemma3"},
115
+ { PROJECTOR_TYPE_IDEFICS3, "idefics3"},
116
+ { PROJECTOR_TYPE_PIXTRAL, "pixtral"},
117
+ };
118
+
119
+ static projector_type clip_projector_type_from_string(const std::string & str) {
120
+ for (const auto & pair : PROJECTOR_TYPE_NAMES) {
121
+ if (pair.second == str) {
122
+ return pair.first;
123
+ }
124
+ }
125
+ return PROJECTOR_TYPE_UNKNOWN;
126
+ }
127
+
128
+ // RGB uint8 image
129
+ struct clip_image_u8 {
130
+ int nx;
131
+ int ny;
132
+
133
+ std::vector<uint8_t> buf;
134
+ };
135
+
136
+ // RGB float32 image (NHWC)
137
+ // Memory layout: RGBRGBRGB...
138
+ struct clip_image_f32 {
139
+ int nx;
140
+ int ny;
141
+
142
+ std::vector<float> buf;
143
+ };
144
+
145
+ //
146
+ // logging
147
+ //
148
+
149
+ static void clip_log_callback_default(enum ggml_log_level level, const char * text, void * user_data) {
150
+ (void) level;
151
+ (void) user_data;
152
+ fputs(text, stderr);
153
+ fflush(stderr);
154
+ }
155
+
156
+ struct clip_logger_state {
157
+ ggml_log_level verbosity_thold;
158
+ ggml_log_callback log_callback;
159
+ void * log_callback_user_data;
160
+ };
161
+
162
+ extern struct clip_logger_state g_logger_state;
163
+
164
+ static void clip_log_internal_v(enum ggml_log_level level, const char * format, va_list args) {
165
+ if (format == NULL) {
166
+ return;
167
+ }
168
+ va_list args_copy;
169
+ va_copy(args_copy, args);
170
+ char buffer[128];
171
+ int len = vsnprintf(buffer, 128, format, args);
172
+ if (len < 128) {
173
+ g_logger_state.log_callback(level, buffer, g_logger_state.log_callback_user_data);
174
+ } else {
175
+ char * buffer2 = (char *) calloc(len + 1, sizeof(char));
176
+ vsnprintf(buffer2, len + 1, format, args_copy);
177
+ buffer2[len] = 0;
178
+ g_logger_state.log_callback(level, buffer2, g_logger_state.log_callback_user_data);
179
+ free(buffer2);
180
+ }
181
+ va_end(args_copy);
182
+ }
183
+
184
+ static void clip_log_internal(enum ggml_log_level level, const char * format, ...) {
185
+ va_list args;
186
+ va_start(args, format);
187
+ clip_log_internal_v(level, format, args);
188
+ va_end(args);
189
+ }
190
+
191
+ #define LOG_TMPL(level, ...) \
192
+ do { \
193
+ if ((level) >= g_logger_state.verbosity_thold) { \
194
+ clip_log_internal((level), __VA_ARGS__); \
195
+ } \
196
+ } while (0)
197
+ #define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, __VA_ARGS__)
198
+ #define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, __VA_ARGS__)
199
+ #define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
200
+ #define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
201
+ #define LOG_CNT(...) LOG_TMPL(GGML_LOG_LEVEL_CONT, __VA_ARGS__)
202
+
203
+ //
204
+ // cpp wrappers
205
+ //
206
+
207
+ // wrapper for clip_image_size
208
+ struct clip_image_size_deleter {
209
+ void operator()(clip_image_size * val) { clip_image_size_free(val); }
210
+ };
211
+ typedef std::unique_ptr<clip_image_size, clip_image_size_deleter> clip_image_size_ptr;
212
+
213
+ // wrapper for clip_image_u8
214
+ struct clip_image_u8_deleter {
215
+ void operator()(clip_image_u8 * val) { clip_image_u8_free(val); }
216
+ };
217
+ typedef std::unique_ptr<clip_image_u8, clip_image_u8_deleter> clip_image_u8_ptr;
218
+
219
+ // wrapper for clip_image_f32
220
+ struct clip_image_f32_deleter {
221
+ void operator()(clip_image_f32 * val) { clip_image_f32_free(val); }
222
+ };
223
+ typedef std::unique_ptr<clip_image_f32, clip_image_f32_deleter> clip_image_f32_ptr;
224
+
225
+ struct clip_image_u8_batch {
226
+ std::vector<clip_image_u8_ptr> entries;
227
+ };
228
+
229
+ struct clip_image_f32_batch {
230
+ std::vector<clip_image_f32_ptr> entries;
231
+ };
232
+
233
+ //
234
+ // common utils
235
+ //
236
+
237
+ static std::string string_format(const char * fmt, ...) {
238
+ va_list ap;
239
+ va_list ap2;
240
+ va_start(ap, fmt);
241
+ va_copy(ap2, ap);
242
+ int size = vsnprintf(NULL, 0, fmt, ap);
243
+ GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
244
+ std::vector<char> buf(size + 1);
245
+ int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
246
+ GGML_ASSERT(size2 == size);
247
+ va_end(ap2);
248
+ va_end(ap);
249
+ return std::string(buf.data(), buf.size());
250
+ }
251
+
252
+ static void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
253
+ if (search.empty()) {
254
+ return;
255
+ }
256
+ std::string builder;
257
+ builder.reserve(s.length());
258
+ size_t pos = 0;
259
+ size_t last_pos = 0;
260
+ while ((pos = s.find(search, last_pos)) != std::string::npos) {
261
+ builder.append(s, last_pos, pos - last_pos);
262
+ builder.append(replace);
263
+ last_pos = pos + search.length();
264
+ }
265
+ builder.append(s, last_pos, std::string::npos);
266
+ s = std::move(builder);
267
+ }
268
+
269
+ // split string by a `std::string delim` instead of `char delim`
270
+ static std::vector<std::string> string_split_str(std::string s, const std::string & delimiter) {
271
+ std::vector<std::string> tokens;
272
+ size_t pos = 0;
273
+ std::string token;
274
+ while ((pos = s.find(delimiter)) != std::string::npos) {
275
+ token = s.substr(0, pos);
276
+ tokens.push_back(token);
277
+ s.erase(0, pos + delimiter.length());
278
+ }
279
+ tokens.push_back(s);
280
+ return tokens;
281
+ }
282
+
283
+ //
284
+ // gguf utils
285
+ //
286
+
287
+ static std::string gguf_data_to_str(enum gguf_type type, const void * data, int i) {
288
+ switch (type) {
289
+ case GGUF_TYPE_UINT8: return std::to_string(((const uint8_t *)data)[i]);
290
+ case GGUF_TYPE_INT8: return std::to_string(((const int8_t *)data)[i]);
291
+ case GGUF_TYPE_UINT16: return std::to_string(((const uint16_t *)data)[i]);
292
+ case GGUF_TYPE_INT16: return std::to_string(((const int16_t *)data)[i]);
293
+ case GGUF_TYPE_UINT32: return std::to_string(((const uint32_t *)data)[i]);
294
+ case GGUF_TYPE_INT32: return std::to_string(((const int32_t *)data)[i]);
295
+ case GGUF_TYPE_UINT64: return std::to_string(((const uint64_t *)data)[i]);
296
+ case GGUF_TYPE_INT64: return std::to_string(((const int64_t *)data)[i]);
297
+ case GGUF_TYPE_FLOAT32: return std::to_string(((const float *)data)[i]);
298
+ case GGUF_TYPE_FLOAT64: return std::to_string(((const double *)data)[i]);
299
+ case GGUF_TYPE_BOOL: return ((const bool *)data)[i] ? "true" : "false";
300
+ default: return string_format("unknown type %d", type);
301
+ }
302
+ }
303
+
304
+ static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {
305
+ const enum gguf_type type = gguf_get_kv_type(ctx_gguf, i);
306
+
307
+ switch (type) {
308
+ case GGUF_TYPE_STRING:
309
+ return gguf_get_val_str(ctx_gguf, i);
310
+ case GGUF_TYPE_ARRAY:
311
+ {
312
+ const enum gguf_type arr_type = gguf_get_arr_type(ctx_gguf, i);
313
+ int arr_n = gguf_get_arr_n(ctx_gguf, i);
314
+ const void * data = arr_type == GGUF_TYPE_STRING ? nullptr : gguf_get_arr_data(ctx_gguf, i);
315
+ std::stringstream ss;
316
+ ss << "[";
317
+ for (int j = 0; j < arr_n; j++) {
318
+ if (arr_type == GGUF_TYPE_STRING) {
319
+ std::string val = gguf_get_arr_str(ctx_gguf, i, j);
320
+ // escape quotes
321
+ string_replace_all(val, "\\", "\\\\");
322
+ string_replace_all(val, "\"", "\\\"");
323
+ ss << '"' << val << '"';
324
+ } else if (arr_type == GGUF_TYPE_ARRAY) {
325
+ ss << "???";
326
+ } else {
327
+ ss << gguf_data_to_str(arr_type, data, j);
328
+ }
329
+ if (j < arr_n - 1) {
330
+ ss << ", ";
331
+ }
332
+ }
333
+ ss << "]";
334
+ return ss.str();
335
+ }
336
+ default:
337
+ return gguf_data_to_str(type, gguf_get_val_data(ctx_gguf, i), 0);
338
+ }
339
+ }
340
+
341
+ //
342
+ // API used internally with mtmd
343
+ //
344
+
345
+ projector_type clip_get_projector_type(const struct clip_ctx * ctx);