Create optimize.py
Browse files- optimize.py +24 -0
optimize.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch.utils.cpp_extension import CUDA_HOME
|
3 |
+
|
4 |
+
def optimize_model(model):
|
5 |
+
"""Apply various optimizations"""
|
6 |
+
# Mixed precision
|
7 |
+
model.half()
|
8 |
+
|
9 |
+
# CUDA optimizations
|
10 |
+
if torch.cuda.is_available():
|
11 |
+
model = model.to('cuda')
|
12 |
+
torch.backends.cudnn.benchmark = True
|
13 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
14 |
+
|
15 |
+
# Compile with torch.compile (PyTorch 2.0+)
|
16 |
+
if hasattr(torch, 'compile'):
|
17 |
+
model = torch.compile(model, mode="reduce-overhead")
|
18 |
+
|
19 |
+
return model
|
20 |
+
|
21 |
+
def memory_optimization():
|
22 |
+
"""Memory optimization techniques"""
|
23 |
+
torch.cuda.empty_cache()
|
24 |
+
torch.backends.cudnn.deterministic = False
|