Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f47a5f7f
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
f47a5f7f
编写于
3月 21, 2023
作者:
C
cyber-pioneer
提交者:
GitHub
3月 21, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[prim] simplify batch_norm composite rule (#51827)
* simplify batch_norm composite rule * polish code
上级
bef4e9f7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
34 addition
and
26 deletion
+34
-26
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
...luid/prim/api/composite_backward/composite_backward_api.h
+1
-1
python/paddle/incubate/autograd/composite_rules.py
python/paddle/incubate/autograd/composite_rules.py
+33
-25
未找到文件。
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
浏览文件 @
f47a5f7f
...
...
@@ -1192,7 +1192,7 @@ void batch_norm_grad(const Tensor& x,
auto
eps
=
full
<
T
>
(
phi
::
vectorize
(
run_var
.
dims
()),
epsilon
,
run_var
.
dtype
());
mean_data
=
run_mean
;
rsqrt_var
=
1
/
(
run_var
+
eps
).
pow
(
0.5
);
rsqrt_var
=
(
run_var
+
eps
).
pow
(
-
0.5
);
}
else
{
mean_data
=
saved_mean
;
rsqrt_var
=
saved_variance
;
...
...
python/paddle/incubate/autograd/composite_rules.py
浏览文件 @
f47a5f7f
...
...
@@ -71,7 +71,11 @@ def composite_batchnorm(
use_global_stats
,
trainable_statistics
,
):
"""define composite rule of op batch_norm"""
"""
define composite rule of op batch_norm
As the same with op kernel, the position of savedvariance indeed return inverse std.
"""
is_amp
=
False
from
paddle.fluid.data_feeder
import
convert_dtype
...
...
@@ -94,38 +98,42 @@ def composite_batchnorm(
1
if
i
in
reduce_axes
else
s
for
i
,
s
in
enumerate
(
x
.
shape
)
)
batch_mean
=
zeros
(
run_mean
.
shape
,
run_mean
.
dtype
)
batch_var
=
zeros
(
run_var
.
shape
,
run_var
.
dtype
)
if
not
use_run_stat
:
half
=
-
0.5
batch_mean
=
mean
(
x
,
reduce_axes
,
keepdim
=
True
)
temp
=
mean
(
x
*
x
,
reduce_axes
,
keepdim
=
True
)
if
not
use_run_stat
:
batch_mean
=
mean
(
x
,
reduce_axes
)
temp
=
mean
(
x
*
x
,
reduce_axes
)
batch_var
=
temp
-
batch_mean
*
batch_mean
inv_std
=
pow
((
batch_var
+
epsilon
),
half
)
if
data_layout
==
"NHWC"
:
x_hat
=
(
x
-
batch_mean
)
*
inv_std
else
:
x_hat
=
(
x
-
reshape
(
batch_mean
,
stats_shape
))
*
reshape
(
inv_std
,
stats_shape
)
x_hat
=
(
x
-
reshape
(
batch_mean
,
stats_shape
))
/
sqrt
(
reshape
(
batch_var
,
stats_shape
)
+
epsilon
)
run_mean
=
momentum
*
run_mean
+
(
1
-
momentum
)
*
reshape
(
batch_mean
,
run_mean
.
shape
)
run_var
=
momentum
*
run_var
+
(
1
-
momentum
)
*
reshape
(
batch_var
,
run_var
.
shape
)
run_mean
=
momentum
*
run_mean
+
(
1
-
momentum
)
*
batch_mean
run_var
=
momentum
*
run_var
+
(
1
-
momentum
)
*
batch_var
else
:
x_hat
=
(
x
-
reshape
(
run_mean
,
stats_shape
))
/
sqrt
(
reshape
(
run_var
,
stats_shape
)
+
epsilon
)
y
=
reshape
(
scale
,
stats_shape
)
*
x_hat
+
reshape
(
bias
,
stats_shape
)
batch_mean
=
zeros
(
run_mean
.
shape
,
run_mean
.
dtype
)
batch_var
=
zeros
(
run_var
.
shape
,
run_var
.
dtype
)
inv_std
=
pow
((
batch_var
+
epsilon
),
half
)
if
data_layout
==
"NHWC"
:
x_hat
=
(
x
-
run_mean
)
*
pow
((
run_var
+
epsilon
),
half
)
else
:
x_hat
=
(
x
-
reshape
(
run_mean
,
stats_shape
))
*
pow
(
(
reshape
(
run_var
,
stats_shape
)
+
epsilon
),
half
)
if
data_layout
==
"NHWC"
:
y
=
scale
*
x_hat
+
bias
else
:
y
=
reshape
(
scale
,
stats_shape
)
*
x_hat
+
reshape
(
bias
,
stats_shape
)
if
is_amp
:
y
=
cast
(
y
,
"float16"
)
# As the same with op kernel, indeed return inverse std
inv_std
=
1.0
/
sqrt
(
batch_var
+
epsilon
)
# add op assign to detach tensor in void unsafe change outside the rule.
batch_mean_
=
assign
(
reshape
(
batch_mean
,
run_mean
.
shape
)
)
inv_std_
=
assign
(
reshape
(
inv_std
,
run_var
.
shape
)
)
batch_mean_
=
assign
(
batch_mean
)
inv_std_
=
assign
(
inv_std
)
run_mean_
=
assign
(
run_mean
)
run_var_
=
assign
(
run_var
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录