
      i                        d dl mZ d dl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Z	 	 ddeeeee
e   f      d	ed
edee
e      fdZdeeee
e   f   deee
e   f   fdZdde
e   d	ed
ede
e   fdZdeee
e   eedf   f   d	ed
edee
e      fdZdd	ed
ede
e   fdZde
e   ddfdZdeddfdZdefdZy)    )MutableSequence)OptionalUnionN)MisconfigurationException)_DEVICEgpusreturnc                 r    | yt        | t              st        d      t        |       dkD  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   s    }/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning_fabric/utilities/device_parser.py_determine_root_gpu_devicer      sC     |dD!/00t9q=;;;= 7N    include_cudainclude_mpsc                    t        |        | 0t        | t              r| dk(  st        |       j	                         dv ryt        |       } t        | ||      } | st        d      t        j                  j                         rGt        j                  j                         r)t        |       dk7  rt        t        ||            dk(  r| S t        |        t        | ||      S )aH  Parses the GPU IDs given in the format as accepted by the :class:`~pytorch_lightning.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   s      r   _parse_gpu_idsr&   1   s    : T |
4-$!)D	@QU`@` -T2D-d[fgD'(PQQ 	&&(668IN'\{[\`aa  $T+VVr   sc                     t        | t              s| S | dk(  ryd| v rE| j                  d      D cg c]*  }t        |      dkD  st	        |j                               , c}S t	        | j                               S c c}w )Nz-1,r   )r   r   splitr   r   r   )r'   xs     r   r   r   j   si    aDy
ax()C1A
AGGICCqwwy> Ds   A;A;c                     t        ||f      dk(  rt        d      t        ||      }| D ]  }||vst        d|  d|        | 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gpus        r   r$   r$   t   sj     L+&'1,EFF0lXcd((+%dV+IJ\I]^  
 Kr   .c                     | J t        | t        t        f      rt        |       S | sy | dk(  rt	        ||      S t        t        |             S )Nr)   r   )r   r   tupler   r"   ranger%   s      r   r   r      sT     $%01Dz rz&LkZZdr   c                 P    ddl m} ddlm} | r |       ng }|r |       ng }||z   S )z7
    Returns:
        A list of all available GPUs
    r   )_get_all_visible_cuda_devices)_get_all_available_mps_gpus)"lightning_fabric.accelerators.cudar6   !lightning_fabric.accelerators.mpsr7   )r   r   r6   r7   	cuda_gpusmps_gpuss         r   r"   r"      s0    
 QM3?-/RI0;*,Hxr   
device_idsc                 Z    t        |       t        t        |             k7  rt        d      y)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   )r<   s    r   r#   r#      s*     :#c*o..'(KLL /r   c                 "   d}| t        | d      t        | t        t        f      r?| D ]9  }t	        |      }|t
        ust        | dt	        |      j                   d       yt	        |       t
        t        fvrt        | d| d      y)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   r3   typer   __name__r   )r<   msgid_id_types       r   r   r      s     ^C3%u&&*67C3iGc!3%tCy7I7I6J! LMM  
j	#s	+3%qa011 
,r   c                      ddl m}  ddlm} ddlm} |j                         ry|j                         ry| j                         ryy)	z8Choose the accelerator type (str) based on availability.r   )CUDAAccelerator)MPSAccelerator)XLAAcceleratortpumpscudacpu)r8   rH   r9   rI   !lightning_fabric.accelerators.xlarJ   r    )rH   rI   rJ   s      r   _select_auto_acceleratorrP      s;    B@@""$""$##%r   )FF)collections.abcr   typingr   r   r   %lightning_fabric.utilities.exceptionsr    lightning_fabric.utilities.typesr   r   r   r   r   boolr&   r   r$   r3   r   r"   r#   objectr   rP    r   r   <module>rX      s   , "  K 4T'] x7H 8 6W
5c49,-
.6W6W 6W d3i	6WrsCc/B)C cSWX[S\nH] DI T PT aefiaj 4
T#Yc3h/
0@DSWd3i 
 $ 
 T 
 VZ[^V_ 
 Md3i MD M2 2D 2.# r   