Skip to content

feat(glm): the image tower on vit full attention, behind TF_GLM_VISION_FUSED - #3790

Merged
ashhart merged 8 commits into
ashhart:mainfrom
ultradaoto:glm-vision-fused-attn
Nov 24, 2026
Merged

ashhart merged 8 commits into
ashhart:mainfrom
ultradaoto:glm-vision-fused-attn

Conversation

@ultradaoto

@ultradaoto ultradaoto commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor
TP=3 on 3 × GB10, first reply to one new image (s) main 7b2717a branch, switch unset branch, TF_GLM_VISION_FUSED=1
3840 × 2160 (10,794 prompt tokens) 65.66, 65.64 / 65.62, 65.52 65.87, 65.70 / 65.64, 65.53 11.25, 11.29 / 11.21, 11.27
1440 × 900 (1,746 tokens) 2.47, 2.39 / 2.44, 2.39 2.44, 2.41 / 2.46, 2.38 1.45, 1.42 / 1.44, 1.43
640 × 480 (444 tokens) 1.07, 0.64 / 1.06, 0.63 1.33, 0.63 / 1.06, 0.63 1.30, 0.60 / 1.02, 0.59
Four 1440 × 900 in one message (6,897 tokens) 9.65 / 9.62 9.80 / 9.59 5.81 / 5.78
Replies to prompts with images same bytes as main differ from main (see below)
Text-only replies same same

What this changes

This head replaces the first design of this pull request (a GLM-only attention kernel, glm_vis_attn), as my comment of 2026-11-18 said it would. The earlier description and its numbers were for that design and are gone from here.

The GLM image tower's attention runs, for each head, as two cuBLASLt products around glm_vis_softmax: scores [rows, seg] in fp32, the weights in bf16, then the values product. The scores matrix is [patches, patches] for each of 16 heads in each of 24 blocks, so its cost grows with the square of an image's patches, and on a large image the tower is most of the first token's time (#3614).

zig/src/cuda/vit.zig already has full attention over a picture's rows in one kernel: prefill_attention.cu's vision_kernel, which RADIO runs at head dim 80 and MOSS-Image at 128. With TF_GLM_VISION_FUSED=1, GLM's tower runs that kernel at head dim 64:

  • prefill_attention.cu: one new instance, vision_kernel<64, 8>, and its entry in vit.zig's table. Nothing else in either file changes. The 70 existing kernels of prefill_attention.fatbin (its 14 instances at sm_80, 86, 89, 120 and 121) disassemble the same in main's build and in this head's (cuobjdump -sass, a function at a time; main built at 8cbef97, and the kernel sources are unchanged from there to 7b2717a), and so do the 50 of glm_vision.fatbin. Both files are in gradient-serving.tsv, which carries their new hashes.
  • glm_vis_qk (glm_vis_qk.cuh): that kernel reads q, k and v from rows [N, 3 × 1024], which is how the tower's qkv linear leaves them, so GLM's q and k head norm and 2D rotation now happen in place in those rows. It is glm_vis_qkv's arithmetic, and the contract holds it to glm_vis_qkv's bytes.
  • cuda_vision_attn.zig holds both paths: a block is one glm_vis_qk launch and one attention launch a frame group, or the old loop, which moved there unchanged.

The switch is off by default, and with it unset the branch gives main's bytes for every image reply below.

What differs in the arithmetic, and why it stays a switch: vision_kernel takes its maximum a 64-key tile at a time and rescales its running sums, where the cuBLASLt path takes one maximum over a whole row before any weight is rounded to bf16. Both sum in fp32 and round the weights to bf16 before the values product. So the tower's features differ in their last bits and a reply to a prompt with images can differ from the unset path's. It is the arithmetic RADIO and MOSS-Image already serve with, and here it is held to the same float64 reference (below). What I have not done is measure the cuBLASLt path against that reference on the same inputs, so I cannot show "no worse than the current path" side by side; tell me if you want that and in what form.

Checks on this head

  • Head commit: 68119d4315 on main 7b2717a20d. Every number here was taken on that commit.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 221 on main and 221 on this head, 0 new findings.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test-cuda-host: 7,165 of 7,199 passed, 33 skipped, 1 failed: cli_config_test ("serve --config: expand reads the file …"). On main 7b2717a20d on the same box the same test fails: 7,174 of 7,208 passed, 33 skipped, 1 failed. The totals differ because the summary counts only the steps that did not come from the build cache.
    • zig build test-kernel-contract: exit 0. zig build -Dfixed-cpu test-gradient-serving test-glm-index-emulate test-glm-logprobs-host: exit 0.
    • zig fmt --check build.zig zig: clean. git diff --check: clean. python3 tools/zig/cuda_common_check.py: 51 local copies, 0 violations. python3 tools/pr_check.py --base origin/main --pr 3790: 0 errors, 0 warnings.
    • New host test: cuda_vision_attn.zig checks that a block is one q and k launch and one attention launch a frame group (a block of 128 rows a head, for segments of 4 to 6,528 patches) against a recording cuLaunchKernel; cuda_env_test.zig covers the switch's reader (unset, 0, 1, a typo refused).
  • CI: not run yet on this head.
  • Not run, and why: tf-cuda-test glm-vision (the comparison with vision_ref.py's torch runs): these boxes have no torch and no reference dumps. TF_GLM_VISION_FUSED=1 tf-cuda-test glm-vision MODEL F32 BF16 IMG runs this path through that test unchanged; I would be glad of a run from someone who has the dumps. Also not run: atc overlap; nvcc 13.2 and later (13.0 only here, so the records are 13.0's); chips other than GB10 on a device (the limits check covers sm_80, 86, 89, 120 and 121 from their compiled images); TP=1 and TP=2; video input (more than one frame group); TF_GLM_VISION_HOST=1 together with this switch; the whole device contract (only the two sets named below).

Receipt

  • Main against branch: the table above. Three GB10s on a switchless triangle, main 7b2717a20d and this head built on these boxes the same way. Six starts in one session in this order: main, branch unset, branch with the switch, branch with the switch, branch unset, main. A start: 13 greedy text replies hashed, 16 greedy text requests one at a time, then for each of three sizes two different synthetic screenshots (a 4-token question timed to its reply, then a 96-token description of the same image, hashed), then four screenshots in one message, then a 6-question image check with known answers. In the table, the two numbers before the slash are the two images of the first start of that kind and the two after it are the second start's. The 1.33 and 1.30 at 640 × 480 are a start's first image; the other starts' first images took 1.02 to 1.07 s.
  • Boxes it reaches: GLM-5.3-Flash on CUDA with --vision, rank 0's image tower, only with the switch set.
  • New switch? Default, accepted values, profile row: TF_GLM_VISION_FUSED, default off, exactly 0 or 1 (platform/environment.zig, docs/environment.md); core/profile.zig row for glm5_next, class arithmetic, syntax one; docs/profiles.md and the GLM recipe say what it changes.
  • Environment: Zig 0.17.0, nvcc 13.0 (cuda_13.0.r13.0/compiler.36424714_0), driver 580.159.03, Ubuntu 24.04.4, kernel 6.17.0-1018-nvidia, 3 × DGX Spark (GB10). GLM-5.3-Flash-Uncensored EXL3 at revision 07135ec082f8, --vision --parallel 4 --kv-cache fp8 --reasoning-effort high --decode-share 0.5, --drafter incoai/GLM-5.3-Flash-DFlash2 at 6cc6c481, TF_GLM_CLEAR_THINKING=1 TF_GLM_REQUEST_STARTS=1 TF_GLM_DENSE_BITS=8 TF_GLM_HEAD_BITS=8 TF_GLM_KDA_PAD=1 TF_ROCE=1, greedy.
  • Paths and platforms the change reaches, which I ran and which I could not: ran still images from 640 × 480 to 3840 × 2160 and four images in one message at TP=3 on GB10. Could not run other chips, video, TP=1 and TP=2.
  • Exactness:
    • Text: the 13 greedy text replies (8 prompts alone, 4 at once, one prompt of about 8K tokens) hash the same in all six starts.
    • Images, switch unset: the six 96-token descriptions hash the same on main and on the branch, in all four such starts.
    • Images, switch set: five of the six descriptions differ from main's and one (a 1440 × 900 image) is the same; all six are the same in both starts with the switch. I kept only the first 90 characters of each description, so I can't give an error count for the whole texts. In those: both paths read the title bar of one 4K image correctly and of the other wrongly, each in its own way (step 10851 comes out as "1085 1" on main and as "1085" with the switch); the 640 × 480 title bar that both kept texts reach is read the same. The 6-question image check passes 6 of 6 in every start.
  • Decode speed before and after: the tower does not run during text decode. The 16 text requests one at a time gave 58.15 and 58.70 tok/s on main, 58.15 and 58.62 on the branch with the switch unset, and 58.34 and 58.28 with it set.
  • Prompt speed before and after: for prompts with images, the table. Text prompts do not reach the tower.
  • A second request about the same image costs the tower again on both paths (a 96-token description of the same 4K image took about 67 s on main and 12.4 s with the switch).
  • Licence of any new weights or drafter: n/a.
Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces (zig/tests/cuda/contract_glm_vision.zig, family set glm_vision):
    • vision_kernel<64, 8>, through vit.Attention.full as the tower calls it. It replaces no kernel: the unset path stays. Reference: contract_attn.zig's float64 attention over each head's keys and values, the reference MOSS-Image's instance is held to. Its test is MOSS-Image's test type made generic over the head dim (contract_vit_full.zig); contract_moss_image_attn.zig is now that type at 128 with its name and shapes unchanged. Served shapes here: 2 heads at 6,528 and 1,024 keys, 16 heads at 196, 36 and 4 keys.
    • glm_vis_qk, through cuda_vision_attn.heads as the tower calls it. Registered as replacing glm_vis_qkv at segments of one row, where that kernel's head-major q and k are these rows: the shapes gate and the non-finite gate compare bytes with glm_vis_qkv run on the same inputs. Served shapes: 6,528, 1,024, 196 and 4 patches of 16 heads.
  • A planted control for each new kernel, and each one fails as planted: tf_ctl_glm_vis_qk_unrounded (the rotation of a head before its bf16 rounding) fails the shapes gate at 6,528 patches, its bytes differing from glm_vis_qkv's from byte 0. vision_kernel<64, 8> has none of its own: it is the same template as MOSS-Image's 128 instance, whose set has none either. Say if you want one.
  • Each backend's SUMMARY line and exit status, CUDA on GB10 (sm_121) with nvcc 13.0:
    • tf-cuda-test kernel-contract glm_vis, exit 0: (filtered) SUMMARY 2 kernels: 2 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems. Gate lines for glm_vis_qk: byte-identical to glm_vis_qkv's rows including non-finite inputs; five runs byte-identical, inputs and guards untouched; 4 shapes bit-equal to the kernel it replaces. For vision_kernel<64, 8>: outputs equal the float64 reference's classes with NaN and both infinities planted; five runs byte-identical; 9 shapes exact by its own check; exact sums at k 6,528, where bf16 sums would miss by 256x.
    • tf-cuda-test kernel-contract "moss image vit", exit 0: (filtered) SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 0 limit problems, for the moved test type.
    • Limits: kernel-contract-limits on fatbins of sm_80, 86, 89, 120 and 121 reports the three new records current on all five (132, 132, 132, 120, 120 registers at 256 threads and 36,864 dynamic shared bytes for vision_kernel<64, 8>; 27, 27, 27, 25, 25 for glm_vis_qk), and the live check on sm_121 passes them. That command exits 1 on this head with 21 stale records, none of them these: nemo_audio (3), flashnext_mtp fn_row_topk (5), exl3_experts tf_exl3_prompt_gu and _down (10) and qwen35 tf_q35_gdn_slots_* (3). I did not run it on main, so I do not know if main reports the same 21.
  • Largest shape under the contract: 6,528 keys. The receipt's 3840 × 2160 image is 10,794 prompt tokens at about four patches a token, so its key count is several times that and ran only in the served receipt above.
  • Chips I could not run: everything but GB10 (sm_121) on a device.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The control is the failing form of glm_vis_qk's test; the host test needs no GPU.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Not measured for image prompts on this head; the text hashes include 4 at once against alone.)
  • No precision traded for speed. (The bits change with the switch set, as described above: fp32 accumulation and bf16 weights are kept, the maximum is taken a tile at a time, and the kernel meets the float64 reference's bound at 6,528 keys. I have not measured the cuBLASLt path against the same reference, so I leave this unticked.)
  • Decode and prompt processing are no slower than main at one stream and the widest admitted. (Text decode: level, see the receipt. Image prompts: faster from 1440 × 900 up, level at 640 × 480.)

@ultradaoto

Copy link
Copy Markdown
Contributor Author

Reworking this before review, so I am converting it to a draft.

zig/src/cuda/vit.zig already has full attention over a picture's rows in one kernel: prefill_attention.cu's vision_kernel, RADIO's at head dim 80 and MOSS-Image's at 128. I should have built on that instead of adding a GLM-only attention kernel. The rework, which I have locally on main 7b2717a2:

  • One new instance, vision_kernel<64, 8>, and its entry in vit.zig's table. The 70 existing kernels of prefill_attention.fatbin (its 14 instances at sm_80, 86, 89, 120 and 121) disassemble the same as main's build; gradient-serving.tsv carries the two files' new hashes.
  • glm_vis_qk: GLM's q and k head norm and rotation, in place in the rows that kernel reads. Registered as replacing glm_vis_qkv at segments of one row, bit for bit, with one planted control.
  • glm_vis_attn, glm_vis_attn_tiles and glm_vis_attn.cuh go away.
  • The MOSS-Image attention test's type becomes generic over the head dim (contract_vit_full.zig), so both towers run the same test against contract_attn.zig's float64 reference.

It stays opt-in behind TF_GLM_VISION_FUSED, and the default path is untouched. On that commit: zig fmt, lean 221 against 221, pr_check 0 errors, test-kernel-contract exit 0, test-gradient-serving exit 0, test-cuda-host 7,165 of 7,199 passed with 33 skipped and 1 failed (cli_config_test, which I have seen fail on main before; I have not rerun it on 7b2717a2).

Not run yet: the device gates for the two registrations, and the TP=3 main-against-branch receipt. I will push the new head and rewrite the description only when both are real, so until then the head here is still the old design and its description still describes that.

@ultradaoto
ultradaoto marked this pull request as draft November 18, 2026 19:57
@ultradaoto
ultradaoto force-pushed the glm-vision-fused-attn branch from a01e2f4 to 68119d4 Compare November 18, 2026 21:36
@ultradaoto ultradaoto changed the title glm cuda: the image tower's attention in one kernel a head behind TF_GLM_VISION_FUSED, a 4K image's first token 65.7 s to 12.9 s at TP=3 feat(glm): the image tower on vit full attention, behind TF_GLM_VISION_FUSED Nov 18, 2026
@ultradaoto
ultradaoto marked this pull request as ready for review November 18, 2026 21:36
@ultradaoto

Copy link
Copy Markdown
Contributor Author

The rework is pushed: head 68119d4315 on main 7b2717a20d, one commit, and the description is rewritten for it. At TP=3 on three GB10s the shared vision_kernel at head dim 64 gives a 3840 × 2160 image's first reply in 11.2 to 11.3 s (main 65.5 to 65.7 s; the GLM-only kernel this replaces took 12.8 to 12.9 s on main 8cbef97), and with the switch unset the branch gives main's bytes. Both registrations pass their device gates on a GB10 and the control goes red as planted. Ready for review again.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Hi @ultradaoto, thanks for the rework. TF_GLM_VISION_FUSED is a strict opt-in that is off by default; with it unset the image path still runs the two cuBLASLt products, and the shared full-attention kernel is one new instance at head dim 64 with the existing kernels left alone. The reply difference on the opt-in path is stated in the description, and the arithmetic is held to the shared float64 reference. The description and serving pins agree with the head, which applies to current main. We do not need the extra side-by-side comparison, and the #3614 question is ours to answer. Nothing is needed from you.

  • Synthena

@ultradaoto

ultradaoto commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

A heads-up on how this meets #3776, for whichever lands second.

#3776 turns cuda_vision.zig's launch into cuda_pdl.launch, and every kernel in glm_vision.cu starts with glm_pdl::enter() there. This PR adds one kernel to that file, glm_vis_qk, and launches it through the same vision.launch. With both on main it would be a dependent launch that reads qkv, qn, kn and pos before any wait.

The fix is one line, glm_pdl::enter(); as glm_vis_qk's first statement (plus the glm_pdl.cuh include that #3776 already adds to the file). I have not built the two together yet; check-pdl-loads is the guard that would flag a missing wait.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @ultradaoto. The rework is accepted as an opt-in, and nothing further is needed from you on this PR.

Noted for landing: if #3776 lands first, glm_vis_qk needs its glm_pdl::enter() line, checked with check-pdl-loads. This PR can land first without that change. CI on this head fails some serving-hash pins against current main; refreshing them is mechanical and we will do it ourselves before landing.

  • Synthena

@ashhart
ashhart force-pushed the glm-vision-fused-attn branch from 68119d4 to 30d9226 Compare November 19, 2026 06:21
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @ultradaoto. As promised, we rebased this onto current main ourselves and refreshed the serving-hash pins for the files it changes, in one commit of ours; your commits are as they were. It is in the landing queue now; nothing is needed from you.

  • Synthena

@AIFlowML

AIFlowML commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Trial-merging 628aa532 into 767d3d75 leaves one conflict in zig/kernels/cuda/contract_controls_set.cu: main adds the HCN control header at the same spot where this PR adds the GLM vision control header. I kept both includes in the scratch merge.

On M5 Max with Zig 0.17.0, the resolved tree passed 64 host tests, including the serving pins, plus index emulation and both the GLM native-open and Linux CUDA test-runner compile checks:

zig build test-gradient-serving test-environment-host test-glm-index-emulate \
  test-glm-logprobs-host test-vision-cli-host compile-glm-native-open \
  -Dcpu=apple_m1 -j5 --summary all
zig build compile-cuda-tests -Dtarget=x86_64-linux -j5 --summary all

A scratch target selecting GLM strict env: from the GLM module also passed 15 tests, including the reader for TF_GLM_VISION_FUSED. These are host and cross-compile checks. They leave the GB10 run as the device evidence for the fused attention path.

On 4a27414f9, the trial merge is clean against 5f53c445e. The environment and serving fixtures passed 29 tests, and the GLM native-open target compiled on M5 Max. I checked the revised cuLaunchKernelEx recorder separately. Filtering the GLM host test root alone did not discover the named vision test. Adding _ = vision_attn; to the test import block in zig/src/families/glm/cuda.zig in scratch made the runner execute it. All eight tests in that filtered binary passed, including the four launch geometries. The import is the first detail I would add so the default GLM test root can see this fixture.

Igor Lessio - AIFLOW LABS

@ashhart
ashhart force-pushed the glm-vision-fused-attn branch from 628aa53 to 4a27414 Compare November 21, 2026 02:46
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thanks @ultradaoto. Nothing more is needed from you on this one. The fused image-tower path stays behind TF_GLM_VISION_FUSED, so the default path is unchanged.

Main has moved since your push: it conflicts in contract_controls_set.cu and contract_limits.zig, and vision_attn needs wiring into the test block in zig/src/families/glm/cuda.zig. We'll rebase it ourselves, keeping your commits as they are. Then on a GB10 we'll run check-pdl-loads, kernel-contract-limits (refreshing glm_vis_qk's register counts with its glm_pdl::enter() wait in place), kernel-contract glm_vis, and glm-vision with the switch on and off, and post the results here.

  • Synthena

@ashhart
ashhart force-pushed the glm-vision-fused-attn branch 2 times, most recently from 45a4023 to 15394a5 Compare November 21, 2026 18:50
@AIFlowML

Copy link
Copy Markdown
Contributor

I trial-merged 15394a58c into main 2ce240cf0 on M5 Max with Zig 0.17.0. It applied without conflicts, and the GLM CUDA test block now imports vision_attn.

zig build test-gradient-serving test-glm-index-emulate test-glm-logprobs-host \
  compile-glm-native-open -Dcpu=apple_m1 -j4 --summary all

Those targets passed. I also used a scratch test target to select with vit's attention from the GLM CUDA module and ran that test binary directly. All nine tests passed, including the named recorder case that counts the Q and K launches and one attention launch per frame group. In total the build reported 19/19 steps and 20/20 tests, with the native-open object compiling for Linux ARM64. This covers the host recorder and serving pin. The GB10 PDL, limits and fused-on/off vision checks are still pending device validation.

Igor Lessio - AIFLOW LABS

ultradaoto and others added 4 commits November 24, 2026 15:15
Follow main 7948cc4, which routes the vision launch helper through cuda_pdl.launch. The new glm_vis_qk entry must open with glm_pdl::enter() before reading its predecessor outputs, like every existing GLM vision entry. Keep the incoming arithmetic and both current serving pins unchanged.
…ernelEx

Follows main's 7948cc4 (GLM's CUDA kernels as dependent launches), which sends every GLM launch through
cuLaunchKernelEx; the test hands those to its recording cuLaunchKernel with cuda_pdl.forwardEx, as main's GLM tests do.
Reference the new vision_attn module from the GLM CUDA test block so the default GLM host test root discovers its launch-geometry coverage.
Combine the PR glm_vision image entry with main’s expanded image list, including idol_decoder and all four Inkling2 images, so every registered CUDA launch resolves its own image.
@ashhart
ashhart force-pushed the glm-vision-fused-attn branch from 5f88647 to 7408f4a Compare November 24, 2026 15:15
@ashhart
ashhart merged commit 2353e87 into ashhart:main Nov 24, 2026
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @ultradaoto. 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.

3 participants