|
| 1 | +#include "models.h" |
| 2 | + |
| 3 | +void llama_model_nanbeige::load_arch_hparams(llama_model_loader & ml) { |
| 4 | + ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
| 5 | + |
| 6 | + uint32_t n_loops_u = 1; |
| 7 | + ml.get_key(LLM_KV_NUM_LOOPS, n_loops_u, false); |
| 8 | + GGML_ASSERT(n_loops_u >= 1); |
| 9 | + |
| 10 | + skip_loop_final_norm = false; |
| 11 | + ml.get_key(LLM_KV_SKIP_LOOP_FINAL_NORM, skip_loop_final_norm, false); |
| 12 | + |
| 13 | + n_layer_phys = (int) hparams.n_layer(); |
| 14 | + |
| 15 | + // Bound-check before casting: signed int mul can overflow and bypass the guard. |
| 16 | + GGML_ASSERT((size_t) n_layer_phys * (size_t) n_loops_u <= (size_t) LLAMA_MAX_LAYERS); |
| 17 | + n_loops = (int) n_loops_u; |
| 18 | + |
| 19 | + // Expand logical layer count before load_tensors() allocates layers / KV. |
| 20 | + if (n_loops > 1) { |
| 21 | + for (int j = 1; j < n_loops; ++j) { |
| 22 | + for (int i = 0; i < n_layer_phys; ++i) { |
| 23 | + const int dst = i + j * n_layer_phys; |
| 24 | + hparams.n_head_arr[dst] = hparams.n_head_arr[i]; |
| 25 | + hparams.n_head_kv_arr[dst] = hparams.n_head_kv_arr[i]; |
| 26 | + hparams.n_ff_arr[dst] = hparams.n_ff_arr[i]; |
| 27 | + hparams.is_swa_impl[dst] = hparams.is_swa_impl[i]; |
| 28 | + hparams.is_recr_impl[dst] = hparams.is_recr_impl[i]; |
| 29 | + } |
| 30 | + } |
| 31 | + hparams.n_layer_all = (uint32_t) ((size_t) n_layer_phys * (size_t) n_loops); |
| 32 | + } |
| 33 | + |
| 34 | + type = LLM_TYPE_UNKNOWN; |
| 35 | +} |
| 36 | + |
| 37 | +void llama_model_nanbeige::load_arch_tensors(llama_model_loader &) { |
| 38 | + LLAMA_LOAD_LOCALS; |
| 39 | + |
| 40 | + tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
| 41 | + |
| 42 | + output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
| 43 | + output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
| 44 | + if (output == NULL) { |
| 45 | + output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
| 46 | + } |
| 47 | + |
| 48 | + const int n_phys = n_layer_phys > 0 ? n_layer_phys : n_layer; |
| 49 | + for (int i = 0; i < n_phys; ++i) { |
| 50 | + auto & layer = layers[i]; |
| 51 | + |
| 52 | + layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
| 53 | + |
| 54 | + create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0); |
| 55 | + layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
| 56 | + |
| 57 | + layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, |
| 58 | + TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
| 59 | + |
| 60 | + layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
| 61 | + layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
| 62 | + layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
| 63 | + layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
| 64 | + } |
| 65 | + |
| 66 | + // Share physical weights across loops; each slot still has its own KV index. |
| 67 | + if (n_loops > 1) { |
| 68 | + for (int j = 1; j < n_loops; ++j) { |
| 69 | + for (int i = 0; i < n_phys; ++i) { |
| 70 | + layers[i + j * n_phys] = layers[i]; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +std::unique_ptr<llm_graph_context> llama_model_nanbeige::build_arch_graph(const llm_graph_params & params) const { |
| 77 | + return std::make_unique<graph>(*this, params); |
| 78 | +} |
| 79 | + |
| 80 | +llama_model_nanbeige::graph::graph(const llama_model & model, const llm_graph_params & params) : |
| 81 | + llm_graph_context(params) { |
| 82 | + const auto & nb = static_cast<const llama_model_nanbeige &>(model); |
| 83 | + |
| 84 | + const int64_t n_embd_head = hparams.n_embd_head_v(); |
| 85 | + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
| 86 | + |
| 87 | + const int n_phys = nb.n_layer_phys > 0 ? nb.n_layer_phys : (int) n_layer; |
| 88 | + const int n_loops = nb.n_loops > 0 ? nb.n_loops : 1; |
| 89 | + |
| 90 | + ggml_tensor * cur; |
| 91 | + ggml_tensor * inpL; |
| 92 | + |
| 93 | + inpL = build_inp_embd(model.tok_embd); |
| 94 | + |
| 95 | + ggml_tensor * inp_pos = build_inp_pos(); |
| 96 | + |
| 97 | + auto * inp_attn = build_attn_inp_kv(); |
| 98 | + |
| 99 | + const float kq_scale = hparams.f_attention_scale == 0.0f |
| 100 | + ? 1.0f / sqrtf(float(n_embd_head)) |
| 101 | + : hparams.f_attention_scale; |
| 102 | + |
| 103 | + ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 104 | + |
| 105 | + for (int il = 0; il < n_layer; ++il) { |
| 106 | + ggml_tensor * inpSA = inpL; |
| 107 | + |
| 108 | + cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
| 109 | + cb(cur, "attn_norm", il); |
| 110 | + |
| 111 | + { |
| 112 | + ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
| 113 | + |
| 114 | + auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
| 115 | + n_embd_head, n_head, n_head_kv, il); |
| 116 | + |
| 117 | + Qcur = ggml_rope_ext( |
| 118 | + ctx0, Qcur, inp_pos, rope_factors, |
| 119 | + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
| 120 | + ext_factor, attn_factor, beta_fast, beta_slow); |
| 121 | + |
| 122 | + Kcur = ggml_rope_ext( |
| 123 | + ctx0, Kcur, inp_pos, rope_factors, |
| 124 | + n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
| 125 | + ext_factor, attn_factor, beta_fast, beta_slow); |
| 126 | + |
| 127 | + cb(Qcur, "Qcur", il); |
| 128 | + cb(Kcur, "Kcur", il); |
| 129 | + cb(Vcur, "Vcur", il); |
| 130 | + |
| 131 | + cur = build_attn(inp_attn, |
| 132 | + model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
| 133 | + Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
| 134 | + cb(cur, "attn_out", il); |
| 135 | + } |
| 136 | + |
| 137 | + if (il == n_layer - 1 && inp_out_ids) { |
| 138 | + cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
| 139 | + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
| 140 | + } |
| 141 | + |
| 142 | + ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
| 143 | + cb(ffn_inp, "ffn_inp", il); |
| 144 | + |
| 145 | + cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
| 146 | + cb(cur, "ffn_norm", il); |
| 147 | + |
| 148 | + cur = build_ffn(cur, |
| 149 | + model.layers[il].ffn_up, model.layers[il].ffn_up_b, model.layers[il].ffn_up_s, |
| 150 | + model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, model.layers[il].ffn_gate_s, |
| 151 | + model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s, |
| 152 | + NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
| 153 | + cb(cur, "ffn_out", il); |
| 154 | + |
| 155 | + cur = ggml_add(ctx0, cur, ffn_inp); |
| 156 | + cb(cur, "ffn_out", il); |
| 157 | + |
| 158 | + cur = build_cvec(cur, il); |
| 159 | + cb(cur, "l_out", il); |
| 160 | + |
| 161 | + inpL = cur; |
| 162 | + |
| 163 | + if (n_loops > 1 && |
| 164 | + ((il + 1) % n_phys) == 0 && |
| 165 | + (il + 1) < n_layer && |
| 166 | + !nb.skip_loop_final_norm) { |
| 167 | + cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, il); |
| 168 | + cb(cur, "loop_norm", il); |
| 169 | + inpL = cur; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + cur = inpL; |
| 174 | + |
| 175 | + cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
| 176 | + cb(cur, "result_norm", -1); |
| 177 | + res->t_embd = cur; |
| 178 | + |
| 179 | + cur = build_lora_mm(model.output, cur, model.output_s); |
| 180 | + cb(cur, "result_output", -1); |
| 181 | + res->t_logits = cur; |
| 182 | + |
| 183 | + ggml_build_forward_expand(gf, cur); |
| 184 | +} |
0 commit comments