o
    zi                     @   s   d dl 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 d dlmZ d d	lmZ d d
lmZ G dd deZdS )    )OptionalN)override)Accelerator)_XLA_AVAILABLE)XLAPrecision)XLACheckpointIO)_StrategyRegistry)SingleDeviceStrategy)_DEVICEc                
       s   e Zd ZdZ			ddedee dee dee f fddZ	e
ed	efd
dZejedee d	dfddZe
ed	efddZejedee d	dfddZeeded	dfddZ  ZS )SingleDeviceXLAStrategyz-Strategy for training on a single XLA device.Ndeviceacceleratorcheckpoint_io	precisionc                    sR   t sttt t|tjr|j}dd lm  m	} t
 j|||||d d S )Nr   )r   r   r   r   )r   ModuleNotFoundErrorstr
isinstancetorchr   indextorch_xla.core.xla_modelcore	xla_modelsuper__init__
xla_device)selfr   r   r   r   xm	__class__ Z/home/ubuntu/.local/lib/python3.10/site-packages/lightning_fabric/strategies/single_xla.pyr      s   
z SingleDeviceXLAStrategy.__init__returnc                 C   s&   | j }|d urt|tsJ |S t S )N)_checkpoint_ior   r   r   pluginr   r   r    r   5   s
   z%SingleDeviceXLAStrategy.checkpoint_ioioc                 C   *   |d urt |tstd| || _d S )NzHThe XLA strategy can only work with the `XLACheckpointIO` plugin, found )r   r   	TypeErrorr"   )r   r%   r   r   r    r   >      
c                 C   s(   | j }|d urt|tsJ |S tdS )Nz32-true)
_precisionr   r   r#   r   r   r    r   E   s
   z!SingleDeviceXLAStrategy.precisionc                 C   r&   )NzEThe XLA strategy can only work with the `XLAPrecision` plugin, found )r   r   r'   r)   )r   r   r   r   r    r   N   r(   strategy_registryc                 C   s   |j d| | jd d S )N
single_xla)description)register__name__)clsr*   r   r   r    register_strategiesU   s   z+SingleDeviceXLAStrategy.register_strategies)NNN)r.   
__module____qualname____doc__r
   r   r   r   r   r   propertyr   r   setterr   classmethodr   r0   __classcell__r   r   r   r    r      s:    r   )typingr   r   typing_extensionsr   lightning_fabric.acceleratorsr   !lightning_fabric.accelerators.xlar   lightning_fabric.pluginsr   lightning_fabric.plugins.io.xlar   lightning_fabric.strategiesr   )lightning_fabric.strategies.single_devicer	    lightning_fabric.utilities.typesr
   r   r   r   r   r    <module>   s   