Repository navigation
Studio INT8 attention: keep per-card verdicts and races across restarts (no re-probe on every start) - #18156
Conversation
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
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| elif (stored := _stored_sink_decision(key)) is not None: | ||
| plan["sinks"] = _decisions()[key] = stored |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| stored_key = repr(("race", *key[1:])) | ||
| try: | ||
| stored = I8.persisted().get(card, stored_key) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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]
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
… 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.
…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.
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"), filediffusion_int8_attention.jsonnext to the quant probe cache):verdict()(accuracy pass / fail, race milliseconds,min_tokens, the long-variant check);_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, anddiffusion_qwenimage21_attn.pyfor 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:
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=0turns 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.
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
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