o
    8wiK                     @   s   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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)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:`~pytorch_lightning.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   h/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pytorch_lightning/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   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:`~pytorch_lightning.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:`~pytorch_lightning.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   r/   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"   r5   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   r6   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#   r2   )r"   r5   r,   r   resultr   r   r   r8   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.)r2   r,   r<   r=   r	   r>   r?   )r"   outputr@   r   r   r   r:   y   s
   z_ManualOptimization.on_run_endc                 C   s   | j   | jjd d S Noptimizer_step)r1   increment_readyr,   profilerstartr!   r   r   r   r>      s   
z#_ManualOptimization._on_before_stepc                 C   s   | j jd | j  d S rH   )r,   rK   stopr1   increment_completedr!   r   r   r   r?      s   z"_ManualOptimization._on_after_step)r,   r-   r   N)r   N)r$   r%   r&   r'   r   rE   r/   r   _OUTPUTS_TYPEr;   r6   r8   r:   r>   r?   __classcell__r   r   r3   r   r+   G   s    	

	r+   )#collectionsr   
contextlibr   dataclassesr   r   typingr   torchr   typing_extensionsr   pytorch_lightningpl pytorch_lightning.core.optimizerr	   pytorch_lightning.loopsr
   ,pytorch_lightning.loops.optimization.closurer    pytorch_lightning.loops.progressr   r   pytorch_lightning.trainerr   &pytorch_lightning.utilities.exceptionsr   !pytorch_lightning.utilities.typesr   r   r   r(   rO   r+   r   r   r   r   <module>   s$   #