Spaces:
Sleeping
Sleeping
Create optimize_memory.py
Browse files- optimize_memory.py +36 -0
optimize_memory.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Script opcional para optimizar la memoria antes de ejecutar la aplicaci贸n.
|
| 3 |
+
Ejecuta este script antes de iniciar la aplicaci贸n si experimentas problemas de memoria.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gc
|
| 7 |
+
import os
|
| 8 |
+
import torch
|
| 9 |
+
import psutil
|
| 10 |
+
|
| 11 |
+
def optimize_memory():
|
| 12 |
+
print("Optimizando memoria del sistema...")
|
| 13 |
+
|
| 14 |
+
# Forzar recolecci贸n de basura de Python
|
| 15 |
+
gc.collect()
|
| 16 |
+
|
| 17 |
+
# Liberar cach茅 de CUDA si est谩 disponible
|
| 18 |
+
if torch.cuda.is_available():
|
| 19 |
+
torch.cuda.empty_cache()
|
| 20 |
+
print("Cach茅 de CUDA liberada")
|
| 21 |
+
|
| 22 |
+
# Informaci贸n sobre memoria disponible
|
| 23 |
+
memory = psutil.virtual_memory()
|
| 24 |
+
print(f"Memoria total: {memory.total / (1024**3):.2f} GB")
|
| 25 |
+
print(f"Memoria disponible: {memory.available / (1024**3):.2f} GB")
|
| 26 |
+
print(f"Porcentaje usado: {memory.percent}%")
|
| 27 |
+
|
| 28 |
+
# Configurar variables de entorno para reducir uso de memoria
|
| 29 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
| 30 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 31 |
+
|
| 32 |
+
print("Optimizaci贸n de memoria completada")
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
optimize_memory()
|
| 36 |
+
|