This repository was archived by the owner on Mar 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtesting_utils.mojo
201 lines (164 loc) · 5.9 KB
/
testing_utils.mojo
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from python.python import Python
from collections import OptionalReg
from testing import assert_equal, assert_almost_equal
from basalt import dtype
from basalt.autograd import Graph, OP
from basalt.autograd.ops.ops import backward_op
from basalt.autograd.attributes import AttributeVector
from basalt.nn import Tensor, TensorShape, Model
from basalt.utils.tensor_creation_utils import to_numpy, to_tensor
# The below regex should be used to convert deprecated calls
# assert_tensors_equal\(([^,]+),\s*([^,]+),\s*"([^"]+)"\)
# assert_tensors_equal["$3"]($1, $2)
fn assert_tensors_equal[
mode: String = "exact", msg: String = "Error"
](t1: Tensor[dtype], t2: Tensor[dtype]) raises:
constrained[
mode == "exact" or mode == "almost", "Mode must be either 'exact' or 'almost'"
]()
assert_equal(t1.shape(), t2.shape(), "Tensor shape mismatch")
for i in range(t1.num_elements()):
if mode == "almost":
assert_almost_equal(t1[i], t2[i], rtol=1e-5, atol=1e-5, msg=msg)
else:
assert_equal(t1[i], t2[i], msg=msg)
fn test_unary_op[
op: OP, t1_shape: TensorShape, attrs: OptionalReg[AttributeVector] = None
](t1: Tensor[dtype], expected: Tensor[dtype]) raises:
fn create_graph() -> Graph:
var g = Graph()
var t1 = g.input(t1_shape)
if attrs:
var res = g.op(op, t1, attributes=attrs.value())
g.out(res)
return g ^
else:
var res = g.op(op, t1)
g.out(res)
return g ^
alias graph = create_graph()
assert_equal(len(graph.nodes), 1)
var model = Model[graph](inference_only=True)
var res = model.inference(t1)[0]
assert_tensors_equal["almost"](res, expected)
fn test_binary_op[
op: OP,
t1_shape: TensorShape,
t2_shape: TensorShape,
attrs: OptionalReg[AttributeVector] = None,
](t1: Tensor[dtype], t2: Tensor[dtype], expected: Tensor[dtype]) raises:
fn create_graph() -> Graph:
var g = Graph()
var t1 = g.input(t1_shape)
var t2 = g.input(t2_shape)
if attrs:
var res = g.op(op, t1, t2, attributes=attrs.value())
g.out(res)
return g ^
else:
var res = g.op(op, t1, t2)
g.out(res)
return g ^
alias graph = create_graph()
assert_equal(len(graph.nodes), 1)
var model = Model[graph](inference_only=True)
var res = model.inference(t1, t2)[0]
assert_tensors_equal["almost"](res, expected)
fn test_ternary_op[
op: OP, t1_shape: TensorShape, t2_shape: TensorShape, t3_shape: TensorShape
](
t1: Tensor[dtype], t2: Tensor[dtype], t3: Tensor[dtype], expected: Tensor[dtype]
) raises:
@parameter
fn create_graph() -> Graph:
var g = Graph()
var t1 = g.input(t1_shape)
var t2 = g.input(t2_shape)
var t3 = g.input(t3_shape)
var res = g.op(op, t1, t2, t3)
g.out(res)
return g ^
alias graph = create_graph()
assert_equal(len(graph.nodes), 1)
var model = Model[graph](inference_only=True)
var res = model.inference(t1, t2, t3)[0]
assert_tensors_equal["almost"](res, expected)
fn test_unary_op_backward[
op: OP,
t1_shape: TensorShape,
ug_shape: TensorShape,
attrs: AttributeVector = AttributeVector(),
](t1: Tensor[dtype], ug: Tensor[dtype], grad_1_expected: Tensor[dtype],) raises:
var grad_1 = Tensor[dtype](t1_shape)
backward_op[0, op, ug_shape, t1_shape, attrs](ug, t1, grad_1)
assert_tensors_equal["almost"](grad_1, grad_1_expected)
fn test_binary_op_backward[
op: OP,
t1_shape: TensorShape,
t2_shape: TensorShape,
ug_shape: TensorShape,
attrs: AttributeVector = AttributeVector(),
](
t1: Tensor[dtype],
t2: Tensor[dtype],
ug: Tensor[dtype],
grad_1_expected: Tensor[dtype],
grad_2_expected: Tensor[dtype],
) raises:
var grad_1 = Tensor[dtype](t1_shape)
backward_op[0, op, ug_shape, t1_shape, t2_shape, attrs](ug, t1, t2, grad_1)
assert_tensors_equal["almost"](grad_1, grad_1_expected)
var grad_2 = Tensor[dtype](t2_shape)
backward_op[1, op, ug_shape, t1_shape, t2_shape, attrs](ug, t1, t2, grad_2)
assert_tensors_equal["almost"](grad_2, grad_2_expected)
fn test_ternary_op_backward[
op: OP,
t1_shape: TensorShape,
t2_shape: TensorShape,
t3_shape: TensorShape,
ug_shape: TensorShape,
attrs: AttributeVector = AttributeVector(),
](
t1: Tensor[dtype],
t2: Tensor[dtype],
t3: Tensor[dtype],
ug: Tensor[dtype],
grad_1_expected: Tensor[dtype],
grad_2_expected: Tensor[dtype],
grad_3_expected: Tensor[dtype],
) raises:
var grad_1 = Tensor[dtype](t1_shape)
backward_op[0, op, ug_shape, t1_shape, t2_shape, t3_shape, attrs](
ug, t1, t2, t3, grad_1
)
assert_tensors_equal["almost"](grad_1, grad_1_expected)
var grad_2 = Tensor[dtype](t2_shape)
backward_op[1, op, ug_shape, t1_shape, t2_shape, t3_shape, attrs](
ug, t1, t2, t3, grad_2
)
assert_tensors_equal["almost"](grad_2, grad_2_expected)
var grad_3 = Tensor[dtype](t3_shape)
backward_op[2, op, ug_shape, t1_shape, t2_shape, t3_shape, attrs](
ug, t1, t2, t3, grad_3
)
assert_tensors_equal["almost"](grad_3, grad_3_expected)
fn create_graph_concat(
t1_shape: TensorShape, t2_shape: TensorShape, t3_shape: TensorShape, dim: Int
) -> Graph:
# Testing with 3 operands
var g = Graph()
var t1 = g.input(t1_shape, trainable=True)
var t2 = g.input(t2_shape, trainable=True)
var t3 = g.input(t3_shape, trainable=True)
var res = g.concat(t1, t2, t3, dim=dim)
g.out(res)
g.loss(res)
return g ^
fn create_graph_split(t_shape: TensorShape, sections: List[Int], dim: Int) -> Graph:
var g = Graph()
var t = g.input(t_shape, trainable=True)
var results = g.split(t, sections=sections, dim=dim)
for i in range(len(sections)):
g.out(results[i])
g.loss(results[0]) # Any one
return g ^