| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_plamo2::llm_build_plamo2(const llama_model & model, const llm_graph_params & params) : |
| 4 | llm_graph_context_mamba(params) { |
| 5 | ggml_tensor * cur; |
| 6 | ggml_tensor * inpL; |
| 7 | |
| 8 | // {n_embd, n_tokens} |
| 9 | inpL = build_inp_embd(model.tok_embd); |
| 10 | cb(inpL, "embedding_output", -1); |
| 11 | |
| 12 | ggml_tensor * inp_pos = build_inp_pos(); |
| 13 | |
| 14 | auto * inp_hybrid = build_inp_mem_hybrid(); |
| 15 | |
| 16 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 17 | |
| 18 | for (int il = 0; il < n_layer; ++il) { |
| 19 | ggml_tensor * residual = inpL; |
| 20 | |
| 21 | // ggml_graph_add_node(gf, model.layers[il].attn_norm); |
| 22 | // cb(model.layers[il].attn_norm, "attn_norm", il); |
| 23 | |
| 24 | // pre_mixer_norm |
| 25 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
| 26 | |
| 27 | // check if this layer is Mamba or Attention |
| 28 | bool is_mamba_layer = hparams.is_recurrent(il); |
| 29 | |
| 30 | if (is_mamba_layer) { |
| 31 | // PLaMo-2 Mamba layer |
| 32 | cur = build_plamo2_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il); |
| 33 | } else { |
| 34 | // PLaMo-2 Attention layer |
| 35 | cur = build_plamo2_attn_layer(inp_hybrid->get_attn(), inp_pos, cur, model, il); |
| 36 | } |
| 37 | |
| 38 | // post_mixer_norm |
| 39 | cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il); |
| 40 | cb(cur, "attn_post_norm", il); |
| 41 | |
| 42 | // residual connection |
| 43 | cur = ggml_add(ctx0, cur, residual); |
| 44 | cb(cur, "attn_residual", il); |
| 45 | residual = cur; |
| 46 | |
| 47 | // pre-ffn norm |
| 48 | cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
| 49 | cb(cur, "ffn_pre_norm", il); |
| 50 | |
| 51 | // feed-forward network |
| 52 | cur = build_ffn(cur, |
| 53 | model.layers[il].ffn_up, NULL, NULL, |
| 54 | NULL, NULL, NULL, |
| 55 | model.layers[il].ffn_down, NULL, NULL, |
| 56 | NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il); |
| 57 | cb(cur, "ffn_out", il); |
| 58 | |
| 59 | // post ffn norm |
| 60 | cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il); |
nothing calls this directly
no test coverage detected