From 7a37929e906707aa82830b96d0660ce28c7f3176 Mon Sep 17 00:00:00 2001 From: enkilee Date: Tue, 8 Aug 2023 03:57:57 +0000 Subject: [PATCH 1/4] fix --- .../phi/kernels/gpu/nanmedian_grad_kernel.cu | 3 +- paddle/phi/kernels/gpu/nanmedian_kernel.cu | 3 +- test/legacy_test/test_nanmedian.py | 44 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/paddle/phi/kernels/gpu/nanmedian_grad_kernel.cu b/paddle/phi/kernels/gpu/nanmedian_grad_kernel.cu index cb627654a7ce1..c2989e6e6075f 100644 --- a/paddle/phi/kernels/gpu/nanmedian_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/nanmedian_grad_kernel.cu @@ -123,4 +123,5 @@ PD_REGISTER_KERNEL(nanmedian_grad, double, int, int64_t, - phi::dtype::float16) {} + phi::dtype::float16, + phi::dtype::bfloat16) {} diff --git a/paddle/phi/kernels/gpu/nanmedian_kernel.cu b/paddle/phi/kernels/gpu/nanmedian_kernel.cu index 5a9d3a07cf55d..ccd1b5561f081 100644 --- a/paddle/phi/kernels/gpu/nanmedian_kernel.cu +++ b/paddle/phi/kernels/gpu/nanmedian_kernel.cu @@ -287,6 +287,7 @@ PD_REGISTER_KERNEL(nanmedian, double, int, int64_t, - phi::dtype::float16) { + phi::dtype::float16, + phi::dtype::bfloat16) { kernel->OutputAt(1).SetDataType(phi::DataType::INT64); } diff --git a/test/legacy_test/test_nanmedian.py b/test/legacy_test/test_nanmedian.py index 8b5f86232f0e3..817966a8f9dc7 100644 --- a/test/legacy_test/test_nanmedian.py +++ b/test/legacy_test/test_nanmedian.py @@ -15,6 +15,7 @@ import unittest import numpy as np +from eager_op_test import OpTest, convert_float_to_uint16 import paddle from paddle.fluid import core @@ -243,5 +244,48 @@ def test_check_grad_0d(self): np.testing.assert_allclose(x.grad, np.array(0.0)) +class TestNanmedianFP16Op(OpTest): + def setUp(self): + self.op_type = "nanmedian" + self.python_api = paddle.nanmedian + self.public_python_api = paddle.nanmedian + self.dtype = np.float16 + X = np.random.random((2, 3)).astype('float16') + Out = np.nanmedian(X) + self.inputs = {'X': X} + self.outputs = {'Out': Out} + + def test_check_output(self): + self.check_output() + + def test_check_grad(self): + self.check_grad(['X'], 'Out') + + +@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 TestNanmedianBF16Op(OpTest): + def setUp(self): + self.op_type = "nanmedian" + self.python_api = paddle.nanmedian + self.public_python_api = paddle.nanmedian + self.dtype = np.uint16 + X = np.random.random((2, 3)).astype('float32') + Out = np.nanmedian(X) + self.inputs = {'X': convert_float_to_uint16(X)} + self.outputs = {'Out': convert_float_to_uint16(Out)} + + def test_check_output(self): + place = core.CUDAPlace(0) + self.check_output_with_place(place) + + def test_check_grad(self): + place = core.CUDAPlace(0) + self.check_grad_with_place(place, ['X'], 'Out') + + if __name__ == "__main__": unittest.main() From 1e685431590d7a3a97b5fe63f3935e77e52b1fb3 Mon Sep 17 00:00:00 2001 From: enkilee Date: Tue, 8 Aug 2023 04:44:50 +0000 Subject: [PATCH 2/4] add uint16 --- python/paddle/tensor/stat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index 69789dbafd429..6fc4fe640e4a3 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -265,7 +265,7 @@ def nanmedian(x, axis=None, keepdim=False, name=None): the average value of both elements in the middle is calculated as the median. Args: - x (Tensor): The input Tensor, it's data type can be int32, int64, float16, float32, float64. + x (Tensor): The input Tensor, it's data type can be int32, int64, float16, bfloat16, float32, float64. axis (None|int|list|tuple, optional): The axis along which to perform median calculations ``axis`` should be int or list of int. ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` . @@ -319,7 +319,7 @@ def nanmedian(x, axis=None, keepdim=False, name=None): check_variable_and_dtype( x, 'X', - ['int32', 'int64', 'float16', 'float32', 'float64'], + ['int32', 'int64', 'float16', 'float32', 'float64', 'uint16'], 'nanmedian', ) From c0b94aeb7f15e709d1b926b3549759acd9afecc2 Mon Sep 17 00:00:00 2001 From: enkilee Date: Tue, 8 Aug 2023 07:52:55 +0000 Subject: [PATCH 3/4] fix --- test/legacy_test/test_nanmedian.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/legacy_test/test_nanmedian.py b/test/legacy_test/test_nanmedian.py index 817966a8f9dc7..ee678747dacd1 100644 --- a/test/legacy_test/test_nanmedian.py +++ b/test/legacy_test/test_nanmedian.py @@ -250,6 +250,7 @@ def setUp(self): self.python_api = paddle.nanmedian self.public_python_api = paddle.nanmedian self.dtype = np.float16 + self.python_out_sig = ["Out"] X = np.random.random((2, 3)).astype('float16') Out = np.nanmedian(X) self.inputs = {'X': X} @@ -273,6 +274,7 @@ def setUp(self): self.python_api = paddle.nanmedian self.public_python_api = paddle.nanmedian self.dtype = np.uint16 + self.python_out_sig = ["Out"] X = np.random.random((2, 3)).astype('float32') Out = np.nanmedian(X) self.inputs = {'X': convert_float_to_uint16(X)} From cc5d43da2ae87939156d5f8b3021d0e446a2eeb0 Mon Sep 17 00:00:00 2001 From: enkilee Date: Wed, 9 Aug 2023 02:48:33 +0000 Subject: [PATCH 4/4] fix --- test/legacy_test/test_nanmedian.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_nanmedian.py b/test/legacy_test/test_nanmedian.py index ee678747dacd1..ba3fe7f5039bd 100644 --- a/test/legacy_test/test_nanmedian.py +++ b/test/legacy_test/test_nanmedian.py @@ -251,7 +251,7 @@ def setUp(self): self.public_python_api = paddle.nanmedian self.dtype = np.float16 self.python_out_sig = ["Out"] - X = np.random.random((2, 3)).astype('float16') + X = np.random.random((100, 100)).astype('float16') Out = np.nanmedian(X) self.inputs = {'X': X} self.outputs = {'Out': Out} @@ -275,7 +275,7 @@ def setUp(self): self.public_python_api = paddle.nanmedian self.dtype = np.uint16 self.python_out_sig = ["Out"] - X = np.random.random((2, 3)).astype('float32') + X = np.random.random((100, 100)).astype('float32') Out = np.nanmedian(X) self.inputs = {'X': convert_float_to_uint16(X)} self.outputs = {'Out': convert_float_to_uint16(Out)}