@fugood/llama.node 0.3.16 → 0.4.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.
- package/CMakeLists.txt +6 -1
- package/bin/darwin/arm64/llama-node.node +0 -0
- package/bin/darwin/x64/llama-node.node +0 -0
- package/bin/linux/arm64/llama-node.node +0 -0
- package/bin/linux/x64/llama-node.node +0 -0
- package/bin/linux-cuda/arm64/llama-node.node +0 -0
- package/bin/linux-cuda/x64/llama-node.node +0 -0
- package/bin/linux-vulkan/arm64/llama-node.node +0 -0
- package/bin/linux-vulkan/x64/llama-node.node +0 -0
- package/bin/win32/arm64/llama-node.node +0 -0
- package/bin/win32/arm64/node.lib +0 -0
- package/bin/win32/x64/llama-node.node +0 -0
- package/bin/win32/x64/node.lib +0 -0
- package/bin/win32-vulkan/arm64/llama-node.node +0 -0
- package/bin/win32-vulkan/arm64/node.lib +0 -0
- package/bin/win32-vulkan/x64/llama-node.node +0 -0
- package/bin/win32-vulkan/x64/node.lib +0 -0
- package/lib/binding.ts +44 -2
- package/lib/index.js +132 -1
- package/lib/index.ts +203 -3
- package/package.json +2 -1
- package/src/EmbeddingWorker.cpp +1 -1
- package/src/LlamaCompletionWorker.cpp +374 -19
- package/src/LlamaCompletionWorker.h +31 -10
- package/src/LlamaContext.cpp +216 -7
- package/src/LlamaContext.h +12 -0
- package/src/common.hpp +15 -0
- package/src/llama.cpp/.github/workflows/build-linux-cross.yml +233 -0
- package/src/llama.cpp/.github/workflows/build.yml +89 -767
- package/src/llama.cpp/.github/workflows/docker.yml +9 -6
- package/src/llama.cpp/.github/workflows/release.yml +716 -0
- package/src/llama.cpp/.github/workflows/server.yml +19 -23
- package/src/llama.cpp/CMakeLists.txt +11 -1
- package/src/llama.cpp/cmake/build-info.cmake +8 -2
- package/src/llama.cpp/cmake/x64-windows-llvm.cmake +0 -6
- package/src/llama.cpp/common/CMakeLists.txt +35 -4
- package/src/llama.cpp/common/arg.cpp +844 -121
- package/src/llama.cpp/common/arg.h +9 -0
- package/src/llama.cpp/common/chat.cpp +129 -107
- package/src/llama.cpp/common/chat.h +2 -0
- package/src/llama.cpp/common/common.cpp +64 -518
- package/src/llama.cpp/common/common.h +35 -45
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +3 -0
- package/src/llama.cpp/common/llguidance.cpp +31 -47
- package/src/llama.cpp/common/minja/chat-template.hpp +23 -11
- package/src/llama.cpp/common/minja/minja.hpp +186 -127
- package/src/llama.cpp/common/regex-partial.cpp +204 -0
- package/src/llama.cpp/common/regex-partial.h +56 -0
- package/src/llama.cpp/common/sampling.cpp +60 -50
- package/src/llama.cpp/docs/build.md +122 -7
- package/src/llama.cpp/examples/CMakeLists.txt +2 -32
- package/src/llama.cpp/examples/batched/batched.cpp +1 -1
- package/src/llama.cpp/examples/embedding/embedding.cpp +9 -12
- package/src/llama.cpp/examples/gritlm/gritlm.cpp +1 -1
- package/src/llama.cpp/examples/llama.android/llama/build.gradle.kts +1 -0
- package/src/llama.cpp/examples/parallel/parallel.cpp +89 -15
- package/src/llama.cpp/examples/passkey/passkey.cpp +1 -1
- package/src/llama.cpp/examples/speculative/speculative.cpp +1 -1
- package/src/llama.cpp/examples/speculative-simple/speculative-simple.cpp +1 -1
- package/src/llama.cpp/examples/sycl/build.sh +2 -2
- package/src/llama.cpp/examples/sycl/win-build-sycl.bat +2 -2
- package/src/llama.cpp/examples/training/CMakeLists.txt +5 -0
- package/src/llama.cpp/examples/training/finetune.cpp +96 -0
- package/src/llama.cpp/ggml/CMakeLists.txt +35 -2
- package/src/llama.cpp/ggml/cmake/GitVars.cmake +22 -0
- package/src/llama.cpp/ggml/include/ggml-backend.h +4 -4
- package/src/llama.cpp/ggml/include/ggml-cpp.h +1 -1
- package/src/llama.cpp/ggml/include/ggml-cpu.h +5 -0
- package/src/llama.cpp/ggml/include/ggml-opt.h +47 -28
- package/src/llama.cpp/ggml/include/ggml-rpc.h +6 -1
- package/src/llama.cpp/ggml/include/ggml.h +76 -106
- package/src/llama.cpp/ggml/src/CMakeLists.txt +11 -8
- package/src/llama.cpp/ggml/src/ggml-alloc.c +4 -1
- package/src/llama.cpp/ggml/src/ggml-backend.cpp +9 -5
- package/src/llama.cpp/ggml/src/ggml-cann/CMakeLists.txt +0 -2
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.cpp +8 -4
- package/src/llama.cpp/ggml/src/ggml-cann/acl_tensor.h +5 -5
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.cpp +692 -1534
- package/src/llama.cpp/ggml/src/ggml-cann/aclnn_ops.h +613 -122
- package/src/llama.cpp/ggml/src/ggml-cann/common.h +135 -1
- package/src/llama.cpp/ggml/src/ggml-cann/ggml-cann.cpp +507 -137
- package/src/llama.cpp/ggml/src/ggml-common.h +12 -6
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +66 -33
- package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.cpp +158 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/common.h +72 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/cpu-feats-x86.cpp +1 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-aarch64.cpp +896 -194
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +2 -21
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu-quants.c +1060 -410
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +1008 -13533
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +31 -16
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +90 -12
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +47 -13
- package/src/llama.cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +266 -72
- package/src/llama.cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +1034 -88
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.cpp +8796 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ops.h +110 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/simd-mappings.h +892 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.cpp +186 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/unary-ops.h +28 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +252 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +802 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt +23 -4
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/hip.h +7 -0
- package/src/llama.cpp/ggml/src/ggml-cuda/vendors/musa.h +1 -0
- package/src/llama.cpp/ggml/src/ggml-hip/CMakeLists.txt +0 -4
- package/src/llama.cpp/ggml/src/ggml-impl.h +52 -18
- package/src/llama.cpp/ggml/src/ggml-metal/ggml-metal-impl.h +106 -14
- package/src/llama.cpp/ggml/src/ggml-opencl/CMakeLists.txt +67 -119
- package/src/llama.cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +1023 -262
- package/src/llama.cpp/ggml/src/ggml-opt.cpp +368 -190
- package/src/llama.cpp/ggml/src/ggml-quants.c +0 -6
- package/src/llama.cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +307 -40
- package/src/llama.cpp/ggml/src/ggml-sycl/CMakeLists.txt +125 -45
- package/src/llama.cpp/ggml/src/ggml-sycl/backend.hpp +10 -8
- package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.cpp +239 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/binbcast.hpp +39 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/common.cpp +0 -35
- package/src/llama.cpp/ggml/src/ggml-sycl/common.hpp +9 -307
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.cpp +72 -25
- package/src/llama.cpp/ggml/src/ggml-sycl/convert.hpp +14 -7
- package/src/llama.cpp/ggml/src/ggml-sycl/dequantize.hpp +59 -21
- package/src/llama.cpp/ggml/src/ggml-sycl/dmmv.cpp +7 -1
- package/src/llama.cpp/ggml/src/ggml-sycl/dpct/helper.hpp +79 -90
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.cpp +944 -438
- package/src/llama.cpp/ggml/src/ggml-sycl/element_wise.hpp +22 -23
- package/src/llama.cpp/ggml/src/ggml-sycl/gemm.hpp +37 -8
- package/src/llama.cpp/ggml/src/ggml-sycl/getrows.cpp +24 -20
- package/src/llama.cpp/ggml/src/ggml-sycl/getrows.hpp +1 -4
- package/src/llama.cpp/ggml/src/ggml-sycl/ggml-sycl.cpp +507 -411
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.cpp +84 -74
- package/src/llama.cpp/ggml/src/ggml-sycl/im2col.hpp +1 -3
- package/src/llama.cpp/ggml/src/ggml-sycl/mmvq.cpp +185 -89
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.cpp +37 -49
- package/src/llama.cpp/ggml/src/ggml-sycl/norm.hpp +7 -22
- package/src/llama.cpp/ggml/src/ggml-sycl/outprod.cpp +4 -14
- package/src/llama.cpp/ggml/src/ggml-sycl/quants.hpp +83 -0
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.cpp +204 -118
- package/src/llama.cpp/ggml/src/ggml-sycl/rope.hpp +1 -3
- package/src/llama.cpp/ggml/src/ggml-sycl/vecdotq.hpp +128 -53
- package/src/llama.cpp/ggml/src/ggml-vulkan/CMakeLists.txt +83 -49
- package/src/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp +1278 -282
- package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/CMakeLists.txt +32 -0
- package/src/llama.cpp/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +133 -30
- package/src/llama.cpp/ggml/src/ggml.c +170 -265
- package/src/llama.cpp/ggml/src/gguf.cpp +34 -33
- package/src/llama.cpp/include/llama.h +82 -22
- package/src/llama.cpp/models/ggml-vocab-llama4.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-llama4.gguf.out +46 -0
- package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.inp +112 -0
- package/src/llama.cpp/models/ggml-vocab-pixtral.gguf.out +46 -0
- package/src/llama.cpp/requirements/requirements-all.txt +5 -3
- package/src/llama.cpp/requirements/requirements-gguf_editor_gui.txt +3 -0
- package/src/llama.cpp/scripts/xxd.cmake +1 -1
- package/src/llama.cpp/src/CMakeLists.txt +4 -2
- package/src/llama.cpp/src/llama-adapter.cpp +43 -1
- package/src/llama.cpp/src/llama-arch.cpp +163 -17
- package/src/llama.cpp/src/llama-arch.h +16 -0
- package/src/llama.cpp/src/llama-batch.cpp +5 -1
- package/src/llama.cpp/src/llama-batch.h +2 -1
- package/src/llama.cpp/src/llama-chat.cpp +91 -16
- package/src/llama.cpp/src/llama-chat.h +7 -2
- package/src/llama.cpp/src/llama-context.cpp +479 -575
- package/src/llama.cpp/src/llama-context.h +44 -33
- package/src/llama.cpp/src/llama-cparams.h +1 -0
- package/src/llama.cpp/src/llama-graph.cpp +209 -157
- package/src/llama.cpp/src/llama-graph.h +38 -14
- package/src/llama.cpp/src/llama-hparams.h +13 -0
- package/src/llama.cpp/src/llama-kv-cache.cpp +1604 -543
- package/src/llama.cpp/src/llama-kv-cache.h +283 -171
- package/src/llama.cpp/src/llama-memory.h +12 -2
- package/src/llama.cpp/src/llama-mmap.cpp +1 -1
- package/src/llama.cpp/src/llama-model-loader.cpp +34 -20
- package/src/llama.cpp/src/llama-model-loader.h +5 -3
- package/src/llama.cpp/src/llama-model-saver.cpp +281 -0
- package/src/llama.cpp/src/llama-model-saver.h +37 -0
- package/src/llama.cpp/src/llama-model.cpp +1803 -330
- package/src/llama.cpp/src/llama-model.h +21 -2
- package/src/llama.cpp/src/llama-quant.cpp +33 -10
- package/src/llama.cpp/src/llama-sampling.cpp +25 -7
- package/src/llama.cpp/src/llama-vocab.cpp +86 -10
- package/src/llama.cpp/src/llama-vocab.h +6 -0
- package/src/llama.cpp/src/llama.cpp +15 -1
- package/src/llama.cpp/tests/CMakeLists.txt +52 -31
- package/src/llama.cpp/tests/test-arg-parser.cpp +51 -4
- package/src/llama.cpp/tests/test-backend-ops.cpp +189 -90
- package/src/llama.cpp/tests/test-chat-template.cpp +26 -6
- package/src/llama.cpp/tests/test-chat.cpp +15 -3
- package/src/llama.cpp/{examples/gbnf-validator/gbnf-validator.cpp → tests/test-gbnf-validator.cpp} +2 -2
- package/src/llama.cpp/tests/test-grammar-integration.cpp +3 -2
- package/src/llama.cpp/tests/test-grammar-llguidance.cpp +63 -2
- package/src/llama.cpp/tests/test-grammar-parser.cpp +3 -1
- package/src/llama.cpp/tests/test-json-schema-to-grammar.cpp +17 -1
- package/src/llama.cpp/tests/test-llama-grammar.cpp +2 -1
- package/src/llama.cpp/tests/test-mtmd-c-api.c +63 -0
- package/src/llama.cpp/tests/test-opt.cpp +33 -21
- package/src/llama.cpp/{examples/quantize-stats/quantize-stats.cpp → tests/test-quantize-stats.cpp} +3 -1
- package/src/llama.cpp/tests/test-regex-partial.cpp +288 -0
- package/src/llama.cpp/tests/test-sampling.cpp +1 -1
- package/src/llama.cpp/tests/test-tokenizer-1-bpe.cpp +2 -1
- package/src/llama.cpp/tests/test-tokenizer-1-spm.cpp +2 -1
- package/src/llama.cpp/tools/CMakeLists.txt +39 -0
- package/src/llama.cpp/{examples → tools}/batched-bench/batched-bench.cpp +3 -3
- package/src/llama.cpp/{examples → tools}/export-lora/export-lora.cpp +1 -1
- package/src/llama.cpp/{examples → tools}/gguf-split/gguf-split.cpp +15 -16
- package/src/llama.cpp/{examples → tools}/imatrix/imatrix.cpp +11 -9
- package/src/llama.cpp/{examples → tools}/llama-bench/llama-bench.cpp +623 -274
- package/src/llama.cpp/{examples → tools}/main/main.cpp +22 -14
- package/src/llama.cpp/tools/mtmd/CMakeLists.txt +47 -0
- package/src/llama.cpp/tools/mtmd/clip-impl.h +365 -0
- package/src/llama.cpp/tools/mtmd/clip.cpp +3646 -0
- package/src/llama.cpp/tools/mtmd/clip.h +99 -0
- package/src/llama.cpp/tools/mtmd/deprecation-warning.cpp +22 -0
- package/src/llama.cpp/tools/mtmd/mtmd-cli.cpp +370 -0
- package/src/llama.cpp/tools/mtmd/mtmd-helper.cpp +310 -0
- package/src/llama.cpp/tools/mtmd/mtmd.cpp +678 -0
- package/src/llama.cpp/tools/mtmd/mtmd.h +331 -0
- package/src/llama.cpp/{examples → tools}/perplexity/perplexity.cpp +21 -5
- package/src/llama.cpp/{examples → tools}/quantize/quantize.cpp +53 -3
- package/src/llama.cpp/tools/rpc/CMakeLists.txt +4 -0
- package/src/llama.cpp/tools/rpc/rpc-server.cpp +322 -0
- package/src/llama.cpp/tools/run/CMakeLists.txt +16 -0
- package/src/llama.cpp/{examples → tools}/run/run.cpp +30 -30
- package/src/llama.cpp/{examples → tools}/server/CMakeLists.txt +2 -1
- package/src/llama.cpp/{examples → tools}/server/httplib.h +313 -247
- package/src/llama.cpp/{examples → tools}/server/server.cpp +529 -215
- package/src/llama.cpp/{examples → tools}/server/utils.hpp +427 -6
- package/src/llama.cpp/{examples → tools}/tts/tts.cpp +6 -9
- package/src/llama.cpp/cmake/arm64-windows-msvc.cmake +0 -6
- package/src/llama.cpp/examples/gbnf-validator/CMakeLists.txt +0 -5
- package/src/llama.cpp/examples/infill/CMakeLists.txt +0 -5
- package/src/llama.cpp/examples/infill/infill.cpp +0 -590
- package/src/llama.cpp/examples/llava/CMakeLists.txt +0 -66
- package/src/llama.cpp/examples/llava/android/build_64.sh +0 -8
- package/src/llama.cpp/examples/llava/clip-quantize-cli.cpp +0 -59
- package/src/llama.cpp/examples/llava/clip.cpp +0 -3206
- package/src/llama.cpp/examples/llava/clip.h +0 -118
- package/src/llama.cpp/examples/llava/gemma3-cli.cpp +0 -341
- package/src/llama.cpp/examples/llava/llava-cli.cpp +0 -332
- package/src/llama.cpp/examples/llava/llava.cpp +0 -574
- package/src/llama.cpp/examples/llava/llava.h +0 -49
- package/src/llama.cpp/examples/llava/minicpmv-cli.cpp +0 -354
- package/src/llama.cpp/examples/llava/qwen2vl-cli.cpp +0 -584
- package/src/llama.cpp/examples/quantize-stats/CMakeLists.txt +0 -6
- package/src/llama.cpp/examples/rpc/CMakeLists.txt +0 -2
- package/src/llama.cpp/examples/rpc/rpc-server.cpp +0 -171
- package/src/llama.cpp/examples/run/CMakeLists.txt +0 -5
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/CMakeLists.txt +0 -30
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/ascendc_kernels.h +0 -19
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/dup.cpp +0 -234
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f16.cpp +0 -197
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_f32.cpp +0 -190
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q4_0.cpp +0 -204
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/get_row_q8_0.cpp +0 -191
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f16_q8_0.cpp +0 -218
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_f32_q8_0.cpp +0 -216
- package/src/llama.cpp/ggml/src/ggml-cann/kernels/quantize_float_to_q4_0.cpp +0 -295
- /package/src/llama.cpp/{examples → tools}/batched-bench/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/completions.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/cvector-generator.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/mean.hpp +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/negative.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/pca.hpp +0 -0
- /package/src/llama.cpp/{examples → tools}/cvector-generator/positive.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/export-lora/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/gguf-split/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/imatrix/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/llama-bench/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/main/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples/llava → tools/mtmd}/requirements.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/perplexity/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/quantize/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/run/linenoise.cpp/linenoise.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/run/linenoise.cpp/linenoise.h +0 -0
- /package/src/llama.cpp/{examples → tools}/server/bench/requirements.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/server/tests/requirements.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/tokenize/CMakeLists.txt +0 -0
- /package/src/llama.cpp/{examples → tools}/tokenize/tokenize.cpp +0 -0
- /package/src/llama.cpp/{examples → tools}/tts/CMakeLists.txt +0 -0
|
@@ -99,14 +99,6 @@ int main(int argc, char ** argv) {
|
|
|
99
99
|
console::init(params.simple_io, params.use_color);
|
|
100
100
|
atexit([]() { console::cleanup(); });
|
|
101
101
|
|
|
102
|
-
if (params.logits_all) {
|
|
103
|
-
LOG_ERR("************\n");
|
|
104
|
-
LOG_ERR("%s: please use the 'perplexity' tool for perplexity calculations\n", __func__);
|
|
105
|
-
LOG_ERR("************\n\n");
|
|
106
|
-
|
|
107
|
-
return 0;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
102
|
if (params.embedding) {
|
|
111
103
|
LOG_ERR("************\n");
|
|
112
104
|
LOG_ERR("%s: please use the 'embedding' tool for embedding calculations\n", __func__);
|
|
@@ -160,7 +152,12 @@ int main(int argc, char ** argv) {
|
|
|
160
152
|
|
|
161
153
|
LOG_INF("%s: llama threadpool init, n_threads = %d\n", __func__, (int) params.cpuparams.n_threads);
|
|
162
154
|
|
|
163
|
-
auto *
|
|
155
|
+
auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
|
|
156
|
+
if (!cpu_dev) {
|
|
157
|
+
LOG_ERR("%s: no CPU backend found\n", __func__);
|
|
158
|
+
return 1;
|
|
159
|
+
}
|
|
160
|
+
auto * reg = ggml_backend_dev_backend_reg(cpu_dev);
|
|
164
161
|
auto * ggml_threadpool_new_fn = (decltype(ggml_threadpool_new) *) ggml_backend_reg_get_proc_address(reg, "ggml_threadpool_new");
|
|
165
162
|
auto * ggml_threadpool_free_fn = (decltype(ggml_threadpool_free) *) ggml_backend_reg_get_proc_address(reg, "ggml_threadpool_free");
|
|
166
163
|
|
|
@@ -865,9 +862,22 @@ int main(int argc, char ** argv) {
|
|
|
865
862
|
console::set_display(console::reset);
|
|
866
863
|
display = true;
|
|
867
864
|
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
865
|
+
if (buffer.empty()) { // Ctrl+D on empty line exits
|
|
866
|
+
LOG("EOF by user\n");
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (buffer.back() == '\n') {
|
|
871
|
+
// Implement #587:
|
|
872
|
+
// If the user wants the text to end in a newline,
|
|
873
|
+
// this should be accomplished by explicitly adding a newline by using \ followed by return,
|
|
874
|
+
// then returning control by pressing return again.
|
|
875
|
+
buffer.pop_back();
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
if (buffer.empty()) { // Enter key on empty line lets the user pass control back
|
|
879
|
+
LOG_DBG("empty line, passing control back\n");
|
|
880
|
+
} else { // Add tokens to embd only if the input buffer is non-empty
|
|
871
881
|
// append input suffix if any
|
|
872
882
|
if (!params.input_suffix.empty() && !params.conversation_mode) {
|
|
873
883
|
LOG_DBG("appending input suffix: '%s'\n", params.input_suffix.c_str());
|
|
@@ -915,8 +925,6 @@ int main(int argc, char ** argv) {
|
|
|
915
925
|
|
|
916
926
|
n_remain -= line_inp.size();
|
|
917
927
|
LOG_DBG("n_remain: %d\n", n_remain);
|
|
918
|
-
} else {
|
|
919
|
-
LOG_DBG("empty line, passing control back\n");
|
|
920
928
|
}
|
|
921
929
|
|
|
922
930
|
input_echo = false; // do not echo this again
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# mtmd
|
|
2
|
+
|
|
3
|
+
add_library(mtmd OBJECT
|
|
4
|
+
mtmd.cpp
|
|
5
|
+
mtmd-helper.cpp
|
|
6
|
+
mtmd.h
|
|
7
|
+
clip.cpp
|
|
8
|
+
clip.h
|
|
9
|
+
clip-impl.h
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
target_link_libraries(mtmd PRIVATE ggml llama ${CMAKE_THREAD_LIBS_INIT})
|
|
13
|
+
|
|
14
|
+
target_include_directories(mtmd PUBLIC .)
|
|
15
|
+
target_include_directories(mtmd PRIVATE ../..)
|
|
16
|
+
target_include_directories(mtmd PRIVATE ../../common) # for stb_image.h
|
|
17
|
+
|
|
18
|
+
target_compile_features(mtmd PRIVATE cxx_std_17)
|
|
19
|
+
|
|
20
|
+
add_library(mtmd_static STATIC $<TARGET_OBJECTS:mtmd>)
|
|
21
|
+
if (BUILD_SHARED_LIBS)
|
|
22
|
+
set_target_properties(mtmd PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
|
23
|
+
target_compile_definitions(mtmd PRIVATE LLAMA_SHARED LLAMA_BUILD)
|
|
24
|
+
add_library(mtmd_shared SHARED $<TARGET_OBJECTS:mtmd>)
|
|
25
|
+
target_link_libraries(mtmd_shared PRIVATE ggml llama ${CMAKE_THREAD_LIBS_INIT})
|
|
26
|
+
install(TARGETS mtmd_shared LIBRARY)
|
|
27
|
+
endif()
|
|
28
|
+
|
|
29
|
+
if (NOT MSVC)
|
|
30
|
+
target_compile_options(mtmd PRIVATE -Wno-cast-qual) # stb_image.h
|
|
31
|
+
endif()
|
|
32
|
+
|
|
33
|
+
if(TARGET BUILD_INFO)
|
|
34
|
+
add_dependencies(mtmd BUILD_INFO)
|
|
35
|
+
endif()
|
|
36
|
+
|
|
37
|
+
add_executable(llama-llava-cli deprecation-warning.cpp)
|
|
38
|
+
add_executable(llama-gemma3-cli deprecation-warning.cpp)
|
|
39
|
+
add_executable(llama-minicpmv-cli deprecation-warning.cpp)
|
|
40
|
+
add_executable(llama-qwen2vl-cli deprecation-warning.cpp)
|
|
41
|
+
|
|
42
|
+
set(TARGET llama-mtmd-cli)
|
|
43
|
+
add_executable(${TARGET} mtmd-cli.cpp)
|
|
44
|
+
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME llama-mtmd-cli)
|
|
45
|
+
install(TARGETS ${TARGET} RUNTIME)
|
|
46
|
+
target_link_libraries(${TARGET} PRIVATE common mtmd ${CMAKE_THREAD_LIBS_INIT})
|
|
47
|
+
target_compile_features(${TARGET} PRIVATE cxx_std_17)
|
|
@@ -0,0 +1,365 @@
|
|
|
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
|
+
#define KEY_SPATIAL_MERGE_SIZE "clip.vision.spatial_merge_size"
|
|
35
|
+
|
|
36
|
+
#define KEY_MM_PATCH_MERGE_TYPE "clip.vision.mm_patch_merge_type"
|
|
37
|
+
#define KEY_IMAGE_GRID_PINPOINTS "clip.vision.image_grid_pinpoints"
|
|
38
|
+
#define KEY_IMAGE_CROP_RESOLUTION "clip.vision.image_crop_resolution"
|
|
39
|
+
#define KEY_WIN_ATTN_PATTERN "clip.vision.n_wa_pattern"
|
|
40
|
+
#define KEY_ATTN_WINDOW_SIZE "clip.vision.window_size"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
//
|
|
44
|
+
// tensor name constants
|
|
45
|
+
//
|
|
46
|
+
|
|
47
|
+
#define TN_POS_EMBD "%s.position_embd.weight"
|
|
48
|
+
#define TN_CLASS_EMBD "v.class_embd"
|
|
49
|
+
#define TN_PATCH_EMBD "v.patch_embd.weight" // not rename tensor with ".0" postfix for backwrad compat
|
|
50
|
+
#define TN_PATCH_EMBD_1 "v.patch_embd.weight.1"
|
|
51
|
+
#define TN_PATCH_BIAS "v.patch_embd.bias"
|
|
52
|
+
#define TN_ATTN_K "%s.blk.%d.attn_k.%s"
|
|
53
|
+
#define TN_ATTN_Q "%s.blk.%d.attn_q.%s"
|
|
54
|
+
#define TN_ATTN_V "%s.blk.%d.attn_v.%s"
|
|
55
|
+
#define TN_ATTN_OUTPUT "%s.blk.%d.attn_out.%s"
|
|
56
|
+
#define TN_ATTN_K_NORM "%s.blk.%d.attn_k_norm.%s"
|
|
57
|
+
#define TN_ATTN_Q_NORM "%s.blk.%d.attn_q_norm.%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" // layer norm
|
|
63
|
+
#define TN_LN_2 "%s.blk.%d.ln2.%s" // layer norm
|
|
64
|
+
#define TN_LS_1 "%s.blk.%d.ls1.%s" // layer scale
|
|
65
|
+
#define TN_LS_2 "%s.blk.%d.ls2.%s" // layer scale
|
|
66
|
+
#define TN_LN_PRE "%s.pre_ln.%s"
|
|
67
|
+
#define TN_LN_POST "%s.post_ln.%s"
|
|
68
|
+
#define TN_LLAVA_PROJ "mm.%d.%s"
|
|
69
|
+
#define TN_MVLM_PROJ_MLP "mm.model.mlp.%d.%s"
|
|
70
|
+
#define TN_MVLM_PROJ_BLOCK "mm.model.mb_block.%d.block.%d.%s"
|
|
71
|
+
#define TN_MVLM_PROJ_PEG "mm.model.peg.%d.%s"
|
|
72
|
+
#define TN_IMAGE_NEWLINE "model.image_newline"
|
|
73
|
+
#define TN_MM_INP_NORM "mm.input_norm.weight"
|
|
74
|
+
#define TN_MM_INP_PROJ "mm.input_projection.weight" // gemma3
|
|
75
|
+
#define TN_MM_SOFT_EMB_N "mm.soft_emb_norm.weight" // gemma3
|
|
76
|
+
#define TN_MM_PROJECTOR "mm.model.fc.weight" // idefics3
|
|
77
|
+
#define TN_MM_PATCH_MERGER "mm.patch_merger.weight" // mistral small 3.1
|
|
78
|
+
#define TN_TOK_IMG_BREAK "v.token_embd.img_break" // pixtral
|
|
79
|
+
#define TN_TOK_GLM_BOI "adapter.boi" // glm-edge (these embeddings are not in text model)
|
|
80
|
+
#define TN_TOK_GLM_EOI "adapter.eoi" // glm-edge (these embeddings are not in text model)
|
|
81
|
+
|
|
82
|
+
// mimicpmv
|
|
83
|
+
#define TN_MINICPMV_POS_EMBD_K "resampler.pos_embed_k"
|
|
84
|
+
#define TN_MINICPMV_QUERY "resampler.query"
|
|
85
|
+
#define TN_MINICPMV_PROJ "resampler.proj.weight"
|
|
86
|
+
#define TN_MINICPMV_KV_PROJ "resampler.kv.weight"
|
|
87
|
+
#define TN_MINICPMV_ATTN "resampler.attn.%s.%s"
|
|
88
|
+
#define TN_MINICPMV_LN "resampler.ln_%s.%s"
|
|
89
|
+
|
|
90
|
+
#define TN_GLM_ADAPER_CONV "adapter.conv.%s"
|
|
91
|
+
#define TN_GLM_ADAPTER_LINEAR "adapter.linear.linear.%s"
|
|
92
|
+
#define TN_GLM_ADAPTER_NORM_1 "adapter.linear.norm1.%s"
|
|
93
|
+
#define TN_GLM_ADAPTER_D_H_2_4H "adapter.linear.dense_h_to_4h.%s"
|
|
94
|
+
#define TN_GLM_ADAPTER_GATE "adapter.linear.gate.%s"
|
|
95
|
+
#define TN_GLM_ADAPTER_D_4H_2_H "adapter.linear.dense_4h_to_h.%s"
|
|
96
|
+
|
|
97
|
+
// align x to upper multiple of n
|
|
98
|
+
#define CLIP_ALIGN(x, n) ((((x) + (n) - 1) / (n)) * (n))
|
|
99
|
+
|
|
100
|
+
enum projector_type {
|
|
101
|
+
PROJECTOR_TYPE_MLP,
|
|
102
|
+
PROJECTOR_TYPE_MLP_NORM,
|
|
103
|
+
PROJECTOR_TYPE_LDP,
|
|
104
|
+
PROJECTOR_TYPE_LDPV2,
|
|
105
|
+
PROJECTOR_TYPE_MINICPMV,
|
|
106
|
+
PROJECTOR_TYPE_GLM_EDGE,
|
|
107
|
+
PROJECTOR_TYPE_QWEN2VL,
|
|
108
|
+
PROJECTOR_TYPE_GEMMA3,
|
|
109
|
+
PROJECTOR_TYPE_IDEFICS3,
|
|
110
|
+
PROJECTOR_TYPE_PIXTRAL,
|
|
111
|
+
PROJECTOR_TYPE_QWEN25VL,
|
|
112
|
+
PROJECTOR_TYPE_INTERNVL,
|
|
113
|
+
PROJECTOR_TYPE_UNKNOWN,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
static std::map<projector_type, std::string> PROJECTOR_TYPE_NAMES = {
|
|
117
|
+
{ PROJECTOR_TYPE_MLP, "mlp" },
|
|
118
|
+
{ PROJECTOR_TYPE_LDP, "ldp" },
|
|
119
|
+
{ PROJECTOR_TYPE_LDPV2, "ldpv2"},
|
|
120
|
+
{ PROJECTOR_TYPE_MINICPMV, "resampler"},
|
|
121
|
+
{ PROJECTOR_TYPE_GLM_EDGE, "adapter"},
|
|
122
|
+
{ PROJECTOR_TYPE_QWEN2VL, "qwen2vl_merger"},
|
|
123
|
+
{ PROJECTOR_TYPE_QWEN25VL, "qwen2.5vl_merger"},
|
|
124
|
+
{ PROJECTOR_TYPE_GEMMA3, "gemma3"},
|
|
125
|
+
{ PROJECTOR_TYPE_IDEFICS3, "idefics3"},
|
|
126
|
+
{ PROJECTOR_TYPE_PIXTRAL, "pixtral"},
|
|
127
|
+
{ PROJECTOR_TYPE_INTERNVL, "internvl"},
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
static projector_type clip_projector_type_from_string(const std::string & str) {
|
|
131
|
+
for (const auto & pair : PROJECTOR_TYPE_NAMES) {
|
|
132
|
+
if (pair.second == str) {
|
|
133
|
+
return pair.first;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return PROJECTOR_TYPE_UNKNOWN;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// RGB uint8 image
|
|
140
|
+
struct clip_image_u8 {
|
|
141
|
+
int nx;
|
|
142
|
+
int ny;
|
|
143
|
+
|
|
144
|
+
std::vector<uint8_t> buf;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// RGB float32 image (NHWC)
|
|
148
|
+
// Memory layout: RGBRGBRGB...
|
|
149
|
+
struct clip_image_f32 {
|
|
150
|
+
int nx;
|
|
151
|
+
int ny;
|
|
152
|
+
|
|
153
|
+
std::vector<float> buf;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
//
|
|
157
|
+
// logging
|
|
158
|
+
//
|
|
159
|
+
|
|
160
|
+
static void clip_log_callback_default(enum ggml_log_level level, const char * text, void * user_data) {
|
|
161
|
+
(void) level;
|
|
162
|
+
(void) user_data;
|
|
163
|
+
fputs(text, stderr);
|
|
164
|
+
fflush(stderr);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
struct clip_logger_state {
|
|
168
|
+
ggml_log_level verbosity_thold;
|
|
169
|
+
ggml_log_callback log_callback;
|
|
170
|
+
void * log_callback_user_data;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
extern struct clip_logger_state g_logger_state;
|
|
174
|
+
|
|
175
|
+
static void clip_log_internal_v(enum ggml_log_level level, const char * format, va_list args) {
|
|
176
|
+
if (format == NULL) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
va_list args_copy;
|
|
180
|
+
va_copy(args_copy, args);
|
|
181
|
+
char buffer[128];
|
|
182
|
+
int len = vsnprintf(buffer, 128, format, args);
|
|
183
|
+
if (len < 128) {
|
|
184
|
+
g_logger_state.log_callback(level, buffer, g_logger_state.log_callback_user_data);
|
|
185
|
+
} else {
|
|
186
|
+
char * buffer2 = (char *) calloc(len + 1, sizeof(char));
|
|
187
|
+
vsnprintf(buffer2, len + 1, format, args_copy);
|
|
188
|
+
buffer2[len] = 0;
|
|
189
|
+
g_logger_state.log_callback(level, buffer2, g_logger_state.log_callback_user_data);
|
|
190
|
+
free(buffer2);
|
|
191
|
+
}
|
|
192
|
+
va_end(args_copy);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static void clip_log_internal(enum ggml_log_level level, const char * format, ...) {
|
|
196
|
+
va_list args;
|
|
197
|
+
va_start(args, format);
|
|
198
|
+
clip_log_internal_v(level, format, args);
|
|
199
|
+
va_end(args);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#define LOG_TMPL(level, ...) \
|
|
203
|
+
do { \
|
|
204
|
+
if ((level) >= g_logger_state.verbosity_thold) { \
|
|
205
|
+
clip_log_internal((level), __VA_ARGS__); \
|
|
206
|
+
} \
|
|
207
|
+
} while (0)
|
|
208
|
+
#define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, __VA_ARGS__)
|
|
209
|
+
#define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, __VA_ARGS__)
|
|
210
|
+
#define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
|
|
211
|
+
#define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, __VA_ARGS__)
|
|
212
|
+
#define LOG_CNT(...) LOG_TMPL(GGML_LOG_LEVEL_CONT, __VA_ARGS__)
|
|
213
|
+
|
|
214
|
+
//
|
|
215
|
+
// cpp wrappers
|
|
216
|
+
//
|
|
217
|
+
|
|
218
|
+
// wrapper for clip_image_size
|
|
219
|
+
struct clip_image_size_deleter {
|
|
220
|
+
void operator()(clip_image_size * val) { clip_image_size_free(val); }
|
|
221
|
+
};
|
|
222
|
+
typedef std::unique_ptr<clip_image_size, clip_image_size_deleter> clip_image_size_ptr;
|
|
223
|
+
|
|
224
|
+
// wrapper for clip_image_u8
|
|
225
|
+
struct clip_image_u8_deleter {
|
|
226
|
+
void operator()(clip_image_u8 * val) { clip_image_u8_free(val); }
|
|
227
|
+
};
|
|
228
|
+
typedef std::unique_ptr<clip_image_u8, clip_image_u8_deleter> clip_image_u8_ptr;
|
|
229
|
+
|
|
230
|
+
// wrapper for clip_image_f32
|
|
231
|
+
struct clip_image_f32_deleter {
|
|
232
|
+
void operator()(clip_image_f32 * val) { clip_image_f32_free(val); }
|
|
233
|
+
};
|
|
234
|
+
typedef std::unique_ptr<clip_image_f32, clip_image_f32_deleter> clip_image_f32_ptr;
|
|
235
|
+
|
|
236
|
+
struct clip_image_u8_batch {
|
|
237
|
+
std::vector<clip_image_u8_ptr> entries;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
struct clip_image_f32_batch {
|
|
241
|
+
std::vector<clip_image_f32_ptr> entries;
|
|
242
|
+
|
|
243
|
+
clip_image_f32_batch clone() const {
|
|
244
|
+
clip_image_f32_batch new_batch;
|
|
245
|
+
new_batch.entries.reserve(entries.size());
|
|
246
|
+
for (const auto & entry : entries) {
|
|
247
|
+
new_batch.entries.emplace_back(new clip_image_f32(*entry));
|
|
248
|
+
}
|
|
249
|
+
return new_batch;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
//
|
|
254
|
+
// common utils
|
|
255
|
+
//
|
|
256
|
+
|
|
257
|
+
static std::string string_format(const char * fmt, ...) {
|
|
258
|
+
va_list ap;
|
|
259
|
+
va_list ap2;
|
|
260
|
+
va_start(ap, fmt);
|
|
261
|
+
va_copy(ap2, ap);
|
|
262
|
+
int size = vsnprintf(NULL, 0, fmt, ap);
|
|
263
|
+
GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
|
|
264
|
+
std::vector<char> buf(size + 1);
|
|
265
|
+
int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
|
|
266
|
+
GGML_ASSERT(size2 == size);
|
|
267
|
+
va_end(ap2);
|
|
268
|
+
va_end(ap);
|
|
269
|
+
return std::string(buf.data(), buf.size());
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
static void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
|
273
|
+
if (search.empty()) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
std::string builder;
|
|
277
|
+
builder.reserve(s.length());
|
|
278
|
+
size_t pos = 0;
|
|
279
|
+
size_t last_pos = 0;
|
|
280
|
+
while ((pos = s.find(search, last_pos)) != std::string::npos) {
|
|
281
|
+
builder.append(s, last_pos, pos - last_pos);
|
|
282
|
+
builder.append(replace);
|
|
283
|
+
last_pos = pos + search.length();
|
|
284
|
+
}
|
|
285
|
+
builder.append(s, last_pos, std::string::npos);
|
|
286
|
+
s = std::move(builder);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// split string by a `std::string delim` instead of `char delim`
|
|
290
|
+
static std::vector<std::string> string_split_str(std::string s, const std::string & delimiter) {
|
|
291
|
+
std::vector<std::string> tokens;
|
|
292
|
+
size_t pos = 0;
|
|
293
|
+
std::string token;
|
|
294
|
+
while ((pos = s.find(delimiter)) != std::string::npos) {
|
|
295
|
+
token = s.substr(0, pos);
|
|
296
|
+
tokens.push_back(token);
|
|
297
|
+
s.erase(0, pos + delimiter.length());
|
|
298
|
+
}
|
|
299
|
+
tokens.push_back(s);
|
|
300
|
+
return tokens;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
//
|
|
304
|
+
// gguf utils
|
|
305
|
+
//
|
|
306
|
+
|
|
307
|
+
static std::string gguf_data_to_str(enum gguf_type type, const void * data, int i) {
|
|
308
|
+
switch (type) {
|
|
309
|
+
case GGUF_TYPE_UINT8: return std::to_string(((const uint8_t *)data)[i]);
|
|
310
|
+
case GGUF_TYPE_INT8: return std::to_string(((const int8_t *)data)[i]);
|
|
311
|
+
case GGUF_TYPE_UINT16: return std::to_string(((const uint16_t *)data)[i]);
|
|
312
|
+
case GGUF_TYPE_INT16: return std::to_string(((const int16_t *)data)[i]);
|
|
313
|
+
case GGUF_TYPE_UINT32: return std::to_string(((const uint32_t *)data)[i]);
|
|
314
|
+
case GGUF_TYPE_INT32: return std::to_string(((const int32_t *)data)[i]);
|
|
315
|
+
case GGUF_TYPE_UINT64: return std::to_string(((const uint64_t *)data)[i]);
|
|
316
|
+
case GGUF_TYPE_INT64: return std::to_string(((const int64_t *)data)[i]);
|
|
317
|
+
case GGUF_TYPE_FLOAT32: return std::to_string(((const float *)data)[i]);
|
|
318
|
+
case GGUF_TYPE_FLOAT64: return std::to_string(((const double *)data)[i]);
|
|
319
|
+
case GGUF_TYPE_BOOL: return ((const bool *)data)[i] ? "true" : "false";
|
|
320
|
+
default: return string_format("unknown type %d", type);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {
|
|
325
|
+
const enum gguf_type type = gguf_get_kv_type(ctx_gguf, i);
|
|
326
|
+
|
|
327
|
+
switch (type) {
|
|
328
|
+
case GGUF_TYPE_STRING:
|
|
329
|
+
return gguf_get_val_str(ctx_gguf, i);
|
|
330
|
+
case GGUF_TYPE_ARRAY:
|
|
331
|
+
{
|
|
332
|
+
const enum gguf_type arr_type = gguf_get_arr_type(ctx_gguf, i);
|
|
333
|
+
int arr_n = gguf_get_arr_n(ctx_gguf, i);
|
|
334
|
+
const void * data = arr_type == GGUF_TYPE_STRING ? nullptr : gguf_get_arr_data(ctx_gguf, i);
|
|
335
|
+
std::stringstream ss;
|
|
336
|
+
ss << "[";
|
|
337
|
+
for (int j = 0; j < arr_n; j++) {
|
|
338
|
+
if (arr_type == GGUF_TYPE_STRING) {
|
|
339
|
+
std::string val = gguf_get_arr_str(ctx_gguf, i, j);
|
|
340
|
+
// escape quotes
|
|
341
|
+
string_replace_all(val, "\\", "\\\\");
|
|
342
|
+
string_replace_all(val, "\"", "\\\"");
|
|
343
|
+
ss << '"' << val << '"';
|
|
344
|
+
} else if (arr_type == GGUF_TYPE_ARRAY) {
|
|
345
|
+
ss << "???";
|
|
346
|
+
} else {
|
|
347
|
+
ss << gguf_data_to_str(arr_type, data, j);
|
|
348
|
+
}
|
|
349
|
+
if (j < arr_n - 1) {
|
|
350
|
+
ss << ", ";
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
ss << "]";
|
|
354
|
+
return ss.str();
|
|
355
|
+
}
|
|
356
|
+
default:
|
|
357
|
+
return gguf_data_to_str(type, gguf_get_val_data(ctx_gguf, i), 0);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
//
|
|
362
|
+
// API used internally with mtmd
|
|
363
|
+
//
|
|
364
|
+
|
|
365
|
+
projector_type clip_get_projector_type(const struct clip_ctx * ctx);
|