diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index aece48f5a714872dd34aa4afb02d05130298b258..ca84ffe5f7291f3a784ce36a50da6e58a90b64ed 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -9238,6 +9238,9 @@ def leaky_relu(x, alpha=0.02, name=None): if in_dygraph_mode(): return core.ops.leaky_relu(x, 'alpha', alpha) + check_variable_and_dtype(x, 'x', ['float16', 'float32', 'float64'], + 'leaky_relu') + inputs = {'X': [x]} attrs = {'alpha': alpha} helper = LayerHelper('leaky_relu', **locals()) diff --git a/python/paddle/fluid/tests/unittests/test_activation_op.py b/python/paddle/fluid/tests/unittests/test_activation_op.py index d37bb3e81cfceb9b1e41274c5c9fe49221f1341f..033e1dafc1ed4e19a2b6c96ae0a8d695d2e613b4 100644 --- a/python/paddle/fluid/tests/unittests/test_activation_op.py +++ b/python/paddle/fluid/tests/unittests/test_activation_op.py @@ -450,6 +450,20 @@ class TestLeakyRelu(TestActivation): self.check_grad(['X'], 'Out') +class TestLeakyReluOpError(unittest.TestCase): + def test_errors(self): + with program_guard(Program()): + # The input type must be Variable. + self.assertRaises(TypeError, fluid.layers.leaky_relu, 1) + # The input dtype must be float16, float32, float64. + x_int32 = fluid.data(name='x_int32', shape=[12, 10], dtype='int32') + self.assertRaises(TypeError, fluid.layers.leaky_relu, x_int32) + # support the input dtype is float32 + x_fp16 = fluid.layers.data( + name='x_fp16', shape=[12, 10], dtype='float32') + fluid.layers.leaky_relu(x_fp16) + + def gelu(x, approximate): if approximate: y_ref = 0.5 * x * (1.0 + np.tanh(