llama-cpp-pro 0.2.2
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/CHANGELOG.md +295 -0
- package/LICENSE +21 -0
- package/LlamaCpp.podspec +33 -0
- package/LlamaCppCapacitor.podspec +33 -0
- package/Package.swift +29 -0
- package/README.md +93 -0
- package/android/build.gradle +88 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/CMakeLists-arm64.txt +138 -0
- package/android/src/main/CMakeLists-x86_64.txt +141 -0
- package/android/src/main/CMakeLists.txt +138 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCpp.java +1340 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/LlamaCppPlugin.java +814 -0
- package/android/src/main/java/ai/annadata/plugin/capacitor/ModelAdmissionController.java +214 -0
- package/android/src/main/jni-chat-session.cpp +261 -0
- package/android/src/main/jni-lora.cpp +197 -0
- package/android/src/main/jni-multimodal.cpp +167 -0
- package/android/src/main/jni-tts.cpp +290 -0
- package/android/src/main/jni-utils.h +148 -0
- package/android/src/main/jni.cpp +1808 -0
- package/android/src/main/jniLibs/arm64-v8a/libllama-cpp-arm64.so +0 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/build-native.sh +280 -0
- package/cmake/desktop-metal-embed.cmake +30 -0
- package/cmake/desktop-sources.cmake +100 -0
- package/cmake/ggml-backends.cmake +44 -0
- package/cpp/LICENSE +21 -0
- package/cpp/README.md +15 -0
- package/cpp/anyascii.c +22223 -0
- package/cpp/anyascii.h +42 -0
- package/cpp/cap-completion.cpp +942 -0
- package/cpp/cap-completion.h +127 -0
- package/cpp/cap-embedding.cpp +193 -0
- package/cpp/cap-embedding.h +35 -0
- package/cpp/cap-ios-bridge.cpp +1810 -0
- package/cpp/cap-ios-bridge.h +61 -0
- package/cpp/cap-llama.cpp +412 -0
- package/cpp/cap-llama.h +161 -0
- package/cpp/cap-mtmd.hpp +602 -0
- package/cpp/cap-native-server.cpp +1095 -0
- package/cpp/cap-native-server.h +40 -0
- package/cpp/cap-tts.cpp +591 -0
- package/cpp/cap-tts.h +59 -0
- package/cpp/cap-wasm-fs.cpp +156 -0
- package/cpp/cap-wasm-jspi.cpp +19 -0
- package/cpp/cap-wasm-jspi.h +30 -0
- package/cpp/cap-wasm-vfs.cpp +22 -0
- package/cpp/chat-parser.cpp +393 -0
- package/cpp/chat-parser.h +120 -0
- package/cpp/chat.cpp +2315 -0
- package/cpp/chat.h +221 -0
- package/cpp/common.cpp +1664 -0
- package/cpp/common.h +744 -0
- package/cpp/ggml-alloc.c +1028 -0
- package/cpp/ggml-alloc.h +76 -0
- package/cpp/ggml-backend-impl.h +255 -0
- package/cpp/ggml-backend-reg.cpp +600 -0
- package/cpp/ggml-backend.cpp +2121 -0
- package/cpp/ggml-backend.h +354 -0
- package/cpp/ggml-common.h +1878 -0
- package/cpp/ggml-cpp.h +39 -0
- package/cpp/ggml-cpu/amx/amx.cpp +221 -0
- package/cpp/ggml-cpu/amx/amx.h +8 -0
- package/cpp/ggml-cpu/amx/common.h +91 -0
- package/cpp/ggml-cpu/amx/mmq.cpp +2512 -0
- package/cpp/ggml-cpu/amx/mmq.h +10 -0
- package/cpp/ggml-cpu/arch/arm/cpu-feats.cpp +94 -0
- package/cpp/ggml-cpu/arch/arm/quants.c +3650 -0
- package/cpp/ggml-cpu/arch/arm/repack.cpp +1891 -0
- package/cpp/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
- package/cpp/ggml-cpu/arch/x86/quants.c +3820 -0
- package/cpp/ggml-cpu/arch/x86/repack.cpp +6307 -0
- package/cpp/ggml-cpu/arch-fallback.h +215 -0
- package/cpp/ggml-cpu/binary-ops.cpp +158 -0
- package/cpp/ggml-cpu/binary-ops.h +16 -0
- package/cpp/ggml-cpu/common.h +73 -0
- package/cpp/ggml-cpu/ggml-cpu-impl.h +559 -0
- package/cpp/ggml-cpu/ggml-cpu.c +3578 -0
- package/cpp/ggml-cpu/ggml-cpu.cpp +672 -0
- package/cpp/ggml-cpu/ops.cpp +10587 -0
- package/cpp/ggml-cpu/ops.h +114 -0
- package/cpp/ggml-cpu/quants.c +1193 -0
- package/cpp/ggml-cpu/quants.h +97 -0
- package/cpp/ggml-cpu/repack.cpp +1982 -0
- package/cpp/ggml-cpu/repack.h +120 -0
- package/cpp/ggml-cpu/simd-mappings.h +1184 -0
- package/cpp/ggml-cpu/traits.cpp +36 -0
- package/cpp/ggml-cpu/traits.h +38 -0
- package/cpp/ggml-cpu/unary-ops.cpp +186 -0
- package/cpp/ggml-cpu/unary-ops.h +28 -0
- package/cpp/ggml-cpu/vec.cpp +348 -0
- package/cpp/ggml-cpu/vec.h +1121 -0
- package/cpp/ggml-cpu.h +145 -0
- package/cpp/ggml-impl.h +622 -0
- package/cpp/ggml-metal-impl.h +688 -0
- package/cpp/ggml-metal.h +66 -0
- package/cpp/ggml-metal.m +6833 -0
- package/cpp/ggml-metal.metal +10754 -0
- package/cpp/ggml-opt.cpp +1093 -0
- package/cpp/ggml-opt.h +256 -0
- package/cpp/ggml-quants.c +5324 -0
- package/cpp/ggml-quants.h +106 -0
- package/cpp/ggml-threading.cpp +12 -0
- package/cpp/ggml-threading.h +14 -0
- package/cpp/ggml.c +7108 -0
- package/cpp/ggml.h +2492 -0
- package/cpp/gguf.cpp +1358 -0
- package/cpp/gguf.h +202 -0
- package/cpp/json-partial.cpp +256 -0
- package/cpp/json-partial.h +38 -0
- package/cpp/json-schema-to-grammar.cpp +985 -0
- package/cpp/json-schema-to-grammar.h +21 -0
- package/cpp/llama-adapter.cpp +388 -0
- package/cpp/llama-adapter.h +76 -0
- package/cpp/llama-arch.cpp +2355 -0
- package/cpp/llama-arch.h +499 -0
- package/cpp/llama-batch.cpp +875 -0
- package/cpp/llama-batch.h +160 -0
- package/cpp/llama-chat.cpp +783 -0
- package/cpp/llama-chat.h +65 -0
- package/cpp/llama-context.cpp +2788 -0
- package/cpp/llama-context.h +306 -0
- package/cpp/llama-cparams.cpp +5 -0
- package/cpp/llama-cparams.h +41 -0
- package/cpp/llama-cpp.h +30 -0
- package/cpp/llama-grammar.cpp +1229 -0
- package/cpp/llama-grammar.h +173 -0
- package/cpp/llama-graph.cpp +1891 -0
- package/cpp/llama-graph.h +810 -0
- package/cpp/llama-hparams.cpp +180 -0
- package/cpp/llama-hparams.h +233 -0
- package/cpp/llama-impl.cpp +167 -0
- package/cpp/llama-impl.h +61 -0
- package/cpp/llama-io.cpp +15 -0
- package/cpp/llama-io.h +35 -0
- package/cpp/llama-kv-cache-iswa.cpp +318 -0
- package/cpp/llama-kv-cache-iswa.h +135 -0
- package/cpp/llama-kv-cache.cpp +2059 -0
- package/cpp/llama-kv-cache.h +374 -0
- package/cpp/llama-kv-cells.h +491 -0
- package/cpp/llama-memory-hybrid.cpp +258 -0
- package/cpp/llama-memory-hybrid.h +137 -0
- package/cpp/llama-memory-recurrent.cpp +1146 -0
- package/cpp/llama-memory-recurrent.h +179 -0
- package/cpp/llama-memory.cpp +59 -0
- package/cpp/llama-memory.h +119 -0
- package/cpp/llama-mmap.cpp +609 -0
- package/cpp/llama-mmap.h +68 -0
- package/cpp/llama-model-loader.cpp +1166 -0
- package/cpp/llama-model-loader.h +170 -0
- package/cpp/llama-model-saver.cpp +282 -0
- package/cpp/llama-model-saver.h +37 -0
- package/cpp/llama-model.cpp +19061 -0
- package/cpp/llama-model.h +491 -0
- package/cpp/llama-sampling.cpp +2575 -0
- package/cpp/llama-sampling.h +32 -0
- package/cpp/llama-vocab.cpp +3792 -0
- package/cpp/llama-vocab.h +176 -0
- package/cpp/llama.cpp +358 -0
- package/cpp/llama.h +1373 -0
- package/cpp/log.cpp +428 -0
- package/cpp/log.h +103 -0
- package/cpp/minja/chat-template.hpp +550 -0
- package/cpp/minja/minja.hpp +3009 -0
- package/cpp/nlohmann/json.hpp +25526 -0
- package/cpp/nlohmann/json_fwd.hpp +187 -0
- package/cpp/regex-partial.cpp +204 -0
- package/cpp/regex-partial.h +56 -0
- package/cpp/sampling.cpp +579 -0
- package/cpp/sampling.h +107 -0
- package/cpp/tools/mtmd/clip-impl.h +473 -0
- package/cpp/tools/mtmd/clip.cpp +4322 -0
- package/cpp/tools/mtmd/clip.h +106 -0
- package/cpp/tools/mtmd/miniaudio/miniaudio.h +93468 -0
- package/cpp/tools/mtmd/mtmd-audio.cpp +769 -0
- package/cpp/tools/mtmd/mtmd-audio.h +47 -0
- package/cpp/tools/mtmd/mtmd-helper.cpp +460 -0
- package/cpp/tools/mtmd/mtmd-helper.h +95 -0
- package/cpp/tools/mtmd/mtmd.cpp +1066 -0
- package/cpp/tools/mtmd/mtmd.h +302 -0
- package/cpp/tools/mtmd/stb/stb_image.h +7988 -0
- package/cpp/unicode-data.cpp +7034 -0
- package/cpp/unicode-data.h +20 -0
- package/cpp/unicode.cpp +1061 -0
- package/cpp/unicode.h +68 -0
- package/cpp/vendor/cpp-httplib/httplib.cpp +16509 -0
- package/cpp/vendor/cpp-httplib/httplib.h +3883 -0
- package/desktop/electron-builder.config.cjs +157 -0
- package/desktop/entitlements.mac.plist +12 -0
- package/desktop/package.json +11 -0
- package/desktop/resolve-package-root.cjs +88 -0
- package/desktop/src/main/backend-selector.cjs +148 -0
- package/desktop/src/main/gpu-probe.cjs +142 -0
- package/desktop/src/main/index.cjs +58 -0
- package/desktop/src/main/ipc-handlers.cjs +212 -0
- package/desktop/src/main/model-store.cjs +50 -0
- package/desktop/src/main/preload.cjs +39 -0
- package/desktop/src/main/sidecar-client.cjs +76 -0
- package/desktop/src/main/sidecar-manager.cjs +364 -0
- package/dist/docs.json +14357 -0
- package/dist/esm/definitions.d.ts +731 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/desktop.d.ts +37 -0
- package/dist/esm/desktop.js +133 -0
- package/dist/esm/desktop.js.map +1 -0
- package/dist/esm/index.d.ts +200 -0
- package/dist/esm/index.js +612 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/isomorphic/desktop.runtime.d.ts +37 -0
- package/dist/esm/isomorphic/desktop.runtime.js +36 -0
- package/dist/esm/isomorphic/desktop.runtime.js.map +1 -0
- package/dist/esm/isomorphic/errors.d.ts +6 -0
- package/dist/esm/isomorphic/errors.js +9 -0
- package/dist/esm/isomorphic/errors.js.map +1 -0
- package/dist/esm/isomorphic/model.admission.d.ts +17 -0
- package/dist/esm/isomorphic/model.admission.js +27 -0
- package/dist/esm/isomorphic/model.admission.js.map +1 -0
- package/dist/esm/isomorphic/model.scheduler.d.ts +31 -0
- package/dist/esm/isomorphic/model.scheduler.js +95 -0
- package/dist/esm/isomorphic/model.scheduler.js.map +1 -0
- package/dist/esm/isomorphic/provider.desktop.d.ts +40 -0
- package/dist/esm/isomorphic/provider.desktop.js +411 -0
- package/dist/esm/isomorphic/provider.desktop.js.map +1 -0
- package/dist/esm/isomorphic/provider.factory.d.ts +2 -0
- package/dist/esm/isomorphic/provider.factory.js +16 -0
- package/dist/esm/isomorphic/provider.factory.js.map +1 -0
- package/dist/esm/isomorphic/provider.interface.d.ts +76 -0
- package/dist/esm/isomorphic/provider.interface.js +2 -0
- package/dist/esm/isomorphic/provider.interface.js.map +1 -0
- package/dist/esm/isomorphic/provider.native.d.ts +18 -0
- package/dist/esm/isomorphic/provider.native.js +173 -0
- package/dist/esm/isomorphic/provider.native.js.map +1 -0
- package/dist/esm/isomorphic/provider.web.d.ts +88 -0
- package/dist/esm/isomorphic/provider.web.js +573 -0
- package/dist/esm/isomorphic/provider.web.js.map +1 -0
- package/dist/esm/isomorphic/sidecar-sse.d.ts +14 -0
- package/dist/esm/isomorphic/sidecar-sse.js +76 -0
- package/dist/esm/isomorphic/sidecar-sse.js.map +1 -0
- package/dist/esm/isomorphic/wasmMemoryCalibration.d.ts +26 -0
- package/dist/esm/isomorphic/wasmMemoryCalibration.js +60 -0
- package/dist/esm/isomorphic/wasmMemoryCalibration.js.map +1 -0
- package/dist/esm/isomorphic/wasmMemoryPolicy.d.ts +55 -0
- package/dist/esm/isomorphic/wasmMemoryPolicy.js +93 -0
- package/dist/esm/isomorphic/wasmMemoryPolicy.js.map +1 -0
- package/dist/esm/storage/manifest.d.ts +13 -0
- package/dist/esm/storage/manifest.js +87 -0
- package/dist/esm/storage/manifest.js.map +1 -0
- package/dist/esm/storage/opfs.store.d.ts +31 -0
- package/dist/esm/storage/opfs.store.js +241 -0
- package/dist/esm/storage/opfs.store.js.map +1 -0
- package/dist/esm/web.d.ts +206 -0
- package/dist/esm/web.js +521 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/esm/workers/async-file.d.ts +13 -0
- package/dist/esm/workers/async-file.js +12 -0
- package/dist/esm/workers/async-file.js.map +1 -0
- package/dist/esm/workers/heapfs.d.ts +55 -0
- package/dist/esm/workers/heapfs.js +115 -0
- package/dist/esm/workers/heapfs.js.map +1 -0
- package/dist/esm/workers/wasm.engine.d.ts +86 -0
- package/dist/esm/workers/wasm.engine.js +504 -0
- package/dist/esm/workers/wasm.engine.js.map +1 -0
- package/dist/esm/workers/worker.protocol.d.ts +160 -0
- package/dist/esm/workers/worker.protocol.js +2 -0
- package/dist/esm/workers/worker.protocol.js.map +1 -0
- package/dist/plugin.cjs +3179 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.js +3181 -0
- package/dist/plugin.js.map +1 -0
- package/dist/wasm/llama_engine.d.ts +74 -0
- package/dist/wasm/llama_engine.js +1829 -0
- package/dist/wasm/llama_engine.wasm +0 -0
- package/dist/wasm/llama_engine_emscripten.mjs +2 -0
- package/dist/wasm/package.json +16 -0
- package/dist/workers/llm.worker.js +1226 -0
- package/dist/workers/llm.worker.js.map +7 -0
- package/extraResources/llama-wasm/llama_engine.d.ts +74 -0
- package/extraResources/llama-wasm/llama_engine.js +1829 -0
- package/extraResources/llama-wasm/llama_engine.wasm +0 -0
- package/extraResources/llama-wasm/llama_engine_emscripten.mjs +2 -0
- package/extraResources/llama-wasm/package.json +16 -0
- package/extraResources/sidecar/README.md +11 -0
- package/extraResources/sidecar/darwin-arm64 +0 -0
- package/extraResources/sidecar/darwin-x64 +0 -0
- package/ios/CMakeLists-arm64.txt +161 -0
- package/ios/CMakeLists-x86_64.txt +188 -0
- package/ios/CMakeLists.txt +161 -0
- package/ios/Frameworks/llama-cpp.framework/Info.plist +28 -0
- package/ios/Frameworks/llama-cpp.framework/llama-cpp +0 -0
- package/ios/Sources/LlamaCppCapacitor/LlamaCpp.swift +1365 -0
- package/ios/Sources/LlamaCppCapacitor/LlamaCppPlugin.swift +694 -0
- package/ios/Sources/LlamaCppCapacitor/LlamaNativeBridge.swift +349 -0
- package/ios/Sources/LlamaCppCapacitor/ModelAdmissionController.swift +177 -0
- package/ios/embed-metal-shaders.sh +24 -0
- package/ios/metal-embed.cmake +29 -0
- package/package.json +281 -0
- package/scripts/build-js-preserve-wasm.cjs +53 -0
- package/scripts/build-sidecar-linux.sh +6 -0
- package/scripts/build-sidecar-win.bat +19 -0
- package/scripts/build-sidecar.sh +184 -0
- package/scripts/embed-llama-ios-app-framework.sh +63 -0
- package/scripts/ensure-desktop-sidecar-bundle.cjs +103 -0
- package/scripts/ensure-llama-ios-xcframework.sh +108 -0
- package/scripts/fix-esm-extensions.cjs +45 -0
- package/scripts/prepare-js-dist.cjs +28 -0
- package/scripts/stage-desktop-resources.cjs +116 -0
- package/sidecar/CMakeLists.txt +192 -0
- package/sidecar/cap-sidecar-main.cpp +68 -0
- package/types/llama-cpp-pro.d.ts +441 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <stdbool.h>
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
// Core
|
|
11
|
+
int64_t llama_init_context(const char *model_path, const char *params_json);
|
|
12
|
+
void llama_release_context(int64_t context_id);
|
|
13
|
+
bool llama_toggle_native_log(bool enabled);
|
|
14
|
+
char *llama_run_completion(int64_t context_id, const char *params_json);
|
|
15
|
+
void llama_free_completion_result(char *result_json);
|
|
16
|
+
void llama_stop_completion(int64_t context_id);
|
|
17
|
+
/** JSON object: {"embedding":[float,...]} — free with llama_free_completion_result */
|
|
18
|
+
char *llama_run_embedding_json(int64_t context_id, const char *text, const char *params_json);
|
|
19
|
+
|
|
20
|
+
// Rerank — returns JSON [{score,index},...]; free with llama_free_completion_result
|
|
21
|
+
char *llama_rerank_json(int64_t context_id, const char *query, const char *docs_json);
|
|
22
|
+
|
|
23
|
+
// Bench — returns raw "[desc,size,nparams,ppAvg,ppStd,tgAvg,tgStd]"; free with llama_free_completion_result
|
|
24
|
+
char *llama_bench(int64_t context_id, int32_t pp, int32_t tg, int32_t pl, int32_t nr);
|
|
25
|
+
|
|
26
|
+
// Session management — returns JSON; free with llama_free_completion_result
|
|
27
|
+
// NOTE: named llama_cap_* to avoid clashing with the deprecated bool-returning
|
|
28
|
+
// llama_load_session_file / llama_save_session_file in llama.h (llama-context.cpp).
|
|
29
|
+
char *llama_cap_load_session_file(int64_t context_id, const char *filepath);
|
|
30
|
+
int32_t llama_cap_save_session_file(int64_t context_id, const char *filepath, int32_t max_tokens);
|
|
31
|
+
|
|
32
|
+
// LoRA adapters
|
|
33
|
+
int32_t llama_apply_lora_adapters(int64_t context_id, const char *lora_adapters_json);
|
|
34
|
+
void llama_remove_lora_adapters(int64_t context_id);
|
|
35
|
+
/** Returns JSON [{path,scale},...]; free with llama_free_completion_result */
|
|
36
|
+
char *llama_get_loaded_lora_adapters(int64_t context_id, const char *unused);
|
|
37
|
+
|
|
38
|
+
// Multimodal
|
|
39
|
+
int32_t llama_init_multimodal(int64_t context_id, const char *mmproj_path, int32_t use_gpu);
|
|
40
|
+
int32_t llama_is_multimodal_enabled(int64_t context_id);
|
|
41
|
+
/** Returns JSON {vision:bool,audio:bool}; free with llama_free_completion_result */
|
|
42
|
+
char *llama_get_multimodal_support(int64_t context_id, const char *unused);
|
|
43
|
+
void llama_release_multimodal(int64_t context_id);
|
|
44
|
+
|
|
45
|
+
// TTS / Vocoder
|
|
46
|
+
int32_t llama_init_vocoder(int64_t context_id, const char *vocoder_path, int32_t n_batch);
|
|
47
|
+
int32_t llama_is_vocoder_enabled(int64_t context_id);
|
|
48
|
+
/** Returns JSON {prompt,grammar?}; free with llama_free_completion_result */
|
|
49
|
+
char *llama_get_formatted_audio_completion(int64_t context_id, const char *speaker_json, const char *text);
|
|
50
|
+
/** Returns JSON [token,...]; free with llama_free_completion_result */
|
|
51
|
+
char *llama_get_audio_completion_guide_tokens(int64_t context_id, const char *text);
|
|
52
|
+
/** Returns JSON [float,...]; free with llama_free_completion_result */
|
|
53
|
+
char *llama_decode_audio_tokens(int64_t context_id, const char *tokens_json);
|
|
54
|
+
void llama_release_vocoder(int64_t context_id);
|
|
55
|
+
|
|
56
|
+
// GPU info — returns JSON {gpu:bool,reasonNoGPU:string}; free with llama_free_completion_result
|
|
57
|
+
char *llama_get_context_gpu_info(int64_t context_id, const char *unused);
|
|
58
|
+
|
|
59
|
+
#ifdef __cplusplus
|
|
60
|
+
}
|
|
61
|
+
#endif
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
#include "cap-llama.h"
|
|
2
|
+
#include "cap-tts.h"
|
|
3
|
+
#include "cap-mtmd.hpp"
|
|
4
|
+
#include "cap-completion.h"
|
|
5
|
+
|
|
6
|
+
// Include multimodal support
|
|
7
|
+
#include "tools/mtmd/mtmd.h"
|
|
8
|
+
#include "tools/mtmd/mtmd-helper.h"
|
|
9
|
+
#include "tools/mtmd/clip.h"
|
|
10
|
+
|
|
11
|
+
namespace capllama {
|
|
12
|
+
|
|
13
|
+
static const std::vector<lm_ggml_type> kv_cache_types = {
|
|
14
|
+
LM_GGML_TYPE_F32,
|
|
15
|
+
LM_GGML_TYPE_F16,
|
|
16
|
+
LM_GGML_TYPE_BF16,
|
|
17
|
+
LM_GGML_TYPE_Q8_0,
|
|
18
|
+
LM_GGML_TYPE_Q4_0,
|
|
19
|
+
LM_GGML_TYPE_Q4_1,
|
|
20
|
+
LM_GGML_TYPE_IQ4_NL,
|
|
21
|
+
LM_GGML_TYPE_Q5_0,
|
|
22
|
+
LM_GGML_TYPE_Q5_1,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
lm_ggml_type kv_cache_type_from_str(const std::string & s) {
|
|
26
|
+
if (s.empty()) {
|
|
27
|
+
return LM_GGML_TYPE_F16; // Default to F16 if empty string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const auto & type : kv_cache_types) {
|
|
31
|
+
if (lm_ggml_type_name(type) == s) {
|
|
32
|
+
return type;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Return default type instead of throwing to avoid crashes
|
|
37
|
+
return LM_GGML_TYPE_F16;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
void log(const char *level, const char *function, int line,
|
|
42
|
+
const char *format, ...)
|
|
43
|
+
{
|
|
44
|
+
va_list args;
|
|
45
|
+
#if defined(__ANDROID__)
|
|
46
|
+
char prefix[256];
|
|
47
|
+
snprintf(prefix, sizeof(prefix), "%s:%d %s", function, line, format);
|
|
48
|
+
|
|
49
|
+
va_start(args, format);
|
|
50
|
+
android_LogPriority priority;
|
|
51
|
+
if (strcmp(level, "ERROR") == 0) {
|
|
52
|
+
priority = ANDROID_LOG_ERROR;
|
|
53
|
+
} else if (strcmp(level, "WARNING") == 0) {
|
|
54
|
+
priority = ANDROID_LOG_WARN;
|
|
55
|
+
} else if (strcmp(level, "INFO") == 0) {
|
|
56
|
+
priority = ANDROID_LOG_INFO;
|
|
57
|
+
} else {
|
|
58
|
+
priority = ANDROID_LOG_DEBUG;
|
|
59
|
+
}
|
|
60
|
+
__android_log_vprint(priority, "RNLlama", prefix, args);
|
|
61
|
+
va_end(args);
|
|
62
|
+
#else
|
|
63
|
+
printf("[%s] %s:%d ", level, function, line);
|
|
64
|
+
va_start(args, format);
|
|
65
|
+
vprintf(format, args);
|
|
66
|
+
va_end(args);
|
|
67
|
+
printf("\n");
|
|
68
|
+
#endif
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static bool ends_with(const std::string &str, const std::string &suffix)
|
|
72
|
+
{
|
|
73
|
+
return str.size() >= suffix.size() &&
|
|
74
|
+
0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static size_t find_partial_stop_string(const std::string &stop,
|
|
78
|
+
const std::string &text)
|
|
79
|
+
{
|
|
80
|
+
if (!text.empty() && !stop.empty())
|
|
81
|
+
{
|
|
82
|
+
const char text_last_char = text.back();
|
|
83
|
+
for (int64_t char_index = stop.size() - 1; char_index >= 0; char_index--)
|
|
84
|
+
{
|
|
85
|
+
if (stop[char_index] == text_last_char)
|
|
86
|
+
{
|
|
87
|
+
const std::string current_partial = stop.substr(0, char_index + 1);
|
|
88
|
+
if (ends_with(text, current_partial))
|
|
89
|
+
{
|
|
90
|
+
return text.size() - char_index - 1;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return std::string::npos;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// format incomplete utf-8 multibyte character for output
|
|
99
|
+
std::string tokens_to_output_formatted_string(const llama_context *ctx, const llama_token token)
|
|
100
|
+
{
|
|
101
|
+
std::string out = token == -1 ? "" : common_token_to_piece(ctx, token);
|
|
102
|
+
// if the size is 1 and first bit is 1, meaning it's a partial character
|
|
103
|
+
// (size > 1 meaning it's already a known token)
|
|
104
|
+
if (out.size() == 1 && (out[0] & 0x80) == 0x80)
|
|
105
|
+
{
|
|
106
|
+
std::stringstream ss;
|
|
107
|
+
ss << std::hex << (out[0] & 0xff);
|
|
108
|
+
std::string res(ss.str());
|
|
109
|
+
out = "byte: \\x" + res;
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
std::string tokens_to_str(llama_context *ctx, const std::vector<llama_token>::const_iterator begin, const std::vector<llama_token>::const_iterator end)
|
|
115
|
+
{
|
|
116
|
+
std::string ret;
|
|
117
|
+
for (auto it = begin; it != end; ++it)
|
|
118
|
+
{
|
|
119
|
+
ret += common_token_to_piece(ctx, *it);
|
|
120
|
+
}
|
|
121
|
+
return ret;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
llama_cap_context::~llama_cap_context() {
|
|
126
|
+
if (completion != nullptr) {
|
|
127
|
+
delete completion;
|
|
128
|
+
completion = nullptr;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
releaseMultimodal();
|
|
132
|
+
releaseVocoder();
|
|
133
|
+
releaseDraftModel(); // Clean up speculative decoding resources
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
bool llama_cap_context::loadModel(common_params ¶ms_)
|
|
137
|
+
{
|
|
138
|
+
params = params_;
|
|
139
|
+
#ifdef __EMSCRIPTEN__
|
|
140
|
+
fprintf(stderr, "@@WASM_LOAD@@ loadModel: common_init_from_params begin\n");
|
|
141
|
+
#endif
|
|
142
|
+
llama_init = common_init_from_params(params);
|
|
143
|
+
model = llama_init.model.get();
|
|
144
|
+
ctx = llama_init.context.get();
|
|
145
|
+
if (model == nullptr)
|
|
146
|
+
{
|
|
147
|
+
LOG_ERROR("unable to load model: %s", params_.model.path.c_str());
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
#ifdef __EMSCRIPTEN__
|
|
151
|
+
fprintf(stderr, "@@WASM_LOAD@@ loadModel: common_init_from_params ok, chat templates begin\n");
|
|
152
|
+
#endif
|
|
153
|
+
templates = common_chat_templates_init(model, params.chat_template);
|
|
154
|
+
#ifdef __EMSCRIPTEN__
|
|
155
|
+
fprintf(stderr, "@@WASM_LOAD@@ loadModel: chat templates ok, completion init begin\n");
|
|
156
|
+
#endif
|
|
157
|
+
n_ctx = llama_n_ctx(ctx);
|
|
158
|
+
|
|
159
|
+
// Initialize completion context
|
|
160
|
+
if (completion != nullptr) {
|
|
161
|
+
delete completion;
|
|
162
|
+
}
|
|
163
|
+
completion = new llama_cap_context_completion(this);
|
|
164
|
+
|
|
165
|
+
#ifdef __EMSCRIPTEN__
|
|
166
|
+
fprintf(stderr, "@@WASM_LOAD@@ loadModel: completion ok\n");
|
|
167
|
+
#endif
|
|
168
|
+
|
|
169
|
+
// Initialize context shift flag
|
|
170
|
+
LOG_INFO("ctx_shift: %s", params.ctx_shift ? "enabled" : "disabled");
|
|
171
|
+
|
|
172
|
+
// We can uncomment for debugging or after this fix: https://github.com/ggerganov/llama.cpp/pull/11101
|
|
173
|
+
// LOG_INFO("%s\n", common_params_get_system_info(params).c_str());
|
|
174
|
+
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
bool llama_cap_context::validateModelChatTemplate(bool use_jinja, const char *name) const {
|
|
180
|
+
const char * tmpl = llama_model_chat_template(model, name);
|
|
181
|
+
if (tmpl == nullptr) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
return common_chat_verify_template(tmpl, use_jinja);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
common_chat_params llama_cap_context::getFormattedChatWithJinja(
|
|
188
|
+
const std::string& messages,
|
|
189
|
+
const std::string& chat_template,
|
|
190
|
+
const std::string& json_schema,
|
|
191
|
+
const std::string& tools,
|
|
192
|
+
const bool& parallel_tool_calls,
|
|
193
|
+
const std::string& tool_choice,
|
|
194
|
+
const bool& enable_thinking,
|
|
195
|
+
const bool& add_generation_prompt,
|
|
196
|
+
const std::string& now_str,
|
|
197
|
+
const std::map<std::string, std::string>& chat_template_kwargs
|
|
198
|
+
) const {
|
|
199
|
+
common_chat_templates_inputs inputs;
|
|
200
|
+
inputs.use_jinja = true;
|
|
201
|
+
inputs.messages = common_chat_msgs_parse_oaicompat(json::parse(messages));
|
|
202
|
+
auto useTools = !tools.empty();
|
|
203
|
+
if (useTools) {
|
|
204
|
+
inputs.tools = common_chat_tools_parse_oaicompat(json::parse(tools));
|
|
205
|
+
}
|
|
206
|
+
inputs.parallel_tool_calls = parallel_tool_calls;
|
|
207
|
+
if (!tool_choice.empty()) {
|
|
208
|
+
inputs.tool_choice = common_chat_tool_choice_parse_oaicompat(tool_choice);
|
|
209
|
+
}
|
|
210
|
+
if (!json_schema.empty()) {
|
|
211
|
+
inputs.json_schema = json::parse(json_schema);
|
|
212
|
+
}
|
|
213
|
+
inputs.enable_thinking = enable_thinking;
|
|
214
|
+
inputs.add_generation_prompt = add_generation_prompt;
|
|
215
|
+
|
|
216
|
+
// Handle now parameter - parse timestamp or use current time
|
|
217
|
+
if (!now_str.empty()) {
|
|
218
|
+
try {
|
|
219
|
+
// Try to parse as timestamp (seconds since epoch)
|
|
220
|
+
auto timestamp = std::stoll(now_str);
|
|
221
|
+
inputs.now = std::chrono::system_clock::from_time_t(timestamp);
|
|
222
|
+
} catch (...) {
|
|
223
|
+
// If parsing fails, use current time
|
|
224
|
+
inputs.now = std::chrono::system_clock::now();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
inputs.chat_template_kwargs = chat_template_kwargs;
|
|
229
|
+
|
|
230
|
+
// If chat_template is provided, create new one and use it (probably slow)
|
|
231
|
+
if (!chat_template.empty()) {
|
|
232
|
+
auto tmps = common_chat_templates_init(model, chat_template);
|
|
233
|
+
return common_chat_templates_apply(tmps.get(), inputs);
|
|
234
|
+
} else {
|
|
235
|
+
return common_chat_templates_apply(templates.get(), inputs);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
std::string llama_cap_context::getFormattedChat(
|
|
240
|
+
const std::string &messages,
|
|
241
|
+
const std::string &chat_template
|
|
242
|
+
) const {
|
|
243
|
+
common_chat_templates_inputs inputs;
|
|
244
|
+
inputs.messages = common_chat_msgs_parse_oaicompat(json::parse(messages));
|
|
245
|
+
inputs.use_jinja = false;
|
|
246
|
+
|
|
247
|
+
// If chat_template is provided, create new one and use it (probably slow)
|
|
248
|
+
if (!chat_template.empty()) {
|
|
249
|
+
auto tmps = common_chat_templates_init(model, chat_template);
|
|
250
|
+
return common_chat_templates_apply(tmps.get(), inputs).prompt;
|
|
251
|
+
} else {
|
|
252
|
+
return common_chat_templates_apply(templates.get(), inputs).prompt;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
llama_cap_tokenize_result llama_cap_context::tokenize(const std::string &text, const std::vector<std::string> &media_paths) {
|
|
257
|
+
if (media_paths.size() > 0) {
|
|
258
|
+
if (!isMultimodalEnabled()) {
|
|
259
|
+
throw std::runtime_error("Multimodal is not enabled but media paths are provided");
|
|
260
|
+
}
|
|
261
|
+
auto result = tokenizeWithMedia(mtmd_wrapper, text, media_paths);
|
|
262
|
+
mtmd_input_chunks_free(result.chunks);
|
|
263
|
+
llama_cap_tokenize_result tokenize_result = {
|
|
264
|
+
.tokens = result.tokens,
|
|
265
|
+
.has_media = true,
|
|
266
|
+
.bitmap_hashes = result.bitmap_hashes,
|
|
267
|
+
.chunk_pos = result.chunk_pos,
|
|
268
|
+
.chunk_pos_media = result.chunk_pos_media,
|
|
269
|
+
};
|
|
270
|
+
return tokenize_result;
|
|
271
|
+
}
|
|
272
|
+
std::vector<llama_token> text_tokens;
|
|
273
|
+
text_tokens = common_tokenize(ctx, text, false);
|
|
274
|
+
llama_cap_tokenize_result tokenize_result = {
|
|
275
|
+
.tokens = text_tokens,
|
|
276
|
+
.has_media = false,
|
|
277
|
+
.bitmap_hashes = {},
|
|
278
|
+
.chunk_pos = {},
|
|
279
|
+
.chunk_pos_media = {},
|
|
280
|
+
};
|
|
281
|
+
return tokenize_result;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
int llama_cap_context::applyLoraAdapters(std::vector<common_adapter_lora_info> lora) {
|
|
285
|
+
for (auto &la : lora) {
|
|
286
|
+
la.ptr = llama_adapter_lora_init(model, la.path.c_str());
|
|
287
|
+
if (la.ptr == nullptr) {
|
|
288
|
+
LOG_ERROR("failed to apply lora adapter '%s'\n", la.path.c_str());
|
|
289
|
+
return -1;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
this->lora = lora;
|
|
293
|
+
common_set_adapter_lora(ctx, lora);
|
|
294
|
+
return 0;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
void llama_cap_context::removeLoraAdapters() {
|
|
298
|
+
this->lora.clear();
|
|
299
|
+
common_set_adapter_lora(ctx, this->lora); // apply empty list
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
std::vector<common_adapter_lora_info> llama_cap_context::getLoadedLoraAdapters() {
|
|
303
|
+
return this->lora;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
bool llama_cap_context::initMultimodal(const std::string &mmproj_path, bool use_gpu) {
|
|
307
|
+
try {
|
|
308
|
+
mtmd_wrapper = new llama_cap_context_mtmd(mmproj_path, use_gpu, model, ctx, params, has_multimodal, params);
|
|
309
|
+
return true;
|
|
310
|
+
} catch (const std::exception& e) {
|
|
311
|
+
LOG_ERROR("[DEBUG] Failed to initialize multimodal: %s", e.what());
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
bool llama_cap_context::isMultimodalEnabled() const {
|
|
317
|
+
return mtmd_wrapper != nullptr && mtmd_wrapper->isEnabled(has_multimodal);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
bool llama_cap_context::isMultimodalSupportVision() const {
|
|
321
|
+
return isMultimodalEnabled() && mtmd_wrapper->supportVision();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
bool llama_cap_context::isMultimodalSupportAudio() const {
|
|
325
|
+
return isMultimodalEnabled() && mtmd_wrapper->supportAudio();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
void llama_cap_context::releaseMultimodal() {
|
|
329
|
+
if (mtmd_wrapper != nullptr) {
|
|
330
|
+
delete mtmd_wrapper;
|
|
331
|
+
mtmd_wrapper = nullptr;
|
|
332
|
+
has_multimodal = false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
bool llama_cap_context::initVocoder(const std::string &vocoder_model_path, int batch_size) {
|
|
337
|
+
try {
|
|
338
|
+
tts_wrapper = new llama_cap_context_tts(vocoder_model_path, batch_size);
|
|
339
|
+
has_vocoder = true;
|
|
340
|
+
return true;
|
|
341
|
+
} catch (const std::exception& e) {
|
|
342
|
+
has_vocoder = false;
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
bool llama_cap_context::isVocoderEnabled() const {
|
|
348
|
+
return has_vocoder && tts_wrapper != nullptr;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
void llama_cap_context::releaseVocoder() {
|
|
352
|
+
if (tts_wrapper != nullptr) {
|
|
353
|
+
delete tts_wrapper;
|
|
354
|
+
tts_wrapper = nullptr;
|
|
355
|
+
}
|
|
356
|
+
has_vocoder = false;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Speculative decoding methods
|
|
360
|
+
bool llama_cap_context::loadDraftModel(const std::string &draft_model_path) {
|
|
361
|
+
if (draft_model_path.empty()) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Create draft model parameters (based on main model params)
|
|
366
|
+
common_params draft_params = params;
|
|
367
|
+
draft_params.model.path = draft_model_path;
|
|
368
|
+
|
|
369
|
+
// Mobile optimization: smaller context for draft model
|
|
370
|
+
if (mobile_speculative) {
|
|
371
|
+
draft_params.n_ctx = std::min(params.n_ctx, 1024); // Limit draft context
|
|
372
|
+
draft_params.n_batch = std::min(params.n_batch, 128); // Smaller batch
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
try {
|
|
376
|
+
// For now, use simplified draft model initialization
|
|
377
|
+
// This would be expanded in a full implementation to properly initialize
|
|
378
|
+
// the draft model and context
|
|
379
|
+
|
|
380
|
+
// TODO: Implement proper draft model loading
|
|
381
|
+
// draft_model = llama_load_model_from_file(draft_model_path.c_str(), draft_params);
|
|
382
|
+
// draft_ctx = llama_new_context_with_model(draft_model, draft_params);
|
|
383
|
+
|
|
384
|
+
// For this implementation, we'll disable speculative decoding
|
|
385
|
+
// until proper model loading is implemented
|
|
386
|
+
printf("Draft model loading not yet implemented - falling back to regular decoding\n");
|
|
387
|
+
speculative_enabled = false;
|
|
388
|
+
return false;
|
|
389
|
+
|
|
390
|
+
} catch (const std::exception& e) {
|
|
391
|
+
printf("Failed to load draft model: %s\n", e.what());
|
|
392
|
+
releaseDraftModel();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
void llama_cap_context::releaseDraftModel() {
|
|
399
|
+
if (draft_ctx) {
|
|
400
|
+
// Note: draft_ctx and draft_model are managed by common_init_result
|
|
401
|
+
// They will be automatically cleaned up
|
|
402
|
+
draft_ctx = nullptr;
|
|
403
|
+
draft_model = nullptr;
|
|
404
|
+
}
|
|
405
|
+
speculative_enabled = false;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
bool llama_cap_context::isSpectulativeEnabled() const {
|
|
409
|
+
return speculative_enabled && draft_model && draft_ctx;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
}
|
package/cpp/cap-llama.h
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
#ifndef CAPLLAMA_H
|
|
2
|
+
#define CAPLLAMA_H
|
|
3
|
+
|
|
4
|
+
#include <sstream>
|
|
5
|
+
#include <iostream>
|
|
6
|
+
#include <thread>
|
|
7
|
+
#include <codecvt>
|
|
8
|
+
#include "chat.h"
|
|
9
|
+
#include "common.h"
|
|
10
|
+
#include "ggml.h"
|
|
11
|
+
#include "gguf.h"
|
|
12
|
+
#include "llama.h"
|
|
13
|
+
#include "llama-model.h"
|
|
14
|
+
#include "llama-impl.h"
|
|
15
|
+
#include "sampling.h"
|
|
16
|
+
#include "nlohmann/json.hpp"
|
|
17
|
+
#include "cap-tts.h"
|
|
18
|
+
#if defined(__ANDROID__)
|
|
19
|
+
#include <android/log.h>
|
|
20
|
+
#endif
|
|
21
|
+
|
|
22
|
+
using json = nlohmann::ordered_json;
|
|
23
|
+
|
|
24
|
+
namespace capllama {
|
|
25
|
+
|
|
26
|
+
std::string tokens_to_output_formatted_string(const llama_context *ctx, const llama_token token);
|
|
27
|
+
|
|
28
|
+
std::string tokens_to_str(llama_context *ctx, const std::vector<llama_token>::const_iterator begin, const std::vector<llama_token>::const_iterator end);
|
|
29
|
+
|
|
30
|
+
lm_ggml_type kv_cache_type_from_str(const std::string & s);
|
|
31
|
+
|
|
32
|
+
// Forward declarations - actual definitions are in cap-completion.h
|
|
33
|
+
// Note: enum forward declarations not allowed in C++, using include in implementation file
|
|
34
|
+
struct completion_token_output;
|
|
35
|
+
struct completion_partial_output;
|
|
36
|
+
struct llama_cap_context_mtmd;
|
|
37
|
+
|
|
38
|
+
struct llama_cap_context_tts;
|
|
39
|
+
|
|
40
|
+
struct llama_cap_context_completion;
|
|
41
|
+
|
|
42
|
+
struct llama_cap_tokenize_result {
|
|
43
|
+
std::vector<llama_token> tokens;
|
|
44
|
+
bool has_media = false;
|
|
45
|
+
std::vector<std::string> bitmap_hashes;
|
|
46
|
+
std::vector<size_t> chunk_pos; // both text and media
|
|
47
|
+
std::vector<size_t> chunk_pos_media; // media only
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Main context class
|
|
51
|
+
struct llama_cap_context {
|
|
52
|
+
// Model state fields
|
|
53
|
+
llama_model *model = nullptr;
|
|
54
|
+
float loading_progress = 0;
|
|
55
|
+
bool is_load_interrupted = false;
|
|
56
|
+
common_params params;
|
|
57
|
+
common_init_result llama_init;
|
|
58
|
+
llama_context *ctx = nullptr;
|
|
59
|
+
common_chat_templates_ptr templates;
|
|
60
|
+
int n_ctx;
|
|
61
|
+
|
|
62
|
+
// Speculative decoding fields
|
|
63
|
+
llama_model *draft_model = nullptr;
|
|
64
|
+
llama_context *draft_ctx = nullptr;
|
|
65
|
+
bool speculative_enabled = false;
|
|
66
|
+
int speculative_samples = 3; // Mobile-optimized default
|
|
67
|
+
bool mobile_speculative = true;
|
|
68
|
+
|
|
69
|
+
// Completion context
|
|
70
|
+
llama_cap_context_completion *completion = nullptr;
|
|
71
|
+
|
|
72
|
+
~llama_cap_context();
|
|
73
|
+
|
|
74
|
+
bool loadModel(common_params ¶ms_);
|
|
75
|
+
|
|
76
|
+
// Speculative decoding methods
|
|
77
|
+
bool loadDraftModel(const std::string &draft_model_path);
|
|
78
|
+
void releaseDraftModel();
|
|
79
|
+
bool isSpectulativeEnabled() const;
|
|
80
|
+
|
|
81
|
+
// Model methods
|
|
82
|
+
bool validateModelChatTemplate(bool use_jinja, const char *name) const;
|
|
83
|
+
common_chat_params getFormattedChatWithJinja(
|
|
84
|
+
const std::string& messages,
|
|
85
|
+
const std::string& chat_template,
|
|
86
|
+
const std::string& json_schema,
|
|
87
|
+
const std::string& tools,
|
|
88
|
+
const bool& parallel_tool_calls,
|
|
89
|
+
const std::string& tool_choice,
|
|
90
|
+
const bool& enable_thinking,
|
|
91
|
+
const bool& add_generation_prompt = true,
|
|
92
|
+
const std::string& now_str = "",
|
|
93
|
+
const std::map<std::string, std::string>& chat_template_kwargs = {}
|
|
94
|
+
) const;
|
|
95
|
+
std::string getFormattedChat(
|
|
96
|
+
const std::string &messages,
|
|
97
|
+
const std::string &chat_template
|
|
98
|
+
) const;
|
|
99
|
+
llama_cap_tokenize_result tokenize(const std::string &text, const std::vector<std::string> &media_paths);
|
|
100
|
+
|
|
101
|
+
// Lora methods
|
|
102
|
+
std::vector<common_adapter_lora_info> lora;
|
|
103
|
+
int applyLoraAdapters(std::vector<common_adapter_lora_info> lora);
|
|
104
|
+
void removeLoraAdapters();
|
|
105
|
+
std::vector<common_adapter_lora_info> getLoadedLoraAdapters();
|
|
106
|
+
|
|
107
|
+
// Multimodal fields and methods
|
|
108
|
+
llama_cap_context_mtmd *mtmd_wrapper = nullptr;
|
|
109
|
+
bool has_multimodal = false;
|
|
110
|
+
bool initMultimodal(const std::string &mmproj_path, bool use_gpu);
|
|
111
|
+
bool isMultimodalEnabled() const;
|
|
112
|
+
bool isMultimodalSupportVision() const;
|
|
113
|
+
bool isMultimodalSupportAudio() const;
|
|
114
|
+
void releaseMultimodal();
|
|
115
|
+
|
|
116
|
+
// TTS fields and methods (delegated to TTS context)
|
|
117
|
+
llama_cap_context_tts *tts_wrapper = nullptr;
|
|
118
|
+
bool has_vocoder = false;
|
|
119
|
+
bool initVocoder(const std::string &vocoder_model_path, int batch_size = -1);
|
|
120
|
+
bool isVocoderEnabled() const;
|
|
121
|
+
void releaseVocoder();
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// Utility functions
|
|
125
|
+
inline void llama_batch_add(llama_batch *batch, llama_token id, llama_pos pos, std::vector<llama_seq_id> seq_ids, bool logits) {
|
|
126
|
+
batch->token [batch->n_tokens] = id;
|
|
127
|
+
batch->pos [batch->n_tokens] = pos;
|
|
128
|
+
batch->n_seq_id[batch->n_tokens] = seq_ids.size();
|
|
129
|
+
for (size_t i = 0; i < seq_ids.size(); i++) {
|
|
130
|
+
batch->seq_id[batch->n_tokens][i] = seq_ids[i];
|
|
131
|
+
}
|
|
132
|
+
batch->logits [batch->n_tokens] = logits ? 1 : 0;
|
|
133
|
+
batch->n_tokens += 1;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Logging functions
|
|
137
|
+
void log(const char *level, const char *function, int line, const char *format, ...);
|
|
138
|
+
|
|
139
|
+
// Logging macros
|
|
140
|
+
extern bool capllama_verbose;
|
|
141
|
+
|
|
142
|
+
#if CAPLLAMA_VERBOSE != 1
|
|
143
|
+
#define LOG_VERBOSE(MSG, ...)
|
|
144
|
+
#else
|
|
145
|
+
#define LOG_VERBOSE(MSG, ...) \
|
|
146
|
+
do \
|
|
147
|
+
{ \
|
|
148
|
+
if (capllama_verbose) \
|
|
149
|
+
{ \
|
|
150
|
+
log("VERBOSE", __func__, __LINE__, MSG, ##__VA_ARGS__); \
|
|
151
|
+
} \
|
|
152
|
+
} while (0)
|
|
153
|
+
#endif
|
|
154
|
+
|
|
155
|
+
#define LOG_ERROR(MSG, ...) log("ERROR", __func__, __LINE__, MSG, ##__VA_ARGS__)
|
|
156
|
+
#define LOG_WARNING(MSG, ...) log("WARNING", __func__, __LINE__, MSG, ##__VA_ARGS__)
|
|
157
|
+
#define LOG_INFO(MSG, ...) log("INFO", __func__, __LINE__, MSG, ##__VA_ARGS__)
|
|
158
|
+
|
|
159
|
+
} // namespace capllama
|
|
160
|
+
|
|
161
|
+
#endif /* CAPLLAMA_H */
|