diff --git a/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc b/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc index 23b22a167eb42d..19a5168cf69453 100644 --- a/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc +++ b/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc @@ -99,7 +99,6 @@ void TuplePushInstruction::Run() { for (size_t i = 0; i < tuple_push_op_.tuple_size(); i++) { auto inlet_element_value = tuple_push_op_.inlet_element(i); Variable* var = value_exe_info_->GetVarByValue(inlet_element_value); - int stack_size = tuple_push_op_.tuple_size(); auto var_name = value_2_var_name.at(inlet_element_value); auto num_str = std::to_string(stack_element_var_array_->size()); @@ -110,7 +109,7 @@ void TuplePushInstruction::Run() { DeepCopyVariable(var, ©_var, value_exe_info_, - stack_size, + stack_element_var_array_->size(), is_optional, &src_to_dst); VLOG(10) << "done DeepCopyVariable " << new_name; diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index e31eeab446adf7..2db5131c3daa4d 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -169,7 +169,15 @@ void ArrayReadInferMeta(const MetaTensor& array, MetaTensor* out, MetaConfig config) { if (!config.is_runtime) { - out->set_dims({-1}); + auto dims = array.dims(); + if (dims.size() > 1) { + for (int i = 0; i < dims.size(); ++i) { + dims[i] = -1; + } + out->set_dims(dims); + } else { + out->set_dims({-1}); + } } else { double index = i.to(); out->set_dims(array.dims(index)); // NOLINT diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 142e80595e99c7..3ac65a31a345fd 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -213,6 +213,7 @@ void ArrayToTensorInferMeta(const MetaTensor& x, dims[axis] = -1; } out->set_dims(dims); + out->set_dtype(x.dtype()); out_index->set_dtype(DataType::INT32); out_index->set_dims(common::make_ddim({-1})); } diff --git a/python/paddle/autograd/ir_backward.py b/python/paddle/autograd/ir_backward.py index 7fafff46b600a9..3f24c38cdc44c4 100644 --- a/python/paddle/autograd/ir_backward.py +++ b/python/paddle/autograd/ir_backward.py @@ -870,9 +870,15 @@ def append_yield( else: state.op_to_opgrad[op] = [] else: - if not is_builtin_op( - op - ) and not paddle.core.is_forward_only(op): + all_results_stop_gradient = True + for value in op.results(): + if not value.stop_gradient: + all_results_stop_gradient = False + if ( + not is_builtin_op(op) + and not paddle.core.is_forward_only(op) + and not all_results_stop_gradient + ): raise ValueError( f"op '{op.name()}' has no grad op, consider enable prim to decompose it." ) diff --git a/python/paddle/nn/layer/rnn.py b/python/paddle/nn/layer/rnn.py index 5888afc638eea2..a036eddd059ebd 100644 --- a/python/paddle/nn/layer/rnn.py +++ b/python/paddle/nn/layer/rnn.py @@ -28,7 +28,7 @@ program_guard, ) from paddle.common_ops_import import Variable -from paddle.framework import core +from paddle.framework import core, in_pir_mode from paddle.nn import ( functional as F, initializer as I, @@ -106,7 +106,7 @@ def rnn( """ - if in_dynamic_or_pir_mode(): + if in_dynamic_mode(): return _rnn_dynamic_graph( cell, inputs, @@ -230,7 +230,9 @@ def _rnn_static_graph( is_reverse=False, **kwargs, ): - check_type(inputs, 'inputs', (Variable, list, tuple), 'rnn') + check_type( + inputs, 'inputs', (Variable, list, tuple, paddle.pir.Value), 'rnn' + ) if isinstance(inputs, (list, tuple)): for i, input_x in enumerate(inputs): check_variable_and_dtype( @@ -239,12 +241,15 @@ def _rnn_static_graph( check_type( initial_states, 'initial_states', - (Variable, list, tuple, type(None)), + (Variable, list, tuple, type(None), paddle.pir.Value), 'rnn', ) check_type( - sequence_length, 'sequence_length', (Variable, type(None)), 'rnn' + sequence_length, + 'sequence_length', + (Variable, type(None), paddle.pir.Value), + 'rnn', ) def _switch_grad(x, stop=False): @@ -261,7 +266,7 @@ def _switch_grad(x, stop=False): inputs = paddle.utils.map_structure(_transpose_batch_time, inputs) max_seq_len = paddle.shape(paddle.utils.flatten(inputs)[0])[0] - if sequence_length: + if sequence_length is not None: mask = paddle.static.nn.sequence_lod.sequence_mask( sequence_length, maxlen=max_seq_len, @@ -272,7 +277,11 @@ def _switch_grad(x, stop=False): inputs = paddle.utils.map_structure( lambda x: paddle.reverse(x, axis=[0]), inputs ) - mask = paddle.reverse(mask, axis=[0]) if sequence_length else None + mask = ( + paddle.reverse(mask, axis=[0]) + if sequence_length is not None + else None + ) with paddle.base.framework.device_guard("cpu"): start_i = paddle.zeros([], dtype="int64") @@ -303,9 +312,11 @@ def _switch_grad(x, stop=False): lambda x: paddle.tensor.array_read(x, start_i), init_array ) outputs, new_states = cell(step_in, pre_state, **kwargs) - assert isinstance(outputs, paddle.base.framework.Variable) + assert isinstance( + outputs, (paddle.base.framework.Variable, paddle.pir.Value) + ) paddle.utils.assert_same_structure(new_states, pre_state) - if sequence_length: + if sequence_length is not None: step_mask = paddle.unsqueeze(mask[start_i], 1) # new_states = map_structure( # partial(_maybe_copy, step_mask=step_mask), @@ -1614,19 +1625,34 @@ def flatten_parameters(self): default_startup_program(), default_startup_program() ): with paddle.no_grad(): - self._helper.append_op( - type="coalesce_tensor", - inputs={"Input": self._all_weights}, - outputs={ - "Output": self._all_weights, - "FusedOutput": self._flat_weight, - }, - attrs={ - "copy_data": True, - "use_align": False, - "dtype": params[0].dtype, - }, - ) + if in_pir_mode(): + _C_ops.coalesce_tensor( + self._all_weights, + params[0].dtype, + True, + False, + False, + 0.0, + False, + -1, + -1, + [], + [], + ) + else: + self._helper.append_op( + type="coalesce_tensor", + inputs={"Input": self._all_weights}, + outputs={ + "Output": self._all_weights, + "FusedOutput": self._flat_weight, + }, + attrs={ + "copy_data": True, + "use_align": False, + "dtype": params[0].dtype, + }, + ) def _cudnn_impl(self, inputs, initial_states, sequence_length): if not self.time_major: @@ -1712,7 +1738,9 @@ def forward(self, inputs, initial_states=None, sequence_length=None): else: initial_states = ( [initial_states] - if isinstance(initial_states, paddle.static.Variable) + if isinstance( + initial_states, (paddle.static.Variable, paddle.pir.Value) + ) else initial_states ) diff --git a/test/deprecated/rnn/CMakeLists.txt b/test/deprecated/rnn/CMakeLists.txt index a06731560086dd..e015f3f6bbcebe 100644 --- a/test/deprecated/rnn/CMakeLists.txt +++ b/test/deprecated/rnn/CMakeLists.txt @@ -10,6 +10,5 @@ endforeach() if(NOT WIN32) set_tests_properties(test_rnn_nets_static PROPERTIES TIMEOUT 120) set_tests_properties(test_rnn_nets_static_deprecated PROPERTIES TIMEOUT 120) - set_tests_properties(test_rnn_nets PROPERTIES TIMEOUT 120) set_tests_properties(test_rnn_nets_deprecated PROPERTIES TIMEOUT 120) endif() diff --git a/test/deprecated/rnn/test_rnn_nets_static.py b/test/deprecated/rnn/test_rnn_nets_static.py index 3ccdad1dfc71e7..20058e2be4bd3d 100644 --- a/test/deprecated/rnn/test_rnn_nets_static.py +++ b/test/deprecated/rnn/test_rnn_nets_static.py @@ -42,9 +42,7 @@ def __init__( self.place = place self.mode = mode - def setUp(self): - # Since `set_device` is global, set `set_device` in `setUp` rather than - # `__init__` to avoid using an error device set by another test case. + def test_with_initial_state(self): place = paddle.set_device(self.place) rnn1 = SimpleRNN( 16, @@ -74,23 +72,6 @@ def setUp(self): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone().clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(12, 4, 16) if not self.time_major: x = np.transpose(x, [1, 0, 2]) @@ -120,12 +101,34 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + place = paddle.set_device(self.place) + rnn1 = SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + nonlinearity=self.mode, + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + activation=self.mode[4:].lower(), + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) x = np.random.randn(12, 4, 16) if not self.time_major: @@ -163,7 +166,7 @@ def __init__(self, time_major=True, direction="forward", place="cpu"): self.num_directions = 2 if direction in bidirectional_list else 1 self.place = place - def setUp(self): + def test_with_initial_state(self): # Since `set_device` is global, set `set_device` in `setUp` rather than # `__init__` to avoid using an error device set by another test case. place = paddle.set_device(self.place) @@ -189,23 +192,6 @@ def setUp(self): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(12, 4, 16) if not self.time_major: x = np.transpose(x, [1, 0, 2]) @@ -236,12 +222,30 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = GRU( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.GRU( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) x = np.random.randn(12, 4, 16) if not self.time_major: @@ -279,7 +283,7 @@ def __init__(self, time_major=True, direction="forward", place="cpu"): self.num_directions = 2 if direction in bidirectional_list else 1 self.place = place - def setUp(self): + def test_with_initial_state(self): # Since `set_device` is global, set `set_device` in `setUp` rather than # `__init__` to avoid using an error device set by another test case. place = paddle.set_device(self.place) @@ -305,23 +309,6 @@ def setUp(self): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(12, 4, 16) if not self.time_major: x = np.transpose(x, [1, 0, 2]) @@ -364,12 +351,30 @@ def test_with_initial_state(self): np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = LSTM( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) x = np.random.randn(12, 4, 16) if not self.time_major: @@ -400,8 +405,15 @@ def runTest(self): self.test_with_zero_state() -class TestLSTMWithProjSize(TestLSTM): - def setUp(self): +class TestLSTMWithProjSize(unittest.TestCase): + def __init__(self, time_major=True, direction="forward", place="cpu"): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + + def test_with_initial_state(self): # Since `set_device` is global, set `set_device` in `setUp` rather than # `__init__` to avoid using an error device set by another test case. place = paddle.set_device(self.place) @@ -432,16 +444,109 @@ def setUp(self): with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) + self.proj_size = 8 + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + prev_h = np.random.randn( + 2 * self.num_directions, 4, getattr(self, "proj_size", 32) + ) + prev_c = np.random.randn(2 * self.num_directions, 4, 32) + + y1, (h1, c1) = rnn1(x, (prev_h, prev_c)) + + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + x_data = paddle.static.data( + "input", + [-1, -1, 16], + dtype=paddle.framework.get_default_dtype(), + ) + init_h = paddle.static.data( + "init_h", + [ + 2 * self.num_directions, + -1, + getattr(self, "proj_size", 32), + ], + dtype=paddle.framework.get_default_dtype(), + ) + init_c = paddle.static.data( + "init_c", + [2 * self.num_directions, -1, 32], + dtype=paddle.framework.get_default_dtype(), + ) + y, (h, c) = rnn2(x_data, (init_h, init_c)) + + feed_dict = {x_data.name: x, init_h.name: prev_h, init_c.name: prev_c} + with paddle.static.scope_guard(scope): + y2, h2, c2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h, c]) + + np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) + + def test_with_zero_state(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) self.proj_size = 8 - self.place = place - self.executor = exe - self.scope = scope + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + + y1, (h1, c1) = rnn1(x) + + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + x_data = paddle.static.data( + "input", + [-1, -1, 16], + dtype=paddle.framework.get_default_dtype(), + ) + y, (h, c) = rnn2(x_data) + + feed_dict = {x_data.name: x} + + with paddle.static.scope_guard(scope): + y2, h2, c2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h, c]) + + np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) + + def runTest(self): + self.test_with_initial_state() + self.test_with_zero_state() def load_tests(loader, tests, pattern): diff --git a/test/rnn/CMakeLists.txt b/test/rnn/CMakeLists.txt index 95739040ef4af7..fe9c6d0eae4ac8 100644 --- a/test/rnn/CMakeLists.txt +++ b/test/rnn/CMakeLists.txt @@ -7,3 +7,7 @@ string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") foreach(TEST_OP ${TEST_OPS}) py_test_modules(${TEST_OP} MODULES ${TEST_OP}) endforeach() + +if(NOT WIN32) + set_tests_properties(test_rnn_nets PROPERTIES TIMEOUT 120) +endif() diff --git a/test/deprecated/rnn/test_rnn_cells_static.py b/test/rnn/test_rnn_cells_static.py similarity index 83% rename from test/deprecated/rnn/test_rnn_cells_static.py rename to test/rnn/test_rnn_cells_static.py index 23f206e295b4ec..2f8b8d68044172 100644 --- a/test/deprecated/rnn/test_rnn_cells_static.py +++ b/test/rnn/test_rnn_cells_static.py @@ -35,7 +35,7 @@ def __init__(self, bias=True, place="cpu"): paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0) ) - def setUp(self): + def test_with_initial_state(self): rnn1 = SimpleRNNCell(16, 32, bias=self.bias) mp = paddle.static.Program() @@ -53,22 +53,6 @@ def setUp(self): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(4, 16) prev_h = np.random.randn(4, 32) @@ -95,12 +79,22 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + rnn1 = SimpleRNNCell(16, 32, bias=self.bias) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.SimpleRNNCell( + 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias + ) + + place = self.place + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_cell_static(rnn1, rnn2, place) x = np.random.randn(4, 16) @@ -137,7 +131,7 @@ def __init__(self, bias=True, place="cpu"): paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0) ) - def setUp(self): + def test_with_initial_state(self): rnn1 = GRUCell(16, 32, bias=self.bias) mp = paddle.static.Program() @@ -155,23 +149,6 @@ def setUp(self): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(4, 16) prev_h = np.random.randn(4, 32) @@ -198,12 +175,22 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + rnn1 = GRUCell(16, 32, bias=self.bias) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.GRUCell( + 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias + ) + + place = self.place + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_cell_static(rnn1, rnn2, place) x = np.random.randn(4, 16) @@ -240,7 +227,7 @@ def __init__(self, bias=True, place="cpu"): paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0) ) - def setUp(self): + def test_with_initial_state(self): rnn1 = LSTMCell(16, 32, bias=self.bias) mp = paddle.static.Program() @@ -258,23 +245,6 @@ def setUp(self): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(4, 16) prev_h = np.random.randn(4, 32) prev_c = np.random.randn(4, 32) @@ -308,12 +278,22 @@ def test_with_initial_state(self): np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + rnn1 = LSTMCell(16, 32, bias=self.bias) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTMCell( + 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias + ) + + place = self.place + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_cell_static(rnn1, rnn2, place) x = np.random.randn(4, 16) @@ -351,3 +331,8 @@ def load_tests(loader, tests, pattern): for test_class in [TestSimpleRNNCell, TestGRUCell, TestLSTMCell]: suite.addTest(test_class(bias, device)) return suite + + +if __name__ == "__main__": + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/rnn/test_rnn_cudnn_params_packing.py b/test/rnn/test_rnn_cudnn_params_packing.py similarity index 96% rename from test/deprecated/rnn/test_rnn_cudnn_params_packing.py rename to test/rnn/test_rnn_cudnn_params_packing.py index 473ad18bfc2e62..de9bc6f4803f12 100644 --- a/test/deprecated/rnn/test_rnn_cudnn_params_packing.py +++ b/test/rnn/test_rnn_cudnn_params_packing.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import unittest from unittest import TestCase import paddle @@ -53,3 +54,7 @@ def test_rnn_with_cudnn_clone(self): with paddle.base.program_guard(test_program, startup_prog): with paddle.base.unique_name.guard(): bilstm = create_model() + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/rnn/test_rnn_nets.py b/test/rnn/test_rnn_nets.py similarity index 100% rename from test/deprecated/rnn/test_rnn_nets.py rename to test/rnn/test_rnn_nets.py