未验证 提交 a128eb7b 编写于 作者: Q Qiao Longfei 提交者: GitHub

improve unique_name, uniq id is related to prefix (#5223)

* improve unique_name, uniq id is related to prefix

* fix join
上级 8d1ad97b
...@@ -15,6 +15,7 @@ limitations under the License. */ ...@@ -15,6 +15,7 @@ limitations under the License. */
#include "paddle/pybind/protobuf.h" #include "paddle/pybind/protobuf.h"
#include <mutex> // for call_once #include <mutex> // for call_once
#include <unordered_map>
#include "gflags/gflags.h" #include "gflags/gflags.h"
#include "paddle/framework/backward.h" #include "paddle/framework/backward.h"
#include "paddle/framework/executor.h" #include "paddle/framework/executor.h"
...@@ -42,9 +43,9 @@ limitations under the License. */ ...@@ -42,9 +43,9 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace pybind { namespace pybind {
static size_t UniqueIntegerGenerator() { static size_t UniqueIntegerGenerator(const std::string &prefix) {
static std::atomic<size_t> generator; static std::unordered_map<std::string, std::atomic<size_t>> generators;
return generator.fetch_add(1); return generators[prefix].fetch_add(1);
} }
std::once_flag gflags_init_flag; std::once_flag gflags_init_flag;
......
...@@ -119,8 +119,9 @@ class Variable(object): ...@@ -119,8 +119,9 @@ class Variable(object):
@staticmethod @staticmethod
def _unique_var_name_(): def _unique_var_name_():
uid = core.unique_integer() # unique during whole process. prefix = "_generated_var"
return "_generated_var_%d" % uid uid = core.unique_integer(prefix) # unique during whole process.
return "_".join([prefix, str(uid)])
@staticmethod @staticmethod
def _convert_np_dtype_to_dtype_(np_dtype): def _convert_np_dtype_to_dtype_(np_dtype):
......
...@@ -8,7 +8,7 @@ from paddle.v2.framework.framework import Variable, g_program, \ ...@@ -8,7 +8,7 @@ from paddle.v2.framework.framework import Variable, g_program, \
def unique_name(prefix): def unique_name(prefix):
uid = core.unique_integer() # unique during whole process. uid = core.unique_integer(prefix) # unique during whole process.
return "_".join([prefix, str(uid)]) return "_".join([prefix, str(uid)])
......
...@@ -37,7 +37,7 @@ class TestLayer(unittest.TestCase): ...@@ -37,7 +37,7 @@ class TestLayer(unittest.TestCase):
layers.batch_norm( layers.batch_norm(
input=images, program=program, init_program=init_program) input=images, program=program, init_program=init_program)
#print str(program) # print str(program)
def test_dropout_layer(self): def test_dropout_layer(self):
program = Program() program = Program()
...@@ -53,7 +53,7 @@ class TestLayer(unittest.TestCase): ...@@ -53,7 +53,7 @@ class TestLayer(unittest.TestCase):
program=program, program=program,
init_program=init_program) init_program=init_program)
#print str(program) # print str(program)
def test_img_conv_group(self): def test_img_conv_group(self):
program = Program() program = Program()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册