Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_eagle3::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6 if (!ml.get_arr(LLM_KV_TARGET_LAYERS, target_layer_ids, false)) {7 throw std::runtime_error("EAGLE3 model requires 'extract_layers' in GGUF metadata");8 }9 if (target_layer_ids.size() != 3) {10 throw std::runtime_error("EAGLE3 requires exactly 3 entries in 'extract_layers'");11 }12 LLAMA_LOG_INFO("%s: EAGLE3 extract_layers = [%d, %d, %d]\n", __func__,13 target_layer_ids[0],14 target_layer_ids[1],15 target_layer_ids[2]);16 17 uint32_t n_embd_tgt = 0;18 19 ml.get_key(LLM_KV_TARGET_HIDDEN_SIZE, n_embd_tgt);20 LLAMA_LOG_INFO("%s: EAGLE3 n_embd_tgt = %u (draft n_embd = %u)\n", __func__, n_embd_tgt, hparams.n_embd);21 22 hparams.n_embd_inp_enc_impl = (uint32_t) target_layer_ids.size() * n_embd_tgt;23 24 // eagle3 norm_before_residual (optional, default false)25 // compatible with Readhat eagle3 speculator model26 ml.get_key(LLM_KV_NORM_BEFORE_RESIDUAL, hparams.norm_before_residual, false);27 if (hparams.norm_before_residual) {28 LLAMA_LOG_INFO("%s: EAGLE3gnorm_before_residual = true\n", __func__);29 }30 31 // eagle3 norm_before_fc (optional, default false)32 // compatible with eagle3.1 (e.g. nvidia/gpt-oss-120b-Eagle3-v3)33 ml.get_key(LLM_KV_NORM_BEFORE_FC, hparams.norm_before_fc, false);34 35 type = LLM_TYPE_UNKNOWN;36}37 38void llama_model_eagle3::load_arch_tensors(llama_model_loader &) {39 LLAMA_LOAD_LOCALS;40 41 const int64_t n_embd_inp = hparams.n_embd_inp_enc();42 const int64_t n_embd_attn_input = 2 * n_embd;43 44 // Get vocab size from the d2t tensor in the GGUF file (optional - only needed if eagle3 has different vocab_size than target)45 // d2t: draft to target vocabulary mapping46 int64_t n_draft_vocab = n_vocab; // Default: same as target vocab47 const struct ggml_tensor * d2t_meta = ml->get_tensor_meta("d2t");48 if (d2t_meta) {49 n_draft_vocab = d2t_meta->ne[0]; // update draft vocab size50 d2t = create_tensor(tn(LLM_TENSOR_D2T), {n_draft_vocab}, 0);51 LLAMA_LOG_INFO("%s: EAGLE3 using d2t mapping (draft_vocab_size = %lld)\n", __func__, (long long)n_draft_vocab);52 } else {53 d2t = nullptr; // no d2t, use default vocab size54 LLAMA_LOG_INFO("%s: EAGLE3 without d2t - sharing same vocab_size with target (vocab_size = %lld)\n", __func__, (long long)n_draft_vocab);55 }56 57 // Feature fusion layer: projects 3 target layers to draft hidden size58 fc = create_tensor(tn(LLM_TENSOR_FC, "weight"), {n_embd_inp, n_embd}, 0);59 60 // RMSNorm on the fused target features (input to fc), only when norm_before_fc is set.61 if (hparams.norm_before_fc) {62 output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd_inp}, 0);63 }64 65 // Output layer (uses draft vocab size)66 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);67 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_draft_vocab}, TENSOR_NOT_REQUIRED);68 69 // Token embeddings (optional - Llama 3.3 70B EAGLE3 has its own)70 const struct ggml_tensor * tok_embd_meta = ml->get_tensor_meta(tn(LLM_TENSOR_TOKEN_EMBD, "weight").str().c_str());71 if (tok_embd_meta) {72 const int64_t n_target_vocab = tok_embd_meta->ne[1];73 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_target_vocab}, 0);74 LLAMA_LOG_INFO("%s: EAGLE3 using its own token_embd (vocab = %lld)\n", __func__, (long long)n_target_vocab);75 }76 77 // Single decoder layer78 for (int i = 0; i < n_layer; ++i) {79 auto & layer = layers[i];80 81 // input_layernorm: applied to token embeddings82 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);83 84 // eagle3 specific: hidden_norm applied to fused target features85 layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);86 87 // Attention takes input_embeds_normed + fused_target_normed as input88 layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd_attn_input, n_embd_head_k * n_head}, 0);89 layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd_attn_input, n_embd_k_gqa}, 0);90 layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd_attn_input, n_embd_v_gqa}, 0);91 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);92 93 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);94 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);95 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);96 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);97 98 // rope_freqs for llama3 rope scaling (optional - only if eagle3 config has rope_scaling)99 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED);100 }101}102 103template <>104ggml_tensor * llama_model_eagle3::graph<true>::build_inp_embd_enc() const {105 ggml_tensor * cur = nullptr;106 107 // Input: Target model features (3 layers concatenated: low, mid, high)108 // Data will be provided via ubatch->embd in encode_eagle3_features()109 auto inp_target = std::make_unique<llm_graph_input_embd>(hparams.n_embd_inp_enc());110 inp_target->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd_inp_enc(), n_tokens);111 ggml_set_input(inp_target->embd);112 113 cur = inp_target->embd;114 cb(cur, "inp_embd", -1);115 116 res->add_input(std::move(inp_target));117 118 return cur;119}120 121// eagle3 Encoder: processes target model features through feature fusion layer122// Input: target_features e.g. [12288, n_tokens] from target model layers low, middle, high123// Output: g_embeddings e.g. [4096, n_tokens] stored in context124template <>125llama_model_eagle3::graph<true>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {126 ggml_tensor * cur = nullptr;127 128 cur = build_inp_embd_enc();129 130 // RMSNorm on the fused target features before fc131 if (hparams.norm_before_fc) {132 cur = build_norm(cur, model.output_norm_enc, NULL, LLM_NORM_RMS, -1);133 cb(cur, "enc_input_norm", -1);134 }135 136 // Feature fusion layer137 cur = build_lora_mm(model.fc, cur);138 cb(cur, "fc_out", -1);139 140 // Output: g_embeddings e.g. [4096, n_tokens]141 // store in t_h_nextn (same as MTP) so can be read via llama_get_embeddings_nextn(ctx_dft)142 ggml_set_output(cur);143 res->t_h_nextn = cur;144 145 ggml_build_forward_expand(gf, cur);146}147 148// eagle3 Decoder: processes draft tokens using g_embeddings from encoder149// Input: draft tokens + g_embeddings from encoder150// Output: draft logits151template <>152llama_model_eagle3::graph<false>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {153 const int64_t n_embd_head = hparams.n_embd_head_v();154 155 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());156 GGML_ASSERT(n_layer == 1); // eagle3 has only one decoder layer157 158 ggml_tensor * cur;159 ggml_tensor * inpL;160 161 // eagle3 Decoder receives:162 // 1. Token embeddings (e.g.from eagle3's own tok_embd for Llama 3.3 70B, or target model for Llama 3.1 8B)163 // 2. g_embeddings from encoder164 auto * tok_embd = model.tok_embd;165 if (model.tok_embd == nullptr) {166 GGML_ASSERT(cparams.ctx_other != nullptr);167 const auto * model_other = llama_get_model(cparams.ctx_other);168 169 GGML_ASSERT(model_other->tok_embd != nullptr && "EAGLE3 decoder requires token embeddings (own or from target model)");170 tok_embd = model_other->tok_embd;171 }172 173 auto inp = std::make_unique<llm_graph_input_embd>(n_embd);174 175 inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);176 ggml_set_input(inp->tokens);177 178 inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, n_tokens);179 ggml_set_input(inp->embd);180 181 ggml_tensor * inp_embd = ggml_get_rows(ctx0, tok_embd, inp->tokens);182 cb(inp_embd, "inp_embd", -1);183 184 ggml_tensor * inp_g = inp->embd;185 cb(inp_g, "inp_g_embeddings", -1);186 187 res->add_input(std::move(inp));188 189 inpL = inp_g;190 191 // inp_pos - contains the positions192 ggml_tensor * inp_pos = build_inp_pos();193 194 auto * inp_attn = build_attn_inp_kv();195 196 const float kq_scale = 1.0f/sqrtf(float(n_embd_head));197 198 // Single decoder layer (il = 0)199 const int il = 0;200 {201 // Apply input_layernorm to the token embeddings202 ggml_tensor * embd_norm = build_norm(inp_embd,203 model.layers[il].attn_norm, NULL,204 LLM_NORM_RMS, il);205 cb(embd_norm, "embd_norm", il);206 207 // Apply hidden_norm to inp_g208 ggml_tensor * g_norm = build_norm(inp_g,209 model.layers[il].attn_norm_2, NULL,210 LLM_NORM_RMS, -1);211 cb(g_norm, "g_norm", il);212 213 // norm_before_residual: determines what goes into the residual connection (compatible with Readhat eagle3 speculator model)214 // - false (default): use raw inp_g for residual215 // - true: use normalized g_norm for residual216 // inpL is the concatenated input (normalized inp_embd + normalized inp_g)217 ggml_tensor * inpSA = hparams.norm_before_residual ? g_norm : inpL;218 219 // Concatenate normalized inp_embd and normalized inp_g220 cur = ggml_concat(ctx0, embd_norm, g_norm, il);221 cb(cur, "concat_embd", il);222 223 // Self-attention with concatenated input224 ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);225 cb(Qcur, "Qcur", il);226 227 ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);228 cb(Kcur, "Kcur", il);229 230 ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);231 cb(Vcur, "Vcur", il);232 233 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);234 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);235 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);236 237 // rope freq factors, returns nullptr if not available238 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);239 240 // RoPE241 Qcur = ggml_rope_ext(242 ctx0, Qcur, inp_pos, rope_factors,243 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,244 ext_factor, attn_factor, beta_fast, beta_slow245 );246 Kcur = ggml_rope_ext(247 ctx0, Kcur, inp_pos, rope_factors,248 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,249 ext_factor, attn_factor, beta_fast, beta_slow250 );251 252 cb(Qcur, "Qcur_rope", il);253 cb(Kcur, "Kcur_rope", il);254 255 cur = build_attn(inp_attn,256 model.layers[il].wo, NULL, nullptr,257 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);258 259 // Add residual and update it260 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);261 cb(ffn_inp, "ffn_inp", il);262 263 // Apply FFN norm to the sum264 cur = build_norm(ffn_inp,265 model.layers[il].ffn_norm, NULL,266 LLM_NORM_RMS, il);267 cb(cur, "post_attn_norm", il);268 269 cur = build_ffn(cur,270 model.layers[il].ffn_up, NULL, NULL,271 model.layers[il].ffn_gate, NULL, NULL,272 model.layers[il].ffn_down, NULL, NULL,273 NULL,274 LLM_FFN_SILU, LLM_FFN_PAR, il);275 cb(cur, "ffn_out", il);276 277 // Output norm with residual278 cur = ggml_add(ctx0, cur, ffn_inp);279 cb(cur, "eagle3_prenorm", il);280 281 inpL = cur;282 }283 284 cur = inpL;285 286 // Output prenorm state (for next token's g_embeddings in autoregressive generation)287 ggml_set_output(cur);288 res->t_h_nextn = cur;289 290 cur = build_norm(cur,291 model.output_norm, NULL,292 LLM_NORM_RMS, -1);293 cb(cur, "result_norm", -1);294 295 // lm_head - projects to draft vocabulary296 // if the draft has no own output projection, inherit the target model's lm_head297 auto * output = model.output;298 if (output == nullptr) {299 GGML_ASSERT(cparams.ctx_other != nullptr);300 const auto * model_other = llama_get_model(cparams.ctx_other);301 302 GGML_ASSERT(model_other->output != nullptr && "EAGLE3 decoder requires an output projection (own or from target model)");303 output = model_other->output;304 }305 cur = build_lora_mm(output, cur);306 307 if (model.d2t) {308 const int64_t n_draft_vocab = cur->ne[0];309 const int64_t n_outputs = cur->ne[1];310 const int64_t n_vocab = (int64_t) model.vocab.n_tokens();311 312 GGML_ASSERT(model.d2t->type == GGML_TYPE_I64);313 GGML_ASSERT(model.d2t->ne[0] == n_draft_vocab);314 315 ggml_tensor * logits = ggml_fill(ctx0, ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, 1, n_vocab, n_outputs), -INFINITY);316 cur = ggml_set_rows(ctx0, logits,317 ggml_reshape_3d(ctx0, cur, 1, n_draft_vocab, n_outputs),318 ggml_reshape_3d(ctx0, model.d2t, n_draft_vocab, 1, 1));319 cur = ggml_reshape_2d(ctx0, cur, n_vocab, n_outputs);320 }321 322 cb(cur, "result_output", -1);323 res->t_logits = cur;324 325 ggml_build_forward_expand(gf, cur);326}327 328std::unique_ptr<llm_graph_context> llama_model_eagle3::build_arch_graph(const llm_graph_params & params) const {329 switch (params.gtype) {330 case LLM_GRAPH_TYPE_ENCODER:331 return std::make_unique<graph<true>>(*this, params);332 case LLM_GRAPH_TYPE_DEFAULT:333 case LLM_GRAPH_TYPE_DECODER:334 return std::make_unique<graph<false>>(*this, params);335 default:336 GGML_ABORT("invalid graph type");337 };338}339 