From f07c3416ae746b644819a40c84c0ed62aa3d87ea Mon Sep 17 00:00:00 2001 From: Joe Rowell Date: Tue, 14 Jul 2026 16:03:36 +0000 Subject: [PATCH] laguna: honor attention_factor as final YaRN scaling, not mscale coefficient _build_rope_from_flat_dict set rp.mscale = attention_factor, but create_sinusoidal_positions_yarn routes mscale through 0.1*mscale*ln(factor)+1, so the HF final scaling got the log applied twice (factor 64 +12.2%, factor 32 +8.9%). Invert so create_sinusoidal reproduces attention_factor exactly, and import math for the log. Signed-off-by: Joe Rowell --- tensorrt_llm/_torch/models/modeling_laguna.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tensorrt_llm/_torch/models/modeling_laguna.py b/tensorrt_llm/_torch/models/modeling_laguna.py index a03114ea38c7..ce170899f899 100644 --- a/tensorrt_llm/_torch/models/modeling_laguna.py +++ b/tensorrt_llm/_torch/models/modeling_laguna.py @@ -14,6 +14,7 @@ # limitations under the License. """Laguna / Laguna-XS model for TensorRT-LLM PyTorch backend.""" +import math from typing import Dict, List, Optional, Type import torch @@ -304,7 +305,14 @@ def _build_rope_from_flat_dict(config, rp_dict: dict) -> RopeParams: rp.beta_slow = float(rp_dict.get("beta_slow", 1.0)) attention_factor = rp_dict.get("attention_factor") if attention_factor is not None: - rp.mscale = float(attention_factor) + attention_factor = float(attention_factor) + # attention_factor is the FINAL YaRN scaling (HF semantics). + # create_sinusoidal_positions_yarn applies get_mscale(factor, rp.mscale) + # = 0.1*rp.mscale*ln(factor)+1 (mscale_all_dim=0), so setting mscale to the + # final value routes it through the log twice. Invert to reproduce it exactly. + rp.mscale = ( + ((attention_factor - 1.0) / (0.1 * math.log(rp.scale))) if rp.scale > 1 else 1.0 + ) rp.original_max_positions = int( rp_dict.get("original_max_position_embeddings", config.max_position_embeddings) )