[HIP] Per-query flash determinism (default scalar) + DSpark ctx scatter kernel

Root cause of speculative losslessness divergence: the FA2 WMMA prefill flash
rescales o_acc/row_sum over a mixed-position query tile up to q_max_pos, so a
query's output depended on its batch neighbours by ~1 ulp (masked extra KV block
still rescales; __expf(0) is not exactly 1). That 1-ulp drift bf16-rounds into the
ShortConv hist and accumulates until a greedy near-tie flips (gate diverged at
token 46). The scalar f16 flash is per-query deterministic. Make WMMA flash OPT-IN
(VETTA_FLASH_WMMA=1); default to the verified scalar baseline. Guard alpha=1.0
exactly when nm==row_max. New flash_batch_determinism test: query0 M=2 vs M=1
maxdiff=0. Also add k_dspark_scatter_bf16 (f32 rows -> bf16 ring at pos%cap) for
DSpark context commit. hip suite 45/45.
This commit is contained in:
adm committed 2026-12-02 21:30:27 +01:00
1 parent ca12801b40
commit 6e854a9d73
2 files changed
+91 -3

No files matched your search

+39 -1
View File
@@ -445,6 +445,38 @@ extern "C" int vetta_dspark_split3(const float* in, float* a, float* b,
return 0;
}
// Scatter f32 rows src[m,C] into a bf16 ring dst[cap,C] at slot pos[i]%cap
// (bf16 RNE round). Commits the DSpark context for the fed tokens; rollback is
// a length truncate (ring slots of kept vs rejected spans are disjoint).
__global__ void k_dspark_scatter_bf16(const float* __restrict__ src,
uint16_t* __restrict__ dst,
const int* __restrict__ pos, int m, int C,
int cap) {
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
const int total = m * C;
if (idx >= total) return;
const int i = idx / C;
const int c = idx % C;
const int slot = ((pos[i] % cap) + cap) % cap;
const float v = src[idx];
const uint32_t u = __float_as_uint(v);
const uint32_t r = 0x7FFFu + ((u >> 16) & 1u);
dst[(size_t)slot * C + c] = (uint16_t)((u + r) >> 16);
}
extern "C" int vetta_dspark_scatter_bf16(const float* src, uint16_t* dst,
const int* pos, int m, int C, int cap,
vetta_stream_t stream) {
if (m <= 0 || C <= 0 || cap <= 0) return 1;
const size_t total = (size_t)m * C;
const int t = 256;
const size_t grid = (total + t - 1) / t;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_dspark_scatter_bf16<<<grid, t, 0, s>>>(src, dst, pos, m, C, cap);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- BF16 helpers ----------------------------------------------------------
__device__ __forceinline__ float bf16_to_f32(uint16_t h) {
@@ -2973,7 +3005,13 @@ __global__ void __launch_bounds__(128) k_flash_swa_gqa_prefill_wmma(
lmax = fmaxf(lmax, __shfl_xor(lmax, 2, 32));
lmax = fmaxf(lmax, __shfl_xor(lmax, 1, 32));
const float nm = fmaxf(row_max[r], lmax);
const float alpha = (row_sum[r] > 0.0f) ? __expf(row_max[r] - nm) : 0.0f;
// alpha must be EXACTLY 1.0 when the running max is unchanged: the
// fast __expf(0) is ~1 ulp off 1.0, and a fully-masked extra KV block
// (q_max_pos > this query's pos in a mixed-position tile) would then
// rescale o_acc/row_sum by ~1.0, making the output depend on batch
// neighbours. Guarding nm==row_max keeps it per-query deterministic
// (required for speculative losslessness + chunked-prefill parity).
const float alpha = (row_sum[r] > 0.0f) ? ((nm == row_max[r]) ? 1.0f : __expf(row_max[r] - nm)) : 0.0f;
#pragma unroll
for (int d = 0; d < 8; ++d) o_acc[d][r] *= alpha;
row_sum[r] *= alpha;