Skip to content

feat(cuda): --device picks the GPU a rank runs on - #354

Closed
gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:cuda-device
Closed

gilby wants to merge 1 commit into
ashhart:mainfrom
gilby:cuda-device

Conversation

@gilby

@gilby gilby commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

--device N (or TF_CUDA_DEVICE=N) makes GPU N the current CUDA device before an engine loads, in every CUDA engine
(27B, Qwen3.6 MoE, Flash Next, GLM-5.3, Nemotron), and the unified-memory check reads the current device instead of
device 0. Two ranks on one machine can then both see both GPUs and each run on its own, which a peer-mapped transfer
between them needs (CUDA_VISIBLE_DEVICES hides the peer). Without the flag nothing changes: device 0.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision: one DGX Spark (GB10, sm_121),
    PyTorch 2.13.0a0 (NGC 26.07 container, CUDA 13.3), Vontra/Qwen3.6-35B-A3B-MLX-4bit-MTP @ f84b054, its own MTP drafter;
    tensorfold serve <checkpoint> --backend cuda --parallel 1 --device 0, measured on this change at 0.6.4 (branch now on
    0.6.5 (609ca41) as 00f4fca; one conflict, in Nemotron's CUDA engine: 0.6.5's new MTP depth and confidence checks are
    kept and its torch.cuda.set_device(0) becomes this PR's device select)
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: not affected
    (no kernel or engine arithmetic changes). With --device 0 the server starts and one request sent twice, the
    second with "draft": false, gives token_sha d594be69c0dd both times, the same sha as plain 0.6.4 on this checkpoint. Resumed == fresh, on v0.6.5 (one DGX Spark): the same 2,640-token prompt three times on one server at temperature 0: fresh, resumed (2,635 tokens from the prompt cache) and resumed with "draft": false, all token_sha 968cfc11c98b.
  • Decode speed before and after, from tools/bench_openai.py: not affected; not run.
  • Prompt speed before and after, from tools/prefill_cold.py: not affected; see the v0.6.5 check below.
  • On v0.6.5: this branch rebased onto v0.6.5 (609ca41) as 00f4fca. Mac host suite (M5 Ultra): 5003 passed, 1 failed, 596 skipped; the one failure, test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on v0.6.5 too. CUDA on v0.6.5 (one DGX Spark, GB10): pytest tests/cuda tests/test_cuda_build.py -k "qwen27 or qwen36 or nvfp4 or b16 or qmm or moe or build or distributed or dflash or dspark or exl3 or graph": 679 passed, 0 failed, 81 skipped.
  • Tests run, with pass, fail and skip counts: Mac host suite: 4695 passed, 1 failed, 595 skipped (the one failure, test_glm5_prompt_kernels::test_fused_index_scores_are_the_three_ops, fails on 0.6.4 too). New tests/test_cuda_device.py (3 tests, no
    GPU needed) fails to collect on 0.6.4 (tensorfold.cuda.device absent) and passes here.
  • Checked on v0.6.5 (2026-10-04): one DGX Spark (GB10), tools/prefill_cold.py (2k / 8k / 16k, 3 reps, median tok/s; runs v0.6.5, this, this, v0.6.5, each side the mean of its two) on RadixArk/Qwen3.8-27B-NVFP4-BF16-LMHead + z-lab/Qwen3.8-27B-DFlash2 at --context 131072 --parallel 1, a checkpoint v0.6.5 serves: v0.6.5 → this 2,595 → 2,604 / 2,846 → 2,839 / 2,728 → 2,725. Vontra/Qwen3.6-35B-A3B-MLX-4bit-MTP with --device 0 --parallel 4: tools/bench_concurrent.py --levels 2,4 --alone --serial (code and chat, temperature 1.0 and 0): 0 failed, alone 72/72 and serial 20/20 equal, 0 unequal in all 8 cells.
  • Not run, and why: two ranks on one machine with two GPUs (--device 0 / --device 1): our two-GPU machine was busy
    with other work during this pass; we ran it there earlier on 0.6.2 with perf(cuda): two-rank NVFP4 layers shard as they load; row-parallel sums over a peer mapping #356's P2P sum (two NVLinked A40s).

Checklist

  • 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.
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release. (One DGX Spark, the 27B NVFP4 + DFlash2, which v0.6.5 serves: −0.3 to +0.3% against v0.6.5; see the v0.6.5 check.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

--device N (TF_CUDA_DEVICE) makes device N the current CUDA device before an engine loads, in every CUDA engine,
and the unified-memory check reads the current device. Two ranks on one machine can then both see both GPUs and
each run on its own, so their transfers can map the peer GPU. Without the flag nothing changes: device 0.
ashhart added a commit that referenced this pull request Oct 6, 2026
The CUDA CLI opened its context on device 0 no matter what, so two ranks on
one machine could not each run on their own GPU while still seeing both, which
a peer-mapped transfer between them needs. --device N in the option loop, or
TF_CUDA_DEVICE in the environment, now selects the ordinal cuda.Context.init
gets before an engine loads; nothing set still means device 0, and no kernel
or engine arithmetic changes. The resolution lives in deviceOrdinal, covered
by a new host test (the flag wins, then the environment, else 0), and the
CLI's tests join zig build test through the CUDA host tests. PR #354's own
receipt showed the same token_sha with and without the flag.

Co-authored-by: gilby <[email protected]>
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. The Python engine is frozen (#286), so this won't land there, but Zig CUDA's command-line tools have the same gap. They open device 0 every time, though the context call already takes an ordinal.

We'll carry --device, with TF_CUDA_DEVICE as the fallback, into the Zig engine, and the commit will carry your credit. Your reason for it, two ranks on one machine that each need to see the other's GPU for peer transfers, is the case it's for.

@ashhart ashhart closed this Oct 6, 2026
@ashhart ashhart mentioned this pull request Oct 6, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
The CUDA CLI opened its context on device 0 no matter what, so two ranks on
one machine could not each run on their own GPU while still seeing both, which
a peer-mapped transfer between them needs. --device N in the option loop, or
TF_CUDA_DEVICE in the environment, now selects the ordinal cuda.Context.init
gets before an engine loads; nothing set still means device 0, and no kernel
or engine arithmetic changes. The resolution lives in deviceOrdinal, covered
by a new host test (the flag wins, then the environment, else 0), and the
CLI's tests join zig build test through the CUDA host tests. PR ashhart#354's own
receipt showed the same token_sha with and without the flag.

Co-authored-by: gilby <[email protected]>
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