-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbfloat16_test.go
175 lines (150 loc) · 4.5 KB
/
bfloat16_test.go
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
package dtype
import (
"fmt"
"testing"
)
func TestNotTruncatedDecimalPart(t *testing.T) {
// See: https://en.wikipedia.org/wiki/Floating-point_arithmetic
// See: https://cloud.google.com/tpu/docs/bfloat16
//Case 1
expected := float32(1.)
actualBF16 := BFloat16fromFloat32(1.)
actual := actualBF16.Float32()
if actual != expected {
t.Errorf("Expected %g, but got %g", expected, actual)
}
//Case 2
expected = float32(6.25)
actualBF16 = BFloat16fromFloat32(6.25)
actual = actualBF16.Float32()
if actual != expected {
t.Errorf("Expected %g, but got %g", expected, actual)
}
}
func TestTruncatedDecimalPart(t *testing.T) {
// See: https://en.wikipedia.org/wiki/Floating-point_arithmetic
// See: https://cloud.google.com/tpu/docs/bfloat16
// Note that, this an expected behaviour.
//Case 1
expected := float32(1.5234375)
actualBF16 := BFloat16fromFloat32(1.53)
actual := actualBF16.Float32()
if actual != expected {
t.Errorf("Expected %g, but got %g", expected, actual)
}
//Case 2
expected = float32(6.5)
actualBF16 = BFloat16fromFloat32(6.53)
actual = actualBF16.Float32()
if actual != expected {
t.Errorf("Expected %g, but got %g", expected, actual)
}
//Case 3
expected = float32(11.3125)
actualBF16 = BFloat16fromFloat32(11.34)
actual = actualBF16.Float32()
if actual != expected {
t.Errorf("Expected %g, but got %g", expected, actual)
}
//Case 3
expected = float32(584)
actualBF16 = BFloat16fromFloat32(586.25)
actual = actualBF16.Float32()
if actual != expected {
t.Errorf("Expected %g, but got %g", expected, actual)
}
}
func TestReadBFloat16LittleEndian(t *testing.T) {
// Values are stored in little endian order
testCases := []map[string]interface{}{
{
"input": []byte{0xA5, 0x35}, //0.0000012293458
"expectedUInt16Bits": uint16(0x35A5),
"expectedF32": float32(0.0000012293458),
},
{
"input": []byte{0xF4, 0xB5}, //-0.00000181794167
"expectedUInt16Bits": uint16(0xB5F4),
"expectedF32": float32(-0.00000181794167),
},
{
"input": []byte{0x92, 0xB6}, //-0.000004351139
"expectedUInt16Bits": uint16(0xB692),
"expectedF32": float32(-0.000004351139),
},
}
for _, testCase := range testCases {
input := testCase["input"].([]byte)
expectedUInt16Bits := testCase["expectedUInt16Bits"].(uint16)
expectedF32 := testCase["expectedF32"].(float32)
actualBF16 := ReadBFloat16LittleEndian(input)
actualUInt16Bits := uint16(actualBF16)
if actualUInt16Bits != expectedUInt16Bits {
t.Errorf("Expected uint16 0x%X, but got 0x%X", expectedUInt16Bits, actualUInt16Bits)
}
actualF32 := actualBF16.Float32()
if actualF32 != expectedF32 {
t.Errorf("Expected float32 %g, but got %g", expectedF32, actualF32)
}
}
}
func TestBFloat16ToString(t *testing.T) {
// Values are stored in little endian order
testCases := []map[string]interface{}{
{
"input": []byte{0xA5, 0x35}, //0.0000012293458
"expectedF": "0.0000012293458",
},
{
"input": []byte{0xF4, 0xB5}, //-0.00000181794167
"expectedF": "-0.0000018179417",
},
{
"input": []byte{0x92, 0xB6}, //-0.000004351139
"expectedF": "-0.000004351139",
},
}
for _, testCase := range testCases {
input := testCase["input"].([]byte)
expected := testCase["expectedF"].(string)
actualBF16 := ReadBFloat16LittleEndian(input)
actual := actualBF16.String()
if actual != expected {
t.Errorf("Expected %s, but got %s", expected, actual)
}
}
}
func TestBFloat16ToFmtString(t *testing.T) {
// Values are stored in little endian order
testCases := []map[string]interface{}{
{
"input": []byte{0xA5, 0x35}, //0.0000012293458
"expectedF": "0.000001",
"expectedG": "1.2293458e-06",
},
{
"input": []byte{0xF4, 0xB5}, //-0.00000181794167
"expectedF": "-0.000002",
"expectedG": "-1.8179417e-06",
},
{
"input": []byte{0x92, 0xB6}, //-0.000004351139
"expectedF": "-0.000004",
"expectedG": "-4.351139e-06",
},
}
for _, testCase := range testCases {
input := testCase["input"].([]byte)
expectedF := testCase["expectedF"].(string)
expectedG := testCase["expectedG"].(string)
actualBF16 := ReadBFloat16LittleEndian(input)
actualF := fmt.Sprintf("%f", actualBF16.Float32())
if actualF != expectedF {
t.Errorf("Expected for %%s: %s, but got %s for input 0x%X", expectedF, actualF, input)
}
actualG := fmt.Sprintf("%g", actualBF16.Float32())
if actualG != expectedG {
t.Errorf("Expected for %%g: %s, but got %s for input 0x%X", expectedG, actualG, input)
}
}
}