Skip to content

Commit a4acffd

Browse files
authored
[CINN]Fix 0 size ops bug (#71586)
* update * update * update * update * remove uselese code * fix roi align grad kernel * [PHI]fix 0 size error (#71485) * update * update * update * update * remove uselese code * fix bug
1 parent 7942f46 commit a4acffd

File tree

8 files changed

+70
-23
lines changed

8 files changed

+70
-23
lines changed

paddle/phi/infermeta/binary.cc

+26-18
Original file line numberDiff line numberDiff line change
@@ -2434,16 +2434,19 @@ void IndexSelectInferMeta(const MetaTensor& x,
24342434
"the dimension of Input(Index) is [%d].",
24352435
index_dim,
24362436
index_dim.size()));
2437-
2438-
PADDLE_ENFORCE_EQ(index_dim[0] != 0,
2439-
true,
2440-
common::errors::InvalidArgument(
2441-
"The length of Input(Index) can't be 0."));
2442-
2443-
auto output_dim = common::vectorize(input_dim);
24442437
if (dim < 0) {
24452438
dim += input_dim.size();
24462439
}
2440+
2441+
if (input_dim[dim] != 0) {
2442+
PADDLE_ENFORCE_EQ(index_dim[0] != 0,
2443+
true,
2444+
common::errors::InvalidArgument(
2445+
"The length of Input(Index) can't be 0."));
2446+
}
2447+
2448+
auto output_dim = common::vectorize(input_dim);
2449+
24472450
output_dim[dim] = index_dim[0];
24482451
output->set_dims(common::make_ddim(output_dim));
24492452
output->set_dtype(x.dtype());
@@ -3668,18 +3671,23 @@ void RepeatInterleaveWithTensorIndexInferMeta(const MetaTensor& x,
36683671
repeats_dim,
36693672
repeats_dim.size()));
36703673

3671-
PADDLE_ENFORCE_EQ(repeats_dim[0] != 0,
3672-
true,
3673-
common::errors::InvalidArgument(
3674-
"The length of Input(RepeatsTensor) can't be 0."));
3675-
PADDLE_ENFORCE_NE(out,
3676-
nullptr,
3677-
common::errors::InvalidArgument(
3678-
"repeat_interleave's output tensor can't be nullptr"));
3679-
if (dim < 0) {
3680-
dim += input_dim.size();
3674+
if (input_dim.size() == 1 && input_dim[0] == 0) {
3675+
output_dim[0] = 0;
3676+
} else {
3677+
PADDLE_ENFORCE_EQ(repeats_dim[0] != 0,
3678+
true,
3679+
common::errors::InvalidArgument(
3680+
"The length of Input(RepeatsTensor) can't be 0."));
3681+
PADDLE_ENFORCE_NE(
3682+
out,
3683+
nullptr,
3684+
common::errors::InvalidArgument(
3685+
"repeat_interleave's output tensor can't be nullptr"));
3686+
if (dim < 0) {
3687+
dim += input_dim.size();
3688+
}
3689+
output_dim[dim] = -1;
36813690
}
3682-
output_dim[dim] = -1;
36833691

36843692
out->set_dims(common::make_ddim(output_dim));
36853693
out->share_lod(x);

paddle/phi/kernels/gpu/concat_kernel.cu

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ void ConcatKernel(const Context& dev_ctx,
4545
out->Resize(out_dims);
4646
dev_ctx.template Alloc<T>(out);
4747

48+
if (out->numel() == 0) {
49+
return;
50+
}
51+
4852
// If axis is 0, the lod of the output is not the same as inputs.
4953
if (axis == 0 && x[0]->lod().size() > 0) {
5054
size_t lod_size_0 = x[0]->lod().size();

paddle/phi/kernels/gpu/masked_select_kernel.cu

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ void MaskedSelectKernel(const Context& dev_ctx,
5353
DenseTensor mask_expand;
5454
DenseTensor x_expand;
5555

56+
if (x.numel() == 0 || mask.numel() == 0) {
57+
out->Resize({0});
58+
dev_ctx.template Alloc<T>(out);
59+
60+
return;
61+
}
62+
5663
auto expanded_size = funcs::MatrixGetBroadcastBatchPortion(
5764
common::vectorize(x.dims()), common::vectorize(mask.dims()));
5865

paddle/phi/kernels/gpu/roi_align_grad_kernel.cu

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "paddle/phi/common/place.h"
2222
#include "paddle/phi/core/kernel_registry.h"
2323
#include "paddle/phi/kernels/empty_kernel.h"
24+
#include "paddle/phi/kernels/full_kernel.h"
2425
#include "paddle/phi/kernels/funcs/math_function.h"
2526

2627
namespace phi {
@@ -176,7 +177,16 @@ void RoiAlignGradKernel(const Context& dev_ctx,
176177
int sampling_ratio,
177178
bool aligned,
178179
DenseTensor* dx) {
180+
if (x.numel() == 0 || boxes.numel() == 0) {
181+
dev_ctx.template Alloc<T>(dx);
182+
183+
phi::FullKernel<T>(
184+
dev_ctx, common::vectorize(dx->dims()), 0.0, dx->dtype(), dx);
185+
return;
186+
}
187+
179188
int rois_num = boxes.dims()[0];
189+
180190
int channels = x.dims()[1];
181191
int height = x.dims()[2];
182192
int width = x.dims()[3];
@@ -185,6 +195,11 @@ void RoiAlignGradKernel(const Context& dev_ctx,
185195
return;
186196
}
187197

198+
// if (dx->numel() == 0) {
199+
// dev_ctx.template Alloc<T>(dx);
200+
201+
// return;
202+
// }
188203
DenseTensor box_batch_id_list;
189204
box_batch_id_list.Resize({rois_num});
190205
int* box_batch_size = dev_ctx.template HostAlloc<int>(&box_batch_id_list);

paddle/phi/kernels/gpu/roi_align_kernel.cu

+4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ void RoiAlignKernel(const Context& dev_ctx,
145145
int sampling_ratio,
146146
bool aligned,
147147
DenseTensor* out) {
148+
if (out->numel() == 0) {
149+
dev_ctx.template Alloc<T>(out);
150+
return;
151+
}
148152
auto in_dims = x.dims();
149153
int batch_size = in_dims[0];
150154
int channels = in_dims[1];

paddle/phi/kernels/impl/repeat_interleave_kernel_impl.h

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ void RepeatInterleaveWithTensorIndexKernel(const Context& ctx,
120120
const DenseTensor& repeats_tensor,
121121
int dim,
122122
DenseTensor* out) {
123+
if (x.numel() == 0) {
124+
ctx.template Alloc<T>(out);
125+
return;
126+
}
123127
auto place = ctx.GetPlace();
124128
auto cpu_place = phi::CPUPlace();
125129

paddle/phi/kernels/kps/reduce_kernel.cu

+10-3
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,16 @@ void SumRawKernel(const Context& dev_ctx,
249249
}
250250
}
251251
out->Resize(phi::make_ddim(out_dims));
252-
dev_ctx.template Alloc<T>(out);
253-
FullKernel<T, Context>(
254-
dev_ctx, out_dims, 0, phi::CppTypeToDataType<T>::Type(), out);
252+
if (x.dtype() == phi::DataType::BOOL || x.dtype() == phi::DataType::INT32) {
253+
dev_ctx.template Alloc<int64_t>(out);
254+
FullKernel<int64_t, Context>(
255+
dev_ctx, out_dims, 0, phi::CppTypeToDataType<int64_t>::Type(), out);
256+
} else {
257+
dev_ctx.template Alloc<T>(out);
258+
FullKernel<T, Context>(
259+
dev_ctx, out_dims, 0, phi::CppTypeToDataType<T>::Type(), out);
260+
}
261+
255262
return;
256263
}
257264
if (x.numel() > std::numeric_limits<int32_t>::max()) {

python/paddle/tensor/manipulation.py

-2
Original file line numberDiff line numberDiff line change
@@ -1409,8 +1409,6 @@ def concat(
14091409
if in_dynamic_mode():
14101410
if isinstance(axis, Variable):
14111411
axis = axis.item(0)
1412-
if not isinstance(input, (Variable, paddle.pir.Value)):
1413-
input = [t for t in input if t.shape.count(0) == 0]
14141412
return _C_ops.concat(input, axis)
14151413
elif in_pir_mode():
14161414

0 commit comments

Comments
 (0)