-
Notifications
You must be signed in to change notification settings - Fork 11
/
predict.go
135 lines (113 loc) · 3.26 KB
/
predict.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
package gomxnet
//#cgo CXXFLAGS: -std=c++11 -I/usr/local/Cellar/openblas/0.2.14_1/include
//#cgo LDFLAGS: -L /usr/local/Cellar/openblas/0.2.14_1/lib/ -lopenblas
////// #cgo LDFLAGS: /Users/jack/Source/go/src/github.com/jdeng/gomxnet/mxnet.a -lstdc++
//#include <stdlib.h>
//#include "mxnet.h"
import "C"
import "unsafe"
import "fmt"
const (
CPU = iota + 1
GPU
CPU_PINNED
)
type Predictor struct {
handle C.PredictorHandle
outputSize uint32
}
type Model struct {
Symbol []byte // json
Params []byte // network
}
type Device struct {
Type int
Id int
}
type InputNode struct {
Key string
Shape []uint32
}
func NewPredictor(model Model, dev Device, input []InputNode) (*Predictor, error) {
shapeInd := []uint32{0}
shapeData := []uint32{}
var b *C.char
keys := C.malloc(C.size_t(len(input)) * C.size_t(unsafe.Sizeof(b)))
defer C.free(unsafe.Pointer(keys))
for i := 0; i < len(input); i++ {
element := (**C.char)(unsafe.Pointer(uintptr(keys) + uintptr(i)*unsafe.Sizeof(b)))
*element = C.CString(input[i].Key)
shapeInd = append(shapeInd, uint32(len(input[i].Shape)))
shapeData = append(shapeData, input[i].Shape...)
}
var handle C.PredictorHandle
n, err := C.MXPredCreate((*C.char)(unsafe.Pointer(&model.Symbol[0])), (*C.char)(unsafe.Pointer(&model.Params[0])), C.size_t(len(model.Params)), C.int(dev.Type), C.int(dev.Id), C.mx_uint(len(input)), (**C.char)(keys), (*C.mx_uint)(&shapeInd[0]), (*C.mx_uint)(&shapeData[0]), &handle)
for i := 0; i < len(input); i++ {
element := (**C.char)(unsafe.Pointer(uintptr(keys) + uintptr(i)*unsafe.Sizeof(b)))
C.free(unsafe.Pointer(*element))
}
if err != nil {
return nil, err
} else if n < 0 {
return nil, GetLastError()
}
return &Predictor{handle, 0}, nil
}
func (p *Predictor) Free() {
if p.handle != nil {
C.MXPredFree(p.handle)
p.handle = nil
}
}
func (p *Predictor) Forward(key string, data []float32) error {
if data != nil {
k := C.CString(key)
defer C.free(unsafe.Pointer(k))
if n, err := C.MXPredSetInput(p.handle, k, (*C.mx_float)(&data[0]), C.mx_uint(len(data))); err != nil {
return err
} else if n < 0 {
return GetLastError()
}
}
if n, err := C.MXPredForward(p.handle); err != nil {
return err
} else if n < 0 {
return GetLastError()
}
return nil
}
func (p *Predictor) GetOutput(index uint32) ([]float32, error) {
if p.outputSize == 0 {
var shapeData *C.mx_uint
var shapeDim C.mx_uint
if n, err := C.MXPredGetOutputShape(p.handle, C.mx_uint(index), (**C.mx_uint)(&shapeData), (*C.mx_uint)(&shapeDim)); err != nil {
return nil, err
} else if n < 0 {
return nil, GetLastError()
}
var size uint32 = 1
for i := 0; i < int(shapeDim); i++ {
n := *(*C.mx_uint)(unsafe.Pointer(uintptr(unsafe.Pointer(shapeData)) + uintptr(i)*unsafe.Sizeof(size)))
size *= uint32(n)
}
p.outputSize = size
}
size := p.outputSize
data := make([]C.mx_float, size)
if n, err := C.MXPredGetOutput(p.handle, C.mx_uint(index), (*C.mx_float)(&data[0]), C.mx_uint(size)); err != nil {
return nil, err
} else if n < 0 {
return nil, GetLastError()
}
out := make([]float32, size)
for i := 0; i < int(size); i++ {
out[i] = float32(data[i])
}
return out, nil
}
func GetLastError() error {
if err := C.MXGetLastError(); err != nil {
return fmt.Errorf(C.GoString(err))
}
return nil
}