import gzip
import json
import os
import tempfile
from enum import Enum
from functools import partial
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
from warnings import warn
import torch
import torch.autograd.profiler as prof
from torch._C._profiler import (
_add_execution_graph_observer,
_disable_execution_graph_observer,
_enable_execution_graph_observer,
_ExperimentalConfig,
_remove_execution_graph_observer,
)
from torch.autograd import kineto_available, ProfilerActivity
from torch.profiler import _memory_profiler
__all__ = [
"supported_activities",
"ProfilerAction",
"schedule",
"tensorboard_trace_handler",
"profile",
"ExecutionGraphObserver",
]
PROFILER_STEP_NAME = "ProfilerStep"
def supported_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.
"""
return torch.autograd._supported_activities()
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``.
Default value: ProfilerActivity.CPU and (when available) ProfilerActivity.CUDA.
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 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.
.. 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):
self.activities = set(activities) if activities else supported_activities()
self.record_shapes = record_shapes
self.with_flops = with_flops
self.profile_memory = profile_memory
self.with_stack = with_stack
self.with_modules = with_modules
self.experimental_config = experimental_config
self.profiler: Optional[prof.profile] = None
def start(self):
self.prepare_trace()
self.start_trace()
def stop(self):
self.stop_trace()
def prepare_trace(self):
self.profiler = prof.profile(
use_cuda=(ProfilerActivity.CUDA in self.activities),
use_cpu=(ProfilerActivity.CPU in self.activities),
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,
)
self.profiler._prepare_trace()
def start_trace(self):
assert self.profiler is not None
self.profiler._start_trace()
if self.profile_memory:
self.add_metadata_json("profile_memory", "1")
if self.with_stack:
self.add_metadata_json("with_stack", "1")
if self.record_shapes:
self.add_metadata_json("record_shapes", "1")
if self.with_modules:
self.add_metadata_json("with_modules", "1")
if self.with_flops:
self.add_metadata_json("with_flops", "1")
if kineto_available():
dist_info = self._get_distributed_info()
if dist_info:
self.add_metadata_json("distributedInfo", json.dumps(dist_info))
def stop_trace(self):
assert self.profiler is not None
self.profiler.__exit__(None, None, None)
def export_chrome_trace(self, path: str):
"""
Exports the collected trace in Chrome JSON format.
"""
assert self.profiler
if path.endswith('.gz'):
fp = tempfile.NamedTemporaryFile('w+t', suffix='.json', delete=False)
fp.close()
retvalue = self.profiler.export_chrome_trace(fp.name)
with open(fp.name) as fin:
with gzip.open(path, 'wt') as fout:
fout.writelines(fin)
os.remove(fp.name)
return retvalue
else:
return self.profiler.export_chrome_trace(path)
def export_stacks(self, path: str, metric: str = "self_cpu_time_total"):
"""Save stack traces in a file in a format suitable for visualization.
Args:
path (str): save stacks file to this location;
metric (str): metric to use: "self_cpu_time_total" or "self_cuda_time_total"
.. note::
Example of using FlameGraph tool:
- git clone https://github.com/brendangregg/FlameGraph
- cd FlameGraph
- ./flamegraph.pl --title "CPU time" --countname "us." profiler.stacks > perf_viz.svg
"""
assert self.profiler
return self.profiler.export_stacks(path, metric)
def key_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.
"""
assert self.profiler
return self.profiler.key_averages(group_by_input_shape, group_by_stack_n)
def events(self):
"""
Returns the list of unaggregated profiler events,
to be used in the trace callback or after the profiling is finished
"""
assert self.profiler
return self.profiler.function_events
def add_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)
def add_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)
def _get_distributed_info(self):
import torch.distributed as dist
if not dist.is_available() or not dist.is_initialized():
return None
return {
"backend": dist.get_backend(),
"rank": dist.get_rank(),
"world_size": dist.get_world_size()
}
def _memory_profile(self) -> _memory_profiler.MemoryProfile:
required = ("record_shapes", "profile_memory", "with_stack")
missing = [f"{i}=True" for i in required if not getattr(self, i)]
if missing:
raise ValueError(f"{', '.join(missing)} required for memory profiling.")
assert self.profiler is not None and self.profiler.kineto_results is not None
return _memory_profiler.MemoryProfile(self.profiler.kineto_results)
class ProfilerAction(Enum):
"""
Profiler actions that can be taken at the specified intervals
"""
NONE = 0
WARMUP = 1
RECORD = 2
RECORD_AND_SAVE = 3
def schedule(*, wait: int, warmup: int, active: int, repeat: int = 0, skip_first: 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.
"""
def schedule_fn(step: int) -> ProfilerAction:
assert step >= 0
if step < skip_first:
return ProfilerAction.NONE
else:
step -= skip_first
num_steps = wait + warmup + active
if repeat > 0 and step / num_steps >= repeat:
return ProfilerAction.NONE
mod_step = step % num_steps
if mod_step < wait:
return ProfilerAction.NONE
elif mod_step < wait + warmup:
return ProfilerAction.WARMUP
else:
return ProfilerAction.RECORD if mod_step < num_steps - 1 \
else ProfilerAction.RECORD_AND_SAVE
assert wait >= 0 and warmup >= 0 and active > 0 and \
repeat >= 0 and skip_first >= 0, "Invalid profiler schedule arguments"
if warmup == 0:
warn("Profiler won't be using warmup, this can skew profiler results")
return schedule_fn
def _default_schedule_fn(_: int) -> ProfilerAction:
"""
Default profiler behavior - immediately starts recording the events,
keeps doing it on every profiler step.
"""
return ProfilerAction.RECORD
def tensorboard_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.
"""
import os
import socket
import time
def handler_fn(prof) -> None:
nonlocal worker_name
if not os.path.isdir(dir_name):
try:
os.makedirs(dir_name, exist_ok=True)
except Exception as e:
raise RuntimeError("Can't create directory: " + dir_name) from e
if not worker_name:
worker_name = "{}_{}".format(socket.gethostname(), str(os.getpid()))
file_name = "{}.{}.pt.trace.json".format(worker_name, int(time.time() * 1000))
if use_gzip:
file_name = file_name + '.gz'
prof.export_chrome_trace(os.path.join(dir_name, file_name))
return handler_fn
class profile(_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``.
Default value: ProfilerActivity.CPU and (when available) ProfilerActivity.CUDA.
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.
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:
Loading ...