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.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
int8 sq GEMV: launch a same-shape pair (gate + up) as one kernel
YanissAmzc34103875049
diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cppindex 9a81c36..48794ee 100644--- a/exllamav3/exllamav3_ext/bindings.cpp+++ b/exllamav3/exllamav3_ext/bindings.cpp@@ -297,6 +297,7 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)m.def("g_get_cc", &g_get_cc, "g_get_cc");m.def("g_get_num_sms", &g_get_num_sms, "g_get_num_sms");m.def("exl3_gemv_int8_max_k", &exl3_gemv_int8_max_k, "exl3_gemv_int8_max_k");+ m.def("exl3_gemv_int8_pair", &exl3_gemv_int8_pair_eager, "exl3_gemv_int8_pair");m.def("exl3_moe_cpu_make_layer", &exl3_moe_cpu_make_layer, "exl3_moe_cpu_make_layer");m.def("exl3_moe_cpu_free_layer", &exl3_moe_cpu_free_layer, "exl3_moe_cpu_free_layer");m.def("exl3_moe_cpu_forward", &exl3_moe_cpu_forward, "exl3_moe_cpu_forward",@@ -455,6 +456,7 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)m.def("exl3_moe_fused_grid", &exl3_moe_fused_grid, "exl3_moe_fused_grid");m.def("exl3_moe_fused_tile_test", &exl3_moe_fused_tile_test, "exl3_moe_fused_tile_test");m.def("exl3_gemv_int8_max_k", &exl3_gemv_int8_max_k, "exl3_gemv_int8_max_k");+ m.def("exl3_gemv_int8_pair", &exl3_gemv_int8_pair_eager, "exl3_gemv_int8_pair");m.def("exl3_moe_gfx12_k3", &exl3_moe_gfx12_k3, "exl3_moe_gfx12_k3");bind_exl3_dec(m);m.def("fused_gated_rms_norm", &fused_gated_rms_norm, "fused_gated_rms_norm");diff --git a/exllamav3/exllamav3_ext/libtorch/mlp.cpp b/exllamav3/exllamav3_ext/libtorch/mlp.cppindex 877a838..bee49a0 100644--- a/exllamav3/exllamav3_ext/libtorch/mlp.cpp+++ b/exllamav3/exllamav3_ext/libtorch/mlp.cpp@@ -6,6 +6,7 @@#include "../util.h"#include "../hgemm.cuh"#include "../quant/exl3_gemm.cuh"+#include "../quant/exl3_gemv_int8.cuh"#include "../activation.cuh"#include "../add.cuh"@@ -69,8 +70,19 @@ void BC_GatedMLP::run_bszN_grat::Tensor u2 = gu_n.select(0, 1);at::Tensor gate_xh = guh_n.select(0, 0);at::Tensor up_xh = guh_n.select(0, 1);- exl3_gemm_gr(x, gate->trellis, g2, gate->suh, gate_xh, gate->svh, -1, gate->mcg, gate->mul1, 0, graph);- exl3_gemm_gr(x, up->trellis, u2, up->suh, up_xh, up->svh, -1, up->mcg, up->mul1, 0, graph);+ // Gate and up are two independent same-shape GEMVs over x: one sq launch for both when the int8+ // GEMV path takes them (same graph patch sites as the two separate launches), else two GEMMs+ bool paired = false;+ if (gate->mul1 && up->mul1 && x.dtype() == at::kHalf && g2.dtype() == u2.dtype())+ {+ cudaStream_t stream = graph ? graph->capture_stream : at::cuda::getCurrentCUDAStream().stream();+ paired = exl3_gemv_int8_pair(x, gate->trellis, g2, gate->suh, gate->svh, up->trellis, u2, up->suh, up->svh, stream, graph);+ }+ if (!paired)+ {+ exl3_gemm_gr(x, gate->trellis, g2, gate->suh, gate_xh, gate->svh, -1, gate->mcg, gate->mul1, 0, graph);+ exl3_gemm_gr(x, up->trellis, u2, up->suh, up_xh, up->svh, -1, up->mcg, up->mul1, 0, graph);+ }if (gate->bias) add_gr(g2, gate->bias.value(), g2, graph);if (up->bias) add_gr(u2, up->bias.value(), u2, graph);}diff --git a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k1.cu b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k1.cuindex da6047f..cb78354 100644--- a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k1.cu+++ b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k1.cu@@ -1,9 +1,12 @@#include "exl3_gemv_int8_instances.cuh"#include "../exl3_gemv_int8_kernel.cuh"-void* exl3_gemv_int8_sq_sel_k1(int M, bool c_fp32, bool residual)+void* exl3_gemv_int8_sq_sel_k1(int M, bool c_fp32, bool residual, bool dual){#define SELM_(M_) \+ if (dual) return residual ? nullptr \+ : c_fp32 ? (void*) exl3_gemv_int8_sq_kernel<1, M_, true, false, true> \+ : (void*) exl3_gemv_int8_sq_kernel<1, M_, false, false, true>; \if (c_fp32) return residual ? (void*) exl3_gemv_int8_sq_kernel<1, M_, true, true> \: (void*) exl3_gemv_int8_sq_kernel<1, M_, true, false>; \else return residual ? (void*) exl3_gemv_int8_sq_kernel<1, M_, false, true> \diff --git a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k2.cu b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k2.cuindex 3251dfb..1ce4480 100644--- a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k2.cu+++ b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k2.cu@@ -1,9 +1,12 @@#include "exl3_gemv_int8_instances.cuh"#include "../exl3_gemv_int8_kernel.cuh"-void* exl3_gemv_int8_sq_sel_k2(int M, bool c_fp32, bool residual)+void* exl3_gemv_int8_sq_sel_k2(int M, bool c_fp32, bool residual, bool dual){#define SELM_(M_) \+ if (dual) return residual ? nullptr \+ : c_fp32 ? (void*) exl3_gemv_int8_sq_kernel<2, M_, true, false, true> \+ : (void*) exl3_gemv_int8_sq_kernel<2, M_, false, false, true>; \if (c_fp32) return residual ? (void*) exl3_gemv_int8_sq_kernel<2, M_, true, true> \: (void*) exl3_gemv_int8_sq_kernel<2, M_, true, false>; \else return residual ? (void*) exl3_gemv_int8_sq_kernel<2, M_, false, true> \diff --git a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k3.cu b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k3.cuindex 59c86a4..eff14bd 100644--- a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k3.cu+++ b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k3.cu@@ -1,9 +1,12 @@#include "exl3_gemv_int8_instances.cuh"#include "../exl3_gemv_int8_kernel.cuh"-void* exl3_gemv_int8_sq_sel_k3(int M, bool c_fp32, bool residual)+void* exl3_gemv_int8_sq_sel_k3(int M, bool c_fp32, bool residual, bool dual){#define SELM_(M_) \+ if (dual) return residual ? nullptr \+ : c_fp32 ? (void*) exl3_gemv_int8_sq_kernel<3, M_, true, false, true> \+ : (void*) exl3_gemv_int8_sq_kernel<3, M_, false, false, true>; \if (c_fp32) return residual ? (void*) exl3_gemv_int8_sq_kernel<3, M_, true, true> \: (void*) exl3_gemv_int8_sq_kernel<3, M_, true, false>; \else return residual ? (void*) exl3_gemv_int8_sq_kernel<3, M_, false, true> \diff --git a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k4.cu b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k4.cuindex 8760ad1..47a957b 100644--- a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k4.cu+++ b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k4.cu@@ -1,9 +1,12 @@#include "exl3_gemv_int8_instances.cuh"#include "../exl3_gemv_int8_kernel.cuh"-void* exl3_gemv_int8_sq_sel_k4(int M, bool c_fp32, bool residual)+void* exl3_gemv_int8_sq_sel_k4(int M, bool c_fp32, bool residual, bool dual){#define SELM_(M_) \+ if (dual) return residual ? nullptr \+ : c_fp32 ? (void*) exl3_gemv_int8_sq_kernel<4, M_, true, false, true> \+ : (void*) exl3_gemv_int8_sq_kernel<4, M_, false, false, true>; \if (c_fp32) return residual ? (void*) exl3_gemv_int8_sq_kernel<4, M_, true, true> \: (void*) exl3_gemv_int8_sq_kernel<4, M_, true, false>; \else return residual ? (void*) exl3_gemv_int8_sq_kernel<4, M_, false, true> \diff --git a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k5.cu b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k5.cuindex 4e5b788..7e48ed2 100644--- a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k5.cu+++ b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k5.cu@@ -1,9 +1,12 @@#include "exl3_gemv_int8_instances.cuh"#include "../exl3_gemv_int8_kernel.cuh"-void* exl3_gemv_int8_sq_sel_k5(int M, bool c_fp32, bool residual)+void* exl3_gemv_int8_sq_sel_k5(int M, bool c_fp32, bool residual, bool dual){#define SELM_(M_) \+ if (dual) return residual ? nullptr \+ : c_fp32 ? (void*) exl3_gemv_int8_sq_kernel<5, M_, true, false, true> \+ : (void*) exl3_gemv_int8_sq_kernel<5, M_, false, false, true>; \if (c_fp32) return residual ? (void*) exl3_gemv_int8_sq_kernel<5, M_, true, true> \: (void*) exl3_gemv_int8_sq_kernel<5, M_, true, false>; \else return residual ? (void*) exl3_gemv_int8_sq_kernel<5, M_, false, true> \diff --git a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k6.cu b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k6.cuindex 53ea9b4..e9a1f01 100644--- a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k6.cu+++ b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_inst_sq_k6.cu@@ -1,9 +1,12 @@#include "exl3_gemv_int8_instances.cuh"#include "../exl3_gemv_int8_kernel.cuh"-void* exl3_gemv_int8_sq_sel_k6(int M, bool c_fp32, bool residual)+void* exl3_gemv_int8_sq_sel_k6(int M, bool c_fp32, bool residual, bool dual){#define SELM_(M_) \+ if (dual) return residual ? nullptr \+ : c_fp32 ? (void*) exl3_gemv_int8_sq_kernel<6, M_, true, false, true> \+ : (void*) exl3_gemv_int8_sq_kernel<6, M_, false, false, true>; \if (c_fp32) return residual ? (void*) exl3_gemv_int8_sq_kernel<6, M_, true, true> \: (void*) exl3_gemv_int8_sq_kernel<6, M_, true, false>; \else return residual ? (void*) exl3_gemv_int8_sq_kernel<6, M_, false, true> \diff --git a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_instances.cuh b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_instances.cuhindex adbf058..7223030 100644--- a/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_instances.cuh+++ b/exllamav3/exllamav3_ext/quant/comp_units/exl3_gemv_int8_instances.cuh@@ -2,12 +2,12 @@// Per-K selectors for the int8 GEMV kernel instances, each defined in its own compilation unit-void* exl3_gemv_int8_sq_sel_k1(int M, bool c_fp32, bool residual);-void* exl3_gemv_int8_sq_sel_k2(int M, bool c_fp32, bool residual);-void* exl3_gemv_int8_sq_sel_k3(int M, bool c_fp32, bool residual);-void* exl3_gemv_int8_sq_sel_k4(int M, bool c_fp32, bool residual);-void* exl3_gemv_int8_sq_sel_k5(int M, bool c_fp32, bool residual);-void* exl3_gemv_int8_sq_sel_k6(int M, bool c_fp32, bool residual);+void* exl3_gemv_int8_sq_sel_k1(int M, bool c_fp32, bool residual, bool dual);+void* exl3_gemv_int8_sq_sel_k2(int M, bool c_fp32, bool residual, bool dual);+void* exl3_gemv_int8_sq_sel_k3(int M, bool c_fp32, bool residual, bool dual);+void* exl3_gemv_int8_sq_sel_k4(int M, bool c_fp32, bool residual, bool dual);+void* exl3_gemv_int8_sq_sel_k5(int M, bool c_fp32, bool residual, bool dual);+void* exl3_gemv_int8_sq_sel_k6(int M, bool c_fp32, bool residual, bool dual);void* exl3_gemv_int8_coop_sel_k1(bool c_fp32, bool residual);void* exl3_gemv_int8_coop_sel_k2(bool c_fp32, bool residual);diff --git a/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cu b/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cuindex a5d1ff1..a350d6c 100644--- a/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cu+++ b/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cu@@ -109,16 +109,16 @@ static void* select_gemv_int8_kernel(int K, bool c_fp32, bool residual)return nullptr;}-static void* select_gemv_int8_sq_kernel(int K, int M, bool c_fp32, bool residual)+static void* select_gemv_int8_sq_kernel(int K, int M, bool c_fp32, bool residual, bool dual){switch (K){- case 1: return exl3_gemv_int8_sq_sel_k1(M, c_fp32, residual);- case 2: return exl3_gemv_int8_sq_sel_k2(M, c_fp32, residual);- case 3: return exl3_gemv_int8_sq_sel_k3(M, c_fp32, residual);- case 4: return exl3_gemv_int8_sq_sel_k4(M, c_fp32, residual);- case 5: return exl3_gemv_int8_sq_sel_k5(M, c_fp32, residual);- case 6: return exl3_gemv_int8_sq_sel_k6(M, c_fp32, residual);+ case 1: return exl3_gemv_int8_sq_sel_k1(M, c_fp32, residual, dual);+ case 2: return exl3_gemv_int8_sq_sel_k2(M, c_fp32, residual, dual);+ case 3: return exl3_gemv_int8_sq_sel_k3(M, c_fp32, residual, dual);+ case 4: return exl3_gemv_int8_sq_sel_k4(M, c_fp32, residual, dual);+ case 5: return exl3_gemv_int8_sq_sel_k5(M, c_fp32, residual, dual);+ case 6: return exl3_gemv_int8_sq_sel_k6(M, c_fp32, residual, dual);}return nullptr;}@@ -143,6 +143,13 @@ static int* gemv_int8_get_ws(int device, size_t ws_ints)return ws.ws;}+// EXL3_GEMV_PAIR=0 disables launching a GEMV pair as one kernel (default on; results are bit-identical)+static int gemv_pair_mode()+{+ static const int mode = [] { const char* e = getenv("EXL3_GEMV_PAIR"); return e ? atoi(e) : 1; }();+ return mode;+}+// m == 1 fast path: per-slice-scale kernel, regular launch. Returns false to fall through to the// cooperative kernel (and from there to the regular fp16 kernel).static bool exl3_gemv_int8_sq@@ -150,12 +157,16 @@ static bool exl3_gemv_int8_sqconst half* A_ptr, const uint16_t* B_ptr, void* C_ptr,int size_m, int size_k, int size_n, int K, bool c_fp32, bool residual,const half* suh_ptr, half* A_had_ptr, const half* svh_ptr,- int device, int num_sms, cudaStream_t stream, Graph* graph+ int device, int num_sms, cudaStream_t stream, Graph* graph,+ // Second matrix of a dual launch (same shape, K and output type), all null for a single GEMV+ const half* A2_ptr = nullptr, const uint16_t* B2_ptr = nullptr, void* C2_ptr = nullptr,+ const half* suh2_ptr = nullptr, const half* svh2_ptr = nullptr){if (size_m > 2) return false;+ const bool dual = B2_ptr != nullptr;int M = size_m;- void* fn = select_gemv_int8_sq_kernel(K, M, c_fp32, residual);+ void* fn = select_gemv_int8_sq_kernel(K, M, c_fp32, residual, dual);if (!fn) return false;int rows_max = gemv_int8_sq_rows_max(M, residual);@@ -215,13 +226,17 @@ static bool exl3_gemv_int8_sqdecomp(6 * num_sms, ksplit, rows_per);size_t smem_guess = smem_for(rows_per);int maxb;- auto occ_key = std::make_pair(fn, smem_guess);+ // A pair takes the grid, and so the slice decomposition, its single-matrix kernel would get: that keeps the pair+ // bit-identical to two launches even where the pair instantiation needs more registers (lower occupancy)+ void* fn_occ = dual ? select_gemv_int8_sq_kernel(K, M, c_fp32, residual, false) : fn;+ if (!fn_occ) return false;+ auto occ_key = std::make_pair(fn_occ, smem_guess);auto occ_it = gemv_occ_cache[device].find(occ_key);if (occ_it != gemv_occ_cache[device].end()) maxb = occ_it->second;else{maxb = 1;- cudaOccupancyMaxActiveBlocksPerMultiprocessor(&maxb, fn, NUM_THREADS, smem_guess);+ cudaOccupancyMaxActiveBlocksPerMultiprocessor(&maxb, fn_occ, NUM_THREADS, smem_guess);gemv_occ_cache[device][occ_key] = maxb;}int grid = MIN(MAX(maxb, 1) * num_sms, 1024);@@ -238,10 +253,11 @@ static bool exl3_gemv_int8_sqint rows_eff = rows_per; // the kernel takes the host's slice height (single source of truth)size_t smem = smem_for(rows_per);if (ksplit > SQ_KSPLIT_CAP) return false;- if (size_n / 256 > SQ_COUNTERS_CAP) return false;+ if ((dual ? 2 : 1) * (size_n / 256) > SQ_COUNTERS_CAP) return false; // counters, per matrix+ if ((dual ? 2 : 1) * ksplit * M > 4 * SQ_KSPLIT_CAP) return false; // (slice, row) scale entriesint pstride = size_n * (residual ? 2 : 1);- int* ws_ptr = gemv_int8_get_ws(device, SQ_WS_RESERVED + (size_t) ksplit * M * pstride);+ int* ws_ptr = gemv_int8_get_ws(device, SQ_WS_RESERVED + (size_t) (dual ? 2 : 1) * ksplit * M * pstride);if (!ws_ptr) return false;void* kernelArgs[] =@@ -256,7 +272,12 @@ static bool exl3_gemv_int8_sq(void*) &suh_ptr,(void*) &A_had_ptr,(void*) &svh_ptr,- (void*) &rows_eff+ (void*) &rows_eff,+ (void*) &A2_ptr,+ (void*) &B2_ptr,+ (void*) &C2_ptr,+ (void*) &suh2_ptr,+ (void*) &svh2_ptr};cudaError_t err = cudaLaunchKernel(fn, dim3(grid), dim3(NUM_THREADS), kernelArgs, smem, stream);@@ -267,9 +288,22 @@ static bool exl3_gemv_int8_sqreturn false;}if (exl3_gemv_int8_trace())- fprintf(stderr, "[exl3_gemv_int8] sq kernel: m=%d k=%d n=%d K=%d residual=%d grid=%d rows_per=%d ksplit=%d smem=%zu\n",- size_m, size_k, size_n, K, (int) residual, grid, rows_per, ksplit, smem);- if (graph)+ fprintf(stderr, "[exl3_gemv_int8] sq kernel: m=%d k=%d n=%d K=%d residual=%d dual=%d grid=%d rows_per=%d ksplit=%d smem=%zu\n",+ size_m, size_k, size_n, K, (int) residual, (int) dual, grid, rows_per, ksplit, smem);+ if (dual)+ {+ // Only the activation and output pointers change between calls (the weights are static); recorded+ // in the (A, C, A, C) order of two back-to-back single GEMVs so the caller's patch list is the same+ if (graph)+ {+ graph->record_param(fn, GP_gemm_A, 0);+ graph->record_param(fn, GP_gemm_C, 2);+ graph->record_param(fn, GP_gemm_A, 11);+ graph->record_param(fn, GP_gemm_C, 13);+ graph->record_param(fn, GP_end, 0);+ }+ }+ else if (graph){graph->record_param(fn, GP_gemm_A, 0);graph->record_param(fn, GP_gemm_B_trellis, 1);@@ -428,3 +462,53 @@ bool exl3_gemv_int8add_graph_args((void*) fn);return true;}++// Gate/up style pair: two GEMVs over the same activations in ONE sq launch (halves the grid fill/drain+// and the launch count; the units of both matrices share one persistent grid). Returns false to let+// the caller issue the two GEMVs separately. Bit-identical to two launches: each matrix keeps the slice height of+// its own single launch and the pair only stacks the two matrices' units (the saving is one launch and one fill/drain).+bool exl3_gemv_int8_pair+(+ const at::Tensor& A,+ const at::Tensor& B1, at::Tensor& C1, const c10::optional<at::Tensor>& suh1, const c10::optional<at::Tensor>& svh1,+ const at::Tensor& B2, at::Tensor& C2, const c10::optional<at::Tensor>& suh2, const c10::optional<at::Tensor>& svh2,+ cudaStream_t stream,+ Graph* graph+)+{+ if (gemv_pair_mode() == 0 || !exl3_gemv_int8_enabled()) return false;+ if (!suh1.has_value() || !svh1.has_value() || !suh2.has_value() || !svh2.has_value()) return false;+ if (B1.sizes() != B2.sizes() || C1.dtype() != C2.dtype()) return false;++ int K = B1.size(2) / 16;+ int size_k = A.size(-1);+ int size_n = B1.size(1) * 16;+ int size_m = A.numel() / size_k;+ if (size_n % 256 || size_k % 128) return false;++ int device;+ cudaGetDevice(&device);+ if (K < 1 || K > exl3_gemv_int8_max_k(device)) return false;+ bool residual = exl3_gemv_int8_mode() == 1;+ if (size_m > (residual ? 1 : 2)) return false;+ int num_sms = DevCtx::instance().get_num_sms(device);+ return exl3_gemv_int8_sq(+ (const half*) A.data_ptr(), (const uint16_t*) B1.data_ptr(), C1.data_ptr(),+ size_m, size_k, size_n, K, C1.dtype() == at::kFloat, residual,+ (const half*) suh1->data_ptr(), nullptr, (const half*) svh1->data_ptr(),+ device, num_sms, stream, graph,+ (const half*) A.data_ptr(), (const uint16_t*) B2.data_ptr(), C2.data_ptr(),+ (const half*) suh2->data_ptr(), (const half*) svh2->data_ptr());+}++// Python-facing form (tests, microbenchmarks): current stream, no graph recording+bool exl3_gemv_int8_pair_eager+(+ const at::Tensor& A,+ const at::Tensor& B1, at::Tensor& C1, const c10::optional<at::Tensor>& suh1, const c10::optional<at::Tensor>& svh1,+ const at::Tensor& B2, at::Tensor& C2, const c10::optional<at::Tensor>& suh2, const c10::optional<at::Tensor>& svh2+)+{+ const at::cuda::OptionalCUDAGuard device_guard(A.device());+ return exl3_gemv_int8_pair(A, B1, C1, suh1, svh1, B2, C2, suh2, svh2, at::cuda::getCurrentCUDAStream().stream(), nullptr);+}diff --git a/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cuh b/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cuhindex ed47919..9a361c1 100644--- a/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cuh+++ b/exllamav3/exllamav3_ext/quant/exl3_gemv_int8.cuh@@ -30,3 +30,20 @@ bool exl3_gemv_int8cudaStream_t stream,Graph* graph);++// Two same-shape mul1 GEMVs over one activation tensor in a single launch (see the definition).+bool exl3_gemv_int8_pair+(+ const at::Tensor& A,+ const at::Tensor& B1, at::Tensor& C1, const c10::optional<at::Tensor>& suh1, const c10::optional<at::Tensor>& svh1,+ const at::Tensor& B2, at::Tensor& C2, const c10::optional<at::Tensor>& suh2, const c10::optional<at::Tensor>& svh2,+ cudaStream_t stream,+ Graph* graph+);++bool exl3_gemv_int8_pair_eager+(+ const at::Tensor& A,+ const at::Tensor& B1, at::Tensor& C1, const c10::optional<at::Tensor>& suh1, const c10::optional<at::Tensor>& svh1,+ const at::Tensor& B2, at::Tensor& C2, const c10::optional<at::Tensor>& suh2, const c10::optional<at::Tensor>& svh2+);diff --git a/exllamav3/exllamav3_ext/quant/exl3_gemv_int8_kernel.cuh b/exllamav3/exllamav3_ext/quant/exl3_gemv_int8_kernel.cuhindex 9a8fc7a..1563993 100644--- a/exllamav3/exllamav3_ext/quant/exl3_gemv_int8_kernel.cuh+++ b/exllamav3/exllamav3_ext/quant/exl3_gemv_int8_kernel.cuh@@ -1018,7 +1018,9 @@ __host__ __device__ constexpr int gemv_int8_sq_rows_max(int M, bool residual)return cap < SQ_ROWS_MAX ? cap : SQ_ROWS_MAX;}-template <int bits, int M, bool c_fp32, bool residual>+// dual: a second matrix (B2, ...) is computed by the same launch; a separate instantiation so the single-matrix+// kernels keep their register budget (and so their occupancy)+template <int bits, int M, bool c_fp32, bool residual, bool dual = false>__global__ __launch_bounds__(NUM_THREADS)void exl3_gemv_int8_sq_kernel(@@ -1032,7 +1034,14 @@ void exl3_gemv_int8_sq_kernelconst half* __restrict__ suh,half* __restrict__ A_had,const half* __restrict__ svh,- const int rows_ovr // 0 = automatic slice height, else forced (multiple of 8)+ const int rows_ovr, // 0 = automatic slice height, else forced (multiple of 8)+ // Optional second GEMV over the same shape (B2 != nullptr): one launch computes both (e.g. gate + up+ // of a gated MLP), bit-identical to two launches+ const half* __restrict__ A2,+ const uint16_t* __restrict__ B2,+ void* __restrict__ C2,+ const half* __restrict__ suh2,+ const half* __restrict__ svh2){extern __shared__ uint32_t shmem[];@@ -1043,7 +1052,10 @@ void exl3_gemv_int8_sq_kernel// take the smallest slice height with units <= gridDim.x - but at least half-wave occupancy up// to 32 rows, since very short slices inflate ksplit and the epilogue's serial per-slice combineint rows_total = size_k >> 4;- int nb256_total = size_n / 256;+ int nb256_mat = size_n / 256;+ // A pair stacks the matrices: each (slice, 256-column) unit runs for both back to back, with the slice+ // height, and so the numerics, of a single launch+ int nb256_total = nb256_mat;int r = CEIL_DIVIDE(rows_total * nb256_total, (int) gridDim.x);int rows_per = (MAX(r, MIN(2 * r, 32)) + 7) & ~7;rows_per = MAX(rows_per, SQ_MINROWS);@@ -1067,33 +1079,49 @@ void exl3_gemv_int8_sq_kernel__shared__ float sh_red[33];__shared__ int sh_last;+ // Both matrices of a dual launch share the staged slice when they read the same activations+ // with the same input signs (the staging depends on nothing else)+ const bool share_stage = dual && A2 == A && suh2 == suh;+ const int ms = size_m * ksplit; // (slice, row) entries per matrix in qsums and the partials+int t = threadIdx.x;- int prev_slice = -1;+ int prev_key = -1;for (int unit = blockIdx.x; unit < units; unit += gridDim.x){- int slice = unit / nb256_total;- int nb256 = unit % nb256_total;+ int slice = unit / nb256_total;+ int nb256 = unit % nb256_total;+ for (int mat = 0; mat < (dual ? 2 : 1); ++mat)+ {+ const half* A_m = mat ? A2 : A;+ const uint16_t* B_m = mat ? B2 : B;+ void* C_m = mat ? C2 : C;+ const half* suh_m = mat ? suh2 : suh;+ const half* svh_m = mat ? svh2 : svh;+ int smat = share_stage ? 0 : mat;+ float* qsums_m = qsums + 4 * smat * ms;int kb0 = slice * rows_per;int nrows = MIN(rows_per, rows_total - kb0);- if (slice != prev_slice)+ int key = slice * 2 + smat;+ if (key != prev_key){- gemv_int8_stage_slice<M, residual>(A, size_m, size_k, suh, qsums + 4 * slice * M,+ gemv_int8_stage_slice<M, residual>(A_m, size_m, size_k, suh_m, qsums_m + 4 * slice * M,sh_ah, sh_as, slice_stride, sh_red, kb0, nrows);- prev_slice = slice;+ prev_key = key;}- int* pacc = partials + (size_t) slice * M * pstride;+ int* pacc = partials + ((size_t) mat * ms + (size_t) slice * M) * pstride;if constexpr (bits == 4)- gemv_int8_unit_wide<M, residual, false>(B, pacc, pstride, sh_as, slice_stride, nb256, kb0, nrows, size_n);+ gemv_int8_unit_wide<M, residual, false>(B_m, pacc, pstride, sh_as, slice_stride, nb256, kb0, nrows, size_n);else if constexpr (gemv_int8_stage_smem(bits))- gemv_int8_unit_smem<bits, M, residual, false>(B, pacc, pstride, sh_as, slice_stride, sh_b, nb256, kb0, nrows, size_n);+ gemv_int8_unit_smem<bits, M, residual, false>(B_m, pacc, pstride, sh_as, slice_stride, sh_b, nb256, kb0, nrows, size_n);else- gemv_int8_unit_narrow<bits, M, residual, false>(B, pacc, pstride, sh_as, slice_stride, nb256, kb0, nrows, size_n);+ gemv_int8_unit_narrow<bits, M, residual, false>(B_m, pacc, pstride, sh_as, slice_stride, nb256, kb0, nrows, size_n);// Completion counter: the ksplit-th contributor runs the epilogue for this 256-column group.// (sh_last reuse across iterations is ordered by the next iteration's __syncthreads.)__threadfence();__syncthreads();- if (t == 0) sh_last = (atomicAdd(&counters[nb256], 1) == ksplit - 1) ? 1 : 0;+ int* counter = counters + mat * nb256_mat + nb256;+ if (t == 0) sh_last = (atomicAdd(counter, 1) == ksplit - 1) ? 1 : 0;__syncthreads();if (sh_last){@@ -1101,10 +1129,11 @@ void exl3_gemv_int8_sq_kernel// Acquire side of the counter handshake (the partials were written by other CUs)__threadfence();#endif- gemv_int8_epilogue_group_sq<M, c_fp32, residual>(partials, qsums, pstride, ksplit, size_m,- C, svh, sh_tmp, nb256, size_n);- if (t == 0) counters[nb256] = 0;+ gemv_int8_epilogue_group_sq<M, c_fp32, residual>(partials + (size_t) mat * ms * pstride, qsums_m, pstride, ksplit,+ size_m, C_m, svh_m, sh_tmp, nb256, size_n);+ if (t == 0) *counter = 0;}+ }}}diff --git a/tests/test_gemv_pair_.py b/tests/test_gemv_pair_.pynew file mode 100644index 0000000..b484aa7--- /dev/null+++ b/tests/test_gemv_pair_.py@@ -0,0 +1,40 @@+"""+exl3_gemv_int8_pair: two same-shape mul1 GEMVs over one activation in a single sq launch, bit-identical+to two single launches (EXL3_GEMV_PAIR=0 disables the pair: the call then returns False).+Needs a CUDA device and EXL3_INT8_GEMV on (default on CUDA).+"""+import os, sys+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))+import torch+from exllamav3.ext import exllamav3_ext as ext++mode = int(os.environ.get("EXL3_GEMV_PAIR", "1"))+dev = torch.device("cuda:0")+g = torch.Generator(device = dev).manual_seed(7)+R = lambda *s: torch.randn(*s, dtype = torch.half, device = dev, generator = g)++taken = 0+for bits, k, n, m in [(4, 2048, 6144, 1), (4, 5120, 5120, 2), (3, 5120, 17408, 1), (4, 5120, 17408, 1), (5, 5120, 17408, 1), (5, 5120, 17408, 2)]:+ mk = lambda: torch.randint(-32768, 32767, (k // 16, n // 16, 16 * bits), dtype = torch.short, device = dev, generator = g)+ w1, w2 = mk(), mk()+ suh1, suh2, svh1, svh2 = R(k), R(k), R(n), R(n)+ a = R(m, k); ah = torch.empty_like(a)+ for dtype in (torch.half, torch.float):+ r1 = torch.zeros(m, n, dtype = dtype, device = dev); r2 = torch.zeros_like(r1)+ ext.exl3_gemm(a, w1, r1, suh1, ah, svh1, 0, False, True, 0)+ ext.exl3_gemm(a, w2, r2, suh2, ah, svh2, 0, False, True, 0)+ c1 = torch.zeros_like(r1); c2 = torch.zeros_like(r1)+ ok = ext.exl3_gemv_int8_pair(a, w1, c1, suh1, svh1, w2, c2, suh2, svh2)+ torch.cuda.synchronize()+ if mode == 0: assert not ok+ if not ok: continue+ taken += 1+ assert torch.equal(c1, r1) and torch.equal(c2, r2), (bits, k, n, m, dtype)+ # shared activations and input signs: one staged slice serves both matrices+ d1 = torch.zeros_like(r1); d2 = torch.zeros_like(r1); r3 = torch.zeros_like(r1)+ assert ext.exl3_gemv_int8_pair(a, w1, d1, suh1, svh1, w2, d2, suh1, svh2)+ ext.exl3_gemm(a, w2, r3, suh1, ah, svh2, 0, False, True, 0)+ torch.cuda.synchronize()+ assert torch.equal(d2, r3)+assert mode == 0 or taken > 0+print(f"gemv pair mode {mode}: ok, {taken} pairs taken")