Skip to content

fix: say why a CUDA start is silent (SIGUSR1 stack dump, extension builds, stale build locks) - #62

Closed
nood-co1 wants to merge 3 commits into
ashhart:mainfrom
blockbrain-ai:contrib/cuda-start-diagnostics
Closed

nood-co1 wants to merge 3 commits into
ashhart:mainfrom
blockbrain-ai:contrib/cuda-start-diagnostics

Conversation

@nood-co1

@nood-co1 nood-co1 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A CUDA start that waits shows nothing: the log stops at loading … and the GPU idles. This PR makes it say why.

  1. kill -USR1 <pid> prints every thread's stack on CUDA too. It worked on the Mac only.
  2. The build helper added in 0.3.6.1, which all eleven CUDA extensions use, now prints a line before an extension builds when it has no built module or one of its source files is newer than the module; a change to a header or to the build flags alone rebuilds without the line. When torch's build lock is there, it names the lock and what to do about it.
  3. RUNBOOK.md gets a troubleshooting row for a silent CUDA start, and a note that the two-rank rendezvous is not authenticated.

Why

At main (71377a5, 0.3.6.2):

  • Stack dump. cmd_serve returns _serve_cuda(...) (cli.py:433-434) before it registers faulthandler.register(signal.SIGUSR1, all_threads=True) (cli.py:438-441). The comment there calls the dump "the way to see where a silent server waits", but a CUDA server never registers it.
  • Silent builds. The CUDA extensions build on first use through tensorfold.cuda.build.load (cuda/build.py:23-29), which adds the GPU's -gencode flag and calls torch.utils.cpp_extension.load. All eleven extensions import it (cuda/experts.py:20, cuda/exl3/experts.py:25, cuda/exl3/linear.py:18, cuda/kernels/gdn.py:15, cuda/kernels/qmm.py:16, families/qwen3_5/cuda/b16.py:13, families/qwen4_exp/cuda/gdn.py:24, families/qwen4_exp/cuda/gdn_io.py:15, families/glm5_next/cuda/kda.py:15, families/glm5_next/cuda/exl3_mm.py:20 and families/nemotron_h/cuda/mamba.py:194) and pass verbose=False. A build prints nothing.
  • Stale locks. torch's _jit_compile takes a FileBaton on <build directory>/lock. A process that finds the file there calls FileBaton.wait(), which polls until the file is gone and says nothing. torch 2.11 does not pass the baton's warn_after_seconds. If a start is killed during a build, the lock stays, and every later start waits on it with no end.
  • Deleting the lock under a waiting start. When the wait ends, _jit_compile imports the module the build directory holds and does not build (torch 2.11, cpp_extension.py:2286 and :2294). Deleting the lock while a start waits therefore loads whatever a killed build left: after an update, the old module. A host probe (torch 2.11 CPU, build and import replaced by recorders, an older module and a newer source in the directory, the lock deleted after 0.5 s) recorded one import and no build. So the hint says to stop the start before deleting the lock.
  • Runbook. The troubleshooting table (RUNBOOK.md:124-133) has no row for this. The two-rank section (:91) gives only the port number. Neither rendezvous is authenticated: TCPStore in cuda/comm.py:50, and init_process_group("nccl", init_method="tcp://…") in families/qwen3_5/cuda/engine.py:44. NCCL's own connections between the ranks carry no authentication either.

Change

  • cli.py: the faulthandler import and registration, with their comment, now come before if backend == "cuda". Nothing else moves.
  • src/tensorfold/cuda/build.py: 0.3.6.1's load(**kwargs) becomes load(name, sources, **kwargs). It still appends the GPU's -gencode flag from arch_flags() first, so a GPU older than compute capability 9.0 is refused before any line, and it passes every argument to torch.utils.cpp_extension.load unchanged. Before that call it finds the build directory: build_directory= when given, else torch's _get_build_directory(name, verbose=False). That is a private torch helper. If it is missing or raises, nothing is printed and the build goes ahead as before. Otherwise it prints:
    • [tensorfold] building CUDA extension NAME (first start after an install or update; later starts reuse it) when the directory holds no built module (NAME plus torch's LIB_EXT), or when a source is newer than the built module. Headers and flags are not compared: a change to a header (cuda/experts.cuh, cuda/kernels/qmm_frag.cuh, cuda/exl3/decode.cuh, cuda/exl3/experts_grouped.cuh) or to an extension's flags alone rebuilds without the line.
    • when <dir>/lock exists, instead: [tensorfold] CUDA extension NAME waits on the build lock PATH; if no other build is running, a killed build left it: stop this start, delete the lock and start again.
    • after 60 s, if the load is still running and the same lock file is still there (same inode and mtime), the hint once more. This runs on a daemon threading.Timer, cancelled when the load returns. The same-file check keeps the hint off a lock this start has taken for its own build.
  • Call sites: unchanged. The eleven modules above already import load from tensorfold.cuda.build and pass name= and sources= by keyword.
  • RUNBOOK.md:
    • a troubleshooting row, "CUDA start stops after loading …, GPU idle": kill -USR1 <pid> prints every thread's Python stack; a wait in the extension build is a build lock, and the start log names its path; when no other build is running, stop the start, delete the lock and start again;
    • beside the default rendezvous port: the rendezvous port and the link between the ranks are not authenticated; keep them on a private link, or firewall the port to the peer.

Not in this PR: building the extensions before the weights load. Since 0.3.6.2, Flash Next loads its prompt kernels at startup instead of on the first request (416106f, from #40); that still runs after the weights are read, and this PR's lines print wherever a build happens. Building before the weights load would change families/qwen4_exp/cuda/engine.py again. It can follow as its own PR, together with an optional tensorfold build command that prebuilds the extensions into an image.

Exactness

Not affected. The same names, sources and flags reach torch, 0.3.6.1's -gencode flag included, so the same modules are built and loaded. The PR adds printed lines and moves one signal registration. No kernel, engine or sampling code changes.

Tests

Host: macOS, Python 3.13.5, PyTorch 2.11.0 CPU, MLX 0.31.1, an import-only stand-in for Triton (no kernel launches).

  • tests/test_cuda_cli.py::test_a_cuda_start_dumps_its_stacks_on_sigusr1: SIGUSR1 is registered with all_threads=True before _serve_cuda runs. test_cuda_dispatch_keeps_the_resolved_context now stubs the registration, as the MLX tests in tests/test_memory_budget.py do.
  • tests/test_cuda_build.py: 13 tests added beside 0.3.6.1's four, under the same torch marker. torch's build and directory lookup are stubbed onto a temporary directory. The tests check that:
    • an empty directory prints the building line, and a built module prints nothing;
    • a source newer than the built module prints the building line;
    • a planted lock prints the lock line with its path and the hint, and no building line;
    • a build_directory= argument is the directory checked;
    • the hint repeats after the delay, and not after the load returns, after the lock goes, or after it is replaced;
    • every other argument reaches torch unchanged, beside the GPU's -gencode flag;
    • a GPU older than compute capability 9.0 is refused with no line and no call to torch;
    • with the lookup missing or raising, nothing is printed and the build goes ahead.
  • tests/test_cuda_build.py::test_the_lock_named_is_the_one_torch_waits_on uses real torch and compiles nothing. It plants a lock at TORCH_EXTENSIONS_DIR/<name>/lock and checks that load waits on that file, that the printed line names it, and that the call returns once the file is deleted (with an import error, since nothing was built).
  • 0.3.6.1's four tests are kept. test_the_container_list_adds_nothing now points TORCH_EXTENSIONS_DIR at a temporary directory, since load looks up the build directory.
Run Result
tests/test_cuda_build.py and tests/test_cuda_cli.py on main's sources the 14 new tests fail (2 failed, 12 errors: no build._say, SIGUSR1 not registered); the 31 existing tests pass
Same files on this branch 45 passed
tests/test_cuda_build.py 25 times in a row (it has timer tests) 25 of 25 runs passed
Whole host suite (tests without tests/cuda), main 136 failed, 2,082 passed, 299 skipped, 18 errors
Same, this branch 136 failed, 2,096 passed, 299 skipped, 18 errors

The same 154 ids fail or error on both, and none is in this PR's two host files. This host has 36 GiB of memory, no Triton backends and no transformers tokenizer, and has MLX 0.31.1 where GLM-5.3-Flash needs 0.32.2; the test tree used here has no tools/ or CHANGELOG.md. Thirteen one-line breakages of the branch, each applied alone, are each caught by the two host files; they include dropping the GPU's flag, printing before the architecture check, and one call site back on torch's load.

tests/cuda/test_build.py (new, GPU) builds tensorfold_qwen4_exp_gdn_io with nvcc into an empty TORCH_EXTENSIONS_DIR in a fresh process. The first start prints the building line and the second prints nothing. It then plants a lock and starts again: the start prints the lock line and waits. The test stops it, deletes the lock and starts again, which exits 0 and prints nothing.

GPU status

Steps 1 and 2 passed; step 3 has not run. Surface: one DGX Spark (GB10); the tensorfold-bench image (PyTorch 2.13.0a0, NVIDIA 26.07; CUDA 13.3), TORCH_CUDA_ARCH_LIST=12.1; TensorFold 215b3f0, this branch on main 71377a5, imported from the branch's tree; toy models.

Step Command Result
1 python3 -m pytest tests/cuda/test_build.py 2 passed, on the container's torch (2.13.0a0)
2 python3 -m pytest tests/cuda 550 passed, 71 skipped; main (71377a5): 548 passed, the same 71 skipped. The 2 more are test_build.py's

The 71 skipped tests need EXL3, Nemotron or MLX checkpoints, ExLlamaV3, or TENSORFOLD_TEST_NCCL=1; 18 of them compare with ExLlamaV3 at rate and codebook pairs it does not support.

Building eight extensions into an empty TORCH_EXTENSIONS_DIR: each printed [tensorfold] building CUDA extension NAME (first start after an install or update; later starts reuse it) on this branch; none printed a line on main (71377a5).

Not run:

  • Step 3, a served start rather than a test run. Expected: a start with an empty TORCH_EXTENSIONS_DIR prints the building lines during loading …; a start after one killed during a build prints the lock line, and 60 s later the repeat; during that wait, kill -USR1 <pid> prints the stacks; once that start is stopped and the lock deleted, the next start prints the building lines and finishes.

Evidence

The wait this PR explains, as observed on TensorFold 0.3.1 before this change (not re-run on this branch; its served start is step 3 above, not run). Surface: one DGX Spark (GB10), NVIDIA's PyTorch container (nvcr.io/nvidia/pytorch:26.07-py3, PyTorch 2.13.0a0), a one-GPU Qwen3.8 Flash Next start.

  • A start killed while it built tensorfold_qwen4_exp_gdn left torch's lock.

  • Every later start stopped at loading … with the GPU idle.

  • A py-spy dump of the waiting start showed:

    wait (torch/utils/file_baton.py:50)
    _jit_compile (torch/utils/cpp_extension.py:2400)
    load (torch/utils/cpp_extension.py:1927)
    _ext (tensorfold/families/qwen4_exp/cuda/gdn.py:34)
    ...
    warm (tensorfold/families/qwen4_exp/cuda/graphs.py:97)
    __init__ (tensorfold/families/qwen4_exp/cuda/engine.py:74)
    
  • Deleting the lock before the next start fixed it.

On main (0.3.6.2), from reading the code (the killed-build wait was not re-run): the eleven extensions still build on first use through torch's load, by way of tensorfold.cuda.build.load, and nothing prints before the wait.

A comment on #42 reports the same wait: a CUDA test slept about ten minutes with the GPU idle on a build lock in a TORCH_EXTENSIONS_DIR shared by builds of eight commits, and passed with a fresh directory.

#56 reported a CUDA extension build that fails rather than waits: on v0.3.5 and v0.3.5.1 (DGX Spark, torch 2.14.0a0), nvcc stopped on tensorfold_qmm_v3 because code that needs sm_89 or sm_90 was compiled for sm_80 and sm_86. 0.3.6.1 fixes that build: every extension builds through tensorfold.cuda.build.load, which passes -gencode for the GPU present and refuses a GPU older than compute capability 9.0. This PR keeps both. Its lines come after that check, and the GPU's flag still reaches torch.

Overlaps


Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).

cmd_serve returned into _serve_cuda before it registered the SIGUSR1
stack dump, so the dump its comment calls "the way to see where a silent
server waits" existed only on the Mac. The registration now comes before
the backend branch; nothing else moves.

Test: the handler is registered before _serve_cuda runs (host; fails
before the change). The CUDA dispatch test stubs the registration, as the
MLX tests do.
…ed build left

The eleven CUDA extensions build on first use through
tensorfold.cuda.build.load, which adds the GPU's -gencode flag and calls
torch.utils.cpp_extension.load; neither prints anything. A start killed
during a build leaves torch's lock in the build directory, and every later
start waits on it without a word: the log stops at "loading ...", the GPU
idles, and only a stack dump shows FileBaton.wait().

tensorfold.cuda.build.load still passes torch the same arguments. After
the architecture check, and before torch's load, it looks up the build
directory (torch's private _get_build_directory; without it, no line)
and prints:

- "building CUDA extension NAME (first start after an install or update;
  later starts reuse it)" when the directory holds no built module, or one
  older than a source;
- the lock's path and "if no other build is running, a killed build left
  it: stop this start, delete the lock and start again" when the lock is
  there, again after 60 s if the same lock file is still there. The start
  is stopped first because a wait that ends imports whatever module the
  directory holds without building it: after an update, the old one.

A GPU older than compute capability 9.0 is still refused before any line.

Tests (host): the lines for a first build, a built module, a newer source
and a lock; the repeat after the delay, and none after the load ends or
the lock changes; the other arguments reach torch unchanged beside the
GPU's flag; an older GPU is refused with no line; no line without the
lookup; with real torch and nothing compiled, load waits on the very lock
the line names until it is deleted. test_the_container_list_adds_nothing
points TORCH_EXTENSIONS_DIR at a temporary directory, since load now looks
up the build directory. tests/cuda/test_build.py builds one extension with
nvcc, checks both lines end to end and that the named steps clear a
planted lock. Not yet run on a GPU.
…ndezvous

- Troubleshooting: a CUDA start that stops after "loading ..." with the GPU
  idle; kill -USR1 prints every thread's stack, and a wait in the
  extension build is a build lock whose path the start log names: stop
  the start, delete the lock and start again.
- Two ranks: the rendezvous port (TCPStore, init_process_group tcp://) and
  the link between the ranks are not authenticated; keep them on a private
  link, or firewall the port to the peer.
@ashhart

ashhart commented Sep 29, 2026

Copy link
Copy Markdown
Owner

This shipped in 0.3.6.3 under your name (a586f3de). A CUDA start that goes quiet now says why: extension builds
report their progress, a stale build lock gets named instead of waited on, and kill -USR1 prints every thread's
stack. One thing we found on the Sparks: after the first requests Triton's in-process LLVM took USR1 over and the
dump went silent, so the server now arms it again after warmup and after each request (ac3de87c). Thank you, it's
already saved us time.

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