Skip to content

fix(flash next cuda): build the CUDA extensions before the weights load - #202

Closed
mcclanahanaman wants to merge 1 commit into
ashhart:mainfrom
mcclanahanaman:fix/flash-next-build-before-load
Closed

mcclanahanaman wants to merge 1 commit into
ashhart:mainfrom
mcclanahanaman:fix/flash-next-build-before-load

Conversation

@mcclanahanaman

Copy link
Copy Markdown
Contributor

Branch: fix/flash-next-build-before-load (one commit, 5c5931b, on v0.6.0, c464617)

TL;DR: On a first start after an install or an update, tensorfold.cuda.build.load compiles each CUDA extension the first time a step needs it. Flash Next met them in its prompt warm-up, after the weights had loaded. On a Jetson AGX Thor (unified memory), that left the server with 1.36-2.00 GiB less MemAvailable than the next start (two sessions), still missing 20 s after the server came up. The engine now loads every extension it uses right after admission, before the weights load. With the same builds, the difference is 0.01-0.15 GiB, about the 0.1 GiB by which two starts that build nothing differ.

The problem

  • Upstream 0.6.0 with an empty extension cache (a first start after pip install -U or tensorfold update) builds six extensions: tensorfold_nvfp4_prompt_v1, tensorfold_qwen4_exp_gdn_io, tensorfold_gdn_v2, tensorfold_experts_v7, tensorfold_nvfp4_v3 and tensorfold_qmm_v3. All six are built after the weights load, in the warm-up.
  • After such a start, MemAvailable is 1.36-2.00 GiB lower than after the next start, which builds nothing (MemFree moves with it), measured 20 s after the server is up.
  • The missing memory is not in the server's resident set (1.88-1.93 GiB on every start). Net Cached moved by 0.07 GiB or less, and AnonHugePages by 0.14 GiB. In the first stock session, about 1 GiB moved from Active(file) to Inactive(file) while net Cached stayed put. Where the missing GiB is held is not identified.
  • Why it matters: on a unified GPU, MemoryGate.fits checks the caches' growth against live MemAvailable. So the streams' caches have 1.4-2 GiB less to grow into for as long as the memory stays missing. How long that lasts in a running server was not measured. The printed "free for their caches" does not show it: it reads the same after both starts.

The change

  • New engine.build_kernels(exl3=, nvfp4=, solo=), which loads:
    • for every start: experts, kernels.gdn, kernels.qmm and qwen4_exp.cuda.gdn_io;
    • for NVFP4 (ModelOpt) checkpoints: the NVFP4 linears' two extensions;
    • for EXL3 packs: the EXL3 experts' and linears' extensions;
    • for one stream only: qwen4_exp.cuda.gdn (concurrent rounds never use it).
  • FlashNextEngine.__init__ calls it right after admission, so a refused start builds nothing, and before weights.load.
  • On a start with a built cache it only loads the modules.
  • Tests:
    • tests/cuda/test_flashnext_forward.py::test_the_engine_loads_its_extensions_before_the_weights, at one and two streams. It records every tensorfold.cuda.build.load call and the weights.load call during a start and a first request on the synthetic checkpoint, and checks that no extension is loaded after the weights. It clears the loaders' lru_cache first, so each one goes through build.load.
    • tests/test_cuda_capacity.py::fake_runtime, which stops a start at its weights load to test admission without a GPU, now stubs build_kernels.

Measurements

Jetson AGX Thor, local-inference-lab/Qwen3.8-Flash-Next-NVFP4 rev 7c4f1bc1, --kv-dtype int8 --parallel 4. Two container sessions (1: up-m1, 2: up-b4), each with four starts in a row; the server was stopped and given 15 s between starts. MemAvailable was read 20 s after the server answered /v1/models:

Session Tree Builds on the first start MemAvailable after the first start after the next start (no builds) Difference
1 0.6.0 6, after the weights load 40.99 GiB 42.99 GiB -2.00 GiB
1 an earlier revision of this change, which also built the one-stream chain 7, before the load 42.87 GiB 42.88 GiB -0.01 GiB
2 0.6.0 6, after the weights load 41.65 GiB 43.01 GiB -1.36 GiB
2 this change 6, before the load 42.91 GiB 43.05 GiB -0.15 GiB
  • With 0.6.0, MemFree fell with MemAvailable (-1.98 and -1.36 GiB). The server's resident set (1.88-1.93 GiB in every start), anonymous memory, page cache and slab each moved by 0.14 GiB or less.
  • Two starts that build nothing differ by up to 0.11 GiB (the two trees' second starts: 42.88 vs 42.99, 43.05 vs 43.01). The -0.15 GiB with this change is close to that.
  • The builds take the same time either way: in session 2, the first start was ready after 285 s on 0.6.0 and 290 s with this change, against 86-87 s for a start that builds nothing. These are single runs.
  • The printed "free for their caches" reads 32.2-32.3 GiB in all eight starts.
  • The measurement is a small shell script around tensorfold serve and /proc/meminfo.

The same kind of loss showed up earlier on a local Thor branch, on a first start that built its extensions after the weights loaded. Not measured: DGX Spark. It is also unified memory, so it may behave the same way.

Tests

  • New test: tests/cuda/test_flashnext_forward.py::test_the_engine_loads_its_extensions_before_the_weights, at one and two streams.
    • Both pass with this change.
    • With 0.6.0's engine.py, both fail. One stream lists tensorfold_experts_v7, tensorfold_qmm_v3, tensorfold_qwen4_exp_gdn, tensorfold_qwen4_exp_gdn_io and tensorfold_gdn_v2. Two streams list the same set without tensorfold_qwen4_exp_gdn. The synthetic checkpoint does not load the NVFP4 extensions.
  • On the same Thor, with this change (up-b4, up-b5):
    • tests/cuda/test_flashnext_forward.py: 56 passed.
    • tests/cuda/test_flashnext_nvfp4_loader.py, test_flashnext_tp.py and test_flashnext_multi.py: 70 passed, 2 skipped, 2 failed. The 2 failures are test_flashnext_multi.py::test_packed_passes_keep_each_prompt_one_token_early[bf16] and [int8]. They fail the same way on plain 0.6.0 (details in the stage-ahead PR).
    • With no GPU visible, the CPU tests that construct engines up to the weights load (tests/test_cuda_capacity.py, test_cuda_kv_dtype.py, test_cuda_parallel_admission.py, test_cuda_admission.py, test_exl3_engines.py and test_cuda_nemotron_admission.py): 147 passed.
  • Not run:
    • an EXL3 pack on a GPU, so the EXL3 loaders in build_kernels are exercised only by the CPU tests, which stub it;
    • DGX Spark.

The commit also applies cleanly to the 0.6.1 branch (pr-141-0.6.1). There, the list in build_kernels should be checked against the vision and image-row paths it adds. The new test would show any extension loaded after the weights.

AI disclosure: an AI coding agent (Claude Code) wrote this change and ran the measurements on my Jetson AGX Thor. I approved posting it; I have not reviewed the code line by line.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PRwDEapgChAWSk1ncJ62Yk

A first start after an install or an update builds each CUDA extension the first time a step needs it
(tensorfold.cuda.build.load). Flash Next met most of them in its prompt warm-up, after the weights had loaded. On a
GPU that shares the host's memory, those builds left the server with less MemAvailable (still missing 20 s after it
came up; the next start did not miss it), which the memory gate's live check works with. engine.build_kernels now
loads every extension a Flash Next start uses (the NVFP4 linears' and the EXL3 packs' only for those checkpoints, the
one-stream chain's only for one stream) right after admission, before the weights load: the same builds, earlier.

Jetson AGX Thor, local-inference-lab Qwen3.8-Flash-Next-NVFP4 rev 7c4f1bc1, --kv-dtype int8 --parallel 4,
MemAvailable 20 s after the server is up, a start with an empty extension cache minus the next start, in two
container sessions: upstream 0.6.0 -2.00 and -1.36 GiB (6 builds, all after the load; MemFree moves with it, while
the server's resident set, page cache and slab do not); this change -0.15 GiB (6 builds, before the load; an earlier
revision that also built the one-stream chain: -0.01 GiB). Two starts that build nothing differ by up to 0.11 GiB.
The printed cache room is 32.2-32.3 GiB in every start.

tests/cuda/test_flashnext_forward.py: a start on the synthetic checkpoint loads every extension before the weights
(one and two streams); with 0.6.0's engine.py it fails at both. tests/test_cuda_capacity.py's fake_runtime, which
stops a start at its weights load to check admission without a GPU, now stubs build_kernels too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PRwDEapgChAWSk1ncJ62Yk
@plotarmordev

Copy link
Copy Markdown
Contributor

Thanks @mcclanahanaman. I measured this on a second unified-memory box: one DGX Spark (GB10, sm_121, 128 GB), Vontra Flash Next MLX 4-bit, --context 32768. The base was v0.6.1, run with and without this commit; it applies cleanly to v0.6.1. A "cold" start means empty extension and Triton caches, so every extension builds. "Warm" means the caches are kept. Each start was measured as MemAvailable 20 s after /v1/models answers, two of each:

cold (GiB) warm (GiB) cold vs warm start to up
v0.6.1 33.58, 32.74 36.20, 36.27 −2.62, −3.53 GiB 117 s, 117 s
v0.6.1 + #202 36.09, 35.56 36.28, 36.27 −0.19, −0.71 GiB 113 s, 117 s

So on GB10 the cold start leaves about 2.5–2.8 GiB more available with the change, and it ends close to a warm start, as you saw on Thor. Startup time didn't change. With the PR, the five extensions build in build_kernels' order (experts, gdn_v2, qmm, gdn_io, gdn) ahead of the load. All eight starts gave the same greedy reply to a fixed prompt. I only exercised solo=True with the MLX 4-bit checkpoint, not the NVFP4 or EXL3 branches.

@ashhart ashhart mentioned this pull request Oct 2, 2026
@ashhart

ashhart commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Thank you, @mcclanahanaman. Building the CUDA extensions before the weights load is in TensorFold 0.6.4, with your commit under your name and a few lines changed to fit main: https://github.com/ashhart/TensorFold/releases/tag/v0.6.4

On a DGX Spark the combined Flash Next stack (#202, #201, #254, #248, #268) matched one-token decoding in 12 of 12 cells, with prompt speed at parity. Thank you for the careful work.

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.

4 participants