o
    z“©i@
  ã                   @   sx   d dl mZmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ e
dƒZeG dd„ dƒƒZG dd	„ d	eee ƒZd
S )é    )ÚABCÚabstractmethod)Ú	dataclass)ÚAnyÚDictÚGenericÚOptionalÚTypeVar)ÚMisconfigurationExceptionÚTc                   @   s"   e Zd Zdeeef fdd„ZdS )ÚOutputResultÚreturnc                 C   s   t ‚©N)ÚNotImplementedError©Úself© r   ú`/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/loops/optimization/closure.pyÚasdict   s   zOutputResult.asdictN)Ú__name__Ú
__module__Ú__qualname__r   Ústrr   r   r   r   r   r   r      s    r   c                       s`   e Zd ZdZd‡ fdd„Zdefdd„Zeded	edefd
d„ƒZ	ded	edefdd„Z
‡  ZS )ÚAbstractClosurea$  Abstract base class for optimizer closures in Lightning.

    Formally, a closure is binding variables from an external scope to a function that does a computation on these
    variables without taking them explicitly as input. This has the benefit that a closure can be passed to an object
    which later can call it like a function but without requiring to pass in any arguments.

    This class provides a simple abstraction making the instance of this class callable like a function while capturing
    the closure result and caching it.

    r   Nc                    s   t ƒ  ¡  d | _d S r   )ÚsuperÚ__init__Ú_resultr   ©Ú	__class__r   r   r   )   s   

zAbstractClosure.__init__c                 C   s$   | j du r	tdƒ‚| j d}| _ |S )zÕThe cached result from the last time the closure was called.

        Once accessed, the internal reference gets reset and the consumer will have to hold on to the reference as long
        as necessary.

        NzÒThe closure hasn't been executed. HINT: did you call `optimizer_closure()` in your `optimizer_step` hook? It could also happen because the `optimizer.step(optimizer_closure)` call did not execute it internally.)r   r
   )r   Úresultr   r   r   Úconsume_result-   s   
ÿzAbstractClosure.consume_resultÚargsÚkwargsc                 O   s   dS )zAImplements the behavior of the closure once it is getting called.Nr   ©r   r!   r"   r   r   r   Úclosure=   s   zAbstractClosure.closurec                 O   s   | j |i |¤Ž| _| S r   )r$   r   r#   r   r   r   Ú__call__B   s   zAbstractClosure.__call__)r   N)r   r   r   Ú__doc__r   r   r    r   r   r$   r%   Ú__classcell__r   r   r   r   r      s    r   N)Úabcr   r   Údataclassesr   Útypingr   r   r   r   r	   Ú&pytorch_lightning.utilities.exceptionsr
   r   r   r   r   r   r   r   Ú<module>   s   