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

[Broadcast]Fix empty broadcast #50064

Closed
wants to merge 10 commits into from
Closed
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
62 changes: 2 additions & 60 deletions paddle/fluid/operators/common_infer_shape_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/operators/common_infer_shape_functions.h"

#include "paddle/phi/kernels/funcs/common_shape.h"
namespace paddle {
namespace framework {
class InferShapeContext;
Expand All @@ -27,65 +27,7 @@ namespace paddle {
namespace operators {
namespace details {

inline void GetBroadcastDimsArrays(const framework::DDim &x_dims,
const framework::DDim &y_dims,
int *x_dims_array,
int *y_dims_array,
int *out_dims_array,
const int max_dim,
const int axis) {
PADDLE_ENFORCE_GE(
axis,
0,
platform::errors::InvalidArgument(
"Axis should be great than or equal to 0, but received axis is %d.",
axis));
PADDLE_ENFORCE_LE(
axis,
max_dim,
platform::errors::InvalidArgument(
"Axis should be less than or equal to %d, but received axis is %d.",
max_dim,
axis));
if (x_dims.size() > y_dims.size()) {
std::fill(y_dims_array, y_dims_array + axis, 1);
if (axis + y_dims.size() < max_dim) {
std::fill(y_dims_array + axis + y_dims.size(), y_dims_array + max_dim, 1);
}
std::copy(x_dims.Get(), x_dims.Get() + x_dims.size(), x_dims_array);
std::copy(y_dims.Get(), y_dims.Get() + y_dims.size(), y_dims_array + axis);
} else {
std::fill(x_dims_array, x_dims_array + axis, 1);
if (axis + x_dims.size() < max_dim) {
std::fill(x_dims_array + axis + x_dims.size(), x_dims_array + max_dim, 1);
}
std::copy(x_dims.Get(), x_dims.Get() + x_dims.size(), x_dims_array + axis);
std::copy(y_dims.Get(), y_dims.Get() + y_dims.size(), y_dims_array);
}

for (int i = 0; i < max_dim; i++) {
PADDLE_ENFORCE_EQ(
x_dims_array[i] == y_dims_array[i] || x_dims_array[i] <= 1 ||
y_dims_array[i] <= 1,
true,
platform::errors::InvalidArgument(
"Broadcast dimension mismatch. Operands could "
"not be broadcast together with the shape of X = [%s] and "
"the shape of Y = [%s]. Received [%d] in X is not equal to "
"[%d] in Y at i:%d.",
x_dims,
y_dims,
x_dims_array[i],
y_dims_array[i],
i));
if ((x_dims_array[i] > 1 || y_dims_array[i] > 1) ||
(x_dims_array[i] == 1 && y_dims_array[i] == 1)) {
out_dims_array[i] = std::max(x_dims_array[i], y_dims_array[i]);
} else {
out_dims_array[i] = -1;
}
}
}
using phi::funcs::GetBroadcastDimsArrays;

framework::DDim BroadcastTwoDims(const framework::DDim &x_dims,
const framework::DDim &y_dims,
Expand Down
12 changes: 6 additions & 6 deletions paddle/phi/kernels/funcs/common_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ inline void GetBroadcastDimsArrays(const DDim &x_dims,

for (int i = 0; i < max_dim; i++) {
PADDLE_ENFORCE_EQ(
x_dims_array[i] == y_dims_array[i] || x_dims_array[i] <= 1 ||
y_dims_array[i] <= 1,
x_dims_array[i] == y_dims_array[i] || abs(x_dims_array[i]) == 1 ||
abs(y_dims_array[i]) == 1,
true,
phi::errors::InvalidArgument(
"Broadcast dimension mismatch. Operands could "
Expand All @@ -83,11 +83,11 @@ inline void GetBroadcastDimsArrays(const DDim &x_dims,
x_dims_array[i],
y_dims_array[i],
i));
if ((x_dims_array[i] > 1 || y_dims_array[i] > 1) ||
(x_dims_array[i] == 1 && y_dims_array[i] == 1)) {
out_dims_array[i] = (std::max)(x_dims_array[i], y_dims_array[i]);
} else {
if (x_dims_array[i] < 0 || y_dims_array[i] < 0) {
out_dims_array[i] = -1;
} else {
out_dims_array[i] =
x_dims_array[i] == 1 ? y_dims_array[i] : x_dims_array[i];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,12 @@ def test_forward_log_det_jacobian(self, x):
self._t.forward_log_det_jacobian(paddle.to_tensor(x)).shape, [1]
)

@param.param_func([((0, 0), (0, 1))])
def test_forward_empty_tensor(self, shape, expected_shape):
if not paddle.device.is_compiled_with_cuda():
return
self.assertEqual(self._t.forward_shape(shape), expected_shape)


# Todo
@param.place(config.DEVICES)
Expand Down
16 changes: 16 additions & 0 deletions python/paddle/fluid/tests/unittests/test_broadcast_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ def test_error(self):
ValueError, paddle.broadcast_shape, [2, 1, 3], [3, 3, 1]
)

def test_error_native(self):
shape = paddle.broadcast_shape([-1, 1, 3], [2, 3, 1])
self.assertEqual(shape, [-1, 3, 3])

def test_same_dim_zero_shape(self):
shape = paddle.broadcast_shape([0, 1, 1], [1, 3, 0])
self.assertEqual(shape, [0, 3, 0])

def test_same_dim_zero_shape_native(self):
shape = paddle.broadcast_shape([-1, 1, 1], [1, 3, 0])
self.assertEqual(shape, [-1, 3, 0])

def test_diffent_dim_zero_shape(self):
shape = paddle.broadcast_shape([0, 1, 1], [1, 1, 0, 1])
self.assertEqual(shape, [1, 0, 0, 1])


if __name__ == "__main__":
unittest.main()
33 changes: 33 additions & 0 deletions python/paddle/fluid/tests/unittests/test_elementwise_add_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,39 @@ def test_float16_add(self):
self.assertTrue(c.dtype == core.VarDesc.VarType.FP16)


@unittest.skipIf(
not core.is_compiled_with_cuda(), "only support compiled with CUDA"
)
class TestEmptyTensorAddOp(unittest.TestCase):
def test_empty_tensor_add(self):
paddle.disable_static()
x = paddle.to_tensor(np.array([]))
y = paddle.to_tensor(np.array([]))
z = x + y
self.assertEqual(z.shape, [0])

def test_empty_diff_shape_tensor_add1(self):
paddle.disable_static()
x = paddle.to_tensor(np.array([]))
y = paddle.to_tensor(np.array([]).reshape([0, 0]))
z = x + y
self.assertEqual(z.shape, [0, 0])

def test_empty_diff_shape_tensor_add2(self):
paddle.disable_static()
x = paddle.to_tensor(np.array([]))
y = paddle.to_tensor(np.array([]).reshape([0, 1]))
z = x + y
self.assertEqual(z.shape, [0, 0])

def test_empty_diff_shape_tensor_add3(self):
paddle.disable_static()
x = paddle.to_tensor(np.array([]).reshape([0, 1]))
y = paddle.to_tensor(np.array([]).reshape([0, 2]))
z = x + y
self.assertEqual(z.shape, [0, 2])


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