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
4.7 KiB99 linesraw
1// metal/include/metal_compat/cuda_fp16.h - the half-precision surface the dsv41 sources use, on a Mac.
2//
3// The engine touches exactly these (counted by compiling src/dsv41/*.cu against this directory, M5 Max 2026-10-23):
4//   __half 16, __half2 11, __half_raw 3, __half_as_ushort 8,
5//   __float2half_rn 17, __half2float 18, __half22float2 6,
6//   the round-to-nearest float helpers (__fmul_rn, __fadd_rn, __fmaf_rn, __frcp_rn, __fsqrt_rn),
7//   and the plain half/half2 typedefs ggml-common.h uses.
8// On Apple Silicon half is IEEE binary16 too, so the storage is a uint16_t and the conversions are the standard
9// round-to-nearest-even ones - the same bits CUDA produces.
10#pragma once
11
12#include "vector_types.h"
13
14#include <cstdint>
15#include <cstring>
16
17struct __half_raw {
18    uint16_t x;
19    __half_raw() = default;
20    constexpr __half_raw(uint16_t bits) : x(bits) {}
21};
22
23struct __half {
24    uint16_t x;
25    __half() = default;
26    __half(const __half_raw& r) : x(r.x) {}
27    operator __half_raw() const { return __half_raw{x}; }
28};
29
30struct __half2 {
31    uint16_t x, y;
32};
33
34typedef __half half;
35typedef __half2 half2;
36
37static inline uint16_t strata_f32_to_f16_bits(float f) {
38    uint32_t u;
39    std::memcpy(&u, &f, sizeof u);
40    const uint32_t sign = (u >> 16) & 0x8000u;
41    const uint32_t mag = u & 0x7fffffffu;
42    if (mag >= 0x7f800000u) return (uint16_t) (sign | (mag > 0x7f800000u ? 0x7e00u : 0x7c00u));
43    if (mag >= 0x477ff000u) return (uint16_t) (sign | 0x7c00u);
44    const uint32_t r = (mag + 0x0fffu + ((mag >> 13) & 1u)) >> 13;
45    if (r >= 0x7c00u) return (uint16_t) (sign | 0x7c00u);
46    return (uint16_t) (sign | r);
47}
48
49static inline float strata_f16_bits_to_f32(uint16_t h) {
50    const uint32_t sign = (uint32_t) (h & 0x8000u) << 16;
51    const uint32_t e = (h >> 10) & 0x1fu, m = h & 0x3ffu;
52    uint32_t bits;
53    if (e == 0) {
54        if (m == 0) bits = sign;
55        else {
56            const float v = (float) m * (1.0f / 16777216.0f);
57            return sign ? -v : v;
58        }
59    } else if (e == 31) {
60        bits = sign | 0x7f800000u | (m << 13);
61    } else {
62        bits = sign | ((e - 15 + 127) << 23) | (m << 13);
63    }
64    float out;
65    std::memcpy(&out, &bits, sizeof out);
66    return out;
67}
68
69static inline __half __float2half_rn(float f) { __half h; h.x = strata_f32_to_f16_bits(f); return h; }
70static inline __half __float2half(float f) { return __float2half_rn(f); }
71static inline float __half2float(__half h) { return strata_f16_bits_to_f32(h.x); }
72static inline uint16_t __half_as_ushort(__half h) { return h.x; }
73static inline __half __ushort_as_half(uint16_t u) { __half h; h.x = u; return h; }
74static inline float2 __half22float2(__half2 h) { return make_float2(strata_f16_bits_to_f32(h.x), strata_f16_bits_to_f32(h.y)); }
75static inline __half2 __floats2half2_rn(float a, float b) {
76    __half2 h;
77    h.x = strata_f32_to_f16_bits(a);
78    h.y = strata_f32_to_f16_bits(b);
79    return h;
80}
81
82// round-to-nearest float helpers: the FP ops here are IEEE and un-fused, which is what these mean
83static inline float __fmul_rn(float a, float b) { return a * b; }
84static inline float __fadd_rn(float a, float b) { return a + b; }
85static inline float __fmaf_rn(float a, float b, float c) { return a * b + c; }
86static inline float __frcp_rn(float a) { return 1.0f / a; }
87static inline float __fsqrt_rn(float a) { return __builtin_sqrtf(a); }
88
89// the half2 pack/unpack and arithmetic helpers the trellis and vision paths use
90static inline __half2 __halves2half2(__half lo, __half hi) { __half2 h; h.x = lo.x; h.y = hi.x; return h; }
91static inline __half2 __half2half2(__half v) { __half2 h; h.x = v.x; h.y = v.x; return h; }
92static inline __half __low2half(__half2 h) { __half o; o.x = h.x; return o; }
93static inline __half __high2half(__half2 h) { __half o; o.x = h.y; return o; }
94static inline float __low2float(__half2 h) { return strata_f16_bits_to_f32(h.x); }
95static inline float __high2float(__half2 h) { return strata_f16_bits_to_f32(h.y); }
96static inline __half2 __hadd2(__half2 a, __half2 b) { return __floats2half2_rn(__half2float(__low2half(a)) + __half2float(__low2half(b)), __half2float(__high2half(a)) + __half2float(__high2half(b))); }
97static inline __half2 __hmul2(__half2 a, __half2 b) { return __floats2half2_rn(__half2float(__low2half(a)) * __half2float(__low2half(b)), __half2float(__high2half(a)) * __half2float(__high2half(b))); }
98static inline __half2 __hfma2(__half2 a, __half2 b, __half2 c) { return __floats2half2_rn(__half2float(__low2half(a)) * __half2float(__low2half(b)) + __half2float(__low2half(c)), __half2float(__high2half(a)) * __half2float(__high2half(b)) + __half2float(__high2half(c))); }
99static inline __half2 __hneg2(__half2 a) { __half2 h; h.x = a.x ^ 0x8000u; h.y = a.y ^ 0x8000u; return h; }