1#include "models.h"
2
3llm_build_llada::llm_build_llada(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4 // LLaDA is similar to LLaMA but uses non-causal attention for diffusion
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6
7 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8 GGML_ASSERT(n_embd_head == hparams.n_rot);
9
10 ggml_tensor * cur;
11 ggml_tensor * inpL;
12
13 inpL = build_inp_embd(tok_embd: model.tok_embd);
14
15 // inp_pos - contains the positions
16 ggml_tensor * inp_pos = build_inp_pos();
17
18 // Non-causal attention for diffusion
19 auto * inp_attn = build_attn_inp_no_cache();
20
21 ggml_tensor * inp_out_ids = build_inp_out_ids();
22
23 for (int il = 0; il < n_layer; ++il) {
24 ggml_tensor * inpSA = inpL;
25
26 // norm
27 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il);
28 cb(cur, name: "attn_norm", il);
29
30 // self-attention
31 {
32 // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock
33 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
34 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
35 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
36
37 cb(cur: Qcur, name: "Qcur", il);
38 cb(cur: Kcur, name: "Kcur", il);
39 cb(cur: Vcur, name: "Vcur", il);
40
41 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
42 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
43 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
44
45 Qcur = ggml_rope_ext(ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
46 ext_factor, attn_factor, beta_fast, beta_slow);
47
48 Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
49 ext_factor, attn_factor, beta_fast, beta_slow);
50
51 cb(cur: Qcur, name: "Qcur", il);
52 cb(cur: Kcur, name: "Kcur", il);
53 cb(cur: Vcur, name: "Vcur", il);
54
55 cur = build_attn(inp: inp_attn,
56 wo: model.layers[il].wo, NULL,
57 q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f / sqrtf(x: float(n_embd_head)), il);
58 }
59 if (il == n_layer - 1 && inp_out_ids) {
60 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
61 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
62 }
63 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
64 cb(cur: ffn_inp, name: "ffn_inp", il);
65
66 // feed-forward network
67 cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il);
68 cb(cur, name: "ffn_norm", il);
69
70 cur = build_ffn(cur,
71 up: model.layers[il].ffn_up, NULL, NULL,
72 gate: model.layers[il].ffn_gate, NULL, NULL,
73 down: model.layers[il].ffn_down, NULL, NULL,
74 NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
75 cb(cur, name: "ffn_out", il);
76
77 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
78
79 cur = build_cvec(cur, il);
80 cb(cur, name: "l_out", il);
81
82 // input for next layer
83 inpL = cur;
84 }
85 cur = inpL;
86
87 cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
88
89 cb(cur, name: "result_norm", il: -1);
90 res->t_embd = cur;
91
92 // lm_head
93 cur = build_lora_mm(w: model.output, cur);
94
95 cb(cur, name: "result_output", il: -1);
96 res->t_logits = cur;
97
98 ggml_build_forward_expand(cgraph: gf, tensor: cur);
99}
100