cumo 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +590 -152
- data/CHANGELOG.md +29 -0
- data/README.md +140 -0
- data/bench/cumo_probe.rb +1 -1
- data/cumo.gemspec +1 -1
- data/ext/cumo/cuda/cudnn.c +18 -0
- data/ext/cumo/cuda/cudnn_impl.cpp +60 -14
- data/ext/cumo/cuda/driver.c +11 -9
- data/ext/cumo/cuda/runtime.c +56 -0
- data/ext/cumo/cumo.c +4 -0
- data/ext/cumo/extconf.rb +2 -0
- data/ext/cumo/include/cumo/bit_reduce_kernel.h +6 -6
- data/ext/cumo/include/cumo/cuda/cudnn.h +40 -0
- data/ext/cumo/include/cumo/cuda/runtime.h +2 -0
- data/ext/cumo/include/cumo/intern.h +9 -0
- data/ext/cumo/include/cumo/narray.h +1 -0
- data/ext/cumo/include/cumo/reduce_kernel.h +32 -28
- data/ext/cumo/include/cumo/row_kernel.h +170 -0
- data/ext/cumo/include/cumo/row_method.h +48 -0
- data/ext/cumo/include/cumo/template_kernel.h +26 -0
- data/ext/cumo/include/cumo/types/bf16_def.h +82 -0
- data/ext/cumo/include/cumo/types/bf16_def_kernel.h +28 -0
- data/ext/cumo/include/cumo/types/bf16_macro.h +186 -0
- data/ext/cumo/include/cumo/types/bf16_macro_kernel.h +213 -0
- data/ext/cumo/include/cumo/types/bfloat.h +22 -0
- data/ext/cumo/include/cumo/types/bfloat_kernel.h +16 -0
- data/ext/cumo/include/cumo/types/bit.h +2 -0
- data/ext/cumo/include/cumo/types/bit_kernel.h +2 -0
- data/ext/cumo/include/cumo/types/complex_macro.h +2 -0
- data/ext/cumo/include/cumo/types/complex_macro_kernel.h +2 -0
- data/ext/cumo/include/cumo/types/float_def.h +8 -0
- data/ext/cumo/include/cumo/types/float_def_kernel.h +8 -0
- data/ext/cumo/include/cumo/types/float_macro.h +19 -17
- data/ext/cumo/include/cumo/types/float_macro_kernel.h +19 -19
- data/ext/cumo/include/cumo/types/half_macro.h +15 -17
- data/ext/cumo/include/cumo/types/half_macro_kernel.h +15 -0
- data/ext/cumo/include/cumo/types/robj_macro.h +2 -0
- data/ext/cumo/include/cumo/types/xint_macro.h +2 -6
- data/ext/cumo/include/cumo/types/xint_macro_kernel.h +2 -0
- data/ext/cumo/include/cumo.h +2 -2
- data/ext/cumo/narray/gen/def/bfloat.rb +56 -0
- data/ext/cumo/narray/gen/def/bit.rb +11 -1
- data/ext/cumo/narray/gen/def/dcomplex.rb +11 -1
- data/ext/cumo/narray/gen/def/dfloat.rb +10 -1
- data/ext/cumo/narray/gen/def/hfloat.rb +10 -1
- data/ext/cumo/narray/gen/def/int16.rb +11 -1
- data/ext/cumo/narray/gen/def/int32.rb +11 -1
- data/ext/cumo/narray/gen/def/int64.rb +11 -1
- data/ext/cumo/narray/gen/def/int8.rb +11 -1
- data/ext/cumo/narray/gen/def/robject.rb +11 -1
- data/ext/cumo/narray/gen/def/scomplex.rb +11 -1
- data/ext/cumo/narray/gen/def/sfloat.rb +10 -1
- data/ext/cumo/narray/gen/def/uint16.rb +11 -1
- data/ext/cumo/narray/gen/def/uint32.rb +11 -1
- data/ext/cumo/narray/gen/def/uint64.rb +11 -1
- data/ext/cumo/narray/gen/def/uint8.rb +11 -1
- data/ext/cumo/narray/gen/narray_def.rb +0 -5
- data/ext/cumo/narray/gen/spec.rb +11 -14
- data/ext/cumo/narray/gen/tmpl/accum_arg.c +1 -1
- data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +9 -9
- data/ext/cumo/narray/gen/tmpl/accum_index.c +1 -1
- data/ext/cumo/narray/gen/tmpl/batch_norm.c +3 -3
- data/ext/cumo/narray/gen/tmpl/batch_norm_backward.c +3 -3
- data/ext/cumo/narray/gen/tmpl/complex_accum_kernel.cu +15 -5
- data/ext/cumo/narray/gen/tmpl/conv.c +88 -121
- data/ext/cumo/narray/gen/tmpl/conv_grad_w.c +89 -81
- data/ext/cumo/narray/gen/tmpl/conv_transpose.c +88 -121
- data/ext/cumo/narray/gen/tmpl/cum.c +8 -7
- data/ext/cumo/narray/gen/tmpl/cum_kernel.cu +7 -6
- data/ext/cumo/narray/gen/tmpl/fixed_batch_norm.c +3 -3
- data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +22 -10
- data/ext/cumo/narray/gen/tmpl/gemm.c +11 -35
- data/ext/cumo/narray/gen/tmpl/layer_norm.c +91 -0
- data/ext/cumo/narray/gen/tmpl/layer_norm_kernel.cu +54 -0
- data/ext/cumo/narray/gen/tmpl/lib.c +1 -0
- data/ext/cumo/narray/gen/tmpl/lib_kernel.cu +1 -0
- data/ext/cumo/narray/gen/tmpl/median.c +1 -1
- data/ext/cumo/narray/gen/tmpl/poly.c +0 -3
- data/ext/cumo/narray/gen/tmpl/poly_kernel.cu +1 -1
- data/ext/cumo/narray/gen/tmpl/pooling_backward.c +2 -1
- data/ext/cumo/narray/gen/tmpl/pooling_forward.c +2 -1
- data/ext/cumo/narray/gen/tmpl/rand.c +9 -9
- data/ext/cumo/narray/gen/tmpl/rand_kernel.cu +9 -9
- data/ext/cumo/narray/gen/tmpl/rand_norm.c +4 -4
- data/ext/cumo/narray/gen/tmpl/rand_norm_kernel.cu +7 -7
- data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +6 -6
- data/ext/cumo/narray/gen/tmpl/softmax.c +56 -0
- data/ext/cumo/narray/gen/tmpl/softmax_kernel.cu +157 -0
- data/ext/cumo/narray/gen/tmpl/sort.c +9 -32
- data/ext/cumo/narray/gen/tmpl/sort_index.c +16 -95
- data/ext/cumo/narray/gen/tmpl/store_array.c +12 -9
- data/ext/cumo/narray/gen/tmpl_bit/mask.c +11 -11
- data/ext/cumo/narray/gen/tmpl_bit/store_array.c +5 -9
- data/ext/cumo/narray/gen/tmpl_bit/where.c +50 -11
- data/ext/cumo/narray/gen/tmpl_bit/where2.c +11 -12
- data/ext/cumo/narray/gen/tmpl_bit/where_kernel.cu +1 -2
- data/ext/cumo/narray/index.c +2 -1
- data/ext/cumo/narray/math.c +2 -0
- data/ext/cumo/narray/narray.c +27 -1
- data/ext/cumo/narray/ndloop.c +73 -4
- data/ext/cumo/narray/sort_kernel.cu +61 -41
- data/ext/cumo/narray/struct.c +3 -0
- data/lib/cumo/cuda/cudnn.rb +1 -1
- data/lib/cumo/narray/extra.rb +2 -2
- data/test/bfloat_test.rb +238 -0
- data/test/cuda/memory_pool_test.rb +8 -7
- data/test/cuda/runtime_test.rb +25 -0
- data/test/cudnn_test.rb +108 -24
- data/test/extra_test.rb +1 -1
- data/test/fused_test.rb +311 -0
- data/test/math_test.rb +77 -2
- data/test/narray_alt_coverage_test.rb +1 -0
- data/test/narray_test.rb +70 -10
- data/test/test_helper.rb +1 -0
- metadata +20 -4
- data/ext/cumo/narray/gen/tmpl/qsort.c +0 -205
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
#include "float_def.h"
|
|
2
|
+
#include "half_def.h"
|
|
3
|
+
#include "bf16_def.h"
|
|
4
|
+
|
|
5
|
+
#define CUMO_BFLOAT_EPSILON 7.8125e-03f
|
|
6
|
+
|
|
7
|
+
#define m_zero cumo_float2bfloat(0.0f)
|
|
8
|
+
#define m_one cumo_float2bfloat(1.0f)
|
|
9
|
+
|
|
10
|
+
#define m_num_to_data(x) cumo_na_ruby_num_to_bfloat(x)
|
|
11
|
+
#define m_data_to_num(x) rb_float_new(cumo_bfloat2float(x))
|
|
12
|
+
|
|
13
|
+
#define m_from_double(x) cumo_double2bfloat((double)(x))
|
|
14
|
+
#define m_from_real(x) cumo_double2bfloat((double)(x))
|
|
15
|
+
#define m_from_sint(x) cumo_double2bfloat((double)(x))
|
|
16
|
+
#define m_from_int32(x) cumo_double2bfloat((double)(x))
|
|
17
|
+
#define m_from_int64(x) cumo_double2bfloat((double)(x))
|
|
18
|
+
#define m_from_uint32(x) cumo_double2bfloat((double)(x))
|
|
19
|
+
#define m_from_uint64(x) cumo_double2bfloat((double)(x))
|
|
20
|
+
#define m_from_bfloat(x) (x)
|
|
21
|
+
#define m_from_half(x) cumo_float2bfloat(cumo_half2float(x))
|
|
22
|
+
|
|
23
|
+
#define m_add(x,y) cumo_float2bfloat(cumo_bfloat2float(x)+cumo_bfloat2float(y))
|
|
24
|
+
#define m_sub(x,y) cumo_float2bfloat(cumo_bfloat2float(x)-cumo_bfloat2float(y))
|
|
25
|
+
#define m_mul(x,y) cumo_float2bfloat(cumo_bfloat2float(x)*cumo_bfloat2float(y))
|
|
26
|
+
#define m_div(x,y) cumo_float2bfloat(cumo_bfloat2float(x)/cumo_bfloat2float(y))
|
|
27
|
+
#define m_div_check(x,y) (cumo_bfloat2float(y)==0)
|
|
28
|
+
|
|
29
|
+
static inline float cumo_bfloat_floored_mod(float x, float y)
|
|
30
|
+
{
|
|
31
|
+
float r = fmodf(x,y);
|
|
32
|
+
if (r != 0 && ((r < 0) != (y < 0))) {
|
|
33
|
+
r += y;
|
|
34
|
+
}
|
|
35
|
+
return r;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#define m_mod(x,y) cumo_float2bfloat(cumo_bfloat_floored_mod(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
39
|
+
#define m_divmod(x,y,a,b) \
|
|
40
|
+
{float cumo_b_x = cumo_bfloat2float(x), cumo_b_y = cumo_bfloat2float(y); \
|
|
41
|
+
float cumo_b_b = fmodf(cumo_b_x,cumo_b_y); \
|
|
42
|
+
float cumo_b_a = roundf((cumo_b_x-cumo_b_b)/cumo_b_y); \
|
|
43
|
+
if (cumo_b_b != 0 && ((cumo_b_b < 0) != (cumo_b_y < 0))) { \
|
|
44
|
+
cumo_b_b += cumo_b_y; cumo_b_a -= 1; \
|
|
45
|
+
} \
|
|
46
|
+
a = cumo_float2bfloat(cumo_b_a); b = cumo_float2bfloat(cumo_b_b);}
|
|
47
|
+
#define m_pow(x,y) cumo_float2bfloat(powf(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
48
|
+
#define m_pow_int(x,y) cumo_float2bfloat(cumo_bfloat_pow_int(cumo_bfloat2float(x),(int)(y)))
|
|
49
|
+
|
|
50
|
+
#define m_abs(x) cumo_float2bfloat(fabsf(cumo_bfloat2float(x)))
|
|
51
|
+
#define m_minus(x) cumo_float2bfloat(-cumo_bfloat2float(x))
|
|
52
|
+
#define m_reciprocal(x) cumo_float2bfloat(1.0f/cumo_bfloat2float(x))
|
|
53
|
+
#define m_square(x) cumo_float2bfloat(cumo_bfloat2float(x)*cumo_bfloat2float(x))
|
|
54
|
+
#define m_floor(x) cumo_float2bfloat(floorf(cumo_bfloat2float(x)))
|
|
55
|
+
#define m_round(x) cumo_float2bfloat(roundf(cumo_bfloat2float(x)))
|
|
56
|
+
#define m_ceil(x) cumo_float2bfloat(ceilf(cumo_bfloat2float(x)))
|
|
57
|
+
#define m_trunc(x) cumo_float2bfloat(truncf(cumo_bfloat2float(x)))
|
|
58
|
+
#define m_rint(x) cumo_float2bfloat(rintf(cumo_bfloat2float(x)))
|
|
59
|
+
#define m_sign(x) cumo_float2bfloat(cumo_bfloat_sign(cumo_bfloat2float(x)))
|
|
60
|
+
#define m_copysign(x,y) cumo_float2bfloat(copysignf(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
61
|
+
#define m_signbit(x) signbit(cumo_bfloat2float(x))
|
|
62
|
+
#define m_modf(x,y,z) {float cumo_b_i; float cumo_b_f = modff(cumo_bfloat2float(x),&cumo_b_i); y = cumo_float2bfloat(cumo_b_f); z = cumo_float2bfloat(cumo_b_i);}
|
|
63
|
+
|
|
64
|
+
#define m_eq(x,y) (cumo_bfloat2float(x)==cumo_bfloat2float(y))
|
|
65
|
+
#define m_ne(x,y) (cumo_bfloat2float(x)!=cumo_bfloat2float(y))
|
|
66
|
+
#define m_gt(x,y) (cumo_bfloat2float(x)>cumo_bfloat2float(y))
|
|
67
|
+
#define m_ge(x,y) (cumo_bfloat2float(x)>=cumo_bfloat2float(y))
|
|
68
|
+
#define m_lt(x,y) (cumo_bfloat2float(x)<cumo_bfloat2float(y))
|
|
69
|
+
#define m_le(x,y) (cumo_bfloat2float(x)<=cumo_bfloat2float(y))
|
|
70
|
+
|
|
71
|
+
#define m_isnan(x) isnan(cumo_bfloat2float(x))
|
|
72
|
+
#define m_isinf(x) isinf(cumo_bfloat2float(x))
|
|
73
|
+
#define m_isposinf(x) (isinf(cumo_bfloat2float(x)) && signbit(cumo_bfloat2float(x))==0)
|
|
74
|
+
#define m_isneginf(x) (isinf(cumo_bfloat2float(x)) && signbit(cumo_bfloat2float(x)))
|
|
75
|
+
#define m_isfinite(x) isfinite(cumo_bfloat2float(x))
|
|
76
|
+
|
|
77
|
+
#define not_nan(x) (cumo_bfloat2float(x)==cumo_bfloat2float(x))
|
|
78
|
+
|
|
79
|
+
#define m_mulsum_init INT2FIX(0)
|
|
80
|
+
|
|
81
|
+
#define m_sprintf(s,x) sprintf(s,"%g",cumo_bfloat2float(x))
|
|
82
|
+
|
|
83
|
+
#define m_sqrt(x) cumo_float2bfloat(sqrtf(cumo_bfloat2float(x)))
|
|
84
|
+
#define m_cbrt(x) cumo_float2bfloat(cbrtf(cumo_bfloat2float(x)))
|
|
85
|
+
#define m_log(x) cumo_float2bfloat(logf(cumo_bfloat2float(x)))
|
|
86
|
+
#define m_log2(x) cumo_float2bfloat(log2f(cumo_bfloat2float(x)))
|
|
87
|
+
#define m_log10(x) cumo_float2bfloat(log10f(cumo_bfloat2float(x)))
|
|
88
|
+
#define m_exp(x) cumo_float2bfloat(expf(cumo_bfloat2float(x)))
|
|
89
|
+
#define m_exp2(x) cumo_float2bfloat(exp2f(cumo_bfloat2float(x)))
|
|
90
|
+
#ifdef HAVE_EXP10
|
|
91
|
+
#define m_exp10(x) cumo_float2bfloat(exp10f(cumo_bfloat2float(x)))
|
|
92
|
+
#else
|
|
93
|
+
#define m_exp10(x) cumo_float2bfloat(powf(10.0f,cumo_bfloat2float(x)))
|
|
94
|
+
#endif
|
|
95
|
+
#define m_expm1(x) cumo_float2bfloat(expm1f(cumo_bfloat2float(x)))
|
|
96
|
+
#define m_log1p(x) cumo_float2bfloat(log1pf(cumo_bfloat2float(x)))
|
|
97
|
+
|
|
98
|
+
#define m_sin(x) cumo_float2bfloat(sinf(cumo_bfloat2float(x)))
|
|
99
|
+
#define m_cos(x) cumo_float2bfloat(cosf(cumo_bfloat2float(x)))
|
|
100
|
+
#define m_tan(x) cumo_float2bfloat(tanf(cumo_bfloat2float(x)))
|
|
101
|
+
#define m_asin(x) cumo_float2bfloat(asinf(cumo_bfloat2float(x)))
|
|
102
|
+
#define m_acos(x) cumo_float2bfloat(acosf(cumo_bfloat2float(x)))
|
|
103
|
+
#define m_atan(x) cumo_float2bfloat(atanf(cumo_bfloat2float(x)))
|
|
104
|
+
#define m_sinh(x) cumo_float2bfloat(sinhf(cumo_bfloat2float(x)))
|
|
105
|
+
#define m_cosh(x) cumo_float2bfloat(coshf(cumo_bfloat2float(x)))
|
|
106
|
+
#define m_tanh(x) cumo_float2bfloat(tanhf(cumo_bfloat2float(x)))
|
|
107
|
+
#define m_asinh(x) cumo_float2bfloat(asinhf(cumo_bfloat2float(x)))
|
|
108
|
+
#define m_acosh(x) cumo_float2bfloat(acoshf(cumo_bfloat2float(x)))
|
|
109
|
+
#define m_atanh(x) cumo_float2bfloat(atanhf(cumo_bfloat2float(x)))
|
|
110
|
+
#define m_atan2(x,y) cumo_float2bfloat(atan2f(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
111
|
+
#define m_hypot(x,y) cumo_float2bfloat(hypotf(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
112
|
+
#define m_sinc(x) cumo_float2bfloat(cumo_bfloat_sinc(cumo_bfloat2float(x)))
|
|
113
|
+
|
|
114
|
+
#define m_erf(x) cumo_float2bfloat(erff(cumo_bfloat2float(x)))
|
|
115
|
+
#define m_erfc(x) cumo_float2bfloat(erfcf(cumo_bfloat2float(x)))
|
|
116
|
+
#define m_gelu(x) cumo_float2bfloat(cumo_bfloat_gelu(cumo_bfloat2float(x)))
|
|
117
|
+
#define m_gelu_tanh(x) cumo_float2bfloat(cumo_bfloat_gelu_tanh(cumo_bfloat2float(x)))
|
|
118
|
+
#define m_ldexp(x,y) cumo_float2bfloat(cumo_bfloat_ldexp(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
119
|
+
#define m_frexp(x,exp) cumo_float2bfloat(frexpf(cumo_bfloat2float(x),exp))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
static inline float cumo_bfloat_pow_positive_int(float x, unsigned int p)
|
|
123
|
+
{
|
|
124
|
+
float r = 1.0f;
|
|
125
|
+
switch (p) {
|
|
126
|
+
case 0: return 1.0f;
|
|
127
|
+
case 1: return x;
|
|
128
|
+
case 2: return x*x;
|
|
129
|
+
case 3: return x*x*x;
|
|
130
|
+
case 4: x = x*x; return x*x;
|
|
131
|
+
}
|
|
132
|
+
if (p > 64) return powf(x, (float)p);
|
|
133
|
+
while (p) {
|
|
134
|
+
if (p & 1) r *= x;
|
|
135
|
+
x *= x;
|
|
136
|
+
p >>= 1;
|
|
137
|
+
}
|
|
138
|
+
return r;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static inline float cumo_bfloat_pow_int(float x, int p)
|
|
142
|
+
{
|
|
143
|
+
if (p < 0) return 1.0f / cumo_bfloat_pow_positive_int(x, -(unsigned int)p);
|
|
144
|
+
return cumo_bfloat_pow_positive_int(x, (unsigned int)p);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static inline float cumo_bfloat_ldexp(float x, float y)
|
|
148
|
+
{
|
|
149
|
+
if (!(y == y)) return y;
|
|
150
|
+
if (y > 64.0f) y = 64.0f;
|
|
151
|
+
if (y < -64.0f) y = -64.0f;
|
|
152
|
+
return ldexpf(x, (int)y);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static inline float cumo_bfloat_sinc(float x)
|
|
156
|
+
{
|
|
157
|
+
return (x==0) ? 1.0f : (sinf(x)/x);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
static inline float cumo_bfloat_sign(float x)
|
|
161
|
+
{
|
|
162
|
+
return (x==0) ? 0.0f : ((x>0) ? 1.0f : ((x<0) ? -1.0f : x));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static inline cumo_bfloat cumo_na_ruby_num_to_bfloat(VALUE x)
|
|
166
|
+
{
|
|
167
|
+
return cumo_double2bfloat(NIL_P(x) ? nan("") : NUM2DBL(x));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static inline cumo_bfloat f_seq(cumo_bfloat x, cumo_bfloat y, double c)
|
|
171
|
+
{
|
|
172
|
+
return cumo_float2bfloat(cumo_bfloat2float(x) + cumo_bfloat2float(y) * (float)c);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#include "real_accum.h"
|
|
176
|
+
|
|
177
|
+
static inline float cumo_bfloat_gelu(float x)
|
|
178
|
+
{
|
|
179
|
+
return 0.5f * x * (1.0f + erff(x * (float)CUMO_M_SQRT1_2));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// The approximation GPT-2 and the transformers after it were trained with.
|
|
183
|
+
static inline float cumo_bfloat_gelu_tanh(float x)
|
|
184
|
+
{
|
|
185
|
+
return 0.5f * x * (1.0f + tanhf((float)CUMO_M_SQRT_2_OVER_PI * (x + (float)CUMO_GELU_TANH_CUBIC * x * x * x)));
|
|
186
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
#ifndef CUMO_BFLOAT_MACRO_KERNEL_H
|
|
2
|
+
#define CUMO_BFLOAT_MACRO_KERNEL_H
|
|
3
|
+
|
|
4
|
+
#include "float_def_kernel.h"
|
|
5
|
+
#include "half_def_kernel.h"
|
|
6
|
+
#include "bf16_def_kernel.h"
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
#define CUMO_BFLOAT_EPSILON 7.8125e-03f
|
|
10
|
+
|
|
11
|
+
#define m_zero cumo_float2bfloat(0.0f)
|
|
12
|
+
#define m_one cumo_float2bfloat(1.0f)
|
|
13
|
+
|
|
14
|
+
#define m_from_double(x) cumo_double2bfloat((double)(x))
|
|
15
|
+
#define m_from_real(x) cumo_double2bfloat((double)(x))
|
|
16
|
+
#define m_from_sint(x) cumo_double2bfloat((double)(x))
|
|
17
|
+
#define m_from_int32(x) cumo_double2bfloat((double)(x))
|
|
18
|
+
#define m_from_int64(x) cumo_double2bfloat((double)(x))
|
|
19
|
+
#define m_from_uint32(x) cumo_double2bfloat((double)(x))
|
|
20
|
+
#define m_from_uint64(x) cumo_double2bfloat((double)(x))
|
|
21
|
+
#define m_from_bfloat(x) (x)
|
|
22
|
+
#define m_from_half(x) cumo_float2bfloat(cumo_half2float(x))
|
|
23
|
+
|
|
24
|
+
// Overloaded rather than substituted, so that a scan or a reduction can apply
|
|
25
|
+
// the same rule to its own wider accumulator.
|
|
26
|
+
__host__ __device__ static inline float cumo_bfloat_add(float x, float y) { return x + y; }
|
|
27
|
+
__host__ __device__ static inline float cumo_bfloat_sub(float x, float y) { return x - y; }
|
|
28
|
+
__host__ __device__ static inline float cumo_bfloat_mul(float x, float y) { return x * y; }
|
|
29
|
+
__host__ __device__ static inline float cumo_bfloat_div(float x, float y) { return x / y; }
|
|
30
|
+
__host__ __device__ static inline bool cumo_bfloat_not_nan(float x) { return x == x; }
|
|
31
|
+
__host__ __device__ static inline cumo_bfloat cumo_bfloat_add(cumo_bfloat x, cumo_bfloat y) { return cumo_float2bfloat(cumo_bfloat2float(x) + cumo_bfloat2float(y)); }
|
|
32
|
+
__host__ __device__ static inline cumo_bfloat cumo_bfloat_sub(cumo_bfloat x, cumo_bfloat y) { return cumo_float2bfloat(cumo_bfloat2float(x) - cumo_bfloat2float(y)); }
|
|
33
|
+
__host__ __device__ static inline cumo_bfloat cumo_bfloat_mul(cumo_bfloat x, cumo_bfloat y) { return cumo_float2bfloat(cumo_bfloat2float(x) * cumo_bfloat2float(y)); }
|
|
34
|
+
__host__ __device__ static inline cumo_bfloat cumo_bfloat_div(cumo_bfloat x, cumo_bfloat y) { return cumo_float2bfloat(cumo_bfloat2float(x) / cumo_bfloat2float(y)); }
|
|
35
|
+
__host__ __device__ static inline bool cumo_bfloat_not_nan(cumo_bfloat x) { return cumo_bfloat2float(x) == cumo_bfloat2float(x); }
|
|
36
|
+
|
|
37
|
+
#define m_add(x,y) cumo_bfloat_add(x,y)
|
|
38
|
+
#define m_sub(x,y) cumo_bfloat_sub(x,y)
|
|
39
|
+
#define m_mul(x,y) cumo_bfloat_mul(x,y)
|
|
40
|
+
#define m_div(x,y) cumo_bfloat_div(x,y)
|
|
41
|
+
#define m_div_check(x,y) (cumo_bfloat2float(y)==0)
|
|
42
|
+
|
|
43
|
+
__host__ __device__ static inline float cumo_bfloat_floored_mod(float x, float y)
|
|
44
|
+
{
|
|
45
|
+
float r = fmodf(x,y);
|
|
46
|
+
if (r != 0 && ((r < 0) != (y < 0))) {
|
|
47
|
+
r += y;
|
|
48
|
+
}
|
|
49
|
+
return r;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#define m_mod(x,y) cumo_float2bfloat(cumo_bfloat_floored_mod(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
53
|
+
#define m_divmod(x,y,a,b) \
|
|
54
|
+
{float cumo_b_x = cumo_bfloat2float(x), cumo_b_y = cumo_bfloat2float(y); \
|
|
55
|
+
float cumo_b_b = fmodf(cumo_b_x,cumo_b_y); \
|
|
56
|
+
float cumo_b_a = roundf((cumo_b_x-cumo_b_b)/cumo_b_y); \
|
|
57
|
+
if (cumo_b_b != 0 && ((cumo_b_b < 0) != (cumo_b_y < 0))) { \
|
|
58
|
+
cumo_b_b += cumo_b_y; cumo_b_a -= 1; \
|
|
59
|
+
} \
|
|
60
|
+
a = cumo_float2bfloat(cumo_b_a); b = cumo_float2bfloat(cumo_b_b);}
|
|
61
|
+
#define m_pow(x,y) cumo_float2bfloat(powf(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
62
|
+
#define m_pow_int(x,y) cumo_float2bfloat(cumo_bfloat_pow_int(cumo_bfloat2float(x),(int)(y)))
|
|
63
|
+
|
|
64
|
+
#define m_abs(x) cumo_float2bfloat(fabsf(cumo_bfloat2float(x)))
|
|
65
|
+
#define m_minus(x) cumo_float2bfloat(-cumo_bfloat2float(x))
|
|
66
|
+
#define m_reciprocal(x) cumo_float2bfloat(1.0f/cumo_bfloat2float(x))
|
|
67
|
+
#define m_square(x) cumo_float2bfloat(cumo_bfloat2float(x)*cumo_bfloat2float(x))
|
|
68
|
+
#define m_floor(x) cumo_float2bfloat(floorf(cumo_bfloat2float(x)))
|
|
69
|
+
#define m_round(x) cumo_float2bfloat(roundf(cumo_bfloat2float(x)))
|
|
70
|
+
#define m_ceil(x) cumo_float2bfloat(ceilf(cumo_bfloat2float(x)))
|
|
71
|
+
#define m_trunc(x) cumo_float2bfloat(truncf(cumo_bfloat2float(x)))
|
|
72
|
+
#define m_rint(x) cumo_float2bfloat(rintf(cumo_bfloat2float(x)))
|
|
73
|
+
#define m_sign(x) cumo_float2bfloat(cumo_bfloat_sign(cumo_bfloat2float(x)))
|
|
74
|
+
#define m_copysign(x,y) cumo_float2bfloat(copysignf(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
75
|
+
#define m_signbit(x) signbit(cumo_bfloat2float(x))
|
|
76
|
+
#define m_modf(x,y,z) {float cumo_b_i; float cumo_b_f = modff(cumo_bfloat2float(x),&cumo_b_i); y = cumo_float2bfloat(cumo_b_f); z = cumo_float2bfloat(cumo_b_i);}
|
|
77
|
+
|
|
78
|
+
#define m_eq(x,y) (cumo_bfloat2float(x)==cumo_bfloat2float(y))
|
|
79
|
+
#define m_ne(x,y) (cumo_bfloat2float(x)!=cumo_bfloat2float(y))
|
|
80
|
+
#define m_gt(x,y) (cumo_bfloat2float(x)>cumo_bfloat2float(y))
|
|
81
|
+
#define m_ge(x,y) (cumo_bfloat2float(x)>=cumo_bfloat2float(y))
|
|
82
|
+
#define m_lt(x,y) (cumo_bfloat2float(x)<cumo_bfloat2float(y))
|
|
83
|
+
#define m_le(x,y) (cumo_bfloat2float(x)<=cumo_bfloat2float(y))
|
|
84
|
+
|
|
85
|
+
#define m_isnan(x) isnan(cumo_bfloat2float(x))
|
|
86
|
+
#define m_isinf(x) isinf(cumo_bfloat2float(x))
|
|
87
|
+
#define m_isposinf(x) (isinf(cumo_bfloat2float(x)) && signbit(cumo_bfloat2float(x))==0)
|
|
88
|
+
#define m_isneginf(x) (isinf(cumo_bfloat2float(x)) && signbit(cumo_bfloat2float(x)))
|
|
89
|
+
#define m_isfinite(x) isfinite(cumo_bfloat2float(x))
|
|
90
|
+
|
|
91
|
+
#define not_nan(x) cumo_bfloat_not_nan(x)
|
|
92
|
+
|
|
93
|
+
#define m_mulsum_init m_zero
|
|
94
|
+
|
|
95
|
+
#define m_sprintf(s,x) sprintf(s,"%g",cumo_bfloat2float(x))
|
|
96
|
+
|
|
97
|
+
#define m_sqrt(x) cumo_float2bfloat(sqrtf(cumo_bfloat2float(x)))
|
|
98
|
+
#define m_cbrt(x) cumo_float2bfloat(cbrtf(cumo_bfloat2float(x)))
|
|
99
|
+
#define m_log(x) cumo_float2bfloat(logf(cumo_bfloat2float(x)))
|
|
100
|
+
#define m_log2(x) cumo_float2bfloat(log2f(cumo_bfloat2float(x)))
|
|
101
|
+
#define m_log10(x) cumo_float2bfloat(log10f(cumo_bfloat2float(x)))
|
|
102
|
+
#define m_exp(x) cumo_float2bfloat(expf(cumo_bfloat2float(x)))
|
|
103
|
+
#define m_exp2(x) cumo_float2bfloat(exp2f(cumo_bfloat2float(x)))
|
|
104
|
+
#ifdef HAVE_EXP10
|
|
105
|
+
#define m_exp10(x) cumo_float2bfloat(exp10f(cumo_bfloat2float(x)))
|
|
106
|
+
#else
|
|
107
|
+
#define m_exp10(x) cumo_float2bfloat(powf(10.0f,cumo_bfloat2float(x)))
|
|
108
|
+
#endif
|
|
109
|
+
#define m_expm1(x) cumo_float2bfloat(expm1f(cumo_bfloat2float(x)))
|
|
110
|
+
#define m_log1p(x) cumo_float2bfloat(log1pf(cumo_bfloat2float(x)))
|
|
111
|
+
|
|
112
|
+
#define m_sin(x) cumo_float2bfloat(sinf(cumo_bfloat2float(x)))
|
|
113
|
+
#define m_cos(x) cumo_float2bfloat(cosf(cumo_bfloat2float(x)))
|
|
114
|
+
#define m_tan(x) cumo_float2bfloat(tanf(cumo_bfloat2float(x)))
|
|
115
|
+
#define m_asin(x) cumo_float2bfloat(asinf(cumo_bfloat2float(x)))
|
|
116
|
+
#define m_acos(x) cumo_float2bfloat(acosf(cumo_bfloat2float(x)))
|
|
117
|
+
#define m_atan(x) cumo_float2bfloat(atanf(cumo_bfloat2float(x)))
|
|
118
|
+
#define m_sinh(x) cumo_float2bfloat(sinhf(cumo_bfloat2float(x)))
|
|
119
|
+
#define m_cosh(x) cumo_float2bfloat(coshf(cumo_bfloat2float(x)))
|
|
120
|
+
#define m_tanh(x) cumo_float2bfloat(tanhf(cumo_bfloat2float(x)))
|
|
121
|
+
#define m_asinh(x) cumo_float2bfloat(asinhf(cumo_bfloat2float(x)))
|
|
122
|
+
#define m_acosh(x) cumo_float2bfloat(acoshf(cumo_bfloat2float(x)))
|
|
123
|
+
#define m_atanh(x) cumo_float2bfloat(atanhf(cumo_bfloat2float(x)))
|
|
124
|
+
#define m_atan2(x,y) cumo_float2bfloat(atan2f(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
125
|
+
#define m_hypot(x,y) cumo_float2bfloat(hypotf(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
126
|
+
#define m_sinc(x) cumo_float2bfloat(cumo_bfloat_sinc(cumo_bfloat2float(x)))
|
|
127
|
+
|
|
128
|
+
#define m_erf(x) cumo_float2bfloat(erff(cumo_bfloat2float(x)))
|
|
129
|
+
#define m_erfc(x) cumo_float2bfloat(erfcf(cumo_bfloat2float(x)))
|
|
130
|
+
#define m_gelu(x) cumo_float2bfloat(cumo_bfloat_gelu(cumo_bfloat2float(x)))
|
|
131
|
+
#define m_gelu_tanh(x) cumo_float2bfloat(cumo_bfloat_gelu_tanh(cumo_bfloat2float(x)))
|
|
132
|
+
#define m_ldexp(x,y) cumo_float2bfloat(cumo_bfloat_ldexp(cumo_bfloat2float(x),cumo_bfloat2float(y)))
|
|
133
|
+
#define m_frexp(x,exp) cumo_float2bfloat(frexpf(cumo_bfloat2float(x),exp))
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
__host__ __device__ static inline float cumo_bfloat_pow_positive_int(float x, unsigned int p)
|
|
137
|
+
{
|
|
138
|
+
float r = 1.0f;
|
|
139
|
+
switch (p) {
|
|
140
|
+
case 0: return 1.0f;
|
|
141
|
+
case 1: return x;
|
|
142
|
+
case 2: return x*x;
|
|
143
|
+
case 3: return x*x*x;
|
|
144
|
+
case 4: x = x*x; return x*x;
|
|
145
|
+
}
|
|
146
|
+
if (p > 64) return powf(x, (float)p);
|
|
147
|
+
while (p) {
|
|
148
|
+
if (p & 1) r *= x;
|
|
149
|
+
x *= x;
|
|
150
|
+
p >>= 1;
|
|
151
|
+
}
|
|
152
|
+
return r;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
__host__ __device__ static inline float cumo_bfloat_pow_int(float x, int p)
|
|
156
|
+
{
|
|
157
|
+
if (p < 0) return 1.0f / cumo_bfloat_pow_positive_int(x, -(unsigned int)p);
|
|
158
|
+
return cumo_bfloat_pow_positive_int(x, (unsigned int)p);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The bit-pattern intrinsics are device-only before CUDA 12, so the bits are
|
|
162
|
+
// taken the way a host caller can take them too.
|
|
163
|
+
__host__ __device__ static inline cumo_bfloat cumo_bfloat_step_down(cumo_bfloat h)
|
|
164
|
+
{
|
|
165
|
+
unsigned short b;
|
|
166
|
+
cumo_bfloat r;
|
|
167
|
+
memcpy(&b, &h, sizeof(b));
|
|
168
|
+
if (b == 0x0000u) {
|
|
169
|
+
b = 0x8001u;
|
|
170
|
+
} else {
|
|
171
|
+
b = (unsigned short)((b & 0x8000u) ? (b + 1) : (b - 1));
|
|
172
|
+
}
|
|
173
|
+
memcpy(&r, &b, sizeof(b));
|
|
174
|
+
return r;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
__host__ __device__ static inline float cumo_bfloat_ldexp(float x, float y)
|
|
178
|
+
{
|
|
179
|
+
if (!(y == y)) return y;
|
|
180
|
+
if (y > 64.0f) y = 64.0f;
|
|
181
|
+
if (y < -64.0f) y = -64.0f;
|
|
182
|
+
return ldexpf(x, (int)y);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
__host__ __device__ static inline float cumo_bfloat_sinc(float x)
|
|
186
|
+
{
|
|
187
|
+
return (x==0) ? 1.0f : (sinf(x)/x);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
__host__ __device__ static inline float cumo_bfloat_sign(float x)
|
|
191
|
+
{
|
|
192
|
+
return (x==0) ? 0.0f : ((x>0) ? 1.0f : ((x<0) ? -1.0f : x));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
__host__ __device__ static inline cumo_bfloat f_seq(cumo_bfloat x, cumo_bfloat y, double c)
|
|
196
|
+
{
|
|
197
|
+
return cumo_float2bfloat(cumo_bfloat2float(x) + cumo_bfloat2float(y) * (float)c);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#include "real_accum_kernel.h"
|
|
201
|
+
|
|
202
|
+
__host__ __device__ static inline float cumo_bfloat_gelu(float x)
|
|
203
|
+
{
|
|
204
|
+
return 0.5f * x * (1.0f + erff(x * (float)CUMO_M_SQRT1_2));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// The approximation GPT-2 and the transformers after it were trained with.
|
|
208
|
+
__host__ __device__ static inline float cumo_bfloat_gelu_tanh(float x)
|
|
209
|
+
{
|
|
210
|
+
return 0.5f * x * (1.0f + tanhf((float)CUMO_M_SQRT_2_OVER_PI * (x + (float)CUMO_GELU_TANH_CUBIC * x * x * x)));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#endif // CUMO_BFLOAT_MACRO_KERNEL_H
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#include "bf16_def.h"
|
|
2
|
+
|
|
3
|
+
typedef cumo_bfloat dtype;
|
|
4
|
+
typedef cumo_bfloat rtype;
|
|
5
|
+
#define cT cumo_cBFloat
|
|
6
|
+
#define cRT cumo_cBFloat
|
|
7
|
+
#define mTM cumo_mBFloatMath
|
|
8
|
+
|
|
9
|
+
#include "bf16_macro.h"
|
|
10
|
+
#include "cublas_v2.h"
|
|
11
|
+
#include "cumo/cuda/cublas.h"
|
|
12
|
+
#include "cumo/cuda/cudnn.h"
|
|
13
|
+
|
|
14
|
+
#define m_extract(x) rb_float_new(cumo_bfloat2float(*(cumo_bfloat*)x))
|
|
15
|
+
#define m_nearly_eq(x,y) (fabsf(cumo_bfloat2float(x)-cumo_bfloat2float(y))<=(fabsf(cumo_bfloat2float(x))+fabsf(cumo_bfloat2float(y)))*CUMO_BFLOAT_EPSILON*2)
|
|
16
|
+
|
|
17
|
+
#define M_EPSILON rb_float_new(7.8125e-03)
|
|
18
|
+
#define M_MIN rb_float_new(1.17549435082228750797e-38)
|
|
19
|
+
#define M_MAX rb_float_new(3.38953138925153547590e+38)
|
|
20
|
+
|
|
21
|
+
#define DATA_MIN cumo_float2bfloat(-3.38953138925153547590e+38f)
|
|
22
|
+
#define DATA_MAX cumo_float2bfloat(3.38953138925153547590e+38f)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#ifndef CUMO_BFLOAT_KERNEL_H
|
|
2
|
+
#define CUMO_BFLOAT_KERNEL_H
|
|
3
|
+
|
|
4
|
+
#include "bf16_def_kernel.h"
|
|
5
|
+
|
|
6
|
+
typedef cumo_bfloat dtype;
|
|
7
|
+
typedef cumo_bfloat rtype;
|
|
8
|
+
|
|
9
|
+
#include "bf16_macro_kernel.h"
|
|
10
|
+
|
|
11
|
+
#define m_nearly_eq(x,y) (fabsf(cumo_bfloat2float(x)-cumo_bfloat2float(y))<=(fabsf(cumo_bfloat2float(x))+fabsf(cumo_bfloat2float(y)))*CUMO_BFLOAT_EPSILON*2)
|
|
12
|
+
|
|
13
|
+
#define DATA_MIN cumo_float2bfloat(-3.38953138925153547590e+38f)
|
|
14
|
+
#define DATA_MAX cumo_float2bfloat(3.38953138925153547590e+38f)
|
|
15
|
+
|
|
16
|
+
#endif // CUMO_BFLOAT_KERNEL_H
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#include "half_def.h"
|
|
2
|
+
#include "bf16_def.h"
|
|
2
3
|
|
|
3
4
|
typedef CUMO_BIT_DIGIT dtype;
|
|
4
5
|
typedef CUMO_BIT_DIGIT rtype;
|
|
@@ -14,6 +15,7 @@ typedef CUMO_BIT_DIGIT rtype;
|
|
|
14
15
|
#define m_from_double(x) (((x)==0) ? 0 : 1)
|
|
15
16
|
#define m_from_real(x) (((x)==0) ? 0 : 1)
|
|
16
17
|
#define m_from_half(x) m_from_real(cumo_half2float(x))
|
|
18
|
+
#define m_from_bfloat(x) m_from_real(cumo_bfloat2float(x))
|
|
17
19
|
#define m_from_sint(x) (((x)==0) ? 0 : 1)
|
|
18
20
|
#define m_from_int32(x) (((x)==0) ? 0 : 1)
|
|
19
21
|
#define m_from_int64(x) (((x)==0) ? 0 : 1)
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#define CUMO_BIT_KERNEL_H
|
|
3
3
|
|
|
4
4
|
#include "half_def_kernel.h"
|
|
5
|
+
#include "bf16_def_kernel.h"
|
|
5
6
|
|
|
6
7
|
typedef CUMO_BIT_DIGIT dtype;
|
|
7
8
|
typedef CUMO_BIT_DIGIT rtype;
|
|
@@ -15,6 +16,7 @@ typedef CUMO_BIT_DIGIT rtype;
|
|
|
15
16
|
#define m_from_double(x) (((x)==0) ? 0 : 1)
|
|
16
17
|
#define m_from_real(x) (((x)==0) ? 0 : 1)
|
|
17
18
|
#define m_from_half(x) m_from_real(cumo_half2float(x))
|
|
19
|
+
#define m_from_bfloat(x) m_from_real(cumo_bfloat2float(x))
|
|
18
20
|
#define m_from_sint(x) (((x)==0) ? 0 : 1)
|
|
19
21
|
#define m_from_int32(x) (((x)==0) ? 0 : 1)
|
|
20
22
|
#define m_from_int64(x) (((x)==0) ? 0 : 1)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#include "float_def.h"
|
|
2
2
|
#include "half_def.h"
|
|
3
|
+
#include "bf16_def.h"
|
|
3
4
|
|
|
4
5
|
extern double round(double);
|
|
5
6
|
extern double log2(double);
|
|
@@ -45,6 +46,7 @@ static inline dtype c_from_dcomplex(cumo_dcomplex x) {
|
|
|
45
46
|
#define m_from_double(x) c_new(x,0)
|
|
46
47
|
#define m_from_real(x) c_new(x,0)
|
|
47
48
|
#define m_from_half(x) m_from_real(cumo_half2float(x))
|
|
49
|
+
#define m_from_bfloat(x) m_from_real(cumo_bfloat2float(x))
|
|
48
50
|
#define m_from_sint(x) c_new(x,0)
|
|
49
51
|
#define m_from_int32(x) c_new(x,0)
|
|
50
52
|
#define m_from_int64(x) c_new(x,0)
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
#include "float_def_kernel.h"
|
|
5
5
|
#include "half_def_kernel.h"
|
|
6
|
+
#include "bf16_def_kernel.h"
|
|
6
7
|
|
|
7
8
|
extern double round(double);
|
|
8
9
|
extern double log2(double);
|
|
@@ -48,6 +49,7 @@ __host__ __device__ static inline dtype c_from_dcomplex(cumo_dcomplex x) {
|
|
|
48
49
|
#define m_from_double(x) c_new(x,0)
|
|
49
50
|
#define m_from_real(x) c_new(x,0)
|
|
50
51
|
#define m_from_half(x) m_from_real(cumo_half2float(x))
|
|
52
|
+
#define m_from_bfloat(x) m_from_real(cumo_bfloat2float(x))
|
|
51
53
|
#define m_from_sint(x) c_new(x,0)
|
|
52
54
|
#define m_from_int32(x) c_new(x,0)
|
|
53
55
|
#define m_from_int64(x) c_new(x,0)
|
|
@@ -32,3 +32,11 @@
|
|
|
32
32
|
#ifndef M_LN10
|
|
33
33
|
#define M_LN10 2.30258509299404568402 /* log_e 10 */
|
|
34
34
|
#endif
|
|
35
|
+
|
|
36
|
+
/* The constants the two gelu forms are written with, spelled once on each side
|
|
37
|
+
so the macro headers cannot drift a digit apart. */
|
|
38
|
+
#ifndef CUMO_M_SQRT1_2
|
|
39
|
+
#define CUMO_M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
|
|
40
|
+
#define CUMO_M_SQRT_2_OVER_PI 0.79788456080286535588 /* sqrt(2/pi) */
|
|
41
|
+
#define CUMO_GELU_TANH_CUBIC 0.044715 /* fitted, not a closed form */
|
|
42
|
+
#endif
|
|
@@ -36,4 +36,12 @@
|
|
|
36
36
|
#define M_LN10 2.30258509299404568402 /* log_e 10 */
|
|
37
37
|
#endif
|
|
38
38
|
|
|
39
|
+
/* The constants the two gelu forms are written with, spelled once on each side
|
|
40
|
+
so the macro headers cannot drift a digit apart. */
|
|
41
|
+
#ifndef CUMO_M_SQRT1_2
|
|
42
|
+
#define CUMO_M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
|
|
43
|
+
#define CUMO_M_SQRT_2_OVER_PI 0.79788456080286535588 /* sqrt(2/pi) */
|
|
44
|
+
#define CUMO_GELU_TANH_CUBIC 0.044715 /* fitted, not a closed form */
|
|
45
|
+
#endif
|
|
46
|
+
|
|
39
47
|
#endif // CUMO_FLOAT_DEF_KERNEL_H
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#include "float_def.h"
|
|
2
2
|
#include "half_def.h"
|
|
3
|
+
#include "bf16_def.h"
|
|
3
4
|
|
|
4
5
|
extern double round(double);
|
|
5
6
|
extern double log2(double);
|
|
@@ -19,6 +20,7 @@ extern double pow(double, double);
|
|
|
19
20
|
#define m_from_double(x) (x)
|
|
20
21
|
#define m_from_real(x) (x)
|
|
21
22
|
#define m_from_half(x) m_from_real(cumo_half2float(x))
|
|
23
|
+
#define m_from_bfloat(x) m_from_real(cumo_bfloat2float(x))
|
|
22
24
|
#define m_from_sint(x) (x)
|
|
23
25
|
#define m_from_int32(x) (x)
|
|
24
26
|
#define m_from_int64(x) (x)
|
|
@@ -79,23 +81,6 @@ static inline dtype m_floored_mod(dtype x, dtype y) {
|
|
|
79
81
|
|
|
80
82
|
#define m_sprintf(s,x) sprintf(s,"%g",x)
|
|
81
83
|
|
|
82
|
-
#define cmp_prnan(a,b) \
|
|
83
|
-
((qsort_cast(a)==qsort_cast(b)) ? 0 : \
|
|
84
|
-
(qsort_cast(a) > qsort_cast(b)) ? 1 : -1)
|
|
85
|
-
|
|
86
|
-
#define cmp_ignan(a,b) \
|
|
87
|
-
(m_isnan(qsort_cast(a)) ? (m_isnan(qsort_cast(b)) ? 0 : 1) : \
|
|
88
|
-
(m_isnan(qsort_cast(b)) ? -1 : \
|
|
89
|
-
((qsort_cast(a)==qsort_cast(b)) ? 0 : \
|
|
90
|
-
(qsort_cast(a) > qsort_cast(b)) ? 1 : -1)))
|
|
91
|
-
|
|
92
|
-
#define cmpgt_prnan(a,b) \
|
|
93
|
-
(qsort_cast(a) > qsort_cast(b))
|
|
94
|
-
|
|
95
|
-
#define cmpgt_ignan(a,b) \
|
|
96
|
-
((m_isnan(qsort_cast(a)) && !m_isnan(qsort_cast(b))) || \
|
|
97
|
-
(qsort_cast(a) > qsort_cast(b)))
|
|
98
|
-
|
|
99
84
|
#define m_sqrt(x) sqrt(x)
|
|
100
85
|
#define m_cbrt(x) cbrt(x)
|
|
101
86
|
#define m_log(x) log(x)
|
|
@@ -129,6 +114,8 @@ static inline dtype m_floored_mod(dtype x, dtype y) {
|
|
|
129
114
|
|
|
130
115
|
#define m_erf(x) erf(x)
|
|
131
116
|
#define m_erfc(x) erfc(x)
|
|
117
|
+
#define m_gelu(x) cumo_gelu(x)
|
|
118
|
+
#define m_gelu_tanh(x) cumo_gelu_tanh(x)
|
|
132
119
|
#define m_ldexp(x,y) ldexp(x,y)
|
|
133
120
|
#define m_frexp(x,exp) frexp(x,exp)
|
|
134
121
|
|
|
@@ -209,3 +196,18 @@ static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
|
|
|
209
196
|
}
|
|
210
197
|
|
|
211
198
|
#include "real_accum.h"
|
|
199
|
+
|
|
200
|
+
// Typed by the dtype so that a float array reaches erff and tanhf. A helper
|
|
201
|
+
// taking double instead widens every element and runs the fp64 routines, which
|
|
202
|
+
// this card answers an order of magnitude slower than the fp32 ones.
|
|
203
|
+
static inline dtype cumo_gelu(dtype x)
|
|
204
|
+
{
|
|
205
|
+
return (dtype)0.5 * x * ((dtype)1 + erf(x * (dtype)CUMO_M_SQRT1_2));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// The approximation GPT-2 and the transformers after it were trained with.
|
|
209
|
+
static inline dtype cumo_gelu_tanh(dtype x)
|
|
210
|
+
{
|
|
211
|
+
return (dtype)0.5 * x * ((dtype)1 + tanh((dtype)CUMO_M_SQRT_2_OVER_PI *
|
|
212
|
+
(x + (dtype)CUMO_GELU_TANH_CUBIC * x * x * x)));
|
|
213
|
+
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
#include "float_def_kernel.h"
|
|
5
5
|
#include "half_def_kernel.h"
|
|
6
|
+
#include "bf16_def_kernel.h"
|
|
6
7
|
|
|
7
8
|
extern double round(double);
|
|
8
9
|
extern double log2(double);
|
|
@@ -22,6 +23,7 @@ extern double pow(double, double);
|
|
|
22
23
|
#define m_from_double(x) (x)
|
|
23
24
|
#define m_from_real(x) (x)
|
|
24
25
|
#define m_from_half(x) m_from_real(cumo_half2float(x))
|
|
26
|
+
#define m_from_bfloat(x) m_from_real(cumo_bfloat2float(x))
|
|
25
27
|
#define m_from_sint(x) (x)
|
|
26
28
|
#define m_from_int32(x) (x)
|
|
27
29
|
#define m_from_int64(x) (x)
|
|
@@ -82,25 +84,6 @@ __host__ __device__ static inline dtype m_floored_mod(dtype x, dtype y) {
|
|
|
82
84
|
|
|
83
85
|
#define m_sprintf(s,x) sprintf(s,"%g",x)
|
|
84
86
|
|
|
85
|
-
/*
|
|
86
|
-
#define cmp_prnan(a,b) \
|
|
87
|
-
((qsort_cast(a)==qsort_cast(b)) ? 0 : \
|
|
88
|
-
(qsort_cast(a) > qsort_cast(b)) ? 1 : -1)
|
|
89
|
-
|
|
90
|
-
#define cmp_ignan(a,b) \
|
|
91
|
-
(m_isnan(qsort_cast(a)) ? (m_isnan(qsort_cast(b)) ? 0 : 1) : \
|
|
92
|
-
(m_isnan(qsort_cast(b)) ? -1 : \
|
|
93
|
-
((qsort_cast(a)==qsort_cast(b)) ? 0 : \
|
|
94
|
-
(qsort_cast(a) > qsort_cast(b)) ? 1 : -1)))
|
|
95
|
-
|
|
96
|
-
#define cmpgt_prnan(a,b) \
|
|
97
|
-
(qsort_cast(a) > qsort_cast(b))
|
|
98
|
-
|
|
99
|
-
#define cmpgt_ignan(a,b) \
|
|
100
|
-
((m_isnan(qsort_cast(a)) && !m_isnan(qsort_cast(b))) || \
|
|
101
|
-
(qsort_cast(a) > qsort_cast(b)))
|
|
102
|
-
*/
|
|
103
|
-
|
|
104
87
|
#define m_sqrt(x) sqrt(x)
|
|
105
88
|
#define m_cbrt(x) cbrt(x)
|
|
106
89
|
#define m_log(x) log(x)
|
|
@@ -134,6 +117,8 @@ __host__ __device__ static inline dtype m_floored_mod(dtype x, dtype y) {
|
|
|
134
117
|
|
|
135
118
|
#define m_erf(x) erf(x)
|
|
136
119
|
#define m_erfc(x) erfc(x)
|
|
120
|
+
#define m_gelu(x) cumo_gelu(x)
|
|
121
|
+
#define m_gelu_tanh(x) cumo_gelu_tanh(x)
|
|
137
122
|
#define m_ldexp(x,y) ldexp(x,y)
|
|
138
123
|
#define m_frexp(x,exp) frexp(x,exp)
|
|
139
124
|
|
|
@@ -172,4 +157,19 @@ __host__ __device__ static inline dtype f_seq(dtype x, dtype y, double c)
|
|
|
172
157
|
|
|
173
158
|
#include "real_accum_kernel.h"
|
|
174
159
|
|
|
160
|
+
// Typed by the dtype so that a float array reaches erff and tanhf. A helper
|
|
161
|
+
// taking double instead widens every element and runs the fp64 routines, which
|
|
162
|
+
// this card answers an order of magnitude slower than the fp32 ones.
|
|
163
|
+
__host__ __device__ static inline dtype cumo_gelu(dtype x)
|
|
164
|
+
{
|
|
165
|
+
return (dtype)0.5 * x * ((dtype)1 + erf(x * (dtype)CUMO_M_SQRT1_2));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// The approximation GPT-2 and the transformers after it were trained with.
|
|
169
|
+
__host__ __device__ static inline dtype cumo_gelu_tanh(dtype x)
|
|
170
|
+
{
|
|
171
|
+
return (dtype)0.5 * x * ((dtype)1 + tanh((dtype)CUMO_M_SQRT_2_OVER_PI *
|
|
172
|
+
(x + (dtype)CUMO_GELU_TANH_CUBIC * x * x * x)));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
175
|
#endif // CUMO_FLOAT_MACRO_KERNEL_H
|