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

Pascal (sm_60): bring the engine up on compute capability 6.0

ruibeikaae200e08f5499
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 10cdc871b..4c430ed33 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -68,10 +68,15 @@ if(STRATA_ENABLE_CUDA)
if(_base STREQUAL "native" OR _base STREQUAL "all" OR _base STREQUAL "all-major")
continue()
endif()
- if(_base LESS 75)
+ # STRATA_EXPERIMENTAL_SM60: a Pascal (sm_60, GP100-class) build. It sits below every architecture
+ # these kernels were written for, so two intrinsics are replaced for it - `__dp4a` (6.1+) and
+ # `__nanosleep` (7.0+) - in include/strata/kernels/dp4a.hpp, which is bit-exact for the first and
+ # merely a shorter pause for the second. RTX 20/30/40/50 cards must use the release engine instead.
+ if(_base LESS 75 AND NOT STRATA_EXPERIMENTAL_SM60)
message(FATAL_ERROR
"Strata needs an NVIDIA GPU of compute capability 7.5 or newer (RTX 20 / 30 / 40 / 50 series), but "
- "CMAKE_CUDA_ARCHITECTURES is '${CMAKE_CUDA_ARCHITECTURES}'.")
+ "CMAKE_CUDA_ARCHITECTURES is '${CMAKE_CUDA_ARCHITECTURES}'. (A Pascal build is "
+ "-DSTRATA_EXPERIMENTAL_SM60=ON; upstream does not support it.)")
endif()
# #220 / #224: RTX 50 engines built with CUDA 12.8 crashed in the prompt path on Linux (13.0 and newer did not)
if(_base GREATER_EQUAL 120 AND CMAKE_CUDA_COMPILER_VERSION VERSION_LESS 13.0)
diff --git a/include/strata/kernels/dp4a.hpp b/include/strata/kernels/dp4a.hpp
new file mode 100644
index 000000000..cbca6a9aa
--- /dev/null
+++ b/include/strata/kernels/dp4a.hpp
@@ -0,0 +1,42 @@
+// include/strata/kernels/dp4a.hpp - the two instructions the kernels use that Pascal (sm_60) does not have.
+//
+// The engine is written for compute capability 7.5 and newer and refuses older cards at CMake configure time
+// (a Pascal build passes -DSTRATA_EXPERIMENTAL_SM60=ON). A GP100-class card is compute capability 6.0, which is
+// missing exactly two things the i-quant kernels rely on:
+//
+// * `__dp4a`, a byte-wise dot product, available from 6.1 (Pascal GP10x/GV11x). Its fallback below is
+// llama.cpp's own (`ggml/src/ggml-cuda/common.cuh`), which is the reference for the kernels in this
+// directory: they are transcribed from llama.cpp's vecdotq.cuh, and that wrapper's operands are the same
+// sites. It reinterprets the operands as SIGNED bytes and accumulates in int32, which is what `__dp4a`
+// does for these calls, so the fallback is bit-exact rather than merely close.
+// * `__nanosleep`, available from 7.0 (Volta). It only paces single-thread doorbell waits, so a loop that
+// spins without it is correct, merely busier.
+//
+// Both are compile-time selections on `__CUDA_ARCH__`, so one source tree builds for Pascal and for RTX
+// 20/30/40/50 without a #define at the call sites. A HIP build never takes either fallback: `__CUDA_ARCH__` is
+// undefined there, so `STRATA_DP4A` is `__dp4a` (which `hip_compat/intrinsics.hpp` provides) and the pause is
+// HIP's own `__nanosleep`.
+#pragma once
+
+#include <cstdint>
+
+#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 610
+__device__ __forceinline__ int strata_dp4a(const int a, const int b, const int c) {
+ const int8_t* a8 = (const int8_t*) &a;
+ const int8_t* b8 = (const int8_t*) &b;
+ return c + a8[0] * b8[0] + a8[1] * b8[1] + a8[2] * b8[2] + a8[3] * b8[3];
+}
+#define STRATA_DP4A(a, b, c) strata_dp4a((a), (b), (c))
+#else
+#define STRATA_DP4A(a, b, c) __dp4a((a), (b), (c))
+#endif
+
+/// Yield the thread while a doorbell flag is being polled. The length of the pause is a backoff hint, not a
+/// contract, and only pre-Volta CUDA has no `__nanosleep` at all.
+__device__ __forceinline__ void strata_spin_pause() {
+#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700
+ // sm_6x: the loop spins. Every call site is a single-thread doorbell wait, so nothing else is delayed.
+#else
+ __nanosleep(100);
+#endif
+}
diff --git a/src/kernels/cuda/elementwise.cu b/src/kernels/cuda/elementwise.cu
index a2e9e53b5..69eb97e21 100644
--- a/src/kernels/cuda/elementwise.cu
+++ b/src/kernels/cuda/elementwise.cu
@@ -1,5 +1,6 @@
// src/kernels/cuda/elementwise.cu - P2.S5's glue kernels. See the header for why each exists.
#include "strata/kernels/elementwise.hpp"
+#include "strata/kernels/dp4a.hpp"
#include "strata/kernels/bf16_bits.hpp"
#include "strata/kernels/f16_bits.hpp"
@@ -208,7 +209,7 @@ __global__ void doorbell_ring_kernel(uint32_t* seq) {
__global__ void doorbell_wait_kernel(const volatile uint32_t* flag, const volatile uint32_t* seq) {
const uint32_t want = *seq;
- while (*flag != want) __nanosleep(100);
+ while (*flag != want) strata_spin_pause();
__threadfence_system();
}
diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu
index f56e152bc..6f1228bb0 100644
--- a/src/kernels/cuda/fused_gr.cu
+++ b/src/kernels/cuda/fused_gr.cu
@@ -342,7 +342,20 @@ void fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, voi
// fits the 48 KB default (4 tokens of the CUDA tile; all 8 of HIP's smaller tile). The down kernel's
// outputs (lo, inject_out) are strictly per-token, so the chunk boundaries are safe, and the up kernel
// below still sees every token of the batch in one launch.
- const int capacity = (optin > 0 ? optin : 48 * 1024) / (int) (TILE * sizeof(float));
+ //
+ // The opt-in is a promise a pre-Volta card does not keep: an sm_60 answers 65536 and accepts the
+ // cudaFuncSetAttribute for 61440 B, then fails the LAUNCH with "invalid argument". What such a card
+ // will launch is its per-block limit, so the capacity comes from that below sm_70 - the same 4 tokens
+ // the "no opt-in" branch assumes, but taken from the attribute that is actually enforced.
+#if defined(__HIPCC__)
+ const int usable = optin > 0 ? optin : 48 * 1024;
+#else
+ int cc = 0, per_block = 0;
+ cudaDeviceGetAttribute(&cc, cudaDevAttrComputeCapabilityMajor, dev);
+ cudaDeviceGetAttribute(&per_block, cudaDevAttrMaxSharedMemoryPerBlock, dev);
+ const int usable = (cc >= 7 && optin > 0) ? optin : per_block;
+#endif
+ const int capacity = usable / (int) (TILE * sizeof(float));
chunk[dev] = capacity < 1 ? 1 : (capacity > kFusedGrMaxT ? kFusedGrMaxT : capacity);
attr[dev] = true;
}
diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu
index ea05d9d23..705ff06e2 100644
--- a/src/kernels/cuda/iq_kernels.cu
+++ b/src/kernels/cuda/iq_kernels.cu
@@ -5,6 +5,7 @@
// MIT license, third_party/ggml/LICENSE). The block structs and codebook grids come from its ggml-common.h,
// included unchanged.
#include "strata/kernels/iq_kernels.hpp"
+#include "strata/kernels/dp4a.hpp"
#include <cuda_fp16.h>
#include <cuda_runtime.h>
@@ -50,7 +51,7 @@ __device__ __forceinline__ int2 get_int_from_table_16(const int& q4, const int8_
}
return make_int2(__byte_perm(tmp[0], tmp[1], 0x6420), __byte_perm(tmp[0], tmp[1], 0x7531));
}
-#define ggml_cuda_dp4a(a, b, c) __dp4a((a), (b), (c))
+#define ggml_cuda_dp4a(a, b, c) STRATA_DP4A((a), (b), (c))
// ---------------------------------------------------------------- the dot products (vecdotq.cuh)
__device__ __forceinline__ float vec_dot_q2_0_q8_1(const void* __restrict__ vbq, const block_q8_1* __restrict__ bq8_1,
diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cu
index a7801fa72..219576f79 100644
--- a/src/kernels/cuda/native_mmvq.cu
+++ b/src/kernels/cuda/native_mmvq.cu
@@ -24,6 +24,7 @@
// SOFTWARE.
#include "strata/kernels/native_mmvq.hpp"
+#include "strata/kernels/dp4a.hpp"
#include "strata/kernels/iq_kernels.hpp"
#include <cuda_fp16.h>
@@ -165,8 +166,8 @@ __device__ __forceinline__ float q5_q8_dot_impl(
const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
const int v0i = vl0i | vh0i;
const int v1i = vl1i | vh1i;
- const int dot1 = __dp4a(v0i, u[2 * i], __dp4a(v1i, u[2 * i + 1], 0));
- const int dot2 = __dp4a(0x01010101, u[2 * i], __dp4a(0x01010101, u[2 * i + 1], 0));
+ const int dot1 = STRATA_DP4A(v0i, u[2 * i], STRATA_DP4A(v1i, u[2 * i + 1], 0));
+ const int dot2 = STRATA_DP4A(0x01010101, u[2 * i], STRATA_DP4A(0x01010101, u[2 * i + 1], 0));
sumf_d += d8[i] * (dot1 * sc[i]);
sumf_m += d8[i] * (dot2 * m[i]);
}
@@ -274,8 +275,8 @@ __device__ __forceinline__ float q2_q8_dot(const Q20Block* __restrict__ w,
const int qo = __byte_perm(0x020100ff, 0x020100ff, q >> 2);
const int qx = __byte_perm(qe, qo, 0x5140);
const int qy = __byte_perm(qe, qo, 0x7362);
- sumi = __dp4a(u, qx, sumi);
- sumi = __dp4a(v, qy, sumi);
+ sumi = STRATA_DP4A(u, qx, sumi);
+ sumi = STRATA_DP4A(v, qy, sumi);
}
const float d8 = __low2float(chunk->ds);
return d2 * d8 * sumi;
@@ -348,7 +349,7 @@ __device__ __forceinline__ float q3_q8_dot_impl(int vl, int vh, const int* __res
const int vil = (vl >> (2 * i)) & 0x03030303;
const int vih = ((vh >> i) << 2) & 0x04040404;
const int vi = __vsubss4(vil, vih);
- sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc);
+ sumf += d8[i] * (STRATA_DP4A(vi, u[i], 0) * sc);
}
return d3 * sumf;
}
@@ -441,8 +442,8 @@ __device__ __forceinline__ float iq4_xs_q8_dot(const IQ4XSBlock* __restrict__ w,
const int2 v = iq4_table_lookup(aux_q4);
const int u0 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j];
const int u1 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j + 4];
- sumi = __dp4a(v.x, u0, sumi);
- sumi = __dp4a(v.y, u1, sumi);
+ sumi = STRATA_DP4A(v.x, u0, sumi);
+ sumi = STRATA_DP4A(v.y, u1, sumi);
}
const int ls = ((w->scales_l[iqs / 8] >> (iqs & 0x04)) & 0x0f) |
(((w->scales_h >> (iqs / 2)) & 0x03) << 4);
@@ -501,8 +502,8 @@ __device__ __forceinline__ float q4_q8_dot_impl(
for (int i = 0; i < 2; ++i) {
const int v0i = (v[0] >> (4 * i)) & 0x0f0f0f0f;
const int v1i = (v[1] >> (4 * i)) & 0x0f0f0f0f;
- const int dot1 = __dp4a(v1i, u[2 * i + 1], __dp4a(v0i, u[2 * i], 0));
- const int dot2 = __dp4a(0x01010101, u[2 * i + 1], __dp4a(0x01010101, u[2 * i], 0));
+ const int dot1 = STRATA_DP4A(v1i, u[2 * i + 1], STRATA_DP4A(v0i, u[2 * i], 0));
+ const int dot2 = STRATA_DP4A(0x01010101, u[2 * i + 1], STRATA_DP4A(0x01010101, u[2 * i], 0));
sumf_d += d8[i] * (dot1 * sc[i]);
sumf_m += d8[i] * (dot2 * m[i]);
}
@@ -599,7 +600,7 @@ __device__ __forceinline__ float q6_q8_dot_impl(int vl, int vh, const int* __res
const int vil = (vl >> (4 * i)) & 0x0f0f0f0f;
const int vih = ((vh >> (4 * i)) << 4) & 0x30303030;
const int vi = __vsubss4(vil | vih, 0x20202020);
- sumf += d8[i] * (__dp4a(vi, u[i], 0) * sc);
+ sumf += d8[i] * (STRATA_DP4A(vi, u[i], 0) * sc);
}
return d * sumf;
}
@@ -671,8 +672,8 @@ __device__ __forceinline__ float small_q8_dot(const Q40Block* __restrict__ w,
const int v = load_int_b2(w->qs, iqs + i);
const int vi0 = (v >> 0) & 0x0f0f0f0f;
const int vi1 = (v >> 4) & 0x0f0f0f0f;
- sumi = __dp4a(vi0, reinterpret_cast<const int*>(x->qs)[iqs + i], sumi);
- sumi = __dp4a(vi1, reinterpret_cast<const int*>(x->qs)[iqs + i + 4], sumi);
+ sumi = STRATA_DP4A(vi0, reinterpret_cast<const int*>(x->qs)[iqs + i], sumi);
+ sumi = STRATA_DP4A(vi1, reinterpret_cast<const int*>(x->qs)[iqs + i + 4], sumi);
}
const float2 ds = __half22float2(x->ds);
const float d = w->d;
@@ -691,13 +692,13 @@ __device__ __forceinline__ float small_q8_dot(const Q50Block* __restrict__ w,
vi0 |= (vh << 11) & 0x00001000;
vi0 |= (vh << 18) & 0x00100000;
vi0 |= (vh << 25) & 0x10000000;
- sumi = __dp4a(vi0, reinterpret_cast<const int*>(x->qs)[iqs + i], sumi);
+ sumi = STRATA_DP4A(vi0, reinterpret_cast<const int*>(x->qs)[iqs + i], sumi);
int vi1 = (vl >> 4) & 0x0f0f0f0f;
vi1 |= (vh >> 12) & 0x00000010;
vi1 |= (vh >> 5) & 0x00001000;
vi1 |= (vh << 2) & 0x00100000;
vi1 |= (vh << 9) & 0x10000000;
- sumi = __dp4a(vi1, reinterpret_cast<const int*>(x->qs)[iqs + i + 4], sumi);
+ sumi = STRATA_DP4A(vi1, reinterpret_cast<const int*>(x->qs)[iqs + i + 4], sumi);
}
const float2 ds = __half22float2(x->ds);
const float d = w->d;
@@ -711,7 +712,7 @@ __device__ __forceinline__ float small_q8_dot(const Q80Block* __restrict__ w,
for (int i = 0; i < 2; ++i) {
const int v = load_int_b2(w->qs, iqs + i);
const int u = reinterpret_cast<const int*>(x->qs)[iqs + i];
- sumi = __dp4a(v, u, sumi);
+ sumi = STRATA_DP4A(v, u, sumi);
}
const float d0 = w->d;
const float d1 = __low2float(x->ds);
@@ -725,8 +726,8 @@ __device__ __forceinline__ float small_q8_dot(const IQ4NLBlock* __restrict__ w,
#pragma unroll
for (int i = 0; i < 2; ++i) {
const int2 v = iq4_table_lookup(load_int_b2(w->qs, iqs + i));
- sumi = __dp4a(v.x, q8[i], sumi);
- sumi = __dp4a(v.y, q8[i + 4], sumi);
+ sumi = STRATA_DP4A(v.x, q8[i], sumi);
+ sumi = STRATA_DP4A(v.y, q8[i + 4], sumi);
}
const float d = __half2float(w->d) * __low2float(x->ds);
return d * sumi;
@@ -890,8 +891,8 @@ struct Q20Traits {
int sumi = 0;
#pragma unroll
for (int j = 0; j < 4; ++j) {
- sumi = __dp4a(q8[j * 2], r.qx[j], sumi);
- sumi = __dp4a(q8[j * 2 + 1], r.qy[j], sumi);
+ sumi = STRATA_DP4A(q8[j * 2], r.qx[j], sumi);
+ sumi = STRATA_DP4A(q8[j * 2 + 1], r.qy[j], sumi);
}
const float d8 = __low2float(chunk->ds);
return r.d2 * d8 * sumi;
@@ -969,8 +970,8 @@ struct IQ4XSTraits {
for (int j = 0; j < 4; ++j) {
const int u0 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j];
const int u1 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j + 4];
- sumi = __dp4a(r.v[j].x, u0, sumi);
- sumi = __dp4a(r.v[j].y, u1, sumi);
+ sumi = STRATA_DP4A(r.v[j].x, u0, sumi);
+ sumi = STRATA_DP4A(r.v[j].y, u1, sumi);
}
sumi *= r.ls - 32;
const float d = r.dw * __low2float(x[iqs / 4].ds);
diff --git a/src/kernels/cuda/s2_expert_grouped.cu b/src/kernels/cuda/s2_expert_grouped.cu
index 3cbf61259..e58dc704a 100644
--- a/src/kernels/cuda/s2_expert_grouped.cu
+++ b/src/kernels/cuda/s2_expert_grouped.cu
@@ -14,6 +14,7 @@
// The two agree to float rounding and not to the bit, which is the same contract `s_gemv_parity` carries for
// the same reason. `bench/micro/moe_hit_parity.cu` is the check.
#include "strata/kernels/s2_expert_grouped.hpp"
+#include "strata/kernels/dp4a.hpp"
#include "strata/kernels/quantize_act.hpp"
#include "strata/kernels/verify_kernels.hpp"
@@ -89,8 +90,8 @@ __device__ __forceinline__ float row_dot_s2_q8(const uint8_t* __restrict__ codes
// it rather than reasoning about which cast happens to work.
int xw;
memcpy(&xw, xq + 4 * j, 4);
- s = __dp4a(cw, xw, s);
- hx = __dp4a(ones, xw, hx);
+ s = STRATA_DP4A(cw, xw, s);
+ hx = STRATA_DP4A(ones, xw, hx);
}
// One weight scale per 64 elements, so per TWO 32-element chunks.
const float dw = f16_at(scales + (size_t) (c >> 1) * 2);
@@ -211,7 +212,7 @@ __device__ __forceinline__ int dot4(const uint8_t* codes, const int8_t* q) {
(((c >> 4) & 3u) << 16) | (((c >> 6) & 3u) << 24));
int xw;
memcpy(&xw, q, sizeof xw);
- return __dp4a(cw, xw, 0);
+ return STRATA_DP4A(cw, xw, 0);
}
__device__ __forceinline__ float row_dot_cpu_order(const uint8_t* codes, const uint8_t* scales,
@@ -530,8 +531,8 @@ __device__ __forceinline__ float chunk_dot(uint2 cb, const int* xw, float dw, fl
const unsigned cbyte = cbytes[j];
const int cw = (int) ((cbyte & 3u) | (((cbyte >> 2) & 3u) << 8) | (((cbyte >> 4) & 3u) << 16) |
(((cbyte >> 6) & 3u) << 24));
- s = __dp4a(cw, xw[j], s);
- hx = __dp4a(ones, xw[j], hx);
+ s = STRATA_DP4A(cw, xw[j], s);
+ hx = STRATA_DP4A(ones, xw[j], hx);
}
return dw * dx * (float) (s - hx);
}
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index b8a3a619f..21d23440d 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -3,6 +3,7 @@
// The per-token arithmetic of every kernel here is transcribed from its single-token original (fused_gdn.cu,
// elementwise.cu) with the same operation order, so a verify window reproduces plain decode bit for bit.
#include "strata/kernels/verify_kernels.hpp"
+#include "strata/kernels/dp4a.hpp"
#include <cuda_runtime.h>
@@ -423,7 +424,7 @@ void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const
namespace {
__global__ void wait_flag_ge_kernel(const volatile uint32_t* flag, uint32_t value) {
- while (*flag < value) __nanosleep(100);
+ while (*flag < value) strata_spin_pause();
__threadfence_system();
}
} // namespace
@@ -472,7 +473,7 @@ __global__ void resident_plan_kernel(const int32_t* __restrict__ ids, int n, int
}
__global__ void wait_flag_ge_or_kernel(const volatile uint32_t* flag, uint32_t value, const volatile uint32_t* skip) {
if (*skip == value) return;
- while (*flag < value) __nanosleep(100);
+ while (*flag < value) strata_spin_pause();
__threadfence_system();
}
__global__ void copy_i32_unless_kernel(int32_t* __restrict__ dst, const volatile int32_t* src, int n,