Skip to content

Fix 空指针 (Null pointer) of case15: paddle.broadcast_tensors #49980

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

Merged
merged 2 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
2 changes: 1 addition & 1 deletion paddle/phi/infermeta/multiary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ void BroadcastTensorsInferMeta(const std::vector<const MetaTensor*>& x,

// We performed bcast semantics check at python level
// So input tensors should all have legal shape
target_dim_size = std::max(target_dim_size, dim_size);
target_dim_size = dim_size == 1 ? target_dim_size : dim_size;
}
target_dims[target_rank - index - 1] = target_dim_size;
}
Expand Down
14 changes: 9 additions & 5 deletions python/paddle/fluid/tests/unittests/test_broadcast_tensors_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ def find_output_shape(input_list):
rank = len(x.shape)
output_rank = max(output_rank, rank)

output_shape = [0 for i in range(output_rank)]
output_shape = [1 for i in range(output_rank)]
for i in range(output_rank):
for x in input_list:
shape = list(reversed(x.shape))
size = 1
if i < len(shape):
size = shape[i]
output_shape[i] = max(output_shape[i], size)
if i < len(shape) and shape[i] != 1:
output_shape[i] = shape[i]
Comment on lines +36 to +41
Copy link
Contributor

Choose a reason for hiding this comment

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

为什么要修改这个单测呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

如果不修改的话,默认的方法不支持含有 dim = 0 的数组。

因为原有 max 取 dim 和 size = 1 时,会忽略 dim == 0 的维度。


return list(reversed(output_shape))

Expand Down Expand Up @@ -80,6 +78,11 @@ def gen_mixed_tensors_test(dtype):
return make_inputs_outputs(input_shapes, dtype)


def gen_empty_tensors_test(dtype):
input_shapes = [(0), (0), (0)]
return make_inputs_outputs(input_shapes, dtype)


class TestCPUBroadcastTensorsOp(OpTest):
def set_place(self):
self.place = core.CPUPlace()
Expand All @@ -95,6 +98,7 @@ def setUp(self):
gen_rank_diff_test,
gen_no_broadcast_test,
gen_mixed_tensors_test,
gen_empty_tensors_test,
]
self.set_place()
self.set_dtypes()
Expand Down