Skip to content

cosmos3: Cosmos3-Edge reasoner (image + text) on Metal (new family, proposal for #1269) - #1271

Closed
drowzeys wants to merge 6 commits into
ashhart:mainfrom
drowzeys:upstream/cosmos3-metal
Closed

drowzeys wants to merge 6 commits into
ashhart:mainfrom
drowzeys:upstream/cosmos3-metal

Conversation

@drowzeys

@drowzeys drowzeys commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Cosmos3-Edge's reasoner (NVIDIA, 4B) serves natively on Metal: text and image chat through the OpenAI server, matching transformers at fixed prompts. Proposed in #1269 as a new family. Off unless a checkpoint's model_type is cosmos3_edge; other families' code paths are unchanged.

Changes since review

  1. Kernel contract. Every cos3_* pipeline the engine launches is registered (zig/tests/contract/metal_cosmos3*.zig, references in cosmos3_ref.zig, gate 5 records in registry_cosmos3.zig), including the tower's and prompt rows' flash attention (cos3_flash_128, cos3_flash_72) and the decode attention. Eight planted controls, each a source mutation (a dropped bf16 point, an unwritten last output, relu² by max alone, a wrong scale group, swapped M-RoPE axes, a skipped rescale, a dropped last key), all go red. Gate 1 found a real bug: cos3_relu2 turned a NaN into 0 (max drops NaN); fixed, finite outputs bit-identical. Three attention forms nothing launched are removed.
  2. Exactness on Metal. tools/bench_concurrent.py --levels 1,2,4 --alone --serial: every concurrent reply equals its solo run, every solo run equals "draft": false. Resumed == fresh: turns 2 and 3 of text and image conversations resumed from the prompt cache's kept states (1,886 and 318 cached tokens) equal the same conversations on a server with --prompt-cache-gib 0.
  3. Reproducible checkpoint. docs/recipes/cosmos3-edge.md pins nvidia/Cosmos3-Edge at 344d602b128d1bbdacb43b08d0a3626f46343e29, lists the publisher sha256 of every file the converter reads and the sha256 of every file it writes. The conversion is byte-identical from run to run (transformers 5.20.2, torch 2.14.1, mlx 0.32.4, numpy 2.4.6, macOS); the GB10 in cosmos3: Cosmos3-Edge reasoner on CUDA (stacked on #1271, proposal for #1269) #1272 serves the same files. No weights are republished.

Also: rebased on current main (past v1.1.5, at 0d24ecb3; two list conflicts with Spark-X resolved), zig fmt fixed (the failing fmt check), and a stray local launch script dropped from the history.

What it adds

  • The family cosmos3 in zig/src/families/cosmos3/: SigLIP2 image tower (variable size, the learned 16 × 16 position grid resized as torch does), projector (LayerNorm, 2 × 2 merge, 4608 → 11520 → 2048) and the dense 28-layer text model (GQA 16/8, relu² MLP, interleaved M-RoPE [24, 20, 20]) behind the lane engine's backend.
  • Kernels in zig/kernels/metal/cosmos3/cosmos3.metal, including flash attention on simdgroup matrices (a 640 × 480 frame's tower: 145 → 30 ms).
  • tools/cosmos3_edge_to_mlx.py (a development tool) writes the reasoner as MLX 8-bit affine (groups of 64); the generation tower, VAE and action heads are left out.

Receipts

M5 Ultra (macOS 27.0.1, Xcode Metal toolchain 32023.921.5), tensorfold-native 1.1.5 built from this branch, the 8-bit conversion above. Reference: transformers fp32 on CPU with the same 8-bit weights dequantized in. Prompts: two text questions; the checkpoint's public assets/example_reasoning_input.png (512 × 341) with its own prompt; its assets/example_i2v_input.jpg resized to 640 × 370 PNG; a synthetic 640 × 480 picture of shapes and a word.

Check Result
Kernel contract, --only cosmos3 SUMMARY 13 kernels: 13 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 red as planted, 0 missed; 0 limit problems
Kernel contract, full run SUMMARY 183 kernels: 183 pass gates 1-4, 0 fail, 2 known findings; 46 controls: 46 red as planted, 0 missed; 0 limit problems
Gate 5 --offline (M1/M2 compiles) all 11 cosmos3 launches pass
First-token logits, 5 prompts top-1 equal 5/5, top-4 equal 5/5 (top-5 3/5); cosine 0.99759–0.99988
48-token greedy 1/5 identical; the other 4 part at near-ties of the fp32 reference (its top-2 margin 0.03–0.26 at the parting token; the engine takes its 2nd or 3rd token)
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, 4 conversations × turns 2–3 8 of 8 equal (4 resumed from kept states)
First token (streamed, median of 7) text 25 ms; images 24–52 ms
Decode, one stream (bench_concurrent, 256 tokens) ≈ 200 tok/s
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, zig build test and zig build test-golden (-Dcpu=apple_m1) pass; --only cosmos3 gives the same 13 / 8 / 0 line; the 5 parity prompts give the same logits and text as before the rebase. The full 183-kernel run was on the v1.1.5 base. zig build test-kernel-contract: pass. zig fmt --check build.zig zig: clean.
  • tools/zig/lean_check.py: 433 findings on current main and the same 433 on the branch, zero new; every new file is under 600 lines.
  • The full Mac contract run needs a rotary receipt for this Xcode's compiler (tf-rotary-sweep, then --rotary-accuracy FILE); plain zig build kernel-contract stops with UnrecordedRotaryCompiler on main too. The 2 known findings are the documented affine_mm q4 ones. --offline reports one inherited record, glm gather glm_rows_bf16_nax (recorded 0/0, this Xcode compiles it for 1,024), in a file this PR does not touch.

Gaps

  • The checkpoint has no drafter, so drafts are off.
  • JPEG input needs the pinned libjpeg-turbo build; PNG and WebP work.
  • A native converter can replace the Python tool if wanted.

Credits: NVIDIA for Cosmos3-Edge (OpenMDW 1.1); the family ports the transformers reference (Apache-2.0), noted in THIRD_PARTY_NOTICES.

@ashhart

ashhart commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Thanks @drowzeys. This is a careful port: transformers-level first-token logits (cosine 0.9998, top-5 identical) and identical greedy answers on text and images. A new family is welcome on our side when it's off by default and leaves the rest of the engine byte-identical, which this does. Before we land it (and #1272 on top), the gaps you listed need to close:

  1. Kernel contract. Register the new Metal kernels (cosmos3.metal, including the tower's flash attention) with the kernel contract, with planted controls, as other families' kernels are.
  2. Exactness on Metal. Run resumed == fresh and the concurrency sweep (tools/bench_concurrent.py --alone --serial, 1/2/4 streams). CUDA's concurrent == solo is in cosmos3: Cosmos3-Edge reasoner on CUDA (stacked on #1271, proposal for #1269) #1272.
  3. A checkpoint anyone can reproduce. Either a published MLX 8-bit conversion pinned by revision, or tools/cosmos3_edge_to_mlx.py run against a pinned upstream revision, with the output's file hashes in the receipt. We'll run your recipe on our Macs and a GB10.

Once those are in, we'll run it on our hardware and land it.

- Synthema, Ash's AI Assistant.

@drowzeys
drowzeys force-pushed the upstream/cosmos3-metal branch from 435802b to 5a04a61 Compare October 26, 2026 00:58
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.
@drowzeys
drowzeys force-pushed the upstream/cosmos3-metal branch from 5a04a61 to b1422dc Compare October 26, 2026 01:13
@drowzeys

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Rebased on current main (past v1.1.5) and updated; CI is green. Point by point:

  1. Kernel contract. All 11 cos3_* pipelines are registered: 13 kernels under gates 1–4, including the tower's and prompt rows' flash attention (cos3_flash_128, cos3_flash_72) and the decode attention. Each output is checked against float64 at its bf16 rounding points. Eight planted controls are source mutations of these kernels; all eight go red. Gate 5 records come from --offline M1/M2 compiles. On an M5 Ultra, --only cosmos3 gives SUMMARY 13 kernels: 13 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 red as planted, 0 missed; 0 limit problems. The full run gives SUMMARY 183 kernels: 183 pass gates 1-4, 0 fail, 2 known findings; 46 controls: 46 red as planted, 0 missed; 0 limit problems. The two known findings are the documented affine_mm q4 ones. Gate 1 found a real bug: cos3_relu2 turned a NaN into 0. That is fixed, and finite outputs are bit-identical. Three attention forms that nothing launched are removed.
  2. Exactness on Metal.
    • tools/bench_concurrent.py --levels 1,2,4 --alone --serial (code and chat, greedy and sampled): all 84 concurrent replies equal their solo runs, and every solo run equals "draft": false.
    • Resumed == fresh: turns 2 and 3 of text and image conversations, resumed from kept states (1,886 and 318 cached tokens), equal the same conversations on a server with --prompt-cache-gib 0. 8 of 8 are equal.
    • Logit parity and greedy are in the description. The fp32 reference and the engine part only at the reference's near-ties, and the description reports those margins.
  3. Checkpoint. docs/recipes/cosmos3-edge.md pins nvidia/Cosmos3-Edge at 344d602b128d1bbdacb43b08d0a3626f46343e29. It lists the publisher sha256 of each file the converter reads and the sha256 of every output file; model.safetensors is e67cf5d59357a81d880ade79c0e700e9f1fb1ff090d5927eeb9ad4b385c29470. With the tool versions named there, the conversion is byte-identical from run to run. The GB10 in cosmos3: Cosmos3-Edge reasoner on CUDA (stacked on #1271, proposal for #1269) #1272 serves the same files. No weights are republished.

Also fixed: the fmt failure, and a stray local launch script that had slipped into the history.

One note for running the full contract on a Mac: plain zig build kernel-contract stops with UnrecordedRotaryCompiler on main as well. It needs a tf-rotary-sweep receipt passed with --rotary-accuracy. Separately, --offline flags one inherited record, glm gather glm_rows_bf16_nax, which this PR doesn't touch.

@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. Cosmos3 is selected only by model_type = cosmos3_edge, existing families and kernels are unchanged, and the shader is registered with 13 contract cases, 8 controls and 11 launch records, which is great to see. Before device runs:

  1. Decode and flash attention use different probability precision: decode multiplies FP32 probabilities by V (cosmos3.metal around 147) while flash rounds them to BF16 (around 236), and text switches between them at eight rows (forward.zig:216). On the same inputs they give different bits (a two-key witness gives 0xbd9b vs 0xbd98), so a resumed or chunked prompt wouldn't equal a fresh one. Please use one arithmetic for both, with 7/8-row seam tests.
  2. NAX prompt projections are on by default on tensor-unit Macs and replace the tile projections at 33 rows, but the load-time check compares only tile and twin. Please qualify NAX against the plain projection at every served shape (32/33 rows, image projections, restored tails).
  3. Concurrency is a serial fallback (factsFor(1, streams), hidden_rows = false, streams take turns). Families need to serve multiple streams through the lane core's shared rounds with unchanged per-stream arithmetic.
  4. The advertised 4- and 6-bit formats can't load: the loader requires a specific packed layout with no fallback. Please list only the formats that load.

@ashhart

ashhart commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Review is on #1272, covering both #1271 and #1272.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @drowzeys for Cosmos3-Edge on Metal. We're closing this for now, as our 26 Oct review hasn't had a reply. Please do reopen it once attention uses one arithmetic at every width, streams share rounds, and it serves a published checkpoint in its own format.

@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