Skip to content

metal: one gemm_nax kernel macro for the vision towers' and Spark2.5's and Llama's prompt GEMMs (#2251) - #2787

Merged
ashhart merged 4 commits into
ashhart:mainfrom
jschmied:gemm-nax-macro
Nov 11, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
jschmied:gemm-nax-macro

Conversation

@jschmied

@jschmied jschmied commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The next section-1 group of #2251: six copies of the same tfp::gemm_nax kernel-wrapper macro become one.

What changes

  • GLMV_GEMM (glm_vision), S5V_GEMM (step5/vision), G4V_GEMM (gemma4/vision), QWV_GEMM (core/qwen_vision), SP_GEMM (spark25/gemm) and LL_GEMM (llama/gemm) each defined the same kernel: buffers A, B, P, D, 256 threads, tfp::gemm_nax<T, TA, TB, AM, AN, false, 64, 128, 256, 2, 4>. QWV_GEMM fixed TA to false; SP_GEMM/LL_GEMM fixed T to bfloat16_t, TA false and TB true.
  • They become one TF_GEMM_NAX_KERNEL(NAME, T, TA, TB, AM, AN) in the new prefill/gemm_nax_kernel.metal; each family's lines call it with the arguments the old macros fixed spelled out. Kernel names and the 56 instantiations are unchanged.
  • sources.zig: a private gemm_nax (prefill/gemm_nax.metal ++ prefill/gemm_nax_kernel.metal) heads the six sources that started with prefill/gemm_nax.metal. gemm_nax.metal itself is unchanged, so check_prefill_ops.py and prefill_launch.py see the same file.
  • 8 files, +72 −108.

Same code before and after (M3 Pro, main cd6060a against this branch)

  • Preprocessed sources: each of the six sources, assembled as sources.zig embeds it with nax.h inlined, preprocesses (xcrun metal -std=metal4.1 -E -P) to byte-identical text on main and the branch.

  • Compiled code: the LLVM IR of all six is identical (-S -emit-llvm, -fmetal-math-mode=safe, with -fmetal-math-fp32-functions=fast and =precise; Gemma 4's with the G4V_OUT define its loader prepends), source path normalized.

  • Not run here: these GEMMs run on the tensor units; this Mac has none, so no GPU run. The evidence is the preprocessor and IR identity above.

  • Tests: native build; test-kernel-symbols-host, test-step5-host, compile-step5-host, test-spark25-host, test-gemma4-contract-host, compile-gemma4-contract-host, compile-gemma4-host, test-qwen-vision-weights-host, test-glm-image-cache-host, test-gemma4-tower-host, test-contract-images-host, test-vision-cli-host, test-qwen35-images-host pass. Lean 386 vs 386, 0 new; zig fmt --check and git diff --check clean.

  • x86 CI (Linux build + host tests on fa4cadf): green.

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

  • With the other metal: unify duplicated kernels (attention partials, norms, gemm_nax wrappers, glue, RoPE) #2251 group: gemm-nax-macro (fa4cadf) and bf16-matvec-unify (b010d38) both edit sources.zig on different lines; git merge-tree merges them cleanly in either order.

  • Overlap: of this PR's files, only sources.zig is also edited by other open PRs (30 of them); whichever lands later may need a small rebase there.

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. All 56 calls pass exactly the type, TA, TB and edge flags the old macros fixed, and the kernel body and attributes in zig/kernels/metal/prefill/gemm_nax_kernel.metal match what they had; the kernel names the loaders build are unchanged. Byte-identical preprocessed sources for all six, plus the IR in both fp32 modes, is the right proof for kernels that only run on the tensor units, and leaving gemm_nax.metal itself untouched so the prefill checkers see the same file was a good call. This is ready to land.

  • Synthena (Ash's Agent)

@ashhart
ashhart merged commit b21f43f into ashhart:main Nov 11, 2026
1 check failed
ashhart added a commit that referenced this pull request Nov 11, 2026
@ashhart

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

BobClawblaw added a commit to BobClawblaw/TensorFold that referenced this pull request Nov 12, 2026
…with its q, k and v biases and YaRN at HF's rounding points

The family admits model_type qwen2 for the checkpoints checked against Hugging Face: the q, k and v biases join
the projections' fp32 sums before their one rounding (ll_matvec_bias_* beside the shared ll_matvec_*, which stay
core/bf16_matvec.metal's; a prompt chunk's GEMM stores its sums through ll_gemm_f32_*, gemm_nax with fp32 output,
and ll_bias_round adds the bias and rounds once), and the rope carries transformers' static YaRN table for 128-wide
heads (rope_yarn.yarnOf; the 64-wide table unchanged), computed once at parse, with its attention factor on the bf16
cosines and sines. Config refuses a root rope_theta that disagrees with rope_parameters', a layer_types entry that is
not full_attention, and reads a null rope_parameters as absent. Every changed and new kernel is under the contract
with five more planted controls; the controls' source edits now name the shared matvec's loop and the GEMM macro
lines (ashhart#2787 left gemm_untransposed's edit matching nothing). --drafter is refused by name for a checkpoint no
drafter was checked with, Qualified.copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent, and the
template's lack of a think block is declared to the server. tools/zig/llama_f64_reference.py takes the Qwen2 layout.
ashhart pushed a commit to BobClawblaw/TensorFold that referenced this pull request Nov 12, 2026
…with its q, k and v biases and YaRN at HF's rounding points

The family admits model_type qwen2 for the checkpoints checked against Hugging Face: the q, k and v biases join
the projections' fp32 sums before their one rounding (ll_matvec_bias_* beside the shared ll_matvec_*, which stay
core/bf16_matvec.metal's; a prompt chunk's GEMM stores its sums through ll_gemm_f32_*, gemm_nax with fp32 output,
and ll_bias_round adds the bias and rounds once), and the rope carries transformers' static YaRN table for 128-wide
heads (rope_yarn.yarnOf; the 64-wide table unchanged), computed once at parse, with its attention factor on the bf16
cosines and sines. Config refuses a root rope_theta that disagrees with rope_parameters', a layer_types entry that is
not full_attention, and reads a null rope_parameters as absent. Every changed and new kernel is under the contract
with five more planted controls; the controls' source edits now name the shared matvec's loop and the GEMM macro
lines (ashhart#2787 left gemm_untransposed's edit matching nothing). --drafter is refused by name for a checkpoint no
drafter was checked with, Qualified.copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent, and the
template's lack of a think block is declared to the server. tools/zig/llama_f64_reference.py takes the Qwen2 layout.
@ashhart

ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

ashhart pushed a commit to BobClawblaw/TensorFold that referenced this pull request Nov 19, 2026
…with its q, k and v biases and YaRN at HF's rounding points

The family admits model_type qwen2 for the checkpoints checked against Hugging Face: the q, k and v biases join
the projections' fp32 sums before their one rounding (ll_matvec_bias_* beside the shared ll_matvec_*, which stay
core/bf16_matvec.metal's; a prompt chunk's GEMM stores its sums through ll_gemm_f32_*, gemm_nax with fp32 output,
and ll_bias_round adds the bias and rounds once), and the rope carries transformers' static YaRN table for 128-wide
heads (rope_yarn.yarnOf; the 64-wide table unchanged), computed once at parse, with its attention factor on the bf16
cosines and sines. Config refuses a root rope_theta that disagrees with rope_parameters', a layer_types entry that is
not full_attention, and reads a null rope_parameters as absent. Every changed and new kernel is under the contract
with five more planted controls; the controls' source edits now name the shared matvec's loop and the GEMM macro
lines (ashhart#2787 left gemm_untransposed's edit matching nothing). --drafter is refused by name for a checkpoint no
drafter was checked with, Qualified.copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent, and the
template's lack of a think block is declared to the server. tools/zig/llama_f64_reference.py takes the Qwen2 layout.
ashhart pushed a commit to BobClawblaw/TensorFold that referenced this pull request Nov 20, 2026
…with its q, k and v biases and YaRN at HF's rounding points

The family admits model_type qwen2 for the checkpoints checked against Hugging Face: the q, k and v biases join
the projections' fp32 sums before their one rounding (ll_matvec_bias_* beside the shared ll_matvec_*, which stay
core/bf16_matvec.metal's; a prompt chunk's GEMM stores its sums through ll_gemm_f32_*, gemm_nax with fp32 output,
and ll_bias_round adds the bias and rounds once), and the rope carries transformers' static YaRN table for 128-wide
heads (rope_yarn.yarnOf; the 64-wide table unchanged), computed once at parse, with its attention factor on the bf16
cosines and sines. Config refuses a root rope_theta that disagrees with rope_parameters', a layer_types entry that is
not full_attention, and reads a null rope_parameters as absent. Every changed and new kernel is under the contract
with five more planted controls; the controls' source edits now name the shared matvec's loop and the GEMM macro
lines (ashhart#2787 left gemm_untransposed's edit matching nothing). --drafter is refused by name for a checkpoint no
drafter was checked with, Qualified.copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent, and the
template's lack of a think block is declared to the server. tools/zig/llama_f64_reference.py takes the Qwen2 layout.
ashhart pushed a commit to BobClawblaw/TensorFold that referenced this pull request Nov 23, 2026
…with its q, k and v biases and YaRN at HF's rounding points

The family admits model_type qwen2 for the checkpoints checked against Hugging Face: the q, k and v biases join
the projections' fp32 sums before their one rounding (ll_matvec_bias_* beside the shared ll_matvec_*, which stay
core/bf16_matvec.metal's; a prompt chunk's GEMM stores its sums through ll_gemm_f32_*, gemm_nax with fp32 output,
and ll_bias_round adds the bias and rounds once), and the rope carries transformers' static YaRN table for 128-wide
heads (rope_yarn.yarnOf; the 64-wide table unchanged), computed once at parse, with its attention factor on the bf16
cosines and sines. Config refuses a root rope_theta that disagrees with rope_parameters', a layer_types entry that is
not full_attention, and reads a null rope_parameters as absent. Every changed and new kernel is under the contract
with five more planted controls; the controls' source edits now name the shared matvec's loop and the GEMM macro
lines (ashhart#2787 left gemm_untransposed's edit matching nothing). --drafter is refused by name for a checkpoint no
drafter was checked with, Qualified.copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent, and the
template's lack of a think block is declared to the server. tools/zig/llama_f64_reference.py takes the Qwen2 layout.
ashhart pushed a commit to BobClawblaw/TensorFold that referenced this pull request Nov 26, 2026
…with its q, k and v biases and YaRN at HF's rounding points

The family admits model_type qwen2 for the checkpoints checked against Hugging Face: the q, k and v biases join
the projections' fp32 sums before their one rounding (ll_matvec_bias_* beside the shared ll_matvec_*, which stay
core/bf16_matvec.metal's; a prompt chunk's GEMM stores its sums through ll_gemm_f32_*, gemm_nax with fp32 output,
and ll_bias_round adds the bias and rounds once), and the rope carries transformers' static YaRN table for 128-wide
heads (rope_yarn.yarnOf; the 64-wide table unchanged), computed once at parse, with its attention factor on the bf16
cosines and sines. Config refuses a root rope_theta that disagrees with rope_parameters', a layer_types entry that is
not full_attention, and reads a null rope_parameters as absent. Every changed and new kernel is under the contract
with five more planted controls; the controls' source edits now name the shared matvec's loop and the GEMM macro
lines (ashhart#2787 left gemm_untransposed's edit matching nothing). --drafter is refused by name for a checkpoint no
drafter was checked with, Qualified.copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent, and the
template's lack of a think block is declared to the server. tools/zig/llama_f64_reference.py takes the Qwen2 layout.
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