a
    d                     @   sR  d dl mZmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ ee ee dddZdeeeeee f  eeeee  d
ddZeeeee f eeee f dddZdee eeee d
ddZeeee eedf f eeeee  d
ddZdeeee dddZee ddddZeddddZdS )     )AnyListMutableSequenceOptionalTupleUnionN)TorchElasticEnvironment)MisconfigurationException)_DEVICE)gpusreturnc                 C   s>   | du rdS t | tstdt| dks2J d| d }|S )a  
    Args:
        gpus: Non-empty list of ints representing which GPUs to use

    Returns:
        Designated root GPU device id

    Raises:
        TypeError:
            If ``gpus`` is not a list
        AssertionError:
            If GPU list is empty
    NzGPUs should be a listr   zGPUs should be a non-empty list)
isinstancelist	TypeErrorlen)r   Zroot_gpu r   q/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/utilities/device_parser.py_determine_root_gpu_device   s    
r   F)r   include_cudainclude_mpsr   c                 C   s   t |  | du s2t| tr"| dks2t|  dv r6dS t| } t| ||d} | sXtdt	 rt
| dkrt
t||ddkr| S t|  t| ||dS )aH  
    Parses the GPU IDs given in the format as accepted by the
    :class:`~pytorch_lightning.trainer.Trainer`.

    Args:
        gpus: An int -1 or string '-1' indicate that all available GPUs should be used.
            A list of unique ints or a string containing a list of comma separated unique integers
            indicates specific GPUs to use.
            An int of 0 means that no GPUs should be used.
            Any int N > 0 indicates that GPUs [0..N) should be used.
        include_cuda: A boolean value indicating whether to include CUDA devices for GPU parsing.
        include_mps: A boolean value indicating whether to include MPS devices for GPU parsing.

    Returns:
        A list of GPUs to be used or ``None`` if no GPUs were requested

    Raises:
        MisconfigurationException:
            If no GPUs are available but the value of gpus variable indicates request for GPUs

    .. note::
        ``include_cuda`` and ``include_mps`` default to ``False`` so that you only
        have to specify which device type to use and all other devices are not disabled.
    Nr   )0z[]r   r   z&GPUs requested but none are available.   )_check_data_typer   intstrstrip!_normalize_parse_gpu_string_input"_normalize_parse_gpu_input_to_listr	   r   detectr   _get_all_available_gpus_check_unique_sanitize_gpu_idsr   r   r   r   r   r   _parse_gpu_ids2   s     *
r$   )sr   c                 C   sB   t | ts| S | dkrdS d| v r6dd | dD S t|  S )Nz-1,c                 S   s$   g | ]}t |d krt| qS )r   )r   r   r   ).0xr   r   r   
<listcomp>q       z5_normalize_parse_gpu_string_input.<locals>.<listcomp>)r   r   splitr   r   )r%   r   r   r   r   k   s    
r   c                 C   sN   t ||fdkrtdt||d}| D ] }||vr(td|  d| q(| S )ar  Checks that each of the GPUs in the list is actually available. Raises a MisconfigurationException if any of
    the GPUs is not available.

    Args:
        gpus: List of ints corresponding to GPU indices

    Returns:
        Unmodified gpus variable

    Raises:
        MisconfigurationException:
            If machine has fewer available GPUs than requested.
    r   z*At least one gpu type should be specified!r   zYou requested gpu: z
 But your machine only has: )sum
ValueErrorr    r	   )r   r   r   Zall_available_gpusZgpur   r   r   r"   u   s    r"   .c                 C   sJ   | d usJ t | ttfr"t| S | s*d S | dkr>t||dS tt| S )Nr&   r   )r   r   tupler   r    ranger#   r   r   r   r      s    r   )r   r   r   c                 C   s,   | rt j ng }|r t j ng }|| S )z7
    Returns:
        A list of all available GPUs
    )acceleratorsZcudaZ_get_all_visible_cuda_devicesZmpsZ_get_all_available_mps_gpus)r   r   Z	cuda_gpusZmps_gpusr   r   r   r       s    r    )
device_idsr   c                 C   s    t | t t| krtddS )zChecks that the device_ids are unique.

    Args:
        device_ids: List of ints corresponding to GPUs indices

    Raises:
        MisconfigurationException:
            If ``device_ids`` of GPUs aren't unique
    z!Device ID's (GPU) must be unique.N)r   setr	   )r2   r   r   r   r!      s    
r!   c                 C   s~   d}| du rdS t | ttfrP| D ]*}t|tur"t| dt|j dq"n*t| ttfvrzt| dt| j ddS )aQ  Checks that the device_ids argument is one of the following: None, int, string, or sequence of integers.

    Args:
        device_ids: gpus/tpu_cores parameter as passed to the Trainer

    Raises:
        MisconfigurationException:
            If ``device_ids`` of GPU/TPUs aren't ``int``, ``str``, sequence of ``int`` or ``None``
    zYDevice IDs (GPU/TPU) must be an int, a string, a sequence of ints or None, but you passedNz a sequence of . )r   r   r/   typer   r	   __name__r   )r2   msgZid_r   r   r   r      s    
r   )FF)FF)FF)typingr   r   r   r   r   r   Zlightning_fabric.acceleratorsr1   Z2lightning_fabric.plugins.environments.torchelasticr   Z%lightning_fabric.utilities.exceptionsr	   Z lightning_fabric.utilities.typesr
   r   r   r   boolr$   r   r"   r   r    r!   r   r   r   r   r   <module>   s*      
9*


