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