Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
dc8e919
fix
enkilee Apr 14, 2023
7e61c2f
fix
enkilee Apr 14, 2023
4f18f49
fix
enkilee Apr 14, 2023
94f67d2
fix
enkilee Apr 14, 2023
e3d16ca
fix
enkilee Apr 15, 2023
74e7d69
fix
enkilee Apr 15, 2023
10d6425
fix
enkilee Apr 16, 2023
49732f5
fix
enkilee Apr 16, 2023
11ae686
fix
enkilee Apr 16, 2023
ae917a3
Merge branch 'PaddlePaddle:develop' into hackathon4-No61-remainder
enkilee Apr 18, 2023
c1af545
fix
enkilee Apr 20, 2023
1b7cd93
fix
enkilee Apr 20, 2023
88cdaa6
fix
enkilee Apr 20, 2023
1b801eb
Merge branch 'PaddlePaddle:develop' into hackathon4-No61-remainder
enkilee Apr 20, 2023
b088ca0
Merge branch 'develop' into hackathon4-No61-remainder
enkilee Apr 20, 2023
bbfd3d1
fix
enkilee Apr 25, 2023
9aaa0b4
fix
enkilee Apr 25, 2023
fee0bfd
Merge branch 'develop' into hackathon4-No61-remainder
enkilee Apr 25, 2023
5a393f0
fix
enkilee Apr 25, 2023
9f7e9e8
Merge branch 'hackathon4-No61-remainder' of https://github.com/enkile…
enkilee Apr 25, 2023
9d16970
fix
enkilee Apr 25, 2023
566cec1
fix
enkilee Apr 25, 2023
b45c546
fix
enkilee May 4, 2023
810a1a0
fix
enkilee May 4, 2023
7b0c693
fix
enkilee May 5, 2023
a745ae3
fix
enkilee May 5, 2023
5937120
fix
enkilee May 5, 2023
6e796c5
fix
enkilee May 5, 2023
c2c6d8d
fix
enkilee May 5, 2023
3b83e04
fix
enkilee May 11, 2023
15f6a3f
Merge branch 'PaddlePaddle:develop' into hackathon4-No61-remainder
enkilee May 11, 2023
49c9b01
fix
enkilee May 12, 2023
22bb915
Merge branch 'hackathon4-No61-remainder' of https://github.com/enkile…
enkilee May 12, 2023
ede0101
fix
enkilee May 12, 2023
14c9fd3
fix
enkilee May 13, 2023
5a17f29
fix
enkilee May 13, 2023
776706e
Merge branch 'PaddlePaddle:develop' into hackathon4-No61-remainder
enkilee May 15, 2023
ac782c6
fix
enkilee May 15, 2023
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
14 changes: 14 additions & 0 deletions paddle/phi/kernels/funcs/elementwise_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,20 @@ struct RemainderFunctor<dtype::float16> {
}
};

template <>
struct RemainderFunctor<dtype::bfloat16> {
inline HOSTDEVICE dtype::bfloat16 operator()(const dtype::bfloat16 a,
const dtype::bfloat16 b) const {
float b_float = static_cast<float>(b);
float res = fmod(static_cast<float>(a), b_float);

// Accoding to #PR26732: in dividen % divsor
// remainder shall have the same sign as divsor.
if ((res != 0.0f) && ((res < 0.0f) != (b_float < 0.0f))) res += b_float;
return static_cast<dtype::bfloat16>(res);
}
};

template <typename T, typename Enable = void>
struct InverseRemainderFunctor {
inline HOSTDEVICE T operator()(const T a, const T b) const {
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/kps/elementwise_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ PD_REGISTER_KERNEL(remainder,
double,
int,
int64_t,
phi::dtype::float16) {}
phi::dtype::float16,
phi::dtype::bfloat16) {}
PD_REGISTER_KERNEL(
floor_divide, KPS, ALL_LAYOUT, phi::FloorDivideKernel, int, int64_t) {}
PD_REGISTER_KERNEL(elementwise_pow,
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/kernels/legacy/kps/elementwise_raw_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ PD_REGISTER_KERNEL(remainder_raw,
double,
int,
float16,
int64_t) {}
int64_t,
bfloat16) {}
PD_REGISTER_KERNEL(floor_divide_raw,
KPS,
ALL_LAYOUT,
Expand Down
91 changes: 88 additions & 3 deletions python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
import unittest

import numpy as np
from eager_op_test import OpTest
from eager_op_test import (
OpTest,
convert_float_to_uint16,
convert_uint16_to_float,
)

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


class TestElementwiseModOp(OpTest):
Expand Down Expand Up @@ -106,14 +111,17 @@ def test_check_output(self):
self.check_output()


class TestElementwiseModOpFp16(TestElementwiseModOp):
@unittest.skipIf(
not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
class TestElementwiseModFP16Op(TestElementwiseModOp):
def init_dtype(self):
self.dtype = np.float16

def init_input_output(self):
self.x = np.random.uniform(-1000, 1000, [10, 10]).astype(self.dtype)
self.y = np.random.uniform(-100, 100, [10, 10]).astype(self.dtype)
self.out = np.mod(self.x, self.y)
self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y)

def test_check_output(self):
if self.attrs['axis'] == -1:
Expand All @@ -122,6 +130,83 @@ def test_check_output(self):
self.check_output()


class TestElementwiseModFP16Op_ZeroDim1(TestElementwiseModFP16Op):
def init_input_output(self):
self.x = np.random.uniform(0, 10000, []).astype(np.float16)
self.y = np.random.uniform(0, 1000, []).astype(np.float16)
self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y)


class TestElementwiseModFP16Op_ZeroDim2(TestElementwiseModFP16Op):
def init_input_output(self):
self.x = np.random.uniform(0, 10000, [10, 10]).astype(np.float16)
self.y = np.random.uniform(0, 1000, []).astype(np.float16)
self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y)


class TestElementwiseModFP16Op_ZeroDim3(TestElementwiseModFP16Op):
def init_input_output(self):
self.x = np.random.uniform(0, 10000, []).astype(np.float16)
self.y = np.random.uniform(0, 1000, [10, 10]).astype(np.float16)
self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y)


@unittest.skipIf(
not core.is_compiled_with_cuda()
or not core.is_bfloat16_supported(core.CUDAPlace(0)),
"core is not compiled with CUDA or not support the bfloat16",
)
class TestElementwiseModBF16Op(OpTest):
def init_kernel_type(self):
self.use_mkldnn = False

def init_input_output(self):
self.x = np.random.uniform(0, 10000, [10, 10]).astype(np.float32)
self.x = convert_uint16_to_float(convert_float_to_uint16(self.x))
self.y = np.random.uniform(0, 1000, [10, 10]).astype(np.float32)
self.y = convert_uint16_to_float(convert_float_to_uint16(self.y))
self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y)

def setUp(self):
self.op_type = "elementwise_mod"
self.python_api = paddle.remainder
self.public_python_api = paddle.remainder
self.axis = -1
self.init_dtype()
self.init_input_output()
self.init_kernel_type()
self.init_axis()
self.inputs = {
'X': convert_float_to_uint16(
OpTest.np_dtype_to_fluid_dtype(self.x)
),
'Y': convert_float_to_uint16(
OpTest.np_dtype_to_fluid_dtype(self.y)
),
}
self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn}
self.outputs = {'Out': convert_float_to_uint16(self.out)}

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

def init_dtype(self):
self.dtype = np.uint16

def init_axis(self):
pass


class TestElementwiseModBF16Op_ZeroDim1(TestElementwiseModBF16Op):
def init_input(self):
self.x = np.random.uniform(0, 10000, []).astype("float32")
self.x = convert_uint16_to_float(convert_float_to_uint16(self.x))
self.y = np.random.uniform(0, 1000, []).astype("float32")
self.y = convert_uint16_to_float(convert_float_to_uint16(self.y))
self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y)


class TestElementwiseModOpDouble(TestElementwiseModOpFloat):
def init_dtype(self):
self.dtype = np.float64
Expand Down