Skip to content

nemotron metal: one decode attention partial source for its 16 host names (#2251) - #2263

Merged
ashhart merged 3 commits into
ashhart:mainfrom
jschmied:metal-unify-partials
Nov 6, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
jschmied:metal-unify-partials

Conversation

@jschmied

@jschmied jschmied commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

The first step of #2251, as asked there: Nemotron's 16 Metal decode attention partials become one source.

What changes

  • nemotron/attn_partial_1.metal … attn_partial_16.metal differed only in the template's row count SG and the host name. They become one nemotron/attn_partial.metal holding the template once.
  • kernels.zig appends each key's one instantiation under its old host name, so all 16 host names and keys stay as they were.
  • Unchanged: the per-key runtime compile (CompileOptions.mlx()), the library each kernel compiles into, and the paged twins.
  • 17 files, +28 −1581; zig/kernels/metal goes from 367 to 352 files.
  • tools/zig/gen_nemotron_kernels.py left the tree at 1.0.0, so kernels.zig is edited by hand.
  • One visible effect: nemotron.sourceHash() follows the source text. Nemotron prompt states saved before this get a new identity and are recomputed once, although the compiled code is the same.

Same results before and after (M3 Pro, main cde4fe7d9 against this branch)

  • Kernel contract (tf-kernel-contract --only nemotron --rotary-accuracy rotary.json; the rotary record comes from tf-rotary-sweep, COMPLETE over 302,008,320 cases):
    • main: SUMMARY 63 kernels: 63 pass gates 1-4, 0 fail, 1 known findings; 29 controls: 29 red as planted, 0 missed; 0 limit problems
    • branch: the same line.
    • The two 1,004-line outputs differ only in the SUMMARY's timings.
    • The partials are covered directly: decode attn_partial_N + merge in metal_nemotron_attention.zig, the paged twins against attn_partial_N's bytes in metal_nemotron_pages.zig, and metal_nemotron_unroll.zig, with their records in core/contract/nemotron_attention_records.zig.
    • The unfiltered contract stops at Gemma 4 with TensorUnitsRequired on this Mac, on main as on the branch, so the run is filtered to Nemotron.
  • Compiled code: the LLVM IR of all 16 kernels is identical old against new (xcrun metal -std=metal4.1 -S -emit-llvm, in both the safe and fast-fp32 modes; only the source path is normalized).
  • Tests:
    • test-metal-kv-pages 45/45.
    • zig build test 691/691 steps (10695 of 10776 passed, 81 skipped).
    • Lean 0 new; fmt and diff-check clean.
  • Served: nemotron-lightning greedy, 64 tokens, main and branch identical (token sha 76b25cdd1535).
  • GB10 (Linux, no Metal): lean, zig build test, the no-GPU CI set, git diff --check and zig fmt --check pass.

The other #2251 groups follow one PR each, starting with the diffed ones. group_norm_xs is next.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. With the name lines removed the 16 files really were one body, the 16 host names keep their own row counts, and identical IR in both fp modes settles the bits. One thing before it lands: since nemotron.sourceHash() follows the source text, saved Nemotron prompt states get a new identity and the first long prompt after upgrading prefills again. Please add a CHANGELOG line under Unreleased that says so. It's the one user-visible effect.

jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 5, 2026
@jschmied

jschmied commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Added in 7ef13dc3a, under Unreleased: the partials share one source with the same kernels and bits, and saved Nemotron prompt states on Metal are not reused after upgrading, so the first long prompt prefills again and is kept as before from then on. CHANGELOG only; git diff --check clean.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied, that's the line. Ready to land.

@jschmied
jschmied force-pushed the metal-unify-partials branch from 7ef13dc to 5a717f9 Compare November 5, 2026 19:26
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 5, 2026
jschmied added a commit to jschmied/TensorFold that referenced this pull request Nov 5, 2026
@jschmied
jschmied force-pushed the metal-unify-partials branch from 5a717f9 to a25a69a Compare November 5, 2026 19:27
@jschmied

jschmied commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 36f849362. Only CHANGELOG conflicted, and the code commit is unchanged (git range-diff: =). The Unreleased line now reads "Saved Nemotron prompt states on Metal take their identity…", as quoted in my last comment; the pushed line had missed "on Metal".

Re-checked on the rebased tree:

  • M3 Pro: tf-kernel-contract --only nemotron gives the same SUMMARY as main (63 pass gates 1-4, 29/29 controls red). test-metal-kv-pages passes, and nemotron-lightning greedy 64 tokens is identical to main (token sha 76b25cdd1535).
  • GB10: lean (0 new), zig build test, the no-GPU CI set, diff-check and fmt pass.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Our M5 Ultra run on this head, Nemotron 3.5 Lightning MLX 4-bit, against its merge base with main: no token differences in 20 prompt rows. Prefill moves -0.4% to +0.2%, decode after a prompt -0.5% to +0.2%, and the stream sweeps -0.8% to -0.2%. The sweeps all lean slightly down, but the 16 kernels compile to the same IR and the repetitions overlap, so we read it as run-to-run spread, not a change. It's in the landing queue.

@ashhart
ashhart force-pushed the metal-unify-partials branch from a25a69a to aae6c0c Compare November 6, 2026 00:21
ashhart pushed a commit to jschmied/TensorFold that referenced this pull request Nov 6, 2026
@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. One small thing turned up at landing, and I've pushed the fix (a5f1f0836) to your branch.

attn_partial.metal is now embedded verbatim in the binary, so its first comment ("... kernels.zig appends one instantiation a key.") ends up in tensorfold-native just after a NUL byte. The release path check (zig/build/release_paths.zig) reads a string there that starts with / as a quoted path, and a name ending in .zig inside it as a leaked source file, so check-release-paths refused the build. The comment now says "the kernel list adds one instance a key". With that, check-release-paths passes (68 of 68 steps), and lean has no new finding. Nothing else changed, and the compiled kernels are the same, since only a comment moved. It's back in the landing queue.

@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Our M5 Ultra kernel contract on this head (a5f1f0836), tf-kernel-contract --only nemotron: 56 kernels pass gates 1 to 4, with 0 failing and 2 known findings; all 33 controls are red as planted, and there are 0 limit problems. So the one shared source keeps every one of the 16 partials' results on the chip with tensor units too. It's in the landing queue.

jschmied and others added 3 commits November 6, 2026 11:12
… each of its 16 host names (ashhart#2251)

The 16 attn_partial_N.metal files differed only in the SG template argument. attn_partial.metal holds the
template once, and kernels.zig appends each key's single instantiation under its old host name, so each key
still compiles alone at run time with CompileOptions.mlx() under the same function name; the KV page twins
build from attn_partial_1's source as before. The generator these came from left the tree in 1.0.0, so
kernels.zig is edited by hand. Nemotron's sourceHash changes with the text, so learned prompt states saved
before this do not match.

On an M3 Pro: the 16 kernels' LLVM IR (metal -std=metal4.1, safe math, fast fp32 functions) is identical
before and after; tf-kernel-contract --only nemotron prints the same 1,004 lines on main and here apart from
its timings (63 kernels pass gates 1-4, 29 controls red); a Nemotron greedy run gives the same 64 tokens.
…le, which the release path check reads as a path
@ashhart
ashhart force-pushed the metal-unify-partials branch from a5f1f08 to 90f51d9 Compare November 6, 2026 11:12
@ashhart
ashhart merged commit 6433d51 into ashhart:main Nov 6, 2026
7 checks passed
ashhart pushed a commit that referenced this pull request Nov 6, 2026
@ashhart

ashhart commented Nov 6, 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.

@ashhart

ashhart commented Nov 7, 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