o
    oiG                     @   s  d dl mZ d dlmZ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m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rzd dl)Z)e( Z*G dd de Z+dS )    )nullcontext)TYPE_CHECKINGAnyCallableContextManager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                   @   s   e Zd ZdZdeddfddZededefdd	Zed
e	de	fddZ
edefddZedefddZededddee de	de	ddfddZededddeg e	f de	de	f
ddZedejfdedeeef deddfd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                 C   sV   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_type r&   a/home/ubuntu/.local/lib/python3.10/site-packages/lightning/pytorch/plugins/precision/deepspeed.py__init__7   s   zDeepSpeedPrecision.__init__modulec                 C   s   d| j v r|j| jdS |S )Ntrue)dtype)r   tor"   )r#   r)   r&   r&   r'   convert_moduleH   s   
z!DeepSpeedPrecision.convert_moduledatac                 C   s   t |tt| jdS )N)functionr+   dst_type)r	   r   r
   r"   )r#   r.   r&   r&   r'   convert_inputN   s   z DeepSpeedPrecision.convert_inputc                 C   s   d| j vrt S t| jS )Nr*   )r   r   r   r"   r#   r&   r&   r'   tensor_init_contextR   s   

z&DeepSpeedPrecision.tensor_init_contextc                 C   s   |   S )N)r3   r2   r&   r&   r'   module_init_contextX   s   z&DeepSpeedPrecision.module_init_contexttensormodelzpl.LightningModule	optimizerargskwargsc                 O   s8   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

        backwardzYou have overridden the `LightningModule.backward` hook but it will be ignored since DeepSpeed handles the backward logic internally.N)r   warning_cachewarntrainerr6   r:   )r#   r5   r6   r7   r8   r9   deepspeed_enginer&   r&   r'   r:   \   s   
zDeepSpeedPrecision.backwardclosurec                 K   sV   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`r&   )
isinstancer   r   _after_closureautomatic_optimizationr=   r6   step)r#   r7   r6   r?   r9   closure_resultskipped_backwardr>   r&   r&   r'   optimizer_stepw   s   

z!DeepSpeedPrecision.optimizer_stepg        clip_valgradient_clip_algorithmc                 C   s   dS )z/DeepSpeed handles gradient clipping internally.Nr&   )r#   r7   rG   rH   r&   r&   r'   clip_gradients   s    z!DeepSpeedPrecision.clip_gradients)__name__
__module____qualname____doc__r   r(   r   r   r-   r   r1   r   r3   r4   r
   r   r   r:   r   rF   r   NORMr   r   intfloatrI   r&   r&   r&   r'   r   (   sb    

r   ),
contextlibr   typingr   r   r   r   r   r   r   lightning_utilitiesr	   r
   torch.nnr   torch.optimr   r   typing_extensionsr   r   lightning.pytorchpytorchpl,lightning.fabric.plugins.precision.deepspeedr   (lightning.fabric.plugins.precision.utilsr   r    lightning.fabric.utilities.typesr   -lightning.pytorch.plugins.precision.precisionr   lightning.pytorch.utilitiesr   &lightning.pytorch.utilities.exceptionsr   )lightning.pytorch.utilities.model_helpersr   %lightning.pytorch.utilities.rank_zeror   	deepspeedr;   r   r&   r&   r&   r'   <module>   s*    