Skip to content

Commit a128eb7

Browse files
authored
improve unique_name, uniq id is related to prefix (#5223)
* improve unique_name, uniq id is related to prefix * fix join
1 parent 8d1ad97 commit a128eb7

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

paddle/pybind/pybind.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License. */
1515
#include "paddle/pybind/protobuf.h"
1616

1717
#include <mutex> // for call_once
18+
#include <unordered_map>
1819
#include "gflags/gflags.h"
1920
#include "paddle/framework/backward.h"
2021
#include "paddle/framework/executor.h"
@@ -42,9 +43,9 @@ limitations under the License. */
4243

4344
namespace paddle {
4445
namespace pybind {
45-
static size_t UniqueIntegerGenerator() {
46-
static std::atomic<size_t> generator;
47-
return generator.fetch_add(1);
46+
static size_t UniqueIntegerGenerator(const std::string &prefix) {
47+
static std::unordered_map<std::string, std::atomic<size_t>> generators;
48+
return generators[prefix].fetch_add(1);
4849
}
4950

5051
std::once_flag gflags_init_flag;

python/paddle/v2/framework/framework.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ def type(self):
119119

120120
@staticmethod
121121
def _unique_var_name_():
122-
uid = core.unique_integer() # unique during whole process.
123-
return "_generated_var_%d" % uid
122+
prefix = "_generated_var"
123+
uid = core.unique_integer(prefix) # unique during whole process.
124+
return "_".join([prefix, str(uid)])
124125

125126
@staticmethod
126127
def _convert_np_dtype_to_dtype_(np_dtype):

python/paddle/v2/framework/layer_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def unique_name(prefix):
11-
uid = core.unique_integer() # unique during whole process.
11+
uid = core.unique_integer(prefix) # unique during whole process.
1212
return "_".join([prefix, str(uid)])
1313

1414

python/paddle/v2/framework/tests/test_image_classification_layer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_batch_norm_layer(self):
3737
layers.batch_norm(
3838
input=images, program=program, init_program=init_program)
3939

40-
#print str(program)
40+
# print str(program)
4141

4242
def test_dropout_layer(self):
4343
program = Program()
@@ -53,7 +53,7 @@ def test_dropout_layer(self):
5353
program=program,
5454
init_program=init_program)
5555

56-
#print str(program)
56+
# print str(program)
5757

5858
def test_img_conv_group(self):
5959
program = Program()

0 commit comments

Comments
 (0)