ezpz.profileΒΆ
- See ezpz/
profile.py
profile.py
Sam Foreman [2024-06-21]
Contains implementation of:
get_context_managerPyInstrumentProfiler
which can be used as a context manager to profile a block of code, e.g.
get_context_manager(rank=None, outdir=None, strict=True, *, profiler_type='pyinstrument', rank_zero_only=True, **profile_kwargs)
ΒΆ
Returns a context manager for profiling code blocks using PyInstrument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
Optional[int]
|
The rank of the process (default: None). If provided, the profiler will only run if rank is 0. |
None
|
outdir
|
Optional[str]
|
The output directory for saving profiles.
Defaults to |
None
|
strict
|
Optional[bool]
|
If True, the profiler will only run if "PYINSTRUMENT_PROFILER" is set in the environment. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
AbstractContextManager |
AbstractContextManager
|
A context manager that starts and stops the PyInstrument profiler. |
Source code in src/ezpz/profile.py
get_profiling_context(profiler_type, wait, warmup, active, repeat, rank_zero_only, record_shapes=True, with_stack=True, with_flops=True, with_modules=True, acc_events=False, profile_memory=False, outdir=None, strict=True)
ΒΆ
Returns a context manager for profiling code blocks using either PyTorch Profiler or PyInstrument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profiler_type
|
str
|
The type of profiler to use. Must be one of ['torch', 'pyinstrument']. |
required |
wait
|
int
|
The number of steps to wait before starting profiling. |
required |
warmup
|
int
|
The number of warmup steps before profiling starts. |
required |
active
|
int
|
The number of active profiling steps. |
required |
repeat
|
int
|
The number of times to repeat the profiling schedule. |
required |
rank_zero_only
|
bool
|
If True, the profiler will only run on rank 0. Defaults to True. |
required |
record_shapes
|
bool
|
If True, shapes of tensors are recorded. Defaults to True. |
True
|
with_stack
|
bool
|
If True, stack traces are recorded. Defaults to True. |
True
|
with_flops
|
bool
|
If True, FLOPs are recorded. Defaults to True. |
True
|
with_modules
|
bool
|
If True, module information is recorded. Defaults to True. |
True
|
acc_events
|
bool
|
If True, accumulated events are recorded. Defaults to False. |
False
|
profile_memory
|
bool
|
If True, memory profiling is enabled. Defaults to False. |
False
|
outdir
|
Optional[str | Path | PathLike]
|
The output directory
for saving profiles. Defaults to |
None
|
strict
|
Optional[bool]
|
If True, the profiler will only run if "PYINSTRUMENT_PROFILER" is set in the environment. Defaults to True. |
True
|
Returns: AbstractContextManager: A context manager that starts and stops the profiler.
Source code in src/ezpz/profile.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
get_torch_profiler(rank=None, schedule=None, on_trace_ready=None, rank_zero_only=True, profile_memory=False, record_shapes=True, with_stack=True, with_flops=True, with_modules=True, acc_events=False)
ΒΆ
A thin wrapper around torch.profiler.profile that:
- Supports automatic device detection {CPU, CUDA, XPU}
- Runs on rank 0 only (by default)
- To run from all ranks, set
rank_zero_only=False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
Optional[int]
|
The rank of the process (default: None). If provided, the profiler will only run if rank is 0. |
None
|
schedule
|
Optional[Callable[[int], ProfilerAction]]
|
A callable
that returns a |
None
|
on_trace_ready
|
Optional[Callable]
|
A callback function that is called when the trace is ready. |
None
|
rank_zero_only
|
bool
|
If True, the profiler will only run on rank 0. Defaults to True. |
True
|
profile_memory
|
bool
|
If True, memory profiling is enabled. Defaults to False. |
False
|
record_shapes
|
bool
|
If True, shapes of tensors are recorded. Defaults to True. |
True
|
with_stack
|
bool
|
If True, stack traces are recorded. Defaults to True. |
True
|
with_flops
|
bool
|
If True, FLOPs are recorded. Defaults to True. |
True
|
with_modules
|
bool
|
If True, module information is recorded. Defaults to True. |
True
|
acc_events
|
bool
|
If True, accumulated events are recorded. Defaults to False. |
False
|
Returns: torch.profiler.profile: A profiler context manager that can be used to profile code blocks.