forked from snuspl/nimble
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intra-op parallel microbenchmarks for PT (#19997)
Summary: Pull Request resolved: pytorch/pytorch#19997 ghimport-source-id: 420d4a68a1ef879beee2734adba8abb575e0b0ab Differential Revision: D15231375 Pulled By: ilia-cher fbshipit-source-id: ce7248ea2ebb54d25c9d831c6e3f23f3534557dd
- Loading branch information
1 parent
481b6d0
commit 19e6886
Showing
4 changed files
with
115 additions
and
8 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
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,92 @@ | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
from operator_benchmark import benchmark_core, benchmark_runner | ||
from operator_benchmark.benchmark_test_generator import * | ||
|
||
import torch | ||
|
||
|
||
"""Microbenchmarks for PyTorch CPU intra-op parallelism. | ||
Tests the following functions: | ||
- bitor, cbitor | ||
- tensor-scalar and tensor-tensor element-wise function, integer-only | ||
- tahn and sigmoid | ||
- unary ops | ||
- sumall | ||
- basic reduction function | ||
""" | ||
|
||
# Config | ||
config = generate_configs( | ||
N=[128, 1024, 4096], | ||
M=[128, 1024, 4096], | ||
dtype=[torch.float32, torch.int32], | ||
contig=[True, False], | ||
mode=['short'], | ||
sample_func=cross_product | ||
) | ||
|
||
|
||
def torch_or(tensor_arg): | ||
jit_ior_loop_code = """\ | ||
def forward(self, a, b, iterations): | ||
# type: (Tensor, Tensor, int) | ||
for _ in range(iterations): | ||
a.__ior__({}) | ||
return a | ||
""" | ||
jit_ior_loop = torch.jit.ScriptModule() | ||
jit_ior_loop.define(jit_ior_loop_code.format("b" if tensor_arg else "42")) | ||
|
||
print("torch_or(", tensor_arg, "):\n", jit_ior_loop.code) | ||
return jit_ior_loop | ||
|
||
|
||
def torch_unary(op_str): | ||
jit_op_loop_code = """\ | ||
def forward(self, a, b, iterations): | ||
# type: (Tensor, Tensor, int) | ||
for _ in range(iterations): | ||
a.{}() | ||
return a | ||
""" | ||
jit_op_loop = torch.jit.ScriptModule() | ||
jit_op_loop.define(jit_op_loop_code.format(op_str)) | ||
|
||
print("torch_unary(", op_str, "):\n", jit_op_loop.code) | ||
return jit_op_loop | ||
|
||
|
||
@torch.jit.script | ||
def torch_sumall(a, b, iterations): | ||
# type: (Tensor, Tensor, int) | ||
result = 0.0 | ||
for _ in range(iterations): | ||
result += float(torch.sum(a)) | ||
a[0][0] += 0.01 | ||
return result | ||
|
||
print("torch_sumall:\n", torch_sumall.code) | ||
|
||
@benchmark_core.register_test | ||
def test_th_intraop(): | ||
generate_pt_test( | ||
[config], | ||
map_pt_config_intraop, | ||
[('bitor', torch_or(False)), | ||
('cbitor', torch_or(True)), | ||
('tanh', torch_unary('tanh_')), | ||
('sigmoid', torch_unary('sigmoid_')), | ||
('sumall', torch_sumall)] | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
benchmark_runner.main() |