-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp16.go
75 lines (64 loc) · 1.6 KB
/
fp16.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
package gotensor
import (
"github.com/chewxy/math32"
"github.com/lwch/gomath/internal/half"
)
func (*GoTensor) FP16Dot(x, w []uint16) uint16 {
dx := make([]float32, len(x))
dw := make([]float32, len(w))
for i := range x {
dx[i] = half.Decode(x[i])
dw[i] = half.Decode(w[i])
}
return half.Encode(dotVector(dx, dw, int64(len(x))))
}
func (*GoTensor) FP16Mul(x, w, y []uint16) {
for i := range x {
y[i] = half.Encode(half.Decode(x[i]) * half.Decode(w[i]))
}
}
func (*GoTensor) FP16ScalarMul(x uint16, w, y []uint16) {
dx := half.Decode(x)
for i := range w {
y[i] = half.Encode(dx * half.Decode(w[i]))
}
}
func (*GoTensor) FP16Div(x, w, y []uint16) {
for i := range x {
y[i] = half.Encode(half.Decode(x[i]) / half.Decode(w[i]))
}
}
func (*GoTensor) FP16ScalarDiv(x uint16, w, y []uint16) {
dx := half.Decode(x)
for i := range w {
y[i] = half.Encode(dx / half.Decode(w[i]))
}
}
func (*GoTensor) FP16Add(x, w, y []uint16) {
for i := range x {
y[i] = half.Encode(half.Decode(x[i]) + half.Decode(w[i]))
}
}
func (*GoTensor) FP16ScalarAdd(x uint16, w, y []uint16) {
dx := half.Decode(x)
for i := range w {
y[i] = half.Encode(dx + half.Decode(w[i]))
}
}
func (*GoTensor) FP16Sub(x, w, y []uint16) {
for i := range x {
y[i] = half.Encode(half.Decode(x[i]) - half.Decode(w[i]))
}
}
func (*GoTensor) FP16ScalarSub(x uint16, w, y []uint16) {
dx := half.Decode(x)
for i := range w {
y[i] = half.Encode(dx - half.Decode(w[i]))
}
}
func (*GoTensor) FP16Pow(x []uint16, n uint16, y []uint16) {
dn := half.Decode(n)
for i := range x {
y[i] = half.Encode(math32.Pow(half.Decode(x[i]), dn))
}
}