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++

fix(cuda,decode): resolve sm_120 IQ3_S generic-pointer fault and restore 100% bit-exactness with main

Stuart Chapin200c35a8e98a
diff --git a/include/strata/kernels/native_mmvq.hpp b/include/strata/kernels/native_mmvq.hpp
index 9f69dd940..a1554e5d1 100644
--- a/include/strata/kernels/native_mmvq.hpp
+++ b/include/strata/kernels/native_mmvq.hpp
@@ -38,6 +38,9 @@ bool native_mmvq_multi_exact();
void native_quantize_q8_1(const float* x, void* x_q8_1, int n_in, int ncols,
void* stream);
+void native_swiglu_quantize_q8_1(const float* gate, const float* up, void* x_q8_1,
+ int n_in, int ncols, void* stream);
+
void native_q5_k_mmvq(const void* weights, const void* x_q8_1, float* y,
int n_in, int n_out, int ncols, void* stream);
diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp
index 1983cc471..e9953e300 100644
--- a/src/core/mtp.cpp
+++ b/src/core/mtp.cpp
@@ -587,8 +587,13 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stri
nsw.down_type = GGML_Q8_0; nsw.down_data = q8("mlp.shared_expert.down_proj.weight");
nsw.q8_1 = xq_;
const SForm none{};
+ const bool need_bf16_x = !shared_expert_native_bf16_enabled();
+ static const bool fuse_head_gr = [] {
+ const char* v = std::getenv("STRATA_FUSE_HEAD_GR");
+ return v != nullptr && std::atoi(v) != 0;
+ }();
for (int t = 0; t < T; ++t) {
- f32_to_bf16_bulk(mixed_ + t * N, x_bf16_, N, cs);
+ if (need_bf16_x) f32_to_bf16_bulk(mixed_ + t * N, x_bf16_, N, cs);
shared_expert(nullptr, nullptr, x_bf16_, none, nullptr, nullptr, nullptr, none, nullptr, nullptr, nullptr, none,
nullptr, nullptr, nullptr, bf16("mlp.shared_expert_gate.weight"), sh_scratch_, shared_ + t * N,
N, g.n_ff, 32, cs, mixed_ + t * N, &nsw);
@@ -596,9 +601,11 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stri
native_moe_combine(parts_ + (size_t) t * K * N, w_ + t * K, shared_ + t * N, y_ + t * N, N, K, cs);
else
moe_combine(parts_ + (size_t) t * K * N, w_ + t * K, shared_ + t * N, y_ + t * N, N, K, cs);
+ if (!fuse_head_gr)
+ gr_write(R_ + (size_t) t * HC * N, y_ + t * N, inj2_ + t * HC, gs, R_ + (size_t) t * HC * N, cs);
}
- // ---- the final mixer (with the MLP write folded in) and the main model's head
- {
+ // ---- the final mixer and the main model's head
+ if (fuse_head_gr) {
FusedGrArgs fa[kFusedGrMaxT];
for (int t = 0; t < T; ++t) {
fa[t].R = R_ + (size_t) t * HC * N; fa[t].R_out = R_ + (size_t) t * HC * N; fa[t].apply = true;
@@ -611,6 +618,12 @@ bool MtpDrafter::record_forward(int T, int step_row0, cudaStream_t cs, std::stri
fa[t].inject_out = dummy_inj_; fa[t].mixed = sample_ + t * N;
}
fused_gr_read_multi(fa, T, xn_, cs);
+ } else {
+ for (int t = 0; t < T; ++t)
+ gr_read(R_ + (size_t) t * HC * N, f32("hyper_connection_mixer.hc_norm.weight"),
+ bf16("hyper_connection_mixer.input_mix_weight_down.weight"),
+ bf16("hyper_connection_mixer.input_mix_weight_up.weight"), nullptr, EPS, gs, ss.block.gr,
+ sample_ + t * N, dummy_inj_, cs);
}
native_quantize_q8_1(sample_, xq_, (int) N, T, cs);
const bool sub = dhead_ != nullptr;
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index 1b6736b84..7e87ed583 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -627,7 +627,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
copy_from_mapped(ple_, m_ple_, (int64_t) T * N, cs);
}
float* normalized = (float*) ((uint8_t*) ss.ple.scratch + ple_block_scratch_bytes());
- const bool ple_batch_kv = dec_batch && n > 1 && ss.ple.w.key_bf16 != nullptr &&
+ static const bool ple_batch_env = [] {
+ const char* e = std::getenv("STRATA_PLE_BATCH");
+ return !e || e[0] != '0';
+ }();
+ const bool ple_batch_kv = ple_batch_env && dec_batch && n > 1 && ss.ple.w.key_bf16 != nullptr &&
ss.ple.w.value_bf16 != nullptr && ple_native_bf16_enabled() &&
ple_native_postops_enabled();
if (ple_batch_kv) {
@@ -842,7 +846,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
}
stamp(l, 16, grp);
gr_read_group(1, true, inj_, inj2_);
- const bool sh_fork = !prof_on_ && sh_cs_ != nullptr && ev_fork_ != nullptr && ev_join_ != nullptr;
+ static const bool sh_stream_env = [] {
+ const char* e = std::getenv("STRATA_SH_STREAM");
+ return !e || e[0] != '0';
+ }();
+ const bool sh_fork = sh_stream_env && !prof_on_ && sh_cs_ != nullptr && ev_fork_ != nullptr && ev_join_ != nullptr;
cudaStream_t sh_stream = sh_fork ? sh_cs_ : cs;
if (sh_fork) {
cudaEventRecord(ev_fork_, cs);
@@ -920,7 +928,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
};
// ---------------------------------------------------------------- post(l, group): experts, combine
- const bool fuse_head_gr = (le_ == g.n_layers) && (head_ != nullptr && head_->loaded()) &&
+ static const bool fuse_head_gr_env = [] {
+ const char* e = std::getenv("STRATA_FUSE_HEAD_GR");
+ return e && e[0] == '1';
+ }();
+ const bool fuse_head_gr = fuse_head_gr_env && (le_ == g.n_layers) && (head_ != nullptr && head_->loaded()) &&
!cvec().covers(g.n_layers - 1);
auto post = [&](int64_t l, int grp) -> bool {
const int tb = tb_[grp], te = te_[grp], n = te - tb;
diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu
index fc78b0651..29b687887 100644
--- a/src/kernels/cuda/iq_kernels.cu
+++ b/src/kernels/cuda/iq_kernels.cu
@@ -558,15 +558,20 @@ template<int TY> struct Split;
// per-entry ones, which call Fmt<TY>::dot per column exactly as before #242 (the launchers test kSplit at compile
// time, so the multi kernels are never instantiated for a type without a Split).
template<int TY> inline constexpr bool kSplit = false;
+template<int TY> inline constexpr bool kStageIqGrid = (TY == 16 || TY == 17 || TY == 22 || TY == 29);
+
template<> inline constexpr bool kSplit<16> = true;
template<> struct Split<16> { // IQ2_XXS
struct W { int g[8]; int ls; float dw; };
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ s_grid = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ s_grid = nullptr) {
const block_iq2_xxs* bq2 = (const block_iq2_xxs*) vbq + kbx;
const int q2 = get_int_b2(bq2->qs, iqs);
const uint8_t* aux8 = (const uint8_t*) &q2;
const uint32_t aux32 = get_int_b2(bq2->qs, iqs + 1);
- const uint2* grid_lut = s_grid ? (const uint2*) s_grid : (const uint2*) iq2xxs_grid;
+ const uint2* grid_lut;
+ if constexpr (STAGE_GRID) grid_lut = (const uint2*) s_grid;
+ else grid_lut = (const uint2*) iq2xxs_grid;
W r;
#pragma unroll
for (int k0 = 0; k0 < 8; k0 += 2) {
@@ -606,11 +611,14 @@ struct SplitLs2 {
};
template<> inline constexpr bool kSplit<17> = true;
template<> struct Split<17> : SplitLs2 { // IQ2_XS
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ s_grid = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ s_grid = nullptr) {
const block_iq2_xs* bq2 = (const block_iq2_xs*) vbq + kbx;
const int2 q2_packed = make_int2(get_int_b2(bq2->qs, iqs + 0), get_int_b2(bq2->qs, iqs + 1));
const uint16_t* q2 = (const uint16_t*) &q2_packed;
- const uint2* grid_lut = s_grid ? (const uint2*) s_grid : (const uint2*) iq2xs_grid;
+ const uint2* grid_lut;
+ if constexpr (STAGE_GRID) grid_lut = (const uint2*) s_grid;
+ else grid_lut = (const uint2*) iq2xs_grid;
W r;
r.ls0 = bq2->scales[iqs / 2] & 0x0F;
r.ls1 = bq2->scales[iqs / 2] >> 4;
@@ -629,14 +637,17 @@ template<> struct Split<17> : SplitLs2 { // IQ2_XS
};
template<> inline constexpr bool kSplit<22> = true;
template<> struct Split<22> : SplitLs2 { // IQ2_S
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ s_grid = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ s_grid = nullptr) {
const block_iq2_s* bq2 = (const block_iq2_s*) vbq + kbx;
const int qs_packed = get_int_b2(bq2->qs, iqs / 2);
const uint8_t* qs = (const uint8_t*) &qs_packed;
const int qh = bq2->qh[iqs / 2];
const int signs_packed_32 = get_int_b2(bq2->qs, QK_K / 32 + iqs / 2);
const uint8_t* signs_packed_8 = (const uint8_t*) &signs_packed_32;
- const uint64_t* grid_lut = s_grid ? (const uint64_t*) s_grid : iq2s_grid;
+ const uint64_t* grid_lut;
+ if constexpr (STAGE_GRID) grid_lut = (const uint64_t*) s_grid;
+ else grid_lut = iq2s_grid;
W r;
r.ls0 = bq2->scales[iqs / 2] & 0x0F;
r.ls1 = bq2->scales[iqs / 2] >> 4;
@@ -655,16 +666,16 @@ template<> struct Split<22> : SplitLs2 { // IQ2_S
template<> inline constexpr bool kSplit<18> = true;
template<> struct Split<18> { // IQ3_XXS
struct W { int g[8]; int ls; float dw; };
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ s_grid = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) {
const block_iq3_xxs* bq3 = (const block_iq3_xxs*) vbq + kbx;
const int2 q3_packed = make_int2(get_int_b2(bq3->qs, iqs), get_int_b2(bq3->qs, iqs + 1));
const uint8_t* q3 = (const uint8_t*) &q3_packed;
const uint32_t aux32 = get_int_b2(bq3->qs, QK_K / 16 + iqs / 2);
- const uint32_t* grid_lut = s_grid ? (const uint32_t*) s_grid : iq3xxs_grid;
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
- const int2 grid_pos = make_int2(grid_lut[q3[l0 + 0]], grid_lut[q3[l0 + 1]]);
+ const int2 grid_pos = make_int2(iq3xxs_grid[q3[l0 + 0]], iq3xxs_grid[q3[l0 + 1]]);
const uint32_t signs = unpack_ksigns(aux32 >> (7 * l0 / 2));
const int signs0 = __vcmpne4(signs & 0x08040201, 0);
r.g[l0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
@@ -687,19 +698,19 @@ template<> struct Split<18> { // IQ3_XXS
template<> inline constexpr bool kSplit<21> = true;
template<> struct Split<21> { // IQ3_S
struct W { int g[8]; int ls; float dw; };
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ s_grid = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) {
const block_iq3_s* bq3 = (const block_iq3_s*) vbq + kbx;
const int2 qs_packed = make_int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1));
const uint8_t* qs = (const uint8_t*) &qs_packed;
const int qh = bq3->qh[iqs / 2];
const int signs_packed_32 = get_int_b2(bq3->signs, iqs / 2);
const uint8_t* signs_packed_8 = (const uint8_t*) &signs_packed_32;
- const uint32_t* grid_lut = s_grid ? (const uint32_t*) s_grid : iq3s_grid;
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
- const int2 grid_pos = make_int2(grid_lut[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],
- grid_lut[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);
+ const int2 grid_pos = make_int2(iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],
+ iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);
const int signs0 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x03) << 7) | ((signs_packed_8[l0 / 2] & 0x0C) << 21), 0x00000000);
const int signs1 = __vcmpne4(((signs_packed_8[l0 / 2] & 0x30) << 3) | ((signs_packed_8[l0 / 2] & 0xC0) << 17), 0x00000000);
r.g[l0 + 0] = __vsub4(grid_pos.x ^ signs0, signs0);
@@ -721,11 +732,14 @@ template<> struct Split<21> { // IQ3_S
template<> inline constexpr bool kSplit<29> = true;
template<> struct Split<29> { // IQ1_M
struct W { int g[8]; float delta[4]; int sc0, sc1; float dw; };
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ s_grid = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ s_grid = nullptr) {
const block_iq1_m* bq1 = (const block_iq1_m*) vbq + kbx;
const int qs_packed = get_int_b4(bq1->qs, iqs);
const uint8_t* qs = (const uint8_t*) &qs_packed;
- const uint32_t* grid_lut = s_grid ? (const uint32_t*) s_grid : iq1s_grid_gpu;
+ const uint32_t* grid_lut;
+ if constexpr (STAGE_GRID) grid_lut = s_grid;
+ else grid_lut = iq1s_grid_gpu;
W r;
#pragma unroll
for (int l0 = 0; l0 < 8; l0 += 2) {
@@ -765,7 +779,8 @@ template<> struct Split<29> { // IQ1_M
template<> inline constexpr bool kSplit<20> = true;
template<> struct Split<20> { // IQ4_NL
struct W { int2 v[2]; float dw; };
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) {
const block_iq4_nl* bq4 = (const block_iq4_nl*) vbq + kbx;
W r;
#pragma unroll
@@ -788,7 +803,8 @@ template<> struct Split<20> { // IQ4_NL
template<> inline constexpr bool kSplit<23> = true;
template<> struct Split<23> { // IQ4_XS
struct W { int2 v[4]; int ls; float dw; };
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) {
const block_iq4_xs* bq4 = (const block_iq4_xs*) vbq + kbx;
W r;
#pragma unroll
@@ -814,7 +830,8 @@ template<> struct Split<23> { // IQ4_XS
template<> inline constexpr bool kSplit<42> = true;
template<> struct Split<42> { // Q2_0
struct W { int qx[4], qy[4]; float d2; };
- __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const void* __restrict__ = nullptr) {
+ template<bool STAGE_GRID = false>
+ __device__ static W load(const void* __restrict__ vbq, int kbx, int iqs, const uint32_t* __restrict__ = nullptr) {
const block_q2_0* bq2_0 = (const block_q2_0*) vbq + kbx;
W r;
r.d2 = bq2_0->d;
@@ -847,16 +864,16 @@ template<> struct Split<42> { // Q2_0
// One row against the n <= NC activations x + off[0..n) (n >= 1, warp-uniform; offsets in q8_1 blocks, 32-bit to
// spare registers), the whole warp. Per activation this is row_dot: the same calls k, lane-strided the same way,
// summed in the same order, then the same warp_sum. Only the weight side moves out of the per-activation loop.
-template<int TY, int NC, bool EXACT_N = false>
+template<int TY, int NC, bool EXACT_N = false, bool STAGE_GRID = false>
__device__ __forceinline__ void row_dot_multi(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n,
- int nb, int lane, float (&s)[NC], const void* __restrict__ s_grid = nullptr) {
+ int nb, int lane, float (&s)[NC], const uint32_t* __restrict__ s_grid = nullptr) {
using F = Fmt<TY>;
using S = Split<TY>;
#pragma unroll
for (int c = 0; c < NC; ++c) s[c] = 0.0f;
for (int k = lane; k < nb * F::ipb; k += 32) {
const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);
- const typename S::W w = S::load(row, kbx, iqs, s_grid);
+ const typename S::W w = S::template load<STAGE_GRID>(row, kbx, iqs, s_grid);
#pragma unroll
for (int c = 0; c < NC; ++c)
if (EXACT_N || c < n) s[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs);
@@ -961,58 +978,52 @@ __device__ __forceinline__ void row_dot_20_sub16(const uint8_t* row, const block
}
}
-template<int TY>
-__device__ __forceinline__ const void* stage_iq_grid(uint32_t* s_buf, int tid, int nthreads) {
- if constexpr (TY == 16) {
+template<int TY, bool STAGE_GRID = kStageIqGrid<TY>>
+__device__ __forceinline__ const uint32_t* stage_iq_grid(uint32_t* s_buf, int tid, int nthreads) {
+ if constexpr (!STAGE_GRID) {
+ return nullptr;
+ } else if constexpr (TY == 16) {
uint2* dst = (uint2*) s_buf;
const uint2* src = (const uint2*) iq2xxs_grid;
for (int i = tid; i < 256; i += nthreads) dst[i] = src[i];
__syncthreads();
- return dst;
+ return s_buf;
} else if constexpr (TY == 17) {
uint2* dst = (uint2*) s_buf;
const uint2* src = (const uint2*) iq2xs_grid;
for (int i = tid; i < 512; i += nthreads) dst[i] = src[i];
__syncthreads();
- return dst;
- } else if constexpr (TY == 18) {
- for (int i = tid; i < 256; i += nthreads) s_buf[i] = iq3xxs_grid[i];
- __syncthreads();
- return s_buf;
- } else if constexpr (TY == 21) {
- for (int i = tid; i < 512; i += nthreads) s_buf[i] = iq3s_grid[i];
- __syncthreads();
return s_buf;
} else if constexpr (TY == 22) {
uint2* dst = (uint2*) s_buf;
const uint2* src = (const uint2*) iq2s_grid;
for (int i = tid; i < 1024; i += nthreads) dst[i] = src[i];
__syncthreads();
- return dst;
+ return s_buf;
} else if constexpr (TY == 29) {
uint2* dst = (uint2*) s_buf;
const uint2* src = (const uint2*) iq1s_grid_gpu;
for (int i = tid; i < 1024; i += nthreads) dst[i] = src[i];
__syncthreads();
- return dst;
+ return s_buf;
} else {
return nullptr;
}
}
-template<int TY>
+template<int TY, bool STAGE_GRID = kStageIqGrid<TY>>
struct IqGridWords {
- static constexpr int value = (TY == 22 || TY == 29) ? 2048 : (TY == 17) ? 1024 : (TY == 16 || TY == 21) ? 512 : (TY == 18) ? 256 : 1;
+ static constexpr int value = !STAGE_GRID ? 4 : (TY == 22 || TY == 29) ? 2048 : (TY == 17) ? 1024 : (TY == 16) ? 512 : 4;
};
// mmvq_kernel with the columns taken NC at a time. After warp_sum every lane holds the same sum, so lane c stores
// column c.
-template<int TY, int NC>
+template<int TY, int NC, bool STAGE_GRID = kStageIqGrid<TY>>
__global__ void __launch_bounds__(128) mmvq_multi_kernel(const uint8_t* __restrict__ w, size_t row_bytes,
const block_q8_1* __restrict__ x, float* __restrict__ y,
int n_in, int n_out, int ncols) {
- __shared__ uint32_t s_grid_buf[IqGridWords<TY>::value];
- const void* s_grid = stage_iq_grid<TY>(s_grid_buf, threadIdx.y * 32 + threadIdx.x, 128);
+ __shared__ alignas(16) uint32_t s_grid_buf[IqGridWords<TY, STAGE_GRID>::value];
+ const uint32_t* s_grid = stage_iq_grid<TY, STAGE_GRID>(s_grid_buf, threadIdx.y * 32 + threadIdx.x, 128);
const int row = blockIdx.x * 4 + threadIdx.y;
if (row >= n_out) return;
const int lane = threadIdx.x;
@@ -1024,7 +1035,7 @@ __global__ void __launch_bounds__(128) mmvq_multi_kernel(const uint8_t* __restri
#pragma unroll
for (int c = 0; c < NC; ++c) off[c] = (c0 + min(c, n - 1)) * xb;
float s[NC];
- row_dot_multi<TY, NC>(wr, x, off, n, nb, lane, s, s_grid);
+ row_dot_multi<TY, NC, false, STAGE_GRID>(wr, x, off, n, nb, lane, s, s_grid);
#pragma unroll
for (int c = 0; c < NC; ++c)
if (c < n && lane == c) y[(size_t) (c0 + c) * n_out + row] = s[c];
@@ -1070,7 +1081,7 @@ __global__ void __launch_bounds__(256) native_gu_kernel(const unsigned long long
// has halves of <= 4), so 4 takes such windows in one pass; 8 would take ~64-80 registers against ~48 (ptxas -v).
constexpr int GRP_NC = 4;
-template<int TG>
+template<int TG, bool STAGE_GRID = kStageIqGrid<TG>>
__global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned long long* __restrict__ grp_ptr,
const int32_t* __restrict__ grp_start,
const int32_t* __restrict__ n_groups,
@@ -1079,8 +1090,8 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lon
float* __restrict__ gate, float* __restrict__ up) {
const int ng = *n_groups;
if (blockIdx.y >= ng) return;
- __shared__ uint32_t s_grid_buf[IqGridWords<TG>::value];
- const void* s_grid = stage_iq_grid<TG>(s_grid_buf, threadIdx.x, 256);
+ __shared__ alignas(16) uint32_t s_grid_buf[IqGridWords<TG, STAGE_GRID>::value];
+ const uint32_t* s_grid = stage_iq_grid<TG, STAGE_GRID>(s_grid_buf, threadIdx.x, 256);
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int row = blockIdx.x * GU_ROWS + warp; // 0 .. 2*n_ff
if (row >= 2 * L.n_ff) return;
@@ -1097,19 +1108,19 @@ __global__ void __launch_bounds__(256) native_gu_multi_kernel(const unsigned lon
if (n == 1) {
const int off1[1] = { ent_tok[e] * xb };
float s1[1];
- row_dot_multi<TG, 1, true>(wr, xq, off1, 1, nb, lane, s1, s_grid);
+ row_dot_multi<TG, 1, true, STAGE_GRID>(wr, xq, off1, 1, nb, lane, s1, s_grid);
if (lane == 0) dst[(size_t) e * L.n_ff + r] = s1[0];
} else if (n == 2) {
const int off2[2] = { ent_tok[e] * xb, ent_tok[e + 1] * xb };
float s2[2];
- row_dot_multi<TG, 2, true>(wr, xq, off2, 2, nb, lane, s2, s_grid);
+ row_dot_multi<TG, 2, true, STAGE_GRID>(wr, xq, off2, 2, nb, lane, s2, s_grid);
if (lane < 2) dst[(size_t) (e + lane) * L.n_ff + r] = s2[lane];
} else {
int off[GRP_NC];
#pragma unroll
for (int c = 0; c < GRP_NC; ++c) off[c] = ent_tok[e + min(c, n - 1)] * xb;
float s[GRP_NC];
- row_dot_multi<TG, GRP_NC>(wr, xq, off, n, nb, lane, s, s_grid);
+ row_dot_multi<TG, GRP_NC, false, STAGE_GRID>(wr, xq, off, n, nb, lane, s, s_grid);
#pragma unroll
for (int c = 0; c < GRP_NC; ++c)
if (c < n && lane == c) dst[(size_t) (e + c) * L.n_ff + r] = s[c];
@@ -1671,6 +1682,11 @@ bool env_on(const char* name) {
}
// STRATA_OLD_IQ_MMVQ=1 keeps the per-column kernels (bitwise equal to the new ones; kept for A/B timing)
bool g_old_kernels = env_on("STRATA_OLD_IQ_MMVQ");
+// STRATA_IQ_STAGE_GRID=0 disables staging 64-bit i-quant codebook tables into shared memory
+bool g_stage_grid = [] {
+ const char* v = std::getenv("STRATA_IQ_STAGE_GRID");
+ return v == nullptr || v[0] == '\0' || v[0] != '0';
+}();
template<int TY>
void launch_mmvq(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int n_in, int n_out, int ncols,
@@ -1678,10 +1694,24 @@ void launch_mmvq(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int
const dim3 grid((unsigned) ((n_out + 3) / 4)), block(32, 4);
if constexpr (!kSplit<TY>) mmvq_kernel<TY><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
else if (g_old_kernels) mmvq_kernel<TY><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
- else if (ncols <= 1) mmvq_multi_kernel<TY, 1><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
- else if (ncols == 2) mmvq_multi_kernel<TY, 2><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
- else if (ncols <= 4) mmvq_multi_kernel<TY, 4><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
- else mmvq_multi_kernel<TY, 8><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); // 8 at a time past 8
+ else if constexpr (kStageIqGrid<TY>) {
+ if (!g_stage_grid) {
+ if (ncols <= 1) mmvq_multi_kernel<TY, 1, false><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else if (ncols == 2) mmvq_multi_kernel<TY, 2, false><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else if (ncols <= 4) mmvq_multi_kernel<TY, 4, false><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else mmvq_multi_kernel<TY, 8, false><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ return;
+ }
+ if (ncols <= 1) mmvq_multi_kernel<TY, 1><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else if (ncols == 2) mmvq_multi_kernel<TY, 2><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else if (ncols <= 4) mmvq_multi_kernel<TY, 4><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else mmvq_multi_kernel<TY, 8><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ } else {
+ if (ncols <= 1) mmvq_multi_kernel<TY, 1><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else if (ncols == 2) mmvq_multi_kernel<TY, 2><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else if (ncols <= 4) mmvq_multi_kernel<TY, 4><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols);
+ else mmvq_multi_kernel<TY, 8><<<grid, block, 0, s>>>(W, rb, X, y, n_in, n_out, ncols); // 8 at a time past 8
+ }
}
template<int TG>
@@ -1690,7 +1720,13 @@ void launch_gu(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, con
float* gate, float* up) {
if constexpr (!kSplit<TG>) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
else if (g_old_kernels) native_gu_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
- else native_gu_multi_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
+ else if constexpr (kStageIqGrid<TG>) {
+ if (!g_stage_grid) {
+ native_gu_multi_kernel<TG, false><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
+ return;
+ }
+ native_gu_multi_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
+ } else native_gu_multi_kernel<TG><<<grid, 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);
}
template<int TD>
diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cu
index 147f385d1..fc1a95955 100644
--- a/src/kernels/cuda/native_mmvq.cu
+++ b/src/kernels/cuda/native_mmvq.cu
@@ -152,6 +152,22 @@ __global__ void native_quantize_q8_1_kernel(const float* __restrict__ x,
if (i % Q8K == 0) y[i / Q8K].ds = q8_1_ds(d, sum);
}
+__launch_bounds__(QUANT_THREADS, 1)
+__global__ void native_swiglu_quantize_q8_1_kernel(const float* __restrict__ gate,
+ const float* __restrict__ up,
+ Q81Block* __restrict__ y, int n_in) {
+ const int i = int(blockIdx.x) * QUANT_THREADS + int(threadIdx.x);
+ if (i >= n_in) return; // n_in is a multiple of 32: only whole warps return.
+ const float gi = gate[i];
+ const float xi = __fmul_rn(__fdividef(gi, __fadd_rn(1.0f, __expf(-gi))), up[i]);
+ const float amax = warp_max(fabsf(xi));
+ const float sum = warp_sum(xi);
+ const float d = amax / 127.0f;
+ const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
+ y[i / Q8K].qs[i % Q8K] = q;
+ if (i % Q8K == 0) y[i / Q8K].ds = make_half2(d, sum);
+}
+
// Exact pinned vec_dot_q5_K_q8_1_impl_vmmq expression and integer dot order.
__device__ __forceinline__ float q5_q8_dot_impl(
const int* __restrict__ vl, const int* __restrict__ vh, const int* __restrict__ u,
@@ -1165,6 +1181,21 @@ void native_quantize_q8_1(const float* x, void* x_q8_1, int n_in, int ncols, voi
launch_check();
}
+void native_swiglu_quantize_q8_1(const float* gate, const float* up, void* x_q8_1,
+ int n_in, int ncols, void* stream) {
+ validate_shape(n_in, ncols);
+ validate_pointer(gate);
+ validate_pointer(up);
+ validate_pointer(x_q8_1);
+ validate_stream(stream);
+ const int n_total = n_in * ncols;
+ const unsigned blocks = unsigned((std::size_t(n_total) + QUANT_THREADS - 1) / QUANT_THREADS);
+ native_swiglu_quantize_q8_1_kernel<<<blocks, QUANT_THREADS, 0,
+ static_cast<cudaStream_t>(stream)>>>(
+ gate, up, static_cast<Q81Block*>(x_q8_1), n_total);
+ launch_check();
+}
+
void native_q5_k_mmvq(const void* weights, const void* x_q8_1, float* y,
int n_in, int n_out, int ncols, void* stream) {
validate_shape(n_in, ncols, QK);
diff --git a/src/kernels/cuda/shared_expert.cu b/src/kernels/cuda/shared_expert.cu
index d4479cbd2..28bb7edc1 100644
--- a/src/kernels/cuda/shared_expert.cu
+++ b/src/kernels/cuda/shared_expert.cu
@@ -161,32 +161,6 @@ void shared_expert_set_native_bf16(bool enabled) { native_bf16 = enabled; }
bool shared_expert_native_bf16_enabled() { return native_bf16; }
namespace {
-struct SharedQ81Block {
- half2 ds;
- int8_t qs[32];
-};
-
-__launch_bounds__(256, 1)
-__global__ void native_swiglu_quantize_q8_1_kernel(const float* __restrict__ gate,
- const float* __restrict__ up,
- SharedQ81Block* __restrict__ y, int n_in) {
- const int i = int(blockIdx.x) * 256 + int(threadIdx.x);
- if (i >= n_in) return;
- const float gi = gate[i];
- const float xi = __fdividef(gi, 1.0f + __expf(-gi)) * up[i];
- float amax = fabsf(xi);
- float sum = xi;
-#pragma unroll
- for (int offset = 16; offset > 0; offset >>= 1) {
- amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, offset, 32));
- sum += __shfl_xor_sync(0xffffffffu, sum, offset, 32);
- }
- const float d = amax / 127.0f;
- const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
- y[i / 32].qs[i % 32] = q;
- if ((i & 31) == 0) y[i / 32].ds = make_half2(d, sum);
-}
-
__global__ void scale_rows_kernel(float* __restrict__ out, const float* __restrict__ g, int n) {
const int t = blockIdx.y;
const int i = blockIdx.x * blockDim.x + threadIdx.x;
@@ -201,6 +175,14 @@ __global__ void sigmoid_scale_rows_kernel(float* __restrict__ out, const float*
out[(size_t) t * n + i] *= gt;
}
}
+
+bool fused_swiglu_q81_enabled() {
+ static const bool on = [] {
+ const char* v = std::getenv("STRATA_FUSED_SWIGLU_Q81");
+ return v == nullptr || std::atoi(v) != 0;
+ }();
+ return on;
+}
} // namespace
void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, const NativeSharedWeights& nw,
@@ -217,8 +199,12 @@ void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, cons
native_mmvq(nw.gate_type, nw.gate_data, x_q8_1, gate, (int) n_embd, (int) n_ff, n_tok, stream);
native_mmvq(nw.up_type, nw.up_data, x_q8_1, up, (int) n_embd, (int) n_ff, n_tok, stream);
const int n = (int) (n_ff * n_tok);
- native_swiglu_quantize_q8_1_kernel<<<(unsigned) ((n + 255) / 256), 256, 0, cs>>>(
- gate, up, (SharedQ81Block*) nw.q8_1, n);
+ if (fused_swiglu_q81_enabled()) {
+ native_swiglu_quantize_q8_1(gate, up, nw.q8_1, (int) n_ff, n_tok, stream);
+ } else {
+ native_swiglu_kernel<<<(unsigned) ((n + THREADS - 1) / THREADS), THREADS, 0, cs>>>(gate, up, gate, n);
+ native_quantize_q8_1(gate, nw.q8_1, (int) n_ff, n_tok, stream);
+ }
native_mmvq(nw.down_type, nw.down_data, nw.q8_1, out, (int) n_ff, (int) n_embd, n_tok, stream);
static const bool batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
if (native_bf16 && batch && n_tok > 1) { // one gemv for all rows (outputs identical), fused sigmoid+scale
@@ -320,9 +306,8 @@ void shared_expert(const uint8_t* x_q8_0, const uint8_t* x_q8k, const uint16_t*
native_mmvq(native->up_type, native->up_data, native->q8_1, up, (int) n_embd, (int) n_ff, 1, stream);
else
gemv(up_form, up_codes, up_scales, up_off, x_q8_0, x_q8k, up, n_embd, n_ff);
- if (native_projection && native_down) {
- native_swiglu_quantize_q8_1_kernel<<<(unsigned) ((n_ff + 255) / 256), 256, 0, (cudaStream_t) stream>>>(
- gate, up, (SharedQ81Block*) native->q8_1, (int) n_ff);
+ if (native_projection && native_down && fused_swiglu_q81_enabled()) {
+ native_swiglu_quantize_q8_1(gate, up, native->q8_1, (int) n_ff, 1, stream);
native_mmvq(native->down_type, native->down_data, native->q8_1, out, (int) n_ff, (int) n_embd, 1, stream);
} else {
if (native_projection)
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index 8a5bcabc6..c26a81340 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -478,7 +478,11 @@ void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const
std::fprintf(stderr, "gdn_step_norm_multi: invalid arguments\n");
std::exit(1);
}
- if (n_keep != nullptr && t_out_begin >= n_tok) {
+ static const bool commit_split = [] {
+ const char* e = std::getenv("STRATA_GDN_COMMIT_SPLIT");
+ return !e || e[0] != '0';
+ }();
+ if (commit_split && n_keep != nullptr && t_out_begin >= n_tok) {
gdn_step_commit_kernel<<<dim3((unsigned) h_v, 4u), dim3(32, RG), 0, (cudaStream_t) stream>>>(
state, h, conv_channels, gate, beta, h_k, h_v, n_keep);
} else {