|
|
|
|
|
import functools |
|
import torch |
|
import threading |
|
import time |
|
|
|
class spaces: |
|
@staticmethod |
|
def GPU(duration=0): |
|
def decorator(func): |
|
@functools.wraps(func) |
|
def wrapper(*args, **kwargs): |
|
try: |
|
|
|
if torch.cuda.is_available(): |
|
device = torch.device('cuda') |
|
print("GPU disponible. Ejecutando en GPU.") |
|
else: |
|
raise RuntimeError("No se detecta la GPU. Aseg煤rate de que los drivers CUDA est谩n instalados y configurados correctamente.") |
|
|
|
|
|
kwargs['device'] = device |
|
|
|
result = [None] |
|
exception = [None] |
|
|
|
def target(): |
|
try: |
|
result[0] = func(*args, **kwargs) |
|
except Exception as e: |
|
exception[0] = e |
|
|
|
|
|
thread = threading.Thread(target=target) |
|
thread.start() |
|
thread.join(duration) |
|
|
|
if thread.is_alive(): |
|
raise TimeoutError(f"La ejecuci贸n de la funci贸n excedi贸 {duration} segundos.") |
|
if exception[0]: |
|
raise exception[0] |
|
return result[0] |
|
except RuntimeError as e: |
|
print(f"Error: {str(e)}") |
|
raise e |
|
return wrapper |
|
return decorator |
|
|