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

[AMP OP&Test]Add fp16/bf16 support isnan/isfinite/isinf op #52259

Merged
merged 17 commits into from
Apr 11, 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
21 changes: 21 additions & 0 deletions paddle/phi/kernels/funcs/isfinite_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ struct IsNanFunctor<phi::dtype::float16, void> {
}
};

template <>
struct IsNanFunctor<phi::dtype::bfloat16, void> {
HOSTDEVICE bool operator()(const phi::dtype::bfloat16& a) const {
return phi::dtype::isnan(a);
}
};

template <typename T, class Enable = void>
struct IsInfFunctor {
HOSTDEVICE bool operator()(const T& a) const {
Expand All @@ -69,6 +76,13 @@ struct IsInfFunctor<phi::dtype::float16, void> {
}
};

template <>
struct IsInfFunctor<phi::dtype::bfloat16, void> {
HOSTDEVICE bool operator()(const phi::dtype::bfloat16& a) const {
return phi::dtype::isinf(a);
}
};

template <typename T, class Enable = void>
struct IsFiniteFunctor {
HOSTDEVICE bool operator()(const T& a) const {
Expand All @@ -94,5 +108,12 @@ struct IsFiniteFunctor<phi::dtype::float16, void> {
}
};

template <>
struct IsFiniteFunctor<phi::dtype::bfloat16, void> {
HOSTDEVICE bool operator()(const phi::dtype::bfloat16& a) const {
return phi::dtype::isfinite(a);
}
};

} // namespace funcs
} // namespace phi
3 changes: 3 additions & 0 deletions paddle/phi/kernels/gpu/isfinite_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PD_REGISTER_KERNEL(isinf,
float,
double,
phi::dtype::float16,
phi::dtype::bfloat16,
int,
int64_t) {
kernel->OutputAt(0).SetDataType(phi::DataType::BOOL);
Expand All @@ -37,6 +38,7 @@ PD_REGISTER_KERNEL(isnan,
float,
double,
phi::dtype::float16,
phi::dtype::bfloat16,
int,
int64_t) {
kernel->OutputAt(0).SetDataType(phi::DataType::BOOL);
Expand All @@ -49,6 +51,7 @@ PD_REGISTER_KERNEL(isfinite,
float,
double,
phi::dtype::float16,
phi::dtype::bfloat16,
int,
int64_t) {
kernel->OutputAt(0).SetDataType(phi::DataType::BOOL);
Expand Down
52 changes: 52 additions & 0 deletions python/paddle/fluid/tests/unittests/test_isfinite_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import numpy as np
from eager_op_test import OpTest
from op_test import convert_float_to_uint16
Copy link
Contributor

Choose a reason for hiding this comment

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

eager_op_test中import

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


from paddle.fluid import core

Expand Down Expand Up @@ -48,6 +49,23 @@ def init_dtype(self):
self.dtype = np.float16


# BFP16 isinf Test
class TestInfBF16(OpTest):
def setUp(self):
self.op_type = "isinf"
self.dtype = np.uint16
x = np.random.uniform(0.1, 1, [11, 17]).astype(np.float32)
x[0] = np.inf
x[-1] = np.inf

out = np.isinf(x).astype(np.float32)
self.inputs = {'X': convert_float_to_uint16(x)}
self.outputs = {'Out': convert_float_to_uint16(out)}

def test_output(self):
self.check_output()
Copy link
Contributor

Choose a reason for hiding this comment

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

这里需要直接调用check_output_with_place,不然CPU的place会报错

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.



class TestNAN(OpTest):
def setUp(self):
self.op_type = "isnan"
Expand Down Expand Up @@ -76,6 +94,23 @@ def init_dtype(self):
self.dtype = np.float16


# BFP16 isnan Test
class TestNANBF16(OpTest):
def setUp(self):
self.op_type = "isnan"
self.dtype = np.uint16
x = np.random.uniform(0.1, 1, [11, 17]).astype(np.float32)
x[0] = np.nan
x[-1] = np.nan

out = np.isnan(x).astype(np.float32)
self.inputs = {'X': convert_float_to_uint16(x)}
self.outputs = {'Out': convert_float_to_uint16(out)}

def test_output(self):
self.check_output()


class TestIsfinite(OpTest):
def setUp(self):
self.op_type = "isfinite"
Expand Down Expand Up @@ -105,5 +140,22 @@ def init_dtype(self):
self.dtype = np.float16


# BFP16 isfinite Test
class TestIsfiniteBF16(OpTest):
def setUp(self):
self.op_type = "isfinite"
self.dtype = np.uint16
x = np.random.uniform(0.1, 1, [11, 17]).astype(np.float32)
x[0] = np.inf
x[-1] = np.nan

out = np.isfinite(x).astype(np.float32)
self.inputs = {'X': convert_float_to_uint16(x)}
self.outputs = {'Out': convert_float_to_uint16(out)}

def test_output(self):
self.check_output()


if __name__ == '__main__':
unittest.main()
33 changes: 30 additions & 3 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -3463,7 +3463,14 @@ def isfinite(x, name=None):
check_variable_and_dtype(
x,
'x',
['float16', 'float32', 'float64', 'int32', 'int64'],
[
'float16',
'float32',
'float64',
'int32',
'int64',
'uint16',
],
'isfinite',
)
out = helper.create_variable_for_type_inference('bool')
Expand Down Expand Up @@ -3499,7 +3506,17 @@ def isinf(x, name=None):
else:
helper = LayerHelper("isinf_v2", **locals())
check_variable_and_dtype(
x, 'x', ['float16', 'float32', 'float64', 'int32', 'int64'], 'isinf'
x,
'x',
[
'float16',
'float32',
'float64',
'int32',
'int64',
'uint16',
],
'isinf',
)
out = helper.create_variable_for_type_inference(dtype='bool')
helper.append_op(type="isinf_v2", inputs={"X": x}, outputs={"Out": out})
Expand Down Expand Up @@ -3532,7 +3549,17 @@ def isnan(x, name=None):
else:
helper = LayerHelper("isnan_v2", **locals())
check_variable_and_dtype(
x, 'x', ['float16', 'float32', 'float64', 'int32', 'int64'], 'isnan'
x,
'x',
[
'float16',
'float32',
'float64',
'int32',
'int64',
'uint16',
],
'isnan',
)
out = helper.create_variable_for_type_inference(dtype='bool')
helper.append_op(type="isnan_v2", inputs={"X": x}, outputs={"Out": out})
Expand Down