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_afmoe::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);6 ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);7 ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);8 ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);9 ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false);10 ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);11 ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);12 13 // Set up interleaved sliding window attention (ISWA)14 // Pattern: 3 sliding - 1 full (global_attn_every_n_layers = 4)15 if (hparams.n_swa > 0) {16 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;17 uint32_t swa_period = 4;18 ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);19 hparams.set_swa_pattern(swa_period);20 21 hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;22 hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;23 ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);24 } else {25 hparams.swa_type = LLAMA_SWA_TYPE_NONE;26 }27 28 // Default to sigmoid if not set29 if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {30 hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;31 }32 33 switch (hparams.n_layer()) {34 case 56: type = LLM_TYPE_6B; break;35 case 32: type = LLM_TYPE_26B; break;36 default: type = LLM_TYPE_UNKNOWN;37 }38}39 40void llama_model_afmoe::load_arch_tensors(llama_model_loader &) {41 LLAMA_LOAD_LOCALS;42 const int64_t n_expert_shared = hparams.n_expert_shared;43 44 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);45 46 // output47 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);48 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);49 50 // if output is NULL, init from the input tok embed51 if (output == NULL) {52 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);53 }54 55 const int64_t n_ff_exp = hparams.n_ff_exp;56 57 for (int i = 0; i < n_layer; ++i) {58 auto & layer = layers[i];59 60 // dual attention normalization61 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);62 layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);63 64 // attention projections65 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);66 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);67 68 // Q/K normalization69 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);70 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);71 72 // attention gating73 layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);74 75 // dual ffn normalization76 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);77 layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);78 79 if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) {80 // MoE layers81 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);82 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0);83 84 // grouped expert weights85 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);86 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);87 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);88 89 // shared expert90 if (n_expert_shared > 0) {91 const int64_t n_ff_shexp = n_ff_exp * n_expert_shared;92 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0);93 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);94 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0);95 }96 } else {97 // Dense layers98 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);99 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);100 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);101 }102 }103}104 105std::unique_ptr<llm_graph_context> llama_model_afmoe::build_arch_graph(const llm_graph_params & params) const {106 return std::make_unique<graph>(*this, params);107}108 109llama_model_afmoe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {110 const int64_t n_embd_head = hparams.n_embd_head_v();111 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());112 113 ggml_tensor * cur;114 ggml_tensor * inpL;115 116 inpL = build_inp_embd(model.tok_embd);117 118 // MuP scaling: embeddings * sqrt(hidden_size)119 // mup_enabled = true, hidden_size = 1024, scale = 32.0120 inpL = ggml_scale(ctx0, inpL, sqrtf(float(n_embd)));121 cb(inpL, "inp_embd_scaled", -1);122 123 // inp_pos - contains the positions124 ggml_tensor * inp_pos = build_inp_pos();125 auto * inp_attn = build_attn_inp_kv_iswa();126 ggml_tensor * inp_out_ids = build_inp_out_ids();127 128 const float kq_scale = 1.0f/sqrtf(float(n_embd_head));129 130 for (int il = 0; il < n_layer; ++il) {131 const float freq_base_l = model.get_rope_freq_base (cparams, il);132 const float freq_scale_l = model.get_rope_freq_scale(cparams, il);133 134 ggml_tensor * inpSA = inpL;135 136 // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous137 const bool use_rope = hparams.n_no_rope_layer_step > 0 &&138 (il + 1) % hparams.n_no_rope_layer_step != 0;139 140 // dual attention normalization (pre)141 cur = build_norm(inpL,142 model.layers[il].attn_norm, NULL,143 LLM_NORM_RMS, il);144 cb(cur, "attn_norm", il);145 146 // self-attention147 {148 ggml_tensor * attn_inp = cur; // save input for gate computation149 150 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,151 n_embd_head, n_head, n_head_kv, il);152 153 // compute gate from input154 ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);155 cb(gate, "attn_gate_proj", il);156 157 // Q/K normalization158 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);159 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);160 cb(Qcur, "Qcur_normed", il);161 cb(Kcur, "Kcur_normed", il);162 163 if (use_rope) {164 Qcur = ggml_rope_ext(165 ctx0, Qcur, inp_pos, nullptr,166 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,167 ext_factor, attn_factor, beta_fast, beta_slow);168 cb(Qcur, "Qcur_rope", il);169 170 Kcur = ggml_rope_ext(171 ctx0, Kcur, inp_pos, nullptr,172 n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,173 ext_factor, attn_factor, beta_fast, beta_slow);174 cb(Kcur, "Kcur_rope", il);175 }176 177 cur = build_attn(inp_attn,178 NULL, NULL, NULL, // wo will be applied after gating179 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);180 cb(cur, "attn_out", il);181 182 // attention gating: attn_out * sigmoid(gate) BEFORE o_proj183 gate = ggml_sigmoid(ctx0, gate);184 cb(gate, "attn_gate_sig", il);185 cur = ggml_mul(ctx0, cur, gate);186 cb(cur, "attn_gated", il);187 188 // now apply output projection189 cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);190 cb(cur, "attn_o_proj", il);191 }192 193 // dual attention normalization (post)194 cur = build_norm(cur,195 model.layers[il].attn_post_norm, NULL,196 LLM_NORM_RMS, il);197 cb(cur, "attn_post_norm", il);198 199 if (il == n_layer - 1 && inp_out_ids) {200 cur = ggml_get_rows(ctx0, cur, inp_out_ids);201 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);202 }203 204 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);205 cb(ffn_inp, "ffn_inp", il);206 207 // dual ffn normalization (pre)208 cur = build_norm(ffn_inp,209 model.layers[il].ffn_norm, NULL,210 LLM_NORM_RMS, il);211 cb(cur, "ffn_norm", il);212 213 // MoE or dense FFN214 if ((uint32_t)il >= hparams.n_layer_dense_lead) {215 // MoE layer with sigmoid routing, normalization, and scaling216 ggml_tensor * moe_out = build_moe_ffn(cur,217 model.layers[il].ffn_gate_inp,218 model.layers[il].ffn_up_exps,219 model.layers[il].ffn_gate_exps,220 model.layers[il].ffn_down_exps,221 model.layers[il].ffn_exp_probs_b,222 n_expert, n_expert_used,223 LLM_FFN_SILU,224 hparams.expert_weights_norm, // norm_w (route_norm=True)225 hparams.expert_weights_scale, // w_scale (route_scale=2.826)226 (llama_expert_gating_func_type) hparams.expert_gating_func,227 il);228 cb(moe_out, "ffn_moe_out", il);229 230 // shared expert231 if (hparams.n_expert_shared > 0) {232 ggml_tensor * ffn_shexp = build_ffn(cur,233 model.layers[il].ffn_up_shexp, NULL, NULL,234 model.layers[il].ffn_gate_shexp, NULL, NULL,235 model.layers[il].ffn_down_shexp, NULL, NULL,236 NULL,237 LLM_FFN_SILU, LLM_FFN_PAR, il);238 cb(ffn_shexp, "ffn_shexp", il);239 240 cur = ggml_add(ctx0, moe_out, ffn_shexp);241 cb(cur, "ffn_out", il);242 } else {243 cur = moe_out;244 }245 } else {246 // dense layer247 cur = build_ffn(cur,248 model.layers[il].ffn_up, NULL, NULL,249 model.layers[il].ffn_gate, NULL, NULL,250 model.layers[il].ffn_down, NULL, NULL,251 NULL,252 LLM_FFN_SILU, LLM_FFN_PAR, il);253 cb(cur, "ffn_out", il);254 }255 256 // dual ffn normalization (post)257 cur = build_norm(cur,258 model.layers[il].ffn_post_norm, NULL,259 LLM_NORM_RMS, il);260 cb(cur, "ffn_post_norm", il);261 262 cur = ggml_add(ctx0, cur, ffn_inp);263 cur = build_cvec(cur, il);264 cb(cur, "l_out", il);265 266 // input for next layer267 inpL = cur;268 }269 270 cur = inpL;271 272 cur = build_norm(cur,273 model.output_norm, NULL,274 LLM_NORM_RMS, -1);275 cb(cur, "result_norm", -1);276 277 res->t_embd = cur;278 279 // lm_head280 cur = build_lora_mm(model.output, cur, model.output_s);281 cb(cur, "result_output", -1);282 res->t_logits = cur;283 284 ggml_build_forward_expand(gf, cur);285}286 