| 1 | #include "models.h" |
| 2 | |
| 3 | |
| 4 | |
| 5 | llm_build_grovemoe::llm_build_grovemoe(const llama_model & model, const llm_graph_params & params) : |
| 6 | llm_graph_context(params) { |
| 7 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 8 | const int64_t n_chunk_expert = n_expert / hparams.n_group_experts; |
| 9 | |
| 10 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 11 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
| 12 | |
| 13 | ggml_tensor * cur; |
| 14 | ggml_tensor * inpL; |
| 15 | |
| 16 | inpL = build_inp_embd(tok_embd: model.tok_embd); |
| 17 | |
| 18 | // inp_pos - contains the positions |
| 19 | ggml_tensor * inp_pos = build_inp_pos(); |
| 20 | |
| 21 | auto * inp_attn = build_attn_inp_kv(); |
| 22 | |
| 23 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 24 | |
| 25 | for (int il = 0; il < n_layer; ++il) { |
| 26 | ggml_tensor * inpSA = inpL; |
| 27 | |
| 28 | // norm |
| 29 | cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il); |
| 30 | cb(cur, name: "attn_norm" , il); |
| 31 | |
| 32 | // self_attention |
| 33 | { |
| 34 | // compute Q and K and RoPE them |
| 35 | ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur); |
| 36 | cb(cur: Qcur, name: "Qcur" , il); |
| 37 | |
| 38 | ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur); |
| 39 | cb(cur: Kcur, name: "Kcur" , il); |
| 40 | |
| 41 | ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur); |
| 42 | cb(cur: Vcur, name: "Vcur" , il); |
| 43 | |
| 44 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens); |
| 45 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 46 | Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 47 | |
| 48 | Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il); |
| 49 | cb(cur: Qcur, name: "Qcur_normed" , il); |
| 50 | |
| 51 | Qcur = ggml_rope_ext(ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 52 | ext_factor, attn_factor, beta_fast, beta_slow); |
| 53 | |
| 54 | Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il); |
| 55 | cb(cur: Kcur, name: "Kcur_normed" , il); |
| 56 | |
| 57 | Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 58 | ext_factor, attn_factor, beta_fast, beta_slow); |
| 59 | |
| 60 | cb(cur: Qcur, name: "Qcur" , il); |
| 61 | cb(cur: Kcur, name: "Kcur" , il); |
| 62 | cb(cur: Vcur, name: "Vcur" , il); |
| 63 | |
| 64 | cur = build_attn(inp: inp_attn, |
| 65 | wo: model.layers[il].wo, wo_b: model.layers[il].bo, |
| 66 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f / sqrtf(x: float(n_embd_head)), il); |
| 67 | } |
| 68 | |
| 69 | if (il == n_layer - 1 && inp_out_ids) { |
| 70 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 71 | inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids); |
| 72 | } |
| 73 | |
| 74 | ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA); |
| 75 | cb(cur: ffn_inp, name: "ffn_inp" , il); |
| 76 | |
| 77 | // MoE branch |
| 78 | cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il); |
| 79 | cb(cur, name: "ffn_norm" , il); |
| 80 | |
| 81 | ggml_tensor * probs = build_lora_mm(w: model.layers[il].ffn_gate_inp, cur); // [n_expert, n_tokens] |
| 82 | cb(cur: probs, name: "ffn_moe_logits" , il); |
| 83 | |
| 84 | ggml_tensor * moe_out = |
| 85 | build_moe_ffn(cur, |
| 86 | gate_inp: nullptr, |
| 87 | up_exps: model.layers[il].ffn_up_exps, |
| 88 | gate_exps: model.layers[il].ffn_gate_exps, |
| 89 | down_exps: model.layers[il].ffn_down_exps, |
| 90 | exp_probs_b: nullptr, |
| 91 | n_expert, n_expert_used, |
| 92 | type_op: LLM_FFN_SILU, norm_w: true, |
| 93 | scale_w: false, w_scale: 0.0, |
| 94 | gating_op: LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
| 95 | il, |
| 96 | probs_in: probs); |
| 97 | cb(cur: moe_out, name: "ffn_moe_out" , il); |
| 98 | cur = moe_out; |
| 99 | |
| 100 | // TODO: Only do the expert selection and weights once |
| 101 | moe_out = build_moe_ffn(cur, |
| 102 | gate_inp: nullptr, |
| 103 | up_exps: model.layers[il].ffn_up_chexps, |
| 104 | gate_exps: model.layers[il].ffn_gate_chexps, |
| 105 | down_exps: model.layers[il].ffn_down_chexps, |
| 106 | exp_probs_b: nullptr, |
| 107 | n_expert: n_chunk_expert, n_expert_used: n_expert_used > n_chunk_expert ? n_chunk_expert : n_expert_used, |
| 108 | type_op: LLM_FFN_SILU, norm_w: true, |
| 109 | scale_w: false, w_scale: 0.0, |
| 110 | gating_op: LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
| 111 | il, |
| 112 | probs_in: probs); |
| 113 | cb(cur: moe_out, name: "ffn_adj_moe_out" , il); |
| 114 | |
| 115 | cur = ggml_add(ctx: ctx0, a: cur, b: ggml_scale(ctx: ctx0, a: moe_out, s: hparams.expert_group_scale)); |
| 116 | cb(cur, name: "ffn_final_moe_out" , il); |
| 117 | |
| 118 | cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp); |
| 119 | |
| 120 | cur = build_cvec(cur, il); |
| 121 | cb(cur, name: "l_out" , il); |
| 122 | |
| 123 | // input for next layer |
| 124 | inpL = cur; |
| 125 | } |
| 126 | |
| 127 | cur = inpL; |
| 128 | |
| 129 | cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1); |
| 130 | |
| 131 | cb(cur, name: "result_norm" , il: -1); |
| 132 | res->t_embd = cur; |
| 133 | |
| 134 | // lm_head |
| 135 | cur = build_lora_mm(w: model.output, cur); |
| 136 | |
| 137 | cb(cur, name: "result_output" , il: -1); |
| 138 | res->t_logits = cur; |
| 139 | |
| 140 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 141 | } |
| 142 | |