o
    oiQ                     @   s   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 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 d dlmZ d dlmZ eG dd deZ e	e!ef Z"G dd deZ#dS )    )OrderedDict)suppress)	dataclassfield)AnyDict)Tensor)overrideN)do_nothing_closure)_Loop)OutputResult)	_Progress_ReadyCompletedTracker)call)MisconfigurationException)STEP_OUTPUTc                   @   s\   e Zd ZU dZeedZeee	f e
d< ededd fddZedeee	f fdd	Zd
S )ManualResultzA container to hold the result returned by ``_ManualOptimization``.

    It is created from the output of :meth:`~lightning.pytorch.core.LightningModule.training_step`.

    Attributes:
        extra: Anything returned by the ``training_step``.

    )default_factoryextratraining_step_outputreturnc                 C   s^   i }t |tr| }nt |trd|i}n|d urtdd|v r*|d  |d< | |dS )NlosszVIn manual optimization, `training_step` must either return a Tensor or have no return.r   )
isinstancedictcopyr   r   detach)clsr   r    r   _/home/ubuntu/.local/lib/python3.10/site-packages/lightning/pytorch/loops/optimization/manual.pyfrom_training_step_output-   s   




z&ManualResult.from_training_step_outputc                 C   s   | j S Nr   selfr   r   r   asdict?   s   zManualResult.asdictN)__name__
__module____qualname____doc__r   r   r   r   strr   __annotations__classmethodr   r    r	   r$   r   r   r   r   r       s   
 	r   c                       sv   e Zd ZdZeZd fddZdedefd	d
Z	dddZ
deddfddZdefddZdddZdddZ  ZS )_ManualOptimizationa  A special loop implementing what is known in Lightning as Manual Optimization where the optimization happens
    entirely in the :meth:`~lightning.pytorch.core.LightningModule.training_step` and therefore the user is responsible
    for back-propagating gradients and making calls to the optimizers.

    This loop is a trivial case because it performs only a single iteration (calling directly into the module's
    :meth:`~lightning.pytorch.core.LightningModule.training_step`) and passing through the output(s).

    trainer
pl.Trainerr   Nc                    s"   t  | tt| _i | _d S r!   )super__init__r   from_defaultsr   optim_step_progress_output)r#   r-   	__class__r   r   r0   S   s   
z_ManualOptimization.__init__kwargsc                 C   sH   |    tt | | W d    n1 sw   Y  d| _|  S )NF)on_run_startr   StopIterationadvance_restarting
on_run_end)r#   r6   r   r   r   run[   s   
z_ManualOptimization.runc                 C   s$   | j jjD ]
}| j|_| j|_qd S r!   )r-   strategy_lightning_optimizers_on_before_step_on_after_step)r#   lightning_optimizerr   r   r   r7   b   s   
z _ManualOptimization.on_run_startc                 C   sF   | j }tj|dg| R  }~| j j  | j|}| | _	dS )zPerforms the training step for manual optimization.

        Args:
            kwargs: The kwargs passed down to the hooks.

        training_stepN)
r-   r   _call_strategy_hookvaluesr=   post_training_stepoutput_result_clsr    r$   r3   )r#   r6   r-   r   resultr   r   r   r9   h   s   z_ManualOptimization.advancec                 C   s.   | j i }| _ | jjjD ]}t|_t|_q|S )zYReturns the result of this loop, i.e., the post-processed outputs from the training step.)r3   r-   r=   r>   r
   r?   r@   )r#   outputrA   r   r   r   r;   y   s
   z_ManualOptimization.on_run_endc                 C   s   | j   | jjd d S Noptimizer_step)r2   increment_readyr-   profilerstartr"   r   r   r   r?      s   
z#_ManualOptimization._on_before_stepc                 C   s   | j jd | j  d S rI   )r-   rL   stopr2   increment_completedr"   r   r   r   r@      s   z"_ManualOptimization._on_after_step)r-   r.   r   N)r   N)r%   r&   r'   r(   r   rF   r0   r   _OUTPUTS_TYPEr<   r7   r9   r;   r?   r@   __classcell__r   r   r4   r   r,   G   s    	

	r,   )$collectionsr   
contextlibr   dataclassesr   r   typingr   r   torchr   typing_extensionsr	   lightning.pytorchpytorchpl lightning.pytorch.core.optimizerr
   lightning.pytorch.loopsr   ,lightning.pytorch.loops.optimization.closurer    lightning.pytorch.loops.progressr   r   lightning.pytorch.trainerr   &lightning.pytorch.utilities.exceptionsr   !lightning.pytorch.utilities.typesr   r   r)   rP   r,   r   r   r   r   <module>   s$   #