Gradients, forward (and what autodiff refuses)#

Push one direction through a kernel instead of pulling one back — and see the two kinds of kernel hawk’s autodiff will not differentiate.

Time: ~8 min · Runs on: CPU · You need: hawk, NumPy and matplotlib; read first: Gradients, backward

hawk.diff.jvp (“Jacobian-vector product”, the forward-mode rule) seeds one tangent plane per wrt input — a small perturbation of that input — instead of one adjoint per output, and pushes it forward along the same derivation vjp uses: the directional derivative of the output along that input direction.

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-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) / "hawk"
    for rel in (
        "docs/content/_shared/nb_helpers.py",
        "docs/content/_shared/themed_figure.py",
    ):
        (root / rel).parent.mkdir(parents=True, exist_ok=True)
        urllib.request.urlretrieve(f"{base}/amasat01/hawk/{ref}/{rel}", root / rel)
    (root / "docs/content/tutorials").mkdir(parents=True, exist_ok=True)
    os.chdir(root / "docs/content/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 hawk, matplotlib  # noqa: F401
except ImportError as err:
    raise ImportError(
        "hawk, matplotlib not found. Install with `pip install matplotlib raptor-hawk`. For a GPU, add [cuda12] (or [cuda13]) to raptor-hawk."
    ) from err

Hide code cell source

import sys, pathlib
sys.path.insert(0, str(pathlib.Path.cwd().parent / "_shared"))
from nb_helpers import plot_style, show
plot_style()
import numpy as np

import hawk
from hawk import Mutable, Scalar, Vector
from hawk.math import dot


@hawk.kernel
def energy(v: Vector[3], out: Mutable[Scalar]):
    out = 0.5 * dot(v, v)


energy
<Kernel energy slots=2>

jvp(primal, wrt=...) derives the forward IR; build both kernels:

from hawk import Kernel
from hawk.diff import jvp

energy_jvp = Kernel("energy_jvp", jvp(energy, wrt=("v",)))
n = 500
rng = np.random.default_rng(3)
v = rng.normal(size=(3, n))
dot_v = rng.normal(size=(3, n))   # the tangent direction to differentiate along
dot_out = np.asarray(energy_jvp(v=v, dot_v=dot_v))
h = 1e-6
fd = (np.asarray(energy(v=v + h * dot_v)) - np.asarray(energy(v=v - h * dot_v))) / (2 * h)
diff = np.max(np.abs(dot_out - fd))
print("max|jvp - central difference| over", n, "samples:", diff)
max|jvp - central difference| over 500 samples: 1.1460876692126476e-09

Hide code cell source

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(4, 4))
lo, hi = dot_out.min(), dot_out.max()
ax.plot([lo, hi], [lo, hi], color="#898781", lw=1, ls="--")
ax.scatter(dot_out, fd, s=6, color="#eb6834", alpha=0.5)
ax.set_xlabel("jvp (analytic directional derivative)"); ax.set_ylabel("central difference")
ax.set_aspect("equal"); ax.grid(alpha=0.4)
show()
Chart

dot_v names the input tangent, dot_out the output tangent — the same dot_<name> convention on every forward-mode kernel.

A terminated sample’s derivative is exactly zero#

A kernel that reads Terminated only as a mask, never its own state from before this launch, can still be differentiated — and a sample the guard skips contributes exactly zero to the derived output, live samples unaffected:

import pathlib, tempfile

from hawk import Terminated


@hawk.kernel
def diagnostic(x: Scalar, terminated: Terminated, y: Mutable[Scalar]):
    y = x + 1.0


diagnostic_jvp = Kernel("diagnostic_jvp", jvp(diagnostic, wrt=("x",)))
diag_dir = pathlib.Path(tempfile.mkdtemp())
hawk.build([diagnostic, diagnostic_jvp], diag_dir, targets=("host",));
x = np.arange(8, dtype=float) + 1.0
terminated = np.zeros(8, dtype=bool)
terminated[4:] = True                 # the second half is already done
dot_x, dot_y = np.full(8, 0.4), np.zeros(8)
diag_jvp_host = hawk.load(diag_dir, "diagnostic_jvp")
hawk.run(diag_jvp_host, terminated=terminated, dot_x=dot_x, dot_y=dot_y)
print("dot_y (live samples keep dy/dx=1, terminated samples are exactly 0):", dot_y)
dot_y (live samples keep dy/dx=1, terminated samples are exactly 0): [0.4 0.4 0.4 0.4 0.  0.  0.  0. ]

What hawk refuses outright#

hawk keeps no tape across launches. A Mutable plane read before it is written in the SAME body — its launch-start value, a running total carried forward from the previous launch — is a recurrence the differentiated launch alone cannot see the whole of, so vjp/jvp refuse a kernel that reads one, by name, rather than silently returning a derivative that is wrong for part of what the kernel computes:

from hawk.ir import HawkError
from hawk.diff import vjp


@hawk.kernel
def running_total(x: Scalar, terminated: Terminated, acc: Mutable[Scalar]):
    acc = acc + x   # reads acc's launch-start value -- a recurrence across launches


try:
    vjp(running_total)
except HawkError as exc:
    print("refused:", exc)
refused: 'acc' is read before it is assigned: a plane's launch-start value is a recurrence across launches; its derivative needs the value the primal launch overwrites, and hawk keeps no tape inside a launch. Across launches, step_jvp (forward) or step_vjp with tape / eagle record=True (reverse) carry it; to differentiate this kernel as it stands, start the plane from zeros (`curve = hawk.math.vec(0.0, ...)`) or write it element by element over every index, and differentiate the per-launch term

What just happened#

  • jvp(primal, wrt=...) pushes one input tangent (dot_<name>) forward into one output tangent (dot_<name>) — the same derivation vjp uses, read the other way.

  • A sample the Terminated guard skips gets an exactly zero derivative (reverse or forward); a live sample’s derivative is unaffected.

  • A kernel that reads a Mutable plane’s launch-start value — a recurrence carried from the previous launch — refuses differentiation outright, naming the plane, rather than returning a derivative that is quietly wrong.

Try this#

Change terminated[4:] = True to terminated[:] = True and re-run: every dot_y is exactly zero.

Next#

Lookups, scatters and sums — read a table, scatter into bins, and reduce a batch — the three plane kinds every tutorial so far has skipped.