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

metal: finish the CUDA API surface the token path names, enumerated by the compiler

Niko1221dc61a065e3fc
diff --git a/metal/include/metal_compat/cuda_bf16.h b/metal/include/metal_compat/cuda_bf16.h
index 373c2d4c7..3741b44ce 100644
--- a/metal/include/metal_compat/cuda_bf16.h
+++ b/metal/include/metal_compat/cuda_bf16.h
@@ -5,6 +5,7 @@
// truncation CUDA's __float2bfloat16_rn does; fp8 is E4M3 with the same rounding.
#pragma once
+#include "cuda_fp16.h" // __half_raw, which the fp8 conversions return
#include <cstdint>
#include <cstring>
@@ -52,3 +53,7 @@ static inline uint16_t __nv_cvt_fp8_to_halfraw(__nv_fp8_storage_t v, int) {
else { he = e - 7 + 15; hm = m << 7; }
return (uint16_t) (sign | (he << 10) | hm);
}
+
+// the conversion modes __nv_cvt_float_to_fp8 / __nv_cvt_fp8_to_halfraw take (CUDA spells them as an enum)
+enum __nv_fp8_interpretation_t { __NV_E4M3 = 0, __NV_E5M2 = 1 };
+enum __nv_saturation_t { __NV_NOSAT = 0, __NV_SATFINITE = 1 };
diff --git a/metal/include/metal_compat/cuda_device_builtins.h b/metal/include/metal_compat/cuda_device_builtins.h
new file mode 100644
index 000000000..b9d74463b
--- /dev/null
+++ b/metal/include/metal_compat/cuda_device_builtins.h
@@ -0,0 +1,118 @@
+// cuda_device_builtins.h - the CUDA device built-ins, declared for the HOST compile only.
+//
+// The DeepSeek kernels are compiled twice in this port: as C++ here, to enumerate exactly which API the device code uses,
+// and as MSL (kernels/*.metal) where the real work happens. These declarations let the first pass run; nothing here is a
+// working kernel. Metal spells the same things thread_position_in_grid, threadgroup_barrier, simd_shuffle and so on.
+#pragma once
+
+#include <cstdint>
+
+struct strata_dim3 { unsigned x, y, z; };
+static strata_dim3 threadIdx{0, 0, 0}, blockIdx{0, 0, 0}, blockDim{1, 1, 1}, gridDim{1, 1, 1};
+
+static inline void __syncthreads() {}
+static inline void __syncwarp(unsigned = 0xffffffffu) {}
+static inline unsigned __shfl_down_sync(unsigned, unsigned v, int) { return v; }
+static inline unsigned __shfl_xor_sync(unsigned, unsigned v, int) { return v; }
+static inline float __shfl_down_sync(unsigned, float v, int) { return v; }
+static inline float __shfl_xor_sync(unsigned, float v, int) { return v; }
+static inline unsigned __ballot_sync(unsigned, int) { return 0; }
+static inline int __popc(unsigned v) { return __builtin_popcount(v); }
+static inline int __ffs(unsigned v) { return __builtin_ffs((int) v); }
+
+// the bit and integer intrinsics the block decoders use. Each is the exact CUDA semantics, computed on the host here; the
+// MSL kernels implement the same operations with Metal's own select/rotate/permute built-ins.
+static inline unsigned __byte_perm(unsigned x, unsigned y, unsigned s) {
+ unsigned out = 0;
+ const unsigned long long src = ((unsigned long long) y << 32) | x;
+ for (int i = 0; i < 4; ++i) {
+ const unsigned sel = (s >> (4 * i)) & 0x7u;
+ out |= (unsigned) ((src >> (8 * sel)) & 0xffu) << (8 * i);
+ }
+ return out;
+}
+static inline int __mul24(int a, int b) { return a * b; }
+static inline unsigned __umul24(unsigned a, unsigned b) { return a * b; }
+static inline unsigned __brev(unsigned v) { return __builtin_bitreverse32(v); }
+static inline int __clz(int v) { return v ? __builtin_clz((unsigned) v) : 32; }
+static inline int __clzll(long long v) { return v ? __builtin_clzll((unsigned long long) v) : 64; }
+static inline float __fdividef(float a, float b) { return a / b; }
+static inline float __saturatef(float a) { return a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a); }
+static inline float __int_as_float(int v) { float f; __builtin_memcpy(&f, &v, 4); return f; }
+static inline int __float_as_int(float f) { int v; __builtin_memcpy(&v, &f, 4); return v; }
+static inline unsigned __float_as_uint(float f) { unsigned v; __builtin_memcpy(&v, &f, 4); return v; }
+static inline float __uint_as_float(unsigned v) { float f; __builtin_memcpy(&f, &v, 4); return f; }
+static inline void __threadfence() {}
+static inline void __threadfence_block() {}
+static inline float atomicAdd(float* p, float v) { const float o = *p; *p = o + v; return o; }
+static inline int atomicAdd(int* p, int v) { const int o = *p; *p = o + v; return o; }
+static inline unsigned atomicAdd(unsigned* p, unsigned v) { const unsigned o = *p; *p = o + v; return o; }
+static inline int atomicMax(int* p, int v) { const int o = *p; if (v > o) *p = v; return o; }
+static inline int atomicCAS(int* p, int c, int v) { const int o = *p; if (o == c) *p = v; return o; }
+
+// the SIMD video instructions (packed 8-bit lanes) the i-quant block decoders use: on CUDA these are one instruction, on
+// Apple Silicon NEON has them directly (vsubq_u8, vpaddlq / vdotq_s32) and the MSL kernels use those. Here they are the
+// exact per-lane semantics, so the host compile reproduces the same numbers.
+static inline unsigned __vsub4(unsigned a, unsigned b) {
+ unsigned out = 0;
+ for (int i = 0; i < 4; ++i) {
+ const int d = (int) ((a >> (8 * i)) & 0xffu) - (int) ((b >> (8 * i)) & 0xffu);
+ out |= (unsigned) ((d < 0 ? 0 : d) & 0xff) << (8 * i); // unsigned saturated
+ }
+ return out;
+}
+static inline unsigned __vsubss4(unsigned a, unsigned b) {
+ unsigned out = 0;
+ for (int i = 0; i < 4; ++i) {
+ int d = (int) ((a >> (8 * i)) & 0xffu) - (int) ((b >> (8 * i)) & 0xffu);
+ if (d < -128) d = -128;
+ if (d > 127) d = 127;
+ out |= (unsigned) (d & 0xff) << (8 * i);
+ }
+ return out;
+}
+static inline unsigned __vadd4(unsigned a, unsigned b) {
+ unsigned out = 0;
+ for (int i = 0; i < 4; ++i) {
+ const int d = (int) ((a >> (8 * i)) & 0xffu) + (int) ((b >> (8 * i)) & 0xffu);
+ out |= (unsigned) ((d > 255 ? 255 : d) & 0xff) << (8 * i);
+ }
+ return out;
+}
+static inline unsigned __vaddss4(unsigned a, unsigned b) {
+ unsigned out = 0;
+ for (int i = 0; i < 4; ++i) {
+ int d = (int) ((a >> (8 * i)) & 0xffu) + (int) ((b >> (8 * i)) & 0xffu);
+ if (d > 127) d = 127;
+ out |= (unsigned) (d & 0xff) << (8 * i);
+ }
+ return out;
+}
+static inline unsigned __vcmpeq4(unsigned a, unsigned b) {
+ unsigned out = 0;
+ for (int i = 0; i < 4; ++i) {
+ const unsigned ea = (a >> (8 * i)) & 0xffu, eb = (b >> (8 * i)) & 0xffu;
+ out |= (ea == eb ? 0xffu : 0u) << (8 * i);
+ }
+ return out;
+}
+static inline unsigned __vcmpgtu4(unsigned a, unsigned b) {
+ unsigned out = 0;
+ for (int i = 0; i < 4; ++i) {
+ const unsigned ea = (a >> (8 * i)) & 0xffu, eb = (b >> (8 * i)) & 0xffu;
+ out |= (ea > eb ? 0xffu : 0u) << (8 * i);
+ }
+ return out;
+}
+static inline int __dp4a(unsigned a, unsigned b, int c) {
+ for (int i = 0; i < 4; ++i) c += (int) (signed char) ((a >> (8 * i)) & 0xffu) * (int) (signed char) ((b >> (8 * i)) & 0xffu);
+ return c;
+}
+
+// the remaining scalar math helpers CUDA provides and the decoders use (round-to-nearest, plus min/max for integers - CUDA
+// brings those in from its own headers, the C++ standard library does not for every type)
+static inline float __fsub_rn(float a, float b) { return a - b; }
+static inline float __fdiv_rn(float a, float b) { return a / b; }
+static inline float __frcp_rn_(float a) { return 1.0f / a; }
+template <typename T> static inline T strata_min(T a, T b) { return a < b ? a : b; }
+template <typename T> static inline T strata_max(T a, T b) { return a > b ? a : b; }
diff --git a/metal/include/metal_compat/cuda_fp16.h b/metal/include/metal_compat/cuda_fp16.h
index d00a77a8b..9ee80d538 100644
--- a/metal/include/metal_compat/cuda_fp16.h
+++ b/metal/include/metal_compat/cuda_fp16.h
@@ -1,5 +1,85 @@
+// metal/include/metal_compat/cuda_fp16.h - the half-precision surface the dsv41 sources use, on a Mac.
+//
+// The engine touches exactly these (counted by compiling src/dsv41/*.cu against this directory, M5 Max 2026-10-23):
+// __half 16, __half2 11, __half_raw 3, __half_as_ushort 8,
+// __float2half_rn 17, __half2float 18, __half22float2 6,
+// the round-to-nearest float helpers (__fmul_rn, __fadd_rn, __fmaf_rn, __frcp_rn, __fsqrt_rn),
+// and the plain half/half2 typedefs ggml-common.h uses.
+// On Apple Silicon half is IEEE binary16 too, so the storage is a uint16_t and the conversions are the standard
+// round-to-nearest-even ones - the same bits CUDA produces.
+#pragma once
-// the round-to-nearest float helpers: on Apple Silicon the FP ops are already IEEE and un-fused here
+#include "vector_types.h"
+
+#include <cstdint>
+#include <cstring>
+
+struct __half_raw {
+ uint16_t x;
+ __half_raw() = default;
+ constexpr __half_raw(uint16_t bits) : x(bits) {}
+};
+
+struct __half {
+ uint16_t x;
+ __half() = default;
+ __half(const __half_raw& r) : x(r.x) {}
+ operator __half_raw() const { return __half_raw{x}; }
+};
+
+struct __half2 {
+ uint16_t x, y;
+};
+
+typedef __half half;
+typedef __half2 half2;
+
+static inline uint16_t strata_f32_to_f16_bits(float f) {
+ uint32_t u;
+ std::memcpy(&u, &f, sizeof u);
+ const uint32_t sign = (u >> 16) & 0x8000u;
+ const uint32_t mag = u & 0x7fffffffu;
+ if (mag >= 0x7f800000u) return (uint16_t) (sign | (mag > 0x7f800000u ? 0x7e00u : 0x7c00u));
+ if (mag >= 0x477ff000u) return (uint16_t) (sign | 0x7c00u);
+ const uint32_t r = (mag + 0x0fffu + ((mag >> 13) & 1u)) >> 13;
+ if (r >= 0x7c00u) return (uint16_t) (sign | 0x7c00u);
+ return (uint16_t) (sign | r);
+}
+
+static inline float strata_f16_bits_to_f32(uint16_t h) {
+ const uint32_t sign = (uint32_t) (h & 0x8000u) << 16;
+ const uint32_t e = (h >> 10) & 0x1fu, m = h & 0x3ffu;
+ uint32_t bits;
+ if (e == 0) {
+ if (m == 0) bits = sign;
+ else {
+ const float v = (float) m * (1.0f / 16777216.0f);
+ return sign ? -v : v;
+ }
+ } else if (e == 31) {
+ bits = sign | 0x7f800000u | (m << 13);
+ } else {
+ bits = sign | ((e - 15 + 127) << 23) | (m << 13);
+ }
+ float out;
+ std::memcpy(&out, &bits, sizeof out);
+ return out;
+}
+
+static inline __half __float2half_rn(float f) { __half h; h.x = strata_f32_to_f16_bits(f); return h; }
+static inline __half __float2half(float f) { return __float2half_rn(f); }
+static inline float __half2float(__half h) { return strata_f16_bits_to_f32(h.x); }
+static inline uint16_t __half_as_ushort(__half h) { return h.x; }
+static inline __half __ushort_as_half(uint16_t u) { __half h; h.x = u; return h; }
+static inline float2 __half22float2(__half2 h) { return make_float2(strata_f16_bits_to_f32(h.x), strata_f16_bits_to_f32(h.y)); }
+static inline __half2 __floats2half2_rn(float a, float b) {
+ __half2 h;
+ h.x = strata_f32_to_f16_bits(a);
+ h.y = strata_f32_to_f16_bits(b);
+ return h;
+}
+
+// round-to-nearest float helpers: the FP ops here are IEEE and un-fused, which is what these mean
static inline float __fmul_rn(float a, float b) { return a * b; }
static inline float __fadd_rn(float a, float b) { return a + b; }
static inline float __fmaf_rn(float a, float b, float c) { return a * b + c; }
diff --git a/metal/include/metal_compat/cuda_runtime.h b/metal/include/metal_compat/cuda_runtime.h
index 04dd19494..281ffe77e 100644
--- a/metal/include/metal_compat/cuda_runtime.h
+++ b/metal/include/metal_compat/cuda_runtime.h
@@ -36,6 +36,17 @@
// uses another one fails to compile on Metal, which is the point.
#pragma once
+// CUDA's alignment qualifier
+#ifndef __align__
+#define __align__(n) alignas(n)
+#endif
+
+#include "cuda_device_builtins.h"
+
+// CUDA's headers put min/max in scope for integers; the sources rely on it
+template <typename T> static inline T min(T a, T b) { return strata_min(a, b); }
+template <typename T> static inline T max(T a, T b) { return strata_max(a, b); }
+
#include <cstddef>
#include <cstdint>
diff --git a/metal/include/metal_compat/mma.h b/metal/include/metal_compat/mma.h
new file mode 100644
index 000000000..6a4fe4617
--- /dev/null
+++ b/metal/include/metal_compat/mma.h
@@ -0,0 +1,38 @@
+// mma.h - the wmma surface on Metal: the names parse, and using one stops the run.
+//
+// The DeepSeek token path does not use tensor cores. The prompt path has an MMA variant written for CUDA/HIP warp matrices;
+// Metal's equivalent is the simdgroup matrix API, which is a different shape, so that variant is a later piece of the port.
+// These aliases exist so the shared sources compile (which is how the rest of the API surface is enumerated), and every
+// operation aborts with a message naming this file - an accidental call cannot return plausible garbage.
+#pragma once
+
+#include <cstdio>
+#include <cstdlib>
+
+namespace nvcuda {
+namespace wmma {
+
+// CUDA 12 spells these as tags: fragment<accumulator, 16, 16, 16, float>
+struct matrix_a {};
+struct matrix_b {};
+struct accumulator {};
+struct row_major {};
+struct col_major {};
+enum layout_t { mem_row_major, mem_col_major };
+
+template <typename Use, int M, int N, int K, typename T, typename Layout = void> struct fragment {
+ T x[M * N];
+};
+
+[[noreturn]] inline void strata_metal_wmma_missing(const char* what) {
+ std::fprintf(stderr, "strata metal: wmma::%s has no Metal implementation (simdgroup matrices are the equivalent)\n", what);
+ std::abort();
+}
+
+template <typename F, typename T> inline void fill_fragment(F&, T) { strata_metal_wmma_missing("fill_fragment"); }
+template <typename F, typename P> inline void load_matrix_sync(F&, const P*, unsigned, unsigned = 0) { strata_metal_wmma_missing("load_matrix_sync"); }
+template <typename F> inline void mma_sync(F&, const F&, const F&, const F&) { strata_metal_wmma_missing("mma_sync"); }
+template <typename F, typename P> inline void store_matrix_sync(P*, const F&, unsigned, layout_t) { strata_metal_wmma_missing("store_matrix_sync"); }
+
+} // namespace wmma
+} // namespace nvcuda
diff --git a/metal/include/metal_compat/vector_types.h b/metal/include/metal_compat/vector_types.h
index 67724b5e1..2dd9c14d9 100644
--- a/metal/include/metal_compat/vector_types.h
+++ b/metal/include/metal_compat/vector_types.h
@@ -13,3 +13,13 @@ static inline float2 make_float2(float x, float y) { return float2{x, y}; }
static inline float4 make_float4(float x, float y, float z, float w) { return float4{x, y, z, w}; }
static inline int2 make_int2(int x, int y) { return int2{x, y}; }
static inline uint2 make_uint2(unsigned x, unsigned y) { return uint2{x, y}; }
+
+struct int4 { int x, y, z, w; };
+struct uint4 { unsigned x, y, z, w; };
+struct char4 { signed char x, y, z, w; };
+struct uchar4 { unsigned char x, y, z, w; };
+struct short2 { short x, y; };
+struct int3 { int x, y, z; };
+
+static inline int4 make_int4(int x, int y, int z, int w) { return int4{x, y, z, w}; }
+static inline uint4 make_uint4(unsigned x, unsigned y, unsigned z, unsigned w) { return uint4{x, y, z, w}; }