Repository navigation
Expand file tree
/
Copy pathsquirrel_noise.cuh
More file actions
80 lines (70 loc) · 3.81 KB
/
Copy pathsquirrel_noise.cuh
File metadata and controls
80 lines (70 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef LLMQ_SQUIRREL_NOISE_CUH
#define LLMQ_SQUIRREL_NOISE_CUH
#include <cuda_bf16.h>
#include <cuda_fp8.h>
// ----------------------------------------------------------------------------
// Random Number Generation used in Stochastic Rounding
// SquirrelNoise5 - Squirrel's Raw Noise utilities (version 5)
// This gives us a random number from threadIdx/blockIdx + a single seed for the entire GPU
// todo - possibly overkill and we don't need such high quality random numbers? (tbd)
// http://eiserloh.net/noise/SquirrelNoise5.hpp
__device__ __host__ constexpr unsigned int squirrel_noise_5(unsigned int positionX, unsigned int seed)
{
constexpr unsigned int SQ5_BIT_NOISE1 = 0xd2a80a3f; // 11010010101010000000101000111111
constexpr unsigned int SQ5_BIT_NOISE2 = 0xa884f197; // 10101000100001001111000110010111
constexpr unsigned int SQ5_BIT_NOISE3 = 0x6C736F4B; // 01101100011100110110111101001011
constexpr unsigned int SQ5_BIT_NOISE4 = 0xB79F3ABB; // 10110111100111110011101010111011
constexpr unsigned int SQ5_BIT_NOISE5 = 0x1b56c4f5; // 00011011010101101100010011110101
unsigned int mangledBits = positionX;
mangledBits *= SQ5_BIT_NOISE1;
mangledBits += seed;
mangledBits ^= (mangledBits >> 9);
mangledBits += SQ5_BIT_NOISE2;
mangledBits ^= (mangledBits >> 11);
mangledBits *= SQ5_BIT_NOISE3;
mangledBits ^= (mangledBits >> 13);
mangledBits += SQ5_BIT_NOISE4;
mangledBits ^= (mangledBits >> 15);
mangledBits *= SQ5_BIT_NOISE5;
mangledBits ^= (mangledBits >> 17);
return mangledBits;
}
__device__ __host__ constexpr unsigned int get_noise_2d(int indexX, int indexY, unsigned int seed)
{
constexpr unsigned int PRIME_NUMBER = 198491317u; // Large prime number with non-boring bits
unsigned int x = static_cast<unsigned int>(indexX);
unsigned int y = static_cast<unsigned int>(indexY);
return squirrel_noise_5(x + (PRIME_NUMBER * y), seed);
}
// stochastic rounding built on top of Squirel Noise above (with seed updated per step via xorshift)
__device__ __forceinline__ void stochastic_rounding(float in, nv_bfloat16 *out, unsigned int seed, bool noise=true) {
// todo - is this stochastic rounding *too good*? can we cut any corners?
// makes sure each thread gets a different random number
unsigned int random = noise ? get_noise_2d(threadIdx.x, blockIdx.x * blockDim.x + blockIdx.y, seed) : seed;
unsigned int threshold = random & 0xFFFF;
unsigned int float_bits = __float_as_uint(in);
unsigned int rounded_bits = float_bits & 0x0000FFFF;
float_bits = (rounded_bits > threshold) ? (float_bits | 0xFFFF) : (float_bits & ~0xFFFF);
*out = __float2bfloat16_rn(__uint_as_float(float_bits));
}
__device__ __forceinline__ void stochastic_rounding(float in, __nv_fp8_e4m3 *out, unsigned int seed, bool noise=true) {
// out of range?
if (fabs(in) > 448.f) {
out->__x = __nv_cvt_float_to_fp8(in, __NV_SATFINITE, __NV_E4M3);
} else if (fabs(in) < 0.015625) {
// TODO align SR to mantissa
out->__x = __nv_cvt_float_to_fp8(in, __NV_SATFINITE, __NV_E4M3);
} else {
// makes sure each thread gets a different random number
unsigned int random = noise ? get_noise_2d(threadIdx.x, blockIdx.x * blockDim.x + blockIdx.y, seed) : seed;
unsigned int threshold = random & 0xFFFFF;
unsigned int float_bits = __float_as_uint(in);
unsigned int rounded_bits = float_bits & 0x000FFFFF;
float_bits = (rounded_bits > threshold) ? (float_bits | 0xFFFFF) : (float_bits & ~0xFFFFF);
out->__x = __nv_cvt_float_to_fp8(__uint_as_float(float_bits), __NV_SATFINITE, __NV_E4M3);
}
}
__device__ __forceinline__ void stochastic_rounding(float in, float *out, unsigned int seed, bool noise=true) {
*out = in; // dummy function for when floatX is float (FP32 mode)
}
#endif //LLMQ_SQUIRREL_NOISE_CUH