o
    oi                     @   s   d dl mZ d dlmZmZ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 dZdS )    nullcontext)AnyContextManagerDict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                
   @   sH  e Zd ZU dZdZeed< dedefddZde	fdd	Z
de	fd
dZde	fddZdedefddZdedefddZdedee defddZdedee dededdf
ddZdedee defddZdededefdd Zdedefd!d"Zdeddfd#d$Zdeeef fd%d&Zd'eeef ddfd(d)Zd,d*d+ZdS )-	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                 C      |S )zConvert the module parameters to the precision type this plugin handles.

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

         )selfr   r   r   `/home/ubuntu/.local/lib/python3.10/site-packages/lightning/fabric/plugins/precision/precision.pyconvert_module0   s   zPrecision.convert_modulec                 C      t  S )z1Controls how tensors get created (device, dtype).r   r    r   r   r!   tensor_init_context8      zPrecision.tensor_init_contextc                 C   r#   )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$   r   r   r!   module_init_context<   s   zPrecision.module_init_contextc                 C   r#   )zWA contextmanager for managing model forward/training_step/evaluation_step/predict_step.r   r$   r   r   r!   forward_contextD   r&   zPrecision.forward_contextdatac                 C   r   )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)   r   r   r!   convert_inputH      zPrecision.convert_inputc                 C   r   )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*   r   r   r!   convert_outputQ   r,   zPrecision.convert_outputtensorc                 C      dS )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    r.   r   r   r   r!   pre_backwardZ       zPrecision.pre_backwardmodelargskwargsNc                 O   s   |j |i | dS )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    r.   r3   r4   r5   r   r   r!   r6   c   s   zPrecision.backwardc                 C   r/   )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   r0   r   r   r!   post_backwardm   r2   zPrecision.post_backward	optimizerc                 K   s   |j di |S )zHook to run the optimizer step.Nr   )step)r    r8   r5   r   r   r!   optimizer_stepv   s   zPrecision.optimizer_stepc                 c   s     |j D ]	}|d E dH  qdS )zThe main params of the model.

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

        paramsN)param_groups)r    r8   groupr   r   r!   main_params~   s   
zPrecision.main_paramsc                 C   s   d S )Nr   )r    r8   r   r   r!   unscale_gradients   s   zPrecision.unscale_gradientsc                 C   s   i S )zCalled when saving a checkpoint, implement to generate precision plugin state_dict.

        Returns:
            A dictionary containing precision plugin state.

        r   r$   r   r   r!   
state_dict   r,   zPrecision.state_dictr@   c                 C   r/   )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    r@   r   r   r!   load_state_dict   s   zPrecision.load_state_dictc                 C   r/   )zThis method is called to teardown the training process.

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

        Nr   r$   r   r   r!   teardown   r2   zPrecision.teardown)r   N) __name__
__module____qualname____doc__r   _PRECISION_INPUT_STR__annotations__r   r"   r   r%   r'   r(   r   r+   r-   r
   r   r1   r6   r7   r   r:   r   r   r>   r?   r   strr@   rA   rB   r   r   r   r!   r   '   s0   
 		"	
	
		
r   N)
contextlibr   typingr   r   r   r   r   r	   torchr
   torch.nnr   torch.optimr    lightning.fabric.utilities.typesr   r   _PRECISION_INPUT_INT%_PRECISION_INPUT_STR_ALIAS_CONVERSION_PRECISION_INPUT_STR_ALIASrG   _PRECISION_INPUTr   r   r   r   r!   <module>   s    
