Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions engine/include/strata/kernels/qsa_decode_attn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,17 @@ void qsa_decode_attn_step(const float* q, const QsaAttnPools& pools, const int32
void qsa_decode_attn_batch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps,
int64_t cap, const QsaShapes& s, float* scratch, float* attn, int64_t n_q, void* stream);

/// Up to this many queries of different slots in one qsa_decode_attn_slots call.
constexpr int kQsaAttnMaxSlots = 8;
/// Host-side descriptor capacity; individual device kernels use compact or smaller by-value descriptors.
constexpr int kQsaAttnMaxSlots = 64;

/// One KV pool set per query of a qsa_decode_attn_slots call (passed by value as a kernel argument).
/// One KV pool set per query of a qsa_decode_attn_slots call (host-side descriptor).
struct QsaAttnSlotPools {
QsaAttnPools p[kQsaAttnMaxSlots];
};
static_assert(sizeof(QsaAttnSlotPools) <= 2048, "sm_86 4KB kernel param limit");

/// qsa_decode_attn_batch for `n_q` queries that each attend over their OWN pools (`pools.p[i]`, one KV cache per batch
/// slot): one launch instead of one call per query, every block computing exactly what the per-query call computes.
/// The pools must all have one KV format. False (nothing launched) where the per-query call would run another kernel
/// (n_q > kQsaAttnMaxSlots, mixed formats, the pre-sm_75 / gfx103x or gfx906 variants): the caller loops instead.
/// slot): the same per-query arithmetic. STRATA_QSA_ATTN_BATCH64=1 enables up to 64 homogeneous slots;
/// otherwise calls split into groups of at most 8. Unsupported combinations return false for caller fallback.
bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const int32_t* ids, const int32_t* steps,
int64_t cap, const QsaShapes& s, float* scratch, float* attn, int64_t n_q, void* stream);
/// For src/kernels/qsa_merge_daily_parity.cpp only: qsa_decode_attn_batch's two passes apart, whatever the environment
Expand Down
28 changes: 18 additions & 10 deletions engine/src/core/verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2230,8 +2230,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
return;
}
if (g_qsa_slots && !streamed) {
for (int t0 = tb; t0 < te; t0 += kQsaAttnMaxSlots) {
const int m = std::min(te - t0, kQsaAttnMaxSlots);
for (int t0 = tb; t0 < te; t0 += 8) {
const int m = std::min(te - t0, 8);
const float* pooled[kQsaAttnMaxSlots];
const float* dead[kQsaAttnMaxSlots];
for (int i = 0; i < m; ++i) {
Expand Down Expand Up @@ -2501,8 +2501,10 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
err = "verify: the queries' branch could not join";
return false;
}
for (int t0 = tb; t0 < te; t0 += kQsaAttnMaxSlots) {
const int m = std::min(te - t0, kQsaAttnMaxSlots);
const char* wide_attn = std::getenv("STRATA_QSA_ATTN_BATCH64");
const int attn_group = wide_attn && wide_attn[0] == '1' && wide_attn[1] == '\0' ? kQsaAttnMaxSlots : 8;
for (int t0 = tb; t0 < te; t0 += attn_group) {
const int m = std::min(te - t0, attn_group);
const float* pooled[kQsaAttnMaxSlots];
const float* dead[kQsaAttnMaxSlots];
QsaAttnSlotPools px;
Expand All @@ -2512,12 +2514,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
dead[i] = sx.idx_dead;
px.p[i] = qsa_attn_pools(sx);
}
// a launcher that would not run the per-row call's kernel launches nothing: per-row calls
if (!qsa_block_scores_slots(pooled, dead, qidx_ + t0 * IQ * ID, step_ + t0 * kStepCount, m,
max_blocks_, s, scores_ + (size_t) t0 * max_blocks_, cs))
for (int t = t0; t < t0 + m; ++t)
qsa_block_scores(pooled[t - t0], dead[t - t0], qidx_ + t * IQ * ID, step_ + t * kStepCount,
1, max_blocks_, s, scores_ + (size_t) t * max_blocks_, cs);
// The score kernel has its own 8-row descriptor limit.
for (int j = 0; j < m; j += 8) {
const int count = std::min(m - j, 8);
const int first = t0 + j;
if (!qsa_block_scores_slots(pooled + j, dead + j, qidx_ + first * IQ * ID,
step_ + first * kStepCount, count, max_blocks_, s,
scores_ + (size_t) first * max_blocks_, cs))
for (int t = first; t < first + count; ++t)
qsa_block_scores(pooled[t - t0], dead[t - t0], qidx_ + t * IQ * ID,
step_ + t * kStepCount, 1, max_blocks_, s,
scores_ + (size_t) t * max_blocks_, cs);
}
qsa_block_topk(scores_ + (size_t) t0 * max_blocks_, step_ + t0 * kStepCount, m, max_blocks_,
cap_, s, sel_ + (size_t) t0 * cap_, cs);
if (!qsa_decode_attn_slots(qcur_ + t0 * NH * HD, px, sel_ + (size_t) t0 * cap_,
Expand Down
92 changes: 87 additions & 5 deletions engine/src/kernels/cuda/qsa_decode_attn.cu
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,39 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __rest
scratch_stride);
}

constexpr int kLegacyAttnSlots = 8;
struct QsaAttnLegacySlots { QsaAttnPools p[kLegacyAttnSlots]; };
static_assert(sizeof(QsaAttnLegacySlots) + 256 <= 4096, "kernel parameter budget");

// Each homogeneous KV mode uses at most five pointers, including its page table.
struct QsaAttnSlotPtrs { const void* k; const void* v; const uint16_t* ks; const uint16_t* vs; const int32_t* pages; };
struct QsaAttnSlotPtrs64 { QsaAttnSlotPtrs p[kQsaAttnMaxSlots]; };
static_assert(sizeof(QsaAttnSlotPtrs) == 5 * sizeof(void*), "unexpected pointer layout");
static_assert(sizeof(QsaAttnSlotPtrs64) + 256 <= 3840, "kernel parameter budget");

template <int MODE>
__device__ __forceinline__ QsaAttnPools unpack_attn_slot(const QsaAttnSlotPtrs& x) {
QsaAttnPools p{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, x.pages
#if defined(STRATA_KV_FP8)
, false
#endif
};
if constexpr (MODE == 0) { p.k_pool = (const uint16_t*) x.k; p.v_pool = (const uint16_t*) x.v; }
else if constexpr (MODE == 1) { p.k_q = (const int8_t*) x.k; p.v_q = (const int8_t*) x.v; p.k_scale = x.ks; p.v_scale = x.vs; }
else if constexpr (MODE == 2) { p.k_q4 = (const uint8_t*) x.k; p.v_q4 = (const uint8_t*) x.v; }
else { p.k_q = (const int8_t*) x.k; p.v_q4 = (const uint8_t*) x.v; p.k_scale = x.ks; }
return p;
}
#if defined(__HIPCC__)
#define STRATA_ATTN_GRID_CONSTANT const
#else
#define STRATA_ATTN_GRID_CONSTANT const __grid_constant__
#endif

// The same blocks for queries of different slots: query blockIdx.z reads its own slot's pools, everything else is
// attn_chunk_kernel's batched form (qsa_decode_attn_slots).
template <int KV_MODE, bool LANE_CELL>
__global__ void __launch_bounds__(THREADS) attn_chunk_slots_kernel(const float* __restrict__ q, QsaAttnSlotPools ps,
__global__ void __launch_bounds__(THREADS) attn_chunk_slots_kernel(const float* __restrict__ q, QsaAttnLegacySlots ps,
const int32_t* __restrict__ ids,
const int32_t* __restrict__ step, int n_kv_heads,
int page_size, float scale,
Expand All @@ -355,6 +384,16 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_slots_kernel(const float*
part_acc, part_m, part_l, n_chunks, cap, scratch_stride);
}

// CUDA graph captures the compact descriptor by value, not a borrowed host pointer.
template <int KV_MODE, bool LANE_CELL>
__global__ void __launch_bounds__(THREADS) attn_chunk_slots64_kernel(const float* __restrict__ q, STRATA_ATTN_GRID_CONSTANT QsaAttnSlotPtrs64 ps,
const int32_t* __restrict__ ids, const int32_t* __restrict__ step, int n_kv_heads, int page_size, float scale,
float* __restrict__ part_acc, float* __restrict__ part_m, float* __restrict__ part_l, int n_chunks, int cap, long long stride) {
const QsaAttnPools p = unpack_attn_slot<KV_MODE>(ps.p[blockIdx.z]);
attn_chunk_body<KV_MODE, LANE_CELL, false, false>(q, p, ids, step, n_kv_heads, page_size, scale,
part_acc, part_m, part_l, n_chunks, cap, stride);
}

// Batch windows: query blockIdx.z attends over its own slot's pools (rows.p[z]); the default kernel otherwise.
template <int KV_MODE>
__global__ void __launch_bounds__(THREADS) attn_chunk_rows_kernel(const float* __restrict__ q, QsaAttnPoolsRows rows,
Expand Down Expand Up @@ -742,13 +781,23 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float*
// with the exact pre75 arithmetic and accumulation order (bitwise identical to per-query attn_chunk_kernel_pre75).
template <int KV_MODE, int NC_ = 2, bool DPP_ = false>
__global__ void __launch_bounds__(THREADS) attn_chunk_slots_pre75_kernel(
const float* __restrict__ q, QsaAttnSlotPools ps, const int32_t* __restrict__ ids,
const float* __restrict__ q, QsaAttnLegacySlots ps, const int32_t* __restrict__ ids,
const int32_t* __restrict__ step, int n_kv_heads, int page_size, float scale, float* __restrict__ part_acc,
float* __restrict__ part_m, float* __restrict__ part_l, int n_chunks, int cap, long long scratch_stride) {
attn_chunk_pre75_body<KV_MODE, NC_, DPP_>(q, ps.p[blockIdx.z], ids, step, n_kv_heads, page_size, scale, part_acc,
part_m, part_l, n_chunks, cap, scratch_stride);
}

template <int KV_MODE, int NC_ = 2, bool DPP_ = false>
__global__ void __launch_bounds__(THREADS) attn_chunk_slots64_pre75_kernel(
const float* __restrict__ q, STRATA_ATTN_GRID_CONSTANT QsaAttnSlotPtrs64 ps, const int32_t* __restrict__ ids,
const int32_t* __restrict__ step, int n_kv_heads, int page_size, float scale, float* __restrict__ part_acc,
float* __restrict__ part_m, float* __restrict__ part_l, int n_chunks, int cap, long long stride) {
const QsaAttnPools p = unpack_attn_slot<KV_MODE>(ps.p[blockIdx.z]);
attn_chunk_pre75_body<KV_MODE, NC_, DPP_>(q, p, ids, step, n_kv_heads, page_size, scale, part_acc, part_m, part_l,
n_chunks, cap, stride);
}

// Per-row batched form of attn_chunk_kernel_pre75 (STRATA_BATCH_ATTN_ROWS=1 with STRATA_ATTN_DAILY=1).
template <int KV_MODE, int NC_ = 2, bool DPP_ = false>
__global__ void __launch_bounds__(THREADS) attn_chunk_rows_pre75_kernel(
Expand Down Expand Up @@ -1155,6 +1204,28 @@ bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const
std::fprintf(stderr, "qsa_decode_attn_slots: unsupported geometry or missing buffers\n");
std::exit(1);
}
static const bool batch64 = [] {
const char* v = std::getenv("STRATA_QSA_ATTN_BATCH64"); return v && v[0] == '1' && v[1] == '\0';
}();
const int group = batch64 ? 64 : kLegacyAttnSlots;
if (n_q > group) {
const size_t stride = (size_t) qsa_decode_attn_scratch_floats(cap, s);
for (int64_t base = 0; base < n_q; base += group) {
const int count = (int) std::min<int64_t>(group, n_q - base);
QsaAttnSlotPools sub{};
for (int i = 0; i < count; ++i) sub.p[i] = pools.p[base + i];
if (!qsa_decode_attn_slots(q + (size_t)base * s.n_head * HD, sub, ids + (size_t)base * cap,
steps + (size_t)base * kStepCount, cap, s, scratch + (size_t)base * stride,
attn + (size_t)base * s.n_head * HD, count, stream)) {
for (int i = 0; i < count; ++i) {
const size_t row = (size_t)base + i;
qsa_decode_attn_batch(q + row * s.n_head * HD, pools.p[row], ids + row * cap,
steps + row * kStepCount, cap, s, scratch + row * stride, attn + row * s.n_head * HD, 1, stream);
}
}
}
return true;
}
const int kv_mode = attn_kv_mode(pools.p[0]);
for (int64_t z = 0; z < n_q; ++z) {
if (!pools.p[z].page_table) {
Expand Down Expand Up @@ -1183,14 +1254,25 @@ bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const
float* part_m = scratch + (size_t) n_chunks * s.n_head * HD;
float* part_l = part_m + (size_t) n_chunks * s.n_head;
const float scale = 1.0f / sqrtf((float) HD);
const bool use64 = batch64 && n_q > kLegacyAttnSlots;
QsaAttnLegacySlots legacy{};
QsaAttnSlotPtrs64 compact{};
for (int64_t z = 0; z < n_q; ++z) {
const auto& p = pools.p[z];
if (!use64) { legacy.p[z] = p; continue; }
auto& d = compact.p[z];
d.k = kv_mode == 0 ? (const void*)p.k_pool : kv_mode == 2 ? (const void*)p.k_q4 : (const void*)p.k_q;
d.v = kv_mode == 0 ? (const void*)p.v_pool : kv_mode == 1 ? (const void*)p.v_q : (const void*)p.v_q4;
d.ks = p.k_scale; d.vs = p.v_scale; d.pages = p.page_table;
}
const dim3 grid((unsigned) n_chunks, (unsigned) s.n_head_kv, (unsigned) n_q);
cudaStream_t st = (cudaStream_t) stream;
#if defined(STRATA_ATTN_PRE75_BUILT)
if (use_pre75) {
#if defined(__HIPCC__)
#define STRATA_ATTN_SLOTS_PRE75(M) attn_chunk_slots_pre75_kernel<M, 8, true><<<grid, THREADS, 0, st>>>(q, pools, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride)
#define STRATA_ATTN_SLOTS_PRE75(M) do { if (use64) attn_chunk_slots64_pre75_kernel<M, 8, true><<<grid, THREADS, 0, st>>>(q, compact, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); else attn_chunk_slots_pre75_kernel<M, 8, true><<<grid, THREADS, 0, st>>>(q, legacy, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); } while (0)
#else
#define STRATA_ATTN_SLOTS_PRE75(M) attn_chunk_slots_pre75_kernel<M, 2, false><<<grid, THREADS, 0, st>>>(q, pools, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride)
#define STRATA_ATTN_SLOTS_PRE75(M) do { if (use64) attn_chunk_slots64_pre75_kernel<M, 2, false><<<grid, THREADS, 0, st>>>(q, compact, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); else attn_chunk_slots_pre75_kernel<M, 2, false><<<grid, THREADS, 0, st>>>(q, legacy, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); } while (0)
#endif
if (kv_mode == 3) STRATA_ATTN_SLOTS_PRE75(3);
else if (kv_mode == 2) STRATA_ATTN_SLOTS_PRE75(2);
Expand All @@ -1200,7 +1282,7 @@ bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const
} else
#endif
{
#define STRATA_ATTN_SLOTS(M, LC) attn_chunk_slots_kernel<M, LC><<<grid, THREADS, 0, st>>>(q, pools, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride)
#define STRATA_ATTN_SLOTS(M, LC) do { if (use64) attn_chunk_slots64_kernel<M, LC><<<grid, THREADS, 0, st>>>(q, compact, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); else attn_chunk_slots_kernel<M, LC><<<grid, THREADS, 0, st>>>(q, legacy, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); } while (0)
if (lane_cell) {
if (kv_mode == 3) STRATA_ATTN_SLOTS(3, true);
else if (kv_mode == 2) STRATA_ATTN_SLOTS(2, true);
Expand Down
Loading