Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
40019793
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看板
未验证
提交
40019793
编写于
12月 10, 2020
作者:
H
huangxu96
提交者:
GitHub
12月 10, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add ReserveSpace in dygraph batch_norm. (#29221)
* Add ReserveSpace in dygraph batch_norm. * Add unittest for reservespace
上级
b781953e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
29 addition
and
0 deletion
+29
-0
python/paddle/fluid/dygraph/nn.py
python/paddle/fluid/dygraph/nn.py
+15
-0
python/paddle/fluid/tests/unittests/test_batch_norm_op.py
python/paddle/fluid/tests/unittests/test_batch_norm_op.py
+14
-0
未找到文件。
python/paddle/fluid/dygraph/nn.py
浏览文件 @
40019793
...
...
@@ -31,6 +31,7 @@ from ..data_feeder import check_variable_and_dtype, check_type
import
numpy
as
np
import
numbers
import
logging
import
os
import
paddle.utils.deprecated
as
deprecated
__all__
=
[
...
...
@@ -1308,6 +1309,12 @@ class BatchNorm(layers.Layer):
dtype
=
self
.
_dtype
)
self
.
_variance
.
stop_gradient
=
True
self
.
_has_reserve_space
=
False
if
data_layout
==
'NHWC'
:
flag
=
os
.
environ
.
get
(
'FLAGS_cudnn_batchnorm_spatial_persistent'
)
if
flag
is
not
None
and
flag
.
lower
()
in
[
'true'
,
'1'
]:
self
.
_has_reserve_space
=
True
self
.
_in_place
=
in_place
self
.
_data_layout
=
data_layout
self
.
_momentum
=
momentum
...
...
@@ -1364,6 +1371,12 @@ class BatchNorm(layers.Layer):
dtype
=
self
.
_dtype
,
stop_gradient
=
True
)
saved_variance
=
self
.
_helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_dtype
,
stop_gradient
=
True
)
reserve_space
=
None
if
self
.
_has_reserve_space
:
reserve_space
=
self
.
_helper
.
create_variable_for_type_inference
(
dtype
=
core
.
VarDesc
.
VarType
.
FP16
,
stop_gradient
=
True
)
batch_norm_out
=
input
if
self
.
_in_place
else
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
...
...
@@ -1374,6 +1387,8 @@ class BatchNorm(layers.Layer):
"SavedMean"
:
[
saved_mean
],
"SavedVariance"
:
[
saved_variance
]
}
if
reserve_space
is
not
None
:
outputs
[
"ReserveSpace"
]
=
reserve_space
self
.
_helper
.
append_op
(
type
=
"batch_norm"
,
inputs
=
inputs
,
outputs
=
outputs
,
attrs
=
attrs
)
...
...
python/paddle/fluid/tests/unittests/test_batch_norm_op.py
浏览文件 @
40019793
...
...
@@ -17,6 +17,7 @@ from __future__ import print_function
import
os
import
unittest
import
numpy
as
np
import
paddle
import
paddle.fluid.core
as
core
from
paddle.fluid.op
import
Operator
import
paddle.fluid
as
fluid
...
...
@@ -671,5 +672,18 @@ class TestDygraphBatchNormTrainableStats(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
y1
,
y2
))
class
TestDygraphBatchNormOpenReserveSpace
(
unittest
.
TestCase
):
def
test_reservespace
(
self
):
with
program_guard
(
Program
(),
Program
()):
paddle
.
enable_static
()
x
=
np
.
random
.
random
(
size
=
(
3
,
10
,
3
,
7
)).
astype
(
'float32'
)
x
=
fluid
.
data
(
name
=
'x'
,
shape
=
x
.
shape
,
dtype
=
x
.
dtype
)
# Set this FLAG, the BatchNorm API will pass "reserve_space" argument into batch_norm op.
os
.
environ
[
'FLAGS_cudnn_batchnorm_spatial_persistent'
]
=
'1'
batch_norm
=
fluid
.
dygraph
.
BatchNorm
(
7
,
data_layout
=
"NHWC"
)
hidden1
=
batch_norm
(
x
)
os
.
environ
[
'FLAGS_cudnn_batchnorm_spatial_persistent'
]
=
'0'
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录