o
    ziC                     @   s   d dl Z d dlmZ d dlmZmZ 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 deZdS )    N)partial)AnyCallable)get_argsoverride)_XLA_AVAILABLE)_PRECISION_INPUT)Optimizable)	Precision)MisconfigurationExceptionc                   @   sz   e Zd ZdZddeddfddZeded	d
deg e	f de	de	f
ddZ
edddZdedeg e	f de	fddZdS )XLAPrecisionzPlugin for training with XLA.

    Args:
        precision: Full precision (32-true) or half precision (16-true, bf16-true).

    Raises:
        ValueError:
            If unsupported ``precision`` is provided.

    32-true	precisionreturnNc                 C   s   t sttt tt}||vrtd|d| d|| _|dkr-dtjd< t	j
| _d S |dkr<dtjd< t	j| _d S t	j| _d S )	Nz`precision=z8)` is not supported in XLA. `precision` must be one of: .z16-true1XLA_USE_F16z	bf16-trueXLA_USE_BF16)r   ModuleNotFoundErrorstrr   r   
ValueErrorr   osenvirontorchfloat16_desired_dtypebfloat16float32)selfr   supported_precision r    [/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/plugins/precision/xla.py__init__)   s"   

zXLAPrecision.__init__	optimizermodelzpl.LightningModuleclosurekwargsc                 K   sj   dd l m  m} t| j||}t| j|||}|jdd|i|}|  |d u }|jr3|r3t	d|S )Nr   r%   zSkipping backward by returning `None` from your `training_step` is not implemented with XLA. Please, open an issue in `https://github.com/Lightning-AI/lightning/issues` requesting this feature.r    )
torch_xla.core.xla_modelcore	xla_modelr   _xla_wrap_closure_wrap_closurestep	mark_stepautomatic_optimizationr   )r   r#   r$   r%   r&   xmclosure_resultskipped_backwardr    r    r!   optimizer_step>   s   
zXLAPrecision.optimizer_stepc                 C   s    t jdd  t jdd  d S )Nr   r   )r   r   pop)r   r    r    r!   teardownW   s   zXLAPrecision.teardownc                 C   s&   dd l m  m} | }|| |S )Nr   )r'   r(   r)   reduce_gradients)r   r#   r%   r/   r0   r    r    r!   r*   \   s   
zXLAPrecision._xla_wrap_closure)r   )r   N)__name__
__module____qualname____doc__r   r"   r   r	   r   r   r2   r4   r*   r    r    r    r!   r      s$    
"r   )r   	functoolsr   typingr   r   r   typing_extensionsr   r   pytorch_lightningpl!lightning_fabric.accelerators.xlar   &lightning_fabric.plugins.precision.xlar    lightning_fabric.utilities.typesr	   -pytorch_lightning.plugins.precision.precisionr
   &pytorch_lightning.utilities.exceptionsr   r   r    r    r    r!   <module>   s   