From 120e9ed0bbd563b22d23f00a0258f3a42240f4af Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Fri, 11 Oct 2019 21:12:37 +0800 Subject: [PATCH] [cherry-pick] Fix fp16 in input.dtype check in layers.fc (#20467) test=develop, (#20500) test=release/1.6 * Add fp16 in input.dtype check test=develop * Add warning of fp16 in CPU test=develop * add unittest code for fp16 test=develop * fix float16 list error test=develop --- python/paddle/fluid/layers/nn.py | 7 +++++-- python/paddle/fluid/tests/unittests/test_fc_op.py | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index c6a1c218911..b6d089a9ab1 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -355,9 +355,12 @@ def fc(input, "The type of 'input' in fc must be Variable, but received %s" % (type(input))) dtype = helper.input_dtype() - if convert_dtype(dtype) not in ['float32', 'float64']: + if convert_dtype(dtype) in ['float16']: + warnings.warn( + "The data type of 'input' in fc only support float16 in GPU now.") + if convert_dtype(dtype) not in ['float16', 'float32', 'float64']: raise TypeError( - "The data type of 'input' in fc must be float32 or float64, but received %s." + "The data type of 'input' in fc must be float16, float32 or float64, but received %s." % (convert_dtype(dtype))) mul_results = [] diff --git a/python/paddle/fluid/tests/unittests/test_fc_op.py b/python/paddle/fluid/tests/unittests/test_fc_op.py index e50579f76d6..0da0fd0789a 100644 --- a/python/paddle/fluid/tests/unittests/test_fc_op.py +++ b/python/paddle/fluid/tests/unittests/test_fc_op.py @@ -148,6 +148,10 @@ class TestFCOpError(OpTest): self.assertRaises(TypeError, test_type) + # The input dtype of fc can be float16 in GPU, test for warning + x3 = fluid.layers.data(name='x3', shape=[4], dtype='float16') + fluid.layers.fc(input=x3, size=1) + if __name__ == "__main__": unittest.main() -- GitLab