§
    “©ºj_  ã                   ó&  — d dl mZm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mZ d dlmZmZ d dlZd d	lmZ d d
lmZ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% d dl&m'Z' erd dl(Z( e'¦   «         Z) G d„ de¦  «        Z*dS )é    )ÚAbstractContextManagerÚnullcontext)ÚTYPE_CHECKINGÚAnyÚCallableÚOptionalÚUnionN)Úapply_to_collection)ÚTensor)ÚModule)ÚLBFGSÚ	Optimizer)Úget_argsÚoverride)Ú_PRECISION_INPUT)Ú_convert_fp_tensorÚ_DtypeContextManager)Ú	Steppable)Ú	Precision)ÚGradClipAlgorithmType)ÚMisconfigurationException)Úis_overridden)ÚWarningCachec                   óZ  — e Zd ZdZdeddfd„Zededefd„¦   «         Zede	de	fd	„¦   «         Z
edefd
„¦   «         Zedefd„¦   «         Zededddee         de	de	ddfd„¦   «         Zededddeg e	f         de	de	f
d„¦   «         Zedej        fdedeeef         deddfd„¦   «         ZdS )ÚDeepSpeedPrecisionat  Precision plugin for DeepSpeed integration.

    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.

    Args:
        precision: Full precision (32-true), half precision (16-true, bf16-true) or
            mixed precision (16-mixed, bf16-mixed).

    Raises:
        ValueError:
            If unsupported ``precision`` is provided.

    Ú	precisionÚreturnNc                 ó  — t          t          ¦  «        }||vrt          d|›d|› d�¦  «        ‚|| _        t          j        t          j        t          j        t          j        t          j        dœ}|| j                 | _        d S )Nz)`Trainer(strategy='deepspeed', precision=z1)` is not supported. `precision` must be one of: ú.)z
bf16-mixedz16-mixedz	bf16-truez16-truez32-true)	r   r   Ú
ValueErrorr   ÚtorchÚbfloat16Úfloat16Úfloat32Ú_desired_dtype)Úselfr   Úsupported_precisionÚprecision_to_types       ú}/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/plugins/precision/deepspeed.pyÚ__init__zDeepSpeedPrecision.__init__7   sž   € Ý&Õ'7Ñ8Ô8ÐØÐ/Ð/Ð/ÝðG¸Ið Gð GØ0CðGð Gð Gñô ð ð #ˆŒåœ.ÝœÝœÝ”}Ý”}ð
ð 
Ðð 0°´Ô?ˆÔÐÐó    Úmodulec                 óN   — d| j         v r|                     | j        ¬¦  «        S |S )NÚtrue)Údtype)r   Útor%   )r&   r,   s     r)   Úconvert_modulez!DeepSpeedPrecision.convert_moduleH   s+   € à�T”^Ð#Ð#Ø—9’9 4Ô#6�9Ñ7Ô7Ð7Øˆr+   Údatac                 óF   — t          |t          t          | j        ¬¦  «        S )N)Úfunctionr/   Údst_type)r
   r   r   r%   )r&   r2   s     r)   Úconvert_inputz DeepSpeedPrecision.convert_inputN   s   € å" 4Õ2DÍFÐ]aÔ]pÐqÑqÔqÐqr+   c                 óX   — d| j         vrt          ¦   «         S t          | j        ¦  «        S )Nr.   )r   r   r   r%   ©r&   s    r)   Útensor_init_contextz&DeepSpeedPrecision.tensor_init_contextR   s*   € à˜œÐ'Ð'Ý‘=”=Ð Ý# DÔ$7Ñ8Ô8Ð8r+   c                 ó*   — |                       ¦   «         S )N)r9   r8   s    r)   Úmodule_init_contextz&DeepSpeedPrecision.module_init_contextX   s   € à×'Ò'Ñ)Ô)Ð)r+   ÚtensorÚmodelzpl.LightningModuleÚ	optimizerÚargsÚkwargsc                 ó”   — t          d|¦  «        rt                               d¦  «         |j        j        } |j        |g|¢R i |¤Ž dS )a¢  Performs back-propagation using DeepSpeed's engine.

        Args:
            tensor: the loss tensor
            model: the model to be optimized
            optimizer: ignored for DeepSpeed
            \*args: additional positional arguments for the :meth:`deepspeed.DeepSpeedEngine.backward` call
            \**kwargs: additional keyword arguments for the :meth:`deepspeed.DeepSpeedEngine.backward` call

        Úbackwardz…You have overridden the `LightningModule.backward` hook but it will be ignored since DeepSpeed handles the backward logic internally.N)r   Úwarning_cacheÚwarnÚtrainerr=   rB   )r&   r<   r=   r>   r?   r@   Údeepspeed_engines          r)   rB   zDeepSpeedPrecision.backward\   sj   € õ& ˜ UÑ+Ô+ð 	Ý×Òð2ñô ð ð 7<´mÔ6IÐØ!ÐÔ! &Ð:¨4Ð:Ð:Ð:°6Ð:Ð:Ð:Ð:Ð:r+   Úclosurec                 óô   — t          |t          ¦  «        rt          d¦  «        ‚ |¦   «         }|                      ||¦  «         |d u }|j        r|rt          d¦  «        ‚|j        j        } |j        di |¤ŽS )Nz5DeepSpeed and the LBFGS optimizer are not compatible.z_Skipping backward by returning `None` from your `training_step` is not supported by `DeepSpeed`© )Ú
isinstancer   r   Ú_after_closureÚautomatic_optimizationrE   r=   Ústep)r&   r>   r=   rG   r@   Úclosure_resultÚskipped_backwardrF   s           r)   Úoptimizer_stepz!DeepSpeedPrecision.optimizer_stepw   s¡   € õ �i¥Ñ'Ô'ð 	eÝ+Ð,cÑdÔdÐdØ ˜™œˆØ×Ò˜E 9Ñ-Ô-Ð-Ø)¨TÐ1ÐàÔ'ð 	Ð,<ð 	Ý+Øqñô ð ð 7<´mÔ6IÐØ$ÐÔ$Ð.Ð. vÐ.Ð.Ð.r+   g        Úclip_valÚgradient_clip_algorithmc                 ó   — dS )z/DeepSpeed handles gradient clipping internally.NrI   )r&   r>   rQ   rR   s       r)   Úclip_gradientsz!DeepSpeedPrecision.clip_gradients�   s   € € € r+   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r   r   r1   r   r6   r   r9   r;   r   r   r   rB   r   rP   r   ÚNORMr   r	   ÚintÚfloatrT   rI   r+   r)   r   r   (   s  € € € € € ðð ð@Ð"2ð @°tð @ð @ð @ð @ð" ð Vð °ð ð ð ñ „Xðð
 ðr #ð r¨#ð rð rð rñ „Xðrð ð9Ð%;ð 9ð 9ð 9ñ „Xð9ð
 ð*Ð%;ð *ð *ð *ñ „Xð*ð ð;àð;ð $ð;ð ˜IÔ&ð	;ð
 ð;ð ð;ð 
ð;ð ;ð ;ñ „Xð;ð4 ð/àð/ð $ð/ð ˜"˜c˜'Ô"ð	/ð
 ð/ð 
ð/ð /ð /ñ „Xð/ð* ð '*Ø9NÔ9Sð	>ð >àð>ð ˜˜U˜
Ô#ð>ð "7ð	>ð
 
ð>ð >ð >ñ „Xð>ð >ð >r+   r   )+Ú
contextlibr   r   Útypingr   r   r   r   r	   r!   Úlightning_utilitiesr
   r   Útorch.nnr   Útorch.optimr   r   Útyping_extensionsr   r   Úpytorch_lightningÚplÚ,lightning_fabric.plugins.precision.deepspeedr   Ú(lightning_fabric.plugins.precision.utilsr   r   Ú lightning_fabric.utilities.typesr   Ú-pytorch_lightning.plugins.precision.precisionr   Úpytorch_lightning.utilitiesr   Ú&pytorch_lightning.utilities.exceptionsr   Ú)pytorch_lightning.utilities.model_helpersr   Ú%pytorch_lightning.utilities.rank_zeror   Ú	deepspeedrC   r   rI   r+   r)   ú<module>rm      s·  ðð ;Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @à €€€Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0à Ð Ð Ð Ø IÐ IÐ IÐ IÐ IÐ IØ ]Ð ]Ð ]Ð ]Ð ]Ð ]Ð ]Ð ]Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø CÐ CÐ CÐ CÐ CÐ CØ =Ð =Ð =Ð =Ð =Ð =Ø LÐ LÐ LÐ LÐ LÐ LØ CÐ CÐ CÐ CÐ CÐ CØ >Ð >Ð >Ð >Ð >Ð >àð ØÐÐÐà�‘”€ðl>ð l>ð l>ð l>ð l>˜ñ l>ô l>ð l>ð l>ð l>r+   