Commit a346a49f8 for llama.cpp
commit a346a49f8880f3a29f9ba18b0fe9a55ecce58976
Author: Xuan-Son Nguyen <son@huggingface.co>
Date: Mon Nov 30 12:50:02 2026 +0100
model: add get_yarn_attn_factor (#33314)
* model: add get_yarn_attn_factor
* nits
Assisted-by: Kimi K3.1 Pro
* deepseek2 : drop constant YaRN warning
Assisted-by: Kimi K3.1 Pro
diff --git a/src/llama-context.cpp b/src/llama-context.cpp
index 35308bf3e..603697802 100644
--- a/src/llama-context.cpp
+++ b/src/llama-context.cpp
@@ -268,43 +268,13 @@ llama_context::llama_context(
cparams.yarn_ext_factor = cparams.rope_scaling_type == LLAMA_ROPE_SCALING_TYPE_YARN ? 1.0f : 0.0f;
}
+ cparams.yarn_attn_factor = model.get_yarn_attn_factor(cparams);
if (cparams.yarn_ext_factor != 0) {
- static auto get_mscale = [](float scale, float mscale) {
- return scale <= 1.0f ? 1.0f : (0.1f * mscale * logf(scale) + 1.0f);
- };
-
const float factor = 1.0f / cparams.rope_freq_scale;
-
- // ref: https://github.com/huggingface/transformers/blob/6d00f6b0a5679c36510f203e4226e36f517c3032/src/transformers/modeling_rope_utils.py#L336-L348
- if (hparams.rope_yarn_log_mul != 0.0f) {
- // note: here we assume `mscale == 1.0f`
- // TODO: start reading the actual value of mscale and handle the case where it is not 1.0f
- float mscale = 1.0f;
- const float mscale_all_dims = hparams.rope_yarn_log_mul;
-
- // [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX]
- // special-case DEEPSEEK v2:
- // https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite-Chat/blob/main/config.json#L42-L43
- if (model.arch == LLM_ARCH_DEEPSEEK2 && mscale_all_dims != 1.0f) {
- mscale = mscale_all_dims;
- }
-
- cparams.yarn_attn_factor = get_mscale(factor, mscale) / get_mscale(factor, mscale_all_dims);
-
- LLAMA_LOG_WARN("%s: setting new yarn_attn_factor = %.4f (mscale == %.1f, mscale_all_dim = %.1f)\n",
- __func__, cparams.yarn_attn_factor, mscale, mscale_all_dims);
- } else {
- cparams.yarn_attn_factor = get_mscale(factor, 1.0f);
- }
-
- // when YARN is applied with yarn_ext_factor != 0.0f, we need to cancel this factor:
- // https://github.com/ggml-org/llama.cpp/blob/a81a569577cc38b32558958b048228150be63eae/ggml/src/ggml-cpu/ops.cpp#L5541-L5544
- //
+ // ggml re-applies mscale * (1 + 0.1f * logf(1.0f / freq_scale)) when ext_factor != 0, so cancel it here
// ref: https://github.com/ggml-org/llama.cpp/discussions/7416
- // https://github.com/ggml-org/llama.cpp/pull/17945
cparams.yarn_attn_factor *= 1.0f / (1.0f + 0.1f * logf(factor));
}
-
cparams.yarn_attn_factor *= hparams.rope_attn_factor;
if (cparams.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED) {
diff --git a/src/llama-model.cpp b/src/llama-model.cpp
index f391b89da..3d133d19c 100644
--- a/src/llama-model.cpp
+++ b/src/llama-model.cpp
@@ -2958,6 +2958,36 @@ llama_rope_params llama_model::get_rope_params_indexer(const llama_cparams & cpa
return res;
}
+float llama_model::get_yarn_attn_factor(const llama_cparams & cparams) const {
+ auto get_mscale = [](float scale, float mscale) {
+ return scale <= 1.0f ? 1.0f : (0.1f * mscale * logf(scale) + 1.0f);
+ };
+
+ if (cparams.yarn_ext_factor == 0.0f) {
+ return cparams.yarn_attn_factor;
+ }
+
+ const float factor = 1.0f / cparams.rope_freq_scale;
+
+ float yarn;
+ if (hparams.rope_yarn_log_mul != 0.0f) {
+ // ref: https://github.com/huggingface/transformers/blob/6d00f6b0a5679c36510f203e4226e36f517c3032/src/transformers/modeling_rope_utils.py#L336-L348
+ // note: we assume mscale == 1.0f
+ // TODO: start reading the actual value of mscale and handle the case where it is not 1.0f
+ const float mscale = 1.0f;
+ const float mscale_all_dims = hparams.rope_yarn_log_mul;
+
+ yarn = get_mscale(factor, mscale) / get_mscale(factor, mscale_all_dims);
+
+ LLAMA_LOG_WARN("%s: setting new yarn_attn_factor = %.4f (mscale == %.1f, mscale_all_dim = %.1f)\n",
+ __func__, yarn, mscale, mscale_all_dims);
+ } else {
+ yarn = get_mscale(factor, 1.0f);
+ }
+
+ return yarn;
+}
+
float llama_model::get_rope_freq_base (const llama_cparams & cparams, int il) const {
return hparams.is_swa(il) ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base;
}
diff --git a/src/llama-model.h b/src/llama-model.h
index c3334469d..a1fb968b8 100644
--- a/src/llama-model.h
+++ b/src/llama-model.h
@@ -1047,6 +1047,10 @@ struct llama_model {
// same, for the keys of the lightning indexer
virtual llama_rope_params get_rope_params_indexer(const llama_cparams & cparams, int il) const;
+ // the YaRN attention factor before the ggml cancellation and rope_attn_factor multiplier
+ // models override it to interpret legacy GGUF rope.scaling.* keys
+ virtual float get_yarn_attn_factor(const llama_cparams & cparams) const;
+
float get_rope_freq_base (const llama_cparams & cparams, int il) const;
float get_rope_freq_scale(const llama_cparams & cparams, int il) const;
diff --git a/src/models/deepseek2.cpp b/src/models/deepseek2.cpp
index b8f6073ec..dff61a954 100644
--- a/src/models/deepseek2.cpp
+++ b/src/models/deepseek2.cpp
@@ -1,5 +1,17 @@
#include "models.h"
+float llama_model_deepseek2::get_yarn_attn_factor(const llama_cparams & cparams) const {
+ // [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX]
+ // mscale == mscale_all_dim in the HF configs (yarn_apply_mscale in modeling_deepseek_v2.py), so the cos/sin magnitude cancels out to 1.0
+ // ref: https://huggingface.co/deepseek-ai/DeepSeek-V2-Lite-Chat/blob/main/config.json#L42-L43
+ const float mscale_all_dims = hparams.rope_yarn_log_mul;
+ if (arch != LLM_ARCH_DEEPSEEK2 || cparams.yarn_ext_factor == 0.0f || mscale_all_dims == 0.0f || mscale_all_dims == 1.0f) {
+ return llama_model::get_yarn_attn_factor(cparams);
+ }
+
+ return 1.0f;
+}
+
void llama_model_deepseek2::load_arch_hparams(llama_model_loader & ml) {
uint32_t n_vocab = 0;
ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
diff --git a/src/models/models.h b/src/models/models.h
index 75446b0de..7c0b9ca2f 100644
--- a/src/models/models.h
+++ b/src/models/models.h
@@ -1183,6 +1183,7 @@ struct llama_model_olmo2 : public llama_model_base {
llama_model_olmo2(const struct llama_model_params & params) : llama_model_base(params) {}
void load_arch_hparams(llama_model_loader & ml) override;
void load_arch_tensors(llama_model_loader & ml) override;
+ float get_yarn_attn_factor(const llama_cparams & cparams) const override;
template <bool iswa>
struct graph : public llm_graph_context {
@@ -1281,6 +1282,7 @@ struct llama_model_deepseek2 : public llama_model_base {
llama_model_deepseek2(const struct llama_model_params & params) : llama_model_base(params) {}
void load_arch_hparams(llama_model_loader & ml) override;
void load_arch_tensors(llama_model_loader & ml) override;
+ float get_yarn_attn_factor(const llama_cparams & cparams) const override;
struct graph : public llm_graph_context {
graph(const llm_graph_params & params);
diff --git a/src/models/olmo2.cpp b/src/models/olmo2.cpp
index 2b35b171e..6031e7a4b 100644
--- a/src/models/olmo2.cpp
+++ b/src/models/olmo2.cpp
@@ -1,5 +1,21 @@
#include "models.h"
+float llama_model_olmo2::get_yarn_attn_factor(const llama_cparams & cparams) const {
+ // old OLMo 3 converters stored the final YaRN magnitude in rope.scaling.attn_factor, so skip the default magnitude
+ // ref: https://github.com/ggml-org/llama.cpp/issues/33191
+ const float rope_attn_factor = hparams.rope_attn_factor;
+ if (cparams.yarn_ext_factor == 0.0f || rope_attn_factor == 1.0f ||
+ hparams.rope_scaling_type_train != LLAMA_ROPE_SCALING_TYPE_YARN ||
+ cparams.rope_scaling_type != LLAMA_ROPE_SCALING_TYPE_YARN) {
+ return llama_model::get_yarn_attn_factor(cparams);
+ }
+
+ LLAMA_LOG_WARN("%s: applying legacy rope.scaling.attn_factor = %.4f as the final YaRN magnitude\n",
+ __func__, rope_attn_factor);
+
+ return 1.0f;
+}
+
void llama_model_olmo2::load_arch_hparams(llama_model_loader & ml) {
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);