Ë
      ç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)Ú	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     ú�/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 )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                  ó   — y)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 |¤Ž y)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                  ó   — y)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              #   óL   K  — |j                   D ]  }|d   E d{  –—†  Œ y7 Œ­w)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~   s(   è ø€ ð ×+Ô+ˆEØ˜X‘×&Ñ&ñ ,Ø&ús   ‚$š"›$c                  ó   — y )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                  ó   — y)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                  ó   — y)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      sw   ð÷ ;ß 0Ó 0å Ý Ý !ç Eà˜zÑ*Ð Ø/8À	ÐQ[ÐeqÑ(rÐ %Ø$Ð%=Ñ>Ð Øðñ	Ð ð Ð-Ð/CÐE_Ð_Ñ`Ð ÷{ò {r#   