From 7ca8966314d44cefe92629d5957256393b45c080 Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 14 Oct 2021 14:24:41 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E4=B8=BAtuple=E7=9A=84=E6=AF=94=E8=BE=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/apibase.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/api/nn/apibase.py b/framework/api/nn/apibase.py index fb6ac295ff..4bb9a09247 100755 --- a/framework/api/nn/apibase.py +++ b/framework/api/nn/apibase.py @@ -328,7 +328,7 @@ def _baserun(self, res, data=None, **kwargs): paddle.seed(self.seed) self._check_params(res, data, **kwargs) dygraph_forward_res = self._dygraph_forward() - if isinstance(dygraph_forward_res, (list)): + if isinstance(dygraph_forward_res, (list, tuple)): compare(dygraph_forward_res, res, self.delta, self.rtol) else: compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) @@ -687,7 +687,7 @@ def compare(result, expect, delta=1e-6, rtol=1e-5): assert res # tools.assert_equal(result.shape, expect.shape) assert result.shape == expect.shape - elif isinstance(result, list): + elif isinstance(result, (list, tuple)): for i, j in enumerate(result): if isinstance(j, (np.generic, np.ndarray)): compare(j, expect[i], delta, rtol) From 60c23292b0c9d3061269c70ed0279b20da6e8b66 Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 14 Oct 2021 14:25:32 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E7=A1=80base?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E3=80=82=E7=A7=BB=E4=BA=A4gpu=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E6=B5=8B=E8=AF=95gpu=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/test_rnn.py | 255 +++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 framework/api/nn/test_rnn.py diff --git a/framework/api/nn/test_rnn.py b/framework/api/nn/test_rnn.py new file mode 100644 index 0000000000..1f4de4ad69 --- /dev/null +++ b/framework/api/nn/test_rnn.py @@ -0,0 +1,255 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python +""" +paddle.nn.AdaptiveAvgPool3D +""" +import copy + +from apibase import APIBase +from apibase import randtool +import paddle +import pytest +import numpy as np + +_dtype = np.float32 + + +class TestRNN(APIBase): + """ + test + """ + + def hook(self): + """ + implement + """ + self.types = [np.float32] + self.seed = 100 + # self.enable_backward=False + self.delta = 0.0001 + + def _static_forward(self, res, data=None, **kwargs): + """ + _static_forward + """ + paddle.disable_static() + params = copy.deepcopy(kwargs) + paddle.enable_static() + + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + main_program.random_seed = self.seed + + # startup_program.random_seed = self.seed + # + # with paddle.fluid.unique_name.guard(): + # with paddle.static.program_guard(main_program, startup_program): + # cell = params['cell'] + + with paddle.utils.unique_name.guard(): + with paddle.static.program_guard(main_program=main_program, startup_program=startup_program): + # PS:没有单列出一个函数做值传递,因为self.kwargs只有一个,就没单列出来 + # for k, v in kwargs.items(): + # if isinstance(v, (np.generic, np.ndarray)): + # # no_grad_Var不需要转换类型 + # if self.no_grad_var is not None and k in self.no_grad_var: + # kwargs[k] = v + # else: + # kwargs[k] = v.astype(self.dtype) + # for k, v in params.items(): + # if isinstance(v, (np.generic, np.ndarray)): + # # no_grad_Var不需要转换类型 + # if self.no_grad_var is not None and k in self.no_grad_var: + # params[k] = paddle.static.data(name=k, shape=v.shape, dtype=v.dtype) + # else: + # params[k] = paddle.static.data(name=k, shape=v.shape, dtype=self.dtype) + # # enable compute gradient + # params[k].stop_gradient = False + if data is not None: + data = data.astype(self.dtype) + self.data = paddle.static.data(name="data", shape=data.shape, dtype=self.dtype) + self.data.stop_gradient = False + data = dict({"data": data}, **kwargs) + static_cell = paddle.nn.SimpleRNNCell(4, 8) + parameters = {} + for k, v in params["cell"].named_parameters(): + parameters[k] = v + + obj = self.func(static_cell) + output, h = obj(self.data) + + if self.enable_backward: + loss = paddle.mean(output) + g = paddle.static.gradients(loss, self.data) + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + for k, v in static_cell.named_parameters(): + v.set_value(parameters[k]) + res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) + grad = {"data": res[2]} + return res[0:2], grad + else: + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + for k, v in static_cell.named_parameters(): + v.set_value(parameters[k]) + res = exe.run(main_program, feed=data, fetch_list=[output, h], return_numpy=True) + return res + + +obj = TestRNN(paddle.nn.RNN) + + +@pytest.mark.api_nn_RNN_parameters +def test_rnn(): + """ + base + """ + np.random.seed(obj.seed) + np_cell_params = { + "weight_ih": np.array( + [ + [-0.22951947, -0.08992132, -0.34953101, -0.175061], + [0.20906496, -0.3427665, 0.06989282, 0.07340089], + [-0.27920275, -0.08347859, -0.32776092, 0.27606266], + [0.3400624, -0.311168, 0.27615769, 0.05437757], + [0.17145903, 0.09205394, 0.05787117, -0.33910074], + [-0.20504217, 0.03159698, 0.19029316, -0.17628509], + [-0.15139461, 0.24918096, 0.33588031, 0.27213237], + [-0.09934296, 0.06990383, -0.10267501, -0.11300258], + ], + dtype=_dtype, + ), + "weight_hh": np.array( + [ + [-0.22763112, -0.1854782, -0.32183097, 0.0038406, -0.08750273, 0.06562333, 0.09188278, -0.25271974], + [0.30677212, 0.31563824, 0.07233466, -0.07936122, -0.09674069, -0.20905946, -0.15785094, -0.1792262], + [-0.230794, 0.32994288, 0.32315671, 0.06927786, 0.16355433, -0.11286469, -0.28846025, -0.0258108], + [0.00615105, -0.2910026, 0.0198239, 0.34800829, -0.0742208, -0.11625087, 0.21598615, 0.1798519], + [-0.13218199, 0.09477825, 0.02857035, -0.14368852, -0.27521451, -0.13248332, -0.03042035, 0.1123876], + [-0.17376618, 0.09977366, -0.21204463, 0.11145757, 0.19678019, 0.19770592, 0.07801379, -0.13505715], + [0.13981969, 0.25428854, 0.08861728, 0.34111385, 0.33693647, -0.23568284, -0.33716397, -0.23988983], + [0.29945748, 0.32070817, -0.20436912, -0.09862354, 0.03491358, -0.16133995, -0.02785886, 0.13870717], + ], + dtype=_dtype, + ), + "bias_ih": np.array( + [ + 2.51656952e-04, + 1.52785263e-01, + 1.83536185e-02, + -3.52564132e-01, + -7.44581413e-02, + -5.53878870e-03, + -6.86739763e-02, + -1.03026660e-01, + ], + dtype=_dtype, + ), + "bias_hh": np.array( + [0.00043439, -0.03876598, -0.28960775, -0.16011519, 0.31358566, -0.33478349, -0.32527005, -0.15334292], + dtype=_dtype, + ), + } + + inputs = randtool("float", 0, 1, (2, 3, 4)) + + # prev_h = randtool("float", 0, 1, (2, 8)) + cell = paddle.nn.SimpleRNNCell(4, 8) + # set the weights + for k, v in cell.named_parameters(): + v.set_value(np_cell_params[k]) + + res_outputs = [ + [ + [ + -0.41805826338051666, + 0.22022120468076872, + -0.338273189723404, + -0.24617478149067853, + 0.09573324424067524, + -0.47079110200581753, + -0.03435668862995602, + -0.40527382273501933, + ], + [ + -0.16655437807618048, + 0.2769589075445124, + -0.14167845285320205, + -0.47032005200197013, + 0.1028938747907291, + -0.2342414953727308, + 0.21773937893402742, + -0.33428361063120415, + ], + [ + -0.30962178642274835, + 0.14995033887532402, + -0.5014789422897543, + -0.5575169371542475, + 0.3508976438664536, + -0.14612803096924393, + 0.055716862608673354, + -0.23707090722122118, + ], + ], + [ + [ + -0.2910432922599807, + 0.20006867702342945, + -0.13309779241132294, + -0.35354838575117636, + -0.0382390069374574, + -0.46651599812797767, + -0.0548339255988763, + -0.3802476536920616, + ], + [ + -0.379896702128952, + 0.440035048064823, + -0.5127017093856834, + -0.2530757972388444, + 0.4495343467088128, + -0.3693239736103003, + -0.04066560667765106, + -0.3609407859877686, + ], + [ + -0.32279289579646764, + 0.07392642831034953, + -0.42972779798163085, + -0.6088280646556109, + 0.320803940263583, + -0.05130848141305536, + 0.3858006307601003, + -0.1703522265261066, + ], + ], + ] + + res_final_test = [ + [ + -0.30962178642274835, + 0.14995033887532402, + -0.5014789422897543, + -0.5575169371542475, + 0.3508976438664536, + -0.14612803096924393, + 0.055716862608673354, + -0.23707090722122118, + ], + [ + -0.32279289579646764, + 0.07392642831034953, + -0.42972779798163085, + -0.6088280646556109, + 0.320803940263583, + -0.05130848141305536, + 0.3858006307601003, + -0.1703522265261066, + ], + ] + + res = [res_outputs, res_final_test] + obj.base(res, data=inputs, cell=cell) From aceaaa01cc013c7f65de9bbb1a63511a1390f89c Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 14 Oct 2021 18:36:08 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E5=9C=A8compare=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E5=89=8D=E5=A2=9E=E5=8A=A0tuple=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/apibase.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/api/nn/apibase.py b/framework/api/nn/apibase.py index 4bb9a09247..4abcd660b0 100755 --- a/framework/api/nn/apibase.py +++ b/framework/api/nn/apibase.py @@ -209,7 +209,7 @@ def run(self, res, data=None, **kwargs): paddle.seed(self.seed) self._check_params(res, data, **kwargs) dygraph_forward_res = self._dygraph_forward() - if isinstance(dygraph_forward_res, (list)): + if isinstance(dygraph_forward_res, (list, tuple)): compare(dygraph_forward_res, res, self.delta, self.rtol) else: compare(dygraph_forward_res.numpy(), res, self.delta, self.rtol) @@ -267,7 +267,7 @@ def _baserun(self, res, data=None, **kwargs): self._check_params(res, data, **kwargs) dygraph_forward_res = self._dygraph_forward() logging.info("dygraph forward result is :") - if isinstance(dygraph_forward_res, (list)): + if isinstance(dygraph_forward_res, (list, tuple)): compare(dygraph_forward_res, res, self.delta, self.rtol) logging.info(dygraph_forward_res) else: From 8a5f652dd056566998d43ac4012842da23f196cd Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 14 Oct 2021 18:37:46 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A03=E4=B8=AA=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/test_rnn.py | 494 ++++++++++++++++++++++++++--------- 1 file changed, 366 insertions(+), 128 deletions(-) diff --git a/framework/api/nn/test_rnn.py b/framework/api/nn/test_rnn.py index 1f4de4ad69..bf146fd153 100644 --- a/framework/api/nn/test_rnn.py +++ b/framework/api/nn/test_rnn.py @@ -28,44 +28,19 @@ def hook(self): self.seed = 100 # self.enable_backward=False self.delta = 0.0001 + self.forward_kwargs = {} # 前向传播参数 def _static_forward(self, res, data=None, **kwargs): """ _static_forward """ - paddle.disable_static() - params = copy.deepcopy(kwargs) - paddle.enable_static() main_program = paddle.static.Program() startup_program = paddle.static.Program() main_program.random_seed = self.seed - # startup_program.random_seed = self.seed - # - # with paddle.fluid.unique_name.guard(): - # with paddle.static.program_guard(main_program, startup_program): - # cell = params['cell'] - with paddle.utils.unique_name.guard(): with paddle.static.program_guard(main_program=main_program, startup_program=startup_program): - # PS:没有单列出一个函数做值传递,因为self.kwargs只有一个,就没单列出来 - # for k, v in kwargs.items(): - # if isinstance(v, (np.generic, np.ndarray)): - # # no_grad_Var不需要转换类型 - # if self.no_grad_var is not None and k in self.no_grad_var: - # kwargs[k] = v - # else: - # kwargs[k] = v.astype(self.dtype) - # for k, v in params.items(): - # if isinstance(v, (np.generic, np.ndarray)): - # # no_grad_Var不需要转换类型 - # if self.no_grad_var is not None and k in self.no_grad_var: - # params[k] = paddle.static.data(name=k, shape=v.shape, dtype=v.dtype) - # else: - # params[k] = paddle.static.data(name=k, shape=v.shape, dtype=self.dtype) - # # enable compute gradient - # params[k].stop_gradient = False if data is not None: data = data.astype(self.dtype) self.data = paddle.static.data(name="data", shape=data.shape, dtype=self.dtype) @@ -73,11 +48,19 @@ def _static_forward(self, res, data=None, **kwargs): data = dict({"data": data}, **kwargs) static_cell = paddle.nn.SimpleRNNCell(4, 8) parameters = {} - for k, v in params["cell"].named_parameters(): + for k, v in kwargs["cell"].named_parameters(): parameters[k] = v - obj = self.func(static_cell) - output, h = obj(self.data) + obj = self.func( + static_cell, + is_reverse=self.kwargs.get("is_reverse", False), + time_major=self.kwargs.get("time_major", False), + ) + output, h = obj( + self.data, + initial_states=self.forward_kwargs.get("initial_states", None), + sequence_length=self.forward_kwargs.get("sequence_length", None), + ) if self.enable_backward: loss = paddle.mean(output) @@ -97,62 +80,308 @@ def _static_forward(self, res, data=None, **kwargs): res = exe.run(main_program, feed=data, fetch_list=[output, h], return_numpy=True) return res + def _dygraph_forward(self): + """ + _dygraph_forward + """ + cell = copy.deepcopy(self.kwargs.get("cell")) + obj = self.func( + cell=cell, is_reverse=self.kwargs.get("is_reverse", False), time_major=self.kwargs.get("time_major", False) + ) + res = obj( + self.data, + initial_states=self.forward_kwargs.get("initial_states", None), + sequence_length=self.forward_kwargs.get("sequence_length", None), + ) + return res + obj = TestRNN(paddle.nn.RNN) +res_outputs = [ + [ + [ + -0.41805826338051666, + 0.22022120468076872, + -0.338273189723404, + -0.24617478149067853, + 0.09573324424067524, + -0.47079110200581753, + -0.03435668862995602, + -0.40527382273501933, + ], + [ + -0.16655437807618048, + 0.2769589075445124, + -0.14167845285320205, + -0.47032005200197013, + 0.1028938747907291, + -0.2342414953727308, + 0.21773937893402742, + -0.33428361063120415, + ], + [ + -0.30962178642274835, + 0.14995033887532402, + -0.5014789422897543, + -0.5575169371542475, + 0.3508976438664536, + -0.14612803096924393, + 0.055716862608673354, + -0.23707090722122118, + ], + ], + [ + [ + -0.2910432922599807, + 0.20006867702342945, + -0.13309779241132294, + -0.35354838575117636, + -0.0382390069374574, + -0.46651599812797767, + -0.0548339255988763, + -0.3802476536920616, + ], + [ + -0.379896702128952, + 0.440035048064823, + -0.5127017093856834, + -0.2530757972388444, + 0.4495343467088128, + -0.3693239736103003, + -0.04066560667765106, + -0.3609407859877686, + ], + [ + -0.32279289579646764, + 0.07392642831034953, + -0.42972779798163085, + -0.6088280646556109, + 0.320803940263583, + -0.05130848141305536, + 0.3858006307601003, + -0.1703522265261066, + ], + ], +] +res_final_test = [ + [ + -0.30962178642274835, + 0.14995033887532402, + -0.5014789422897543, + -0.5575169371542475, + 0.3508976438664536, + -0.14612803096924393, + 0.055716862608673354, + -0.23707090722122118, + ], + [ + -0.32279289579646764, + 0.07392642831034953, + -0.42972779798163085, + -0.6088280646556109, + 0.320803940263583, + -0.05130848141305536, + 0.3858006307601003, + -0.1703522265261066, + ], +] +np_cell_params = { + "weight_ih": np.array( + [ + [-0.22951947, -0.08992132, -0.34953101, -0.175061], + [0.20906496, -0.3427665, 0.06989282, 0.07340089], + [-0.27920275, -0.08347859, -0.32776092, 0.27606266], + [0.3400624, -0.311168, 0.27615769, 0.05437757], + [0.17145903, 0.09205394, 0.05787117, -0.33910074], + [-0.20504217, 0.03159698, 0.19029316, -0.17628509], + [-0.15139461, 0.24918096, 0.33588031, 0.27213237], + [-0.09934296, 0.06990383, -0.10267501, -0.11300258], + ], + dtype=_dtype, + ), + "weight_hh": np.array( + [ + [-0.22763112, -0.1854782, -0.32183097, 0.0038406, -0.08750273, 0.06562333, 0.09188278, -0.25271974], + [0.30677212, 0.31563824, 0.07233466, -0.07936122, -0.09674069, -0.20905946, -0.15785094, -0.1792262], + [-0.230794, 0.32994288, 0.32315671, 0.06927786, 0.16355433, -0.11286469, -0.28846025, -0.0258108], + [0.00615105, -0.2910026, 0.0198239, 0.34800829, -0.0742208, -0.11625087, 0.21598615, 0.1798519], + [-0.13218199, 0.09477825, 0.02857035, -0.14368852, -0.27521451, -0.13248332, -0.03042035, 0.1123876], + [-0.17376618, 0.09977366, -0.21204463, 0.11145757, 0.19678019, 0.19770592, 0.07801379, -0.13505715], + [0.13981969, 0.25428854, 0.08861728, 0.34111385, 0.33693647, -0.23568284, -0.33716397, -0.23988983], + [0.29945748, 0.32070817, -0.20436912, -0.09862354, 0.03491358, -0.16133995, -0.02785886, 0.13870717], + ], + dtype=_dtype, + ), + "bias_ih": np.array( + [ + 2.51656952e-04, + 1.52785263e-01, + 1.83536185e-02, + -3.52564132e-01, + -7.44581413e-02, + -5.53878870e-03, + -6.86739763e-02, + -1.03026660e-01, + ], + dtype=_dtype, + ), + "bias_hh": np.array( + [0.00043439, -0.03876598, -0.28960775, -0.16011519, 0.31358566, -0.33478349, -0.32527005, -0.15334292], + dtype=_dtype, + ), +} -@pytest.mark.api_nn_RNN_parameters -def test_rnn(): +@pytest.mark.api_nn_RNN_vartype +def test_rnn_base(): """ base """ np.random.seed(obj.seed) - np_cell_params = { - "weight_ih": np.array( + inputs = randtool("float", 0, 1, (2, 3, 4)) + + # prev_h = randtool("float", 0, 1, (2, 8)) + cell = paddle.nn.SimpleRNNCell(4, 8) + # set the weights + for k, v in cell.named_parameters(): + v.set_value(np_cell_params[k]) + + res = [res_outputs, res_final_test] + obj.base(res, data=inputs, cell=cell) + + +@pytest.mark.api_nn_RNN_parameters +def test_rnn1(): + """ + default + """ + np.random.seed(obj.seed) + inputs = randtool("float", 0, 1, (2, 3, 4)) + + # prev_h = randtool("float", 0, 1, (2, 8)) + cell = paddle.nn.SimpleRNNCell(4, 8) + # set the weights + for k, v in cell.named_parameters(): + v.set_value(np_cell_params[k]) + + res = [res_outputs, res_final_test] + obj.run(res, data=inputs, cell=cell) + + +# +@pytest.mark.apt_nn_RNN_parameters +def test_rnn2(): + """ + set is_reverse=True + """ + np.random.seed(obj.seed) + inputs = randtool("float", 0, 1, (2, 3, 4)) + # prev_h = randtool("float", 0, 1, (2, 8)) + cell = paddle.nn.SimpleRNNCell(4, 8) + # set the weights + for k, v in cell.named_parameters(): + v.set_value(np_cell_params[k]) + + res_outputs = [ + [ + [ + -0.2589979759048145, + 0.3240111769424469, + -0.3930827679031371, + -0.4550450697483109, + 0.14750135709874887, + -0.4174765422774691, + -0.09569240111718305, + -0.3245812517782133, + ], [ - [-0.22951947, -0.08992132, -0.34953101, -0.175061], - [0.20906496, -0.3427665, 0.06989282, 0.07340089], - [-0.27920275, -0.08347859, -0.32776092, 0.27606266], - [0.3400624, -0.311168, 0.27615769, 0.05437757], - [0.17145903, 0.09205394, 0.05787117, -0.33910074], - [-0.20504217, 0.03159698, 0.19029316, -0.17628509], - [-0.15139461, 0.24918096, 0.33588031, 0.27213237], - [-0.09934296, 0.06990383, -0.10267501, -0.11300258], + -0.08013370039343207, + 0.1170042730339912, + -0.30515483626503426, + -0.46595279283909635, + 0.012902156875043688, + -0.1443459732860601, + 0.060135292018463474, + -0.36570477654937, ], - dtype=_dtype, - ), - "weight_hh": np.array( [ - [-0.22763112, -0.1854782, -0.32183097, 0.0038406, -0.08750273, 0.06562333, 0.09188278, -0.25271974], - [0.30677212, 0.31563824, 0.07233466, -0.07936122, -0.09674069, -0.20905946, -0.15785094, -0.1792262], - [-0.230794, 0.32994288, 0.32315671, 0.06927786, 0.16355433, -0.11286469, -0.28846025, -0.0258108], - [0.00615105, -0.2910026, 0.0198239, 0.34800829, -0.0742208, -0.11625087, 0.21598615, 0.1798519], - [-0.13218199, 0.09477825, 0.02857035, -0.14368852, -0.27521451, -0.13248332, -0.03042035, 0.1123876], - [-0.17376618, 0.09977366, -0.21204463, 0.11145757, 0.19678019, 0.19770592, 0.07801379, -0.13505715], - [0.13981969, 0.25428854, 0.08861728, 0.34111385, 0.33693647, -0.23568284, -0.33716397, -0.23988983], - [0.29945748, 0.32070817, -0.20436912, -0.09862354, 0.03491358, -0.16133995, -0.02785886, 0.13870717], + -0.40579899720523244, + 0.02312562316595211, + -0.5312022907527016, + -0.3693065415065509, + 0.28778352515924405, + -0.21408476966018827, + 0.08476590823896107, + -0.331850261752073, + ], + ], + [ + [ + -0.04323715734876021, + 0.2758733339839664, + -0.11169581261110856, + -0.5577216056359302, + -0.09549122438928458, + -0.26012886317201583, + 0.15354536934902546, + -0.24630044505196771, ], - dtype=_dtype, - ), - "bias_ih": np.array( [ - 2.51656952e-04, - 1.52785263e-01, - 1.83536185e-02, - -3.52564132e-01, - -7.44581413e-02, - -5.53878870e-03, - -6.86739763e-02, - -1.03026660e-01, + -0.1823227115460985, + 0.19892074311953015, + -0.6418025254265327, + -0.20776887808392297, + 0.34250159108287603, + -0.17313980368980414, + -0.1723277295300889, + -0.4085417079960605, ], - dtype=_dtype, - ), - "bias_hh": np.array( - [0.00043439, -0.03876598, -0.28960775, -0.16011519, 0.31358566, -0.33478349, -0.32527005, -0.15334292], - dtype=_dtype, - ), - } + [ + -0.48351457743645987, + -0.03610278540308465, + -0.5686091525998158, + -0.3921202383856199, + 0.32123451854395846, + -0.29386941479433254, + 0.1401073686084718, + -0.34122503415683153, + ], + ], + ] + + res_final_test = [ + [ + -0.2589979759048145, + 0.3240111769424469, + -0.3930827679031371, + -0.4550450697483109, + 0.14750135709874887, + -0.4174765422774691, + -0.09569240111718305, + -0.3245812517782133, + ], + [ + -0.04323715734876021, + 0.2758733339839664, + -0.11169581261110856, + -0.5577216056359302, + -0.09549122438928458, + -0.26012886317201583, + 0.15354536934902546, + -0.24630044505196771, + ], + ] + res = [res_outputs, res_final_test] + obj.run(res, data=inputs, cell=cell, is_reverse=True) + + +@pytest.mark.apt_nn_RNN_parameters +def test_rnn3(): + """ + set time_major = True + """ + np.random.seed(obj.seed) inputs = randtool("float", 0, 1, (2, 3, 4)) # prev_h = randtool("float", 0, 1, (2, 8)) @@ -160,7 +389,6 @@ def test_rnn(): # set the weights for k, v in cell.named_parameters(): v.set_value(np_cell_params[k]) - res_outputs = [ [ [ @@ -174,82 +402,92 @@ def test_rnn(): -0.40527382273501933, ], [ - -0.16655437807618048, - 0.2769589075445124, - -0.14167845285320205, - -0.47032005200197013, - 0.1028938747907291, - -0.2342414953727308, - 0.21773937893402742, - -0.33428361063120415, + -0.3716626927858596, + 0.178889158651149, + -0.26787978607034324, + -0.30838768287984186, + 0.00989695818447031, + -0.3411515688379077, + 0.08545800935661334, + -0.38892505034367336, ], [ - -0.30962178642274835, - 0.14995033887532402, - -0.5014789422897543, - -0.5575169371542475, - 0.3508976438664536, - -0.14612803096924393, - 0.055716862608673354, - -0.23707090722122118, + -0.40579899720523244, + 0.02312562316595211, + -0.5312022907527016, + -0.3693065415065509, + 0.28778352515924405, + -0.21408476966018827, + 0.08476590823896107, + -0.331850261752073, ], ], [ [ - -0.2910432922599807, - 0.20006867702342945, - -0.13309779241132294, - -0.35354838575117636, - -0.0382390069374574, - -0.46651599812797767, - -0.0548339255988763, - -0.3802476536920616, + -0.07732081532070446, + 0.29711704205119077, + -0.0019518920644305531, + -0.508875989551415, + 0.055048667001486175, + -0.3703987511750884, + 0.08055090371865832, + -0.325207265349378, ], [ - -0.379896702128952, - 0.440035048064823, - -0.5127017093856834, - -0.2530757972388444, - 0.4495343467088128, - -0.3693239736103003, - -0.04066560667765106, - -0.3609407859877686, + -0.30498842239321955, + 0.35928847744894904, + -0.5660679969868672, + -0.22544992794847946, + 0.4196988258521016, + -0.2874247771733212, + -0.11196088638313648, + -0.3880208965645975, ], [ - -0.32279289579646764, - 0.07392642831034953, - -0.42972779798163085, - -0.6088280646556109, - 0.320803940263583, - -0.05130848141305536, - 0.3858006307601003, - -0.1703522265261066, + -0.2141493311252869, + -0.09908455318304277, + -0.5954582803292919, + -0.537354492372074, + 0.3239273501904272, + -0.09249704773496159, + 0.11506418946857457, + -0.3170834149460797, ], ], ] res_final_test = [ [ - -0.30962178642274835, - 0.14995033887532402, - -0.5014789422897543, - -0.5575169371542475, - 0.3508976438664536, - -0.14612803096924393, - 0.055716862608673354, - -0.23707090722122118, + -0.07732081532070446, + 0.29711704205119077, + -0.0019518920644305531, + -0.508875989551415, + 0.055048667001486175, + -0.3703987511750884, + 0.08055090371865832, + -0.325207265349378, ], [ - -0.32279289579646764, - 0.07392642831034953, - -0.42972779798163085, - -0.6088280646556109, - 0.320803940263583, - -0.05130848141305536, - 0.3858006307601003, - -0.1703522265261066, + -0.30498842239321955, + 0.35928847744894904, + -0.5660679969868672, + -0.22544992794847946, + 0.4196988258521016, + -0.2874247771733212, + -0.11196088638313648, + -0.3880208965645975, + ], + [ + -0.2141493311252869, + -0.09908455318304277, + -0.5954582803292919, + -0.537354492372074, + 0.3239273501904272, + -0.09249704773496159, + 0.11506418946857457, + -0.3170834149460797, ], ] res = [res_outputs, res_final_test] - obj.base(res, data=inputs, cell=cell) + obj.run(res, data=inputs, cell=cell, time_major=True) From 631ab79581978c86891e3fe2975740a5d8aaec97 Mon Sep 17 00:00:00 2001 From: spade Date: Fri, 15 Oct 2021 15:36:51 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0rnn=5Fnumpy,=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=87=AApaddlepaddle=E3=80=82=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0float64=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/rnn_numpy.py | 565 +++++++++++++++++++++++++++ framework/api/nn/test_rnn.py | 423 +++----------------- framework/api/nn/test_rnn_float64.py | 187 +++++++++ 3 files changed, 811 insertions(+), 364 deletions(-) create mode 100644 framework/api/nn/rnn_numpy.py create mode 100644 framework/api/nn/test_rnn_float64.py diff --git a/framework/api/nn/rnn_numpy.py b/framework/api/nn/rnn_numpy.py new file mode 100644 index 0000000000..b0fe266ad4 --- /dev/null +++ b/framework/api/nn/rnn_numpy.py @@ -0,0 +1,565 @@ +""" +numpy 实现的各种rnn +""" +import math +import numpy as np + + +class LayerMixin(object): + """ + base class of layerMixin + """ + + def __call__(self, *args, **kwargs): + return self.forward(*args, **kwargs) + + +class LayerListMixin(LayerMixin): + """ + base class + """ + + def __init__(self, layers=None): + self._layers = list(layers) if layers else [] + + def append(self, layer): + """ + append + """ + self._layers.append(layer) + + def __iter__(self): + return iter(self._layers) + + +class SimpleRNNCell(LayerMixin): + """ + Simple RNN Cell + """ + + def __init__(self, input_size, hidden_size, bias=True, nonlinearity="RNN_TANH", dtype="float64"): + self.input_size = input_size + self.hidden_size = hidden_size + self.bias = bias + if nonlinearity == "RNN_TANH": + self.nonlinearity = np.tanh + else: + self.nonlinearity = lambda x: np.maximum(x, 0.0) + + self.parameters = dict() + std = 1.0 / math.sqrt(hidden_size) + self.weight_ih = np.random.uniform(-std, std, (hidden_size, input_size)).astype(dtype) + self.weight_hh = np.random.uniform(-std, std, (hidden_size, hidden_size)).astype(dtype) + self.parameters["weight_ih"] = self.weight_ih + self.parameters["weight_hh"] = self.weight_hh + if bias: + self.bias_ih = np.random.uniform(-std, std, (hidden_size,)).astype(dtype) + self.bias_hh = np.random.uniform(-std, std, (hidden_size,)).astype(dtype) + self.parameters["bias_ih"] = self.bias_ih + self.parameters["bias_hh"] = self.bias_hh + else: + self.bias_ih = None + self.bias_hh = None + + def init_state(self, inputs, batch_dim_index=0): + """ + init state + """ + batch_size = inputs.shape[batch_dim_index] + return np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype) + + def forward(self, inputs, hx=None): + """ + forward + """ + if hx is None: + hx = self.init_state(inputs) + pre_h = hx + i2h = np.matmul(inputs, self.weight_ih.T) + if self.bias_ih is not None: + i2h += self.bias_ih + h2h = np.matmul(pre_h, self.weight_hh.T) + if self.bias_hh is not None: + h2h += self.bias_hh + h = self.nonlinearity(i2h + h2h) + return h, h + + +class GRUCell(LayerMixin): + """ + GRU Cell + """ + + def __init__(self, input_size, hidden_size, bias=True, dtype="float64"): + self.input_size = input_size + self.hidden_size = hidden_size + self.bias = bias + self.parameters = dict() + std = 1.0 / math.sqrt(hidden_size) + self.weight_ih = np.random.uniform(-std, std, (3 * hidden_size, input_size)).astype(dtype) + self.weight_hh = np.random.uniform(-std, std, (3 * hidden_size, hidden_size)).astype(dtype) + self.parameters["weight_ih"] = self.weight_ih + self.parameters["weight_hh"] = self.weight_hh + if bias: + self.bias_ih = np.random.uniform(-std, std, (3 * hidden_size)).astype(dtype) + self.bias_hh = np.random.uniform(-std, std, (3 * hidden_size)).astype(dtype) + self.parameters["bias_ih"] = self.bias_ih + self.parameters["bias_hh"] = self.bias_hh + else: + self.bias_ih = None + self.bias_hh = None + + def init_state(self, inputs, batch_dim_index=0): + """ + init state + """ + batch_size = inputs.shape[batch_dim_index] + return np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype) + + def forward(self, inputs, hx=None): + """ + forward + """ + if hx is None: + hx = self.init_state(inputs) + pre_hidden = hx + x_gates = np.matmul(inputs, self.weight_ih.T) + if self.bias_ih is not None: + x_gates = x_gates + self.bias_ih + h_gates = np.matmul(pre_hidden, self.weight_hh.T) + if self.bias_hh is not None: + h_gates = h_gates + self.bias_hh + x_r, x_z, x_c = np.split(x_gates, 3, 1) + h_r, h_z, h_c = np.split(h_gates, 3, 1) + + r = 1.0 / (1.0 + np.exp(-(x_r + h_r))) + z = 1.0 / (1.0 + np.exp(-(x_z + h_z))) + c = np.tanh(x_c + r * h_c) # apply reset gate after mm + h = (pre_hidden - c) * z + c + return h, h + + +class LSTMCell(LayerMixin): + """ + LSTM Cell + """ + + def __init__(self, input_size, hidden_size, bias=True, dtype="float64"): + self.input_size = input_size + self.hidden_size = hidden_size + self.bias = bias + self.parameters = dict() + std = 1.0 / math.sqrt(hidden_size) + self.weight_ih = np.random.uniform(-std, std, (4 * hidden_size, input_size)).astype(dtype) + self.weight_hh = np.random.uniform(-std, std, (4 * hidden_size, hidden_size)).astype(dtype) + self.parameters["weight_ih"] = self.weight_ih + self.parameters["weight_hh"] = self.weight_hh + if bias: + self.bias_ih = np.random.uniform(-std, std, (4 * hidden_size)).astype(dtype) + self.bias_hh = np.random.uniform(-std, std, (4 * hidden_size)).astype(dtype) + self.parameters["bias_ih"] = self.bias_ih + self.parameters["bias_hh"] = self.bias_hh + else: + self.bias_ih = None + self.bias_hh = None + + def init_state(self, inputs, batch_dim_index=0): + """ + init state + """ + batch_size = inputs.shape[batch_dim_index] + init_h = np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype) + init_c = np.zeros((batch_size, self.hidden_size), dtype=inputs.dtype) + return init_h, init_c + + def forward(self, inputs, hx=None): + """ + forward + """ + if hx is None: + hx = self.init_state(inputs) + pre_hidden, pre_cell = hx + gates = np.matmul(inputs, self.weight_ih.T) + if self.bias_ih is not None: + gates = gates + self.bias_ih + gates += np.matmul(pre_hidden, self.weight_hh.T) + if self.bias_hh is not None: + gates = gates + self.bias_hh + + chunked_gates = np.split(gates, 4, -1) + + i = 1.0 / (1.0 + np.exp(-chunked_gates[0])) + f = 1.0 / (1.0 + np.exp(-chunked_gates[1])) + o = 1.0 / (1.0 + np.exp(-chunked_gates[3])) + c = f * pre_cell + i * np.tanh(chunked_gates[2]) + h = o * np.tanh(c) + + return h, (h, c) + + +def sequence_mask(lengths, max_len=None): + """ + sequence mask + """ + if max_len is None: + max_len = np.max(lengths) + else: + assert max_len >= np.max(lengths) + return np.arange(max_len) < np.expand_dims(lengths, -1) + + +def update_state(mask, new, old): + """ + update + """ + if not isinstance(old, (tuple, list)): + return np.where(mask, new, old) + else: + return tuple(map(lambda x, y: np.where(mask, x, y), new, old)) + + +def rnn(cell, inputs, initial_states, sequence_length=None, time_major=False, is_reverse=False): + """ + run + """ + if not time_major: + inputs = np.transpose(inputs, [1, 0, 2]) + if is_reverse: + inputs = np.flip(inputs, 0) + + if initial_states is None: + initial_states = cell.init_state(inputs, 1) + + if sequence_length is None: + mask = None + else: + mask = np.transpose(sequence_mask(sequence_length), [1, 0]) + mask = np.expand_dims(mask, -1) + if is_reverse: + mask = np.flip(mask, 0) + + time_steps = inputs.shape[0] + state = initial_states + outputs = [] + for t in range(time_steps): + x_t = inputs[t] + if mask is not None: + m_t = mask[t] + y, new_state = cell(x_t, state) + y = np.where(m_t, y, 0.0) + outputs.append(y) + state = update_state(m_t, new_state, state) + else: + y, new_state = cell(x_t, state) + outputs.append(y) + state = new_state + + outputs = np.stack(outputs) + final_state = state + + if is_reverse: + outputs = np.flip(outputs, 0) + if not time_major: + outputs = np.transpose(outputs, [1, 0, 2]) + return outputs, final_state + + +def birnn(cell_fw, cell_bw, inputs, initial_states, sequence_length=None, time_major=False): + """ + fun birnn + """ + states_fw, states_bw = initial_states + outputs_fw, states_fw = rnn(cell_fw, inputs, states_fw, sequence_length, time_major=time_major) + + outputs_bw, states_bw = rnn(cell_bw, inputs, states_bw, sequence_length, time_major=time_major, is_reverse=True) + + outputs = np.concatenate((outputs_fw, outputs_bw), -1) + final_states = (states_fw, states_bw) + return outputs, final_states + + +def flatten(nested): + """ + fun flatten + """ + return list(_flatten(nested)) + + +def _flatten(nested): + """ + flatten + """ + for item in nested: + if isinstance(item, (list, tuple)): + for subitem in _flatten(item): + yield subitem + else: + yield item + + +def unstack(array, axis=0): + """ + unstack + """ + num = array.shape[axis] + sub_arrays = np.split(array, num, axis) + return [np.squeeze(sub_array, axis) for sub_array in sub_arrays] + + +def dropout(array, p=0.5): + """ + dropout + """ + if p == 0.0: + return array + mask = (np.random.uniform(size=array.shape) < (1 - p)).astype(array.dtype) + return array * (mask / (1 - p)) + + +def split_states(states, bidirectional=False, state_components=1): + """ + split states + """ + if state_components == 1: + states = unstack(states) + if not bidirectional: + return states + else: + return list(zip(states[::2], states[1::2])) + else: + assert len(states) == state_components + states = tuple([unstack(item) for item in states]) + if not bidirectional: + return list(zip(*states)) + else: + states = list(zip(*states)) + return list(zip(states[::2], states[1::2])) + + +def concat_states(states, bidirectional=False, state_components=1): + """ + concat states + """ + if state_components == 1: + return np.stack(flatten(states)) + else: + states = flatten(states) + componnets = [] + for i in range(state_components): + componnets.append(states[i::state_components]) + return [np.stack(item) for item in componnets] + + +class RNN(LayerMixin): + """ + RNN class + """ + + def __init__(self, cell, is_reverse=False, time_major=False): + super(RNN, self).__init__() + self.cell = cell + if not hasattr(self.cell, "call"): + # for non-dygraph mode, `rnn` api uses cell.call + self.cell.call = self.cell.forward + self.is_reverse = is_reverse + self.time_major = time_major + + def forward(self, inputs, initial_states=None, sequence_length=None): + """ + forward + """ + final_outputs, final_states = rnn( + self.cell, + inputs, + initial_states=initial_states, + sequence_length=sequence_length, + time_major=self.time_major, + is_reverse=self.is_reverse, + ) + return final_outputs, final_states + + +class BiRNN(LayerMixin): + """ + BiRNN class + """ + + def __init__(self, cell_fw, cell_bw, time_major=False): + super(BiRNN, self).__init__() + self.cell_fw = cell_fw + self.cell_bw = cell_bw + self.time_major = time_major + + def forward(self, inputs, initial_states=None, sequence_length=None, **kwargs): + """ + forward + """ + if isinstance(initial_states, (list, tuple)): + assert len(initial_states) == 2, "length of initial_states should be 2 when it is a list/tuple" + else: + initial_states = [initial_states, initial_states] + + outputs, final_states = birnn( + self.cell_fw, self.cell_bw, inputs, initial_states, sequence_length, self.time_major + ) + return outputs, final_states + + +class RNNMixin(LayerListMixin): + """ + RNNMixin class + """ + + def forward(self, inputs, initial_states=None, sequence_length=None): + """ + forward + """ + batch_index = 1 if self.time_major else 0 + batch_size = inputs.shape[batch_index] + dtype = inputs.dtype + if initial_states is None: + state_shape = (self.num_layers * self.num_directions, batch_size, self.hidden_size) + if self.state_components == 1: + initial_states = np.zeros(state_shape, dtype) + else: + initial_states = tuple([np.zeros(state_shape, dtype) for _ in range(self.state_components)]) + + states = split_states(initial_states, self.num_directions == 2, self.state_components) + final_states = [] + input_temp = inputs + for i, rnn_layer in enumerate(self): + if i > 0: + input_temp = dropout(inputs, self.dropout) + outputs, final_state = rnn_layer(input_temp, states[i], sequence_length) + final_states.append(final_state) + inputs = outputs + + final_states = concat_states(final_states, self.num_directions == 2, self.state_components) + return outputs, final_states + + +class SimpleRNN(RNNMixin): + """ + SimpleRNN class + """ + + def __init__( + self, + input_size, + hidden_size, + num_layers=1, + nonlinearity="RNN_TANH", + direction="forward", + dropout=0.0, + time_major=False, + dtype="float64", + ): + super(SimpleRNN, self).__init__() + bidirectional_list = ["bidirectional", "bidirect"] + if direction in ["forward"]: + is_reverse = False + cell = SimpleRNNCell(input_size, hidden_size, nonlinearity=nonlinearity, dtype=dtype) + self.append(RNN(cell, is_reverse, time_major)) + for i in range(1, num_layers): + cell = SimpleRNNCell(hidden_size, hidden_size, nonlinearity=nonlinearity, dtype=dtype) + self.append(RNN(cell, is_reverse, time_major)) + elif direction in bidirectional_list: + cell_fw = SimpleRNNCell(input_size, hidden_size, nonlinearity=nonlinearity, dtype=dtype) + cell_bw = SimpleRNNCell(input_size, hidden_size, nonlinearity=nonlinearity, dtype=dtype) + self.append(BiRNN(cell_fw, cell_bw, time_major)) + for i in range(1, num_layers): + cell_fw = SimpleRNNCell(2 * hidden_size, hidden_size, nonlinearity, dtype=dtype) + cell_bw = SimpleRNNCell(2 * hidden_size, hidden_size, nonlinearity, dtype=dtype) + self.append(BiRNN(cell_fw, cell_bw, time_major)) + else: + raise ValueError( + "direction should be forward, backward or bidirectional, " "received direction = {}".format(direction) + ) + + self.input_size = input_size + self.hidden_size = hidden_size + self.dropout = dropout + self.num_directions = 2 if direction in bidirectional_list else 1 + self.time_major = time_major + self.num_layers = num_layers + self.state_components = 1 + + +class LSTM(RNNMixin): + """ + LSTM calss + """ + + def __init__( + self, input_size, hidden_size, num_layers=1, direction="forward", dropout=0.0, time_major=False, dtype="float64" + ): + super(LSTM, self).__init__() + + bidirectional_list = ["bidirectional", "bidirect"] + if direction in ["forward"]: + is_reverse = False + cell = LSTMCell(input_size, hidden_size, dtype=dtype) + self.append(RNN(cell, is_reverse, time_major)) + for i in range(1, num_layers): + cell = LSTMCell(hidden_size, hidden_size, dtype=dtype) + self.append(RNN(cell, is_reverse, time_major)) + elif direction in bidirectional_list: + cell_fw = LSTMCell(input_size, hidden_size, dtype=dtype) + cell_bw = LSTMCell(input_size, hidden_size, dtype=dtype) + self.append(BiRNN(cell_fw, cell_bw, time_major)) + for i in range(1, num_layers): + cell_fw = LSTMCell(2 * hidden_size, hidden_size, dtype=dtype) + cell_bw = LSTMCell(2 * hidden_size, hidden_size, dtype=dtype) + self.append(BiRNN(cell_fw, cell_bw, time_major)) + else: + raise ValueError( + "direction should be forward, backward or bidirectional, " "received direction = {}".format(direction) + ) + + self.input_size = input_size + self.hidden_size = hidden_size + self.dropout = dropout + self.num_directions = 2 if direction in bidirectional_list else 1 + self.time_major = time_major + self.num_layers = num_layers + self.state_components = 2 + + +class GRU(RNNMixin): + """ + GRU class + """ + + def __init__( + self, input_size, hidden_size, num_layers=1, direction="forward", dropout=0.0, time_major=False, dtype="float64" + ): + super(GRU, self).__init__() + + bidirectional_list = ["bidirectional", "bidirect"] + if direction in ["forward"]: + is_reverse = False + cell = GRUCell(input_size, hidden_size, dtype=dtype) + self.append(RNN(cell, is_reverse, time_major)) + for i in range(1, num_layers): + cell = GRUCell(hidden_size, hidden_size, dtype=dtype) + self.append(RNN(cell, is_reverse, time_major)) + elif direction in bidirectional_list: + cell_fw = GRUCell(input_size, hidden_size, dtype=dtype) + cell_bw = GRUCell(input_size, hidden_size, dtype=dtype) + self.append(BiRNN(cell_fw, cell_bw, time_major)) + for i in range(1, num_layers): + cell_fw = GRUCell(2 * hidden_size, hidden_size, dtype=dtype) + cell_bw = GRUCell(2 * hidden_size, hidden_size, dtype=dtype) + self.append(BiRNN(cell_fw, cell_bw, time_major)) + else: + raise ValueError( + "direction should be forward, backward or bidirectional, " "received direction = {}".format(direction) + ) + + self.input_size = input_size + self.hidden_size = hidden_size + self.dropout = dropout + self.num_directions = 2 if direction in bidirectional_list else 1 + self.time_major = time_major + self.num_layers = num_layers + self.state_components = 1 diff --git a/framework/api/nn/test_rnn.py b/framework/api/nn/test_rnn.py index bf146fd153..268275ec37 100644 --- a/framework/api/nn/test_rnn.py +++ b/framework/api/nn/test_rnn.py @@ -2,16 +2,17 @@ # -*- coding: utf-8 -*- # encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python """ -paddle.nn.AdaptiveAvgPool3D +paddle.nn.RNN float32测试 """ import copy from apibase import APIBase -from apibase import randtool import paddle import pytest import numpy as np +from framework.api.nn.rnn_numpy import SimpleRNNCell, RNN + _dtype = np.float32 @@ -24,9 +25,8 @@ def hook(self): """ implement """ - self.types = [np.float32] + self.types = ["float32"] self.seed = 100 - # self.enable_backward=False self.delta = 0.0001 self.forward_kwargs = {} # 前向传播参数 @@ -56,11 +56,8 @@ def _static_forward(self, res, data=None, **kwargs): is_reverse=self.kwargs.get("is_reverse", False), time_major=self.kwargs.get("time_major", False), ) - output, h = obj( - self.data, - initial_states=self.forward_kwargs.get("initial_states", None), - sequence_length=self.forward_kwargs.get("sequence_length", None), - ) + + output, h = obj(self.data) if self.enable_backward: loss = paddle.mean(output) @@ -97,174 +94,54 @@ def _dygraph_forward(self): obj = TestRNN(paddle.nn.RNN) -res_outputs = [ - [ - [ - -0.41805826338051666, - 0.22022120468076872, - -0.338273189723404, - -0.24617478149067853, - 0.09573324424067524, - -0.47079110200581753, - -0.03435668862995602, - -0.40527382273501933, - ], - [ - -0.16655437807618048, - 0.2769589075445124, - -0.14167845285320205, - -0.47032005200197013, - 0.1028938747907291, - -0.2342414953727308, - 0.21773937893402742, - -0.33428361063120415, - ], - [ - -0.30962178642274835, - 0.14995033887532402, - -0.5014789422897543, - -0.5575169371542475, - 0.3508976438664536, - -0.14612803096924393, - 0.055716862608673354, - -0.23707090722122118, - ], - ], - [ - [ - -0.2910432922599807, - 0.20006867702342945, - -0.13309779241132294, - -0.35354838575117636, - -0.0382390069374574, - -0.46651599812797767, - -0.0548339255988763, - -0.3802476536920616, - ], - [ - -0.379896702128952, - 0.440035048064823, - -0.5127017093856834, - -0.2530757972388444, - 0.4495343467088128, - -0.3693239736103003, - -0.04066560667765106, - -0.3609407859877686, - ], - [ - -0.32279289579646764, - 0.07392642831034953, - -0.42972779798163085, - -0.6088280646556109, - 0.320803940263583, - -0.05130848141305536, - 0.3858006307601003, - -0.1703522265261066, - ], - ], -] -res_final_test = [ - [ - -0.30962178642274835, - 0.14995033887532402, - -0.5014789422897543, - -0.5575169371542475, - 0.3508976438664536, - -0.14612803096924393, - 0.055716862608673354, - -0.23707090722122118, - ], - [ - -0.32279289579646764, - 0.07392642831034953, - -0.42972779798163085, - -0.6088280646556109, - 0.320803940263583, - -0.05130848141305536, - 0.3858006307601003, - -0.1703522265261066, - ], -] -np_cell_params = { - "weight_ih": np.array( - [ - [-0.22951947, -0.08992132, -0.34953101, -0.175061], - [0.20906496, -0.3427665, 0.06989282, 0.07340089], - [-0.27920275, -0.08347859, -0.32776092, 0.27606266], - [0.3400624, -0.311168, 0.27615769, 0.05437757], - [0.17145903, 0.09205394, 0.05787117, -0.33910074], - [-0.20504217, 0.03159698, 0.19029316, -0.17628509], - [-0.15139461, 0.24918096, 0.33588031, 0.27213237], - [-0.09934296, 0.06990383, -0.10267501, -0.11300258], - ], - dtype=_dtype, - ), - "weight_hh": np.array( - [ - [-0.22763112, -0.1854782, -0.32183097, 0.0038406, -0.08750273, 0.06562333, 0.09188278, -0.25271974], - [0.30677212, 0.31563824, 0.07233466, -0.07936122, -0.09674069, -0.20905946, -0.15785094, -0.1792262], - [-0.230794, 0.32994288, 0.32315671, 0.06927786, 0.16355433, -0.11286469, -0.28846025, -0.0258108], - [0.00615105, -0.2910026, 0.0198239, 0.34800829, -0.0742208, -0.11625087, 0.21598615, 0.1798519], - [-0.13218199, 0.09477825, 0.02857035, -0.14368852, -0.27521451, -0.13248332, -0.03042035, 0.1123876], - [-0.17376618, 0.09977366, -0.21204463, 0.11145757, 0.19678019, 0.19770592, 0.07801379, -0.13505715], - [0.13981969, 0.25428854, 0.08861728, 0.34111385, 0.33693647, -0.23568284, -0.33716397, -0.23988983], - [0.29945748, 0.32070817, -0.20436912, -0.09862354, 0.03491358, -0.16133995, -0.02785886, 0.13870717], - ], - dtype=_dtype, - ), - "bias_ih": np.array( - [ - 2.51656952e-04, - 1.52785263e-01, - 1.83536185e-02, - -3.52564132e-01, - -7.44581413e-02, - -5.53878870e-03, - -6.86739763e-02, - -1.03026660e-01, - ], - dtype=_dtype, - ), - "bias_hh": np.array( - [0.00043439, -0.03876598, -0.28960775, -0.16011519, 0.31358566, -0.33478349, -0.32527005, -0.15334292], - dtype=_dtype, - ), -} + + +def copy_cell_params(np_cell, paddle_cell, dtype="float32"): + """ + 将np_cell的参数复制到paddle_cell中 + """ + paddle.disable_static() + state = np_cell.parameters + for k, v in paddle_cell.named_parameters(): + t = state[k].astype(dtype) + v.set_value(t) + return paddle_cell @pytest.mark.api_nn_RNN_vartype def test_rnn_base(): """ - base + base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 """ np.random.seed(obj.seed) - inputs = randtool("float", 0, 1, (2, 3, 4)) - - # prev_h = randtool("float", 0, 1, (2, 8)) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float32"), res_final_states.astype("float32")] + paddle.disable_static() cell = paddle.nn.SimpleRNNCell(4, 8) - # set the weights - for k, v in cell.named_parameters(): - v.set_value(np_cell_params[k]) - - res = [res_outputs, res_final_test] + cell = copy_cell_params(np_cell, cell) obj.base(res, data=inputs, cell=cell) @pytest.mark.api_nn_RNN_parameters def test_rnn1(): """ - default + 测试默认参数 """ + # numpy np.random.seed(obj.seed) - inputs = randtool("float", 0, 1, (2, 3, 4)) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float32"), res_final_states.astype("float32")] - # prev_h = randtool("float", 0, 1, (2, 8)) - cell = paddle.nn.SimpleRNNCell(4, 8) - # set the weights - for k, v in cell.named_parameters(): - v.set_value(np_cell_params[k]) + paddle.disable_static() - res = [res_outputs, res_final_test] + cell = paddle.nn.SimpleRNNCell(4, 8) + cell = copy_cell_params(np_cell, cell) obj.run(res, data=inputs, cell=cell) @@ -272,222 +149,40 @@ def test_rnn1(): @pytest.mark.apt_nn_RNN_parameters def test_rnn2(): """ - set is_reverse=True + 测试 is_reverse=True """ + + # numpy np.random.seed(obj.seed) - inputs = randtool("float", 0, 1, (2, 3, 4)) - # prev_h = randtool("float", 0, 1, (2, 8)) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell, is_reverse=True) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float32"), res_final_states.astype("float32")] + + paddle.disable_static() + cell = paddle.nn.SimpleRNNCell(4, 8) - # set the weights - for k, v in cell.named_parameters(): - v.set_value(np_cell_params[k]) - - res_outputs = [ - [ - [ - -0.2589979759048145, - 0.3240111769424469, - -0.3930827679031371, - -0.4550450697483109, - 0.14750135709874887, - -0.4174765422774691, - -0.09569240111718305, - -0.3245812517782133, - ], - [ - -0.08013370039343207, - 0.1170042730339912, - -0.30515483626503426, - -0.46595279283909635, - 0.012902156875043688, - -0.1443459732860601, - 0.060135292018463474, - -0.36570477654937, - ], - [ - -0.40579899720523244, - 0.02312562316595211, - -0.5312022907527016, - -0.3693065415065509, - 0.28778352515924405, - -0.21408476966018827, - 0.08476590823896107, - -0.331850261752073, - ], - ], - [ - [ - -0.04323715734876021, - 0.2758733339839664, - -0.11169581261110856, - -0.5577216056359302, - -0.09549122438928458, - -0.26012886317201583, - 0.15354536934902546, - -0.24630044505196771, - ], - [ - -0.1823227115460985, - 0.19892074311953015, - -0.6418025254265327, - -0.20776887808392297, - 0.34250159108287603, - -0.17313980368980414, - -0.1723277295300889, - -0.4085417079960605, - ], - [ - -0.48351457743645987, - -0.03610278540308465, - -0.5686091525998158, - -0.3921202383856199, - 0.32123451854395846, - -0.29386941479433254, - 0.1401073686084718, - -0.34122503415683153, - ], - ], - ] - - res_final_test = [ - [ - -0.2589979759048145, - 0.3240111769424469, - -0.3930827679031371, - -0.4550450697483109, - 0.14750135709874887, - -0.4174765422774691, - -0.09569240111718305, - -0.3245812517782133, - ], - [ - -0.04323715734876021, - 0.2758733339839664, - -0.11169581261110856, - -0.5577216056359302, - -0.09549122438928458, - -0.26012886317201583, - 0.15354536934902546, - -0.24630044505196771, - ], - ] - - res = [res_outputs, res_final_test] + cell = copy_cell_params(np_cell, cell) obj.run(res, data=inputs, cell=cell, is_reverse=True) @pytest.mark.apt_nn_RNN_parameters def test_rnn3(): """ - set time_major = True + 测试 time_major = True """ + # numpy np.random.seed(obj.seed) - inputs = randtool("float", 0, 1, (2, 3, 4)) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell, time_major=True) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float32"), res_final_states.astype("float32")] + + paddle.disable_static() - # prev_h = randtool("float", 0, 1, (2, 8)) cell = paddle.nn.SimpleRNNCell(4, 8) - # set the weights - for k, v in cell.named_parameters(): - v.set_value(np_cell_params[k]) - res_outputs = [ - [ - [ - -0.41805826338051666, - 0.22022120468076872, - -0.338273189723404, - -0.24617478149067853, - 0.09573324424067524, - -0.47079110200581753, - -0.03435668862995602, - -0.40527382273501933, - ], - [ - -0.3716626927858596, - 0.178889158651149, - -0.26787978607034324, - -0.30838768287984186, - 0.00989695818447031, - -0.3411515688379077, - 0.08545800935661334, - -0.38892505034367336, - ], - [ - -0.40579899720523244, - 0.02312562316595211, - -0.5312022907527016, - -0.3693065415065509, - 0.28778352515924405, - -0.21408476966018827, - 0.08476590823896107, - -0.331850261752073, - ], - ], - [ - [ - -0.07732081532070446, - 0.29711704205119077, - -0.0019518920644305531, - -0.508875989551415, - 0.055048667001486175, - -0.3703987511750884, - 0.08055090371865832, - -0.325207265349378, - ], - [ - -0.30498842239321955, - 0.35928847744894904, - -0.5660679969868672, - -0.22544992794847946, - 0.4196988258521016, - -0.2874247771733212, - -0.11196088638313648, - -0.3880208965645975, - ], - [ - -0.2141493311252869, - -0.09908455318304277, - -0.5954582803292919, - -0.537354492372074, - 0.3239273501904272, - -0.09249704773496159, - 0.11506418946857457, - -0.3170834149460797, - ], - ], - ] - - res_final_test = [ - [ - -0.07732081532070446, - 0.29711704205119077, - -0.0019518920644305531, - -0.508875989551415, - 0.055048667001486175, - -0.3703987511750884, - 0.08055090371865832, - -0.325207265349378, - ], - [ - -0.30498842239321955, - 0.35928847744894904, - -0.5660679969868672, - -0.22544992794847946, - 0.4196988258521016, - -0.2874247771733212, - -0.11196088638313648, - -0.3880208965645975, - ], - [ - -0.2141493311252869, - -0.09908455318304277, - -0.5954582803292919, - -0.537354492372074, - 0.3239273501904272, - -0.09249704773496159, - 0.11506418946857457, - -0.3170834149460797, - ], - ] - - res = [res_outputs, res_final_test] + cell = copy_cell_params(np_cell, cell) + obj.run(res, data=inputs, cell=cell, time_major=True) diff --git a/framework/api/nn/test_rnn_float64.py b/framework/api/nn/test_rnn_float64.py new file mode 100644 index 0000000000..c57e208d0e --- /dev/null +++ b/framework/api/nn/test_rnn_float64.py @@ -0,0 +1,187 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python +""" +paddle.nn.RNN float64测试 +""" +import copy + +from apibase import APIBase +import paddle +import pytest +import numpy as np + +from framework.api.nn.rnn_numpy import SimpleRNNCell, RNN + + +class TestRNN(APIBase): + """ + test + """ + + def hook(self): + """ + implement + """ + self.types = ["float64"] + self.seed = 100 + self.delta = 0.0001 + self.forward_kwargs = {} # 前向传播参数 + paddle.set_default_dtype("float64") + + def _static_forward(self, res, data=None, **kwargs): + """ + _static_forward + """ + + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + main_program.random_seed = self.seed + + with paddle.utils.unique_name.guard(): + with paddle.static.program_guard(main_program=main_program, startup_program=startup_program): + if data is not None: + data = data.astype(self.dtype) + self.data = paddle.static.data(name="data", shape=data.shape, dtype=self.dtype) + self.data.stop_gradient = False + data = dict({"data": data}, **kwargs) + static_cell = paddle.nn.SimpleRNNCell(4, 8) + parameters = {} + for k, v in kwargs["cell"].named_parameters(): + parameters[k] = v + + obj = self.func( + static_cell, + is_reverse=self.kwargs.get("is_reverse", False), + time_major=self.kwargs.get("time_major", False), + ) + + output, h = obj(self.data) + + if self.enable_backward: + loss = paddle.mean(output) + g = paddle.static.gradients(loss, self.data) + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + for k, v in static_cell.named_parameters(): + v.set_value(parameters[k]) + res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) + grad = {"data": res[2]} + return res[0:2], grad + else: + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + for k, v in static_cell.named_parameters(): + v.set_value(parameters[k]) + res = exe.run(main_program, feed=data, fetch_list=[output, h], return_numpy=True) + return res + + def _dygraph_forward(self): + """ + _dygraph_forward + """ + cell = copy.deepcopy(self.kwargs.get("cell")) + obj = self.func( + cell=cell, is_reverse=self.kwargs.get("is_reverse", False), time_major=self.kwargs.get("time_major", False) + ) + res = obj( + self.data, + initial_states=self.forward_kwargs.get("initial_states", None), + sequence_length=self.forward_kwargs.get("sequence_length", None), + ) + return res + + +obj = TestRNN(paddle.nn.RNN) + + +def copy_cell_params(np_cell, paddle_cell, dtype="float64"): + """ + 将np_cell的参数复制到paddle_cell中 + """ + paddle.disable_static() + state = np_cell.parameters + for k, v in paddle_cell.named_parameters(): + t = state[k].astype(dtype) + v.set_value(t) + return paddle_cell + + +@pytest.mark.api_nn_RNN_vartype +def test_rnn_base(): + """ + base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 + """ + np.random.seed(obj.seed) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + paddle.disable_static() + cell = paddle.nn.SimpleRNNCell(4, 8) + cell = copy_cell_params(np_cell, cell) + obj.base(res, data=inputs, cell=cell) + + +@pytest.mark.api_nn_RNN_parameters +def test_rnn1(): + """ + 测试默认参数 + """ + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + + paddle.disable_static() + + cell = paddle.nn.SimpleRNNCell(4, 8) + cell = copy_cell_params(np_cell, cell) + obj.run(res, data=inputs, cell=cell) + + +# +@pytest.mark.apt_nn_RNN_parameters +def test_rnn2(): + """ + 测试 is_reverse=True + """ + + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell, is_reverse=True) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + + paddle.disable_static() + + cell = paddle.nn.SimpleRNNCell(4, 8) + cell = copy_cell_params(np_cell, cell) + obj.run(res, data=inputs, cell=cell, is_reverse=True) + + +@pytest.mark.apt_nn_RNN_parameters +def test_rnn3(): + """ + 测试 time_major = True + """ + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell, time_major=True) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + + paddle.disable_static() + + cell = paddle.nn.SimpleRNNCell(4, 8) + cell = copy_cell_params(np_cell, cell) + + obj.run(res, data=inputs, cell=cell, time_major=True) From a1ae97c1a73f421e8012bc206bd2e2230c388705 Mon Sep 17 00:00:00 2001 From: spade Date: Sun, 17 Oct 2021 15:01:06 +0800 Subject: [PATCH 06/12] fixed --- framework/api/nn/test_rnn.py | 2 +- framework/api/nn/test_rnn_float64.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/api/nn/test_rnn.py b/framework/api/nn/test_rnn.py index 268275ec37..86a39f6982 100644 --- a/framework/api/nn/test_rnn.py +++ b/framework/api/nn/test_rnn.py @@ -11,7 +11,7 @@ import pytest import numpy as np -from framework.api.nn.rnn_numpy import SimpleRNNCell, RNN +from rnn_numpy import SimpleRNNCell, RNN _dtype = np.float32 diff --git a/framework/api/nn/test_rnn_float64.py b/framework/api/nn/test_rnn_float64.py index c57e208d0e..d34a2a57c9 100644 --- a/framework/api/nn/test_rnn_float64.py +++ b/framework/api/nn/test_rnn_float64.py @@ -11,7 +11,7 @@ import pytest import numpy as np -from framework.api.nn.rnn_numpy import SimpleRNNCell, RNN +from rnn_numpy import SimpleRNNCell, RNN class TestRNN(APIBase): From 59c61fb17bcde63317b8ad72e64731a6fc23d5e2 Mon Sep 17 00:00:00 2001 From: spade Date: Wed, 27 Oct 2021 15:20:29 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0shape=E7=9A=84=E8=8C=83?= =?UTF-8?q?=E5=9B=B4,=20=E5=90=88=E5=B9=B6float64=E5=92=8Cfloat32=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/test_rnn.py | 138 +++++++++++++++++--- framework/api/nn/test_rnn_float64.py | 187 --------------------------- 2 files changed, 123 insertions(+), 202 deletions(-) delete mode 100644 framework/api/nn/test_rnn_float64.py diff --git a/framework/api/nn/test_rnn.py b/framework/api/nn/test_rnn.py index 86a39f6982..2ca846f7e1 100644 --- a/framework/api/nn/test_rnn.py +++ b/framework/api/nn/test_rnn.py @@ -13,12 +13,10 @@ from rnn_numpy import SimpleRNNCell, RNN -_dtype = np.float32 - class TestRNN(APIBase): """ - test + test RNN float32 """ def hook(self): @@ -29,6 +27,7 @@ def hook(self): self.seed = 100 self.delta = 0.0001 self.forward_kwargs = {} # 前向传播参数 + paddle.set_default_dtype("float32") def _static_forward(self, res, data=None, **kwargs): """ @@ -39,6 +38,8 @@ def _static_forward(self, res, data=None, **kwargs): startup_program = paddle.static.Program() main_program.random_seed = self.seed + cell = kwargs["cell"] + with paddle.utils.unique_name.guard(): with paddle.static.program_guard(main_program=main_program, startup_program=startup_program): if data is not None: @@ -46,7 +47,8 @@ def _static_forward(self, res, data=None, **kwargs): self.data = paddle.static.data(name="data", shape=data.shape, dtype=self.dtype) self.data.stop_gradient = False data = dict({"data": data}, **kwargs) - static_cell = paddle.nn.SimpleRNNCell(4, 8) + + static_cell = paddle.nn.SimpleRNNCell(cell.input_size, cell.hidden_size) parameters = {} for k, v in kwargs["cell"].named_parameters(): parameters[k] = v @@ -64,8 +66,10 @@ def _static_forward(self, res, data=None, **kwargs): g = paddle.static.gradients(loss, self.data) exe = paddle.static.Executor(self.place) exe.run(startup_program) + for k, v in static_cell.named_parameters(): v.set_value(parameters[k]) + res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) grad = {"data": res[2]} return res[0:2], grad @@ -113,6 +117,7 @@ def test_rnn_base(): """ base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 """ + paddle.set_default_dtype("float32") np.random.seed(obj.seed) inputs = np.random.random((2, 3, 4)) np_cell = SimpleRNNCell(4, 8) @@ -130,39 +135,39 @@ def test_rnn1(): """ 测试默认参数 """ + paddle.set_default_dtype("float32") # numpy np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) + inputs = np.random.random((10, 8, 16)) + np_cell = SimpleRNNCell(16, 32) rnn = RNN(cell=np_cell) res_outputs, res_final_states = rnn(inputs) res = [res_outputs.astype("float32"), res_final_states.astype("float32")] paddle.disable_static() - cell = paddle.nn.SimpleRNNCell(4, 8) + cell = paddle.nn.SimpleRNNCell(16, 32) cell = copy_cell_params(np_cell, cell) obj.run(res, data=inputs, cell=cell) -# @pytest.mark.apt_nn_RNN_parameters def test_rnn2(): """ 测试 is_reverse=True """ - + paddle.set_default_dtype("float32") # numpy np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) + inputs = np.random.random((20, 10, 32)) + np_cell = SimpleRNNCell(32, 16) rnn = RNN(cell=np_cell, is_reverse=True) res_outputs, res_final_states = rnn(inputs) res = [res_outputs.astype("float32"), res_final_states.astype("float32")] paddle.disable_static() - cell = paddle.nn.SimpleRNNCell(4, 8) + cell = paddle.nn.SimpleRNNCell(32, 16) cell = copy_cell_params(np_cell, cell) obj.run(res, data=inputs, cell=cell, is_reverse=True) @@ -172,17 +177,120 @@ def test_rnn3(): """ 测试 time_major = True """ + paddle.set_default_dtype("float32") # numpy np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) + inputs = np.random.random((10, 4, 128)) + np_cell = SimpleRNNCell(128, 32) rnn = RNN(cell=np_cell, time_major=True) res_outputs, res_final_states = rnn(inputs) res = [res_outputs.astype("float32"), res_final_states.astype("float32")] paddle.disable_static() - cell = paddle.nn.SimpleRNNCell(4, 8) + cell = paddle.nn.SimpleRNNCell(128, 32) cell = copy_cell_params(np_cell, cell) obj.run(res, data=inputs, cell=cell, time_major=True) + + +class TestRNN64(TestRNN): + """ + test RNN float64 + """ + + def hook(self): + """ + implement + """ + self.types = ["float64"] + self.seed = 100 + self.delta = 0.0001 + self.forward_kwargs = {} # 前向传播参数 + paddle.set_default_dtype("float64") + + +obj64 = TestRNN64(paddle.nn.RNN) + + +@pytest.mark.api_nn_RNN_vartype +def test_rnn_base_64(): + """ + float64 base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 + """ + paddle.set_default_dtype("float64") + np.random.seed(obj64.seed) + inputs = np.random.random((2, 3, 4)) + np_cell = SimpleRNNCell(4, 8) + rnn = RNN(cell=np_cell) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + paddle.disable_static() + cell = paddle.nn.SimpleRNNCell(4, 8) + cell = copy_cell_params(np_cell, cell, dtype="float64") + obj64.base(res, data=inputs, cell=cell) + + +@pytest.mark.api_nn_RNN_parameters +def test_rnn1_64(): + """ + 测试默认参数 + """ + paddle.set_default_dtype("float64") + # numpy + np.random.seed(obj64.seed) + inputs = np.random.random((10, 8, 16)) + np_cell = SimpleRNNCell(16, 32) + rnn = RNN(cell=np_cell) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + + paddle.disable_static() + + cell = paddle.nn.SimpleRNNCell(16, 32) + cell = copy_cell_params(np_cell, cell, dtype="float64") + obj64.run(res, data=inputs, cell=cell) + + +# +@pytest.mark.apt_nn_RNN_parameters +def test_rnn2_64(): + """ + 测试 is_reverse=True + """ + paddle.set_default_dtype("float64") + # numpy + np.random.seed(obj64.seed) + inputs = np.random.random((20, 10, 32)) + np_cell = SimpleRNNCell(32, 16) + rnn = RNN(cell=np_cell, is_reverse=True) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + + paddle.disable_static() + + cell = paddle.nn.SimpleRNNCell(32, 16) + cell = copy_cell_params(np_cell, cell, dtype="float64") + obj64.run(res, data=inputs, cell=cell, is_reverse=True) + + +@pytest.mark.apt_nn_RNN_parameters +def test_rnn3_64(): + """ + 测试 time_major = True + """ + paddle.set_default_dtype("float64") + # numpy + np.random.seed(obj64.seed) + inputs = np.random.random((10, 4, 128)) + np_cell = SimpleRNNCell(128, 32) + rnn = RNN(cell=np_cell, time_major=True) + res_outputs, res_final_states = rnn(inputs) + res = [res_outputs.astype("float64"), res_final_states.astype("float64")] + + paddle.disable_static() + + cell = paddle.nn.SimpleRNNCell(128, 32) + cell = copy_cell_params(np_cell, cell, dtype="float64") + + obj64.run(res, data=inputs, cell=cell, time_major=True) diff --git a/framework/api/nn/test_rnn_float64.py b/framework/api/nn/test_rnn_float64.py deleted file mode 100644 index d34a2a57c9..0000000000 --- a/framework/api/nn/test_rnn_float64.py +++ /dev/null @@ -1,187 +0,0 @@ -#!/bin/env python -# -*- coding: utf-8 -*- -# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python -""" -paddle.nn.RNN float64测试 -""" -import copy - -from apibase import APIBase -import paddle -import pytest -import numpy as np - -from rnn_numpy import SimpleRNNCell, RNN - - -class TestRNN(APIBase): - """ - test - """ - - def hook(self): - """ - implement - """ - self.types = ["float64"] - self.seed = 100 - self.delta = 0.0001 - self.forward_kwargs = {} # 前向传播参数 - paddle.set_default_dtype("float64") - - def _static_forward(self, res, data=None, **kwargs): - """ - _static_forward - """ - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - main_program.random_seed = self.seed - - with paddle.utils.unique_name.guard(): - with paddle.static.program_guard(main_program=main_program, startup_program=startup_program): - if data is not None: - data = data.astype(self.dtype) - self.data = paddle.static.data(name="data", shape=data.shape, dtype=self.dtype) - self.data.stop_gradient = False - data = dict({"data": data}, **kwargs) - static_cell = paddle.nn.SimpleRNNCell(4, 8) - parameters = {} - for k, v in kwargs["cell"].named_parameters(): - parameters[k] = v - - obj = self.func( - static_cell, - is_reverse=self.kwargs.get("is_reverse", False), - time_major=self.kwargs.get("time_major", False), - ) - - output, h = obj(self.data) - - if self.enable_backward: - loss = paddle.mean(output) - g = paddle.static.gradients(loss, self.data) - exe = paddle.static.Executor(self.place) - exe.run(startup_program) - for k, v in static_cell.named_parameters(): - v.set_value(parameters[k]) - res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) - grad = {"data": res[2]} - return res[0:2], grad - else: - exe = paddle.static.Executor(self.place) - exe.run(startup_program) - for k, v in static_cell.named_parameters(): - v.set_value(parameters[k]) - res = exe.run(main_program, feed=data, fetch_list=[output, h], return_numpy=True) - return res - - def _dygraph_forward(self): - """ - _dygraph_forward - """ - cell = copy.deepcopy(self.kwargs.get("cell")) - obj = self.func( - cell=cell, is_reverse=self.kwargs.get("is_reverse", False), time_major=self.kwargs.get("time_major", False) - ) - res = obj( - self.data, - initial_states=self.forward_kwargs.get("initial_states", None), - sequence_length=self.forward_kwargs.get("sequence_length", None), - ) - return res - - -obj = TestRNN(paddle.nn.RNN) - - -def copy_cell_params(np_cell, paddle_cell, dtype="float64"): - """ - 将np_cell的参数复制到paddle_cell中 - """ - paddle.disable_static() - state = np_cell.parameters - for k, v in paddle_cell.named_parameters(): - t = state[k].astype(dtype) - v.set_value(t) - return paddle_cell - - -@pytest.mark.api_nn_RNN_vartype -def test_rnn_base(): - """ - base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 - """ - np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) - rnn = RNN(cell=np_cell) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - paddle.disable_static() - cell = paddle.nn.SimpleRNNCell(4, 8) - cell = copy_cell_params(np_cell, cell) - obj.base(res, data=inputs, cell=cell) - - -@pytest.mark.api_nn_RNN_parameters -def test_rnn1(): - """ - 测试默认参数 - """ - # numpy - np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) - rnn = RNN(cell=np_cell) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(4, 8) - cell = copy_cell_params(np_cell, cell) - obj.run(res, data=inputs, cell=cell) - - -# -@pytest.mark.apt_nn_RNN_parameters -def test_rnn2(): - """ - 测试 is_reverse=True - """ - - # numpy - np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) - rnn = RNN(cell=np_cell, is_reverse=True) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(4, 8) - cell = copy_cell_params(np_cell, cell) - obj.run(res, data=inputs, cell=cell, is_reverse=True) - - -@pytest.mark.apt_nn_RNN_parameters -def test_rnn3(): - """ - 测试 time_major = True - """ - # numpy - np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) - rnn = RNN(cell=np_cell, time_major=True) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(4, 8) - cell = copy_cell_params(np_cell, cell) - - obj.run(res, data=inputs, cell=cell, time_major=True) From f0635f98a328d5219ecf7dd408278a8c55be3295 Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 28 Oct 2021 16:54:29 +0800 Subject: [PATCH 08/12] birnn test --- framework/api/nn/test_birnn.py | 311 +++++++++++++++++++++++++++++++++ framework/api/nn/test_rnn.py | 296 ------------------------------- 2 files changed, 311 insertions(+), 296 deletions(-) create mode 100644 framework/api/nn/test_birnn.py delete mode 100644 framework/api/nn/test_rnn.py diff --git a/framework/api/nn/test_birnn.py b/framework/api/nn/test_birnn.py new file mode 100644 index 0000000000..b755736b3c --- /dev/null +++ b/framework/api/nn/test_birnn.py @@ -0,0 +1,311 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python +""" +paddle.nn.RNN float32测试 +""" +import copy + +from apibase import APIBase +import paddle +import pytest +import numpy as np + +from rnn_numpy import SimpleRNNCell, BiRNN + + +class TestBiRNN(APIBase): + """ + test RNN float32 + """ + + def hook(self): + """ + implement + """ + self.types = ["float32"] + self.seed = 100 + self.delta = 0.0001 + self.forward_kwargs = {} # 前向传播参数 + paddle.set_default_dtype("float32") + + def _static_forward(self, res, data=None, **kwargs): + """ + _static_forward + """ + + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + main_program.random_seed = self.seed + + cell_fw = kwargs["cell_fw"] + cell_bw = kwargs["cell_bw"] + + with paddle.utils.unique_name.guard(): + with paddle.static.program_guard(main_program=main_program, startup_program=startup_program): + if data is not None: + data = data.astype(self.dtype) + self.data = paddle.static.data(name="data", shape=data.shape, dtype=self.dtype) + self.data.stop_gradient = False + data = dict({"data": data}, **kwargs) + + static_cell_fw = paddle.nn.SimpleRNNCell(cell_fw.input_size, cell_fw.hidden_size) + static_cell_bw = paddle.nn.SimpleRNNCell(cell_bw.input_size, cell_bw.hidden_size) + + parameters_fw = {} + for k, v in kwargs["cell_fw"].named_parameters(): + parameters_fw[k] = v + + parameters_bw = {} + for k, v in kwargs["cell_bw"].named_parameters(): + parameters_bw[k] = v + + obj = self.func( + cell_fw=static_cell_fw, cell_bw=static_cell_bw, time_major=self.kwargs.get("time_major", False) + ) + + output, h = obj(self.data) + + if self.enable_backward: + loss = paddle.mean(output) + g = paddle.static.gradients(loss, self.data) + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + + for k, v in static_cell_fw.named_parameters(): + v.set_value(parameters_fw[k]) + for k, v in static_cell_fw.named_parameters(): + v.set_value(parameters_bw[k]) + + res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) + grad = {"data": res[2]} + return res[0], grad + else: + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + + for k, v in static_cell_fw.named_parameters(): + v.set_value(parameters_fw[k]) + for k, v in static_cell_fw.named_parameters(): + v.set_value(parameters_bw[k]) + + res = exe.run(main_program, feed=data, fetch_list=[output, h], return_numpy=True) + return res[0] + + def _dygraph_forward(self): + """ + _dygraph_forward + """ + cell_fw = copy.deepcopy(self.kwargs.get("cell_fw")) + cell_bw = copy.deepcopy(self.kwargs.get("cell_bw")) + obj = self.func(cell_fw=cell_fw, cell_bw=cell_bw, time_major=self.kwargs.get("time_major", False)) + res = obj( + self.data, + initial_states=self.forward_kwargs.get("initial_states", None), + sequence_length=self.forward_kwargs.get("sequence_length", None), + ) + return res[0] + + +obj = TestBiRNN(paddle.nn.BiRNN) + + +def copy_cell_params(np_cell, paddle_cell, dtype="float32"): + """ + 将np_cell的参数复制到paddle_cell中 + """ + paddle.disable_static() + state = np_cell.parameters + for k, v in paddle_cell.named_parameters(): + t = state[k].astype(dtype) + v.set_value(t) + return paddle_cell + + +@pytest.mark.api_nn_RNN_vartype +def test_rnn_base(): + """ + base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 + """ + paddle.set_default_dtype("float32") + np.random.seed(obj.seed) + inputs = np.random.random((3, 3, 4)) + np_cell_fw = SimpleRNNCell(4, 8) + np_cell_bw = SimpleRNNCell(4, 8) + rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) + res_outputs = rnn(inputs)[0] + res = res_outputs.astype("float32") + paddle.disable_static() + cell_fw = paddle.nn.SimpleRNNCell(4, 8) + cell_bw = paddle.nn.SimpleRNNCell(4, 8) + cell_fw = copy_cell_params(np_cell_fw, cell_fw) + cell_bw = copy_cell_params(np_cell_bw, cell_bw) + + obj.base(res, data=inputs, cell_fw=cell_fw, cell_bw=cell_bw) + + +# +# @pytest.mark.api_nn_RNN_parameters +# def test_rnn1(): +# """ +# 测试默认参数 +# """ +# paddle.set_default_dtype("float32") +# # numpy +# np.random.seed(obj.seed) +# inputs = np.random.random((10, 8, 16)) +# np_cell = SimpleRNNCell(16, 32) +# rnn = BiRNN(cell=np_cell) +# res_outputs, res_final_states = rnn(inputs) +# res = [res_outputs.astype("float32"), res_final_states.astype("float32")] +# +# paddle.disable_static() +# +# cell = paddle.nn.SimpleRNNCell(16, 32) +# cell = copy_cell_params(np_cell, cell) +# obj.run(res, data=inputs, cell=cell) +# +# +# @pytest.mark.apt_nn_RNN_parameters +# def test_rnn2(): +# """ +# 测试 is_reverse=True +# """ +# paddle.set_default_dtype("float32") +# # numpy +# np.random.seed(obj.seed) +# inputs = np.random.random((20, 10, 32)) +# np_cell = SimpleRNNCell(32, 16) +# rnn = RNN(cell=np_cell, is_reverse=True) +# res_outputs, res_final_states = rnn(inputs) +# res = [res_outputs.astype("float32"), res_final_states.astype("float32")] +# +# paddle.disable_static() +# +# cell = paddle.nn.SimpleRNNCell(32, 16) +# cell = copy_cell_params(np_cell, cell) +# obj.run(res, data=inputs, cell=cell, is_reverse=True) +# +# +# @pytest.mark.apt_nn_RNN_parameters +# def test_rnn3(): +# """ +# 测试 time_major = True +# """ +# paddle.set_default_dtype("float32") +# # numpy +# np.random.seed(obj.seed) +# inputs = np.random.random((10, 4, 128)) +# np_cell = SimpleRNNCell(128, 32) +# rnn = RNN(cell=np_cell, time_major=True) +# res_outputs, res_final_states = rnn(inputs) +# res = [res_outputs.astype("float32"), res_final_states.astype("float32")] +# +# paddle.disable_static() +# +# cell = paddle.nn.SimpleRNNCell(128, 32) +# cell = copy_cell_params(np_cell, cell) +# +# obj.run(res, data=inputs, cell=cell, time_major=True) +# +# +# class TestRNN64(TestRNN): +# """ +# test RNN float64 +# """ +# +# def hook(self): +# """ +# implement +# """ +# self.types = ["float64"] +# self.seed = 100 +# self.delta = 0.0001 +# self.forward_kwargs = {} # 前向传播参数 +# paddle.set_default_dtype("float64") +# +# +# obj64 = TestRNN64(paddle.nn.RNN) +# +# +# @pytest.mark.api_nn_RNN_vartype +# def test_rnn_base_64(): +# """ +# float64 base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 +# """ +# paddle.set_default_dtype("float64") +# np.random.seed(obj64.seed) +# inputs = np.random.random((2, 3, 4)) +# np_cell = SimpleRNNCell(4, 8) +# rnn = RNN(cell=np_cell) +# res_outputs, res_final_states = rnn(inputs) +# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] +# paddle.disable_static() +# cell = paddle.nn.SimpleRNNCell(4, 8) +# cell = copy_cell_params(np_cell, cell, dtype="float64") +# obj64.base(res, data=inputs, cell=cell) +# +# +# @pytest.mark.api_nn_RNN_parameters +# def test_rnn1_64(): +# """ +# 测试默认参数 +# """ +# paddle.set_default_dtype("float64") +# # numpy +# np.random.seed(obj64.seed) +# inputs = np.random.random((10, 8, 16)) +# np_cell = SimpleRNNCell(16, 32) +# rnn = RNN(cell=np_cell) +# res_outputs, res_final_states = rnn(inputs) +# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] +# +# paddle.disable_static() +# +# cell = paddle.nn.SimpleRNNCell(16, 32) +# cell = copy_cell_params(np_cell, cell, dtype="float64") +# obj64.run(res, data=inputs, cell=cell) +# +# +# # +# @pytest.mark.apt_nn_RNN_parameters +# def test_rnn2_64(): +# """ +# 测试 is_reverse=True +# """ +# paddle.set_default_dtype("float64") +# # numpy +# np.random.seed(obj64.seed) +# inputs = np.random.random((20, 10, 32)) +# np_cell = SimpleRNNCell(32, 16) +# rnn = RNN(cell=np_cell, is_reverse=True) +# res_outputs, res_final_states = rnn(inputs) +# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] +# +# paddle.disable_static() +# +# cell = paddle.nn.SimpleRNNCell(32, 16) +# cell = copy_cell_params(np_cell, cell, dtype="float64") +# obj64.run(res, data=inputs, cell=cell, is_reverse=True) +# +# +# @pytest.mark.apt_nn_RNN_parameters +# def test_rnn3_64(): +# """ +# 测试 time_major = True +# """ +# paddle.set_default_dtype("float64") +# # numpy +# np.random.seed(obj64.seed) +# inputs = np.random.random((10, 4, 128)) +# np_cell = SimpleRNNCell(128, 32) +# rnn = RNN(cell=np_cell, time_major=True) +# res_outputs, res_final_states = rnn(inputs) +# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] +# +# paddle.disable_static() +# +# cell = paddle.nn.SimpleRNNCell(128, 32) +# cell = copy_cell_params(np_cell, cell, dtype="float64") +# +# obj64.run(res, data=inputs, cell=cell, time_major=True) diff --git a/framework/api/nn/test_rnn.py b/framework/api/nn/test_rnn.py deleted file mode 100644 index 2ca846f7e1..0000000000 --- a/framework/api/nn/test_rnn.py +++ /dev/null @@ -1,296 +0,0 @@ -#!/bin/env python -# -*- coding: utf-8 -*- -# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python -""" -paddle.nn.RNN float32测试 -""" -import copy - -from apibase import APIBase -import paddle -import pytest -import numpy as np - -from rnn_numpy import SimpleRNNCell, RNN - - -class TestRNN(APIBase): - """ - test RNN float32 - """ - - def hook(self): - """ - implement - """ - self.types = ["float32"] - self.seed = 100 - self.delta = 0.0001 - self.forward_kwargs = {} # 前向传播参数 - paddle.set_default_dtype("float32") - - def _static_forward(self, res, data=None, **kwargs): - """ - _static_forward - """ - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - main_program.random_seed = self.seed - - cell = kwargs["cell"] - - with paddle.utils.unique_name.guard(): - with paddle.static.program_guard(main_program=main_program, startup_program=startup_program): - if data is not None: - data = data.astype(self.dtype) - self.data = paddle.static.data(name="data", shape=data.shape, dtype=self.dtype) - self.data.stop_gradient = False - data = dict({"data": data}, **kwargs) - - static_cell = paddle.nn.SimpleRNNCell(cell.input_size, cell.hidden_size) - parameters = {} - for k, v in kwargs["cell"].named_parameters(): - parameters[k] = v - - obj = self.func( - static_cell, - is_reverse=self.kwargs.get("is_reverse", False), - time_major=self.kwargs.get("time_major", False), - ) - - output, h = obj(self.data) - - if self.enable_backward: - loss = paddle.mean(output) - g = paddle.static.gradients(loss, self.data) - exe = paddle.static.Executor(self.place) - exe.run(startup_program) - - for k, v in static_cell.named_parameters(): - v.set_value(parameters[k]) - - res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) - grad = {"data": res[2]} - return res[0:2], grad - else: - exe = paddle.static.Executor(self.place) - exe.run(startup_program) - for k, v in static_cell.named_parameters(): - v.set_value(parameters[k]) - res = exe.run(main_program, feed=data, fetch_list=[output, h], return_numpy=True) - return res - - def _dygraph_forward(self): - """ - _dygraph_forward - """ - cell = copy.deepcopy(self.kwargs.get("cell")) - obj = self.func( - cell=cell, is_reverse=self.kwargs.get("is_reverse", False), time_major=self.kwargs.get("time_major", False) - ) - res = obj( - self.data, - initial_states=self.forward_kwargs.get("initial_states", None), - sequence_length=self.forward_kwargs.get("sequence_length", None), - ) - return res - - -obj = TestRNN(paddle.nn.RNN) - - -def copy_cell_params(np_cell, paddle_cell, dtype="float32"): - """ - 将np_cell的参数复制到paddle_cell中 - """ - paddle.disable_static() - state = np_cell.parameters - for k, v in paddle_cell.named_parameters(): - t = state[k].astype(dtype) - v.set_value(t) - return paddle_cell - - -@pytest.mark.api_nn_RNN_vartype -def test_rnn_base(): - """ - base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 - """ - paddle.set_default_dtype("float32") - np.random.seed(obj.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) - rnn = RNN(cell=np_cell) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float32"), res_final_states.astype("float32")] - paddle.disable_static() - cell = paddle.nn.SimpleRNNCell(4, 8) - cell = copy_cell_params(np_cell, cell) - obj.base(res, data=inputs, cell=cell) - - -@pytest.mark.api_nn_RNN_parameters -def test_rnn1(): - """ - 测试默认参数 - """ - paddle.set_default_dtype("float32") - # numpy - np.random.seed(obj.seed) - inputs = np.random.random((10, 8, 16)) - np_cell = SimpleRNNCell(16, 32) - rnn = RNN(cell=np_cell) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float32"), res_final_states.astype("float32")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(16, 32) - cell = copy_cell_params(np_cell, cell) - obj.run(res, data=inputs, cell=cell) - - -@pytest.mark.apt_nn_RNN_parameters -def test_rnn2(): - """ - 测试 is_reverse=True - """ - paddle.set_default_dtype("float32") - # numpy - np.random.seed(obj.seed) - inputs = np.random.random((20, 10, 32)) - np_cell = SimpleRNNCell(32, 16) - rnn = RNN(cell=np_cell, is_reverse=True) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float32"), res_final_states.astype("float32")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(32, 16) - cell = copy_cell_params(np_cell, cell) - obj.run(res, data=inputs, cell=cell, is_reverse=True) - - -@pytest.mark.apt_nn_RNN_parameters -def test_rnn3(): - """ - 测试 time_major = True - """ - paddle.set_default_dtype("float32") - # numpy - np.random.seed(obj.seed) - inputs = np.random.random((10, 4, 128)) - np_cell = SimpleRNNCell(128, 32) - rnn = RNN(cell=np_cell, time_major=True) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float32"), res_final_states.astype("float32")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(128, 32) - cell = copy_cell_params(np_cell, cell) - - obj.run(res, data=inputs, cell=cell, time_major=True) - - -class TestRNN64(TestRNN): - """ - test RNN float64 - """ - - def hook(self): - """ - implement - """ - self.types = ["float64"] - self.seed = 100 - self.delta = 0.0001 - self.forward_kwargs = {} # 前向传播参数 - paddle.set_default_dtype("float64") - - -obj64 = TestRNN64(paddle.nn.RNN) - - -@pytest.mark.api_nn_RNN_vartype -def test_rnn_base_64(): - """ - float64 base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 - """ - paddle.set_default_dtype("float64") - np.random.seed(obj64.seed) - inputs = np.random.random((2, 3, 4)) - np_cell = SimpleRNNCell(4, 8) - rnn = RNN(cell=np_cell) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - paddle.disable_static() - cell = paddle.nn.SimpleRNNCell(4, 8) - cell = copy_cell_params(np_cell, cell, dtype="float64") - obj64.base(res, data=inputs, cell=cell) - - -@pytest.mark.api_nn_RNN_parameters -def test_rnn1_64(): - """ - 测试默认参数 - """ - paddle.set_default_dtype("float64") - # numpy - np.random.seed(obj64.seed) - inputs = np.random.random((10, 8, 16)) - np_cell = SimpleRNNCell(16, 32) - rnn = RNN(cell=np_cell) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(16, 32) - cell = copy_cell_params(np_cell, cell, dtype="float64") - obj64.run(res, data=inputs, cell=cell) - - -# -@pytest.mark.apt_nn_RNN_parameters -def test_rnn2_64(): - """ - 测试 is_reverse=True - """ - paddle.set_default_dtype("float64") - # numpy - np.random.seed(obj64.seed) - inputs = np.random.random((20, 10, 32)) - np_cell = SimpleRNNCell(32, 16) - rnn = RNN(cell=np_cell, is_reverse=True) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(32, 16) - cell = copy_cell_params(np_cell, cell, dtype="float64") - obj64.run(res, data=inputs, cell=cell, is_reverse=True) - - -@pytest.mark.apt_nn_RNN_parameters -def test_rnn3_64(): - """ - 测试 time_major = True - """ - paddle.set_default_dtype("float64") - # numpy - np.random.seed(obj64.seed) - inputs = np.random.random((10, 4, 128)) - np_cell = SimpleRNNCell(128, 32) - rnn = RNN(cell=np_cell, time_major=True) - res_outputs, res_final_states = rnn(inputs) - res = [res_outputs.astype("float64"), res_final_states.astype("float64")] - - paddle.disable_static() - - cell = paddle.nn.SimpleRNNCell(128, 32) - cell = copy_cell_params(np_cell, cell, dtype="float64") - - obj64.run(res, data=inputs, cell=cell, time_major=True) From e3d00dabdaf6d2806a719092b217e1a34446ab2c Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 28 Oct 2021 20:48:04 +0800 Subject: [PATCH 09/12] save --- framework/api/nn/test_birnn.py | 70 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/framework/api/nn/test_birnn.py b/framework/api/nn/test_birnn.py index b755736b3c..a442bcb4c3 100644 --- a/framework/api/nn/test_birnn.py +++ b/framework/api/nn/test_birnn.py @@ -23,7 +23,7 @@ def hook(self): """ implement """ - self.types = ["float32"] + self.types = [np.float32] self.seed = 100 self.delta = 0.0001 self.forward_kwargs = {} # 前向传播参数 @@ -74,7 +74,7 @@ def _static_forward(self, res, data=None, **kwargs): for k, v in static_cell_fw.named_parameters(): v.set_value(parameters_fw[k]) - for k, v in static_cell_fw.named_parameters(): + for k, v in static_cell_bw.named_parameters(): v.set_value(parameters_bw[k]) res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) @@ -86,7 +86,7 @@ def _static_forward(self, res, data=None, **kwargs): for k, v in static_cell_fw.named_parameters(): v.set_value(parameters_fw[k]) - for k, v in static_cell_fw.named_parameters(): + for k, v in static_cell_bw.named_parameters(): v.set_value(parameters_bw[k]) res = exe.run(main_program, feed=data, fetch_list=[output, h], return_numpy=True) @@ -122,50 +122,52 @@ def copy_cell_params(np_cell, paddle_cell, dtype="float32"): return paddle_cell +def create_cell(input_size, hidden_size): + """ + update + """ + np_cell_fw = SimpleRNNCell(input_size, hidden_size) + np_cell_bw = SimpleRNNCell(input_size, hidden_size) + paddle_cell_fw = paddle.nn.SimpleRNNCell(input_size, hidden_size) + paddle_cell_bw = paddle.nn.SimpleRNNCell(input_size, hidden_size) + + paddle_cell_fw = copy_cell_params(np_cell_fw, paddle_cell_fw) + paddle_cell_bw = copy_cell_params(np_cell_bw, paddle_cell_bw) + + return np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw + + @pytest.mark.api_nn_RNN_vartype def test_rnn_base(): """ base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 """ - paddle.set_default_dtype("float32") np.random.seed(obj.seed) inputs = np.random.random((3, 3, 4)) - np_cell_fw = SimpleRNNCell(4, 8) - np_cell_bw = SimpleRNNCell(4, 8) + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(4, 8) rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) res_outputs = rnn(inputs)[0] res = res_outputs.astype("float32") - paddle.disable_static() - cell_fw = paddle.nn.SimpleRNNCell(4, 8) - cell_bw = paddle.nn.SimpleRNNCell(4, 8) - cell_fw = copy_cell_params(np_cell_fw, cell_fw) - cell_bw = copy_cell_params(np_cell_bw, cell_bw) + obj.base(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) - obj.base(res, data=inputs, cell_fw=cell_fw, cell_bw=cell_bw) + +@pytest.mark.api_nn_RNN_parameters +def test_rnn1(): + """ + 测试默认参数 + """ + paddle.set_default_dtype("float32") + paddle.disable_static() + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((10, 8, 16)) + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(16, 32) + rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) + res_outputs = rnn(inputs)[0] + res = res_outputs.astype("float32") + obj.run(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) -# -# @pytest.mark.api_nn_RNN_parameters -# def test_rnn1(): -# """ -# 测试默认参数 -# """ -# paddle.set_default_dtype("float32") -# # numpy -# np.random.seed(obj.seed) -# inputs = np.random.random((10, 8, 16)) -# np_cell = SimpleRNNCell(16, 32) -# rnn = BiRNN(cell=np_cell) -# res_outputs, res_final_states = rnn(inputs) -# res = [res_outputs.astype("float32"), res_final_states.astype("float32")] -# -# paddle.disable_static() -# -# cell = paddle.nn.SimpleRNNCell(16, 32) -# cell = copy_cell_params(np_cell, cell) -# obj.run(res, data=inputs, cell=cell) -# -# # @pytest.mark.apt_nn_RNN_parameters # def test_rnn2(): # """ From 02794f06f4ffd3640387a4f82bd86cfa193e93af Mon Sep 17 00:00:00 2001 From: spade Date: Thu, 28 Oct 2021 21:37:37 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=AE=8C=E6=88=90test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/test_birnn.py | 236 +++++++++++++-------------------- 1 file changed, 89 insertions(+), 147 deletions(-) diff --git a/framework/api/nn/test_birnn.py b/framework/api/nn/test_birnn.py index a442bcb4c3..2fcbd4cb83 100644 --- a/framework/api/nn/test_birnn.py +++ b/framework/api/nn/test_birnn.py @@ -78,7 +78,7 @@ def _static_forward(self, res, data=None, **kwargs): v.set_value(parameters_bw[k]) res = exe.run(main_program, feed=data, fetch_list=[output, h, g], return_numpy=True) - grad = {"data": res[2]} + grad = {"data": res[3]} return res[0], grad else: exe = paddle.static.Executor(self.place) @@ -122,7 +122,7 @@ def copy_cell_params(np_cell, paddle_cell, dtype="float32"): return paddle_cell -def create_cell(input_size, hidden_size): +def create_cell(input_size, hidden_size, dtype="float32"): """ update """ @@ -131,8 +131,8 @@ def create_cell(input_size, hidden_size): paddle_cell_fw = paddle.nn.SimpleRNNCell(input_size, hidden_size) paddle_cell_bw = paddle.nn.SimpleRNNCell(input_size, hidden_size) - paddle_cell_fw = copy_cell_params(np_cell_fw, paddle_cell_fw) - paddle_cell_bw = copy_cell_params(np_cell_bw, paddle_cell_bw) + paddle_cell_fw = copy_cell_params(np_cell_fw, paddle_cell_fw, dtype) + paddle_cell_bw = copy_cell_params(np_cell_bw, paddle_cell_bw, dtype) return np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw @@ -142,6 +142,7 @@ def test_rnn_base(): """ base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 """ + paddle.set_default_dtype("float32") np.random.seed(obj.seed) inputs = np.random.random((3, 3, 4)) np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(4, 8) @@ -168,146 +169,87 @@ def test_rnn1(): obj.run(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) -# @pytest.mark.apt_nn_RNN_parameters -# def test_rnn2(): -# """ -# 测试 is_reverse=True -# """ -# paddle.set_default_dtype("float32") -# # numpy -# np.random.seed(obj.seed) -# inputs = np.random.random((20, 10, 32)) -# np_cell = SimpleRNNCell(32, 16) -# rnn = RNN(cell=np_cell, is_reverse=True) -# res_outputs, res_final_states = rnn(inputs) -# res = [res_outputs.astype("float32"), res_final_states.astype("float32")] -# -# paddle.disable_static() -# -# cell = paddle.nn.SimpleRNNCell(32, 16) -# cell = copy_cell_params(np_cell, cell) -# obj.run(res, data=inputs, cell=cell, is_reverse=True) -# -# -# @pytest.mark.apt_nn_RNN_parameters -# def test_rnn3(): -# """ -# 测试 time_major = True -# """ -# paddle.set_default_dtype("float32") -# # numpy -# np.random.seed(obj.seed) -# inputs = np.random.random((10, 4, 128)) -# np_cell = SimpleRNNCell(128, 32) -# rnn = RNN(cell=np_cell, time_major=True) -# res_outputs, res_final_states = rnn(inputs) -# res = [res_outputs.astype("float32"), res_final_states.astype("float32")] -# -# paddle.disable_static() -# -# cell = paddle.nn.SimpleRNNCell(128, 32) -# cell = copy_cell_params(np_cell, cell) -# -# obj.run(res, data=inputs, cell=cell, time_major=True) -# -# -# class TestRNN64(TestRNN): -# """ -# test RNN float64 -# """ -# -# def hook(self): -# """ -# implement -# """ -# self.types = ["float64"] -# self.seed = 100 -# self.delta = 0.0001 -# self.forward_kwargs = {} # 前向传播参数 -# paddle.set_default_dtype("float64") -# -# -# obj64 = TestRNN64(paddle.nn.RNN) -# -# -# @pytest.mark.api_nn_RNN_vartype -# def test_rnn_base_64(): -# """ -# float64 base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 -# """ -# paddle.set_default_dtype("float64") -# np.random.seed(obj64.seed) -# inputs = np.random.random((2, 3, 4)) -# np_cell = SimpleRNNCell(4, 8) -# rnn = RNN(cell=np_cell) -# res_outputs, res_final_states = rnn(inputs) -# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] -# paddle.disable_static() -# cell = paddle.nn.SimpleRNNCell(4, 8) -# cell = copy_cell_params(np_cell, cell, dtype="float64") -# obj64.base(res, data=inputs, cell=cell) -# -# -# @pytest.mark.api_nn_RNN_parameters -# def test_rnn1_64(): -# """ -# 测试默认参数 -# """ -# paddle.set_default_dtype("float64") -# # numpy -# np.random.seed(obj64.seed) -# inputs = np.random.random((10, 8, 16)) -# np_cell = SimpleRNNCell(16, 32) -# rnn = RNN(cell=np_cell) -# res_outputs, res_final_states = rnn(inputs) -# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] -# -# paddle.disable_static() -# -# cell = paddle.nn.SimpleRNNCell(16, 32) -# cell = copy_cell_params(np_cell, cell, dtype="float64") -# obj64.run(res, data=inputs, cell=cell) -# -# -# # -# @pytest.mark.apt_nn_RNN_parameters -# def test_rnn2_64(): -# """ -# 测试 is_reverse=True -# """ -# paddle.set_default_dtype("float64") -# # numpy -# np.random.seed(obj64.seed) -# inputs = np.random.random((20, 10, 32)) -# np_cell = SimpleRNNCell(32, 16) -# rnn = RNN(cell=np_cell, is_reverse=True) -# res_outputs, res_final_states = rnn(inputs) -# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] -# -# paddle.disable_static() -# -# cell = paddle.nn.SimpleRNNCell(32, 16) -# cell = copy_cell_params(np_cell, cell, dtype="float64") -# obj64.run(res, data=inputs, cell=cell, is_reverse=True) -# -# -# @pytest.mark.apt_nn_RNN_parameters -# def test_rnn3_64(): -# """ -# 测试 time_major = True -# """ -# paddle.set_default_dtype("float64") -# # numpy -# np.random.seed(obj64.seed) -# inputs = np.random.random((10, 4, 128)) -# np_cell = SimpleRNNCell(128, 32) -# rnn = RNN(cell=np_cell, time_major=True) -# res_outputs, res_final_states = rnn(inputs) -# res = [res_outputs.astype("float64"), res_final_states.astype("float64")] -# -# paddle.disable_static() -# -# cell = paddle.nn.SimpleRNNCell(128, 32) -# cell = copy_cell_params(np_cell, cell, dtype="float64") -# -# obj64.run(res, data=inputs, cell=cell, time_major=True) +@pytest.mark.api_nn_RNN_parameters +def test_rnn2(): + """ + 测试默认参数 + """ + paddle.set_default_dtype("float32") + paddle.disable_static() + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((10, 4, 128)) + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(128, 32) + rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw, time_major=True) + res_outputs = rnn(inputs)[0] + res = res_outputs.astype("float32") + obj.run(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw, time_major=True) + + +class TestBiRNN64(TestBiRNN): + """ + test RNN float64 + """ + + def hook(self): + """ + implement + """ + self.types = [np.float64] + self.seed = 100 + self.delta = 0.0001 + self.forward_kwargs = {} # 前向传播参数 + paddle.set_default_dtype("float64") + + +obj64 = TestBiRNN64(paddle.nn.BiRNN) + + +@pytest.mark.api_nn_RNN_vartype +def test_rnn_base_64(): + """ + base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 + """ + paddle.disable_static() + paddle.set_default_dtype("float64") + np.random.seed(obj64.seed) + inputs = np.random.random((3, 3, 4)) + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(4, 8, dtype="float64") + rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) + res_outputs = rnn(inputs)[0] + res = res_outputs.astype(np.float64) + obj64.base(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) + + +@pytest.mark.api_nn_RNN_parameters +def test_rnn1_64(): + """ + 测试默认参数 + """ + paddle.set_default_dtype("float64") + paddle.disable_static() + # numpy + np.random.seed(obj64.seed) + inputs = np.random.random((10, 8, 16)) + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(16, 32, dtype="float64") + rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) + res_outputs = rnn(inputs)[0] + res = res_outputs.astype(np.float64) + obj64.run(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) + + +@pytest.mark.api_nn_RNN_parameters +def test_rnn2_64(): + """ + 测试默认参数 + """ + paddle.set_default_dtype("float64") + paddle.disable_static() + # numpy + np.random.seed(obj64.seed) + inputs = np.random.random((10, 4, 128)) + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(128, 32, dtype="float64") + rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw, time_major=True) + res_outputs = rnn(inputs)[0] + res = res_outputs.astype(np.float64) + obj64.run(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw, time_major=True) From 374a8323b303995f41d9586d5f29b01996103ce9 Mon Sep 17 00:00:00 2001 From: spade Date: Tue, 2 Nov 2021 15:49:01 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B8=A6=E6=9C=89?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E7=8A=B6=E6=80=81=E9=AA=8C=E8=AF=81=E3=80=82?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/api/nn/test_birnn.py | 148 ++++++++++++++++++++++++++++----- 1 file changed, 129 insertions(+), 19 deletions(-) diff --git a/framework/api/nn/test_birnn.py b/framework/api/nn/test_birnn.py index 2fcbd4cb83..72de5127a9 100644 --- a/framework/api/nn/test_birnn.py +++ b/framework/api/nn/test_birnn.py @@ -6,7 +6,7 @@ """ import copy -from apibase import APIBase +from apibase import APIBase, compare import paddle import pytest import numpy as np @@ -110,6 +110,25 @@ def _dygraph_forward(self): obj = TestBiRNN(paddle.nn.BiRNN) +class TestBiRNN64(TestBiRNN): + """ + test RNN float64 + """ + + def hook(self): + """ + implement + """ + self.types = [np.float64] + self.seed = 100 + self.delta = 0.0001 + self.forward_kwargs = {} # 前向传播参数 + paddle.set_default_dtype("float64") + + +obj64 = TestBiRNN64(paddle.nn.BiRNN) + + def copy_cell_params(np_cell, paddle_cell, dtype="float32"): """ 将np_cell的参数复制到paddle_cell中 @@ -124,7 +143,7 @@ def copy_cell_params(np_cell, paddle_cell, dtype="float32"): def create_cell(input_size, hidden_size, dtype="float32"): """ - update + 创建RNNCell,保证numpy版本和Paddle初始化参数相同。 """ np_cell_fw = SimpleRNNCell(input_size, hidden_size) np_cell_bw = SimpleRNNCell(input_size, hidden_size) @@ -155,7 +174,9 @@ def test_rnn_base(): @pytest.mark.api_nn_RNN_parameters def test_rnn1(): """ - 测试默认参数 + float32默认参数测试。 + time_major=False + RnnCell size = (16, 32) """ paddle.set_default_dtype("float32") paddle.disable_static() @@ -172,7 +193,7 @@ def test_rnn1(): @pytest.mark.api_nn_RNN_parameters def test_rnn2(): """ - 测试默认参数 + time_major=True """ paddle.set_default_dtype("float32") paddle.disable_static() @@ -186,29 +207,51 @@ def test_rnn2(): obj.run(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw, time_major=True) -class TestBiRNN64(TestBiRNN): +def test_rnn3(): """ - test RNN float64 + 带初始化状态测试, 带有initial_states """ + dtype = "float32" + paddle.set_default_dtype(dtype) + paddle.disable_static() + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((10, 8, 16)) + initial_states0 = np.random.random((10, 32)) + initial_states1 = np.random.random((10, 32)) + initial_states = [initial_states0, initial_states1] + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(16, 32, dtype=dtype) + np_rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) + np_outputs = np_rnn(inputs, initial_states=initial_states)[0].astype(dtype) - def hook(self): - """ - implement - """ - self.types = [np.float64] - self.seed = 100 - self.delta = 0.0001 - self.forward_kwargs = {} # 前向传播参数 - paddle.set_default_dtype("float64") + paddle_rnn = paddle.nn.BiRNN(cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) + initial_states = [paddle.to_tensor(t, dtype=dtype) for t in initial_states] + paddle_outputs = paddle_rnn(paddle.to_tensor(inputs, dtype=dtype), initial_states=initial_states)[0] + compare(expect=np_outputs, result=paddle_outputs.numpy(), delta=obj.delta, rtol=obj.rtol) -obj64 = TestBiRNN64(paddle.nn.BiRNN) + +@pytest.mark.api_nn_RNN_parameters +def test_rnn4(): + """ + float32 异常测试,fw_cell和bw_cell尺寸不同 + """ + paddle.set_default_dtype("float32") + paddle.disable_static() + # numpy + np.random.seed(obj64.seed) + inputs = np.random.random((10, 4, 128)) + + paddle_cell_fw = paddle.nn.SimpleRNNCell(4, 16) + paddle_cell_bw = paddle.nn.SimpleRNNCell(8, 16) + + obj.exception(etype=ValueError, mode="python", data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) @pytest.mark.api_nn_RNN_vartype def test_rnn_base_64(): """ - base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 + float64 base测试,包括动态度、静态图、cpu/gpu,grad,动态静态图的结果一致性 """ paddle.disable_static() paddle.set_default_dtype("float64") @@ -224,7 +267,9 @@ def test_rnn_base_64(): @pytest.mark.api_nn_RNN_parameters def test_rnn1_64(): """ - 测试默认参数 + float64默认参数测试。 + time_major=False + RnnCell size = (16, 32) """ paddle.set_default_dtype("float64") paddle.disable_static() @@ -241,7 +286,7 @@ def test_rnn1_64(): @pytest.mark.api_nn_RNN_parameters def test_rnn2_64(): """ - 测试默认参数 + time_major=True """ paddle.set_default_dtype("float64") paddle.disable_static() @@ -253,3 +298,68 @@ def test_rnn2_64(): res_outputs = rnn(inputs)[0] res = res_outputs.astype(np.float64) obj64.run(res, data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw, time_major=True) + + +def test_rnn3_64(): + """ + 带初始化状态测试, 带有initial_states, time_major=False + """ + dtype = "float64" + paddle.set_default_dtype(dtype) + paddle.disable_static() + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((10, 8, 16)) + initial_states0 = np.random.random((10, 32)) + initial_states1 = np.random.random((10, 32)) + initial_states = [initial_states0, initial_states1] + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(16, 32, dtype=dtype) + np_rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) + np_outputs = np_rnn(inputs, initial_states=initial_states)[0].astype(dtype) + + paddle_rnn = paddle.nn.BiRNN(cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) + initial_states = [paddle.to_tensor(t, dtype=dtype) for t in initial_states] + paddle_outputs = paddle_rnn(paddle.to_tensor(inputs, dtype=dtype), initial_states=initial_states)[0] + + compare(expect=np_outputs, result=paddle_outputs.numpy(), delta=obj.delta, rtol=obj.rtol) + + +def test_rnn3_64(): + """ + 带初始化状态测试, 带有initial_states,time_major=False + """ + dtype = "float64" + paddle.set_default_dtype(dtype) + paddle.disable_static() + # numpy + np.random.seed(obj.seed) + inputs = np.random.random((10, 8, 16)) + initial_states0 = np.random.random((10, 32)) + initial_states1 = np.random.random((10, 32)) + initial_states = [initial_states0, initial_states1] + np_cell_fw, np_cell_bw, paddle_cell_fw, paddle_cell_bw = create_cell(16, 32, dtype=dtype) + np_rnn = BiRNN(cell_fw=np_cell_fw, cell_bw=np_cell_bw) + np_outputs = np_rnn(inputs, initial_states=initial_states)[0].astype(dtype) + + paddle_rnn = paddle.nn.BiRNN(cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) + initial_states = [paddle.to_tensor(t, dtype=dtype) for t in initial_states] + paddle_outputs = paddle_rnn(paddle.to_tensor(inputs, dtype=dtype), initial_states=initial_states)[0] + + compare(expect=np_outputs, result=paddle_outputs.numpy(), delta=obj.delta, rtol=obj.rtol) + + +@pytest.mark.api_nn_RNN_parameters +def test_rnn4_64(): + """ + float64 异常测试,fw_cell和bw_cell尺寸不同 + """ + paddle.set_default_dtype("float64") + paddle.disable_static() + # numpy + np.random.seed(obj64.seed) + inputs = np.random.random((10, 4, 128)) + + paddle_cell_fw = paddle.nn.SimpleRNNCell(4, 16) + paddle_cell_bw = paddle.nn.SimpleRNNCell(8, 16) + + obj.exception(etype=ValueError, mode="python", data=inputs, cell_fw=paddle_cell_fw, cell_bw=paddle_cell_bw) From a52f01bea4ca52b4b457395493883e9482dcb6bb Mon Sep 17 00:00:00 2001 From: spade Date: Sat, 6 Nov 2021 11:00:15 +0800 Subject: [PATCH 12/12] retest ci --- framework/api/nn/test_birnn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/api/nn/test_birnn.py b/framework/api/nn/test_birnn.py index 72de5127a9..45d5f8a52a 100644 --- a/framework/api/nn/test_birnn.py +++ b/framework/api/nn/test_birnn.py @@ -175,7 +175,7 @@ def test_rnn_base(): def test_rnn1(): """ float32默认参数测试。 - time_major=False + time_major=False, RnnCell size = (16, 32) """ paddle.set_default_dtype("float32")