Coverage Report

Created: 2026-07-16 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/llama-graph.h
Line
Count
Source
1
#pragma once
2
3
#include "llama-arch.h"
4
#include "llama-batch.h"
5
#include "llama-hparams.h"
6
#include "llama-adapter.h"
7
8
#include <cstdint>
9
#include <vector>
10
#include <memory>
11
#include <set>
12
#include <functional>
13
#include <map>
14
15
struct ggml_cgraph;
16
struct ggml_context;
17
struct ggml_tensor;
18
19
struct llama_cparams;
20
struct llama_layer;
21
22
struct llama_memory_context_i;
23
24
class llama_kv_cache_context;
25
class llama_kv_cache_dsa_context;
26
class llama_kv_cache_dsv4_raw_context;
27
class llama_kv_cache_dsv4_context;
28
class llama_kv_cache_iswa_context;
29
class llama_memory_recurrent_context;
30
class llama_memory_hybrid_context;
31
class llama_memory_hybrid_iswa_context;
32
33
// certain models (typically multi-modal) can produce different types of graphs
34
enum llm_graph_type {
35
    LLM_GRAPH_TYPE_DEFAULT,
36
    LLM_GRAPH_TYPE_ENCODER,
37
    LLM_GRAPH_TYPE_DECODER,
38
    LLM_GRAPH_TYPE_DECODER_MTP,
39
};
40
41
enum llm_fused_op {
42
    LLM_FUSED_OP_FLASH_ATTN,
43
    LLM_FUSED_OP_GDN_AR,
44
    LLM_FUSED_OP_GDN_CH,
45
    LLM_FUSED_OP_LIGHTNING_INDEXER,
46
};
47
48
enum llm_ffn_op_type : int {
49
    LLM_FFN_NONE = 0,           // sentinel: unset; archs must assign before use
50
    LLM_FFN_SILU,
51
    LLM_FFN_GELU,
52
    LLM_FFN_RELU,
53
    LLM_FFN_RELU_SQR,
54
    LLM_FFN_SWIGLU,
55
    LLM_FFN_GEGLU,
56
    LLM_FFN_REGLU,
57
    LLM_FFN_SWIGLU_OAI_MOE,
58
};
59
60
enum llm_ffn_gate_type {
61
    LLM_FFN_SEQ,
62
    LLM_FFN_PAR, // ffn_gate is parallel to ffn_up
63
};
64
65
enum llm_norm_type {
66
    LLM_NORM,
67
    LLM_NORM_RMS,
68
    LLM_NORM_GROUP,
69
};
70
71
// TODO: tmp - need something better to pass the data from the encoder to the decoder
72
struct llama_cross {
73
    // the output embeddings from the encoder as a ggml tensor
74
    // TODO: this needs more work to be correct, for now copy the embeddings data to host memory
75
    //       ref: https://github.com/ggml-org/llama.cpp/pull/11213#discussion_r1969892524
76
    //ggml_tensor * t_embd = nullptr;
77
78
    int64_t n_embd = 0;
79
    int64_t n_enc  = 0;
80
81
    // embeddings data copied to host memory (tmp)
82
    std::vector<float> v_embd;
83
84
    // needed to construct the cross-attention mask in the decoder
85
    std::vector<std::set<llama_seq_id>> seq_ids_enc;
86
};
87
88
struct llm_graph_params;
89
90
//
91
// llm_graph_input
92
//
93
94
class llm_graph_input_i {
95
public:
96
0
    llm_graph_input_i() {
97
0
        const char * LLAMA_GRAPH_INPUT_DEBUG = getenv("LLAMA_GRAPH_INPUT_DEBUG");
98
0
        debug = LLAMA_GRAPH_INPUT_DEBUG ? atoi(LLAMA_GRAPH_INPUT_DEBUG) : 0;
99
0
    }
100
101
0
    virtual ~llm_graph_input_i() = default;
102
103
    virtual void set_input(const llama_ubatch * ubatch) = 0;
104
105
    // return true if the resulting input tensors using the provided graph parameters would be
106
    //   the same as the previous input tensors that we have currently stored in the object
107
0
    virtual bool can_reuse(const llm_graph_params & params) {
108
        // returning false here by default will prevent from reusing the graph if the check
109
        //   for the input type has not been implemented yet
110
0
        GGML_UNUSED(params);
111
0
        return false;
112
0
    }
113
protected:
114
    // env: LLAMA_GRAPH_INPUT_DEBUG
115
    int debug = 0;
116
};
117
118
using llm_graph_input_ptr = std::unique_ptr<llm_graph_input_i>;
119
120
class llm_graph_input_embd : public llm_graph_input_i {
121
public:
122
0
    llm_graph_input_embd(int64_t n_embd) : n_embd(n_embd) {}
123
    virtual ~llm_graph_input_embd() = default;
124
125
    void set_input(const llama_ubatch * ubatch) override;
126
127
    bool can_reuse(const llm_graph_params & params) override;
128
129
    ggml_tensor * tokens = nullptr; // I32 [n_batch]
130
    ggml_tensor * embd   = nullptr; // F32 [n_embd, n_batch]
131
132
    const int64_t n_embd = 0;
133
};
134
135
// similar to llm_graph_input_embd but with an additional hidden state input
136
class llm_graph_input_embd_h : public llm_graph_input_i {
137
public:
138
0
    llm_graph_input_embd_h(int64_t n_embd) : n_embd(n_embd) {}
139
    virtual ~llm_graph_input_embd_h() = default;
140
141
    void set_input(const llama_ubatch * ubatch) override;
142
143
    bool can_reuse(const llm_graph_params & params) override;
144
145
    ggml_tensor * tokens = nullptr; // I32 [n_batch]
146
    ggml_tensor * embd   = nullptr; // F32 [n_embd, n_batch]
147
    ggml_tensor * h      = nullptr; // F32 [n_embd, n_batch]
148
149
    const int64_t n_embd = 0;
150
};
151
152
class llm_graph_input_pos : public llm_graph_input_i {
153
public:
154
0
    llm_graph_input_pos(uint32_t n_pos_per_embd) : n_pos_per_embd(n_pos_per_embd) {}
155
    virtual ~llm_graph_input_pos() = default;
156
157
    void set_input(const llama_ubatch * ubatch) override;
158
159
    bool can_reuse(const llm_graph_params & params) override;
160
161
    ggml_tensor * pos = nullptr; // I32 [n_batch]
162
163
    const uint32_t n_pos_per_embd = 1;
164
};
165
166
// temperature tuning, used by llama4
167
class llm_graph_input_attn_temp : public llm_graph_input_i {
168
public:
169
    llm_graph_input_attn_temp(uint32_t n_attn_temp_floor_scale, float f_attn_temp_scale, float f_attn_temp_offset)
170
0
        : n_attn_temp_floor_scale(n_attn_temp_floor_scale), f_attn_temp_scale(f_attn_temp_scale), f_attn_temp_offset(f_attn_temp_offset) {}
171
    virtual ~llm_graph_input_attn_temp() = default;
172
173
    void set_input(const llama_ubatch * ubatch) override;
174
175
    ggml_tensor * attn_scale = nullptr; // F32 [n_batch]
176
177
    const uint32_t n_attn_temp_floor_scale;
178
    const float    f_attn_temp_scale;
179
    const float    f_attn_temp_offset;
180
};
181
182
class llm_graph_input_pos_bucket : public llm_graph_input_i {
183
public:
184
0
    llm_graph_input_pos_bucket(const llama_hparams & hparams) : hparams(hparams) {}
185
    virtual ~llm_graph_input_pos_bucket() = default;
186
187
    void set_input(const llama_ubatch * ubatch) override;
188
189
    ggml_tensor * pos_bucket = nullptr; // I32 [n_batch, n_batch]
190
191
    const llama_hparams hparams;
192
};
193
194
class llm_graph_input_pos_bucket_kv : public llm_graph_input_i {
195
public:
196
    llm_graph_input_pos_bucket_kv(
197
            const llama_hparams & hparams,
198
0
            const llama_kv_cache_context * mctx) : hparams(hparams), mctx(mctx) {}
199
    virtual ~llm_graph_input_pos_bucket_kv() = default;
200
201
    void set_input(const llama_ubatch * ubatch) override;
202
203
    ggml_tensor * pos_bucket = nullptr; // I32 [n_kv, n_batch]
204
205
    const llama_hparams hparams;
206
207
    const llama_kv_cache_context * mctx;
208
};
209
210
class llm_graph_input_out_ids : public llm_graph_input_i {
211
public:
212
    llm_graph_input_out_ids(
213
            const llama_hparams & hparams,
214
            const llama_cparams & cparams,
215
0
            uint32_t n_outputs) : hparams(hparams), cparams(cparams), n_outputs(n_outputs) {}
216
0
    virtual ~llm_graph_input_out_ids() = default;
217
218
    void set_input(const llama_ubatch * ubatch) override;
219
220
    bool can_reuse(const llm_graph_params & params) override;
221
222
    ggml_tensor * out_ids; // I32 [n_outputs]
223
224
    const llama_hparams hparams;
225
    const llama_cparams cparams;
226
227
    const uint32_t n_outputs;
228
};
229
230
class llm_graph_input_mean : public llm_graph_input_i {
231
public:
232
0
    llm_graph_input_mean(const llama_cparams & cparams) : cparams(cparams) {}
233
0
    virtual ~llm_graph_input_mean() = default;
234
235
    void set_input(const llama_ubatch * ubatch) override;
236
237
    ggml_tensor * mean; // F32 [n_batch, n_batch]
238
239
    const llama_cparams cparams;
240
};
241
242
class llm_graph_input_cls : public llm_graph_input_i {
243
public:
244
0
    llm_graph_input_cls(const llama_cparams & cparams, const llm_arch arch) : cparams(cparams), arch(arch) {}
245
0
    virtual ~llm_graph_input_cls() = default;
246
247
    void set_input(const llama_ubatch * ubatch) override;
248
249
    ggml_tensor * cls; // I32 [n_batch]
250
251
    const llama_cparams cparams;
252
    const llm_arch arch;
253
};
254
255
class llm_graph_input_rs : public llm_graph_input_i {
256
public:
257
0
    llm_graph_input_rs(const llama_memory_recurrent_context * mctx) : mctx(mctx) {}
258
    virtual ~llm_graph_input_rs() = default;
259
260
    void set_input(const llama_ubatch * ubatch) override;
261
262
    bool can_reuse(const llm_graph_params & params) override;
263
264
    ggml_tensor * s_copy;  // I32 [n_rs]
265
266
    // views of s_copy, computed once per graph
267
    // and shared across layers which use build_rs
268
    ggml_tensor * s_copy_main;   // I32 [n_seqs]
269
    ggml_tensor * s_copy_extra;  // I32 [n_rs - n_seqs]
270
271
    const llama_memory_recurrent_context * mctx;
272
273
    // used in view offsets, need to match for valid graph reuse
274
    uint32_t head;
275
    int32_t rs_z;
276
};
277
278
class llm_graph_input_cross_embd : public llm_graph_input_i {
279
public:
280
    llm_graph_input_cross_embd(
281
0
            const llama_cross * cross) : cross(cross) {}
282
    virtual ~llm_graph_input_cross_embd() = default;
283
284
    void set_input(const llama_ubatch * ubatch) override;
285
286
    ggml_tensor * cross_embd; // F32 [n_embd, n_outputs_enc]
287
288
    const llama_cross * cross;
289
};
290
291
class llm_graph_input_attn_no_cache : public llm_graph_input_i {
292
public:
293
    llm_graph_input_attn_no_cache(const llama_hparams & hparams, const llama_cparams & cparams) :
294
0
        hparams(hparams),
295
0
        cparams(cparams) {
296
0
    }
297
0
    ~llm_graph_input_attn_no_cache() = default;
298
299
    void set_input(const llama_ubatch * ubatch) override;
300
301
0
    ggml_tensor * get_kq_mask()     const { return self_kq_mask_cnv; }
302
0
    ggml_tensor * get_kq_mask_swa() const { return self_kq_mask_swa_cnv; }
303
304
    // n_tokens == n_batch
305
    ggml_tensor * self_kq_mask         = nullptr; // F32/F16 [n_tokens, n_batch/n_stream, 1, n_stream]
306
    ggml_tensor * self_kq_mask_cnv     = nullptr; //         [n_tokens, n_batch/n_stream, 1, n_stream]
307
    ggml_tensor * self_kq_mask_swa     = nullptr; // F32/F16 [n_tokens, n_batch/n_stream, 1, n_stream]
308
    ggml_tensor * self_kq_mask_swa_cnv = nullptr; //         [n_tokens, n_batch/n_stream, 1, n_stream]
309
310
    const llama_hparams hparams;
311
    const llama_cparams cparams;
312
};
313
314
class llm_graph_input_attn_kv : public llm_graph_input_i {
315
public:
316
    llm_graph_input_attn_kv(
317
            const llama_hparams & hparams,
318
            const llama_cparams & cparams,
319
            const llama_kv_cache_context * mctx) :
320
0
        hparams(hparams),
321
0
        cparams(cparams),
322
0
        mctx(mctx) {
323
0
    }
324
0
    ~llm_graph_input_attn_kv() = default;
325
326
    void set_input(const llama_ubatch * ubatch) override;
327
328
    bool can_reuse(const llm_graph_params & params) override;
329
330
0
    ggml_tensor * get_k_idxs() const { return self_k_idxs; }
331
0
    ggml_tensor * get_v_idxs() const { return self_v_idxs; }
332
333
0
    ggml_tensor * get_kq_mask() const { return self_kq_mask_cnv; }
334
335
    ggml_tensor * self_k_idxs = nullptr; // I64 [n_batch]
336
    ggml_tensor * self_v_idxs = nullptr; // I64 [n_batch] or [n_batch*n_embd_v_gqa]
337
338
    ggml_tensor * self_kq_mask     = nullptr; // F32/F16 [n_kv, n_batch/n_stream, 1, n_stream]
339
    ggml_tensor * self_kq_mask_cnv = nullptr; //         [n_kv, n_batch/n_stream, 1, n_stream]
340
341
    // note: assumes v_rot^2 == I
342
    ggml_tensor * self_k_rot = nullptr;
343
    ggml_tensor * self_v_rot = nullptr;
344
345
    // note: these have to be copies because in order to be able to reuse a graph, its inputs
346
    //       need to carry these parameters with them. otherwise, they can point to freed
347
    //       llm_graph_params from a previous batch, causing stack-use-after-return
348
    const llama_hparams hparams;
349
    const llama_cparams cparams;
350
351
    const llama_kv_cache_context * mctx;
352
};
353
354
// V-less input for the KV cache
355
// ref: https://github.com/ggml-org/llama.cpp/pull/19067
356
class llm_graph_input_attn_k : public llm_graph_input_i {
357
public:
358
    llm_graph_input_attn_k(
359
            const llama_hparams & hparams,
360
            const llama_cparams & cparams,
361
            const llama_kv_cache_context * mctx) :
362
0
        hparams(hparams),
363
0
        cparams(cparams),
364
0
        mctx(mctx) {
365
0
    }
366
0
    ~llm_graph_input_attn_k() = default;
367
368
    void set_input(const llama_ubatch * ubatch) override;
369
370
    bool can_reuse(const llm_graph_params & params) override;
371
372
0
    ggml_tensor * get_k_idxs() const { return self_k_idxs; }
373
374
0
    ggml_tensor * get_kq_mask() const { return self_kq_mask_cnv; }
375
376
    ggml_tensor * self_k_idxs = nullptr; // I64 [n_batch]
377
378
    ggml_tensor * self_kq_mask     = nullptr; // F32/F16 [n_kv, n_batch/n_stream, 1, n_stream]
379
    ggml_tensor * self_kq_mask_cnv = nullptr; //         [n_kv, n_batch/n_stream, 1, n_stream]
380
381
    const llama_hparams hparams;
382
    const llama_cparams cparams;
383
384
    const llama_kv_cache_context * mctx;
385
};
386
387
class llm_graph_input_attn_k_dsa : public llm_graph_input_i {
388
public:
389
    llm_graph_input_attn_k_dsa(
390
            const llama_hparams & hparams,
391
            const llama_cparams & cparams,
392
            const llama_kv_cache_dsa_context * mctx) :
393
0
        hparams(hparams),
394
0
        cparams(cparams),
395
0
        mctx(mctx) {
396
0
    }
397
0
    ~llm_graph_input_attn_k_dsa() = default;
398
399
    void set_input(const llama_ubatch * ubatch) override;
400
401
    bool can_reuse(const llm_graph_params & params) override;
402
403
0
    ggml_tensor * get_k_idxs_mla() const { return self_k_idxs_mla; }
404
0
    ggml_tensor * get_k_idxs_lid() const { return self_k_idxs_lid; }
405
406
0
    ggml_tensor * get_kq_mask_mla() const { return self_kq_mask_mla_cnv; }
407
0
    ggml_tensor * get_kq_mask_lid() const { return self_kq_mask_lid; }
408
409
    ggml_tensor * self_k_idxs_mla = nullptr; // I64 [n_batch]
410
    ggml_tensor * self_k_idxs_lid = nullptr; // I64 [n_batch]
411
412
    ggml_tensor * self_kq_mask_mla     = nullptr; // F32/F16 [n_kv, n_batch/n_stream, 1, n_stream]
413
    ggml_tensor * self_kq_mask_mla_cnv = nullptr; //         [n_kv, n_batch/n_stream, 1, n_stream]
414
    ggml_tensor * self_kq_mask_lid     = nullptr; // F32     [n_kv, n_batch/n_stream, 1, n_stream]
415
    ggml_tensor * self_kq_mask_lid_cnv = nullptr; //         [n_kv, n_batch/n_stream, 1, n_stream]
416
417
    ggml_tensor * self_k_rot_lid = nullptr;
418
419
    const llama_hparams hparams;
420
    const llama_cparams cparams;
421
422
    const llama_kv_cache_dsa_context * mctx;
423
};
424
425
class llm_graph_input_attn_kv_iswa : public llm_graph_input_i {
426
public:
427
    llm_graph_input_attn_kv_iswa(
428
            const llama_hparams & hparams,
429
            const llama_cparams & cparams,
430
            const llama_kv_cache_iswa_context * mctx) :
431
0
        hparams(hparams),
432
0
        cparams(cparams),
433
0
        mctx(mctx) {
434
0
    }
435
0
    ~llm_graph_input_attn_kv_iswa() = default;
436
437
    void set_input(const llama_ubatch * ubatch) override;
438
439
    bool can_reuse(const llm_graph_params & params) override;
440
441
0
    ggml_tensor * get_k_idxs()     const { return self_k_idxs; }
442
0
    ggml_tensor * get_v_idxs()     const { return self_v_idxs; }
443
0
    ggml_tensor * get_k_idxs_swa() const { return self_k_idxs_swa; }
444
0
    ggml_tensor * get_v_idxs_swa() const { return self_v_idxs_swa; }
445
446
0
    ggml_tensor * get_kq_mask()     const { return self_kq_mask_cnv; }
447
0
    ggml_tensor * get_kq_mask_swa() const { return self_kq_mask_swa_cnv; }
448
449
    ggml_tensor * self_k_idxs     = nullptr; // I64 [n_batch]
450
    ggml_tensor * self_v_idxs     = nullptr; // I64 [n_batch] or [n_batch*n_embd_v_gqa]
451
    ggml_tensor * self_k_idxs_swa = nullptr; // I64 [n_batch]
452
    ggml_tensor * self_v_idxs_swa = nullptr; // I64 [n_batch] or [n_batch*n_embd_v_gqa]
453
454
    ggml_tensor * self_kq_mask         = nullptr; // F32/F16 [n_kv, n_batch/n_stream, 1, n_stream]
455
    ggml_tensor * self_kq_mask_cnv     = nullptr; //         [n_kv, n_batch/n_stream, 1, n_stream]
456
    ggml_tensor * self_kq_mask_swa     = nullptr; // F32/F16 [n_kv, n_batch/n_stream, 1, n_stream]
457
    ggml_tensor * self_kq_mask_swa_cnv = nullptr; //         [n_kv, n_batch/n_stream, 1, n_stream]
458
459
    ggml_tensor * self_k_rot = nullptr;
460
    ggml_tensor * self_v_rot = nullptr;
461
462
    ggml_tensor * self_k_rot_swa = nullptr;
463
    ggml_tensor * self_v_rot_swa = nullptr;
464
465
    const llama_hparams hparams;
466
    const llama_cparams cparams;
467
468
    const llama_kv_cache_iswa_context * mctx;
469
};
470
471
// DSV4 raw graph inputs are SWA-only, but their mask may be stream-shaped
472
// so raw K can be concatenated with DSV4 compressed K in one attention op.
473
class llm_graph_input_dsv4_raw {
474
public:
475
    llm_graph_input_dsv4_raw(
476
            const llama_cparams & cparams,
477
            const llama_kv_cache_dsv4_raw_context * mctx) :
478
0
        cparams(cparams),
479
0
        mctx(mctx) {
480
0
    }
481
482
    void set_input(const llama_ubatch * ubatch);
483
484
0
    ggml_tensor * get_k_idxs() const { return self_k_idxs; }
485
0
    ggml_tensor * get_kq_mask() const { return self_kq_mask_cnv; }
486
487
    ggml_tensor * self_k_idxs = nullptr; // I64 [n_batch]
488
489
    ggml_tensor * self_kq_mask     = nullptr; // F32/F16 [n_kv, n_batch/n_stream, 1, n_stream]
490
    ggml_tensor * self_kq_mask_cnv = nullptr; //         [n_kv, n_batch/n_stream, 1, n_stream]
491
492
    ggml_tensor * self_k_rot = nullptr;
493
494
    const llama_cparams cparams;
495
496
    const llama_kv_cache_dsv4_raw_context * mctx;
497
};
498
499
class llm_graph_input_dsv4 : public llm_graph_input_i {
500
public:
501
    struct comp_input {
502
        ggml_tensor * state_pos        = nullptr; // I32 [n_state]
503
        ggml_tensor * state_persist_src_idxs = nullptr; // I32 [n_state_persist]
504
        ggml_tensor * state_persist_dst_idxs = nullptr; // I32 [n_state_persist]
505
        ggml_tensor * state_read_idxs  = nullptr; // I32 [ratio*n_state_write]
506
        ggml_tensor * state_write_idxs = nullptr; // I64 [n_state_write]
507
        ggml_tensor * state_write_pos  = nullptr; // I32 [n_state_write]
508
509
        ggml_tensor * kq_mask    = nullptr; // F32 [n_kv, n_batch/n_stream, 1, n_stream]
510
511
        ggml_tensor * k_rot      = nullptr;
512
    };
513
514
    llm_graph_input_dsv4(
515
            const llama_cparams & cparams,
516
            std::unique_ptr<llm_graph_input_dsv4_raw> inp_raw,
517
            const llama_kv_cache_dsv4_context * mctx) :
518
0
        inp_raw(std::move(inp_raw)),
519
0
        cparams(cparams),
520
0
        mctx(mctx) {
521
0
    }
522
0
    ~llm_graph_input_dsv4() = default;
523
524
    void set_input(const llama_ubatch * ubatch) override;
525
526
    bool can_reuse(const llm_graph_params & params) override;
527
528
0
    llm_graph_input_dsv4_raw * get_raw() const { return inp_raw.get(); }
529
0
    const comp_input & get_csa() const { return inp_csa; }
530
0
    const comp_input & get_hca() const { return inp_hca; }
531
0
    const comp_input & get_lid() const { return inp_lid; }
532
533
    std::unique_ptr<llm_graph_input_dsv4_raw> inp_raw;
534
535
    comp_input inp_csa;
536
    comp_input inp_hca;
537
    comp_input inp_lid;
538
539
    const llama_cparams cparams;
540
541
    const llama_kv_cache_dsv4_context * mctx;
542
};
543
544
class llm_graph_input_attn_cross : public llm_graph_input_i {
545
public:
546
0
    llm_graph_input_attn_cross(const llama_cross * cross) : cross(cross) {}
547
    ~llm_graph_input_attn_cross() = default;
548
549
    void set_input(const llama_ubatch * ubatch) override;
550
551
0
    ggml_tensor * get_kq_mask_cross() const { return cross_kq_mask_cnv; }
552
553
    ggml_tensor * cross_kq_mask     = nullptr; // F32/F16 [n_outputs_enc, n_batch, 1, 1]
554
    ggml_tensor * cross_kq_mask_cnv = nullptr; // F32/F16 [n_outputs_enc, n_batch, 1, 1]
555
556
    const llama_cross * cross = nullptr;
557
};
558
559
class llm_graph_input_mem_hybrid : public llm_graph_input_i {
560
public:
561
    llm_graph_input_mem_hybrid(
562
            const llama_cparams & cparams,
563
            std::unique_ptr<llm_graph_input_attn_kv> inp_attn,
564
            std::unique_ptr<llm_graph_input_rs>      inp_rs,
565
            const llama_memory_hybrid_context *      mctx) :
566
0
        inp_attn(std::move(inp_attn)),
567
0
        inp_rs(std::move(inp_rs)),
568
0
        cparams(cparams),
569
0
        mctx(mctx) { }
570
0
    virtual ~llm_graph_input_mem_hybrid() = default;
571
572
    void set_input(const llama_ubatch * ubatch) override;
573
574
    bool can_reuse(const llm_graph_params & params) override;
575
576
    std::unique_ptr<llm_graph_input_attn_kv> inp_attn;
577
    std::unique_ptr<llm_graph_input_rs>      inp_rs;
578
579
0
    llm_graph_input_attn_kv * get_attn() const { return inp_attn.get(); }
580
0
    llm_graph_input_rs      * get_recr() const { return inp_rs.get(); }
581
582
    const llama_cparams cparams;
583
584
    const llama_memory_hybrid_context * mctx;
585
};
586
587
class llm_graph_input_mem_hybrid_k : public llm_graph_input_i {
588
public:
589
    llm_graph_input_mem_hybrid_k(
590
            const llama_cparams & cparams,
591
            std::unique_ptr<llm_graph_input_attn_k> inp_attn,
592
            std::unique_ptr<llm_graph_input_rs>      inp_rs,
593
            const llama_memory_hybrid_context *      mctx) :
594
0
        inp_attn(std::move(inp_attn)),
595
0
        inp_rs(std::move(inp_rs)),
596
0
        cparams(cparams),
597
0
        mctx(mctx) { }
598
0
    virtual ~llm_graph_input_mem_hybrid_k() = default;
599
600
    void set_input(const llama_ubatch * ubatch) override;
601
602
    bool can_reuse(const llm_graph_params & params) override;
603
604
    std::unique_ptr<llm_graph_input_attn_k> inp_attn;
605
    std::unique_ptr<llm_graph_input_rs>      inp_rs;
606
607
0
    llm_graph_input_attn_k * get_attn() const { return inp_attn.get(); }
608
0
    llm_graph_input_rs      * get_recr() const { return inp_rs.get(); }
609
610
    const llama_cparams cparams;
611
612
    const llama_memory_hybrid_context * mctx;
613
};
614
615
class llm_graph_input_mem_hybrid_iswa : public llm_graph_input_i {
616
public:
617
    llm_graph_input_mem_hybrid_iswa(
618
            const llama_cparams & cparams,
619
            std::unique_ptr<llm_graph_input_attn_kv_iswa> inp_attn,
620
            std::unique_ptr<llm_graph_input_rs>          inp_rs,
621
            const llama_memory_hybrid_iswa_context *     mctx) :
622
0
        inp_attn(std::move(inp_attn)),
623
0
        inp_rs(std::move(inp_rs)),
624
0
        cparams(cparams),
625
0
        mctx(mctx) { }
626
0
    virtual ~llm_graph_input_mem_hybrid_iswa() = default;
627
628
    void set_input(const llama_ubatch * ubatch) override;
629
630
    bool can_reuse(const llm_graph_params & params) override;
631
632
    std::unique_ptr<llm_graph_input_attn_kv_iswa> inp_attn;
633
    std::unique_ptr<llm_graph_input_rs>          inp_rs;
634
635
0
    llm_graph_input_attn_kv_iswa * get_attn() const { return inp_attn.get(); }
636
0
    llm_graph_input_rs           * get_recr() const { return inp_rs.get(); }
637
638
    const llama_cparams cparams;
639
640
    const llama_memory_hybrid_iswa_context * mctx;
641
};
642
643
class llm_graph_input_sampling : public llm_graph_input_i {
644
public:
645
    llm_graph_input_sampling(std::map<llama_seq_id, llama_sampler *> samplers) :
646
0
        samplers(std::move(samplers)) { }
647
0
    virtual ~llm_graph_input_sampling() = default;
648
649
    void set_input(const llama_ubatch * ubatch) override;
650
    bool can_reuse(const llm_graph_params & params) override;
651
652
    std::map<llama_seq_id, llama_sampler *> samplers;
653
};
654
655
//
656
// llm_graph_result
657
//
658
659
// these objects deliver the result from the graph build process back to the llama_context
660
// note that the input tensors created for the graph are referenced here - the goal is to be able to populate their
661
//   specific data, by calling the set_inputs() method
662
// along with the input tensors, the object also provides commonly used outputs tensors, such as logits, embeddings, etc.
663
//   these are used by the llama_context to extact the relevant data, based on the compute parameters
664
665
// callback that allows us to apply custom logic to each tensor (e.g. ggml-alloc, offloading, etc.)
666
using llm_graph_cb = std::function<void(const llama_ubatch & ubatch, ggml_tensor * cur, const char * name, int il)>;
667
668
class llm_graph_result;
669
670
struct llm_graph_params {
671
    llm_arch arch = LLM_ARCH_UNKNOWN;
672
673
    llama_hparams hparams;
674
    llama_cparams cparams;
675
676
    llama_ubatch ubatch; // note: intentionally make a copy
677
678
    llm_graph_type gtype;
679
680
    ggml_backend_sched_t sched;
681
    ggml_backend_t backend_cpu;
682
683
    const llama_adapter_cvec     * cvec;
684
    const llama_adapter_loras    * loras;
685
    const llama_memory_context_i * mctx;
686
    const llama_cross            * cross;
687
688
    std::map<llama_seq_id, llama_sampler *> samplers;
689
690
    static bool samplers_equal(
691
          const std::map<llama_seq_id, llama_sampler *> & lhs,
692
0
          const std::map<llama_seq_id, llama_sampler *> & rhs) {
693
0
        if (lhs.size() != rhs.size()) {
694
0
            return false;
695
0
        }
696
0
        for (const auto & [seq_id, sampler] : lhs) {
697
0
            auto it = rhs.find(seq_id);
698
0
            if (it == rhs.end() || it->second != sampler) {
699
0
                return false;
700
0
            }
701
0
        }
702
0
        return true;
703
0
    }
704
705
    uint32_t n_outputs;
706
707
    llm_graph_cb cb;
708
709
    llm_graph_result * res;
710
711
    // return true if the "other" params would result in a graph with the same topology as with the current params
712
    //   having the same topology allows us to reuse the graph in some cases
713
0
    bool allow_reuse(const llm_graph_params & other) const {
714
        // first check the ubatch
715
0
        bool can_reuse_ubatch =
716
0
            ubatch.equal_seqs() == other.ubatch.equal_seqs() &&
717
0
            ubatch.n_tokens     == other.ubatch.n_tokens &&
718
0
            ubatch.n_seq_tokens == other.ubatch.n_seq_tokens &&
719
0
            ubatch.n_seqs       == other.ubatch.n_seqs &&
720
0
            ubatch.n_seqs_unq   == other.ubatch.n_seqs_unq &&
721
0
            (
722
0
                (!ubatch.token && !other.ubatch.token) ||
723
0
                (!ubatch.embd  && !other.ubatch.embd)  ||
724
0
                (ubatch.token && other.ubatch.token && ubatch.embd && other.ubatch.embd)
725
0
            );
726
727
        // when we split the batch using "equal_seqs" we have to verify that the participating sequences are the same
728
        //   the reason is because the set of attention streams would be different for different sequences
729
0
        if (can_reuse_ubatch && ubatch.equal_seqs()) {
730
0
            if (!ubatch.data) {
731
                // if the old ubatch does not own it's data, then we cannot guarantee that it is still alive, and
732
                //   therefore we cannot perform the sequence id check. normally should never happen
733
0
                can_reuse_ubatch = false;
734
0
            } else {
735
0
                for (uint32_t s = 0; s < ubatch.n_seqs_unq; ++s) {
736
0
                    can_reuse_ubatch &= ubatch.seq_id_unq[s] == other.ubatch.seq_id_unq[s];
737
0
                }
738
0
            }
739
0
        }
740
741
0
        if (!can_reuse_ubatch) {
742
0
            return false;
743
0
        }
744
745
0
        if (n_outputs != other.n_outputs) {
746
0
            return false;
747
0
        }
748
749
0
        if (!samplers_equal(samplers, other.samplers)) {
750
0
            return false;
751
0
        }
752
753
0
        if (samplers.size() > 0) {
754
0
            if (!ubatch.data || !other.ubatch.data) {
755
0
                return false;
756
0
            }
757
758
            // check that the outputs are the same for all samplers
759
0
            for (uint32_t i = 0; i < ubatch.n_tokens; ++i) {
760
0
                if (ubatch.output[i]    != other.ubatch.output[i] ||
761
0
                    ubatch.seq_id[i][0] != other.ubatch.seq_id[i][0]) {
762
0
                    return false;
763
0
                }
764
0
            }
765
0
        }
766
767
        // TODO: https://github.com/ggml-org/llama.cpp/pull/24340#discussion_r3448035248
768
0
        if (cparams.nextn_layer_offset != other.cparams.nextn_layer_offset) {
769
0
            return false;
770
0
        }
771
772
0
        return
773
0
            cparams.embeddings              == other.cparams.embeddings              &&
774
0
            cparams.embeddings_nextn        == other.cparams.embeddings_nextn        &&
775
0
            cparams.embeddings_nextn_masked == other.cparams.embeddings_nextn_masked &&
776
0
            cparams.causal_attn             == other.cparams.causal_attn             &&
777
0
            arch  == other.arch  &&
778
0
            gtype == other.gtype &&
779
0
            cvec  == other.cvec  &&
780
0
            loras == other.loras &&
781
0
            cross == other.cross;
782
0
    }
783
};
784
785
struct llm_graph_fused_node {
786
    llm_fused_op op;
787
    ggml_tensor * tensor;
788
    int il;
789
};
790
791
class llm_graph_result {
792
public:
793
    llm_graph_result(int64_t max_nodes);
794
795
0
    virtual ~llm_graph_result() = default;
796
797
0
    ggml_tensor * get_inp_tokens()  const { return t_inp_tokens; }
798
0
    ggml_tensor * get_logits()      const { return t_logits; }
799
0
    ggml_tensor * get_embd()        const { return t_embd; }
800
0
    ggml_tensor * get_embd_pooled() const { return t_embd_pooled; }
801
0
    ggml_tensor * get_h_nextn()     const { return t_h_nextn; }
802
803
0
    ggml_tensor * get_layer_inp(int il) const { return t_layer_inp[il]; }
804
805
0
    ggml_cgraph  * get_gf()  const { return gf; }
806
0
    ggml_context * get_ctx() const { return ctx_compute.get(); }
807
808
    int64_t get_max_nodes() const;
809
810
    void reset();
811
812
    void set_inputs(const llama_ubatch * ubatch);
813
    void set_outputs(const llm_graph_params & params);
814
815
    // try to update the existing graph result using the new graph parameters in order to reuse it
816
    // this can only be done if we determine that the resulting graph using the new graph parameters
817
    //   would be identical to the existing graph. in that case, we simply have to update the memory
818
    //   contexts of the input tensors of the graph and we can reuse it for another computation
819
    // return true if the graph was updated and can be reused
820
    bool can_reuse(const llm_graph_params & params);
821
822
    llm_graph_input_i * add_input(llm_graph_input_ptr input);
823
824
    void add_fused_node(llm_graph_fused_node result);
825
826
0
    const std::vector<llm_graph_fused_node> & get_fused_nodes() const { return fused_nodes; }
827
828
    void set_params(const llm_graph_params & params);
829
830
    // important graph nodes
831
    ggml_tensor * t_inp_tokens  = nullptr;
832
    ggml_tensor * t_inp_embd    = nullptr; // [n_embd_inp, n_tokens]
833
    ggml_tensor * t_logits      = nullptr;
834
    ggml_tensor * t_embd        = nullptr;
835
    ggml_tensor * t_embd_pooled = nullptr;
836
    ggml_tensor * t_h_nextn     = nullptr; // [n_embd, n_outputs] hidden state before final output norm
837
838
    std::vector<ggml_tensor *> t_layer_inp;
839
840
    std::map<llama_seq_id, ggml_tensor *> t_sampled_logits;
841
    std::map<llama_seq_id, ggml_tensor *> t_candidates;
842
    std::map<llama_seq_id, ggml_tensor *> t_sampled;
843
    std::map<llama_seq_id, ggml_tensor *> t_sampled_probs;
844
845
    std::vector<llm_graph_input_ptr> inputs;
846
    std::vector<llm_graph_fused_node> fused_nodes;
847
848
    ggml_context_ptr ctx_compute;
849
850
    // memory buffers used to evaluate the model
851
    std::vector<uint8_t> buf_compute_meta;
852
853
    ggml_cgraph * gf;
854
855
    int64_t max_nodes;
856
857
private:
858
    // keep a copy of the previous graph parameters
859
    // we will use this to determine whether the graph can be reused by comparing them with the new parameters
860
    // note: these are updated after constructing the new graph
861
    llm_graph_params params;
862
863
    // env: LLAMA_GRAPH_RESULT_DEBUG
864
    int debug = 0;
865
};
866
867
using llm_graph_result_ptr = std::unique_ptr<llm_graph_result>;
868
869
//
870
// llm_graph_context
871
//
872
873
// used in build_rs to properly order writes and avoid unnecessary copies
874
using llm_graph_get_rows_fn = std::function<ggml_tensor * (ggml_context *, ggml_tensor * states, ggml_tensor * ids)>;
875
876
struct llm_graph_qkv {
877
    ggml_tensor * q; // [n_embd_head, n_head,    n_tokens]
878
    ggml_tensor * k; // [n_embd_head, n_head_kv, n_tokens]
879
    ggml_tensor * v; // [n_embd_head, n_head_kv, n_tokens]
880
};
881
882
struct llm_graph_context {
883
    const llm_arch arch;
884
885
    const llama_hparams & hparams;
886
    const llama_cparams & cparams;
887
    const llama_ubatch  & ubatch;
888
889
    const int64_t n_embd;
890
    const int64_t n_layer;
891
    const int64_t n_layer_nextn;
892
    const int64_t n_rot;
893
    const int64_t n_ctx;       // user-specified context size (can be different from n_ctx_train)
894
    const int64_t n_head;
895
    const int64_t n_head_kv;
896
    const int64_t n_embd_head_k;
897
    const int64_t n_embd_k_gqa;
898
    const int64_t n_embd_head_v;
899
    const int64_t n_embd_v_gqa;
900
    const int64_t n_expert;
901
    const int64_t n_expert_used;
902
903
    const float freq_base;
904
    const float freq_scale;
905
    const float ext_factor;
906
    const float attn_factor;
907
    const float beta_fast;
908
    const float beta_slow;
909
    const float norm_eps;
910
    const float norm_rms_eps;
911
912
    const int64_t n_tokens;
913
    const int64_t n_outputs;
914
    const int32_t n_ctx_orig; // yarn
915
916
    const enum llama_pooling_type pooling_type;
917
    const enum llama_rope_type    rope_type;
918
919
    ggml_backend_sched_t sched;
920
921
    ggml_backend_t backend_cpu; // TODO: needed by build_attn_mha, figure out a way to remove?
922
923
    const llama_adapter_cvec     * cvec;
924
    const llama_adapter_loras    * loras;
925
    const llama_memory_context_i * mctx;
926
    const llama_cross            * cross;
927
928
    std::map<llama_seq_id, llama_sampler *> samplers;
929
930
    const llm_graph_cb & cb_func;
931
932
    llm_graph_result * res;
933
934
    ggml_context * ctx0 = nullptr;
935
    ggml_cgraph  * gf   = nullptr;
936
937
    llm_graph_context(const llm_graph_params & params);
938
0
    virtual ~llm_graph_context() = default;
939
940
    void cb(ggml_tensor * cur, const char * name, int il) const;
941
942
    //
943
    // common
944
    //
945
946
    ggml_tensor * build_cvec(
947
             ggml_tensor * cur,
948
                     int   il) const;
949
950
    // do mat_mul, while optionally apply lora and per-tensor scale
951
    ggml_tensor * build_lora_mm(
952
              ggml_tensor * w,
953
              ggml_tensor * cur,
954
              ggml_tensor * w_s = nullptr) const;
955
956
    // do mat_mul_id, while optionally apply lora and per-expert scale
957
    ggml_tensor * build_lora_mm_id(
958
              ggml_tensor * w,   // ggml_tensor * as
959
              ggml_tensor * cur, // ggml_tensor * b
960
              ggml_tensor * ids,
961
              ggml_tensor * w_s = nullptr) const;
962
963
    ggml_tensor * build_norm(
964
             ggml_tensor * cur,
965
             ggml_tensor * mw,
966
             ggml_tensor * mb,
967
           llm_norm_type   type,
968
                     int   il) const;
969
970
971
    // compute Q, K, V projections with optional bias and reshape
972
    // supports both fused wqkv and separate wq/wk/wv paths
973
    llm_graph_qkv build_qkv(
974
        const llama_layer & layer,
975
              ggml_tensor * cur,
976
                  int64_t   n_embd_head,
977
                  int64_t   n_head,
978
                  int64_t   n_head_kv,
979
                      int   il) const;
980
981
    ggml_tensor * build_ffn(
982
             ggml_tensor * cur,
983
             ggml_tensor * up,
984
             ggml_tensor * up_b,
985
             ggml_tensor * up_s,
986
             ggml_tensor * gate,
987
             ggml_tensor * gate_b,
988
             ggml_tensor * gate_s,
989
             ggml_tensor * down,
990
             ggml_tensor * down_b,
991
             ggml_tensor * down_s,
992
             ggml_tensor * act_scales,
993
         llm_ffn_op_type   type_op,
994
       llm_ffn_gate_type   type_gate,
995
                     int   il) const;
996
997
    // build MoE FFN without bias tensors
998
    ggml_tensor * build_moe_ffn(
999
             ggml_tensor * cur,
1000
             ggml_tensor * gate_inp,
1001
             ggml_tensor * up_exps,
1002
             ggml_tensor * gate_exps,
1003
             ggml_tensor * down_exps,
1004
             ggml_tensor * exp_probs_b,
1005
                 int64_t   n_expert,
1006
                 int64_t   n_expert_used,
1007
         llm_ffn_op_type   type_op,
1008
                    bool   norm_w,
1009
                   float   w_scale,
1010
            llama_expert_gating_func_type gating_op,
1011
                     int   il,
1012
             ggml_tensor * probs_in = nullptr,
1013
             ggml_tensor * gate_up_exps = nullptr,
1014
             ggml_tensor * up_exps_s = nullptr,
1015
             ggml_tensor * gate_exps_s = nullptr,
1016
             ggml_tensor * down_exps_s = nullptr,
1017
             ggml_tensor * selected_experts_in = nullptr) const;
1018
1019
    ggml_tensor * build_moe_ffn(
1020
             ggml_tensor * cur,
1021
             ggml_tensor * gate_inp,
1022
             ggml_tensor * gate_inp_b,
1023
             ggml_tensor * up_exps,
1024
             ggml_tensor * up_exps_b,
1025
             ggml_tensor * gate_exps,
1026
             ggml_tensor * gate_exps_b,
1027
             ggml_tensor * down_exps,
1028
             ggml_tensor * down_exps_b,
1029
             ggml_tensor * exp_probs_b,
1030
                 int64_t   n_expert,
1031
                 int64_t   n_expert_used,
1032
         llm_ffn_op_type   type_op,
1033
                    bool   norm_w,
1034
                   float   w_scale,
1035
            llama_expert_gating_func_type gating_op,
1036
                     int   il,
1037
             ggml_tensor * probs_in = nullptr,
1038
             ggml_tensor * gate_up_exps = nullptr,
1039
             ggml_tensor * gate_up_exps_b = nullptr,
1040
             ggml_tensor * up_exps_s = nullptr,
1041
             ggml_tensor * gate_exps_s = nullptr,
1042
             ggml_tensor * down_exps_s = nullptr,
1043
             ggml_tensor * selected_experts_in = nullptr) const;
1044
1045
    //
1046
    // inputs
1047
    //
1048
1049
    ggml_tensor * build_inp_embd(ggml_tensor * tok_embd) const;
1050
    ggml_tensor * build_inp_pos() const;
1051
    ggml_tensor * build_inp_attn_scale() const;
1052
    ggml_tensor * build_inp_out_ids() const;
1053
    ggml_tensor * build_inp_mean() const;
1054
    ggml_tensor * build_inp_cls() const;
1055
1056
    ggml_tensor * build_inp_cross_embd() const;
1057
    ggml_tensor * build_inp_pos_bucket_enc() const;
1058
    ggml_tensor * build_inp_pos_bucket_dec() const;
1059
    ggml_tensor * build_pos_bias(ggml_tensor * pos_bucket, ggml_tensor * attn_rel_b) const;
1060
1061
    //
1062
    // attention
1063
    //
1064
1065
    ggml_tensor * build_attn_mha(
1066
            ggml_tensor * q,       // [n_embd_head_q, n_head_q, n_tokens]
1067
            ggml_tensor * k,       // [n_embd_head_k, n_head_k, n_tokens]
1068
            ggml_tensor * v,       // [n_embd_head_v, n_head_v, n_tokens] (v_trans == false)
1069
            ggml_tensor * kq_b,
1070
            ggml_tensor * kq_mask,
1071
            ggml_tensor * sinks,   // [n_head_q]
1072
            ggml_tensor * v_mla,   // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
1073
                  float   kq_scale,
1074
                    int   il) const;
1075
1076
    llm_graph_input_attn_no_cache * build_attn_inp_no_cache() const;
1077
1078
    ggml_tensor * build_attn(
1079
            llm_graph_input_attn_no_cache * inp,
1080
            ggml_tensor * wo,
1081
            ggml_tensor * wo_b,
1082
            ggml_tensor * wo_s,
1083
            ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
1084
            ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
1085
            ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
1086
            ggml_tensor * kq_b,
1087
            ggml_tensor * sinks, // [n_head_q]
1088
            ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
1089
                  float   kq_scale,
1090
                    int   il) const;
1091
1092
    llm_graph_input_attn_kv * build_attn_inp_kv() const;
1093
1094
    ggml_tensor * build_attn(
1095
            llm_graph_input_attn_kv * inp,
1096
            ggml_tensor * wo,
1097
            ggml_tensor * wo_b,
1098
            ggml_tensor * wo_s,
1099
            ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
1100
            ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
1101
            ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
1102
            ggml_tensor * kq_b,
1103
            ggml_tensor * sinks, // [n_head_q]
1104
            ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v] // TODO: remove
1105
                  float   kq_scale,
1106
                    int   il) const;
1107
1108
    llm_graph_input_attn_k  * build_attn_inp_k() const;
1109
1110
    ggml_tensor * build_attn(
1111
            llm_graph_input_attn_k * inp,
1112
            ggml_tensor * wo,
1113
            ggml_tensor * wo_b,
1114
            ggml_tensor * wo_s,
1115
            ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
1116
            ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
1117
            ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
1118
            ggml_tensor * kq_b,
1119
            ggml_tensor * sinks, // [n_head_q]
1120
            ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
1121
                  float   kq_scale,
1122
                    int   il) const;
1123
1124
    llm_graph_input_attn_k_dsa * build_attn_inp_k_dsa() const;
1125
1126
    ggml_tensor * build_attn(
1127
            llm_graph_input_attn_k_dsa * inp,
1128
            ggml_tensor * wo,
1129
            ggml_tensor * wo_b,
1130
            ggml_tensor * wo_s,
1131
            ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
1132
            ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
1133
            ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
1134
            ggml_tensor * kq_b,
1135
            ggml_tensor * sinks, // [n_head_q]
1136
            ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
1137
            ggml_tensor * top_k, // [n_indexer_top_k, n_tokens]
1138
                  float   kq_scale,
1139
                    int   il) const;
1140
1141
    llm_graph_input_attn_kv_iswa * build_attn_inp_kv_iswa() const;
1142
1143
    llm_graph_input_dsv4 * build_inp_dsv4() const;
1144
1145
    // note: if k_cur or v_cur are not provided, they will not be stored in the memory
1146
    ggml_tensor * build_attn(
1147
            llm_graph_input_attn_kv_iswa * inp,
1148
            ggml_tensor * wo,
1149
            ggml_tensor * wo_b,
1150
            ggml_tensor * wo_s,
1151
            ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
1152
            ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens] optional
1153
            ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens] optional
1154
            ggml_tensor * kq_b,
1155
            ggml_tensor * sinks, // [n_head_q]
1156
            ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
1157
                  float   kq_scale,
1158
                    int   il) const;
1159
1160
    llm_graph_input_attn_cross * build_attn_inp_cross() const;
1161
1162
    ggml_tensor * build_attn(
1163
            llm_graph_input_attn_cross * inp,
1164
            ggml_tensor * wo,
1165
            ggml_tensor * wo_b,
1166
            ggml_tensor * wo_s,
1167
            ggml_tensor * q_cur, // [n_embd_head_q, n_head_q, n_tokens]
1168
            ggml_tensor * k_cur, // [n_embd_head_k, n_head_k, n_tokens]
1169
            ggml_tensor * v_cur, // [n_embd_head_v, n_head_v, n_tokens]
1170
            ggml_tensor * kq_b,
1171
            ggml_tensor * sinks, // [n_head_q]
1172
            ggml_tensor * v_mla, // [n_embd_head_v_mla, n_embd_head_v, n_head_v]
1173
                  float   kq_scale,
1174
                    int   il) const;
1175
1176
    //
1177
    // recurrent
1178
    //
1179
1180
    // TODO: move this implementation to llama_memory_recurrent.
1181
    //       this is analogous to llama_kv_cache::cpy_k / cpy_v
1182
    //       when moving, avoid passing `ggml_cgraph` - only pass `ggml_context`. would likely need to split the
1183
    //         implementation in 2 separate methods. the goal is to avoid calling `ggml_build_forward_expand` in
1184
    //         `llama_memory_recurrent`
1185
    ggml_tensor * build_rs(
1186
            ggml_tensor * s,
1187
            ggml_tensor * state_copy_main,
1188
            ggml_tensor * state_copy_extra,
1189
                int32_t   state_size,
1190
                int32_t   n_seqs,
1191
               uint32_t   n_rs,
1192
               uint32_t   rs_head,
1193
               uint32_t   rs_size,
1194
                int32_t   rs_zero,
1195
            const llm_graph_get_rows_fn & get_state_rows = ggml_get_rows) const;
1196
1197
    llm_graph_input_rs * build_rs_inp() const;
1198
1199
    ggml_tensor * build_rs(
1200
            llm_graph_input_rs * inp,
1201
            ggml_tensor * s,
1202
                int32_t   state_size,
1203
                int32_t   n_seqs,
1204
            const llm_graph_get_rows_fn & get_state_rows = ggml_get_rows) const;
1205
1206
    ggml_tensor * build_rwkv_token_shift_load(
1207
        llm_graph_input_rs * inp,
1208
        const llama_ubatch & ubatch,
1209
                       int   il) const;
1210
1211
    ggml_tensor * build_rwkv_token_shift_store(
1212
             ggml_tensor * token_shift,
1213
      const llama_ubatch & ubatch,
1214
                     int   il) const;
1215
    //
1216
    // hybrid
1217
    //
1218
1219
    llm_graph_input_mem_hybrid * build_inp_mem_hybrid() const;
1220
    llm_graph_input_mem_hybrid_k * build_inp_mem_hybrid_k() const;
1221
1222
    llm_graph_input_mem_hybrid_iswa * build_inp_mem_hybrid_iswa() const;
1223
1224
    //
1225
    // pooling
1226
    //
1227
1228
    void build_pooling(
1229
            ggml_tensor * cls,
1230
            ggml_tensor * cls_b,
1231
            ggml_tensor * cls_out,
1232
            ggml_tensor * cls_out_b,
1233
            ggml_tensor * cls_norm) const;
1234
1235
    //
1236
    // sampling (backend sampling)
1237
    //
1238
1239
    void build_sampling() const;
1240
1241
    //
1242
    // dense (out)
1243
    //
1244
1245
    void build_dense_out(
1246
            ggml_tensor * dense_2,
1247
            ggml_tensor * dense_2_b,
1248
            ggml_tensor * dense_3) const;
1249
};
1250
1251
// TODO: better name
1252
int32_t llama_relative_position_bucket(llama_pos x, llama_pos y, uint64_t n_buckets, bool bidirectional);