|
|
|
@ -56,23 +56,24 @@ generator = UniqueNameGenerator()
|
|
|
|
|
|
|
|
|
|
def generate(key):
|
|
|
|
|
"""
|
|
|
|
|
Generate unique name with prefix key.
|
|
|
|
|
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 generated name prefix. All generated name will be
|
|
|
|
|
started with this prefix.
|
|
|
|
|
Args:
|
|
|
|
|
key(str): The prefix of generated name.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: A unique string with the prefix key.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
name1 = fluid.unique_name.generate('fc')
|
|
|
|
|
name2 = fluid.unique_name.generate('fc')
|
|
|
|
|
# The result is fc_0, fc_1
|
|
|
|
|
print name1, name2
|
|
|
|
|
print(name1, name2) # fc_0, fc_1
|
|
|
|
|
"""
|
|
|
|
|
return generator(key)
|
|
|
|
|
|
|
|
|
@ -102,27 +103,36 @@ def generate_with_ignorable_key(key):
|
|
|
|
|
|
|
|
|
|
def switch(new_generator=None):
|
|
|
|
|
"""
|
|
|
|
|
Switch the Global namespace to a new namespace.
|
|
|
|
|
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(None|UniqueNameGenerator): A new UniqueNameGenerator.
|
|
|
|
|
Args:
|
|
|
|
|
new_generator(UniqueNameGenerator, optional): A new UniqueNameGenerator, not
|
|
|
|
|
required normally. Default is None, which means switch to a new anonymous
|
|
|
|
|
namespace.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
UniqueNameGenerator: The previous UniqueNameGenerator.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
name1 = fluid.unique_name.generate('fc')
|
|
|
|
|
name2 = fluid.unique_name.generate('fc')
|
|
|
|
|
# The result is fc_0, fc_1
|
|
|
|
|
print name1, name2
|
|
|
|
|
print(name1, name2) # fc_0, fc_1
|
|
|
|
|
|
|
|
|
|
fluid.unique_name.switch()
|
|
|
|
|
pre_generator = fluid.unique_name.switch() # switch to a new anonymous namespace.
|
|
|
|
|
name2 = fluid.unique_name.generate('fc')
|
|
|
|
|
# The result is fc_0
|
|
|
|
|
print name2
|
|
|
|
|
print(name2) # fc_0
|
|
|
|
|
|
|
|
|
|
fluid.unique_name.switch(pre_generator) # switch back to pre_generator.
|
|
|
|
|
name3 = fluid.unique_name.generate('fc')
|
|
|
|
|
print(name3) # fc_2, since pre_generator has generated fc_0, fc_1.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
global generator
|
|
|
|
|
old = generator
|
|
|
|
@ -136,14 +146,21 @@ def switch(new_generator=None):
|
|
|
|
|
@signature_safe_contextmanager
|
|
|
|
|
def guard(new_generator=None):
|
|
|
|
|
"""
|
|
|
|
|
Change the global namespace with `with` statement.
|
|
|
|
|
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 spilted 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()`.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
new_generator(None|str|bytes): New name of global namespace.
|
|
|
|
|
Note that str in Python2 was spilted into str and bytes in Python3,
|
|
|
|
|
so here are two types. Default is None.
|
|
|
|
|
Returns:
|
|
|
|
|
None.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
@ -151,15 +168,13 @@ def guard(new_generator=None):
|
|
|
|
|
name_1 = fluid.unique_name.generate('fc')
|
|
|
|
|
with fluid.unique_name.guard():
|
|
|
|
|
name_2 = fluid.unique_name.generate('fc')
|
|
|
|
|
# The result is fc_0, fc_0
|
|
|
|
|
print name_1, name_2
|
|
|
|
|
print(name_1, name_2) # fc_0, fc_0
|
|
|
|
|
|
|
|
|
|
with fluid.unique_name.guard('A'):
|
|
|
|
|
name_1 = fluid.unique_name.generate('fc')
|
|
|
|
|
with fluid.unique_name.guard('B'):
|
|
|
|
|
name_2 = fluid.unique_name.generate('fc')
|
|
|
|
|
# The result is Afc_0, Bfc_0
|
|
|
|
|
print name_1, name_2
|
|
|
|
|
name_2 = fluid.unique_name.generate('fc')
|
|
|
|
|
print(name_1, name_2) # Afc_0, Bfc_0
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(new_generator, six.string_types):
|
|
|
|
|
new_generator = UniqueNameGenerator(new_generator)
|
|
|
|
|