import torch


def gpu_count(gpus: int) -> int:
    """
    Find the number of GPUs to use.

    By default, the application attempts to use all GPUs available in the
    system but users can specify a different number of GPUs if desired. If the
    system doesn't have any GPUs available, it will default to the CPUs. If the
    user specifies a specific number of GPUs, it needs to be verified that it
    is a positive integer and less than or equal to the total number of GPUs
    available in the system.

    Parameters
    ----------
    gpus : int
        An ``int`` of the number of GPUs the user requested via the CLI,
        defaulting to 0.

    Returns
    -------
    int
        Returns an ``int`` of the number of GPUs to use in the system.
    """
    count = torch.cuda.device_count()
    if not torch.cuda.is_available():
        print('No GPUs available. Running on CPUs instead.')
        return 0
    if gpus is None:
        return count
    if gpus <= 0:
        print('No GPUs requested. Running on CPUs instead.')
        return 0
    if gpus <= count:
        return gpus
    if gpus > count:
        print(f'Requested {gpus} GPUs but only found {count}')
        print(f'Using {count} GPUs instead')
        return count
    return 0