Skip to content

Commit 8a7d9c5

Browse files
kmbandyclaude
andcommitted
wip(pagedattn): snapshot in-progress mt_pagedattn fp8/aiter tweaks
Snapshot of local work-in-progress on the paged-attention path (mt_pagedattn.cu, mt_pagedattn_aiter.cu, mt_pagedattn_turbo_fp8.cuh) so it rides along in this branch push. Small, self-contained (14 insertions / 14 deletions); unrelated to the DSWS spike work in the rest of the branch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0132aDSBLwusCJ4KzHQTnvdu
1 parent c33d065 commit 8a7d9c5

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

ggml/src/ggml-cuda/mt_pagedattn.cu

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ static int get_paged_decode_mode() {
9595

9696
// ───────────────────────── helpers ─────────────────────────
9797

98-
// Warp-level reduce across 32 lanes (HIP wavefront is 64 on some
99-
// GPUs but ggml-cuda's WARP_SIZE is fixed at 32 — works correctly on
100-
// gfx1xxx because __shfl_xor_sync over the active mask).
98+
// Warp-level reduce across 32 lanes. HIP wavefronts can be 64 lanes, while
99+
// ggml-cuda's WARP_SIZE is fixed at 32, so every shuffle is width-scoped.
101100
template <typename T>
102101
__device__ __forceinline__ T warp_reduce_sum(T v) {
103102
#pragma unroll
@@ -373,7 +372,7 @@ __global__ void mt_scatter_kv_turbo4_0_kernel(
373372
{
374373
float v_sq = x[j] * x[j];
375374
for (int offset = WARP_SIZE / 2; offset > 0; offset >>= 1) {
376-
v_sq += __shfl_xor_sync(0xffffffffu, v_sq, offset);
375+
v_sq += __shfl_xor_sync(0xffffffffu, v_sq, offset, WARP_SIZE);
377376
}
378377
if (j % WARP_SIZE == 0) warp_accum[j / WARP_SIZE] = v_sq;
379378
}
@@ -431,7 +430,7 @@ __global__ void mt_scatter_kv_turbo4_0_kernel(
431430
const float c = TURBO_CENTROIDS_4BIT[idx];
432431
float rc = c * c;
433432
for (int offset = WARP_SIZE / 2; offset > 0; offset >>= 1) {
434-
rc += __shfl_xor_sync(0xffffffffu, rc, offset);
433+
rc += __shfl_xor_sync(0xffffffffu, rc, offset, WARP_SIZE);
435434
}
436435
if (j % WARP_SIZE == 0) warp_accum[j / WARP_SIZE] = rc;
437436
}
@@ -505,7 +504,7 @@ __global__ void mt_scatter_kv_turbo4_64_kernel(
505504
{
506505
float v_sq = x[j] * x[j];
507506
for (int offset = WARP_SIZE / 2; offset > 0; offset >>= 1) {
508-
v_sq += __shfl_xor_sync(0xffffffffu, v_sq, offset);
507+
v_sq += __shfl_xor_sync(0xffffffffu, v_sq, offset, WARP_SIZE);
509508
}
510509
if (j % WARP_SIZE == 0) warp_accum[j / WARP_SIZE] = v_sq;
511510
}
@@ -540,7 +539,7 @@ __global__ void mt_scatter_kv_turbo4_64_kernel(
540539
const float c = TURBO_CENTROIDS_4BIT[idx];
541540
float rc = c * c;
542541
for (int offset = WARP_SIZE / 2; offset > 0; offset >>= 1) {
543-
rc += __shfl_xor_sync(0xffffffffu, rc, offset);
542+
rc += __shfl_xor_sync(0xffffffffu, rc, offset, WARP_SIZE);
544543
}
545544
if (j % WARP_SIZE == 0) warp_accum[j / WARP_SIZE] = rc;
546545
}

ggml/src/ggml-cuda/mt_pagedattn_aiter.cu

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ __global__ void mt_scatter_kv_turbo4_aiter_kernel(
296296
{
297297
float v_sq = x[j] * x[j];
298298
for (int offset = WARP_SIZE / 2; offset > 0; offset >>= 1) {
299-
v_sq += __shfl_xor_sync(0xffffffffu, v_sq, offset);
299+
v_sq += __shfl_xor_sync(0xffffffffu, v_sq, offset, WARP_SIZE);
300300
}
301301
if (j % WARP_SIZE == 0) warp_accum[j / WARP_SIZE] = v_sq;
302302
}
@@ -332,7 +332,7 @@ __global__ void mt_scatter_kv_turbo4_aiter_kernel(
332332
const float c = TURBO_CENTROIDS_4BIT[idx];
333333
float rc = c * c;
334334
for (int offset = WARP_SIZE / 2; offset > 0; offset >>= 1) {
335-
rc += __shfl_xor_sync(0xffffffffu, rc, offset);
335+
rc += __shfl_xor_sync(0xffffffffu, rc, offset, WARP_SIZE);
336336
}
337337
if (j % WARP_SIZE == 0) warp_accum[j / WARP_SIZE] = rc;
338338
}
@@ -456,7 +456,7 @@ __global__ void mt_scatter_kv_turbo4_fp8_aiter_kernel(
456456
// ── Stage 2: per-block max-abs scale ──
457457
float v_abs = fabsf(x[j]);
458458
for (int off = 16; off > 0; off >>= 1) {
459-
v_abs = fmaxf(v_abs, __shfl_xor_sync(0xffffffffffffffffull, v_abs, off));
459+
v_abs = fmaxf(v_abs, __shfl_xor_sync(0xffffffffffffffffull, v_abs, off, WARP_SIZE));
460460
}
461461
__shared__ float warp_max[8];
462462
if ((j % 32) == 0) warp_max[j / 32] = v_abs;
@@ -496,16 +496,17 @@ __global__ void mt_scatter_kv_turbo4_fp8_aiter_kernel(
496496
}
497497

498498
const uint8_t my_nib = (uint8_t)(best_idx & 0xF);
499-
const uint8_t partner_nib = (uint8_t) __shfl_xor_sync(0xffffffffffffffffull, (int) my_nib, 1);
499+
const uint8_t partner_nib = (uint8_t) __shfl_xor_sync(0xffffffffffffffffull, (int) my_nib, 1, WARP_SIZE);
500500
if ((j & 1) == 0) {
501501
blk[2 + j / 2] = my_nib | (uint8_t)(partner_nib << 4);
502502
}
503503

504504
const uint64_t sign_mask = __ballot_sync(0xffffffffffffffffull, sgn);
505505
if ((j & 7) == 0) {
506506
const int byte_idx = j / 8;
507-
const int warp_off = (j % 32) / 8;
508-
blk[130 + byte_idx] = (uint8_t)((sign_mask >> (warp_off * 8)) & 0xFF);
507+
const int hw_lane_base = (threadIdx.x % warpSize) & ~(WARP_SIZE - 1);
508+
const int bit_off = hw_lane_base + ((j % WARP_SIZE) & ~7);
509+
blk[130 + byte_idx] = (uint8_t)((sign_mask >> bit_off) & 0xFF);
509510
}
510511
}
511512

ggml/src/ggml-cuda/mt_pagedattn_turbo_fp8.cuh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static __device__ __forceinline__ void coop_stage_turbo4_fp8_bs256_tile(
126126
// Broadcast scale from lane 0 to all 32 lanes of this warp.
127127
// 64-bit mask literal required on RDNA4 (gfx12, wave32) per
128128
// amd_warp_sync_functions.h static_assert.
129-
scale_f = __shfl_sync(0xFFFFFFFFFFFFFFFFull, scale_f, 0);
129+
scale_f = __shfl_sync(0xFFFFFFFFFFFFFFFFull, scale_f, 0, WARP_SIZE);
130130

131131
// Read this lane's 4-byte qs window (8 nibbles) and 1-byte signs window.
132132
uint32_t qs_word = 0;

0 commit comments

Comments
 (0)