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.

nc2 S1-rest: pitched silu_mul lives in the ROCm fused_silu_mul kernel (same per-element math, PITCHED store); CUDA activation.cu variant removed (not built on ROCm)

YanissAmzf09466251604
diff --git a/exllamav3/exllamav3_ext/activation.cu b/exllamav3/exllamav3_ext/activation.cu
index b5eda93..9b4cd19 100644
--- a/exllamav3/exllamav3_ext/activation.cu
+++ b/exllamav3/exllamav3_ext/activation.cu
@@ -97,41 +97,6 @@ void silu_mul
silu_mul_gr(x, y, z, act_limit, nullptr);
}
-// nc2 S1-rest: silu_mul into z = (rows, cols) view of a (rows, pitch) buffer; x, y contiguous (rows, cols)
-void silu_mul_pitched
-(
- const at::Tensor& x,
- const at::Tensor& y,
- at::Tensor& z,
- const float act_limit
-)
-{
- const at::cuda::OptionalCUDAGuard device_guard(x.device());
- cudaStream_t stream = at::cuda::getCurrentCUDAStream().stream();
-
- bool float_input = x.dtype() == at::kFloat;
- if (float_input) { TORCH_CHECK_DTYPE(y, kFloat); }
- else { TORCH_CHECK_DTYPE(x, kHalf); TORCH_CHECK_DTYPE(y, kHalf); }
- TORCH_CHECK_DTYPE(z, kHalf);
- TORCH_CHECK(x.is_contiguous() && y.is_contiguous(), "silu_mul_pitched: x, y must be contiguous");
- TORCH_CHECK(z.dim() == 2 && z.stride(1) == 1, "silu_mul_pitched: z must be a 2D row view");
- int64_t rows = z.size(0), cols = z.size(1), pitch = z.stride(0);
- TORCH_CHECK(x.numel() == rows * cols && y.numel() == rows * cols, "silu_mul_pitched: shape mismatch");
- TORCH_CHECK(cols % 2 == 0 && pitch % 2 == 0 && pitch >= cols, "silu_mul_pitched: cols, pitch must be even, pitch >= cols");
- TORCH_CHECK(((uintptr_t) z.data_ptr()) % 4 == 0, "silu_mul_pitched: z misaligned");
- TORCH_CHECK((z.storage_offset() + (rows - 1) * pitch + cols) * 2 <= (int64_t) z.storage().nbytes(), "silu_mul_pitched: z overruns storage");
-
- size_t numel = x.numel();
- size_t blocks = CEIL_DIVIDE(numel, 2 * NUM_THREADS);
- if (float_input)
- silu_mul_pitched_kernel_f<<<blocks, NUM_THREADS, 0, stream>>>
- ((const float*) x.data_ptr(), (const float*) y.data_ptr(), (half*) z.data_ptr(), act_limit, numel, (int) (cols / 2), (int) (pitch / 2));
- else
- silu_mul_pitched_kernel_h<<<blocks, NUM_THREADS, 0, stream>>>
- ((const half*) x.data_ptr(), (const half*) y.data_ptr(), (half*) z.data_ptr(), act_limit, numel, (int) (cols / 2), (int) (pitch / 2));
- cuda_check(cudaPeekAtLastError());
-}
-
// gpt-oss clamped swiglu: (clamp(y, -limit, limit) + 1) * g * sigmoid(1.702 * g),
// g = min(x, limit) -> z, in-place if z == x or z == y
diff --git a/exllamav3/exllamav3_ext/activation.cuh b/exllamav3/exllamav3_ext/activation.cuh
index c87cbc8..68c81f6 100644
--- a/exllamav3/exllamav3_ext/activation.cuh
+++ b/exllamav3/exllamav3_ext/activation.cuh
@@ -20,14 +20,6 @@ void silu_mul
const float act_limit
);
-void silu_mul_pitched
-(
- const at::Tensor& x,
- const at::Tensor& y,
- at::Tensor& z,
- const float act_limit
-);
-
void silu_oai_mul_gr
(
const at::Tensor& x,
diff --git a/exllamav3/exllamav3_ext/activation_kernels.cuh b/exllamav3/exllamav3_ext/activation_kernels.cuh
index ee6611e..a6df688 100644
--- a/exllamav3/exllamav3_ext/activation_kernels.cuh
+++ b/exllamav3/exllamav3_ext/activation_kernels.cuh
@@ -252,81 +252,6 @@ void act_mul_kernel_f
}
-// nc2 S1-rest (EXL3_NC2_SILU_PAD): silu_mul whose output rows sit at a padded pitch, so the down projection
-// reads it without a pad copy. Inputs contiguous (rows, cols); same per-element math as act_mul_kernel_h/f<ACT_SILU>,
-// only the store address differs. cols and pitch are even (half2 units: cols2, pitch2)
-__global__ __launch_bounds__(NUM_THREADS)
-void silu_mul_pitched_kernel_h
-(
- const half* __restrict__ x,
- const half* __restrict__ y,
- half* __restrict__ z,
- const float act_limit,
- const size_t numel,
- const int cols2,
- const int pitch2
-)
-{
- size_t idx = (blockIdx.x * NUM_THREADS + threadIdx.x);
- if (idx >= numel / 2) return;
- size_t r = idx / cols2;
- size_t zi = r * pitch2 + (idx - r * cols2);
-
- half2 x2 = ((const half2*) x)[idx];
- half2 y2 = ((const half2*) y)[idx];
-
- x2 = _silu(x2);
-
- if (act_limit != 0.0f)
- {
- y2 = __hmax2(y2, __float2half2_rn(-act_limit));
- y2 = __hmin2(y2, __float2half2_rn(act_limit));
- x2 = __hmin2(x2, __float2half2_rn(act_limit));
- }
-
- ((half2*) z)[zi] = __hmul2(x2, y2);
-}
-
-__global__ __launch_bounds__(NUM_THREADS)
-void silu_mul_pitched_kernel_f
-(
- const float* __restrict__ x,
- const float* __restrict__ y,
- half* __restrict__ z,
- const float act_limit,
- const size_t numel,
- const int cols2,
- const int pitch2
-)
-{
- size_t idx = (blockIdx.x * NUM_THREADS + threadIdx.x);
- if (idx >= numel / 2) return;
- size_t r = idx / cols2;
- size_t zi = r * pitch2 + (idx - r * cols2);
-
- float2 x2 = ((const float2*) x)[idx];
- float2 y2 = ((const float2*) y)[idx];
-
- x2.x = _silu(x2.x);
- x2.y = _silu(x2.y);
-
- if (act_limit != 0.0f)
- {
- if (y2.x < -act_limit) y2.x = -act_limit;
- if (y2.y < -act_limit) y2.y = -act_limit;
- if (y2.x > act_limit) y2.x = act_limit;
- if (y2.y > act_limit) y2.y = act_limit;
- if (x2.x > act_limit) x2.x = act_limit;
- if (x2.y > act_limit) x2.y = act_limit;
- }
-
- x2.x *= y2.x;
- x2.y *= y2.y;
- half2 r2 = __float22half2_rn(x2);
- r2 = clamp_half2_to_finite(r2);
- ((half2*) z)[zi] = r2;
-}
-
__global__ __launch_bounds__(NUM_THREADS)
void xielu_kernel_f
(
diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cpp
index 248e7f2..e4cb1ee 100644
--- a/exllamav3/exllamav3_ext/bindings.cpp
+++ b/exllamav3/exllamav3_ext/bindings.cpp
@@ -252,7 +252,6 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
m.def("rope", &rope, "rope");
m.def("gen_mrope_pos_ids", &gen_mrope_pos_ids, "gen_mrope_pos_ids");
m.def("silu_mul", &silu_mul, "silu_mul");
- m.def("silu_mul_pitched", &silu_mul_pitched, "silu_mul_pitched");
m.def("silu_oai_mul", &silu_oai_mul, "silu_oai_mul");
m.def("gelu_mul", &gelu_mul, "gelu_mul");
m.def("relu2_mul", &relu2_mul, "relu2_mul");
@@ -428,6 +427,7 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
m.def("mul_sigmoid_pad", &mul_sigmoid_pad, "mul_sigmoid_pad");
m.def("pad_copy", &pad_copy, "pad_copy");
m.def("fused_silu_mul", &fused_silu_mul, "fused_silu_mul");
+ m.def("silu_mul_pitched", &silu_mul_pitched, "silu_mul_pitched");
m.def("skinny_set_onepass", &skinny_set_onepass, "skinny_set_onepass");
m.def("kda_gate", &kda_gate, "kda_gate");
m.def("kda_fb_gate", &kda_fb_gate, "kda_fb_gate");
diff --git a/exllamav3/exllamav3_ext/fused_elt.cuh b/exllamav3/exllamav3_ext/fused_elt.cuh
index e746849..305707f 100644
--- a/exllamav3/exllamav3_ext/fused_elt.cuh
+++ b/exllamav3/exllamav3_ext/fused_elt.cuh
@@ -48,6 +48,14 @@ void fused_silu_mul
double act_limit
);
+void silu_mul_pitched
+(
+ const at::Tensor& x,
+ const at::Tensor& y,
+ at::Tensor& z,
+ double act_limit
+);
+
// KDA gates: beta = bf16(sigmoid(b * beta_scale)), g = lb * sigmoid(exp(a_log) * (f + dt_bias))
// (or -exp(a_log) * softplus20(f + dt_bias) when has_lb is false). All inputs fp32.
void kda_gate
diff --git a/exllamav3/exllamav3_ext/fused_elt_rocm.cu b/exllamav3/exllamav3_ext/fused_elt_rocm.cu
index b582bd0..d894539 100644
--- a/exllamav3/exllamav3_ext/fused_elt_rocm.cu
+++ b/exllamav3/exllamav3_ext/fused_elt_rocm.cu
@@ -295,12 +295,16 @@ void pad_copy
__device__ __forceinline__ float fe_clamp_max(float v, float hi) { return v > hi ? hi : v; }
__device__ __forceinline__ float fe_clamp(float v, float lo, float hi) { return v < lo ? lo : (v > hi ? hi : v); }
-template <bool F32, bool LIM>
+// PITCHED (nc2 S1-rest, EXL3_NC2_SILU_PAD): z rows sit at row pitch `pitch` (>= cols), x / y contiguous (rows, cols);
+// same per-element math, only the store address differs
+template <bool F32, bool LIM, bool PITCHED = false>
__global__ __launch_bounds__(SM_THREADS)
-void fused_silu_mul_kernel(const void* __restrict__ x, const void* y, half* z, const size_t n, const float lim)
+void fused_silu_mul_kernel(const void* __restrict__ x, const void* y, half* z, const size_t n, const float lim,
+ const size_t cols = 0, const size_t pitch = 0)
{
for (size_t i = (size_t) blockIdx.x * SM_THREADS + threadIdx.x; i < n; i += (size_t) gridDim.x * SM_THREADS)
{
+ const size_t zi = PITCHED ? (i / cols) * pitch + i % cols : i;
if (F32)
{
float s = fe_silu(((const float*) x)[i]);
@@ -308,7 +312,7 @@ void fused_silu_mul_kernel(const void* __restrict__ x, const void* y, half* z, c
if (LIM) { s = fe_clamp_max(s, lim); yv = fe_clamp(yv, -lim, lim); }
float r = s * yv;
r = r < -65504.0f ? -65504.0f : (r > 65504.0f ? 65504.0f : r);
- z[i] = __float2half_rn(r);
+ z[zi] = __float2half_rn(r);
}
else
{
@@ -320,7 +324,7 @@ void fused_silu_mul_kernel(const void* __restrict__ x, const void* y, half* z, c
s = __half2float(__float2half_rn(fe_clamp_max(s, lim)));
yv = __half2float(__float2half_rn(fe_clamp(yv, -lim, lim)));
}
- z[i] = __float2half_rn(s * yv);
+ z[zi] = __float2half_rn(s * yv);
}
}
}
@@ -354,6 +358,40 @@ void fused_silu_mul
cuda_check(hipPeekAtLastError());
}
+// nc2 S1-rest: fused_silu_mul into z = (rows, cols) row view of a (rows, pitch) buffer; x, y contiguous (rows, cols)
+void silu_mul_pitched
+(
+ const at::Tensor& x,
+ const at::Tensor& y,
+ at::Tensor& z,
+ double act_limit
+)
+{
+ const at::Device device = x.device();
+ c10::cuda::OptionalCUDAGuard device_guard(device);
+ hipStream_t stream = c10::hip::getCurrentHIPStream(device.index()).stream();
+
+ TORCH_CHECK(x.dtype() == at::kHalf || x.dtype() == at::kFloat, "silu_mul_pitched: x must be f16/f32");
+ TORCH_CHECK(y.dtype() == x.dtype(), "silu_mul_pitched: x/y dtype mismatch");
+ TORCH_CHECK_DTYPE(z, kHalf);
+ TORCH_CHECK(x.is_contiguous() && y.is_contiguous(), "silu_mul_pitched: x, y must be contiguous");
+ TORCH_CHECK(z.dim() == 2 && z.stride(1) == 1 && z.stride(0) >= z.size(1), "silu_mul_pitched: z must be a 2D row view");
+ const size_t rows = z.size(0), cols = z.size(1), pitch = z.stride(0);
+ TORCH_CHECK(x.numel() == (int64_t) (rows * cols) && y.numel() == x.numel(), "silu_mul_pitched: shape mismatch");
+ TORCH_CHECK((z.storage_offset() + (int64_t) ((rows - 1) * pitch + cols)) * 2 <= (int64_t) z.storage().nbytes(),
+ "silu_mul_pitched: z overruns its storage");
+ const size_t n = x.numel();
+ if (n == 0) return;
+ const int blocks = (int) std::min<size_t>((n + SM_THREADS - 1) / SM_THREADS, 4096);
+ const float lim = (float) act_limit;
+ const bool f32 = x.dtype() == at::kFloat;
+ #define SM_LAUNCH(F, L) fused_silu_mul_kernel<F, L, true><<<blocks, SM_THREADS, 0, stream>>>(x.data_ptr(), y.data_ptr(), (half*) z.data_ptr(), n, lim, cols, pitch)
+ if (act_limit != 0.0) { if (f32) SM_LAUNCH(true, true); else SM_LAUNCH(false, true); }
+ else { if (f32) SM_LAUNCH(true, false); else SM_LAUNCH(false, false); }
+ #undef SM_LAUNCH
+ cuda_check(hipPeekAtLastError());
+}
+
// ------------------------------------------------------------------------------------------------
// KDA gates, one thread per g element; the first element of each head also writes beta
diff --git a/exllamav3/modules/mlp.py b/exllamav3/modules/mlp.py
index 57a8b5d..827e6cf 100644
--- a/exllamav3/modules/mlp.py
+++ b/exllamav3/modules/mlp.py
@@ -791,13 +791,17 @@ class GatedMLP(Module):
# nc2 S1-rest (EXL3_NC2_SILU_PAD, default off): silu_mul writes its rows at the down projection's
# padded pitch, so the down GEMM skips its pad_copy_in (same values, bit-exact)
a_pitch = 0
- if os.environ.get("EXL3_NC2_SILU_PAD", "0") == "1" and self.activation_fn_call is ext.silu_mul and g.is_contiguous() and u.is_contiguous():
+ # (only where ext.silu_mul itself runs fused_silu_mul, the kernel the pitched variant shares: ext.py)
+ if os.environ.get("EXL3_NC2_SILU_PAD", "0") == "1" and self.activation_fn_call is ext.silu_mul and \
+ hasattr(ext, "silu_mul_pitched") and os.environ.get("EXL3_FUSE_SILU", "1") != "0" and \
+ not self.act_limit and g.dtype == u.dtype and g.dtype in (torch.half, torch.float) and \
+ g.shape == u.shape and g.is_contiguous() and u.is_contiguous():
ik = u.shape[-1]
a_pitch = _row_pad_pitch(self.downs[s], ik, u.numel() // ik, params)
if a_pitch:
rows_ = u.numel() // ik
a = torch.empty((rows_, a_pitch), dtype = torch.half, device = u.device)[:, :ik]
- ext.silu_mul_pitched(g, u, a, self.act_limit)
+ ext.silu_mul_pitched(g, u, a, 0.0)
a = a.view(*u.shape)
else:
a = torch.empty_like(u, dtype = torch.half) if self.interm_dtype != torch.half else u