
      iC                        d dl Z d dlmZmZ d dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ  G d d	e
      Z ed
d      Z ed      Z ed      ZdefdZdeeeee   f   deeee   f   fdZdeddfdZdedeeee   f   fdZy)    N)AnyUnion)RequirementCache)override)Accelerator)_AcceleratorRegistry)_check_data_typec            	           e Zd ZdZdededdf fdZedej                  ddfd       Z	edd	       Z
eed
eeeee   f   deeee   f   fd              Zeed
eeee   f   deej                     fd              Zee ej&                  d      defd                     Zee ej&                  d      defd                     Zeedefd              Zeededdfd              Z xZS )XLAAcceleratorzAccelerator for XLA devices, normally TPUs.

    .. warning::  Use of this accelerator beyond import and instantiation is experimental.

    argskwargsreturnNc                     t         st        t        t                     t               st	        d      t        |   |i | y )Nz-The XLA XRT runtime is not supported anymore.)_XLA_AVAILABLEModuleNotFoundErrorstr_using_pjrtRuntimeErrorsuper__init__)selfr   r   	__class__s      v/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/accelerators/xla.pyr   zXLAAccelerator.__init__!   s9    %c.&9::}NOO$)&)    devicec                      y N )r   r   s     r   setup_devicezXLAAccelerator.setup_device(       r   c                      y r   r   )r   s    r   teardownzXLAAccelerator.teardown,   r    r   devicesc                     t        |       S )z!Accelerator device parsing logic.)_parse_tpu_devicesr#   s    r   parse_deviceszXLAAccelerator.parse_devices0   s     "'**r   c                     t        |       } t        | t              r-t        |       D cg c]  }t	        j
                  d|       c}S t	        j
                  d| d         gS c c}w )z*Gets parallel devices for the Accelerator.xlar   )r%   
isinstanceintrangetorchr   )r#   is     r   get_parallel_devicesz#XLAAccelerator.get_parallel_devices6   sZ     %W-gs#49'NCNqELL*NCCUGAJ/00 Ds   A#   )maxsizec                      t         syt        rddlm}  | j	                         S ddlm}  dddd}|j                  | j                         d      S )z!Get the devices when set to auto.r   )tpu      )      r5   )r   _XLA_GREATER_EQUAL_2_1torch_xla._internalr3   num_available_devicestorch_xla.experimentalgetversion)r3   device_count_on_versions     r   auto_device_countz XLAAccelerator.auto_device_countC   sH     !/,,...&'A!"4&**3;;=!<<r   c                  f    	 t         j                         dkD  S # t        t        t        f$ r Y yw xY w)Nr   F)r   r?   
ValueErrorAssertionErrorOSErrorr   r   r   is_availablezXLAAccelerator.is_availableU   s6    	!33599NG4 	 	s    00c                       y)Nr3   r   r   r   r   namezXLAAccelerator.name`   s     r   accelerator_registryc                 \    |j                  | j                         | | j                         y )N)description)registerrF   __name__)clsrG   s     r   register_acceleratorsz$XLAAccelerator.register_acceleratorse   s*     	%%HHJ 	& 	
r   )r   N)rK   
__module____qualname____doc__r   r   r   r-   r   r   r"   staticmethodr   r+   r   listr'   r/   	functools	lru_cacher?   boolrD   rF   classmethodr   rM   __classcell__)r   s   @r   r   r      s   *c *S *T * 5<< D     +uS#tCy%89 +eCcN>S +  + 1eCcN&; 1U\\@R 1  1  Y#=s = $  
= Y#$  $   #    
9M 
RV 
  
r   r   ztorch_xla>=1.13	torch_xlaztorch_xla>=2.1ztorch_xla>=2.5r   c                      t         rddlm}  | j                         d uS t        rddlm}  | j                         S ddlm} |j                         S )Nr   )runtime)pjrt)_XLA_GREATER_EQUAL_2_5rX   rZ   device_typer8   
using_pjrtr;   r[   )xrr[   s     r   r   r   u   s<    +~~t+++}}+??r   r#   c                 h    t        |        t        | t              rt        |       } t	        |        | S )a  Parses the TPU devices given in the format as accepted by the
    :class:`~lightning.pytorch.trainer.trainer.Trainer` and :class:`~lightning.fabric.Fabric`.

    Args:
        devices: An int of 1 or string '1' indicates that 1 core with multi-processing should be used
            An int 8 or string '8' indicates that all 8 cores with multi-processing should be used
            A single element list of int or string can be used to indicate the specific TPU core to use.

    Returns:
        A list of tpu cores to be used.

    )r	   r*   r   _parse_tpu_devices_str_check_tpu_devices_validr&   s    r   r%   r%      s-     W'3(1W%Nr   c                     t         j                         }t        | t              r| d|hv s8t        | t        t
        f      r#t        |       dk(  rd| d   cxk  r|dz
  k  ry  ny t        d| d|dz
   d|       )Nr0   r   z!`devices` can only be 'auto', 1, z or [<0-z>] for TPUs. Got )r   r?   r*   r+   rR   tuplelenrA   )r#   device_counts     r   rb   rb      s    !335L 	7C <((ge}-LA/|a// 0

+L>,QRBRASSdeldop r   c                 &   | j                         } 	 t        |       S # t        $ rj 	 | j                  d      D cg c]*  }t	        |      dkD  st        |j                               , nc c}w c}cY S # t        $ r t        d|       w xY ww xY w)N,r   z*Could not parse the selected TPU devices: )stripr+   rA   splitre   )r#   xs     r   ra   ra      s    mmoGW7| W	W,3MM#,>M,>q#a&1*C	N,>MM 	WI'UVV	WWs1   
 	BA4A*A*)A41B4BB)rS   typingr   r   r-    lightning_utilities.core.importsr   typing_extensionsr   )lightning.fabric.accelerators.acceleratorr   &lightning.fabric.accelerators.registryr   (lightning.fabric.utilities.device_parserr	   r   r   r8   r\   rU   r   r+   r   rR   r%   objectrb   ra   r   r   r   <module>rs      s       = & A G ER
[ R
l ""3[A)*:; )*:; T "c3S	&9 : uS$s)^?T (f  "WC WE#tCy.,A Wr   