from typing import Any
import torch


def get_valid_device(device: Any) -> str:
    """Checks tensor device input for validity, defaults to 'cpu' for input 'None'

    :param device: Tensor device to be checked
    :return: Valid tensor device, either 'cpu' (default for input 'None') or 'cuda'
    """
    if device is None:
        device = 'cpu'
    else:
        if device not in ['cpu', 'cuda']:
            raise ValueError("Error setting tensor device: Input is not 'cpu' or 'cuda', but {}".format(device))
        if device == 'cuda' and not torch.cuda.is_available():
            raise ValueError("Error setting tensor device: Input is 'cuda', but cuda is not available")
    return device