Files
tokenspeed/python
Nicolas Perrin 8214efc977 perf(startup): freeze the collector once around every graph capture (#3555)
## Summary

Prefill and drafter graph captures (`BreakableCapture`) collect garbage
on entry, because a collection inside a capture would run graph
destructors on a capturing stream; decode captures (`torch.cuda.graph`)
collect only under `force_cudagraph_gc`. Only the decode ladder ran with
the heap frozen (`freeze_gc`). So the prefill buckets and the drafter's
prefill and extend graphs each ran a full collection over the startup
heap: 150-200 ms apiece, about 150 per start.

- `ModelExecutor.capture_graphs` now holds one frozen heap around every
capture: decode, prefill and drafter graphs.
- A boot that captures nothing (enforce-eager) skips the freeze and its
two collections, as on main.
- `--enable-cudagraph-gc` still keeps the heap unfrozen.
- The decode runner no longer freezes on its own. Nested scopes would
break, because an inner `gc.unfreeze()` thaws the outer scope.

## Results

Qwen3.8-Flash-Next-NVFP4, TP1, MTP, 56 prefill buckets plus the drafter
graphs. One GPU, warm compile caches, fresh server per run:

| | main | this PR |
|---|---|---|
| full collections in the scheduler process | 148, 24.0 s | 148, 3.4 s |
| server ready | 204.6, 207.6, 209.6 s (mean 207.3) | 187.5, 190.5 s
(mean 189.0) |

Both arms capture the same graphs with the same memory:

| | both arms |
|---|---|
| KV cache | 182.05 GB |
| activations + graphs | 3.98 GB |
| device free | 5.63 GB |

One consequence: an object that existed before capture and becomes
cyclic garbage during a capture is now freed after the last capture
instead of between capture groups. On this model's boot with the memory
probe enabled, allocated memory is byte-identical at every one of the
5714 captures and the probe series are identical.

Serving is unchanged:

- First 64k-token prefill: 0.88 s; later ones: 0.655 s.
- The same kernels compile on the first requests.
- No collection runs while serving.

## Test Plan

- `test_dispatch_autotune.py` runs `ModelExecutor.capture_graphs` with
its capture doubles:
- `test_capture_freezes_the_heap_once_around_every_capture`: the decode,
prefill and drafter captures see a frozen heap unless
`--enable-cudagraph-gc`; one `gc.freeze` spans them all; a boot that
captures nothing collects nothing.
- `test_a_failed_capture_leaves_the_heap_unfrozen`: a capture failing at
any position leaves the heap as it found it.
- Each of these mutations makes them fail: no `try`/`finally` in
`freeze_gc`, one freeze per capture group, a decode-only freeze, no
early return, `or` in its predicate, the flag ignored, no freeze.
- `pre-commit run --all-files` passes.
- The 11 test files that use `capture_graphs`, the decode runner's
capture or `freeze_gc` fail the same tests on this branch and on main
(28 FlashInfer BF16 adapter tests in `test_dispatch_autotune.py`, and
`test_draft_final_step_follows_the_complete_drafter_run`, which fails on
main since #3534).

---------

Signed-off-by: Nicolas Perrin <nperrin.ai@gmail.com>
2026-11-18 11:08:38 -05:00
..
2026-05-06 13:27:02 +00:00