| File: | root/firefox-clang/third_party/llama.cpp/src/models/mimo2.cpp |
| Warning: | line 101, column 9 Value stored to 'cur' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include "models.h" |
| 2 | |
| 3 | void llama_model_mimo2::load_arch_hparams(llama_model_loader & ml) { |
| 4 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
| 5 | |
| 6 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
| 7 | |
| 8 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
| 9 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
| 10 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
| 11 | |
| 12 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer()); |
| 13 | |
| 14 | float value_scale = 0.0f; |
| 15 | if (ml.get_key(LLM_KV_ATTENTION_VALUE_SCALE, value_scale, false) && value_scale != 1.0f) { |
| 16 | hparams.f_attn_value_scale = value_scale; |
| 17 | } |
| 18 | |
| 19 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false); |
| 20 | GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl")if (!(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl")) ggml_abort("/root/firefox-clang/third_party/llama.cpp/src/models/mimo2.cpp" , 20, "GGML_ASSERT(%s) failed", "hparams.n_layer_nextn < hparams.n_layer_all && \"n_layer_nextn must be < n_layer_impl\"" ); |
| 21 | |
| 22 | switch (hparams.n_layer()) { |
| 23 | case 48: type = LLM_TYPE_310B_A15B; break; |
| 24 | default: type = LLM_TYPE_UNKNOWN; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | void llama_model_mimo2::load_arch_tensors(llama_model_loader &) { |
| 29 | LLAMA_LOAD_LOCALSconst int n_layer = hparams.n_layer(); (void)(n_layer); const int n_layer_all = hparams.n_layer_all; (void)(n_layer_all); const int n_layer_nextn = hparams.n_layer_nextn; (void)(n_layer_nextn ); const int64_t n_head = hparams.n_head(); (void)(n_head); const int64_t n_head_kv = hparams.n_head_kv(); (void)(n_head_kv); const int64_t n_embd = hparams.n_embd; (void)(n_embd); const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(); (void)(n_embd_k_gqa); const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(); (void)( n_embd_v_gqa); const int64_t n_embd_head_k = hparams.n_embd_head_k (); (void)(n_embd_head_k); const int64_t n_embd_head_v = hparams .n_embd_head_v(); (void)(n_embd_head_v); const int64_t n_ff = hparams.n_ff(); (void)(n_ff); const int64_t n_embd_gqa = n_embd_v_gqa ; (void)(n_embd_gqa); const int64_t n_vocab = vocab.n_tokens( ); (void)(n_vocab); const int64_t n_token_types = vocab.n_token_types (); (void)(n_token_types); const int64_t n_rot = hparams.n_rot (); (void)(n_rot); const int64_t n_expert = hparams.n_expert; (void)(n_expert); const int64_t n_expert_used = hparams.n_expert_used ; (void)(n_expert_used); const int64_t n_ctx_train = hparams. n_ctx_train; (void)(n_ctx_train);; |
| 30 | |
| 31 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
| 32 | |
| 33 | // output |
| 34 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
| 35 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
| 36 | |
| 37 | for (int i = 0; i < n_layer_all; ++i) { |
| 38 | auto & layer = layers[i]; |
| 39 | uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i); |
| 40 | uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i); |
| 41 | uint32_t n_head = hparams.n_head(i); |
| 42 | |
| 43 | // NextN/MTP layers (the last n_nextn blocks) are preserved but disabled pending support |
| 44 | const bool is_nextn = i >= n_layer; |
| 45 | const int skip = is_nextn ? TENSOR_SKIP : 0; |
| 46 | |
| 47 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, skip); |
| 48 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_v * n_head, n_embd }, skip); |
| 49 | |
| 50 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, skip); |
| 51 | layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, TENSOR_NOT_REQUIRED | skip); |
| 52 | |
| 53 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, skip); |
| 54 | |
| 55 | // non-MoE branch |
| 56 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED | skip); |
| 57 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, TENSOR_NOT_REQUIRED | skip); |
| 58 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED | skip); |
| 59 | |
| 60 | // MoE branch |
| 61 | int64_t n_ff_exp = hparams.n_ff_exp; |
| 62 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED | skip); |
| 63 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED | skip); |
| 64 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, TENSOR_NOT_REQUIRED | skip); |
| 65 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED | skip); |
| 66 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | skip); |
| 67 | |
| 68 | if (is_nextn) { |
| 69 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2 * n_embd, n_embd}, skip); |
| 70 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), {n_embd}, skip); |
| 71 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), {n_embd}, skip); |
| 72 | layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, skip); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | std::unique_ptr<llm_graph_context> llama_model_mimo2::build_arch_graph(const llm_graph_params & params) const { |
| 78 | return std::make_unique<graph>(*this, params); |
| 79 | } |
| 80 | |
| 81 | llama_model_mimo2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 82 | ggml_tensor * cur; |
| 83 | ggml_tensor * inpL; |
| 84 | |
| 85 | inpL = build_inp_embd(model.tok_embd); |
| 86 | |
| 87 | ggml_tensor * inp_pos = build_inp_pos(); |
| 88 | auto * inp_attn = build_attn_inp_kv_iswa(); |
| 89 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 90 | |
| 91 | const float v_scale = hparams.f_attn_value_scale; |
| 92 | |
| 93 | for (int il = 0; il < n_layer; ++il) { |
| 94 | ggml_tensor * inpSA = inpL; |
| 95 | |
| 96 | uint32_t n_head_l = hparams.n_head(il); |
| 97 | uint32_t n_head_kv_l = hparams.n_head_kv(il); |
| 98 | const float freq_base_l = model.get_rope_freq_base(cparams, il); |
| 99 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
| 100 | |
| 101 | cur = inpL; |
Value stored to 'cur' is never read | |
| 102 | |
| 103 | // self_attention |
| 104 | { |
| 105 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL__null, LLM_NORM_RMS, il); |
| 106 | cb(cur, "attn_norm", il); |
| 107 | |
| 108 | ggml_tensor * Qcur; |
| 109 | ggml_tensor * Kcur; |
| 110 | ggml_tensor * Vcur; |
| 111 | |
| 112 | if (model.layers[il].wqkv) { |
| 113 | // Fused qkv_proj - Q/K share head_dim_k, V uses head_dim_v |
| 114 | ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur); |
| 115 | cb(qkv, "wqkv", il); |
| 116 | |
| 117 | const size_t row_k = ggml_row_size(qkv->type, n_embd_head_k); |
| 118 | const size_t row_v = ggml_row_size(qkv->type, n_embd_head_v); |
| 119 | const size_t row_full = qkv->nb[1]; |
| 120 | const size_t k_off = row_k * n_head_l; |
| 121 | const size_t v_off = k_off + row_k * n_head_kv_l; |
| 122 | |
| 123 | Qcur = ggml_view_3d(ctx0, qkv, n_embd_head_k, n_head_l, n_tokens, row_k, row_full, 0); |
| 124 | Kcur = ggml_view_3d(ctx0, qkv, n_embd_head_k, n_head_kv_l, n_tokens, row_k, row_full, k_off); |
| 125 | Vcur = ggml_view_3d(ctx0, qkv, n_embd_head_v, n_head_kv_l, n_tokens, row_v, row_full, v_off); |
| 126 | } else { |
| 127 | // Split path |
| 128 | Qcur = build_lora_mm(model.layers[il].wq, cur); |
| 129 | cb(Qcur, "Qcur", il); |
| 130 | |
| 131 | Kcur = build_lora_mm(model.layers[il].wk, cur); |
| 132 | cb(Kcur, "Kcur", il); |
| 133 | |
| 134 | Vcur = build_lora_mm(model.layers[il].wv, cur); |
| 135 | cb(Vcur, "Vcur", il); |
| 136 | |
| 137 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head_k, n_head_l, n_tokens); |
| 138 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv_l, n_tokens); |
| 139 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head_v, n_head_kv_l, n_tokens); |
| 140 | } |
| 141 | |
| 142 | Qcur = ggml_rope_ext( |
| 143 | ctx0, Qcur, inp_pos, nullptr, |
| 144 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
| 145 | ext_factor, attn_factor, beta_fast, beta_slow |
| 146 | ); |
| 147 | |
| 148 | Kcur = ggml_rope_ext( |
| 149 | ctx0, Kcur, inp_pos, nullptr, |
| 150 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
| 151 | ext_factor, attn_factor, beta_fast, beta_slow |
| 152 | ); |
| 153 | |
| 154 | cb(Qcur, "Qcur", il); |
| 155 | cb(Kcur, "Kcur", il); |
| 156 | cb(Vcur, "Vcur", il); |
| 157 | |
| 158 | ggml_tensor * sinks = model.layers[il].attn_sinks; |
| 159 | |
| 160 | cur = build_attn(inp_attn, |
| 161 | model.layers[il].wo, NULL__null, model.layers[il].wo_s, |
| 162 | Qcur, Kcur, Vcur, nullptr, sinks, nullptr, 1.0f/sqrtf(float(n_embd_head_k)), il); |
| 163 | cb(cur, "attn_out", il); |
| 164 | |
| 165 | if (v_scale) { |
| 166 | cur = ggml_scale(ctx0, cur, v_scale); |
| 167 | cb(cur, "attn_out_scaled", il); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (il == n_layer - 1 && inp_out_ids) { |
| 172 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
| 173 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
| 174 | } |
| 175 | |
| 176 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
| 177 | cb(ffn_inp, "ffn_inp", il); |
| 178 | |
| 179 | cur = build_norm(ffn_inp, |
| 180 | model.layers[il].ffn_norm, NULL__null, |
| 181 | LLM_NORM_RMS, il); |
| 182 | cb(cur, "ffn_norm", il); |
| 183 | |
| 184 | // feed-forward network |
| 185 | if (model.layers[il].ffn_gate_inp == nullptr) { |
| 186 | // dense branch |
| 187 | cur = build_ffn(cur, |
| 188 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL__null, |
| 189 | model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL__null, |
| 190 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL__null, |
| 191 | NULL__null, |
| 192 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
| 193 | cb(cur, "ffn_out", il); |
| 194 | } else { |
| 195 | // MoE branch |
| 196 | cur = build_moe_ffn(cur, |
| 197 | model.layers[il].ffn_gate_inp, |
| 198 | model.layers[il].ffn_up_exps, |
| 199 | model.layers[il].ffn_gate_exps, |
| 200 | model.layers[il].ffn_down_exps, |
| 201 | model.layers[il].ffn_exp_probs_b, |
| 202 | n_expert, n_expert_used, |
| 203 | LLM_FFN_SILU, true, |
| 204 | hparams.expert_weights_scale, |
| 205 | LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID, |
| 206 | il); |
| 207 | cb(cur, "ffn_moe_out", il); |
| 208 | } |
| 209 | |
| 210 | cur = ggml_add(ctx0, cur, ffn_inp); |
| 211 | |
| 212 | cur = build_cvec(cur, il); |
| 213 | cb(cur, "l_out", il); |
| 214 | |
| 215 | // input for next layer |
| 216 | inpL = cur; |
| 217 | } |
| 218 | |
| 219 | cur = inpL; |
| 220 | |
| 221 | cur = build_norm(cur, |
| 222 | model.output_norm, NULL__null, |
| 223 | LLM_NORM_RMS, -1); |
| 224 | |
| 225 | cb(cur, "result_norm", -1); |
| 226 | res->t_embd = cur; |
| 227 | |
| 228 | // lm_head |
| 229 | cur = build_lora_mm(model.output, cur, model.output_s); |
| 230 | |
| 231 | cb(cur, "result_output", -1); |
| 232 | res->t_logits = cur; |
| 233 | |
| 234 | ggml_build_forward_expand(gf, cur); |
| 235 | } |