stormlog.context_profiler
Context profiler for easy function and code block profiling.
Functions
Clear global profiler results. |
|
|
Get or create the global profiler instance. |
|
Return recent profile results captured by the global profiler. |
Get global profiler summary. |
|
|
Context manager for profiling a block of code. |
|
Decorator to profile a function's GPU memory usage. |
|
Profile an entire training loop. |
|
Set the global profiler instance. |
|
Start global memory monitoring. |
Stop global memory monitoring. |
Classes
|
High-level memory profiler with convenient methods. |
|
Wrapper for PyTorch modules that automatically profiles forward passes. |
- stormlog.context_profiler.get_global_profiler(device=None)[source]
Get or create the global profiler instance.
- Parameters:
device (str | int | torch.device | None)
- Return type:
- stormlog.context_profiler.set_global_profiler(profiler)[source]
Set the global profiler instance.
- Parameters:
profiler (GPUMemoryProfiler)
- Return type:
None
- stormlog.context_profiler.profile_function(func=None, *, name=None, device=None, profiler=None)[source]
Decorator to profile a function’s GPU memory usage.
Can be used as @profile_function or @profile_function(name=”custom_name”)
- Parameters:
func (F | None) – Function to profile (when used as @profile_function)
name (str | None) – Custom name for the profiled function
device (str | int | torch.device | None) – GPU device to use for profiling
profiler (GPUMemoryProfiler | None) – Custom profiler instance to use
- Returns:
Decorated function or ProfileResult if called directly
- Return type:
Callable[[F], F] | F
- stormlog.context_profiler.profile_context(name='context', device=None, profiler=None)[source]
Context manager for profiling a block of code.
- Parameters:
name (str) – Name for the profiled context
device (str | int | torch.device | None) – GPU device to use for profiling
profiler (GPUMemoryProfiler | None) – Custom profiler instance to use
- Yields:
ProfileResult after the context exits
- Return type:
Iterator[GPUMemoryProfiler]
Example
- with profile_context(“model_forward”) as prof:
output = model(input)
- class stormlog.context_profiler.ProfiledModule(*args, **kwargs)[source]
Bases:
ModuleWrapper for PyTorch modules that automatically profiles forward passes.
Example
model = ProfiledModule(original_model, name=”my_model”) output = model(input) # Automatically profiled
- Parameters:
module (torch.nn.Module)
name (str | None)
device (str | int | torch.device | None)
profiler (GPUMemoryProfiler | None)
- class stormlog.context_profiler.MemoryProfiler(device=None)[source]
Bases:
objectHigh-level memory profiler with convenient methods.
This class provides a simplified interface for common profiling tasks.
- Parameters:
device (str | int | torch.device | None)
- start_monitoring(interval=0.1)[source]
Start continuous memory monitoring.
- Parameters:
interval (float)
- Return type:
None
- profile(func, *args, **kwargs)[source]
Profile a function call.
- Parameters:
func (Callable[[...], Any])
args (Any)
kwargs (Any)
- Return type:
- context(name='context')[source]
Context manager for profiling code blocks.
- Parameters:
name (str)
- Return type:
Iterator[None]
- wrap_module(module, name=None)[source]
Wrap a PyTorch module for automatic profiling.
- Parameters:
module (torch.nn.Module)
name (str | None)
- Return type:
- stormlog.context_profiler.start_monitoring(interval=0.1, device=None)[source]
Start global memory monitoring.
- Parameters:
interval (float)
device (str | int | torch.device | None)
- Return type:
None
- stormlog.context_profiler.stop_monitoring()[source]
Stop global memory monitoring.
- Return type:
None
- stormlog.context_profiler.get_profile_results(limit=None)[source]
Return recent profile results captured by the global profiler.
- Parameters:
limit (int | None)
- Return type:
List[ProfileResult]
- stormlog.context_profiler.profile_model_training(model, train_loader, epochs=1, device=None)[source]
Profile an entire training loop.
- Parameters:
model (torch.nn.Module) – PyTorch model to train
train_loader (Any) – DataLoader for training data
epochs (int) – Number of epochs to profile
device (str | int | torch.device | None) – GPU device to use
- Returns:
Dictionary with profiling results
- Return type:
Dict[str, Any]