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,127 @@
|
|
|
1
|
+
#ifndef CAP_COMPLETION_H
|
|
2
|
+
#define CAP_COMPLETION_H
|
|
3
|
+
|
|
4
|
+
#include "common.h"
|
|
5
|
+
#include "llama.h"
|
|
6
|
+
#include "sampling.h"
|
|
7
|
+
#include "nlohmann/json.hpp"
|
|
8
|
+
#include "chat.h"
|
|
9
|
+
|
|
10
|
+
using json = nlohmann::ordered_json;
|
|
11
|
+
|
|
12
|
+
namespace capllama {
|
|
13
|
+
|
|
14
|
+
// Utility functions
|
|
15
|
+
static inline void llama_batch_clear(llama_batch *batch) {
|
|
16
|
+
batch->n_tokens = 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Forward declarations
|
|
20
|
+
struct llama_cap_context;
|
|
21
|
+
|
|
22
|
+
// Types defined in cap-llama.h (needed here for compilation)
|
|
23
|
+
enum stop_type
|
|
24
|
+
{
|
|
25
|
+
STOP_FULL,
|
|
26
|
+
STOP_PARTIAL,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
struct completion_token_output
|
|
30
|
+
{
|
|
31
|
+
struct token_prob
|
|
32
|
+
{
|
|
33
|
+
llama_token tok;
|
|
34
|
+
float prob;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
std::vector<token_prob> probs;
|
|
38
|
+
llama_token tok;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
struct completion_partial_output
|
|
42
|
+
{
|
|
43
|
+
std::string content;
|
|
44
|
+
std::string reasoning_content;
|
|
45
|
+
std::vector<common_chat_tool_call> tool_calls;
|
|
46
|
+
std::string accumulated_text;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Completion context class
|
|
50
|
+
struct llama_cap_context_completion {
|
|
51
|
+
// Reference to parent context
|
|
52
|
+
llama_cap_context* parent_ctx;
|
|
53
|
+
|
|
54
|
+
// Completion state fields
|
|
55
|
+
bool is_predicting = false;
|
|
56
|
+
bool is_interrupted = false;
|
|
57
|
+
bool has_next_token = false;
|
|
58
|
+
std::string prefill_text;
|
|
59
|
+
std::string generated_text;
|
|
60
|
+
std::vector<completion_token_output> generated_token_probs;
|
|
61
|
+
size_t num_prompt_tokens = 0;
|
|
62
|
+
size_t num_tokens_predicted = 0;
|
|
63
|
+
llama_pos n_past = 0;
|
|
64
|
+
size_t n_remain = 0;
|
|
65
|
+
std::vector<llama_token> embd;
|
|
66
|
+
bool incomplete = false;
|
|
67
|
+
bool context_full = false;
|
|
68
|
+
bool truncated = false;
|
|
69
|
+
bool stopped_eos = false;
|
|
70
|
+
bool stopped_word = false;
|
|
71
|
+
bool stopped_limit = false;
|
|
72
|
+
std::string stopping_word;
|
|
73
|
+
// Current completion parameters for chat parsing
|
|
74
|
+
int current_chat_format = COMMON_CHAT_FORMAT_CONTENT_ONLY;
|
|
75
|
+
common_reasoning_format current_reasoning_format = COMMON_REASONING_FORMAT_NONE;
|
|
76
|
+
bool current_thinking_forced_open = false;
|
|
77
|
+
|
|
78
|
+
// Sampling context
|
|
79
|
+
common_sampler *ctx_sampling = nullptr;
|
|
80
|
+
|
|
81
|
+
// Speculative decoding state
|
|
82
|
+
std::vector<llama_token> draft_tokens;
|
|
83
|
+
int n_drafted = 0;
|
|
84
|
+
int n_accepted = 0;
|
|
85
|
+
bool use_speculative = false;
|
|
86
|
+
|
|
87
|
+
// Constructor
|
|
88
|
+
llama_cap_context_completion(llama_cap_context* parent);
|
|
89
|
+
|
|
90
|
+
// Destructor
|
|
91
|
+
~llama_cap_context_completion();
|
|
92
|
+
|
|
93
|
+
// Completion processing methods
|
|
94
|
+
void rewind();
|
|
95
|
+
bool initSampling();
|
|
96
|
+
void truncatePrompt(std::vector<llama_token> &prompt_tokens);
|
|
97
|
+
void loadPrompt(const std::vector<std::string> &media_paths);
|
|
98
|
+
void beginCompletion();
|
|
99
|
+
void beginCompletion(int chat_format, common_reasoning_format reasoning_format, bool thinking_forced_open);
|
|
100
|
+
void endCompletion();
|
|
101
|
+
completion_token_output nextToken();
|
|
102
|
+
completion_token_output nextTokenSpeculative(); // NEW: Speculative version
|
|
103
|
+
size_t findStoppingStrings(const std::string &text, const size_t last_token_size, const stop_type type);
|
|
104
|
+
completion_token_output doCompletion();
|
|
105
|
+
completion_partial_output getPartialOutput(const std::string &token_text);
|
|
106
|
+
|
|
107
|
+
// Speculative decoding methods
|
|
108
|
+
std::vector<llama_token> draftTokens(int n_draft);
|
|
109
|
+
int verifyAndAcceptTokens(const std::vector<llama_token> &draft_tokens);
|
|
110
|
+
|
|
111
|
+
// Embedding methods
|
|
112
|
+
std::vector<float> getEmbedding(common_params &embd_params);
|
|
113
|
+
std::vector<float> rerank(const std::string &query, const std::vector<std::string> &documents);
|
|
114
|
+
|
|
115
|
+
// Benchmarking methods
|
|
116
|
+
std::string bench(int pp, int tg, int pl, int nr);
|
|
117
|
+
|
|
118
|
+
// Multimodal processing methods
|
|
119
|
+
void processMedia(
|
|
120
|
+
const std::string &prompt,
|
|
121
|
+
const std::vector<std::string> &media_paths
|
|
122
|
+
);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
} // namespace capllama
|
|
126
|
+
|
|
127
|
+
#endif /* CAP_COMPLETION_H */
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// C wrapper functions for iOS and Wasm embedding support
|
|
2
|
+
|
|
3
|
+
#include "cap-llama.h"
|
|
4
|
+
#include "llama.h"
|
|
5
|
+
#include "common.h"
|
|
6
|
+
#include "nlohmann/json.hpp"
|
|
7
|
+
#include <cstdio>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include <string>
|
|
10
|
+
#include <vector>
|
|
11
|
+
#include <map>
|
|
12
|
+
#include <mutex>
|
|
13
|
+
|
|
14
|
+
#ifdef __EMSCRIPTEN__
|
|
15
|
+
#define WASM_EMBED_LOG(fmt, ...) fprintf(stderr, "@@WASM_EMBED@@ " fmt "\n", ##__VA_ARGS__)
|
|
16
|
+
#else
|
|
17
|
+
#define WASM_EMBED_LOG(fmt, ...) ((void)0)
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
using json = nlohmann::ordered_json;
|
|
21
|
+
|
|
22
|
+
// Global context storage for iOS
|
|
23
|
+
static std::map<int64_t, capllama::llama_cap_context*> g_contexts;
|
|
24
|
+
static std::mutex g_contexts_mutex;
|
|
25
|
+
|
|
26
|
+
extern "C" void llama_embedding_register_context(int64_t contextId, void* contextPtr) {
|
|
27
|
+
if (contextPtr == nullptr) return;
|
|
28
|
+
std::lock_guard<std::mutex> lock(g_contexts_mutex);
|
|
29
|
+
g_contexts[contextId] = static_cast<capllama::llama_cap_context*>(contextPtr);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
extern "C" void llama_embedding_unregister_context(int64_t contextId) {
|
|
33
|
+
std::lock_guard<std::mutex> lock(g_contexts_mutex);
|
|
34
|
+
g_contexts.erase(contextId);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static capllama::llama_cap_context * resolve_context(int64_t contextId) {
|
|
38
|
+
std::lock_guard<std::mutex> lock(g_contexts_mutex);
|
|
39
|
+
auto it = g_contexts.find(contextId);
|
|
40
|
+
if (it == g_contexts.end() || it->second == nullptr) {
|
|
41
|
+
return nullptr;
|
|
42
|
+
}
|
|
43
|
+
return it->second;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static int32_t parse_embd_normalize(const char * paramsJson) {
|
|
47
|
+
int32_t embd_norm = 2; // euclidean / L2 — matches common_params default
|
|
48
|
+
if (paramsJson == nullptr || paramsJson[0] == '\0') {
|
|
49
|
+
return embd_norm;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
json p = json::parse(paramsJson);
|
|
53
|
+
if (p.contains("embd_normalize")) {
|
|
54
|
+
embd_norm = p.at("embd_normalize").get<int32_t>();
|
|
55
|
+
}
|
|
56
|
+
} catch (...) {
|
|
57
|
+
// keep default
|
|
58
|
+
}
|
|
59
|
+
return embd_norm;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Returns pointer to thread-local storage valid until the next call on this thread.
|
|
63
|
+
extern "C" float* llama_embedding(int64_t contextId, const char* text, const char* paramsJson) {
|
|
64
|
+
static thread_local std::vector<float> embedding_storage;
|
|
65
|
+
|
|
66
|
+
if (text == nullptr) {
|
|
67
|
+
WASM_EMBED_LOG("fail: null text ctx=%lld", (long long) contextId);
|
|
68
|
+
return nullptr;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
capllama::llama_cap_context * ctx_ptr = resolve_context(contextId);
|
|
72
|
+
if (ctx_ptr == nullptr || ctx_ptr->ctx == nullptr || ctx_ptr->model == nullptr) {
|
|
73
|
+
WASM_EMBED_LOG("fail: context not found ctx=%lld", (long long) contextId);
|
|
74
|
+
return nullptr;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const int32_t n_embd = llama_model_n_embd(ctx_ptr->model);
|
|
79
|
+
if (n_embd <= 0) {
|
|
80
|
+
WASM_EMBED_LOG("fail: n_embd=%d", n_embd);
|
|
81
|
+
return nullptr;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const int32_t embd_norm = parse_embd_normalize(paramsJson);
|
|
85
|
+
const enum llama_pooling_type pooling_type = llama_pooling_type(ctx_ptr->ctx);
|
|
86
|
+
const bool use_encode = llama_get_memory(ctx_ptr->ctx) == nullptr;
|
|
87
|
+
|
|
88
|
+
WASM_EMBED_LOG(
|
|
89
|
+
"begin ctx=%lld n_embd=%d pooling=%d encode=%d text_len=%zu",
|
|
90
|
+
(long long) contextId,
|
|
91
|
+
n_embd,
|
|
92
|
+
(int) pooling_type,
|
|
93
|
+
use_encode ? 1 : 0,
|
|
94
|
+
std::strlen(text));
|
|
95
|
+
|
|
96
|
+
std::string text_str(text);
|
|
97
|
+
capllama::llama_cap_tokenize_result tokenize_result = ctx_ptr->tokenize(text_str, {});
|
|
98
|
+
std::vector<llama_token> tokens = tokenize_result.tokens;
|
|
99
|
+
if (tokens.empty()) {
|
|
100
|
+
WASM_EMBED_LOG("fail: empty tokenization");
|
|
101
|
+
return nullptr;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const int32_t max_tokens = (int32_t) llama_n_ubatch(ctx_ptr->ctx);
|
|
105
|
+
if (max_tokens > 0 && (int32_t) tokens.size() > max_tokens) {
|
|
106
|
+
WASM_EMBED_LOG("truncating tokens %zu -> %d (n_ubatch)", tokens.size(), max_tokens);
|
|
107
|
+
tokens.resize((size_t) max_tokens);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
llama_set_embeddings(ctx_ptr->ctx, true);
|
|
111
|
+
|
|
112
|
+
llama_batch batch = llama_batch_get_one(tokens.data(), (int32_t) tokens.size());
|
|
113
|
+
const int rc = use_encode
|
|
114
|
+
? llama_encode(ctx_ptr->ctx, batch)
|
|
115
|
+
: llama_decode(ctx_ptr->ctx, batch);
|
|
116
|
+
|
|
117
|
+
if (rc != 0) {
|
|
118
|
+
WASM_EMBED_LOG("fail: %s rc=%d n_tokens=%zu",
|
|
119
|
+
use_encode ? "llama_encode" : "llama_decode",
|
|
120
|
+
rc,
|
|
121
|
+
tokens.size());
|
|
122
|
+
llama_set_embeddings(ctx_ptr->ctx, false);
|
|
123
|
+
return nullptr;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
llama_synchronize(ctx_ptr->ctx);
|
|
127
|
+
|
|
128
|
+
float * data = nullptr;
|
|
129
|
+
if (pooling_type == LLAMA_POOLING_TYPE_NONE) {
|
|
130
|
+
data = llama_get_embeddings(ctx_ptr->ctx);
|
|
131
|
+
} else {
|
|
132
|
+
data = llama_get_embeddings_seq(ctx_ptr->ctx, 0);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (data == nullptr) {
|
|
136
|
+
WASM_EMBED_LOG("fail: null embedding ptr pooling=%d", (int) pooling_type);
|
|
137
|
+
llama_set_embeddings(ctx_ptr->ctx, false);
|
|
138
|
+
return nullptr;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
embedding_storage.assign(data, data + n_embd);
|
|
142
|
+
std::vector<float> normalized(n_embd);
|
|
143
|
+
common_embd_normalize(embedding_storage.data(), normalized.data(), n_embd, embd_norm);
|
|
144
|
+
embedding_storage = std::move(normalized);
|
|
145
|
+
|
|
146
|
+
llama_set_embeddings(ctx_ptr->ctx, false);
|
|
147
|
+
|
|
148
|
+
WASM_EMBED_LOG("ok ctx=%lld dim=%d norm=%d", (long long) contextId, n_embd, embd_norm);
|
|
149
|
+
return embedding_storage.data();
|
|
150
|
+
} catch (const std::exception & e) {
|
|
151
|
+
WASM_EMBED_LOG("exception: %s", e.what());
|
|
152
|
+
if (ctx_ptr && ctx_ptr->ctx) {
|
|
153
|
+
llama_set_embeddings(ctx_ptr->ctx, false);
|
|
154
|
+
}
|
|
155
|
+
return nullptr;
|
|
156
|
+
} catch (...) {
|
|
157
|
+
WASM_EMBED_LOG("exception: unknown");
|
|
158
|
+
if (ctx_ptr && ctx_ptr->ctx) {
|
|
159
|
+
llama_set_embeddings(ctx_ptr->ctx, false);
|
|
160
|
+
}
|
|
161
|
+
return nullptr;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
extern "C" const char* llama_embedding_json(int64_t contextId, const char* text, const char* paramsJson) {
|
|
166
|
+
static thread_local std::string json_buf;
|
|
167
|
+
|
|
168
|
+
int32_t n_embd = 0;
|
|
169
|
+
{
|
|
170
|
+
capllama::llama_cap_context * ctx_ptr = resolve_context(contextId);
|
|
171
|
+
if (ctx_ptr != nullptr && ctx_ptr->model != nullptr) {
|
|
172
|
+
n_embd = llama_model_n_embd(ctx_ptr->model);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
float* floats = llama_embedding(contextId, text, paramsJson);
|
|
177
|
+
if (!floats || n_embd <= 0) {
|
|
178
|
+
json_buf = "{\"embedding\":[],\"error\":\"embed_failed\"}";
|
|
179
|
+
return json_buf.c_str();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
json_buf.clear();
|
|
183
|
+
json_buf.reserve(16 + (size_t) n_embd * 14);
|
|
184
|
+
json_buf += "{\"embedding\":[";
|
|
185
|
+
for (int i = 0; i < n_embd; ++i) {
|
|
186
|
+
if (i > 0) json_buf += ',';
|
|
187
|
+
char tmp[32];
|
|
188
|
+
std::snprintf(tmp, sizeof(tmp), "%.8g", static_cast<double>(floats[i]));
|
|
189
|
+
json_buf += tmp;
|
|
190
|
+
}
|
|
191
|
+
json_buf += "]}";
|
|
192
|
+
return json_buf.c_str();
|
|
193
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#ifndef CAP_EMBEDDING_H
|
|
2
|
+
#define CAP_EMBEDDING_H
|
|
3
|
+
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
// Register a context for embedding operations (called when context is created)
|
|
11
|
+
// contextId: The context ID
|
|
12
|
+
// contextPtr: Pointer to the llama_cap_context
|
|
13
|
+
void llama_embedding_register_context(int64_t contextId, void* contextPtr);
|
|
14
|
+
|
|
15
|
+
// Unregister a context (called when context is released)
|
|
16
|
+
void llama_embedding_unregister_context(int64_t contextId);
|
|
17
|
+
|
|
18
|
+
// Generate embeddings for text
|
|
19
|
+
// contextId: The context ID
|
|
20
|
+
// text: Input text to embed
|
|
21
|
+
// paramsJson: JSON string with parameters (e.g., {"embd_normalize": 1.0})
|
|
22
|
+
// Returns: Pointer to float array of size n_embd, or NULL on error
|
|
23
|
+
// Note: The returned pointer is valid until the next call to this function on the same thread
|
|
24
|
+
float* llama_embedding(int64_t contextId, const char* text, const char* paramsJson);
|
|
25
|
+
|
|
26
|
+
// JSON wrapper for embedding — used by Rust FFI (ffi.rs).
|
|
27
|
+
// Returns: C string with {"embedding": [f32, ...]} or {"embedding": []} on error.
|
|
28
|
+
// Note: Returned pointer is valid until the next call on the same thread.
|
|
29
|
+
const char* llama_embedding_json(int64_t contextId, const char* text, const char* paramsJson);
|
|
30
|
+
|
|
31
|
+
#ifdef __cplusplus
|
|
32
|
+
}
|
|
33
|
+
#endif
|
|
34
|
+
|
|
35
|
+
#endif // CAP_EMBEDDING_H
|