Repository navigation
Conversation
warm() defers every graph's capture for Gemma 4 as for Llama (rkinas#37): the warm-up calls in the grid's order, then the captures largest first. The deferred graphs are captured before _warm_long_sfx, whose pass-2 graphs over the longer roots are then captured one at a time as before, so its PASS2_POOL budget measures only what they add.
Reviewer's GuideThis PR extends the existing deferred, largest-first CUDA graph capture strategy from Llama to Gemma 4 by capturing the complete grid before _warm_long_sfx, while retaining individual capture and pool-budget accounting for longer-root pass-2 graphs. It adds focused ordering tests and documents the resulting memory reduction and bit-identical outputs. Sequence diagram for Gemma 4 largest-first graph capturesequenceDiagram
participant GraphBackend
participant Grid as Graph_grid
participant Capture as CUDA_graph_capture
participant LongSfx as _warm_long_sfx
GraphBackend->>Grid: warm()
Grid->>GraphBackend: _graph_c() and _graph_sfx()
GraphBackend->>GraphBackend: _capture_deferred()
GraphBackend->>Capture: _record() deferred graphs by _graph_tokens descending
GraphBackend->>LongSfx: _warm_long_sfx()
LongSfx->>Capture: capture each longer-root pass-2 graph
Capture-->>LongSfx: update PASS2_POOL accounting
LongSfx-->>GraphBackend: complete warm-up
Flow diagram for deferred Gemma 4 graph warming and captureflowchart TD
A["GraphBackend.warm()"] --> B["Warm compact, plain, and pass-2 graphs in grid order"]
B --> C{"Gemma 4"}
C -->|yes| D["_capture_deferred()"]
D --> E["Capture deferred graphs by _graph_tokens descending"]
E --> F["_warm_long_sfx()"]
F --> G["Warm and capture longer-root pass-2 graphs individually"]
G --> H["Keep additions within PASS2_POOL"]
C -->|no| I["_capture_deferred()"]
I --> J["Capture remaining deferred graphs by _graph_tokens descending"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/test_engine16.py" line_range="260" />
<code_context>
+ caps = [k for kind, k in grid if kind == "capture"]
+ assert [kind for kind, _ in grid] == ["warm"] * len(caps) + ["capture"] * len(caps)
+ assert [_graph_tokens(k) for k in caps] == sorted(map(_graph_tokens, caps), reverse=True)
+ assert {"c", "p", "x"} <= {k[0] for k in caps}
+ assert [kind for kind, _ in long] == ["warm", "capture"] * (len(long) // 2) and len(long) == 2 * 2 * 2
+ assert all(k[0] == "x" and k[3] == 64 for kind, k in long if kind == "capture")
</code_context>
<issue_to_address>
**Omitted graphs pass the order test**
When `warm()` omits a grid graph while still emitting graphs from all three families, `test_gemma_warm_captures_largest_first_then_the_long_roots` only checks that `caps` contains each graph family; line 258 compares event counts, not the expected graph keys. If `warm()` skips a grid graph but still emits at least one graph from each family, the test passes without detecting the omission.
Build the expected grid-key set and assert that the captured keys match it.
</issue_to_address>…basal-2.0-4.5B on 16 GB cards) #41 by Miłosz Kucharski (@Butterski), rebased on basal 2.3.4 without #40 (main already has the same change, a63cfdb): pass-2 graphs last, the smallest first, while PASS2_RESERVE stays free; Gemma 4's long-root graphs within it too; WHATS_NEW and the README contributor name.
|
Thank you, Miłosz. #40 is the same change as our commit a63cfdb, written in parallel: yours came 17 minutes earlier, and 2.3.4 shipped ours (basal-omni-1.0-mini on an H100: 27.8 -> 18.9 GB after start-up, every probability the same). |
The follow-up suggested in #37: Gemma 4's graphs still captured smallest first.
Problem
#37 made
warm()warm every graph up in the grid's order and capture them largest first, so each capture reuses thepool blocks a larger one freed. Gemma 4 (basal-omni) was left out because
_warm_long_sfxmeasures the pool growthof its pass-2 graphs over the longer roots against
PASS2_POOLas it captures them. Its compact graphs (16 ... 12,288tokens), plain graphs and pass-2 graphs within
LENStherefore still capture smallest first: on an H100,basal-omni-1.0-mini with compact rows keeps 10.6 GiB of graph pool of which 0.7 GiB is in use (#37).
Change
GraphBackend.warm()(basal/engine.py): every model defers its captures; for Gemma 4 the deferred graphs arecaptured (largest first) right before
_warm_long_sfx, which then captures its graphs one at a time as before, so itsbudget measures only what they add. Gemma 4's graph outputs are already small (readout rows only), so only the order
changes; the compiled kernels are those of today (the warm-up calls keep the grid's order).
Measurements
A100 PCIe 40 GB, torch 2.13.0+cu130, basal-omni-1.0-mini with its default kernels there (math, uncompiled; compact
rows need FlashAttention 3),
GraphBackendbuilt as--mode fastbuilds it,main(47ae982, 2.3.3) vs this branch,memory from
torch.cuda.mem_get_infoafter warm-up, the identity items ofbasal.identity.build(200 singlequestions, 40 five-question requests, 20 long):
mainThe 1.0 GiB that
_warm_long_sfxmay add (PASS2_POOL) is part of both. Not measured here: compact rows (Hopper withFlashAttention 3), where #37 found 10.6 GiB of pool with 0.7 GiB in use; that is the pattern this order removed for
the Llama compact graphs (9.0 -> 1.0 GiB on mini, H100 confirmed in #37). An H100 was not available to me (three
rented hosts did not finish loading).
Tests
test_gemma_warm_captures_largest_first_then_the_long_roots(tests/test_engine16.py): with Gemma 4 andcompact rows, every graph of the grid (compact, plain, pass 2) is warmed up before the first capture and captured in
descending
_graph_tokens, then each pass-2 graph over a longer root is captured right after its own warm-up. The2.3.3 order (Gemma 4 not deferred) and a missing capture before
_warm_long_sfxeach fail it.tests/(withouttest_gguf.py): CPU 702 passed, 81 skipped; A100 736 passed, 47 skipped.Summary by Sourcery
Reduce Gemma 4 CUDA graph memory overhead by capturing deferred graphs largest-first before processing longer-root pass-2 graphs.
Enhancements:
Documentation:
Tests: