!2778 Modify Ascend profiling to improve ease of use

Merge pull request !2778 from hanjun/v1.11.0
This commit is contained in:
i-robot
2023-03-31 08:28:56 +00:00
committed by Gitee
+19 -13
View File
@@ -30,16 +30,20 @@ def Singleton(cls):
@Singleton
class Profile(object):
def __init__(self,
total_steps: int = 10,
start_step: int = 10,
save_path: str = "./npu_profiling",
profile_type: str = None,
use_npu = True,
use_e2e_profiler = True,
**kwargs):
r"""Dump TORCH/CANN/GE profiling data
Args:
total_steps: The step to dump profiling data
start_step: The step to dump profiling data
save_path: Save path of data
profile_type: The type of profile, Optional['TORCH', 'CANN', 'GE']
use_npu: Whether to output npu data, when torch profiling, Optional[True, False]
use_e2e_profiler: Whether to use e2e profile, when cann/ge profiling, Optional[True, False]
note:
When dumping GE profiling data, the configuration of 'GE_PROFILING_TO_STD_OUT'
@@ -54,25 +58,27 @@ class Profile(object):
self.save_path = save_path
self.profile_type = profile_type
self.enable = self.profile_type in ['TORCH', 'CANN', 'GE']
self.total_steps = total_steps
self.start_step = start_step
self.use_npu = use_npu
self.use_e2e_profiler = use_e2e_profiler
self.count = 0
if self.enable:
torch_args_set = set(["enabled", "use_cuda", "use_npu", "record_shapes", "with_flops", "profile_memory",
torch_args_set = set(["enabled", "use_cuda", "record_shapes", "with_flops", "profile_memory",
"with_stack", "use_kineto", "use_cpu", "use_npu_simple"])
cann_ge_args_set = set(["use_e2e_profiler", "config"])
cann_ge_args_set = set(["config"])
if self.profile_type == "TORCH":
if not set(kwargs.keys()).issubset(torch_args_set):
raise ValueError("the args '%s' is invaild." % (kwargs.keys()))
self.prof = torch.autograd.profiler.profile(**kwargs)
raise ValueError("Args '%s' invaild, expect args '%s' ." % (kwargs.keys(), torch_args_set))
self.prof = torch.autograd.profiler.profile(use_npu = self.use_npu, **kwargs)
elif self.profile_type == "CANN":
if not set(kwargs.keys()).issubset(cann_ge_args_set):
raise ValueError("the args '%s' is invaild." % (kwargs.keys()))
self.prof = torch.npu.profile(self.save_path, **kwargs)
raise ValueError("Args '%s' invaild, expect args '%s' ." % (kwargs.keys(), cann_ge_args_set))
self.prof = torch.npu.profile(self.save_path, self.use_e2e_profiler, **kwargs)
elif self.profile_type == "GE":
if not set(kwargs.keys()).issubset(cann_ge_args_set):
raise ValueError("the args '%s' is invaild." % (kwargs.keys()))
self.prof = torch.npu.profile(self.save_path, **kwargs)
raise ValueError("Args '%s' invaild, expect args '%s' ." % (kwargs.keys(), cann_ge_args_set))
self.prof = torch.npu.profile(self.save_path, self.use_e2e_profiler, **kwargs)
try:
os.makedirs(self.save_path, exist_ok=True)
@@ -90,7 +96,7 @@ class Profile(object):
if self.enable:
self.step_count = self.step_count + 1
if self.step_count == self.total_steps:
if self.step_count == self.start_step:
self.prof.__enter__()
def end(self):
@@ -98,7 +104,7 @@ class Profile(object):
raise ValueError("start interface must be called after end")
self.count = 0
if self.enable and self.step_count == self.total_steps:
if self.enable and self.step_count == self.start_step:
self.prof.__exit__(None, None, None)
if self.profile_type == "TORCH":
filename = "torch_prof_" + time.strftime("%Y%m%d%H%M%S") + ".json"