from typing import Optional
import torch


def resolve_device(device: Optional[torch.device]):
    """
    Resolve the torch device.

    :param device:
        The device.

    :return:
        If device is not None: the device
        Else: cuda if available, else cpu
    """
    if device is None:
        device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
    return device