未验证 提交 292738f3 编写于 作者: J JYChen 提交者: GitHub

[zero-dim] Support 0-d for kthvalue and mode (#49340)

* add 0-d support for paddle.kthvalue

* add 0-d support for paddle.mode

* fix coverage test for device

* fix check-bug in windows

* change axis check from LT to LE

* add shape & value check for grad when input is 0d tensor
上级 6ec8dfdd
...@@ -1785,13 +1785,14 @@ void KthvalueInferMeta(const MetaTensor& x, ...@@ -1785,13 +1785,14 @@ void KthvalueInferMeta(const MetaTensor& x,
MetaConfig config) { MetaConfig config) {
auto input_dims = x.dims(); auto input_dims = x.dims();
const int& dim_size = input_dims.size(); const int& dim_size = input_dims.size();
PADDLE_ENFORCE_LT(axis, PADDLE_ENFORCE_LE(axis,
dim_size, dim_size,
phi::errors::InvalidArgument( phi::errors::InvalidArgument(
"the axis must be [-%d, %d), but received %d .", "the axis must be [-%d, %d), but received %d .",
dim_size, dim_size,
dim_size, dim_size,
axis)); axis));
if (dim_size > 0) {
PADDLE_ENFORCE_GE(axis, PADDLE_ENFORCE_GE(axis,
-dim_size, -dim_size,
phi::errors::InvalidArgument( phi::errors::InvalidArgument(
...@@ -1799,6 +1800,7 @@ void KthvalueInferMeta(const MetaTensor& x, ...@@ -1799,6 +1800,7 @@ void KthvalueInferMeta(const MetaTensor& x,
dim_size, dim_size,
dim_size, dim_size,
axis)); axis));
}
if (axis < 0) axis += dim_size; if (axis < 0) axis += dim_size;
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
k, k,
...@@ -1807,9 +1809,9 @@ void KthvalueInferMeta(const MetaTensor& x, ...@@ -1807,9 +1809,9 @@ void KthvalueInferMeta(const MetaTensor& x,
"the k in the kthvalue must >= 1, but received %d .", k)); "the k in the kthvalue must >= 1, but received %d .", k));
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
input_dims.size(), input_dims.size(),
1, 0,
phi::errors::InvalidArgument("input of kthvalue must have >= 1d shape")); phi::errors::InvalidArgument("input of kthvalue must have >= 0d shape"));
if (config.is_runtime) { if (dim_size > 0 && config.is_runtime) {
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
input_dims[axis], input_dims[axis],
k, k,
...@@ -1822,7 +1824,7 @@ void KthvalueInferMeta(const MetaTensor& x, ...@@ -1822,7 +1824,7 @@ void KthvalueInferMeta(const MetaTensor& x,
for (int64_t i = 0; i < axis; i++) { for (int64_t i = 0; i < axis; i++) {
dimvec.emplace_back(input_dims[i]); dimvec.emplace_back(input_dims[i]);
} }
if (keepdim) { if (keepdim && dim_size > 0) {
dimvec.emplace_back(static_cast<int64_t>(1)); dimvec.emplace_back(static_cast<int64_t>(1));
} }
for (int64_t i = axis + 1; i < dim_size; i++) { for (int64_t i = axis + 1; i < dim_size; i++) {
...@@ -2071,33 +2073,38 @@ void ModeInferMeta(const MetaTensor& x, ...@@ -2071,33 +2073,38 @@ void ModeInferMeta(const MetaTensor& x,
MetaTensor* indices) { MetaTensor* indices) {
auto input_dims = x.dims(); auto input_dims = x.dims();
const int& dim_size = input_dims.size(); const int& dim_size = input_dims.size();
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_LE(axis,
(axis < dim_size) && (axis >= (-1 * dim_size)), dim_size,
true, phi::errors::InvalidArgument(
errors::InvalidArgument( "the axis must be [-%d, %d), but received %d .",
"the axis of ModeOp must be [-%d, %d), but you set axis is %d",
dim_size, dim_size,
dim_size, dim_size,
axis)); axis));
if (dim_size > 0) {
PADDLE_ENFORCE_GE(axis,
-dim_size,
phi::errors::InvalidArgument(
"the axis must be [-%d, %d), but received %d .",
dim_size,
dim_size,
axis));
}
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
input_dims.size(), input_dims.size(),
1, 0,
errors::InvalidArgument("input of ModeOp must have >= 1d shape")); errors::InvalidArgument("input of ModeOp must have >= 0d shape"));
if (axis < 0) axis += dim_size; if (axis < 0) axis += dim_size;
std::vector<int64_t> dimvec; std::vector<int64_t> dimvec;
for (int64_t i = 0; i < axis; i++) { for (int64_t i = 0; i < axis; i++) {
dimvec.emplace_back(input_dims[i]); dimvec.emplace_back(input_dims[i]);
} }
if (keepdim) { if (keepdim && dim_size > 0) {
dimvec.emplace_back(static_cast<int64_t>(1)); dimvec.emplace_back(static_cast<int64_t>(1));
} }
for (int64_t i = axis + 1; i < dim_size; i++) { for (int64_t i = axis + 1; i < dim_size; i++) {
dimvec.emplace_back(input_dims[i]); dimvec.emplace_back(input_dims[i]);
} }
DDim dims = phi::make_ddim(dimvec); DDim dims = phi::make_ddim(dimvec);
PADDLE_ENFORCE_GE(input_dims.size(),
1,
errors::InvalidArgument("input shape should >= 1d"));
out->set_dims(dims); out->set_dims(dims);
out->share_lod(x); out->share_lod(x);
out->set_dtype(x.dtype()); out->set_dtype(x.dtype());
......
...@@ -55,6 +55,14 @@ void KthvalueGradKernel(const Context& dev_ctx, ...@@ -55,6 +55,14 @@ void KthvalueGradKernel(const Context& dev_ctx,
DenseTensor* d_x) { DenseTensor* d_x) {
auto in_dims = x.dims(); auto in_dims = x.dims();
auto out_dims = indices.dims(); auto out_dims = indices.dims();
T* x_grad_data = dev_ctx.template Alloc<T>(d_x);
// For 0D Tensor
if (in_dims.size() == 0) {
phi::funcs::set_constant(dev_ctx, d_x, 1.0);
return;
}
axis = (axis < 0) ? (in_dims.size() + axis) : axis; axis = (axis < 0) ? (in_dims.size() + axis) : axis;
if (!keepdim) { if (!keepdim) {
std::vector<int> tmp_out_shape; std::vector<int> tmp_out_shape;
...@@ -67,7 +75,7 @@ void KthvalueGradKernel(const Context& dev_ctx, ...@@ -67,7 +75,7 @@ void KthvalueGradKernel(const Context& dev_ctx,
} }
out_dims = phi::make_ddim(tmp_out_shape); out_dims = phi::make_ddim(tmp_out_shape);
} }
T* x_grad_data = dev_ctx.template Alloc<T>(d_x);
if (axis == in_dims.size() - 1) { if (axis == in_dims.size() - 1) {
const int64_t input_height = const int64_t input_height =
phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1)); phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1));
......
...@@ -82,8 +82,22 @@ void KthvalueKernel(const Context& dev_ctx, ...@@ -82,8 +82,22 @@ void KthvalueKernel(const Context& dev_ctx,
DenseTensor* indices) { DenseTensor* indices) {
const auto& in_dims = x.dims(); const auto& in_dims = x.dims();
if (axis < 0) axis += in_dims.size(); if (axis < 0) axis += in_dims.size();
T* output_data = dev_ctx.template Alloc<T>(output); T* output_data = dev_ctx.template Alloc<T>(output);
int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices); int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices);
// For 0D Tensor
if (in_dims.size() == 0) {
PADDLE_ENFORCE_EQ(k,
1,
phi::errors::InvalidArgument(
"the k in the kthvalue must less equal than the "
"elemenents number of the input X, but received %d .",
k));
phi::Copy<Context>(dev_ctx, x, dev_ctx.GetPlace(), false, output);
phi::funcs::set_constant(dev_ctx, indices, 0);
return;
}
auto out_dims = output->dims(); auto out_dims = output->dims();
if (axis == in_dims.size() - 1) { if (axis == in_dims.size() - 1) {
const int64_t& input_height = const int64_t& input_height =
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/mode.h" #include "paddle/phi/kernels/funcs/mode.h"
namespace phi { namespace phi {
...@@ -32,9 +33,17 @@ void ModeGradKernel(const Context& dev_ctx, ...@@ -32,9 +33,17 @@ void ModeGradKernel(const Context& dev_ctx,
auto in_dims = x.dims(); auto in_dims = x.dims();
auto out_dims = indices.dims(); auto out_dims = indices.dims();
T* x_grad_data = dev_ctx.template Alloc<T>(x_grad);
// axis < 0, get the real axis // axis < 0, get the real axis
axis = (axis < 0) ? (in_dims.size() + axis) : axis; axis = (axis < 0) ? (in_dims.size() + axis) : axis;
// For 0D Tensor
if (in_dims.size() == 0) {
phi::funcs::set_constant(dev_ctx, x_grad, 1.0);
return;
}
if (!keepdim) { if (!keepdim) {
std::vector<int> tmp_out_shape; std::vector<int> tmp_out_shape;
for (int i = 0; i < axis; i++) { for (int i = 0; i < axis; i++) {
...@@ -46,7 +55,6 @@ void ModeGradKernel(const Context& dev_ctx, ...@@ -46,7 +55,6 @@ void ModeGradKernel(const Context& dev_ctx,
} }
out_dims = phi::make_ddim(tmp_out_shape); out_dims = phi::make_ddim(tmp_out_shape);
} }
T* x_grad_data = dev_ctx.template Alloc<T>(x_grad);
if (axis == in_dims.size() - 1) { if (axis == in_dims.size() - 1) {
// allocate the memory for the input_grad // allocate the memory for the input_grad
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/mode.h" #include "paddle/phi/kernels/funcs/mode.h"
namespace phi { namespace phi {
...@@ -34,6 +35,13 @@ void ModeKernel(const Context& dev_ctx, ...@@ -34,6 +35,13 @@ void ModeKernel(const Context& dev_ctx,
T* output_data = dev_ctx.template Alloc<T>(out); T* output_data = dev_ctx.template Alloc<T>(out);
int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices); int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices);
if (in_dims.size() == 0) {
phi::Copy<Context>(dev_ctx, x, dev_ctx.GetPlace(), false, out);
phi::funcs::set_constant(dev_ctx, indices, 0);
return;
}
// if axis is not the last dim, transpose it to the last dim, do the // if axis is not the last dim, transpose it to the last dim, do the
// calculation, then tranpose it back to original axis. // calculation, then tranpose it back to original axis.
if (axis == in_dims.size() - 1) { if (axis == in_dims.size() - 1) {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/top_k_function_cuda.h" #include "paddle/phi/kernels/funcs/top_k_function_cuda.h"
namespace phi { namespace phi {
...@@ -43,8 +44,15 @@ void KthvalueGradKernel(const Context& dev_ctx, ...@@ -43,8 +44,15 @@ void KthvalueGradKernel(const Context& dev_ctx,
DenseTensor* d_x) { DenseTensor* d_x) {
const auto& in_dims = x.dims(); const auto& in_dims = x.dims();
auto out_dims = indices.dims(); auto out_dims = indices.dims();
if (axis < 0) axis += in_dims.size();
T* x_grad_data = dev_ctx.template Alloc<T>(d_x); T* x_grad_data = dev_ctx.template Alloc<T>(d_x);
// For 0D Tensor
if (in_dims.size() == 0) {
phi::funcs::set_constant(dev_ctx, d_x, 1.0);
return;
}
if (axis < 0) axis += in_dims.size();
const T* out_grad_data = d_out.data<T>(); const T* out_grad_data = d_out.data<T>();
const int64_t* indices_data = indices.data<int64_t>(); const int64_t* indices_data = indices.data<int64_t>();
int pre, n, post; int pre, n, post;
......
...@@ -167,6 +167,19 @@ void KthvalueKernel(const Context& dev_ctx, ...@@ -167,6 +167,19 @@ void KthvalueKernel(const Context& dev_ctx,
T* output_data = dev_ctx.template Alloc<T>(output); T* output_data = dev_ctx.template Alloc<T>(output);
int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices); int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices);
// For 0D Tensor
if (in_dims.size() == 0) {
PADDLE_ENFORCE_EQ(k,
1,
phi::errors::InvalidArgument(
"the k in the kthvalue must less equal than the "
"elemenents number of the input X, but received %d .",
k));
phi::Copy<Context>(dev_ctx, x, dev_ctx.GetPlace(), false, output);
phi::funcs::set_constant(dev_ctx, indices, 0);
return;
}
if (axis == in_dims.size() - 1) { if (axis == in_dims.size() - 1) {
const int64_t& input_height = const int64_t& input_height =
phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1)); phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1));
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/mode.h" #include "paddle/phi/kernels/funcs/mode.h"
namespace phi { namespace phi {
...@@ -61,6 +62,12 @@ void ModeGradKernel(const Context& dev_ctx, ...@@ -61,6 +62,12 @@ void ModeGradKernel(const Context& dev_ctx,
const T* out_grad_data = out_grad.data<T>(); const T* out_grad_data = out_grad.data<T>();
const int64_t* indices_data = indices.data<int64_t>(); const int64_t* indices_data = indices.data<int64_t>();
// For 0D Tensor
if (in_dims.size() == 0) {
phi::funcs::set_constant(dev_ctx, x_grad, 1.0);
return;
}
int pre, n, post; int pre, n, post;
funcs::GetDims(in_dims, axis, &pre, &n, &post); funcs::GetDims(in_dims, axis, &pre, &n, &post);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/mode.h" #include "paddle/phi/kernels/funcs/mode.h"
namespace phi { namespace phi {
...@@ -38,6 +39,13 @@ void ModeKernel(const Context& dev_ctx, ...@@ -38,6 +39,13 @@ void ModeKernel(const Context& dev_ctx,
T* output_data = dev_ctx.template Alloc<T>(out); T* output_data = dev_ctx.template Alloc<T>(out);
int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices); int64_t* indices_data = dev_ctx.template Alloc<int64_t>(indices);
// For 0D Tensor
if (in_dims.size() == 0) {
phi::Copy<Context>(dev_ctx, x, dev_ctx.GetPlace(), false, out);
phi::funcs::set_constant(dev_ctx, indices, 0);
return;
}
if (axis == in_dims.size() - 1) { if (axis == in_dims.size() - 1) {
const int64_t& input_height = const int64_t& input_height =
phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1)); phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1));
......
...@@ -177,6 +177,12 @@ class TestKthvalueOpErrors(unittest.TestCase): ...@@ -177,6 +177,12 @@ class TestKthvalueOpErrors(unittest.TestCase):
self.assertRaises(ValueError, test_dim_range_error) self.assertRaises(ValueError, test_dim_range_error)
def test_k_error_0_dim_input():
x_0d = paddle.full([], 1)
x_0d.kthvalue(k=8)
self.assertRaises(ValueError, test_k_error_0_dim_input)
class TestModeOpInStatic(unittest.TestCase): class TestModeOpInStatic(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -721,6 +721,48 @@ class TestSundryAPI(unittest.TestCase): ...@@ -721,6 +721,48 @@ class TestSundryAPI(unittest.TestCase):
self.assertEqual(out.numpy()[3], 2) self.assertEqual(out.numpy()[3], 2)
self.assertEqual(out.grad.shape, [5]) self.assertEqual(out.grad.shape, [5])
def test_kthvalue(self):
places = ['cpu']
if paddle.is_compiled_with_cuda():
places.append('gpu')
for place in places:
paddle.set_device(place)
x = paddle.randn(())
x.stop_gradient = False
out = paddle.kthvalue(x, 1)
out[0].backward()
# check shape of output value and indice
self.assertEqual(out[0].shape, [])
self.assertEqual(out[1].shape, [])
# check grad shape and value
self.assertEqual(x.grad.shape, [])
self.assertTrue(x.grad.numpy() == 1)
def test_mode(self):
places = ['cpu']
if paddle.is_compiled_with_cuda():
places.append('gpu')
for place in places:
paddle.set_device(place)
x = paddle.randn(())
x.stop_gradient = False
out = paddle.mode(x)
out[0].backward()
# check shape of output value and indice
self.assertEqual(out[0].shape, [])
self.assertEqual(out[1].shape, [])
# check grad shape and value
self.assertEqual(x.grad.shape, [])
self.assertTrue(x.grad.numpy() == 1)
def test_flatten(self): def test_flatten(self):
x = paddle.rand([]) x = paddle.rand([])
x.stop_gradient = False x.stop_gradient = False
...@@ -1126,6 +1168,28 @@ class TestSundryAPIStatic(unittest.TestCase): ...@@ -1126,6 +1168,28 @@ class TestSundryAPIStatic(unittest.TestCase):
self.assertEqual(res[0].shape, (5,)) self.assertEqual(res[0].shape, (5,))
self.assertEqual(res[0][3], 2) self.assertEqual(res[0][3], 2)
@prog_scope()
def test_kthvalue(self):
x = paddle.full([], 1, 'float32')
out = paddle.kthvalue(x, 1)
paddle.static.append_backward(out[0])
prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[out])
self.assertEqual(len(res[0].shape), 0)
self.assertEqual(len(res[0].shape), 0)
@prog_scope()
def test_mode(self):
x = paddle.full([], 1, 'float32')
out = paddle.mode(x)
paddle.static.append_backward(out[0])
prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[out])
self.assertEqual(len(res[0].shape), 0)
self.assertEqual(len(res[0].shape), 0)
@prog_scope() @prog_scope()
def test_flatten(self): def test_flatten(self):
x = paddle.full([], 1, 'float32') x = paddle.full([], 1, 'float32')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册