What a graph is#

One launch replays a whole run; the host reads nothing until it’s done. That is the whole reason eagle captures a graph instead of looping.

Time: ~2 min · Runs on: CPU up to What just happened (GPU: this notebook picks it up automatically); the last section, the low-level door, needs a CUDA GPU · You need: eagle, hawk, NumPy, matplotlib and CuPy (GPU only); read first: Python quickstart

You will build the figure below: a schematic of the two ways to run many steps — reading the device after every one, or recording the sequence once and replaying it.

Colab or Kaggle: run this first cell (it installs the packages and downloads the helper files this notebook uses). Elsewhere it only checks the imports.

Hide code cell source

# Colab setup, generated by python -m raptor_docs.nb colab_setup (do not edit).
import os

try:
    import google.colab  # noqa: F401
    HOSTED = True  # a Colab runtime
except ImportError:
    HOSTED = "KAGGLE_KERNEL_RUN_TYPE" in os.environ  # a Kaggle kernel

if HOSTED:
    import importlib.metadata, pathlib, re, shutil, subprocess, sys, urllib.request

    gpu = bool(shutil.which("nvidia-smi")) and subprocess.run(["nvidia-smi", "-L"], capture_output=True).returncode == 0
    extra = ""
    if gpu:
        # the CUDA major the runtime already has: a CuPy that is installed, else torch's, else the driver's
        major = None
        for m in ("13", "12"):
            try:
                importlib.metadata.version(f"cupy-cuda{m}x")
                major = m
                break
            except importlib.metadata.PackageNotFoundError:
                pass
        if major is None:
            try:
                import torch
                major = "13" if str(torch.version.cuda).startswith("13") else None
            except ImportError:
                pass
        if major is None:
            smi = re.search(r"CUDA Version: (\d+)", subprocess.run(["nvidia-smi"], capture_output=True, text=True).stdout)
            major = "13" if smi and int(smi.group(1)) >= 13 else "12"
        extra = f"[cuda{major}]"
        print(f"GPU found: installing the {extra} extras to match this runtime.")
    pre = ["--pre"] if os.environ.get("RAPTOR_PRE") else []  # optional: test a release candidate
    ref = os.environ.get("RAPTOR_REF", "main")  # optional: fetch the helper files from this git ref
    if pre or "RAPTOR_REF" in os.environ:
        print(f"Pre-release test: installing with --pre, helper files from {ref}.")
    # our packages with --upgrade, so a runtime that kept an older release gets the current one
    subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "--upgrade", *pre, f"raptor-eagle{extra}", f"raptor-hawk{extra}"])
    subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "matplotlib"])
    base = os.environ.get("RAPTOR_RAW_BASE", "https://raw.githubusercontent.com")  # optional: a local mirror
    top = os.environ.get("RAPTOR_WORKDIR") or ("/kaggle/working" if "KAGGLE_KERNEL_RUN_TYPE" in os.environ else "/content")
    root = pathlib.Path(top) / "eagle"
    for rel in (
        "docs/content/_shared/nb_helpers.py",
        "docs/content/_shared/themed_figure.py",
        "docs/content/userguide/_fixtures/drag_ps.json",
        "docs/content/userguide/_fixtures/drag_ps.ptx",
        "docs/content/userguide/_fixtures/gravity.json",
        "docs/content/userguide/_fixtures/gravity.ptx",
    ):
        (root / rel).parent.mkdir(parents=True, exist_ok=True)
        urllib.request.urlretrieve(f"{base}/amasat01/eagle/{ref}/{rel}", root / rel)
    (root / "docs/content/userguide/tutorials").mkdir(parents=True, exist_ok=True)
    os.chdir(root / "docs/content/userguide/tutorials")
    if not gpu:
        print("Running on the CPU (no GPU in this runtime). For a GPU: " + ("Settings → Accelerator → GPU T4 x2." if "KAGGLE_KERNEL_RUN_TYPE" in os.environ else "Runtime → Change runtime type → T4 GPU."))

try:
    import eagle, hawk, matplotlib  # noqa: F401
except ImportError as err:
    raise ImportError(
        "eagle, hawk, matplotlib not found. Install with `pip install matplotlib raptor-eagle raptor-hawk`. For a GPU, add [cuda12] (or [cuda13]) to raptor-eagle and raptor-hawk."
    ) from err

Why this matters#

A GPU kernel launch is cheap, but a host round trip — launch, then read a device value back before deciding what to launch next — is not: the host and the device have to synchronize. A loop that reads “is anyone still running?” after every step pays that cost every step. A CUDA graph records a sequence of launches once, and the recorded graph can then repeat itself on the device, with a stop condition baked in — no round trip until the whole thing is done.

The performance pages time the arms of the ensemble card and the feature card on every device.

Hide code cell source

import matplotlib.pyplot as plt
import sys, pathlib
sys.path.insert(0, str(pathlib.Path.cwd().parent.parent / "_shared"))
from nb_helpers import show
import matplotlib.patches as mpatches

fig, axes = plt.subplots(2, 1, figsize=(5.5, 3.2), sharex=True)

ax = axes[0]
for i in range(8):
    ax.add_patch(mpatches.FancyArrow(i, 0, 0.6, 0, width=0.08, color="C0"))
    if i < 7:
        ax.plot([i + 0.6, i + 1], [0, 0], ":", color="gray", lw=1)
ax.set_title("eager loop: a host round trip after every launch", fontsize=9)
ax.set_yticks([])
ax.set_xlim(-0.3, 8)

ax = axes[1]
ax.add_patch(mpatches.FancyArrow(0, 0, 7.6, 0, width=0.08, color="C1"))
ax.set_title("eagle graph: one launch, the device loops itself", fontsize=9)
ax.set_yticks([])
ax.set_xlim(-0.3, 8)
ax.set_xlabel("time (not to scale)")

fig.tight_layout()
show()
Chart: eager loop: a host round trip after every launch; eagle graph: one launch, the device loops itself

The code#

eagle.deploy() builds the same kernel from Python quickstart into a plan; eagle.until_done() binds it and builds the loop — on the GPU, one GraphPipeline capturing the whole run; on the CPU, the identical loop runs directly (eagle.repeat_while: “a host loop everywhere else”). Either way runner.run() is the one call that crosses into the device or the host team and does not return until every sample is done (or max_steps).

Hide code cell source

import sys, pathlib
sys.path.insert(0, str(pathlib.Path.cwd().parent.parent / "_shared"))
from nb_helpers import gpu_available, xp_for
DEVICE = "gpu" if gpu_available() else "cpu"
xp = xp_for(DEVICE)
import eagle
import hawk
from hawk import Mutable, Param, Scalar, Terminated


@hawk.kernel
def oscillator(omega: Scalar, t_end: Param, dt: Param, terminated: Terminated,
               x: Mutable[Scalar], v: Mutable[Scalar], t: Mutable[Scalar]):
    x0, v0 = x, v
    x = x0 + dt * v0
    v = v0 - dt * omega * omega * x0
    t += dt
    terminated = t >= t_end


n = 1_000_000 if DEVICE == "gpu" else 100_000
omega = xp.linspace(1.0, 3.0, n)
runner = eagle.until_done(eagle.deploy(oscillator), max_steps=10_000,
                          x=xp.ones(n), v=xp.zeros(n), t=xp.zeros(n),
                          omega=omega, t_end=1.0, dt=1e-3)
report = runner.run()
print(f"one runner.run() call -- build (capture) took {report.build_s * 1e3:.2f} ms, "
      f"the kernel fired {report.launches} times while it ran")
one runner.run() call -- build (capture) took 0.00 ms, the kernel fired 1 times while it ran

What just happened#

  • runner.run() crossed into the device (or the host team) once; on the GPU, build_s is the one-time cost of recording the graph — every later run() on the same runner replays it with build_s == 0.

  • report.launches counts how many times the kernel actually fired while the loop ran — that work happens inside the one captured graph (or the one host loop), not as separate round trips from Python.

  • The same two lines (eagle.deploy + eagle.until_done) build a CUDA graph on the GPU and a plain loop on the CPU; nothing in the call changes.

Try this#

Call runner.run() a second time without runner.reset() first: it returns immediately (launches == 0) because every sample is already done.

Next#

  • Below: the graph engine’s low-level door directly — the same GraphPipeline/Launcher classes eagle.until_done() builds on, recording raw launches by hand (needs a CUDA GPU).

  • Performance (deeper): the eager-loop-vs-graph numbers above come from its card, on both a GPU and a CPU.

The low-level door: capturing a raw launch yourself#

Needs a CUDA GPU. GraphPipeline is the Python face of eagle’s graph engine: it drives the same eagle::cuda C++ classes (Stream, StreamCapturer, Graph, Launcher) every native consumer uses. Record a sequence of launches once, then replay the instantiated graph as many times as you like with no further Python-side dispatch.

Load the compiled kernel: LoadedVector wraps one already-compiled PTX artifact (gravity.ptx, a two-body point-mass acceleration) with its own sidecar metadata, with no hawk source and no recompilation.

import pathlib

import cupy as cp
import numpy as np

from eagle.loaded import LoadedVector
from eagle.pipeline import GraphPipeline

FIXTURES = pathlib.Path("../_fixtures").resolve()
force = LoadedVector(FIXTURES / "gravity.ptx")

Allocate the buffers the launch will read and write. Nothing inside a captured step may allocate device memory or synchronize, so position, out and terminated are all pre-allocated here, before any capture begins.

MU = 3.986004418e5
N = 1024
rng = np.random.default_rng(2)
position = cp.asarray(rng.uniform(7.0e3, 4.2e4, size=(3, N)))
out = cp.zeros((3, N), dtype=cp.float64)
terminated = cp.zeros(N, dtype=cp.bool_)

Build the pipeline: add takes a zero-argument callable issuing exactly one launch. Nothing inside a step may allocate device memory or synchronize — out, position and terminated are all pre-allocated above. build() captures the steps into a graph once and instantiates it.

pipe = GraphPipeline()
pipe.add(lambda: force.launch(out=out, position=position, mu=MU, terminated=terminated))
pipe.build()
<eagle.pipeline.GraphPipeline at 0x7f9cd416c770>

gravity’s sidecar declares accumulate: true, so each replay adds into out rather than overwriting it — replaying the graph k times accumulates k times the single-launch answer, which is a sharp way to check that a replay is really re-issuing the captured launch and not a no-op.

replays = 5
out.fill(0.0)
pipe.launch(n=replays)
cp.cuda.runtime.deviceSynchronize()

r = cp.linalg.norm(position, axis=0)
expected_once = -MU * position / r**3
diff = cp.abs(out - replays * expected_once)
rel_err = cp.max(diff / cp.abs(replays * expected_once))
print(f"{replays} replays vs {replays}x the single-launch answer: "
      f"max rel err = {float(rel_err):.3e}")
5 replays vs 5x the single-launch answer: max rel err = 1.024e-15

See also: Use eagle with other frameworks for feeding cupy/torch tensors through the same launchers, and eagle.compose.GraphComposer (API reference) for overlapping several independent pipelines on their own streams.