cui-llama.rn 1.4.0 → 1.4.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/README.md +4 -23
- package/android/build.gradle +12 -3
- package/android/src/main/CMakeLists.txt +13 -7
- package/android/src/main/java/com/rnllama/LlamaContext.java +27 -20
- package/android/src/main/java/com/rnllama/RNLlama.java +5 -1
- package/android/src/main/jni.cpp +15 -12
- package/android/src/main/jniLibs/arm64-v8a/librnllama.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_dotprod_i8mm.so +0 -0
- package/android/src/main/jniLibs/arm64-v8a/librnllama_v8_2_i8mm.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama.so +0 -0
- package/android/src/main/jniLibs/x86_64/librnllama_x86_64.so +0 -0
- package/cpp/README.md +1 -1
- package/cpp/common.cpp +158 -267
- package/cpp/common.h +46 -12
- package/cpp/ggml-alloc.c +1042 -1037
- package/cpp/ggml-backend-impl.h +255 -256
- package/cpp/ggml-backend-reg.cpp +582 -582
- package/cpp/ggml-backend.cpp +2002 -2002
- package/cpp/ggml-backend.h +354 -352
- package/cpp/ggml-common.h +1853 -1853
- package/cpp/ggml-cpp.h +39 -39
- package/cpp/ggml-cpu-aarch64.cpp +4247 -4247
- package/cpp/ggml-cpu-aarch64.h +8 -8
- package/cpp/ggml-cpu-impl.h +386 -386
- package/cpp/ggml-cpu-quants.c +10920 -10839
- package/cpp/ggml-cpu-traits.cpp +36 -36
- package/cpp/ggml-cpu-traits.h +38 -38
- package/cpp/ggml-cpu.c +329 -60
- package/cpp/ggml-cpu.cpp +10 -2
- package/cpp/ggml-cpu.h +135 -135
- package/cpp/ggml-impl.h +567 -567
- package/cpp/ggml-metal-impl.h +17 -17
- package/cpp/ggml-metal.m +4884 -4884
- package/cpp/ggml-quants.c +5238 -5238
- package/cpp/ggml-threading.h +14 -14
- package/cpp/ggml.c +6514 -6448
- package/cpp/ggml.h +2194 -2163
- package/cpp/gguf.cpp +1329 -1325
- package/cpp/gguf.h +202 -202
- package/cpp/json-schema-to-grammar.cpp +1045 -1045
- package/cpp/json-schema-to-grammar.h +8 -8
- package/cpp/json.hpp +24766 -24766
- package/cpp/llama-adapter.cpp +347 -346
- package/cpp/llama-adapter.h +74 -73
- package/cpp/llama-arch.cpp +1487 -1434
- package/cpp/llama-arch.h +400 -395
- package/cpp/llama-batch.cpp +368 -368
- package/cpp/llama-batch.h +88 -88
- package/cpp/llama-chat.cpp +578 -567
- package/cpp/llama-chat.h +52 -51
- package/cpp/llama-context.cpp +1775 -1771
- package/cpp/llama-context.h +128 -128
- package/cpp/llama-cparams.cpp +1 -1
- package/cpp/llama-cparams.h +37 -37
- package/cpp/llama-cpp.h +30 -30
- package/cpp/llama-grammar.cpp +1139 -1139
- package/cpp/llama-grammar.h +143 -143
- package/cpp/llama-hparams.cpp +71 -71
- package/cpp/llama-hparams.h +139 -140
- package/cpp/llama-impl.cpp +167 -167
- package/cpp/llama-impl.h +61 -61
- package/cpp/llama-kv-cache.cpp +718 -718
- package/cpp/llama-kv-cache.h +218 -218
- package/cpp/llama-mmap.cpp +2 -1
- package/cpp/llama-mmap.h +67 -67
- package/cpp/llama-model-loader.cpp +1124 -1011
- package/cpp/llama-model-loader.h +167 -158
- package/cpp/llama-model.cpp +3997 -2202
- package/cpp/llama-model.h +370 -391
- package/cpp/llama-sampling.cpp +2408 -2406
- package/cpp/llama-sampling.h +32 -48
- package/cpp/llama-vocab.cpp +3247 -1982
- package/cpp/llama-vocab.h +125 -182
- package/cpp/llama.cpp +416 -2886
- package/cpp/llama.h +1323 -1285
- package/cpp/log.cpp +401 -401
- package/cpp/log.h +121 -121
- package/cpp/rn-llama.cpp +822 -0
- package/cpp/rn-llama.h +123 -0
- package/cpp/rn-llama.hpp +18 -12
- package/cpp/sampling.cpp +505 -500
- package/cpp/sgemm.cpp +2597 -2597
- package/cpp/speculative.cpp +277 -274
- package/cpp/speculative.h +28 -28
- package/cpp/unicode.cpp +2 -3
- package/ios/CMakeLists.txt +99 -0
- package/ios/RNLlama.h +5 -1
- package/ios/RNLlama.mm +2 -2
- package/ios/RNLlamaContext.h +8 -1
- package/ios/RNLlamaContext.mm +15 -11
- package/ios/rnllama.xcframework/Info.plist +74 -0
- package/jest/mock.js +3 -2
- package/lib/commonjs/NativeRNLlama.js.map +1 -1
- package/lib/commonjs/index.js +4 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/NativeRNLlama.js.map +1 -1
- package/lib/module/index.js +4 -2
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/NativeRNLlama.d.ts +5 -1
- package/lib/typescript/NativeRNLlama.d.ts.map +1 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/llama-rn.podspec +8 -2
- package/package.json +5 -2
- package/src/NativeRNLlama.ts +5 -1
- package/src/index.ts +9 -2
package/cpp/llama-hparams.h
CHANGED
@@ -1,140 +1,139 @@
|
|
1
|
-
#pragma once
|
2
|
-
|
3
|
-
#include "llama.h"
|
4
|
-
|
5
|
-
#include <array>
|
6
|
-
|
7
|
-
// bump if necessary
|
8
|
-
#define LLAMA_MAX_LAYERS 512
|
9
|
-
#define LLAMA_MAX_EXPERTS 256 // DeepSeekV3
|
10
|
-
|
11
|
-
enum llama_expert_gating_func_type {
|
12
|
-
LLAMA_EXPERT_GATING_FUNC_TYPE_NONE = 0,
|
13
|
-
LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX = 1,
|
14
|
-
LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID = 2,
|
15
|
-
};
|
16
|
-
|
17
|
-
struct llama_hparams_posnet {
|
18
|
-
uint32_t n_embd;
|
19
|
-
uint32_t n_layer;
|
20
|
-
};
|
21
|
-
|
22
|
-
struct llama_hparams_convnext {
|
23
|
-
uint32_t n_embd;
|
24
|
-
uint32_t n_layer;
|
25
|
-
};
|
26
|
-
|
27
|
-
struct llama_hparams {
|
28
|
-
bool vocab_only;
|
29
|
-
bool rope_finetuned;
|
30
|
-
bool use_par_res;
|
31
|
-
bool swin_norm;
|
32
|
-
|
33
|
-
uint32_t
|
34
|
-
uint32_t
|
35
|
-
uint32_t
|
36
|
-
uint32_t
|
37
|
-
uint32_t
|
38
|
-
uint32_t
|
39
|
-
uint32_t
|
40
|
-
uint32_t
|
41
|
-
uint32_t
|
42
|
-
uint32_t
|
43
|
-
uint32_t
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
std::array<uint32_t, LLAMA_MAX_LAYERS>
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
uint32_t
|
56
|
-
uint32_t
|
57
|
-
uint32_t
|
58
|
-
uint32_t
|
59
|
-
uint32_t
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
float
|
68
|
-
|
69
|
-
float
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
uint32_t
|
76
|
-
uint32_t
|
77
|
-
uint32_t
|
78
|
-
|
79
|
-
|
80
|
-
float
|
81
|
-
float
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
uint32_t
|
90
|
-
uint32_t
|
91
|
-
uint32_t
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
float
|
97
|
-
float
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
float
|
102
|
-
float
|
103
|
-
|
104
|
-
|
105
|
-
bool
|
106
|
-
bool
|
107
|
-
|
108
|
-
|
109
|
-
//
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
enum
|
114
|
-
enum
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
//
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include "llama.h"
|
4
|
+
|
5
|
+
#include <array>
|
6
|
+
|
7
|
+
// bump if necessary
|
8
|
+
#define LLAMA_MAX_LAYERS 512
|
9
|
+
#define LLAMA_MAX_EXPERTS 256 // DeepSeekV3
|
10
|
+
|
11
|
+
enum llama_expert_gating_func_type {
|
12
|
+
LLAMA_EXPERT_GATING_FUNC_TYPE_NONE = 0,
|
13
|
+
LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX = 1,
|
14
|
+
LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID = 2,
|
15
|
+
};
|
16
|
+
|
17
|
+
struct llama_hparams_posnet {
|
18
|
+
uint32_t n_embd;
|
19
|
+
uint32_t n_layer;
|
20
|
+
};
|
21
|
+
|
22
|
+
struct llama_hparams_convnext {
|
23
|
+
uint32_t n_embd;
|
24
|
+
uint32_t n_layer;
|
25
|
+
};
|
26
|
+
|
27
|
+
struct llama_hparams {
|
28
|
+
bool vocab_only;
|
29
|
+
bool rope_finetuned;
|
30
|
+
bool use_par_res;
|
31
|
+
bool swin_norm;
|
32
|
+
|
33
|
+
uint32_t n_ctx_train; // context size the model was trained on
|
34
|
+
uint32_t n_embd;
|
35
|
+
uint32_t n_embd_features = 0;
|
36
|
+
uint32_t n_layer;
|
37
|
+
uint32_t n_rot;
|
38
|
+
uint32_t n_swa = 0; // sliding window attention (SWA)
|
39
|
+
uint32_t n_embd_head_k; // dimension of keys (d_k). d_q is assumed to be the same, but there are n_head q heads, and only n_head_kv k-v heads
|
40
|
+
uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head
|
41
|
+
uint32_t n_expert = 0;
|
42
|
+
uint32_t n_expert_used = 0;
|
43
|
+
uint32_t n_rel_attn_bkts = 0;
|
44
|
+
|
45
|
+
// for WavTokenizer
|
46
|
+
struct llama_hparams_posnet posnet;
|
47
|
+
struct llama_hparams_convnext convnext;
|
48
|
+
|
49
|
+
std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr;
|
50
|
+
std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
|
51
|
+
std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
|
52
|
+
|
53
|
+
uint32_t n_layer_dense_lead = 0;
|
54
|
+
uint32_t n_lora_q = 0;
|
55
|
+
uint32_t n_lora_kv = 0;
|
56
|
+
uint32_t n_ff_exp = 0;
|
57
|
+
uint32_t n_ff_shexp = 0;
|
58
|
+
uint32_t n_expert_shared = 0;
|
59
|
+
uint32_t n_norm_groups = 0;
|
60
|
+
|
61
|
+
float expert_weights_scale = 0.0;
|
62
|
+
bool expert_weights_norm = false;
|
63
|
+
uint32_t expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE;
|
64
|
+
|
65
|
+
float f_norm_eps;
|
66
|
+
float f_norm_rms_eps;
|
67
|
+
float f_norm_group_eps;
|
68
|
+
|
69
|
+
float f_attn_logit_softcapping = 50.0f;
|
70
|
+
float f_final_logit_softcapping = 30.0f;
|
71
|
+
|
72
|
+
// for RWKV
|
73
|
+
uint32_t rescale_every_n_layers = 0;
|
74
|
+
uint32_t time_mix_extra_dim = 0;
|
75
|
+
uint32_t time_decay_extra_dim = 0;
|
76
|
+
uint32_t wkv_head_size = 0;
|
77
|
+
uint32_t token_shift_count = 2;
|
78
|
+
|
79
|
+
float rope_attn_factor = 1.0f;
|
80
|
+
float rope_freq_base_train;
|
81
|
+
float rope_freq_scale_train;
|
82
|
+
uint32_t n_ctx_orig_yarn;
|
83
|
+
float rope_yarn_log_mul;
|
84
|
+
|
85
|
+
std::array<int, 4> rope_sections;
|
86
|
+
|
87
|
+
// for State Space Models
|
88
|
+
uint32_t ssm_d_conv = 0;
|
89
|
+
uint32_t ssm_d_inner = 0;
|
90
|
+
uint32_t ssm_d_state = 0;
|
91
|
+
uint32_t ssm_dt_rank = 0;
|
92
|
+
|
93
|
+
bool ssm_dt_b_c_rms = false;
|
94
|
+
|
95
|
+
float f_clamp_kqv = 0.0f;
|
96
|
+
float f_max_alibi_bias = 0.0f;
|
97
|
+
float f_logit_scale = 0.0f;
|
98
|
+
|
99
|
+
// Additional scale factors (Granite/Granite MoE)
|
100
|
+
float f_residual_scale = 0.0f;
|
101
|
+
float f_embedding_scale = 0.0f;
|
102
|
+
float f_attention_scale = 0.0f;
|
103
|
+
|
104
|
+
bool causal_attn = true;
|
105
|
+
bool use_alibi = false;
|
106
|
+
bool attn_soft_cap = false;
|
107
|
+
|
108
|
+
// needed by encoder-decoder models (e.g. T5, FLAN-T5)
|
109
|
+
// ref: https://github.com/ggerganov/llama.cpp/pull/8141
|
110
|
+
llama_token dec_start_token_id = LLAMA_TOKEN_NULL;
|
111
|
+
|
112
|
+
enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE;
|
113
|
+
enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE;
|
114
|
+
enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE;
|
115
|
+
|
116
|
+
uint32_t n_head(uint32_t il = 0) const;
|
117
|
+
|
118
|
+
uint32_t n_head_kv(uint32_t il = 0) const;
|
119
|
+
|
120
|
+
uint32_t n_ff(uint32_t il = 0) const;
|
121
|
+
|
122
|
+
uint32_t n_gqa(uint32_t il = 0) const;
|
123
|
+
|
124
|
+
// dimension of key embeddings across all k-v heads
|
125
|
+
uint32_t n_embd_k_gqa(uint32_t il = 0) const;
|
126
|
+
|
127
|
+
// dimension of value embeddings across all k-v heads
|
128
|
+
uint32_t n_embd_v_gqa(uint32_t il = 0) const;
|
129
|
+
|
130
|
+
// dimension of the rolling state embeddings
|
131
|
+
// corresponds to Mamba's conv_states size or RWKV's token_shift states size
|
132
|
+
uint32_t n_embd_k_s() const;
|
133
|
+
|
134
|
+
// dimension of the recurrent state embeddings
|
135
|
+
uint32_t n_embd_v_s() const;
|
136
|
+
};
|
137
|
+
|
138
|
+
static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
|
139
|
+
|
package/cpp/llama-impl.cpp
CHANGED
@@ -1,167 +1,167 @@
|
|
1
|
-
#include "llama-impl.h"
|
2
|
-
|
3
|
-
#include "gguf.h"
|
4
|
-
#include "llama.h"
|
5
|
-
|
6
|
-
#include <cinttypes>
|
7
|
-
#include <climits>
|
8
|
-
#include <cstdarg>
|
9
|
-
#include <cstring>
|
10
|
-
#include <vector>
|
11
|
-
#include <sstream>
|
12
|
-
|
13
|
-
struct llama_logger_state {
|
14
|
-
lm_ggml_log_callback log_callback = llama_log_callback_default;
|
15
|
-
void * log_callback_user_data = nullptr;
|
16
|
-
};
|
17
|
-
|
18
|
-
static llama_logger_state g_logger_state;
|
19
|
-
|
20
|
-
time_meas::time_meas(int64_t & t_acc, bool disable) : t_start_us(disable ? -1 : lm_ggml_time_us()), t_acc(t_acc) {}
|
21
|
-
|
22
|
-
time_meas::~time_meas() {
|
23
|
-
if (t_start_us >= 0) {
|
24
|
-
t_acc += lm_ggml_time_us() - t_start_us;
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
void llama_log_set(lm_ggml_log_callback log_callback, void * user_data) {
|
29
|
-
lm_ggml_log_set(log_callback, user_data);
|
30
|
-
g_logger_state.log_callback = log_callback ? log_callback : llama_log_callback_default;
|
31
|
-
g_logger_state.log_callback_user_data = user_data;
|
32
|
-
}
|
33
|
-
|
34
|
-
static void llama_log_internal_v(lm_ggml_log_level level, const char * format, va_list args) {
|
35
|
-
va_list args_copy;
|
36
|
-
va_copy(args_copy, args);
|
37
|
-
char buffer[128];
|
38
|
-
int len = vsnprintf(buffer, 128, format, args);
|
39
|
-
if (len < 128) {
|
40
|
-
g_logger_state.log_callback(level, buffer, g_logger_state.log_callback_user_data);
|
41
|
-
} else {
|
42
|
-
char * buffer2 = new char[len + 1];
|
43
|
-
vsnprintf(buffer2, len + 1, format, args_copy);
|
44
|
-
buffer2[len] = 0;
|
45
|
-
g_logger_state.log_callback(level, buffer2, g_logger_state.log_callback_user_data);
|
46
|
-
delete[] buffer2;
|
47
|
-
}
|
48
|
-
va_end(args_copy);
|
49
|
-
}
|
50
|
-
|
51
|
-
void llama_log_internal(lm_ggml_log_level level, const char * format, ...) {
|
52
|
-
va_list args;
|
53
|
-
va_start(args, format);
|
54
|
-
llama_log_internal_v(level, format, args);
|
55
|
-
va_end(args);
|
56
|
-
}
|
57
|
-
|
58
|
-
void llama_log_callback_default(lm_ggml_log_level level, const char * text, void * user_data) {
|
59
|
-
(void) level;
|
60
|
-
(void) user_data;
|
61
|
-
fputs(text, stderr);
|
62
|
-
fflush(stderr);
|
63
|
-
}
|
64
|
-
|
65
|
-
void replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
66
|
-
if (search.empty()) {
|
67
|
-
return;
|
68
|
-
}
|
69
|
-
std::string builder;
|
70
|
-
builder.reserve(s.length());
|
71
|
-
size_t pos = 0;
|
72
|
-
size_t last_pos = 0;
|
73
|
-
while ((pos = s.find(search, last_pos)) != std::string::npos) {
|
74
|
-
builder.append(s, last_pos, pos - last_pos);
|
75
|
-
builder.append(replace);
|
76
|
-
last_pos = pos + search.length();
|
77
|
-
}
|
78
|
-
builder.append(s, last_pos, std::string::npos);
|
79
|
-
s = std::move(builder);
|
80
|
-
}
|
81
|
-
|
82
|
-
std::string format(const char * fmt, ...) {
|
83
|
-
va_list ap;
|
84
|
-
va_list ap2;
|
85
|
-
va_start(ap, fmt);
|
86
|
-
va_copy(ap2, ap);
|
87
|
-
int size = vsnprintf(NULL, 0, fmt, ap);
|
88
|
-
LM_GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
|
89
|
-
std::vector<char> buf(size + 1);
|
90
|
-
int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
|
91
|
-
LM_GGML_ASSERT(size2 == size);
|
92
|
-
va_end(ap2);
|
93
|
-
va_end(ap);
|
94
|
-
return std::string(buf.data(), size);
|
95
|
-
}
|
96
|
-
|
97
|
-
std::string llama_format_tensor_shape(const std::vector<int64_t> & ne) {
|
98
|
-
char buf[256];
|
99
|
-
snprintf(buf, sizeof(buf), "%5" PRId64, ne.at(0));
|
100
|
-
for (size_t i = 1; i < ne.size(); i++) {
|
101
|
-
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ", %5" PRId64, ne.at(i));
|
102
|
-
}
|
103
|
-
return buf;
|
104
|
-
}
|
105
|
-
|
106
|
-
std::string llama_format_tensor_shape(const struct lm_ggml_tensor * t) {
|
107
|
-
char buf[256];
|
108
|
-
snprintf(buf, sizeof(buf), "%5" PRId64, t->ne[0]);
|
109
|
-
for (int i = 1; i < LM_GGML_MAX_DIMS; i++) {
|
110
|
-
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ", %5" PRId64, t->ne[i]);
|
111
|
-
}
|
112
|
-
return buf;
|
113
|
-
}
|
114
|
-
|
115
|
-
static std::string lm_gguf_data_to_str(enum lm_gguf_type type, const void * data, int i) {
|
116
|
-
switch (type) {
|
117
|
-
case LM_GGUF_TYPE_UINT8: return std::to_string(((const uint8_t *)data)[i]);
|
118
|
-
case LM_GGUF_TYPE_INT8: return std::to_string(((const int8_t *)data)[i]);
|
119
|
-
case LM_GGUF_TYPE_UINT16: return std::to_string(((const uint16_t *)data)[i]);
|
120
|
-
case LM_GGUF_TYPE_INT16: return std::to_string(((const int16_t *)data)[i]);
|
121
|
-
case LM_GGUF_TYPE_UINT32: return std::to_string(((const uint32_t *)data)[i]);
|
122
|
-
case LM_GGUF_TYPE_INT32: return std::to_string(((const int32_t *)data)[i]);
|
123
|
-
case LM_GGUF_TYPE_UINT64: return std::to_string(((const uint64_t *)data)[i]);
|
124
|
-
case LM_GGUF_TYPE_INT64: return std::to_string(((const int64_t *)data)[i]);
|
125
|
-
case LM_GGUF_TYPE_FLOAT32: return std::to_string(((const float *)data)[i]);
|
126
|
-
case LM_GGUF_TYPE_FLOAT64: return std::to_string(((const double *)data)[i]);
|
127
|
-
case LM_GGUF_TYPE_BOOL: return ((const bool *)data)[i] ? "true" : "false";
|
128
|
-
default: return format("unknown type %d", type);
|
129
|
-
}
|
130
|
-
}
|
131
|
-
|
132
|
-
std::string lm_gguf_kv_to_str(const struct lm_gguf_context * ctx_gguf, int i) {
|
133
|
-
const enum lm_gguf_type type = lm_gguf_get_kv_type(ctx_gguf, i);
|
134
|
-
|
135
|
-
switch (type) {
|
136
|
-
case LM_GGUF_TYPE_STRING:
|
137
|
-
return lm_gguf_get_val_str(ctx_gguf, i);
|
138
|
-
case LM_GGUF_TYPE_ARRAY:
|
139
|
-
{
|
140
|
-
const enum lm_gguf_type arr_type = lm_gguf_get_arr_type(ctx_gguf, i);
|
141
|
-
int arr_n = lm_gguf_get_arr_n(ctx_gguf, i);
|
142
|
-
const void * data = arr_type == LM_GGUF_TYPE_STRING ? nullptr : lm_gguf_get_arr_data(ctx_gguf, i);
|
143
|
-
std::stringstream ss;
|
144
|
-
ss << "[";
|
145
|
-
for (int j = 0; j < arr_n; j++) {
|
146
|
-
if (arr_type == LM_GGUF_TYPE_STRING) {
|
147
|
-
std::string val = lm_gguf_get_arr_str(ctx_gguf, i, j);
|
148
|
-
// escape quotes
|
149
|
-
replace_all(val, "\\", "\\\\");
|
150
|
-
replace_all(val, "\"", "\\\"");
|
151
|
-
ss << '"' << val << '"';
|
152
|
-
} else if (arr_type == LM_GGUF_TYPE_ARRAY) {
|
153
|
-
ss << "???";
|
154
|
-
} else {
|
155
|
-
ss << lm_gguf_data_to_str(arr_type, data, j);
|
156
|
-
}
|
157
|
-
if (j < arr_n - 1) {
|
158
|
-
ss << ", ";
|
159
|
-
}
|
160
|
-
}
|
161
|
-
ss << "]";
|
162
|
-
return ss.str();
|
163
|
-
}
|
164
|
-
default:
|
165
|
-
return lm_gguf_data_to_str(type, lm_gguf_get_val_data(ctx_gguf, i), 0);
|
166
|
-
}
|
167
|
-
}
|
1
|
+
#include "llama-impl.h"
|
2
|
+
|
3
|
+
#include "gguf.h"
|
4
|
+
#include "llama.h"
|
5
|
+
|
6
|
+
#include <cinttypes>
|
7
|
+
#include <climits>
|
8
|
+
#include <cstdarg>
|
9
|
+
#include <cstring>
|
10
|
+
#include <vector>
|
11
|
+
#include <sstream>
|
12
|
+
|
13
|
+
struct llama_logger_state {
|
14
|
+
lm_ggml_log_callback log_callback = llama_log_callback_default;
|
15
|
+
void * log_callback_user_data = nullptr;
|
16
|
+
};
|
17
|
+
|
18
|
+
static llama_logger_state g_logger_state;
|
19
|
+
|
20
|
+
time_meas::time_meas(int64_t & t_acc, bool disable) : t_start_us(disable ? -1 : lm_ggml_time_us()), t_acc(t_acc) {}
|
21
|
+
|
22
|
+
time_meas::~time_meas() {
|
23
|
+
if (t_start_us >= 0) {
|
24
|
+
t_acc += lm_ggml_time_us() - t_start_us;
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
void llama_log_set(lm_ggml_log_callback log_callback, void * user_data) {
|
29
|
+
lm_ggml_log_set(log_callback, user_data);
|
30
|
+
g_logger_state.log_callback = log_callback ? log_callback : llama_log_callback_default;
|
31
|
+
g_logger_state.log_callback_user_data = user_data;
|
32
|
+
}
|
33
|
+
|
34
|
+
static void llama_log_internal_v(lm_ggml_log_level level, const char * format, va_list args) {
|
35
|
+
va_list args_copy;
|
36
|
+
va_copy(args_copy, args);
|
37
|
+
char buffer[128];
|
38
|
+
int len = vsnprintf(buffer, 128, format, args);
|
39
|
+
if (len < 128) {
|
40
|
+
g_logger_state.log_callback(level, buffer, g_logger_state.log_callback_user_data);
|
41
|
+
} else {
|
42
|
+
char * buffer2 = new char[len + 1];
|
43
|
+
vsnprintf(buffer2, len + 1, format, args_copy);
|
44
|
+
buffer2[len] = 0;
|
45
|
+
g_logger_state.log_callback(level, buffer2, g_logger_state.log_callback_user_data);
|
46
|
+
delete[] buffer2;
|
47
|
+
}
|
48
|
+
va_end(args_copy);
|
49
|
+
}
|
50
|
+
|
51
|
+
void llama_log_internal(lm_ggml_log_level level, const char * format, ...) {
|
52
|
+
va_list args;
|
53
|
+
va_start(args, format);
|
54
|
+
llama_log_internal_v(level, format, args);
|
55
|
+
va_end(args);
|
56
|
+
}
|
57
|
+
|
58
|
+
void llama_log_callback_default(lm_ggml_log_level level, const char * text, void * user_data) {
|
59
|
+
(void) level;
|
60
|
+
(void) user_data;
|
61
|
+
fputs(text, stderr);
|
62
|
+
fflush(stderr);
|
63
|
+
}
|
64
|
+
|
65
|
+
void replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
66
|
+
if (search.empty()) {
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
std::string builder;
|
70
|
+
builder.reserve(s.length());
|
71
|
+
size_t pos = 0;
|
72
|
+
size_t last_pos = 0;
|
73
|
+
while ((pos = s.find(search, last_pos)) != std::string::npos) {
|
74
|
+
builder.append(s, last_pos, pos - last_pos);
|
75
|
+
builder.append(replace);
|
76
|
+
last_pos = pos + search.length();
|
77
|
+
}
|
78
|
+
builder.append(s, last_pos, std::string::npos);
|
79
|
+
s = std::move(builder);
|
80
|
+
}
|
81
|
+
|
82
|
+
std::string format(const char * fmt, ...) {
|
83
|
+
va_list ap;
|
84
|
+
va_list ap2;
|
85
|
+
va_start(ap, fmt);
|
86
|
+
va_copy(ap2, ap);
|
87
|
+
int size = vsnprintf(NULL, 0, fmt, ap);
|
88
|
+
LM_GGML_ASSERT(size >= 0 && size < INT_MAX); // NOLINT
|
89
|
+
std::vector<char> buf(size + 1);
|
90
|
+
int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
|
91
|
+
LM_GGML_ASSERT(size2 == size);
|
92
|
+
va_end(ap2);
|
93
|
+
va_end(ap);
|
94
|
+
return std::string(buf.data(), size);
|
95
|
+
}
|
96
|
+
|
97
|
+
std::string llama_format_tensor_shape(const std::vector<int64_t> & ne) {
|
98
|
+
char buf[256];
|
99
|
+
snprintf(buf, sizeof(buf), "%5" PRId64, ne.at(0));
|
100
|
+
for (size_t i = 1; i < ne.size(); i++) {
|
101
|
+
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ", %5" PRId64, ne.at(i));
|
102
|
+
}
|
103
|
+
return buf;
|
104
|
+
}
|
105
|
+
|
106
|
+
std::string llama_format_tensor_shape(const struct lm_ggml_tensor * t) {
|
107
|
+
char buf[256];
|
108
|
+
snprintf(buf, sizeof(buf), "%5" PRId64, t->ne[0]);
|
109
|
+
for (int i = 1; i < LM_GGML_MAX_DIMS; i++) {
|
110
|
+
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ", %5" PRId64, t->ne[i]);
|
111
|
+
}
|
112
|
+
return buf;
|
113
|
+
}
|
114
|
+
|
115
|
+
static std::string lm_gguf_data_to_str(enum lm_gguf_type type, const void * data, int i) {
|
116
|
+
switch (type) {
|
117
|
+
case LM_GGUF_TYPE_UINT8: return std::to_string(((const uint8_t *)data)[i]);
|
118
|
+
case LM_GGUF_TYPE_INT8: return std::to_string(((const int8_t *)data)[i]);
|
119
|
+
case LM_GGUF_TYPE_UINT16: return std::to_string(((const uint16_t *)data)[i]);
|
120
|
+
case LM_GGUF_TYPE_INT16: return std::to_string(((const int16_t *)data)[i]);
|
121
|
+
case LM_GGUF_TYPE_UINT32: return std::to_string(((const uint32_t *)data)[i]);
|
122
|
+
case LM_GGUF_TYPE_INT32: return std::to_string(((const int32_t *)data)[i]);
|
123
|
+
case LM_GGUF_TYPE_UINT64: return std::to_string(((const uint64_t *)data)[i]);
|
124
|
+
case LM_GGUF_TYPE_INT64: return std::to_string(((const int64_t *)data)[i]);
|
125
|
+
case LM_GGUF_TYPE_FLOAT32: return std::to_string(((const float *)data)[i]);
|
126
|
+
case LM_GGUF_TYPE_FLOAT64: return std::to_string(((const double *)data)[i]);
|
127
|
+
case LM_GGUF_TYPE_BOOL: return ((const bool *)data)[i] ? "true" : "false";
|
128
|
+
default: return format("unknown type %d", type);
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
std::string lm_gguf_kv_to_str(const struct lm_gguf_context * ctx_gguf, int i) {
|
133
|
+
const enum lm_gguf_type type = lm_gguf_get_kv_type(ctx_gguf, i);
|
134
|
+
|
135
|
+
switch (type) {
|
136
|
+
case LM_GGUF_TYPE_STRING:
|
137
|
+
return lm_gguf_get_val_str(ctx_gguf, i);
|
138
|
+
case LM_GGUF_TYPE_ARRAY:
|
139
|
+
{
|
140
|
+
const enum lm_gguf_type arr_type = lm_gguf_get_arr_type(ctx_gguf, i);
|
141
|
+
int arr_n = lm_gguf_get_arr_n(ctx_gguf, i);
|
142
|
+
const void * data = arr_type == LM_GGUF_TYPE_STRING ? nullptr : lm_gguf_get_arr_data(ctx_gguf, i);
|
143
|
+
std::stringstream ss;
|
144
|
+
ss << "[";
|
145
|
+
for (int j = 0; j < arr_n; j++) {
|
146
|
+
if (arr_type == LM_GGUF_TYPE_STRING) {
|
147
|
+
std::string val = lm_gguf_get_arr_str(ctx_gguf, i, j);
|
148
|
+
// escape quotes
|
149
|
+
replace_all(val, "\\", "\\\\");
|
150
|
+
replace_all(val, "\"", "\\\"");
|
151
|
+
ss << '"' << val << '"';
|
152
|
+
} else if (arr_type == LM_GGUF_TYPE_ARRAY) {
|
153
|
+
ss << "???";
|
154
|
+
} else {
|
155
|
+
ss << lm_gguf_data_to_str(arr_type, data, j);
|
156
|
+
}
|
157
|
+
if (j < arr_n - 1) {
|
158
|
+
ss << ", ";
|
159
|
+
}
|
160
|
+
}
|
161
|
+
ss << "]";
|
162
|
+
return ss.str();
|
163
|
+
}
|
164
|
+
default:
|
165
|
+
return lm_gguf_data_to_str(type, lm_gguf_get_val_data(ctx_gguf, i), 0);
|
166
|
+
}
|
167
|
+
}
|