-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
attributes.go
67 lines (64 loc) · 1.79 KB
/
attributes.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
package onnx
import (
"github.com/owulveryck/onnx-go/internal/onnx/ir"
)
func toOperationAttributes(attrs []*ir.AttributeProto) (map[string]interface{}, error) {
output := make(map[string]interface{}, len(attrs))
for _, attr := range attrs {
o, err := toOperationAttribute(attr)
if err != nil {
return nil, err
}
output[attr.GetName()] = o
}
return output, nil
}
func toOperationAttribute(attr *ir.AttributeProto) (interface{}, error) {
switch attr.GetType() {
case ir.AttributeProto_UNDEFINED:
return struct{}{}, nil
case ir.AttributeProto_FLOAT:
return attr.GetF(), nil
case ir.AttributeProto_INT:
return attr.GetI(), nil
case ir.AttributeProto_STRING:
return string(attr.GetS()), nil
case ir.AttributeProto_TENSOR:
return attr.GetT().Tensor()
case ir.AttributeProto_GRAPH:
return nil, &ErrNotImplemented{
AttributeName: attr.GetName(),
AttributeValue: attr,
Message: "ir.AttributeProto_GRAPH not handled yet",
}
case ir.AttributeProto_FLOATS:
return attr.GetFloats(), nil
case ir.AttributeProto_INTS:
return attr.GetInts(), nil
case ir.AttributeProto_STRINGS:
s := attr.GetStrings()
strings := make([]string, len(s))
for i := 0; i < len(s); i++ {
strings[i] = string(s[i])
}
return strings, nil
case ir.AttributeProto_TENSORS:
return nil, &ErrNotImplemented{
AttributeName: attr.GetName(),
AttributeValue: attr,
Message: "ir.AttributeProto_TENSORS not handled yet",
}
case ir.AttributeProto_GRAPHS:
return nil, &ErrNotImplemented{
AttributeName: attr.GetName(),
AttributeValue: attr,
Message: "ir.AttributeProto_GRAPHS not handled yet",
}
default:
return nil, &ErrNotImplemented{
AttributeName: attr.GetName(),
AttributeValue: attr,
Message: "undefined attributeproto type",
}
}
}