§
    “©ºjØ  ã                   óÐ   — 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dS )é    )Ú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            
       óV  — 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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                 ó   — |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   s     ú|/root/private/project/pallet_detection_v2/.venv/lib/python3.11/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 )zµInstantiate 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 )zîConvert 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 )zïConvert 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                 ó   — 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    r4   r   s      r!   Úpre_backwardzPrecision.pre_backwardZ   ó   € € € r#   ÚmodelÚargsÚkwargsNc                 ó    —  |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    r4   r9   r:   r;   s        r!   r=   zPrecision.backwardc   s!   € ð 	ˆŒ˜Ð( Ð(Ð(Ð(Ð(Ð(r#   c                 ó   — 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   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              #   ó:   K  — |j         D ]}|d         E d{V —† ŒdS )z�The 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~   sC   è è € ð Ô+ð 	'ð 	'ˆEØ˜X”Ð&Ð&Ð&Ð&Ð&Ð&Ð&Ð&ð	'ð 	'r#   c                 ó   — d S )Nr   )r    r@   s     r!   Úunscale_gradientszPrecision.unscale_gradients‡   s   € Øˆr#   c                 ó   — i S )z«Called 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                 ó   — 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    rL   s     r!   Úload_state_dictzPrecision.load_state_dict“   s	   € ð 	ˆr#   c                 ó   — 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&   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\  € € € € € € ðð ð '0€IÐ#Ð/Ð/Ñ/ð Vð °ð ð ð ð ðÐ%;ð ð ð ð ðÐ%;ð ð ð ð ðÐ!7ð ð ð ð ð #ð ¨#ð ð ð ð ð 3ð ¨3ð ð ð ð ð 6ð °8¸FÔ3Cð Èð ð ð ð ð)˜vð )¨h°vÔ.>ð )Àsð )ÐVYð )Ð^bð )ð )ð )ð )ð Fð °H¸VÔ4Dð Èð ð ð ð ð(àð(ð ð(ð 
ð	(ð (ð (ð (ð' Yð '°;ð 'ð 'ð 'ð 'ð¨9ð ¸ð ð ð ð ð˜D  c œNð ð ð ð ð¨$¨s°C¨x¬.ð ¸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      s  ðð ;Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !à EÐ EÐ EÐ EÐ EÐ EÐ EÐ Eà˜zÔ*Ð Ø/8À	ÐQ[ÐeqÐ(rÐ(rÐ %Ø$Ð%=Ô>Ð Øðô	Ð ð Ð-Ð/CÐE_Ð_Ô`Ð ð{ð {ð {ð {ð {ñ {ô {ð {ð {ð {r#   