metal: the dsv41 core's CUDA surface, part one (bf16/fp8 conversions, the cuBLAS names, the compile audit)
Compiling src/dsv41/*.cu against metal/include/metal_compat as C++ is how the DeepSeek-on-Metal port is scoped, and it is much smaller than assumed: ds4_model.cu is 8,232 lines of *host* code with zero device markers, and the six non-kernel sources already compile. What was missing at the compat layer: * cuda_bf16.h - __nv_bfloat16/__nv_bfloat162/__nv_fp8_storage_t and the four conversions the engine uses (__float2bfloat16_rn, __bfloat16_as_ushort, __nv_cvt_float_to_fp8, __nv_cvt_fp8_to_halfraw), E4M3 with round-to-nearest. * cublas_v2.h - the nine GEMM calls (7 cublasGemmEx, 2 cublasSgemm) and their enums. The dense GEMM itself is NOT wired: it aborts with a message naming kernels/dense_mm.metal (which exists and passes its parity test) rather than returning a silently wrong result. After these, ds4_model.cu's remaining 20 errors are all in ds4_os.hpp - the Linux-only OS surface (O_DIRECT, posix_fadvise, mincore, MADV_HUGEPAGE, SYS_mbind, cpu_set_t/sched_setaffinity) - which needs a __APPLE__ branch.
This commit is contained in:
1 parent
f0f056b9bb
commit
bcb2133a28
2 files changed
+105
No files matched your search
@@ -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");
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user