cui-llama.rn 1.4.0 → 1.4.1
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/android/src/main/jni.cpp +9 -9
- package/cpp/common.cpp +163 -60
- package/cpp/common.h +43 -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.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/package.json +1 -1
package/cpp/llama-adapter.cpp
CHANGED
@@ -1,346 +1,347 @@
|
|
1
|
-
#include "llama-adapter.h"
|
2
|
-
|
3
|
-
#include "llama-
|
4
|
-
|
5
|
-
#include
|
6
|
-
|
7
|
-
#include <
|
8
|
-
#include <
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
LM_GGML_ASSERT(
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
/*.
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
const
|
96
|
-
|
97
|
-
|
98
|
-
int32_t
|
99
|
-
int32_t
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
if (
|
279
|
-
throw std::runtime_error("tensor
|
280
|
-
}
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
read_buf.
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
1
|
+
#include "llama-adapter.h"
|
2
|
+
|
3
|
+
#include "llama-impl.h"
|
4
|
+
#include "llama-mmap.h"
|
5
|
+
#include "llama-model.h"
|
6
|
+
|
7
|
+
#include <algorithm>
|
8
|
+
#include <map>
|
9
|
+
#include <cassert>
|
10
|
+
#include <stdexcept>
|
11
|
+
|
12
|
+
// vec
|
13
|
+
|
14
|
+
struct lm_ggml_tensor * llama_adapter_cvec::tensor_for(int il) const {
|
15
|
+
if (il < 0 || il < layer_start || il > layer_end || (size_t) il >= tensors.size()) {
|
16
|
+
return nullptr;
|
17
|
+
}
|
18
|
+
|
19
|
+
return tensors[il];
|
20
|
+
}
|
21
|
+
|
22
|
+
struct lm_ggml_tensor * llama_adapter_cvec::apply_to(struct lm_ggml_context * ctx, struct lm_ggml_tensor * cur, int il) const {
|
23
|
+
lm_ggml_tensor * layer_dir = tensor_for(il);
|
24
|
+
if (layer_dir != nullptr) {
|
25
|
+
cur = lm_ggml_add(ctx, cur, layer_dir);
|
26
|
+
}
|
27
|
+
|
28
|
+
return cur;
|
29
|
+
}
|
30
|
+
|
31
|
+
bool llama_adapter_cvec::init(const llama_model & model) {
|
32
|
+
const auto & hparams = model.hparams;
|
33
|
+
|
34
|
+
LM_GGML_ASSERT(tensors.empty());
|
35
|
+
LM_GGML_ASSERT(ctxs.empty());
|
36
|
+
LM_GGML_ASSERT(bufs.empty());
|
37
|
+
|
38
|
+
// create a context for each buffer type
|
39
|
+
std::map<lm_ggml_backend_buffer_type_t, lm_ggml_context *> ctx_map;
|
40
|
+
auto ctx_for_buft = [&](lm_ggml_backend_buffer_type_t buft) -> lm_ggml_context * {
|
41
|
+
auto it = ctx_map.find(buft);
|
42
|
+
if (it == ctx_map.end()) {
|
43
|
+
struct lm_ggml_init_params params = {
|
44
|
+
/*.mem_size =*/ hparams.n_layer*lm_ggml_tensor_overhead(),
|
45
|
+
/*.mem_buffer =*/ NULL,
|
46
|
+
/*.no_alloc =*/ true,
|
47
|
+
};
|
48
|
+
|
49
|
+
lm_ggml_context * ctx = lm_ggml_init(params);
|
50
|
+
if (!ctx) {
|
51
|
+
return nullptr;
|
52
|
+
}
|
53
|
+
|
54
|
+
ctx_map[buft] = ctx;
|
55
|
+
ctxs.emplace_back(ctx);
|
56
|
+
|
57
|
+
return ctx;
|
58
|
+
}
|
59
|
+
|
60
|
+
return it->second;
|
61
|
+
};
|
62
|
+
|
63
|
+
// make tensors
|
64
|
+
tensors.reserve(hparams.n_layer);
|
65
|
+
tensors.push_back(nullptr); // there's never a tensor for layer 0
|
66
|
+
for (size_t il = 1; il < hparams.n_layer; il++) {
|
67
|
+
lm_ggml_backend_buffer_type_t buft = model.select_buft(il);
|
68
|
+
lm_ggml_context * ctx = ctx_for_buft(buft);
|
69
|
+
if (!ctx) {
|
70
|
+
LLAMA_LOG_ERROR("%s: failed to allocate context for control vector\n", __func__);
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
lm_ggml_tensor * tensor = lm_ggml_new_tensor_1d(ctx, LM_GGML_TYPE_F32, hparams.n_embd);
|
74
|
+
tensors.push_back(tensor);
|
75
|
+
}
|
76
|
+
|
77
|
+
// allocate tensors / buffers and zero
|
78
|
+
bufs.reserve(ctx_map.size());
|
79
|
+
for (auto it : ctx_map) {
|
80
|
+
lm_ggml_backend_buffer_type_t buft = it.first;
|
81
|
+
lm_ggml_context * ctx = it.second;
|
82
|
+
lm_ggml_backend_buffer_t buf = lm_ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
|
83
|
+
if (!buf) {
|
84
|
+
LLAMA_LOG_ERROR("%s: failed to allocate buffer for control vector\n", __func__);
|
85
|
+
return false;
|
86
|
+
}
|
87
|
+
lm_ggml_backend_buffer_clear(buf, 0);
|
88
|
+
bufs.emplace_back(buf);
|
89
|
+
}
|
90
|
+
|
91
|
+
return true;
|
92
|
+
}
|
93
|
+
|
94
|
+
int32_t llama_adapter_cvec::apply(
|
95
|
+
const llama_model & model,
|
96
|
+
const float * data,
|
97
|
+
size_t len,
|
98
|
+
int32_t n_embd,
|
99
|
+
int32_t il_start,
|
100
|
+
int32_t il_end) {
|
101
|
+
const auto & hparams = model.hparams;
|
102
|
+
|
103
|
+
if (data == nullptr) {
|
104
|
+
// disable the current control vector (but leave allocated for later)
|
105
|
+
layer_start = -1;
|
106
|
+
layer_end = -1;
|
107
|
+
return 0;
|
108
|
+
}
|
109
|
+
|
110
|
+
if (n_embd != (int) hparams.n_embd) {
|
111
|
+
LLAMA_LOG_ERROR("%s: control vector n_embd does not match model\n", __func__);
|
112
|
+
return 1;
|
113
|
+
}
|
114
|
+
|
115
|
+
if (tensors.empty()) {
|
116
|
+
if (!init(model)) {
|
117
|
+
return 1;
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
layer_start = il_start;
|
122
|
+
layer_end = il_end;
|
123
|
+
|
124
|
+
for (size_t il = 1; il < hparams.n_layer; il++) {
|
125
|
+
assert(tensors[il] != nullptr);
|
126
|
+
|
127
|
+
const size_t off = n_embd * (il - 1); // buffer doesn't have data for layer 0, since it's never present
|
128
|
+
if (off + n_embd <= len) {
|
129
|
+
lm_ggml_backend_tensor_set(tensors[il], data + off, 0, n_embd * lm_ggml_element_size(tensors[il]));
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
return 0;
|
134
|
+
}
|
135
|
+
|
136
|
+
// lora
|
137
|
+
|
138
|
+
llama_adapter_lora_weight * llama_adapter_lora::get_weight(struct lm_ggml_tensor * w) {
|
139
|
+
const std::string name(w->name);
|
140
|
+
|
141
|
+
const auto pos = ab_map.find(name);
|
142
|
+
if (pos != ab_map.end()) {
|
143
|
+
return &pos->second;
|
144
|
+
}
|
145
|
+
|
146
|
+
return nullptr;
|
147
|
+
}
|
148
|
+
|
149
|
+
static void llama_adapter_lora_init_impl(struct llama_model & model, const char * path_lora, struct llama_adapter_lora & adapter) {
|
150
|
+
LLAMA_LOG_INFO("%s: loading lora adapter from '%s' ...\n", __func__, path_lora);
|
151
|
+
|
152
|
+
lm_ggml_context * ctx_init;
|
153
|
+
struct lm_gguf_init_params meta_lm_gguf_params = {
|
154
|
+
/* .no_alloc = */ true,
|
155
|
+
/* .ctx = */ &ctx_init,
|
156
|
+
};
|
157
|
+
|
158
|
+
lm_gguf_context_ptr ctx_gguf { lm_gguf_init_from_file(path_lora, meta_lm_gguf_params) };
|
159
|
+
if (!ctx_gguf) {
|
160
|
+
throw std::runtime_error("failed to load lora adapter file from " + std::string(path_lora));
|
161
|
+
}
|
162
|
+
|
163
|
+
lm_ggml_context_ptr ctx { ctx_init };
|
164
|
+
|
165
|
+
// check metadata
|
166
|
+
{
|
167
|
+
auto get_kv_str = [&](const std::string & key) -> std::string {
|
168
|
+
int id = lm_gguf_find_key(ctx_gguf.get(), key.c_str());
|
169
|
+
return id < 0 ? "" : std::string(lm_gguf_get_val_str(ctx_gguf.get(), id));
|
170
|
+
};
|
171
|
+
auto get_kv_f32 = [&](const std::string & key) -> float {
|
172
|
+
int id = lm_gguf_find_key(ctx_gguf.get(), key.c_str());
|
173
|
+
return id < 0 ? 0.0f : lm_gguf_get_val_f32(ctx_gguf.get(), id);
|
174
|
+
};
|
175
|
+
LLM_KV llm_kv = LLM_KV(LLM_ARCH_UNKNOWN);
|
176
|
+
|
177
|
+
auto general_type = get_kv_str(llm_kv(LLM_KV_GENERAL_TYPE));
|
178
|
+
if (general_type != "adapter") {
|
179
|
+
throw std::runtime_error("expect general.type to be 'adapter', but got: " + general_type);
|
180
|
+
}
|
181
|
+
|
182
|
+
auto general_arch_str = get_kv_str(llm_kv(LLM_KV_GENERAL_ARCHITECTURE));
|
183
|
+
auto general_arch = llm_arch_from_string(general_arch_str);
|
184
|
+
if (general_arch != model.arch) {
|
185
|
+
throw std::runtime_error("model arch and LoRA arch mismatch");
|
186
|
+
}
|
187
|
+
|
188
|
+
auto adapter_type = get_kv_str(llm_kv(LLM_KV_ADAPTER_TYPE));
|
189
|
+
if (adapter_type != "lora") {
|
190
|
+
throw std::runtime_error("expect adapter.type to be 'lora', but got: " + adapter_type);
|
191
|
+
}
|
192
|
+
|
193
|
+
adapter.alpha = get_kv_f32(llm_kv(LLM_KV_ADAPTER_LORA_ALPHA));
|
194
|
+
}
|
195
|
+
|
196
|
+
int n_tensors = lm_gguf_get_n_tensors(ctx_gguf.get());
|
197
|
+
|
198
|
+
// contexts for each buffer type
|
199
|
+
std::map<lm_ggml_backend_buffer_type_t, lm_ggml_context *> ctx_map;
|
200
|
+
auto ctx_for_buft = [&](lm_ggml_backend_buffer_type_t buft) -> lm_ggml_context * {
|
201
|
+
auto it = ctx_map.find(buft);
|
202
|
+
if (it == ctx_map.end()) {
|
203
|
+
// add a new context
|
204
|
+
struct lm_ggml_init_params params = {
|
205
|
+
/*.mem_size =*/ n_tensors*lm_ggml_tensor_overhead(),
|
206
|
+
/*.mem_buffer =*/ NULL,
|
207
|
+
/*.no_alloc =*/ true,
|
208
|
+
};
|
209
|
+
lm_ggml_context * buft_ctx = lm_ggml_init(params);
|
210
|
+
if (!buft_ctx) {
|
211
|
+
return nullptr;
|
212
|
+
}
|
213
|
+
ctx_map[buft] = buft_ctx;
|
214
|
+
adapter.ctxs.emplace_back(buft_ctx);
|
215
|
+
return buft_ctx;
|
216
|
+
};
|
217
|
+
return it->second;
|
218
|
+
};
|
219
|
+
|
220
|
+
// bundle lora_a and lora_b into pairs
|
221
|
+
std::map<std::string, llama_adapter_lora_weight> ab_map;
|
222
|
+
auto str_endswith = [](const std::string & str, const std::string & suffix) {
|
223
|
+
return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0;
|
224
|
+
};
|
225
|
+
|
226
|
+
for (lm_ggml_tensor * cur = lm_ggml_get_first_tensor(ctx.get()); cur; cur = lm_ggml_get_next_tensor(ctx.get(), cur)) {
|
227
|
+
std::string name(cur->name);
|
228
|
+
if (str_endswith(name, ".lora_a")) {
|
229
|
+
replace_all(name, ".lora_a", "");
|
230
|
+
if (ab_map.find(name) == ab_map.end()) {
|
231
|
+
ab_map[name] = llama_adapter_lora_weight(cur, nullptr);
|
232
|
+
} else {
|
233
|
+
ab_map[name].a = cur;
|
234
|
+
}
|
235
|
+
} else if (str_endswith(name, ".lora_b")) {
|
236
|
+
replace_all(name, ".lora_b", "");
|
237
|
+
if (ab_map.find(name) == ab_map.end()) {
|
238
|
+
ab_map[name] = llama_adapter_lora_weight(nullptr, cur);
|
239
|
+
} else {
|
240
|
+
ab_map[name].b = cur;
|
241
|
+
}
|
242
|
+
} else if (str_endswith(name, "_norm.weight")) {
|
243
|
+
// TODO: add support for norm vector
|
244
|
+
// for now, we don't really care because most adapters still work fine without it
|
245
|
+
continue;
|
246
|
+
} else {
|
247
|
+
throw std::runtime_error("LoRA tensor '" + name + "' has unexpected suffix");
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
// add tensors
|
252
|
+
for (auto & it : ab_map) {
|
253
|
+
const std::string & name = it.first;
|
254
|
+
llama_adapter_lora_weight & w = it.second;
|
255
|
+
bool is_token_embd = str_endswith(name, "token_embd.weight");
|
256
|
+
|
257
|
+
if (!w.a || !w.b) {
|
258
|
+
throw std::runtime_error("LoRA tensor pair for '" + name + "' is missing one component");
|
259
|
+
}
|
260
|
+
|
261
|
+
// device buft and device ctx
|
262
|
+
const auto * model_tensor = model.get_tensor(name.c_str());
|
263
|
+
if (!model_tensor) {
|
264
|
+
throw std::runtime_error("LoRA tensor '" + name + "' does not exist in base model (hint: maybe wrong base model?)");
|
265
|
+
}
|
266
|
+
|
267
|
+
struct lm_ggml_context * dev_ctx = ctx_for_buft(lm_ggml_backend_buffer_get_type(model_tensor->buffer));
|
268
|
+
// validate tensor shape
|
269
|
+
if (is_token_embd) {
|
270
|
+
// expect B to be non-transposed, A and B are flipped; see llm_build_inp_embd()
|
271
|
+
if (model_tensor->ne[0] != w.b->ne[1] || model_tensor->ne[1] != w.a->ne[1]) {
|
272
|
+
throw std::runtime_error("tensor '" + name + "' has incorrect shape (hint: maybe wrong base model?)");
|
273
|
+
}
|
274
|
+
} else {
|
275
|
+
if (model_tensor->ne[0] != w.a->ne[0] || model_tensor->ne[1] != w.b->ne[1]) {
|
276
|
+
throw std::runtime_error("tensor '" + name + "' has incorrect shape (hint: maybe wrong base model?)");
|
277
|
+
}
|
278
|
+
if (w.a->ne[1] != w.b->ne[0]) {
|
279
|
+
throw std::runtime_error("lora_a tensor is not transposed (hint: adapter from \"finetune\" example is no longer supported)");
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
// save tensor to adapter
|
284
|
+
struct lm_ggml_tensor * tensor_a = lm_ggml_dup_tensor(dev_ctx, w.a);
|
285
|
+
struct lm_ggml_tensor * tensor_b = lm_ggml_dup_tensor(dev_ctx, w.b);
|
286
|
+
lm_ggml_set_name(tensor_a, w.a->name);
|
287
|
+
lm_ggml_set_name(tensor_b, w.b->name);
|
288
|
+
adapter.ab_map[name] = llama_adapter_lora_weight(tensor_a, tensor_b);
|
289
|
+
}
|
290
|
+
|
291
|
+
// allocate tensors / buffers and zero
|
292
|
+
{
|
293
|
+
adapter.ctxs.reserve(ctx_map.size());
|
294
|
+
adapter.bufs.reserve(ctx_map.size());
|
295
|
+
for (auto & it : ctx_map) {
|
296
|
+
lm_ggml_backend_buffer_type_t buft = it.first;
|
297
|
+
lm_ggml_context * ctx_dev = it.second;
|
298
|
+
lm_ggml_backend_buffer_ptr buf { lm_ggml_backend_alloc_ctx_tensors_from_buft(ctx_dev, buft) };
|
299
|
+
if (!buf) {
|
300
|
+
throw std::runtime_error("failed to allocate buffer for lora adapter\n");
|
301
|
+
}
|
302
|
+
LLAMA_LOG_INFO("%s: %10s LoRA buffer size = %8.2f MiB\n", __func__, lm_ggml_backend_buffer_name(buf.get()), lm_ggml_backend_buffer_get_size(buf.get())/1024.0/1024.0);
|
303
|
+
adapter.bufs.emplace_back(std::move(buf));
|
304
|
+
}
|
305
|
+
}
|
306
|
+
|
307
|
+
// set tensor data
|
308
|
+
{
|
309
|
+
llama_file lm_gguf_file(path_lora, "rb");
|
310
|
+
std::vector<uint8_t> read_buf;
|
311
|
+
auto set_tensor = [&](struct lm_ggml_tensor * orig, struct lm_ggml_tensor * dev) {
|
312
|
+
size_t offs = lm_gguf_get_data_offset(ctx_gguf.get()) + lm_gguf_get_tensor_offset(ctx_gguf.get(), lm_gguf_find_tensor(ctx_gguf.get(), orig->name));
|
313
|
+
size_t size = lm_ggml_nbytes(orig);
|
314
|
+
read_buf.resize(size);
|
315
|
+
lm_gguf_file.seek(offs, SEEK_SET);
|
316
|
+
lm_gguf_file.read_raw(read_buf.data(), size);
|
317
|
+
lm_ggml_backend_tensor_set(dev, read_buf.data(), 0, size);
|
318
|
+
};
|
319
|
+
for (auto & it : adapter.ab_map) {
|
320
|
+
auto orig = ab_map[it.first];
|
321
|
+
auto dev = it.second;
|
322
|
+
set_tensor(orig.a, dev.a);
|
323
|
+
set_tensor(orig.b, dev.b);
|
324
|
+
}
|
325
|
+
}
|
326
|
+
|
327
|
+
LLAMA_LOG_INFO("%s: loaded %zu tensors from lora file\n", __func__, adapter.ab_map.size()*2);
|
328
|
+
}
|
329
|
+
|
330
|
+
struct llama_adapter_lora * llama_adapter_lora_init(struct llama_model * model, const char * path_lora) {
|
331
|
+
struct llama_adapter_lora * adapter = new llama_adapter_lora();
|
332
|
+
|
333
|
+
try {
|
334
|
+
llama_adapter_lora_init_impl(*model, path_lora, *adapter);
|
335
|
+
return adapter;
|
336
|
+
} catch (const std::exception & err) {
|
337
|
+
LLAMA_LOG_ERROR("%s: failed to apply lora adapter: %s\n", __func__, err.what());
|
338
|
+
|
339
|
+
delete adapter;
|
340
|
+
}
|
341
|
+
|
342
|
+
return nullptr;
|
343
|
+
}
|
344
|
+
|
345
|
+
void llama_adapter_lora_free(struct llama_adapter_lora * adapter) {
|
346
|
+
delete adapter;
|
347
|
+
}
|