未验证 提交 432c5a39 编写于 作者: L Leo Chen 提交者: GitHub

don't add unregisterd attr, test=develop (#23047) (#23403)

* don't add unregisterd attr, test=develop

* add some unittests,test=develop
上级 3271cf54
...@@ -19,8 +19,8 @@ from .framework import dygraph_only ...@@ -19,8 +19,8 @@ from .framework import dygraph_only
@dygraph_only @dygraph_only
def _append_activation_in_dygraph(input, def _append_activation_in_dygraph(input,
act=None, act=None,
use_cudnn=False, use_cudnn=None,
use_mkldnn=False): use_mkldnn=None):
"""Append activation in dygraph mode. """Append activation in dygraph mode.
Args: Args:
...@@ -33,8 +33,11 @@ def _append_activation_in_dygraph(input, ...@@ -33,8 +33,11 @@ def _append_activation_in_dygraph(input,
""" """
if not act: if not act:
return input return input
attrs = {}
attrs = {'use_cudnn': use_cudnn, 'use_mkldnn': use_mkldnn} if (use_cudnn is not None) and use_cudnn:
attrs['use_cudnn'] = use_cudnn
if (use_mkldnn is not None) and use_mkldnn:
attrs['use_mkldnn'] = use_mkldnn
inputs = {"X": [input]} inputs = {"X": [input]}
act_op = getattr(core.ops, act) act_op = getattr(core.ops, act)
res = act_op(inputs, attrs) res = act_op(inputs, attrs)
...@@ -42,10 +45,7 @@ def _append_activation_in_dygraph(input, ...@@ -42,10 +45,7 @@ def _append_activation_in_dygraph(input,
@dygraph_only @dygraph_only
def _append_bias_in_dygraph( def _append_bias_in_dygraph(input, bias=None, axis=1):
input,
bias=None,
axis=1, ):
"""Append bias operation in dygraph mode. """Append bias operation in dygraph mode.
Args: Args:
......
...@@ -20,6 +20,7 @@ import paddle.fluid as fluid ...@@ -20,6 +20,7 @@ import paddle.fluid as fluid
from paddle.fluid import core from paddle.fluid import core
from paddle.fluid import Linear from paddle.fluid import Linear
from test_imperative_base import new_program_scope from test_imperative_base import new_program_scope
import paddle.fluid.dygraph_utils as dygraph_utils
class MyLayer(fluid.Layer): class MyLayer(fluid.Layer):
...@@ -511,5 +512,48 @@ class TestImperative(unittest.TestCase): ...@@ -511,5 +512,48 @@ class TestImperative(unittest.TestCase):
self.assertEqual(len(my_layer.sublayers()), 0) self.assertEqual(len(my_layer.sublayers()), 0)
class TestDygraphUtils(unittest.TestCase):
def test_append_activation_in_dygraph_exception(self):
with new_program_scope():
np_inp = np.random.random(size=(10, 20, 30)).astype(np.float32)
a = fluid.layers.data("a", [10, 20])
func = dygraph_utils._append_activation_in_dygraph
self.assertRaises(AssertionError, func, a, act="sigmoid")
def test_append_activation_in_dygraph1(self):
a_np = np.random.random(size=(10, 20, 30)).astype(np.float32)
func = dygraph_utils._append_activation_in_dygraph
with fluid.dygraph.guard():
a = fluid.dygraph.to_variable(a_np)
res1 = func(a, act="hard_sigmoid")
res2 = fluid.layers.hard_sigmoid(a)
self.assertTrue(np.array_equal(res1.numpy(), res2.numpy()))
def test_append_activation_in_dygraph2(self):
a_np = np.random.random(size=(10, 20, 30)).astype(np.float32)
func = dygraph_utils._append_activation_in_dygraph
with fluid.dygraph.guard():
a = fluid.dygraph.to_variable(a_np)
res1 = func(a, act="sigmoid", use_mkldnn=True, use_cudnn=True)
res2 = fluid.layers.sigmoid(a)
self.assertTrue(np.array_equal(res1.numpy(), res2.numpy()))
def test_append_bias_in_dygraph_exception(self):
with new_program_scope():
np_inp = np.random.random(size=(10, 20, 30)).astype(np.float32)
a = fluid.layers.data("a", [10, 20])
func = dygraph_utils._append_bias_in_dygraph
self.assertRaises(AssertionError, func, a)
def test_append_bias_in_dygraph(self):
a_np = np.random.random(size=(10, 20, 30)).astype(np.float32)
func = dygraph_utils._append_bias_in_dygraph
with fluid.dygraph.guard():
a = fluid.dygraph.to_variable(a_np)
res1 = func(a, bias=a)
res2 = a + a
self.assertTrue(np.array_equal(res1.numpy(), res2.numpy()))
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.
先完成此消息的编辑!
想要评论请 注册