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/dream.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_dream::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
6
    // Dream models are primarily 7B with 28 layers
7
0
    switch (hparams.n_layer()) {
8
0
        case 28:
9
0
            type = LLM_TYPE_7B;
10
0
            break;
11
0
        default:
12
0
            type = LLM_TYPE_UNKNOWN;
13
0
    }
14
    // Set non-causal attention for diffusion models
15
0
    hparams.causal_attn = false;
16
0
}
17
18
0
void llama_model_dream::load_arch_tensors(llama_model_loader &) {
19
0
    LLAMA_LOAD_LOCALS;
20
21
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
22
23
    // output
24
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
25
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
26
0
    output_b    = create_tensor(tn(LLM_TENSOR_OUTPUT,      "bias"),   {n_vocab}, TENSOR_NOT_REQUIRED);
27
    // if output is NULL, init from the input tok embed
28
0
    if (output == NULL) {
29
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
30
0
    }
31
32
0
    for (int i = 0; i < n_layer; ++i) {
33
0
        auto & layer = layers[i];
34
35
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
36
37
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
38
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
39
40
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
41
42
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
43
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
44
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
45
0
    }
46
0
}
47
48
0
std::unique_ptr<llm_graph_context> llama_model_dream::build_arch_graph(const llm_graph_params & params) const {
49
0
    return std::make_unique<graph>(*this, params);
50
0
}
51
52
llama_model_dream::graph::graph(const llama_model & model, const llm_graph_params & params) :
53
0
    llm_graph_context(params) {
54
    //copied from qwen2
55
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
56
57
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
58
0
    GGML_ASSERT(n_embd_head == n_rot);
59
60
0
    ggml_tensor * cur;
61
0
    ggml_tensor * inpL;
62
63
0
    inpL = build_inp_embd(model.tok_embd);
64
65
    // inp_pos - contains the positions
66
0
    ggml_tensor * inp_pos = build_inp_pos();
67
68
0
    auto * inp_attn = build_attn_inp_no_cache();
69
70
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
71
72
0
    for (int il = 0; il < n_layer; ++il) {
73
0
        ggml_tensor * inpSA = inpL;
74
75
        // norm
76
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
77
0
        cb(cur, "attn_norm", il);
78
79
        // self-attention
80
0
        {
81
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
82
0
                    n_embd_head, n_head, n_head_kv, il);
83
84
0
            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
85
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
86
87
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
88
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
89
90
0
            cb(Qcur, "Qcur", il);
91
0
            cb(Kcur, "Kcur", il);
92
0
            cb(Vcur, "Vcur", il);
93
94
0
            cur = build_attn(inp_attn,
95
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
96
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
97
0
        }
98
0
        if (il == n_layer - 1 && inp_out_ids) {
99
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
100
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
101
0
        }
102
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
103
0
        cb(ffn_inp, "ffn_inp", il);
104
105
        // feed-forward network
106
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
107
0
        cb(cur, "ffn_norm", il);
108
109
0
        cur = build_ffn(cur,
110
0
            model.layers[il].ffn_up, NULL, NULL,
111
0
            model.layers[il].ffn_gate, NULL, NULL,
112
0
            model.layers[il].ffn_down, NULL, NULL,
113
0
            NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
114
0
        cb(cur, "ffn_out", il);
115
116
0
        cur = ggml_add(ctx0, cur, ffn_inp);
117
118
0
        cur = build_cvec(cur, il);
119
0
        cb(cur, "l_out", il);
120
121
        // input for next layer
122
0
        inpL = cur;
123
0
    }
124
0
    cur = inpL;
125
126
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
127
128
0
    cb(cur, "result_norm", -1);
129
0
    res->t_embd = cur;
130
131
    // lm_head
132
0
    cur = build_lora_mm(model.output, cur, model.output_s);
133
134
0
    cb(cur, "result_output", -1);
135
0
    res->t_logits = cur;
136
137
0
    ggml_build_forward_expand(gf, cur);
138
0
}