stormlog.profiler

Core Stormlog for PyTorch.

Classes

GPUMemoryProfiler([device, track_tensors, ...])

Comprehensive GPU memory profiler for PyTorch operations.

MemorySnapshot(timestamp, allocated_memory, ...)

Represents a memory snapshot at a specific point in time.

ProfileResult(function_name, execution_time, ...)

Results from profiling a function or operation.

TensorTracker()

Tracks tensor creation and deletion for memory profiling.

class stormlog.profiler.MemorySnapshot(timestamp, allocated_memory, reserved_memory, max_memory_allocated, max_memory_reserved, active_memory, inactive_memory, cpu_memory, device_id=0, operation=None, stack_trace=None)[source]

Bases: object

Represents a memory snapshot at a specific point in time.

Parameters:
  • timestamp (float)

  • allocated_memory (int)

  • reserved_memory (int)

  • max_memory_allocated (int)

  • max_memory_reserved (int)

  • active_memory (int)

  • inactive_memory (int)

  • cpu_memory (int)

  • device_id (int)

  • operation (str | None)

  • stack_trace (str | None)

timestamp: float
allocated_memory: int
reserved_memory: int
max_memory_allocated: int
max_memory_reserved: int
active_memory: int
inactive_memory: int
cpu_memory: int
device_id: int = 0
operation: str | None = None
stack_trace: str | None = None
to_dict()[source]

Convert snapshot to dictionary.

Return type:

Dict[str, Any]

class stormlog.profiler.ProfileResult(function_name, execution_time, memory_before, memory_after, memory_peak, memory_allocated, memory_freed, tensors_created, tensors_deleted, call_count=1)[source]

Bases: object

Results from profiling a function or operation.

Parameters:
  • function_name (str)

  • execution_time (float)

  • memory_before (MemorySnapshot)

  • memory_after (MemorySnapshot)

  • memory_peak (MemorySnapshot)

  • memory_allocated (int)

  • memory_freed (int)

  • tensors_created (int)

  • tensors_deleted (int)

  • call_count (int)

function_name: str
execution_time: float
memory_before: MemorySnapshot
memory_after: MemorySnapshot
memory_peak: MemorySnapshot
memory_allocated: int
memory_freed: int
tensors_created: int
tensors_deleted: int
call_count: int = 1
memory_diff()[source]

Calculate memory difference between before and after.

Return type:

int

peak_memory_usage()[source]

Get peak memory usage during execution.

Return type:

int

to_dict()[source]

Convert result to dictionary.

Return type:

Dict[str, Any]

class stormlog.profiler.GPUMemoryProfiler(device=None, track_tensors=True, track_cpu_memory=True, collect_stack_traces=False)[source]

Bases: object

Comprehensive GPU memory profiler for PyTorch operations.

Parameters:
  • device (str | int | torch.device | None)

  • track_tensors (bool)

  • track_cpu_memory (bool)

  • collect_stack_traces (bool)

profile_function(func, *args, **kwargs)[source]

Profile a single function call.

Parameters:
  • func (Callable[[...], Any]) – Function to profile

  • *args (Any) – Arguments to pass to function

  • **kwargs (Any) – Keyword arguments to pass to function

Returns:

ProfileResult with profiling information

Return type:

ProfileResult

profile_context(name='context')[source]

Context manager for profiling a block of code.

Parameters:

name (str) – Name for the profiled context

Yields:

ProfileResult after the context exits

Return type:

Any

start_monitoring(interval=0.1)[source]

Start continuous memory monitoring.

Parameters:

interval (float) – Monitoring interval in seconds

Return type:

None

stop_monitoring()[source]

Stop continuous memory monitoring.

Return type:

None

get_summary()[source]

Get a summary of all profiling results.

Return type:

Dict[str, Any]

clear_results()[source]

Clear all profiling results and reset state.

Return type:

None

class stormlog.profiler.TensorTracker[source]

Bases: object

Tracks tensor creation and deletion for memory profiling.

count_tensors()[source]

Count current number of tracked tensors.

Return type:

int