Skip to content

Studio INT8 attention: keep per-card verdicts and races across restarts (no re-probe on every start) - #18156

Merged
danielhanchen merged 6 commits into
mainfrom
studio-int8-attention-persisted-verdicts
Nov 17, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-int8-attention-persisted-verdicts

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Studio decides per card whether the INT8 attention kernel is accurate and faster than the incumbent: the accuracy check and card race, then the long / 2048-token races. Today those verdicts live in in-process dicts, so every Studio start runs them again. This PR stores them per machine, so a later process reuses them and loads the kernels eagerly without probing or timing.

What is persisted (new diffusion_probe_cache.Namespace("int8_attention"), file diffusion_int8_attention.json next to the quant probe cache):

  • the card verdict from verdict() (accuracy pass / fail, race milliseconds, min_tokens, the long-variant check);
  • the long / 2048-token race results from the dispatch route (_race_wins).

The entry key is the card (name, UUID, capability, driver), torch / torch CUDA / Triton / torchao versions, the cuDNN, diffusers and NVRTC versions, every UNSLOTH_DIFFUSION_* / UNSLOTH_INT8* / TORCHINDUCTOR_* switch, and a hash of the kernel and race sources (diffusion_int8_attention.py, _cuda.py, _triton.py, _dispatch.py, and diffusion_qwenimage21_attn.py for the strided cuDNN incumbent). Inside an entry a verdict is keyed by dtype, heads, head dim, incumbent backend (a strided cuDNN pin apart from plain cuDNN), timed / untimed and implementation; a long / mid race by dtype, heads, head dim, incumbent, token count and the implementation that served it (a CUDA kernel that failed at this start falls back to Triton under the same fingerprint, so neither timing answers for the other).

Not persisted:

  • the attention-sink decisions. That race also gates the plain kernels on their error against torch on the model's own inputs, and its bucket key cannot tell checkpoints or LoRA sets of the same geometry apart, so it stays per load as before;
  • a check that raised (a failure may be transient), a timed verdict missing a race size, an out-of-memory race loss, and anything when a key component cannot be read.

A stored passing verdict still loads and launches its kernels once before it is used; if they no longer run, the check runs again. UNSLOTH_DIFFUSION_PROBE_CACHE=0 turns persistence off.

Results

Colab, fresh process per arm on one VM, in the order main, head, main, head, main, head. Main and head each keep one Studio home per model, so arm 1 is cold (no persisted verdicts, no NVRTC images, no compile cache) and arms 2 and 3 are warm. Default Studio settings with INT8 attention on auto, 1024x1024, 3 images per process (image 0 = first image). "Saved per start" is the wall time a warm head process no longer spends in the accuracy check and the card / long / mid races, measured per main process; the sink race (last column) still runs once per load.

Card Model Saved per start Load + first image, main warm (2 runs) Same, head warm (2 runs) Steady s/img main Steady s/img head Sink race per load (unchanged)
T4 Z-Image-Turbo 1.14 / 1.15 s 193.8 / 164.8 s 150.5 / 178.2 s 23.69 / 23.84 23.70 / 23.92 1.02 s
T4 FLUX.2 klein 4B 0.80 / 0.80 s 58.8 / 56.5 s 58.1 / 58.0 s 7.18 / 7.16 7.07 / 7.09 0.98 s
A100 80GB Z-Image-Turbo 3.81 / 3.73 s 34.37 / 34.41 s 30.56 / 30.82 s 1.815 / 1.817 1.818 / 1.815 none
A100 80GB FLUX.2 klein 4B 0.79 / 0.84 s 26.01 / 25.75 s 25.34 / 25.18 s 0.642 / 0.642 0.644 / 0.642 0.14 s
A100 40GB Z-Image-Turbo 3.73 / 3.77 s 30.32 / 29.75 s 26.75 / 26.59 s 1.816 / 1.817 1.819 / 1.817 none
A100 40GB FLUX.2 klein 4B 0.77 / 0.78 s 21.32 / 21.36 s 20.64 / 20.80 s 0.640 / 0.640 0.641 / 0.641 0.13 s
RTX PRO 6000 (G4) Z-Image-Turbo 0.35 / 0.34 s 16.32 / 16.44 s 16.08 / 16.30 s 1.003 / 1.003 1.002 / 1.003 0.08 s
RTX PRO 6000 (G4) FLUX.2 klein 4B 0.35 / 0.35 s 13.63 / 13.74 s 13.55 / 13.54 s 0.357 / 0.357 0.357 / 0.357 0.08 s
  • Every warm head process ran 0 accuracy checks and 0 card / long / mid races and adopted the stored verdict (1 adoption per process); the cold head process ran the same check and races as main.
  • On A100, Z-Image-Turbo loses the race, so INT8 attention is not used there, yet main paid the check plus the card and long races (about 3.7 s) on every start. Head pays it once: load + first image drops by 3.2 to 3.8 s against an A/A spread of 0.04 to 0.6 s.
  • Where the saving is under a second, or the T4 load time swings by 30 s from disk and host RAM, the end-to-end first-image difference is within the A/A noise; the measured check + race time is what is saved.
  • The head columns were measured on a revision that also persisted the sink decisions, so they skipped the sink race too; with this PR a warm start pays it again (last column), as main does.
  • Images: on every card, every image of every process (cold, warm, main, head) is bit-identical to main's first process (equal pixels, LPIPS 0).
  • Steady s/img is unchanged within the main A/A spread.
  • Stale and corrupted entries: with a key component changed (an extra UNSLOTH_DIFFUSION_* switch) the next process ran the check and races again; with the stored entries overwritten by garbage the next process ran them again and rewrote the entries, and the process after that reused them. Checked on T4, A100 and G4 with Z-Image-Turbo.

Tests

  • New studio/backend/tests/test_diffusion_int8_attention_persisted.py (20 tests, CPU, fake card): reuse across processes without probing, kernels warmed on adoption, key separation (dtype, geometry, incumbent, timed, strided cuDNN incumbent), toolchain / card / cuDNN / diffusers / NVRTC change re-checks, an unreadable key component is never stored, exceptions and partial timings never stored, failed accuracy checks stored, kernels that no longer load re-check, long-variant check restored, the off switch, long / mid races persisted but not out-of-memory losses, long / mid races keyed by the implementation that served them, sink decisions raced again for every load, torn file, corrupted entries, entries under another fingerprint.
  • test_diffusion_int8_attention*.py, test_diffusion_moss_*.py, test_diffusion_qwenimage21_strided_attn.py, test_media_auto_gpu_probe_caches.py (-o addopts=): 470 passed; the only failure is the same as on main (test_diffusion_qwenimage21_strided_attn.py::test_real_processor_decode_call_takes_the_strided_path, RecursionError).

Not covered

  • L4: both attempts lost the Colab connection before the job started, so L4 has no numbers (T4, A100 and G4 do).
  • Qwen-Image-2.1 (strided cuDNN incumbent), MOSS-Image, FLUX.1, HunyuanVideo-1.5 and Wan were not run on a GPU; their keys are covered by the CPU tests.
  • Multi-GPU (secondary-card verdicts), ROCm and Windows were not run.
  • Several Studio processes writing the file at the same moment: writes are atomic replaces that merge the current file, but this was not stress-tested.

The card verdict (accuracy check and race), the long / mid race floors and the sink decisions persist per card,
toolchain and kernel sources through a new diffusion_probe_cache.Namespace; a later process reuses them, loading
the kernels eagerly without probing or timing. Failures, out-of-memory losses and verdicts with an exception are
never stored. UNSLOTH_DIFFUSION_PROBE_CACHE=0 turns it off.
…bent, cuDNN, diffusers and NVRTC

A strided cuDNN pin (Qwen-Image-2.1) races on the strided views rather than diffusers' copies, so its verdict and
long / mid races are keyed apart from the plain cuDNN incumbent. The namespace key also carries the cuDNN, diffusers
and NVRTC versions and the strided incumbent's source; an unreadable component is never persisted.
…-persisted-verdicts

# Conflicts:
#	studio/backend/core/inference/diffusion_int8_attention_dispatch.py
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T14:36:44.000301Z ad6eb1c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a9c69c473

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +919 to +920
elif (stored := _stored_sink_decision(key)) is not None:
plan["sinks"] = _decisions()[key] = stored

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Scope persisted sink decisions to the pipeline being checked

When a different checkpoint or LoRA uses the same attention geometry, this reuses the previous pipeline's sink decision without checking the new inputs. The decision key contains configuration, geometry, dtype, scale, mode and a Boolean indicating whether sinks were seen, but no model identity. Unlike the synthetic card verdict, _sink_race also checks input-dependent accuracy: _pick_path permits the plain kernel only when its error is within PLAIN_ERROR_SLACK of the sink kernel's error. A cached False therefore bypasses that accuracy guard for a new model, potentially changing generated results. apply_attention_backend previously cleared these decisions through forget_plans() on each pipeline load. Include pipeline/weights identity in the persisted key, or revalidate the accuracy-dependent choice on the current inputs before adopting it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in ad6eb1c. Sink decisions are no longer persisted: they stay per load, as on main. No available identity is sound for this key: load_identity has no revision or weight fingerprint, and adapters can change later in the same pipeline. A warm start now runs the sink race again (0.08 to 1.0 s). Test: test_a_sink_decision_is_raced_again_for_every_load.

Comment on lines +547 to +549
stored_key = repr(("race", *key[1:]))
try:
stored = I8.persisted().get(card, stored_key)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include the serving implementation in persisted race keys

When a CUDA compile, module load or launch fails transiently, verdict() can fall back to Triton without changing the card/toolchain fingerprint. Long and mid races then measure Triton, but this persisted key does not identify the implementation. On a later startup where CUDA succeeds, _card_floor() checks CUDA's own verdict and then can reuse Triton's long/mid race results here (or vice versa), selecting a token floor based on a different kernel's performance. Include the implementation that actually served the race in the persisted key so a fallback's win or loss cannot control routing after recovery.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in ad6eb1c. _race_wins reads the implementation serving the race (CUDA or the Triton fallback) before it times, and adds it to both the in-process and the persisted key. If that read fails, the race is timed but never stored. Test: test_a_long_race_is_keyed_by_the_implementation_that_served_it.

…ces keyed by the serving kernel

The sink race also gates the plain kernels on their error against torch on the model's own inputs, and its bucket
key cannot tell checkpoints or LoRA sets of the same geometry apart, so its decisions stay per load as before.
The long / mid races are keyed by the implementation that served them: a CUDA kernel that failed this start falls
back to Triton under the same fingerprint, and neither timing answers for the other.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: ad6eb1c65c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 19c1bce into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the studio-int8-attention-persisted-verdicts branch November 17, 2026 15:21
danielhanchen added a commit that referenced this pull request Nov 20, 2026
… GPU driver (#18574)

#18156 made INT8 attention verdicts persist across processes, keyed by
the shared probe-cache fingerprint (torch, CUDA, driver, card). The
quant probe verdicts use that same fingerprint. The driver part comes
from `torch._C._cuda_getDriverVersion`, and no current torch build has
that function (2.12.1+cu130, 2.13.0, 2.14.1+cu130 checked). So the
fingerprint always records `driver=None`. After a driver update, a
passing persisted INT8 attention verdict is still adopted with only a
launch warm-up, a negative one skips even that, and the quant verdicts
are reused the same way.

## Change

`fingerprint()` now reads the driver itself, once per process and
without creating a CUDA context:

- NVIDIA: the kernel module version from `/proc/driver/nvidia/version`,
or if that file is missing (WSL, some containers, Windows) the name,
size and mtime of the driver library this process loaded (`libcuda.so*`
from `/proc/self/maps`, or `System32\nvcuda.dll`). `cuDriverGetVersion`
is added when it answers.
- ROCm: the existing `_rocm_driver_version()` from `diffusion_attention`
(HIP driver version, plus the amdgpu module version or kernel release).

If no driver identity can be read, `fingerprint()` returns None. Then
nothing is stored or adopted and the probes run fresh, as they did
before persistence. Every namespace built on `fingerprint()` gets this
fix. Existing cache entries were keyed with `driver=None`, so each one
misses once and is probed again on the first start after the update.

## Tests

`tests/test_diffusion_probe_cache_driver.py` (CPU only; the card, the
proc file and the driver library are faked):

- a driver update changes the fingerprint key
- a verdict stored under one driver is not adopted under another, for
both `Namespace` and `load`/`store`
- an unreadable driver stores and admits nothing
- the same driver still reuses the verdict
- ROCm keys on the HIP driver identity and refuses one it could not read

On main, 4 of the 5 fail. The same-driver reuse case passes on both, as
it should. With this change, all 5 pass. The existing probe-cache users
(`test_diffusion_int8_attention_persisted.py`,
`test_diffusion_transformer_quant.py`, `test_early_quant_probe.py`,
`test_diffusion_probe_overlap.py`, `test_diffusion_cold_start.py`,
`test_diffusion_early_probe_prewarm.py`,
`test_diffusion_compat_preflight.py`,
`test_diffusion_rocm_tag_detect.py` and one more block test) fail the
same 36 test IDs on main and with this change. The 35 in
`test_diffusion_compat_preflight.py` come from `gguf` not being
installed in the test venv; the remaining one, in
`test_diffusion_cold_start.py`, fails identically on main. There are no
new failures.

## Not proven

- Not run on a real GPU. The NVIDIA reader was checked by hand on a
Linux host (`cuda13010,nvrm590.48.01`). The WSL, Windows and ROCm
readers were exercised only through fakes.
- We have not measured whether a particular driver update actually
changes INT8 attention numerics. This change only makes sure such an
update is noticed.
- The library-identity fallback keys on file metadata, so reinstalling
the same driver also causes one re-probe.
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…e first render (#18532)

## Summary

On the INT8 attention sink route (FLUX.3, FLUX.2, Z-Image,
Qwen-Image-2.1, HunyuanVideo-1.5), a launch plan runs the sink path
while it learns over its first 32 calls whether any head clipped V (an
attention sink), and races at its 48th call. When a head clipped and the
race picked the kernels without the sink path (Qwen-Image-2.1 on L4 and
A100), or torch's attention, every call of the next render ran that
winner. So the first render of a load was a different image from the
next render of the same seed.

On main this only shows when no thrown-away CUDA graph warm-ups use up
those 48 calls first: CUDA graphs off, a LoRA scale in
`attention_kwargs`, or a prefix K/V over 1 GiB. Qwen-Image-2.1 runs 31
INT8 attention calls per step, so its decision landed inside step 2.

## Change

The race, its call, its inputs and its scope stay as they were: a sink
race verdict is per load, never persisted, so every load races on its
own inputs (#18156).

- Every undecided call before the decision (not only the first 32)
records its clipped-key counts, and whether its output was used. A call
inside a capture's side-stream warm-ups is thrown away; anything else is
used.
- At the decision, a verdict other than the sink path is taken only
where no used call clipped on the sink path. Otherwise the plan keeps
the sink path for the load, which is what its earlier calls computed, so
the next render repeats the seed. The bucket still keeps the race
verdict for later plans of the same load.
- A torch attention verdict does not move routes (floor raise or stock
processor) that another plan of the load already ran used INT8 calls on.
- Main's default path (CUDA graphs on, side-stream warm-ups) is
unchanged by these rules: its learning calls are thrown away.

## Evidence

Colab L4, torch 2.13.0+cu130, Qwen-Image-2.1 INT8 (Auto), 1024 px, 8
steps, CUDA graphs off, the same prompt and seed twice per process, one
VM:

| | load 1: render 1 | load 1: render 2 | load 2 (new process, same
Studio home): render 1 | render 2 |
|---|---|---|---|---|
| main | `fede6637` | `b80aa53a` | | |
| this branch | `b2d547c3` | `b2d547c3` | `b2d547c3` | `b2d547c3` |

- The second load raced again: its plan carries its own race result, and
the Studio home's caches hold no sink verdict entry after either load.
- Second-render s/step: main 1.352, this branch 1.366 and 1.367 (one
sample per arm). Peak allocation is unchanged on the first load (17681
MiB in both arms).
- With CUDA graphs on (the default), the stacked warm-up PR's tree
(which contains this change) renders FLUX.2 klein byte-identical to main
(`6aff2096`) in all 8 processes on the same VM.

- B200, correctness only (earlier revision with the same rule): a toy
8-block DiT on the real INT8 attention kernels with V sinks on the first
tokens, eager and `torch.compile`d, 8 steps, two renders of the same
inputs. Main with graphs off differs between render 1 and render 2 for
every verdict; this rule gives `torch.equal` on every step. With every
call treated as a thrown-away warm-up (main's rule) the mismatch comes
back. No new dynamo graphs, breaks or recompiles.
- CPU (`-o addopts=`): the INT8 attention, CUDA graph and probe cache
suites, 595 passed on main and 607 here, no failures on either.
`test_differential.py` against the merge base: DISCRIMINATING, 12 fixed,
0 head failures. New tests cover the kept sink path after a used clip, a
bucket verdict of the same load after a used clip, thrown-away clips
taking the verdict as before, the route guard, the used/thrown-away
mark, every undecided call being checked, and a load that kept the sink
path persisting nothing (the next load and process race again).

## Not covered

- HunyuanVideo-1.5's known first-render difference may be the same
mechanism; it was not run.
- Only L4 and B200 ran this rule; A100, T4, G4 and Windows did not.
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.

1 participant