Skip to content

perf(fast): one warm-up call per CUDA graph (start-up 31-38% shorter on a 16 GB card) - #43

Closed
Butterski wants to merge 7 commits into
rkinas:mainfrom
Butterski:perf/warm-once
Closed

Butterski wants to merge 7 commits into
rkinas:mainfrom
Butterski:perf/warm-once

Conversation

@Butterski

@Butterski Butterski commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

GraphBackend._capture ran every graph's function 3 times (plain and compact graphs) or 2 times (pass-2 graphs)
before capturing it. What a capture cannot do happens at the first call: torch.compile compiles (or recompiles for a
shape its guards reject), cuBLAS sets up its handle and workspace, Triton loads its kernels. The second and third
calls run the same kernels on the same shapes again. On a consumer card that is most of start-up: basal-2.0-4.5B has
169 plain graphs over 796,928 padded tokens and 264 pass-2 graphs, and on an RTX 4060 Ti the repeated calls took 414 s
of its 1,094 s warm-up (phase timers below).

Change

_warm_up(fn, static) calls fn once on the side stream; _capture, _graph_sfx and 2.4.0's
GDNGraphBackend._graph_sfx (Qwen3.5) lose their warmup argument. Nothing else changes: the same shapes are warmed
up in the same order (so torch.compile builds the same kernels, and fla's kernels pick their launch configs at the
same first call), and the captures are the same.

Measurements

torch 2.13.0+cu130, GraphBackend built as --mode fast builds it (warm() timed, compile caches cleared before
every run), then the identity items of basal.identity.build (200 single questions in batches of 16, 40
five-question requests, 20 long), probabilities compared with the run before the change on the same machine.
Measured on 47ae982 (2.3.3), the 4.5B with #41 (now in 2.3.5); the branch is rebased on 2.4.0, which leaves basal-2.0
bit-identical to 2.3.5 (4,960 of 4,960 readouts, its release notes).

card, model warm-up before after repeated calls removed GPU memory after warm-up probabilities
RTX 4060 Ti 16 GB, basal-2.0-mini (main) 468 s 323 s 142 s 8.38 GiB, same 420 / 420 bit-identical
RTX 4060 Ti 16 GB, basal-2.0-4.5B (with #41, needed on 16 GB) 1,094 s 680 s 414 s 15.38 GiB, same 420 / 420 bit-identical

basal-omni-pico (Qwen3.5, 2.4.0's GDNGraphBackend), basal-serve --mode fast on one RTX 4060 Ti, fla 0.5.2,
TRITON_CACHE_AUTOTUNING=1 so that starts sharing a cache reuse fla's timed launch configs; answers to 10 requests
(compress over 1.5k-6k tokens, 5-40 questions; 679 probabilities) per start:

start main (2.4.0) this branch probabilities
cold compile caches ready in 411 s 324 s bit-identical, branch against main
warm caches (the other one's) 183 s 100 s bit-identical, all four starts

Not measured: basal-omni (Gemma 4), whose graphs go through the same _warm_up. My rented 40-48 GB hosts failed
(a driver too old for torch's CUDA 13 build, two that never finished loading), and basal-omni-1.0-mini does not load
in this probe on a 24 GB card. Could you run your usual H100 identity check on basal-omni with this branch?

Where the time goes now (RTX 4060 Ti, 4.5B, after): the first call of the 54 plain graphs up to 2,048 tokens takes
450 s (compiling), the first call of the 115 longer ones 171 s (running them once), the pass-2 graphs 45 s, the
captures 8 s. The identity items took the same time before and after (77 s on the 4.5B, 27 s on mini), so nothing is
compiled late at run time.

basal-serve --model Remek/basal-2.0-4.5B with #41 on the same RTX 4060 Ti: ready in 689 s, 14,424 MiB after
start, basal-loadtest 21.6 decisions/s on single questions and 9.6 questions/s five at a time over 1.5k-token
states, 0 errors.

Tests

  • New test_each_graph_warmed_up_once_before_its_capture (tests/test_engine16.py): the engine's _capture /
    _warm_up with torch.cuda's stream calls as no-ops; each graph's function runs once before its capture, a waiting
    key is not warmed up again. Three calls, or a waiting key warmed up again, make it fail.
  • tests/test_engine16.py (perf(fast): pass-2 graphs only while GPU memory stays free (basal-2.0-4.5B on 16 GB cards) #41's budget tests included), tests/test_gemma4_graph.py: the stubs follow the new
    signatures.
  • tests/ (without test_gguf.py): CPU 739 passed, 81 skipped (on 2.4.0).

ExitGraphBackend (--mode fast-exit) keeps its own 3-call warm-up loop (not measured here).

@sourcery-ai

sourcery-ai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The fast CUDA graph backend now performs exactly one side-stream warm-up call per graph before capture instead of three calls for regular graphs and two for pass-2 graphs, while preserving graph ordering, deferred capture, shapes, and runtime behavior. Tests and release notes were updated; the reported RTX 4060 Ti measurements show 31–38% shorter warm-up with identical probabilities and GPU memory.

Sequence diagram for single CUDA graph warm-up and capture

sequenceDiagram
    participant GraphBackend
    participant SideStream as CUDA side stream
    participant Function as Graph function
    participant Compiler as torch.compile
    participant Capture as CUDA graph capture

    GraphBackend->>GraphBackend: _capture(key, fn, static)
    GraphBackend->>GraphBackend: _warm_up(fn, static)
    GraphBackend->>SideStream: wait_stream(current_stream)
    SideStream->>Function: fn(*static)
    Function->>Compiler: compile or recompile if needed
    Function->>Function: Initialize cuBLAS and Triton kernels
    SideStream-->>GraphBackend: warm-up complete
    GraphBackend->>Capture: _record(key, fn, static)
    Capture->>Function: capture fn(*static)
    Capture-->>GraphBackend: CUDA graph ready
Loading

Flow diagram for fast CUDA graph warm-up optimization

flowchart LR
    A[Graph requested] --> B{Deferred capture active?}
    B -->|Yes and key is new| C["_warm_up(fn, static)"]
    B -->|No| C
    C --> D[One call on side stream]
    D --> E{Deferred capture active?}
    E -->|Yes| F[Queue fn and static]
    E -->|No| G["_record(key, fn, static)"]
    F --> H[Capture later]
    G --> I[CUDA graph ready]
    H --> I
Loading

File-Level Changes

Change Details Files
Reduce CUDA graph warm-up to a single side-stream invocation before capture.
  • Remove the configurable warm-up count from graph capture.
  • Invoke each graph function once while preserving existing ordering, deferred-capture behavior, static shapes, and capture flow.
  • Apply the unified signature to pass-2 graph capture paths, eliminating their previous two-call warm-up.
basal/engine.py
Update graph-engine documentation with measured start-up improvements and unchanged runtime results.
  • Document the one-call warm-up behavior in the unreleased 2.3.5 notes.
  • Record RTX 4060 Ti warm-up reductions, unchanged GPU memory and throughput, and bit-identical probabilities.
docs/WHATS_NEW.md
Align graph backend test doubles with the simplified capture and warm-up interfaces.
  • Remove warm-up count parameters from test capture overrides.
  • Keep event recording and single-call warm-up behavior in engine16 and Gemma 4 graph stubs.
tests/test_engine16.py
tests/test_gemma4_graph.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

Butterski added a commit to Butterski/basal that referenced this pull request Oct 22, 2026

@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="209" />
<code_context>
         return self._record(key, fn, static)

-    def _warm_up(self, fn, static, n):
+    def _warm_up(self, fn, static):
+        """One call of fn on the side stream before its capture: what a capture cannot do happens at a forward's first
+        call (torch.compile compiles, or recompiles for a shape its guards reject; cuBLAS sets up its handle and
</code_context>
<issue_to_address>
**Repeated graph warm-ups go undetected**

When `GraphBackend._warm_up` is changed to invoke `fn` more than once, `test_warm_captures_largest_first_after_every_warm_up` and `test_warm_captures_largest_first_then_long_pass2` replace `_warm_up` with overrides that log one event and call `fn` once. Their event-count assertions still pass if `GraphBackend._warm_up` regresses to multiple calls, so the startup regression goes undetected.

Add a test that exercises the production `_warm_up` and counts invocations of `fn`.

Also at `tests/test_gemma4_graph.py:319`.
</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
Butterski added a commit to Butterski/basal that referenced this pull request Oct 23, 2026
Plain graphs were warmed up 3 times and pass-2 graphs twice. What a
capture cannot do (torch.compile compiling, cuBLAS handle and workspace,
Triton loading kernels) happens at the first call; the further calls ran
the same kernels again and only took start-up time.
rkinas pushed a commit that referenced this pull request Oct 23, 2026
rkinas added a commit that referenced this pull request Oct 23, 2026
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! #43 is in basal 2.4.1: https://github.com/rkinas/basal/releases/tag/v2.4.1

We rebased it on 2.3.5 and then on 2.4.0, and ran our H100 identity check, including basal-omni as you asked:

  • Answers: every probability is the same as before. That covers basal-2.0-mini with default and compact rows, the 4.5B with compact rows, basal-omni-1.0-mini and 1.0-max (5,800 readouts), and basal-omni-pico on 2.4.0's Qwen3.5 engine (1,700 items).

  • Determinism: the 2.3.4 check still holds with one warm-up call. Two runs on empty compile caches and one on a warm cache give bit-identical probabilities. The Gemma 4 forward compiles in Inductor's deterministic mode, so the first call already picks the same kernels.

  • Start-up on an H100, warm compile cache: 15-16% shorter on basal-2.0, 19% on basal-omni-1.0-mini and 40% on basal-omni-1.0-max (203 -> 123 s). GPU memory is the same.

  • Two small follow-ups in the rebase:

    Both now use your one-call _warm_up(fn, static).

It's credited to you in the 2.4.1 notes, the README and WHATS_NEW. Closing this PR as merged there. Thanks again!

@rkinas rkinas closed this Oct 23, 2026
kardysm pushed a commit to kardysm/basal that referenced this pull request Dec 3, 2026
… are captured without a warm-up call

warm() ran every grid shape once before its capture (rkinas#43: one call instead of
three). What only a first call does happens at a family's first shapes: the
forwards are compiled with dynamic shapes (one-row shapes specialised), and
pass 2 runs uncompiled. Warmed up as before, in the grid's order: plain shapes
of the first two lengths, compact shapes of the first two, pass-2 shapes of
every (rows, branch length) at their first root length. The others are
captured directly under torch.compiler.set_stance("fail_on_recompile"): a
shape that would compile again (or fail in the capture for another reason) is
warmed up and captured again, with a line. Gemma 4, Qwen3.5 (fla tunes per
shape) and fast-exit warm up every shape as before.
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