fastvideo.profiler
#
Utilities for managing the PyTorch profiler within FastVideo.
The profiler is shared across the process; this module adds a light-weight
controller that gates collection based on named regions. Regions may be
enabled through dedicated environment variables (e.g.
FASTVIDEO_TORCH_PROFILE_MODEL_LOADING=1
) or via the consolidated
FASTVIDEO_TORCH_PROFILE_REGIONS
comma-separated list (e.g.
FASTVIDEO_TORCH_PROFILE_REGIONS=model_loading,training_dit
).
Typical usage from client code::
controller = TorchProfilerController(profiler, activities)
with controller.region("training_dit"):
run_training_step()
To introduce a new region, register it via :func:register_profiler_region
and wrap the corresponding code in :meth:TorchProfilerController.region
.
Module Contents#
Classes#
Metadata describing a profiler region. |
|
Configuration for torch profiler region control. |
|
Helper that toggles torch profiler collection for named regions. |
Functions#
Return the global profiler instance if one was created. |
|
Create or reuse the process-wide torch profiler controller. |
|
Return all registered profiler regions sorted by canonical name. |
|
Wrap a bound method so it runs inside a profiler region if available. |
|
Register a profiler region so configuration can validate inputs. |
|
Return the registered region matching |
|
Data#
API#
- class fastvideo.profiler.TorchProfilerConfig[source]#
Configuration for torch profiler region control.
Use :meth:
from_env
to construct an instance with defaults inherited from registered regions and optional overrides from theFASTVIDEO_TORCH_PROFILE_REGIONS
environment variable. The resultingregions
map is consumed by :class:TorchProfilerController
to decide when collection should be enabled.- classmethod from_env() fastvideo.profiler.TorchProfilerConfig [source]#
Build a configuration from process environment variables.
- class fastvideo.profiler.TorchProfilerController(profiler: Any, activities: collections.abc.Iterable[torch.profiler.ProfilerActivity], config: fastvideo.profiler.TorchProfilerConfig | None = None, disabled: bool = False)[source]#
Helper that toggles torch profiler collection for named regions.
- Parameters:
profiler β The shared :class:
torch.profiler.profile
instance, orNone
if profiling is disabled.activities β Iterable of :class:
torch.profiler.ProfilerActivity
recorded by the profiler.config β Optional :class:
TorchProfilerConfig
. If omitted, :meth:from_env
constructs one during initialization.
.. rubric:: Examples
Enabling an existing region from the command line::
FASTVIDEO_TORCH_PROFILE_REGIONS=model_loading,training_dit python fastvideo/training/wan_training_pipeline.py ...
Wrapping a code block in a custom region::
controller = TorchProfilerController(profiler, activities) with controller.region("training_validation"): run_validation_epoch()
Adding a new region requires three steps:
Define an env var in
envs.py
.Add a default entry to
register_profiler_region
in this module.Wrap the target code in :meth:
region
using the new name.
Initialization
- property activities: tuple[torch.profiler.ProfilerActivity, ...][source]#
- property profiler: torch.profiler.profile | None[source]#
- fastvideo.profiler.get_global_controller() TorchProfilerController | None [source]#
- fastvideo.profiler.get_global_profiler() torch.profiler.profile | None [source]#
Return the global profiler instance if one was created.
- fastvideo.profiler.get_or_create_profiler(trace_dir: str | None) TorchProfilerController [source]#
Create or reuse the process-wide torch profiler controller.
- fastvideo.profiler.list_profiler_regions() list[fastvideo.profiler.ProfilerRegion] [source]#
Return all registered profiler regions sorted by canonical name.
- fastvideo.profiler.profile_region(region: str) collections.abc.Callable[[collections.abc.Callable[..., Any]], collections.abc.Callable[..., Any]] [source]#
Wrap a bound method so it runs inside a profiler region if available.
- fastvideo.profiler.register_profiler_region(name: str, description: str, *, default_enabled: bool = False) None [source]#
Register a profiler region so configuration can validate inputs.
- fastvideo.profiler.resolve_profiler_region(name: str) fastvideo.profiler.ProfilerRegion | None [source]#
Return the registered region matching
name
orNone
if absent.
- fastvideo.profiler.set_global_controller(controller: TorchProfilerController | None) None [source]#
- fastvideo.profiler.set_global_profiler(profiler: torch.profiler.profile | None) None [source]#