-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mlops_torch.mojo
More file actions
136 lines (99 loc) · 3.81 KB
/
test_mlops_torch.mojo
File metadata and controls
136 lines (99 loc) · 3.81 KB
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
from random import rand
from testing import assert_equal
from test_tensorutils import assert_tensors_equal
from python.python import Python
import qryptum.nn as nn
from qryptum import Tensor, TensorShape
from qryptum import Graph, Symbol, OP
from qryptum.autograd.attributes import Attribute, AttributeVector
from qryptum.utils.tensorutils import fill
from qryptum.autograd.ops.ops import backward_op
from test_utils_extras import to_numpy, to_tensor
alias dtype = DType.float32
alias nelts: Int = simdwidthof[dtype]()
# ------ Test Unary Ops ------
@value
struct torch_output_unary_op:
var expected: Tensor[dtype]
var grad_1: Tensor[dtype]
fn torch_unary_op(op: OP, input_1: Tensor, upper_grad: Tensor) -> torch_output_unary_op:
try:
var torch = Python.import_module("torch")
var np = Python.import_module("numpy")
var input_1 = torch.from_numpy(to_numpy(input_1)).requires_grad_(True)
var expected: PythonObject
if op == OP.SIGMOID:
expected = torch.sigmoid(input_1)
elif op == OP.RELU:
expected = torch.relu(input_1)
elif op == OP.TANH:
expected = torch.tanh(input_1)
else:
print("Error: op not supported (returning the value input_1): ", op)
expected = input_1
# uppergrad & backwards
var upper_grad = torch.from_numpy(to_numpy(upper_grad))
_ = expected.backward(upper_grad)
return torch_output_unary_op(
to_tensor(expected.detach().numpy()),
to_tensor(input_1.grad.numpy()),
)
except:
print("Error importing torch")
var d = Tensor[dtype](1)
return torch_output_unary_op(d, d)
fn test_unary_op[
op: OP, t1_shape: TensorShape
](t1: Tensor[dtype], expected: Tensor[dtype]) raises:
fn create_graph() -> Graph:
var g = Graph()
var t1 = g.input(t1_shape)
var res = g.op(op, t1)
g.out(res)
return g ^
alias graph = create_graph()
assert_equal(len(graph.nodes), 1)
var model = nn.Model[graph](inference_only=True)
var res = model.inference(t1)[0]
assert_tensors_equal(res, expected, "almost")
fn test_unary_op_backward[
op: OP, t1_shape: TensorShape, ug_shape: TensorShape
](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, AttributeVector()](ug, t1, grad_1)
assert_tensors_equal(grad_1, grad_1_expected, "almost")
fn test_SIGMOID() raises:
alias t1_shape = TensorShape(37, 63, 107)
alias ug_shape = TensorShape(37, 63, 107)
var t1: Tensor[dtype] = Tensor[dtype](t1_shape)
rand(t1.data(), t1.num_elements())
var ug = Tensor[dtype](ug_shape)
rand(ug.data(), ug.num_elements())
var expected_and_grad = torch_unary_op(OP.SIGMOID, t1, ug)
fn test_RELU() raises:
alias t1_shape = TensorShape(37, 63, 107)
alias ug_shape = TensorShape(37, 63, 107)
var t1: Tensor[dtype] = Tensor[dtype](t1_shape)
rand(t1.data(), t1.num_elements())
var ug = Tensor[dtype](ug_shape)
rand(ug.data(), ug.num_elements())
var expected_and_grad = torch_unary_op(OP.RELU, t1, ug)
fn test_TANH() raises:
alias t1_shape = TensorShape(37, 63, 107)
alias ug_shape = TensorShape(37, 63, 107)
var t1: Tensor[dtype] = Tensor[dtype](t1_shape)
rand(t1.data(), t1.num_elements())
var ug = Tensor[dtype](ug_shape)
rand(ug.data(), ug.num_elements())
var expected_and_grad = torch_unary_op(OP.TANH, t1, ug)
fn main():
print("Running mlops (compare with torch) tests")
try:
test_SIGMOID()
test_RELU()
test_TANH()
except e:
print("[ERROR] Error in mlops (compare with torch)")
print(e)
return
print("Finished mlops (compare with torch) tests")