/* r3d_dflash_f32.hip -- DFlash2's candidate walk, `dflash_select`. * * A block-diffusion drafter proposes every position of a block in one backbone pass, so position * l+1 is not conditioned on the drafter's choice at l. This op restores that dependence: it walks * the `steps` positions in order and at each picks the candidate that maximises * * score[l, c] = unary[l, c] + sum_r pred[pid(l)][r] * hp[l][r] * succ[cand[l, c]][r] * * where pid(0) is the anchor (the token the target just committed, at `anchor[m * anchor_stride]`) * and pid(l) is the walk's previous pick, `cand[l-1, chosen]`. With `hp` absent it is a plane of * ones and the form reduces to a low-rank bigram (DSpark's Markov head). This file transcribes the * arithmetic and order of libref's `ref_dflash_select`. * * Parallelism. The walk is serial in l, because step l's predecessor is the pick of step l-1; * scoring the whole [steps, K, K] edge tensor up front would compute K times the edges read. The * sequences and, within a step, the K candidates are parallel: a block takes `256 / top_k` * sequences, each lane scores one (sequence, candidate) edge, and one lane per sequence takes the * argmax. Each step costs two LDS round trips (scores out, pick back). * * Cost. At the band (steps=7, top_k=16, rank=256, M=256) a walk is 7 * 16 * 256 = 28672 terms, and * the op is a gather over `succ` [n_vocab, rank]: 66.8 MiB of codebook, of which the walk reads * M * steps * K = 28672 rows (about 14.7 MiB) plus one predecessor row per step. There is no * arithmetic to hide the gather behind, so a tree reduction gains nothing and would break the * agreement below. * * Agreement with the reference is bit-exact: * * - the rank sum is `sc = unary; for (i = 0; i < rank; ++i) sc += ax[i] * succ[i];`, ascending in * r, one accumulator, with `-ffp-contract=off` so no FMA changes a term's rounding; * - `ax[i] = pred[pid][i] * hp[l][i]` is rounded to f32 before it meets `succ`, as the reference * stores it in a float array first (with `hp` absent, `pred * 1.0f` is the same value); * - the argmax is a strict `>` over c ascending, so the earliest candidate wins a tie and a NaN * never displaces the incumbent; * - a predecessor outside the vocabulary contributes zero and a candidate outside it is scored by * `unary` alone (the reference's `pid >= 0 && pid < nv`, `tok >= 0 && tok < nv`), so padding * rows (a shard's unused rows, a block shorter than `steps`) are legal inputs; * - the pick of step l is written to `tokens[m, l]` and becomes pid(l+1) unchanged, even when it * is out of range for the codebooks. * * The shim refuses, none of it reachable from a row whose constraints hold: a codebook (`pred`, * `succ`, `hp`) or band `dtype` other than bf16, the one instantiation (RAD_E_DTYPE); `unary` not * f32 or `cand`/`anchor`/`tokens` not i32, as the schema types them (RAD_E_DTYPE); a codebook whose * rows are not contiguous, since the gather is `token * rank + r` (RAD_E_STRIDE); `top_k` above * 256, one lane per candidate (RAD_E_UNSUPPORTED); a `cand` plane describing more sequences than * the band's `M` (RAD_E_SHAPE, not a truncation). */ #include "r3d.h" #include "r3d_args.h" #include "r3d_codecs.h" #include "r3d_common.h" #include "r3d_launch.h" #include namespace { constexpr uint32_t kThr = 256; /* 8 waves: the block's (sequence, candidate) lanes */ constexpr uint32_t kKMax = kThr; /* one lane a candidate, so top_k <= 256 */ /* Whole-register loads of the planes the walk gathers through. bf16 is a 16-bit load decoded with * the library's codec, the same decode as libref's `ldt`. */ __device__ __forceinline__ int32_t ldg_i32(const int32_t* p) { return (int32_t)ldg_u32((const uint32_t*)p); } __device__ __forceinline__ float ldg_f32(const float* p) { return *(const float*)p; } __device__ __forceinline__ uint16_t ldg_u16(const uint16_t* p) { return *(const uint16_t*)p; } /* ------------------------------------------------------------------ the walk * * `per` sequences per block, `K` candidates per sequence; lane (s, c) owns the edge * (sequence s, candidate c) of the current step. `hp` may be null, and the factor is then 1.0f, as * in the reference's `(hp ? ldt(hp, ...) : 1.0f)`. */ __global__ __launch_bounds__(kThr) void dflash_select_kernel( const int32_t* __restrict__ cand, const float* __restrict__ un, const uint16_t* __restrict__ hp, const int32_t* __restrict__ anc, const uint16_t* __restrict__ pred, const uint16_t* __restrict__ succ, int32_t* __restrict__ out, uint32_t M, uint32_t steps, uint32_t K, uint32_t R, uint32_t nv, uint32_t astr, long long cand_ld, long long un_ld, long long hp_ld, long long pred_ld, long long succ_ld, long long tok_ld, uint32_t per) { __shared__ int32_t s_pid[kThr]; /* the walk's predecessor, one per sequence */ __shared__ float s_sc[kThr]; /* this step's K scores, one per lane */ const uint32_t s = threadIdx.x / K; /* the sequence this lane walks for */ const uint32_t c = threadIdx.x - s * K; /* the candidate this lane scores */ const uint32_t seq0 = blockIdx.x * per + s; /* A block of 256 lanes holds ceil(256 / K) sequences and the last group can be partial. A lane * with no sequence replays the last real one, so it reaches every __syncthreads() and its loads * stay in range; its stores are suppressed. */ const uint32_t seq = seq0 < M ? seq0 : M - 1; const bool mine = seq0 < M; /* pid(0): the anchor, at `m * anchor_stride` in a flat plane. As in the reference (`ld_int`), * a negative id means "no predecessor", not an error. */ int32_t pid = ldg_i32(anc + (size_t)seq * (size_t)astr); const bool has_hp = (hp != nullptr); for (uint32_t l = 0; l < steps; ++l) { const uint32_t r = seq * steps + l; /* cand/unary/hp are one row per (m, l) */ const size_t cb = (size_t)r * (size_t)cand_ld; const int32_t tok = ldg_i32(cand + cb + c); float sc = ldg_f32(un + (size_t)r * (size_t)un_ld + c); /* Out-of-vocabulary predecessor: no contribution. Out-of-vocabulary candidate: `unary` * alone. See the header. */ const bool have_p = pid >= 0 && (uint32_t)pid < nv; const bool have_s = tok >= 0 && (uint32_t)tok < nv; if (have_s) { const uint16_t* pp = pred + (size_t)(have_p ? pid : 0) * (size_t)pred_ld; const uint16_t* sp = succ + (size_t)tok * (size_t)succ_ld; const uint16_t* hpb = hp + (size_t)r * (size_t)hp_ld; for (uint32_t i = 0; i < R; ++i) { /* ax[i] is rounded to f32 before the multiply, as in the reference. */ const float ax = have_p ? bf16_to_f32(ldg_u16(pp + i)) * (has_hp ? bf16_to_f32(ldg_u16(hpb + i)) : 1.0f) : 0.0f; sc += ax * bf16_to_f32(ldg_u16(sp + i)); } } s_sc[threadIdx.x] = sc; /* == s_sc[s * K + c] */ __syncthreads(); if (c == 0) { /* Strict `>`, c ascending: the earliest candidate wins a tie, a NaN never leads. */ uint32_t best_c = 0; float best_s = -INFINITY; for (uint32_t j = 0; j < K; ++j) { const float v = s_sc[s * K + j]; if (v > best_s) { best_s = v; best_c = j; } } const int32_t pick = ldg_i32(cand + cb + best_c); if (mine) out[(size_t)seq * (size_t)tok_ld + l] = pick; s_pid[s] = pick; /* pid(l+1), out-of-range included */ } __syncthreads(); pid = s_pid[s]; } } /* ------------------------------------------------------------------ shim helpers */ /* The plane's row count (`rows`) and row pitch (`ld`) for rows of K elements. A rank-1 plane is a * run of rows K apart, as the reference's rowoff treats it. */ static inline int rows_pitch(const RadTensor* t, long long K, long long* rows, long long* ld) { const long long p = r3d_rows_pitch(t, K, rows); if (p < 0) return RAD_E_STRIDE; if (K <= 0 || *rows <= 0 || *rows > 0x7fffffffLL) return RAD_E_SHAPE; if (p < K) return RAD_E_SHAPE; *ld = p; return RAD_OK; } /* A codebook: [n, rank] bf16 with contiguous rows, because the gather is `token * rank + r`. */ static inline int codebook_ok(const RadTensor* t, long long rank) { if (!t || !t->data) return RAD_E_INVAL; if (!r3d_is_bf16(t)) return RAD_E_DTYPE; if (!r3d_aligned(r3d_p(t), 2)) return RAD_E_ALIGN; if (rank > 0 && t->rank >= 1 && r3d_stride(t, (int)t->rank - 1) != 1) return RAD_E_STRIDE; return RAD_OK; } static inline int r3d_launch_status(int rc) { if (rc == 0) return RAD_OK; switch (rc) { case -1: return RAD_E_INVAL; case -2: return RAD_E_SHAPE; case -3: return RAD_E_STRIDE; default: return RAD_E_DEVICE; } } } /* namespace */ #define R3D_NEED(x) \ do { const int _s = (x); if (_s != RAD_OK) return R3D_NO(_s); } while (0) /* ================================================================== launcher * * cand [M*steps][top_k] i32, candidate token ids, -1 for padding * unary [M*steps][top_k] f32, the candidate-independent half of the score * hp [M*steps][rank] bf16, the step's factor; 0 (absent) is a plane of ones * anchor [M*anchor_stride] i32, the token each walk starts from, at m*anchor_stride * pred [n_vocab][rank] bf16, the predecessor codebook * succ [n_vocab][rank] bf16, the successor codebook the walk gathers with its picks * tokens [M][steps] i32, out: the accepted prefix, one pick per step * * 0, -1 null, -2 bad extent, -3 pitch, -4 the HIP launch failed. No allocation, no synchronisation. */ extern "C" int r3d_dflash_select(long cand, long unary, long hp, long anchor, long pred, long succ, long tokens, int M, int steps, int top_k, int rank, int n_vocab, int anchor_stride, long long cand_ld, long long un_ld, long long hp_ld, long long pred_ld, long long succ_ld, long long tok_ld, long stream) { if (!cand || !unary || !anchor || !pred || !succ || !tokens) return -1; if (M <= 0 || steps <= 0 || top_k <= 0 || rank <= 0 || n_vocab <= 0 || anchor_stride <= 0) return -2; if ((unsigned)top_k > kKMax) return -2; /* one lane a candidate */ if (!cand_ld) cand_ld = top_k; if (!un_ld) un_ld = top_k; if (!hp_ld) hp_ld = rank; if (!pred_ld) pred_ld = rank; if (!succ_ld) succ_ld = rank; if (!tok_ld) tok_ld = steps; if (cand_ld < top_k || un_ld < top_k) return -3; if (hp_ld < rank || pred_ld < rank || succ_ld < rank) return -3; if (tok_ld < steps) return -3; const uint32_t per = (kThr + (uint32_t)top_k - 1) / (uint32_t)top_k; R3D_KLAUNCH(R3D_KNAME(dflash_select_kernel), dim3((M + per - 1) / per), dim3(kThr), (hipStream_t)(uintptr_t)stream, (const int32_t*)(uintptr_t)cand, (const float*)(uintptr_t)unary, (const uint16_t*)(uintptr_t)hp, (const int32_t*)(uintptr_t)anchor, (const uint16_t*)(uintptr_t)pred, (const uint16_t*)(uintptr_t)succ, (int32_t*)(uintptr_t)tokens, (uint32_t)M, (uint32_t)steps, (uint32_t)top_k, (uint32_t)rank, (uint32_t)n_vocab, (uint32_t)anchor_stride, cand_ld, un_ld, hp_ld, pred_ld, succ_ld, tok_ld, per); return hipGetLastError() == hipSuccess ? 0 : -4; } /* ================================================================== shim * * libref's operand list is {cand, unary, hp?, anchor, pred, succ, tokens} and its parameter list is * {M, steps, top_k, rank, n_vocab, anchor_stride?, dtype}. The sequence count comes from `cand` * (M * steps * top_k ids), with the band's M as a ceiling: a plane describing more sequences is * refused, not truncated. `rank` and `n_vocab` default to the predecessor codebook's shape, as in * the reference, and `anchor_stride` to 1. */ extern "C" int r3d_rad_dflash_select(const RadArgs* a, RadStream stream) { const RadTensor* cand = r3d_opt(a, 0); const RadTensor* un = r3d_opt(a, 1); const RadTensor* hp = r3d_opt(a, 2); /* OPD_O: absent is a plane of ones */ const RadTensor* anc = r3d_opt(a, 3); const RadTensor* pred = r3d_opt(a, 4); const RadTensor* succ = r3d_opt(a, 5); const RadTensor* tok = r3d_opt(a, 6); if (!cand || !cand->data || !un || !un->data || !anc || !anc->data || !pred || !pred->data || !succ || !succ->data || !tok || !tok->data) return R3D_NO(RAD_E_INVAL); /* The band names the codebooks' width; this is the bf16 instantiation. */ const char* dt = r3d_gets(a, "dtype"); if (dt && !r3d_dt_named(RAD_BF16, dt)) return R3D_NO(RAD_E_DTYPE); const long long steps = r3d_geti_or(a, "steps", 0); const long long K = r3d_geti_or(a, "top_k", 0); const long long astr = r3d_geti_or(a, "anchor_stride", 1); long long R = r3d_geti_or(a, "rank", pred->rank >= 2 ? r3d_dim(pred, (int)pred->rank - 1) : 0); long long nv = r3d_geti_or(a, "n_vocab", pred->rank >= 2 ? r3d_dim(pred, (int)pred->rank - 2) : 0); const long long band_M = r3d_geti_or(a, "M", 0); if (steps <= 0 || K <= 0 || astr <= 0 || R <= 0 || nv <= 0) return R3D_NO(RAD_E_SHAPE); if (K > (long long)kKMax) return R3D_NO(RAD_E_UNSUPPORTED); if (steps > 0x7fffffffLL / (K * 256) ) return R3D_NO(RAD_E_SHAPE); /* Sequences from the plane, the band as a ceiling. */ const long long all = steps * K; if (all <= 0 || r3d_numel(cand) % all) return R3D_NO(RAD_E_SHAPE); const long long M = r3d_numel(cand) / all; if (M <= 0 || (band_M > 0 && M > band_M)) return R3D_NO(RAD_E_SHAPE); /* The reference's extent tests, in its order. */ if (r3d_numel(un) < M * steps * K) return R3D_NO(RAD_E_SHAPE); if (r3d_numel(anc) < (M - 1) * astr + 1) return R3D_NO(RAD_E_SHAPE); if (r3d_numel(tok) < M * steps) return R3D_NO(RAD_E_SHAPE); if (r3d_numel(pred) < nv * R || r3d_numel(succ) < nv * R) return R3D_NO(RAD_E_SHAPE); if (hp && hp->data && r3d_numel(hp) < M * steps * R) return R3D_NO(RAD_E_SHAPE); if (!r3d_is_i32(cand)) return R3D_NO(RAD_E_DTYPE); if (!r3d_is_f32(un)) return R3D_NO(RAD_E_DTYPE); if (!r3d_is_i32(anc)) return R3D_NO(RAD_E_DTYPE); if (!r3d_is_i32(tok)) return R3D_NO(RAD_E_DTYPE); R3D_NEED(codebook_ok(pred, R)); R3D_NEED(codebook_ok(succ, R)); if (hp && hp->data) R3D_NEED(codebook_ok(hp, R)); if (!r3d_aligned(r3d_p(cand), 4) || !r3d_aligned(r3d_p(un), 4) || !r3d_aligned(r3d_p(anc), 4) || !r3d_aligned(r3d_p(tok), 4)) return R3D_NO(RAD_E_ALIGN); long long cand_ld = 0, un_ld = 0, tok_ld = 0, rows = 0; R3D_NEED(rows_pitch(cand, K, &rows, &cand_ld)); R3D_NEED(rows_pitch(un, K, &rows, &un_ld)); R3D_NEED(rows_pitch(tok, steps, &rows, &tok_ld)); long long hp_ld = R, pred_ld = R, succ_ld = R; if (hp && hp->data) R3D_NEED(rows_pitch(hp, R, &rows, &hp_ld)); R3D_NEED(rows_pitch(pred, R, &rows, &pred_ld)); R3D_NEED(rows_pitch(succ, R, &rows, &succ_ld)); return r3d_launch_status(r3d_dflash_select( r3d_addr(cand), r3d_addr(un), (hp && hp->data) ? r3d_addr(hp) : 0L, r3d_addr(anc), r3d_addr(pred), r3d_addr(succ), r3d_addr(tok), (int)M, (int)steps, (int)K, (int)R, (int)nv, (int)astr, cand_ld, un_ld, hp_ld, pred_ld, succ_ld, tok_ld, (long)(uintptr_t)stream)); }