Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

perf(qsa): early exit in block_scores_multi_kernel and skip a no-op atomic in kv_stream resolve (#783 PR-a)

Niko12211690a4a38be0
diff --git a/src/kernels/cuda/kv_stream.cu b/src/kernels/cuda/kv_stream.cu
index 9f8232e2e..209984b4f 100644
--- a/src/kernels/cuda/kv_stream.cu
+++ b/src/kernels/cuda/kv_stream.cu
@@ -111,7 +111,7 @@ __global__ void __launch_bounds__(RT) resolve_kernel(KvStreamMap m, const int32_
}
}
}
- atomicAdd(&s_lookups, lookups);
+ if (lookups > 0) atomicAdd(&s_lookups, lookups); // (#783, stuchapin909) most threads see none: skip the shared atomic
__syncthreads();
// 2. one victim per miss: a clock sweep from the hand. A slot this call uses (stamp == epoch) is never taken;
// a referenced one loses its bit as the hand passes it and is taken on the next pass.
diff --git a/src/kernels/cuda/qsa_select.cu b/src/kernels/cuda/qsa_select.cu
index bddd8476c..917cc19fe 100644
--- a/src/kernels/cuda/qsa_select.cu
+++ b/src/kernels/cuda/qsa_select.cu
@@ -749,10 +749,12 @@ __global__ void __launch_bounds__(SCORE_WARPS * 32) block_scores_multi_kernel(co
const float* __restrict__ dead,
const float* __restrict__ q_idx,
const int32_t* __restrict__ steps, int nq,
- int64_t max_blocks, float* __restrict__ out) {
+ int64_t max_blocks, float* __restrict__ out,
+ bool early_exit) {
__shared__ __align__(16) float qs[MQ * IDX_HEADS * IDX_DIM];
__shared__ int64_t s_nkv[MQ], s_nbid[MQ];
- for (int i = threadIdx.x; i < nq * IDX_HEADS * IDX_DIM; i += blockDim.x) qs[i] = q_idx[i];
+ if (!early_exit)
+ for (int i = threadIdx.x; i < nq * IDX_HEADS * IDX_DIM; i += blockDim.x) qs[i] = q_idx[i];
if (threadIdx.x < nq) {
s_nkv[threadIdx.x] = steps[threadIdx.x * kStepCount + kStepNKv];
s_nbid[threadIdx.x] = steps[threadIdx.x * kStepCount + kStepNBid];
@@ -760,6 +762,14 @@ __global__ void __launch_bounds__(SCORE_WARPS * 32) block_scores_multi_kernel(co
__syncthreads();
int64_t top = 0;
for (int q = 0; q < nq; ++q) top = s_nbid[q] > top ? s_nbid[q] : top;
+ if (early_exit) {
+ // (#783, stuchapin909) a CTA whose first block is past the batch's largest n_bid has no work: the grid is a
+ // fixed 256 CTAs and a decode window reaches only a few of them, so those return before staging the queries.
+ // Every thread of the CTA takes the same branch (top and blockIdx are CTA-uniform), so the barrier is safe.
+ if ((int64_t) blockIdx.x * SCORE_WARPS > top) return;
+ for (int i = threadIdx.x; i < nq * IDX_HEADS * IDX_DIM; i += blockDim.x) qs[i] = q_idx[i];
+ __syncthreads();
+ }
const int lane = threadIdx.x & 31;
const int64_t wstride = (int64_t) gridDim.x * SCORE_WARPS;
for (int64_t b = (int64_t) blockIdx.x * SCORE_WARPS + (threadIdx.x >> 5); b <= top && b < max_blocks; b += wstride) {
@@ -1021,8 +1031,10 @@ void qsa_block_scores(const float* pooled, const float* dead, const float* q_idx
// a block past a query's n_bid returns at once: the grid need only reach the batch's largest n_bid (C-1)
static const bool multi = [] { const char* v = std::getenv("STRATA_SCORES_MULTI"); return v == nullptr || std::atoi(v) != 0; }();
if (multi && nq <= MQ && active_blocks <= 0) { // no active count: decode (captured or not) and prefill's pooled16
+ // STRATA_QSA_EARLY_EXIT=0 keeps the old staging (every CTA loads the queries first); the scores are the same bits
+ static const bool early = [] { const char* v = std::getenv("STRATA_QSA_EARLY_EXIT"); return v == nullptr || std::atoi(v) != 0; }();
block_scores_multi_kernel<<<256, SCORE_WARPS * 32, 0, (cudaStream_t) stream>>>(pooled, dead, q_idx, steps, (int) nq,
- max_blocks, scores);
+ max_blocks, scores, early);
const cudaError_t e = cudaGetLastError();
if (e != cudaSuccess) { std::fprintf(stderr, "qsa_block_scores multi: %s\n", cudaGetErrorString(e)); std::exit(1); }
return;