o
    9wi                     @   s`  d dl mZmZmZmZmZ d dlZd dlmZ d dl	m
Z
 dee
 dee
 fddZ				ddeeeeee f  d
ededeee  fddZdeeeee f deeee f fddZddee d
ededee fddZdeeee eedf f d
ededeee  fddZdd
ededee fddZdee ddfddZdeddfddZdS )    )ListMutableSequenceOptionalTupleUnionN)MisconfigurationException)_DEVICEgpusreturnc                 C   s:   | du rdS t | tstdt| dksJ 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	    r   e/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/lightning/fabric/utilities/device_parser.py_determine_root_gpu_device   s   
r   Finclude_cudainclude_mpsc                 C   s   t |  | du st| tr| dkst|  dv rdS t| } t| ||d} | s,tdtj	
 rHtj	 rHt| dkrHtt||ddkrH| S t|  t| ||dS )aH  Parses the GPU IDs given in the format as accepted by the :class:`~lightning.pytorch.trainer.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   torchdistributedis_availableis_torchelastic_launchedr   _get_all_available_gpus_check_unique_sanitize_gpu_idsr	   r   r   r   r   r   _parse_gpu_ids0   s    *r%   sc                 C   sB   t | ts| S | dkrdS d| v rd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>o   s   $ z5_normalize_parse_gpu_string_input.<locals>.<listcomp>)r   r   splitr   r   )r&   r   r   r   r   i   s   
r   c                 C   sN   t ||fdkrtdt||d}| D ]}||vr$td|  d| q| S )as  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   all_available_gpusgpur   r   r   r#   s   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   c                 C   s<   ddl m} ddlm} | r| ng }|r| ng }|| S )z7
    Returns:
        A list of all available GPUs
    r   )_get_all_visible_cuda_devices)_get_all_available_mps_gpus)"lightning.fabric.accelerators.cudar3   !lightning.fabric.accelerators.mpsr4   )r   r   r3   r4   	cuda_gpusmps_gpusr   r   r   r!      s
   r!   
device_idsc                 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   )r9   r   r   r   r"      s   r"   c                 C   s   d}| du rt | dt| ttfr0| D ]}t|}|tur-t | dt|j dqdS t| ttfvrBt | d| ddS )a3  Checks that the device_ids argument is one of the following: int, string, or sequence of integers.

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

    Raises:
        TypeError:
            If ``device_ids`` of GPU/TPUs aren't ``int``, ``str`` or sequence of ``int```

    zQDevice IDs (GPU/TPU) must be an int, a string, a sequence of ints, but you passedNz Nonez a sequence of . )r   r   r   r1   typer   __name__r   )r9   msgid_id_typer   r   r   r      s   r   )FF)typingr   r   r   r   r   r   %lightning.fabric.utilities.exceptionsr    lightning.fabric.utilities.typesr   r   r   r   boolr%   r   r#   r   r!   r"   objectr   r   r   r   r   <module>   s<   

,9$


