Skip to content

metal: the image towers' bf16 rounding, threadgroup sum and max, softmax and LayerNorm bodies from one source (#2251) - #2948

Merged
ashhart merged 3 commits into
ashhart:mainfrom
jschmied:vision-glue-unify
Nov 13, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
jschmied:vision-glue-unify

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

The next section-1 group of #2251: the glue the four image towers had each copied. It builds on the gemm_nax macro (#2787, merged): vision_glue starts from its gemm_nax const. Head 7c7226e, two commits on main a0e2665.

What changes

  • GLM-5.3-Flash's (glm_vision), Step-5's (step5/vision), Qwen-VL's (core/qwen_vision) and Gemma 4's (gemma4/vision) towers each defined:
    • *_bf (round to bf16 and back);
    • a threadgroup sum (glmv_group_sum, s5v_group_sum with an unused t parameter; g4v_group_sum, qwv_sum);
    • Qwen also defined a threadgroup max (qwv_max).
  • They become tf_vis_bf, tf_vis_sum and tf_vis_max in the new prefill/vision_glue.metal. sources.zig's vision_glue (gemm_nax ++ it) heads the four towers' sources.
  • GLM's, Step-5's and Gemma 4's softmax kernels had the same body; Gemma's has no scale. That body is now TF_VIS_SOFTMAX_BODY(SCALE): TF_VIS_SOFTMAX_BODY(* scale) for GLM and Step-5, TF_VIS_SOFTMAX_BODY() for Gemma 4. Each kernel keeps its own signature and attributes.
    • The body is a macro, not a function. Even an inline helper for the max changes the compiled code's block layout, and the macro keeps it as it was.
    • Qwen's softmax already reduced through its helpers, so it keeps its own body, on tf_vis_max and tf_vis_sum.
  • Second commit: the one-rounding LayerNorm (bf16((x - mean) * rsqrt(var + eps) * w + b)).
    • s5v_layer_norm (bf16 input), qwv_layer_norm (fp32 input) and g4u_norm (input through g4u_in's add modes) had the same statistics and output loop.
    • They are now TF_VIS_LAYER_NORM_STATS(IN) and TF_VIS_LAYER_NORM_OUT(IN) in vision_glue.metal, where IN(i) reads one input value.
    • The body is split in two because each kernel declares its output row pointer where it did before. Declaring it in one place changed one side's IR.
    • GLM's glmv_layer_norm rounds twice and stays its own.
  • The heads kernels stay per tower: their head widths, bounds and arguments differ.
  • No kernel names, signatures or host code change. 6 files, +128 −226 over both commits.

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

Each source was assembled as sources.zig embeds it, with nax.h inlined, and compiled with xcrun metal -std=metal4.1 -S -emit-llvm. That ran under -fmetal-math-mode=safe, with -fmetal-math-fp32-functions=fast and with =precise. Gemma 4's source was compiled with the G4V_OUT define its loader prepends.

  • core_qwen_vision: the LLVM IR is byte-identical, source path aside.

  • The LayerNorm commit on its own: all three towers' IR is byte-identical to the first commit's.

  • glm_vision, step5_vision and gemma4_vision:

    • Every defined function's IR is identical to before, in order, once attribute-group and metadata ids are normalized.
    • The module's declarations, attribute groups and metadata are equal by content. Only their order and numbering in the module differ, because the shared helpers come first in the source.
  • Not run here: these towers run on the tensor units; this Mac has none, so no GPU run. The evidence is the IR above.

  • Tests: native build; test-kernel-symbols-host, test-step5-host, compile-step5-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 7c7226e): green, 700/700 steps; check-native-cuda-host compile-cuda-tests green; pr_check 0 errors; fmt, diff --check clean; lean 386 vs 386, 0 new.

Rebased onto a0e2665 after #2787 merged: git range-diff shows both patches unchanged, only sources.zig context lines moved; all checks above were rerun on the rebased head.

Not run: seeded, resume, concurrency and refusal reply checks against main, because the compiled IR is identical, so no served output can change.

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 11, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. We checked the claim independently: each image tower's library built from sources.zig exactly as the loader concatenates it (with G4V_OUT prepended for Gemma 4), compiled to LLVM IR with the same flags on main f0d8c8d50 and on 7c7226e54. gemma4_vision (94 functions), glm_vision (94), core_qwen_vision (92) and step5_vision (93) are the same code. The only differences are the source path in the debug metadata and, in two libraries, where the simd_sum/simd_max intrinsic declarations sit in the module. Keeping each kernel's output row pointer where it was, as you did, is what keeps the IR equal. It's queued to land once CI is green.

  • Synthena (Ash's Agent)

@ashhart
ashhart merged commit 1e8c7a6 into ashhart:main Nov 13, 2026
@ashhart

ashhart commented Nov 13, 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 added a commit that referenced this pull request Nov 13, 2026
Figures and scope corrected to the PRs (#2948, #2863, #2934, #2933, #2921, #2906, #2878, #2892, #2851, #2728, #3072,
#2915, #2864, #2854 and others), promises of later work removed (#2903, #2875, #1630, #1720), #3074 and #3047/#3046
said as their PRs scope them, the duplicate #2861, #2585, #2321 and #2719 entries and two copies of released 1.2.5
lines removed, #1953's line named for its PR, and a line for #3255. Released sections unchanged.
@ashhart

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