o
    #Õj¶  ã                   @   s@   d dl mZ dgZG dd„ dƒZeƒ add„ ZG dd„ dƒZdS )	é   )Ú	frameworkÚ	LazyGuardc                   @   sD   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	dd„ ƒZ
dS )ÚLazyInitHelperz‡
    A Helper Context to trigger switching mode between dygraph and static graph mode,
    and holds the startup program resource.
    c                 C   s   d| _ d | _d| _d S )NF)Ú_stateÚ_tracerÚ	_in_guard©Úself© r
   ú`/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/nn/initializer/lazy_init.pyÚ__init__   s   
zLazyInitHelper.__init__c                 C   s$   | j rdS t ¡ sJ dƒ‚d| _ dS )zs
        Switch into lazy mode.

        NOTE(dev): This is a very low level API and not exposed for user.
        Nz4LazyInit.enable() is only available in dygraph mode.T)r   r   Zin_dygraph_moder   r
   r
   r   Úenable   s   ÿþ
zLazyInitHelper.enablec                 C   s   | j sdS d| _ dS )zq
        Exit from lazy mode.

        NOTE(dev): This is a very low level API and not exposed for user.
        NF©r   r   r
   r
   r   Údisable,   s   
zLazyInitHelper.disablec                 C   s.   |   ¡  | jr	dS tjj| _dtj_d| _dS )z‚
        Switch into lazy mode and set _dygraph_tracer_ with None to convert
        dygraph mode into static graph mode.
        NT)r   r   r   Ú
global_varÚ_dygraph_tracer_r   r   r
   r
   r   Ú	__enter__6   s   

zLazyInitHelper.__enter__c                 O   s:   |   ¡  | js	dS | jdusJ ‚| jtj_d| _d| _dS )zC
        Exit from lazy mode and recover _dygraph_tracer_.
        NF)r   r   r   r   r   r   ©r	   ÚargsÚkwargsr
   r
   r   Ú__exit__B   s   

zLazyInitHelper.__exit__c                 C   s   | j S ©Nr   r   r
   r
   r   ÚstateN   s   zLazyInitHelper.stateN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   Úpropertyr   r
   r
   r
   r   r      s    
r   c                   C   s   t S r   )Ú_lazy_init_helperr
   r
   r
   r   Úlazy_init_helperV   s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   a±  
    LazyGuard is a wrapper interface for nn.Layer, it forwards the construct
    process of user defined Layer. Meanwhile, it provides necessary API to
    trigger EagerParamBase Lazy Initialization and get startup Program.

    Examples:

        .. code-block:: python

            >>> from paddle import LazyGuard
            >>> from paddle.nn import Linear

            >>> with LazyGuard():
            ...     # w and b are initialized lazily and have no memory.
            ...     net = Linear(10, 10)
            ...
            >>> for param in net.parameters():
            ...     # Initialize param and allocate memory explicitly.
            ...     param.initialize()
    c                 C   s   t ƒ  ¡  dS )a¼  
        Construct instance from class_obj by Lazy Initializing parameters.

        Examples:

            .. code-block:: python

                >>> from paddle import LazyGuard
                >>> from paddle.nn import Linear

                >>> with LazyGuard():
                ...     fc = LazyInit(Linear)(10, 10)
                ...
                >>> for param in fc.parameters():
                ...     param.initialize()
        N)r   r   r   r
   r
   r   r   q   s   zLazyGuard.__enter__c                 O   s   t ƒ  ¡  d S r   )r   r   r   r
   r
   r   r   „   s   zLazyGuard.__exit__N)r   r   r   r   r   r   r
   r
   r
   r   r   [   s    N)Úbaser   Ú__all__r   r   r   r   r
   r
   r
   r   Ú<module>   s   ?