Update decorators.py
Browse files- decorators.py +30 -18
decorators.py
CHANGED
@@ -7,30 +7,42 @@ import time
|
|
7 |
|
8 |
class spaces:
|
9 |
@staticmethod
|
10 |
-
def GPU(duration=
|
11 |
def decorator(func):
|
12 |
@functools.wraps(func)
|
13 |
def wrapper(*args, **kwargs):
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
result[0] = func(*args, **kwargs)
|
23 |
-
except Exception as e:
|
24 |
-
exception[0] = e
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
return wrapper
|
36 |
return decorator
|
|
|
7 |
|
8 |
class spaces:
|
9 |
@staticmethod
|
10 |
+
def GPU(duration=0):
|
11 |
def decorator(func):
|
12 |
@functools.wraps(func)
|
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
|