llm_build_llada_moe.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "../llama-model.h"
  2. #include "../llama-graph.h"
  3. #include "llm_build_llada_moe.h"
  4. #include <cmath>
  5. llm_build_llada_moe::llm_build_llada_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6. const int64_t n_embd_head = hparams.n_embd_head_v;
  7. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9. ggml_tensor * cur;
  10. ggml_tensor * inpL;
  11. inpL = build_inp_embd(model.tok_embd);
  12. // inp_pos - contains the positions
  13. ggml_tensor * inp_pos = build_inp_pos();
  14. auto * inp_attn = build_attn_inp_no_cache();
  15. ggml_tensor * inp_out_ids = build_inp_out_ids();
  16. for (int il = 0; il < n_layer; ++il) {
  17. ggml_tensor * inpSA = inpL;
  18. // norm
  19. cur = build_norm(inpL,
  20. model.layers[il].attn_norm, NULL,
  21. LLM_NORM_RMS, il);
  22. cb(cur, "attn_norm", il);
  23. // self_attention
  24. {
  25. // compute Q and K and RoPE them
  26. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  27. cb(Qcur, "Qcur", il);
  28. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  29. cb(Kcur, "Kcur", il);
  30. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  31. cb(Vcur, "Vcur", il);
  32. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  33. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  34. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  35. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  36. cb(Qcur, "Qcur_normed", il);
  37. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  38. cb(Kcur, "Kcur_normed", il);
  39. Qcur = ggml_rope_ext(
  40. ctx0, Qcur, inp_pos, nullptr,
  41. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  42. ext_factor, attn_factor, beta_fast, beta_slow
  43. );
  44. Kcur = ggml_rope_ext(
  45. ctx0, Kcur, inp_pos, nullptr,
  46. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  47. ext_factor, attn_factor, beta_fast, beta_slow
  48. );
  49. cb(Qcur, "Qcur", il);
  50. cb(Kcur, "Kcur", il);
  51. cb(Vcur, "Vcur", il);
  52. cur = build_attn(inp_attn,
  53. model.layers[il].wo, NULL,
  54. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  55. }
  56. ;
  57. if (il == n_layer - 1 && inp_out_ids) {
  58. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  59. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  60. }
  61. ;
  62. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  63. cb(ffn_inp, "ffn_inp", il);
  64. // MoE branch
  65. cur = build_norm(ffn_inp,
  66. model.layers[il].ffn_norm, NULL,
  67. LLM_NORM_RMS, il);
  68. cb(cur, "ffn_norm", il);
  69. cur = build_moe_ffn(cur,
  70. model.layers[il].ffn_gate_inp,
  71. model.layers[il].ffn_up_exps,
  72. model.layers[il].ffn_gate_exps,
  73. model.layers[il].ffn_down_exps,
  74. nullptr,
  75. n_expert, n_expert_used,
  76. LLM_FFN_SILU, false,
  77. false, 0.0,
  78. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  79. il);
  80. cb(cur, "ffn_moe_out", il);
  81. cur = ggml_add(ctx0, cur, ffn_inp);
  82. cur = build_cvec(cur, il);
  83. cb(cur, "l_out", il);
  84. // input for next layer
  85. inpL = cur;
  86. }
  87. ;
  88. cur = inpL;
  89. cur = build_norm(cur,
  90. model.output_norm, NULL,
  91. LLM_NORM_RMS, -1);
  92. cb(cur, "result_norm", -1);
  93. res->t_embd = cur;
  94. // lm_head
  95. cur = build_lora_mm(model.output, cur);
  96. cb(cur, "result_output", -1);
  97. res->t_logits = cur;
  98. ggml_build_forward_expand(gf, cur);
  99. }
  100. ;