
      i	                     x    d dl Z d dlmZmZ d dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlmZ ed   Z G d d	e      Zy)
    N)AnyLiteral)get_argsoverride)_XLA_AVAILABLE)	Precision)Optimizable)z32-true16-true	bf16-truec                   P    e Zd ZdZdeddfdZedededefd       Z	ed
d	       Z
y)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.

    	precisionreturnNc                 z   t         st        t        t                     t        t              }||vrt        d|d| d      || _        |dk(  r)dt        j                  d<   t        j                  | _        y |dk(  r)dt        j                  d<   t        j                  | _        y t        j                  | _        y )	Nz`precision=z8)` is not supported in XLA. `precision` must be one of: .r
   1XLA_USE_F16r   XLA_USE_BF16)r   ModuleNotFoundErrorstrr   _PRECISION_INPUT
ValueErrorr   osenvirontorchfloat16_desired_dtypebfloat16float32)selfr   supported_precisions      {/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/plugins/precision/xla.py__init__zXLAPrecision.__init__'   s    %c.&9::&'78//i] +00C/DAG  #	!(+BJJ}%"'--D+%),BJJ~&"'..D"'--D    	optimizerkwargsc                 <    dd l mc m} |j                  ||d      S )Nr   T)optimizer_argsbarrier)torch_xla.core.xla_modelcore	xla_modeloptimizer_step)r    r%   r&   xms       r"   r-   zXLAPrecision.optimizer_step;   s$     	.-   64 PPr$   c                     t         j                  j                  dd        t         j                  j                  dd        y )Nr   r   )r   r   pop)r    s    r"   teardownzXLAPrecision.teardownF   s&    


~t,


}d+r$   )r   N)__name__
__module____qualname____doc__r   r#   r   r	   r   r-   r1    r$   r"   r   r      sd    	0"2 0t 0( QQ Q 
	Q Q , ,r$   r   )r   typingr   r   r   typing_extensionsr   r   !lightning.fabric.accelerators.xlar   ,lightning.fabric.plugins.precision.precisionr    lightning.fabric.utilities.typesr	   r   r   r6   r$   r"   <module>r<      s4    
   0 < B 8<= .,9 .,r$   