Skip to content

perf: Improve cuda startup/loading performance (3-15x faster on DGX spark) - #82

Closed
pmeenan wants to merge 1 commit into
ashhart:mainfrom
pmeenan:perf
Closed

pmeenan wants to merge 1 commit into
ashhart:mainfrom
pmeenan:perf

Conversation

@pmeenan

@pmeenan pmeenan commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

This is a set of changes targeting the initial startup/loading of models in TensorFold (trying to get it closer to the hardware I/O limits). It mostly involves better streaming of the data from disk, running the n-gram loading in parallel and offloading some of the packing into the GPU (off of the CPU).

This was heavily aided by Opus 5.5 and Astra 6 but I guided the specific changes and looked over the code after they both cross-reviewed it as well (though I did defer to Opus on the write-up below because it was a lot more diligent than I would have been in writing it up).

perf: 3–15× faster cold start on CUDA (every loader)

Cold starts on a DGX Spark were dominated by how checkpoints reach the GPU, not by the size of the models:

  • small buffered reads one at a time (0.2–1.1 GB/s from an SSD that sustains ~13 GB/s);
  • an expert repack running about 40 elementwise passes;
  • a device synchronization after every layer.

This PR reworks the load path of every CUDA family: Flash Next (MLX and EXL3), Qwen3.8-27B (MLX and EXL3, with DFlash2), Qwen3.6-35B, Nemotron, and GLM-5.3-Flash on two ranks. Every model gives the same greedy output as before, token for token.

Results

Time from docker run to the first streamed token, on a cold page cache with the kernel cache warm. Baseline is TensorFold 0.3.6.2 (71377a5), and each result is on this branch.

Model Checkpoint Before After Speedup
Qwen3.8 Flash Next Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP 146 s 24 s 6.1×
Qwen3.8 Flash Next turboderp/Qwen3.8-Flash-Next-exl3 (3.05bpw_h5_ng5) 94 s 27.5 s 3.4×
Qwen3.8-27B + DFlash2 Vontra/Qwen3.8-27B-MLX-4bit 96–109 s 15.7 s ~6.5×
Qwen3.8-27B + DFlash2 turboderp/Qwen3.8-27B-exl3 (3.00bpw) 23–25 s 8.4 s 2.9×
Qwen3.6-35B-A3B Vontra/Qwen3.6-35B-A3B-MLX-4bit-MTP 116–122 s 7.7 s ~15×
Nemotron 3.5 Lightning 30B-A3B Vontra/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit 30 s 8.6 s 3.5×
GLM-5.3-Flash (--tp 2, two Sparks) Vontra/GLM-5.3-Flash-MLX-4bit-MTP 335–348 s 41 s 8.3×

Before: two cold starts each. After: one start on the final tree. Across repeated runs during development, results varied by about ±1 s.

Setup:

  • NVIDIA DGX Spark (GB10, 128 GB unified memory, internal NVMe, ext4), driver 580.178.04.
  • nvcr.io/nvidia/pytorch:26.07-py3: PyTorch 2.13, CUDA 13.3, Triton 3.7.1.
  • The checkpoint (and any draft model) was dropped from the page cache with POSIX_FADV_DONTNEED before each start, with the GPU idle and ≥115 GiB available.
  • GLM ran rank 0 and rank 1 on two Sparks over the direct QSFP link.

How Flash Next MLX got there (146 s → 24 s), with the step each change was measured on:

Change First token
Baseline 146 s
Direct reads into pinned buffers 47 s
Fused expert pack kernel 35 s
empty_cache() every 8 layers 29 s
n-gram table prefetch overlapped with the load 23 s
Read-ahead on a side upload stream 22–24 s (the SSD is now the limit, at ~9 GB/s)

How GLM got there (341 s → 41 s):

Change First token
Baseline 341 s
Direct reads 184 s
Read-ahead on 8 threads 125 s
Experts stacked on the GPU 71 s
Neighbouring tensors in shared 128 MiB reads 56 s
Fused pack + upload stream 48 s
empty_cache() every 8 layers 41 s

What changed

1. Direct checkpoint reads: tensorfold/cuda/direct_read.py (new)

  • Reader reads byte ranges with O_DIRECT in 4 KiB-aligned spans of up to 64 MiB. Each span goes out as hundreds of requests in flight.
    • To the GPU: through two reused pinned pieces with async copies, so the read of one piece overlaps the copy of the other. Each copy's event is recorded on the destination device's stream.
    • To host memory: into aligned buffers.
  • It falls back to buffered reads for good when O_DIRECT is refused: no os.O_DIRECT, or EINVAL on the open or on a read. A read past the end of a truncated file raises instead of looping.
  • SafeTensors is a header index plus direct reads, and replaces safe_open in every CUDA loader: the 27B (MLX, EXL3 and the DFlash2 draft model), Qwen3.6's MTP tensors, and Nemotron. The Flash Next MLX reader and the EXL3 pack reader use Reader directly.
  • The 27B's safe_open(device="cpu") path went through the memory map at ~0.2 GB/s; it now reads at 1.6–5 GB/s.

2. Read-ahead: ReadAhead in the same module

  • Neighbouring tensors of a file come in one direct read of up to 128 MiB: tensors 1 MiB or less apart share a read, and a larger gap starts a new one. Reads run on 8 threads.
  • With a CUDA device, each read lands in pinned memory, goes up in one async copy on a dedicated stream, and is cut into per-tensor copies there. take() makes the caller's stream wait on the upload event and calls record_stream, so the allocator can't reuse the memory early.
  • Flash Next queues each layer's tensors and the next layer's as a layer starts, excluding the n-gram shards (a memory map reads those). What a layer doesn't take is dropped.
  • GLM's RankReader queues each layer's experts. Its row, column and dim1 splits run on the GPU (split_device), and pre-split rank folders use the same path. GLM's routed experts are ~2,300 small tensors a layer, so one-at-a-time reads had left the SSD mostly idle.
  • Both loaders wait on the CUDA event from two layers back before starting a layer. That caps the GPU work in flight, and so the buffers read ahead.

3. Fused expert pack: tensorfold/cuda/experts_pack.cu (new)

  • experts.pack regroups MLX 4-bit experts into the grouped kernels' blocks. It was an int64 per-nibble loop plus permute copies, about 40 elementwise passes.
  • One kernel now reads each word, scale and bias once and writes its block slot directly.
  • On a 257-expert 2048×4096 projection (1.08 GB), it takes 12 ms against 850 ms, bit-identical.
  • It speeds up Flash Next, Nemotron, Qwen3.6 and GLM.
  • The extension is renamed tensorfold_experts_v7, so it rebuilds once after upgrading, in about 30 s.

4. GLM: experts stacked on the GPU

  • Each layer's 257 expert tensors per projection were stacked with torch.stack on the CPU and then copied, single-threaded.
  • They now arrive on the GPU through the read-ahead and are stacked there in one kernel.

5. Fewer device synchronizations

  • torch.cuda.empty_cache() runs every eighth layer instead of every layer, in Flash Next (MLX and EXL3), Nemotron and GLM. Each call waited for the GPU.
  • With the fused pack, a layer leaves few temporaries.
  • The lowest available memory during a start is unchanged for Flash Next and Nemotron (33.5, 25.7 and 97.2 GiB). GLM's rank 0 held about 3 GiB more in the allocator cache, at 23.9 GiB.

6. The n-gram tables

  • Flash Next's 32 GB of host tables were prefetched after the load. They are now prefetched on a background thread started when the table is built (layer 1), so the reads overlap the weight load.
  • The engine owns the prefetch futures. It waits for them after the load (on failure too), and a prefetch error is raised.
  • EXL3's table used to be locked with mlock directly, which faulted 33 GB in one page at a time (28 s). The 8-thread prefetch now runs first, and the lock takes 0.4 s.
  • The startup line now reports how long the start waited for the tables after the weights, and the lock time.

Correctness

  • Every model's greedy 64-token reply (temperature 0, drafts on) has the same token_sha before and after:
    • Flash Next MLX 1ab8fcf3894c, Flash Next EXL3 c8ee64ccacb3;
    • 27B MLX 20f815d12e6d, 27B EXL3 ff73a0b1628a;
    • Qwen3.6 6df10f796821, Nemotron 373fb6409cf4, GLM de5a23e55503.
  • New tests:
    • tests/test_direct_read.py (host, no GPU): direct and buffered reads against safe_open, unaligned tensors, truncated files, refused O_DIRECT, ReadAhead.
    • tests/cuda/test_direct_read_cuda.py: device reads; the Flash Next reader, direct and read-ahead; the fused pack against the original torch pack for both group sizes and non-contiguous inputs; the background-prefetch futures.
    • tests/cuda/test_glm_split_and_policy.py: read-ahead in runs, GPU uploads, rank folders, and uploads consumed without synchronization.
  • The full suite runs patched and unpatched in the same container, and the two failure lists are compared. No test fails with this change that passes without it:
    • patched: 1,992 passed;
    • the 121 failures and collection errors left fail identically unpatched. They include MLX-only modules that can't import on Linux and tests that fail in this container either way;
    • the 8 that fail only unpatched are the new tests, which the old code can't pass.

Notes for review

  • All changes live in CUDA-only modules (tensorfold/cuda/, families/*/cuda/). host_table.py, which the MLX path shares, is unchanged.
  • Admission doesn't count the read-ahead's in-flight buffers: at most two layers ahead, plus up to 8 × 128 MiB of pinned staging. The measured headroom stayed 24–33 GiB, but the estimate could include them.
  • Not covered here:
    • GLM's EXL3 path keeps host reads, and wasn't measured (no checkpoint).
    • GLM's DFlash2 loader and the offline split_file tool are unchanged.
    • The first start in a new container still compiles kernels (+87 s on Flash Next). Persisting ~/.cache and ~/.triton across containers, a RUNBOOK change, would address that.
  • tests/test_cancellation.py::test_streaming_socket_disconnect_during_decode_releases_the_active_row hangs on Linux with or without this change: its recv loop never ends once the server closes. The suite ran with pytest-timeout.

@ashhart

ashhart commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Landed in 0.5.0 as 8ae247f. Thank you! CUDA startup and model loading are faster. On our side the direct reader now gives its pinned staging back after each load (da8a5df). More like this is very welcome.

@ashhart ashhart closed this Sep 29, 2026
@ashhart ashhart added the landed Landed on main by the maintainers; the commits keep the contributor's authorship label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

landed Landed on main by the maintainers; the commits keep the contributor's authorship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants