
      i                         d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ  ej                  e      Z G d de      Zy)    N)Any)override)_XLA_AVAILABLE_XLA_GREATER_EQUAL_2_1XLAAccelerator)ClusterEnvironmentc                       e Zd ZdZdededdf fdZeedefd              Z	eede
fd              Zeedefd	              Zeedefd
              Ze ej"                  d      defd              Zededdfd       Ze ej"                  d      defd              Zededdfd       Ze ej"                  d      defd              Ze ej"                  d      defd              Z xZS )XLAEnvironmenta  Cluster environment for training on a TPU Pod with the `PyTorch/XLA <https://pytorch.org/xla>`_ library.

    A list of environment variables set by XLA can be found
    `here <https://github.com/pytorch/xla/blob/master/torch_xla/core/xla_env_vars.py>`_.

    argskwargsreturnNc                 `    t         st        t        t                     t        |   |i | y N)r   ModuleNotFoundErrorstrsuper__init__)selfr   r   	__class__s      ~/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/plugins/environments/xla.pyr   zXLAEnvironment.__init__"   s(    %c.&9::$)&)    c                      y)NF r   s    r   creates_processes_externallyz+XLAEnvironment.creates_processes_externally'   s     r   c                     t         r   NotImplementedErrorr   s    r   main_addresszXLAEnvironment.main_address,   
     "!r   c                     t         r   r   r   s    r   	main_portzXLAEnvironment.main_port2   r    r   c                  *    t        j                         S r   )r   is_availabler   r   r   detectzXLAEnvironment.detect8   s     **,,r      )maxsizec                 l    t         rddlm} |j                         S ddlmc m} |j                         S )znThe number of processes across all devices and hosts.

        The output is cached for performance.

        r   runtimeN)r   	torch_xlar*   
world_sizetorch_xla.core.xla_modelcore	xla_modelxrt_world_sizer   xrxms      r   r,   zXLAEnvironment.world_size=   s+     "/==?"--  ""r   sizec                 .    t         j                  d       y )NzYXLAEnvironment.set_world_size was called, but setting world size is not allowed. Ignored.logdebug)r   r4   s     r   set_world_sizezXLAEnvironment.set_world_sizeN   s    		mnr   c                 l    t         rddlm} |j                         S ddlmc m} |j                         S )zThe rank (index) of the currently running process across all host and devices.

        The output is cached for performance.

        r   r)   N)r   r+   r*   global_ordinalr-   r.   r/   get_ordinalr1   s      r   global_rankzXLAEnvironment.global_rankR   s,     "/$$&&--~~r   rankc                 .    t         j                  d       y )Nz[XLAEnvironment.set_global_rank was called, but setting global rank is not allowed. Ignored.r6   )r   r>   s     r   set_global_rankzXLAEnvironment.set_global_rankc   s    		opr   c                 l    t         rddlm} |j                         S ddlmc m} |j                         S )zThe rank (index) of the currently running process inside of the current host.

        The output is cached for performance.

        r   r)   N)r   r+   r*   local_ordinalr-   r.   r/   get_local_ordinalr1   s      r   
local_rankzXLAEnvironment.local_rankg   s.     "/##%%--##%%r   c                     t         rddlm} |j                         S ddlmc m} ddlm}  ||j                  t        d      S )zxThe rank (index) of the host on which the current process runs.

        The output is cached for performance.

        r   r)   N)	getenv_as)r   r+   r*   
host_indextorch_xla.core.xla_env_varsr.   xla_env_varstorch_xla.utils.utilsrF   HOST_ORDINALint)r   r2   xenvrF   s       r   	node_rankzXLAEnvironment.node_rankx   s4     "/==?"223**C33r   )__name__
__module____qualname____doc__r   r   propertyr   boolr   r   r   rL   r"   staticmethodr%   	functools	lru_cacher,   r9   r=   r@   rD   rN   __classcell__)r   s   @r   r
   r
      s   *c *S *T *
 d    "c "  " "3 "  " -D -  - Y##C # $ # o3 o4 o o Y# S   $   qC qD q q Y#&C & $ & Y#43 4 $ 4r   r
   )rV   loggingtypingr   typing_extensionsr   !lightning.fabric.accelerators.xlar   r   r   9lightning.fabric.plugins.environments.cluster_environmentr   	getLoggerrO   r7   r
   r   r   r   <module>r_      s=       & d d Xg!m4' m4r   