import torch


def get_devices_spec(devices=None):
    """
    Get a valid specification for one or more devices. If `devices` is None get devices for all CUDA devices available.
    If `devices` is and zero-length structure a single CPU compute device is returned. In any other cases `devices` is
    returned unchanged.

    Args:
        devices (list, optional): list of devices to request, None for all GPU devices, [] for CPU.

    Returns:
        list of torch.device: list of devices.
    """
    if devices is None:
        devices = [torch.device('cuda:%i' % d) for d in range(torch.cuda.device_count())]

        if len(devices) == 0:
            raise ValueError("No GPU devices available")

    elif len(devices) == 0:
        devices = [torch.device("cpu")]

    return devices