Skip to content
Closed
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
16 changes: 16 additions & 0 deletions paddle/phi/kernels/cpu/mv_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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/blas/blas.h"

namespace phi {
Expand All @@ -30,6 +31,21 @@ void MvGradKernel(const Context& dev_ctx,
auto dout = out_grad;
auto dx = x_grad;
auto dvec = vec_grad;
if (x.numel() == 0 || vec.numel() == 0) {
if (dx) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(dx->dims())),
static_cast<T>(0),
dx);
}
if (dvec) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(dvec->dims())),
static_cast<T>(0),
dvec);
}
return;
}

const auto& dim_x = x.dims();
int m = static_cast<int>(dim_x[0]);
Expand Down
16 changes: 16 additions & 0 deletions paddle/phi/kernels/gpu/mv_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/phi/backends/gpu/gpu_launch_config.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"

namespace phi {
Expand All @@ -41,6 +42,21 @@ void MvGradKernel(const Context &dev_ctx,
auto dout = out_grad;
auto dx = x_grad;
auto dvec = vec_grad;
if (x.numel() == 0 || vec.numel() == 0) {
if (dx) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(dx->dims())),
static_cast<T>(0),
dx);
}
if (dvec) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(dvec->dims())),
static_cast<T>(0),
dvec);
}
return;
}

auto dim_x = x.dims();
int m = dim_x[0];
Expand Down
13 changes: 12 additions & 1 deletion paddle/phi/kernels/impl/mv_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#pragma once

#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"

namespace phi {

template <typename T, typename Context>
Expand All @@ -29,6 +29,17 @@ void MvKernel(const Context& dev_ctx,
const T* x_data = x.data<T>();
const T* vec_data = vec.data<T>();
T* out_data = dev_ctx.template Alloc<T>(out);
if (out && out->numel() == 0) {
return;
}
// x.shape [10, 0], vec.shape [0], out.shape [10]
if (vec.numel() == 0) {
phi::Full<T, Context>(dev_ctx,
phi::IntArray(common::vectorize(out->dims())),
static_cast<T>(0),
out);
return;
}

auto blas = phi::funcs::GetBlas<Context, T>(dev_ctx);

Expand Down
18 changes: 18 additions & 0 deletions test/legacy_test/test_mv_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def init_config(self):
self.vec = np.random.random(100).astype("float64")


class TestMVOp_ZeroSize1(TestMVOp):
def init_config(self):
self.x = np.random.random((0, 100)).astype("float64")
self.vec = np.random.random(100).astype("float64")


class TestMVOp_ZeroSize2(TestMVOp):
def init_config(self):
self.x = np.random.random((100, 0)).astype("float64")
self.vec = np.random.random(0).astype("float64")


class TestMVOp_ZeroSize3(TestMVOp):
def init_config(self):
self.x = np.random.random((0, 0)).astype("float64")
self.vec = np.random.random(0).astype("float64")


class TestMVAPI(unittest.TestCase):
def test_dygraph_api_out(self):
paddle.disable_static()
Expand Down