| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_afmoe::llm_build_afmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 4 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 5 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 6 | |
| 7 | ggml_tensor * cur; |
| 8 | ggml_tensor * inpL; |
| 9 | |
| 10 | inpL = build_inp_embd(model.tok_embd); |
| 11 | |
| 12 | // MuP scaling: embeddings * sqrt(hidden_size) |
| 13 | // mup_enabled = true, hidden_size = 1024, scale = 32.0 |
| 14 | inpL = ggml_scale(ctx0, inpL, sqrtf(float(n_embd))); |
| 15 | cb(inpL, "inp_embd_scaled", -1); |
| 16 | |
| 17 | // inp_pos - contains the positions |
| 18 | ggml_tensor * inp_pos = build_inp_pos(); |
| 19 | auto * inp_attn = build_attn_inp_kv_iswa(); |
| 20 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 21 | |
| 22 | const float kq_scale = 1.0f/sqrtf(float(n_embd_head)); |
| 23 | |
| 24 | for (int il = 0; il < n_layer; ++il) { |
| 25 | const float freq_base_l = model.get_rope_freq_base (cparams, il); |
| 26 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
| 27 | |
| 28 | ggml_tensor * inpSA = inpL; |
| 29 | |
| 30 | // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous |
| 31 | const bool use_rope = hparams.n_no_rope_layer_step > 0 && |
| 32 | (il + 1) % hparams.n_no_rope_layer_step != 0; |
| 33 | |
| 34 | // dual attention normalization (pre) |
| 35 | cur = build_norm(inpL, |
| 36 | model.layers[il].attn_norm, NULL, |
| 37 | LLM_NORM_RMS, il); |
| 38 | cb(cur, "attn_norm", il); |
| 39 | |
| 40 | // self-attention |
| 41 | { |
| 42 | ggml_tensor * attn_inp = cur; // save input for gate computation |
| 43 | |
| 44 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
| 45 | cb(Qcur, "Qcur", il); |
| 46 | |
| 47 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
| 48 | cb(Kcur, "Kcur", il); |
| 49 | |
| 50 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
| 51 | cb(Vcur, "Vcur", il); |
| 52 | |
| 53 | // compute gate from input |
| 54 | ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp); |
| 55 | cb(gate, "attn_gate_proj", il); |
| 56 | |
| 57 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
| 58 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
| 59 | |
| 60 | // Q/K normalization |
nothing calls this directly
no test coverage detected