Memory Analysis¶
Probing provides comprehensive tools for analyzing memory usage in AI applications.
Overview¶
Memory issues are common in AI workloads, especially during training. Probing helps you:
- Track GPU and CPU memory allocation
- Detect memory leaks
- Analyze memory usage patterns
- Optimize memory efficiency
Quick Memory Check¶
# Get current memory state
probing $ENDPOINT eval "
import torch
import psutil
proc = psutil.Process()
print(f'CPU Memory: {proc.memory_info().rss / 1024**3:.2f} GB')
if torch.cuda.is_available():
print(f'GPU Allocated: {torch.cuda.memory_allocated() / 1024**3:.2f} GB')
print(f'GPU Cached: {torch.cuda.memory_reserved() / 1024**3:.2f} GB')
"
Memory Usage Trends¶
Track Memory Over Training Steps¶
SELECT
step,
avg(allocated) as avg_memory_mb,
max(allocated) as peak_memory_mb,
min(allocated) as min_memory_mb
FROM python.torch_trace
WHERE step IS NOT NULL
GROUP BY step
ORDER BY step;
Detect Memory Growth¶
SELECT
step,
max(allocated) - min(allocated) as memory_growth_mb
FROM python.torch_trace
WHERE step > (SELECT max(step) - 10 FROM python.torch_trace)
GROUP BY step
HAVING max(allocated) - min(allocated) > 50
ORDER BY step;
Memory by Module¶
Identify which model components use the most memory:
SELECT
module,
stage,
avg(allocated) as avg_memory,
count(*) as execution_count
FROM python.torch_trace
WHERE module IS NOT NULL
GROUP BY module, stage
ORDER BY avg_memory DESC
LIMIT 10;
Memory Leak Detection¶
Pattern: Monotonic Growth¶
WITH memory_trend AS (
SELECT
step,
max(allocated) as peak_memory,
LAG(max(allocated)) OVER (ORDER BY step) as prev_peak
FROM python.torch_trace
GROUP BY step
)
SELECT step, peak_memory, prev_peak,
peak_memory - prev_peak as growth
FROM memory_trend
WHERE peak_memory > prev_peak
ORDER BY step;
Force Garbage Collection¶
probing $ENDPOINT eval "
import gc
import torch
# Force garbage collection
gc.collect()
# Clear CUDA cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
print('CUDA cache cleared')
print(f'Garbage collected: {gc.get_count()}')
"
GPU Memory Analysis¶
Current GPU State¶
probing $ENDPOINT eval "
import torch
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
props = torch.cuda.get_device_properties(i)
allocated = torch.cuda.memory_allocated(i) / 1024**3
reserved = torch.cuda.memory_reserved(i) / 1024**3
total = props.total_memory / 1024**3
print(f'GPU {i}: {props.name}')
print(f' Total: {total:.2f} GB')
print(f' Allocated: {allocated:.2f} GB')
print(f' Reserved: {reserved:.2f} GB')
print(f' Free: {total - reserved:.2f} GB')
"
Memory by Operation Stage¶
SELECT
stage,
avg(allocated) as avg_allocated,
avg(max_allocated) as avg_peak,
avg(cached) as avg_cached
FROM python.torch_trace
WHERE step = (SELECT max(step) FROM python.torch_trace)
GROUP BY stage
ORDER BY avg_peak DESC;
Best Practices¶
1. Regular Memory Snapshots¶
Take periodic snapshots during training:
# Add to training loop
probing $ENDPOINT eval "
import torch
step = ... # current step
if step % 100 == 0:
allocated = torch.cuda.memory_allocated() / 1024**3
print(f'Step {step}: {allocated:.2f} GB')
"
2. Profile Memory-Intensive Operations¶
-- Find operations with highest memory variance
SELECT
module,
stage,
stddev(allocated) as memory_variance,
max(allocated) - min(allocated) as memory_range
FROM python.torch_trace
GROUP BY module, stage
HAVING stddev(allocated) > 10
ORDER BY memory_variance DESC;
3. Monitor Memory Fragmentation¶
probing $ENDPOINT eval "
import torch
if torch.cuda.is_available():
stats = torch.cuda.memory_stats()
print(f'Allocated blocks: {stats.get(\"allocated_bytes.all.current\", 0) / 1024**3:.2f} GB')
print(f'Reserved blocks: {stats.get(\"reserved_bytes.all.current\", 0) / 1024**3:.2f} GB')
"
Troubleshooting¶
Out of Memory (OOM)¶
- Check current memory state
- Identify memory-heavy modules
- Force garbage collection
- Reduce batch size or model size
Memory Not Released¶
- Check for circular references
- Verify tensors are not held in lists/dicts
- Use
delexplicitly for large tensors - Call
torch.cuda.empty_cache()
Next Steps¶
- SQL Analytics - More query patterns
- Debugging - Stack analysis techniques
- Troubleshooting - Common issues