Archived
1138 lines
52 KiB
Plaintext
1138 lines
52 KiB
Plaintext
// r4d_gdn: the gated-delta-net chunked scan, gfx1201 WMMA. Entry point
|
|
// r4d_gdn_chunk_scan_k128_v128_c64_bf16 -- head_k 128, head_v 128, chunk 64, bf16 in.
|
|
// Named for the algorithm rather than the serving phase: the chunked scan is what prefill and
|
|
// chunked prefill both run, while decode runs the recurrent update, which is a different kernel.
|
|
// It replaces FLA's recompute_w_u_fwd + chunk_gated_delta_rule_fwd_h + chunk_fwd_o: the
|
|
// recurrent state lives in WMMA accumulators across the whole chunk loop, so h / w / u /
|
|
// v_new are never written to or read from HBM.
|
|
//
|
|
// Every decay factor this kernel forms is <= 1. That is a correctness requirement, not
|
|
// a style: the intra-chunk gate span reaches 558 nats on real Qwen3.8 prefill
|
|
// activations, so any factorisation that puts part of the exponent on
|
|
// its own -- e^{g_i-c} and e^{c-g_j} about a per-chunk reference c, say -- sends one half
|
|
// past fp32's e^88 while the other underflows to 0, whatever c is chosen. The pair
|
|
// e^{g_i-g_j} (j <= i) and e^{g_last-g_j} never leave (0, 1].
|
|
//
|
|
// ge[j]=e^{g_j} gg[j]=g_j gs[j]=e^{g_last-g_j}
|
|
// U = K@S W = beta*(V - ge*U) V' = A@W (undecayed)
|
|
// P = Q@K^T sA = scale*e^{g_i-g_j}*tril(P)
|
|
// O = scale*ge*(Q@S) + sA@V'
|
|
// S = e^{g_last}*S + V'^T@(gs*K)
|
|
// A is FLA's solve_tril output (decay already folded in), so no decay enters W or V'.
|
|
// The (i,j) weight is therefore applied per element in the score step -- eight __expf per
|
|
// tile -- and the state's e^{g_last-g_j} rides on K, which is dead by then. V' itself has
|
|
// to stay undecayed because its two consumers want different references: the output wants
|
|
// e^{g_i-g_j}, which is row-dependent, and the state wants e^{g_last-g_j}. One shared
|
|
// scaling of V' can only serve both through a split, which is exactly what has no safe c.
|
|
#include <hip/hip_runtime.h>
|
|
#include "r4d.h"
|
|
#include "r4d_gdn_args.h"
|
|
#include "r4d_gdn_wmma.h"
|
|
// TRIL: skip GEMM tiles above the diagonal (A from solve_tril is unit lower
|
|
// triangular, and sA is masked). 2.4% slower, and left off; the reason is
|
|
// structural rather than tuning: the waves rendezvous at a workgroup barrier, so only
|
|
// the critical path matters. Every wave computes 2 P tiles; only 10 of the 16 are
|
|
// needed, but 10 tiles over 8 waves still leaves some wave holding 2 -- the barrier's
|
|
// arrival time does not move, and the skipped waves just wait longer. On top of that
|
|
// the guards stop the compiler batching fragment loads ahead of the WMMA chain.
|
|
// Correctness is unaffected either way, which is why the flag can stay.
|
|
#ifndef TRIL
|
|
#define TRIL 0
|
|
#endif
|
|
// LDSBAR: __syncthreads() is an ALL-address-space fence, so the barrier that follows
|
|
// the global O store compiles to s_wait_storecnt_dscnt 0x0 -- every wave waits for its
|
|
// output stores to reach cache before an LDS-only rendezvous. Scoping the fence to
|
|
// "local" keeps the required LDS ordering and drops the storecnt wait.
|
|
#ifndef LDSBAR
|
|
#define LDSBAR 1
|
|
#endif
|
|
#if LDSBAR
|
|
#define BAR() do { \
|
|
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup", "local"); \
|
|
__builtin_amdgcn_s_barrier(); \
|
|
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "workgroup", "local"); \
|
|
} while (0)
|
|
#else
|
|
#define BAR() __syncthreads()
|
|
#endif
|
|
|
|
#define BT 64
|
|
#define KD 128
|
|
#define VD 128
|
|
#define BV 64 // V per workgroup; also halves the Q@K^T redundancy
|
|
#ifndef KP
|
|
#define KP 132 // row pitch of the [.][KD] buffers, in shorts
|
|
#endif
|
|
#ifndef AP
|
|
#define AP 68 // row pitch of the [.][BT] buffers, in shorts
|
|
#endif
|
|
#define WP AP
|
|
// NW: waves per workgroup. The [4 token][4 v] tile grid is fixed by BT and BV, so the
|
|
// wave count decides how many tiles each wave carries: 8 waves take two, 16 take one.
|
|
// One tile per wave halves the live accumulators (and the state slice), which is what
|
|
// this kernel is short of -- at NW=8 it sits on 242 VGPRs and keeping one more address
|
|
// live spills 146 of them.
|
|
#ifndef NW
|
|
#define NW 8
|
|
#endif
|
|
#define NTHR (NW * 32)
|
|
#if NW == 8
|
|
#define NTV 2 // n tiles per wave in the main / V' / O loops
|
|
#elif NW == 16
|
|
#define NTV 1
|
|
#else
|
|
#error "NW must be 8 or 16"
|
|
#endif
|
|
// SASG: how a wave's slice of the 8 k-tile x 4 v-tile state grid is shaped. The A
|
|
// operand of the state update is K TRANSPOSED, which costs eight scalar LDS reads per
|
|
// fragment against one for an untransposed one, so the shape that reads the FEWEST
|
|
// k-tiles per wave would win: one k-tile x four v-tiles reads 4 fragT + 16 fragP per chunk
|
|
// where two x two reads 8 + 8. The transposed reads are only ~3% of the kernel (ABL=7
|
|
// prices them), and the 1x4 shape is about 20% SLOWER: reading half as many transposed
|
|
// fragments does not pay for holding four V' fragments live per k step, because the shape
|
|
// that reads fewer k-tiles reads more of everything else. Left at 0 (two k-tiles x two
|
|
// v-tiles).
|
|
#ifndef SASG
|
|
#define SASG 0
|
|
#endif
|
|
#if SASG && NW == 8
|
|
#define SKT 1 // state k-tiles per wave
|
|
#define SVT 4 // state v-tiles per wave
|
|
#elif SASG && NW == 16
|
|
#define SKT 1
|
|
#define SVT 2
|
|
#elif NW == 8
|
|
#define SKT 2
|
|
#define SVT 2
|
|
#else
|
|
#define SKT 1
|
|
#define SVT 2
|
|
#endif
|
|
#define NST (SKT * SVT) // state tiles per wave
|
|
|
|
struct Smem {
|
|
unsigned short k[BT][KP]; // A: K / B: K^T for P=Q@K^T
|
|
unsigned short A[BT][AP]; // B-source for sA (written as [i][j])
|
|
unsigned short S[BV][KP]; // B: state, V-major (matches FLA's [V,K] layout)
|
|
unsigned short W[BV][WP]; // B: W (transposed)
|
|
unsigned short D[BV][AP]; // staged V as [token][v]; then V' as [v][token].
|
|
// Both live inside one chunk and never overlap: V dies
|
|
// in the W update, V' is written after barrier (2).
|
|
float ge[BT], gv[BT], gg[BT], bt[BT]; // gv = gs (or e^{c-g} under GSPLIT), gg = raw g
|
|
};
|
|
|
|
// LDSO: hold each LDS row pointer as one opaque 32-bit byte offset (see fragO).
|
|
#ifndef LDSO
|
|
#define LDSO 1
|
|
#endif
|
|
#ifndef SCHED
|
|
#define SCHED 5
|
|
#endif
|
|
#ifndef SB // k steps per sched group in the V' and O+= loops
|
|
#define SB 2
|
|
#endif
|
|
#ifndef SS // k steps per sched group in the state update
|
|
#define SS 1
|
|
#endif
|
|
// GO: address the global q / A fragments as a 32-bit element offset off a uniform base
|
|
// (fragGo) instead of a per-lane 64-bit pointer (rowG + fragP). See r4d_gdn_wmma.h --
|
|
// the pointer form makes LLVM rebuild v_mad_co_i64_i32 + v_lshlrev_b64 + a 64-bit add
|
|
// per fragment at this kernel's register pressure.
|
|
#ifndef GO
|
|
#define GO 1
|
|
#endif
|
|
// LDSO2: the same treatment for the V' / O / state loops' LDS rows -- one opaque byte
|
|
// offset per buffer, so every k step is an immediate on ds_load_2addr_b32 (LDSO already
|
|
// does this for the main loop's five fragments).
|
|
// It SPILLS at NW=8 (242 VGPRs already, 146 spilled, 2.5x slower) and is only usable
|
|
// when a wave carries one tile.
|
|
#ifndef LDSO2
|
|
#define LDSO2 (NW == 16)
|
|
#endif
|
|
// WIDE: stage k and v eight shorts per lane (global_load_b128) instead of four, and
|
|
// address them as a 32-bit element offset off a chunk-uniform base. Halves the staging
|
|
// loads, and with a wave-uniform row (PFV >= 1) the address is one v_add on an SALU row
|
|
// offset instead of v_mad_co_i64_i32 + v_lshlrev_b64 + a 64-bit add.
|
|
#ifndef WIDE
|
|
#define WIDE 1
|
|
#endif
|
|
// PFIN: issue the next chunk's six staging loads INSIDE the main WMMA loop, one per k step,
|
|
// instead of as a block before it. Nothing orders them -- they read global memory and write
|
|
// registers consumed a whole chunk later -- and as a block they land in the 267-instruction
|
|
// stretch between the main loop's last WMMA and the V' loop's first, which has no WMMA to hide
|
|
// their addressing behind.
|
|
// It is ~5% slower and left at 0: the loads themselves are one v_min and an access, but
|
|
// interleaving them costs the main loop's WMMA schedule more than the block costs in the gap.
|
|
// (Nor does an in-loop bf16 rounding trick exist to shrink that gap: gfx1201 has no
|
|
// v_add_nc_u64, so two floats cannot be rounded with one add, and v_pk_add_u16 does not carry
|
|
// between halves -- which IS the rounding.)
|
|
#ifndef PFIN
|
|
#define PFIN 0
|
|
#endif
|
|
// APRE: issue the V' loop's four A fragments (global) BEFORE the main WMMA loop instead
|
|
// of at the top of the V' loop. They are read immediately after barrier (2), so their HBM
|
|
// latency is otherwise fully exposed; hoisted, they land during the main loop's 48 WMMAs.
|
|
// Costs 16 VGPRs.
|
|
// It is ~16% slower and left at 0. It does not spill (227 VGPRs), but the sixteen registers
|
|
// it keeps live across the main loop cost more than the latency it hides, and the V' loop's
|
|
// sched group already batches those loads a whole step ahead.
|
|
#ifndef APRE
|
|
#define APRE 0
|
|
#endif
|
|
// ABL: timing-only ablation ladder -- every nonzero value produces WRONG results and
|
|
// exists solely to price one stage. 1 = drop the state-update WMMAs, 2 = drop O += sA@V',
|
|
// 3 = drop V' = A@W, 4 = drop P from the main loop, 5 = drop U, 6 = drop O^T,
|
|
// 7 = read the state update's K^T operand with a normal (untransposed) fragP, which has
|
|
// the same WMMA count and 2 LDS reads per fragment instead of 8.
|
|
#ifndef ABL
|
|
#define ABL 0
|
|
#endif
|
|
// BFR: which bf16 packs round. 1 = all (safe), 2 = intermediates truncate but the
|
|
// recurrent state and the kernel output still round, 0 = all truncate.
|
|
// Left at 1: truncating is 1.5% (BFR=2) / 2.2% (BFR=0) faster, but it takes the fp64-oracle
|
|
// error from 3.30e-03 (FLA parity) to 9.02e-03 / 1.17e-02, and the state error from
|
|
// 2.4e-03 to 5.1e-03 -- W/sA/V' feed the recurrence, so even BFR=2 pollutes the state.
|
|
#ifndef BFR
|
|
#define BFR 1
|
|
#endif
|
|
// GSPLIT: the split-decay form, e^{g_i-g_j} = e^{g_i-c}.e^{c-g_j} about the chunk midpoint,
|
|
// with both halves clamped at e^80. IT IS WRONG AND IT IS OFF; it stays reachable only so
|
|
// the failure it produces can be reproduced on demand. It is one exp per token instead of
|
|
// eight per tile, but a chunk whose gate span passes ~176 nats has the growing half clamped
|
|
// and the shrinking one at 0, and the terms it should have produced are attenuated to
|
|
// nothing. On real prefill activations that fires on 3.5% of (head, chunk) pairs and takes
|
|
// the fp64-oracle error from 3.4e-03 to 4.0e-02 - 7.2e-02, bimodally: a chunk is either
|
|
// untouched or wrecked. This is the factorisation the file header rules out.
|
|
#ifndef GSPLIT
|
|
#define GSPLIT 0
|
|
#endif
|
|
// PRIO: raise this wave's issue priority across the WMMA-dense region so it is not
|
|
// starved by waves in their memory phase.
|
|
#ifndef PRIO
|
|
#define PRIO 0
|
|
#endif
|
|
// SID: give each WMMA loop its own sched_group_barrier syncid, so one loop's group
|
|
// pattern cannot be satisfied by another loop's instructions when the scheduler puts
|
|
// them in the same region.
|
|
#ifndef SID
|
|
#define SID 0
|
|
#endif
|
|
// NOVM: leave the global q/A loads out of the group pattern and let the scheduler
|
|
// place them freely.
|
|
#ifndef NOVM
|
|
#define NOVM 0
|
|
#endif
|
|
#if SID
|
|
#define SIDF 1
|
|
#define SIDV 2
|
|
#define SIDO 3
|
|
#define SIDS 4
|
|
#else
|
|
#define SIDF 0
|
|
#define SIDV 0
|
|
#define SIDO 0
|
|
#define SIDS 0
|
|
#endif
|
|
#if PRIO
|
|
#define PRIO_HI() __builtin_amdgcn_s_setprio(1)
|
|
#define PRIO_LO() __builtin_amdgcn_s_setprio(0)
|
|
#else
|
|
#define PRIO_HI() ((void)0)
|
|
#define PRIO_LO() ((void)0)
|
|
#endif
|
|
#if BFR == 1
|
|
#define F2BF4I f2bf4 // W, sA, V' -- consumed inside one chunk
|
|
#define F2BF4O f2bf4 // s.S and o -- carried across chunks / returned
|
|
#elif BFR == 2
|
|
#define F2BF4I f2bf4t
|
|
#define F2BF4O f2bf4
|
|
#else
|
|
#define F2BF4I f2bf4t
|
|
#define F2BF4O f2bf4t
|
|
#endif
|
|
// LDS reads and WMMAs per k step of the main loop, for the sched groups below:
|
|
// one A fragment (K row mt) shared by every tile, plus S and K per n tile.
|
|
// One ds_load_2addr_b64 per LDS fragment (see fragP in r4d_gdn_wmma.h) -- these counts
|
|
// are what the sched groups below pattern on, and a count that does not match what the
|
|
// loop actually contains produces a WORSE schedule than no pattern at all.
|
|
#define MLDS (1 + 2 * NTV)
|
|
#define MWMA (3 * NTV)
|
|
// byte offset of buffer[base + lo][4*hi] from the base of the shared block
|
|
#define ROWO(buf, pitch, base) \
|
|
((unsigned)((const char*)&(buf)[0][0] - (const char*)&s) + \
|
|
(unsigned)((((base) + lo) * (pitch) + 4 * hi) * 2))
|
|
|
|
// SDT is the STATE CACHE's element type -- 0 = fp32, 1 = fp16 -- and nothing else: every value
|
|
// this kernel computes with is fp32 either way, and the recurrence still carries the state in
|
|
// WMMA accumulators across the whole chunk loop. It exists because vLLM allocates the mamba cache
|
|
// from --mamba-ssm-cache-dtype and the recurrent update, whose time IS the state traffic, wants
|
|
// the narrow one; the two kernels share the cache, so they have to agree on its width.
|
|
typedef _Float16 cs_h8 __attribute__((ext_vector_type(8)));
|
|
template <int SDT> struct cs_state { using T = float; };
|
|
template <> struct cs_state<1> { using T = _Float16; };
|
|
|
|
// H0 / HT say whether the optional initial and final state operands were passed. They are template
|
|
// parameters rather than runtime tests so that the form every caller in this tree launches -- both
|
|
// present -- carries no test for them; the absent forms are what libref reads the op as: no initial
|
|
// state starts the recurrence from zero, no final state stores nothing.
|
|
template <int SDT, bool H0 = true, bool HT = true>
|
|
__global__ __launch_bounds__(NTHR) void r4d_gdn_chunk_scan_kernel(
|
|
const unsigned short* __restrict__ q, const unsigned short* __restrict__ k,
|
|
const unsigned short* __restrict__ v, const unsigned short* __restrict__ Amat,
|
|
const float* __restrict__ gcs, const float* __restrict__ beta,
|
|
const typename cs_state<SDT>::T* __restrict__ h0, unsigned short* __restrict__ o,
|
|
typename cs_state<SDT>::T* __restrict__ ht, const int* __restrict__ cu,
|
|
const int* __restrict__ sidx, long sidx_stride, long st_slot, long st_head,
|
|
int H, int Hg, float scale)
|
|
{
|
|
__shared__ Smem s;
|
|
const int bv = blockIdx.x, hv = blockIdx.y, nq = blockIdx.z;
|
|
const int tid = threadIdx.x, w = tid >> 5, lane = tid & 31;
|
|
const int hq = hv / (H / Hg), v0 = bv * BV;
|
|
const int tb = cu[nq], te = cu[nq + 1];
|
|
// THE STATE SLOT IS THE MANAGER'S, NOT THE BATCH POSITION. Without `sidx` this kernel writes
|
|
// state[nq] -- correct only while a sequence's slot happens to equal its row in the batch,
|
|
// which holds until the first slot is reused out of order and then silently does not.
|
|
// COLUMN 0 IS THE STATE. A row may name more than one slot -- a decode kernel serving
|
|
// speculation asks for scratch beside the state -- but the scan writes the state and nothing
|
|
// else, so it reads the first column and ignores the width.
|
|
const long si = sidx ? (long)sidx[(size_t)nq * sidx_stride] : (long)nq;
|
|
if (si < 0) return; // a padded row owns no state
|
|
const size_t st_base = (size_t)si * st_slot + (size_t)hv * st_head;
|
|
const int nchunk = (te - tb + BT - 1) / BT;
|
|
const int lo = lane & 15, hi = lane >> 4;
|
|
|
|
#if NW == 8
|
|
const int mt = w >> 1, ntb = (w & 1) * 2; // [BT][BV]: 2 tiles/wave
|
|
#else
|
|
const int mt = w >> 2, ntb = (w & 3); // [BT][BV]: 1 tile/wave
|
|
#endif
|
|
// The state accumulator is held TRANSPOSED, C[m=k-dim][n=v]. WMMA A and B fragments
|
|
// share a lane layout, so swapping the operands of the state update is free -- and it
|
|
// makes k-dim the per-lane contiguous axis, so the per-chunk writeback to s.S is four
|
|
// packed 8-short stores instead of 32 scalar ds_store_b16.
|
|
#if SASG && NW == 8
|
|
const int kt0 = w, vt0 = 0; // [KD][BV]: 1 x 4 tiles/wave
|
|
#elif SASG && NW == 16
|
|
const int kt0 = (w >> 1), vt0 = (w & 1) * 2; // 1 x 2
|
|
#elif NW == 8
|
|
const int kt0 = (w >> 1) * 2, vt0 = (w & 1) * 2; // 2 x 2
|
|
#else
|
|
const int kt0 = (w >> 1), vt0 = (w & 1) * 2; // 1 x 2
|
|
#endif
|
|
|
|
#if WIDE
|
|
#define PFW 8 // shorts staged per lane per step
|
|
#define pfv_t uint4
|
|
#define PFZERO make_uint4(0u, 0u, 0u, 0u)
|
|
#else
|
|
#define PFW 4
|
|
#define pfv_t uint2
|
|
#define PFZERO make_uint2(0u, 0u)
|
|
#endif
|
|
#define NKB (BT * KD / (NTHR * PFW))
|
|
#define NVB (BT * BV / (NTHR * PFW))
|
|
pfv_t kb[NKB], vb[NVB];
|
|
// Staging index, split into a WAVE-UNIFORM row and a loop-invariant lane column.
|
|
// Written as `i = tid*4 + u*NTHR*4; r = i>>7` the row is opaquely divergent to LLVM,
|
|
// so each of the 12 loads rebuilt a full 64-bit address (v_mad_co_i64_i32 +
|
|
// v_lshlrev_b64 + 64-bit add) with the bounds test as exec-mask manipulation --
|
|
// ~13 instructions per 8-byte load. readfirstlane makes the row provably uniform, so
|
|
// the row address is SALU and the vector part is one loop-invariant register.
|
|
// Identities (exact, since u*NTHR*4 is a multiple of both 128 and 64):
|
|
// k: r = (tid>>5) + NW*u c = (tid*4) & 127
|
|
// v: r = 2*(tid>>5) + vh + 2*NW*u c = (tid*4) & 63 vh = (tid>>4) & 1
|
|
const int wu = __builtin_amdgcn_readfirstlane(tid >> 5);
|
|
const int kc = (tid * PFW) & (KD - 1), vc = (tid * PFW) & (BV - 1);
|
|
#if WIDE
|
|
const int kr0 = 2 * wu + ((tid >> 4) & 1), vr0 = 4 * wu + ((tid >> 3) & 3);
|
|
#define KRU (2 * NW)
|
|
#define VRU (4 * NW)
|
|
#else
|
|
const int kr0 = wu, vr0 = 2 * wu + ((tid >> 4) & 1);
|
|
#define KRU NW
|
|
#define VRU (2 * NW)
|
|
#endif
|
|
// ROWS PAST THE CHUNK END ARE ZEROED, AND CLAMPING THEM TO THE LAST LIVE ROW IS WRONG.
|
|
// DO NOT "OPTIMISE" THIS BACK TO A CLAMP. The apparent argument that a duplicated row
|
|
// cannot matter is:
|
|
// * beta[t>=nval] = 0, so W = beta*(V - ge*U) is 0 whatever V and U hold;
|
|
// * gv[t>=nval] = 0, and s.k is scaled by gv before the state update, so the
|
|
// contraction over t already drops those columns;
|
|
// * the tril mask kills j>i, so a padding j never reaches a live row of sA;
|
|
// * the O store is guarded by tok < nval.
|
|
// Every clause of that is true and the conclusion is false. With the clamp, any prompt
|
|
// whose trailing chunk is not a multiple of 16 tokens -- 65, 75, 81, 200 -- comes back as
|
|
// one token repeated, and the process never serves a correct answer again; 64, 80, 96,
|
|
// 112 and 128 stay right. Zeroing the staged rows makes all of them right, and zeroing k
|
|
// alone is enough with v still clamped -- so the leak is through a consumer of s.k, and
|
|
// the four clauses above cover every consumer of s.k this kernel has. One of them is
|
|
// therefore not the whole story. Until it is known which, the padding rows do not get to
|
|
// hold live data.
|
|
// PFV: 0 = divergent index, predicated 1 = wave-uniform + predicate
|
|
// 2 = wave-uniform + clamp 3 = divergent index + clamp
|
|
// All four produce bit-identical output where the clamp is safe. Variant 1 is ~12% faster than
|
|
// variant 0, and the CLAMPED variants only pay once the load is wide: at PFW=4 a clamp saves the
|
|
// selects but nothing else, while at PFW=8 it also removes the exec-mask manipulation around
|
|
// a 128-bit load, which is worth a further ~4%.
|
|
// The clamped variants are unreachable in the shipped configuration -- see the paragraph above --
|
|
// and are LEFT IN only as the comparison point for that cost.
|
|
#ifndef PFV
|
|
#define PFV 1
|
|
#endif
|
|
// 4 = the clamp applied to a HOISTED per-lane byte offset. Once a lane stages eight
|
|
// shorts the staging row is no longer wave-uniform (lanes 0-15 and 16-31 are different
|
|
// rows), so `r * (Hg*KD)` is a v_mul_lo_u32 per load per chunk. Clamping the OFFSET
|
|
// instead of the row is the same value -- both add the same column -- and lets the whole
|
|
// product be computed once, outside the chunk loop.
|
|
#if PFV == 0 || PFV == 3
|
|
#define PFROWK (i / KD)
|
|
#define PFROWV (i / BV)
|
|
#define PFCOLK (i & (KD - 1))
|
|
#define PFCOLV (i % BV)
|
|
#define PFIDX int i = tid * PFW + u * NTHR * PFW;
|
|
#else
|
|
#define PFROWK (kr0 + KRU * u)
|
|
#define PFROWV (vr0 + VRU * u)
|
|
#define PFCOLK kc
|
|
#define PFCOLV vc
|
|
#define PFIDX
|
|
#endif
|
|
#if PFV == 0 || PFV == 1
|
|
#define PFK(r, pnv, e) ((r) < (pnv) ? (e) : PFZERO)
|
|
/* The `(void)` is what keeps PREFETCH's `prl` from being an unused local in this variant -- it is
|
|
* the last row of the chunk, and only the unconditional-read variant below needs it to clamp with.
|
|
* Here the read is already guarded by PFK's `(r) < (pnv)`, so there is nothing to clamp.
|
|
* Discarding it in the macro rather than not declaring it keeps ONE PREFETCH body for both. */
|
|
#define PFCLAMP(r, prl) ((void)(prl), (r))
|
|
#else
|
|
#define PFK(r, pnv, e) (e)
|
|
#define PFCLAMP(r, prl) min((r), (prl))
|
|
#endif
|
|
#if PFV == 4
|
|
#define PFHOIST() \
|
|
const unsigned krs = (unsigned)(Hg * KD), vrs = (unsigned)(H * VD); \
|
|
unsigned koff0[NKB], voff0[NVB]; \
|
|
_Pragma("unroll") for (int u = 0; u < NKB; ++u) \
|
|
koff0[u] = (unsigned)(kr0 + KRU * u) * krs + (unsigned)kc; \
|
|
_Pragma("unroll") for (int u = 0; u < NVB; ++u) \
|
|
voff0[u] = (unsigned)(vr0 + VRU * u) * vrs + (unsigned)vc;
|
|
#else
|
|
#define PFHOIST()
|
|
#endif
|
|
#define PFSTORE(dst, val) \
|
|
do { \
|
|
unsigned short* dp_ = (dst); \
|
|
*(uint2*)dp_ = make_uint2((val).x, (val).y); \
|
|
if (PFW == 8) *(uint2*)(dp_ + 4) = make_uint2(*(&(val).x + 2), *(&(val).x + 3)); \
|
|
} while (0)
|
|
#if PFV == 4
|
|
// the chunk-uniform half, so the loads themselves are one v_min plus the access
|
|
#define PFBASE(CH) \
|
|
const int pc0_ = tb + (CH) * BT, prl_ = min(BT, te - pc0_) - 1; \
|
|
const unsigned short* kb_ = k + ((size_t)pc0_ * Hg + hq) * KD; \
|
|
const unsigned short* vb_ = v + ((size_t)pc0_ * H + hv) * VD + v0; \
|
|
const unsigned kcl = (unsigned)prl_ * krs + (unsigned)kc; \
|
|
const unsigned vcl = (unsigned)prl_ * vrs + (unsigned)vc;
|
|
#define PFONE(u) \
|
|
do { \
|
|
if ((u) < NKB) kb[(u) < NKB ? (u) : 0] = \
|
|
*(const pfv_t*)(kb_ + min(koff0[(u) < NKB ? (u) : 0], kcl)); \
|
|
else if ((u) - NKB < NVB) vb[(u) >= NKB ? (u) - NKB : 0] = \
|
|
*(const pfv_t*)(vb_ + min(voff0[(u) >= NKB ? (u) - NKB : 0], vcl)); \
|
|
} while (0)
|
|
#define PREFETCH(CH) \
|
|
{ \
|
|
PFBASE(CH) \
|
|
_Pragma("unroll") for (int u = 0; u < NKB + NVB; ++u) PFONE(u); \
|
|
}
|
|
#else
|
|
#define PREFETCH(CH) \
|
|
{ \
|
|
int pc0 = tb + (CH) * BT, pnv = min(BT, te - pc0), prl = pnv - 1; \
|
|
const unsigned short* kb_ = k + ((size_t)pc0 * Hg + hq) * KD; \
|
|
const unsigned short* vb_ = v + ((size_t)pc0 * H + hv) * VD + v0; \
|
|
_Pragma("unroll") for (int u = 0; u < NKB; ++u) { \
|
|
PFIDX int r0 = PFROWK, r = PFCLAMP(r0, prl); \
|
|
kb[u] = PFK(r0, pnv, \
|
|
*(const pfv_t*)(kb_ + (unsigned)(r * (Hg * KD)) + PFCOLK)); \
|
|
} \
|
|
_Pragma("unroll") for (int u = 0; u < NVB; ++u) { \
|
|
PFIDX int r0 = PFROWV, r = PFCLAMP(r0, prl); \
|
|
vb[u] = PFK(r0, pnv, \
|
|
*(const pfv_t*)(vb_ + (unsigned)(r * (H * VD)) + PFCOLV)); \
|
|
} \
|
|
}
|
|
#endif
|
|
|
|
v8f St[NST]; // state: NSK k-tiles x 2 v-tiles
|
|
#define KT(t) (kt0 + (t) / SVT)
|
|
#define VT(t) (vt0 + (t) % SVT)
|
|
#define SROW(t) ((size_t)(v0 + VT(t) * 16 + lo) * KD + KT(t) * 16 + 8 * hi)
|
|
if constexpr (!H0) {
|
|
#pragma unroll
|
|
for (int t = 0; t < NST; ++t)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) St[t][e] = 0.0f;
|
|
} else {
|
|
const typename cs_state<SDT>::T* p = h0 + st_base;
|
|
#pragma unroll
|
|
for (int t = 0; t < NST; ++t) {
|
|
// Eight elements a row-tile: two 16-byte accesses in fp32, ONE in fp16. SROW ends in
|
|
// 8*hi, so the fp16 arm is 16-byte aligned for the same reason the fp32 arm is.
|
|
if constexpr (SDT) {
|
|
const cs_h8 x = *(const cs_h8*)(p + SROW(t));
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) St[t][e] = (float)x[e];
|
|
} else {
|
|
const float4 a = *(const float4*)(p + SROW(t));
|
|
const float4 b = *(const float4*)(p + SROW(t) + 4);
|
|
St[t][0]=a.x; St[t][1]=a.y; St[t][2]=a.z; St[t][3]=a.w;
|
|
St[t][4]=b.x; St[t][5]=b.y; St[t][6]=b.z; St[t][7]=b.w;
|
|
}
|
|
}
|
|
}
|
|
#define SWRITE() \
|
|
_Pragma("unroll") for (int t = 0; t < NST; ++t) { \
|
|
unsigned short* sp = &s.S[VT(t) * 16 + lo][KT(t) * 16 + 8 * hi]; \
|
|
*(uint2*)sp = F2BF4O(St[t][0], St[t][1], St[t][2], St[t][3]); \
|
|
*(uint2*)(sp + 4) = F2BF4O(St[t][4], St[t][5], St[t][6], St[t][7]); \
|
|
}
|
|
SWRITE();
|
|
PFHOIST();
|
|
BAR();
|
|
PREFETCH(0);
|
|
|
|
const char* const sbase = (const char*)&s;
|
|
// scale rides inside the score exponent: exp(g_i + log(scale) - g_j) is the same
|
|
// instruction count as exp(g_i - g_j) and drops the eight multiplies per tile.
|
|
const float lgs = __logf(scale);
|
|
(void)lgs;
|
|
for (int ci = 0; ci < nchunk; ++ci) {
|
|
const int c0 = tb + ci * BT, nval = min(BT, te - c0);
|
|
const int lim = nval - 1;
|
|
const unsigned short* qg = q + ((size_t)c0 * Hg + hq) * KD;
|
|
const unsigned short* ag = Amat + ((size_t)c0 * H + hv) * BT;
|
|
const size_t qrs = (size_t)Hg * KD, ars = (size_t)H * BT;
|
|
const float gl = gcs[(size_t)(c0 + lim) * H + hv];
|
|
#if GSPLIT
|
|
const float cref = 0.5f * (gcs[(size_t)c0 * H + hv] + gl);
|
|
#endif
|
|
|
|
// Issued before the staging store so the LDS writes below cover their latency:
|
|
// only waves 0-1 (tid < BT) run this, and every other wave waits for them at (1).
|
|
float ggr = 0.f, btr = 0.f;
|
|
if (tid < BT) {
|
|
bool ok = tid < nval;
|
|
ggr = ok ? gcs[(size_t)(c0 + tid) * H + hv] : 0.f;
|
|
btr = ok ? beta[(size_t)(c0 + tid) * H + hv] : 0.f;
|
|
}
|
|
{ // consume the previous iteration's prefetch: the s_wait_loadcnt for these
|
|
// loads now sits a whole chunk of compute after they were issued
|
|
#pragma unroll
|
|
for (int u = 0; u < NKB; ++u) { PFIDX PFSTORE(&s.k[PFROWK][PFCOLK], kb[u]); }
|
|
#pragma unroll
|
|
for (int u = 0; u < NVB; ++u) { PFIDX PFSTORE(&s.D[PFROWV][PFCOLV], vb[u]); }
|
|
}
|
|
if (tid < BT) {
|
|
bool ok = tid < nval;
|
|
s.bt[tid] = btr;
|
|
s.ge[tid] = __expf(ggr);
|
|
#if GSPLIT
|
|
s.gg[tid] = ok ? scale * __expf(fminf(ggr - cref, 80.0f)) : 0.f; // rs
|
|
s.gv[tid] = ok ? __expf(fminf(cref - ggr, 80.0f)) : 0.f;
|
|
#else
|
|
// g is a within-chunk cumsum of log-sigmoid gates, so it is non-increasing and gl is
|
|
// its minimum: gl - ggr <= 0, and gs underflowing to 0 is the right answer for a token
|
|
// the chunk's own decay has already annihilated.
|
|
// A PADDING ROW GETS gl, NOT 0. The score exponent is exp(g_i - g_j + log scale) and the
|
|
// mask only removes j > i, so a padding row i (which has no gate of its own) is scored
|
|
// against LIVE j with a real, very negative g_j: at 0 that is exp(+400) = inf, written
|
|
// into s.A as bf16 and multiplied by V-prime. The row is discarded either way -- the O
|
|
// store is guarded by tok < nval -- but an inf that only exists because a lane had no
|
|
// token is not something to leave in a shared buffer. gl is the chunk minimum, so
|
|
// gl - g_j <= 0 and the exponent cannot leave (0, scale].
|
|
s.gg[tid] = ok ? ggr : gl;
|
|
s.gv[tid] = ok ? __expf(gl - ggr) : 0.f; // gs
|
|
#endif
|
|
}
|
|
BAR(); // (1)
|
|
#if PFIN && PFV == 4
|
|
PFBASE(min(ci + 1, nchunk - 1)) // issued one per k step in the loop below
|
|
#else
|
|
PREFETCH(min(ci + 1, nchunk - 1)); // issue only; consumed next iteration
|
|
#endif
|
|
|
|
#if APRE
|
|
v8bf apre[BT / 16];
|
|
{
|
|
#if GO
|
|
const unsigned aoh = rowGo((unsigned)ars, mt * 16, lim, lane);
|
|
#pragma unroll
|
|
for (int sk = 0; sk < BT / 16; ++sk) apre[sk] = fragGo(ag, aoh + sk * 16);
|
|
#else
|
|
const unsigned short* pah = rowG(ag, ars, mt * 16, lim, lane);
|
|
#pragma unroll
|
|
for (int sk = 0; sk < BT / 16; ++sk) apre[sk] = fragP(pah + sk * 16);
|
|
#endif
|
|
}
|
|
#endif
|
|
// ---- U = K@S, O^T = S@Q^T and P^T = K@Q^T share the q and k fragments.
|
|
// tril: a whole P tile is masked away when its j-tile is above the i-tile, and the
|
|
// O += sA@V' below skips the same tiles, so they need neither computing nor storing.
|
|
v8f O[NTV], u[NTV], p[NTV];
|
|
bool doP[NTV];
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
O[i] = (v8f){}; u[i] = (v8f){}; p[i] = (v8f){};
|
|
doP[i] = (TRIL ? ntb + i <= mt : true);
|
|
}
|
|
PRIO_HI();
|
|
{
|
|
#if LDSO
|
|
const char* sb = sbase;
|
|
unsigned oSn[NTV], okn[NTV];
|
|
unsigned okm = ROWO(s.k, KP, mt * 16);
|
|
LDSOPAQUE(okm);
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
oSn[i] = ROWO(s.S, KP, (ntb + i) * 16);
|
|
okn[i] = ROWO(s.k, KP, (ntb + i) * 16);
|
|
LDSOPAQUE(oSn[i]); LDSOPAQUE(okn[i]);
|
|
}
|
|
#define pSnf(i, ko) fragO(sb, oSn[i] + (ko) * 2u)
|
|
#define pkmf(ko) fragO(sb, okm + (ko) * 2u)
|
|
#define pknf(i, ko) fragO(sb, okn[i] + (ko) * 2u)
|
|
#else
|
|
const unsigned short* pSn[NTV];
|
|
const unsigned short* pkn[NTV];
|
|
const unsigned short* pkm = rowP(&s.k[0][0], KP, mt * 16, lane);
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
pSn[i] = rowP(&s.S[0][0], KP, (ntb + i) * 16, lane);
|
|
pkn[i] = rowP(&s.k[0][0], KP, (ntb + i) * 16, lane);
|
|
}
|
|
#define pSnf(i, ko) fragP(pSn[i] + (ko))
|
|
#define pkmf(ko) fragP(pkm + (ko))
|
|
#define pknf(i, ko) fragP(pkn[i] + (ko))
|
|
#endif
|
|
#if GO
|
|
const unsigned qo = rowGo((unsigned)qrs, mt * 16, lim, lane);
|
|
#define pqf(ko) fragGo(qg, qo + (ko))
|
|
#else
|
|
const unsigned short* pq = rowG(qg, qrs, mt * 16, lim, lane);
|
|
#define pqf(ko) fragP(pq + (ko))
|
|
#endif
|
|
#pragma unroll
|
|
for (int sk = 0; sk < KD / 16; ++sk) {
|
|
const int ko = sk * 16;
|
|
v8bf ak = pkmf(ko), aq = pqf(ko);
|
|
#if PFIN && PFV == 4
|
|
if (sk < NKB + NVB) PFONE(sk);
|
|
#endif
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
v8bf b = pSnf(i, ko);
|
|
#if ABL != 5
|
|
u[i] = mma(ak, b, u[i]);
|
|
#endif
|
|
#if ABL != 6
|
|
O[i] = mma(b, aq, O[i]); // O^T: m = v-tile, n = token-tile
|
|
#endif
|
|
#if ABL != 4
|
|
if (doP[i]) p[i] = mma(pknf(i, ko), aq, p[i]); // P^T: m = j
|
|
#endif
|
|
}
|
|
}
|
|
#if SCHED
|
|
// Ask for one k step's LDS reads to issue against the previous step's WMMAs.
|
|
// Per step: 5 LDS fragments = 10 ds_read2_b32, 1 global q fragment = 2 loads,
|
|
// 6 WMMA. Without this the scheduler sinks each read onto its consumer and
|
|
// every WMMA is preceded by a full s_wait_dscnt 0x0 drain.
|
|
#if SCHED == 4
|
|
// Explicit one-step software pipeline: ask for step i+1's operand reads to be
|
|
// issued BEFORE step i's WMMAs, so the wait in front of each WMMA group is for
|
|
// loads that have had a full WMMA group to land.
|
|
__builtin_amdgcn_sched_group_barrier(0x020, 2, 0);
|
|
__builtin_amdgcn_sched_group_barrier(0x100, MLDS, 0);
|
|
#pragma unroll
|
|
for (int i = 0; i < KD / 16 - 1; ++i) {
|
|
__builtin_amdgcn_sched_group_barrier(0x008, MWMA, 0); // WMMA step i
|
|
__builtin_amdgcn_sched_group_barrier(0x020, 2, 0); // reads step i+1
|
|
__builtin_amdgcn_sched_group_barrier(0x100, MLDS, 0);
|
|
}
|
|
__builtin_amdgcn_sched_group_barrier(0x008, MWMA, 0);
|
|
#elif SCHED == 5 || SCHED == 6 || SCHED == 7
|
|
// GRAN k steps per group. Coarser is better here: the group barrier PINS the
|
|
// order at every boundary, so a per-step pattern over-constrains a loop the
|
|
// scheduler can already interleave -- it just needs to be told not to sink every
|
|
// read onto its consumer.
|
|
#if SCHED == 5
|
|
#define GRAN 2
|
|
#elif SCHED == 6
|
|
#define GRAN 4
|
|
#else
|
|
#define GRAN 8
|
|
#endif
|
|
#pragma unroll
|
|
for (int i = 0; i < KD / (16 * GRAN); ++i) {
|
|
#if NOVM == 0
|
|
__builtin_amdgcn_sched_group_barrier(0x020, 2 * GRAN, SIDF);
|
|
#endif
|
|
__builtin_amdgcn_sched_group_barrier(0x100, MLDS * GRAN, SIDF);
|
|
__builtin_amdgcn_sched_group_barrier(0x008, MWMA * GRAN, SIDF);
|
|
}
|
|
#else
|
|
#pragma unroll
|
|
for (int i = 0; i < KD / 16; ++i) {
|
|
#if SCHED == 3 // finer: rotate reads through the WMMA stream
|
|
_Pragma("unroll") for (int j = 0; j < 3; ++j) {
|
|
__builtin_amdgcn_sched_group_barrier(0x100, MLDS / 2, 0);
|
|
__builtin_amdgcn_sched_group_barrier(0x008, MWMA / 2, 0);
|
|
}
|
|
#else
|
|
__builtin_amdgcn_sched_group_barrier(0x020, 2, 0); // VMEM read (q)
|
|
__builtin_amdgcn_sched_group_barrier(0x100, MLDS, 0); // DS read
|
|
__builtin_amdgcn_sched_group_barrier(0x008, MWMA, 0); // WMMA
|
|
#endif
|
|
}
|
|
#endif
|
|
#endif
|
|
}
|
|
PRIO_LO();
|
|
{
|
|
float g = scale * s.ge[mt * 16 + lo]; // token index is constant per lane
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) O[i][e] *= g;
|
|
}
|
|
// W = beta * (V - ge*U) (beta folded here: (A*beta_col)@W == A@(beta_row*W))
|
|
{
|
|
float wa[NTV][8];
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) { // beta / ge read once for both tiles
|
|
const int tok = mt * 16 + 8 * hi + e;
|
|
const float f = s.bt[tok], g = s.ge[tok];
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i)
|
|
wa[i][e] = f * (bf2f(s.D[tok][(ntb + i) * 16 + lo]) - g * u[i][e]);
|
|
}
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
unsigned short* wp = &s.W[(ntb + i) * 16 + lo][mt * 16 + 8 * hi];
|
|
*(uint2*)wp = F2BF4I(wa[i][0], wa[i][1], wa[i][2], wa[i][3]);
|
|
*(uint2*)(wp + 4) = F2BF4I(wa[i][4], wa[i][5], wa[i][6], wa[i][7]);
|
|
}
|
|
}
|
|
// sA = scale * e^{g_i-g_j} * tril(P). The row token ii is constant per lane and the
|
|
// eight g_j are contiguous and wave-uniform in groups of 16 lanes, so this is eight
|
|
// broadcast LDS reads, eight v_sub and eight v_exp per tile -- no multiply, because
|
|
// log(scale) is already inside the exponent. The mask makes g_i - g_j <= 0 wherever the
|
|
// value is kept, so the exp cannot overflow and its underflow is the true answer.
|
|
{
|
|
const int ii = mt * 16 + lo;
|
|
#if GSPLIT
|
|
const float r = s.gg[ii]; // rs
|
|
#else
|
|
const float gis = s.gg[ii] + lgs;
|
|
#endif
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
const int j0 = (ntb + i) * 16 + 8 * hi;
|
|
float a[8];
|
|
#if GSPLIT
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) a[e] = (j0 + e <= ii) ? p[i][e] * r : 0.f;
|
|
#else
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e)
|
|
a[e] = (j0 + e <= ii) ? p[i][e] * __expf(gis - s.gg[j0 + e]) : 0.f;
|
|
#endif
|
|
if (doP[i]) {
|
|
*(uint2*)&s.A[ii][j0] = F2BF4I(a[0], a[1], a[2], a[3]);
|
|
*(uint2*)&s.A[ii][j0 + 4] = F2BF4I(a[4], a[5], a[6], a[7]);
|
|
}
|
|
}
|
|
}
|
|
BAR(); // (2)
|
|
|
|
// ---- V' = gv * (A @ W) -> D (transposed store is one contiguous pair of dwords)
|
|
{
|
|
v8f d[NTV];
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) d[i] = (v8f){};
|
|
#if APRE
|
|
#define paf(ko) apre[(ko) / 16]
|
|
#elif GO
|
|
const unsigned ao = rowGo((unsigned)ars, mt * 16, lim, lane);
|
|
#define paf(ko) fragGo(ag, ao + (ko))
|
|
#else
|
|
const unsigned short* pa = rowG(ag, ars, mt * 16, lim, lane);
|
|
#define paf(ko) fragP(pa + (ko))
|
|
#endif
|
|
#if LDSO2
|
|
unsigned own[NTV];
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) { own[i] = ROWO(s.W, WP, (ntb + i) * 16);
|
|
LDSOPAQUE(own[i]); }
|
|
#define pwnf(i, ko) fragO(sbase, own[i] + (ko) * 2u)
|
|
#else
|
|
const unsigned short* pwn[NTV];
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) pwn[i] = rowP(&s.W[0][0], WP, (ntb + i) * 16, lane);
|
|
#define pwnf(i, ko) fragP(pwn[i] + (ko))
|
|
#endif
|
|
#pragma unroll
|
|
for (int sk = 0; sk < BT / 16; ++sk) {
|
|
if (TRIL && sk > mt) break; // A is unit lower triangular
|
|
const int ko = sk * 16;
|
|
v8bf a = paf(ko);
|
|
#if ABL != 3
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) d[i] = mma(a, pwnf(i, ko), d[i]);
|
|
#endif
|
|
}
|
|
#if SCHED >= 2 // per group: SB steps x (1 global A frag, NTV LDS, NTV WMMA)
|
|
#pragma unroll
|
|
for (int i = 0; i < BT / (16 * SB); ++i) {
|
|
#if NOVM == 0 && APRE == 0
|
|
__builtin_amdgcn_sched_group_barrier(0x020, 2 * SB, SIDV);
|
|
#endif
|
|
__builtin_amdgcn_sched_group_barrier(0x100, NTV * SB, SIDV);
|
|
__builtin_amdgcn_sched_group_barrier(0x008, NTV * SB, SIDV);
|
|
}
|
|
#endif
|
|
#if GSPLIT
|
|
float gvv[8];
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) gvv[e] = s.gv[mt * 16 + 8 * hi + e];
|
|
#endif
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
unsigned short* dp = &s.D[(ntb + i) * 16 + lo][mt * 16 + 8 * hi];
|
|
#if GSPLIT
|
|
*(uint2*)dp = F2BF4I(d[i][0]*gvv[0], d[i][1]*gvv[1],
|
|
d[i][2]*gvv[2], d[i][3]*gvv[3]);
|
|
*(uint2*)(dp + 4) = F2BF4I(d[i][4]*gvv[4], d[i][5]*gvv[5],
|
|
d[i][6]*gvv[6], d[i][7]*gvv[7]);
|
|
#else
|
|
*(uint2*)dp = F2BF4I(d[i][0], d[i][1], d[i][2], d[i][3]);
|
|
*(uint2*)(dp + 4) = F2BF4I(d[i][4], d[i][5], d[i][6], d[i][7]);
|
|
#endif
|
|
}
|
|
}
|
|
#if !GSPLIT
|
|
// K <- gs * K, in place, for the state's V'^T @ K. The state wants
|
|
// sum_j e^{gl-g_j} v'_j k_j^T, and (diag(gs) V')^T K == V'^T (diag(gs) K), so the
|
|
// per-token factor can ride on either operand; K is the one that is already dead.
|
|
// Every raw-K read -- U = K@S and P = Q@K^T -- retired before barrier (2), the state
|
|
// matmul is the only reader left, and s.k is restaged from the prefetch registers at
|
|
// the top of the next chunk, so the scaling dies with the chunk. Barrier (3) below
|
|
// publishes it. 32 elements per lane; the alternative, a second decayed copy of V',
|
|
// costs an extra workgroup barrier because s.S is the only buffer free to hold it and
|
|
// SWRITE races the state matmul for it.
|
|
#pragma unroll
|
|
for (int u = 0; u < BT * (KD / 4) / NTHR; ++u) {
|
|
const int idx = tid + u * NTHR;
|
|
const int t = idx / (KD / 4), c = (idx % (KD / 4)) * 4;
|
|
const float f = s.gv[t];
|
|
unsigned short* kp = &s.k[t][c];
|
|
*(uint2*)kp = F2BF4O(bf2f(kp[0]) * f, bf2f(kp[1]) * f,
|
|
bf2f(kp[2]) * f, bf2f(kp[3]) * f);
|
|
}
|
|
#endif
|
|
BAR(); // (3)
|
|
|
|
// ---- O += sA @ V' ; store. V' also feeds the state update, unchanged.
|
|
{
|
|
#if LDSO2
|
|
unsigned oA = ROWO(s.A, AP, mt * 16); // sA as [n=i][k=j]
|
|
unsigned odn[NTV];
|
|
LDSOPAQUE(oA);
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) { odn[i] = ROWO(s.D, AP, (ntb + i) * 16);
|
|
LDSOPAQUE(odn[i]); }
|
|
#define pAf(ko) fragO(sbase, oA + (ko) * 2u)
|
|
#define pdnf(i, ko) fragO(sbase, odn[i] + (ko) * 2u)
|
|
#else
|
|
const unsigned short* pA = rowP(&s.A[0][0], AP, mt * 16, lane);
|
|
const unsigned short* pdn[NTV];
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) pdn[i] = rowP(&s.D[0][0], AP, (ntb + i) * 16, lane);
|
|
#define pAf(ko) fragP(pA + (ko))
|
|
#define pdnf(i, ko) fragP(pdn[i] + (ko))
|
|
#endif
|
|
#pragma unroll
|
|
for (int sk = 0; sk < BT / 16; ++sk) {
|
|
if (TRIL && sk > mt) break; // sA is masked to lower triangular
|
|
const int ko = sk * 16;
|
|
v8bf b = pAf(ko);
|
|
#if ABL != 2
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) O[i] = mma(pdnf(i, ko), b, O[i]);
|
|
#endif
|
|
}
|
|
#if SCHED >= 2 // per group: SB steps x (1 + NTV LDS frags, NTV WMMA)
|
|
#pragma unroll
|
|
for (int i = 0; i < BT / (16 * SB); ++i) {
|
|
__builtin_amdgcn_sched_group_barrier(0x100, (1 + NTV) * SB, SIDO);
|
|
__builtin_amdgcn_sched_group_barrier(0x008, NTV * SB, SIDO);
|
|
}
|
|
#endif
|
|
}
|
|
{
|
|
int tok = mt * 16 + lo;
|
|
if (tok < nval) {
|
|
unsigned short* op = o + ((size_t)(c0 + tok) * H + hv) * VD + v0;
|
|
#pragma unroll
|
|
for (int i = 0; i < NTV; ++i) {
|
|
unsigned short* q2 = op + (ntb + i) * 16 + 8 * hi;
|
|
*(uint2*)q2 = F2BF4O(O[i][0], O[i][1], O[i][2], O[i][3]);
|
|
*(uint2*)(q2 + 4) = F2BF4O(O[i][4], O[i][5], O[i][6], O[i][7]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- S = e^{gl} * S + V'^T @ (gs*K) (B needs K as [dim][token] -> strided fragT)
|
|
{
|
|
#if GSPLIT
|
|
const float ec = __expf(cref), lam = __expf(gl - cref);
|
|
#else
|
|
const float ec = __expf(gl); // gl <= 0: no lam, the decay is already on K
|
|
#endif
|
|
#pragma unroll
|
|
for (int t = 0; t < NST; ++t)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) St[t][e] *= ec;
|
|
#if LDSO2
|
|
unsigned ovn[SVT];
|
|
#pragma unroll
|
|
for (int j = 0; j < SVT; ++j) { ovn[j] = ROWO(s.D, AP, (vt0 + j) * 16);
|
|
LDSOPAQUE(ovn[j]); } // V': n = v
|
|
#define pvnf(j, ko) fragO(sbase, ovn[j] + (ko) * 2u)
|
|
#else
|
|
const unsigned short* pvn[SVT];
|
|
#pragma unroll
|
|
for (int j = 0; j < SVT; ++j) pvn[j] = rowP(&s.D[0][0], AP, (vt0 + j) * 16, lane);
|
|
#define pvnf(j, ko) fragP(pvn[j] + (ko))
|
|
#endif
|
|
#pragma unroll
|
|
for (int sk = 0; sk < BT / 16; ++sk) {
|
|
v8bf bv[SVT];
|
|
#pragma unroll
|
|
for (int j = 0; j < SVT; ++j) bv[j] = pvnf(j, sk * 16);
|
|
#pragma unroll
|
|
for (int ki = 0; ki < SKT; ++ki) {
|
|
#if ABL == 7
|
|
v8bf ak = fragP(rowP(&s.k[0][0], KP, (kt0 + ki) * 16, lane) + sk * 16);
|
|
#else
|
|
v8bf ak = fragT(&s.k[0][0], KP, (kt0 + ki) * 16, sk * 16, lane); // K^T: m = k-dim
|
|
#endif
|
|
#if ABL != 1
|
|
#pragma unroll
|
|
for (int j = 0; j < SVT; ++j)
|
|
St[ki * SVT + j] = mma(ak, bv[j], St[ki * SVT + j]);
|
|
#else
|
|
(void)ak;
|
|
#endif
|
|
}
|
|
#if ABL == 1
|
|
#pragma unroll
|
|
for (int j = 0; j < SVT; ++j) (void)bv[j];
|
|
#endif
|
|
}
|
|
#if SCHED >= 2 // per group: SS steps x (SKT fragT = 8 scalar reads each + SVT fragP, NST WMMA)
|
|
#pragma unroll
|
|
for (int i = 0; i < BT / (16 * SS); ++i) {
|
|
__builtin_amdgcn_sched_group_barrier(0x100, (8 * SKT + SVT) * SS, SIDS);
|
|
__builtin_amdgcn_sched_group_barrier(0x008, NST * SS, SIDS);
|
|
}
|
|
#endif
|
|
#if GSPLIT
|
|
#pragma unroll
|
|
for (int t = 0; t < NST; ++t)
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) St[t][e] *= lam;
|
|
#endif
|
|
}
|
|
// no barrier: s.S aliases nothing, and barrier (2) already proves every wave
|
|
// finished its U/O reads of s.S this chunk
|
|
SWRITE();
|
|
BAR(); // (4)
|
|
}
|
|
|
|
if constexpr (!HT) return;
|
|
typename cs_state<SDT>::T* p = ht + st_base;
|
|
#pragma unroll
|
|
for (int t = 0; t < NST; ++t) {
|
|
if constexpr (SDT) {
|
|
cs_h8 x;
|
|
#pragma unroll
|
|
for (int e = 0; e < 8; ++e) x[e] = (_Float16)St[t][e];
|
|
*(cs_h8*)(p + SROW(t)) = x;
|
|
} else {
|
|
*(float4*)(p + SROW(t)) = make_float4(St[t][0], St[t][1], St[t][2], St[t][3]);
|
|
*(float4*)(p + SROW(t) + 4) = make_float4(St[t][4], St[t][5], St[t][6], St[t][7]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// THE SLOT-INDEXED FORM, and the one radiance calls. `sidx` names the state slot each row of the
|
|
// batch owns; without it the kernel indexes the cache by ROW, which is the same thing only while
|
|
// slots are handed out in batch order. `st_slot` and `st_head` are the strides of the cache
|
|
// itself, in elements: a manager that pads a linear slot to the attention page size (radiance
|
|
// does) has a slot wider than H*V*K, and the packed formula then reads the wrong memory for every
|
|
// slot but the first. Pass sidx = null and either stride 0 for the packed [N, H, V, K] layout.
|
|
extern "C" int r4d_gdn_chunk_scan_k128_v128_c64_bf16_sidx(
|
|
const void* q, const void* k, const void* v, const void* A, const void* g,
|
|
const void* beta, const void* h0, void* o, void* ht, const void* cu,
|
|
const void* ssm_state_indices, long indices_stride, long state_slot_stride,
|
|
long state_head_stride,
|
|
int N, int H, int Hg, int K, int V, int bt, float scale, int state_fp16, void* stream)
|
|
{
|
|
if (K != KD || V != VD || bt != BT) return -1;
|
|
// The kernel maps a v head to its k head with `hq = hv / (H / Hg)`, a truncating divide. An H
|
|
// that is not a multiple of Hg sends every v head to the wrong k head -- wrong numbers, no
|
|
// error. The two siblings that share the mapping refuse it with the same -2
|
|
// (r4d_gdn_kkt_solve_k128_c64_bf16, r4d_gdn_recurrent_update_k128_v128_bf16_fp32state).
|
|
if (Hg <= 0 || H % Hg) return -2;
|
|
const long st_head = state_head_stride > 0 ? state_head_stride : (long)VD * KD;
|
|
const long st_slot = state_slot_stride > 0 ? state_slot_stride : (long)H * VD * KD;
|
|
dim3 grid(V / BV, H, N), blk(NTHR);
|
|
#define CS_LAUNCH(SDTC, H0C, HTC) \
|
|
hipLaunchKernelGGL((r4d_gdn_chunk_scan_kernel<SDTC, H0C, HTC>), grid, blk, 0, \
|
|
(hipStream_t)stream, \
|
|
(const unsigned short*)q, (const unsigned short*)k, (const unsigned short*)v, \
|
|
(const unsigned short*)A, (const float*)g, (const float*)beta, \
|
|
(const cs_state<SDTC>::T*)h0, (unsigned short*)o, (cs_state<SDTC>::T*)ht, \
|
|
(const int*)cu, (const int*)ssm_state_indices, (indices_stride > 0 ? indices_stride : 1), \
|
|
st_slot, st_head, H, Hg, scale)
|
|
#define CS_PICK(SDTC) \
|
|
do { if (h0 && ht) CS_LAUNCH(SDTC, true, true); \
|
|
else if (h0) CS_LAUNCH(SDTC, true, false); \
|
|
else if (ht) CS_LAUNCH(SDTC, false, true); \
|
|
else CS_LAUNCH(SDTC, false, false); } while (0)
|
|
if (state_fp16) CS_PICK(1); else CS_PICK(0);
|
|
#undef CS_PICK
|
|
#undef CS_LAUNCH
|
|
return (int)hipGetLastError();
|
|
}
|
|
|
|
// The packed-layout entry point, for the out-of-tree vLLM caller: one state per batch row,
|
|
// [N, H, V, K].
|
|
extern "C" int r4d_gdn_chunk_scan_k128_v128_c64_bf16(
|
|
const void* q, const void* k, const void* v, const void* A, const void* g,
|
|
const void* beta, const void* h0, void* o, void* ht, const void* cu,
|
|
int N, int H, int Hg, int K, int V, int bt, float scale, int state_fp16, void* stream)
|
|
{
|
|
return r4d_gdn_chunk_scan_k128_v128_c64_bf16_sidx(q, k, v, A, g, beta, h0, o, ht, cu,
|
|
nullptr, 0, 0, 0, N, H, Hg, K, V, bt, scale,
|
|
state_fp16, stream);
|
|
}
|
|
|
|
// The geometry baked into the kernel above, so a caller can test a model's GDN shape against it
|
|
// instead of hardcoding a copy of these three numbers.
|
|
extern "C" void r4d_gdn_dims(int* head_k, int* head_v, int* chunk) {
|
|
if (head_k) *head_k = KD;
|
|
if (head_v) *head_v = VD;
|
|
if (chunk) *chunk = BT;
|
|
}
|
|
|
|
// ============================================================== the plugin ABI entry points
|
|
//
|
|
// The shared operand helpers are r4d_gdn_args.h. See libr4d/r4d_args.h for why there is no
|
|
// adapter directory between the core and these launches.
|
|
/* ------------------------------------------------------------------ chunk scan */
|
|
|
|
enum { CS_Q = 0, CS_K, CS_V, CS_A, CS_G, CS_BETA, CS_H0, CS_CU, CS_O, CS_HT, CS_SIDX };
|
|
|
|
extern "C" int r4d_rad_gdn_chunk_scan(const RadArgs* a, RadStream s) {
|
|
if (gdn_dims_ok() != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
|
const RadTensor* q = r4d_opt(a, CS_Q);
|
|
const RadTensor* k = r4d_opt(a, CS_K);
|
|
const RadTensor* v = r4d_opt(a, CS_V);
|
|
const RadTensor* A = r4d_opt(a, CS_A);
|
|
const RadTensor* g = r4d_opt(a, CS_G);
|
|
const RadTensor* bt = r4d_opt(a, CS_BETA);
|
|
const RadTensor* h0 = r4d_opt(a, CS_H0);
|
|
const RadTensor* cu = r4d_opt(a, CS_CU);
|
|
const RadTensor* o = r4d_opt(a, CS_O);
|
|
const RadTensor* ht = r4d_opt(a, CS_HT);
|
|
const RadTensor* sidx = r4d_opt(a, CS_SIDX);
|
|
if (!q || !k || !v || !A || !g || !bt || !cu || !o) return R4D_NO(RAD_E_INVAL);
|
|
if (q->rank != 3 || k->rank != 3 || v->rank != 3 || o->rank != 3) return R4D_NO(RAD_E_SHAPE);
|
|
if (A->rank != 3 || g->rank != 2 || bt->rank != 2) return R4D_NO(RAD_E_SHAPE);
|
|
|
|
int N = 0;
|
|
int rc = seqs_of(cu, &N);
|
|
if (rc != RAD_OK) return rc;
|
|
|
|
/* Everything is dense: the entry point takes no pitch at all. */
|
|
if (!r4d_contig(q) || !r4d_contig(k) || !r4d_contig(v) || !r4d_contig(A) ||
|
|
!r4d_contig(g) || !r4d_contig(bt) || !r4d_contig(o)) return R4D_NO(RAD_E_STRIDE);
|
|
|
|
/* "bf16 q/k/v/A, fp32 gate/beta/state, bf16 out, fp32 final state" -- this row's own dtype
|
|
* line, enforced here rather than merely described. */
|
|
R4D_DT(q, RAD_BF16); R4D_DT(k, RAD_BF16); R4D_DT(v, RAD_BF16); R4D_DT(A, RAD_BF16);
|
|
R4D_DT(o, RAD_BF16); R4D_DT(g, RAD_F32); R4D_DT(bt, RAD_F32);
|
|
/* THE STATE CACHE IS f32 OR f16 AND THE OPERAND SAYS WHICH. libr4d compiles both -- `SDT` is a
|
|
* template parameter on this kernel and on the decode recurrent update, and the state read and
|
|
* write ARE those kernels. Taking the width from a declared `state_fp16` parameter instead
|
|
* leaves half the instantiation unreachable unless something remembers to set it. The cache's
|
|
* dtype is the one honest source: it is a property of the allocation, and the two kernels that
|
|
* share the cache cannot disagree about it if both read it off the operand. */
|
|
const uint32_t st_dt = h0 ? h0->dtype : (ht ? ht->dtype : (uint32_t)RAD_F32);
|
|
if (st_dt != RAD_F32 && st_dt != RAD_F16) return R4D_NO(RAD_E_DTYPE);
|
|
if (h0) R4D_DT(h0, st_dt);
|
|
if (ht) R4D_DT(ht, st_dt);
|
|
/* THE SLOT STRIDES COME OFF THE TENSOR, NOT OFF THE SHAPE. A linear slot is padded to the
|
|
* attention page size (spec 7.2), so the cache is not dense in [N, H, V, K] and requiring
|
|
* contiguity here would refuse the very layout the manager hands out. What has to hold is
|
|
* that a head is dense and that h0 and ht agree, because the kernel reads and writes one
|
|
* base address for both. */
|
|
long long st_slot = 0, st_head = 0;
|
|
if (h0) {
|
|
if (h0->rank < 2) return R4D_NO(RAD_E_SHAPE);
|
|
st_slot = h0->stride[0];
|
|
st_head = h0->stride[1];
|
|
}
|
|
if (ht) {
|
|
if (ht->rank < 2) return R4D_NO(RAD_E_SHAPE);
|
|
if (h0 && (ht->stride[0] != st_slot || ht->stride[1] != st_head))
|
|
return R4D_NO(RAD_E_STRIDE);
|
|
st_slot = ht->stride[0];
|
|
st_head = ht->stride[1];
|
|
}
|
|
if (sidx) {
|
|
R4D_DT(sidx, RAD_I32);
|
|
if (sidx->rank < 1 || sidx->shape[0] != N) return R4D_NO(RAD_E_SHAPE);
|
|
if (sidx->stride[sidx->rank - 1] != 1) return R4D_NO(RAD_E_STRIDE);
|
|
}
|
|
|
|
const long long T = q->shape[0];
|
|
const long long Hg = q->shape[1];
|
|
const long long K = q->shape[2];
|
|
const long long H = v->shape[1];
|
|
const long long V = v->shape[2];
|
|
const long long bt_chunk = A->shape[2];
|
|
if (k->shape[0] != T || k->shape[1] != Hg || k->shape[2] != K) return R4D_NO(RAD_E_SHAPE);
|
|
if (v->shape[0] != T || o->shape[0] != T || o->shape[1] != H || o->shape[2] != V)
|
|
return R4D_NO(RAD_E_SHAPE);
|
|
if (A->shape[0] != T || A->shape[1] != H) return R4D_NO(RAD_E_SHAPE);
|
|
if (g->shape[0] != T || g->shape[1] != H) return R4D_NO(RAD_E_SHAPE);
|
|
if (bt->shape[0] != T || bt->shape[1] != H) return R4D_NO(RAD_E_SHAPE);
|
|
if (H == 0 || Hg == 0 || H % Hg) return R4D_NO(RAD_E_SHAPE);
|
|
|
|
const float scale = (float)r4d_getf(a, "scale", 1.0);
|
|
const int state_fp16 = st_dt == RAD_F16;
|
|
|
|
return r4d_gdn_chunk_scan_k128_v128_c64_bf16_sidx(
|
|
q->data, k->data, v->data, A->data, g->data, bt->data,
|
|
h0 ? h0->data : nullptr, o->data, ht ? ht->data : nullptr, cu->data,
|
|
sidx ? sidx->data : nullptr, sidx && sidx->rank >= 2 ? (long)sidx->stride[0] : 1,
|
|
(long)st_slot, (long)st_head,
|
|
N, (int)H, (int)Hg, (int)K, (int)V, (int)bt_chunk, scale, state_fp16, s);
|
|
}
|
|
|