Skip to content

Commit a269d77

Browse files
authored
Fix DenseTensor::Slice to support 0-size (#73377)
1 parent 3fffcef commit a269d77

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

paddle/phi/core/dense_tensor_impl.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,29 @@ DenseTensor DenseTensor::Slice(int64_t begin_idx, int64_t end_idx) const {
308308
end_idx,
309309
meta_.dims[0],
310310
common::errors::OutOfRange("The end row index is out of bound."));
311-
PADDLE_ENFORCE_LT(
311+
PADDLE_ENFORCE_LE(
312312
begin_idx,
313313
end_idx,
314314
common::errors::InvalidArgument(
315-
"The start row index must be less than the end row index."
315+
"The start row index must be equal or less than the end row index."
316316
"But received the start index = %d, the end index = %d.",
317317
begin_idx,
318318
end_idx));
319319

320320
if (meta_.dims[0] == 1) {
321321
return *this;
322+
} else if (begin_idx == end_idx) {
323+
DenseTensor dst;
324+
// create an holder
325+
dst.holder_ =
326+
std::make_shared<phi::Allocation>(nullptr, 0, holder_->place());
327+
dst.set_layout(meta_.layout);
328+
dst.meta_.dtype = meta_.dtype;
329+
DDim dst_dims = meta_.dims;
330+
dst_dims[0] = end_idx - begin_idx;
331+
dst.Resize(dst_dims);
332+
dst.meta_.offset = 0;
333+
return dst;
322334
} else {
323335
size_t base = numel() / meta_.dims[0];
324336
DenseTensor dst;

0 commit comments

Comments
 (0)