-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_data.go
56 lines (49 loc) · 1.3 KB
/
model_data.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
package menoh
import (
"errors"
"github.com/pfnet-research/go-menoh/external"
)
// ModelData is a wrap of menoh.ModelData.
type ModelData struct {
external.ModelData
}
// NewRawModelData return empty ModelData to be setup outer.
func NewRawModelData() (*ModelData, error) {
m, err := external.MakeModelData()
if err != nil {
return nil, err
}
return &ModelData{*m}, nil
}
// NewModelDataFromPath returns ModelData using ONNX file placed on the path.
func NewModelDataFromPath(path string) (*ModelData, error) {
m, err := external.MakeModelDataFromONNX(path)
if err != nil {
return nil, err
}
return &ModelData{*m}, nil
}
// NewModelDataFromBytes return ModelData from binary data.
func NewModelDataFromBytes(data []byte) (*ModelData, error) {
if len(data) == 0 {
return nil, errors.New("ONNX file data is empty")
}
m, err := external.MakeModelDataFromONNXBytes(data)
if err != nil {
return nil, err
}
return &ModelData{*m}, nil
}
// AddTensorParameter adds tensor to named parameter.
func (m *ModelData) AddTensorParameter(name string, param Tensor) error {
menohDtype, err := toMenohDtype(param.dtype())
if err != nil {
return err
}
variable := external.Variable{
Dtype: menohDtype,
Dims: param.Shape(),
BufferHandle: param.ptr(),
}
return m.AddParameter(name, variable)
}