-
Notifications
You must be signed in to change notification settings - Fork 21
/
MyFirstOP.py
70 lines (60 loc) · 1.71 KB
/
MyFirstOP.py
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
import sys
sys.path.append('../') # Add MobulaOP path
import mobula
@mobula.op.register
class MyFirstOP:
def forward(self, x, y):
return x + y
def backward(self, dy):
return [dy, dy]
def infer_shape(self, in_shape):
assert in_shape[0] == in_shape[1]
return in_shape, [in_shape[0]]
try:
import mxnet as mx
print('MXNet:')
print('mx.nd.NDArray:')
a = mx.nd.array([1, 2, 3])
b = mx.nd.array([4, 5, 6])
c = MyFirstOP(a, b)
print('a + b = c\n{} + {} = {}\n'.format(a.asnumpy(),
b.asnumpy(), c.asnumpy())) # [5, 7, 9]
if hasattr(mx, 'numpy'):
# MXNet NumPy-compatible API
print('mx.np.ndarray:')
a = mx.np.array([1, 2, 3])
b = mx.np.array([4, 5, 6])
c = MyFirstOP(a, b)
print('a + b = c\n{} + {} = {}\n'.format(a.asnumpy(),
b.asnumpy(), c.asnumpy())) # [5, 7, 9]
except ImportError:
pass
try:
import numpy as np
print('NumPy:')
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
op = MyFirstOP[np.ndarray]()
c = op(a, b)
print('a + b = c\n{} + {} = {}\n'.format(a, b, c)) # [5, 7, 9]
except ImportError:
pass
try:
import torch
print('PyTorch:')
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = MyFirstOP(a, b)
print('a + b = c\n{} + {} = {}\n'.format(a, b, c)) # [5, 7, 9]
except ImportError:
pass
try:
import cupy as cp
print('CuPy:')
a = cp.array([1, 2, 3])
b = cp.array([4, 5, 6])
op = MyFirstOP[cp.ndarray]()
c = op(a, b)
print('a + b = c\n{} + {} = {}\n'.format(a, b, c)) # [5, 7, 9]
except ImportError:
pass