Skip to content

cosmos3: Cosmos3-Edge reasoner on CUDA (stacked on #1271, proposal for #1269) - #1272

Closed
drowzeys wants to merge 13 commits into
ashhart:mainfrom
drowzeys:upstream/cosmos3-cuda
Closed

drowzeys wants to merge 13 commits into
ashhart:mainfrom
drowzeys:upstream/cosmos3-cuda

Conversation

@drowzeys

@drowzeys drowzeys commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Cosmos3-Edge's reasoner on CUDA (DGX Spark, GB10): the Metal family's twin from #1271, as a lane backend. Stacked on #1271 (its first six commits are #1271's); proposed with it in #1269. Off unless a checkpoint's model_type is cosmos3_edge.

Changes since the first version

  • Rebased on current main (past v1.1.5, at 0d24ecb3) and onto the updated cosmos3: Cosmos3-Edge reasoner (image + text) on Metal (new family, proposal for #1269) #1271; build/cuda.zig and the CUDA contract runner kept within 600 lines on it; zig fmt fixed (the failing fmt check). The earlier zig-windows failure was tool_parse_test's timing assertion (namespaced openers without closers read in linear time), unrelated to this family.
  • Kernel contract. All eight tf_cos3_* kernels are registered (zig/tests/cuda/contract_cosmos3*.zig, nine registrations) with twelve planted controls and nvcc 13.0.88 records for sm_80, 86, 89, 120 and 121 (cosmos3_cuda_records.zig). Kernel bodies moved to cosmos3.cuh, templated on the rounding points they keep, so each control is the production kernel with one point dropped or one fault planted.
  • Gate 1 found a real bug: tf_cos3_relu2 turned a NaN into 0 through fmaxf. Fixed as on Metal; finite outputs unchanged (logit dumps and greedy answers byte-identical before and after).
  • Checkpoint: the recipe in cosmos3: Cosmos3-Edge reasoner (image + text) on Metal (new family, proposal for #1269) #1271 (docs/recipes/cosmos3-edge.md, pinned revision and output hashes) now has a CUDA section; the GB10 serves the same files, hash for hash.

What it adds

  • The text model on the MLX 8-bit tiles (one-row decode on group sums up to 16 rows, the prompt GEMM past that), q, k and v stacked into one projection.
  • zig/kernels/cuda/cosmos3.cu: the glue at cosmos3.metal's rounding points (8-bit embedding rows, RMS norms, interleaved M-RoPE with the cache write, relu², causal GQA over a stream's cache plus a split-key decode kernel), every group sum computed by the kernel that writes its rows.
  • The image tower widens its 8-bit weights to bf16 at load and runs on cuBLASLt with vit's LayerNorm, GELU and the 72-dim full attention.

Receipts

GB10, CUDA 13.0 (nvcc 13.0.88), tensorfold-native 1.1.5 from this branch, the pinned 8-bit conversion. Reference and prompts as in #1271: transformers fp32 with the same 8-bit weights; two text prompts, the checkpoint's public reasoning image, its i2v example at 640 px, a synthetic 640 × 480 picture.

Check Result
Kernel contract, cosmos3 9 registrations pass gates 1–4; 12 of 12 controls red; all 40 cosmos3 launch × SM records pass gate 5
Kernel contract, full run (sm_121 build, on v1.1.5) SUMMARY 347 kernels: 347 pass gates 1-4, 0 fail, 5 known findings; 104 controls: 104 red as planted, 0 missed; 52 limit problems (see below)
First-token logits, 5 prompts top-1 equal 4/5 (the 5th takes the reference's 2nd token, 0.026 behind); top-5 identical 3/5, 4 of 5 shared on the rest; cosine 0.99682–0.99983
48-token greedy 3/5 identical; the other 2 part at the reference's near-ties (margins 0.026 and 0.26)
Concurrent == solo, 1/2/4 streams (code + chat, greedy + sampled) 84 of 84 concurrent replies equal their solo runs; every solo run equals "draft": false
Resumed == fresh the CUDA backend keeps no prompt states yet, so there is no resumed path; turns 2–3 of four conversations equal a --prompt-cache-gib 0 server (8 of 8)
First token (streamed, median of 7) text 36 ms; images 70–101 ms (GPU shared with other engines during the run)
This branch vs the 1.1.4-based build same logits and identical greedy text on all 5 prompts
  • On the head of this branch (rebased on current main): zig build native -Doptimize=ReleaseFast -Dnvcc=…, zig build test-cuda-host and zig build test-kernel-contract pass; zig fmt --check build.zig zig is clean; the 5 parity prompts give the same logits and text as before the rebase. tools/zig/lean_check.py: 433 findings on current main and the same 433 on the branch.
  • The full run's 52 limit problems are the Qwen3.5 lane records main's changelog lists as open for 1.1.5 ("52 problems in Qwen3.5 lane kernels' records"); this branch touches no qwen35 source or record. Main's line is 338 kernels and 92 controls; this run adds cosmos3's 9 registrations and 12 controls (347 and 104). The 5 known findings are the existing qwen35 and GLM entries.
  • The upstream CUDA runner has no name filter, so the cosmos3 figures come from the full run's log.

Gaps and next steps

  • No prompt-state keeping on CUDA yet (Metal has it): a follow-up.
  • nvcc 13.2+ records stay pending, as Dory's do.
  • 8-bit conversions only; no bf16 path yet.
  • Decode is bound by the 8-bit projections' bandwidth (≈ 212 GB/s measured).

A native Metal family for NVIDIA Cosmos3-Edge's understanding tower: dense GQA with
interleaved M-RoPE [24, 20, 20] and relu^2, the SigLIP2 tower with its learned 16 x 16
position grid resized as torch's antialiased bilinear, and the 2 x 2 patch merger.
MLX affine 4/6/8-bit checkpoints from tools/cosmos3_edge_to_mlx.py (reasoner only).

Against the transformers model on the same 8-bit weights in fp32: text logits
cosine 0.99980, image prompt (758 tokens) 0.99976, both 48-token greedy exact.
… row

The image tower and the text model's prompt rows attend in 32-row tiles on 8 x 8 bf16
MMAs with each 32-key block staged once (P rounded to bf16 as the eager weights are):
a 640 x 480 frame's tower 145 -> 30 ms on an M5 Ultra. Calls under 33 rows take the
decode twin. Image prompt parity after: cosine 0.99974, 48-token greedy exact.

THIRD_PARTY_NOTICES names the transformers code the family ports (Apache-2.0) and
NVIDIA's weights (OpenMDW 1.1); README lists the converted reasoner.
The kernel contract's non-finite gate caught cos3_relu2 turning a NaN into 0 (Metal's max drops a NaN, as
fmax does); it now keeps it, and every finite output is unchanged bit for bit. cos3_attn_72 and the
cos3_attn_tile forms had no caller and leave the source and the pipeline list.
Every cos3 kernel the engine launches goes through gates 1 to 4 (tests/contract/metal_cosmos3*.zig): each
output within float64's range at its bf16 rounding points over the spread its fp32 steps and Metal's fast
exp allow (cosmos3_ref.zig), the norms' and attention's sums shown fp32 at gate 4, and eight planted
controls, each red at its gate. Gate 5 records the eleven launches with their M1/M2 compiled limits
(registry_cosmos3.zig).

On an M5 Ultra: SUMMARY 13 kernels: 13 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 red as
planted, 0 missed; 0 limit problems.
docs/recipes/cosmos3-edge.md pins nvidia/Cosmos3-Edge at 344d602b128d1bbdacb43b08d0a3626f46343e29, the
publisher sha256 of the files tools/cosmos3_edge_to_mlx.py reads, and the sha256 of every file the 8-bit conversion
writes (byte-identical from run to run with the versions named), with the family's checks: parity against
transformers in fp32, concurrent == solo at 1, 2 and 4 streams, resumed == fresh across conversation turns. The
README row and the converter's usage line point at it; the embedding kernel's comment says it rounds once.
…er on cuBLASLt)

The Metal family's twin for NVIDIA GPUs, a lane backend in native/cosmos3_cuda.zig. The text
model runs on qlinear's MLX 8-bit tiles (one-row decode on group sums up to 16 rows, the
prompt GEMM past that) with q, k and v stacked into one projection; cosmos3.cu holds the glue
at cosmos3.metal's rounding points: 8-bit embedding rows, RMS norms, interleaved M-RoPE with
the cache write, relu^2, causal GQA over a stream's cache (eight rows a block, and a
split-key kernel for decode rows), and every group sum computed in the kernel that writes its
rows, summed in input order. The image tower widens its 8-bit weights to bf16 at load and
runs on vit.zig's cuBLASLt Linears, LayerNorm, GELUs and the 72-dim full attention; a
640 x 480 frame's shapes are planned at load.

On a GB10 against the transformers model on the same 8-bit weights in fp32: text logits
cosine 0.99980 (top-5 identical), image prompt (758 tokens) 0.99977 (top-4 identical), both
48-token greedy exact. A 640 x 480 frame: tower 34 ms, text prompt 43 ms; decode 107 tok/s
(the 8-bit projections at ~212 GB/s). Seven camera questions: first token 0.10 s, answer
0.32 s (median).
… named; relu^2 keeps NaN

cosmos3.cu's kernels now call cosmos3.cuh's bodies with every rounding point kept (K) and no fault planted (F), so
the contract's controls can drop exactly one. relu^2 kept fmaxf(x, 0), which turns a NaN input into 0 where torch's
relu keeps it (the contract's gate 1 found it); a NaN now stays NaN and every finite output is unchanged.

On a GB10 the rebuilt server and the previous one give byte-identical first-token logits and identical 64-token
greedy answers for a text, a code and a synthetic image prompt.
…planted controls and nvcc 13.0.88 records

Nine registrations through gates 1 to 4: the 8-bit embedding rows and the tower widening bit-equal to float64 at
MLX's points, the image placement, the RMS norm rows (with and without the residual add) within the spread their
fp32 steps allow, relu^2, the interleaved M-RoPE with its cache writes, and the prompt and decode attention within
float64's range (core/attention_range.zig); every 64-value group sum is replayed bit for bit. Twelve controls each
drop one rounding point or plant one fault, and each goes red. Gate 5 holds all eight launches with registers and
shared bytes measured by nvcc 13.0.88 for sm_80, sm_86, sm_89, sm_120 and sm_121; later compilers stay pending.
…d checks on a GB10

The same pinned conversion on a GB10 against the transformers fp32 reference: first-token top-1 on four of five
prompts (the fifth at a 0.026 near-tie), three of five 48-token greedy replies identical and the other two parting at
the reference's near-ties, concurrent == solo at 1, 2 and 4 streams. The CUDA backend keeps no prompt states yet.
@drowzeys
drowzeys force-pushed the upstream/cosmos3-cuda branch from 7897c00 to 61aeb65 Compare October 26, 2026 01:13
@drowzeys

Copy link
Copy Markdown
Contributor Author

Updated alongside #1271: rebased on current main (past v1.1.5) and onto #1271's new head.

  • CI. fmt now passes. zig, desktop, desktop-linux, appimage-linux and mobile pass. zig-windows failed on qwen35_cost_cache_host's "concurrent writers and readers see only complete tables" with NTSTATUS=0xc0000123 (FILE_DELETED). That test doesn't involve this family; cosmos3: Cosmos3-Edge reasoner (image + text) on Metal (new family, proposal for #1269) #1271's zig-windows passes on the same shared code; and the previous failure here was a different timing test (tool_parse_test). Could you re-run that job? Fork pushes can't.
  • Kernel contract. All eight tf_cos3_* kernels are registered: 9 registrations, 12 planted controls, and nvcc 13.0.88 records for sm_80/86/89/120/121. On a GB10 every cosmos3 registration passes gates 1–4, every control goes red, and all 40 cosmos3 launch × SM records pass. The full run gives 347 kernels: 347 pass gates 1-4, 0 fail, 5 known findings; 104 controls: 104 red as planted, 0 missed; 52 limit problems. The 52 are the open Qwen3.5 lane records in main's changelog; this adds 9 kernels and 12 controls to main's 338 / 92. Gate 1 caught tf_cos3_relu2 dropping a NaN, the same bug as on Metal. It's fixed, and logits and greedy answers are unchanged.
  • Exactness, re-run on the rebased code. Concurrent == solo holds at 1/2/4 streams: 84 of 84 equal, and solo equals "draft": false. The CUDA backend keeps no prompt states yet, so it has no resumed path; that is listed as a follow-up. Logits and greedy text are identical to the 1.1.4-based build. The receipts are in the description.

@ashhart ashhart left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @drowzeys. On top of #1271's points (which carry over), the CUDA side needs:

  1. Attention changes arithmetic between one and several rows (attn1 for one row, another kernel for more), so resumed or chunked prompts won't equal fresh ones. Please use one arithmetic for both.
  2. Decode tail padding evaluates zero times infinity: attn1 loops over all 32 value lanes and loads padding, which can turn a finite result into NaN. Mask the tail.
  3. Concurrent serving still uses the serial fallback (exact_width = 1); families need shared rounds through the lane core.
  4. Native open forwards the requested streams without a complete admission budget, so capacity is advertised that may not fit.
  5. The vision tower expands affine weights at load; its precision and the model's fidelity need qualifying against a reference.

@ashhart

ashhart commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Review of #1271 (Metal) and #1272 (CUDA) against main 3f408b6 (74 behind; conflicts only in lists). Selection is clean (only model_type = cosmos3_edge), other families' kernels are unchanged, 13 Metal and 9 CUDA kernels are registered with controls, and the notices look right. Three things to fix on both:

  1. Arithmetic depends on row count. forward.zig:216 uses cos3_flash_128 (BF16 probabilities) at rows >= 8 and cos3_attn_128 (FP32) below, so a resumed or chunked prompt's bits differ from a fresh one. Please use one arithmetic at every width, with 7/8/9-row seam tests (resumed vs fresh at those chunk widths).
  2. Concurrency. Metal factsFor(1, …) and CUDA exact_width = 1 / hidden_rows = false are serial fallbacks; streams should share rounds, with concurrent equal to solo.
  3. Formats. The config accepts 4- and 6-bit, which the loader can't load (please refuse them by name), and the CUDA tower widens 8-bit weights to BF16 at load; please serve them as stored.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @drowzeys for the CUDA side of Cosmos3-Edge. We're closing this with #1271, as our 26 Oct review hasn't had a reply. Please reopen it on top of a new #1271 once attention has one arithmetic for one and many rows and the tower's weights are served as stored.

@ashhart ashhart closed this Oct 27, 2026
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