-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_test.go
153 lines (140 loc) · 3.68 KB
/
math_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
package arboreal_test
import (
"github.com/viterin/vek/vek32"
"testing"
math "github.com/chewxy/math32"
"github.com/stillmatic/arboreal"
"github.com/stretchr/testify/require"
)
func sigmoid(x float32) float32 {
return 1.0 / (1.0 + math.Exp(-x))
}
func softmax(ys []float32) []float32 {
output := make([]float32, len(ys))
var sum float32
for i, y := range ys {
exp := math.Exp(y)
sum += exp
output[i] = exp
}
if sum != 0.0 {
for i := range output {
output[i] /= sum
}
}
return output
}
func softmaxSimd(vector []float32) []float32 {
r := make([]float32, len(vector))
vek32.Exp_Into(r, vector)
sum := vek32.Sum(r)
if sum != float32(0.0) {
inverseSum := float32(1.0) / sum
vek32.MulNumber_Inplace(r, inverseSum)
}
return r
}
func softmaxSimdInplace(vector []float32) []float32 {
vek32.Exp_Inplace(vector)
sum := vek32.Sum(vector)
if sum != float32(0.0) {
inverseSum := float32(1.0) / sum
vek32.MulNumber_Inplace(vector, inverseSum)
}
return vector
}
func softmaxAlt(vector []float32) []float32 {
sum := float32(0.0)
r := make([]float32, len(vector))
for i, v := range vector {
exp := math.Exp(v)
r[i] = exp
sum += exp
}
if sum != float32(0.0) {
inverseSum := float32(1.0) / sum
for i := range r {
r[i] *= inverseSum
}
}
return r
}
type sigmoidTable struct {
expTable []float32
expTableSize int
maxExp float32
cache float32
}
func newSigmoidTable() *sigmoidTable {
s := new(sigmoidTable)
s.expTableSize = 1024
s.maxExp = 6.0
s.cache = float32(s.expTableSize) / s.maxExp / 2.0
s.expTable = make([]float32, s.expTableSize)
for i := 0; i < s.expTableSize; i++ {
expval := math.Exp((float32(i)/float32(s.expTableSize)*2. - 1.) * s.maxExp)
s.expTable[i] = expval / (expval + 1.)
}
return s
}
// sigmoid returns: f(x) = (x + max_exp) * (exp_table_size / max_exp / 2)
// If you set x to over |max_exp|, it raises index out of range error.
func (s *sigmoidTable) sigmoid(x float32) float32 {
if x < -s.maxExp {
return 0.0
} else if x > s.maxExp {
return 1.0
}
return s.expTable[int((x+s.maxExp)*s.cache)]
}
// inplace SIMD saves an alloc and a couple nanoseconds but it's not a big difference.
// BenchmarkSoftmax/softmax-10 10690101 107.1 ns/op 48 B/op 1 allocs/op
// BenchmarkSoftmax/softmaxAlt-10 11185256 107.5 ns/op 48 B/op 1 allocs/op
// BenchmarkSoftmax/SIMD-10 10492280 113.2 ns/op 48 B/op 1 allocs/op
// BenchmarkSoftmax/SIMD_Inplace-10 11581250 103.2 ns/op 0 B/op 0 allocs/op
func BenchmarkSoftmax(b *testing.B) {
vector := []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
b.ResetTimer()
b.Run("softmax", func(b *testing.B) {
for i := 0; i < b.N; i++ {
arboreal.Softmax(vector)
}
})
b.Run("softmaxAlt", func(b *testing.B) {
for i := 0; i < b.N; i++ {
softmaxAlt(vector)
}
})
b.Run("SIMD", func(b *testing.B) {
for i := 0; i < b.N; i++ {
softmaxSimd(vector)
}
})
b.Run("SIMD_Inplace", func(b *testing.B) {
for i := 0; i < b.N; i++ {
softmaxSimdInplace(vector)
}
})
}
func BenchmarkSigmoid(b *testing.B) {
vector := []float32{-7.0, -1.0, -0.5, 0.0, 0.5, 1.0, 7.0}
b.Run("sigmoid", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = sigmoid(vector[i%len(vector)])
// require.NotEqual(b, res, 0)
}
})
b.Run("lookup table", func(b *testing.B) {
st := newSigmoidTable()
for i := 0; i < b.N; i++ {
_ = st.sigmoid(vector[i%len(vector)])
}
})
}
func TestSigmoid(t *testing.T) {
vector := []float32{-7.0, -1.0, -0.5, 0.0, 0.5, 1.0, 7.0}
st := newSigmoidTable()
for _, v := range vector {
require.InDelta(t, st.sigmoid(v), sigmoid(v), 0.002)
}
}