1#include "models.h"
2
3
4llm_build_arwkv7::llm_build_arwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
5 GGML_ASSERT(n_embd == hparams.n_embd_r());
6
7 ggml_tensor * cur;
8 ggml_tensor * inpL;
9 ggml_tensor * v_first = nullptr;
10
11 inpL = build_inp_embd(tok_embd: model.tok_embd);
12
13 auto * rs_inp = build_rs_inp();
14
15 const auto n_embd = hparams.n_embd;
16 const auto n_seq_tokens = ubatch.n_seq_tokens;
17 const auto n_seqs = ubatch.n_seqs;
18
19 ggml_tensor * inp_out_ids = build_inp_out_ids();
20
21 for (int il = 0; il < n_layer; ++il) {
22 const llama_layer * layer = &model.layers[il];
23 inpL = ggml_reshape_3d(ctx: ctx0, a: inpL, ne0: n_embd, ne1: n_seq_tokens, ne2: n_seqs);
24
25 ggml_tensor * token_shift = build_rwkv_token_shift_load(inp: rs_inp, ubatch, il);
26
27 ggml_tensor * att_norm = build_norm(cur: inpL, mw: layer->attn_norm, mb: layer->attn_norm_b, type: LLM_NORM_RMS, il);
28 cb(cur: att_norm, name: "attn_norm", il);
29
30 ggml_tensor * x_prev = ggml_concat(
31 ctx: ctx0,
32 a: token_shift,
33 b: ggml_view_3d(ctx: ctx0, a: att_norm, ne0: n_embd, ne1: n_seq_tokens - 1, ne2: n_seqs, nb1: att_norm->nb[1], nb2: att_norm->nb[2], offset: 0),
34 dim: 1
35 );
36
37 cur = build_rwkv7_time_mix(inp: rs_inp, cur: att_norm, x_prev, first_layer_value&: v_first, ubatch, il);
38
39 token_shift = ggml_view_3d(ctx: ctx0, a: att_norm, ne0: n_embd, ne1: 1, ne2: n_seqs, nb1: att_norm->nb[1], nb2: att_norm->nb[2], offset: (n_seq_tokens-1)*n_embd*ggml_element_size(tensor: att_norm));
40 ggml_build_forward_expand(cgraph: gf, tensor: build_rwkv_token_shift_store(token_shift, ubatch, il));
41
42 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpL);
43 cb(cur: ffn_inp, name: "ffn_inp", il);
44
45 cur = ggml_reshape_2d(ctx: ctx0, a: cur, ne0: n_embd, ne1: n_tokens);
46 ffn_inp = ggml_reshape_2d(ctx: ctx0, a: ffn_inp, ne0: n_embd, ne1: n_tokens);
47
48 if (il == n_layer - 1 && inp_out_ids) {
49 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
50 ffn_inp = ggml_get_rows(ctx: ctx0, a: ffn_inp, b: inp_out_ids);
51 }
52 // feed-forward network
53 cur = build_norm(cur: ffn_inp,
54 mw: model.layers[il].ffn_norm, NULL,
55 type: LLM_NORM_RMS, il);
56 cb(cur, name: "ffn_norm", il);
57
58 cur = build_ffn(cur,
59 up: model.layers[il].ffn_up, NULL, NULL,
60 gate: model.layers[il].ffn_gate, NULL, NULL,
61 down: model.layers[il].ffn_down, NULL, NULL,
62 NULL,
63 type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
64 cb(cur, name: "ffn_out", il);
65
66 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
67
68 cur = build_cvec(cur, il);
69 cb(cur, name: "l_out", il);
70
71 // input for next layer
72 inpL = cur;
73 }
74 cur = inpL;
75 cur = build_norm(cur, mw: model.output_norm, mb: model.output_norm_b, type: LLM_NORM_RMS, il: -1);
76
77 cb(cur, name: "result_norm", il: -1);
78 res->t_embd = cur;
79
80 cur = build_lora_mm(w: model.output, cur);
81
82 cb(cur, name: "result_output", il: -1);
83 res->t_logits = cur;
84
85 ggml_build_forward_expand(cgraph: gf, tensor: cur);
86}
87