Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
1c0db09a
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看板
未验证
提交
1c0db09a
编写于
8月 24, 2023
作者:
N
niuliling123
提交者:
GitHub
8月 24, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add enable/disable_model_check_nan_inf op (#54081)
上级
a15fec8b
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
244 addition
and
0 deletion
+244
-0
paddle/phi/api/yaml/legacy_backward.yaml
paddle/phi/api/yaml/legacy_backward.yaml
+22
-0
paddle/phi/api/yaml/legacy_ops.yaml
paddle/phi/api/yaml/legacy_ops.yaml
+22
-0
paddle/phi/kernels/cpu/debug_tools_kernel.cc
paddle/phi/kernels/cpu/debug_tools_kernel.cc
+35
-0
paddle/phi/kernels/gpu/debug_tools_kernel.cu
paddle/phi/kernels/gpu/debug_tools_kernel.cu
+36
-0
paddle/phi/kernels/impl/debug_tools_impl.h
paddle/phi/kernels/impl/debug_tools_impl.h
+36
-0
python/paddle/amp/debugging.py
python/paddle/amp/debugging.py
+72
-0
test/amp/test_tensor_checker.py
test/amp/test_tensor_checker.py
+21
-0
未找到文件。
paddle/phi/api/yaml/legacy_backward.yaml
浏览文件 @
1c0db09a
...
...
@@ -758,6 +758,28 @@
kernel
:
func
:
triu_grad
-
backward_op
:
disable_check_model_nan_inf_grad
forward
:
disable_check_model_nan_inf (Tensor x, int flag=0) -> Tensor(out)
args
:
(Tensor out_grad, int unsetflag = 1)
output
:
Tensor(x_grad)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
out_grad
]
kernel
:
func
:
check_model_nan_inf
data_type
:
out_grad
-
backward_op
:
enable_check_model_nan_inf_grad
forward
:
enable_check_model_nan_inf (Tensor x, int flag=1) -> Tensor(out)
args
:
(Tensor out_grad, int unsetflag = 0)
output
:
Tensor(x_grad)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
out_grad
]
kernel
:
func
:
check_model_nan_inf
data_type
:
out_grad
-
backward_op
:
unpool_grad
forward
:
unpool (Tensor x, Tensor indices, int[] ksize, int[] strides, int[] padding, IntArray output_size, str data_format) -> Tensor(out)
args
:
(Tensor x, Tensor indices, Tensor out, Tensor out_grad, int[] ksize, int[] strides, int[] padding, IntArray output_size, str data_format)
...
...
paddle/phi/api/yaml/legacy_ops.yaml
浏览文件 @
1c0db09a
...
...
@@ -209,6 +209,17 @@
data_type
:
x
backward
:
depthwise_conv2d_transpose_grad
-
op
:
disable_check_model_nan_inf
args
:
(Tensor x, int flag = 0)
output
:
Tensor(out)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
x
]
kernel
:
func
:
check_model_nan_inf
data_type
:
x
backward
:
disable_check_model_nan_inf_grad
-
op
:
distribute_fpn_proposals
args
:
(Tensor fpn_rois, Tensor rois_num, int min_level, int max_level, int refer_level, int refer_scale, bool pixel_offset)
output
:
Tensor[](multi_fpn_rois){max_level - min_level + 1}, Tensor[](multi_level_rois_num){max_level - min_level + 1}, Tensor(restore_index)
...
...
@@ -305,6 +316,17 @@
data_type
:
dtype > x
backend
:
place > x
-
op
:
enable_check_model_nan_inf
args
:
(Tensor x, int flag = 1)
output
:
Tensor(out)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
x
]
kernel
:
func
:
check_model_nan_inf
data_type
:
x
backward
:
enable_check_model_nan_inf_grad
-
op
:
equal
args
:
(Tensor x, Tensor y)
output
:
Tensor(out)
...
...
paddle/phi/kernels/cpu/debug_tools_kernel.cc
0 → 100644
浏览文件 @
1c0db09a
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/impl/debug_tools_impl.h"
PD_REGISTER_KERNEL
(
check_model_nan_inf
,
CPU
,
ALL_LAYOUT
,
phi
::
CheckModelNanInfKernel
,
bool
,
float
,
double
,
int32_t
,
int64_t
,
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
,
phi
::
dtype
::
complex
<
float
>
,
phi
::
dtype
::
complex
<
double
>
)
{}
paddle/phi/kernels/gpu/debug_tools_kernel.cu
0 → 100644
浏览文件 @
1c0db09a
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/backends/gpu/gpu_launch_config.h"
#include "paddle/phi/backends/gpu/gpu_primitives.h"
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/debug_tools_impl.h"
PD_REGISTER_KERNEL
(
check_model_nan_inf
,
GPU
,
ALL_LAYOUT
,
phi
::
CheckModelNanInfKernel
,
bool
,
float
,
double
,
int32_t
,
int64_t
,
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
,
phi
::
dtype
::
complex
<
float
>
,
phi
::
dtype
::
complex
<
double
>
)
{}
paddle/phi/kernels/impl/debug_tools_impl.h
0 → 100644
浏览文件 @
1c0db09a
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "glog/logging.h"
#include "paddle/phi/core/flags.h"
#include "paddle/phi/kernels/cast_kernel.h"
#include "paddle/phi/kernels/funcs/math_function.h"
PHI_DECLARE_bool
(
check_nan_inf
);
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
CheckModelNanInfKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
int
flag
,
DenseTensor
*
out
)
{
phi
::
CastKernel
<
T
>
(
dev_ctx
,
x
,
x
.
dtype
(),
out
);
VLOG
(
6
)
<<
"model_check_nan_inf: Change FLAGS_check_nan_inf "
<<
FLAGS_check_nan_inf
<<
" to "
<<
flag
;
FLAGS_check_nan_inf
=
flag
;
}
}
// namespace phi
python/paddle/amp/debugging.py
浏览文件 @
1c0db09a
...
...
@@ -35,6 +35,7 @@ __all__ = [
"enable_tensor_checker"
,
"disable_tensor_checker"
,
"compare_accuracy"
,
"check_layer_numerics"
,
]
...
...
@@ -60,6 +61,77 @@ class DebugMode(Enum):
# DUMP_ALL = 5
def
check_layer_numerics
(
func
):
"""
This decorator is used to check the numerical values of the layer's input and output data.
Args:
func (callable): The function to be decorated.
Returns:
callable: The decorated function.
Raises:
None.
Example:
import paddle
class MyLayer(paddle.nn.Layer):
def __init__(self, dtype):
super().__init__()
self._w = self.create_parameter([2, 3], dtype=dtype)
self._b = self.create_parameter([2, 3], dtype=dtype)
@paddle.amp.debugging.check_layer_numerics
def forward(self, x):
# return 1/x * self._w + self._b open it you will see the error log
return x * self._w + self._b
dtype = 'float32'
x = paddle.rand([10, 2, 2], dtype=dtype)
model = MyLayer(dtype)
x[0] = float(0)
loss = model(x)
adam = paddle.optimizer.Adam(parameters=model.parameters())
loss.backward()
adam.step()
#error log
#[PRECISION] [ERROR] in [device=gpu:0, op=divide, tensor=, dtype=fp32], numel=40, num_nan=0, num_inf=4, num_zero=0, max=inf, min=1.048930e+00, mean=inf
#Traceback (most recent call last):
# File "tmp.py", line 16, in <module>
# loss = model(x)
# File "/paddle/nn/layer/layers.py", line 1254, in __call__
# return self.forward(*inputs, **kwargs)
# File "/paddle/amp/debugging.py", line 116, in wrapper
# out_data = func(self, *modified_args, **kwargs)
# File "test.py", line 10, in forward
# return 1/x * self._w+ self._b
#RuntimeError: (PreconditionNotMet) There are NAN or INF (num_nan=0, num_inf=4, num_zero=0) in [device=gpu:0, op=divide, tensor=, dtype=fp32].
"""
def
wrapper
(
self
,
*
args
,
**
kwargs
):
if
args
:
# Set temp data and temp.gradient = False
start_data
=
args
[
0
]
start_data
.
stop_gradient
=
False
modified_args
=
list
(
args
)
# Convert args to a mutable list
# Set FLAGS_check_nan_inf = 1
modified_args
[
0
]
=
_C_ops
.
enable_check_model_nan_inf
(
start_data
,
1
)
# Call the forward function
out_data
=
func
(
self
,
*
modified_args
,
**
kwargs
)
# Set FLAGS_check_nan_inf = 0
out
=
_C_ops
.
disable_check_model_nan_inf
(
out_data
,
0
)
return
out
else
:
print
(
"No elements found in args"
)
out
=
func
(
self
,
*
args
,
**
kwargs
)
return
out
return
wrapper
def
set_checked_op_list
(
checked_op_list
):
# check checked_op_list
if
checked_op_list
is
not
None
:
...
...
test/amp/test_tensor_checker.py
浏览文件 @
1c0db09a
...
...
@@ -118,5 +118,26 @@ class TestTensorChecker(unittest.TestCase):
_assert_flag
(
False
)
class
TestCheckLayerNumerics
(
unittest
.
TestCase
):
def
test_layer_checker
(
self
):
class
MyLayer
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
,
dtype
):
super
().
__init__
()
self
.
_w
=
self
.
create_parameter
([
2
,
3
],
dtype
=
dtype
)
self
.
_b
=
self
.
create_parameter
([
2
,
3
],
dtype
=
dtype
)
@
paddle
.
amp
.
debugging
.
check_layer_numerics
def
forward
(
self
,
x
):
return
x
*
self
.
_w
+
self
.
_b
dtype
=
'float32'
x
=
paddle
.
rand
([
10
,
2
,
3
],
dtype
=
dtype
)
model
=
MyLayer
(
dtype
)
loss
=
model
(
x
)
adam
=
paddle
.
optimizer
.
Adam
(
parameters
=
model
.
parameters
())
loss
.
backward
()
adam
.
step
()
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录