-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6618445
commit 336a2ef
Showing
14 changed files
with
191 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import torch | ||
import torch.utils.benchmark as benchmark | ||
import torch_dipu | ||
from itertools import product | ||
|
||
x = torch.randn(10000, 64).cuda() | ||
|
||
def batched_dot_mul_sum(a, b): | ||
'''Computes batched dot by multiplying and summing''' | ||
return a.mul(b).sum(-1) | ||
|
||
t0 = benchmark.Timer( | ||
stmt='batched_dot_mul_sum(x, x)', | ||
setup='from __main__ import batched_dot_mul_sum', | ||
globals={'x': x}) | ||
r0 = t0.timeit(100) | ||
print(r0) | ||
assert r0.mean < 8.8e-5 | ||
|
||
|
||
def batched_dot_bmm(a, b): | ||
'''Computes batched dot by reducing to ``bmm``''' | ||
a = a.reshape(-1, 1, a.shape[-1]) | ||
b = b.reshape(-1, b.shape[-1], 1) | ||
return torch.bmm(a, b).flatten(-3) | ||
|
||
t1 = benchmark.Timer( | ||
stmt='batched_dot_bmm(x, x)', | ||
setup='from __main__ import batched_dot_bmm', | ||
globals={'x': x}) | ||
|
||
r1 = t1.timeit(100) | ||
print(r1) | ||
assert r1.mean < 8.5e-5 | ||
|
||
|
||
# Compare takes a list of measurements which we'll save in results. | ||
results = [] | ||
sizes = [1, 64, 32, 120] | ||
for b, n in product(sizes, sizes): | ||
# label and sub_label are the rows | ||
# description is the column | ||
label = 'Batched dot' | ||
sub_label = f'[{b}, {n}]' | ||
x = torch.ones((b, n)) | ||
for num_threads in [1, 4, 16, 32]: | ||
results.append(benchmark.Timer( | ||
stmt='batched_dot_mul_sum(x, x)', | ||
setup='from __main__ import batched_dot_mul_sum', | ||
globals={'x': x}, | ||
num_threads=num_threads, | ||
label=label, | ||
sub_label=sub_label, | ||
description='mul/sum', | ||
).blocked_autorange(min_run_time=1)) | ||
results.append(benchmark.Timer( | ||
stmt='batched_dot_bmm(x, x)', | ||
setup='from __main__ import batched_dot_bmm', | ||
globals={'x': x}, | ||
num_threads=num_threads, | ||
label=label, | ||
sub_label=sub_label, | ||
description='bmm', | ||
).blocked_autorange(min_run_time=1)) | ||
|
||
compare = benchmark.Compare(results) | ||
compare.print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.