import torch


def get_cuda_device_if_available(index: int = 0) -> torch.device:
    """Tries to get cuda device, if fail, returns cpu.

    Args:
        index: cuda device index

    Returns:
        torch.device
    """
    try:
        if torch.cuda.is_available():
            dev = torch.device(f'cuda:{index}')
        else:
            dev = torch.device('cpu')
    except BaseException as e:  # noqa: F841
        dev = torch.device('cpu')
    return dev