未验证 提交 f301eb7f 编写于 作者: C Chen Weihang 提交者: GitHub

api dygraph layer norm input check, test=develop (#23534)

上级 d7e549f5
...@@ -1478,6 +1478,9 @@ class LayerNorm(layers.Layer): ...@@ -1478,6 +1478,9 @@ class LayerNorm(layers.Layer):
return dygraph_utils._append_activation_in_dygraph( return dygraph_utils._append_activation_in_dygraph(
pre_act, act=self._act) pre_act, act=self._act)
check_variable_and_dtype(input, 'input', ['float32', 'float64'],
'LayerNorm')
inputs = dict() inputs = dict()
inputs['X'] = [input] inputs['X'] = [input]
if self._scale: if self._scale:
......
...@@ -21,6 +21,7 @@ import paddle.fluid.core as core ...@@ -21,6 +21,7 @@ import paddle.fluid.core as core
import paddle.fluid as fluid import paddle.fluid as fluid
from functools import reduce from functools import reduce
from op_test import _set_use_system_allocator from op_test import _set_use_system_allocator
from paddle.fluid import Program, program_guard
np.random.random(123) np.random.random(123)
...@@ -213,5 +214,19 @@ class TestLayerNormAPI(unittest.TestCase): ...@@ -213,5 +214,19 @@ class TestLayerNormAPI(unittest.TestCase):
bias_attr="shift") 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__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册