llama: fix kv shifting, add tests, add per-model build_rope (#31683)

* llama: fix kv shifting and add tests

* llama: add graph build_rope (#31693)

* llama: add graph build_rope

* improve

* migrate new models

* update review skill

* fix ci

* add --kv-shift to CI
This commit is contained in:
Xuan-Son Nguyen authored and GitHub committed 2026-11-05 21:10:36 +01:00
1 parent b1c902f026
commit 6023692b1e
144 files changed
+1154 -1847

No files matched your search

+14 -31
View File
@@ -172,6 +172,16 @@ void llama_model_step35::load_arch_tensors(llama_model_loader & ml) {
}
}
llama_rope_params llama_model_step35::get_rope_params(const llama_cparams & cparams, int il) const {
auto res = llama_model::get_rope_params(cparams, il);
if (hparams.is_swa(il)) {
res.factors = nullptr;
}
return res;
}
std::unique_ptr<llm_graph_context> llama_model_step35::build_arch_graph(const llm_graph_params & params) const {
if (params.gtype == LLM_GRAPH_TYPE_DECODER_MTP) {
return std::make_unique<graph_mtp>(params);
@@ -195,9 +205,6 @@ llama_model_step35::graph::graph(const llm_graph_params & params) : llm_graph_co
const uint32_t n_head_l = hparams.n_head(il);
const uint32_t n_head_kv_l = hparams.n_head_kv(il);
const float freq_base_l = model.get_rope_freq_base(cparams, il);
const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
cur = inpL;
// dump pre-attn RMSNorm input to pinpoint layer boundary issues
@@ -232,19 +239,8 @@ llama_model_step35::graph::graph(const llm_graph_params & params) : llm_graph_co
}
// RoPE (partial rotary factors per layer)
const bool is_swa = hparams.is_swa(il);
ggml_tensor * rope_factors = is_swa ? nullptr : model.get_rope_factors(cparams, il);
const int64_t n_rot_l = hparams.n_rot(il);
Qcur = ggml_rope_ext(
ctx0, Qcur, inp_pos, rope_factors,
n_rot_l, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
ext_factor, attn_factor, beta_fast, beta_slow
);
Kcur = ggml_rope_ext(
ctx0, Kcur, inp_pos, rope_factors,
n_rot_l, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
ext_factor, attn_factor, beta_fast, beta_slow
);
Qcur = build_rope(Qcur, inp_pos, il);
Kcur = build_rope(Kcur, inp_pos, il);
cb(Qcur, "Qcur_pos", il);
cb(Kcur, "Kcur_pos", il);
@@ -375,9 +371,6 @@ llama_model_step35::graph_mtp::graph_mtp(const llm_graph_params & params)
const uint32_t n_head_l = hparams.n_head(il);
const uint32_t n_head_kv_l = hparams.n_head_kv(il);
const float freq_base_l = model.get_rope_freq_base(cparams, il);
const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
auto inp = std::make_unique<llm_graph_input_embd_h>(hparams.n_embd_inp(), hparams.n_embd);
inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
@@ -441,18 +434,8 @@ llama_model_step35::graph_mtp::graph_mtp(const llm_graph_params & params)
cb(Kcur, "mtp_Kcur_normed", il);
}
const bool is_swa = hparams.is_swa(il);
ggml_tensor * rope_factors = is_swa ? nullptr : model.get_rope_factors(cparams, il);
const int64_t n_rot_l = hparams.n_rot(il);
Qcur = ggml_rope_ext(
ctx0, Qcur, inp_pos, rope_factors,
n_rot_l, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
ext_factor, attn_factor, beta_fast, beta_slow);
Kcur = ggml_rope_ext(
ctx0, Kcur, inp_pos, rope_factors,
n_rot_l, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
ext_factor, attn_factor, beta_fast, beta_slow);
Qcur = build_rope(Qcur, inp_pos, il);
Kcur = build_rope(Kcur, inp_pos, il);
cb(Qcur, "mtp_Qcur_pos", il);
cb(Kcur, "mtp_Kcur_pos", il);