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 空指针 (Null pointer) of case 14 paddle.atan2 #49973

Merged
merged 3 commits into from
Jan 31, 2023
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
20 changes: 20 additions & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ void KLDivInferMeta(const MetaTensor& x,
}

void Atan2InferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) {
auto x_dims = x.dims();
auto y_dims = y.dims();

PADDLE_ENFORCE_EQ(
x_dims.size(),
y_dims.size(),
phi::errors::InvalidArgument("The rank (%d) of X shall be same as "
"rank (%d) of Y.",
x_dims.size(),
y_dims.size()));

if (x_dims.size() > 0)
PADDLE_ENFORCE_LE(x_dims[0],
y_dims[0],
phi::errors::InvalidArgument(
"The count (%d) of elements of X shall not "
"greater than count (%d) of elements of Y.",
x_dims[0],
y_dims[0]));

out->share_meta(x);
if (x.dtype() == DataType::INT32 || x.dtype() == DataType::INT64 ||
y.dtype() == DataType::INT32 || y.dtype() == DataType::INT64) {
Expand Down
8 changes: 8 additions & 0 deletions paddle/phi/kernels/impl/atan2_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ void Atan2Kernel(const Context& ctx,
auto x_data = x.data<T>();
auto y_data = y.data<T>();

PADDLE_ENFORCE_LE(
numel,
y.numel(),
phi::errors::InvalidArgument("The count (%d) of elements of X shall not "
"greater than count (%d) of elements of Y.",
numel,
y.numel()));

auto* out_data = ctx.template Alloc<typename Atan2Out<T>::type>(
out, size_t(x.numel() * sizeof(typename Atan2Out<T>::type)));

Expand Down
12 changes: 12 additions & 0 deletions python/paddle/fluid/tests/unittests/test_atan2_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ def run(place):
run(place)


class TestAtan2Error(unittest.TestCase):
def test_mismatch(self):
paddle.enable_static()

def test_mismatch_numel():
X = paddle.fluid.data('X', (1,), dtype=np.float64)
Y = paddle.fluid.data('Y', (0,), dtype=np.float64)
out = paddle.atan2(X, Y)

self.assertRaises(ValueError, test_mismatch_numel)


if __name__ == '__main__':
paddle.enable_static()
unittest.main()