
    Αi-                          S SK r S SKJr  SSKJr  / r " S S5      r " S S5      r\" 5       q\" 5       q	S	\
S
\
4S jrS rSS jr\SS j5       rg)    N)deepcopy   )signature_safe_contextmanagerc                   :    \ rS rSrSrS
S jrS rS rS rS r	S	r
g)UniqueNameGenerator   z
Generate unique name with prefix.

Args:
    prefix(str): The generated name prefix. All generated name will be
                 started with this prefix.
Nc                 X    [         R                  " [        5      U l        Uc  SnXl        g )N )collectionsdefaultdictintidsprefix)selfr   s     W/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/base/unique_name.py__init__UniqueNameGenerator.__init__    s#    **3/>F    c                 $    U R                  U5      $ N)generate)r   keys     r   __call__UniqueNameGenerator.__call__&   s    }}S!!r   c                     U R                   U   nU R                   U==   S-  ss'   U R                  SR                  U[        U5      /5      -   $ )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   UniqueNameGenerator.generate)   sC     hhsm{{SXXsCHo666r   c                 v    SSK JnJn  U" 5       (       a  U" 5       R                  5       $ U R	                  U5      $ Nr   )_dygraph_tracerin_dygraph_mode)	frameworkr#   r$   _generate_unique_namer   )r   r   r#   r$   s       r   generate_with_ignorable_key/UniqueNameGenerator.generate_with_ignorable_key6   s/    ?"$::<<}}S!!r   c                 d    [        U R                  5      n[        U R                  5      Ul        U$ r   )r   r   r   r   )r   rets     r   cloneUniqueNameGenerator.clone>   s%    !$++.488$
r   )r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r   r'   r+   __static_attributes__ r   r   r   r      s     "7"r   r   c                   $    \ rS rSrSrS rS rSrg)DygraphParameterNameCheckerD   z.
Check whether the name of parameter is used.
c                 "    [        5       U l        g r   )set	_name_set)r   s    r   r   $DygraphParameterNameChecker.__init__I   s    r   c                 Z    XR                   ;   a  gU R                   R                  U5        g)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)r9   add)r   names     r   r   $DygraphParameterNameChecker.__call__L   s&     >>!NNt$r   )r9   N)r-   r.   r/   r0   r1   r   r   r2   r3   r   r   r5   r5   D   s    r   r5   r   returnc                     [        U 5      $ )a  
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                 j    SSK JnJn  U" 5       (       a  U" 5       R                  5       $ [	        U 5      $ r"   )r%   r#   r$   r&   rA   )r   r#   r$   s      r   r'   r'      s*    ; 6688S>r   c                 d    [         n[        nU c  [        5       q OU q Uc  [        5       qX#4$ UqX#4$ )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
)rA   dygraph_parameter_name_checkerr   r5   )new_generatornew_para_name_checkerold_generatorold_para_name_checkers       r   switchrI      sN    P M:')	!	$)D)F& // *?&//r   c              #     #    [        U [        5      (       a  [        U 5      n O.[        U [        5      (       a  [        U R	                  5       5      n [        U 5      u  p Sv   [        X5        g! [        X5        f = f7f)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decoderI   )rE   rG   rH   s      r   guardrN      sd     F -%%+M:	M5	)	)+M,@,@,BC+1-+@(M5}4}4s   AB  A0 $B 0A==B )NNr   )r   copyr   wrapped_decoratorr   __all__r   r5   rD   rA   r   r   r'   rI   rN   r3   r   r   <module>rR      sp      <
* *Z 2 "=!> !	# # V40n +5 +5r   