Table of Contents
Shortcuts

# torch.profiler

## Overview

PyTorch Profiler is a tool that allows the collection of performance metrics during training and inference. Profiler’s context manager API can be used to better understand what model operators are the most expensive, examine their input shapes and stack traces, study device kernel activity and visualize the execution trace.

Note

An earlier version of the API in `torch.autograd` module is considered legacy and will be deprecated.

## API Reference

class torch.profiler._KinetoProfile(*, activities=None, record_shapes=False, profile_memory=False, with_stack=False, with_flops=False, with_modules=False, experimental_config=None)

  Low-level profiler wrap the autograd profile

  Parameters:

    - 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.

  add_metadata(key, value)

    Adds a user defined metadata with a string key and a string value into the trace file

  add_metadata_json(key, value)

    Adds a user defined metadata with a string key and a valid json value into the trace file

  events()

    Returns the list of unaggregated profiler events, to be used in the trace callback or after the profiling is finished

  export_chrome_trace(path)

    Exports the collected trace in Chrome JSON format.

  export_memory_timeline(path, device=None)

    Extract the memory information from the memory profile collected tree for a given device, and export a timeline plot 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 (by default) or gzipped JSON.

    Input: (path of file, device) Output: File written as JSON or gzipped JSON

  export_stacks(path, metric='self_cpu_time_total')

    Save stack traces in a file in a format suitable for visualization.

    Parameters:

      - 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

  key_averages(group_by_input_shape=False, group_by_stack_n=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.

class torch.profiler.profile(*, activities=None, schedule=None, on_trace_ready=None, record_shapes=False, profile_memory=False, with_stack=False, with_flops=False, with_modules=False, experimental_config=None, use_cuda=None)

  Profiler context manager.

  Parameters:

    - 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 since version 1.8.1: use `activities` instead.

  Note

  Use `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 `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

  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:

      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:

      # 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()

  step()

    Signals the profiler that the next profiling step has started.

class torch.profiler.ProfilerAction(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

  Profiler actions that can be taken at the specified intervals

class torch.profiler.ProfilerActivity

  Members:

  CPU

  XPU

  MTIA

  CUDA

  property name

torch.profiler.schedule(*, wait, warmup, active, repeat=0, skip_first=0)

  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.

  Return type:

    Callable

torch.profiler.tensorboard_trace_handler(dir_name, worker_name=None, use_gzip=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.

## Intel Instrumentation and Tracing Technology APIs

torch.profiler.itt.is_available()

  Check if ITT feature is available or not

torch.profiler.itt.mark(msg)

  Describe an instantaneous event that occurred at some point.

  Parameters:

    msg (str) – ASCII message to associate with the event.

torch.profiler.itt.range_push(msg)

  Pushes a range onto a stack of nested range span. Returns zero-based depth of the range that is started.

  Parameters:

    msg (str) – ASCII message to associate with range

torch.profiler.itt.range_pop()

  Pops a range off of a stack of nested range spans. Returns the zero-based depth of the range that is ended.

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

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.