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.
Classes¶
fastvideo.profiler.ProfilerRegion
dataclass
¶
Metadata describing a profiler region.
fastvideo.profiler.TorchProfilerConfig
dataclass
¶
Configuration for torch profiler region control.
Use :meth:from_env to construct an instance with defaults inherited from
registered regions and optional overrides from the
FASTVIDEO_TORCH_PROFILE_REGIONS environment variable. The resulting
regions map is consumed by :class:TorchProfilerController to decide
when collection should be enabled.
Functions¶
fastvideo.profiler.TorchProfilerConfig.from_env
classmethod
¶
from_env() -> TorchProfilerConfig
Build a configuration from process environment variables.
Source code in fastvideo/profiler.py
fastvideo.profiler.TorchProfilerController
¶
TorchProfilerController(profiler: Any, activities: Iterable[ProfilerActivity], config: TorchProfilerConfig | None = None, disabled: bool = False)
Helper that toggles torch profiler collection for named regions.
Parameters¶
profiler:
The shared :class:torch.profiler.profile instance, or None 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.
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_regionin this module. - Wrap the target code in :meth:
regionusing the new name.
Source code in fastvideo/profiler.py
Attributes¶
fastvideo.profiler.TorchProfilerController.has_profiler
property
¶
has_profiler: bool
Return True when a profiler instance is available.
fastvideo.profiler.TorchProfilerController.is_enabled
property
¶
is_enabled: bool
Return True when the underlying profiler is collecting.
Functions¶
fastvideo.profiler.TorchProfilerController.is_region_enabled
¶
fastvideo.profiler.TorchProfilerController.region
¶
region(region: str)
Context manager that enables profiling for region if configured.
Source code in fastvideo/profiler.py
fastvideo.profiler.TorchProfilerController.start
¶
Start the profiler and pause collection until a region is entered.
Source code in fastvideo/profiler.py
fastvideo.profiler.TorchProfilerController.stop
¶
Stop the profiler after disabling collection and clearing state.
Source code in fastvideo/profiler.py
Functions¶
fastvideo.profiler.get_global_profiler
¶
fastvideo.profiler.get_or_create_profiler
¶
get_or_create_profiler(trace_dir: str | None) -> TorchProfilerController
Create or reuse the process-wide torch profiler controller.
Source code in fastvideo/profiler.py
fastvideo.profiler.list_profiler_regions
¶
list_profiler_regions() -> list[ProfilerRegion]
Return all registered profiler regions sorted by canonical name.
fastvideo.profiler.profile_region
¶
Wrap a bound method so it runs inside a profiler region if available.
Source code in fastvideo/profiler.py
fastvideo.profiler.register_profiler_region
¶
Register a profiler region so configuration can validate inputs.
Source code in fastvideo/profiler.py
fastvideo.profiler.resolve_profiler_region
¶
resolve_profiler_region(name: str) -> ProfilerRegion | None
Return the registered region matching name or None if absent.