[HIP][Core] DSpark GPU drafter + verify perf & correctness fixes (13.93 tok/s, strata parity)

Fix all DSpark speculative bottlenecks and long-context/multi-turn bugs:
- Single-pass base-unembed Top-512 screen + on-GPU Markov loop & argmax (reads 922 MB unembed once per round, <=32 B D2H).
- On-GPU batched verify argmax + forward_batch_taps_no_logits (eliminates double unembed and 4.8 MB D2H per round).
- Wave32 online-softmax dspark_block_attn (0 bytes LDS, fixes >16.2k token crash on Global layer 1; regression-tested at c_len=17000).
- Vectorized M-tiled Wave32 router_gemv_batched, zero-scratch small-M SWA GQA, WMMA gemm_bf16 for m>=1, fdot2 MoE for b<=12.
- Persistent DsparkContext + PrefixCache DsparkSnapshot save/restore (multi-turn RESUME parity tested).
- Accepted-count EMA governor starting at n_draft=5 (adapts 2..7).
- Docs (README.md, docs/ORIENTATION.md) + changelog in review/2026-12-02-fix-drafter-e-ottimizzazioni.md.

Measured on gfx1150 (Inkling-2-Mini, temperature=0, 100% greedy EXACT):
- Rust coding (RingBuffer<T>, 128 tok): OFF 6.83 -> VETTA_SPEC=1 13.93 tok/s (2.04x, n_draft=5; strata-inkling 13.66 tok/s).
- Short prose (capital, 96 tok): nd=5 4.58 -> 9.09 tok/s (+98.5%); adaptive EMA 8.93 tok/s decode.
Validated: cargo test --workspace (-D warnings), all 7 Lean 4 proofs (0 sorryAx), HIP/LD/GW GPU parity gates.
This commit is contained in:
adm committed 2026-12-02 23:13:43 +01:00
1 parent 91c1baaf27
commit c1c5221642
9 files changed
+1433 -186

No files matched your search

+670 -54
View File
@@ -147,6 +147,20 @@ extern "C" int vetta_rms_norm_f32(const float* x, const float* w, float* y,
return 0;
}
// ---- BF16 & Wave32 helpers (shared across DSpark, router, MoE kernels) -----
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
return __uint_as_float(((uint32_t)h) << 16);
}
// DPP-accelerated wave32 sum (gfx11): 2 DPP moves + 3 shfl instead of 5 shfl.
__device__ __forceinline__ float wave32_sum(float v) {
v += __builtin_bit_cast(float, __builtin_amdgcn_mov_dpp(__builtin_bit_cast(int, v), 0xB1, 0xf, 0xf, true));
v += __builtin_bit_cast(float, __builtin_amdgcn_mov_dpp(__builtin_bit_cast(int, v), 0x4E, 0xf, 0xf, true));
v += __shfl_xor(v, 4, 32);
v += __shfl_xor(v, 8, 32);
v += __shfl_xor(v, 16, 32);
return v;
}
// ---- DSpark drafter: mix 4 taps + RMSNorm, and per-head RMSNorm + NeoX RoPE --
// Exact port of strata k_dspark_mix_taps / k_dspark_rmsnorm_rope (f32 throughout;
// the bf16 store happens in the caller). NeoX half-split pairs (d, d+hd/2).
@@ -254,9 +268,65 @@ extern "C" int vetta_dspark_rmsnorm_rope(float* x, const float* nw,
// Non-causal DSpark block attention: each of the B draft query slots attends to
// the committed ctx window [c0, c0+c_len) (bf16 ring, slot=pos%cap) PLUS ALL B
// draft keys/values (f32). GQA n_rep=H/KV. One block per (qi,hh); dynamic shared
// holds tot_k scores + hd reduction scratch. Exact port of the reference loop.
__device__ __forceinline__ float bf16_to_f32(uint16_t h); // defined below
// draft keys/values (f32). GQA n_rep=H/KV.
// Fast Wave32 online-softmax path for hd==128: ZERO shared memory scores array
// (eliminates the 64 KiB LDS crash on global layer 1 above ~16,248 tokens!) +
// vectorized float4/uint2 loads + DPP wave32_sum (~4-8x faster than scalar).
__global__ void __launch_bounds__(128) k_dspark_block_attn_w32(
const float* __restrict__ q, const float* __restrict__ kd,
const float* __restrict__ vd, const uint16_t* __restrict__ ck,
const uint16_t* __restrict__ cv, float* __restrict__ out,
int B, int H, int KV, int c0, int c_len, int cap,
float scale, int n_rep) {
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int hh = blockIdx.x * 4 + wave;
const int qi = blockIdx.y;
if (hh >= H || qi >= B) return;
const int d0 = lane * 4;
const int kvh = hh / n_rep;
const float4 qv = *reinterpret_cast<const float4*>(q + ((size_t)qi * H + hh) * 128 + d0);
const float q0 = qv.x * scale, q1 = qv.y * scale, q2 = qv.z * scale, q3 = qv.w * scale;
float max_l = -1e30f;
float sum_e = 0.0f;
float o0 = 0.0f, o1 = 0.0f, o2 = 0.0f, o3 = 0.0f;
const int tot = c_len + B;
for (int t = 0; t < tot; ++t) {
float k0, k1, k2, k3, v0, v1, v2, v3;
if (t < c_len) {
const int slot = ((c0 + t) % cap + cap) % cap;
const size_t idx = ((size_t)slot * KV + kvh) * 128 + d0;
const uint2 kw = *reinterpret_cast<const uint2*>(ck + idx);
const uint2 vw = *reinterpret_cast<const uint2*>(cv + idx);
k0 = bf16_to_f32(kw.x & 0xFFFFu); k1 = bf16_to_f32(kw.x >> 16);
k2 = bf16_to_f32(kw.y & 0xFFFFu); k3 = bf16_to_f32(kw.y >> 16);
v0 = bf16_to_f32(vw.x & 0xFFFFu); v1 = bf16_to_f32(vw.x >> 16);
v2 = bf16_to_f32(vw.y & 0xFFFFu); v3 = bf16_to_f32(vw.y >> 16);
} else {
const int di = t - c_len;
const size_t idx = ((size_t)di * KV + kvh) * 128 + d0;
const float4 kw = *reinterpret_cast<const float4*>(kd + idx);
const float4 vw = *reinterpret_cast<const float4*>(vd + idx);
k0 = kw.x; k1 = kw.y; k2 = kw.z; k3 = kw.w;
v0 = vw.x; v1 = vw.y; v2 = vw.z; v3 = vw.w;
}
const float sc = wave32_sum(fmaf(q0, k0, fmaf(q1, k1, fmaf(q2, k2, q3 * k3))));
const float nm = fmaxf(max_l, sc);
const float a = expf(max_l - nm);
const float b = expf(sc - nm);
o0 = fmaf(o0, a, b * v0); o1 = fmaf(o1, a, b * v1);
o2 = fmaf(o2, a, b * v2); o3 = fmaf(o3, a, b * v3);
sum_e = fmaf(sum_e, a, b);
max_l = nm;
}
const float inv = 1.0f / fmaxf(sum_e, 1e-30f);
*reinterpret_cast<float4*>(out + ((size_t)qi * H + hh) * 128 + d0) =
make_float4(o0 * inv, o1 * inv, o2 * inv, o3 * inv);
}
// Generic fallback for synthetic hd != 128 tests.
__global__ void k_dspark_block_attn(
const float* __restrict__ q, const float* __restrict__ kd,
const float* __restrict__ vd, const uint16_t* __restrict__ ck,
@@ -277,7 +347,7 @@ __global__ void k_dspark_block_attn(
for (int t = tid; t < tot_k; t += nt) {
float dot = 0.f;
if (t < c_len) {
const uint16_t* kp = ck + (((size_t)((c0 + t) % cap) * KV + kvh) * hd);
const uint16_t* kp = ck + (((size_t)(((c0 + t) % cap + cap) % cap) * KV + kvh) * hd);
for (int j = 0; j < hd; ++j) dot += qv[j] * bf16_to_f32(kp[j]);
} else {
const float* kp = kd + (((size_t)(t - c_len) * KV + kvh) * hd);
@@ -313,7 +383,7 @@ __global__ void k_dspark_block_attn(
for (int d = tid; d < hd; d += nt) {
float acc = 0.f;
for (int t = 0; t < c_len; ++t) {
const uint16_t* vp = cv + (((size_t)((c0 + t) % cap) * KV + kvh) * hd);
const uint16_t* vp = cv + (((size_t)(((c0 + t) % cap + cap) % cap) * KV + kvh) * hd);
acc += sc[t] * inv * bf16_to_f32(vp[d]);
}
for (int di = 0; di < B; ++di) {
@@ -331,9 +401,16 @@ extern "C" int vetta_dspark_block_attn(
float scale, vetta_stream_t stream) {
if (B <= 0 || H <= 0 || KV <= 0 || hd <= 0 || hd > 128 || H % KV != 0) return 1;
const int n_rep = H / KV;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
if (hd == 128) {
dim3 grid((H + 3) / 4, B);
k_dspark_block_attn_w32<<<grid, 128, 0, s>>>(q, kd, vd, ck, cv, out, B, H, KV,
c0, c_len, cap, scale, n_rep);
HIP_CHECK(hipGetLastError());
return 0;
}
const int tot_k = c_len + B;
const size_t sh = (size_t)(tot_k + hd) * 4;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_dspark_block_attn<<<B * H, hd, sh, s>>>(q, kd, vd, ck, cv, out, B, H, KV,
hd, c0, c_len, cap, scale, n_rep);
HIP_CHECK(hipGetLastError());
@@ -477,11 +554,7 @@ extern "C" int vetta_dspark_scatter_bf16(const float* src, uint16_t* dst,
return 0;
}
// ---- BF16 helpers ----------------------------------------------------------
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
return __uint_as_float(((uint32_t)h) << 16);
}
// ---- BF16/FP16 rounding helpers (bf16_to_f32 defined at top) ---------------
// round-to-nearest-even f32 -> bf16 -> f32 (exact strata f32_to_bf16 round trip).
__device__ __forceinline__ float bf16_round(float f) {
@@ -1080,58 +1153,98 @@ extern "C" int vetta_gather_channel(const float* msgs, float* dst, int ch, int B
// (NOT bf16-rounded) against the bf16 gate weight promoted to f32. Rounding the
// router input to bf16 flips top-k expert selection -> large MoE divergence.
__global__ void k_router_gemv(const float* __restrict__ x,
const uint16_t* __restrict__ w,
float* __restrict__ y, int N, int K) {
const int n = blockIdx.x;
// ---- k_router_gemv & k_router_gemv_batched: Wave32 vectorized F32 x BF16 ----
// 8 waves/block (1 wave per output row n), uint4 (8 BF16) + 2x float4 loads,
// DPP wave32_sum (zero shared memory). Batched path tiles M by 8 so each wave
// reads W[n,:] ONCE across up to 8 tokens (8x less weight bandwidth for DSpark
// B=8 layers and trunk router!), while preserving the exact per-(m,n) lane
// accumulation order of k_router_gemv (bit-exact parity).
__global__ void __launch_bounds__(256) k_router_gemv(
const float* __restrict__ x, const uint16_t* __restrict__ w,
float* __restrict__ y, int N, int K) {
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int n = blockIdx.x * 8 + wave;
if (n >= N) return;
const int tid = threadIdx.x;
const int nt = blockDim.x;
const uint16_t* wr = w + (size_t)n * K;
const int k8 = K & ~7;
float acc = 0.f;
for (int kk = tid; kk < K; kk += nt) acc += x[kk] * bf16_to_f32(wr[kk]);
__shared__ float red[256];
red[tid] = acc;
__syncthreads();
for (int s = nt / 2; s > 0; s >>= 1) { if (tid < s) red[tid] += red[tid + s]; __syncthreads(); }
if (tid == 0) y[n] = red[0];
for (int kk = lane * 8; kk < k8; kk += 256) {
const uint4 w8 = *reinterpret_cast<const uint4*>(wr + kk);
const float4 x0 = *reinterpret_cast<const float4*>(x + kk);
const float4 x1 = *reinterpret_cast<const float4*>(x + kk + 4);
acc = fmaf(bf16_to_f32(w8.x & 0xFFFFu), x0.x, fmaf(bf16_to_f32(w8.x >> 16), x0.y, acc));
acc = fmaf(bf16_to_f32(w8.y & 0xFFFFu), x0.z, fmaf(bf16_to_f32(w8.y >> 16), x0.w, acc));
acc = fmaf(bf16_to_f32(w8.z & 0xFFFFu), x1.x, fmaf(bf16_to_f32(w8.z >> 16), x1.y, acc));
acc = fmaf(bf16_to_f32(w8.w & 0xFFFFu), x1.z, fmaf(bf16_to_f32(w8.w >> 16), x1.w, acc));
}
for (int kk = k8 + lane; kk < K; kk += 32) {
acc = fmaf(bf16_to_f32(wr[kk]), x[kk], acc);
}
acc = wave32_sum(acc);
if (lane == 0) y[n] = acc;
}
extern "C" int vetta_router_gemv(const float* x, const uint16_t* w, float* y,
int N, int K, vetta_stream_t stream) {
if (N <= 0 || K <= 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_router_gemv<<<N, 256, 0, s>>>(x, w, y, N, K);
k_router_gemv<<<(N + 7) / 8, 256, 0, s>>>(x, w, y, N, K);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- k_router_gemv_batched: f32 router logits for M tokens ------------------
// x [M,K] f32 (router reads F32 mlpin, exact strata launch_f32_linear), w [N,K]
// bf16, y [M,N]. grid (N, M); block (n,m) reduces x[m,:]·w[n,:].
__global__ void k_router_gemv_batched(const float* __restrict__ x,
const uint16_t* __restrict__ w,
float* __restrict__ y, int N, int K, int M) {
const int n = blockIdx.x;
const int m = blockIdx.y;
if (n >= N || m >= M) return;
const int tid = threadIdx.x;
const int nt = blockDim.x;
__global__ void __launch_bounds__(256) k_router_gemv_batched(
const float* __restrict__ x, const uint16_t* __restrict__ w,
float* __restrict__ y, int N, int K, int M) {
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int n = blockIdx.x * 8 + wave;
const int m0 = blockIdx.y * 8;
if (n >= N || m0 >= M) return;
const int tm = (M - m0 < 8) ? (M - m0) : 8;
const uint16_t* wr = w + (size_t)n * K;
const float* xr = x + (size_t)m * K;
float acc = 0.f;
for (int kk = tid; kk < K; kk += nt) acc += xr[kk] * bf16_to_f32(wr[kk]);
__shared__ float red[256];
red[tid] = acc;
__syncthreads();
for (int s = nt / 2; s > 0; s >>= 1) { if (tid < s) red[tid] += red[tid + s]; __syncthreads(); }
if (tid == 0) y[(size_t)m * N + n] = red[0];
const int k8 = K & ~7;
float acc[8] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
for (int kk = lane * 8; kk < k8; kk += 256) {
const uint4 w8 = *reinterpret_cast<const uint4*>(wr + kk);
const float f0 = bf16_to_f32(w8.x & 0xFFFFu), f1 = bf16_to_f32(w8.x >> 16);
const float f2 = bf16_to_f32(w8.y & 0xFFFFu), f3 = bf16_to_f32(w8.y >> 16);
const float f4 = bf16_to_f32(w8.z & 0xFFFFu), f5 = bf16_to_f32(w8.z >> 16);
const float f6 = bf16_to_f32(w8.w & 0xFFFFu), f7 = bf16_to_f32(w8.w >> 16);
#pragma unroll
for (int mi = 0; mi < 8; ++mi) {
if (mi < tm) {
const float* xr = x + (size_t)(m0 + mi) * K + kk;
const float4 x0 = *reinterpret_cast<const float4*>(xr);
const float4 x1 = *reinterpret_cast<const float4*>(xr + 4);
acc[mi] = fmaf(f0, x0.x, fmaf(f1, x0.y, acc[mi]));
acc[mi] = fmaf(f2, x0.z, fmaf(f3, x0.w, acc[mi]));
acc[mi] = fmaf(f4, x1.x, fmaf(f5, x1.y, acc[mi]));
acc[mi] = fmaf(f6, x1.z, fmaf(f7, x1.w, acc[mi]));
}
}
}
for (int kk = k8 + lane; kk < K; kk += 32) {
const float fv = bf16_to_f32(wr[kk]);
#pragma unroll
for (int mi = 0; mi < 8; ++mi) {
if (mi < tm) acc[mi] = fmaf(fv, x[(size_t)(m0 + mi) * K + kk], acc[mi]);
}
}
#pragma unroll
for (int mi = 0; mi < 8; ++mi) {
if (mi < tm) {
const float sum = wave32_sum(acc[mi]);
if (lane == 0) y[(size_t)(m0 + mi) * N + n] = sum;
}
}
}
extern "C" int vetta_router_gemv_batched(const float* x, const uint16_t* w, float* y,
int N, int K, int M, vetta_stream_t stream) {
if (N <= 0 || K <= 0 || M <= 0) return 1;
dim3 grid(N, M);
dim3 grid((N + 7) / 8, (M + 7) / 8);
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_router_gemv_batched<<<grid, 256, 0, s>>>(x, w, y, N, K, M);
HIP_CHECK(hipGetLastError());
@@ -1228,15 +1341,7 @@ __device__ __forceinline__ float fdot2_f32(uint32_t a, uint32_t b, float c) {
return __builtin_amdgcn_fdot2(__builtin_bit_cast(__half2, a),
__builtin_bit_cast(__half2, b), c, false);
}
// DPP-accelerated wave32 sum (gfx11): 2 DPP moves + 3 shfl instead of 5 shfl.
__device__ __forceinline__ float wave32_sum(float v) {
v += __builtin_bit_cast(float, __builtin_amdgcn_mov_dpp(__builtin_bit_cast(int, v), 0xB1, 0xf, 0xf, true));
v += __builtin_bit_cast(float, __builtin_amdgcn_mov_dpp(__builtin_bit_cast(int, v), 0x4E, 0xf, 0xf, true));
v += __shfl_xor(v, 4, 32);
v += __shfl_xor(v, 8, 32);
v += __shfl_xor(v, 16, 32);
return v;
}
// (wave32_sum defined at top of file)
// ---- RDNA3.5 (gfx11) FP16 WMMA tensor-core helpers -------------------------
// 16x16x16 f16->f32 matrix tile. The f16 product is exact in f32; only the
@@ -3080,4 +3185,515 @@ extern "C" int vetta_flash_swa_gqa_prefill_wmma(const float* q, const uint16_t*
sink_logit, gate, M);
HIP_CHECK(hipGetLastError());
return 0;
}
}
// ============================================================================
// Fast On-GPU DSpark Drafting & Batched Verify Argmax (zero mid-loop host syncs)
// ============================================================================
// Single-pass base-unembed Top-2-per-block screen across ALL n_draft <= 7 steps!
// Reads unembed_bf16[V, D] ONCE (~922 MB) instead of n_draft times (~4.6-6.5 GB).
// cur_steps [n_draft, D] (f32) is <= 64 KB so it stays L1/L2 resident with zero
// LDS pressure and exact F32 precision. Grid = 256 blocks -> 512 candidates/step.
// When V <= 512 (unit tests), scores all V tokens directly into cand_val/cand_id.
__global__ void __launch_bounds__(256) k_dspark_base_top2_screen(
const uint16_t* __restrict__ unembed_bf16, // [V, D]
const float* __restrict__ cur_steps, // [n_draft, D] (cur + D)
float* __restrict__ cand_val, // [n_draft, n_cand]
int32_t* __restrict__ cand_id, // [n_draft, n_cand]
int n_draft, int D, int V, int n_cand) {
const int tid = threadIdx.x;
const int wave = tid >> 5;
const int lane = tid & 31;
const int k8 = D & ~7;
if (V <= n_cand) {
// Small-vocab exact mode (e.g. unit test V=512): score every token v < V.
const int v = blockIdx.x * 8 + wave;
if (v >= n_cand) return;
if (v >= V) {
if (lane == 0) {
for (int t = 0; t < n_draft; ++t) {
cand_val[(size_t)t * n_cand + v] = -1e30f;
cand_id[(size_t)t * n_cand + v] = -1;
}
}
return;
}
const uint16_t* wr = unembed_bf16 + (size_t)v * D;
float acc[7] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
for (int kk = lane * 8; kk < k8; kk += 256) {
const uint4 w8 = *reinterpret_cast<const uint4*>(wr + kk);
const float f0 = bf16_to_f32(w8.x & 0xFFFFu), f1 = bf16_to_f32(w8.x >> 16);
const float f2 = bf16_to_f32(w8.y & 0xFFFFu), f3 = bf16_to_f32(w8.y >> 16);
const float f4 = bf16_to_f32(w8.z & 0xFFFFu), f5 = bf16_to_f32(w8.z >> 16);
const float f6 = bf16_to_f32(w8.w & 0xFFFFu), f7 = bf16_to_f32(w8.w >> 16);
#pragma unroll
for (int t = 0; t < 7; ++t) {
if (t < n_draft) {
const float* cr = cur_steps + (size_t)t * D + kk;
const float4 x0 = *reinterpret_cast<const float4*>(cr);
const float4 x1 = *reinterpret_cast<const float4*>(cr + 4);
acc[t] = fmaf(f0, x0.x, fmaf(f1, x0.y, acc[t]));
acc[t] = fmaf(f2, x0.z, fmaf(f3, x0.w, acc[t]));
acc[t] = fmaf(f4, x1.x, fmaf(f5, x1.y, acc[t]));
acc[t] = fmaf(f6, x1.z, fmaf(f7, x1.w, acc[t]));
}
}
}
for (int kk = k8 + lane; kk < D; kk += 32) {
const float fv = bf16_to_f32(wr[kk]);
#pragma unroll
for (int t = 0; t < 7; ++t) {
if (t < n_draft) acc[t] = fmaf(fv, cur_steps[(size_t)t * D + kk], acc[t]);
}
}
#pragma unroll
for (int t = 0; t < 7; ++t) {
if (t < n_draft) {
const float z = wave32_sum(acc[t]);
if (lane == 0) {
cand_val[(size_t)t * n_cand + v] = z;
cand_id[(size_t)t * n_cand + v] = v;
}
}
}
return;
}
float best1_v[7], best2_v[7];
int32_t best1_i[7], best2_i[7];
#pragma unroll
for (int t = 0; t < 7; ++t) {
best1_v[t] = -1e30f; best2_v[t] = -1e30f;
best1_i[t] = -1; best2_i[t] = -1;
}
const int stride = gridDim.x * 8;
for (int v = blockIdx.x * 8 + wave; v < V; v += stride) {
const uint16_t* wr = unembed_bf16 + (size_t)v * D;
float acc[7] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
for (int kk = lane * 8; kk < k8; kk += 256) {
const uint4 w8 = *reinterpret_cast<const uint4*>(wr + kk);
const float f0 = bf16_to_f32(w8.x & 0xFFFFu), f1 = bf16_to_f32(w8.x >> 16);
const float f2 = bf16_to_f32(w8.y & 0xFFFFu), f3 = bf16_to_f32(w8.y >> 16);
const float f4 = bf16_to_f32(w8.z & 0xFFFFu), f5 = bf16_to_f32(w8.z >> 16);
const float f6 = bf16_to_f32(w8.w & 0xFFFFu), f7 = bf16_to_f32(w8.w >> 16);
#pragma unroll
for (int t = 0; t < 7; ++t) {
if (t < n_draft) {
const float* cr = cur_steps + (size_t)t * D + kk;
const float4 x0 = *reinterpret_cast<const float4*>(cr);
const float4 x1 = *reinterpret_cast<const float4*>(cr + 4);
acc[t] = fmaf(f0, x0.x, fmaf(f1, x0.y, acc[t]));
acc[t] = fmaf(f2, x0.z, fmaf(f3, x0.w, acc[t]));
acc[t] = fmaf(f4, x1.x, fmaf(f5, x1.y, acc[t]));
acc[t] = fmaf(f6, x1.z, fmaf(f7, x1.w, acc[t]));
}
}
}
for (int kk = k8 + lane; kk < D; kk += 32) {
const float fv = bf16_to_f32(wr[kk]);
#pragma unroll
for (int t = 0; t < 7; ++t) {
if (t < n_draft) acc[t] = fmaf(fv, cur_steps[(size_t)t * D + kk], acc[t]);
}
}
#pragma unroll
for (int t = 0; t < 7; ++t) {
if (t < n_draft) {
const float z = wave32_sum(acc[t]);
if (lane == 0) {
if (z > best1_v[t]) {
best2_v[t] = best1_v[t]; best2_i[t] = best1_i[t];
best1_v[t] = z; best1_i[t] = v;
} else if (z > best2_v[t]) {
best2_v[t] = z; best2_i[t] = v;
}
}
}
}
}
__shared__ float s_v[7][16];
__shared__ int32_t s_i[7][16];
if (lane == 0) {
for (int t = 0; t < n_draft; ++t) {
s_v[t][wave * 2 + 0] = best1_v[t]; s_i[t][wave * 2 + 0] = best1_i[t];
s_v[t][wave * 2 + 1] = best2_v[t]; s_i[t][wave * 2 + 1] = best2_i[t];
}
}
__syncthreads();
if (tid < n_draft) {
float b1v = -1e30f, b2v = -1e30f;
int32_t b1i = -1, b2i = -1;
for (int j = 0; j < 16; ++j) {
const float z = s_v[tid][j];
const int32_t id = s_i[tid][j];
if (z > b1v) {
b2v = b1v; b2i = b1i;
b1v = z; b1i = id;
} else if (z > b2v) {
b2v = z; b2i = id;
}
}
cand_val[(size_t)tid * n_cand + blockIdx.x * 2 + 0] = b1v;
cand_id[(size_t)tid * n_cand + blockIdx.x * 2 + 0] = b1i;
cand_val[(size_t)tid * n_cand + blockIdx.x * 2 + 1] = b2v;
cand_id[(size_t)tid * n_cand + blockIdx.x * 2 + 1] = b2i;
}
}
extern "C" int vetta_dspark_base_top2_screen(
const uint16_t* unembed_bf16, const float* cur_steps,
float* cand_val, int32_t* cand_id,
int n_draft, int D, int V, int n_cand, vetta_stream_t stream) {
if (n_draft <= 0 || n_draft > 7 || D <= 0 || V <= 0 || n_cand <= 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
const int n_blks = (V <= n_cand) ? ((n_cand + 7) / 8) : (n_cand / 2);
k_dspark_base_top2_screen<<<n_blks, 256, 0, s>>>(
unembed_bf16, cur_steps, cand_val, cand_id, n_draft, D, V, n_cand);
HIP_CHECK(hipGetLastError());
return 0;
}
// On-GPU Markov RNN projection + GRU state update (zero host round-trips!).
// Reads *prev_tok_ptr on device, gathers markov_w1[prev_tok] (BF16) and
// frnn_ring[frnn_slot] (BF16), computes 3*rank rows of rnn_proj_w (BF16).
__global__ void __launch_bounds__(256) k_dspark_markov_proj_bf16(
const int32_t* __restrict__ prev_tok_ptr, // [1] device token id
const float* __restrict__ state_in, // [rank]
float* __restrict__ state_out, // [rank]
float* __restrict__ latent_out, // [rank]
const float* __restrict__ cur_step, // [D] f32
const uint16_t* __restrict__ frnn_ring, // [frnn_cap, D] bf16
int frnn_slot,
const uint16_t* __restrict__ markov_w1, // [V_pad, rank] bf16
const uint16_t* __restrict__ rnn_proj_w, // [3*rank, 2*rank + 2*D] bf16
const float* __restrict__ rnn_proj_b, // [3*rank] f32
int D, int rank) {
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int r = blockIdx.x * 8 + wave;
if (r >= rank) return;
const int prev_tok = *prev_tok_ptr;
const int in_dim = 2 * rank + 2 * D;
const uint16_t* mw_row = markov_w1 + (size_t)prev_tok * rank;
const uint16_t* fr_row = frnn_ring + (size_t)frnn_slot * D;
const uint16_t* w0 = rnn_proj_w + (size_t)r * in_dim;
const uint16_t* w1 = rnn_proj_w + (size_t)(rank + r) * in_dim;
const uint16_t* w2 = rnn_proj_w + (size_t)(2 * rank + r) * in_dim;
auto dot4_bf_f4 = [](uint2 w4, float4 xv, float acc) -> float {
acc = fmaf(bf16_to_f32(w4.x & 0xFFFFu), xv.x, acc);
acc = fmaf(bf16_to_f32(w4.x >> 16), xv.y, acc);
acc = fmaf(bf16_to_f32(w4.y & 0xFFFFu), xv.z, acc);
acc = fmaf(bf16_to_f32(w4.y >> 16), xv.w, acc);
return acc;
};
auto dot4_bf_bf = [](uint2 w4, uint2 x4, float acc) -> float {
acc = fmaf(bf16_to_f32(w4.x & 0xFFFFu), bf16_to_f32(x4.x & 0xFFFFu), acc);
acc = fmaf(bf16_to_f32(w4.x >> 16), bf16_to_f32(x4.x >> 16), acc);
acc = fmaf(bf16_to_f32(w4.y & 0xFFFFu), bf16_to_f32(x4.y & 0xFFFFu), acc);
acc = fmaf(bf16_to_f32(w4.y >> 16), bf16_to_f32(x4.y >> 16), acc);
return acc;
};
float d0 = 0.f, d1 = 0.f, d2 = 0.f;
for (int i = lane * 4; i < rank; i += 128) {
const float4 sv = *reinterpret_cast<const float4*>(state_in + i);
const uint2 mv = *reinterpret_cast<const uint2*>(mw_row + i);
d0 = dot4_bf_f4(*reinterpret_cast<const uint2*>(w0 + i), sv,
dot4_bf_bf(*reinterpret_cast<const uint2*>(w0 + rank + i), mv, d0));
d1 = dot4_bf_f4(*reinterpret_cast<const uint2*>(w1 + i), sv,
dot4_bf_bf(*reinterpret_cast<const uint2*>(w1 + rank + i), mv, d1));
d2 = dot4_bf_f4(*reinterpret_cast<const uint2*>(w2 + i), sv,
dot4_bf_bf(*reinterpret_cast<const uint2*>(w2 + rank + i), mv, d2));
}
const int off_h = 2 * rank;
const int off_c = 2 * rank + D;
for (int i = lane * 4; i < D; i += 128) {
const float4 hv = *reinterpret_cast<const float4*>(cur_step + i);
const uint2 cv = *reinterpret_cast<const uint2*>(fr_row + i);
d0 = dot4_bf_f4(*reinterpret_cast<const uint2*>(w0 + off_h + i), hv,
dot4_bf_bf(*reinterpret_cast<const uint2*>(w0 + off_c + i), cv, d0));
d1 = dot4_bf_f4(*reinterpret_cast<const uint2*>(w1 + off_h + i), hv,
dot4_bf_bf(*reinterpret_cast<const uint2*>(w1 + off_c + i), cv, d1));
d2 = dot4_bf_f4(*reinterpret_cast<const uint2*>(w2 + off_h + i), hv,
dot4_bf_bf(*reinterpret_cast<const uint2*>(w2 + off_c + i), cv, d2));
}
d0 = wave32_sum(d0) + rnn_proj_b[r];
d1 = wave32_sum(d1) + rnn_proj_b[rank + r];
d2 = wave32_sum(d2) + rnn_proj_b[2 * rank + r];
if (lane == 0) {
const float gate = 1.0f / (1.0f + expf(-d0));
const float cand = tanhf(d1);
state_out[r] = fmaf(gate, state_in[r] - cand, cand);
latent_out[r] = tanhf(d2);
}
}
extern "C" int vetta_dspark_markov_proj_bf16(
const int32_t* prev_tok_ptr, const float* state_in, float* state_out,
float* latent_out, const float* cur_step, const uint16_t* frnn_ring,
int frnn_slot, const uint16_t* markov_w1, const uint16_t* rnn_proj_w,
const float* rnn_proj_b, int D, int rank, vetta_stream_t stream) {
if (D <= 0 || rank <= 0 || (D & 3) != 0 || (rank & 3) != 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_dspark_markov_proj_bf16<<<(rank + 7) / 8, 256, 0, s>>>(
prev_tok_ptr, state_in, state_out, latent_out, cur_step, frnn_ring,
frnn_slot, markov_w1, rnn_proj_w, rnn_proj_b, D, rank);
HIP_CHECK(hipGetLastError());
return 0;
}
// On-GPU Markov correction vector: corr[d] = dot(hidden_out_w[d], latent) + bias[d].
__global__ void __launch_bounds__(256) k_dspark_markov_corr_bf16(
const float* __restrict__ latent, // [rank]
const uint16_t* __restrict__ hidden_out_w, // [D, rank] bf16
const float* __restrict__ hidden_out_b, // [D] f32
float* __restrict__ corr_out, // [D] f32
int D, int rank) {
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int d = blockIdx.x * 8 + wave;
if (d >= D) return;
const uint16_t* wh = hidden_out_w + (size_t)d * rank;
float acc = 0.f;
for (int r = lane * 8; r < rank; r += 256) {
const uint4 w8 = *reinterpret_cast<const uint4*>(wh + r);
const float4 l0 = *reinterpret_cast<const float4*>(latent + r);
const float4 l1 = *reinterpret_cast<const float4*>(latent + r + 4);
acc = fmaf(bf16_to_f32(w8.x & 0xFFFFu), l0.x, fmaf(bf16_to_f32(w8.x >> 16), l0.y, acc));
acc = fmaf(bf16_to_f32(w8.y & 0xFFFFu), l0.z, fmaf(bf16_to_f32(w8.y >> 16), l0.w, acc));
acc = fmaf(bf16_to_f32(w8.z & 0xFFFFu), l1.x, fmaf(bf16_to_f32(w8.z >> 16), l1.y, acc));
acc = fmaf(bf16_to_f32(w8.w & 0xFFFFu), l1.z, fmaf(bf16_to_f32(w8.w >> 16), l1.w, acc));
}
acc = wave32_sum(acc) + hidden_out_b[d];
if (lane == 0) corr_out[d] = acc;
}
extern "C" int vetta_dspark_markov_corr_bf16(
const float* latent, const uint16_t* hidden_out_w, const float* hidden_out_b,
float* corr_out, int D, int rank, vetta_stream_t stream) {
if (D <= 0 || rank <= 0 || (rank & 7) != 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_dspark_markov_corr_bf16<<<(D + 7) / 8, 256, 0, s>>>(
latent, hidden_out_w, hidden_out_b, corr_out, D, rank);
HIP_CHECK(hipGetLastError());
return 0;
}
// On-GPU 512-candidate scorer + 2-stage deterministic argmax -> writes winning
// token id directly into *out_tok on device (zero logits D2H!).
__global__ void __launch_bounds__(256) k_dspark_cand_argmax_s1(
const uint16_t* __restrict__ unembed_bf16, // [V, D]
const float* __restrict__ cand_base_val, // [n_cand]
const int32_t* __restrict__ cand_id, // [n_cand]
const float* __restrict__ corr, // [D]
float* __restrict__ blk_val, // [gridDim.x]
int32_t* __restrict__ blk_id, // [gridDim.x]
int n_cand, int D, int V) {
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int ci = blockIdx.x * 8 + wave;
float score = -1e30f;
int32_t tok = V;
if (ci < n_cand) {
const int32_t v = cand_id[ci];
if (v >= 0 && v < V) {
const uint16_t* wr = unembed_bf16 + (size_t)v * D;
const int k8 = D & ~7;
float acc = 0.f;
for (int kk = lane * 8; kk < k8; kk += 256) {
const uint4 w8 = *reinterpret_cast<const uint4*>(wr + kk);
const float4 c0 = *reinterpret_cast<const float4*>(corr + kk);
const float4 c1 = *reinterpret_cast<const float4*>(corr + kk + 4);
acc = fmaf(bf16_to_f32(w8.x & 0xFFFFu), c0.x, fmaf(bf16_to_f32(w8.x >> 16), c0.y, acc));
acc = fmaf(bf16_to_f32(w8.y & 0xFFFFu), c0.z, fmaf(bf16_to_f32(w8.y >> 16), c0.w, acc));
acc = fmaf(bf16_to_f32(w8.z & 0xFFFFu), c1.x, fmaf(bf16_to_f32(w8.z >> 16), c1.y, acc));
acc = fmaf(bf16_to_f32(w8.w & 0xFFFFu), c1.z, fmaf(bf16_to_f32(w8.w >> 16), c1.w, acc));
}
for (int kk = k8 + lane; kk < D; kk += 32) {
acc = fmaf(bf16_to_f32(wr[kk]), corr[kk], acc);
}
score = cand_base_val[ci] + wave32_sum(acc);
tok = v;
}
}
__shared__ float s_v[8];
__shared__ int32_t s_i[8];
if (lane == 0) { s_v[wave] = score; s_i[wave] = tok; }
__syncthreads();
if (threadIdx.x == 0) {
float bv = -1e30f;
int32_t bi = V;
for (int w = 0; w < 8; ++w) {
if (s_v[w] > bv || (s_v[w] == bv && s_i[w] < bi)) {
bv = s_v[w]; bi = s_i[w];
}
}
blk_val[blockIdx.x] = bv;
blk_id[blockIdx.x] = bi;
}
}
__global__ void __launch_bounds__(256) k_dspark_cand_argmax_s2(
const float* __restrict__ blk_val,
const int32_t* __restrict__ blk_id,
int n_blks, int32_t* __restrict__ out_tok) {
const int tid = threadIdx.x;
float v = (tid < n_blks) ? blk_val[tid] : -1e30f;
int32_t id = (tid < n_blks) ? blk_id[tid] : 0x7FFFFFFF;
__shared__ float s_v[256];
__shared__ int32_t s_i[256];
s_v[tid] = v; s_i[tid] = id;
__syncthreads();
for (int s = 128; s > 0; s >>= 1) {
if (tid < s) {
if (s_v[tid + s] > s_v[tid] || (s_v[tid + s] == s_v[tid] && s_i[tid + s] < s_i[tid])) {
s_v[tid] = s_v[tid + s]; s_i[tid] = s_i[tid + s];
}
}
__syncthreads();
}
if (tid == 0) *out_tok = s_i[0];
}
extern "C" int vetta_dspark_cand_argmax(
const uint16_t* unembed_bf16, const float* cand_base_val, const int32_t* cand_id,
const float* corr, float* blk_val, int32_t* blk_id, int32_t* out_tok,
int n_cand, int D, int V, vetta_stream_t stream) {
if (n_cand <= 0 || D <= 0 || V <= 0) return 1;
const int n_blks = (n_cand + 7) / 8;
if (n_blks > 256) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_dspark_cand_argmax_s1<<<n_blks, 256, 0, s>>>(
unembed_bf16, cand_base_val, cand_id, corr, blk_val, blk_id, n_cand, D, V);
k_dspark_cand_argmax_s2<<<1, 256, 0, s>>>(blk_val, blk_id, n_blks, out_tok);
HIP_CHECK(hipGetLastError());
return 0;
}
// On-GPU batched argmax over logits[B, V] -> out_ids[B] (deterministic first-max).
// Eliminates copying B * 800 KB of verify logits to CPU!
__global__ void __launch_bounds__(256) k_batched_argmax_f32(
const float* __restrict__ logits, int32_t* __restrict__ out_ids, int B, int V) {
const int b = blockIdx.x;
if (b >= B) return;
const int tid = threadIdx.x;
const float* row = logits + (size_t)b * V;
float bv = -INFINITY;
int32_t bi = V;
for (int v = tid; v < V; v += 256) {
const float z = row[v];
if (z > bv || (z == bv && v < bi)) { bv = z; bi = v; }
}
__shared__ float s_v[256];
__shared__ int32_t s_i[256];
s_v[tid] = bv; s_i[tid] = bi;
__syncthreads();
for (int s = 128; s > 0; s >>= 1) {
if (tid < s) {
if (s_v[tid + s] > s_v[tid] || (s_v[tid + s] == s_v[tid] && s_i[tid + s] < s_i[tid])) {
s_v[tid] = s_v[tid + s]; s_i[tid] = s_i[tid + s];
}
}
__syncthreads();
}
if (tid == 0) out_ids[b] = s_i[0];
}
extern "C" int vetta_batched_argmax_f32(
const float* logits, int32_t* out_ids, int B, int V, vetta_stream_t stream) {
if (B <= 0 || V <= 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_batched_argmax_f32<<<B, 256, 0, s>>>(logits, out_ids, B, V);
HIP_CHECK(hipGetLastError());
return 0;
}
// Per-query deterministic Wave32 online-softmax SWA GQA over FP16 KV (hd==128).
// Used for BOTH M=1 decode (pos_arr==NULL -> cur_pos_scalar) and M<16 spec verify
// (pos_arr!=NULL): ZERO global scores_scratch (eliminates 48x sync hipMalloc/hipFree
// per verify round!), vectorized uint2 FP16 loads + DPP wave32_sum, and bit-identical
// between M=1 and M=nd+1 for exact lossless speculation.
__global__ void __launch_bounds__(128) k_flash_swa_gqa_smallm_f16(
const float* __restrict__ q,
const uint16_t* __restrict__ k,
const uint16_t* __restrict__ v,
float* __restrict__ out,
int n_head, int n_kv, int seq,
float scale, int swa_window,
const int32_t* __restrict__ pos_arr, int cur_pos_scalar,
const float* __restrict__ rel_bias, int max_rel,
const float* __restrict__ sink_logit,
const float* __restrict__ gate, int M) {
const int wave = threadIdx.x >> 5;
const int lane = threadIdx.x & 31;
const int h = blockIdx.x * 4 + wave;
const int m = blockIdx.y;
if (h >= n_head || m >= M) return;
const int d0 = lane * 4;
const int kvh = h / (n_head / n_kv);
const int cur_pos = pos_arr ? pos_arr[m] : cur_pos_scalar;
const int k_start = (swa_window > 0 && cur_pos >= swa_window) ? (cur_pos - swa_window + 1) : 0;
const float4 qv = *reinterpret_cast<const float4*>(q + ((size_t)m * n_head + h) * 128 + d0);
const float q0 = qv.x * scale, q1 = qv.y * scale, q2 = qv.z * scale, q3 = qv.w * scale;
const uint16_t* kh = k + (size_t)kvh * seq * 128;
const uint16_t* vh = v + (size_t)kvh * seq * 128;
const float* rb = rel_bias ? (rel_bias + ((size_t)m * n_head + h) * max_rel) : nullptr;
float max_l = sink_logit ? sink_logit[(size_t)m * n_head + h] : -1e30f;
float sum_e = sink_logit ? 1.0f : 0.0f;
float o0 = 0.0f, o1 = 0.0f, o2 = 0.0f, o3 = 0.0f;
for (int s = k_start; s <= cur_pos; ++s) {
const size_t off = (size_t)(s % seq) * 128 + d0;
const uint2 kw = *reinterpret_cast<const uint2*>(kh + off);
const uint2 vw = *reinterpret_cast<const uint2*>(vh + off);
const float k0 = f16_to_f32(kw.x & 0xFFFFu), k1 = f16_to_f32(kw.x >> 16);
const float k2 = f16_to_f32(kw.y & 0xFFFFu), k3 = f16_to_f32(kw.y >> 16);
const float v0 = f16_to_f32(vw.x & 0xFFFFu), v1 = f16_to_f32(vw.x >> 16);
const float v2 = f16_to_f32(vw.y & 0xFFFFu), v3 = f16_to_f32(vw.y >> 16);
float sc = wave32_sum(fmaf(q0, k0, fmaf(q1, k1, fmaf(q2, k2, q3 * k3))));
const int dist = cur_pos - s;
if (rb && dist < max_rel) sc += rb[dist];
const float nm = fmaxf(max_l, sc);
const float a = (sum_e > 0.0f) ? expf(max_l - nm) : 0.0f;
const float b = expf(sc - nm);
o0 = fmaf(o0, a, b * v0); o1 = fmaf(o1, a, b * v1);
o2 = fmaf(o2, a, b * v2); o3 = fmaf(o3, a, b * v3);
sum_e = fmaf(sum_e, a, b);
max_l = nm;
}
const float inv = 1.0f / fmaxf(sum_e, 1e-30f);
o0 *= inv; o1 *= inv; o2 *= inv; o3 *= inv;
if (gate) {
const float4 gv = *reinterpret_cast<const float4*>(gate + ((size_t)m * n_head + h) * 128 + d0);
o0 *= 1.0f / (1.0f + expf(-gv.x));
o1 *= 1.0f / (1.0f + expf(-gv.y));
o2 *= 1.0f / (1.0f + expf(-gv.z));
o3 *= 1.0f / (1.0f + expf(-gv.w));
}
*reinterpret_cast<float4*>(out + ((size_t)m * n_head + h) * 128 + d0) =
make_float4(o0, o1, o2, o3);
}
extern "C" int vetta_flash_swa_gqa_smallm_f16(
const float* q, const uint16_t* k, const uint16_t* v, float* out,
int n_head, int n_kv, int seq, int hd, float scale, int swa_window,
const int32_t* pos_arr, int cur_pos_scalar,
const float* rel_bias, int max_rel, const float* sink_logit,
const float* gate, int M, vetta_stream_t stream) {
if (n_head <= 0 || n_kv <= 0 || seq <= 0 || hd != 128 || (n_head % n_kv) != 0 || M <= 0) return 1;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
dim3 grid((n_head + 3) / 4, M);
k_flash_swa_gqa_smallm_f16<<<grid, 128, 0, s>>>(
q, k, v, out, n_head, n_kv, seq, scale, swa_window,
pos_arr, cur_pos_scalar, rel_bias, max_rel, sink_logit, gate, M);
HIP_CHECK(hipGetLastError());
return 0;
}