@@ -180,6 +180,8 @@ llama_context::llama_context(
180
180
llama_context::~llama_context () = default ;
181
181
182
182
void llama_context::init () {
183
+ LLAMA_LOG_DEBUG (" %s: call\n " , __func__);
184
+
183
185
const auto & hparams = model.hparams ;
184
186
185
187
if (hparams.vocab_only ) {
@@ -188,13 +190,15 @@ void llama_context::init() {
188
190
}
189
191
190
192
{
191
- // buffer types used for the compute buffer of each backend
193
+ LLAMA_LOG_DEBUG (" %s: enumerating backends\n " , __func__);
194
+
192
195
backend_buft.clear ();
193
196
backend_ptrs.clear ();
194
197
195
198
for (auto & backend : backends) {
196
199
auto * buft = ggml_backend_get_default_buffer_type (backend.get ());
197
200
auto backend_type = ggml_backend_dev_type (ggml_backend_get_device (backend.get ()));
201
+
198
202
if (backend_type == GGML_BACKEND_DEVICE_TYPE_CPU && !model.devices .empty ()) {
199
203
// use the host buffer of the first device CPU for faster transfer of the intermediate state
200
204
auto * dev = model.devices [0 ];
@@ -203,14 +207,18 @@ void llama_context::init() {
203
207
buft = host_buft;
204
208
}
205
209
}
210
+
206
211
backend_buft.push_back (buft);
207
212
backend_ptrs.push_back (backend.get ());
208
213
}
209
214
215
+ LLAMA_LOG_DEBUG (" %s: backend_ptrs.size() = %zu\n " , __func__, backend_ptrs.size ());
216
+
210
217
const size_t max_nodes = this ->max_nodes ();
211
218
219
+ LLAMA_LOG_DEBUG (" %s: max_nodes = %zu\n " , __func__, max_nodes);
220
+
212
221
// buffer used to store the computation graph and the tensor meta data
213
- // TODO: move to base class
214
222
buf_compute_meta.resize (ggml_tensor_overhead ()*max_nodes + ggml_graph_overhead_custom (max_nodes, false ));
215
223
216
224
// TODO: move these checks to ggml_backend_sched
@@ -247,6 +255,8 @@ void llama_context::init() {
247
255
}
248
256
}
249
257
258
+ LLAMA_LOG_DEBUG (" %s: calling reserve()\n " , __func__);
259
+
250
260
reserve ();
251
261
}
252
262
@@ -286,15 +296,17 @@ void llama_context::reserve() {
286
296
287
297
llama_token token = model.vocab .token_bos (); // not actually used by llama_build_graph, but required to choose between token and embedding inputs graph
288
298
299
+ // max number of outputs
300
+ n_outputs = n_tokens;
301
+
302
+ LLAMA_LOG_DEBUG (" %s: n_tokens = %d, n_seqs = %d, n_outputs = %d\n " , __func__, n_tokens, n_seqs, n_outputs);
303
+
289
304
int n_splits_pp = -1 ;
290
305
int n_nodes_pp = -1 ;
291
306
292
307
int n_splits_tg = -1 ;
293
308
int n_nodes_tg = -1 ;
294
309
295
- // max number of outputs
296
- n_outputs = n_tokens;
297
-
298
310
// reserve pp graph first so that buffers are only allocated once
299
311
{
300
312
llama_ubatch ubatch_pp = { true , n_tokens, n_tokens / n_seqs, n_seqs, &token, nullptr , nullptr , nullptr , nullptr , nullptr };
@@ -521,21 +533,29 @@ int64_t llama_context::n_pos_per_token() const {
521
533
void llama_context::attach_threadpool (
522
534
ggml_threadpool_t threadpool,
523
535
ggml_threadpool_t threadpool_batch) {
536
+ LLAMA_LOG_DEBUG (" %s: call\n " , __func__);
537
+
524
538
this ->threadpool = threadpool;
525
539
this ->threadpool_batch = threadpool_batch ? threadpool_batch : threadpool;
526
540
}
527
541
528
542
void llama_context::detach_threadpool () {
543
+ LLAMA_LOG_DEBUG (" %s: call\n " , __func__);
544
+
529
545
this ->threadpool = nullptr ;
530
546
this ->threadpool_batch = nullptr ;
531
547
}
532
548
533
549
void llama_context::set_n_threads (int32_t n_threads, int32_t n_threads_batch) {
550
+ LLAMA_LOG_DEBUG (" %s: n_threads = %d, n_threads_batch = %d\n " , __func__, n_threads, n_threads_batch);
551
+
534
552
cparams.n_threads = n_threads;
535
553
cparams.n_threads_batch = n_threads_batch;
536
554
}
537
555
538
556
void llama_context::set_abort_callback (bool (*abort_callback)(void * data), void * abort_callback_data) {
557
+ LLAMA_LOG_DEBUG (" %s: call\n " , __func__);
558
+
539
559
this ->abort_callback = abort_callback;
540
560
this ->abort_callback_data = abort_callback_data;
541
561
@@ -549,21 +569,29 @@ void llama_context::set_abort_callback(bool (*abort_callback)(void * data), void
549
569
}
550
570
551
571
void llama_context::set_embeddings (bool value) {
572
+ LLAMA_LOG_DEBUG (" %s: value = %d\n " , __func__, value);
573
+
552
574
cparams.embeddings = value;
553
575
}
554
576
555
577
void llama_context::set_causal_attn (bool value) {
578
+ LLAMA_LOG_DEBUG (" %s: value = %d\n " , __func__, value);
579
+
556
580
cparams.causal_attn = value;
557
581
}
558
582
559
583
void llama_context::set_adapter_lora (
560
- struct llama_adapter_lora * adapter,
584
+ llama_adapter_lora * adapter,
561
585
float scale) {
586
+ LLAMA_LOG_DEBUG (" %s: adapter = %p, scale = %f\n " , __func__, (void *) adapter, scale);
587
+
562
588
loras[adapter] = scale;
563
589
}
564
590
565
591
bool llama_context::rm_adapter_lora (
566
- struct llama_adapter_lora * adapter) {
592
+ llama_adapter_lora * adapter) {
593
+ LLAMA_LOG_DEBUG (" %s: adapter = %p\n " , __func__, (void *) adapter);
594
+
567
595
auto pos = loras.find (adapter);
568
596
if (pos != loras.end ()) {
569
597
loras.erase (pos);
@@ -574,6 +602,8 @@ bool llama_context::rm_adapter_lora(
574
602
}
575
603
576
604
void llama_context::clear_adapter_lora () {
605
+ LLAMA_LOG_DEBUG (" %s: call\n " , __func__);
606
+
577
607
loras.clear ();
578
608
}
579
609
@@ -583,6 +613,8 @@ bool llama_context::apply_adapter_cvec(
583
613
int32_t n_embd,
584
614
int32_t il_start,
585
615
int32_t il_end) {
616
+ LLAMA_LOG_DEBUG (" %s: il_start = %d, il_end = %d\n " , __func__, il_start, il_end);
617
+
586
618
return cvec.apply (model, data, len, n_embd, il_start, il_end);
587
619
}
588
620
@@ -2085,15 +2117,21 @@ size_t llama_context::state_seq_save_file(llama_seq_id seq_id, const char * file
2085
2117
}
2086
2118
2087
2119
size_t llama_context::state_get_data (llama_io_write_i & io) {
2120
+ LLAMA_LOG_DEBUG (" %s: writing state\n " , __func__);
2121
+
2088
2122
// write model info
2089
2123
{
2124
+ LLAMA_LOG_DEBUG (" %s: - writing model info\n " , __func__);
2125
+
2090
2126
const std::string arch_str = llm_arch_name (model.arch );
2091
2127
io.write_string (arch_str);
2092
2128
// TODO: add more model-specific info which should prevent loading the session file if not identical
2093
2129
}
2094
2130
2095
2131
// write output ids
2096
2132
{
2133
+ LLAMA_LOG_DEBUG (" %s: - writing output ids\n " , __func__);
2134
+
2097
2135
output_reorder ();
2098
2136
2099
2137
const auto n_outputs = this ->n_outputs ;
@@ -2124,6 +2162,8 @@ size_t llama_context::state_get_data(llama_io_write_i & io) {
2124
2162
2125
2163
// write logits
2126
2164
{
2165
+ LLAMA_LOG_DEBUG (" %s: - writing logits\n " , __func__);
2166
+
2127
2167
const uint64_t logits_size = std::min ((uint64_t ) this ->logits_size , (uint64_t ) n_outputs * model.vocab .n_tokens ());
2128
2168
2129
2169
io.write (&logits_size, sizeof (logits_size));
@@ -2135,6 +2175,8 @@ size_t llama_context::state_get_data(llama_io_write_i & io) {
2135
2175
2136
2176
// write embeddings
2137
2177
{
2178
+ LLAMA_LOG_DEBUG (" %s: - writing embeddings\n " , __func__);
2179
+
2138
2180
const uint64_t embd_size = std::min ((uint64_t ) this ->embd_size , (uint64_t ) n_outputs * model.hparams .n_embd );
2139
2181
2140
2182
io.write (&embd_size, sizeof (embd_size));
@@ -2148,8 +2190,12 @@ size_t llama_context::state_get_data(llama_io_write_i & io) {
2148
2190
}
2149
2191
2150
2192
size_t llama_context::state_set_data (llama_io_read_i & io) {
2193
+ LLAMA_LOG_DEBUG (" %s: reading state\n " , __func__);
2194
+
2151
2195
// read model info
2152
2196
{
2197
+ LLAMA_LOG_DEBUG (" %s: - reading model info\n " , __func__);
2198
+
2153
2199
const std::string cur_arch_str = llm_arch_name (model.arch );
2154
2200
2155
2201
std::string arch_str;
@@ -2162,6 +2208,8 @@ size_t llama_context::state_set_data(llama_io_read_i & io) {
2162
2208
2163
2209
// read output ids
2164
2210
{
2211
+ LLAMA_LOG_DEBUG (" %s: - reading output ids\n " , __func__);
2212
+
2165
2213
auto n_outputs = this ->n_outputs ;
2166
2214
io.read_to (&n_outputs, sizeof (n_outputs));
2167
2215
@@ -2189,6 +2237,8 @@ size_t llama_context::state_set_data(llama_io_read_i & io) {
2189
2237
2190
2238
// read logits
2191
2239
{
2240
+ LLAMA_LOG_DEBUG (" %s: - reading logits\n " , __func__);
2241
+
2192
2242
uint64_t logits_size;
2193
2243
io.read_to (&logits_size, sizeof (logits_size));
2194
2244
@@ -2203,6 +2253,8 @@ size_t llama_context::state_set_data(llama_io_read_i & io) {
2203
2253
2204
2254
// read embeddings
2205
2255
{
2256
+ LLAMA_LOG_DEBUG (" %s: - reading embeddings\n " , __func__);
2257
+
2206
2258
uint64_t embd_size;
2207
2259
io.read_to (&embd_size, sizeof (embd_size));
2208
2260
@@ -2285,6 +2337,8 @@ void llama_context_kv_self::reserve() {
2285
2337
// simulate full KV cache
2286
2338
kv_self.n = kv_self.size ;
2287
2339
2340
+ LLAMA_LOG_DEBUG (" %s: kv_self.n = %u\n " , __func__, kv_self.n );
2341
+
2288
2342
llama_context::reserve ();
2289
2343
}
2290
2344
@@ -2297,6 +2351,8 @@ const llama_kv_cache * llama_context_kv_self::get_kv_self() const {
2297
2351
}
2298
2352
2299
2353
void llama_context_kv_self::kv_self_update () {
2354
+ LLAMA_LOG_DEBUG (" %s: kv_self_update()\n " , __func__);
2355
+
2300
2356
auto & kv = kv_self;
2301
2357
2302
2358
bool need_reserve = false ;
@@ -2306,6 +2362,8 @@ void llama_context_kv_self::kv_self_update() {
2306
2362
GGML_ABORT (" The current context does not support K-shift" );
2307
2363
}
2308
2364
2365
+ LLAMA_LOG_DEBUG (" %s: applying K-shift\n " , __func__);
2366
+
2309
2367
// apply K-shift if needed
2310
2368
if (model.hparams .rope_type != LLAMA_ROPE_TYPE_NONE) {
2311
2369
ggml_backend_sched_reset (sched.get ());
@@ -2334,6 +2392,8 @@ void llama_context_kv_self::kv_self_update() {
2334
2392
2335
2393
// defragment the KV cache if needed
2336
2394
if (kv.do_defrag ) {
2395
+ LLAMA_LOG_DEBUG (" %s: defragmenting KV cache\n " , __func__);
2396
+
2337
2397
ggml_backend_sched_reset (sched.get ());
2338
2398
2339
2399
auto * gf = graph_init ();
@@ -3667,6 +3727,7 @@ ggml_tensor * llama_context_kv_self::build_inp_kq_mask_cross(
3667
3727
size_t llama_context_kv_self::state_get_data (llama_io_write_i & io) {
3668
3728
llama_context::state_get_data (io);
3669
3729
3730
+ LLAMA_LOG_DEBUG (" %s: - writing KV self\n " , __func__);
3670
3731
kv_self.state_write (io);
3671
3732
3672
3733
return io.n_bytes ();
@@ -3675,6 +3736,7 @@ size_t llama_context_kv_self::state_get_data(llama_io_write_i & io) {
3675
3736
size_t llama_context_kv_self::state_set_data (llama_io_read_i & io) {
3676
3737
llama_context::state_set_data (io);
3677
3738
3739
+ LLAMA_LOG_DEBUG (" %s: - reading KV self\n " , __func__);
3678
3740
kv_self.state_read (io);
3679
3741
3680
3742
return io.n_bytes ();
0 commit comments