Coverage Report

Created: 2025-11-24 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/glm4.cpp
Line
Count
Source
1
#include "models.h"
2
3
4
5
0
llm_build_glm4::llm_build_glm4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
6
0
    const int64_t n_embd_head = hparams.n_embd_head_v;
7
0
    const int64_t n_embd_gqa  = hparams.n_embd_v_gqa();
8
9
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
10
11
0
    ggml_tensor * cur;
12
0
    ggml_tensor * inpL;
13
14
0
    inpL = build_inp_embd(model.tok_embd);
15
16
    // inp_pos - contains the positions
17
0
    ggml_tensor * inp_pos = build_inp_pos();
18
19
0
    auto * inp_attn = build_attn_inp_kv();
20
21
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
22
23
0
    for (int il = 0; il < n_layer; ++il) {
24
0
        ggml_tensor * inpSA = inpL;
25
26
        // Pre-attention norm
27
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
28
0
        cb(cur, "attn_norm", il);
29
30
        // self-attention
31
0
        {
32
0
            ggml_tensor * Qcur = nullptr;
33
0
            ggml_tensor * Kcur = nullptr;
34
0
            ggml_tensor * Vcur = nullptr;
35
36
0
            if (model.layers[il].wqkv == nullptr) {
37
0
                Qcur = build_lora_mm(model.layers[il].wq, cur);
38
0
                if (model.layers[il].bq) {
39
0
                    Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
40
0
                }
41
0
                Kcur = build_lora_mm(model.layers[il].wk, cur);
42
0
                if (model.layers[il].bk) {
43
0
                    Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
44
0
                }
45
0
                Vcur = build_lora_mm(model.layers[il].wv, cur);
46
0
                if (model.layers[il].bv) {
47
0
                    Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
48
0
                }
49
0
                Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
50
0
                Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
51
0
                Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
52
0
            } else {
53
0
                cur = build_lora_mm(model.layers[il].wqkv, cur);
54
0
                cb(cur, "wqkv", il);
55
0
                if (model.layers[il].bqkv) {
56
0
                    cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
57
0
                    cb(cur, "bqkv", il);
58
0
                }
59
0
                Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), cur->nb[1],
60
0
                                    0 * sizeof(float) * (n_embd));
61
0
                Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
62
0
                                    cur->nb[1], 1 * sizeof(float) * (n_embd));
63
0
                Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
64
0
                                    cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa));
65
0
            }
66
0
            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
67
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
68
69
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
70
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
71
72
0
            cb(Qcur, "Qcur", il);
73
0
            cb(Kcur, "Kcur", il);
74
0
            cb(Vcur, "Vcur", il);
75
76
0
            cur = build_attn(inp_attn,
77
0
                    model.layers[il].wo, NULL,
78
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
79
0
        }
80
0
        if (il == n_layer - 1 && inp_out_ids) {
81
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
82
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
83
0
        }
84
        // Post-attention norm (new!)
85
0
        cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
86
0
        cb(cur, "post_attn_norm", il);
87
88
        // Add the input (residual connection after post-attention norm)
89
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
90
0
        cb(ffn_inp, "ffn_inp", il);
91
92
        // FF
93
0
        {
94
            // Pre-MLP norm
95
0
            cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
96
0
            cb(cur, "ffn_norm", il);
97
98
            // MLP
99
0
            cur = build_ffn(cur,
100
0
                    model.layers[il].ffn_up, NULL, NULL,
101
0
                    NULL, NULL, NULL,
102
0
                    model.layers[il].ffn_down, NULL, NULL,
103
0
                    NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
104
0
            cb(cur, "ffn_out", il);
105
106
            // Post-MLP norm
107
0
            cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
108
0
            cb(cur, "post_mlp_norm", il);
109
0
        }
110
        // Add residual connection after post-MLP norm
111
0
        inpL = ggml_add(ctx0, cur, ffn_inp);
112
0
        cb(inpL, "l_out", il);
113
0
    }
114
    // Final norm
115
0
    cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
116
117
0
    cb(cur, "result_norm", -1);
118
0
    res->t_embd = cur;
119
120
    // Output projection
121
0
    cur = build_lora_mm(model.output, cur);
122
123
0
    cb(cur, "result_output", -1);
124
0
    res->t_logits = cur;
125
126
0
    ggml_build_forward_expand(gf, cur);
127
0
}