diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index 03fe763aaef1a203597d27f3d000713eefb25f16..bea1f17e04937dc1670399a38b2249c1dcb2b84c 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 18e83f9a56964b1372ad199bc765577bd4847243..bc475e19f4c6f51ad87916c77ff5e20e1343503b 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()