-
Notifications
You must be signed in to change notification settings - Fork 6k
Mutiply bug allow non-tensor data input #27690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d3022bb
28aa96f
8f6f236
ed1a0e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ class TestMultiplyAPI(unittest.TestCase): | |
|
|
||
| def __run_static_graph_case(self, x_data, y_data, axis=-1): | ||
| with program_guard(Program(), Program()): | ||
| paddle.enable_static() | ||
| x = paddle.static.data( | ||
| name='x', shape=x_data.shape, dtype=x_data.dtype) | ||
| y = paddle.static.data( | ||
|
|
@@ -42,66 +43,140 @@ def __run_static_graph_case(self, x_data, y_data, axis=-1): | |
| res = outs[0] | ||
| return res | ||
|
|
||
| def __run_static_graph_case_with_numpy_input(self, x_data, y_data, axis=-1): | ||
| with program_guard(Program(), Program()): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. program是静态图才有的,所以最好paddle.enable_static写在with program_guard前面 |
||
| paddle.enable_static() | ||
|
|
||
| res = tensor.multiply(x_data, y_data, axis=axis) | ||
| place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda( | ||
| ) else fluid.CPUPlace() | ||
| exe = fluid.Executor(place) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 将来可以尽量用2.0 迁移的写法,比如paddle.CPUPlace, paddle.CUDAPlace,paddle.static.Executor |
||
| outs = exe.run(fluid.default_main_program(), | ||
| feed={'x': x_data, | ||
| 'y': y_data}, | ||
| fetch_list=[res]) | ||
| res = outs[0] | ||
| return res | ||
|
|
||
| def __run_dynamic_graph_case(self, x_data, y_data, axis=-1): | ||
| paddle.disable_static() | ||
| x = paddle.to_tensor(x_data) | ||
| y = paddle.to_tensor(y_data) | ||
| res = paddle.multiply(x, y, axis=axis) | ||
| return res.numpy() | ||
|
|
||
| def __run_dynamic_graph_case_with_numpy_input(self, x_data, y_data, | ||
| axis=-1): | ||
| paddle.disable_static() | ||
| res = paddle.multiply(x_data, y_data, axis=axis) | ||
| return res.numpy() | ||
|
|
||
| def test_multiply(self): | ||
| """test_multiply.""" | ||
| np.random.seed(7) | ||
|
|
||
| # test static computation graph: 1-d array | ||
| x_data = np.random.rand(200) | ||
| y_data = np.random.rand(200) | ||
| res = self.__run_static_graph_case(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test static computation graph: 1-d array | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 像我以前说过,python里面用注释分隔测试并不是最好的方法,你可以直接创建一个测试method: def test_static_multiply_1d(self) 这类的 |
||
| x_data = np.random.rand(200) | ||
| y_data = np.random.rand(200) | ||
| res = self.__run_static_graph_case_with_numpy_input(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test static computation graph: 2-d array | ||
| x_data = np.random.rand(2, 500) | ||
| y_data = np.random.rand(2, 500) | ||
| res = self.__run_static_graph_case(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test static computation graph with_primitives: 2-d array | ||
| x_data = np.random.rand(2, 500) | ||
| y_data = np.random.rand(2, 500) | ||
| res = self.__run_static_graph_case_with_numpy_input(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test static computation graph: broadcast | ||
| x_data = np.random.rand(2, 500) | ||
| y_data = np.random.rand(500) | ||
| res = self.__run_static_graph_case(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test static computation graph with_primitives: broadcast | ||
| x_data = np.random.rand(2, 500) | ||
| y_data = np.random.rand(500) | ||
| res = self.__run_static_graph_case_with_numpy_input(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test static computation graph: broadcast with axis | ||
| x_data = np.random.rand(2, 300, 40) | ||
| y_data = np.random.rand(300) | ||
| res = self.__run_static_graph_case(x_data, y_data, axis=1) | ||
| expected = np.multiply(x_data, y_data[..., np.newaxis]) | ||
| self.assertTrue(np.allclose(res, expected)) | ||
|
|
||
| # test static computation graph with_primitives: broadcast with axis | ||
| x_data = np.random.rand(2, 300, 40) | ||
| y_data = np.random.rand(300) | ||
| res = self.__run_static_graph_case_with_numpy_input( | ||
| x_data, y_data, axis=1) | ||
| expected = np.multiply(x_data, y_data[..., np.newaxis]) | ||
| self.assertTrue(np.allclose(res, expected)) | ||
|
|
||
| # test dynamic computation graph: 1-d array | ||
| x_data = np.random.rand(200) | ||
| y_data = np.random.rand(200) | ||
| res = self.__run_dynamic_graph_case(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test dynamic numpy input computation graph: 1-d array | ||
| x_data = np.random.rand(200) | ||
| y_data = np.random.rand(200) | ||
| res = self.__run_dynamic_graph_case_with_numpy_input(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test dynamic computation graph: 2-d array | ||
| x_data = np.random.rand(20, 50) | ||
| y_data = np.random.rand(20, 50) | ||
| res = self.__run_dynamic_graph_case(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test dynamic numpy input computation graph: 1-d array | ||
| x_data = np.random.rand(20, 50) | ||
| y_data = np.random.rand(20, 50) | ||
| res = self.__run_dynamic_graph_case_with_numpy_input(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test dynamic computation graph: broadcast | ||
| x_data = np.random.rand(2, 500) | ||
| y_data = np.random.rand(500) | ||
| res = self.__run_dynamic_graph_case(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test dynamic computation graph with numpy tensor: broadcast | ||
| x_data = np.random.rand(2, 500) | ||
| y_data = np.random.rand(500) | ||
| res = self.__run_dynamic_graph_case_with_numpy_input(x_data, y_data) | ||
| self.assertTrue(np.allclose(res, np.multiply(x_data, y_data))) | ||
|
|
||
| # test dynamic computation graph: broadcast with axis | ||
| x_data = np.random.rand(2, 300, 40) | ||
| y_data = np.random.rand(300) | ||
| res = self.__run_dynamic_graph_case(x_data, y_data, axis=1) | ||
| expected = np.multiply(x_data, y_data[..., np.newaxis]) | ||
| self.assertTrue(np.allclose(res, expected)) | ||
|
|
||
| # test dynamic computation graph with numpy tensor: broadcast with axis | ||
| x_data = np.random.rand(2, 300, 40) | ||
| y_data = np.random.rand(300) | ||
| res = self.__run_dynamic_graph_case_with_numpy_input( | ||
| x_data, y_data, axis=1) | ||
| expected = np.multiply(x_data, y_data[..., np.newaxis]) | ||
| self.assertTrue(np.allclose(res, expected)) | ||
|
|
||
|
|
||
| class TestMultiplyError(unittest.TestCase): | ||
| """TestMultiplyError.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
program是静态图才有的,所以最好paddle.enable_static写在with program_guard前面