Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03k
1#include "models.h"2 3void llama_model_nemotron_h::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);5 ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);6 ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);7 ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);8 ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);9 10 // NextN/MTP: optional draft head appended as extra trailing block(s)11 ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);12 GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_all");13 14 // A layer is recurrent IFF the n_head_kv value is set to 0 and15 // the n_ff value is set to 0. Appended MTP blocks are dense (non-recurrent)16 for (uint32_t i = 0; i < hparams.n_layer_all; ++i) {17 hparams.is_recr_impl[i] = i < hparams.n_layer() && hparams.n_head_kv(i) == 0 && hparams.n_ff(i) == 0;18 }19 20 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);21 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); // MTP head final_layernorm22 23 ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);24 ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);25 ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared, false);26 ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);27 ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false);28 ml.get_key(LLM_KV_MOE_LATENT_SIZE, hparams.moe_latent_size, false);29 30 switch (hparams.n_layer()) {31 case 52: type = LLM_TYPE_31B_A3_5B; break; // Nemotron-H_MOE 31B32 case 56: type = LLM_TYPE_9B; break;33 case 88: type = LLM_TYPE_120B_A12B; break;34 default: type = LLM_TYPE_UNKNOWN;35 }36}37 38void llama_model_nemotron_h::load_arch_tensors(llama_model_loader & ml) {39 LLAMA_LOAD_LOCALS;40 41 const bool mtp_only = hparams.n_layer_nextn > 0 && ml.get_weight("blk.0.attn_norm.weight") == nullptr;42 const int trunk_flags = mtp_only ? TENSOR_NOT_REQUIRED : 0;43 const int mtp_flags = !ml.load_mtp ? TENSOR_SKIP : 0;44 45 // mamba2 Mixer SSM params46 // NOTE: int64_t for tensor dimensions47 const int64_t d_conv = hparams.ssm_d_conv;48 const int64_t d_inner = hparams.ssm_d_inner;49 const int64_t d_state = hparams.ssm_d_state;50 const int64_t n_ssm_head = hparams.ssm_dt_rank;51 const int64_t n_group = hparams.ssm_n_group;52 const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;53 const int64_t moe_n_embd = hparams.moe_latent_size > 0 ? hparams.moe_latent_size : n_embd;54 55 // embeddings56 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);57 58 // output59 {60 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);61 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);62 // if output is NULL, init from the input tok embed, duplicated to allow offloading63 if (output == NULL) {64 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);65 }66 }67 68 for (int i = 0; i < n_layer; ++i) {69 auto & layer = layers[i];70 71 // all blocks use the attn norm72 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, trunk_flags);73 74 if (hparams.is_recr(i)) {75 // ssm layers76 layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, trunk_flags);77 78 layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, trunk_flags);79 layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);80 81 layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, trunk_flags);82 83 // no "weight" suffix for these84 layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, trunk_flags);85 layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, trunk_flags);86 87 layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, trunk_flags);88 89 // out_proj90 layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, trunk_flags);91 } else if (hparams.n_ff(i) == 0) {92 // attention layers (with optional bias)93 const int64_t n_head_i = hparams.n_head(i);94 const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);95 const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);96 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head_i, n_embd_k_gqa_i, n_embd_v_gqa_i, trunk_flags);97 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, trunk_flags);98 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);99 } else {100 if (n_expert != 0) {101 const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;102 const int64_t n_ff_shexp = hparams.n_ff_shexp;103 104 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, trunk_flags);105 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert }, trunk_flags);106 107 // MoE branch108 layer.ffn_latent_down = create_tensor(tn(LLM_TENSOR_FFN_LATENT_DOWN, "weight", i), {n_embd, moe_n_embd}, TENSOR_NOT_REQUIRED);109 layer.ffn_latent_up = create_tensor(tn(LLM_TENSOR_FFN_LATENT_UP, "weight", i), {moe_n_embd, n_embd}, TENSOR_NOT_REQUIRED);110 111 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, moe_n_embd, n_expert}, trunk_flags);112 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {moe_n_embd, n_ff_exp, n_expert}, trunk_flags);113 114 // Shared expert branch115 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, trunk_flags);116 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, trunk_flags);117 118 } else {119 // mlp layers120 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { hparams.n_ff(i), n_embd}, trunk_flags);121 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, hparams.n_ff(i)}, trunk_flags);122 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);123 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {hparams.n_ff(i)}, TENSOR_NOT_REQUIRED);124 }125 }126 }127 128 // NextN/MTP draft head: each predict layer folds an attention sub-layer and a MoE129 // sub-layer into a single trailing block130 for (int i = n_layer; i < n_layer_all; ++i) {131 auto & layer = layers[i];132 133 const int64_t n_head_i = hparams.n_head(i);134 const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);135 const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);136 const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;137 const int64_t n_ff_shexp = hparams.n_ff_shexp;138 139 // NextN input-fusion tensors140 layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), {n_embd}, mtp_flags);141 layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), {n_embd}, mtp_flags);142 layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2*n_embd, n_embd}, mtp_flags);143 layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), {n_embd}, mtp_flags);144 145 // attention sub-layer146 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, mtp_flags);147 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head_i, n_embd_k_gqa_i, n_embd_v_gqa_i, mtp_flags);148 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, mtp_flags);149 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, mtp_flags | TENSOR_NOT_REQUIRED);150 151 // MoE sub-layer152 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, mtp_flags);153 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, mtp_flags);154 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, mtp_flags);155 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, moe_n_embd, n_expert}, mtp_flags);156 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {moe_n_embd, n_ff_exp, n_expert}, mtp_flags);157 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, mtp_flags);158 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, mtp_flags);159 }160}161 162std::unique_ptr<llm_graph_context> llama_model_nemotron_h::build_arch_graph(const llm_graph_params & params) const {163 return std::make_unique<graph>(*this, params);164}165 166llama_model_nemotron_h::graph::graph(const llama_model & model, const llm_graph_params & params) :167 llm_build_mamba_base(params) {168 const int64_t n_embd_head = hparams.n_embd_head_v();169 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());170 171 ggml_tensor * cur;172 ggml_tensor * inpL;173 174 inpL = build_inp_embd(model.tok_embd);175 ggml_build_forward_expand(gf, inpL);176 177 auto * inp = build_inp_mem_hybrid();178 179 ggml_tensor * inp_out_ids = build_inp_out_ids();180 181 for (int il = 0; il < n_layer; ++il) {182 struct ggml_tensor * inpSA = inpL;183 184 // norm185 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);186 cb(cur, "attn_norm", il);187 188 if (hparams.is_recr(il)) {189 // ssm layer //190 cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);191 } else if (hparams.n_ff(il) == 0) {192 // attention layer //193 cur = build_attention_layer(cur, inp->get_attn(), model, n_embd_head, il);194 } else {195 cur = build_ffn_layer(cur, model, il);196 }197 198 if (il == n_layer - 1 && inp_out_ids && cparams.embeddings_nextn_masked) {199 cur = ggml_get_rows(ctx0, cur, inp_out_ids);200 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);201 }202 203 // add residual204 cur = ggml_add(ctx0, cur, inpSA);205 cb(cur, "nemotron_h_block_out", il);206 207 // input for next layer208 inpL = cur;209 }210 211 cur = inpL;212 213 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);214 215 // seed for the MTP/NextN draft head216 cb(cur, "h_nextn", -1);217 res->t_h_nextn = cur;218 219 if (!cparams.embeddings_nextn_masked && inp_out_ids) {220 cur = ggml_get_rows(ctx0, cur, inp_out_ids);221 }222 223 cb(cur, "result_norm", -1);224 res->t_embd = cur;225 226 // lm_head227 cur = build_lora_mm(model.output, cur, model.output_s);228 cb(cur, "result_output", -1);229 res->t_logits = cur;230 231 ggml_build_forward_expand(gf, cur);232}233 234ggml_tensor * llama_model_nemotron_h::graph::build_attention_layer(ggml_tensor * cur,235 llm_graph_input_attn_kv * inp_attn,236 const llama_model & model,237 int64_t n_embd_head,238 int il) {239 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);240 241 const float kq_scale =242 hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;243 cur = build_attn(inp_attn,244 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,245 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);246 cb(cur, "attn_out", il);247 return cur;248}249 250ggml_tensor * llama_model_nemotron_h::graph::build_ffn_layer(ggml_tensor * cur, const llama_model & model, int il) {251 if (model.layers[il].ffn_gate_inp == nullptr) {252 cur = build_ffn(cur,253 model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s,254 NULL, NULL, NULL,255 model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,256 NULL,257 LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);258 cb(cur, "ffn_out", il);259 } else {260 ggml_tensor * inp_emb = cur;261 ggml_tensor * inp_latent = cur;262 263 if (model.layers[il].ffn_latent_down) {264 inp_latent = ggml_mul_mat(ctx0, model.layers[il].ffn_latent_down, cur);265 }266 267 ggml_tensor * router_logits = build_lora_mm(model.layers[il].ffn_gate_inp, cur);268 cb(router_logits, "ffn_moe_logits", il);269 270 ggml_tensor * moe_out =271 build_moe_ffn(inp_latent,272 model.layers[il].ffn_gate_inp,273 model.layers[il].ffn_up_exps,274 nullptr, // no gate275 model.layers[il].ffn_down_exps,276 model.layers[il].ffn_exp_probs_b,277 n_expert, n_expert_used,278 LLM_FFN_RELU_SQR, hparams.expert_weights_norm,279 hparams.expert_weights_scale,280 LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,281 il,282 router_logits, nullptr,283 model.layers[il].ffn_up_exps_s,284 nullptr, // no gate285 model.layers[il].ffn_down_exps_s);286 cb(moe_out, "ffn_moe_out", il);287 288 if (model.layers[il].ffn_latent_up) {289 moe_out = ggml_mul_mat(ctx0, model.layers[il].ffn_latent_up, moe_out);290 }291 292 ggml_tensor * ffn_shexp = build_ffn(inp_emb,293 model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,294 NULL /* no gate */ , NULL, NULL,295 model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,296 NULL,297 LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);298 cb(ffn_shexp, "ffn_shexp", il);299 300 cur = ggml_add(ctx0, moe_out, ffn_shexp);301 cb(cur, "ffn_out", il);302 }303 304 cur = build_cvec(cur, il);305 cb(cur, "l_out", il);306 307 return cur;308}309 