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