|
import torch |
|
import gc |
|
|
|
class GPUOptimizer: |
|
def __init__(self): |
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
def optimize(self): |
|
if torch.cuda.is_available(): |
|
|
|
torch.cuda.empty_cache() |
|
gc.collect() |
|
|
|
|
|
torch.cuda.set_per_process_memory_fraction(0.9) |
|
|
|
|
|
torch.backends.cuda.matmul.allow_tf32 = True |
|
torch.backends.cudnn.allow_tf32 = True |
|
|
|
|
|
torch.cuda.amp.autocast(enabled=True) |
|
|
|
def get_memory_usage(self): |
|
if torch.cuda.is_available(): |
|
return { |
|
'allocated': torch.cuda.memory_allocated() / 1024**2, |
|
'reserved': torch.cuda.memory_reserved() / 1024**2 |
|
} |
|
return {'allocated': 0, 'reserved': 0} |