Skip to content

RFC: zig cuda: serve through tensorfold-native, with a CUDA family registry (Nemotron first) - #443

Merged
ashhart merged 1 commit into
ashhart:zig-flashnextfrom
jschmied:zig-cuda-native
Oct 6, 2026
Merged

ashhart merged 1 commit into
ashhart:zig-flashnextfrom
jschmied:zig-cuda-native

Conversation

@jschmied

@jschmied jschmied commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

RFC, draft. ZIG-PREVIEW.md lists "the CUDA backend … needs the server wiring and more families". This is a proposal for that wiring. Before I go further, I'd like to know whether the shape suits you.

Design question

native/metal.zig opens each family inline (if model_type == …). For CUDA this PR puts the device, the lane round loop and the lane host in one generic file, zig/src/native/cuda.zig. Each family is one entry in its registry and brings
only its lane backend through open: families/nemotron/cuda_native.zig, about 60 lines, loads the engine, MTP head and Lanes the way tensorfold lanes does. Flash Next (NVFP4) or another family would then add an entry and its lanes.Backend, with no server code. Would you rather keep the per-family pattern of metal.zig?

What changes

  • zig/src/native/cuda.zig (new):
    • backends, families (built from the registry), and chip() (nvidia-sm<capability>, e.g. nvidia-sm121 on a
      GB10);
    • open() checks the context, finds the kernel set (TENSORFOLD_CUDA_KERNELS, else
      share/tensorfold/cuda/sm<cc> beside the binary, as the CLI does), reads TF_CUDA_DEVICE, then builds
      lanes.Config, lanes.Engine and LaneHost.
    • The lane host steps rounds on its own thread, and a CUDA context is current per thread. So the family's backend is
      wrapped so that its first call on a thread makes the context current; the family code doesn't see it.
  • zig/src/families/nemotron/cuda_native.zig (new), and nemotron.native exported from cuda.zig.
  • zig/build/cuda.zig: zig build native on Linux installs zig-out/native/bin/tensorfold-native with these engines.
    The adapter's tests join the host tests.
  • zig/src/core/root.zig: core takes the tokenizer as a module (@import("tokenizer")). Otherwise the server's
    tokenizer module and core would both own tokenizer.zig, which Zig refuses. The Metal build doesn't build core.
  • zig/src/server/auth.zig: the key-file checks use statx on Linux, because Zig 0.17's std.c has no fstat/stat
    there. Every other OS keeps the existing stat/fstat path unchanged.

Tested

  • Machine: DGX Spark (GB10, sm_121), Zig 0.17.0, nvcc 13.0, base zig-flashnext 88c424e.
  • zig build, zig build native and zig build test (the host tests, including the two new ones) all pass.
  • tensorfold-native capabilities --json on the GB10 reports "chip": "nvidia-sm121", "backends": ["cuda"],
    "families": {"nemotron_h": ["mlx-q4g64"]}, and --backend takes auto and cuda.

Not tested yet

  • Serving Nemotron end to end, and so all exactness receipts: drafted against "draft": false, concurrent against
    solo, and server against tensorfold run / gate.py token hashes. That needs the kernel capture
    (zig/tests/cuda/nemotron/box) and the checkpoint on the box. I'll add the receipts here if the design is welcome.
  • No macOS build: the auth.zig change keeps the non-Linux path as it was, but I haven't compiled it on a Mac.

Open points, besides the design question

  • The chip class name for gate cells: nvidia-sm121, or something else?
  • prefill_step is the engine's 2,048-row chunk. Graphs stay off, as in tensorfold lanes, because lane windows vary.

…ne CUDA registry

native/cuda.zig owns the device, the lane round loop and the lane host; each CUDA family brings only its
lane backend through `open` (families/nemotron/cuda_native.zig, as `tensorfold lanes` loads it). The lane host's
thread gets the context made current on its first backend call. `zig build native` on Linux installs
tensorfold-native with these engines; core takes the tokenizer as a module so the server shares it, and the
server's key-file checks use statx on Linux, whose libc has no stat.
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. We want this shape and we're building on it: your commit is now the base of our Zig CUDA serving branch, under your name, and the work is running on GB10 and RTX PRO 6000. The registry stays as you designed it: native/cuda.zig beside native/metal.zig, with one adapter per family. On top we're adding what the RFC left open: --device and --segments through the native server, memory facts with admission that fails closed, the #291 next-chunk cancel, /metrics, and exactness receipts against "draft": false and the Python CUDA engine. The Linux statx change in server/auth.zig gets its own review. We'll close this when it lands, with your credit.

@BHCC2025

BHCC2025 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Receipts for this branch on a DGX Spark (GB10), in case they help the review. I did not change any code.

Environment

Build and tests

  • zig build native -Dnvcc=/usr/local/cuda/bin/nvcc -Dsm=121 succeeds (37 s). zig build test passes. Without -Dnvcc the binary has no kernels, so a plain zig build native starts and then fails at the first launch.
  • tensorfold-native capabilities --json reports "chip": "nvidia-sm121", "backends": ["cuda"], "families": {"nemotron_h": ["mlx-q4g64"]}.

Exactness, through the server (tensorfold-native serve ... --temperature 0 --no-thinking --parallel 4, 4 prompts, 200 tokens each, reply text hashed)

Check Result
drafted == --no-drafts, each prompt alone 4 of 4
drafted == --no-drafts, all four at once 4 of 4
concurrent == alone, drafted 4 of 4
concurrent == alone, --no-drafts 4 of 4

Exactness, command line (zig/tests/cuda/nemotron/gate.py, 256 tokens, 4 captured prompts): Zig serial equals the Python engine's serial on 4 of 4, and Zig drafted equals the Python engine's serial on 4 of 4. The Python engine's own drafted replies equal its serial ones on 4 of 4.

Speed (single run each, not alternated)

alone 4 at once, each
drafted 117 to 156 tok/s 34 to 47 tok/s
--no-drafts about 95 tok/s 23.6 tok/s

From gate.py, ms/token for Zig against the Python engine: serial 9.75 to 9.91 against 9.92 to 10.03, drafted 5.16 to 7.75 against 5.29 to 7.79.

Not run

  • Long prompts (the prompts here are short; context was the default 16,384), cancel, and sampling above temperature 0.
  • build.sh's SASS comparison against the Python extensions (I built with the host nvcc, not the container's).
  • 16 prompts (I used 4), bench_concurrent.py, a before and after comparison, and any other GPU.
  • Two Sparks, since NCCL is not installed on the host.

@ashhart
ashhart merged commit 7742ddd into ashhart:zig-flashnext Oct 6, 2026
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. To your design question: the registry is the right shape, and it has held up. We built a dozen commits on your commit unchanged, and a lone-stream driver, graph capture per draw mode and shared rounds all fit without changing it. It's merged into zig-flashnext as your own commit 59e77e8 (merge 7742ddd). We plan to push our extensions on top later, with your credit on the parts that touch your files.

@ashhart ashhart mentioned this pull request Oct 6, 2026
@jschmied
jschmied deleted the zig-cuda-native branch October 11, 2026 17:36
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…h tensorfold-native with a CUDA family registry
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