|
| 1 | +#include "../llama-model.h" |
| 2 | +#include "../llama-graph.h" |
| 3 | + |
| 4 | +#include "llm_build_bailingmoe2.h" |
| 5 | + |
| 6 | +#include <cmath> |
| 7 | + |
| 8 | + |
| 9 | +llm_build_bailingmoe2::llm_build_bailingmoe2(const llama_model & model, const llm_graph_params & params) : |
| 10 | + llm_graph_context(params) { |
| 11 | + const int64_t n_embd_head = hparams.n_embd_head_v; |
| 12 | + const int64_t n_embd_gqa = hparams.n_embd_v_gqa(); |
| 13 | + |
| 14 | + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 15 | + |
| 16 | + ggml_tensor * cur; |
| 17 | + ggml_tensor * inpL; |
| 18 | + |
| 19 | + inpL = build_inp_embd(model.tok_embd); |
| 20 | + |
| 21 | + // inp_pos - contains the positions |
| 22 | + ggml_tensor * inp_pos = build_inp_pos(); |
| 23 | + |
| 24 | + auto * inp_attn = build_attn_inp_kv(); |
| 25 | + |
| 26 | + ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 27 | + |
| 28 | + const int n_transformer_layers = n_layer - hparams.nextn_predict_layers; |
| 29 | + for (int il = 0; il < n_transformer_layers; ++il) { |
| 30 | + ggml_tensor * inpSA = inpL; |
| 31 | + |
| 32 | + // norm |
| 33 | + cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
| 34 | + cb(cur, "attn_norm", il); |
| 35 | + |
| 36 | + // self_attention |
| 37 | + { |
| 38 | + cur = build_lora_mm(model.layers[il].wqkv, cur); |
| 39 | + cb(cur, "wqkv", il); |
| 40 | + |
| 41 | + ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), |
| 42 | + cur->nb[1], 0 * sizeof(float) * (n_embd)); |
| 43 | + ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
| 44 | + cur->nb[1], 1 * sizeof(float) * (n_embd)); |
| 45 | + ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
| 46 | + cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa)); |
| 47 | + |
| 48 | + Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
| 49 | + cb(Qcur, "Qcur_normed", il); |
| 50 | + |
| 51 | + Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
| 52 | + ext_factor, attn_factor, beta_fast, beta_slow); |
| 53 | + |
| 54 | + Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
| 55 | + cb(Kcur, "Kcur_normed", il); |
| 56 | + |
| 57 | + Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
| 58 | + ext_factor, attn_factor, beta_fast, beta_slow); |
| 59 | + |
| 60 | + cb(Qcur, "Qcur", il); |
| 61 | + cb(Kcur, "Kcur", il); |
| 62 | + cb(Vcur, "Vcur", il); |
| 63 | + |
| 64 | + cur = build_attn(inp_attn, model.layers[il].wo, model.layers[il].bo, Qcur, Kcur, Vcur, nullptr, nullptr, |
| 65 | + nullptr, 1.0f / sqrtf(float(n_embd_head)), il); |
| 66 | + } |
| 67 | + |
| 68 | + if (il == n_transformer_layers - 1 && inp_out_ids) { |
| 69 | + cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
| 70 | + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
| 71 | + } |
| 72 | + |
| 73 | + ggml_tensor * sa_out = ggml_add(ctx0, cur, inpSA); |
| 74 | + cb(sa_out, "sa_out", il); |
| 75 | + |
| 76 | + // MoE branch |
| 77 | + cur = build_norm(sa_out, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
| 78 | + cb(cur, "ffn_norm", il); |
| 79 | + |
| 80 | + if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) { |
| 81 | + cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL, |
| 82 | + model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
| 83 | + cb(cur, "ffn_out", il); |
| 84 | + } else { |
| 85 | + ggml_tensor * moe_out = build_moe_ffn( |
| 86 | + cur, model.layers[il].ffn_gate_inp, model.layers[il].ffn_up_exps, model.layers[il].ffn_gate_exps, |
| 87 | + model.layers[il].ffn_down_exps, model.layers[il].ffn_exp_probs_b, n_expert, n_expert_used, LLM_FFN_SILU, |
| 88 | + hparams.expert_weights_norm, true, hparams.expert_weights_scale, |
| 89 | + (llama_expert_gating_func_type) hparams.expert_gating_func, il); |
| 90 | + cb(moe_out, "ffn_moe_out", il); |
| 91 | + |
| 92 | + { |
| 93 | + ggml_tensor * ffn_shexp = |
| 94 | + build_ffn(cur, model.layers[il].ffn_up_shexp, NULL, NULL, model.layers[il].ffn_gate_shexp, NULL, |
| 95 | + NULL, model.layers[il].ffn_down_shexp, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
| 96 | + cb(ffn_shexp, "ffn_shexp", il); |
| 97 | + |
| 98 | + cur = ggml_add(ctx0, moe_out, ffn_shexp); |
| 99 | + cb(cur, "ffn_out", il); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + cur = ggml_add(ctx0, cur, sa_out); |
| 104 | + |
| 105 | + cur = build_cvec(cur, il); |
| 106 | + cb(cur, "l_out", il); |
| 107 | + |
| 108 | + // input for next layer |
| 109 | + inpL = cur; |
| 110 | + } |
| 111 | + |
| 112 | + cur = inpL; |
| 113 | + |
| 114 | + cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
| 115 | + |
| 116 | + cb(cur, "result_norm", -1); |
| 117 | + res->t_embd = cur; |
| 118 | + |
| 119 | + // lm_head |
| 120 | + cur = build_lora_mm(model.output, cur); |
| 121 | + |
| 122 | + cb(cur, "result_output", -1); |
| 123 | + res->t_logits = cur; |
| 124 | + |
| 125 | + ggml_build_forward_expand(gf, cur); |
| 126 | +} |
0 commit comments