Update decorators.py
Browse files- decorators.py +10 -46
decorators.py
CHANGED
|
@@ -1,48 +1,12 @@
|
|
| 1 |
# decorators.py
|
| 2 |
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def wrapper(*args, **kwargs):
|
| 14 |
-
try:
|
| 15 |
-
# Verifica si la GPU est谩 disponible, si no lanza un error controlado
|
| 16 |
-
if torch.cuda.is_available():
|
| 17 |
-
device = torch.device('cuda')
|
| 18 |
-
print("GPU disponible. Ejecutando en GPU.")
|
| 19 |
-
else:
|
| 20 |
-
raise RuntimeError("No se detecta la GPU. Aseg煤rate de que los drivers CUDA est谩n instalados y configurados correctamente.")
|
| 21 |
-
|
| 22 |
-
# Pasar el dispositivo a la funci贸n como argumento
|
| 23 |
-
kwargs['device'] = device
|
| 24 |
-
|
| 25 |
-
result = [None]
|
| 26 |
-
exception = [None]
|
| 27 |
-
|
| 28 |
-
def target():
|
| 29 |
-
try:
|
| 30 |
-
result[0] = func(*args, **kwargs)
|
| 31 |
-
except Exception as e:
|
| 32 |
-
exception[0] = e
|
| 33 |
-
|
| 34 |
-
# Ejecutar la funci贸n en un hilo separado
|
| 35 |
-
thread = threading.Thread(target=target)
|
| 36 |
-
thread.start()
|
| 37 |
-
thread.join(duration)
|
| 38 |
-
|
| 39 |
-
if thread.is_alive():
|
| 40 |
-
raise TimeoutError(f"La ejecuci贸n de la funci贸n excedi贸 {duration} segundos.")
|
| 41 |
-
if exception[0]:
|
| 42 |
-
raise exception[0]
|
| 43 |
-
return result[0]
|
| 44 |
-
except RuntimeError as e:
|
| 45 |
-
print(f"Error: {str(e)}")
|
| 46 |
-
raise e # Lanzar el error de GPU para que se maneje adecuadamente
|
| 47 |
-
return wrapper
|
| 48 |
-
return decorator
|
|
|
|
| 1 |
# decorators.py
|
| 2 |
|
| 3 |
+
import spaces
|
| 4 |
+
|
| 5 |
+
# Decorador personalizado que usa GPU si est谩 disponible
|
| 6 |
+
def gpu_decorator(duration=100):
|
| 7 |
+
def decorator(func):
|
| 8 |
+
@spaces.GPU(duration=duration)
|
| 9 |
+
def wrapper(*args, **kwargs):
|
| 10 |
+
return func(*args, **kwargs)
|
| 11 |
+
return wrapper
|
| 12 |
+
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|