Skip to content

Commit 61ec75c

Browse files
authored
register fp16 for assign OP, test=release/1.7 (#22842)
1 parent 26d4513 commit 61ec75c

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

paddle/fluid/operators/assign_op.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ DECLARE_INPLACE_OP_INFERER(AssignOpInplaceInferer, {"X", "Out"});
114114
} // namespace paddle
115115

116116
namespace ops = paddle::operators;
117+
namespace plat = paddle::platform;
117118
REGISTER_OPERATOR(assign, ops::AssignOp,
118119
ops::AssignGradMaker<paddle::framework::OpDesc>,
119120
ops::AssignGradMaker<paddle::imperative::OpBase>,
@@ -122,11 +123,13 @@ REGISTER_OPERATOR(assign, ops::AssignOp,
122123
REGISTER_OP_CPU_KERNEL_FUNCTOR(assign, float, ops::AssignKernel, double,
123124
ops::AssignKernel, int, ops::AssignKernel,
124125
int64_t, ops::AssignKernel, bool,
126+
ops::AssignKernel, plat::float16,
125127
ops::AssignKernel);
126128

127129
#ifdef PADDLE_WITH_CUDA
128130
REGISTER_OP_CUDA_KERNEL_FUNCTOR(assign, float, ops::AssignKernel, double,
129131
ops::AssignKernel, int, ops::AssignKernel,
130132
int64_t, ops::AssignKernel, bool,
133+
ops::AssignKernel, plat::float16,
131134
ops::AssignKernel);
132135
#endif

python/paddle/fluid/tests/unittests/test_assign_op.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,39 @@ def test_backward(self):
3737
self.check_grad(['X'], 'Out')
3838

3939

40+
class TestAssignFP16Op(op_test.OpTest):
41+
def setUp(self):
42+
self.op_type = "assign"
43+
x = np.random.random(size=(100, 10)).astype('float16')
44+
self.inputs = {'X': x}
45+
self.outputs = {'Out': x}
46+
47+
def test_forward(self):
48+
self.check_output()
49+
50+
def test_backward(self):
51+
self.check_grad(['X'], 'Out')
52+
53+
4054
class TestAssignOpError(unittest.TestCase):
4155
def test_errors(self):
4256
with program_guard(Program(), Program()):
4357
# The type of input must be Variable or numpy.ndarray.
4458
x1 = fluid.create_lod_tensor(
4559
np.array([[-1]]), [[1]], fluid.CPUPlace())
4660
self.assertRaises(TypeError, fluid.layers.assign, x1)
47-
# When the type of input is Variable, the dtype of input must be float32, float64, int32, int64, bool.
48-
x3 = fluid.layers.data(name='x3', shape=[4], dtype="float16")
61+
# When the type of input is Variable, the dtype of input must be float16, float32, float64, int32, int64, bool.
62+
x3 = fluid.layers.data(name='x3', shape=[4], dtype="uint8")
4963
self.assertRaises(TypeError, fluid.layers.assign, x3)
50-
x4 = fluid.layers.data(name='x4', shape=[4], dtype="uint8")
51-
self.assertRaises(TypeError, fluid.layers.assign, x4)
5264
# When the type of input is numpy.ndarray, the dtype of input must be float32, int32.
53-
x5 = np.array([[2.5, 2.5]], dtype='bool')
65+
x4 = np.array([[2.5, 2.5]], dtype='bool')
66+
self.assertRaises(TypeError, fluid.layers.assign, x4)
67+
x5 = np.array([[2.5, 2.5]], dtype='float64')
5468
self.assertRaises(TypeError, fluid.layers.assign, x5)
55-
x6 = np.array([[2.5, 2.5]], dtype='float16')
69+
x6 = np.array([[2.5, 2.5]], dtype='int64')
5670
self.assertRaises(TypeError, fluid.layers.assign, x6)
57-
x7 = np.array([[2.5, 2.5]], dtype='float64')
71+
x7 = np.array([[2.5, 2.5]], dtype='uint8')
5872
self.assertRaises(TypeError, fluid.layers.assign, x7)
59-
x8 = np.array([[2.5, 2.5]], dtype='int64')
60-
self.assertRaises(TypeError, fluid.layers.assign, x8)
61-
x9 = np.array([[2.5, 2.5]], dtype='uint8')
62-
self.assertRaises(TypeError, fluid.layers.assign, x9)
6373

6474

6575
if __name__ == '__main__':

0 commit comments

Comments
 (0)