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: the dsv41 core's CUDA surface, part one (bf16/fp8 conversions, the cuBLAS names, the compile audit)

Niko1221bcb2133a2810
diff --git a/metal/include/metal_compat/cublas_v2.h b/metal/include/metal_compat/cublas_v2.h
new file mode 100644
index 000000000..dfc87e74a
--- /dev/null
+++ b/metal/include/metal_compat/cublas_v2.h
@@ -0,0 +1,51 @@
+// metal/include/metal_compat/cublas_v2.h - the cuBLAS surface the dsv41 sources use, over the Metal device runtime.
+//
+// The engine makes 9 GEMM calls (7 cublasGemmEx, 2 cublasSgemm) for the dense projections, the LM head and the vision
+// tower's matmuls. On Metal the dense path is kernels/dense_mm.metal; this header gives the calls the CUDA names so the
+// shared sources compile unchanged, and routes them to strata::gpu::dense_gemm(), which the metal host runtime provides.
+//
+// Compile-audit finding (M5 Max, 2026-10-23): the dsv41 core needs exactly cublasCreate/Destroy/SetStream/SetWorkspace,
+// cublasSgemm and cublasGemmEx, plus cublasStatus_t/cublasHandle_t/cublasOperation_t/cublasComputeType_t/cudaDataType_t.
+#pragma once
+
+#include <cstdio>
+#include <cstdlib>
+
+#include "cuda_runtime.h"
+
+typedef void* cublasHandle_t;
+
+enum cublasStatus_t { CUBLAS_STATUS_SUCCESS = 0, CUBLAS_STATUS_NOT_INITIALIZED = 1, CUBLAS_STATUS_ALLOC_FAILED = 3,
+ CUBLAS_STATUS_INVALID_VALUE = 7, CUBLAS_STATUS_ARCH_MISMATCH = 8, CUBLAS_STATUS_NOT_SUPPORTED = 14 };
+enum cublasOperation_t { CUBLAS_OP_N = 0, CUBLAS_OP_T = 1, CUBLAS_OP_C = 2 };
+enum cublasComputeType_t { CUBLAS_COMPUTE_16F = 64, CUBLAS_COMPUTE_32F = 68, CUBLAS_COMPUTE_32F_FAST_16F = 74 };
+enum cudaDataType_t { CUDA_R_32F = 0, CUDA_R_16F = 2, CUDA_R_16BF = 14, CUDA_R_8F_E4M3 = 28 };
+
+static inline cublasStatus_t cublasCreate(cublasHandle_t* h) {
+ *h = (void*) 1; // the Metal runtime is a singleton: the handle carries no state
+ return CUBLAS_STATUS_SUCCESS;
+}
+static inline cublasStatus_t cublasDestroy(cublasHandle_t) { return CUBLAS_STATUS_SUCCESS; }
+static inline cublasStatus_t cublasSetStream(cublasHandle_t, cudaStream_t) { return CUBLAS_STATUS_SUCCESS; }
+static inline cublasStatus_t cublasSetWorkspace(cublasHandle_t, void*, size_t) { return CUBLAS_STATUS_SUCCESS; }
+
+// C = alpha * op(A) * op(B) + beta * C, all row-major floats.
+// NOT WIRED YET: the metal host runtime has no dense GEMM entry point. This aborts with a message instead of returning a
+// wrong result, so the shared sources compile and a run fails where the work is, not somewhere confusing. The real
+// implementation drives kernels/dense_mm.metal (the kernel exists and passes its parity test).
+[[noreturn]] static inline void strata_metal_dense_gemm_missing(const char* what) {
+ std::fprintf(stderr, "strata metal: %s is not wired to kernels/dense_mm.metal yet\n", what);
+ std::abort();
+}
+
+static inline cublasStatus_t cublasSgemm(cublasHandle_t, cublasOperation_t, cublasOperation_t, int, int, int,
+ const float*, const float*, int, const float*, int, const float*, float*, int) {
+ strata_metal_dense_gemm_missing("cublasSgemm");
+}
+
+// the mixed-precision form: the engine passes a compute type and a scale; on Metal the kernels read A/B by their own type
+static inline cublasStatus_t cublasGemmEx(cublasHandle_t, cublasOperation_t, cublasOperation_t, int, int, int,
+ const void*, const void*, cudaDataType_t, int, const void*, cudaDataType_t, int,
+ const void*, void*, cudaDataType_t, cublasComputeType_t, int) {
+ strata_metal_dense_gemm_missing("cublasGemmEx");
+}
diff --git a/metal/include/metal_compat/cuda_bf16.h b/metal/include/metal_compat/cuda_bf16.h
new file mode 100644
index 000000000..373c2d4c7
--- /dev/null
+++ b/metal/include/metal_compat/cuda_bf16.h
@@ -0,0 +1,54 @@
+// metal/include/metal_compat/cuda_bf16.h - the handful of CUDA bf16/fp8 names the dsv41 sources use, on Metal.
+//
+// Only what the engine touches (found by compiling src/dsv41/*.cu against this directory, 2026-10-23): the storage typedef
+// and four conversions. On a Mac the bf16 storage is a uint16_t and the conversion is the standard round-to-nearest-even
+// truncation CUDA's __float2bfloat16_rn does; fp8 is E4M3 with the same rounding.
+#pragma once
+
+#include <cstdint>
+#include <cstring>
+
+typedef uint16_t __nv_bfloat16;
+typedef uint16_t __nv_bfloat162;
+typedef uint8_t __nv_fp8_storage_t;
+
+static inline uint16_t strata_f32_to_bf16_rn(float f) {
+ uint32_t u;
+ std::memcpy(&u, &f, sizeof u);
+ if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t) ((u >> 16) | 0x0040u); // NaN keeps a payload
+ uint32_t r = (u + 0x7fffu + ((u >> 16) & 1u)) >> 16;
+ return (uint16_t) r;
+}
+
+static inline __nv_bfloat16 __float2bfloat16_rn(float f) { return strata_f32_to_bf16_rn(f); }
+static inline uint16_t __bfloat16_as_ushort(__nv_bfloat16 v) { return v; }
+
+// E4M3 (no infinities, max 448), round to nearest even - the inverse of the half conversion the engine also uses
+static inline __nv_fp8_storage_t __nv_cvt_float_to_fp8(float f, int, int) {
+ if (f != f) return 0x7f;
+ float a = f < 0 ? -f : f;
+ int sign = f < 0 ? 0x80 : 0;
+ if (a == 0.0f) return (__nv_fp8_storage_t) sign;
+ if (a >= 448.0f) return (__nv_fp8_storage_t) (sign | 0x7e);
+ int e = 0;
+ while (a >= 2.0f) { a *= 0.5f; ++e; }
+ while (a < 1.0f) { a *= 2.0f; --e; }
+ // a in [1,2): 3 mantissa bits, bias 7
+ float scaled = (a - 1.0f) * 8.0f;
+ int m = (int) (scaled + 0.5f);
+ int biased = e + 7;
+ if (m == 8) { m = 0; ++biased; }
+ if (biased <= 0) return (__nv_fp8_storage_t) sign; // subnormal -> zero here
+ if (biased >= 15) return (__nv_fp8_storage_t) (sign | 0x7e); // clamp to 448
+ return (__nv_fp8_storage_t) (sign | (biased << 3) | m);
+}
+static inline uint16_t __nv_cvt_fp8_to_halfraw(__nv_fp8_storage_t v, int) {
+ // half (IEEE binary16) from E4M3: the engine only reads these back for the vision tower's tables
+ const int sign = (v & 0x80) ? 0x8000 : 0;
+ const int e = (v >> 3) & 0xf, m = v & 0x7;
+ if (e == 0 && m == 0) return (uint16_t) sign;
+ int he, hm;
+ if (e == 0) { he = 0; hm = m << 7; }
+ else { he = e - 7 + 15; hm = m << 7; }
+ return (uint16_t) (sign | (he << 10) | hm);
+}