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
11 changes: 10 additions & 1 deletion paddle/phi/kernels/cpu/box_coder_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/impl/box_coder.h"

namespace phi {

template <typename T>
Expand Down Expand Up @@ -179,6 +179,15 @@ void BoxCoderKernel(const Context &dev_ctx,
int axis,
const std::vector<float> &variance,
DenseTensor *output_box) {
// prior_box and prior_box_var have the same shape, so do not judge
// prior_box_var
if (prior_box.numel() == 0 || target_box.numel() == 0) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(output_box->dims())),
0,
output_box);
return;
}
if (!target_box.lod().empty()) {
PADDLE_ENFORCE_EQ(target_box.lod().size(),
1UL,
Expand Down
10 changes: 9 additions & 1 deletion paddle/phi/kernels/cpu/prior_box_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "paddle/phi/kernels/prior_box_kernel.h"

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"

namespace phi {

template <typename T, typename Context>
Expand All @@ -35,6 +35,14 @@ void PriorBoxKernel(const Context& ctx,
bool min_max_aspect_ratios_order,
DenseTensor* out,
DenseTensor* var) {
if (input.numel() == 0 || image.numel() == 0) {
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(var->dims())), 0, var);
return;
}

std::vector<float> new_aspect_ratios;
ExpandAspectRatios(aspect_ratios, flip, &new_aspect_ratios);

Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/cpu/repeat_interleave_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ void RepeatInterleaveGradKernel(const Context& ctx,
int repeats,
int dim,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
ctx.template Alloc<T>(x_grad);
return;
}
auto input_dim = x_grad->dims();
if (dim < 0) {
dim += input_dim.size();
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/cpu/yolo_box_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/yolo_box_util.h"

namespace phi {
Expand All @@ -35,6 +36,14 @@ void YoloBoxKernel(const Context& dev_ctx,
float iou_aware_factor,
DenseTensor* boxes,
DenseTensor* scores) {
if (x.numel() == 0 || img_size.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(boxes->dims())), 0, boxes);
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(scores->dims())), 0, scores);
return;
}

auto* input = &x;
auto* imgsize = &img_size;
float scale = scale_x_y;
Expand Down
12 changes: 11 additions & 1 deletion paddle/phi/kernels/gpu/box_coder_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "paddle/phi/backends/gpu/gpu_primitives.h"
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/impl/box_coder.h"

namespace phi {

template <typename T>
Expand Down Expand Up @@ -157,6 +157,16 @@ void BoxCoderKernel(const Context &dev_ctx,
int axis,
const std::vector<float> &variance,
DenseTensor *output_box) {
// prior_box and prior_box_var have the same shape, so do not judge
// prior_box_var
if (prior_box.numel() == 0 || target_box.numel() == 0) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(output_box->dims())),
0,
output_box);
return;
}

const T *prior_box_data = prior_box.template data<T>();
const T *target_box_data = target_box.template data<T>();
const T *prior_box_var_data = nullptr;
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/gpu/prior_box_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/full_kernel.h"

namespace phi {

Expand Down Expand Up @@ -122,6 +123,14 @@ void PriorBoxKernel(const Context& ctx,
bool min_max_aspect_ratios_order,
DenseTensor* out,
DenseTensor* var) {
if (input.numel() == 0 || image.numel() == 0) {
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(var->dims())), 0, var);
return;
}

std::vector<float> new_aspect_ratios;
ExpandAspectRatios(aspect_ratios, flip, &new_aspect_ratios);

Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/gpu/yolo_box_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "paddle/phi/backends/gpu/gpu_launch_config.h"
#include "paddle/phi/common/memory_utils.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/yolo_box_util.h"

Expand Down Expand Up @@ -115,6 +116,14 @@ void YoloBoxKernel(const Context& dev_ctx,
float iou_aware_factor,
DenseTensor* boxes,
DenseTensor* scores) {
if (x.numel() == 0 || img_size.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(boxes->dims())), 0, boxes);
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(scores->dims())), 0, scores);
return;
}

auto* input = &x;
float scale = scale_x_y;
float bias = -0.5 * (scale - 1.);
Expand Down
13 changes: 12 additions & 1 deletion paddle/phi/kernels/impl/kron_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#pragma once

#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/impl/kron_kernel_impl.h"
#include "paddle/phi/kernels/reduce_sum_kernel.h"

namespace phi {

template <typename T>
Expand Down Expand Up @@ -266,6 +266,17 @@ void KronGradKernel(const Context &ctx,
const DenseTensor &out_grad,
DenseTensor *x_grad,
DenseTensor *y_grad) {
if (out_grad.numel() == 0) {
if (x_grad) {
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(x_grad->dims())), 0, x_grad);
}
if (y_grad) {
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(y_grad->dims())), 0, y_grad);
}
return;
}
if (x_grad) {
ctx.template Alloc<T>(x_grad);
}
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/impl/kron_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ void KronKernel(const Context &ctx,
const DenseTensor &y,
DenseTensor *out) {
ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}

int ndims = out->dims().size();
DenseTensor xx = UnsqueezeTo(x, ndims);
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/impl/renorm_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ void RenormGradKernel(const Context& dev_ctx,
auto dimension_each = input_dims[dim];
dx->Resize(x.dims());
dev_ctx.template Alloc<T>(dx);
if (dx && dx->numel() == 0) {
return;
}
phi::funcs::RenormGradFunc(dev_ctx,
x_data,
dout_data,
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/renorm_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
Expand All @@ -28,6 +29,9 @@ void RenormKernel(const Context& dev_ctx,
DenseTensor* out) {
out->Resize(x.dims());
dev_ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}
auto x_ptr = x.template data<T>();
auto numel = x.numel();
int dim = axis;
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/repeat_interleave_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ void RepeatInterleaveGradKernel(const Context& ctx,
int repeats,
int dim,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
ctx.template Alloc<T>(x_grad);
return;
}
auto input_dim = x_grad->dims();
if (dim < 0) {
dim += input_dim.size();
Expand Down
5 changes: 4 additions & 1 deletion paddle/phi/kernels/impl/repeat_interleave_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ void RepeatInterleaveKernel(const Context& ctx,
0,
common::errors::InvalidArgument(
"repeats must grater than 0, but got %d", repeats));

if (out && out->numel() == 0) {
ctx.template Alloc<T>(out);
return;
}
auto place = ctx.GetPlace();
auto cpu_place = phi::CPUPlace();

Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/xpu/prior_box_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"

namespace phi {
Expand All @@ -36,6 +37,14 @@ void PriorBoxKernel(const Context& ctx,
bool min_max_aspect_ratios_order,
DenseTensor* out,
DenseTensor* var) {
if (input.numel() == 0 || image.numel() == 0) {
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
phi::Full<T, Context>(
ctx, phi::IntArray(common::vectorize(var->dims())), 0, var);
return;
}

std::vector<float> new_aspect_ratios;
ExpandAspectRatios(aspect_ratios, flip, &new_aspect_ratios);

Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/xpu/repeat_interleave_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ void RepeatInterleaveKernel(const Context& ctx,
0,
common::errors::InvalidArgument(
"repeats must grater than 0, but got %d", repeats));
if (out && out->numel() == 0) {
ctx.template Alloc<T>(out);
return;
}
using XPUType = typename XPUTypeTrait<T>::Type;

auto input_dim = x.dims();
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/xpu/yolo_box_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/yolo_box_util.h"

namespace phi {
Expand All @@ -34,6 +35,14 @@ void YoloBoxKernel(const Context& dev_ctx,
float iou_aware_factor,
DenseTensor* boxes,
DenseTensor* scores) {
if (x.numel() == 0 || img_size.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(boxes->dims())), 0, boxes);
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(scores->dims())), 0, scores);
return;
}

using XPUType = typename XPUTypeTrait<T>::Type;
int r = 0;
auto* input = &x;
Expand Down
33 changes: 33 additions & 0 deletions test/legacy_test/test_box_coder_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,39 @@ def test_support_tuple(self):
paddle.enable_static()


class TestBoxCoderOp_ZeroSize(OpTest):
def test_check_output(self):
self.check_output(check_pir=True)

def setUp(self):
self.op_type = "box_coder"
self.python_api = paddle.vision.ops.box_coder
lod = [[1, 1, 1, 1, 1]]
prior_box = np.random.random((81, 4)).astype('float32')
prior_box_var = np.random.random((81, 4)).astype('float32')
target_box = np.random.random((0, 81, 4)).astype('float32')
code_type = "DecodeCenterSize"
box_normalized = False
output_box = batch_box_coder(
prior_box,
prior_box_var,
target_box,
lod[0],
code_type,
box_normalized,
)
self.inputs = {
'PriorBox': prior_box,
'PriorBoxVar': prior_box_var,
'TargetBox': target_box,
}
self.attrs = {
'code_type': 'decode_center_size',
'box_normalized': False,
}
self.outputs = {'OutputBox': output_box}


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
27 changes: 27 additions & 0 deletions test/legacy_test/test_kron_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ def _init_dtype(self):
return "float16"


class TestKronOp_ZeroSize(OpTest):
def setUp(self):
self.op_type = "kron"
self.prim_op_type = "prim"
self.python_api = paddle.kron
self.public_python_api = paddle.kron
self.dtype = self._init_dtype()
x = np.random.uniform(size=(10, 0, 2)).astype(self.dtype)
y = np.random.uniform(size=(2, 3, 4, 3, 2)).astype(self.dtype)
out_ref = np.kron(x, y)
self.inputs = {'X': x, 'Y': y}
self.outputs = {'Out': out_ref}

def _init_dtype(self):
return "float64"

def test_check_output(self):
self.check_output(check_pir=True)

def test_check_grad(self):
self.check_grad(
['X', 'Y'],
'Out',
check_pir=True,
)


@unittest.skipIf(
not core.is_compiled_with_cuda()
or not core.is_bfloat16_supported(core.CUDAPlace(0)),
Expand Down
Loading