Implement exact DSpark attention and Markov ops

Shared-anchor window attention and full-vocabulary greedy Markov as named
ops, with libref oracles. Register the anchor-window kernel constraints.
This commit is contained in:
Constantin committed 2026-11-03 13:56:45 -05:00
1 parent b43088a334
commit 340ff8781a
13 files changed
+723 -19

No files matched your search

+1
View File
@@ -201,6 +201,7 @@ set(R4D_UNITS
"r4d_lora_bf16|"
"r4d_dflash_conv_t2_g16_bf16|"
"r4d_dflash_select_bf16|"
"r4d_dflash_markov_bf16|"
"r4d_dflash_tree_bf16|"
# Routed MoE: router, scatter, the grouped block-scaled fp8 GEMM and gather, in one unit because
# the other three only feed the GEMM.
+32 -10
View File
@@ -194,6 +194,7 @@ struct AttnArgs {
int* tinfo; /* [tile] = (first row, positions) */
float qscale; /* softmax scale * log2(e) */
int max_blocks, n_seq, q_len, tps, kv_heads, causal, window, n_split;
int shared_window; /* all query rows use sequence anchor */
/* A tree verify (attn_paged_qrow_tree): RadBatch::spec_parent, one a query row; read only by
* the TREE instances. */
const int* tpar;
@@ -302,15 +303,16 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
const int qbase = ctx - qlen + pfirst; /* absolute position of the tile's first query */
const int plast = qbase + npos - 1;
const int causal = A.causal, window = A.window;
const int tbase = ctx - qlen; /* first query position, the block anchor */
const int window_base = A.shared_window ? tbase : ((TREE && A.tpar) ? tbase : qbase);
/* ---- the tile's key range, and this split's share of it */
int hi = causal ? plast + 1 : ctx;
if (!causal && window > 0) hi = imin(hi, plast + window);
int hi = causal ? (A.shared_window ? tbase + 1 : plast + 1) : ctx;
if (!causal && window > 0 && !A.shared_window) hi = imin(hi, plast + window);
hi = imin(imin(hi, ctx), A.max_blocks * 16);
/* A tree row's window is measured from the block's first position plus its depth, which can
* be below its own position, so a tree launch starts every tile at the block's first. */
const int tbase = ctx - qlen;
const int lo = window > 0 ? imax(0, ((TREE && A.tpar) ? tbase : qbase) - window + 1) : 0;
* be below its own position; an anchor-window launch also uses the first row for every query. */
const int lo = window > 0 ? imax(0, window_base - window + 1) : 0;
const int lo_a = lo & ~(BK - 1);
const int nkt = hi > lo_a ? (hi - lo_a + BK - 1) / BK : 0;
const int per = (nkt + A.n_split - 1) / A.n_split;
@@ -434,11 +436,14 @@ __device__ __forceinline__ void attn_body(AttnArgs A) {
reduce_scatter<8, BK>(sc, lane);
}
const int pq = pos[i];
bool vis = kok && (causal ? kk <= pq : true);
bool vis = kok && (causal ? kk <= (A.shared_window ? tbase : pq) : true);
if constexpr (TREE)
if (tr[i] && vis && kk >= tbase) vis = ((tm[i] >> (kk - tbase)) & 1ull) != 0;
const int wpq = TREE ? wq[i] : pq;
if (window > 0) vis = vis && (wpq - kk < window) && (causal || kk - wpq < window);
const int wpq = A.shared_window ? tbase : (TREE ? wq[i] : pq);
if (A.shared_window)
vis = vis && (wpq - kk < window);
else if (window > 0)
vis = vis && (wpq - kk < window) && (causal || kk - wpq < window);
const float sv = vis ? sc[0] : -__builtin_inff();
const float mn = __builtin_fmaxf(m[i], wave_max(sv));
if (mn == -__builtin_inff()) return; /* nothing visible yet: uniform */
@@ -1040,7 +1045,7 @@ __global__ __launch_bounds__(kThreads) void r4dchain_merge_window(AttnArgs A) {
r4d_chain_done();
}
template <int HD, int GQA, bool KV8, bool TREE = false>
template <int HD, int GQA, bool KV8, bool TREE = false, bool SHARED = false>
static int launch_attn(const RadArgs* a, RadStream stream) {
constexpr int R = Geo<HD>::R, BK = Geo<HD>::BK, BQ = R / GQA, ESZ = KV8 ? 1 : 2;
if (!a || a->n_t < (TREE ? 9 : 8)) return RAD_E_INVAL;
@@ -1137,7 +1142,8 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
if (qmax_w <= 0 || qmax_w > rows) qmax_w = rows;
}
/* Never a tree: its verify blocks are decode-sized, and only the scalar kernel reads one. */
const bool shaped = !TREE && qmax_w >= kWmmaMinQ && rows >= n_seq * (kWmmaMinQ / 2);
const bool shaped = !TREE && !SHARED && qmax_w >= kWmmaMinQ &&
rows >= n_seq * (kWmmaMinQ / 2);
const bool aligned = rad_ptr_aligned(q->data, 16) && rad_ptr_aligned(o->data, 16) &&
(q_rs | q_hs | o_rs | o_hs) % 8 == 0;
if (shaped && aligned) {
@@ -1171,6 +1177,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
W.kv_heads = (int)kvh;
W.causal = (int)causal;
W.window = (int)window;
W.shared_window = SHARED;
W.n_split = 1;
R4D_KLAUNCH(R4D_KNAME(attn_wmma_kernel<HD, GQA, KV8>), dim3((unsigned)ntw, (unsigned)kvh, 1u),
dim3(kWThreads), (hipStream_t)stream, W);
@@ -1214,6 +1221,7 @@ static int launch_attn(const RadArgs* a, RadStream stream) {
A.kv_heads = (int)kvh;
A.causal = (int)causal;
A.window = (int)window;
A.shared_window = SHARED;
A.n_split = (int)ns;
A.tpar = TREE ? (const int*)tp->data : nullptr;
@@ -1267,6 +1275,20 @@ R4D_WIN_TREE_DEF(256, 1) R4D_WIN_TREE_DEF(256, 2) R4D_WIN_TREE_DEF(256, 4) R4D_W
R4D_WIN_TREE_DEF(512, 1) R4D_WIN_TREE_DEF(512, 2) R4D_WIN_TREE_DEF(512, 4) R4D_WIN_TREE_DEF(512, 8)
#undef R4D_WIN_TREE_DEF
/* Exact DSpark mask: every query row uses the first query position as the sliding-window anchor.
* This is a distinct op name so a library without this semantic cannot silently resolve it. */
#define R4D_WIN_ANCHOR_DEF(HD, G) \
int r4d_rad_attn_anchor_window_h##HD##_gqa##G##_bf16kv(const RadArgs* a, RadStream s) { \
return r4d_win::launch_attn<HD, G, false, false, true>(a, s); \
} \
int r4d_rad_attn_anchor_window_h##HD##_gqa##G##_fp8kv(const RadArgs* a, RadStream s) { \
return r4d_win::launch_attn<HD, G, true, false, true>(a, s); \
}
R4D_WIN_ANCHOR_DEF(128, 1)
R4D_WIN_ANCHOR_DEF(256, 1) R4D_WIN_ANCHOR_DEF(256, 2) R4D_WIN_ANCHOR_DEF(256, 4) R4D_WIN_ANCHOR_DEF(256, 8)
R4D_WIN_ANCHOR_DEF(512, 1) R4D_WIN_ANCHOR_DEF(512, 2) R4D_WIN_ANCHOR_DEF(512, 4) R4D_WIN_ANCHOR_DEF(512, 8)
#undef R4D_WIN_ANCHOR_DEF
/* Called at declare with no tensors. Partials are capped at kPartBudget (tile, head, split)
* triples for any batch, so the size is a constant. */
int64_t r4d_rad_attn_window_h128_scratch(const RadArgs*) { return r4d_win::scratch_need<128>(); }
+191
View File
@@ -0,0 +1,191 @@
// Full-vocabulary DSpark Markov greedy walk. Unlike dflash_select's candidate-lattice walk, this
// operation applies the learned bigram bias to every draft-head logit and greedily selects the
// largest biased score at each position, matching the pinned llama.cpp reference.
#include <hip/hip_runtime.h>
#include <climits>
#include "r4d_gdn_args.h"
#include "r4d_dflash_conv_body.h" // r4d_bf2f, r4d_u16
#include "r4d_common.h" // r4d_wave_sum
namespace {
constexpr int kThreads = 256;
constexpr int kWave = 32;
constexpr int kWarps = kThreads / kWave;
constexpr int kCandidatesPerTile = 64;
constexpr int kCandidatesPerWarp = kCandidatesPerTile / kWarps;
constexpr int kMaxRank = 512;
// One CTA scans 64 vocabulary rows, eight at a time (one warp per row). The rank reduction is
// parallel across the warp and codebook loads remain contiguous. Each CTA writes its exact local
// argmax; a second kernel reduces those tile winners before the next dependent position starts.
__global__ __launch_bounds__(kThreads) void markov_score_tiles(
const r4d_u16* __restrict__ logits, long logits_pitch,
const int* __restrict__ anchor, long anchor_stride,
const r4d_u16* __restrict__ pred, const r4d_u16* __restrict__ succ,
float* __restrict__ tile_scores, int* __restrict__ tile_tokens,
const int* __restrict__ out, long out_pitch,
int step, int steps, int rank, int vocab, int n_tiles) {
const int seq = blockIdx.y;
const int tile = blockIdx.x;
const int tid = threadIdx.x;
const int lane = tid & (kWave - 1);
const int warp = tid / kWave;
const int prev = step == 0 ? anchor[(long)seq * anchor_stride]
: out[(long)seq * out_pitch + step - 1];
const bool prev_ok = prev >= 0 && prev < vocab;
__shared__ float s_pred[kMaxRank];
__shared__ float s_best[kWarps];
__shared__ int s_token[kWarps];
for (int r = tid; r < rank; r += kThreads)
s_pred[r] = prev_ok ? r4d_bf2f(pred[(long)prev * rank + r]) : 0.0f;
__syncthreads();
float warp_best = -INFINITY;
int warp_token = tile * kCandidatesPerTile + warp;
#pragma unroll
for (int j = 0; j < kCandidatesPerWarp; ++j) {
const int token = tile * kCandidatesPerTile + warp + j * kWarps;
if (token < vocab) {
float dot = 0.0f;
#pragma unroll 8
for (int r = lane; r < rank; r += kWave)
dot += s_pred[r] * r4d_bf2f(succ[(long)token * rank + r]);
dot = r4d_wave_sum(dot);
if (lane == 0) {
const float score = r4d_bf2f(logits[(long)seq * steps * logits_pitch +
(long)step * logits_pitch + token]) + dot;
if (score > warp_best || (score == warp_best && token < warp_token)) {
warp_best = score;
warp_token = token;
}
}
}
}
if (lane == 0) {
s_best[warp] = warp_best;
s_token[warp] = warp_token;
}
__syncthreads();
if (tid == 0) {
float best = -INFINITY;
int best_token = tile * kCandidatesPerTile;
#pragma unroll
for (int w = 0; w < kWarps; ++w) {
const float score = s_best[w];
const int token = s_token[w];
if (score > best || (score == best && token < best_token)) {
best = score;
best_token = token;
}
}
tile_scores[(long)seq * n_tiles + tile] = best;
tile_tokens[(long)seq * n_tiles + tile] = best_token;
}
}
__global__ __launch_bounds__(kThreads) void markov_reduce_tiles(
const float* __restrict__ tile_scores, const int* __restrict__ tile_tokens,
int* __restrict__ out, long out_pitch, int step, int n_tiles) {
const int seq = blockIdx.x;
const int tid = threadIdx.x;
__shared__ float s_best[kThreads];
__shared__ int s_token[kThreads];
float best = -INFINITY;
int best_token = INT_MAX;
for (int tile = tid; tile < n_tiles; tile += kThreads) {
const float score = tile_scores[(long)seq * n_tiles + tile];
const int token = tile_tokens[(long)seq * n_tiles + tile];
if (score > best || (score == best && token < best_token)) {
best = score;
best_token = token;
}
}
s_best[tid] = best;
s_token[tid] = best_token;
__syncthreads();
for (int stride = kThreads / 2; stride > 0; stride >>= 1) {
if (tid < stride) {
const float score = s_best[tid + stride];
const int token = s_token[tid + stride];
if (score > s_best[tid] || (score == s_best[tid] && token < s_token[tid])) {
s_best[tid] = score;
s_token[tid] = token;
}
}
__syncthreads();
}
if (tid == 0) out[(long)seq * out_pitch + step] = s_token[0];
}
int markov_emit(const r4d_u16* logits, long logits_pitch, const int* anchor, long anchor_stride,
const r4d_u16* pred, const r4d_u16* succ, float* tile_scores, int* tile_tokens,
int* out, long out_pitch, int sequences, int steps, int rank, int vocab,
int n_tiles, RadStream stream) {
for (int step = 0; step < steps; ++step) {
hipLaunchKernelGGL(markov_score_tiles, dim3(n_tiles, sequences), dim3(kThreads), 0,
(hipStream_t)stream, logits, logits_pitch, anchor, anchor_stride, pred,
succ, tile_scores, tile_tokens, out, out_pitch, step, steps, rank, vocab,
n_tiles);
hipLaunchKernelGGL(markov_reduce_tiles, dim3(sequences), dim3(kThreads), 0,
(hipStream_t)stream, tile_scores, tile_tokens, out, out_pitch, step,
n_tiles);
}
return (int)hipGetLastError();
}
} // namespace
extern "C" int r4d_rad_dflash_markov_greedy(const RadArgs* a, RadStream stream) {
constexpr int LOGITS = 0, ANCHOR = 1, PRED = 2, SUCC = 3;
constexpr int TILE_SCORE = 4, TILE_TOKEN = 5, OUT = 6;
const RadTensor* logits = r4d_opt(a, LOGITS);
const RadTensor* anchor = r4d_opt(a, ANCHOR);
const RadTensor* pred = r4d_opt(a, PRED);
const RadTensor* succ = r4d_opt(a, SUCC);
const RadTensor* scores = r4d_opt(a, TILE_SCORE);
const RadTensor* tokens = r4d_opt(a, TILE_TOKEN);
const RadTensor* out = r4d_opt(a, OUT);
if (!logits || !anchor || !pred || !succ || !scores || !tokens || !out)
return R4D_NO(RAD_E_INVAL);
const long long steps = r4d_geti_or(a, "steps", 0);
const long long rank = r4d_geti_or(a, "rank", 0);
const long long vocab = r4d_geti_or(a, "n_vocab", 0);
const long long anchor_stride = r4d_geti_or(a, "anchor_stride", 1);
if (steps <= 0 || rank < kWave || rank > kMaxRank || rank % kWave || vocab <= 0 ||
anchor_stride <= 0)
return R4D_NO(RAD_E_INVAL);
if (out->dtype != RAD_I32 || out->rank != 2 || out->shape[1] < steps)
return R4D_NO(RAD_E_SHAPE);
const long long sequences = out->shape[0];
const long long out_pitch = r4d_row_pitch(out);
const long long logits_pitch = r4d_row_pitch(logits);
if (sequences <= 0 || out_pitch < steps || logits->dtype != RAD_BF16 || logits->rank != 2 ||
logits->shape[0] < sequences * steps || logits->shape[1] < vocab || logits_pitch < vocab)
return R4D_NO(RAD_E_SHAPE);
if (anchor->dtype != RAD_I32 || !r4d_contig(anchor) ||
r4d_numel(anchor) < (sequences - 1) * anchor_stride + 1)
return R4D_NO(RAD_E_SHAPE);
if (pred->dtype != RAD_BF16 || succ->dtype != RAD_BF16 || !r4d_contig(pred) ||
!r4d_contig(succ) || r4d_numel(pred) < vocab * rank || r4d_numel(succ) < vocab * rank)
return R4D_NO(RAD_E_SHAPE);
const long long n_tiles = (vocab + kCandidatesPerTile - 1) / kCandidatesPerTile;
if (scores->dtype != RAD_F32 || tokens->dtype != RAD_I32 || scores->rank != 2 ||
tokens->rank != 2 || scores->shape[0] < sequences || tokens->shape[0] < sequences ||
scores->shape[1] < n_tiles || tokens->shape[1] < n_tiles ||
r4d_row_pitch(scores) < n_tiles || r4d_row_pitch(tokens) < n_tiles)
return R4D_NO(RAD_E_SHAPE);
return R4D_RAW(markov_emit((const r4d_u16*)logits->data, (long)logits_pitch,
(const int*)anchor->data, (long)anchor_stride,
(const r4d_u16*)pred->data, (const r4d_u16*)succ->data,
(float*)scores->data, (int*)tokens->data, (int*)out->data,
(long)out_pitch, (int)sequences, (int)steps, (int)rank,
(int)vocab, (int)n_tiles, stream));
}
+11
View File
@@ -188,6 +188,16 @@ int r4d_rad_attn_window_h512_gqa8_fp8 (const RadArgs*, RadStream);
int64_t r4d_rad_attn_window_h128_scratch(const RadArgs*);
int64_t r4d_rad_attn_window_h256_scratch(const RadArgs*);
int64_t r4d_rad_attn_window_h512_scratch(const RadArgs*);
#define R4D_ATTN_ANCHOR_DECL(HD, G) \
int r4d_rad_attn_anchor_window_h##HD##_gqa##G##_bf16kv(const RadArgs*, RadStream); \
int r4d_rad_attn_anchor_window_h##HD##_gqa##G##_fp8kv(const RadArgs*, RadStream);
R4D_ATTN_ANCHOR_DECL(128, 1)
R4D_ATTN_ANCHOR_DECL(256, 1) R4D_ATTN_ANCHOR_DECL(256, 2) R4D_ATTN_ANCHOR_DECL(256, 4)
R4D_ATTN_ANCHOR_DECL(256, 8)
R4D_ATTN_ANCHOR_DECL(512, 1) R4D_ATTN_ANCHOR_DECL(512, 2) R4D_ATTN_ANCHOR_DECL(512, 4)
R4D_ATTN_ANCHOR_DECL(512, 8)
#undef R4D_ATTN_ANCHOR_DECL
int r4d_rad_dflash_markov_greedy(const RadArgs*, RadStream);
/* attn_paged_qrow_tree (docs/OPS.md): the h256/gqa2 split-KV decode and the windowed kernels with a
* tree verify's mask from operand 8, `spec_parent`. */
int r4d_rad_attn_qrow_tree_decode_h256_gqa2_fp8 (const RadArgs*, RadStream);
@@ -1001,6 +1011,7 @@ int r4d_shape_qsa_work (const RadParam*, int, int operand, RadOpdDesc*
int r4d_shape_dflash_conv (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_dflash_select (const RadParam*, int, int operand, RadOpdDesc*);
int r4d_shape_dflash_markov (const RadParam*, int, int operand, RadOpdDesc*);
/* -- declared fusions (r4d_fuse.cpp, pure host) --
*
+65
View File
@@ -713,6 +713,20 @@ static const RadOperandSpec oDflashSelect[] = {
{ "succ", RAD_OPD_WEIGHT, 0 },
{ "tokens", RAD_OPD_OUT, 0 },
};
/* DSpark's exact, vocabulary-wide greedy Markov walk. Per-tile score/id planes are explicit
* outputs so the device implementation can parallelize each full-vocabulary step and reduce it
* before the next autoregressive step. */
static const RadParamSpec pDflashMarkov[] = {
{ "M", RAD_P_INT, RAD_REQUIRED }, { "steps", RAD_P_INT, RAD_REQUIRED },
{ "rank", RAD_P_INT, RAD_REQUIRED }, { "n_vocab", RAD_P_INT, RAD_REQUIRED },
{ "anchor_stride", RAD_P_INT, RAD_OPTIONAL }, { "dtype", RAD_P_STR, RAD_REQUIRED },
};
static const RadOperandSpec oDflashMarkov[] = {
{ "logits", RAD_OPD_IN, 0 }, { "anchor", RAD_OPD_IN, 0 },
{ "pred", RAD_OPD_WEIGHT, 0 }, { "succ", RAD_OPD_WEIGHT, 0 },
{ "tile_scores", RAD_OPD_OUT, 0 }, { "tile_tokens", RAD_OPD_OUT, 0 },
{ "tokens", RAD_OPD_OUT, 0 },
};
/* dflash_tree: dflash_select's, plus the node count, the softmax temperature and the parents. */
static const RadParamSpec pDflashTree[] = {
{ "M", RAD_P_INT, RAD_REQUIRED },
@@ -3259,6 +3273,8 @@ static const RadOpSchema kSchemas[] = {
"paged causal attention, varlen. The ranged parameter is q_len, not M: one query row "
"at decode, up to 64/gqa at a speculative verify, thousands in a prefill chunk, and "
"libr4d has a different kernel for each band."),
SCHEMA("attn_paged_anchor_window", pAttnPaged, oAttnPaged,
"paged attention whose query rows all use the sequence's first query as the sliding-window anchor."),
/* attn_paged under its own name, for q read at its own row pitch. The windowed kernels
* (r4d_attn_paged_window.hip) read q in place in a fused [q|k|v] projection; the other
* attn_paged kernels read q dense and refuse a slice (RAD_E_STRIDE). Under this name the op
@@ -3379,6 +3395,10 @@ static const RadOpSchema kSchemas[] = {
"positions, and the greedy walk that turns a block of independent top-k sets into one "
"path. Only the K edges leaving the previous pick are scored, because the walk is "
"serial and the other K-1 rows of the score matrix are never read."),
SCHEMA("dflash_markov_greedy", pDflashMarkov, oDflashMarkov,
"DSpark's exact greedy Markov walk: add the full-vocabulary bilinear bias to each "
"draft-head logit, choose the greatest token (ties to the lower id), then use it as the "
"next position's predecessor."),
SCHEMA("dflash_tree", pDflashTree, oDflashTree,
"dflash_select's lattice and edge scores grown best-first into a tree of `nodes` nodes: "
"a taken node's children are the valid candidates at the next position, each with log "
@@ -4209,6 +4229,16 @@ R4D_WIN_C(cWinH512G1Bf16, 512, 1, "bf16", 0) R4D_WIN_C(cWinH512G1Fp8, 512, 1, "f
R4D_WIN_C(cWinH512G2Bf16, 512, 2, "bf16", 0) R4D_WIN_C(cWinH512G2Fp8, 512, 2, "fp8_e4m3", 0)
R4D_WIN_C(cWinH512G4Bf16, 512, 4, "bf16", 0) R4D_WIN_C(cWinH512G4Fp8, 512, 4, "fp8_e4m3", 0)
R4D_WIN_C(cWinH512G8Bf16, 512, 8, "bf16", 0) R4D_WIN_C(cWinH512G8Fp8, 512, 8, "fp8_e4m3", 0)
/* DSpark uses the same geometry constraints, but requires a positive window. */
R4D_WIN_C(cAnchorH128G1Bf16, 128, 1, "bf16", 1) R4D_WIN_C(cAnchorH128G1Fp8, 128, 1, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH256G1Bf16, 256, 1, "bf16", 1) R4D_WIN_C(cAnchorH256G1Fp8, 256, 1, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH256G2Bf16, 256, 2, "bf16", 1) R4D_WIN_C(cAnchorH256G2Fp8, 256, 2, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH256G4Bf16, 256, 4, "bf16", 1) R4D_WIN_C(cAnchorH256G4Fp8, 256, 4, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH256G8Bf16, 256, 8, "bf16", 1) R4D_WIN_C(cAnchorH256G8Fp8, 256, 8, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH512G1Bf16, 512, 1, "bf16", 1) R4D_WIN_C(cAnchorH512G1Fp8, 512, 1, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH512G2Bf16, 512, 2, "bf16", 1) R4D_WIN_C(cAnchorH512G2Fp8, 512, 2, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH512G4Bf16, 512, 4, "bf16", 1) R4D_WIN_C(cAnchorH512G4Fp8, 512, 4, "fp8_e4m3", 1)
R4D_WIN_C(cAnchorH512G8Bf16, 512, 8, "bf16", 1) R4D_WIN_C(cAnchorH512G8Fp8, 512, 8, "fp8_e4m3", 1)
#undef R4D_WIN_C
/* Twelve queries per KV head, head_dim 256 (r4d_attn_paged_h256_gqa12.hip): Qwen3.8-Flash-Next's
* QSA layers, 24 query heads over 2. `q_len <= 5` is the same `q_len * gqa <= 64` bound, tighter
@@ -4622,6 +4652,10 @@ static const RadConstraint cDflashSelect[] = {
C_GE("steps", 1), C_GE("top_k", 1), C_LE("top_k", 32),
C_DIV("rank", 32), C_LE("rank", 512), C_IN("dtype", "bf16"),
};
static const RadConstraint cDflashMarkov[] = {
C_GE("steps", 1), C_DIV("rank", 32), C_LE("rank", 512),
C_GE("n_vocab", 1), C_IN("dtype", "bf16"),
};
/* dflash_tree's frontier (nodes x top_k children) and its node table live in LDS. */
static const RadConstraint cDflashTree[] = {
C_GE("steps", 1), C_LE("steps", 63), C_GE("top_k", 1), C_LE("top_k", 32),
@@ -7199,6 +7233,27 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
#undef R4D_WIN_PAIR
#undef R4D_WIN_ROW
/* DSpark's shared-anchor mask is intentionally a distinct op/kernel family, not an accepted
* `window` variation of attn_paged's per-query visibility. Its base/upper key bounds are tied to
* the sequence's first query row. */
#define R4D_ANCHOR_ROW(HD, G, KVN, KVS, C, KVDOC) \
{ "attn_anchor_window_h" #HD "_gqa" #G "_" #KVN, "attn_paged_anchor_window", "attn", \
"paged attention with the first query row as the shared sliding-window anchor", \
"head_dim " #HD ", " #G " queries per kv head, paged block 16, any q_len, varlen", \
"bf16 query, " KVDOC " kv cache, bf16 out, f32 partials", \
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_WINDOW, \
ROW(C), NOTUNE, \
nullptr, nullptr, r4d_rad_attn_anchor_window_h##HD##_gqa##G##_##KVS, \
r4d_rad_attn_window_h##HD##_scratch, nullptr, nullptr, r4d_shape_attn_paged },
#define R4D_ANCHOR_PAIR(HD, GQ) \
R4D_ANCHOR_ROW(HD, GQ, bf16kv, bf16kv, cAnchorH##HD##G##GQ##Bf16, "bf16") \
R4D_ANCHOR_ROW(HD, GQ, fp8kv, fp8kv, cAnchorH##HD##G##GQ##Fp8, "fp8-e4m3 (per-(seq,head) descales)")
R4D_ANCHOR_PAIR(128, 1)
R4D_ANCHOR_PAIR(256, 1) R4D_ANCHOR_PAIR(256, 2) R4D_ANCHOR_PAIR(256, 4) R4D_ANCHOR_PAIR(256, 8)
R4D_ANCHOR_PAIR(512, 1) R4D_ANCHOR_PAIR(512, 2) R4D_ANCHOR_PAIR(512, 4) R4D_ANCHOR_PAIR(512, 8)
#undef R4D_ANCHOR_PAIR
#undef R4D_ANCHOR_ROW
/* ...and under attn_paged_qrow_tree, the scalar kernel with a tree verify's mask (the launch
* refuses a non-causal one and never takes the WMMA kernel). */
#define R4D_WIN_TREE_ROW(HD, G, KVN, KVS, C, KVDOC) \
@@ -9541,6 +9596,16 @@ R4D_WIN_TREE_PAIR(512, 4) R4D_WIN_TREE_PAIR(512, 8)
nullptr, nullptr, r4d_rad_dflash_conv, nullptr, nullptr, nullptr,
r4d_shape_dflash_conv },
{ "dflash_markov_greedy_bf16", "dflash_markov_greedy", "dflash",
"Full-vocabulary DSpark Markov bias and serial greedy argmax; dependent positions each follow "
"a complete scan of the draft-head vocabulary",
"steps >= 1, rank a multiple of 32 and <= 512, 64 vocabulary ids per scratch tile",
"bf16 logits and codebooks, i32 anchor, f32/i32 tile winners, i32 tokens out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cDflashMarkov), NOTUNE,
nullptr, nullptr, r4d_rad_dflash_markov_greedy, nullptr, nullptr, nullptr,
r4d_shape_dflash_markov },
{ "dflash_select_bf16", "dflash_select", "dflash",
"DFlash2 candidate selector: the low-rank edge scores and the greedy walk over them, one "
"workgroup a sequence and no launch between steps",
+24
View File
@@ -2201,3 +2201,27 @@ extern "C" int r4d_shape_dflash_select(const RadParam* p, int n_p, int operand,
};
R4D_PICK(l);
}
extern "C" int r4d_shape_dflash_markov(const RadParam* p, int n_p, int operand,
RadOpdDesc* out) {
const int64_t M = pi(p, n_p, "M");
const int64_t steps = pi(p, n_p, "steps");
const int64_t R = pi(p, n_p, "rank");
const int64_t V = pi(p, n_p, "n_vocab");
const int64_t astr = pi(p, n_p, "anchor_stride", 1);
const uint32_t D = dt_act(p, n_p, "dtype");
if (M <= 0 || steps <= 0 || R <= 0 || V <= 0 || astr < 1)
return RAD_E_UNSUPPORTED;
const int64_t tiles = (V + 63) / 64;
const int64_t rows = M * steps;
const RadOpdDesc l[] = {
opd(D, rows, V),
opd_idx((M - 1) * astr + 1, 0, V),
opd(D, V, R),
opd(D, V, R),
opd((uint32_t)RAD_F32, M, tiles),
opd_idx(M, tiles, V),
opd_idx(M, steps, V),
};
R4D_PICK(l);
}