utils module provides helper functions for formatting, system information, and memory analysis.
Functions
format_bytes()
Format bytes into human-readable format.Number of bytes to format
Decimal precision for the output
Formatted string (e.g., “1.25 GB”)
convert_bytes()
Convert between different byte units.Value to convert
Source unit: “B”, “KB”, “MB”, “GB”, or “TB”
Target unit: “B”, “KB”, “MB”, “GB”, or “TB”
Converted value
get_gpu_info()
Get comprehensive GPU information.GPU device to query (None for current device)
Dictionary with GPU information:
device_id: Device identifierdevice_name: GPU model namedevice_capability: CUDA compute capabilitytotal_memory: Total GPU memory in bytesmultiprocessor_count: Number of multiprocessorscuda_version: CUDA versionpytorch_version: PyTorch versionallocated_memory: Currently allocated memoryreserved_memory: Reserved memorymax_memory_allocated: Maximum allocated since resetmemory_stats: Detailed memory statisticsnvidia_smi_info: Additional info from nvidia-smi (if available)
get_system_info()
Get system information relevant to GPU profiling.System information including:
platform: Operating systemarchitecture: CPU architecturepython_version: Python version stringtorch_available: Whether PyTorch is availablecuda_available: Whether CUDA is availablerocm_available: Whether ROCm is availablerocm_version: ROCm version (if available)mps_available: Whether MPS backend is available (macOS)mps_built: Whether MPS is built into PyTorchdetected_backend: Auto-detected backend (cuda/rocm/mps/cpu)cuda_device_count: Number of CUDA devicescuda_version: CUDA versioncudnn_version: cuDNN versioncurrent_device: Current CUDA devicecpu_count: Physical CPU countcpu_count_logical: Logical CPU countmemory_total: Total system RAMmemory_available: Available system RAMmemory_percent: RAM usage percentage
check_memory_fragmentation()
Check GPU memory fragmentation.GPU device to check
Fragmentation analysis:
device_id: Device identifiertotal_memory: Total GPU memoryallocated_memory: Allocated memoryreserved_memory: Reserved memoryactive_memory: Active memoryinactive_memory: Inactive memoryfree_memory: Free memoryfragmentation_ratio: Fragmentation ratio (inactive/reserved)utilization_ratio: Memory utilization (allocated/total)reservation_ratio: Reservation ratio (reserved/total)waste_ratio: Wasted memory ratio*_formatted: Formatted versions of memory values
suggest_memory_optimization()
Suggest memory optimization strategies.Output from check_memory_fragmentation()
List of optimization suggestions based on memory state
memory_summary()
Generate a comprehensive memory summary.GPU device to summarize
Formatted memory summary string
MemoryContext
Context manager for tracking memory usage in a code block.Constructor
Name for this memory context
GPU device to track
Methods
get_summary()
Get memory usage summary for the context.Summary with:
name: Context namestart_memory: Starting memoryend_memory: Ending memorypeak_memory: Peak memory during contextmemory_diff: Net memory changepeak_memory_usage: Peak usage above baseline*_formatted: Human-readable versions