Skip to content

core: rope_freq, transformers' RoPE inverse frequencies; Gemma 4's CUDA global table uses it - #1853

Merged
ashhart merged 3 commits into
ashhart:mainfrom
jschmied:rope-freq-core
Nov 2, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
jschmied:rope-freq-core

Conversation

@jschmied

@jschmied jschmied commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Part of #1852. One RoPE inverse-frequency helper in core, and the one copy whose words don't change moved onto it.

  • core/rope_freq.zig: inverseFrequencies(theta, dim, out) (default rope; partial rotary = the rotated width) and proportional(theta, dim, rotated, out) (Gemma 4's global rope, zeros past rotated). Form: 1 / f32(pow_f64(theta, f32(2i) / f32(dim))), transformers' words under torch's scalar CPU path.
  • Tests: transformers' words as literals (torch 2.15.0, transformers 5.22.1) for theta 1e7 over 64 (Qwen3.5 / Flash Next / Qwen3.8-27B / MOSS), 5e6 over 64 (Spark2.5 full layers), 1e4 over 36 (Qwen-VL tower heads) and Gemma 4's proportional global table (1e6, 64 of 256 pairs over 512). Planted control: the fp32-pow form misses 15 of 32 words, by up to 5 ulp.
  • gemma4/cuda_forward.zig: the global table calls rope_freq.proportional. Its words are unchanged: a test keeps the old expression and checks all 256 words against the helper for the served config (the same in the E4B, 12B, 31B and assistant fixtures). Swapping the old expression for the f64-reciprocal form makes that test fail.

No other copy changes here: the ones in #1852 change words, so they move only if you want them to. spark25/config.zig and glm/vision.zig already equal the helper, but need core reachable from their module trees first.

Checks: lean 0 new, zig build test and the CI set pass, git diff --check clean, test-gemma4-cuda-host 54/54 (GB10 host).

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Our GB10 run on Gemma 4 31B (main against this change) is GREEN: prompt tokens identical in all 20 rows, 2K prefill 994.7 -> 995.7 tok/s (+0.1%), and decode 26.6 -> 26.5 tok/s (base's own runs were 26.66 and 26.49, so that's within noise). The gate passes on current main too, so like #1864, it lands as soon as it's marked ready for review.

@jschmied
jschmied marked this pull request as ready for review November 2, 2026 03:48
jschmied and others added 3 commits November 2, 2026 08:31
…frequencies in fp32

1 / theta^(f32(2i) / dim) with theta^e rounded once from f64 and an fp32 reciprocal: torch's words wherever its
pow is correctly rounded (its default CPU path everywhere). Tests: transformers 5.22.1's words for four real
configs as literals, and a planted control (the fp32-pow form misses 15 of 32 words).
The replaced expression and the helper give the same 256 words for Gemma 4's global layers (theta 1e6, 64 of
256 pairs over 512); the test keeps the old expression and checks it word for word.
@ashhart
ashhart merged commit 6fb7c4d into ashhart:main Nov 2, 2026
4 of 9 checks passed
@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

laudney added a commit to laudney/TensorFold that referenced this pull request Nov 2, 2026
Upstream main added the ashhart#1853 entry at the top of the Unreleased
section after the last merge, so CHANGELOG.md conflicted again. This
merge keeps main's entries and this branch's one entry above them;
the other files merged without a change from this branch.
@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

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