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
6 changes: 6 additions & 0 deletions paddle/phi/kernels/funcs/gather.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
#include <glog/logging.h>
#include <memory.h>

#include <cstring>
Expand All @@ -38,6 +39,11 @@ void CPUGather(const phi::CPUContext& ctx UNUSED,
const DenseTensor& src,
const DenseTensor& index,
DenseTensor* output) {
if (src.numel() == 0 || index.numel() == 0) {
VLOG(6) << "Do nothing for CPUGather since inputs has 0-size tensor.";
return;
}

if (index.dims().size() == 2) {
PADDLE_ENFORCE_EQ(
index.dims()[1],
Expand Down
10 changes: 10 additions & 0 deletions paddle/phi/kernels/funcs/scatter.cu.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ void GPUScatterAssign(const phi::GPUContext& ctx,
DenseTensor* output,
bool overwrite = true,
bool need_init_zero = true) {
if (src.numel() == 0 || index.numel() == 0) {
VLOG(6)
<< "Do nothing for GPUScatterAssign since inputs has 0-size tensor.";
return;
}

if (index.dims().size() == 2) {
PADDLE_ENFORCE_EQ(
index.dims()[1],
Expand Down Expand Up @@ -237,6 +243,10 @@ template <typename T, typename IndexT = int>
void GPUScatterGradForX(const phi::GPUContext& ctx,
const DenseTensor& index,
DenseTensor* output) {
if (index.numel() == 0) {
VLOG(6) << "Do nothing for GPUScatterGradX since index is 0-size tensor.";
return;
}
int64_t index_size = index.dims().size() == 0 ? 1 : index.dims()[0];
auto dst_dims = output->dims();
// slice size
Expand Down
16 changes: 16 additions & 0 deletions paddle/phi/kernels/funcs/scatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
#include <glog/logging.h>
#include <cstring>
#include <string>
#include <unordered_set>
Expand Down Expand Up @@ -76,6 +77,10 @@ void ScatterAssign(const phi::CPUContext& ctx UNUSED,
const DenseTensor& src,
const DenseTensor& index,
DenseTensor* output) {
if (src.numel() == 0 || index.numel() == 0) {
VLOG(6) << "Do nothing for CPUGather since inputs has 0-size tensor.";
return;
}
if (index.dims().size() == 2) {
PADDLE_ENFORCE_EQ(
index.dims()[1],
Expand Down Expand Up @@ -164,6 +169,12 @@ void ScatterAssignAdd(const phi::CPUContext& ctx,
const DenseTensor& src,
const DenseTensor& index,
DenseTensor* output) {
if (src.numel() == 0 || index.numel() == 0) {
VLOG(6)
<< "Do nothing for ScatterAssignAdd since inputs has 0-size tensor.";
return;
}

PADDLE_ENFORCE_EQ(
index.dims().size() == 1 || index.dims().size() == 0 ||
(index.dims().size() == 2 && index.dims()[1] == 1),
Expand Down Expand Up @@ -250,6 +261,11 @@ template <typename T, typename IndexT = int>
void CPUScatterGradForX(const phi::CPUContext& ctx UNUSED,
const DenseTensor& index,
DenseTensor* output) {
if (index.numel() == 0) {
VLOG(6)
<< "Do nothing for CPUScatterGradForX since inputs has 0-size tensor.";
return;
}
int64_t index_size = index.dims().size() == 0 ? 1 : index.dims()[0];
auto dst_dims = output->dims();
const IndexT* p_index = index.data<IndexT>();
Expand Down
4 changes: 3 additions & 1 deletion paddle/phi/kernels/xpu/gather_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ void GatherGradKernel(const Context& dev_ctx,

const auto& index_type = index.dtype();

if (out_grad.numel() == 0) {
if (x.numel() == 0 || index.numel() == 0 || out_grad.numel() == 0) {
VLOG(6)
<< "Do nothing for GatherGradKernel since inputs has 0-size tensor.";
return;
}

Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/kernels/xpu/scatter_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ void ScatterKernel(const Context &ctx,
auto *out_data = reinterpret_cast<XPUTypeT *>(ctx.template Alloc<T>(out));
int ret = xpu::copy(ctx.x_context(), x_data, out_data, x.numel());
PADDLE_ENFORCE_XDNN_SUCCESS(ret, "copy");

if (x.numel() == 0 || index.numel() == 0 || updates.numel() == 0) {
VLOG(6) << "Do nothing for ScatterKernel since inputs has 0-size tensor.";
return;
}

// Apply ScatterUpdate: Out[index] = Updates[:]
const auto &index_type = index.dtype();
bool index_type_match =
Expand Down
Loading