Skip to content

Commit 7a485f8

Browse files
authored
compile e2e benchmarks (#163)
1 parent 997b6f2 commit 7a485f8

File tree

13 files changed

+884
-49
lines changed

13 files changed

+884
-49
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e2e/smole/*mp4 binary

e2e/mojo_mandelbrot/compiled.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import numpy as np # otherwise @compile is ImportError
2+
import torch._dynamo.config as cfg
3+
import torch
4+
5+
cfg.numpy_ndarray_as_tensor = True
6+
device="cpu"
7+
8+
9+
def init():
10+
# Define the boundaries of the complex plane
11+
xn = 450
12+
yn = 375
13+
xmin = -2.25
14+
xmax = 0.75
15+
ymin = -1.25
16+
ymax = 1.25
17+
18+
# Create the grid of complex numbers
19+
x_values = np.linspace(xmin, xmax, xn, dtype='float32')
20+
y_values = np.linspace(ymin, ymax, yn, dtype='float32')
21+
22+
rx, iy = np.meshgrid(x_values, y_values, indexing='xy')
23+
24+
return rx, iy
25+
26+
27+
def simulate(rx, iy, step, max_iter=200):
28+
x = rx.copy()
29+
y = iy.copy()
30+
mask = np.zeros_like(x)
31+
for _ in range(max_iter):
32+
mask = step(x, y, rx, iy, mask)
33+
return mask
34+
35+
36+
def step(x, y, rx, iy, mask):
37+
x_prev = x
38+
y_prev = y
39+
x = x_prev**2 - y_prev**2 + rx
40+
y = 2*x_prev*y_prev + iy
41+
inside = np.sqrt(x**2 + y**2) <= 2 # sqrt is from the OP
42+
mask += inside
43+
return mask
44+
45+
46+
47+
rx, iy = init()
48+
mask = simulate(rx, iy, step, max_iter=3)
49+
50+
print(np.count_nonzero(mask))
51+
52+
53+
################ benchmark #####################
54+
import time
55+
56+
# ### numpy ###
57+
58+
59+
rx, iy = init()
60+
start_time = time.time()
61+
mask = simulate(rx, iy, step, max_iter=1000)
62+
end_time = time.time()
63+
numpy_time = end_time - start_time
64+
print("\n\nnumpy: elapsed=", numpy_time)
65+
66+
67+
# ### compile ###
68+
69+
step_c = torch.compile(step)
70+
71+
# ### warm up ###
72+
rx, iy = init()
73+
for _ in range(50):
74+
simulate(rx, iy, step_c, max_iter=3)
75+
76+
77+
rx, iy = init()
78+
start_time = time.time()
79+
mask = simulate(rx, iy, step_c, max_iter=1000)
80+
end_time = time.time()
81+
82+
83+
compiled_time = end_time - start_time
84+
print("compiled: elapsed=", compiled_time, ' speedup = ', numpy_time / compiled_time)
85+

e2e/nn_from_scratch/eager/nn.py

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# from https://www.geeksforgeeks.org/implementation-of-neural-network-from-scratch-using-numpy/
2+
3+
4+
import numpy as _np
5+
import torch_np as np
6+
7+
# To run on CUDA, change "cpu" to "cuda" below.
8+
import torch
9+
torch.set_default_device("cpu")
10+
11+
12+
# Creating data set
13+
14+
# A
15+
a =[0, 0, 1, 1, 0, 0,
16+
0, 1, 0, 0, 1, 0,
17+
1, 1, 1, 1, 1, 1,
18+
1, 0, 0, 0, 0, 1,
19+
1, 0, 0, 0, 0, 1]
20+
# B
21+
b =[0, 1, 1, 1, 1, 0,
22+
0, 1, 0, 0, 1, 0,
23+
0, 1, 1, 1, 1, 0,
24+
0, 1, 0, 0, 1, 0,
25+
0, 1, 1, 1, 1, 0]
26+
# C
27+
c =[0, 1, 1, 1, 1, 0,
28+
0, 1, 0, 0, 0, 0,
29+
0, 1, 0, 0, 0, 0,
30+
0, 1, 0, 0, 0, 0,
31+
0, 1, 1, 1, 1, 0]
32+
33+
# Creating labels
34+
y =[[1, 0, 0],
35+
[0, 1, 0],
36+
[0, 0, 1]]
37+
38+
39+
# converting data and labels into numpy array
40+
41+
"""
42+
Convert the matrix of 0 and 1 into one hot vector
43+
so that we can directly feed it to the neural network,
44+
these vectors are then stored in a list x.
45+
"""
46+
47+
x =[np.array(a).reshape(1, 30), np.array(b).reshape(1, 30),
48+
np.array(c).reshape(1, 30)]
49+
50+
51+
# Labels are also converted into NumPy array
52+
y = np.array(y)
53+
54+
55+
print(x, "\n\n", y)
56+
57+
58+
# activation function
59+
60+
def sigmoid(x):
61+
return(1/(1 + np.exp(-x)))
62+
63+
# Creating the Feed forward neural network
64+
# 1 Input layer(1, 30)
65+
# 1 hidden layer (1, 5)
66+
# 1 output layer(3, 3)
67+
68+
def f_forward(x, w1, w2):
69+
# hidden
70+
z1 = x.dot(w1)# input from layer 1
71+
a1 = sigmoid(z1)# out put of layer 2
72+
73+
# Output layer
74+
z2 = a1.dot(w2)# input of out layer
75+
a2 = sigmoid(z2)# output of out layer
76+
return(a2)
77+
78+
# initializing the weights randomly
79+
def generate_wt(x, y):
80+
81+
_np.random.seed(1234)
82+
83+
l =[]
84+
for i in range(x * y):
85+
l.append(_np.random.randn())
86+
return(np.array(l).reshape(x, y))
87+
88+
# for loss we will be using mean square error(MSE)
89+
def loss(out, Y):
90+
s =(np.square(out-Y))
91+
s = np.sum(s)/len(y)
92+
return(s)
93+
94+
# Back propagation of error
95+
def back_prop(x, y, w1, w2, alpha):
96+
97+
# hidden layer
98+
z1 = x.dot(w1)# input from layer 1
99+
a1 = sigmoid(z1)# output of layer 2
100+
101+
# Output layer
102+
z2 = a1.dot(w2)# input of out layer
103+
a2 = sigmoid(z2)# output of out layer
104+
# error in output layer
105+
d2 =(a2-y)
106+
d1 = np.multiply((w2.dot((d2.transpose()))).transpose(),
107+
(np.multiply(a1, 1-a1)))
108+
109+
# Gradient for w1 and w2
110+
w1_adj = x.transpose().dot(d1)
111+
w2_adj = a1.transpose().dot(d2)
112+
113+
# Updating parameters
114+
w1 = w1-(alpha*(w1_adj))
115+
w2 = w2-(alpha*(w2_adj))
116+
117+
return(w1, w2)
118+
119+
def train(x, Y, w1, w2, alpha = 0.01, epoch = 10):
120+
acc =[]
121+
losss =[]
122+
for j in range(epoch):
123+
l =[]
124+
for i in range(len(x)):
125+
out = f_forward(x[i], w1, w2)
126+
l.append((loss(out, Y[i])))
127+
w1, w2 = back_prop(x[i], y[i], w1, w2, alpha)
128+
print("epochs:", j + 1, "======== acc:", (1-(sum(l)/len(x)))*100)
129+
acc.append((1-(sum(l)/len(x)))*100)
130+
losss.append(sum(l)/len(x))
131+
return(acc, losss, w1, w2)
132+
133+
def predict(x, w1, w2):
134+
Out = f_forward(x, w1, w2)
135+
maxm = 0
136+
k = 0
137+
for i in range(len(Out[0])):
138+
if(maxm<Out[0][i]):
139+
maxm = Out[0][i]
140+
k = i
141+
if(k == 0):
142+
print("Image is of letter A.")
143+
elif(k == 1):
144+
print("Image is of letter B.")
145+
else:
146+
print("Image is of letter C.")
147+
# plt.imshow(x.reshape(5, 6))
148+
# plt.show()
149+
150+
w1 = generate_wt(30, 5)
151+
w2 = generate_wt(5, 3)
152+
print(w1, "\n\n", w2)
153+
154+
155+
"""The arguments of train function are data set list x,
156+
correct labels y, weights w1, w2, learning rate = 0.1,
157+
no of epochs or iteration.The function will return the
158+
matrix of accuracy and loss and also the matrix of
159+
trained weights w1, w2"""
160+
161+
acc, losss, w1, w2 = train(x, y, w1, w2, 0.1, 100)
162+
163+
164+
"""
165+
The predict function will take the following arguments:
166+
1) image matrix
167+
2) w1 trained weights
168+
3) w2 trained weights
169+
"""
170+
predict(x[1], w1, w2)
171+

0 commit comments

Comments
 (0)