From d176742d2fbb19fe456da3468d53b134fc6451a1 Mon Sep 17 00:00:00 2001 From: cobra91 Date: Sat, 5 Dec 2026 13:48:31 +0100 Subject: [PATCH] cuda(qsa): support up to 64 slots in qsa_decode_attn_slots via compact descriptors --- .../strata/kernels/qsa_decode_attn.hpp | 12 +-- engine/src/core/verify.cpp | 28 ++++-- engine/src/kernels/cuda/qsa_decode_attn.cu | 92 ++++++++++++++++++- engine/src/kernels/qsa_slots_parity.cpp | 88 +++++++++++++++--- terminal/data/settings-register.json | 39 ++++++++ 5 files changed, 226 insertions(+), 33 deletions(-) diff --git a/engine/include/strata/kernels/qsa_decode_attn.hpp b/engine/include/strata/kernels/qsa_decode_attn.hpp index d7747ba487..819eb09062 100644 --- a/engine/include/strata/kernels/qsa_decode_attn.hpp +++ b/engine/include/strata/kernels/qsa_decode_attn.hpp @@ -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 diff --git a/engine/src/core/verify.cpp b/engine/src/core/verify.cpp index 200ee638da..b6ae3b2b93 100644 --- a/engine/src/core/verify.cpp +++ b/engine/src/core/verify.cpp @@ -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) { @@ -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; @@ -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_, diff --git a/engine/src/kernels/cuda/qsa_decode_attn.cu b/engine/src/kernels/cuda/qsa_decode_attn.cu index 07116248f2..bae3bcfef0 100644 --- a/engine/src/kernels/cuda/qsa_decode_attn.cu +++ b/engine/src/kernels/cuda/qsa_decode_attn.cu @@ -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 +__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 -__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, @@ -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 +__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(ps.p[blockIdx.z]); + attn_chunk_body(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 __global__ void __launch_bounds__(THREADS) attn_chunk_rows_kernel(const float* __restrict__ q, QsaAttnPoolsRows rows, @@ -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 __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(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 +__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(ps.p[blockIdx.z]); + attn_chunk_pre75_body(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 __global__ void __launch_bounds__(THREADS) attn_chunk_rows_pre75_kernel( @@ -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(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) { @@ -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<<>>(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<<>>(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<<>>(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<<>>(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<<>>(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<<>>(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); @@ -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<<>>(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<<>>(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<<>>(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); diff --git a/engine/src/kernels/qsa_slots_parity.cpp b/engine/src/kernels/qsa_slots_parity.cpp index bd804cb234..3b1a04fca3 100644 --- a/engine/src/kernels/qsa_slots_parity.cpp +++ b/engine/src/kernels/qsa_slots_parity.cpp @@ -48,8 +48,6 @@ uint16_t rnd_half(std::mt19937& r) { return (uint16_t) (((x & 1u) << 15) | ((10u + (x >> 1) % 7u) << 10) | ((x >> 4) & 0x3ffu)); } -constexpr int64_t kMaxCells = 32768; - struct Slot { float *pooled = nullptr, *dead = nullptr; int32_t* page_table = nullptr; @@ -57,7 +55,7 @@ struct Slot { k::QsaAttnPools pools; }; -Slot make_slot(int fmt, const k::QsaShapes& s, int64_t max_blocks, std::mt19937& r) { +Slot make_slot(int fmt, const k::QsaShapes& s, int64_t max_blocks, int64_t max_cells, std::mt19937& r) { Slot sl; std::normal_distribution nd(0.f, 1.f); std::vector pooled((size_t) (max_blocks * s.idx_dim)), dead((size_t) s.idx_dim); @@ -65,7 +63,7 @@ Slot make_slot(int fmt, const k::QsaShapes& s, int64_t max_blocks, std::mt19937& for (auto& v : dead) v = nd(r); sl.pooled = up(pooled); sl.dead = up(dead); - const int64_t pages = kMaxCells / s.page_size; + const int64_t pages = (max_cells + s.page_size - 1) / s.page_size; std::vector pt((size_t) pages); std::iota(pt.begin(), pt.end(), 0); std::shuffle(pt.begin(), pt.end(), r); @@ -114,13 +112,19 @@ enum Result { kPass, kFail, kSkip }; Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { const k::QsaShapes s = k::qsa_real_shapes(); const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s); - const int64_t max_blocks = kMaxCells / s.idx_block + 2; + const int64_t max_cells = *std::max_element(ctx, ctx + m); + const int64_t max_blocks = max_cells / s.idx_block + 2; const int64_t NH = s.n_head, HD = s.head_dim, QI = s.idx_n_head * s.idx_dim; const int64_t sf = (int64_t) k::qsa_decode_attn_scratch_floats(cap, s); std::mt19937 r(seed); std::normal_distribution nd(0.f, 1.f); std::vector slots; - for (int i = 0; i < m; ++i) slots.push_back(make_slot(fmt, s, max_blocks, r)); + for (int i = 0; i < m; ++i) { + slots.push_back(make_slot(fmt == 4 ? 1 : fmt, s, max_blocks, max_cells, r)); +#if defined(STRATA_KV_FP8) + if (fmt == 4) slots.back().pools.fp8 = true; +#endif + } std::vector qidx((size_t) (m * QI)), qcur((size_t) (m * NH * HD)); for (auto& v : qidx) v = nd(r); for (auto& v : qcur) v = nd(r); @@ -157,10 +161,14 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { // a declined launch is no failure: the engine runs the per-row calls there (STRATA_SCORES_MULTI=0, the pre-sm_75 // attention, the gfx906 query swizzle) Result res = kPass; - if (!k::qsa_block_scores_slots(pooled, dead, d_qidx, d_steps, m, max_blocks, s, sc[1], nullptr)) { - std::puts("SKIP: qsa_block_scores_slots declined (STRATA_SCORES_MULTI=0); the engine keeps the per-row calls"); - res = kSkip; - } else { + for (int base = 0; base < m; base += 8) { + const int count = std::min(8, m - base); + if (!k::qsa_block_scores_slots(pooled + base, dead + base, d_qidx + base * QI, + d_steps + base * k::kStepCount, count, max_blocks, s, sc[1] + base * max_blocks, nullptr)) { + std::puts("SKIP: qsa_block_scores_slots declined"); res = kSkip; break; + } + } + if (res == kPass) { k::qsa_block_topk(sc[1], d_steps, m, max_blocks, cap, s, sel[1], nullptr); if (!k::qsa_decode_attn_slots(d_qcur, px, sel[1], d_steps, cap, s, scratch[1], attn[1], m, nullptr)) { std::puts("SKIP: qsa_decode_attn_slots declined (the pre-sm_75 or gfx906 query-swizzle attention); the " @@ -170,6 +178,16 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { } ck(cudaDeviceSynchronize()); bool ok = res == kPass; + if (ok && m > 8 && fmt == 1) { + const char* flag = std::getenv("STRATA_QSA_ATTN_BATCH64"); + if (flag && std::strcmp(flag, "1") == 0) { + auto mixed = px; + mixed.p[1].k_q4 = reinterpret_cast(mixed.p[1].k_q); + if (k::qsa_decode_attn_slots(d_qcur, mixed, sel[1], d_steps, cap, s, scratch[1], attn[1], m, nullptr)) { + std::fprintf(stderr, "FAIL: mixed KV modes were not declined\n"); ok = false; + } + } + } const auto s0 = down(sc[0], (size_t) (m * max_blocks)), s1 = down(sc[1], (size_t) (m * max_blocks)); const auto i0 = down(sel[0], (size_t) (m * cap)), i1 = down(sel[1], (size_t) (m * cap)); const auto a0 = down(attn[0], (size_t) (m * NH * HD)), a1 = down(attn[1], (size_t) (m * NH * HD)); @@ -186,6 +204,27 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { ok = false; } } + if (ok && m > 8) { + cudaStream_t stream; cudaGraph_t graph; cudaGraphExec_t exec; + ck(cudaStreamCreate(&stream)); ck(cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal)); + if (!k::qsa_decode_attn_slots(d_qcur, px, sel[1], d_steps, cap, s, scratch[1], attn[1], m, stream)) { + std::fprintf(stderr, "FAIL: attention declined during graph capture\n"); std::exit(1); + } + ck(cudaStreamEndCapture(stream, &graph)); + const char* flag = std::getenv("STRATA_QSA_ATTN_BATCH64"); + const bool wide = flag && std::strcmp(flag, "1") == 0; + size_t nodes = 0; ck(cudaGraphGetNodes(graph, nullptr, &nodes)); + const size_t expected = wide && fmt != 4 ? 2 : (size_t) (2 * ((m + 7) / 8)); + if (nodes != expected) { std::fprintf(stderr, "FAIL: expected %zu nodes, got %zu\n", expected, nodes); ok = false; } + ck(cudaGraphInstantiate(&exec, graph, nullptr, nullptr, 0)); + for (int i = 0; i < 10; ++i) ck(cudaGraphLaunch(exec, stream)); + ck(cudaStreamSynchronize(stream)); + const auto replay = down(attn[1], (size_t) (m * NH * HD)); + if (std::memcmp(a0.data(), replay.data(), replay.size() * sizeof(float))) { + std::fprintf(stderr, "FAIL: captured attention replay differs\n"); ok = false; + } + cudaGraphExecDestroy(exec); cudaGraphDestroy(graph); cudaStreamDestroy(stream); + } double mag = 0; // the attention output is not trivially zero for (float v : a0) mag += std::fabs(v); if (ok && !(mag > 0)) { std::fprintf(stderr, "FAIL fmt=%d rows=%d: attention output is zero\n", fmt, m); ok = false; } @@ -198,7 +237,15 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { } // namespace -int main() { +int main(int argc, char** argv) { + const bool wide = argc == 2 && std::strcmp(argv[1], "--wide64") == 0; + const bool wide_default = argc == 2 && std::strcmp(argv[1], "--wide-default") == 0; + if (argc > 1 && !wide && !wide_default) { std::fprintf(stderr, "usage: qsa_slots_parity [--wide64|--wide-default]\n"); return 2; } +#ifdef _WIN32 + _putenv_s("STRATA_QSA_ATTN_BATCH64", wide ? "1" : "0"); +#else + setenv("STRATA_QSA_ATTN_BATCH64", wide ? "1" : "0", 1); +#endif int devices = 0; if (cudaGetDeviceCount(&devices) != cudaSuccess || devices == 0) { std::puts("SKIP: no CUDA device"); @@ -208,6 +255,25 @@ int main() { std::puts("SKIP: STRATA_SCORES_MULTI=0 (the slots launch is off; the engine keeps the per-row calls)"); return 77; } + if (wide || wide_default) { + std::vector contexts(64, 320); + for (int m : {8, 24, 64}) { + const Result res = check(1, m, contexts.data(), 4242u + (uint32_t)m); + if (res != kPass) return res == kSkip ? 77 : 1; + } + if (wide) for (int fmt : {0, 2, 3}) { + const Result res = check(fmt, 25, contexts.data(), 9000u + (uint32_t)fmt); + if (res != kPass) return res == kSkip ? 77 : 1; + } +#if defined(STRATA_KV_FP8) + if (wide) { + const Result res = check(4, 25, contexts.data(), 9044u); + if (res != kPass) return res == kSkip ? 77 : 1; + } +#endif + std::puts("PASS: wide QSA attention matches per-row and captured replay"); + return 0; + } // contexts per row: under and over the selection width (2048 cells), partial and complete indexer blocks const int64_t ctx[8] = {1500, 37, 9000, 2049, 32768, 300, 20001, 4096}; int cases = 0, skipped = 0; // the gfx906 query swizzle declines only the int8 K/V format diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 7397636882..4ed3e035c1 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -41800,6 +41800,45 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_QSA_ATTN_BATCH64", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/core/verify.cpp", + "line": 2874, + "backend": "cuda-hip", + "fn": "env_on" + }, + { + "file": "engine/src/kernels/cuda/qsa_decode_attn.cu", + "line": 1221, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "off", + "code_default": "0", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "", + "first_date": "", + "since": "", + "last_commit": "", + "last_date": "", + "status": "opt-in", + "owner": "kernels/cuda", + "note": "Opt-in compact 64-slot QSA decode attention batching; off by default. FP8 pools retain the 8-slot path.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_QSA_CLUSTER", "backends": [