| 4 | |
| 5 | |
| 6 | llm_build_lfm2::llm_build_lfm2(const llama_model & model, const llm_graph_params & params) : |
| 7 | llm_graph_context(params), |
| 8 | model(model) { |
| 9 | ggml_tensor * cur = build_inp_embd(model.tok_embd); |
| 10 | cb(cur, "model.embed_tokens", -1); |
| 11 | |
| 12 | ggml_build_forward_expand(gf, cur); |
| 13 | |
| 14 | ggml_tensor * inp_pos = build_inp_pos(); |
| 15 | auto * inp_hybrid = build_inp_mem_hybrid(); |
| 16 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 17 | |
| 18 | for (int il = 0; il < n_layer; ++il) { |
| 19 | const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead); |
| 20 | |
| 21 | auto * prev_cur = cur; |
| 22 | cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
| 23 | cb(cur, "model.layers.{}.operator_norm", il); |
| 24 | |
| 25 | cur = hparams.is_recurrent(il) ? build_shortconv_block(cur, inp_hybrid->get_recr(), il) : |
| 26 | build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il); |
| 27 | |
| 28 | if (il == n_layer - 1 && inp_out_ids) { |
| 29 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
| 30 | prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids); |
| 31 | } |
| 32 | |
| 33 | cur = ggml_add(ctx0, prev_cur, cur); |
| 34 | |
| 35 | auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
| 36 | cb(ffn_norm_out, "model.layers.{}.ffn_norm", il); |
| 37 | |
| 38 | ggml_tensor * ffn_out = |
| 39 | is_moe_layer ? build_moe_feed_forward(ffn_norm_out, il) : build_dense_feed_forward(ffn_norm_out, il); |
| 40 | cb(ffn_norm_out, "model.layers.{}.ffn_out", il); |
| 41 | |
| 42 | cur = ggml_add(ctx0, cur, ffn_out); |
| 43 | } |
| 44 | |
| 45 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
| 46 | cb(cur, "result_norm", -1); |
| 47 | res->t_embd = cur; |
| 48 | |
| 49 | cur = build_lora_mm(model.output, cur); |
| 50 | cb(cur, "result_output", -1); |
| 51 | |
| 52 | res->t_logits = cur; |
| 53 | |
| 54 | ggml_build_forward_expand(gf, cur); |
| 55 | } |
| 56 | |
| 57 | ggml_tensor * llm_build_lfm2::build_moe_feed_forward(ggml_tensor * cur, int il) const { |
| 58 | return build_moe_ffn(cur, |
nothing calls this directly
no test coverage detected