
    zj-                          d dl Z d dlmZ ddlmZ g Z G d d          Z G d d          Z e            a e            a	d	e
d
e
fdZd ZddZedd            ZdS )    N)deepcopy   )signature_safe_contextmanagerc                   2    e Zd ZdZddZd Zd Zd Z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                 X    t          j        t                    | _        |d}|| _        d S )N )collectionsdefaultdictintidsprefix)selfr   s     g/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/base/unique_name.py__init__zUniqueNameGenerator.__init__    s)    *3//>F    c                 ,    |                      |          S N)generate)r   keys     r   __call__zUniqueNameGenerator.__call__&   s    }}S!!!r   c                     | j         |         }| j         |xx         dz  cc<   | j        d                    |t          |          g          z   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   tmps      r   r   zUniqueNameGenerator.generate)   sJ     hsm{SXXsCHHo6666r   c                     ddl m}m}  |            r |                                            S |                     |          S Nr   )_dygraph_tracerin_dygraph_mode)	frameworkr   r    _generate_unique_namer   )r   r   r   r    s       r   generate_with_ignorable_keyz/UniqueNameGenerator.generate_with_ignorable_key6   sV    ????????? 	="?$$::<<<}}S!!!r   c                 `    t          | j                  }t          | j                  |_        |S r   )r   r   r   r   )r   rets     r   clonezUniqueNameGenerator.clone>   s'    !$+..48$$
r   r   )	__name__
__module____qualname____doc__r   r   r   r#   r&    r   r   r   r      sn            " " "7 7 7" " "    r   r   c                       e Zd ZdZd Zd ZdS )DygraphParameterNameCheckerz6
    Check whether the name of parameter is used.
    c                 ,    t                      | _        d S r   )set	_name_set)r   s    r   r   z$DygraphParameterNameChecker.__init__I   s    r   c                 P    || 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)r0   add)r   names     r   r   z$DygraphParameterNameChecker.__call__L   s1     4>!!4Nt$$$5r   N)r'   r(   r)   r*   r   r   r+   r   r   r-   r-   D   s<               r   r-   r   returnc                      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   s    r   r   r   b   s    , S>>r   c                 |    ddl m}m}  |            r |                                            S t	          |           S r   )r!   r   r    r"   r6   )r   r   r    s      r   r#   r#      sP    ;;;;;;;; 9  66888S>>r   c                 r    t           }t          }| t                      a n| a |t                      an|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
    )r6   dygraph_parameter_name_checkerr   r-   )new_generatornew_para_name_checkerold_generatorold_para_name_checkers       r   switchr>      sO    P M:'))		!	$)D)F)F&&)>&///r   c              #   8  K   t          | t                    rt          |           } n6t          | t                    r!t          |                                           } t          |           \  }}	 dV  t          ||           dS # t          ||           w xY 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=   s      r   guardrC      s      F -%% D+M::	M5	)	) D+M,@,@,B,BCC+1-+@+@(M(5}344444}34444s   1B B)NNr   )r
   copyr   wrapped_decoratorr   __all__r   r-   r9   r6   r   r   r#   r>   rC   r+   r   r   <module>rG      s             < < < < < <
* * * * * * * *Z       2 "=!<!>!> !!	# #    V  40 40 40 40n +5 +5 +5 +5 +5 +5r   