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