o
    zi5                     @   sf   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eZG dd deZdS )    N)Any)override)_XLA_AVAILABLE_XLA_GREATER_EQUAL_2_1XLAAccelerator)ClusterEnvironmentc                       s*  e Zd ZdZdededdf fddZeedefdd	Z	eede
fd
dZeedefddZeedefddZeejdddefddZededdfddZeejdddefddZededdfddZeejdddefddZeejdddefddZ  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                    s&   t sttt t j|i | d S N)r   ModuleNotFoundErrorstrsuper__init__)selfr	   r
   	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/lightning_fabric/plugins/environments/xla.pyr   "   s   zXLAEnvironment.__init__c                 C   s   dS )NFr   r   r   r   r   creates_processes_externally'   s   z+XLAEnvironment.creates_processes_externallyc                 C      t r   NotImplementedErrorr   r   r   r   main_address,      zXLAEnvironment.main_addressc                 C   r   r   r   r   r   r   r   	main_port2   r   zXLAEnvironment.main_portc                   C   s   t  S r   )r   is_availabler   r   r   r   detect8   s   zXLAEnvironment.detect   )maxsizec                 C      ddl m  m} | S )znThe number of processes across all devices and hosts.

        The output is cached for performance.

        r   N)torch_xla.core.xla_modelcore	xla_modelxrt_world_sizer   xmr   r   r   
world_size=      zXLAEnvironment.world_sizesizec                 C      t d d S )NzYXLAEnvironment.set_world_size was called, but setting world size is not allowed. Ignored.logdebug)r   r+   r   r   r   set_world_sizeI      zXLAEnvironment.set_world_sizec                 C   r"   )zThe rank (index) of the currently running process across all host and devices.

        The output is cached for performance.

        r   N)r#   r$   r%   get_ordinalr'   r   r   r   global_rankM   r*   zXLAEnvironment.global_rankrankc                 C   r,   )Nz[XLAEnvironment.set_global_rank was called, but setting global rank is not allowed. Ignored.r-   )r   r4   r   r   r   set_global_rankY   r1   zXLAEnvironment.set_global_rankc                 C   r"   )zThe rank (index) of the currently running process inside of the current host.

        The output is cached for performance.

        r   N)r#   r$   r%   get_local_ordinalr'   r   r   r   
local_rank]   r*   zXLAEnvironment.local_rankc                 C   sD   t rddlm} | S ddlm  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   )runtimeN)	getenv_as)r   	torch_xlar8   
host_indextorch_xla.core.xla_env_varsr$   xla_env_varstorch_xla.utils.utilsr9   HOST_ORDINALint)r   xrxenvr9   r   r   r   	node_ranki   s   zXLAEnvironment.node_rank)__name__
__module____qualname____doc__r   r   propertyr   boolr   r   r   r@   r   staticmethodr   	functools	lru_cacher)   r0   r3   r5   r7   rC   __classcell__r   r   r   r   r      s>    






r   )rK   loggingtypingr   typing_extensionsr   !lightning_fabric.accelerators.xlar   r   r   9lightning_fabric.plugins.environments.cluster_environmentr   	getLoggerrD   r.   r   r   r   r   r   <module>   s   
