okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0271-macds (DeepSeek on the Mac) into 0.2.7.1: setup.py keeps the Mac sentence and adds the DGX Spark one for Linux ARM649258e4086

main
8.9 KiB165 linesraw
1#ifndef __grid_constant__
2#define __grid_constant__
3#endif
4#ifndef __device_builtin__
5#define __device_builtin__
6#endif
7
8// cuda_device_builtins.h - the CUDA device built-ins, declared for the HOST compile only.
9//
10// The DeepSeek kernels are compiled twice in this port: as C++ here, to enumerate exactly which API the device code uses,
11// and as MSL (kernels/*.metal) where the real work happens.  These declarations let the first pass run; nothing here is a
12// working kernel.  Metal spells the same things thread_position_in_grid, threadgroup_barrier, simd_shuffle and so on.
13#pragma once
14
15#include <cstdint>
16
17struct dim3 {
18    unsigned x, y, z;
19    dim3(unsigned _x = 1, unsigned _y = 1, unsigned _z = 1) : x(_x), y(_y), z(_z) {}
20};
21struct strata_dim3 { unsigned x, y, z; };
22static strata_dim3 threadIdx{0, 0, 0}, blockIdx{0, 0, 0}, blockDim{1, 1, 1}, gridDim{1, 1, 1};
23
24static inline void __syncthreads() {}
25static inline void __syncwarp(unsigned = 0xffffffffu) {}
26static inline unsigned __ballot_sync(unsigned, int) { return 0; }
27static inline int __popc(unsigned v) { return __builtin_popcount(v); }
28static inline int __ffs(unsigned v) { return __builtin_ffs((int) v); }
29
30// the bit and integer intrinsics the block decoders use.  Each is the exact CUDA semantics, computed on the host here; the
31// MSL kernels implement the same operations with Metal's own select/rotate/permute built-ins.
32static inline unsigned __byte_perm(unsigned x, unsigned y, unsigned s) {
33    unsigned out = 0;
34    const unsigned long long src = ((unsigned long long) y << 32) | x;
35    for (int i = 0; i < 4; ++i) {
36        const unsigned sel = (s >> (4 * i)) & 0x7u;
37        out |= (unsigned) ((src >> (8 * sel)) & 0xffu) << (8 * i);
38    }
39    return out;
40}
41static inline int __mul24(int a, int b) { return a * b; }
42static inline unsigned __umul24(unsigned a, unsigned b) { return a * b; }
43static inline unsigned __brev(unsigned v) { return __builtin_bitreverse32(v); }
44static inline int __clz(int v) { return v ? __builtin_clz((unsigned) v) : 32; }
45static inline int __clzll(long long v) { return v ? __builtin_clzll((unsigned long long) v) : 64; }
46static inline float __fdividef(float a, float b) { return a / b; }
47static inline float __saturatef(float a) { return a < 0.0f ? 0.0f : (a > 1.0f ? 1.0f : a); }
48static inline float __int_as_float(int v) { float f; __builtin_memcpy(&f, &v, 4); return f; }
49static inline int __float_as_int(float f) { int v; __builtin_memcpy(&v, &f, 4); return v; }
50static inline unsigned __float_as_uint(float f) { unsigned v; __builtin_memcpy(&v, &f, 4); return v; }
51static inline float __uint_as_float(unsigned v) { float f; __builtin_memcpy(&f, &v, 4); return f; }
52static inline void __threadfence() {}
53static inline void __threadfence_block() {}
54static inline float atomicAdd(float* p, float v) { const float o = *p; *p = o + v; return o; }
55static inline int atomicAdd(int* p, int v) { const int o = *p; *p = o + v; return o; }
56static inline unsigned atomicAdd(unsigned* p, unsigned v) { const unsigned o = *p; *p = o + v; return o; }
57static inline int atomicMax(int* p, int v) { const int o = *p; if (v > o) *p = v; return o; }
58static inline int atomicCAS(int* p, int c, int v) { const int o = *p; if (o == c) *p = v; return o; }
59
60// the SIMD video instructions (packed 8-bit lanes) the i-quant block decoders use: on CUDA these are one instruction, on
61// Apple Silicon NEON has them directly (vsubq_u8, vpaddlq / vdotq_s32) and the MSL kernels use those.  Here they are the
62// exact per-lane semantics, so the host compile reproduces the same numbers.
63static inline unsigned __vsub4(unsigned a, unsigned b) {
64    unsigned out = 0;
65    for (int i = 0; i < 4; ++i) {
66        const int d = (int) ((a >> (8 * i)) & 0xffu) - (int) ((b >> (8 * i)) & 0xffu);
67        out |= (unsigned) ((d < 0 ? 0 : d) & 0xff) << (8 * i);   // unsigned saturated
68    }
69    return out;
70}
71static inline unsigned __vsubss4(unsigned a, unsigned b) {
72    unsigned out = 0;
73    for (int i = 0; i < 4; ++i) {
74        int d = (int) ((a >> (8 * i)) & 0xffu) - (int) ((b >> (8 * i)) & 0xffu);
75        if (d < -128) d = -128;
76        if (d > 127) d = 127;
77        out |= (unsigned) (d & 0xff) << (8 * i);
78    }
79    return out;
80}
81static inline unsigned __vadd4(unsigned a, unsigned b) {
82    unsigned out = 0;
83    for (int i = 0; i < 4; ++i) {
84        const int d = (int) ((a >> (8 * i)) & 0xffu) + (int) ((b >> (8 * i)) & 0xffu);
85        out |= (unsigned) ((d > 255 ? 255 : d) & 0xff) << (8 * i);
86    }
87    return out;
88}
89static inline unsigned __vaddss4(unsigned a, unsigned b) {
90    unsigned out = 0;
91    for (int i = 0; i < 4; ++i) {
92        int d = (int) ((a >> (8 * i)) & 0xffu) + (int) ((b >> (8 * i)) & 0xffu);
93        if (d > 127) d = 127;
94        out |= (unsigned) (d & 0xff) << (8 * i);
95    }
96    return out;
97}
98static inline unsigned __vcmpeq4(unsigned a, unsigned b) {
99    unsigned out = 0;
100    for (int i = 0; i < 4; ++i) {
101        const unsigned ea = (a >> (8 * i)) & 0xffu, eb = (b >> (8 * i)) & 0xffu;
102        out |= (ea == eb ? 0xffu : 0u) << (8 * i);
103    }
104    return out;
105}
106static inline unsigned __vcmpgtu4(unsigned a, unsigned b) {
107    unsigned out = 0;
108    for (int i = 0; i < 4; ++i) {
109        const unsigned ea = (a >> (8 * i)) & 0xffu, eb = (b >> (8 * i)) & 0xffu;
110        out |= (ea > eb ? 0xffu : 0u) << (8 * i);
111    }
112    return out;
113}
114static inline int __dp4a(unsigned a, unsigned b, int c) {
115    for (int i = 0; i < 4; ++i) c += (int) (signed char) ((a >> (8 * i)) & 0xffu) * (int) (signed char) ((b >> (8 * i)) & 0xffu);
116    return c;
117}
118
119// the remaining scalar math helpers CUDA provides and the decoders use (round-to-nearest, plus min/max for integers - CUDA
120// brings those in from its own headers, the C++ standard library does not for every type)
121static inline float __fsub_rn(float a, float b) { return a - b; }
122static inline float __fdiv_rn(float a, float b) { return a / b; }
123static inline float __frcp_rn_(float a) { return 1.0f / a; }
124template <typename T> static inline T strata_min(T a, T b) { return a < b ? a : b; }
125template <typename T> static inline T strata_max(T a, T b) { return a > b ? a : b; }
126
127// the last names the kernel sources use (a full-list compile found twelve families, 2026-10-23): the grid type above, a
128// system fence, the funnel shift, the hardware transcendentals and the read-only/volatile loads.
129static inline void __threadfence_system() {}
130static inline unsigned __funnelshift_r(unsigned lo, unsigned hi, unsigned shift) {
131    const unsigned long long v = ((unsigned long long) hi << 32) | lo;
132    return (unsigned) ((v >> (shift & 31)) & 0xffffffffu);
133}
134static inline unsigned __funnelshift_l(unsigned lo, unsigned hi, unsigned shift) {
135    const unsigned long long v = ((unsigned long long) hi << 32) | lo;
136    return (unsigned) (((v << (shift & 31)) >> 32) & 0xffffffffu);
137}
138static inline float rsqrtf(float a) { return 1.0f / __builtin_sqrtf(a); }
139static inline float __expf(float a) { return __builtin_expf(a); }
140static inline float __logf(float a) { return __builtin_logf(a); }
141static inline float __sinf(float a) { return __builtin_sinf(a); }
142static inline float __cosf(float a) { return __builtin_cosf(a); }
143static inline void sincosf(float a, float* s, float* c) { *s = __builtin_sinf(a); *c = __builtin_cosf(a); }
144static inline int __float2int_rn(float a) { return (int) __builtin_roundf(a); }
145static inline int __float2int_rz(float a) { return (int) a; }
146static inline int __float2int_rd(float a) { return (int) __builtin_floorf(a); }
147static inline int __float2int_ru(float a) { return (int) __builtin_ceilf(a); }
148template <typename T> static inline T __ldg(const T* p) { return *p; }
149template <typename T> static inline T __ldcv(const T* p) { return *p; }
150
151// shuffles: one template each, so int/unsigned/float/short all bind (CUDA has overloads; a template is the same thing here)
152template <typename T> static inline T __shfl_xor_sync(unsigned, T v, int) { return v; }
153template <typename T> static inline T __shfl_down_sync(unsigned, T v, int) { return v; }
154template <typename T> static inline T __shfl_up_sync(unsigned, T v, int) { return v; }
155template <typename T> static inline T __shfl_sync(unsigned, T v, int) { return v; }
156
157// the integer multiply-high and the bitwise atomics the sampler uses
158static inline unsigned __umulhi(unsigned a, unsigned b) { return (unsigned) (((unsigned long long) a * b) >> 32); }
159static inline int __mulhi(int a, int b) { return (int) (((long long) a * b) >> 32); }
160static inline unsigned atomicOr(unsigned* p, unsigned v) { const unsigned o = *p; *p = o | v; return o; }
161static inline unsigned atomicAnd(unsigned* p, unsigned v) { const unsigned o = *p; *p = o & v; return o; }
162static inline unsigned atomicXor(unsigned* p, unsigned v) { const unsigned o = *p; *p = o ^ v; return o; }
163static inline unsigned atomicExch(unsigned* p, unsigned v) { const unsigned o = *p; *p = v; return o; }
164static inline unsigned atomicInc(unsigned* p, unsigned v) { const unsigned o = *p; *p = (o >= v ? 0 : o + 1); return o; }
165static inline unsigned atomicDec(unsigned* p, unsigned v) { const unsigned o = *p; *p = (o == 0 || o > v ? v : o - 1); return o; }