diff --git a/paddle/phi/kernels/cpu/debug_tools_kernel.cc b/paddle/phi/kernels/cpu/debug_tools_kernel.cc index b158e111a228dada4cefe8dfa18fc63e607fae3f..45e15b58a9c28ad4be853dc7395b4da74a0b0cbd 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 0997b9b43a79e8f8da97e6b6c23f6442ddc5b076..7b7136b32a17fe62f6034bc24dd7718073a63f12 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 6ac8b6fd2536421cea502053f3fc69b7a631648d..243f1d56d482060e8c32333130712f3c7b90cf38 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 d8616571211cbdcb6e3d6cbb21083af3fe30a145..7bfa24897f0cc0de1c68b4f115dd88d064b00968 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()