pytorch/torch/csrc/profiler
PyTorch MergeBot a58a565819 Revert "[Environment Variable][6/N] Use thread-safe getenv functions (#140200)"
This reverts commit 7d4f5f7508.

Reverted https://github.com/pytorch/pytorch/pull/140200 on behalf of https://github.com/ezyang due to One of these diffs had incorrect downstream optional handling, we must reaudit all of these diffs ([comment](https://github.com/pytorch/pytorch/pull/140200#issuecomment-2473956859))
2024-11-13 15:33:23 +00:00
..
orchestration Enable cppcoreguidelines-special-member-functions (#139132) 2024-11-06 13:42:20 +00:00
python Use Wextra-semi (#140236) 2024-11-13 02:15:16 +00:00
standalone Revert "[pytorch/profiler] Profiler NCCL metadata can now contain collective Input and Ouput Tensor addrs (#139837)" 2024-11-13 12:26:43 +00:00
stubs Turn static inline into static function (#139843) 2024-11-07 23:58:18 +00:00
unwind Revert "[Environment Variable][6/N] Use thread-safe getenv functions (#140200)" 2024-11-13 15:33:23 +00:00
api.h
collection.cpp Revert "[pytorch/profiler] Profiler NCCL metadata can now contain collective Input and Ouput Tensor addrs (#139837)" 2024-11-13 12:26:43 +00:00
collection.h Revert "[pytorch/profiler] Profiler NCCL metadata can now contain collective Input and Ouput Tensor addrs (#139837)" 2024-11-13 12:26:43 +00:00
combined_traceback.cpp
combined_traceback.h
containers.h [2/N] Enable cppcoreguidelines-special-member-functions (#138670) 2024-10-24 04:35:18 +00:00
data_flow.cpp
data_flow.h
events.h
kineto_client_interface.cpp init kineto after torch module initialized (#131448) 2024-10-31 13:24:24 +00:00
kineto_client_interface.h [11/N] Fix extra warnings brought by clang-tidy-17 (#139599) 2024-11-04 23:57:41 +00:00
kineto_shim.cpp [11/N] Fix extra warnings brought by clang-tidy-17 (#139599) 2024-11-04 23:57:41 +00:00
kineto_shim.h [Profiler] Create Auto-Trace Frontend for Trace ID (#139310) 2024-10-31 19:02:57 +00:00
perf-inl.h
perf.cpp [6/N] Fix Wextra-semi warning (#139605) 2024-11-04 13:43:16 +00:00
perf.h [3/N] Fix cppcoreguidelines-special-member-functions warnings (#138796) 2024-10-28 10:53:11 +00:00
README.md
util.cpp Revert "[pytorch/profiler] Profiler NCCL metadata can now contain collective Input and Ouput Tensor addrs (#139837)" 2024-11-13 12:26:43 +00:00
util.h Revert "[pytorch/profiler] Profiler NCCL metadata can now contain collective Input and Ouput Tensor addrs (#139837)" 2024-11-13 12:26:43 +00:00

Profiler Overview

This README describes the details of how the profiler is implemented.

The profiler instruments PyTorch to collect information about the model's execution. Its main features are:

  • Instrumenting op calls on the CPU side
  • Interfacing with Kineto to collect information from the GPU (or other accelerators)
  • Collecting python stack traces
  • Exporting this information, e.g. in a chrome trace, or to be processed by downstream tools like HTA

Table of Contents

Codebase Structure

TODO

RecordFunction

/aten/src/ATen/record_function.h

RecordFunction is used by the profiler to instrument CPU-side events.

RecordFunction is a general method of instrumenting function calls in PyTorch. It can be used for other general applications, e.g. see Features for Large-Scale Deployments. In PyTorch, it is already included at some important locations; notably, in the dispatcher, surrounding every op.

Users (or PyTorch itself) can register callbacks that will be executed whenever a RecordFunction guard is encountered. The profiler uses this mechanism to record the start and end times for each op call, as well as user-provided RecordFunction annotations. The RecordFunction machinery is designed to have relatively low overhead, especially when there are no callbacks registered. Nevertheless, there can still be some overhead.

There is also a python binding for RecordFunction in python (with torch.profiler.record_function); this is often used by users to annotate events corresponding to module-level events.

Autograd Integration

The autograd engine is responsible for automatically computing gradients.

The profiler records two pieces of information from the autograd engine:

  • Sequence number: this is a unique-per-thread index assigned to each op call(*) in the forward pass. When a backward op is triggered, it is also assigned a sequence number matching the sequence number of the forward op that caused that backward op to be executed. Using this information, the profiler is able to match forward and backward ops; in chrome traces, this feature can be enabled with the "fwd_bwd" flow events
  • Forward thread id: Autograd can be used in multi-threaded environments. The forward thread ID indicates the ID of the thread on which the forward op was executed on. This information is needed because the sequence number, mentioned above, is only unique within a thread; the forward thread ID is used for differentiating different ops with the same sequence number.

(*) Note that only op invocations whose inputs require gradients are assigned a sequence number

Collection and Post-Processing

TODO

Kineto Integration

TODO

Python Tracing

TODO