Coverage Report

Created: 2026-06-22 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/t5.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_t5::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,      hparams.f_norm_rms_eps);
5
0
    ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
6
7
0
    uint32_t dec_start_token_id;
8
0
    if (ml.get_key(LLM_KV_DECODER_START_TOKEN_ID, dec_start_token_id, false)) {
9
0
        hparams.dec_start_token_id = dec_start_token_id;
10
0
    }
11
12
0
    hparams.dec_n_layer = hparams.n_layer();
13
0
    ml.get_key(LLM_KV_DECODER_BLOCK_COUNT, hparams.dec_n_layer, false);
14
15
0
    switch (hparams.n_layer()) {
16
0
        case 6:  type = LLM_TYPE_60M;  break; // t5-small
17
0
        case 8:  type = LLM_TYPE_80M;  break; // flan-t5-small
18
0
        case 12:
19
0
            switch (hparams.n_ff()) {
20
0
                case 3072: type = LLM_TYPE_220M; break; // t5-base
21
0
                case 2048: type = LLM_TYPE_250M; break; // flan-t5-base
22
0
                default: type = LLM_TYPE_UNKNOWN;
23
0
            } break;
24
0
        case 24:
25
0
            switch (hparams.n_ff()) {
26
0
                case 4096:  type = LLM_TYPE_770M; break; // t5-large
27
0
                case 2816:  type = LLM_TYPE_780M; break; // flan-t5-large
28
0
                case 16384: type = LLM_TYPE_3B;   break; // t5-3b
29
0
                case 5120:  type = LLM_TYPE_3B;   break; // flan-t5-xl
30
0
                case 65536: type = LLM_TYPE_11B;  break; // t5-11b
31
0
                case 10240: type = LLM_TYPE_11B;  break; // flan-t5-xxl
32
0
                default: type = LLM_TYPE_UNKNOWN;
33
0
            } break;
34
0
        default: type = LLM_TYPE_UNKNOWN;
35
0
   }
36
0
}
37
38
0
void llama_model_t5::load_arch_tensors(llama_model_loader &) {
39
0
    LLAMA_LOAD_LOCALS;
40
41
0
    const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
42
43
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
44
45
    // output
46
0
    output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
47
0
    output_norm     = create_tensor(tn(LLM_TENSOR_DEC_OUTPUT_NORM, "weight"), {n_embd}, 0);
48
49
0
    output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
50
    // if output is NULL, init from the input tok embed
51
0
    if (output == NULL) {
52
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
53
0
    }
54
55
    // n_layer:     number of encoder_layers
56
    // dec_n_layer: number of decoder_layers
57
0
    const int dec_n_layer = hparams.dec_n_layer;
58
0
    if (dec_n_layer > n_layer) {
59
0
        layers.resize(dec_n_layer);
60
0
    }
61
62
    // load encoder layers
63
0
    for (int i = 0; i < n_layer; ++i) {
64
0
        auto & layer = layers[i];
65
66
0
        layer.attn_norm_enc  = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM,  "weight", i), {n_embd}, 0);
67
0
        layer.attn_rel_b_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
68
69
0
        layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q,   "weight", i), {n_embd, n_embd_k_gqa}, 0);
70
0
        layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K,   "weight", i), {n_embd, n_embd_k_gqa}, 0);
71
0
        layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V,   "weight", i), {n_embd, n_embd_v_gqa}, 0);
72
0
        layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
73
74
0
        layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
75
0
        layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd,   n_ff}, TENSOR_NOT_REQUIRED);
76
0
        layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
77
0
        layer.ffn_up_enc   = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
78
0
    }
79
80
    // load decoder layers
81
0
    for (int i = 0; i < dec_n_layer; ++i) {
82
0
        auto & layer = layers[i];
83
84
0
        layer.attn_norm  = create_tensor(tn(LLM_TENSOR_DEC_ATTN_NORM,  "weight", i), {n_embd}, 0);
85
0
        layer.attn_rel_b = create_tensor(tn(LLM_TENSOR_DEC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
86
87
0
        layer.wq = create_tensor(tn(LLM_TENSOR_DEC_ATTN_Q,   "weight", i), {n_embd, n_embd_k_gqa}, 0);
88
0
        layer.wk = create_tensor(tn(LLM_TENSOR_DEC_ATTN_K,   "weight", i), {n_embd, n_embd_k_gqa}, 0);
89
0
        layer.wv = create_tensor(tn(LLM_TENSOR_DEC_ATTN_V,   "weight", i), {n_embd, n_embd_v_gqa}, 0);
90
0
        layer.wo = create_tensor(tn(LLM_TENSOR_DEC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
91
92
0
        layer.attn_norm_cross  = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_NORM,  "weight", i), {n_embd}, 0);
93
        // this tensor seems to be unused in HF transformers implementation
94
0
        layer.attn_rel_b_cross = create_tensor(
95
0
            tn(LLM_TENSOR_DEC_CROSS_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL);
96
97
0
        layer.wq_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_Q,   "weight", i), {n_embd, n_embd_k_gqa}, 0);
98
0
        layer.wk_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_K,   "weight", i), {n_embd, n_embd_k_gqa}, 0);
99
0
        layer.wv_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_V,   "weight", i), {n_embd, n_embd_v_gqa}, 0);
100
0
        layer.wo_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
101
102
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_DEC_FFN_NORM, "weight", i), {n_embd}, 0);
103
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_DEC_FFN_GATE, "weight", i), {n_embd,   n_ff}, TENSOR_NOT_REQUIRED);
104
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
105
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
106
0
    }
107
0
}
108
109
0
std::unique_ptr<llm_graph_context> llama_model_t5::build_arch_graph(const llm_graph_params & params) const {
110
0
    switch (params.gtype) {
111
0
        case LLM_GRAPH_TYPE_ENCODER:
112
0
            return std::make_unique<graph<true>>(*this, params);
113
0
        case LLM_GRAPH_TYPE_DEFAULT:
114
0
        case LLM_GRAPH_TYPE_DECODER:
115
0
            return std::make_unique<graph<false>>(*this, params);
116
0
        default:
117
0
            GGML_ABORT("invalid graph type");
118
0
    };
119
0
}
120
121
template <>
122
0
llama_model_t5::graph<false>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
123
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
124
    //const int64_t n_embd_gqa  = hparams.n_embd_v_gqa();
125
126
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
127
128
0
    ggml_tensor * cur;
129
0
    ggml_tensor * inpL;
130
131
0
    inpL = build_inp_embd(model.tok_embd);
132
133
0
    ggml_tensor * embd_enc       = build_inp_cross_embd();
134
0
    ggml_tensor * pos_bucket_dec = build_inp_pos_bucket_dec();
135
136
0
    const int64_t n_outputs_enc = embd_enc->ne[1];
137
138
0
    auto * inp_attn_self  = build_attn_inp_kv();
139
0
    auto * inp_attn_cross = build_attn_inp_cross();
140
141
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
142
143
0
    const int64_t dec_n_layer = hparams.dec_n_layer;
144
145
0
    for (int il = 0; il < dec_n_layer; ++il) {
146
0
        ggml_tensor * inpSA = inpL;
147
148
        // norm
149
0
        cur = build_norm(inpL,
150
0
                model.layers[il].attn_norm, NULL,
151
0
                LLM_NORM_RMS, il);
152
0
        cb(cur, "attn_norm", il);
153
154
        // self-attention
155
0
        {
156
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, n_embd_head, n_head, n_head_kv, il);
157
158
0
            ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b ? model.layers[il].attn_rel_b : model.layers[0].attn_rel_b;
159
0
            ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
160
161
0
            cur = build_attn(inp_attn_self,
162
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
163
0
                    Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
164
0
            cb(cur, "kqv_out", il);
165
0
        }
166
0
        cur = ggml_add(ctx0, cur, inpSA);
167
0
        cb(cur, "cross_inp", il);
168
169
0
        ggml_tensor * inpCA = cur;
170
171
        // norm
172
0
        cur = build_norm(cur,
173
0
                model.layers[il].attn_norm_cross, NULL,
174
0
                LLM_NORM_RMS, il);
175
0
        cb(cur, "attn_norm_cross", il);
176
177
        // cross-attention
178
0
        {
179
0
            ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_cross, cur);
180
0
            cb(Qcur, "Qcur", il);
181
182
0
            ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_cross, embd_enc);
183
0
            cb(Kcur, "Kcur", il);
184
185
0
            ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_cross, embd_enc);
186
0
            cb(Vcur, "Vcur", il);
187
188
0
            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);
189
0
            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_outputs_enc);
190
0
            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
191
192
0
            cur = build_attn(inp_attn_cross,
193
0
                    model.layers[il].wo_cross, nullptr, nullptr,
194
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
195
0
            cb(cur, "kqv_out", il);
196
197
            //ggml_tensor * q =                 ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
198
            //ggml_tensor * k = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
199
200
            //ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
201
            //cb(kq, "kq", il);
202
203
            //kq = ggml_soft_max_ext(ctx0, kq, KQ_mask_cross, 1.0f, hparams.f_max_alibi_bias);
204
            //cb(kq, "kq_soft_max_ext", il);
205
206
            //ggml_tensor * v = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, Vcur, n_embd_gqa, n_outputs_enc)));
207
            //cb(v, "v", il);
208
209
            //ggml_tensor * kqv = ggml_mul_mat(ctx0, ggml_reshape_3d(ctx0, v, n_outputs_enc, n_embd_head, n_head_kv), kq);
210
            //cb(kqv, "kqv", il);
211
212
            //ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
213
            //cb(kqv_merged, "kqv_merged", il);
214
215
            //cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_gqa, n_tokens);
216
            //cb(cur, "kqv_merged_cont", il);
217
218
            //ggml_build_forward_expand(gf, cur);
219
220
            //cur = build_lora_mm(model.layers[il].wo_cross, cur);
221
            //cb(cur, "kqv_out", il);
222
0
        }
223
0
        if (il == dec_n_layer - 1 && inp_out_ids) {
224
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
225
0
            inpCA = ggml_get_rows(ctx0, inpCA, inp_out_ids);
226
0
        }
227
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpCA);
228
0
        cb(ffn_inp, "ffn_inp", il);
229
230
        // feed-forward network
231
0
        {
232
0
            cur = build_norm(ffn_inp,
233
0
                    model.layers[il].ffn_norm, NULL,
234
0
                    LLM_NORM_RMS, il);
235
0
            cb(cur, "ffn_norm", il);
236
237
            // T5 uses relu, flan-T5 uses gelu-gated
238
0
            cur = build_ffn(cur,
239
0
                    model.layers[il].ffn_up,   NULL, NULL,
240
0
                    model.layers[il].ffn_gate, NULL, NULL,
241
0
                    model.layers[il].ffn_down, NULL, NULL,
242
0
                    NULL,
243
0
                    model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_RELU,
244
0
                    model.layers[il].ffn_gate ? LLM_FFN_PAR : LLM_FFN_SEQ,
245
0
                    il);
246
0
            cb(cur, "ffn_out", il);
247
0
        }
248
0
        cur = ggml_add(ctx0, cur, ffn_inp);
249
0
        cb(cur, "ffn_out", il);
250
251
0
        cur = build_cvec(cur, il);
252
0
        cb(cur, "l_out", il);
253
254
        // input for next layer
255
0
        inpL = cur;
256
0
    }
257
0
    cur = inpL;
258
0
    cb(cur, "result_embd", -1);
259
260
0
    cur = build_norm(cur,
261
0
            model.output_norm, NULL,
262
0
            LLM_NORM_RMS, -1);
263
264
0
    cb(cur, "result_norm", -1);
265
0
    res->t_embd = cur;
266
267
    // lm_head
268
0
    cur = build_lora_mm(model.output, cur, model.output_s);
269
270
0
    cb(cur, "result_output", -1);
271
0
    res->t_logits = cur;
272
273
0
    ggml_build_forward_expand(gf, cur);
274
0
}
275
276
template <>
277
0
llama_model_t5::graph<true>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
278
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
279
280
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
281
282
0
    ggml_tensor * cur;
283
0
    ggml_tensor * inpL;
284
285
0
    inpL = build_inp_embd(model.tok_embd);
286
287
0
    ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc();
288
289
0
    auto * inp_attn = build_attn_inp_no_cache();
290
291
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
292
293
0
    for (int il = 0; il < n_layer; ++il) {
294
0
        ggml_tensor * inpSA = inpL;
295
296
        // norm
297
0
        cur = build_norm(inpL,
298
0
                model.layers[il].attn_norm_enc, NULL,
299
0
                LLM_NORM_RMS, il);
300
0
        cb(cur, "attn_norm", il);
301
302
        // self-attention
303
0
        {
304
0
            ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur);
305
0
            cb(Qcur, "Qcur", il);
306
307
0
            ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur);
308
0
            cb(Kcur, "Kcur", il);
309
310
0
            ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur);
311
0
            cb(Vcur, "Vcur", il);
312
313
0
            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);
314
0
            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
315
0
            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
316
317
0
            ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b_enc ? model.layers[il].attn_rel_b_enc : model.layers[0].attn_rel_b_enc;
318
0
            ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
319
320
0
            cur = build_attn(inp_attn,
321
0
                    model.layers[il].wo_enc, nullptr, nullptr,
322
0
                    Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
323
0
            cb(cur, "kqv_out", il);
324
0
        }
325
0
        if (il == n_layer - 1 && inp_out_ids) {
326
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
327
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
328
0
        }
329
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
330
0
        cb(ffn_inp, "ffn_inp", il);
331
332
        // feed-forward network
333
0
        {
334
0
            cur = build_norm(ffn_inp,
335
0
                    model.layers[il].ffn_norm_enc, NULL,
336
0
                    LLM_NORM_RMS, il);
337
0
            cb(cur, "ffn_norm", il);
338
339
            // T5 uses relu, flan-T5 uses gelu-gated
340
0
            cur = build_ffn(cur,
341
0
                    model.layers[il].ffn_up_enc,   NULL, NULL,
342
0
                    model.layers[il].ffn_gate_enc, NULL, NULL,
343
0
                    model.layers[il].ffn_down_enc, NULL, NULL,
344
0
                    NULL,
345
0
                    model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
346
0
                    model.layers[il].ffn_gate_enc ? LLM_FFN_PAR  : LLM_FFN_SEQ,
347
0
                    il);
348
0
            cb(cur, "ffn_out", il);
349
0
        }
350
0
        cur = ggml_add(ctx0, cur, ffn_inp);
351
0
        cb(cur, "ffn_out", il);
352
353
0
        cur = build_cvec(cur, il);
354
0
        cb(cur, "l_out", il);
355
356
        // input for next layer
357
0
        inpL = cur;
358
0
    }
359
0
    cur = inpL;
360
0
    cb(cur, "result_embd", -1);
361
362
0
    cur = build_norm(cur,
363
0
            model.output_norm_enc, NULL,
364
0
            LLM_NORM_RMS, -1);
365
366
0
    cb(cur, "result_norm", -1);
367
0
    res->t_embd = cur;
368
369
0
    ggml_build_forward_expand(gf, cur);
370
0
}