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

【Hackathon4 No58】kthvalue #51615

Merged
merged 29 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion paddle/phi/kernels/gpu/kthvalue_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include "paddle/phi/kernels/kthvalue_grad_kernel.h"

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
Expand Down Expand Up @@ -76,4 +75,5 @@ PD_REGISTER_KERNEL(kthvalue_grad,
double,
int,
int64_t,
phi::dtype::bfloat16,
phi::dtype::float16) {}
3 changes: 2 additions & 1 deletion paddle/phi/kernels/gpu/kthvalue_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

#include "paddle/phi/kernels/kthvalue_kernel.h"

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/common/amp_type_traits.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"
Expand Down Expand Up @@ -287,6 +287,7 @@ PD_REGISTER_KERNEL(kthvalue,
double,
int,
int64_t,
phi::dtype::bfloat16,
phi::dtype::float16) {
kernel->OutputAt(1).SetDataType(phi::DataType::INT64);
}
72 changes: 71 additions & 1 deletion python/paddle/fluid/tests/unittests/test_kthvalue_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import unittest

import numpy as np
from eager_op_test import OpTest
from eager_op_test import OpTest, convert_float_to_uint16

import paddle
from paddle import fluid
from paddle.fluid import core


def cal_kthvalue(x, k, axis, keepdim=False):
Expand Down Expand Up @@ -207,5 +208,74 @@ def test_run_static(self):
np.testing.assert_allclose(paddle_result, expect_value, rtol=1e-05)


class TestKthvalueFP16Op(OpTest):
def init_args(self):
self.k = 5
self.axis = -1
self.keepdim = False
self.input_data = np.random.random((2, 1, 2, 4, 10))
self.dtype = np.float16

def setUp(self):
self.op_type = "kthvalue"
self.python_api = paddle.kthvalue
self.init_args()
self.inputs = {'X': self.input_data}
self.attrs = {'k': self.k, 'axis': self.axis, 'keepdim': self.keepdim}
output, indices = cal_kthvalue(
self.input_data, k=self.k, axis=self.axis, keepdim=self.keepdim
)
self.outputs = {'Out': output, 'Indices': indices}

def test_check_output(self):
paddle.enable_static()
self.check_output()

def test_check_grad(self):
paddle.enable_static()
self.check_grad({'X'}, 'Out')


class TestKthvalueWithKeepdimFP16Op(TestKthvalueFP16Op):
def init_args(self):
self.k = 2
self.axis = 1
self.keepdim = True
self.input_data = np.random.random((1, 3, 2, 4, 10))
self.dtype = np.float16


@unittest.skipIf(
not core.is_compiled_with_cuda()
or not core.is_bfloat16_supported(core.CUDAPlace(0)),
"core is not complied with CUDA and not support the bfloat16",
)
class TestKthvalueBF16Op(OpTest):
def init_args(self):
self.k = 2
self.axis = 1

def setUp(self):
self.init_args()
self.op_type = 'kthvalue'
self.python_api = paddle.kthvalue
self.dtype = np.uint16
x = np.random.random((1, 3, 2, 4, 10))
self.inputs = {'X': convert_float_to_uint16(x)}
self.attrs = {'k': self.k, 'axis': self.axis, 'keepdim': True}
out, indices = cal_kthvalue(x, k=self.k, axis=self.axis, keepdim=True)
self.outputs = {'Out': convert_float_to_uint16(out), 'Indices': indices}

def test_check_output(self):
paddle.enable_static()
place = core.CUDAPlace(0)
self.check_output_with_place(place)

def test_check_grad(self):
paddle.enable_static()
place = core.CUDAPlace(0)
self.check_grad_with_place(place, {'X'}, 'Out')


if __name__ == '__main__':
unittest.main()