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
4 changes: 4 additions & 0 deletions paddle/phi/kernels/gpu/determinant_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ template <typename T, typename Context>
void DeterminantKernel(const Context& dev_ctx,
const DenseTensor& x,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
auto input_dim = common::vectorize(x.dims());
auto input_dim_size = input_dim.size();

Expand Down
19 changes: 19 additions & 0 deletions paddle/phi/kernels/gpu/slogdeterminant_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ void SlogDeterminantKernel(const Context& dev_ctx,
auto input_dim = common::vectorize(x.dims());
auto input_dim_size = input_dim.size();

// shape [*, M, M], check whether it contains 0 in '*'.
if (input_dim.size() > 2) {
bool size_0 = false;
std::vector<int> tmp_dim_vec(input_dim.begin(), input_dim.end() - 2);
for (size_t i = 0; i < tmp_dim_vec.size(); ++i) {
if (tmp_dim_vec[i] == 0) {
size_0 = true;
break;
}
}
if (size_0) {
tmp_dim_vec.insert(tmp_dim_vec.begin(),
2); // make the output dims as same as numpy
out->Resize(common::make_ddim(tmp_dim_vec));
dev_ctx.template Alloc<T>(out);
return;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议在kernel中重新进行shape推导和Resize操作。如果可以请修改到infermeta中以及同步修改到符号推导中。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原kernel是在结尾做的Resize,如果修改到infermeta,需要kernel都修改,改动较多
image


auto batch_count = detail::GetBatchCount(x.dims());
VLOG(2) << "input dim:" << x.dims();
PADDLE_ENFORCE_GE(
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/determinant_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ void DeterminantGradKernel(const Context& dev_ctx,
const DenseTensor& out,
const DenseTensor& out_grad,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
return;
}
auto input_dims_size = x.dims().size();
if (input_dims_size > 2) {
PADDLE_ENFORCE_EQ(
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/determinant_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ template <typename T, typename Context>
void DeterminantKernel(const Context& dev_ctx,
const DenseTensor& x,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
auto input_dim = common::vectorize(x.dims());
auto input_dim_size = input_dim.size();

Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/impl/slogdeterminant_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ void SlogDeterminantGradKernel(const Context& dev_ctx,
const DenseTensor& out,
const DenseTensor& out_grad,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
return;
}
PADDLE_ENFORCE_EQ(
out_grad.dims()[0],
2,
Expand Down
19 changes: 19 additions & 0 deletions paddle/phi/kernels/impl/slogdeterminant_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ void SlogDeterminantKernel(const Context& dev_ctx,
auto input_dim = common::vectorize(x.dims());
auto input_dim_size = input_dim.size();

// shape [*, M, M], check whether it contains 0 in '*'.
if (input_dim.size() > 2) {
bool size_0 = false;
std::vector<int> tmp_dim_vec(input_dim.begin(), input_dim.end() - 2);
for (size_t i = 0; i < tmp_dim_vec.size(); ++i) {
if (tmp_dim_vec[i] == 0) {
size_0 = true;
break;
}
}
if (size_0) {
tmp_dim_vec.insert(tmp_dim_vec.begin(),
2); // make the output dims as same as numpy
out->Resize(common::make_ddim(tmp_dim_vec));
dev_ctx.template Alloc<T>(out);
return;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和paddle/phi/kernels/gpu/slogdeterminant_kernel.cu中的修改同理。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原kernel是在结尾做的Resize,如果修改到infermeta,需要kernel都修改,改动较多

auto batch_count = detail::GetBatchCount(x.dims());
VLOG(2) << "input dim:" << x.dims();
PADDLE_ENFORCE_GE(
Expand Down
32 changes: 32 additions & 0 deletions test/legacy_test/test_determinant_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ def init_data(self):
self.target = np.linalg.det(self.case)


class TestDeterminantOp_ZeroSize(TestDeterminantOp):
def init_data(self):
np.random.seed(0)
self.case = np.random.rand(0, 10, 10)
self.inputs = {'Input': self.case}
self.target = np.linalg.det(self.case)


class TestDeterminantOp_ZeroSize2(TestDeterminantOp):
def init_data(self):
np.random.seed(0)
self.case = np.random.rand(0, 0, 0)
self.inputs = {'Input': self.case}
self.target = np.linalg.det(self.case)


class TestDeterminantAPI(unittest.TestCase):
def setUp(self):
np.random.seed(0)
Expand Down Expand Up @@ -324,6 +340,22 @@ def init_data(self):
self.target = np.array(np.linalg.slogdet(self.case))


class TestSlogDeterminantOp_ZeroSize(TestSlogDeterminantOp):
def init_data(self):
np.random.seed(0)
self.case = np.random.rand(0, 5, 5).astype('float64')
self.inputs = {'Input': self.case}
self.target = np.array(np.linalg.slogdet(self.case))


class TestSlogDeterminantOp_ZeroSize2(TestSlogDeterminantOp):
def init_data(self):
np.random.seed(0)
self.case = np.random.rand(0, 0, 0).astype('float64')
self.inputs = {'Input': self.case}
self.target = np.array(np.linalg.slogdet(self.case))


class TestSlogDeterminantAPI(unittest.TestCase):
def setUp(self):
np.random.seed(0)
Expand Down