Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
d523dffb
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d523dffb
编写于
6月 04, 2021
作者:
L
Leo Chen
提交者:
GitHub
6月 04, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NPU] avoid tensor copy in check_finite_and_scale (#33244)
上级
53d3f5eb
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
15 addition
and
31 deletion
+15
-31
paddle/fluid/operators/amp/check_finite_and_unscale_op_npu.cc
...le/fluid/operators/amp/check_finite_and_unscale_op_npu.cc
+15
-31
未找到文件。
paddle/fluid/operators/amp/check_finite_and_unscale_op_npu.cc
浏览文件 @
d523dffb
...
...
@@ -42,13 +42,11 @@ class CheckFiniteAndUnscaleNPUKernel : public framework::OpKernel<T> {
found_inf
->
mutable_data
<
bool
>
(
ctx
.
GetPlace
());
bool
found_inf_data
=
false
;
auto
stream
=
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
// step1: inverse scale
(RealDiv)
// step1: inverse scale
Tensor
const_tensor
;
const_tensor
.
mutable_data
<
T
>
({
1
},
ctx
.
GetPlace
());
FillNpuTensorWithConstant
<
T
>
(
&
const_tensor
,
static_cast
<
T
>
(
1.0
));
...
...
@@ -66,7 +64,6 @@ class CheckFiniteAndUnscaleNPUKernel : public framework::OpKernel<T> {
// NOTE(zhiqiu):
Tensor
tmp
;
tmp
.
mutable_data
<
float
>
({
8
},
ctx
.
GetPlace
());
// NOTE(zhiqiu): NPUGetFloatStatus updates data on input in-place.
// tmp is only placeholder.
const
auto
&
runner_float_status
=
...
...
@@ -81,39 +78,26 @@ class CheckFiniteAndUnscaleNPUKernel : public framework::OpKernel<T> {
{{
"axes"
,
std
::
vector
<
int
>
{
0
}},
{
"keep_dims"
,
true
}});
runner_reduce_sum
.
Run
(
stream
);
std
::
vector
<
float
>
sum_vec
;
TensorToVector
(
sum
,
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>(),
&
sum_vec
);
found_inf_data
=
(
sum_vec
[
0
]
>
1
);
VLOG
(
4
)
<<
"found_inf_data:"
<<
found_inf_data
;
const
auto
&
runner_greater
=
NpuOpRunner
(
"GreaterEqual"
,
{
sum
,
const_tensor
},
{
*
found_inf
},
{});
runner_greater
.
Run
(
stream
);
// NOTE(zhiqiu): The normal logic is :
// out = in, if found_inf = true
// out = in/scale, if found_inf = false
// However, on NPU, in order to avoid stream sync, we do not copy the
// found_inf data to cpu to check whether to unscale or not.
// Instead, we do the Mul no matter found_inf or not.
// And, a fact is, only few steps contains nan/inf during training.
for
(
size_t
i
=
0
;
i
<
xs
.
size
();
++
i
)
{
const
auto
*
x
=
xs
[
i
];
auto
*
out
=
outs
[
i
];
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
if
(
!
found_inf_data
)
{
// MatMul
const
auto
&
runner_matmul
=
NpuOpRunner
(
"Mul"
,
{
*
x
,
*
tmp_inverse_out
},
{
*
out
},
{});
runner_matmul
.
Run
(
stream
);
}
const
auto
&
runner_mul
=
NpuOpRunner
(
"Mul"
,
{
*
x
,
*
tmp_inverse_out
},
{
*
out
},
{});
runner_mul
.
Run
(
stream
);
}
// set found_inf to true
VLOG
(
4
)
<<
"found overflow:"
<<
found_inf_data
;
Tensor
found_inf_tensor
;
found_inf_tensor
.
Resize
({
1
});
bool
*
is_found_inf
=
found_inf_tensor
.
mutable_data
<
bool
>
(
paddle
::
platform
::
CPUPlace
());
*
is_found_inf
=
found_inf_data
;
framework
::
TensorCopy
(
found_inf_tensor
,
ctx
.
GetPlace
(),
ctx
.
template
device_context
<
platform
::
DeviceContext
>(),
found_inf
);
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>().
Wait
();
const
auto
&
runner_clear_status
=
NpuOpRunner
(
"NPUClearFloatStatus"
,
{
*
float_status
},
{
tmp
});
runner_clear_status
.
Run
(
stream
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录