o
    pi-                      @   s   d dl Z d dlmZ ddlmZ g ZG dd dZG dd dZe ae a	d	e
d
e
fddZdd ZdddZedddZdS )    N)deepcopy   )signature_safe_contextmanagerc                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )UniqueNameGeneratorz
    Generate unique name with prefix.

    Args:
        prefix(str): The generated name prefix. All generated name will be
                     started with this prefix.
    Nc                 C   s"   t t| _|d u rd}|| _d S )N )collectionsdefaultdictintidsprefix)selfr    r   ^/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/base/unique_name.py__init__    s   
zUniqueNameGenerator.__init__c                 C   s
   |  |S N)generate)r   keyr   r   r   __call__&   s   
zUniqueNameGenerator.__call__c                 C   s4   | j | }| j |  d7  < | jd|t|g S )z
        Generate unique names with prefix

        Args:
            key(str): The key of return string.

        Returns(str): A unique string with the prefix
        r   _)r
   r   joinstr)r   r   tmpr   r   r   r   )   s   
	zUniqueNameGenerator.generatec                 C   s*   ddl m}m} | r|  S | |S Nr   )_dygraph_tracerin_dygraph_mode)	frameworkr   r   _generate_unique_namer   )r   r   r   r   r   r   r   generate_with_ignorable_key6   s   

z/UniqueNameGenerator.generate_with_ignorable_keyc                 C   s   t | j}t| j|_|S r   )r   r   r   r
   )r   retr   r   r   clone>   s   
zUniqueNameGenerator.cloner   )	__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r      s    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )DygraphParameterNameCheckerz6
    Check whether the name of parameter is used.
    c                 C   s   t  | _d S r   )set	_name_set)r   r   r   r   r   I   s   z$DygraphParameterNameChecker.__init__c                 C   s   || j v rdS | j | dS )z
        Check whether the name is used. If not used, insert into the _name_set.

        Args:
            name(str): The name of parameter to check.

        Returns(bool): If the name is in name_set,  return True; Otherwise, return False.

        TF)r&   add)r   namer   r   r   r   L   s   

z$DygraphParameterNameChecker.__call__N)r    r!   r"   r#   r   r   r   r   r   r   r$   D   s    r$   r   returnc                 C   s   t | S )aZ  
    Generate unique name with prefix key. Currently, Paddle distinguishes the
    names of the same key by numbering it from zero. For example, when key=fc,
    it continuously generates fc_0, fc_1, fc_2, etc.

    Args:
        key(str): The prefix of generated name.

    Returns:
        str: A unique string with the prefix key.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> name1 = paddle.utils.unique_name.generate('fc')
            >>> name2 = paddle.utils.unique_name.generate('fc')
            >>> print(name1, name2)
            fc_0 fc_1
    )	generator)r   r   r   r   r   b   s   r   c                 C   s(   ddl m}m} | r|  S t| S r   )r   r   r   r   r*   )r   r   r   r   r   r   r      s   
r   c                 C   s>   t }t}| du rt a n| a |du rt a||fS |a||fS )a/  
    Switch the namespace of in current context to a new namespace. Though
    :code:`switch()` and :code:`guard()` can both change namespace,
    :code:`guard()` is recommended since it can manage the context better
    together with :code:`with` statement.

    Args:
        new_generator(UniqueNameGenerator, optional): A new UniqueNameGenerator, not
            required normally. Default is None, which means switch to a new anonymous
            namespace.
        new_para_name_checker(DygraphParameterNameChecker, optional): A new DygraphParameterNameChecker,
            not required normally. Default is None, which means  switch to a new parameter name
            checker.

    Returns:
        UniqueNameGenerator: The previous UniqueNameGenerator.
        DygraphParameterNameChecker: The previous DygraphParameterNameChecker

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> name1 = paddle.utils.unique_name.generate('fc')
            >>> name2 = paddle.utils.unique_name.generate('fc')
            >>> print(name1, name2)
            fc_0 fc_1

            >>> pre_generator, pre_dygraph_name_checker = paddle.utils.unique_name.switch() # switch to a new anonymous namespace.
            >>> name2 = paddle.utils.unique_name.generate('fc')
            >>> print(name2)
            fc_0

            >>> paddle.utils.unique_name.switch(pre_generator, pre_dygraph_name_checker) # switch back to pre_generator.
            >>> name3 = paddle.utils.unique_name.generate('fc')
            >>> print(name3)
            fc_2
    N)r*   dygraph_parameter_name_checkerr   r$   )new_generatorZnew_para_name_checkerold_generatorold_para_name_checkerr   r   r   switch   s   (r/   c              	   c   s\    t | trt| } nt | trt|  } t| \}}zdV  W t|| dS t|| w )a#  
    Change the namespace of unique name with :code:`with` statement. After calling it,
    a new namespace in the context of :code:`with` will be created, and it will number
    names from zero again when calling :code:`generate()` with same key.

    Args:
        new_generator(str|bytes, optional): New name of global namespace. Note that str
            in Python2 was split into str and bytes in Python3, so here are two
            types. Default is None. If not None, new_generator will be added into
            the prefix of unique name generated by :code:`generate()`.

    Returns:
        None.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> with paddle.utils.unique_name.guard():
            ...     name_1 = paddle.utils.unique_name.generate('fc')
            >>> with paddle.utils.unique_name.guard():
            ...     name_2 = paddle.utils.unique_name.generate('fc')
            >>> print(name_1, name_2)
            fc_0 fc_0

            >>> with paddle.utils.unique_name.guard('A'):
            ...     name_1 = paddle.utils.unique_name.generate('fc')
            >>> with paddle.utils.unique_name.guard('B'):
            ...     name_2 = paddle.utils.unique_name.generate('fc')
            >>> print(name_1, name_2)
            Afc_0 Bfc_0
    N)
isinstancer   r   bytesdecoder/   )r,   r-   r.   r   r   r   guard   s   
#

r3   )NNr   )r   copyr   wrapped_decoratorr   __all__r   r$   r+   r*   r   r   r   r/   r3   r   r   r   r   <module>   s   -+
	7