Skip to content

Studio INT8 attention: verify the long-sequence kernel at install, fall back to the short one - #17796

Merged
danielhanchen merged 4 commits into
mainfrom
studio-int8-attention-long-config-probe
Nov 15, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-int8-attention-long-config-probe

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #17769. Some INT8 attention kernel configs carry a long_keys / long override (sm80 hd128 and sm89 hd64 switch to wpc=8 from 4608 keys; ROCm gfx1151 hd128 from 8000). select_config turns that into a second compiled kernel, but the install-time check never reached it: the accuracy probe uses about 1000 tokens and the race uses RACE_TOKENS = (4096, 1024). Two problems followed:

  1. The long kernel was compiled on the first real call with at least 4608 keys (FLUX at 1024x1024 is exactly 4608). That call could be under CUDA graph capture, and that kernel never passed the cosine check that sets _VERIFIED.
  2. If that compile or launch failed, the except in int8_attention_impl added (index, head_dim) to _BROKEN. That also turned off the verified short kernel for the rest of the process.

Changes

studio/backend/core/inference/diffusion_int8_attention.py:

  • _long_check: when the card's config has long_keys, the install check (_check, so also verdict) compiles and checks the long variant once per (card, head dim, dtype, impl). It runs eagerly, outside any capture, on the cheapest probe that selects it: batch 1, 2 heads, 1000 queries against long_keys + 100 keys (the last key tile is ragged). The probe uses the same plain and sharp accuracy bars as the short check. A pass goes into _LONG_VERIFIED. The result shows up as long in the verdict and in status()["verdicts"], and the install log line gets long_ok=....
  • _run: when no explicit config is passed, a call long enough for the long variant runs it only if that variant was verified (and has not failed since). Otherwise the call runs the short config. The short config is a valid kernel at any length, just slower on long calls, and the output is the same either way: the block shape does not change any output bit (test_long_overrides_do_not_change_the_output). An explicit config= is still run exactly as given.
  • int8_attention_impl: a runtime failure in a call that ran the long variant now adds (index, head_dim, impl) to _LONG_BROKEN. That call runs native attention, and later long calls go back to the short kernel. A failure in a short-kernel call still marks (index, head_dim) broken, as before.
  • If the long variant fails its check or fails to load, the card keeps INT8 with the short kernel for every call. ok is unchanged.
  • Configs without long_keys behave exactly as before: no extra probe, no extra compile, same call path.

Tests

New studio/backend/tests/test_diffusion_int8_attention_long_probe.py (CPU, a faked sm89 card with the compile / launch mocked):

test main this PR
the card check compiles and verifies the long variant FAIL pass
a long variant that fails its check leaves long calls on the short kernel FAIL (the long call raises, so the card goes native: kernel failed, native attention from here on) pass
a long variant that misses the accuracy bar is not used FAIL pass
a long call that fails at runtime disables only the long variant FAIL pass
a short kernel failure still turns the card off pass pass
a config without long_keys is checked and run as before pass pass
an explicit config is run as given pass pass

The existing INT8 suites (test_diffusion_int8_attention.py, _dispatch, _triton_fallback, _rocm) pass on CPU on both trees: 126 passed, 93 skipped on this branch; main fails only the 4 new tests.

GPU evidence (Colab L4 sm89 and A100 sm80, torch 2.14.1+cu130, main at f490069 vs this branch)

INT8 GPU suites (test_diffusion_int8_attention.py, _dispatch, _triton_fallback, and the new file) on each card: 197 passed, 6 skipped on this branch. Main fails only the 4 new tests (193 passed). The 6 skips need two CUDA cards.

The long variant is verified at install, as recorded in the verdict:

  • L4 hd64: {'ok': True, 'keys': 4708, 'cos': 0.99996, 'rel_l1': 0.0077, 'sharp_cos': 0.9971}, _LONG_VERIFIED = [(0, 64, bf16, cuda)]
  • A100 hd128: {'ok': True, 'keys': 4708, 'cos': 0.99996, 'rel_l1': 0.0081, 'sharp_cos': 0.9970}, _LONG_VERIFIED = [(0, 128, bf16, cuda)]
  • L4 hd128 and A100 hd64 have no long_keys: no long verdict, no extra kernel.

Output and per-call time through the registered op, bf16, 24 heads, 8 fresh processes in ABBA order (main head head main head main main head). The A/A noise is the spread across the 4 main runs:

card tokens head dim wpc run sha main = head main ms head ms delta A/A
L4 4608 64 8 / 8 yes 1.6566 1.6462 -0.63% 4.38%
L4 4608 128 12 / 12 yes 2.9600 2.9398 -0.68% 2.52%
L4 16384 64 8 / 8 yes 17.7035 17.5745 -0.73% 2.41%
L4 16384 128 12 / 12 yes 31.7831 31.6264 -0.49% 3.42%
A100 4608 64 4 / 4 yes 0.6966 0.6975 +0.13% 0.07%
A100 4608 128 8 / 8 yes 1.4826 1.4840 +0.10% 7.33%
A100 16384 64 4 / 4 yes 7.6951 7.6963 +0.02% 0.02%
A100 16384 128 8 / 8 yes 12.6990 12.6990 0.00% 0.02%

Both trees run the same kernel (same wpc) at every shape, the outputs are bit-identical, and every call engaged INT8. One row is above its A/A spread: A100 4608 / hd64, about 1 us per call. That config has no long_keys, so the GPU work is the same kernel. On the host the only addition there is one dict lookup in _run.

Install-time cost

Median of 3 fresh processes per arm. "Fresh driver cache" is a new CUDA_CACHE_PATH; "warm driver cache" is a second process on the same path. "In-process" means the verdict caches were dropped but the compiled kernels kept, so it measures the probes only.

card, head dim with long_keys fresh driver cache warm driver cache in-process
L4 hd64, main / head 2249 / 3548 ms (+1.3 s) 62 / 110 ms (+48 ms) 8.6 / 25.1 ms (+17 ms)
A100 hd128, main / head 2224 / 3332 ms (+1.1 s) 264 / 322 ms (+58 ms) 14.5 / 48.3 ms (+34 ms)

This is a cost moved, not added, wherever a long call follows. On main, the first 4608-key call pays the long compile. For L4 hd64 that first call is 1258 to 1276 ms on main vs 1.7 ms on head with a fresh driver cache, and 30.8 ms vs 1.7 ms with a warm one. On A100 hd128 the first 4608-key call is 2112 to 6942 ms on main vs 1075 to 1100 ms on head. Both arms include about 1 s of first-op-call setup there, which also appears on cards without long_keys. Cards and head dims without long_keys do not change (L4 hd128: 2162 vs 2159 ms; A100 hd64: 2225 vs 2239 ms). Only a process that never makes a call at or above long_keys pays the extra compile and gets nothing for it.

With #17791's disk compile cache (now on main), measured on this head with a fresh driver cache and a fresh kernel cache per arm: the long compile moves from the first long call to install (L4 hd64: about 2330 ms vs 2360 ms total, first long call 840-853 ms down to 1.7 ms; A100 hd128: about 3325 ms vs 3375 ms, first long call 1785 ms down to about 1080 ms). What is added is the long accuracy probe itself, about 18 ms on L4 and 33 ms on A100, once per card, head dim and dtype per process.

Not covered

  • H100, sm75 and sm120 were not run (their configs have no long_keys on the CUDA path).
  • The ROCm gfx1151 hd128 long_keys = 8000 entry goes through the same _long_check / _run code, but it was not run on AMD hardware here.
  • A real compile failure of the long kernel on hardware was not reproduced. That path is covered by the CPU tests only.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T08:55:58.913935Z 7c031eb 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: a8c06befac

ℹ️ 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".

…-long-config-probe

# Conflicts:
#	studio/backend/core/inference/diffusion_int8_attention.py
…-long-config-probe

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 7c031eb7b3

ℹ️ 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".

[skip ci]
@danielhanchen
danielhanchen merged commit a721a38 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the studio-int8-attention-long-config-probe branch November 15, 2026 12:10
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…on mode (diffusion red since #18004) (#18035)

## Summary

#18004 (INT8 attention on by default) gave `quantise` and
`_cuda_attention` in `core/inference/diffusion_int8_attention.py` a new
`mode` argument.
`studio/backend/tests/test_diffusion_int8_attention_long_probe.py` (from
#17796) replaces both with CPU stand-ins that still take the old
arguments. All 7 tests in the file fail on main, which makes `(Python
3.13, diffusion)` red for every PR based on `b2ad0b4493` or later:

```
TypeError: _fake_card.<locals>.quantise() takes from 4 to 5 positional arguments but 6 were given
KeyError: 'long'
assert (False)
```

The `KeyError` and the failed asserts follow from the same `TypeError`:
the card check catches the error from the stand-in and reports a failed
check, so the assertions about what it verified fail. With only the
stand-ins updated, all 7 pass.

The change in #18004 is fine. The test's fakes did not follow it.

## Change

- The fake `quantise` takes `mode` (default `MODE_MAIN`), and the
`_cuda_attention` stand-in becomes a function with the same parameters
as the real one and passes `mode` through.
- `_fake_card` now checks that each stand-in takes exactly the real
function's parameters, by name. The next signature change then fails
every test with `update the fake quantise: it takes [...]`, instead of a
`TypeError` that the card check swallows and turns into confusing
assertion failures.

No production code changes.

## Verification

- `tests/test_diffusion_int8_attention_long_probe.py`: 7 failed on main,
7 passed with this change.
- Adding a parameter to the real `quantise` makes all 7 fail with the
`update the fake quantise` message.
- `tests/test_diffusion_int8*.py` and
`tests/test_diffusion_attention*.py`: 1044 passed, 307 skipped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant