Skip to content
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

Add new API inner&outer #37706

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
from .tensor.math import angle # noqa: F401
from .tensor.math import fmax # noqa: F401
from .tensor.math import fmin # noqa: F401
from .tensor.math import inner # noqa: F401
from .tensor.math import outer # noqa: F401

from .tensor.random import multinomial # noqa: F401
from .tensor.random import standard_normal # noqa: F401
Expand Down Expand Up @@ -495,6 +497,8 @@
'lgamma',
'lerp',
'erfinv',
'inner',
'outer',
'square',
'divide',
'ceil',
Expand Down
166 changes: 166 additions & 0 deletions python/paddle/fluid/tests/unittests/test_inner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import unittest

import numpy as np

import paddle
from paddle.static import Program, program_guard


class TestMultiplyApi(unittest.TestCase):
def _run_static_graph_case(self, x_data, y_data):
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(
name='y', shape=y_data.shape, dtype=y_data.dtype)
res = paddle.inner(x, y)

place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda(
) else paddle.CPUPlace()
exe = paddle.static.Executor(place)
outs = exe.run(paddle.static.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):
paddle.disable_static()
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
res = paddle.inner(x, y)
return res.numpy()

def test_multiply(self):
np.random.seed(7)

# test static computation graph: 3-d array
x_data = np.random.rand(2, 10, 10).astype(np.float64)
y_data = np.random.rand(2, 5, 10).astype(np.float64)
res = self._run_static_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))

# test static computation graph: 2-d array
x_data = np.random.rand(200, 5).astype(np.float64)
y_data = np.random.rand(50, 5).astype(np.float64)
res = self._run_static_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))

# test static computation graph: 1-d array
x_data = np.random.rand(50).astype(np.float64)
y_data = np.random.rand(50).astype(np.float64)
res = self._run_static_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))

# test dynamic computation graph: 3-d array
x_data = np.random.rand(5, 10, 10).astype(np.float64)
y_data = np.random.rand(2, 10).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))

# test dynamic computation graph: 2-d array
x_data = np.random.rand(20, 50).astype(np.float64)
y_data = np.random.rand(50).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))

# test dynamic computation graph: Scalar
x_data = np.random.rand(20, 10).astype(np.float32)
y_data = np.random.rand(1).astype(np.float32).item()
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))

# test dynamic computation graph: 2-d array Complex
x_data = np.random.rand(20,
50).astype(np.float64) + 1J * np.random.rand(
20, 50).astype(np.float64)
y_data = np.random.rand(50).astype(np.float64) + 1J * np.random.rand(
50).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))

# test dynamic computation graph: 3-d array Complex
x_data = np.random.rand(5, 10,
10).astype(np.float64) + 1J * np.random.rand(
5, 10, 10).astype(np.float64)
y_data = np.random.rand(2, 10).astype(np.float64) + 1J * np.random.rand(
2, 10).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.inner(x_data, y_data)))


class TestMultiplyError(unittest.TestCase):
def test_errors(self):
# test static computation graph: dtype can not be int8
paddle.enable_static()
with program_guard(Program(), Program()):
x = paddle.static.data(name='x', shape=[100], dtype=np.int8)
y = paddle.static.data(name='y', shape=[100], dtype=np.int8)
self.assertRaises(TypeError, paddle.inner, x, y)

# test static computation graph: inputs must be broadcastable
with program_guard(Program(), Program()):
x = paddle.static.data(name='x', shape=[20, 50], dtype=np.float64)
y = paddle.static.data(name='y', shape=[20], dtype=np.float64)
self.assertRaises(ValueError, paddle.inner, x, y)

np.random.seed(7)
# test dynamic computation graph: dtype can not be int8
paddle.disable_static()
x_data = np.random.randn(200).astype(np.int8)
y_data = np.random.randn(200).astype(np.int8)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
self.assertRaises(RuntimeError, paddle.inner, x, y)

# test dynamic computation graph: inputs must be broadcastable
x_data = np.random.rand(20, 5)
y_data = np.random.rand(10, 2)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
self.assertRaises(ValueError, paddle.inner, x, y)

# test dynamic computation graph: dtype must be same
x_data = np.random.randn(200).astype(np.float32)
y_data = np.random.randn(200).astype(np.float64)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
self.assertRaises(ValueError, paddle.inner, x, y)

# test dynamic computation graph: dtype must be Tensor type
x_data = np.random.randn(200).astype(np.float64)
y_data = np.random.randn(200).astype(np.float64)
y = paddle.to_tensor(y_data)
self.assertRaises(ValueError, paddle.inner, x_data, y)

# test dynamic computation graph: dtype must be Tensor type
x_data = np.random.randn(200).astype(np.float64)
y_data = np.random.randn(200).astype(np.float64)
x = paddle.to_tensor(x_data)
self.assertRaises(ValueError, paddle.inner, x, y_data)

# test dynamic computation graph: dtype must be Tensor type
x_data = np.random.randn(200).astype(np.float32)
y_data = np.random.randn(200).astype(np.float32)
self.assertRaises(ValueError, paddle.inner, x_data, y_data)


if __name__ == '__main__':
unittest.main()
153 changes: 153 additions & 0 deletions python/paddle/fluid/tests/unittests/test_outer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import unittest

import numpy as np

import paddle
from paddle.static import Program, program_guard


class TestMultiplyApi(unittest.TestCase):
def _run_static_graph_case(self, x_data, y_data):
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(
name='y', shape=y_data.shape, dtype=y_data.dtype)
res = paddle.outer(x, y)

place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda(
) else paddle.CPUPlace()
exe = paddle.static.Executor(place)
outs = exe.run(paddle.static.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):
paddle.disable_static()
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
res = paddle.outer(x, y)
return res.numpy()

def test_multiply(self):
np.random.seed(7)

# test static computation graph: 3-d array
x_data = np.random.rand(2, 10, 10).astype(np.float64)
y_data = np.random.rand(2, 5, 10).astype(np.float64)
res = self._run_static_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data)))

# test static computation graph: 2-d array
x_data = np.random.rand(200, 5).astype(np.float64)
y_data = np.random.rand(50, 5).astype(np.float64)
res = self._run_static_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data)))

# test static computation graph: 1-d array
x_data = np.random.rand(50).astype(np.float64)
y_data = np.random.rand(50).astype(np.float64)
res = self._run_static_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data)))

# test dynamic computation graph: 3-d array
x_data = np.random.rand(5, 10, 10).astype(np.float64)
y_data = np.random.rand(2, 10).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data)))

# test dynamic computation graph: 2-d array
x_data = np.random.rand(20, 50).astype(np.float64)
y_data = np.random.rand(50).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data)))

# test dynamic computation graph: Scalar
x_data = np.random.rand(20, 10).astype(np.float32)
y_data = np.random.rand(1).astype(np.float32).item()
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data), rtol=1e4))

# test dynamic computation graph: 2-d array Complex
x_data = np.random.rand(20,
50).astype(np.float64) + 1J * np.random.rand(
20, 50).astype(np.float64)
y_data = np.random.rand(50).astype(np.float64) + 1J * np.random.rand(
50).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data)))

# test dynamic computation graph: 3-d array Complex
x_data = np.random.rand(5, 10,
10).astype(np.float64) + 1J * np.random.rand(
5, 10, 10).astype(np.float64)
y_data = np.random.rand(2, 10).astype(np.float64) + 1J * np.random.rand(
2, 10).astype(np.float64)
res = self._run_dynamic_graph_case(x_data, y_data)
self.assertTrue(np.allclose(res, np.outer(x_data, y_data)))


class TestMultiplyError(unittest.TestCase):
def test_errors(self):
# test static computation graph: dtype can not be int8
paddle.enable_static()
with program_guard(Program(), Program()):
x = paddle.static.data(name='x', shape=[100], dtype=np.int8)
y = paddle.static.data(name='y', shape=[100], dtype=np.int8)
self.assertRaises(TypeError, paddle.outer, x, y)

np.random.seed(7)
# test dynamic computation graph: dtype can not be int8
paddle.disable_static()
x_data = np.random.randn(200).astype(np.int8)
y_data = np.random.randn(200).astype(np.int8)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
self.assertRaises(RuntimeError, paddle.outer, x, y)

# test dynamic computation graph: dtype must be same
x_data = np.random.randn(200).astype(np.float32)
y_data = np.random.randn(200).astype(np.float64)
x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_data)
self.assertRaises(ValueError, paddle.outer, x, y)

# test dynamic computation graph: dtype must be Tensor type
x_data = np.random.randn(200).astype(np.float64)
y_data = np.random.randn(200).astype(np.float64)
y = paddle.to_tensor(y_data)
self.assertRaises(ValueError, paddle.outer, x_data, y)

# test dynamic computation graph: dtype must be Tensor type
x_data = np.random.randn(200).astype(np.float32)
y_data = np.random.randn(200).astype(np.float32)
x = paddle.to_tensor(x_data)
self.assertRaises(ValueError, paddle.outer, x, y_data)

# test dynamic computation graph: dtype must be Tensor type
x_data = np.random.randn(200).astype(np.float32)
y_data = np.random.randn(200).astype(np.float32)
self.assertRaises(ValueError, paddle.outer, x_data, y_data)


if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@
from .math import angle # noqa: F401
from .math import fmax # noqa: F401
from .math import fmin # noqa: F401
from .math import inner # noqa: F401
from .math import outer # noqa: F401

from .random import multinomial # noqa: F401
from .random import standard_normal # noqa: F401
Expand Down Expand Up @@ -319,6 +321,8 @@
'fmax',
'fmin',
'mm',
'inner',
'outer',
'divide',
'floor_divide',
'remainder',
Expand Down
Loading