Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mpact][benchmark] manual sum of squares benchmark #65

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions benchmark/python/manual/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Benchmarks run by hand

These benchmarks are not run as part of MPACT's regular testing or benchmarking.
To run an individual test, build the MPACT compiler, cd into this directory,
and then simply run a benchmark as follows:

```shell
python <benchmark-name>.py
```
59 changes: 59 additions & 0 deletions benchmark/python/manual/sum_of_sq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import torch
import numpy as np
import time

from mpact.mpactbackend import mpact_jit_compile, mpact_jit_run
from mpact_benchmark.utils.tensor_generator import generate_tensor


def runbench_eager(tag, sp, net, x, num_iters=1000):
net(x) # warmup
checksum = 0
start = time.time()
for i in range(num_iters):
res = net(x).item()
checksum = checksum + res
end = time.time()
time_ms = (end - start) * 1000 / num_iters
print("%s : %.2f : %8.4f ms. : checksum=%d" % (tag, sp, time_ms, checksum))


def runbench_mpact(tag, sp, net, x, num_iters=1000):
invoker, fn = mpact_jit_compile(net, x)
mpact_jit_run(invoker, fn, x) # warmup
checksum = 0
start = time.time()
for i in range(num_iters):
res = mpact_jit_run(invoker, fn, x)
checksum = checksum + res
end = time.time()
time_ms = (end - start) * 1000 / num_iters
print("%s : %.2f : %8.4f ms. : checksum=%d" % (tag, sp, time_ms, checksum))


class SqSumNet(torch.nn.Module):
def forward(self, x):
# TODO: make this work too: return (x ** 2).sum()
return (x * x).sum()


net = SqSumNet()
h = 1024 * 4
w = 1024 * 4

for d in range(0, 101, 10):
sparsity = 1.0 - (d / 100.0)
x = generate_tensor(
seed=0, shape=(h, w), sparsity=sparsity, dtype=np.float32, drange=(1.0, 1.0)
)

# Note, we don't have binary-valued sparse tensors in PyTorch
# so we are using csr. For now, we have to hack the
# "explicitVal=1.0:f32"
# into the MLIR sparse tensor type to make optimize it fully.
s = x.to_sparse_csr()

runbench_eager("PyTorch (dense) ", sparsity, net, x)
runbench_mpact("MPACT (dense) ", sparsity, net, x)
runbench_eager("PyTorch (sparse)", sparsity, net, s)
runbench_mpact("MPACT (sparse)", sparsity, net, s)
Loading