HIP gfx103x: PR #540's attention kernel with 8 cells per step and DPP lane exchanges (bit-exact)
On HIP the default attention kernel's score loop is bound by the LDS pipe on gfx1030: its 12 x 5 __shfl_xor_sync per cell compile to ds_bpermute_b32 and share that pipe with the shared-memory q reads. attn_chunk_kernel_pre75 (PR #540, bit for bit the default kernel) is now built for HIP wave32 (not the gfx906 backend) and run on gfx103x by default: - NC_ template parameter, cells per step: CUDA 2 as before, HIP 8 (on gfx1030 every step up in NC was faster although occupancy fell from NC 1: the q reads bound it, not occupancy) - DPP_ template parameter: the butterfly's lane exchanges (reduce12, the softmax max / sum) through DPP row_xmask within a row and v_permlanex16 across rows instead of ds_bpermute; same values, same pairing order, unchanged bit for bit - pre75_attn() on HIP: gfx103x per device; STRATA_ATTN_PRE75=0 off, =1 also another wave32 card CUDA is unchanged: the non-experimental build does not build the kernel, and the template defaults equal the old constants with shfl_x<false> = __shfl_xor_sync. RX 6900 XT, IQ3_S, prompt tok/s at 9.4K / 34.7K / 105.8K: 441 / 467 / 460 -> 459 / 494 / 488; with #835's FP16 GEMMs 746 / 917 / 928 -> 801 / 1,022 / 1,041. Teacher-forced log-probabilities against the default kernel: KL 0, argmax 100%. bench, docs: the pre75 attention kernel on RX 6900 XT - measurements, exactness, decode A/B; OLDER_GPUS.md notes bench/results/2026-10-04-rdna2-pre75-attention: the rig, the compiler output (registers, occupancy, ds_bpermute counts) that motivated NC 8 and DPP, prompt speed on this branch (+4.2 / +5.7 / +6.1% at 9.4K / 34.7K / 105.8K tokens), the step-by-step NC / DPP series on the FP16 prompt path, the teacher-forced exactness check (KL 0 over 1,578 positions), a decode A/B (0.3-0.6 ms shorter windows, two rounds), the limits, and the configuration used. docs/OLDER_GPUS.md: the RX 6800 / 6900 row says this kernel is the default on gfx1030 and names the switch; the A/B switches line says what STRATA_ATTN_PRE75 does there.
This commit is contained in:
1 parent
0ff5b4145b
commit
6126f8dbeb
4 files changed
+224
-24
No files matched your search
@@ -0,0 +1,116 @@
|
||||
# RX 6900 XT (gfx1030): PR #540's attention kernel with 8 cells per step and DPP lane exchanges (2026-10-04)
|
||||
|
||||
`attn_chunk_kernel_pre75` (PR #540) is the default attention kernel with its 12-head reductions reorganised so that each
|
||||
cell needs 16 lane exchanges instead of 60, bit for bit the same sums. It was built only into the experimental CUDA build
|
||||
and used below sm_75. On HIP the default kernel ran, whose score loop is bound on gfx1030 by the LDS pipe: every
|
||||
`__shfl_xor_sync` compiles to `ds_bpermute_b32`, which shares that pipe with the shared-memory q reads. This change builds
|
||||
the pre75 kernel for HIP wave32 and runs it on gfx103x by default, with 8 cells per step (`NC_`, CUDA keeps 2) and the
|
||||
lane exchanges in registers (`DPP_`: `row_xmask` within a 16-lane row, `v_permlanex16` across the two rows; CUDA keeps
|
||||
`__shfl_xor_sync`). `STRATA_ATTN_PRE75=0` is the default kernel, so the main numbers below are one binary with and
|
||||
without that switch.
|
||||
|
||||
## Rig
|
||||
|
||||
- One AMD Radeon RX 6900 XT 16 GB (gfx1030, PCIe 4.0 x8) of the two in the machine; the other card idle. AMD Ryzen 5
|
||||
5600X, 128 GB DDR4-3200.
|
||||
- Ubuntu 26.04, kernel 7.0.0-38-generic, ROCm 10.0.0 (HIP 7.15.26333, rocBLAS 5.6).
|
||||
- Engine: `origin/main` at `6f32ec0` (0.1.39) with this change (`a649e42`), `-DSTRATA_ENABLE_HIP=ON
|
||||
-DCMAKE_HIP_ARCHITECTURES=gfx1030 -DSTRATA_PREFILL_MMQ=ON` (as setup builds it).
|
||||
- Model: Qwen3.8-Flash-Next GSQ-RCO IQ3_S (2 shards, `--native`), MTP draft layer on, `--kv int8 --kv-resident 32768
|
||||
--max-context 131072 --expert-cache auto`; [strata-1gpu.json](strata-1gpu.json) (`/m` the model directory, `/work`
|
||||
the build). Not one of setup's models.
|
||||
|
||||
## Why: the score loop on gfx1030
|
||||
|
||||
Compiler output for the INT8-KV kernels (`-Rpass-analysis=kernel-resource-usage` for registers and occupancy; the
|
||||
assembly for the instruction counts):
|
||||
|
||||
| kernel | VGPRs | waves / SIMD | `ds_bpermute` per warp and 8 cells (score loop) |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| default | 40 | 16 | 480 |
|
||||
| pre75, NC 1 | 101 | 9 | 128 |
|
||||
| pre75, NC 2 (CUDA's value) | 142 | 7 | 128 |
|
||||
| pre75, NC 4 | 149 | 6 | 128 |
|
||||
| pre75, NC 8 | 144 | 7 | 128 |
|
||||
| **pre75, NC 8 + DPP (this change)** | 148 | 6 | **0** |
|
||||
|
||||
LDS per block is 15.5 KB and does not limit occupancy. The DPP variant's kernel has 103 LDS instructions where the NC 8
|
||||
variant has 257 (static counts); its 154 `ds_bpermute` are gone. ROCm 10.0's hipcc does not turn an xor shuffle into
|
||||
DPP on its own, so the exchanges are written with `__builtin_amdgcn_mov_dpp` (`row_xmask`, M = 1, 2, 4, 8) and
|
||||
`__builtin_amdgcn_permlanex16` (M = 16), in the same pairing order as the shuffles; a 32-lane unit test of the exchange
|
||||
primitives against `__shfl_xor` matched bit for bit before the kernel was changed.
|
||||
|
||||
## Prompt speed, this branch
|
||||
|
||||
Cold server per run; three chats whose first message is 9,427 / 34,659 / 105,811 tokens of this repository's docs and
|
||||
sources, read by the batched prompt path; the server's own line `strata serve: prompt N tokens = 0 reused + N read in T
|
||||
ms`. One run per arm (the two are the exactness runs below):
|
||||
|
||||
| prompt tokens | default kernel (`STRATA_ATTN_PRE75=0`) | this change | gain | read time |
|
||||
| ---: | ---: | ---: | ---: | ---: |
|
||||
| 9,427 | 440.7 tok/s | 459.1 tok/s | +4.2% | 21.4 s -> 20.5 s |
|
||||
| 34,659 | 466.9 tok/s | 493.6 tok/s | +5.7% | 74.2 s -> 70.2 s |
|
||||
| 105,811 | 460.4 tok/s | 488.3 tok/s | +6.1% | 229.8 s -> 216.7 s |
|
||||
|
||||
Attention is a small share of the prompt on this card while the 16-bit GEMMs run rocBLAS's slow fallback kernels (#835
|
||||
changes that); the gain grows with the prompt because the attention's share does.
|
||||
|
||||
## Step by step, on the FP16 prompt path
|
||||
|
||||
With #835 applied the GEMMs are about twice as fast and attention is a larger share, so the kernel variants are easier to
|
||||
tell apart. These runs are a test build of `6f32ec0` + #835 with the variants selectable by environment variables
|
||||
(`STRATA_ATTN_PRE75=1`, `STRATA_ATTN_NC`, `STRATA_ATTN_DPP`), not the binary of this branch; same card, same three
|
||||
chats, cold server per run, one run per row except NC 2 (two runs, 778.9 / 981.1 / 998.0 and 779.0 / 981.6 / 996.9 tok/s,
|
||||
within 0.2%):
|
||||
|
||||
| attention kernel | 9,427 | 34,659 | 105,811 |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| default | 746 | 917 | 928 |
|
||||
| pre75, NC 1 | 763 (+2.3%) | 950 (+3.6%) | 960 (+3.4%) |
|
||||
| pre75, NC 2 | 779 (+4.4%) | 982 (+7.0%) | 997 (+7.5%) |
|
||||
| pre75, NC 4 | 787 (+5.5%) | 997 (+8.7%) | 1,008 (+8.6%) |
|
||||
| pre75, NC 8 | 792 (+6.2%) | 1,006 (+9.7%) | 1,025 (+10.5%) |
|
||||
| **pre75, NC 8 + DPP** | **801 (+7.4%)** | **1,022 (+11.4%)** | **1,041 (+12.2%)** |
|
||||
|
||||
Every step up in NC was faster although occupancy went down from NC 1 (9 to 6-7 waves per SIMD): on this card the q reads
|
||||
and the lane exchanges on the LDS pipe bound the loop, not occupancy. That is the opposite of the V100, where #540 found
|
||||
NC 4 slower than 2, which is why the template keeps CUDA at 2. NC can only be a divisor of 8 (a warp handles 8 cells), so
|
||||
8 is the largest value as well as the fastest measured. The DPP variant on top is another +1.1 / +1.6 / +1.5%.
|
||||
|
||||
With both changes a prompt reads at 1.82x / 2.19x / 2.26x `6f32ec0` on this card (439 / 466 / 461 -> 801 / 1,022 /
|
||||
1,041 tok/s). The two changes touch different files and either applies alone.
|
||||
|
||||
## Exactness
|
||||
|
||||
Teacher-forced log-probabilities (`STRATA_LOGPOS`, top 256, the method of `bench/results/2026-10-03-v100-prompt-attn`):
|
||||
the three chats above, each with a fixed assistant reply and a last message of about 580 tokens read through the verify
|
||||
windows, every position scored: 578 / 519 / 481 = 1,578 positions. This change against the default kernel, same binary:
|
||||
KL 0 at every position, argmax and top-10 agreement 100%. The same held for every NC variant and for the DPP variant in
|
||||
the step-by-step runs (each against its own default-kernel run on the FP16 base). The sums are the same values added in
|
||||
the same pairing order; the kernel only moves them differently.
|
||||
|
||||
## Decode
|
||||
|
||||
The verify window runs the same attention kernel over its few rows. Measured on 2026-10-05, one card, this branch's binary,
|
||||
greedy, 256-token cap (the answers stopped at 128-145 tokens) to a 2,267-token prompt and three of 3,906-4,919 tokens,
|
||||
`STRATA_DECODE_TIMING=1`, two rounds alternating the arms with a fresh server each:
|
||||
|
||||
| prompt tokens | default kernel, ms / window (round 1 / 2) | this change | difference |
|
||||
| ---: | ---: | ---: | ---: |
|
||||
| 2,267 | 56.00 / 56.08 | 55.68 / 55.63 | -0.4 ms (-0.7%) |
|
||||
| 4,380 | 42.14 / 42.19 | 41.88 / 41.89 | -0.3 ms (-0.7%) |
|
||||
| 4,919 | 42.61 / 42.39 | 42.03 / 41.95 | -0.5 ms (-1.2%) |
|
||||
| 3,906 | 44.17 / 44.21 | 43.59 / 43.67 | -0.6 ms (-1.3%) |
|
||||
|
||||
The window is 0.3-0.6 ms shorter with this change in both rounds, where the two rounds of one arm differ by at most
|
||||
0.2 ms; decode 43-53 tok/s against 43-56. The greedy answers took the same number of verify windows in 7 of the 8
|
||||
request pairs (the 3,906-token prompt once differed by two windows: adaptive expert swaps were on, so an expert can run
|
||||
on the GPU in one run and on the CPU in the other, and the two round differently).
|
||||
|
||||
## Limits
|
||||
|
||||
One machine, one model (IQ3_S packed from a non-setup GGUF), one corpus; one run per arm on this branch. The step-by-step
|
||||
series was measured on a test build with #835, not on this branch alone. CUDA builds were not run (no CUDA here): the
|
||||
CUDA path is unchanged by construction, since the template defaults equal the old constants and `shfl_x<false>` is
|
||||
`__shfl_xor_sync`. Other wave32 AMD cards (gfx1031 / gfx1032, RDNA3, RDNA4) were not run; `STRATA_ATTN_PRE75=1` turns
|
||||
the kernel on there.
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"exe": "/work/build-hip/strata",
|
||||
"args": [
|
||||
"--pack",
|
||||
"/m/packs/iq3_s",
|
||||
"--native",
|
||||
"/m/gguf/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf",
|
||||
"--ple-gguf",
|
||||
"/m/gguf/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00002-of-00002.gguf",
|
||||
"--expert-profile",
|
||||
"/work/data/expert-profile.bin",
|
||||
"--expert-cache",
|
||||
"auto",
|
||||
"--prefill",
|
||||
"auto",
|
||||
"--spec",
|
||||
"4",
|
||||
"--spec-min-p",
|
||||
"0.5",
|
||||
"--mtp",
|
||||
"/m/mtp/rt",
|
||||
"--max-context",
|
||||
"131072",
|
||||
"--kv",
|
||||
"int8",
|
||||
"--kv-resident",
|
||||
"32768"
|
||||
],
|
||||
"cwd": "/work",
|
||||
"tokenizer": "/m/packs/iq3_s/tokenizer",
|
||||
"model_name": "qwen3.8-flash-next-iq3_s",
|
||||
"log": "/work/logs/strata.log",
|
||||
"lib_dirs": [
|
||||
"/opt/rocm/lib"
|
||||
],
|
||||
"port": 8080,
|
||||
"backend": "hip",
|
||||
"gpu": 1,
|
||||
"gpus_asked": true
|
||||
}
|
||||
+3
-2
@@ -23,7 +23,7 @@ capability 6.1 too, and cannot hold any of the model).
|
||||
|
||||
| Cards | Architecture | How it runs | Reported |
|
||||
| --- | --- | --- | --- |
|
||||
| RX 6800 / 6900 series | gfx1030 | setup (`--backend hip`), unvalidated | [AMD_HIP.md](AMD_HIP.md#rdna2-gfx1030) |
|
||||
| RX 6800 / 6900 series | gfx1030 | setup (`--backend hip`), unvalidated; #540's attention kernel is the default there, with 8 cells per step and DPP lane exchanges (bit-exact; `STRATA_ATTN_PRE75=0` runs the standard kernel) | [AMD_HIP.md](AMD_HIP.md#rdna2-gfx1030); RX 6900 XT, IQ3_S: prompts +4-6% alone, +7-12% with #835 ([bench/results/2026-10-04-rdna2-pre75-attention](../bench/results/2026-10-04-rdna2-pre75-attention/README.md)) |
|
||||
| RX 6700 XT | gfx1031 | setup (`--backend hip`), unvalidated (#524) | used daily by its reporter, one card |
|
||||
| RX 5500 XT (RDNA1) | gfx1012 | built by hand: `-DCMAKE_HIP_ARCHITECTURES=gfx1012` (HIP 5.7 or 7) | 8 GB card, IQ3_S, 8K prompt: 15.3 tok/s decode (#442) |
|
||||
| RX 5700 XT (RDNA1) | gfx1010 | built by hand: `-DCMAKE_HIP_ARCHITECTURES=gfx1010`, on ROCm 7.14's `gfx101X-dgpu` wheels; needs `ROCR_VISIBLE_DEVICES=0` in a PC that also has an AMD iGPU | 8 GB card, Coder IQ1_M, 32K context: prompt 115 / 144 / 149 tok/s and decode 18.0 / 20.3 / 23.3 tok/s at 4K / 16K / 30K prompt tokens, 6 of 6 needle checks at 8K and 30K, 61 of 61 ctest on the card, 607 of 12,288 experts in VRAM |
|
||||
@@ -93,7 +93,8 @@ sums round differently): #540 measured a mean KL of 8.4e-3 on the next-token dis
|
||||
product on an RTX 2080 Ti.
|
||||
|
||||
A/B switches: `STRATA_BF16_TC=0|1`, `STRATA_PROMPT_ATTN_OLD=1` (the decode kernel for prompts), `STRATA_ATTN_PRE75=0`
|
||||
(#540's kernel off). [NVIDIA_V100.md](NVIDIA_V100.md) has the V100 build, its measurements and the parity test.
|
||||
(#540's kernel off; on gfx103x with HIP that kernel is the default, see the AMD table above, and `=1` turns it on for
|
||||
another wave32 AMD card). [NVIDIA_V100.md](NVIDIA_V100.md) has the V100 build, its measurements and the parity test.
|
||||
|
||||
## AMD: building gfx906 and gfx1012
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <cfloat>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
namespace strata::kernels {
|
||||
namespace {
|
||||
@@ -218,34 +219,63 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __rest
|
||||
|
||||
// PR #540 (sskver): the same attention with fewer shuffles and less shared-memory traffic, bit for bit the kernel
|
||||
// above (same operands, same order); +7% prompt speed on a V100, where this kernel reads every prompt chunk. It
|
||||
// uses more registers (80 vs 38 on sm_70), so it runs on cards below sm_75 only, and exists only in the experimental
|
||||
// build (-DSTRATA_EXPERIMENTAL_SM60=ON): the ready-made engine keeps exactly the kernel above.
|
||||
#if defined(STRATA_EXPERIMENTAL_SM60)
|
||||
// uses more registers (80 vs 38 on sm_70), so on CUDA it runs on cards below sm_75 only, and exists only in the
|
||||
// experimental build (-DSTRATA_EXPERIMENTAL_SM60=ON). HIP: built for wave32 cards (not the gfx906 wave64 backend) and
|
||||
// run on gfx103x (RDNA2), with 8 cells per step and the butterfly's lane exchanges in registers (DPP / permlanex16)
|
||||
// instead of ds_bpermute: there the score loop is bound by the LDS pipe (the q reads and ds_bpermute share it), not by
|
||||
// occupancy - 8 cells per step at 7 waves/SIMD beat 1 cell at 9. Every other card keeps exactly the kernel above.
|
||||
#if defined(STRATA_EXPERIMENTAL_SM60) || (defined(__HIPCC__) && !defined(STRATA_HIP_GFX906))
|
||||
#define STRATA_ATTN_PRE75_BUILT 1
|
||||
#endif
|
||||
#if defined(STRATA_ATTN_PRE75_BUILT)
|
||||
// A butterfly step's lane exchange, `M` = 1, 2, 4, 8 or 16. DPP (HIP, wave32): in registers - DPP row_xmask within a
|
||||
// 16-lane row, v_permlanex16 across the two rows - instead of ds_bpermute; the same value moves, so every sum is
|
||||
// unchanged bit for bit.
|
||||
template <bool DPP, int M> __device__ __forceinline__ float shfl_x(float v) {
|
||||
#if defined(__HIPCC__)
|
||||
if constexpr (DPP) {
|
||||
const int x = __float_as_int(v);
|
||||
if constexpr (M == 16) return __int_as_float(__builtin_amdgcn_permlanex16(x, x, 0x76543210, 0xfedcba98, false, false));
|
||||
else return __int_as_float(__builtin_amdgcn_mov_dpp(x, 0x160 | M, 0xF, 0xF, false)); // row_xmask:M
|
||||
}
|
||||
#endif
|
||||
return __shfl_xor_sync(0xffffffffu, v, M);
|
||||
}
|
||||
// warp_sum / warp_max with the same pairing order (xor 16, 8, 4, 2, 1)
|
||||
template <bool DPP> __device__ __forceinline__ float warp_sum_x(float v) {
|
||||
v += shfl_x<DPP, 16>(v); v += shfl_x<DPP, 8>(v); v += shfl_x<DPP, 4>(v); v += shfl_x<DPP, 2>(v); v += shfl_x<DPP, 1>(v);
|
||||
return v;
|
||||
}
|
||||
template <bool DPP> __device__ __forceinline__ float warp_max_x(float v) {
|
||||
v = fmaxf(v, shfl_x<DPP, 16>(v)); v = fmaxf(v, shfl_x<DPP, 8>(v)); v = fmaxf(v, shfl_x<DPP, 4>(v));
|
||||
v = fmaxf(v, shfl_x<DPP, 2>(v)); v = fmaxf(v, shfl_x<DPP, 1>(v));
|
||||
return v;
|
||||
}
|
||||
// The 12 heads' lane sums (`part[12..15]` zero), reduce-scattered with warp_sum's pairing order (xor 16, 8, 4, 2, 1):
|
||||
// every output adds the same two operands at every level, so each sum is bit for bit warp_sum's (float add commutes),
|
||||
// for 16 shuffles instead of 12 x 5. Lane l ends holding head head_of_lane(l); lanes l and l^1 agree.
|
||||
__device__ __forceinline__ float reduce12(const float (&part)[16], int lane) {
|
||||
template <bool DPP> __device__ __forceinline__ float reduce12(const float (&part)[16], int lane) {
|
||||
float r8[8];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
const bool hi = (lane & 16) != 0;
|
||||
r8[i] = (hi ? part[i + 8] : part[i]) + __shfl_xor_sync(0xffffffffu, hi ? part[i] : part[i + 8], 16);
|
||||
r8[i] = (hi ? part[i + 8] : part[i]) + shfl_x<DPP, 16>(hi ? part[i] : part[i + 8]);
|
||||
}
|
||||
float r4[4];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const bool hi = (lane & 8) != 0;
|
||||
r4[i] = (hi ? r8[i + 4] : r8[i]) + __shfl_xor_sync(0xffffffffu, hi ? r8[i] : r8[i + 4], 8);
|
||||
r4[i] = (hi ? r8[i + 4] : r8[i]) + shfl_x<DPP, 8>(hi ? r8[i] : r8[i + 4]);
|
||||
}
|
||||
float r2[2];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
const bool hi = (lane & 4) != 0;
|
||||
r2[i] = (hi ? r4[i + 2] : r4[i]) + __shfl_xor_sync(0xffffffffu, hi ? r4[i] : r4[i + 2], 4);
|
||||
r2[i] = (hi ? r4[i + 2] : r4[i]) + shfl_x<DPP, 4>(hi ? r4[i] : r4[i + 2]);
|
||||
}
|
||||
const bool hi2 = (lane & 2) != 0;
|
||||
const float r1 = (hi2 ? r2[1] : r2[0]) + __shfl_xor_sync(0xffffffffu, hi2 ? r2[0] : r2[1], 2);
|
||||
return r1 + __shfl_xor_sync(0xffffffffu, r1, 1);
|
||||
const float r1 = (hi2 ? r2[1] : r2[0]) + shfl_x<DPP, 2>(hi2 ? r2[0] : r2[1]);
|
||||
return r1 + shfl_x<DPP, 1>(r1);
|
||||
}
|
||||
__device__ __forceinline__ int head_of_lane(int lane) {
|
||||
return ((lane >> 4) & 1) * 8 + ((lane >> 3) & 1) * 4 + ((lane >> 2) & 1) * 2 + ((lane >> 1) & 1);
|
||||
@@ -272,7 +302,7 @@ __device__ __forceinline__ float load_v1(const QsaAttnPools& p, long long row, i
|
||||
}
|
||||
}
|
||||
|
||||
template <int KV_MODE>
|
||||
template <int KV_MODE, int NC_ = 2, bool DPP_ = false>
|
||||
__global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float* __restrict__ q, QsaAttnPools p,
|
||||
const int32_t* __restrict__ ids,
|
||||
const int32_t* __restrict__ step, int n_kv_heads,
|
||||
@@ -321,7 +351,7 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float*
|
||||
// (warp + 8 * (i + j)): a lane's q slice comes out of shared memory once for all of them, which cuts the traffic
|
||||
// that bounds this phase (24 x 16 B per lane per cell, against the FMAs) by NC; every dot product is the same
|
||||
// expression as for a single cell, so the scores are unchanged.
|
||||
constexpr int NC = 2; // 4 measured slower on a V100 (the registers cost occupancy)
|
||||
constexpr int NC = NC_; // CUDA 2 (4 measured slower on a V100: the registers cost occupancy); HIP gfx103x 8
|
||||
const int hd = head_of_lane(lane);
|
||||
for (int i = 0; i < CHUNK / WARPS; i += NC) {
|
||||
int cc[NC];
|
||||
@@ -349,7 +379,7 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float*
|
||||
for (int j = 0; j < NC; ++j) {
|
||||
#pragma unroll
|
||||
for (int h = G; h < 16; ++h) pp[j][h] = 0.0f;
|
||||
const float sj = reduce12(pp[j], lane);
|
||||
const float sj = reduce12<DPP_>(pp[j], lane);
|
||||
if ((lane & 1) == 0 && hd < G) sp[hd][cc[j]] = sj * scale;
|
||||
}
|
||||
} else {
|
||||
@@ -371,7 +401,7 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float*
|
||||
}
|
||||
#pragma unroll
|
||||
for (int h = G; h < 16; ++h) part[h] = 0.0f;
|
||||
const float s = reduce12(part, lane);
|
||||
const float s = reduce12<DPP_>(part, lane);
|
||||
if ((lane & 1) == 0 && hd < G) sp[hd][c] = s * scale;
|
||||
}
|
||||
}
|
||||
@@ -380,12 +410,12 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float*
|
||||
// per-head chunk max and exp-sum: warp w handles heads w and w+8.
|
||||
for (int h = warp; h < G; h += WARPS) {
|
||||
const float a = sp[h][lane], b = sp[h][lane + 32];
|
||||
const float m = warp_max(fmaxf(a, b));
|
||||
const float m = warp_max_x<DPP_>(fmaxf(a, b));
|
||||
const float ea = (lane < n_here && srow[lane] >= 0) ? __expf(a - m) : 0.0f;
|
||||
const float eb = (lane + 32 < n_here && srow[lane + 32] >= 0) ? __expf(b - m) : 0.0f;
|
||||
spt[lane][h] = ea;
|
||||
spt[lane + 32][h] = eb;
|
||||
const float l = warp_sum(ea + eb);
|
||||
const float l = warp_sum_x<DPP_>(ea + eb);
|
||||
if (lane == 0) { part_m[slot * G + h] = m; part_l[slot * G + h] = l; }
|
||||
}
|
||||
__syncthreads();
|
||||
@@ -425,7 +455,7 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float*
|
||||
#pragma unroll
|
||||
for (int h = 0; h < G; ++h) part_acc[((size_t) slot * G + h) * HD + t] = acc[h];
|
||||
}
|
||||
#endif // STRATA_EXPERIMENTAL_SM60
|
||||
#endif // STRATA_ATTN_PRE75_BUILT
|
||||
|
||||
__global__ void __launch_bounds__(HD) attn_merge_kernel(const float* __restrict__ part_acc,
|
||||
const float* __restrict__ part_m,
|
||||
@@ -452,18 +482,26 @@ __global__ void __launch_bounds__(HD) attn_merge_kernel(const float* __restrict_
|
||||
attn[(size_t) h * HD + d] = L > 0.0f ? acc / L : 0.0f;
|
||||
}
|
||||
|
||||
#if defined(STRATA_EXPERIMENTAL_SM60)
|
||||
// the current device is below sm_75 (per device: a layer split can mix cards); STRATA_ATTN_PRE75=0 turns PR #540's
|
||||
// kernel off (A/B)
|
||||
#if defined(STRATA_ATTN_PRE75_BUILT)
|
||||
// CUDA: the current device is below sm_75; HIP: it is gfx103x (per device: a layer split can mix cards).
|
||||
// STRATA_ATTN_PRE75=0 turns PR #540's kernel off (A/B); on HIP =1 also runs it on another wave32 card.
|
||||
bool pre75_attn() {
|
||||
static const bool off = [] {
|
||||
static const int env = [] {
|
||||
const char* e = std::getenv("STRATA_ATTN_PRE75");
|
||||
return e != nullptr && e[0] == '0';
|
||||
return e == nullptr ? -1 : e[0] == '0' ? 0 : e[0] == '1' ? 1 : -1;
|
||||
}();
|
||||
static int cc[64] = {};
|
||||
int dev = 0;
|
||||
if (off) return false;
|
||||
if (env == 0) return false;
|
||||
if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return false; }
|
||||
#if defined(__HIPCC__)
|
||||
if (cc[dev] == 0) { // 2: gfx103x, 1: another card
|
||||
hipDeviceProp_t prop{};
|
||||
if (hipGetDeviceProperties(&prop, dev) != hipSuccess) { (void) hipGetLastError(); return false; }
|
||||
cc[dev] = std::strncmp(prop.gcnArchName, "gfx103", 6) == 0 ? 2 : 1;
|
||||
}
|
||||
return env == 1 || cc[dev] == 2;
|
||||
#else
|
||||
if (cc[dev] == 0) {
|
||||
int major = 0, minor = 0;
|
||||
if (cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev) != cudaSuccess ||
|
||||
@@ -474,8 +512,13 @@ bool pre75_attn() {
|
||||
cc[dev] = 10 * major + minor;
|
||||
}
|
||||
return cc[dev] < 75;
|
||||
#endif
|
||||
}
|
||||
#if defined(__HIPCC__)
|
||||
#define STRATA_ATTN_CHUNK(M) (pre75_attn() ? attn_chunk_kernel_pre75<M, 8, true> : attn_chunk_kernel<M>)
|
||||
#else
|
||||
#define STRATA_ATTN_CHUNK(M) (pre75_attn() ? attn_chunk_kernel_pre75<M> : attn_chunk_kernel<M>)
|
||||
#endif
|
||||
#else
|
||||
#define STRATA_ATTN_CHUNK(M) attn_chunk_kernel<M>
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user