
    {-j                        d dl mZ d dlmZ ddlmZ erd dlmZ dgZ G d d          Z	 e	            a
d	 Z G d
 d          ZdS )    )annotations)TYPE_CHECKING   )	framework)TracebackType	LazyGuardc                  F    e Zd ZdZd Zd Zd Zd Zd Ze	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                0    d| _         d | _        d| _        d S )NF)_state_tracer	_in_guardselfs    _/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/initializer/lazy_init.py__init__zLazyInitHelper.__init__"   s        c                `    | j         rdS t          j                    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   in_dygraph_moder   s    r   enablezLazyInitHelper.enable'   sF     ; 	F(** 	
 	
B	
 	
* r   c                &    | 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   s    r   disablezLazyInitHelper.disable4   s     { 	Fr   c                    |                                   | 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   s    r   	__enter__zLazyInitHelper.__enter__>   sC    
 	> 	F +<04	-r   c                    |                                   | j        sdS | j        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kwargss      r   __exit__zLazyInitHelper.__exit__J   sM     	~ 	F|'''04	-r   c                    | j         S Nr   r   s    r   statezLazyInitHelper.stateV   s
    {r   N)__name__
__module____qualname____doc__r   r   r   r   r!   propertyr$    r   r   r
   r
      s         
  
    
 
 

 
 
   X  r   r
   c                     t           S r#   )_lazy_init_helperr*   r   r   lazy_init_helperr-   ^   s    r   c                  "    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()
    returnNonec                F    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   s    r   r   zLazyGuard.__enter__y   s"    " 	!!#####r   exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                F    t                                                       d S r#   )r-   r   )r   r2   r4   r6   s       r   r!   zLazyGuard.__exit__   s"     	""$$$$$r   N)r/   r0   )r2   r3   r4   r5   r6   r7   r/   r0   )r%   r&   r'   r(   r   r!   r*   r   r   r   r   c   sF         *$ $ $ $&% % % % % %r   N)
__future__r   typingr   baser   typesr   __all__r
   r,   r-   r   r*   r   r   <module>r>      s    # " " " " "                   $###### -< < < < < < < <~ #N$$   
/% /% /% /% /% /% /% /% /% /%r   