From d7e0f875700b121c01f63aab1a41066833b1fc4c Mon Sep 17 00:00:00 2001 From: niuliling123 <51102941+niuliling123@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:32:51 +0800 Subject: [PATCH] Change the print in debugging to RuntimeError (#56622) --- paddle/phi/kernels/cpu/debug_tools_kernel.cc | 2 +- paddle/phi/kernels/gpu/debug_tools_kernel.cu | 2 +- python/paddle/amp/debugging.py | 4 ++- test/amp/test_tensor_checker.py | 31 ++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/paddle/phi/kernels/cpu/debug_tools_kernel.cc b/paddle/phi/kernels/cpu/debug_tools_kernel.cc index b158e111a22..45e15b58a9c 100644 --- a/paddle/phi/kernels/cpu/debug_tools_kernel.cc +++ b/paddle/phi/kernels/cpu/debug_tools_kernel.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// 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. diff --git a/paddle/phi/kernels/gpu/debug_tools_kernel.cu b/paddle/phi/kernels/gpu/debug_tools_kernel.cu index 0997b9b43a7..7b7136b32a1 100644 --- a/paddle/phi/kernels/gpu/debug_tools_kernel.cu +++ b/paddle/phi/kernels/gpu/debug_tools_kernel.cu @@ -1,4 +1,4 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// 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. diff --git a/python/paddle/amp/debugging.py b/python/paddle/amp/debugging.py index 6ac8b6fd253..243f1d56d48 100644 --- a/python/paddle/amp/debugging.py +++ b/python/paddle/amp/debugging.py @@ -115,6 +115,8 @@ def check_layer_numerics(func): if args: # Set temp data and temp.gradient = False start_data = args[0] + if not isinstance(start_data, paddle.Tensor): + raise RuntimeError("First input of this layer must be tensor.") start_data.stop_gradient = False modified_args = list(args) # Convert args to a mutable list # Set FLAGS_check_nan_inf = 1 @@ -125,7 +127,7 @@ def check_layer_numerics(func): out = _C_ops.disable_check_model_nan_inf(out_data, 0) return out else: - print("No elements found in args") + raise RuntimeError("No elements found in args.") out = func(self, *args, **kwargs) return out diff --git a/test/amp/test_tensor_checker.py b/test/amp/test_tensor_checker.py index d8616571211..7bfa24897f0 100644 --- a/test/amp/test_tensor_checker.py +++ b/test/amp/test_tensor_checker.py @@ -138,6 +138,37 @@ class TestCheckLayerNumerics(unittest.TestCase): loss.backward() adam.step() + def test_error_no_element(self): + class MyLayer(paddle.nn.Layer): + def __init__(self, dtype): + super().__init__() + self._w = self.create_parameter([2, 3], dtype=dtype) + + @paddle.amp.debugging.check_layer_numerics + def forward(self): + return self._w + + with self.assertRaises(RuntimeError): + dtype = 'float32' + model = MyLayer(dtype) + data = model() + + def test_error_type_error(self): + class MyLayer(paddle.nn.Layer): + def __init__(self, dtype): + super().__init__() + self._w = self.create_parameter([2, 3], dtype=dtype) + + @paddle.amp.debugging.check_layer_numerics + def forward(self, x): + return self._w * x + + x = 1 + with self.assertRaises(RuntimeError): + dtype = 'float32' + model = MyLayer(dtype) + data = model(x) + if __name__ == '__main__': unittest.main() -- GitLab