Skip to content

metal: the two pairs of u32 copy kernels from one source (#2251) - #4740

Merged
ashhart merged 3 commits into
ashhart:mainfrom
jschmied:dedup-2251-copy-u32
Nov 24, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
jschmied:dedup-2251-copy-u32

Conversation

@jschmied

@jschmied jschmied commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2251 (group: the two copy pairs). Head e8e86f0, two commits on main b192eaa.

Review 7105928679: Drama 3's metallib step now lists core/copy_u32.metal as an input of the codec.metal compile, beside d3_common.h (zig/build/drama3_metal.zig). Checked on Linux with a stub xcrun that only writes outputs, zig build -Dtarget=aarch64-macos drama3-metal-quant-check --summary all: with nothing changed every .air is cached; an edit to copy_u32.metal alone reruns d3_codec.air only; the same edit without the change reruns nothing. On a Mac (M3 Pro), zig build drama3-generate --summary all on e8e86f0 rebuilds d3_codec.air after touching or editing only core/copy_u32.metal; on the parent 478fcb6 it stays cached. No shader text changed, so the rows below (on 478fcb6) still hold. x86 full set on e8e86f0: zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths 2431/2431 steps, 25036/25195 tests (159 skipped); check-native-cuda-host compile-cuda-tests 17/17; pr_check 0 errors; lean 165 vs 165, 0 new; zig fmt --check and git diff --check clean.

What changes

  • Two pairs of u32 copy kernels had the same bodies:
    • glm_copy_u32 (glm_glue.metal) and Drama 3's c_copy (drama3/codec.metal): n values, bounds-checked;
    • Nemotron's tf_copy_u32 (nemotron_glue.metal) and Flash Next's pf_copy (prefill/fn_prompt.metal): one value a thread.
  • They now come from TF_COPY_U32_N(NAME) and TF_COPY_U32(NAME) in the new core/copy_u32.metal. c_copy keeps its max_total_threads_per_threadgroup(256). Names and buffer indices are unchanged; pf_copy's arguments, S and T, are now src and dst like the others'.
  • How the macros reach each library:
    • sources.zig puts core_copy_u32 ahead of glm_glue, nemotron_glue and flashnext_prompt;
    • drama3/codec.metal includes ../core/copy_u32.metal, which the offline Drama 3 metallib resolves; sources.drama3.codec (the text the kernel contract compiles) has it inlined at comptime.
  • 6 files, +30 −20.

Core vs backend-only: core/copy_u32.metal is the shared Metal piece; the rest is each family's Metal source and its assembly in sources.zig. Drama 3's CUDA c_copy is untouched.

Same bits (M3 Pro, macOS 27.2, metal 32023.921, Zig 0.17.0; main b192eaa against this head)

  • IR: the runtime libraries emitted on both arms (with and without tensor units) change only in nemotron_glue, the GLM glue and the two Flash Next prompt libraries. Each compiled with -std=metal4.1 -S -emit-llvm under its manifest's flags:

    • nemotron_glue (10 functions) and the GLM glue (23): identical;
    • the Flash Next prompt libraries (16 functions each): every function identical; the only metadata change is pf_copy's air.arg_name (S/T to src/dst);
    • Drama 3's codec as the offline metallib compiles it (precise): 14 functions identical; as the contract compiles it, under precise and fast: identical;
    • drama3.metallib from zig build drama3-generate: byte-identical on both arms (md5 af34df1a184d5d3259d4ee72a1dd41c0), the same 43 functions.
  • Kernel contract, tf-kernel-contract --only, main = head:

    • glm: 108 pass gates 1-4, 0 fail; 76/76 controls red; 0 limit problems;
    • drama3: 14 pass, 0 fail; 0 limit problems;
    • nemotron: 67 pass, 0 fail, 1 known; 29/29 red; 0 limit problems;
    • flashnext: 64 PASS lines and PASS limits: 42 selected registered Metal launches against M1-M5, 0 problems, identical on both arms. The --only flashnext run then stops with error: UnknownLaunch before its SUMMARY line, on main as on this head: metal_drama3.zig's source lookup is reached from that filter. Not from this change; I can open an issue for it if you want one.
  • Family checks: test-glm-copies-host test-nemotron-copies-host test-kernel-symbols-host test-flashnext-glue-contract-host: 21/21 steps, 66/66 tests. Drama 3's host set (check-drama-metal-host, test-drama-metal-quant-host, check-drama-codec-bindings, the prefill/owner/host/cfg/nvfp4-reference/combine-stress tests): 35/35 steps, 111/113 (2 skipped).

  • Served hash: not run. Every served kernel's IR is identical and the Drama 3 metallib is byte-identical, so no reply can change; for the same reason no seeded, resume, concurrency or refusal checks against main.

  • x86 CI green on a merge of this head with three sibling metal: unify duplicated kernels (attention partials, norms, gemm_nax wrappers, glue, RoPE) #2251 groups: merge 72b153f490 of b83b26a, a3e5359, 478fcb6 and ebf6db7 on main b192eaa (the groups touch separate files, apart from separate hunks of sources.zig in the copy and vision groups). zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths: 2431/2431 steps, 16239/16350 tests (111 skipped); check-native-cuda-host compile-cuda-tests 17/17; zig fmt --check and git diff --check clean. pr_check and lean flagged one new multi-line comment in the prompt-pick group's core/prompt_pick.metal; on the merge with that one-line fix (16ea8fe6e4, prompt pick at 7cd71d9): pr_check 0 errors, lean 165 vs 165 on main, 0 new.

Proposed CHANGELOG line (for you to place):

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied for consolidating these copy kernels. The wrappers keep the existing kernel names, arguments and bounds.

One build dependency needs adding. The new shared copy include isn't registered with Drama 3's Metal library compile step. If only that include changes, a rebuild can reuse the cached codec library with the old copy kernel while other kernels get the update.

Please register the shared file as an input to that compile.

  • Synthena

@jschmied

Copy link
Copy Markdown
Contributor Author

Done in e8e86f0932: Drama 3's metallib step now lists core/copy_u32.metal as an input of the codec.metal compile, beside d3_common.h (zig/build/drama3_metal.zig).

How it was checked, on Linux with a stub xcrun that only writes the outputs, zig build -Dtarget=aarch64-macos drama3-metal-quant-check --summary all:

  • nothing changed: every .air cached;
  • an edit to copy_u32.metal alone: d3_codec.air reruns, the other seven stay cached;
  • the same edit without this change: nothing reruns, which is the stale codec you described.

On a Mac (M3 Pro), zig build drama3-generate --summary all on e8e86f0 rebuilds d3_codec.air after touching or editing only core/copy_u32.metal; on the parent 478fcb6 it stays cached.

No shader text changed, so the Mac rows in the body still hold. x86 full set on e8e86f0932: zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths 2431/2431 steps, 25036/25195 tests (159 skipped); check-native-cuda-host compile-cuda-tests 17/17; pr_check 0 errors; lean 165 vs 165, 0 new; zig fmt --check and git diff --check clean.

@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks, @jschmied. The stale codec rebuild issue is fixed: the codec compile now explicitly tracks the shared copy include. The cache invalidation check addresses the exact failure from the previous review.

The two macro forms preserve the original bounds, kernel names, buffer positions and threadgroup attribute, and the source assembly supplies each consumer consistently. Combined with the supplied identical IR and codec library, I found no runtime byte or token change.

No additional device cells are needed from this review.

  • Synthena

jschmied and others added 3 commits November 24, 2026 12:23
glm_copy_u32 and Drama 3's codec c_copy (n values, bounds-checked) and Nemotron's tf_copy_u32 and Flash Next's
pf_copy (one value a thread) had the same bodies. They now come from TF_COPY_U32_N and TF_COPY_U32 in
core/copy_u32.metal. c_copy keeps its 256-thread reservation; names and buffer indices are unchanged, and
pf_copy's arguments are named src and dst like the others. The offline Drama 3 metallib includes the core file;
the embedded codec text has it inlined.
…nput

The codec includes the shared u32 copy macros, but its compile step only listed d3_common.h, so a change to
copy_u32.metal alone reused the cached codec .air with the old copy kernel.
@ashhart
ashhart force-pushed the dedup-2251-copy-u32 branch from e8e86f0 to e2fa228 Compare November 24, 2026 12:23
@ashhart
ashhart merged commit 1baa88e into ashhart:main Nov 24, 2026
2 of 12 checks passed
@ashhart

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

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