Skip to content

perf(fast): Gemma 4 graphs captured largest first too (basal-omni) - #40

Closed
Butterski wants to merge 4 commits into
rkinas:mainfrom
Butterski:perf/gemma-capture-order
Closed

Butterski wants to merge 4 commits into
rkinas:mainfrom
Butterski:perf/gemma-capture-order

Conversation

@Butterski

@Butterski Butterski commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 the
pool blocks a larger one freed. Gemma 4 (basal-omni) was left out because _warm_long_sfx measures the pool growth
of its pass-2 graphs over the longer roots against PASS2_POOL as it captures them. Its compact graphs (16 ... 12,288
tokens), plain graphs and pass-2 graphs within LENS therefore 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 are
captured (largest first) right before _warm_long_sfx, which then captures its graphs one at a time as before, so its
budget 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), GraphBackend built as --mode fast builds it, main (47ae982, 2.3.3) vs this branch,
memory from torch.cuda.mem_get_info after warm-up, the identity items of basal.identity.build (200 single
questions, 40 five-question requests, 20 long):

main this branch
graph pool after warm-up 4.53 GiB 3.20 GiB
GPU memory used after warm-up 37,717 MiB 36,351 MiB
warm-up 302 s 298 s
probabilities 420 / 420 bit-identical

The 1.0 GiB that _warm_long_sfx may add (PASS2_POOL) is part of both. Not measured here: compact rows (Hopper with
FlashAttention 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

  • New test_gemma_warm_captures_largest_first_then_the_long_roots (tests/test_engine16.py): with Gemma 4 and
    compact 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. The
    2.3.3 order (Gemma 4 not deferred) and a missing capture before _warm_long_sfx each fail it.
  • tests/ (without test_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:

  • Capture Gemma 4 CUDA graphs largest-first while preserving the existing warm-up order, reducing graph-pool and startup GPU memory usage.

Documentation:

  • Document the Gemma 4 graph-capture memory improvement in the 2.3.4 release notes.

Tests:

  • Add coverage verifying Gemma 4 graphs are warmed before largest-first capture and that longer-root pass-2 graphs are captured within their existing budget.

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.
@sourcery-ai

sourcery-ai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This 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 capture

sequenceDiagram
    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
Loading

Flow diagram for deferred Gemma 4 graph warming and capture

flowchart 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"]
Loading

File-Level Changes

Change Details Files
Defer and batch Gemma 4 CUDA graph captures so all grid graphs are captured largest-first before long-root pass-2 graphs are measured.
  • Initialize deferred capture collection for every model.
  • Capture deferred compact, plain, and in-grid pass-2 graphs in descending token order.
  • Keep longer-root pass-2 graphs in _warm_long_sfx captured individually after warm-up, preserving PASS2_POOL accounting.
  • Update graph-capture documentation and add the 2.3.4 release note.
basal/engine.py
docs/WHATS_NEW.md
Add coverage for Gemma 4 warm-up and capture ordering, including compact and long-root pass-2 graphs.
  • Mock graph builders and record warm-up/capture events.
  • Assert all grid graphs warm before capture and capture in descending token order.
  • Assert longer-root graphs alternate warm-up and capture after the deferred batch.
tests/test_engine16.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/test_engine16.py Resolved
rkinas pushed a commit that referenced this pull request Oct 23, 2026
…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.
rkinas added a commit that referenced this pull request Oct 23, 2026
@rkinas

rkinas commented Oct 23, 2026

Copy link
Copy Markdown
Owner

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).
Your A100 numbers with default rows (graph pool 4.5 -> 3.2 GiB, 420 readouts bit-identical) and your test of the exact set of captured graphs add to what we measured. We credit you for it in the 2.3.4 release notes, WHATS_NEW and the README's Contributors list.
Since main already has the change, I'm closing #40 as included. #41 went into 2.3.5 rebased without it.

@rkinas rkinas closed this Oct 23, 2026
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