Coverage Report

Created: 2026-07-16 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/modern-bert.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_modern_bert::load_arch_hparams(llama_model_loader & ml) {
4
0
    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
5
0
    if (found_swa && hparams.n_swa > 0) {
6
0
        hparams.swa_type = LLAMA_SWA_TYPE_SYMMETRIC;
7
0
        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
8
0
        uint32_t swa_period = 3;
9
0
        ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
10
0
        hparams.set_swa_pattern(swa_period, true);
11
0
    } else {
12
0
        hparams.swa_type = LLAMA_SWA_TYPE_NONE;
13
0
    }
14
15
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
16
17
    // Some ModernBert derivatives (e.g. IBM Granite Embedding 97m R2) use
18
    // SiLU/SwiGLU in the FFN instead of the default GELU/GeGLU.
19
0
    hparams.llm_ffn_op = LLM_FFN_GEGLU;
20
0
    std::string hidden_act;
21
0
    if (ml.get_key(LLM_KV_HIDDEN_ACT, hidden_act, false)) {
22
0
        hparams.llm_ffn_op = llm_ffn_op_type_from_string(hidden_act, LLM_FFN_GEGLU);
23
0
    }
24
25
0
    switch (hparams.n_layer()) {
26
0
        case 12:
27
0
            type = LLM_TYPE_47M; break; // granite-embedding-small
28
0
        case 22:
29
0
            type = LLM_TYPE_149M; break; // modern-bert-base
30
0
        case 28:
31
0
            type = LLM_TYPE_395M; break; // modern-bert-large
32
0
        default: type = LLM_TYPE_UNKNOWN;
33
0
    }
34
0
}
35
36
0
void llama_model_modern_bert::load_arch_tensors(llama_model_loader &) {
37
0
    LLAMA_LOAD_LOCALS;
38
39
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
40
0
    tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight", 0), {n_embd}, 0);
41
42
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
43
44
0
    for(int i = 0; i < n_layer; ++i) {
45
0
        auto& layer = layers[i];
46
47
0
        if ( i != 0 ) {
48
0
            layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
49
0
        } else{
50
            // layer 0 uses identity
51
0
            layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
52
0
        }
53
54
55
0
        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, 3 * n_embd }, 0);
56
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT,   "weight", i), {n_embd, n_embd}, 0);
57
58
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, 2 * n_ff}, 0);
59
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
60
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
61
0
    }
62
63
0
    cls_out   = create_tensor(tn(LLM_TENSOR_CLS_OUT,  "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
64
0
    cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT,  "bias"),   {hparams.n_cls_out},         TENSOR_NOT_REQUIRED);
65
0
    cls       = create_tensor(tn(LLM_TENSOR_CLS,      "weight"), {n_embd, n_embd},            TENSOR_NOT_REQUIRED);
66
0
    cls_norm  = create_tensor(tn(LLM_TENSOR_CLS_NORM, "weight"), {n_embd},                    TENSOR_NOT_REQUIRED);
67
68
0
}
69
70
0
std::unique_ptr<llm_graph_context> llama_model_modern_bert::build_arch_graph(const llm_graph_params & params) const {
71
0
    return std::make_unique<graph>(*this, params);
72
0
}
73
74
0
llama_model_modern_bert::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
75
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
76
77
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
78
79
0
    ggml_tensor * cur;
80
0
    ggml_tensor * inpL;
81
0
    ggml_tensor * inp_pos = build_inp_pos();
82
83
    // construct input embeddings (token, type, position)
84
0
    inpL = build_inp_embd(model.tok_embd);
85
0
    cb(inpL, "inp_embd", -1);
86
87
    // embed layer norm
88
0
    inpL = build_norm(inpL, model.tok_norm, nullptr, LLM_NORM, 0);
89
0
    cb(inpL, "inp_norm", 0);
90
91
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
92
93
0
    auto * inp_attn = build_attn_inp_no_cache();
94
95
0
    for (int il = 0; il < n_layer; ++il) {
96
0
        const float freq_base_l  = model.get_rope_freq_base(cparams, il);
97
0
        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
98
99
0
        cur = inpL;
100
101
        // attention layer norm
102
0
        if (model.layers[il].attn_norm) {
103
0
            cur = build_norm(inpL,
104
0
                    model.layers[il].attn_norm, NULL,
105
0
                    LLM_NORM, il);
106
0
            cb(cur, "attn_norm", il);
107
0
        }
108
109
        // self attention
110
0
        auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
111
0
                n_embd_head, n_head, n_head_kv, il);
112
113
        // RoPE
114
0
        Qcur = ggml_rope_ext(
115
0
                ctx0, Qcur, inp_pos, nullptr,
116
0
                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
117
0
                ext_factor, attn_factor, beta_fast, beta_slow
118
0
                );
119
120
0
        Kcur = ggml_rope_ext(
121
0
                ctx0, Kcur, inp_pos, nullptr,
122
0
                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
123
0
                ext_factor, attn_factor, beta_fast, beta_slow
124
0
                );
125
126
0
        cb(Qcur, "Qcur", il);
127
0
        cb(Kcur, "Kcur", il);
128
0
        cb(Vcur, "Vcur", il);
129
130
0
        cur = build_attn(inp_attn,
131
0
                    model.layers[il].wo, nullptr, model.layers[il].wo_s,
132
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
133
0
        cb(cur, "kqv_out", il);
134
135
0
        if (il == n_layer - 1 && inp_out_ids) {
136
0
            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
137
0
            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
138
0
        }
139
140
        // re-add the layer input
141
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
142
0
        cb(ffn_inp, "ffn_inp", il);
143
144
        // attention layer norm
145
0
        cur = build_norm(ffn_inp,
146
0
                model.layers[il].ffn_norm, NULL,
147
0
                LLM_NORM, il);
148
0
        cb(cur, "ffn_norm", il);
149
150
0
        cur = build_ffn(cur,
151
0
                model.layers[il].ffn_up,   NULL, NULL,
152
0
                NULL,                      NULL, NULL,
153
0
                model.layers[il].ffn_down, NULL, NULL,
154
0
                NULL,
155
0
                hparams.llm_ffn_op,
156
0
                LLM_FFN_SEQ, il);
157
158
        // attentions bypass the intermediate layer
159
0
        cur = ggml_add(ctx0, cur, ffn_inp);
160
161
        // input for next layer
162
0
        inpL = cur;
163
0
    }
164
165
0
    cur = inpL;
166
167
0
    cur = build_norm(cur,
168
0
            model.output_norm, NULL,
169
0
            LLM_NORM, -1);
170
0
    cb(cur, "final_norm_out", -1);
171
172
0
    res->t_embd = cur;
173
0
    ggml_build_forward_expand(gf, cur);
174
0
}