-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbfloat16_math.go
More file actions
196 lines (183 loc) · 4.05 KB
/
bfloat16_math.go
File metadata and controls
196 lines (183 loc) · 4.05 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package float16
import (
"math"
)
// Mathematical functions for BFloat16
// BFloat16Sqrt returns the square root of the BFloat16 value.
func BFloat16Sqrt(b BFloat16) BFloat16 {
if b.IsZero() {
return b
}
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16PositiveInfinity
}
if b.Signbit() {
return BFloat16QuietNaN
}
result := math.Sqrt(float64(b.ToFloat32()))
return BFloat16FromFloat32(float32(result))
}
// BFloat16Exp returns e^b.
func BFloat16Exp(b BFloat16) BFloat16 {
if b.IsZero() {
return BFloat16One
}
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16PositiveInfinity
}
if b.IsInf(-1) {
return BFloat16PositiveZero
}
result := math.Exp(float64(b.ToFloat32()))
return BFloat16FromFloat32(float32(result))
}
// BFloat16Log returns the natural logarithm of b.
func BFloat16Log(b BFloat16) BFloat16 {
if b.IsZero() {
return BFloat16NegativeInfinity
}
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16PositiveInfinity
}
if b.Signbit() {
return BFloat16QuietNaN
}
result := math.Log(float64(b.ToFloat32()))
return BFloat16FromFloat32(float32(result))
}
// BFloat16Log2 returns the base-2 logarithm of b.
func BFloat16Log2(b BFloat16) BFloat16 {
if b.IsZero() {
return BFloat16NegativeInfinity
}
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16PositiveInfinity
}
if b.Signbit() {
return BFloat16QuietNaN
}
result := math.Log2(float64(b.ToFloat32()))
return BFloat16FromFloat32(float32(result))
}
// BFloat16Sin returns the sine of b (in radians).
func BFloat16Sin(b BFloat16) BFloat16 {
if b.IsZero() {
return b
}
if b.IsNaN() || b.IsInf(0) {
return BFloat16QuietNaN
}
result := math.Sin(float64(b.ToFloat32()))
return BFloat16FromFloat32(float32(result))
}
// BFloat16Cos returns the cosine of b (in radians).
func BFloat16Cos(b BFloat16) BFloat16 {
if b.IsZero() {
return BFloat16One
}
if b.IsNaN() || b.IsInf(0) {
return BFloat16QuietNaN
}
result := math.Cos(float64(b.ToFloat32()))
return BFloat16FromFloat32(float32(result))
}
// BFloat16Tanh returns the hyperbolic tangent of b.
func BFloat16Tanh(b BFloat16) BFloat16 {
if b.IsZero() {
return b
}
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16One
}
if b.IsInf(-1) {
return BFloat16FromFloat32(-1)
}
result := math.Tanh(float64(b.ToFloat32()))
return BFloat16FromFloat32(float32(result))
}
// BFloat16Sigmoid returns 1 / (1 + exp(-b)).
func BFloat16Sigmoid(b BFloat16) BFloat16 {
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16One
}
if b.IsInf(-1) {
return BFloat16PositiveZero
}
x := float64(b.ToFloat32())
result := 1.0 / (1.0 + math.Exp(-x))
return BFloat16FromFloat32(float32(result))
}
// FastMode variants using polynomial approximations.
// These trade accuracy for speed, suitable for ML inference workloads
// where BFloat16 precision is already limited.
// BFloat16FastSigmoid computes an approximate sigmoid using a rational polynomial.
// Uses the approximation: sigmoid(x) ≈ 0.5 + 0.5 * x / (1 + |x|)
// which avoids exp() entirely.
func BFloat16FastSigmoid(b BFloat16) BFloat16 {
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16One
}
if b.IsInf(-1) {
return BFloat16PositiveZero
}
x := float64(b.ToFloat32())
abs := x
if abs < 0 {
abs = -abs
}
result := 0.5 + 0.5*x/(1.0+abs)
return BFloat16FromFloat32(float32(result))
}
// BFloat16FastTanh computes an approximate tanh using a rational polynomial.
// Uses the approximation: tanh(x) ≈ x*(27 + x*x) / (27 + 9*x*x)
// which is a Padé approximant accurate to within ~0.004 for |x| < 3.
func BFloat16FastTanh(b BFloat16) BFloat16 {
if b.IsZero() {
return b
}
if b.IsNaN() {
return b
}
if b.IsInf(1) {
return BFloat16One
}
if b.IsInf(-1) {
return BFloat16FromFloat32(-1)
}
x := float64(b.ToFloat32())
abs := x
if abs < 0 {
abs = -abs
}
// Clamp for large values where tanh saturates
if abs > 4.0 {
if x > 0 {
return BFloat16One
}
return BFloat16FromFloat32(-1)
}
x2 := x * x
result := x * (27.0 + x2) / (27.0 + 9.0*x2)
return BFloat16FromFloat32(float32(result))
}