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 3 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
89 changes: 60 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 All @@ -138,6 +141,7 @@ struct DotGradFunction<DeviceContext, T, phi::funcs::DisableComplex<T>> {
const DenseTensor* tensor_dout,
DenseTensor* tensor_dx,
DenseTensor* tensor_dy) {
VLOG(1) << "disable route";
#if defined(__NVCC__) || defined(__HIPCC__)
if (1 == tensor_dout->dims().size()) {
auto dout = EigenVector<T>::Flatten(*tensor_dout);
Expand Down Expand Up @@ -182,7 +186,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 +303,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 +322,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 +344,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 +370,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 +394,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 +485,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 +501,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 +517,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 +537,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 +557,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 +786,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 +801,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 +828,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 +842,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 +878,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 +895,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 +912,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 +933,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 +1167,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 +1179,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 +1204,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 +1216,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 +1249,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 +1265,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 +1281,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 +1300,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
11 changes: 11 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,17 @@ 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

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