Skip to content

Cagri/farlukas/unified attention refactor - #5185

Merged
cagrikymk merged 11 commits into
farlukas/unified-attention-refactorfrom
cagri/farlukas/unified-attention-refactor
Sep 1, 2026
Merged

cagrikymk merged 11 commits into
farlukas/unified-attention-refactorfrom
cagri/farlukas/unified-attention-refactor

Conversation

@cagrikymk

Copy link
Copy Markdown
Contributor

Restructuring format

cagrikymk and others added 11 commits September 1, 2026 00:03
One nesting level per axis with complete leaves, replacing the composite-key
cross product. TILE_SIZE is derived from tuned bounds, so the BS_LEQ_* ladder
is gone and lookup is total over page sizes. Triton tables only; gfx1250/gluon
is not yet converted.

Resolution verified identical to the previous tables over 21,070 situations
per arch.

Co-Authored-By: Claude Opus 5 <[email protected]>
Brings gfx1250/gluon onto the same flat, axis-keyed tables as the triton
ones: 7,496 lines down to 201. Most of the old file was duplication -- the
head-size buckets held byte-identical copies everywhere except
hlt128_shuffled_kv, so D survives only where it earns its keep, and reduce
collapses to a single config.

Three gluon-specific points the conversion had to handle:

  * attn_2d keeps BS as an axis; LOOP_VARIANT changes with the page size, so
    it cannot fold into TILE_SIZE_MIN/MAX the way the triton tables do.
  * the 2d tile override runs the other way round -- the config value is read
    only with a shuffled cache, which unified_attention() asserts has a
    power-of-2 page. next_pow2(page) == page there, so a cap can never bind
    past the wrapper's own floor: those entries carry a floor alone.
  * dtype pairs cover nvfp4. DT_bf16_any + DT_any_bf16 names the five
    bf16-involving pairs in two keys instead of four.

Also fixes the triton tables, which disagreed with their own buckets at page
sizes between bucket bounds -- gfx942 kv_split returned 128 segments at page
17 where the pre-refactor table gives 64, and gfx1250 capped TILE_SIZE at
2048 where the old table caps at 256. The conversion grid now samples every
gap and two sizes above the largest bound, and drops entries that resolution
reaches identically by falling through.

Verified by replaying 312,984 situations through the loader against the
pre-refactor tables at 665c87f: 0 mismatches across all eight tables.

Co-Authored-By: Claude Opus 5 <[email protected]>
Every lookup probed for UNIFIED-ATTENTION-D=<n>-QPKV=<n>.json and
UNIFIED-ATTENTION-D=<n>.json before falling back to DEFAULT.json. No such
file exists anywhere in the tree, nothing in the repo writes one, and the
is_tuned flag it returned is discarded at all seven call sites. So it was
two filesystem probes per distinct shape and a return value nobody read.

Removing it also drops num_queries_per_kv from the cache key, which only the
tuned lookup consumed -- it is not a config axis, so it was splitting the
cache without changing any answer.

get_unified_attention_config() now returns the config itself rather than
(config, is_tuned). The per-shape convention is still there in
resolve_config_dir/load_config_json if a tuner ever produces those files.

Co-Authored-By: Claude Opus 5 <[email protected]>
NUM_SEGMENTS is a split-K decision, not a tuned constant: the useful split
count depends on how much parallelism the launch already has -- batch, KV
heads, CU count -- and on how many KV tiles the context holds. kv_split keys
on (D, SHUF, DT, BS), so it can name none of those, and the branch had frozen
the count at one operating point.

That point turns out to be a low-batch decode: every value in the tables is
exactly what select_3d_config produced at batch 1-15 (gfx942 D=128 gives 128
at batch 1-9, gfx1250 gives 16 at batch 8-14, and the windows shift with CU
count as the formula predicts). So this restores the batch/context/CU
dependence around the same point rather than discarding tuning.

The table now carries the parameters -- MIN_SEGMENTS, MAX_SEGMENTS,
SEGMENTS_PER_CU and the segment tile bounds -- and compute_segment_params()
does the arithmetic, the same split TILE_SIZE already uses. It runs outside
the lru_cache, since the program count is not part of the key, and reads only
host-side ints already in _UAParams: no sync, nothing graph-hostile.

What this fixes, on gfx1250 (256 CU, 32 q / 8 kv heads, D=128, page 16),
where decode always takes the 3D path and there is no batch cap:

    batch        1     4    16    64   256   512
    before      64    16     2     1     1     1
    after       16    16    16    16    16    16
    scratch    0.4M   1M    4M   16M   65M  130M   (after)
                1M    1M    1M    1M    2M    8M   (before)

so 8x under-split at batch 1 and 8-16x over-split from batch 64 up. gfx942
over-splits up to 16x for batch 16-152 (309 MiB against 19 MiB at batch 152);
gfx950 under-splits 8x at batch 1.

One deliberate simplification: the pre-JSON code rounded gfx1250's budget
share down and everyone else's up. Those agree at every power-of-2 program
count -- num_sms * SEGMENTS_PER_CU always carries a large power-of-2 factor,
so the division is exact, and past it the quotient has fallen below
MIN_SEGMENTS where the clamp hides it. batch and num_kv_heads are both powers
of two in practice, so one rule (round up) serves both. gfx1250 keeps a
"segment_tile": "page" key, which is not cosmetic: its 3D kernel makes a tile
exactly one page rather than a power-of-2 KV tile.

Verified against select_3d_config as of 9e487ce^: 1,640,520 lookups through
the shipped loader over head size, page size, dtype, shuffled, context, CU
count and power-of-2 program count -- 0 mismatches on all eight tables. The
other three ops still replay identically to the pre-refactor tables (312,984
situations), and 1,088 kernel tests pass on gfx950.

Co-Authored-By: Claude Opus 5 <[email protected]>
Injecting a plausible hand-edit into a non-native table -- Q_LEQ_1 mistyped
as Q_LE_1 on gfx1201 -- was caught by neither suite. The kernel tests only
run the native arch, and the structural tests only checked that a component
names a known axis: _axis_of reads the prefix, so Q_LE_1 still parses as axis
Q and passed. The entry was simply never matched.

Two tests close it. One checks each component has a shape _candidates can
actually emit (X_LEQ_n, X_GEQ_n, a bare flag, or DT_q_kv). The other sweeps
the grid, collects the keys that matched, and fails on any entry that never
did -- which also catches an entry shadowed by a more general one, or left
behind by an edit.

The dtype grid had a hole of its own: it paired dtypes only as (bf16, bf16),
(fp8, fp8), (bf16, fp8), (nvfp4, nvfp4), and DT_any_bf16 is reachable only
through a mixed pair like (fp8, bf16). It now covers all nine combinations,
without which the reachability test reports live gluon entries as dead.

Co-Authored-By: Claude Opus 5 <[email protected]>
The tables were fitted against the branch's own starting point, which had
already turned main's formulas into bucket ladders -- so the fit faithfully
reproduced their rounding along with the values. Replaying every reachable
situation against origin/main's select_2d_config and select_3d_config turned
up three classes of divergence, all predating these commits:

  * TILE_SIZE at pages the ladder could not name. The ladder's rungs are
    16/32/64/128/256, so a page below the bottom rung got 16 where main gives
    1..8, one between rungs got rounded up, and one above the top got capped.
    On gfx1250 that is every page, because main sets TILE_SIZE = block_size
    there rather than min(64, next_pow2(block_size)) -- an identity a bucket
    cannot hold.
  * the reduce's warp count. main drops it to 1 when the split lands on its
    floor; the branch froze the section to a static config, so it always
    launched 2.

Both are fixed by taking main as the target: TILE_SIZE fits MIN=1 MAX=64
(MIN=32 for fp8, 64 for the gfx950 wide-copy path), and the reduce carries the
split parameters plus SMALL_SPLIT_MAX so compute_segment_params can decide.

One divergence is deliberately not reproduced. On gfx1250 a non-power-of-2
page makes main's TILE_SIZE = block_size non-power-of-2 too, and the kernels
index a tile with tl.arange(0, TILE_SIZE) -- so main cannot compile those
shapes at all. next_pow2(page) matches main at every page that can actually
run and stays legal at the rest, which is what the tables now hold.

Verified against origin/main over 1,025,280 reachable situations -- every op,
every table, sweeping head size, page size, dtype, shuffled, context, batch
and CU count: 0 divergences, with 5,600 points excluded as unrunnable.
1,088 kernel tests pass on gfx950.

Co-Authored-By: Claude Opus 5 <[email protected]>
@cagrikymk
cagrikymk merged commit c56699a into farlukas/unified-attention-refactor Sep 1, 2026
2 checks passed
@cagrikymk
cagrikymk deleted the cagri/farlukas/unified-attention-refactor branch September 1, 2026 14:41
cagrikymk added a commit that referenced this pull request Sep 2, 2026
* Unified parameters

* Added backend parameter and 2d json config getter

* Switced key to prefix + suffix

* Fixed backend parameter

* Added backend parameter to UT

* Cleaned up subwrappers and created new utils file

* Added get_unified_attention_config

* Fixed import

* Fixed APIs and moved block_size to a bucket

* Added json configs for gfx950 and gfx1250 and removed SK and SQ buckets

* Replaced heuristics with json configs for UA2D triton

* Added json configs for missing arch

* Added docstring to config getter

* Removed select_2d_config

* Applied json configs to UA2D gluon

* Renamed config key helper

* Fixed RDNA regression

* Switched UA3D and reduce to json

* Fixed key errors for some non power of 2 block sizes

* black

* Added backend argument to ubench

* unified attention refactor (#5185)

* Fixed min > max config

* black formatter

---------

Co-authored-by: Mehmet Cagri <[email protected]>
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