This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operator.go
68 lines (63 loc) · 1.75 KB
/
operator.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
package gorgonnx
import (
"github.com/owulveryck/gorgonnx/operators"
onnx "github.com/owulveryck/onnx-go"
"gorgonia.org/gorgonia"
)
// AvailableOperators is the list of the onnx operators available linked to their implementation
var AvailableOperators = map[string]Operator{
"Conv": &operators.Conv{},
"Mul": &operators.Mul{},
"MatMul": &operators.Matmul{},
"Div": &operators.Div{},
"Add": &operators.Add{},
"Relu": &operators.Relu{},
"MaxPool": &operators.Maxpool{},
"Concat": &operators.Concat{},
"Constant": &operators.Constant{},
"Reshape": &operators.Reshape{},
"BatchNormalization": &operators.Batchnorm{},
"Unsqueeze": &operators.Unsqueeze{},
}
// Operator can be added to the computation graph
type Operator interface {
Init([]*onnx.AttributeProto) error
Apply(input ...*gorgonia.Node) ([]*gorgonia.Node, error)
}
func (cg *computationGraph) processNode(nx *onnx.NodeProto) error {
op, ok := AvailableOperators[*nx.OpType]
if !ok {
return ErrNotImplemented{
Operator: *nx.OpType,
}
}
err := op.Init(nx.Attribute)
if err != nil {
return err
}
inputs := make([]*gorgonia.Node, len(nx.Input))
for i := 0; i < len(nx.Input); i++ {
input, err := cg.loadNode(nx.Input[i])
if err != nil {
return err
}
inputs[i] = input
}
o, err := op.Apply(inputs...)
if err != nil {
return err
}
/*
if len(o) != len(nx.Output) {
return fmt.Errorf("Bad number of output for op %v, expected %v, got %v", op, len(nx.Output), len(o))
}
*/
for i := 0; i < len(nx.Output); i++ {
o[i].SetName(nx.Output[i])
err := cg.storeNode(nx.Output[i], o[i])
if err != nil {
return err
}
}
return nil
}