-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_in_GPU.py
More file actions
59 lines (46 loc) · 1.9 KB
/
run_in_GPU.py
File metadata and controls
59 lines (46 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 22 13:29:53 2026
@author: anafd
"""
import tensorflow as tf
import os
import runpy
import sys
def configurar_gpu():
"""Configura la GPU para evitar errores de memoria en Windows."""
# Asegurar que TensorFlow vea la GPU (0 es la primera tarjeta)
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# Listar dispositivos físicos
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
# IMPORTANTE: Habilitar el crecimiento de memoria.
# Esto evita que TF acapare toda la VRAM y crashee el sistema.
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
detalles = tf.config.experimental.get_device_details(gpus[0])
nombre_gpu = detalles.get('device_name', 'GPU desconocida')
print(f"✅ GPU Detectada y Configurada: {nombre_gpu}")
print(f"ℹ️ Modo Memory Growth: ACTIVADO")
except RuntimeError as e:
# El crecimiento de memoria debe configurarse antes de inicializar tensores
print(f"❌ Error configurando GPU: {e}")
else:
print("⚠️ No se detectó GPU. TensorFlow usará la CPU.")
def ejecutar_servidor():
script_objetivo = 'main_server.py'
if not os.path.exists(script_objetivo):
print(f"❌ Error: No encuentro el archivo '{script_objetivo}' en este directorio.")
return
print(f"\n🚀 Iniciando {script_objetivo}...\n" + "-"*30)
# run_path ejecuta el script como si lo hubieras llamado directamente
try:
runpy.run_path(script_objetivo, run_name="__main__")
except KeyboardInterrupt:
print("\n🛑 Ejecución detenida por el usuario.")
except Exception as e:
print(f"\n❌ Ocurrió un error en la ejecución: {e}")
if __name__ == "__main__":
configurar_gpu()
ejecutar_servidor()