Coverage Report

Created: 2026-06-13 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/rwkv7.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_rwkv7::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS,                hparams.f_norm_eps, false);
5
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,            hparams.f_norm_rms_eps, false);
6
0
    ml.get_key(LLM_KV_WKV_HEAD_SIZE,                          hparams.wkv_head_size);
7
0
    ml.get_key(LLM_KV_ATTENTION_DECAY_LORA_RANK,              hparams.n_lora_decay);
8
0
    ml.get_key(LLM_KV_ATTENTION_ICLR_LORA_RANK,               hparams.n_lora_iclr);
9
0
    ml.get_key(LLM_KV_ATTENTION_VALUE_RESIDUAL_MIX_LORA_RANK, hparams.n_lora_value_res_mix);
10
0
    ml.get_key(LLM_KV_ATTENTION_GATE_LORA_RANK,               hparams.n_lora_gate, false);
11
0
    ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT,                      hparams.token_shift_count, false);
12
13
0
    switch (hparams.n_layer()) {
14
0
        case 12:
15
0
            switch (hparams.n_embd) {
16
0
                case 768: type = LLM_TYPE_190M; break;
17
0
                default: type = LLM_TYPE_UNKNOWN;
18
0
            } break;
19
0
        case 24:
20
0
            switch (hparams.n_embd) {
21
0
                case 1024: type = LLM_TYPE_450M; break;
22
0
                case 2048: type = LLM_TYPE_1_5B; break;
23
0
                default: type = LLM_TYPE_UNKNOWN;
24
0
            } break;
25
0
        case 28:
26
0
            switch (hparams.n_embd) {
27
0
                case 1536: type = LLM_TYPE_1_5B; break;
28
0
                case 3584: type = LLM_TYPE_7B; break;
29
0
                default: type = LLM_TYPE_UNKNOWN;
30
0
            } break;
31
0
        case 32:
32
0
            switch (hparams.n_embd) {
33
0
                case 2560: type = LLM_TYPE_2_9B; break;
34
0
                case 4096: type = LLM_TYPE_7B; break;
35
0
                default: type = LLM_TYPE_UNKNOWN;
36
0
            } break;
37
0
        case 61:
38
0
            switch (hparams.n_embd) {
39
0
                case 4096: type = LLM_TYPE_14B; break;
40
0
                default: type = LLM_TYPE_UNKNOWN;
41
0
            } break;
42
0
        default: type = LLM_TYPE_UNKNOWN;
43
0
    }
44
0
}
45
46
0
void llama_model_rwkv7::load_arch_tensors(llama_model_loader &) {
47
0
    LLAMA_LOAD_LOCALS;
48
49
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
50
51
    // Block 0, LN0
52
0
    tok_norm   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight", 0), {n_embd}, 0);
53
0
    tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias",   0), {n_embd}, 0);
54
55
    // output
56
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
57
0
    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
58
0
    output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
59
60
0
    const int n_lora_decay = hparams.n_lora_decay;
61
0
    const int n_lora_iclr = hparams.n_lora_iclr;
62
0
    const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
63
0
    const int n_lora_gate = hparams.n_lora_gate;
64
0
    const int attn_hidden_size = n_embd;
65
0
    const int ffn_size = hparams.n_ff_arr[0];
66
67
0
    for (int i = 0; i < n_layer; ++i) {
68
0
        auto & layer = layers[i];
69
70
0
        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
71
0
        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i),   {n_embd}, 0);
72
73
0
        layer.attn_norm_2   = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
74
0
        layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i),   {n_embd}, 0);
75
76
0
        layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
77
0
        layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
78
0
        layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
79
80
0
        layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
81
0
        layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
82
0
        layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
83
84
0
        if (i == 0) {
85
            // actually not used
86
0
            layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
87
0
            layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
88
0
            layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
89
0
        } else {
90
0
            layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
91
0
            layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
92
0
            layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
93
0
        }
94
95
0
        layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, 0);
96
0
        layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, 0);
97
98
0
        layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
99
100
0
        layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
101
0
        layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
102
0
        layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
103
104
0
        layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
105
0
        layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
106
0
        layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
107
108
0
        layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
109
0
        layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
110
0
        layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
111
112
0
        layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
113
114
0
        layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
115
0
        layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
116
0
    }
117
118
0
}
119
120
0
std::unique_ptr<llm_graph_context> llama_model_rwkv7::build_arch_graph(const llm_graph_params & params) const {
121
0
    return std::make_unique<graph>(*this, params);
122
0
}
123
124
llama_model_rwkv7::graph::graph(const llama_model & model, const llm_graph_params & params) :
125
0
    llm_build_rwkv7_base(model, params) {
126
0
    GGML_ASSERT(hparams.token_shift_count == 2);
127
128
0
    ggml_tensor * cur;
129
0
    ggml_tensor * inpL;
130
0
    ggml_tensor * v_first = nullptr;
131
132
0
    inpL = build_inp_embd(model.tok_embd);
133
0
    inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, 0);
134
135
0
    auto * rs_inp = build_rs_inp();
136
137
0
    const auto n_embd       = hparams.n_embd;
138
0
    const auto n_seq_tokens = ubatch.n_seq_tokens;
139
0
    const auto n_seqs       = ubatch.n_seqs;
140
141
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
142
143
0
    for (int il = 0; il < n_layer; ++il) {
144
0
        const llama_layer * layer = &model.layers[il];
145
0
        inpL                      = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
146
147
0
        ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
148
149
0
        ggml_tensor * att_shift =
150
0
            ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
151
0
        ggml_tensor * ffn_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1],
152
0
                                               token_shift->nb[2], n_embd * ggml_element_size(token_shift));
153
154
0
        ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
155
0
        cb(att_norm, "attn_norm", il);
156
157
0
        ggml_tensor * x_prev = ggml_concat(
158
0
            ctx0, att_shift,
159
0
            ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0), 1);
160
161
0
        cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
162
163
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
164
0
        cb(ffn_inp, "ffn_inp", il);
165
166
0
        ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
167
0
        cb(ffn_norm, "ffn_norm", il);
168
169
0
        x_prev = ggml_concat(
170
0
            ctx0, ffn_shift,
171
0
            ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0), 1);
172
173
0
        token_shift = ggml_concat(ctx0,
174
0
                                  ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2],
175
0
                                               (n_seq_tokens - 1) * n_embd * ggml_element_size(att_norm)),
176
0
                                  ggml_view_3d(ctx0, ffn_norm, n_embd, 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2],
177
0
                                               (n_seq_tokens - 1) * n_embd * ggml_element_size(ffn_norm)),
178
0
                                  1);
179
0
        ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
180
181
0
        ffn_inp  = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
182
0
        ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
183
0
        x_prev   = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
184
185
0
        if (il == n_layer - 1 && inp_out_ids) {
186
0
            ffn_inp  = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
187
0
            ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
188
0
            x_prev   = ggml_get_rows(ctx0, x_prev, inp_out_ids);
189
0
        }
190
0
        cur = build_rwkv7_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV7);
191
0
        cur = ggml_add(ctx0, cur, ffn_inp);
192
193
0
        cur = build_cvec(cur, il);
194
0
        cb(cur, "l_out", il);
195
196
        // input for next layer
197
0
        inpL = cur;
198
0
    }
199
0
    cur = inpL;
200
0
    cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
201
202
0
    cb(cur, "result_norm", -1);
203
0
    res->t_embd = cur;
204
205
0
    cur = build_lora_mm(model.output, cur, model.output_s);
206
207
0
    cb(cur, "result_output", -1);
208
0
    res->t_logits = cur;
209
210
0
    ggml_build_forward_expand(gf, cur);
211
0
}