Skip to content
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
3 changes: 3 additions & 0 deletions paddle/phi/kernels/funcs/elementwise_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ void ElementwiseCompute(const CPUContext &dev_ctx,
DenseTensor *z,
int axis = -1) {
dev_ctx.Alloc<OutType>(z);
if (z && z->numel() == 0) {
return;
}
auto x_dims = x.dims();
auto y_dims = y.dims();
bool is_xsize_larger = true;
Expand Down
42 changes: 42 additions & 0 deletions paddle/phi/kernels/impl/elementwise_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,27 @@ void ElementwiseFMaxGradKernel(const Context& dev_ctx,
auto x_dim = x.dims();
auto y_dim = y.dims();
int axis = -1;
if (out_grad.numel() == 0) {
if (x_grad) {
dev_ctx.template Alloc<T>(x_grad);
if (x_grad->numel() != 0) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(x_grad->dims())),
0,
x_grad);
}
}
if (y_grad) {
dev_ctx.template Alloc<T>(y_grad);
if (y_grad->numel() != 0) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(y_grad->dims())),
0,
y_grad);
}
}
return;
}
if (x.dims() == y.dims()) {
funcs::ElemwiseGradComputeNoBroadcast<Context,
T,
Expand Down Expand Up @@ -791,6 +812,27 @@ void ElementwiseFMinGradKernel(const Context& dev_ctx,
DenseTensor* y_grad) {
funcs::ElementwiseGradPreProcess(out_grad, x_grad);
auto out = out_grad; // Fake out, not used
if (out_grad.numel() == 0) {
if (x_grad) {
dev_ctx.template Alloc<T>(x_grad);
if (x_grad->numel() != 0) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(x_grad->dims())),
0,
x_grad);
}
}
if (y_grad) {
dev_ctx.template Alloc<T>(y_grad);
if (y_grad->numel() != 0) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(y_grad->dims())),
0,
y_grad);
}
}
return;
}
auto x_dim = x.dims();
auto y_dim = y.dims();
int axis = -1;
Expand Down
43 changes: 40 additions & 3 deletions test/legacy_test/test_fmax_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ def setUp(self):
# If x and y have the same value, the max() is not differentiable.
# So we generate test data by the following method
# to avoid them being too close to each other.
x = np.random.uniform(0.1, 1, [13, 17]).astype("float64")
sgn = np.random.choice([-1, 1], [13, 17]).astype("float64")
y = x + sgn * np.random.uniform(0.1, 1, [13, 17]).astype("float64")
self.init_shape()
x = np.random.uniform(0.1, 1, self.shape).astype("float64")
sgn = np.random.choice([-1, 1], self.shape).astype("float64")
y = x + sgn * np.random.uniform(0.1, 1, self.shape).astype("float64")
self.inputs = {'X': x, 'Y': y}
self.outputs = {'Out': np.fmax(self.inputs['X'], self.inputs['Y'])}

def init_shape(self):
self.shape = [13, 17]

def test_check_output(self):
"""test_check_output"""
self.check_output(check_pir=True, check_symbol_infer=False)
Expand Down Expand Up @@ -286,5 +290,38 @@ def test_check_grad(self):
)


class TestElementwiseFmaxOpZeroSize(TestElementwiseFmaxOp):
def init_shape(self):
self.shape = [0, 15]


class TestElementwiseFmaxOpZeroSize1(TestElementwiseFmaxOp):
def init_shape(self):
self.shape = [0, 15, 0]


class ApiFMaxTestZeroSize(unittest.TestCase):
"""ApiFMaxTest"""

def setUp(self):
"""setUp"""
if core.is_compiled_with_cuda():
self.place = core.CUDAPlace(0)
else:
self.place = core.CPUPlace()

self.input_x = np.random.rand(0, 15).astype("float32")
self.input_y = np.random.rand(0, 15).astype("float32")
self.input_z = np.random.rand(1, 15).astype("float32")
self.input_a = np.random.rand(15, 0).astype('int64')
self.input_b = np.random.rand(15, 0, 1).astype('int64')
self.input_c = np.random.rand(15, 0, 2).astype('int64')

self.np_expected1 = np.fmax(self.input_x, self.input_y)
self.np_expected2 = np.fmax(self.input_x, self.input_z)
self.np_expected3 = np.fmax(self.input_a, self.input_c)
self.np_expected4 = np.fmax(self.input_b, self.input_c)


if __name__ == "__main__":
unittest.main()
21 changes: 18 additions & 3 deletions test/legacy_test/test_fmin_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,17 @@ def setUp(self):
# If x and y have the same value, the min() is not differentiable.
# So we generate test data by the following method
# to avoid them being too close to each other.
x = np.random.uniform(0.1, 1, [13, 17]).astype("float64")
sgn = np.random.choice([-1, 1], [13, 17]).astype("float64")
y = x + sgn * np.random.uniform(0.1, 1, [13, 17]).astype("float64")
self.init_shape()
x = np.random.uniform(0.1, 1, self.shape).astype("float64")
sgn = np.random.choice([-1, 1], self.shape).astype("float64")
y = x + sgn * np.random.uniform(0.1, 1, self.shape).astype("float64")
self.inputs = {'X': x, 'Y': y}
self.outputs = {'Out': np.fmin(self.inputs['X'], self.inputs['Y'])}

def init_shape(self):
"""init_shape"""
self.shape = [13, 17]

def test_check_output(self):
"""test_check_output"""
self.check_output(check_pir=True, check_symbol_infer=False)
Expand Down Expand Up @@ -288,6 +293,16 @@ def test_check_grad(self):
)


class TestElementwiseFminOpZeroSize(TestElementwiseFminOp):
def init_shape(self):
self.shape = [0, 9]


class TestElementwiseFminOpZeroSize1(TestElementwiseFminOp):
def init_shape(self):
self.shape = [9, 0]


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