Skip to content

RoPE reads its angles from a table, and the backward rotation folds into the join (#88) - #89

Merged
feitreim merged 1 commit into
mainfrom
rope-table
Aug 5, 2026
Merged

feitreim merged 1 commit into
mainfrom
rope-table

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Phase 1 of #88.

RoPE's angles depend only on (position, pair) — T × HD floats, 1 MiB at the training shape — and the kernel recomputed a powf, a sin and a cos for every element of every rotated tensor. Two threads redid the same angle for the two halves of one rotated pair, and each loaded both halves, so the pass also moved twice its compulsory reads. bench_rope's own comment had named this in #70; nothing had moved the angles out of the inner loop.

The table. ops::rope_table(sequence_length, head_dim) builds the [T, HD/2] (cos, sin) couples on the host, with nn::Rope::sin_cos's expression verbatim. That is the point of building it host-side: the CPU reference and every device consumer now share one set of angles instead of comparing libdevice's sinf against the host's. It is uploaded once per workspace and read from L2 — 1 MiB against a 126 MiB L2, re-read N·H/T times.

The rotations. rope_forward / rope_backward become one thread per rotated pair: two loads, two table loads, four multiplies, two stores. Launches go from N·D threads to N·D/2.

The backward join. join_group3_rope folds the transposed rotation into the join that was already reading dQ/dK/dV and writing the [N,3D] panel, so the two backward.*_rope passes disappear rather than getting faster. The arithmetic is rope_backward's, elementwise and fp32, so the substitution owes exact parity and --kernel ops asserts it at zero tolerance against rope_backward + join_group3 run on device.

Measurement

Same-container A/B, BASELINE_REF=ea8e147 ./run.sh model profile, §13.9 shape. (The container this pair landed on runs the tcgen05 expert GEMMs 3–6× slower than a clean one — its absolute step is 838 ms where a standalone profile of the same baseline commit read 401.8 ms. Both halves saw it, which is what the same-container rule is for; the bandwidth-bound rows below are within 7% of their standalone values either way.)

row baseline candidate Δ
backward.q_rope 8.0388 — −8.0388
backward.k_rope 7.9717 — −7.9717
forward.q_rope 7.9446 2.5460 −5.3986
forward.k_rope 7.9206 2.5415 −5.3791
backward.qkv_proj.join 6.9178 4.6779 −2.2399
full step 838.2415 811.8184 −26.4231 (−3.15%)

Rows touched sum to −29.03 against a −26.42 step; the difference is three expert-GEMM rows drifting +0.50 / +0.60 / +0.64, which is this pair's noise floor and nothing this change goes near.

The forward rotations are 3.1× faster on the same bytes. The join is faster than the plain join it replaces (−2.24 ms) despite carrying the rotation: one thread per pair means half the threads and 8-byte accesses where the old join had one thread per element.

Standalone-baseline framing, since the A/B container understates the percentage: against the 401.81 ms profile of ea8e147, −29 ms of rope rows is −7.2% of the step.

Gates

  • --kernel ops — green, including the new zero-tolerance join_group3_rope vs rope_backward + join_group3 check and the existing rope y / rope dx checks against nn::Rope at 2e-6.
  • --kernel model — green (full Dense forward/backward + AdamW, Muon, both MoE substitutions, aligned tcgen05 linears, checkpoint v5, all overfit gates).
  • No GEMM epilogue touched, so no --kernel gemm; no flash kernel touched, so no budget moves.

Notes

rope_backward keeps its kernel and its parity check: it is the oracle join_group3_rope is tested against (§11 — the naive kernels are never deleted). rope_into lost its backward arm because the model no longer has a caller for it.

@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Note on the shas: both branches were rustfmt-ed after the measurements above. The diff from the measured commit is line wrapping only (git diff -w reduces it to nothing but a joined argument list) — measured 7e948cf / 29511f3, now 1ce7cee / 7782e5c.

@feitreim

feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner Author

Cross-check at unthrottled clocks (standalone ./run.sh model profile of each tree, one container each — corroboration, not the §10.1 gate). This PR's four rows on main:

row main ea8e147 with #89 stacked under #90 note
forward.q_rope 8.5304 — gone at #90; 2.55 with #89 alone
forward.k_rope 8.5300 — gone at #90; 2.54 with #89 alone
backward.q_rope 8.5950 — gone here
backward.k_rope 8.5831 — gone here
backward.qkv_proj.join 7.0409 5.0046 carries the rotation and is still cheaper

The forward rotations at 2.55 ms against 8.53 is the table on its own: same bytes, 3.3× the rate, and the difference is that the angles left the inner loop. #90 then removes even that.

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.

1 participant