Skip to content

Commit

Permalink
Create numpy-linear-relu.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsjoberg committed Aug 8, 2023
1 parent 40755a4 commit 3227e11
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions python/compilers/numpy-linear-relu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np

dtype = "float32"

# numpy
a_np = np.random.rand(128, 128).astype(dtype)
b_np = np.random.rand(128, 128).astype(dtype)
# matmul -> relu
c_mm_relu = np.maximum(a_np @ b_np, 0)

# implementation
def mm_relu(A: np.ndarray, B: np.ndarray, C: np.ndarray):
# A, B, C: 128x128
Y = np.empty((128, 128), dtype=dtype)
# matmul
for i in range(128):
for j in range(128):
for k in range(128):
if k == 0: Y[i, j] = 0
Y[i, j] += A[i, k] * B[k, j]
# relu
for i in range(128):
for j in range(128):
C[i, j] = max(Y[i, j], 0)

# test
c_np = np.empty((128, 128), dtype=dtype)
mm_relu(a_np, b_np, c_np)
# check within tolerance
np.testing.assert_allclose(c_mm_relu, c_np, rtol=1e-5)

0 comments on commit 3227e11

Please sign in to comment.