o
    ûpi½  ã                   @  sh   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„ dƒZ	e	ƒ a
d	d
„ ZG dd„ dƒZdS )é    )Úannotations)ÚTYPE_CHECKINGé   )Ú	framework)ÚTracebackTypeÚ	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   úf/home/app/PaddleOCR-VL/.venv_paddleocr/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   Údisable4   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__>   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__J   s   

zLazyInitHelper.__exit__c                 C  s   | j S ©Nr   r   r   r   r   ÚstateV   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_helper^   s   r"   c                   @  s$   e Zd ZdZddd„Zd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                 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   y   s   zLazyGuard.__enter__Úexc_typeútype[BaseException] | NoneÚexc_valúBaseException | NoneÚexc_tbúTracebackType | Nonec                 C  s   t ƒ  ¡  d S r   )r"   r   )r   r%   r'   r)   r   r   r   r   Œ   s   zLazyGuard.__exit__N)r#   r$   )r%   r&   r'   r(   r)   r*   r#   r$   )r   r   r   r   r   r   r   r   r   r   r   c   s    
N)Ú
__future__r   Útypingr   Úbaser   Útypesr   Ú__all__r   r!   r"   r   r   r   r   r   Ú<module>   s   ?