Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ likelihoods XLA compilation is a first-class cost in its own right —
`scripts/misc/jax_compile/` measures trace / compile / first-call / steady-state
separately per likelihood × transform. Standing conclusions:

- **Settings suffice** — the persistent compilation cache and `--xla_gpu_autotune_level=0`
(both shipped as autonerves defaults) take the worst measured first fit from
~70 min to ~35 s. Never restructure a likelihood or sampler for compile time
- **Settings suffice** — the persistent compilation cache, `--xla_gpu_autotune_level=0`
and `--xla_gpu_enable_triton_gemm=false` (all shipped as autonerves defaults) take the
worst measured first fit from ~70 min to ~35 s; the Triton flag is there because at
autotune level 0 XLA's default Triton tile makes dense fp64 GEMMs ~5× slower than
cuBLAS ([`results/notes/xla_autotune_triton_gemm.md`](./results/notes/xla_autotune_triton_gemm.md)).
Never restructure a likelihood or sampler for compile time
([`scripts/misc/jax_compile/README.md`](./scripts/misc/jax_compile/README.md)).
- **`af.MultiStartProdigy` compile is a non-problem** on MGE and every pixelized
mesh (rectangular / KNN / Delaunay) — ≤ 75 s cold, ≤ 2 s warm on a 32-core
Expand Down
116 changes: 116 additions & 0 deletions hpc/batch_gpu/submit_xla_autotune_gemm_probe
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash -l
#
# XLA GPU autotuning / Triton-GEMM probe (autolens_profiling, 2026-09-08).
#
# Five arms of one dense fp64 GEMM — the (15361 x 1560) mapping-matrix shape of
# the HST / Delaunay-1500 imaging cell's Curvature matrix (F) step — run in one
# job on one node, to separate the effect of --xla_gpu_autotune_level from the
# effect of the per-fusion autotune cache JAX persists under
# JAX_COMPILATION_CACHE_DIR.
#
# A level 0, fresh cache -- the shipped default on a virgin node
# B level 4, fresh cache -- XLA's default; writes autotune entries
# C level 0, reading B's cache -- the trap: level 0 gets B's kernel
# D level 0, reading its own -- level 0 stays slow when nothing seeded it
# E level 0 + triton_gemm=false -- the cure; cuBLAS without autotuning
#
# Each arm prints a RESULT line (compile_s / first_ms / steady_mean_ms), then
# the arm's cache listing and its autotune-entry count (AUTOTUNE_ENTRIES), and
# the job ends with an HLO pass reporting whether each arm's dot lowered to a
# Triton __triton_nested_gemm_fusion or to the __cublas$lt$matmul custom call.
#
# Findings: results/notes/xla_autotune_triton_gemm.md (job 342333).

#SBATCH -J xla_autotune_gemm_probe
#SBATCH --partition=gpu
#SBATCH --gres=gpu:1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem=64gb
#SBATCH --time=00:30:00
#SBATCH -o output/output.%A.out
#SBATCH -e error/error.%A.err

# Derive the repo root from where this was submitted (hpc/batch_gpu/../..)
# rather than hardcoding the RAL checkout path, so this runs from a worktree.
export AP_ROOT="$(cd "${SLURM_SUBMIT_DIR:-$(dirname "$0")}/../.." && pwd)"
echo "AP_ROOT=$AP_ROOT"
source $AP_ROOT/activate.sh

export JAX_PLATFORM_NAME=cuda
export JAX_PLATFORMS=cuda,cpu
export XLA_PYTHON_CLIENT_PREALLOCATE=false
export JAX_ENABLE_X64=True
export NUMBA_CACHE_DIR=/tmp/numba_cache
export MPLCONFIGDIR=/tmp/matplotlib

# Per-arm scratch: compilation caches and HLO dumps. Wiped at the start of every
# job — arms A, B and E must each begin from an empty cache or the A/B is void.
S=$AP_ROOT/output/xla_autotune_probe
mkdir -p $S

echo "=== node ==="
hostname
date
nvidia-smi
echo "python3: $(which python3)"
echo "scratch: $S"

rm -rf $S/cache_A $S/cache_B $S/cache_E
rm -rf $S/hlo_A $S/hlo_B $S/hlo_C $S/hlo_D $S/hlo_E
mkdir -p $S/cache_A $S/cache_B $S/cache_E
mkdir -p $S/hlo_A $S/hlo_B $S/hlo_C $S/hlo_D $S/hlo_E

BASE="--xla_disable_hlo_passes=constant_folding"

run_arm () {
ARM=$1
LEVELFLAGS=$2
CACHE=$3
echo ""
echo "############################################################"
echo "### ARM $ARM flags=[$LEVELFLAGS] cache=$CACHE"
echo "############################################################"
export XLA_FLAGS="$BASE $LEVELFLAGS --xla_dump_to=$S/hlo_$ARM --xla_dump_hlo_as_text"
export JAX_COMPILATION_CACHE_DIR=$CACHE
python3 -u $AP_ROOT/scripts/misc/jax_compile/gemm_probe.py $ARM || echo "ARM $ARM FAILED"
echo "--- cache listing for $CACHE ---"
ls -R $CACHE 2>/dev/null | head -20
echo "--- autotune cache entry count ---"
echo -n "AUTOTUNE_ENTRIES arm=$ARM count="
ls $CACHE/xla_gpu_per_fusion_autotune_cache_dir 2>/dev/null | wc -l
}

run_arm A "--xla_gpu_autotune_level=0" $S/cache_A
run_arm B "--xla_gpu_autotune_level=4" $S/cache_B
run_arm C "--xla_gpu_autotune_level=0" $S/cache_B
run_arm D "--xla_gpu_autotune_level=0" $S/cache_A
run_arm E "--xla_gpu_autotune_level=0 --xla_gpu_enable_triton_gemm=false" $S/cache_E

echo ""
echo "############################################################"
echo "### HLO lowering analysis"
echo "############################################################"
for ARM in A B C D E; do
D=$S/hlo_$ARM
echo "--- arm $ARM ($D) ---"
ls $D 2>/dev/null | head -20
CUB=$(cd $D 2>/dev/null && grep -l -E 'custom_call_target="__cublas\$gemm"|__cublas\$lt\$matmul' *after_optimizations*.txt 2>/dev/null | head)
TRI=$(cd $D 2>/dev/null && grep -l -E 'triton_gemm|__triton' *after_optimizations*.txt 2>/dev/null | head)
echo "LOWERING arm=$ARM cublas_files=[$CUB] triton_files=[$TRI]"
if [ -n "$CUB" ] && [ -z "$TRI" ]; then echo "LOWERING_VERDICT arm=$ARM cuBLAS"
elif [ -n "$TRI" ] && [ -z "$CUB" ]; then echo "LOWERING_VERDICT arm=$ARM Triton"
elif [ -n "$TRI" ] && [ -n "$CUB" ]; then echo "LOWERING_VERDICT arm=$ARM both"
else echo "LOWERING_VERDICT arm=$ARM unknown"; fi
echo "--- dot lines arm=$ARM ---"
for f in $D/*after_optimizations*.txt; do
[ -e "$f" ] || continue
echo " file: $(basename $f)"
grep -E 'custom-call|kind=kCustom' "$f" 2>/dev/null | head -5
done
done

echo ""
echo "=== RESULT LINES ==="
date
echo "Finished."
15 changes: 15 additions & 0 deletions results/notes/delaunay_nn_breakdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,21 @@ program alone, +1.59 s across all breakdown compiles and +8.75 s across the runt
cell's lower/compile/first-call phases — so the flag pays for itself after ~68 calls of
the F step and ~453 batched likelihood evaluations respectively.

### Addendum (2026-09-08)

The pair above is real, but the level-0 rows recorded after it — 342315, 342316, 342321
and 342322, all measuring F at 4.80–4.83 ms with autotuning still off — do **not**
contradict it. They were reading a per-fusion autotune cache that jobs 342282/342283
seeded on `euclid-ral-gpu-2`: JAX persists XLA's autotune cache under
`JAX_COMPILATION_CACHE_DIR` keyed by fusion fingerprint rather than by the autotune
flag, and RAL `$HOME` is node-local. The controlled fresh-cache-per-arm probe (job
342333, 2026-09-08) reproduces the pair on a bare GEMM — 25.52 ms at level 0 against
4.92 ms at level 4 — and shows the level-0 arm lowering to an un-autotuned Triton
fusion. The cure is not to re-enable autotuning but to add
`--xla_gpu_enable_triton_gemm=false`, which reaches cuBLAS (4.79 ms) with no autotune
compile cost; adopted in PyAutoNerves#161. Full write-up:
[`xla_autotune_triton_gemm.md`](./xla_autotune_triton_gemm.md).

## See also

- `results/notes/preopt_breakdown_baseline.md` — the 2026-07-10 A100 tier this note
Expand Down
127 changes: 127 additions & 0 deletions results/notes/xla_autotune_triton_gemm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# XLA autotuning, Triton GEMM and the persistent autotune cache (A100, 2026-09-08)

PyAutoNerves issue #161, task `xla-triton-gemm-off`. Controlled probe:
`scripts/misc/jax_compile/gemm_probe.py` via
`hpc/batch_gpu/submit_xla_autotune_gemm_probe`.

## What was measured and why

The 2026-09-05 A/B in [`delaunay_nn_breakdown.md`](./delaunay_nn_breakdown.md)
("XLA GPU autotuning A/B") measured the Curvature matrix (F) step at **25.63 ms**
with the shipped `--xla_gpu_autotune_level=0` against **4.90 ms** at XLA's default
level 4 — a 5.2× penalty on one dense fp64 GEMM. But four later jobs (342315,
342316, 342321, 342322), all with autotuning still at level 0, measured F at
**4.80–4.83 ms** with `curvature_matrix_jit_compile` at 0.066 s. Both cannot be
explained by the flag alone, so the flag was not the whole story. This note is the
controlled experiment that separates the flag from the state it leaves behind.

## Job

| Job | Node | GPU | Start (2026-09-08) | Elapsed | State |
|---|---|---|---|---:|---|
| 342333 | `euclid-ral-gpu-2` | NVIDIA A100 80GB PCIe | 13:32:26 | 0:14 | COMPLETED |

`jax 0.10.2`, `jax_persistent_cache_min_compile_time_secs=0`,
`jax_persistent_cache_enable_xla_caches = xla_gpu_per_fusion_autotune_cache_dir`. The
timed program is `F = (M*w[:,None]).T @ (M*w[:,None])` with `M` a (15361, 1560) float64
array from `default_rng(0)` — the masked-pixel × mesh-vertex mapping-matrix shape of the
HST / Delaunay-1500 cell, likelihood stripped away. All five arms in one job, one node.

## Arms

Every arm carries `--xla_disable_hlo_passes=constant_folding` plus the HLO dump
flags; the arms differ only in the autotune/Triton flags and in which
`JAX_COMPILATION_CACHE_DIR` they are pointed at. **A** is level 0 on a fresh cache
(the shipped default on a node nothing has seeded); **B** is level 4 on a fresh
cache (XLA's own default); **C** is level 0 pointed at the cache **B** just wrote;
**D** is level 0 pointed at its own cache **A** wrote; **E** is level 0 plus
`--xla_gpu_enable_triton_gemm=false` on a fresh cache.

## Results

| arm | flags beyond the base | cache | compile s | first ms | steady mean ms | steady min ms | autotune entries after | lowering |
|---|---|---|---:|---:|---:|---:|---:|---|
| A | level 0 | fresh `cache_A` | 0.461 | 25.99 | 25.523 | 25.338 | 0 | Triton `__triton_nested_gemm_fusion`, num_warps=2, output_tiles [16,8], num_stages=1 |
| B | level 4 | fresh `cache_B` | 2.080 | 5.50 | 4.918 | 4.815 | 3 | cuBLASLt `__cublas$lt$matmul`, selected_algorithm 1 |
| C | level 0 | `cache_B` (seeded by B) | 0.072 | 43.8 | 4.832 | 4.711 | 3 | cuBLASLt algorithm 1 |
| D | level 0 | `cache_A` (own) | 0.005 | 25.76 | 25.510 | 25.326 | 0 | Triton (executable-cache hit) |
| E | level 0 + `--xla_gpu_enable_triton_gemm=false` | fresh `cache_E` | 0.074 | 43.4 | 4.789 | 4.696 | 0 | cuBLASLt, `autotune_workspace_size` 0 |

## HLO lowering

Arm A (and D) lower the dot to a Triton fusion carrying XLA's *default* tile
configuration — never measured, because nothing autotuned it:

```
%gemm_fusion_dot_general.1 = f64[1560,1560]{1,0} fusion(...), kind=kCustom,
backend_config={..."fusion_backend_config":{"kind":"__triton_nested_gemm_fusion",
"block_level_fusion_config":{"num_warps":"2","output_tiles":[{"sizes":["16","8"]}],
"num_ctas":1,"num_stages":1,...}}...}
```

Arms B, C and E lower it to the cuBLASLt custom call instead:

```
%custom-call.1 = (f64[1560,1560]{1,0}, s8[4194304]{0}) custom-call(...),
custom_call_target="__cublas$lt$matmul",
backend_config={..."gemm_backend_config":{"selected_algorithm":"1","alpha_real":1,
"beta":0,...,"autotune_workspace_size":"4194304",...}...}
```

In arm E the same call carries `"autotune_workspace_size":"0"` and no
`selected_algorithm` — cuBLAS is reached without the autotuner running at all.

## Mechanism

JAX persists XLA's **per-fusion autotune cache** under `JAX_COMPILATION_CACHE_DIR` by
default, in `xla_gpu_per_fusion_autotune_cache_dir/`, keyed by fusion fingerprint —
*not* by the autotune flag. The flag **is** part of the executable-cache key, so arm C
was a genuine recompile (0.072 s, no executable hit) that nevertheless read arm B's
autotune entry and emitted B's cuBLAS kernel. Arm D hit the executable cache outright
(0.005 s) and stayed on Triton.

RAL `$HOME` — hence the default cache root `~/.cache/pyauto_jax` — is **node-local**. The
2026-09-05 level-4 jobs 342282 and 342283 therefore seeded `euclid-ral-gpu-2`, and every
later level-0 run on that node (342315, 342316, 342321, 342322) read that seeded entry:
F 4.80–4.83 ms, `curvature_matrix_jit_compile` 0.066 s. The two 2026-09-05 level-0 rows
(342277 on `gpu-1`, 342278 on `gpu-2`) predate any level-4 run on their nodes, which is
why they measured 25.6 ms. A level-0 run never writes autotune entries itself, so a
fresh node — or a new fusion shape on a seeded node — always pays the un-tuned Triton
default.

## Numerics

`F[0,0]` is `7685.1171726898065` in arms A, D and E (bit-identical) and
`7685.1171726897901` in arms B and C — about 2 ULP apart. The cure (arm E)
reproduces the Triton arms bit-for-bit, so adopting it changes no pinned value; it
is the autotuned cuBLAS kernel that sits 2 ULP away.

## Consequences

- **The A/B trap.** A valid autotune A/B needs a **fresh `JAX_COMPILATION_CACHE_DIR` per
arm**, or a node no level-4 run has ever touched; otherwise the level-0 arm quietly
inherits the level-4 arm's kernel and the comparison collapses — exactly what happened
to the four post-09-05 `gpu-2` rows.
- **Read the F compile time.** ~2 s means the autotuner ran; ~0.4 s means a real compile
without it (and so the slow Triton default); ~0.07 s or less means a cache hit and
tells you nothing about which kernel is underneath — check the HLO.
- **Dashboard rows recorded 2026-07-17 → 2026-09-08** (the window in which
`--xla_gpu_autotune_level=0` shipped without the Triton flag) are slow on GEMM-bound
steps *unless* the node's cache happened to be seeded. The `_autotune4` rows, and the
post-09-05 `gpu-2` rows, are the fast reference.

## Cure

Adopted in PyAutoNerves#161: keep `--xla_gpu_autotune_level=0` and also append
`--xla_gpu_enable_triton_gemm=false` by default, so dense fp64 GEMMs reach cuBLAS
without paying autotune compile time (an explicit autotune level or an explicit
Triton flag in `XLA_FLAGS` is still respected).

## Files

- `scripts/misc/jax_compile/gemm_probe.py` — the probe.
- `hpc/batch_gpu/submit_xla_autotune_gemm_probe` — the five-arm submit script.
- `results/notes/xla_autotune_triton_gemm.md` — this note.

The raw job output lives on RAL under `/mnt/ral/jnightin/scratch_xla_autotune/` and is
not committed.
13 changes: 13 additions & 0 deletions scripts/misc/jax_compile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,19 @@ every point in the lifecycle. Any further reduction would come from upstream
JAX (tracing speed, compile speed), not from this stack — no further
engineering is warranted here. The compile-time arc (#71 → #74 → #77) is done.

**Amendment (2026-09-08) — autotune-off stays, but it needed a companion flag.**
Autotuning off remains the right compile-time call; finding 7's "no measurable
evaluation penalty", however, was measured on cells whose GEMMs were not the
bottleneck, or on a node whose per-fusion autotune cache a level-4 run had
already seeded (JAX persists that cache under `JAX_COMPILATION_CACHE_DIR`
keyed by fusion fingerprint, not by the autotune flag). The controlled
fresh-cache probe of 2026-09-08 (job 342333) found the un-autotuned Triton
kernel **5× slower than cuBLAS on a dense fp64 GEMM** — 25.52 vs 4.79 ms on the
(15361×1560) mapping-matrix shape — cured by additionally passing
`--xla_gpu_enable_triton_gemm=false`, which reaches cuBLAS with no autotune
compile cost at all (PyAutoNerves#161). Details, arms and HLO evidence:
[`results/notes/xla_autotune_triton_gemm.md`](../../../results/notes/xla_autotune_triton_gemm.md).

## Multi-band `FactorGraphModel` `value_and_grad` — the heterogeneous-shape cliff

The #71 → #77 census measured **single-band** cells only. Real multi-wavelength
Expand Down
Loading
Loading