Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix div 0 error of case25: paddle.dot #50014

Merged
merged 5 commits into from
Feb 13, 2023
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
8 changes: 7 additions & 1 deletion paddle/phi/kernels/cpu/dot_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ void DotKernel(const Context& dev_ctx,
// B pairs along the way where B is the dimension of the least ordered axis
auto&& d = x.dims();
auto const N = x.numel();
auto const B = d[d.size() - 1];

// prevent div 0
auto const _B = d.size() == 0 ? 1 : d[d.size() - 1];
auto const B = _B != 0 ? _B : 1;

// initialize for N / B <= 0
z[0] = 0;

for (int j = 0; j < N / B; j++) {
T ss = 0;
Expand Down
88 changes: 59 additions & 29 deletions paddle/phi/kernels/impl/dot_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct DotGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
const DenseTensor* tensor_dout,
DenseTensor* tensor_dx,
DenseTensor* tensor_dy) {
VLOG(1) << "enable route";
RedContritio marked this conversation as resolved.
Show resolved Hide resolved
#if defined(__NVCC__) || defined(__HIPCC__)
if (1 == tensor_dout->dims().size()) {
auto dout = EigenVector<T>::Flatten(*tensor_dout);
Expand Down Expand Up @@ -103,7 +104,8 @@ struct DotGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
const DDim& dim = tensor_x->dims();
size_t N = static_cast<size_t>(phi::product(dim));

auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;

int s = -1;
for (size_t i = 0; i < N; ++i) {
Expand All @@ -118,7 +120,8 @@ struct DotGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
const DDim& dim = tensor_y->dims();
size_t N = static_cast<size_t>(phi::product(dim));

auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;

int s = -1;
for (size_t i = 0; i < N; ++i) {
Expand Down Expand Up @@ -182,7 +185,8 @@ struct DotGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
*dz = tensor_dout->data<T>();
auto&& d = tensor_x->dims();
auto const N = tensor_x->numel();
auto const B = d[d.size() - 1];
auto const _B = d.size() == 0 ? 1 : d[d.size() - 1];
auto const B = _B != 0 ? _B : 1;

if (tensor_dx) {
auto* dx = ctx.template Alloc<T>(tensor_dx);
Expand Down Expand Up @@ -298,7 +302,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
const DDim& dim = tensor_dx->dims();
size_t N = static_cast<size_t>(product(dim));

auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;

int s = -1;
for (size_t i = 0; i < N; ++i) {
Expand All @@ -316,7 +321,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
const DDim& dim = tensor_dy->dims();
size_t N = static_cast<size_t>(product(dim));

auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;

int s = -1;
for (size_t i = 0; i < N; ++i) {
Expand All @@ -337,7 +343,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {

const DDim& dim = tensor_dy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;

Expand All @@ -362,7 +369,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {

const DDim& dim = tensor_dy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;

Expand All @@ -385,7 +393,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {

const DDim& dim = tensor_dx->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;

Expand Down Expand Up @@ -475,7 +484,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_dx = ctx.template Alloc<T>(tensor_dx);
const DDim& dim = tensor_dx->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -490,7 +500,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_dy = ctx.template Alloc<T>(tensor_dy);
const DDim& dim = tensor_dy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -505,7 +516,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
const DDim& dim = tensor_dy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;
for (size_t i = 0; i < N; ++i) {
Expand All @@ -524,7 +536,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
const DDim& dim = tensor_dy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;
for (size_t i = 0; i < N; ++i) {
Expand All @@ -543,7 +556,8 @@ struct DotDoubleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_ddout = ctx.template Alloc<T>(tensor_ddout);
const DDim& dim = tensor_dx->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;
for (size_t i = 0; i < N; ++i) {
Expand Down Expand Up @@ -771,7 +785,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_y = ctx.template Alloc<T>(out_tensor_d_y);
const DDim& dim = out_tensor_d_y->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;

for (size_t i = 0; i < N; ++i) {
Expand All @@ -785,7 +800,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
const DDim& dim = in_tensor_x->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;
for (size_t i = 0; i < N; ++i) {
Expand All @@ -811,7 +827,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_x = ctx.template Alloc<T>(out_tensor_d_x);
const DDim& dim = out_tensor_d_x->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;

for (size_t i = 0; i < N; ++i) {
Expand All @@ -824,7 +841,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
const DDim& dim = in_tensor_x->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;
if (d_dout_flag) {
Expand Down Expand Up @@ -859,7 +877,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
const DDim& dim = out_tensor_d_ddy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -875,7 +894,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
const DDim& dim = out_tensor_d_ddx->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -891,7 +911,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
const DDim& dim = out_tensor_d_ddx->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
if (d_ddx_flag) {
for (size_t i = 0; i < N; ++i) {
Expand All @@ -911,7 +932,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::EnableComplex<T>> {
auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
const DDim& dim = out_tensor_d_ddy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
if (d_ddy_flag) {
for (size_t i = 0; i < N; ++i) {
Expand Down Expand Up @@ -1144,7 +1166,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_y = ctx.template Alloc<T>(out_tensor_d_y);
const DDim& dim = out_tensor_d_y->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -1155,7 +1178,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
const DDim& dim = in_tensor_x->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;
for (size_t i = 0; i < N; ++i) {
Expand All @@ -1179,7 +1203,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_x = ctx.template Alloc<T>(out_tensor_d_x);
const DDim& dim = out_tensor_d_x->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -1190,7 +1215,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_dout = ctx.template Alloc<T>(out_tensor_d_dout);
const DDim& dim = in_tensor_x->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
bool new_s = false;
if (d_dout_flag) {
Expand Down Expand Up @@ -1222,7 +1248,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
const DDim& dim = out_tensor_d_ddy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -1237,7 +1264,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
const DDim& dim = out_tensor_d_ddx->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
for (size_t i = 0; i < N; ++i) {
if (0 == i % step) ++s;
Expand All @@ -1252,7 +1280,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_ddx = ctx.template Alloc<T>(out_tensor_d_ddx);
const DDim& dim = out_tensor_d_ddx->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
if (d_ddx_flag) {
for (size_t i = 0; i < N; ++i) {
Expand All @@ -1270,7 +1299,8 @@ struct DotTripleGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
auto* data_d_ddy = ctx.template Alloc<T>(out_tensor_d_ddy);
const DDim& dim = out_tensor_d_ddy->dims();
size_t N = static_cast<size_t>(product(dim));
auto step = dim[dim.size() - 1];
auto _step = dim.size() > 0 ? dim[dim.size() - 1] : 1;
auto step = _step != 0 ? _step : 1;
int s = -1;
if (d_ddy_flag) {
for (size_t i = 0; i < N; ++i) {
Expand Down
26 changes: 26 additions & 0 deletions python/paddle/fluid/tests/unittests/test_dot_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ def init_dtype(self):
self.dtype = np.float64


class DotOpEmptyInput(unittest.TestCase):
def test_1d_input(self):
data = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(data, [0]), dtype='float32')
y = paddle.to_tensor(np.reshape(data, [0]), dtype='float32')
np_out = np.dot(data, data)
pd_out = paddle.dot(x, y)

self.assertEquals(np_out, pd_out)

RedContritio marked this conversation as resolved.
Show resolved Hide resolved
def test_2d_input(self):
data = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(data, [0, 0]), dtype='float32')
y = paddle.to_tensor(np.reshape(data, [0, 0]), dtype='float32')
pd_out = paddle.dot(x, y)

self.assertEqual(pd_out.shape, (0, 1))

def test_3d_input_error(self):
data = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(data, [0, 0, 0]), dtype='float32')
y = paddle.to_tensor(np.reshape(data, [0, 0, 0]), dtype='float32')

self.assertRaises(Exception, paddle.dot, x, y)


class DotOpBatch(DotOp):
def init_input_output(self):
self.x = (
Expand Down