| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_rwkv6::llm_build_rwkv6(const llama_model & model, const llm_graph_params & params) : |
| 4 | llm_build_rwkv6_base(model, params) { |
| 5 | GGML_ASSERT(hparams.token_shift_count == 2); |
| 6 | |
| 7 | ggml_tensor * cur; |
| 8 | ggml_tensor * inpL; |
| 9 | |
| 10 | inpL = build_inp_embd(model.tok_embd); |
| 11 | inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1); |
| 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(ctx0, inpL, n_embd, n_seq_tokens, n_seqs); |
| 24 | |
| 25 | ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il); |
| 26 | |
| 27 | ggml_tensor * att_shift = |
| 28 | ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0); |
| 29 | ggml_tensor * ffn_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], |
| 30 | token_shift->nb[2], n_embd * ggml_element_size(token_shift)); |
| 31 | |
| 32 | ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il); |
| 33 | cb(att_norm, "attn_norm", il); |
| 34 | |
| 35 | ggml_tensor * x_prev = ggml_concat( |
| 36 | ctx0, att_shift, |
| 37 | ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0), 1); |
| 38 | |
| 39 | cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il); |
| 40 | |
| 41 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL); |
| 42 | cb(ffn_inp, "ffn_inp", il); |
| 43 | |
| 44 | ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il); |
| 45 | cb(ffn_norm, "ffn_norm", il); |
| 46 | |
| 47 | x_prev = ggml_concat( |
| 48 | ctx0, ffn_shift, |
| 49 | ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0), 1); |
| 50 | |
| 51 | token_shift = ggml_concat(ctx0, |
| 52 | ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2], |
| 53 | (n_seq_tokens - 1) * n_embd * ggml_element_size(att_norm)), |
| 54 | ggml_view_3d(ctx0, ffn_norm, n_embd, 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], |
| 55 | (n_seq_tokens - 1) * n_embd * ggml_element_size(ffn_norm)), |
| 56 | 1); |
| 57 | ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il)); |
| 58 | |
| 59 | ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens); |
| 60 | ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens); |
nothing calls this directly
no test coverage detected