Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e0b58212
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
e0b58212
编写于
3月 22, 2023
作者:
C
cyber-pioneer
提交者:
GitHub
3月 22, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Prim] fix batch_norm custom_vjp dtype (#51843)
* fix batch_norm custom_vjp dtype * add nhwc test example * fix typo
上级
f293e36c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
19 addition
and
12 deletion
+19
-12
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
...luid/prim/api/composite_backward/composite_backward_api.h
+1
-1
python/paddle/fluid/tests/unittests/prim/composite_ops/test_composite_batch_norm.py
...unittests/prim/composite_ops/test_composite_batch_norm.py
+18
-11
未找到文件。
paddle/fluid/prim/api/composite_backward/composite_backward_api.h
浏览文件 @
e0b58212
...
...
@@ -1282,7 +1282,7 @@ void batch_norm_grad(const Tensor& x,
auto
tmp
=
out_grad_data
*
x_sub_mean
*
rsqrt_var
*
rsqrt_var
/
nhw
;
auto
mean_temp2
=
sum
<
T
>
(
tmp
,
reduce_axis
,
dtype
,
false
);
auto
part2
=
out_grad
-
mean_temp1
-
x_sub_mean
*
mean_temp2
;
auto
part2
=
out_grad
_data
-
mean_temp1
-
x_sub_mean
*
mean_temp2
;
auto
x_grad_data
=
part1
*
part2
;
if
(
x
.
dtype
()
==
phi
::
DataType
::
FLOAT16
)
{
...
...
python/paddle/fluid/tests/unittests/prim/composite_ops/test_composite_batch_norm.py
浏览文件 @
e0b58212
...
...
@@ -349,14 +349,10 @@ def apply_to_static(net, use_cinn):
class
PrimeNet
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
):
def
__init__
(
self
,
data_layout
=
'NCHW'
):
super
().
__init__
()
self
.
conv
=
nn
.
Conv2D
(
4
,
2
,
(
3
,
3
),
bias_attr
=
False
)
self
.
bn
=
BatchNorm
(
2
,
act
=
"relu"
)
self
.
run_mean
=
zeros
([
2
])
self
.
run_var
=
ones
([
2
])
self
.
scale
=
ones
([
2
])
self
.
bias
=
ones
([
2
])
self
.
conv
=
nn
.
Conv2D
(
2
,
4
,
(
3
,
3
),
bias_attr
=
False
)
self
.
bn
=
BatchNorm
(
4
,
act
=
"relu"
,
data_layout
=
data_layout
)
def
forward
(
self
,
x
):
y
=
self
.
conv
(
x
)
...
...
@@ -372,13 +368,13 @@ class TestPrimForwardAndBackward(unittest.TestCase):
def
setUp
(
self
):
paddle
.
seed
(
2022
)
self
.
x
=
paddle
.
randn
([
4
,
4
,
6
,
6
],
dtype
=
"float32"
)
self
.
x
=
paddle
.
randn
([
4
,
2
,
6
,
6
],
dtype
=
"float32"
)
self
.
x
.
stop_gradient
=
False
def
train
(
self
,
use_prim
):
def
train
(
self
,
use_prim
,
data_layout
=
"NCHW"
):
core
.
_set_prim_all_enabled
(
use_prim
)
paddle
.
seed
(
2022
)
net
=
PrimeNet
()
net
=
PrimeNet
(
data_layout
)
sgd
=
paddle
.
optimizer
.
SGD
(
learning_rate
=
0.1
,
parameters
=
net
.
parameters
()
)
...
...
@@ -394,7 +390,7 @@ class TestPrimForwardAndBackward(unittest.TestCase):
sgd
.
clear_grad
()
return
loss
def
test_amp
(
self
):
def
test_amp
_nchw
(
self
):
if
not
isinstance
(
framework
.
_current_expected_place
(),
core
.
CPUPlace
):
expected
=
self
.
train
(
False
)
actual
=
self
.
train
(
True
)
...
...
@@ -405,6 +401,17 @@ class TestPrimForwardAndBackward(unittest.TestCase):
atol
=
1e-3
,
)
def
test_amp_nhwc
(
self
):
if
not
isinstance
(
framework
.
_current_expected_place
(),
core
.
CPUPlace
):
expected
=
self
.
train
(
use_prim
=
False
,
data_layout
=
"NHWC"
)
actual
=
self
.
train
(
use_prim
=
True
,
data_layout
=
"NHWC"
)
np
.
testing
.
assert_allclose
(
expected
,
actual
,
rtol
=
1e-3
,
atol
=
1e-3
,
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录