This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
forked from oramasearch/onnx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy_backend_test.go
161 lines (132 loc) · 3.39 KB
/
dummy_backend_test.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package onnx
import (
"fmt"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/encoding"
"gonum.org/v1/gonum/graph/simple"
"gorgonia.org/tensor"
)
// testBackend is a simple directed graph
type testBackend struct {
g *simple.WeightedDirectedGraph
}
// Node of the graph
type nodeTest struct {
id int64
name string
description string
value tensor.Tensor
opType string
//attributes []*ir.Attribute
}
func (n *nodeTest) SetTensor(t tensor.Tensor) error {
n.value = t
return nil
}
func (n *nodeTest) GetTensor() tensor.Tensor {
return n.value
}
// ID to fulfil the graph.Node interface
func (n *nodeTest) ID() int64 {
return n.id
}
// Attributes it a method to fulfil the encoding/dot package
func (n *nodeTest) Attributes() []encoding.Attribute {
var value string
value = fmt.Sprintf(`%v`, n.name)
if n.opType != "" {
value = fmt.Sprintf(`%v|{Op|%v}`, value, n.opType)
}
if n.value != nil {
value = fmt.Sprintf(`%v|{shape|%v}|{type|%v}`, value, n.value.Shape(), n.value.Dtype())
}
value = fmt.Sprintf(`"%v"`, value)
return []encoding.Attribute{
{
Key: "shape",
Value: "Mrecord",
},
{
Key: "label",
Value: value,
},
}
}
// SetName to fulfil the Namer interface
func (n *nodeTest) SetName(desc string) {
n.name = desc
}
// GetName to fulfil the Namer interface
func (n *nodeTest) GetName() string {
return n.name
}
// SetDescription to fulfil the Namer interface
func (n *nodeTest) SetDescription(desc string) {
n.description = desc
}
// GetDescription to fulfil the Namer interface
func (n *nodeTest) GetDescription() string {
return n.description
}
// ApplyTensor to fulfil the TensorCarrier interface
func (n *nodeTest) ApplyTensor(t tensor.Tensor) error {
n.value = t
return nil
}
// NewSimpleGraph ...
func newTestBackend() *testBackend {
return &testBackend{
g: simple.NewWeightedDirectedGraph(self, -1),
}
}
func (g *testBackend) SetWeightedEdge(e graph.WeightedEdge) {
g.g.SetWeightedEdge(e)
}
func (g *testBackend) NewWeightedEdge(from, to graph.Node, w float64) graph.WeightedEdge {
return g.g.NewWeightedEdge(from, to, w)
}
func (g *testBackend) AddNode(n graph.Node) {
g.g.AddNode(n)
}
func (g *testBackend) NewNode() graph.Node {
n := g.g.NewNode()
return &nodeTest{
id: n.ID(),
}
}
func (g *testBackend) Node(id int64) graph.Node {
return g.g.Node(id)
}
func (g *testBackend) Nodes() graph.Nodes {
return g.g.Nodes()
}
func (g *testBackend) From(id int64) graph.Nodes {
return g.g.From(id)
}
func (g *testBackend) HasEdgeBetween(xid, yid int64) bool {
return g.g.HasEdgeBetween(xid, yid)
}
func (g *testBackend) Edges() graph.Edges {
return g.g.Edges()
}
func (g *testBackend) Edge(uid, vid int64) graph.Edge {
return g.g.Edge(uid, vid)
}
func (g *testBackend) HasEdgeFromTo(uid, vid int64) bool {
return g.g.HasEdgeFromTo(uid, vid)
}
func (g *testBackend) To(id int64) graph.Nodes {
return g.g.To(id)
}
func (g *testBackend) ApplyOperation(_ Operation, _ ...graph.Node) error {
return nil
}
// WeightedEdge returns the weighted edge from u to v if such an edge exists and nil otherwise.
// The node v must be directly reachable from u as defined by the From method.
func (g *testBackend) WeightedEdge(uid, vid int64) graph.WeightedEdge {
return g.g.WeightedEdge(uid, vid)
}
// WeightedEdges returns all the weighted edges in the graph.
func (g *testBackend) WeightedEdges() graph.WeightedEdges {
return g.g.WeightedEdges()
}