unsloth/unsloth
Daniel Han 2fada48ef5
Fix llama3 RoPE scaling dropped on transformers v5 (#6907)
* Fix llama3 RoPE scaling dropped on transformers v5

transformers v5 loads on meta then blanks non-persistent buffers, so
_fix_rope_inv_freq rebuilds inv_freq after load. It recomputed a vanilla
inv_freq and applied _apply_inv_freq_scaling, a no-op on the base
LlamaRotaryEmbedding used by the config/llama3 path, so inv_freq ended up
divided by 1 instead of the config factor (8 for Llama 3.1, 32 for Llama
3.2). This corrupts long-range positions and inflates long-context loss
about 3-5x. transformers 4.x was unaffected.

Route __init__ and the v5 repair through one _unsloth_recompute_inv_freq
so they cannot diverge, and stash the config on the rotary module so the
repair can rebuild the same scaled value.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add test for llama3 RoPE scaling under the transformers v5 repair

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update RoPE drift guard for the recompute refactor and guard the v5 repair

The drift guard's AST tripwire asserted the config-scaling call lived in the
if config is not None branch of LlamaRotaryEmbedding.__init__. The fix moved
that into _unsloth_recompute_inv_freq, so follow it there (with a fallback to
the old inline branch) and add a guard that loader._fix_rope_inv_freq rebuilds
inv_freq through the same helper. Also add a CPU functional check of the helper
and drop the redundant standalone test.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-07-06 09:13:14 -07:00
..
dataprep fix: correct class name in SyntheticDataKit.chunk_data guard message (#6901) 2026-07-06 07:11:57 -07:00
kernels fix(fp8): use int64 offsets in weight_dequant_kernel (#6884) 2026-07-06 07:11:41 -07:00
models Fix llama3 RoPE scaling dropped on transformers v5 (#6907) 2026-07-06 09:13:14 -07:00
optimizers Reduce and tighten code comments and docstrings repo-wide (#6095) 2026-06-08 23:09:51 -07:00
registry Reduce and tighten code comments and docstrings repo-wide (#6095) 2026-06-08 23:09:51 -07:00
utils Add PrefixGrouper for GRPO: dedup the shared prompt across a group's completions (#6871) 2026-07-06 05:26:24 -07:00
__init__.py Add MLX-aware public Unsloth trainer API (#6462) 2026-07-02 23:02:26 +01:00
_auto_install.py Upgrade setuptools and wheel in the auto-install command (#6282) 2026-06-13 03:59:15 -07:00
_compressed_quantize.py Fix export-time trust_remote_code bypass in FP8/INT8/GGUF-LoRA export (#6869) 2026-07-05 05:16:39 -07:00
_gpu_init.py fix: keep LoRA reloads working with PEFT 0.19 (#6748) 2026-06-30 20:26:57 +01:00
chat_templates.py Add MLX-aware public Unsloth trainer API (#6462) 2026-07-02 23:02:26 +01:00
device_type.py Reduce and tighten code comments and docstrings repo-wide (#6095) 2026-06-08 23:09:51 -07:00
import_fixes.py Reduce comments across recent fixes (#6776) 2026-06-30 23:13:36 -07:00
ollama_template_mappers.py Re-apply #4939: updated models template mappers (#4950) 2026-04-15 07:52:12 -07:00
save.py fix: map None quant method to q8_0 before lowercasing in GGUF export (#6889) 2026-07-06 07:11:49 -07:00
tokenizer_utils.py Auto Xet to HTTP download fallback in from_pretrained; share Studio's fallback via unsloth_zoo (#6638) 2026-07-06 05:13:25 -07:00
trainer.py Reset torch.compile cache poisoned by a stray forward before trainer.train() (#6511) 2026-06-22 05:39:48 -07:00