
      i                         d dl mZmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ ed   Zdd	d
ddZed   Zed   Zeeeef   Z G d d      Zy)    )AbstractContextManagernullcontext)AnyLiteralOptionalUnion)Tensor)Module)	Optimizer)_PARAMETERSOptimizable)@          64-true32-true16-mixed
bf16-mixed)643216bf16)ztransformer-engineztransformer-engine-float16z16-truer   z	bf16-truer   r   r   c            
       6   e Zd ZU dZdZeed<   dedefdZde	fdZ
de	fdZde	fd	Zd
edefdZd
edefdZdedee   defdZdedee   dededdf
dZdedee   defdZdededefdZdedefdZdeddfdZdeeef   fdZdeeef   ddfdZddZy)	PrecisionzBase class for all plugins handling the precision-specific parts of the training.

    The class attribute precision must be overwritten in child classes. The default value reflects fp32 training.

    r   	precisionmodulereturnc                     |S )zConvert the module parameters to the precision type this plugin handles.

        This is optional and depends on the precision limitations during optimization.

         )selfr   s     /Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/plugins/precision/precision.pyconvert_modulezPrecision.convert_module0   s	         c                     t               S )z1Controls how tensors get created (device, dtype).r   r    s    r!   tensor_init_contextzPrecision.tensor_init_context8   
    }r#   c                     t               S )zInstantiate module parameters or tensors in the precision type this plugin handles.

        This is optional and depends on the precision limitations during optimization.

        r%   r&   s    r!   module_init_contextzPrecision.module_init_context<   s     }r#   c                     t               S )zWA contextmanager for managing model forward/training_step/evaluation_step/predict_step.r%   r&   s    r!   forward_contextzPrecision.forward_contextD   r(   r#   datac                     |S )zConvert model inputs (forward) to the floating point precision type of this plugin.

        This is a no-op in the base precision plugin, since we assume the data already has the desired type (default is
        torch.float32).

        r   r    r-   s     r!   convert_inputzPrecision.convert_inputH   	     r#   c                     |S )zConvert outputs to the floating point precision type expected after model's forward.

        This is a no-op in the base precision plugin, since we assume the data already has the desired type (default is
        torch.float32).

        r   r/   s     r!   convert_outputzPrecision.convert_outputQ   r1   r#   tensorc                      y)zRuns before precision plugin executes backward.

        Args:
            tensor: The tensor that will be used for backpropagation
            module: The module that was involved in producing the tensor and whose parameters need the gradients

        Nr   r    r4   r   s      r!   pre_backwardzPrecision.pre_backwardZ       r#   modelargskwargsNc                 (     |j                   |i | y)zPerforms the actual backpropagation.

        Args:
            tensor: The tensor that will be used for backpropagation
            model: The module that was involved in producing the tensor and whose parameters need the gradients

        N)backward)r    r4   r9   r:   r;   s        r!   r=   zPrecision.backwardc   s     	((r#   c                      y)zRuns after precision plugin executes backward.

        Args:
            tensor: The tensor that will be used for backpropagation
            module: The module that was involved in producing the tensor and whose parameters need the gradients

        Nr   r6   s      r!   post_backwardzPrecision.post_backwardm   r8   r#   	optimizerc                 &     |j                   di |S )zHook to run the optimizer step.r   )step)r    r@   r;   s      r!   optimizer_stepzPrecision.optimizer_stepv   s     y~~'''r#   c              #   L   K   |j                   D ]  }|d   E d{     y7 w)zThe main params of the model.

        Returns the plain model params here. Maybe different in other precision plugins.

        paramsN)param_groups)r    r@   groups      r!   main_paramszPrecision.main_params~   s(      ++EX&& ,&s   $"$c                      y )Nr   )r    r@   s     r!   unscale_gradientszPrecision.unscale_gradients   s    r#   c                     i S )zCalled when saving a checkpoint, implement to generate precision plugin state_dict.

        Returns:
            A dictionary containing precision plugin state.

        r   r&   s    r!   
state_dictzPrecision.state_dict   s	     	r#   rL   c                      y)zCalled when loading a checkpoint, implement to reload precision plugin state given precision plugin
        state_dict.

        Args:
            state_dict: the precision plugin state returned by ``state_dict``.

        Nr   )r    rL   s     r!   load_state_dictzPrecision.load_state_dict   s     	r#   c                      y)zThis method is called to teardown the training process.

        It is the right place to release memory and free other resources.

        Nr   r&   s    r!   teardownzPrecision.teardown   r8   r#   )r   N) __name__
__module____qualname____doc__r   _PRECISION_INPUT_STR__annotations__r
   r"   r   r'   r*   r,   r   r0   r3   r	   r   r7   r=   r?   r   rC   r   r   rH   rJ   dictstrrL   rN   rP   r   r#   r!   r   r   '   s\    '0I#/V  %; %; !7 # # 3 3 6 8F3C  )v )hv.> )s )VY )^b )F HV4D  (( ( 
	('Y '; '9  DcN $sCx. T r#   r   N)
contextlibr   r   typingr   r   r   r   torchr	   torch.nnr
   torch.optimr    lightning.fabric.utilities.typesr   r   _PRECISION_INPUT_INT%_PRECISION_INPUT_STR_ALIAS_CONVERSION_PRECISION_INPUT_STR_ALIASrU   _PRECISION_INPUTr   r   r#   r!   <module>rc      sw    ; 0 0   ! Ez* /8	Q[eq(r %$%=> 	  -/CE__` { {r#   