236 lines
7.9 KiB
Python
236 lines
7.9 KiB
Python
# Copyright (c) 2019 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.
|
|
import unittest
|
|
|
|
import numpy as np
|
|
from op_test import get_device_place, is_custom_device
|
|
|
|
import paddle
|
|
from paddle.base import core
|
|
|
|
|
|
class TestGcdAPI(unittest.TestCase):
|
|
def setUp(self):
|
|
self.x_np = [12]
|
|
self.y_np = [20]
|
|
self.x_shape = [1]
|
|
self.y_shape = [1]
|
|
|
|
def test_static_graph(self):
|
|
if core.is_compiled_with_cuda() or is_custom_device():
|
|
place = get_device_place()
|
|
else:
|
|
place = core.CPUPlace()
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(
|
|
name='input1', dtype='int32', shape=self.x_shape
|
|
)
|
|
y = paddle.static.data(
|
|
name='input2', dtype='int32', shape=self.y_shape
|
|
)
|
|
out = paddle.gcd(x, y)
|
|
out_ref = np.gcd(self.x_np, self.y_np)
|
|
|
|
exe = paddle.static.Executor(place)
|
|
res = exe.run(
|
|
paddle.static.default_main_program(),
|
|
feed={'input1': self.x_np, 'input2': self.y_np},
|
|
fetch_list=[out],
|
|
)
|
|
self.assertTrue((res[0] == out_ref).all())
|
|
|
|
def test_dygraph(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(self.x_np)
|
|
y = paddle.to_tensor(self.y_np)
|
|
result = paddle.gcd(x, y)
|
|
np.testing.assert_allclose(
|
|
np.gcd(self.x_np, self.y_np), result.numpy(), rtol=1e-05
|
|
)
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
class TestGcdAPI2(TestGcdAPI):
|
|
def setUp(self):
|
|
self.x_np = np.arange(6).astype(np.int32)
|
|
self.y_np = np.array([20]).astype(np.int32)
|
|
self.x_shape = [6]
|
|
self.y_shape = [1]
|
|
|
|
|
|
class TestGcdAPI3(TestGcdAPI):
|
|
def setUp(self):
|
|
self.x_np = 0
|
|
self.y_np = 20
|
|
self.x_shape = []
|
|
self.y_shape = []
|
|
|
|
|
|
class TestGcdAPI4(TestGcdAPI):
|
|
def setUp(self):
|
|
self.x_np = [0]
|
|
self.y_np = [0]
|
|
self.x_shape = [1]
|
|
self.y_shape = [1]
|
|
|
|
|
|
class TestGcdAPI5(TestGcdAPI):
|
|
def setUp(self):
|
|
self.x_np = 12
|
|
self.y_np = -20
|
|
self.x_shape = []
|
|
self.y_shape = []
|
|
|
|
|
|
class TestGcd(unittest.TestCase):
|
|
def setUp(self):
|
|
self.x_np = np.array([12, 18, 24]).astype(np.int32)
|
|
self.y_np = np.array([20, 24, 36]).astype(np.int32)
|
|
self.expected = np.gcd(self.x_np, self.y_np)
|
|
|
|
def test_gcd_with_y_parameter(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(self.x_np)
|
|
y = paddle.to_tensor(self.y_np)
|
|
x_clone = x.clone()
|
|
out = paddle.gcd(x_clone, y)
|
|
np.testing.assert_allclose(out.numpy(), self.expected, rtol=1e-05)
|
|
paddle.enable_static()
|
|
|
|
def test_gcd_with_other_parameter(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(self.x_np)
|
|
other = paddle.to_tensor(self.y_np)
|
|
x_clone = x.clone()
|
|
out = paddle.gcd(x_clone, other=other)
|
|
np.testing.assert_allclose(out.numpy(), self.expected, rtol=1e-05)
|
|
paddle.enable_static()
|
|
|
|
def test_gcd_with_both_parameters_error(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(self.x_np)
|
|
y = paddle.to_tensor(self.y_np)
|
|
x_clone = x.clone()
|
|
other = paddle.to_tensor(self.y_np)
|
|
with self.assertRaises(TypeError):
|
|
out = paddle.gcd(x_clone, y, other=other)
|
|
paddle.enable_static()
|
|
|
|
|
|
class TestGcdOutParameter(unittest.TestCase):
|
|
def test_dygraph_out(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor([12, 18, 24], dtype='int64')
|
|
y = paddle.to_tensor([20, 24, 36], dtype='int64')
|
|
out = paddle.zeros([3], dtype='int64')
|
|
result = paddle.gcd(x, y, out=out)
|
|
expected = np.gcd([12, 18, 24], [20, 24, 36])
|
|
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-05)
|
|
self.assertIs(result, out)
|
|
paddle.enable_static()
|
|
|
|
def test_dygraph_out_none(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor([12, 18, 24], dtype='int64')
|
|
y = paddle.to_tensor([20, 24, 36], dtype='int64')
|
|
result = paddle.gcd(x, y)
|
|
expected = np.gcd([12, 18, 24], [20, 24, 36])
|
|
np.testing.assert_allclose(result.numpy(), expected, rtol=1e-05)
|
|
paddle.enable_static()
|
|
|
|
def test_dygraph_out_scalar(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(12, dtype='int64')
|
|
y = paddle.to_tensor(20, dtype='int64')
|
|
out = paddle.zeros([], dtype='int64')
|
|
result = paddle.gcd(x, y, out=out)
|
|
self.assertEqual(out.numpy(), 4)
|
|
self.assertIs(result, out)
|
|
paddle.enable_static()
|
|
|
|
def test_dygraph_out_broadcast(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor([12, 18, 24], dtype='int64')
|
|
y = paddle.to_tensor([20], dtype='int64')
|
|
out = paddle.zeros([3], dtype='int64')
|
|
result = paddle.gcd(x, y, out=out)
|
|
expected = np.gcd([12, 18, 24], [20])
|
|
np.testing.assert_allclose(out.numpy(), expected, rtol=1e-05)
|
|
self.assertIs(result, out)
|
|
paddle.enable_static()
|
|
|
|
def test_static_out(self):
|
|
if core.is_compiled_with_cuda() or is_custom_device():
|
|
place = get_device_place()
|
|
else:
|
|
place = core.CPUPlace()
|
|
x_np = np.array([12, 18, 24]).astype(np.int64)
|
|
y_np = np.array([20, 24, 36]).astype(np.int64)
|
|
with paddle.static.program_guard(
|
|
paddle.static.Program(), paddle.static.Program()
|
|
):
|
|
x = paddle.static.data(name='x', dtype='int64', shape=[3])
|
|
y = paddle.static.data(name='y', dtype='int64', shape=[3])
|
|
out = paddle.static.data(name='out', dtype='int64', shape=[3])
|
|
result = paddle.gcd(x, y, out=out)
|
|
exe = paddle.static.Executor(place)
|
|
res = exe.run(
|
|
feed={'x': x_np, 'y': y_np, 'out': np.zeros(3, dtype=np.int64)},
|
|
fetch_list=[result],
|
|
)
|
|
np.testing.assert_allclose(res[0], np.gcd(x_np, y_np), rtol=1e-05)
|
|
|
|
|
|
class TestGcdInplaceAPI(unittest.TestCase):
|
|
def setUp(self):
|
|
self.x_np = np.array([12, 18, 24]).astype(np.int32)
|
|
self.y_np = np.array([20, 24, 36]).astype(np.int32)
|
|
self.expected = np.gcd(self.x_np, self.y_np)
|
|
|
|
def test_gcd_inplace_with_y_parameter(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(self.x_np)
|
|
y = paddle.to_tensor(self.y_np)
|
|
x_clone = x.clone()
|
|
out = paddle.gcd_(x_clone, y)
|
|
np.testing.assert_allclose(out.numpy(), self.expected, rtol=1e-05)
|
|
paddle.enable_static()
|
|
|
|
def test_gcd_inplace_with_other_parameter(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(self.x_np)
|
|
other = paddle.to_tensor(self.y_np)
|
|
x_clone = x.clone()
|
|
out = paddle.gcd_(x_clone, other=other)
|
|
np.testing.assert_allclose(out.numpy(), self.expected, rtol=1e-05)
|
|
paddle.enable_static()
|
|
|
|
def test_gcd_inplace_with_both_parameters_error(self):
|
|
paddle.disable_static()
|
|
x = paddle.to_tensor(self.x_np)
|
|
y = paddle.to_tensor(self.y_np)
|
|
other = paddle.to_tensor(self.y_np)
|
|
x_clone = x.clone()
|
|
with self.assertRaises(TypeError):
|
|
out = paddle.gcd_(x_clone, y, other=other)
|
|
paddle.enable_static()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
paddle.enable_static()
|
|
unittest.main()
|