a
    þdœ  ã                   @   s¦   d dl Z d dlmZmZmZ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 d dlmZmZ ed	 Zed
 Zeeef ZG dd„ dƒZdS )é    N)ÚAnyÚDictÚ	GeneratorÚOptionalÚUnion)ÚTensor)ÚModule)Ú	Optimizer)ÚLiteral)Ú_convert_fp_tensor)Ú_PARAMETERSÚOptimizable)é@   é    é   )Z64Ú32Z16Zbf16c                   @   sú   e Zd ZU dZdZeed< eedœdd„Ze	j
ed dœd	d
„ƒZeedœdd„Zeee edœdd„Zeee eeddœdd„Zeee edœdd„Zeeedœdd„Zeedœdd„Zeeef dœdd„Zeeef ddœd d!„Zddœd"d#„ZdS )$Ú	PrecisionzÉBase 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   |S )z©Convert the module parameters to the precision type this plugin handles.

        This is optional and depends on the precision limitations during optimization.
        © )Úselfr   r   r   úu/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/plugins/precision/precision.pyÚconvert_module'   s    zPrecision.convert_module)NNN)r   c                 c   s
   dV  dS )zWA contextmanager for managing model forward/training_step/evaluation_step/predict_step.Nr   ©r   r   r   r   Úforward_context.   s    zPrecision.forward_context)Údatar   c                 C   s   t |tjƒS )zÇConvert model inputs (forward) to the floating point precision type of this plugin.

        This is a no-op for tensors that are not of floating-point type or already have the desired type.
        )r   ÚtorchÚfloat32)r   r   r   r   r   Úconvert_input3   s    zPrecision.convert_input)Útensorr   r   c                 C   s   dS )zýRuns 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_backward:   s    zPrecision.pre_backwardN)r    ÚmodelÚargsÚkwargsr   c                 O   s   |j |i |¤Ž dS )zñPerforms 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    r#   r$   r%   r   r   r   r&   B   s    zPrecision.backwardc                 C   s   dS )züRuns 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   r!   r   r   r   Úpost_backwardK   s    zPrecision.post_backward)Ú	optimizerr%   r   c                 K   s   |j f i |¤ŽS )zHook to run the optimizer step.)Ústep)r   r(   r%   r   r   r   Úoptimizer_stepS   s    zPrecision.optimizer_step)r(   r   c                 c   s   |j D ]}|d E dH  qdS )z€The main params of the model.

        Returns the plain model params here. Maybe different in other precision plugins.
        ÚparamsN)Zparam_groups)r   r(   Úgroupr   r   r   Úmain_params[   s    
zPrecision.main_paramsc                 C   s   i S )zªCalled when saving a checkpoint, implement to generate precision plugin state_dict.

        Returns:
            A dictionary containing precision plugin state.
        r   r   r   r   r   Ú
state_dictc   s    zPrecision.state_dict)r.   r   c                 C   s   dS )zÞCalled 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_dictk   s    zPrecision.load_state_dictc                 C   s   dS )z‹This 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   Úteardownt   s    zPrecision.teardown)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú_PRECISION_INPUT_STRÚ__annotations__r   r   Ú
contextlibÚcontextmanagerr   r   r   r   r   r   r"   r&   r'   r   r*   r	   r   r-   r   Ústrr.   r/   r0   r   r   r   r   r      s"   
	
ü	r   )r7   Útypingr   r   r   r   r   r   r   Ztorch.nnr   Ztorch.optimr	   Ztyping_extensionsr
   Z(lightning_fabric.plugins.precision.utilsr   Z lightning_fabric.utilities.typesr   r   Z_PRECISION_INPUT_INTr5   Z_PRECISION_INPUTr   r   r   r   r   Ú<module>   s   