torch.memory_summary
function memory_summary(): voidReturns a human-readable summary of WebGPU memory usage.
Generates a formatted string representation of GPU memory statistics, useful for quickly understanding memory consumption during development, debugging, and optimization. Displays active memory (tensors in use), pooled memory (cached but unused), peak memory (maximum ever allocated), and total allocation count. Perfect for:
- Monitoring GPU memory in training loops
- Debugging memory leaks or excessive memory usage
- Optimization work (comparing memory before/after changes)
- Profiling and performance analysis
- Including in log files for reproducibility
- Format: Returns a pre-formatted string ready for console output or logging
- Automatic formatting: Converts bytes to MB automatically (divide by 1024²)
- Quick reference: Easier than manually parsing memory_stats() for debugging
- No overhead: Fast operation, safe to call frequently
Not guaranteed consistent formatting: The exact formatting may change between versions
Returns
Formatted string with memory summary in human-readable format (MB units)
Examples
// Print memory summary during training
console.log(torch.webgpu.memory_summary());
// Output:
// GPU Memory Summary
// -------------------------------------------------------
// Active Memory: 128.50 MB
// Pooled Memory: 64.25 MB
// Peak Memory: 256.75 MB
// Total Allocs: 1542
// -------------------------------------------------------
// Use in training loop to monitor memory usage
for (let epoch = 0; epoch < 10; epoch++) {
console.log(`Epoch ${epoch}:`);
console.log(torch.webgpu.memory_summary());
trainEpoch();
torch.webgpu.empty_cache();
}
// Compare memory before and after optimization
console.log('Memory before optimization:');
console.log(torch.webgpu.memory_summary());
optimizeModel();
console.log('Memory after optimization:');
console.log(torch.webgpu.memory_summary());See Also
- PyTorch torch.cuda.memory_summary()
- memory_stats - Get raw memory statistics as numbers for programmatic use
- empty_cache - Free pooled memory to reduce memory usage
- reset_peak_memory_stats - Reset peak memory counter for profiling specific code sections