forked from Project-MONAI/MONAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyspy_profiling.py
More file actions
39 lines (30 loc) · 1.41 KB
/
pyspy_profiling.py
File metadata and controls
39 lines (30 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
To be used with py-spy, comparing torch.Tensor, SubTensor, SubWithTorchFunc, MetaTensor
Adapted from https://github.com/pytorch/pytorch/tree/v1.11.0/benchmarks/overrides_benchmark
"""
from __future__ import annotations
import argparse
import torch
Tensor = torch.Tensor
NUM_REPEATS = 1000000
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run the torch.add for a given class a given number of times.")
parser.add_argument("tensor_class", metavar="TensorClass", type=str, help="The class to benchmark.")
parser.add_argument("--nreps", "-n", type=int, default=NUM_REPEATS, help="The number of repeats.")
args = parser.parse_args()
TensorClass = globals()[args.tensor_class]
NUM_REPEATS = args.nreps
t1 = TensorClass(1)
t2 = TensorClass(2)
for _ in range(NUM_REPEATS):
torch.add(t1, t2)