Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

ROCm EXL3 decode GEMV (multi-row): one cross-block arrival per block for all of its rows

YanissAmzefc0bb330d8d
diff --git a/exllamav3/exllamav3_ext/quant/exl3_dec.cu b/exllamav3/exllamav3_ext/quant/exl3_dec.cu
index d82a99c..af78cac 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_dec.cu
+++ b/exllamav3/exllamav3_ext/quant/exl3_dec.cu
@@ -667,11 +667,10 @@ __device__ __forceinline__ void block_reduce(const float* acc, float* red, float
__syncthreads();
}
-// Cross-block k reduction: store this block's z to scratch, count arrivals; returns true in the
-// last arriving block (for this counter), which then holds nothing yet (caller sums partials).
-__device__ __forceinline__ bool arrive(const float* z, float* part, int ncols, int* counter, int target, int tid)
+// Cross-block arrival only (the caller has already stored its partials): fence, count, and return
+// true in the last arriving block (which then resets the counter for the next launch).
+__device__ __forceinline__ bool arrive_count(int* counter, int target, int tid)
{
- for (int col = tid; col < ncols; col += THREADS) part[col] = z[col];
__threadfence();
__syncthreads();
__shared__ int s_last;
@@ -687,6 +686,14 @@ __device__ __forceinline__ bool arrive(const float* z, float* part, int ncols, i
return true;
}
+// Cross-block k reduction: store this block's z to scratch, count arrivals; returns true in the
+// last arriving block (for this counter), which then holds nothing yet (caller sums partials).
+__device__ __forceinline__ bool arrive(const float* z, float* part, int ncols, int* counter, int target, int tid)
+{
+ for (int col = tid; col < ncols; col += THREADS) part[col] = z[col];
+ return arrive_count(counter, target, tid);
+}
+
// Ragged k split: the k extent is G groups of 8 tiles (128 inputs); block kb of kbs takes groups
// [kb * G / kbs, (kb + 1) * G / kbs), i.e. ktw = group count tiles per wave (1 .. KTW_MAX).
__device__ __forceinline__ void k_split(int G, int kb, int kbs, int& k0, int& ktw)
@@ -2413,8 +2420,37 @@ struct JobsR
int rpb; // rows per block (chunk size); grid.y = ceil(R / rpb)
int swap; // 1: grid = (row chunks, blocks), so the chunks of one weight block run side by side and share its reads
int x_row_pitch; // half-elements between consecutive rows of x (K for contiguous x)
+ int arr1; // 1: one cross-block arrival per block for all its rows (see gemv_kernel_r), else one per row
};
+// Output rotation (Hadamard + svh scale) and store of one row's reduced strip from the LDS z (waves 0-3).
+__device__ __forceinline__ void rotate_store_row(const float* z, const JobR& job, int col0, int ncols, int row, int wave, int lane)
+{
+ if (wave >= 4 || wave * 128 >= ncols) return;
+ float v[4];
+ #pragma unroll
+ for (int i = 0; i < 4; ++i) v[i] = z[wave * 128 + lane * 4 + i];
+ const int col = col0 + wave * 128 + lane * 4;
+#if !defined(EXL3_DEC_DEBUG)
+ fwht128(v, lane);
+ const uint2 sr = *reinterpret_cast<const uint2*>(job.svh + col);
+ const half* sh = reinterpret_cast<const half*>(&sr);
+ #pragma unroll
+ for (int i = 0; i < 4; ++i) v[i] *= HAD_SCALE * __half2float(sh[i]);
+#endif
+ if (job.out_fp32)
+ *reinterpret_cast<float4*>(reinterpret_cast<float*>(job.out) + (size_t) row * job.N + col) =
+ make_float4(v[0], v[1], v[2], v[3]);
+ else
+ {
+ half o[4];
+ #pragma unroll
+ for (int i = 0; i < 4; ++i) o[i] = sat_half(v[i]);
+ *reinterpret_cast<uint2*>(reinterpret_cast<half*>(job.out) + (size_t) row * job.N + col) =
+ *reinterpret_cast<uint2*>(o);
+ }
+}
+
constexpr int GEMV_R_ROW_BUDGET = MOE_R_MAX * MOE_R_KTW_MAX; // 128: max rpb * ktw
// Rows per exl3_dec_gemv_r call. One block still holds at most MOE_R_MAX rows (LDS staging, accumulators); rows beyond that run as
// further grid.y row chunks of the same launch. Every row keeps batch-1's ktw / kbs / reduction order, so the bits of a row do not
@@ -2518,6 +2554,35 @@ void gemv_kernel_r(const half* __restrict__ x, JobsR jobs, float* scratch, int*
const int col0 = strip * STRIP;
const int ncols = min(STRIP, job.N - col0);
+ if (jobs.arr1 && jobs.kbs > 1)
+ {
+ // One arrival for every row this block carries: rows differ only in their partial buffers,
+ // so a single fence + atomic (target kbs, row 0's counter slot) replaces one serialized
+ // arrival per row. Per-row reduction order (kb index order) and rotation are unchanged.
+ #pragma unroll
+ for (int j = 0; j < RM; ++j)
+ {
+ if (j >= nrows) break;
+ float xsum = 0.0f;
+ for (int c = 0; c < ktw; ++c) xsum += s_xsum[j][c];
+ block_reduce<CB>(acc[j], red, z, xsum, wave, lane, tid);
+ float* part = scratch + job.part_base + (size_t) (row0 + j) * jobs.kbs * job.N + (size_t) kb * job.N + col0;
+ for (int col = tid; col < ncols; col += THREADS) part[col] = z[col];
+ }
+ if (!arrive_count(counters + job.ctr_base + strip, jobs.kbs, tid)) return;
+ #pragma unroll
+ for (int j = 0; j < RM; ++j)
+ {
+ if (j >= nrows) break;
+ const int row = row0 + j;
+ const float* p = scratch + job.part_base + (size_t) row * jobs.kbs * job.N + col0;
+ for (int col = tid; col < ncols; col += THREADS) z[col] = sum_coherent(p + col, job.N, jobs.kbs);
+ __syncthreads();
+ rotate_store_row(z, job, col0, ncols, row, wave, lane);
+ __syncthreads();
+ }
+ return;
+ }
#pragma unroll
for (int j = 0; j < RM; ++j)
{
@@ -2541,31 +2606,7 @@ void gemv_kernel_r(const half* __restrict__ x, JobsR jobs, float* scratch, int*
__syncthreads();
}
- if (wave < 4 && wave * 128 < ncols)
- {
- float v[4];
- #pragma unroll
- for (int i = 0; i < 4; ++i) v[i] = z[wave * 128 + lane * 4 + i];
- const int col = col0 + wave * 128 + lane * 4;
-#if !defined(EXL3_DEC_DEBUG)
- fwht128(v, lane);
- const uint2 sr = *reinterpret_cast<const uint2*>(job.svh + col);
- const half* sh = reinterpret_cast<const half*>(&sr);
- #pragma unroll
- for (int i = 0; i < 4; ++i) v[i] *= HAD_SCALE * __half2float(sh[i]);
-#endif
- if (job.out_fp32)
- *reinterpret_cast<float4*>(reinterpret_cast<float*>(job.out) + (size_t) row * job.N + col) =
- make_float4(v[0], v[1], v[2], v[3]);
- else
- {
- half o[4];
- #pragma unroll
- for (int i = 0; i < 4; ++i) o[i] = sat_half(v[i]);
- *reinterpret_cast<uint2*>(reinterpret_cast<half*>(job.out) + (size_t) row * job.N + col) =
- *reinterpret_cast<uint2*>(o);
- }
- }
+ rotate_store_row(z, job, col0, ncols, row, wave, lane);
if (jobs.kbs > 1) __syncthreads();
}
}
@@ -2638,6 +2679,7 @@ static void dec_gemv_r_impl
// 5..8 rows only (the <= 4 row launches keep their grid); 9..32 rows swap by default (EXL3_GEMV_R_SWAP9=0 restores the plain grid):
// the chunks of one weight block then run side by side and share its reads through L2 instead of re-streaming the weights per chunk
jobs.swap = (env_int("EXL3_GEMV_R_CHUNK_FAST", 0) != 0 || (R > MOE_R_MAX && env_int("EXL3_GEMV_R_SWAP9", 1) != 0)) && R > 4 && R > rpb;
+ jobs.arr1 = env_int("EXL3_GEMV_R_ARR1", 1) != 0;
jobs.x_row_pitch = Kdim;
int blocks = 0, part = 0, ctr = 0;
for (int i = 0; i < n; ++i)
diff --git a/exllamav3/util/knobs.py b/exllamav3/util/knobs.py
index ce25d64..1c47516 100644
--- a/exllamav3/util/knobs.py
+++ b/exllamav3/util/knobs.py
@@ -13,6 +13,7 @@ from typing import Callable, Optional
KNOBS = {
"EXL3_DEQ_WINDOW_TRITON": "1",
"EXL3_GEMV_TRI": "1",
+ "EXL3_GEMV_R_ARR1": "1",
"EXL3_BC_KVWIN": "1",
"EXL3_CUDA_GRAPH_RING": "1",
"EXL3_DEC_MOE_KBS_A": "0",