llm_build_rwkv6qwen2.cpp 2.9 KB

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