Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
23252a34
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
23252a34
编写于
8月 07, 2018
作者:
Q
Qiao Longfei
提交者:
GitHub
8月 07, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12463 from jacquesqiao/remove-duplicated-init-op
avoid duplicated init op for one parameter
上级
93152b0b
641a7fb3
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
190 addition
and
125 deletion
+190
-125
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+20
-1
python/paddle/fluid/tests/unittests/test_dist_transpiler.py
python/paddle/fluid/tests/unittests/test_dist_transpiler.py
+17
-8
python/paddle/fluid/tests/unittests/test_initializer.py
python/paddle/fluid/tests/unittests/test_initializer.py
+153
-116
未找到文件。
python/paddle/fluid/framework.py
浏览文件 @
23252a34
...
@@ -1038,7 +1038,26 @@ class Block(object):
...
@@ -1038,7 +1038,26 @@ class Block(object):
global_block
=
self
.
program
.
global_block
()
global_block
=
self
.
program
.
global_block
()
param
=
Parameter
(
global_block
,
*
args
,
**
kwargs
)
param
=
Parameter
(
global_block
,
*
args
,
**
kwargs
)
if
'initializer'
in
kwargs
:
if
'initializer'
in
kwargs
:
kwargs
[
'initializer'
](
param
,
self
)
def
_is_inited_by
(
block
,
var
):
init_ops
=
[]
for
op
in
block
.
ops
:
if
var
.
name
in
op
.
output_arg_names
:
init_ops
.
append
(
op
)
return
init_ops
initializer
=
kwargs
[
'initializer'
]
init_ops
=
_is_inited_by
(
global_block
,
param
)
init_ops_len
=
len
(
init_ops
)
if
init_ops_len
>
1
:
raise
RuntimeError
(
"param "
+
param
.
name
+
" is inited by multiple init ops "
+
str
(
init_ops
))
elif
init_ops_len
==
1
:
#TODO already inited, do nothing, should log a warning
pass
else
:
initializer
(
param
,
self
)
return
param
return
param
def
append_op
(
self
,
*
args
,
**
kwargs
):
def
append_op
(
self
,
*
args
,
**
kwargs
):
...
...
python/paddle/fluid/tests/unittests/test_dist_transpiler.py
浏览文件 @
23252a34
...
@@ -73,9 +73,18 @@ class TranspilerTest(unittest.TestCase):
...
@@ -73,9 +73,18 @@ class TranspilerTest(unittest.TestCase):
return
self
.
transpiler
return
self
.
transpiler
def
transpiler_test_impl
(
self
):
pass
class
TestBasicModel
(
TranspilerTest
):
def
test_transpiler
(
self
):
def
test_transpiler
(
self
):
main
=
fluid
.
Program
()
startup
=
fluid
.
Program
()
with
fluid
.
program_guard
(
main
,
startup
):
self
.
transpiler_test_impl
()
class
TestBasicModel
(
TranspilerTest
):
def
transpiler_test_impl
(
self
):
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
pserver2
,
startup2
=
self
.
get_pserver
(
self
.
pserver2_ep
)
pserver2
,
startup2
=
self
.
get_pserver
(
self
.
pserver2_ep
)
...
@@ -123,7 +132,7 @@ class TestBasicModel(TranspilerTest):
...
@@ -123,7 +132,7 @@ class TestBasicModel(TranspilerTest):
class
TestBasicModelWithLargeBlockSize
(
TranspilerTest
):
class
TestBasicModelWithLargeBlockSize
(
TranspilerTest
):
def
t
est_transpiler
(
self
):
def
t
ranspiler_test_impl
(
self
):
config
=
fluid
.
DistributeTranspilerConfig
()
config
=
fluid
.
DistributeTranspilerConfig
()
config
.
min_block_size
=
1048576
config
.
min_block_size
=
1048576
...
@@ -148,7 +157,7 @@ class TestBasicModelWithLargeBlockSize(TranspilerTest):
...
@@ -148,7 +157,7 @@ class TestBasicModelWithLargeBlockSize(TranspilerTest):
[
"sum"
,
"scale"
,
"sgd"
])
[
"sum"
,
"scale"
,
"sgd"
])
# confirm startup program
# confirm startup program
self
.
assertEqual
([
op
.
type
for
op
in
startup
.
global_block
().
ops
],
self
.
assertEqual
([
op
.
type
for
op
in
startup
.
global_block
().
ops
],
[
"fill_constant"
,
"fill_constant"
,
"fill_constant"
])
[
"fill_constant"
,
"fill_constant"
])
# the variable #fc_w will be split into two blocks
# the variable #fc_w will be split into two blocks
fc_w_var
=
startup2
.
global_block
().
var
(
"fc_w"
)
fc_w_var
=
startup2
.
global_block
().
var
(
"fc_w"
)
self
.
assertEqual
(
fc_w_var
.
shape
,
(
1000L
,
1000L
))
self
.
assertEqual
(
fc_w_var
.
shape
,
(
1000L
,
1000L
))
...
@@ -177,7 +186,7 @@ class TestNoSliceVar(TranspilerTest):
...
@@ -177,7 +186,7 @@ class TestNoSliceVar(TranspilerTest):
def
setUp
(
self
):
def
setUp
(
self
):
super
(
TestNoSliceVar
,
self
).
setUp
()
super
(
TestNoSliceVar
,
self
).
setUp
()
def
t
est_transpiler
(
self
):
def
t
ranspiler_test_impl
(
self
):
config
=
fluid
.
DistributeTranspilerConfig
()
config
=
fluid
.
DistributeTranspilerConfig
()
config
.
slice_var_up
=
False
config
.
slice_var_up
=
False
...
@@ -212,7 +221,7 @@ class TestLRDecay(TranspilerTest):
...
@@ -212,7 +221,7 @@ class TestLRDecay(TranspilerTest):
sgd_optimizer
.
minimize
(
avg_cost
)
sgd_optimizer
.
minimize
(
avg_cost
)
return
return
def
t
est_transpiler
(
self
):
def
t
ranspiler_test_impl
(
self
):
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
trainer
=
self
.
get_trainer
()
trainer
=
self
.
get_trainer
()
...
@@ -242,7 +251,7 @@ class TestLRDecayConditional(TranspilerTest):
...
@@ -242,7 +251,7 @@ class TestLRDecayConditional(TranspilerTest):
sgd_optimizer
.
minimize
(
avg_cost
)
sgd_optimizer
.
minimize
(
avg_cost
)
return
return
def
t
est_transpiler
(
self
):
def
t
ranspiler_test_impl
(
self
):
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
trainer
=
self
.
get_trainer
()
trainer
=
self
.
get_trainer
()
...
@@ -291,7 +300,7 @@ class TestL2Decay(TranspilerTest):
...
@@ -291,7 +300,7 @@ class TestL2Decay(TranspilerTest):
sgd_optimizer
.
minimize
(
avg_cost
)
sgd_optimizer
.
minimize
(
avg_cost
)
return
return
def
t
est_transpiler
(
self
):
def
t
ranspiler_test_impl
(
self
):
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
trainer
=
self
.
get_trainer
()
trainer
=
self
.
get_trainer
()
...
@@ -326,7 +335,7 @@ class TestL2DecayWithPiecewise(TranspilerTest):
...
@@ -326,7 +335,7 @@ class TestL2DecayWithPiecewise(TranspilerTest):
sgd_optimizer
.
minimize
(
avg_cost
)
sgd_optimizer
.
minimize
(
avg_cost
)
return
return
def
t
est_transpiler
(
self
):
def
t
ranspiler_test_impl
(
self
):
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
pserver
,
startup
=
self
.
get_pserver
(
self
.
pserver1_ep
)
trainer
=
self
.
get_trainer
()
trainer
=
self
.
get_trainer
()
...
...
python/paddle/fluid/tests/unittests/test_initializer.py
浏览文件 @
23252a34
...
@@ -27,6 +27,7 @@ class TestConstantInitializer(unittest.TestCase):
...
@@ -27,6 +27,7 @@ class TestConstantInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -43,6 +44,7 @@ class TestConstantInitializer(unittest.TestCase):
...
@@ -43,6 +44,7 @@ class TestConstantInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -61,6 +63,7 @@ class TestUniformInitializer(unittest.TestCase):
...
@@ -61,6 +63,7 @@ class TestUniformInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -80,17 +83,18 @@ class TestUniformInitializer(unittest.TestCase):
...
@@ -80,17 +83,18 @@ class TestUniformInitializer(unittest.TestCase):
program
=
framework
.
Program
()
program
=
framework
.
Program
()
program
.
random_seed
=
123
program
.
random_seed
=
123
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
lod_level
=
0
,
lod_level
=
0
,
name
=
"param
"
,
name
=
"param1
"
,
initializer
=
initializer
.
UniformInitializer
())
initializer
=
initializer
.
UniformInitializer
())
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
lod_level
=
0
,
lod_level
=
0
,
name
=
"param
"
,
name
=
"param2
"
,
initializer
=
initializer
.
UniformInitializer
(
seed
=
456
))
initializer
=
initializer
.
UniformInitializer
(
seed
=
456
))
init_op
=
block
.
ops
[
1
]
init_op
=
block
.
ops
[
1
]
self
.
assertEqual
(
init_op
.
attr
(
"seed"
),
123
)
self
.
assertEqual
(
init_op
.
attr
(
"seed"
),
123
)
...
@@ -102,6 +106,7 @@ class TestUniformInitializer(unittest.TestCase):
...
@@ -102,6 +106,7 @@ class TestUniformInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -115,6 +120,25 @@ class TestUniformInitializer(unittest.TestCase):
...
@@ -115,6 +120,25 @@ class TestUniformInitializer(unittest.TestCase):
self
.
assertAlmostEqual
(
init_op
.
attr
(
'max'
),
3.1
,
delta
=
DELTA
)
self
.
assertAlmostEqual
(
init_op
.
attr
(
'max'
),
3.1
,
delta
=
DELTA
)
self
.
assertEqual
(
init_op
.
attr
(
'seed'
),
123
)
self
.
assertEqual
(
init_op
.
attr
(
'seed'
),
123
)
def
test_uniform_initializer_two_op
(
self
):
"""Test uniform initializer with supplied attributes
"""
program
=
framework
.
Program
()
block
=
program
.
global_block
()
for
i
in
range
(
2
):
block
.
create_parameter
(
dtype
=
"float32"
,
shape
=
[
5
,
10
],
lod_level
=
0
,
name
=
"param"
,
initializer
=
initializer
.
UniformInitializer
(
-
4.2
,
float
(
i
),
123
))
self
.
assertEqual
(
len
(
block
.
ops
),
1
)
init_op0
=
block
.
ops
[
0
]
self
.
assertEqual
(
init_op0
.
type
,
'uniform_random'
)
self
.
assertAlmostEqual
(
init_op0
.
attr
(
'min'
),
-
4.2
,
delta
=
DELTA
)
self
.
assertAlmostEqual
(
init_op0
.
attr
(
'max'
),
0.0
,
delta
=
DELTA
)
self
.
assertEqual
(
init_op0
.
attr
(
'seed'
),
123
)
class
TestNormalInitializer
(
unittest
.
TestCase
):
class
TestNormalInitializer
(
unittest
.
TestCase
):
def
test_normal_initializer_default_value
(
self
):
def
test_normal_initializer_default_value
(
self
):
...
@@ -122,6 +146,7 @@ class TestNormalInitializer(unittest.TestCase):
...
@@ -122,6 +146,7 @@ class TestNormalInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -140,6 +165,7 @@ class TestNormalInitializer(unittest.TestCase):
...
@@ -140,6 +165,7 @@ class TestNormalInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -161,6 +187,7 @@ class TestXavierInitializer(unittest.TestCase):
...
@@ -161,6 +187,7 @@ class TestXavierInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -181,6 +208,7 @@ class TestXavierInitializer(unittest.TestCase):
...
@@ -181,6 +208,7 @@ class TestXavierInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
,
15
,
20
],
shape
=
[
5
,
10
,
15
,
20
],
...
@@ -203,6 +231,7 @@ class TestXavierInitializer(unittest.TestCase):
...
@@ -203,6 +231,7 @@ class TestXavierInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -223,6 +252,7 @@ class TestXavierInitializer(unittest.TestCase):
...
@@ -223,6 +252,7 @@ class TestXavierInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
,
15
,
20
],
shape
=
[
5
,
10
,
15
,
20
],
...
@@ -244,6 +274,7 @@ class TestXavierInitializer(unittest.TestCase):
...
@@ -244,6 +274,7 @@ class TestXavierInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -267,6 +298,7 @@ class TestMSRAInitializer(unittest.TestCase):
...
@@ -267,6 +298,7 @@ class TestMSRAInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -287,6 +319,7 @@ class TestMSRAInitializer(unittest.TestCase):
...
@@ -287,6 +319,7 @@ class TestMSRAInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
,
15
,
20
],
shape
=
[
5
,
10
,
15
,
20
],
...
@@ -308,6 +341,7 @@ class TestMSRAInitializer(unittest.TestCase):
...
@@ -308,6 +341,7 @@ class TestMSRAInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -328,6 +362,7 @@ class TestMSRAInitializer(unittest.TestCase):
...
@@ -328,6 +362,7 @@ class TestMSRAInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
param
=
block
.
create_parameter
(
param
=
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
,
15
,
20
],
shape
=
[
5
,
10
,
15
,
20
],
...
@@ -348,6 +383,7 @@ class TestMSRAInitializer(unittest.TestCase):
...
@@ -348,6 +383,7 @@ class TestMSRAInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
5
,
10
],
shape
=
[
5
,
10
],
...
@@ -370,6 +406,7 @@ class TestMSRAInitializer(unittest.TestCase):
...
@@ -370,6 +406,7 @@ class TestMSRAInitializer(unittest.TestCase):
"""
"""
program
=
framework
.
Program
()
program
=
framework
.
Program
()
block
=
program
.
global_block
()
block
=
program
.
global_block
()
for
_
in
range
(
2
):
block
.
create_parameter
(
block
.
create_parameter
(
dtype
=
"float32"
,
dtype
=
"float32"
,
shape
=
[
8
,
1
,
3
,
3
],
shape
=
[
8
,
1
,
3
,
3
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录