# mypy: allow-untyped-defsimportgzipimportjsonimportosimportshutilimporttempfilefromabcimportABC,abstractmethodfromenumimportEnumfromfunctoolsimportpartialfromtypingimportAny,Callable,Dict,Iterable,List,Optional,Tuplefromtyping_extensionsimportSelffromwarningsimportwarnimporttorchimporttorch.autograd.profilerasproffromtorch._Cimport_get_privateuse1_backend_namefromtorch._C._profilerimport(_add_execution_trace_observer,_disable_execution_trace_observer,_enable_execution_trace_observer,_ExperimentalConfig,_remove_execution_trace_observer,)fromtorch.autogradimportkineto_available,ProfilerActivityfromtorch.profiler._memory_profilerimportMemoryProfile,MemoryProfileTimeline__all__=["supported_activities","ProfilerAction","schedule","tensorboard_trace_handler","profile","ExecutionTraceObserver",]PROFILER_STEP_NAME="ProfilerStep"class_NumpyEncoder(json.JSONEncoder):""" Json encoder for numpy types (np.int, np.float, np.array etc.) Returns default encoder if numpy is not available """defdefault(self,obj):"""Encode NumPy types to JSON"""try:importnumpyasnpexceptImportError:returnjson.JSONEncoder.default(self,obj)ifisinstance(obj,np.integer):returnint(obj)elifisinstance(obj,np.floating):returnfloat(obj)elifisinstance(obj,np.ndarray):returnobj.tolist()else:returnjson.JSONEncoder.default(self,obj)defsupported_activities():""" Returns a set of supported profiler tracing activities. Note: profiler uses CUPTI library to trace on-device CUDA kernels. In case when CUDA is enabled but CUPTI is not available, passing ``ProfilerActivity.CUDA`` to profiler results in using the legacy CUDA profiling code (same as in the legacy ``torch.autograd.profiler``). This, in turn, results in including CUDA time in the profiler table output, but not in the JSON trace. """returntorch.autograd._supported_activities()class_ITraceObserver(ABC):"""Abstract interface for a Trace observer. This satisfies 3 methods: start, stop and cleanup"""@abstractmethoddefstart(self):pass@abstractmethoddefstop(self):pass@abstractmethoddefcleanup(self):pass
[docs]class_KinetoProfile:"""Low-level profiler wrap the autograd profile Args: activities (iterable): list of activity groups (CPU, CUDA) to use in profiling, supported values: ``torch.profiler.ProfilerActivity.CPU``, ``torch.profiler.ProfilerActivity.CUDA``, ``torch.profiler.ProfilerActivity.XPU``. Default value: ProfilerActivity.CPU and (when available) ProfilerActivity.CUDA or (when available) ProfilerActivity.XPU. record_shapes (bool): save information about operator's input shapes. profile_memory (bool): track tensor memory allocation/deallocation (see ``export_memory_timeline`` for more details). with_stack (bool): record source information (file and line number) for the ops. with_flops (bool): use formula to estimate the FLOPS of specific operators (matrix multiplication and 2D convolution). with_modules (bool): record module hierarchy (including function names) corresponding to the callstack of the op. e.g. If module A's forward call's module B's forward which contains an aten::add op, then aten::add's module hierarchy is A.B Note that this support exist, at the moment, only for TorchScript models and not eager mode models. experimental_config (_ExperimentalConfig) : A set of experimental options used by profiler libraries like Kineto. Note, backward compatibility is not guaranteed. execution_trace_observer (ExecutionTraceObserver) : A PyTorch Execution Trace Observer object. `PyTorch Execution Traces <https://arxiv.org/pdf/2305.14516.pdf>`__ offer a graph based representation of AI/ML workloads and enable replay benchmarks, simulators, and emulators. When this argument is included the observer start() and stop() will be called for the same time window as PyTorch profiler. acc_events (bool): Enable the accumulation of FunctionEvents across multiple profiling cycles .. note:: This API is experimental and subject to change in the future. Enabling shape and stack tracing results in additional overhead. When record_shapes=True is specified, profiler will temporarily hold references to the tensors; that may further prevent certain optimizations that depend on the reference count and introduce extra tensor copies. """def__init__(self,*,activities:Optional[Iterable[ProfilerActivity]]=None,record_shapes:bool=False,profile_memory:bool=False,with_stack:bool=False,with_flops:bool=False,with_modules:bool=False,experimental_config:Optional[_ExperimentalConfig]=None,execution_trace_observer:Optional[_ITraceObserver]=None,acc_events:bool=False,custom_trace_id_callback:Optional[Callable[[],str]]=None,):self.activities=set(activities)ifactivitieselsesupported_activities()self.record_shapes=record_shapesself.with_flops=with_flopsself.profile_memory=profile_memoryself.with_stack=with_stackself.with_modules=with_modulesself.experimental_config=experimental_configself.execution_trace_observer=execution_trace_observerself.acc_events=acc_eventsself.custom_trace_id_callback=custom_trace_id_callbackself.profiler:Optional[prof.profile]=Noneself.mem_tl:Optional[MemoryProfileTimeline]=Noneself.use_device=NoneifProfilerActivity.CUDAinself.activities:self.use_device="cuda"elifProfilerActivity.XPUinself.activities:self.use_device="xpu"elifProfilerActivity.MTIAinself.activities:self.use_device="mtia"elifProfilerActivity.PrivateUse1inself.activities:self.use_device=_get_privateuse1_backend_name()# user-defined metadata to be amended to the traceself.preset_metadata:Dict[str,str]={}defstart(self):self.prepare_trace()self.start_trace()defstop(self):self.stop_trace()defprepare_trace(self):if(self.profilerisNone)or(notself.acc_events):self.profiler=prof.profile(use_cpu=(ProfilerActivity.CPUinself.activities),use_device=self.use_device,record_shapes=self.record_shapes,with_flops=self.with_flops,profile_memory=self.profile_memory,with_stack=self.with_stack,with_modules=self.with_modules,use_kineto=True,experimental_config=self.experimental_config,acc_events=self.acc_events,custom_trace_id_callback=self.custom_trace_id_callback,)self.profiler._prepare_trace()defstart_trace(self):ifself.execution_trace_observer:self.execution_trace_observer.start()assertself.profilerisnotNoneself.profiler._start_trace()ifself.profile_memory:self.add_metadata_json("profile_memory","1")ifself.with_stack:self.add_metadata_json("with_stack","1")ifself.record_shapes:self.add_metadata_json("record_shapes","1")ifself.with_modules:self.add_metadata_json("with_modules","1")ifself.with_flops:self.add_metadata_json("with_flops","1")ifkineto_available():dist_info=self._get_distributed_info()ifdist_info:self.add_metadata_json("distributedInfo",json.dumps(dist_info,cls=_NumpyEncoder))ifhasattr(torch,"_inductor"):importtorch._inductor.configasinductor_configifinductor_config.triton.cudagraphs:os.environ["DISABLE_CUPTI_LAZY_REINIT"]="1"self.add_metadata_json("DISABLE_CUPTI_LAZY_REINIT","1")# FIXME: CUDA Graph does not work well with CUPTI teardown.# 1) crashes on 1st lazy CUPTI re-init after teardown (CUDA 11)# 2) crashes on 2nd non-lazy CUPTI re-init after teardown (CUDA 12)# Workaround: turn off CUPTI teardown when using CUDA Graphs.os.environ["TEARDOWN_CUPTI"]="0"# Insert the preset user metadata to the tracefork,vinself.preset_metadata.items():self.add_metadata_json(k,v)defstop_trace(self):ifself.execution_trace_observer:self.execution_trace_observer.stop()assertself.profilerisnotNoneself.profiler.__exit__(None,None,None)
[docs]defexport_chrome_trace(self,path:str):""" Exports the collected trace in Chrome JSON format. If kineto is enabled, only last cycle in schedule is exported. """assertself.profilerifpath.endswith(".gz"):fp=tempfile.NamedTemporaryFile("w+b",suffix=".json",delete=False)fp.close()retvalue=self.profiler.export_chrome_trace(fp.name)withopen(fp.name,"rb")asfin:withgzip.open(path,"wb")asfout:fout.writelines(fin)os.remove(fp.name)returnretvalueelse:returnself.profiler.export_chrome_trace(path)
[docs]defexport_stacks(self,path:str,metric:str="self_cpu_time_total"):"""Save stack traces to a file Args: path (str): save stacks file to this location; metric (str): metric to use: "self_cpu_time_total" or "self_cuda_time_total" """assertself.profilerreturnself.profiler.export_stacks(path,metric)
[docs]deftoggle_collection_dynamic(self,enable:bool,activities:Iterable[ProfilerActivity]):"""Toggle collection of activities on/off at any point of collection. Currently supports toggling Torch Ops (CPU) and CUDA activity supported in Kineto Args: activities (iterable): list of activity groups to use in profiling, supported values: ``torch.profiler.ProfilerActivity.CPU``, ``torch.profiler.ProfilerActivity.CUDA`` Examples: .. code-block:: python with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ] ) as p: code_to_profile_0() // turn off collection of all CUDA activity p.toggle_collection_dynamic(False, [torch.profiler.ProfilerActivity.CUDA]) code_to_profile_1() // turn on collection of all CUDA activity p.toggle_collection_dynamic(True, [torch.profiler.ProfilerActivity.CUDA]) code_to_profile_2() print(p.key_averages().table( sort_by="self_cuda_time_total", row_limit=-1)) """ifnotself.profiler:returnself.profiler.toggle_collection_dynamic(enable,activities)
[docs]defkey_averages(self,group_by_input_shape:bool=False,group_by_stack_n:int=0):"""Averages events, grouping them by operator name and (optionally) input shapes and stack. .. note:: To use shape/stack functionality make sure to set record_shapes/with_stack when creating profiler context manager. """assertself.profilerreturnself.profiler.key_averages(group_by_input_shape,group_by_stack_n)
[docs]defevents(self):""" Returns the list of unaggregated profiler events, to be used in the trace callback or after the profiling is finished """assertself.profilerreturnself.profiler.function_events
[docs]defadd_metadata(self,key:str,value:str):""" Adds a user defined metadata with a string key and a string value into the trace file """wrapped_value='"'+value.replace('"','\\"')+'"'torch.autograd._add_metadata_json(key,wrapped_value)
[docs]defadd_metadata_json(self,key:str,value:str):""" Adds a user defined metadata with a string key and a valid json value into the trace file """torch.autograd._add_metadata_json(key,value)
[docs]defpreset_metadata_json(self,key:str,value:str):""" Preset a user defined metadata when the profiler is not started and added into the trace file later. Metadata is in the format of a string key and a valid json value """self.preset_metadata[key]=value
def_get_distributed_info(self):importtorch.distributedasdistifnotdist.is_available()ornotdist.is_initialized():returnNonebackend=dist.get_backend()dist_info={"backend":backend,"rank":dist.get_rank(),"world_size":dist.get_world_size(),"pg_count":dist.get_pg_count(),"pg_config":dist.distributed_c10d._get_all_pg_configs(),}ifbackend=="nccl":nccl_version=torch.cuda.nccl.version()dist_info["nccl_version"]=".".join(str(v)forvinnccl_version)returndist_infodef_memory_profile(self)->MemoryProfile:required=("record_shapes","profile_memory","with_stack")missing=[f"{i}=True"foriinrequiredifnotgetattr(self,i)]ifmissing:raiseValueError(f"{', '.join(missing)} required for memory profiling.")assertself.profilerisnotNoneandself.profiler.kineto_resultsisnotNonereturnMemoryProfile(self.profiler.kineto_results)
[docs]defexport_memory_timeline(self,path:str,device:Optional[str]=None)->None:"""Export memory event information from the profiler collected tree for a given device, and export a timeline plot. There are 3 exportable files using ``export_memory_timeline``, each controlled by the ``path``'s suffix. - For an HTML compatible plot, use the suffix ``.html``, and a memory timeline plot will be embedded as a PNG file in the HTML file. - For plot points consisting of ``[times, [sizes by category]]``, where ``times`` are timestamps and ``sizes`` are memory usage for each category. The memory timeline plot will be saved a JSON (``.json``) or gzipped JSON (``.json.gz``) depending on the suffix. - For raw memory points, use the suffix ``.raw.json.gz``. Each raw memory event will consist of ``(timestamp, action, numbytes, category)``, where ``action`` is one of ``[PREEXISTING, CREATE, INCREMENT_VERSION, DESTROY]``, and ``category`` is one of the enums from ``torch.profiler._memory_profiler.Category``. Output: Memory timeline written as gzipped JSON, JSON, or HTML. """# Default to device 0, if unset. Fallback on cpu.ifdeviceisNoneandself.use_deviceandself.use_device!="cuda":device=self.use_device+":0"ifdeviceisNone:device="cuda:0"iftorch.cuda.is_available()else"cpu"# Construct the memory timeline plot dataself.mem_tl=MemoryProfileTimeline(self._memory_profile())# Depending on the file suffix, save the data as json.gz or json.# For html, we can embed the image into an HTML file.ifpath.endswith(".html"):self.mem_tl.export_memory_timeline_html(path,device)elifpath.endswith(".gz"):fp=tempfile.NamedTemporaryFile("w+t",suffix=".json",delete=False)fp.close()ifpath.endswith("raw.json.gz"):self.mem_tl.export_memory_timeline_raw(fp.name,device)else:self.mem_tl.export_memory_timeline(fp.name,device)withopen(fp.name)asfin:withgzip.open(path,"wt")asfout:fout.writelines(fin)os.remove(fp.name)else:self.mem_tl.export_memory_timeline(path,device)
[docs]classProfilerAction(Enum):""" Profiler actions that can be taken at the specified intervals """NONE=0WARMUP=1RECORD=2RECORD_AND_SAVE=3
[docs]defschedule(*,wait:int,warmup:int,active:int,repeat:int=0,skip_first:int=0,skip_first_wait:int=0,)->Callable:""" Returns a callable that can be used as profiler ``schedule`` argument. The profiler will skip the first ``skip_first`` steps, then wait for ``wait`` steps, then do the warmup for the next ``warmup`` steps, then do the active recording for the next ``active`` steps and then repeat the cycle starting with ``wait`` steps. The optional number of cycles is specified with the ``repeat`` parameter, the zero value means that the cycles will continue until the profiling is finished. The ``skip_first_wait`` parameter controls whether the first ``wait`` stage should be skipped. This can be useful if a user wants to wait longer than ``skip_first`` between cycles, but not for the first profile. For example, if ``skip_first`` is 10 and ``wait`` is 20, the first cycle will wait 10 + 20 = 30 steps before warmup if ``skip_first_wait`` is zero, but will wait only 10 steps if ``skip_first_wait`` is non-zero. All subsequent cycles will then wait 20 steps between the last active and warmup. """defschedule_fn(step:int)->ProfilerAction:assertstep>=0ifstep<skip_first:returnProfilerAction.NONEelse:step-=skip_first# If wait >> skip_first and we want to grab profiling early, shift left by wait if skip_first_wait is Trueifskip_first_wait!=0:step+=waitnum_steps=wait+warmup+activeifrepeat>0andstep/num_steps>=repeat:returnProfilerAction.NONEmod_step=step%num_stepsifmod_step<wait:returnProfilerAction.NONEelifmod_step<wait+warmup:returnProfilerAction.WARMUPelse:return(ProfilerAction.RECORDifmod_step<num_steps-1elseProfilerAction.RECORD_AND_SAVE)assert(wait>=0andwarmup>=0andactive>0andrepeat>=0andskip_first>=0),"Invalid profiler schedule arguments"ifwarmup==0:warn("Profiler won't be using warmup, this can skew profiler results")returnschedule_fn
def_default_schedule_fn(_:int)->ProfilerAction:""" Default profiler behavior - immediately starts recording the events, keeps doing it on every profiler step. """returnProfilerAction.RECORD
[docs]deftensorboard_trace_handler(dir_name:str,worker_name:Optional[str]=None,use_gzip:bool=False):""" Outputs tracing files to directory of ``dir_name``, then that directory can be directly delivered to tensorboard as logdir. ``worker_name`` should be unique for each worker in distributed scenario, it will be set to '[hostname]_[pid]' by default. """importosimportsocketimporttimedefhandler_fn(prof)->None:nonlocalworker_nameifnotos.path.isdir(dir_name):try:os.makedirs(dir_name,exist_ok=True)exceptExceptionase:raiseRuntimeError("Can't create directory: "+dir_name)fromeifnotworker_name:worker_name=f"{socket.gethostname()}_{os.getpid()}"# Use nanosecond here to avoid naming clash when exporting the tracefile_name=f"{worker_name}.{time.time_ns()}.pt.trace.json"ifuse_gzip:file_name=file_name+".gz"prof.export_chrome_trace(os.path.join(dir_name,file_name))returnhandler_fn
[docs]classprofile(_KinetoProfile):"""Profiler context manager. Args: activities (iterable): list of activity groups (CPU, CUDA) to use in profiling, supported values: ``torch.profiler.ProfilerActivity.CPU``, ``torch.profiler.ProfilerActivity.CUDA``, ``torch.profiler.ProfilerActivity.XPU``. Default value: ProfilerActivity.CPU and (when available) ProfilerActivity.CUDA or (when available) ProfilerActivity.XPU. schedule (Callable): callable that takes step (int) as a single parameter and returns ``ProfilerAction`` value that specifies the profiler action to perform at each step. on_trace_ready (Callable): callable that is called at each step when ``schedule`` returns ``ProfilerAction.RECORD_AND_SAVE`` during the profiling. record_shapes (bool): save information about operator's input shapes. profile_memory (bool): track tensor memory allocation/deallocation. with_stack (bool): record source information (file and line number) for the ops. with_flops (bool): use formula to estimate the FLOPs (floating point operations) of specific operators (matrix multiplication and 2D convolution). with_modules (bool): record module hierarchy (including function names) corresponding to the callstack of the op. e.g. If module A's forward call's module B's forward which contains an aten::add op, then aten::add's module hierarchy is A.B Note that this support exist, at the moment, only for TorchScript models and not eager mode models. experimental_config (_ExperimentalConfig) : A set of experimental options used for Kineto library features. Note, backward compatibility is not guaranteed. execution_trace_observer (ExecutionTraceObserver) : A PyTorch Execution Trace Observer object. `PyTorch Execution Traces <https://arxiv.org/pdf/2305.14516.pdf>`__ offer a graph based representation of AI/ML workloads and enable replay benchmarks, simulators, and emulators. When this argument is included the observer start() and stop() will be called for the same time window as PyTorch profiler. See the examples section below for a code sample. acc_events (bool): Enable the accumulation of FunctionEvents across multiple profiling cycles use_cuda (bool): .. deprecated:: 1.8.1 use ``activities`` instead. .. note:: Use :func:`~torch.profiler.schedule` to generate the callable schedule. Non-default schedules are useful when profiling long training jobs and allow the user to obtain multiple traces at the different iterations of the training process. The default schedule simply records all the events continuously for the duration of the context manager. .. note:: Use :func:`~torch.profiler.tensorboard_trace_handler` to generate result files for TensorBoard: ``on_trace_ready=torch.profiler.tensorboard_trace_handler(dir_name)`` After profiling, result files can be found in the specified directory. Use the command: ``tensorboard --logdir dir_name`` to see the results in TensorBoard. For more information, see `PyTorch Profiler TensorBoard Plugin <https://github.com/pytorch/kineto/tree/master/tb_plugin>`__ .. note:: Enabling shape and stack tracing results in additional overhead. When record_shapes=True is specified, profiler will temporarily hold references to the tensors; that may further prevent certain optimizations that depend on the reference count and introduce extra tensor copies. Examples: .. code-block:: python with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ] ) as p: code_to_profile() print(p.key_averages().table( sort_by="self_cuda_time_total", row_limit=-1)) Using the profiler's ``schedule``, ``on_trace_ready`` and ``step`` functions: .. code-block:: python # Non-default profiler schedule allows user to turn profiler on and off # on different iterations of the training loop; # trace_handler is called every time a new trace becomes available def trace_handler(prof): print(prof.key_averages().table( sort_by="self_cuda_time_total", row_limit=-1)) # prof.export_chrome_trace("/tmp/test_trace_" + str(prof.step_num) + ".json") with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ], # In this example with wait=1, warmup=1, active=2, repeat=1, # profiler will skip the first step/iteration, # start warming up on the second, record # the third and the forth iterations, # after which the trace will become available # and on_trace_ready (when set) is called; # the cycle repeats starting with the next step schedule=torch.profiler.schedule( wait=1, warmup=1, active=2, repeat=1), on_trace_ready=trace_handler # on_trace_ready=torch.profiler.tensorboard_trace_handler('./log') # used when outputting for tensorboard ) as p: for iter in range(N): code_iteration_to_profile(iter) # send a signal to the profiler that the next iteration has started p.step() The following sample shows how to setup up an Execution Trace Observer (`execution_trace_observer`) .. code-block:: python with torch.profiler.profile( ... execution_trace_observer=( ExecutionTraceObserver().register_callback("./execution_trace.json") ), ) as p: for iter in range(N): code_iteration_to_profile(iter) p.step() You can also refer to test_execution_trace_with_kineto() in tests/profiler/test_profiler.py. Note: One can also pass any object satisfying the _ITraceObserver interface. """def__init__(self,*,activities:Optional[Iterable[ProfilerActivity]]=None,schedule:Optional[Callable[[int],ProfilerAction]]=None,on_trace_ready:Optional[Callable[...,Any]]=None,record_shapes:bool=False,profile_memory:bool=False,with_stack:bool=False,with_flops:bool=False,with_modules:bool=False,experimental_config:Optional[_ExperimentalConfig]=None,execution_trace_observer:Optional[_ITraceObserver]=None,acc_events:bool=False,# deprecated:use_cuda:Optional[bool]=None,custom_trace_id_callback:Optional[Callable[[],str]]=None,):activities_set=set(activities)ifactivitieselsesupported_activities()ifuse_cudaisnotNone:warn("`use_cuda` is deprecated, use `activities` argument instead",FutureWarning,stacklevel=2,)ifuse_cuda:activities_set.add(ProfilerActivity.CUDA)elifProfilerActivity.CUDAinactivities_set:activities_set.remove(ProfilerActivity.CUDA)assertlen(activities_set)>0,"No valid profiler activities found"super().__init__(activities=activities,record_shapes=record_shapes,profile_memory=profile_memory,with_stack=with_stack,with_flops=with_flops,with_modules=with_modules,experimental_config=experimental_config,execution_trace_observer=execution_trace_observer,acc_events=acc_events,custom_trace_id_callback=custom_trace_id_callback,)ifschedule:self.schedule=schedule# add step markers into the trace and table viewself.record_steps=Trueelse:self.schedule=_default_schedule_fnself.record_steps=Falseself.on_trace_ready=on_trace_readyself.step_num=0self.current_action=self.schedule(self.step_num)self.step_rec_fn:Optional[prof.record_function]=Noneself.action_map:Dict[Tuple[ProfilerAction,Optional[ProfilerAction]],List[Any]]={# key is (prev_action, current_action), value is action list corresponding to the state pair.(ProfilerAction.NONE,ProfilerAction.NONE):[],(ProfilerAction.NONE,ProfilerAction.WARMUP):[self.prepare_trace],(ProfilerAction.NONE,ProfilerAction.RECORD):[self.prepare_trace,self.start_trace,],(ProfilerAction.NONE,ProfilerAction.RECORD_AND_SAVE):[self.prepare_trace,self.start_trace,],(ProfilerAction.WARMUP,ProfilerAction.NONE):[partial(warn,"Incorrect schedule: WARMUP followed by NONE"),self.start_trace,self.stop_trace,],(ProfilerAction.WARMUP,ProfilerAction.WARMUP):[],(ProfilerAction.WARMUP,ProfilerAction.RECORD):[self.start_trace],(ProfilerAction.WARMUP,ProfilerAction.RECORD_AND_SAVE):[self.start_trace],(ProfilerAction.RECORD,ProfilerAction.NONE):[partial(warn,"Incorrect schedule: RECORD followed by NONE"),self.stop_trace,],(ProfilerAction.RECORD,ProfilerAction.WARMUP):[partial(warn,"Incorrect schedule: RECORD followed by WARMUP"),self.stop_trace,],(ProfilerAction.RECORD,ProfilerAction.RECORD):[],(ProfilerAction.RECORD,ProfilerAction.RECORD_AND_SAVE):[],(ProfilerAction.RECORD_AND_SAVE,ProfilerAction.NONE):[self.stop_trace,self._trace_ready,],(ProfilerAction.RECORD_AND_SAVE,ProfilerAction.WARMUP):[self.stop_trace,self._trace_ready,self.prepare_trace,],(ProfilerAction.RECORD_AND_SAVE,ProfilerAction.RECORD):[self.stop_trace,self._trace_ready,self.prepare_trace,self.start_trace,],(ProfilerAction.RECORD_AND_SAVE,ProfilerAction.RECORD_AND_SAVE):[self.stop_trace,self._trace_ready,self.prepare_trace,self.start_trace,],# used for exit action(ProfilerAction.WARMUP,None):[self.start_trace,self.stop_trace],(ProfilerAction.RECORD,None):[self.stop_trace,self._trace_ready],(ProfilerAction.RECORD_AND_SAVE,None):[self.stop_trace,self._trace_ready,],}# Start tracking increments to profiler step, this will be used# by Kinetoprof.KinetoStepTracker.init_step_count(PROFILER_STEP_NAME)def__enter__(self):self.start()returnselfdef__exit__(self,exc_type,exc_val,exc_tb):self.stop()prof.KinetoStepTracker.erase_step_count(PROFILER_STEP_NAME)ifself.execution_trace_observer:self.execution_trace_observer.cleanup()defstart(self):self._transit_action(ProfilerAction.NONE,self.current_action)ifself.record_steps:self.step_rec_fn=prof.record_function("ProfilerStep#"+str(self.step_num))self.step_rec_fn.__enter__()defstop(self):ifself.record_stepsandself.step_rec_fn:self.step_rec_fn.__exit__(None,None,None)self._transit_action(self.current_action,None)
[docs]defstep(self):""" Signals the profiler that the next profiling step has started. """ifself.record_stepsandself.step_rec_fn:self.step_rec_fn.__exit__(None,None,None)prev_action=self.current_actionself.step_num+=1self.current_action=self.schedule(self.step_num)self._transit_action(prev_action,self.current_action)prof.KinetoStepTracker.increment_step(PROFILER_STEP_NAME)ifself.record_steps:self.step_rec_fn=prof.record_function("ProfilerStep#"+str(self.step_num))self.step_rec_fn.__enter__()
[docs]defset_custom_trace_id_callback(self,callback):""" Sets a callback to be called when a new trace ID is generated. """self.custom_trace_id_callback=callback
[docs]defget_trace_id(self):""" Returns the current trace ID. """ifself.profilerisNone:returnNonereturnself.profiler.trace_id
classExecutionTraceObserver(_ITraceObserver):"""Execution Trace Observer Each process can have a single ExecutionTraceObserver instance. The observer can be added to record function callbacks via calling register_callback() explicitly. Without calling unregister_callback(), repeated calls to register_callback() will not add additional observers to record function callbacks. Once an ExecutionTraceObserver is created, the start() and stop() methods control when the event data is recorded. Deleting or calling unregister_callback() will remove the observer from the record function callbacks, finalize the output file, and will stop incurring any overheads. """def__init__(self)->None:""" Initializes the default states. """self._registered=Falseself._execution_trace_running=Falsedef__del__(self):""" Calls unregister_callback() to make sure to finalize outputs. """self.unregister_callback()defregister_callback(self,output_file_path:str)->Self:""" Adds ET observer to record function callbacks. The data will be written to output_file_path. """ifnotself._registered:self._output_file_path=output_file_pathself._registered=_add_execution_trace_observer(output_file_path)returnselfdefunregister_callback(self):""" Removes ET observer from record function callbacks. """def_save_triton_kernels():# Save the kernel paths for the generated kernelsfromtorch._inductor.codecacheimportPyCodeCacheasPyCodeCachekernel_files=[v.__file__forvinPyCodeCache.modulesifgetattr(v,"__file__",None)isnotNone]work_dir,file_name=os.path.split(self._output_file_path)resource_dir=os.path.join(work_dir,os.path.splitext(file_name)[0]+"_resources")ifnotos.path.exists(resource_dir):os.mkdir(resource_dir)forkernel_fileinkernel_files:ifkernel_fileisNone:continuename=os.path.basename(kernel_file)dst=os.path.join(resource_dir,name)shutil.copyfile(kernel_file,dst)ifself._registered:self.stop()try:_save_triton_kernels()exceptExceptionase:warn(f"Execution trace failed to save kernels: {e}")_remove_execution_trace_observer()self._registered=False@propertydefis_registered(self):""" Returns True if the execution trace observer is registered, otherwise False. """returnself._registereddefis_running(self):""" Returns True if the observer is running, otherwise False. """returnself._execution_trace_runningdefstart(self):""" Starts to capture. """ifself._registeredandnotself._execution_trace_running:_enable_execution_trace_observer()self._execution_trace_running=Trueself._record_pg_config()defstop(self):""" Stops to capture. """ifself._execution_trace_running:_disable_execution_trace_observer()self._execution_trace_running=Falsedefcleanup(self):""" Calls unregister_callback() to make sure to finalize outputs. """self.unregister_callback()defget_output_file_path(self)->str:""" Returns the output file name. """ifself.is_registered:returnself._output_file_pathelse:raiseRuntimeError("A callback to the ET profiler needs to be registered ""first before getting the output file path")def_record_pg_config(self)->None:# Records the PG config info to the trace as node:# ## process_group:init ##if(self.is_registeredandtorch.distributed.is_available()andtorch.distributed.is_initialized()):pg_config_info=torch.distributed.distributed_c10d._world.pg_config_infotorch.autograd._record_function_with_args_enter("## process_group:init ##",json.dumps(pg_config_info,cls=_NumpyEncoder),)
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.