Skip to content

Keep rotary angles in float32 after model dtype conversion - #2173

Open
sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/keep-rotary-positions-in-full-precision
Open

sylvesterkaczmarek wants to merge 2 commits into
openai:mainfrom
sylvesterkaczmarek:fix/keep-rotary-positions-in-full-precision

Conversation

@sylvesterkaczmarek

@sylvesterkaczmarek sylvesterkaczmarek commented Oct 5, 2026 •

Copy link
Copy Markdown

Problem

The baseline converts the model to bfloat16, including Rotary's floating inverse-frequency buffer. Rotary then creates its position sequence in that buffer's dtype. Positions 256 and 257 become identical, and frequency rounding introduces further angular error.

A CPU reproduction is Rotary(64).bfloat16()(1024, torch.device("cpu"), torch.float32): positions 256 and 257 have identical cosine and sine tables. The maximum cosine difference from the float32 construction is approximately 1.9684.

Change

Keep nonpersistent integer frequency indices and construct inverse frequencies and positions explicitly in float32 whenever the cache is rebuilt. The integer buffer still follows device moves but survives model dtype conversion. Cache reuse, requested output dtype, and checkpoint keys are preserved. The baseline remains under its 1,500-line limit.

PyTorch documents the reduced-precision/repeated-value risk in torch.arange.

Validation

  • 18 CPU tests passed. Fourteen fail against unchanged production code, while four float32 controls pass.
  • Independent float64 references cover float32/float16/bfloat16 model conversions, warm and cold caches, neighboring positions, output dtypes, and rotary application gradients.
  • Autocast tests pass in eager execution and full-graph capture with torch.compile(backend="eager"), including cache rebuilds at different sequence lengths.
  • A bfloat16 grouped-query attention forward/backward smoke test passes with finite outputs and gradients.
  • Ruff syntax/error checks and git diff --check pass.

Python 3.12.14 and PyTorch 2.14.1 on macOS arm64. No model or dataset downloads, CUDA kernels, distributed training, or benchmark runs were used. Graph capture does not establish CUDA/Inductor performance. This changes future baseline calculations; it does not modify archived submissions or claim a leaderboard improvement.

Reproduced against f5c079314c4877fbb0af378c0abade5a8ca33d3a.

Maintenance validation on 6 November 2026

Added a README note explaining that float32 rotary calculations can change future baseline training results while archived submissions and scores remain unchanged. This addresses the independent reproduction and documentation suggestion in the discussion.

Focused validation at 2a107cbe4b48: 18 CPU tests passed. The combined six-PR checkout passed 121 tests, including the actual PyTorch and MLX CPU implementations. The two baseline scripts remain below 1,500 lines; archived record scripts were not modified.

Python 3.12.14, PyTorch 2.15.0 and SentencePiece 0.2.3 on macOS arm64. Tests used synthetic inputs with external sockets disabled. No pretrained checkpoint, external dataset, GPU training, full validation benchmark or leaderboard measurement was used. There are no hosted test runs on these PR heads. Earlier validation counts above are historical.

@Evreu1pro

Evreu1pro commented Oct 22, 2026 •

Copy link
Copy Markdown

Reproduced this on main (torch 2.14.1, CPU), using the Rotary class straight from train_gpt.py with the baseline head dim (512 / 8 = 64). It matters in the real training path. The model gets .bfloat16() at L838, and restore_low_dim_params_to_fp32 only walks named_parameters(), so the inv_freq buffer stays bf16. With the default TRAIN_SEQ_LEN=1024:

  • torch.arange(1024, dtype=torch.bfloat16) has only 513 distinct values: exact up to 256, then steps of 2, then steps of 4 past 512
  • so there are only 513 distinct (cos, sin) rows across the 1024 positions, and max |cos diff| vs the fp32 table is 1.968 (same as your number)

So in the second half of every training sequence, groups of ~4 neighbouring tokens get the same rotary angle. Not fatal, since the causal mask still separates them, but it's a real handicap for a baseline.

Some context that might help review: I grepped records/track_10min_16mb/*/train_gpt*.py. The early records copy the baseline pattern (dtype=self.inv_freq.dtype, or a local inv_freq = self.inv_freq.to(device) that is still bf16). The record scripts from 2026-04-10_VarLenAttn onward do self.inv_freq.float() plus dtype=torch.float32. So the record line already fixed this along the way, and only the baseline people start from still has it. That seems like a decent argument for merging. Fixing it will probably move the naive-baseline number a bit (I haven't measured that on GPU), so it might deserve a line in the README/changelog.

Copy link
Copy Markdown
Author

Thanks for independently reproducing this and checking the record scripts. I added the README note you suggested: the precision correction can change future baseline runs, while archived submissions and scores remain unchanged. The 18 rotary tests still pass, and the combined baseline-fix checkout passed 121 CPU tests across PyTorch and MLX. I have not measured a GPU training or leaderboard improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants