Skip to content

[llama3] Use Llama 3.2's rope scaling factor for the 1B and 3B flavors - #5528

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
David-Wu1119:fix/llama32-rope-scaling-factor
Oct 19, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
David-Wu1119:fix/llama32-rope-scaling-factor

Conversation

@David-Wu1119

@David-Wu1119 David-Wu1119 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

The 1B and 3B llama3 flavors mirror meta-llama/Llama-3.2-1B and Llama-3.2-3B (same dims, tied embeddings), but their ComplexRoPE.Config doesn't set scaling_factor, so they get the Llama 3.1 default of 8.0. Both published config.json files use rope_scaling = {"factor": 32.0, "low_freq_factor": 1.0, "high_freq_factor": 4.0, "original_max_position_embeddings": 8192, "rope_type": "llama3"}.

With the default, a model loaded from those checkpoints computes different rotary frequencies from the ones it was trained with. A CPU comparison against transformers, with the flavor cut to 1 layer and a small vocab and both models given the same random weights:

rotary frequencies off max angle error at pos 2048 / 8192 logits max abs diff vs HF
1B, main 17 / 32 (up to 4x) 0.195 / 0.781 rad 1.2e-4
3B, main 35 / 64 (up to 4x) 0.195 / 0.781 rad 3.2e-4
1B / 3B, this PR 0 0 / 0 7e-7 / 9.5e-7 (same as the control run with the factor forced to match)

This sets scaling_factor=32.0 in the two flavors. The 8B/70B/405B flavors (Llama 3.1) keep 8.0.

Note: a 1B or 3B model trained from scratch in torchtitan with the old frequencies will see different rotary frequencies after this change.

Test

  • New tests/unit_tests/cpu/models/llama3/test_rope_scaling.py compares the flavors' scaled inverse frequencies with transformers' llama3 rope init, using each checkpoint's published rope_scaling. On main the 1B and 3B cases fail and 8B passes; with this change all three pass. tests/unit_tests/cpu/models/llama3: 7 passed.
  • ufmt (black 22.12.0, usort 1.0.5) and flake8 with the pre-commit plugins are clean.

🤖 Generated with Claude Code

The 1B and 3B flavors mirror meta-llama/Llama-3.2-1B and Llama-3.2-3B
(tied embeddings, same dims), but their ComplexRoPE configs fall back
to the Llama 3.1 default scaling_factor=8.0. Both published config.json
files set rope_scaling.factor=32.0, so 17 of 32 (1B) and 35 of 64 (3B)
rotary frequencies were off by up to 4x after an HF load.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 19, 2026 06:03
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 19, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 19, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@tianyu-l

Copy link
Copy Markdown
Contributor

@claude review

@claude

claude Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 13s —— View job


Review: [llama3] Use Llama 3.2's rope scaling factor for the 1B and 3B flavors

  • Gather context (diff, flavors, RoPE impl)
  • Check the scaling factor and other rope params against the HF Llama 3.2 configs
  • Review the test
  • Post review

Verdict: the fix is correct and I'd merge it. I have one suggestion for the test and one thing to call out in the release notes.

Correctness

  • ComplexRoPE.Config.scaling_factor defaults to 8.0 (torchtitan/models/common/rope.py:97), and _1b/_3b never set it, so both flavors got the Llama 3.1 value. Both meta-llama/Llama-3.2-{1B,3B} configs use factor=32.0. The other llama3 params (low_freq_factor=1.0, high_freq_factor=4.0, original_max_position_embeddings=8192, theta=500000) already match the defaults, so scaling_factor is the only field that needs to change.
  • The rest of the 1B/3B flavors also match HF, so the claim that these flavors mirror Llama 3.2 holds:
    • 1B: dim 2048, 32 heads, 8 KV heads, 16 layers, FFN compute_ffn_hidden_dim(2048, 1024, 1.5) = 8192
    • 3B: dim 3072, 24 heads, 8 KV heads, 28 layers, FFN 8192
    • Both: tied embeddings, max context 131072 (flavors.py:467-468)
  • 8B/70B/405B keep 8.0, which is right for Llama 3.1.
  • No other callsites are affected. scaling_factor isn't set anywhere else, and tests/unit_tests/cpu/config/test_tp_kv_heads_validation.py builds its own RoPE config.

Behavior change to call out (flavors.py:226-227, :285-286)

The PR description notes this, but it's worth repeating per the "protect battle-tested code paths" guideline: RoPE.cache is registered with persistent=False (rope.py:111). A 1B/3B checkpoint trained from scratch in torchtitan will still load without any error after this change, but it will silently use different rotary frequencies. Only the ~half of frequencies with wavelength > 2048 change, but they apply at every position. I think that's the right tradeoff, since the flavors exist to match the HF checkpoints. Please mention it in the release notes / PR summary so users resuming 1B/3B runs aren't surprised.

Test (tests/unit_tests/cpu/models/llama3/test_rope_scaling.py)

Comparing against transformers' ROPE_INIT_FUNCTIONS["llama3"] is a good independent oracle. Taking angle(cache[1]) to get the inverse frequencies is safe because every inv_freq is <= 1 < pi, so there's no wraparound.

  1. (Suggestion) Use rope_parameters= instead of the legacy rope_scaling= + rope_theta= kwargs (lines 35-47).
    • Why: CI installs unpinned transformers (.ci/docker/requirements-dev.txt), and the repo is on transformers 5.x elsewhere (5.9 / 5.10.1). On 5.x, rope_scaling only works through a backward-compat shim. The existing test in this repo already uses the 5.x form (torchtitan/experiments/transformers_modeling_backend/tests/test_rope_init.py:68-75).
    • Suggested code:
      hf_config = LlamaConfig(
          hidden_size=config.dim,
          num_attention_heads=config.layers[0].attention.n_heads,
          max_position_embeddings=131072,
          rope_parameters={
              "rope_type": "llama3",
              "rope_theta": 500000.0,
              "factor": factor,
              "low_freq_factor": 1.0,
              "high_freq_factor": 4.0,
              "original_max_position_embeddings": 8192,
          },
      )
    • Check: whether the ROPE_INIT_FUNCTIONS[...](config, device) signature is stable across the versions CI may pick up.
    • Fix this ->
  2. (Nit) You could add 70B and 405B (both 8.0) to _PUBLISHED_ROPE_SCALING to cover every published flavor. RoPE is built from the config alone, so they cost almost nothing to add.

Not verified

I couldn't run pytest in this environment because the command needed approval. I haven't independently confirmed the "7 passed" result or the HF logits numbers in the PR description.

@tianyu-l
tianyu-l merged commit 48bbce4 into pytorch:main Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants