From f301eb7f345288c39d7d8c3b7aa9c558d0258ab2 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 9 Apr 2020 10:58:36 +0800 Subject: [PATCH] api dygraph layer norm input check, test=develop (#23534) --- python/paddle/fluid/dygraph/nn.py | 3 +++ .../fluid/tests/unittests/test_layer_norm_op.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index 03fe763aaef..bea1f17e049 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -1478,6 +1478,9 @@ class LayerNorm(layers.Layer): return dygraph_utils._append_activation_in_dygraph( pre_act, act=self._act) + check_variable_and_dtype(input, 'input', ['float32', 'float64'], + 'LayerNorm') + inputs = dict() inputs['X'] = [input] if self._scale: diff --git a/python/paddle/fluid/tests/unittests/test_layer_norm_op.py b/python/paddle/fluid/tests/unittests/test_layer_norm_op.py index 18e83f9a569..bc475e19f4c 100644 --- a/python/paddle/fluid/tests/unittests/test_layer_norm_op.py +++ b/python/paddle/fluid/tests/unittests/test_layer_norm_op.py @@ -21,6 +21,7 @@ import paddle.fluid.core as core import paddle.fluid as fluid from functools import reduce from op_test import _set_use_system_allocator +from paddle.fluid import Program, program_guard np.random.random(123) @@ -213,5 +214,19 @@ class TestLayerNormAPI(unittest.TestCase): bias_attr="shift") +class TestDygraphLayerNormAPIError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + layer_norm = fluid.LayerNorm([32, 32]) + # the input of LayerNorm must be Variable. + x1 = np.random.random((3, 32, 32)).astype('float32') + self.assertRaises(TypeError, layer_norm, x1) + + # the input dtype of LayerNorm must be float32 or float64 + # float16 only can be set on GPU place + x2 = fluid.layers.data(name='x2', shape=[3, 32, 32], dtype="int32") + self.assertRaises(TypeError, layer_norm, x2) + + if __name__ == '__main__': unittest.main() -- GitLab