Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some PADDLE_THROW error type and change test cases #60487

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/common/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ enum ErrorCode {
EXTERNAL = 12,

// Client specified an unmatched type.
// Error type string: "INVALID_TYPE"
// Error type string: "InvalidTypeError"
INVALID_TYPE = 13,
};

Expand Down
88 changes: 44 additions & 44 deletions paddle/fluid/pybind/eager_utils.cc

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions paddle/fluid/pybind/op_function_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ bool CastPyArg2Boolean(PyObject* obj,
} else if (obj == Py_True) {
return true;
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

辛苦把这里需要改动的原因在pr描述中补充一下吧~

"%s(): argument (position %d) must be "
"bool, but got %s",
op_type,
Expand All @@ -158,7 +158,7 @@ int CastPyArg2Int(PyObject* obj, const std::string& op_type, ssize_t arg_pos) {
if (PyObject_CheckLongOrToLong(&obj)) {
return (int)PyLong_AsLong(obj); // NOLINT
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"int, but got %s",
op_type,
Expand All @@ -183,7 +183,7 @@ int64_t CastPyArg2Long(PyObject* obj,
if (PyObject_CheckLongOrToLong(&obj)) {
return (int64_t)PyLong_AsLongLong(obj); // NOLINT
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"long, but got %s",
op_type,
Expand Down Expand Up @@ -236,7 +236,7 @@ double CastPyArg2Double(PyObject* obj,
if (PyObject_CheckFloatOrToFloat(&obj)) {
return PyFloat_AsDouble(obj); // NOLINT
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"double, but got %s",
op_type,
Expand All @@ -260,7 +260,7 @@ phi::dtype::complex<float> CastPyArg2Complex(PyObject* obj,
Py_complex v = PyComplex_AsCComplex(obj);
return phi::dtype::complex<float>(v.real, v.imag);
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"complex, but got %s",
op_type,
Expand All @@ -279,7 +279,7 @@ phi::dtype::complex<double> CastPyArg2Complex128(PyObject* obj,
double imag = PyComplex_ImagAsDouble(obj);
return phi::dtype::complex<double>(real, imag);
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"complex, but got %s",
op_type,
Expand Down Expand Up @@ -307,7 +307,7 @@ std::string CastPyArg2String(PyObject* obj,
data = PyUnicode_AsUTF8AndSize(obj, &size);
return std::string(data, (size_t)size); // NOLINT
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"str, but got %s",
op_type,
Expand Down Expand Up @@ -338,7 +338,7 @@ std::vector<bool> CastPyArg2Booleans(PyObject* obj,
if (PyObject_CheckBool(&item)) {
value.emplace_back(PyLong_AsLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of bool, but got %s at pos %d",
op_type,
Expand All @@ -355,7 +355,7 @@ std::vector<bool> CastPyArg2Booleans(PyObject* obj,
if (PyObject_CheckBool(&item)) {
value.emplace_back(PyLong_AsLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of bool, but got %s at pos %d",
op_type,
Expand All @@ -365,7 +365,7 @@ std::vector<bool> CastPyArg2Booleans(PyObject* obj,
}
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list or tuple, but got %s",
op_type,
Expand Down Expand Up @@ -397,7 +397,7 @@ std::vector<int> CastPyArg2Ints(PyObject* obj,
if (PyObject_CheckLongOrToLong(&item)) {
value.emplace_back(PyLong_AsLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of int, but got %s at pos %d",
op_type,
Expand All @@ -415,7 +415,7 @@ std::vector<int> CastPyArg2Ints(PyObject* obj,
if (PyObject_CheckLongOrToLong(&item)) {
value.emplace_back(PyLong_AsLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of int, but got %s at pos %d",
op_type,
Expand All @@ -433,7 +433,7 @@ std::vector<int> CastPyArg2Ints(PyObject* obj,
if (PyObject_CheckLongOrToLong(&item)) {
value.emplace_back(PyLong_AsLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of int, but got %s at pos %d",
op_type,
Expand All @@ -443,7 +443,7 @@ std::vector<int> CastPyArg2Ints(PyObject* obj,
}
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list or tuple, but got %s",
op_type,
Expand Down Expand Up @@ -474,7 +474,7 @@ std::vector<int64_t> CastPyArg2Longs(PyObject* obj,
if (PyObject_CheckLongOrToLong(&item)) {
value.emplace_back((int64_t)PyLong_AsLongLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of int, but got %s at pos %d",
op_type,
Expand All @@ -491,7 +491,7 @@ std::vector<int64_t> CastPyArg2Longs(PyObject* obj,
if (PyObject_CheckLongOrToLong(&item)) {
value.emplace_back((int64_t)PyLong_AsLongLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of int, but got %s at pos %d",
op_type,
Expand All @@ -508,7 +508,7 @@ std::vector<int64_t> CastPyArg2Longs(PyObject* obj,
if (PyObject_CheckLongOrToLong(&item)) {
value.emplace_back((int64_t)PyLong_AsLongLong(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of int, but got %s at pos %d",
op_type,
Expand All @@ -522,7 +522,7 @@ std::vector<int64_t> CastPyArg2Longs(PyObject* obj,
} else if (PyObject_CheckLongOrToLong(&obj)) {
return {(int64_t)PyLong_AsLongLong(obj)};
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list or tuple, but got %s",
op_type,
Expand Down Expand Up @@ -553,7 +553,7 @@ std::vector<float> CastPyArg2Floats(PyObject* obj,
if (PyObject_CheckFloatOrToFloat(&item)) {
value.emplace_back(PyFloat_AsDouble(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of float, but got %s at pos %d",
op_type,
Expand All @@ -570,7 +570,7 @@ std::vector<float> CastPyArg2Floats(PyObject* obj,
if (PyObject_CheckFloatOrToFloat(&item)) {
value.emplace_back(PyFloat_AsDouble(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of float, but got %s at pos %d",
op_type,
Expand All @@ -587,7 +587,7 @@ std::vector<float> CastPyArg2Floats(PyObject* obj,
if (PyObject_CheckFloatOrToFloat(&item)) {
value.emplace_back(PyFloat_AsDouble(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of float, but got %s at pos %d",
op_type,
Expand All @@ -597,7 +597,7 @@ std::vector<float> CastPyArg2Floats(PyObject* obj,
}
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list or tuple, but got %s",
op_type,
Expand Down Expand Up @@ -628,7 +628,7 @@ std::vector<double> CastPyArg2Float64s(PyObject* obj,
if (PyObject_CheckFloatOrToFloat(&item)) {
value.emplace_back(PyFloat_AsDouble(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of float, but got %s at pos %d",
op_type,
Expand All @@ -645,7 +645,7 @@ std::vector<double> CastPyArg2Float64s(PyObject* obj,
if (PyObject_CheckFloatOrToFloat(&item)) {
value.emplace_back(PyFloat_AsDouble(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of float, but got %s at pos %d",
op_type,
Expand All @@ -662,7 +662,7 @@ std::vector<double> CastPyArg2Float64s(PyObject* obj,
if (PyObject_CheckFloatOrToFloat(&item)) {
value.emplace_back(PyFloat_AsDouble(item));
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of float, but got %s at pos %d",
op_type,
Expand All @@ -672,7 +672,7 @@ std::vector<double> CastPyArg2Float64s(PyObject* obj,
}
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list or tuple, but got %s",
op_type,
Expand Down Expand Up @@ -714,7 +714,7 @@ std::vector<std::string> CastPyArg2Strings(PyObject* obj,
data = PyUnicode_AsUTF8AndSize(item, &size);
value.emplace_back(std::string(data, (size_t)size)); // NOLINT
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of str, but got %s at pos %d",
op_type,
Expand All @@ -734,7 +734,7 @@ std::vector<std::string> CastPyArg2Strings(PyObject* obj,
data = PyUnicode_AsUTF8AndSize(item, &size);
value.emplace_back(std::string(data, (size_t)size)); // NOLINT
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list of str, but got %s at pos %d",
op_type,
Expand All @@ -744,7 +744,7 @@ std::vector<std::string> CastPyArg2Strings(PyObject* obj,
}
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"list or tuple, but got %s",
op_type,
Expand All @@ -766,7 +766,7 @@ void CastPyArg2AttrStrings(PyObject* obj,
std::vector<paddle::experimental::Scalar> CastPyArg2Scalars(
PyObject* obj, const std::string& op_type, ssize_t arg_pos) {
if (obj == Py_None) {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"a list of int, float, or bool, but got %s",
op_type,
Expand Down Expand Up @@ -808,7 +808,7 @@ std::vector<paddle::experimental::Scalar> CastPyArg2Scalars(
return value;
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"a list of int, float, complex, or bool, but got %s",
op_type,
Expand All @@ -830,7 +830,7 @@ void CastPyArg2AttrBlock(PyObject* obj,
(::pybind11::detail::instance*)obj; // NOLINT

if (!PyObject_TypeCheck((PyObject*)inst, g_blockdesc_pytype)) { // NOLINT
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"BlockDesc, but got %s",
op_type,
Expand Down Expand Up @@ -879,7 +879,7 @@ void CastPyArg2AttrValues(PyObject* obj,
}
}
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
PADDLE_THROW(platform::errors::InvalidType(
"%s(): argument (position %d) must be "
"a list of int, float, complex, or bool, but got %s",
op_type,
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/nn/functional/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def affine_grid(theta, out_shape, align_corners=True, name=None):
[-0.43333334, 2.23333335]]]])
"""
if not isinstance(theta, (Variable, paddle.pir.Value)):
raise ValueError("The theta should be a Tensor.")
raise TypeError("The theta should be a Tensor.")

cudnn_version = get_cudnn_version()
if cudnn_version is not None and cudnn_version >= 6000 and align_corners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_set_static_op_arg_pre_cast_hook(self):

with static_guard():
with self.assertRaisesRegex(
ValueError,
TypeError,
r"abs\(\): argument \(position 1\) must be OpResult, but got Tensor",
):
paddle.abs(eager_tensor)
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_affine_grid_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class AffineGridErrorTestCase(AffineGridTestCase):
def runTest(self):
place = base.CPUPlace()
with dg.guard(place):
with self.assertRaises(ValueError):
with self.assertRaises(TypeError):
self.paddle_dygraph_layer()


Expand Down
5 changes: 1 addition & 4 deletions test/legacy_test/test_deformable_conv_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,7 @@ def test_invalid_input():
in_channels=input[1], out_channels=4, kernel_size=1
)(input, offset, mask)

err_type = (
ValueError if paddle.base.framework.in_pir_mode() else TypeError
)
self.assertRaises(err_type, test_invalid_input)
self.assertRaises(TypeError, test_invalid_input)

def test_invalid_offset():
paddle.enable_static()
Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_fill_constant_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def test_shape_tensor_list_dtype():
)

self.assertRaises(
ValueError,
TypeError,
paddle.tensor.fill_constant,
shape=[1.1],
value=5,
Expand All @@ -511,7 +511,7 @@ def test_shape_tensor_list_dtype():

x3 = np.random.randn(100, 100).astype('int32')
self.assertRaises(
ValueError,
TypeError,
paddle.tensor.fill_constant,
shape=[100, 100],
value=5,
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_gumbel_softmax_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def test_argument2():
x = paddle.to_tensor([0.2, 0.3, 0.4])
paddle.nn.functional.gumbel_softmax(x, axis=1.1)

self.assertRaises(ValueError, test_argument2)
self.assertRaises(TypeError, test_argument2)

paddle.enable_static()

Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_layer_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ def test_errors(self):
layer_norm = paddle.nn.LayerNorm([32, 32])
# the input of LayerNorm must be Variable.
x1 = np.random.random((3, 32, 32)).astype('float32')
self.assertRaises(ValueError, layer_norm, x1)
self.assertRaises(TypeError, layer_norm, x1)


@unittest.skipIf(
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_mean_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_errors(self):

with paddle.pir_utils.IrGuard(), program_guard(Program(), Program()):
input1 = 12
self.assertRaises(ValueError, paddle.mean, input1)
self.assertRaises(TypeError, paddle.mean, input1)

input2 = paddle.static.data(
name='input2', shape=[2, 3, 4, 5], dtype="int32"
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_numel_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_x_type():
input_1 = np.random.random(shape).astype("int32")
out_1 = paddle.numel(input_1)

self.assertRaises(ValueError, test_x_type)
self.assertRaises(TypeError, test_x_type)


if __name__ == '__main__':
Expand Down
Loading