okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
2.6 KiB65 linesraw
1// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17).
2// Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file.
3// src/ds4/ds4_numerics.cuh - the BF16 and FP8 (E4M3) conversions of maya-ds4's kernels, on CUDA through its
4// intrinsics (cuda_bf16.h / cuda_fp8.h) and on HIP in plain arithmetic that gives the same bits: BF16 round to
5// nearest even; E4M3 (OCP "fn": no infinities, max 448) decoded exactly, rounded to nearest even and saturated.
6#pragma once
7
8#include <cstdint>
9#include <cuda_runtime.h>
10#if !defined(STRATA_USE_HIP)
11#include <cuda_bf16.h>
12#include <cuda_fp16.h>
13#include <cuda_fp8.h>
14#endif
15
16namespace strata::ds4 {
17
18/// F32 -> BF16 bits, round to nearest even (a NaN stays a NaN).
19__device__ __forceinline__ uint16_t bf16_rn(float f) {
20#if !defined(STRATA_USE_HIP)
21    return __bfloat16_as_ushort(__float2bfloat16_rn(f));
22#else
23    const uint32_t u = __float_as_uint(f);
24    if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t) ((u >> 16) | 0x40u);
25    return (uint16_t) ((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
26#endif
27}
28
29/// An E4M3 code's value (exact in F32).
30__device__ __forceinline__ float e4m3_value(uint8_t q) {
31#if !defined(STRATA_USE_HIP)
32    const __half_raw h = __nv_cvt_fp8_to_halfraw((__nv_fp8_storage_t) q, __NV_E4M3);
33    return __half2float(__half(h));
34#else
35    const uint32_t e = (q >> 3) & 15u, m = q & 7u;
36    float v;
37    if (e == 15u && m == 7u) v = __uint_as_float(0x7fc00000u);                         // NaN
38    else if (e == 0u) v = (float) m * 0.001953125f;                                      // m 2^-9
39    else v = __uint_as_float(((e + 120u) << 23) | (m << 20));                           // (1 + m / 8) 2^(e - 7)
40    return (q & 0x80u) ? -v : v;
41#endif
42}
43
44/// v rounded to the nearest E4M3 value (ties to even), saturated at +-448.
45__device__ __forceinline__ float e4m3_round(float v) {
46#if !defined(STRATA_USE_HIP)
47    const __nv_fp8_storage_t q = __nv_cvt_float_to_fp8(v, __NV_SATFINITE, __NV_E4M3);
48    const __half_raw h = __nv_cvt_fp8_to_halfraw(q, __NV_E4M3);
49    return __half2float(__half(h));
50#else
51    const float a = fabsf(v);
52    if (!(a == a)) return v;
53    float r;
54    if (a >= 448.0f) r = 448.0f;
55    else {
56        const int e = (int) ((__float_as_uint(a) >> 23) & 0xffu) - 127;   // a's binade (a > 0)
57        const float step = e < -6 ? 0.001953125f : __uint_as_float((uint32_t) (e - 3 + 127) << 23);
58        r = a == 0.0f ? 0.0f : rintf(a / step) * step;                      // (a power of two: exact)
59        r = fminf(r, 448.0f);
60    }
61    return v < 0.0f ? -r : r;
62#endif
63}
64
65}  // namespace strata::ds4