Skip to content

RoPE: Qwen3.8-27B on XPU from core's exact helper (#1852 batch 8; 1-ulp value changes) - #2000

Merged
ashhart merged 3 commits into
mainfrom
lead/1852-rope-89
Nov 17, 2026
Merged

ashhart merged 3 commits into
mainfrom
lead/1852-rope-89

Conversation

@ashhart

@ashhart ashhart commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Part of #1852, audit batch 8. Qwen3.8-27B's RoPE tables on XPU now come from core's rope_freq, so they hold transformers' fp32 words. Values move by exactly 1 ulp.

  • Both target loaders (xpu/load.zig: load and loadGptq) and the DFlash2 drafter (xpu/drafter.zig) take their tables from a new xpu/rope_table.zig: inverseFrequencies(1e7, 64) for the target's 32 words, (1e7, 128) for the drafter's 64.
  • Target (θ 1e7 over 64): 15 of 32 words move. Drafter (1e7 over 128): 18 of 64 move. The largest change is 2.98e-8 absolute, 1.18e-7 relative.

Why the words move: the old code was the more precise one. Both old forms did all the arithmetic in f64 and rounded to fp32 once: the loaders as f32(1e7^(-i/32)), the drafter as f32(1 / 1e7^(2i/128)). rope_freq does what transformers does: it rounds 1e7^(2i/d) to fp32, then takes the reciprocal in fp32, so it rounds twice. Each moved word is one where that second rounding lands 1 ulp away, and on every moved word the old value is the nearer one to the exact 1e7^(-2i/d). So this change matches transformers' bits; it is not an accuracy fix. θ and the exponents are exact in both fp32 and f64, so nothing else differs.

The new target words equal the transformers 5.22.1 words that rope_freq's own test pins for 1e7/64. The drafter's even words are those same words; its three moved odd words are correctly rounded at each step but not yet checked against torch.

Tests: zig build test-rope-xpu-host (new step; also part of zig build test) prints the old and new tables side by side and checks the exact moved indices, at most 1 ulp, and the pinned words. With test-slide-xpu-host test-draft-xpu-host test-xpu-refusal-host test-qwen27-config-xpu-host: 24/24 steps, 19/19 tests. A planted @compileError in each of the three changed call sites fails test-slide-xpu-host, so that step compiles all three.

The Qwen4 rotary table and its reference oracle, which this PR used to carry, landed in #2223. This PR is now the XPU change alone and no longer touches tensorfold.zig.

Before landing, on an XPU (Arc): a 27B greedy token A/B of old and new builds on the same ids, and drafted equals undrafted (XPU_VERIFY_TEST, XPU_DRAFTER with XPU_TREE=15, XPU_MULTI_TEST).

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner Author

Device check: zig build test-qwen4-metal (native Qwen4 Metal logits, picks and state exactness on a GPU, M3 Ultra) passes at this head: 86/86 steps. The Qwen4 host gates also pass: test-qwen4-host, -reference-host, -device-host and -cuda-nodes-host. The tree has no GPU-run Qwen4 CUDA step yet, so on CUDA the host gates are the check. Still owed: the Qwen3.8-27B XPU token A/B on an Arc GPU (drafted equal to undrafted). We have no Arc box, so this waits for one.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

The Qwen4 half of this is now #2223, which has passed its host gates. This PR keeps the Qwen3.8-27B on XPU half, which needs a token A/B on an Arc GPU that we don't have, so it stays open for anyone who can run that.

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner Author

@Mirrdhyn, since you have Qwen3.8-27B running on an Arc Pro B70: this PR moves the XPU target's and drafter's rotary tables onto core's exact helper (15 of 32 and 18 of 64 words move by 1 ulp). Could you run main against this head on the B70, greedy and seeded, with the prompts you used on #2460? Please post the token hashes, where any reply first differs (with its margin), and drafted against undrafted. No rush.

  • Synthena (Ash's Agent)

@Mirrdhyn

Copy link
Copy Markdown
Contributor

@ashhart main (merge-base 6194b8c31) against this head (dceb04b10) on one Arc Pro B70, with the #2460 protocol: run_2460_xpu_drafter.sh prompts p1-p4, 256 tokens, Qwen3.8-27B GPTQ 4/128 + z-lab/Qwen3.8-27B-DFlash2, serve --no-drafts as the undrafted reference, equality on tensorfold.token_sha256. Both trees carry the same local change that scopes the VRAM guard to one card (zig/src/xpu/rt.zig only). md5 base 267cb0f48b7305c85965fd6034c3ae32, head f6e53fc66318ccc8453441427f60895e; compute-runtime 26.35.39758.21, IGC 2.41.5.

  • Greedy: p1-p4 identical, main against head, drafted and undrafted.
  • temperature 0.8, seed 7: p1 identical; p2, p3 and p4 differ, the same way drafted and undrafted.
  • Drafted == undrafted on both binaries, 8/8 each (4 prompts × greedy and seeded).
  • First differing token in the seeded replies (ids from return_token_ids):
    • p2, index 23: three (main) / final (head); the reply then runs 114 against 246 tokens
    • p3, index 147: ],[ / ]])
    • p4, index 107: hous / housing

No margins yet: the XPU backend doesn't declare logprobs (neither zig/src/native/xpu.zig nor qwen27_served.zig sets .logprobs), so the server refuses them, and there is no teacher-forced logit harness for the 27B on XPU like nem_kl.zig for Nemotron. I can add one, or wire logprobs into the XPU host, if either helps; say which you'd rather have. Every difference is in a seeded draw; greedy doesn't move.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Thanks @Mirrdhyn, that's exactly the run we needed. Greedy is identical on all four prompts, drafted equals undrafted on both binaries, and the only differences are in seeded draws, where a 1-ulp move in the rotary table can flip a sample by design. That's enough for this change without margins, so no harness is needed for it.

Logprobs on the XPU host would still be welcome as their own PR, since they'd make every later XPU comparison easier. If you take it, the usual logprobs/top_logprobs fields with a host test would do.

On our side, the Qwen4 half already landed separately, so we'll cut this down to the XPU commit, fix the description, and land it.

  • Synthena (Ash's Agent)

@ashhart ashhart changed the title RoPE: Qwen3.8-27B on XPU and the Qwen4 rotary table from core's exact helper (#1852 batches 8-9; 1-ulp value changes) RoPE: Qwen3.8-27B on XPU from core's exact helper (#1852 batch 8; 1-ulp value changes) Nov 12, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
ashhart added a commit that referenced this pull request Nov 17, 2026
…inted commit

Eight are for PRs that haven't landed (#3457, #3560, #2979, #3588, #2824, #3233, #2000, #3038); they return when those PRs land. The ninth repeated #2878's line, which is already in its release.
…ords (value change)

Both target loaders (load.zig: load and loadGptq) and the DFlash2 drafter (drafter.zig, Drafter.init) take their
inverse frequencies from xpu/rope_table.zig: rope_freq.inverseFrequencies(1e7, 64) for the target's 32 words
(attn_prep: 64 rotated dims of 256), (1e7, 128) for the drafter's 64 (dprep: full 128-dim heads). Every word that
moves moves by exactly 1 ulp.

Target, theta 1e7 over 64: 15 of 32 differ; largest |old - new| 2.980e-8 (word 2), largest relative 1.181e-7.
  word  2 0x3ebaf81a -> 0x3ebaf81b   word 16 0x39a5cb5f -> 0x39a5cb60   word 22 0x37812dac -> 0x37812dab
  word  3 0x3e61f836 -> 0x3e61f835   word 18 0x38f22ce3 -> 0x38f22ce2   word 26 0x3609cf4b -> 0x3609cf4a
  word  5 0x3da50957 -> 0x3da50956   word 19 0x3892587f -> 0x3892587e   word 28 0x35494c56 -> 0x35494c57
  word  7 0x3cf11176 -> 0x3cf11177   word 20 0x3830df51 -> 0x3830df52   word 29 0x34f3499c -> 0x34f3499d
  word 10 0x3bd4ca14 -> 0x3bd4ca15   word 21 0x37d5c442 -> 0x37d5c441
  word 13 0x3abbd3ec -> 0x3abbd3ed

Drafter, theta 1e7 over 128: 18 of 64 differ; largest |old - new| 2.980e-8 (word 4), largest relative 1.181e-7.
  Even words 2j equal the target's word j, old and new, so 15 are the target's above at twice the index:
  4 6 10 14 20 26 32 36 38 40 42 44 52 56 58. Three odd words also move:
  word 25 0x3af19f03 -> 0x3af19f02   word 35 0x391bc44d -> 0x391bc44e   word 39 0x3863872d -> 0x3863872c

Why the words differ:
- Formula order: the loaders computed f32(1e7^(-i / 32)), a negative exponent with no reciprocal; transformers and
  rope_freq compute 1 / 1e7^(2i / d).
- Precision: both old forms kept everything in f64 and rounded to fp32 once (the drafter's f32(1 / 1e7^(2i / 128))
  took its reciprocal in f64). transformers rounds 1e7^e to fp32, then takes an fp32 reciprocal: two roundings.
  Every moved word is a case where those two roundings land 1 ulp from the single one. On all 18 moved words the
  old word is the nearer one to the exact 1e7^(-2i / d) (checked in 60-digit decimal). So this is a move to
  transformers' bits, not an accuracy fix.
- Theta and exponent handling contribute nothing: 1e7 is exact in fp32 and f64, and 2i / 64 and 2i / 128 are exact
  in both, so old and new raise the same base to the same exponent.

Test (zig build test, through the XPU host tests): rope_table.zig prints the old and new tables side by side with
each table's largest absolute and relative difference. The replaced expressions live only in the tests, as
references. The target's words are pinned to the transformers 5.22.1 literals that rope_freq's own test holds for
1e7/64. The drafter's are pinned to new 1e7/128 literals, each step correctly rounded in 60-digit decimal; their
even words are the 1e7/64 transformers words, and their odd words are not yet checked against torch. The tests
also assert exactly the moved indices and at most 1 ulp.

Not run on an XPU. Before landing it needs a token A/B of old and new xpu-qwen27 builds on the same ids, and
drafted == serial on the new build (XPU_VERIFY_TEST, XPU_DRAFTER with XPU_TREE=15, XPU_MULTI_TEST).

Refs #1852.
xpu/rope_table.zig's two tests (the target's and the drafter's words against their old expressions) ran only
under the aggregate test step. test-rope-xpu-host runs them alone, without devices or ocloc; the aggregate step
still depends on them.

Refs #1852.
@ashhart
ashhart merged commit 6d87164 into main Nov 17, 2026
2 of 11 checks passed
@ashhart
ashhart deleted the lead/1852-rope-89 branch November 17, 2026 09:39
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