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

Fix the argsort and sort api for the paddle api2.0 #25514

Merged
merged 8 commits into from
Jul 29, 2020
88 changes: 57 additions & 31 deletions python/paddle/fluid/tests/unittests/test_argsort_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import unittest
import paddle
import paddle.fluid as fluid
import paddle.imperative as imperative
import paddle.fluid.layers as layers
import numpy as np
import six
Expand Down Expand Up @@ -321,58 +322,83 @@ def init_direction(self):
self.descending = True


class TestSortOnCPU(TestArgsortOpCPU):
def init_place(self):
class TestArgsortErrorOnCPU(unittest.TestCase):
def setUp(self):
self.place = core.CPUPlace()

def test_out(self):
self.init_place()
with fluid.program_guard(fluid.Program()):
input = fluid.data(name="input", shape=[2, 3, 4], dtype="float32")

res = fluid.data(name="output", shape=[2, 3, 4], dtype="float32")
output = paddle.tensor.sort(input=input, out=res)

exe = fluid.Executor(self.place)
data = np.array(
[[[5, 8, 9, 5], [0, 0, 1, 7], [6, 9, 2, 4]],
[[5, 2, 4, 2], [4, 7, 7, 9], [1, 7, 0, 6]]],
dtype='float32')
result = exe.run(feed={'input': data}, fetch_list=[res, output[0]])
def test_error(self):
def test_fluid_var_type():
with fluid.program_guard(fluid.Program()):
x = [1]
output = fluid.layers.argsort(input=x)
self.assertRaises(TypeError, test_fluid_var_type)

self.assertEqual((result[0] == result[1]).all(), True)
def test_paddle_var_type():
with fluid.program_guard(fluid.Program()):
x = [1]
output = paddle.argsort(input=x)
self.assertRaises(TypeError, test_paddle_var_type)


class TestSortOnGPU(TestSortOnCPU):
def init_place(self):
class TestArgsortErrorOnGPU(TestArgsortErrorOnCPU):
def setUp(self):
if core.is_compiled_with_cuda():
self.place = core.CUDAPlace(0)
else:
self.place = core.CPUPlace()


class TestArgsortErrorOnCPU(unittest.TestCase):
def init_place(self):
self.place = core.CPUPlace()
class TestArgsort(unittest.TestCase):
def setUp(self):
if core.is_compiled_with_cuda():
self.place = core.CUDAPlace(0)
else:
self.place = core.CPUPlace()
self.data = np.random.rand(2, 3, 4).astype("float32")

def test_error(self):
self.init_place()
def test_api_0(self):
with fluid.program_guard(fluid.Program()):
input = fluid.data(name="input", shape=[2, 3, 4], dtype="float32")
output = paddle.argsort(x=input)
exe = fluid.Executor(self.place)
result, = exe.run(feed={'input': self.data}, fetch_list=[output])
np_result = np.argsort(self.data)
self.assertEqual((result == np_result).all(), True)

def test_input_type():
x = [1]
output = fluid.layers.argsort(input=x)

self.assertRaises(TypeError, test_input_type)
def test_api_1(self):
with fluid.program_guard(fluid.Program()):
input = fluid.data(name="input", shape=[2, 3, 4], dtype="float32")
output = paddle.argsort(x=input, axis=1)
exe = fluid.Executor(self.place)
result, = exe.run(feed={'input': self.data}, fetch_list=[output])
np_result = np.argsort(self.data, axis=1)
self.assertEqual((result == np_result).all(), True)


class TestArgsortErrorOnGPU(TestArgsortErrorOnCPU):
def init_place(self):
class TestArgsortDygraph(unittest.TestCase):
def setUp(self):
self.input_data = np.random.rand(10, 10)
if core.is_compiled_with_cuda():
self.place = core.CUDAPlace(0)
else:
self.place = core.CPUPlace()

def test_api_0(self):
with imperative.guard(self.place):
var_x = imperative.to_variable(self.input_data)
out = paddle.argsort(var_x)
self.assertEqual((np.argsort(self.input_data) == out.numpy()).all(),
True)

def test_api_1(self):
with imperative.guard(self.place):
var_x = imperative.to_variable(self.input_data)
out = paddle.argsort(var_x, axis=-1)
self.assertEqual(
(np.argsort(
self.input_data, axis=-1) == out.numpy()).all(),
True)


if __name__ == "__main__":
unittest.main()
88 changes: 88 additions & 0 deletions python/paddle/fluid/tests/unittests/test_sort_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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 paddle
import paddle.fluid as fluid
import paddle.imperative as imperative
import paddle.fluid.layers as layers
import numpy as np
import six
import paddle.fluid.core as core


class TestSortOnCPU(unittest.TestCase):
def setUp(self):
self.place = core.CPUPlace()

def test_api_0(self):
with fluid.program_guard(fluid.Program()):
input = fluid.data(name="input", shape=[2, 3, 4], dtype="float32")
output = paddle.sort(x=input)
exe = fluid.Executor(self.place)
data = np.array(
[[[5, 8, 9, 5], [0, 0, 1, 7], [6, 9, 2, 4]],
[[5, 2, 4, 2], [4, 7, 7, 9], [1, 7, 0, 6]]],
dtype='float32')
result, = exe.run(feed={'input': data}, fetch_list=[output[0]])
np_result = np.sort(result)
self.assertEqual((result == np_result).all(), True)

def test_api_1(self):
with fluid.program_guard(fluid.Program()):
input = fluid.data(name="input", shape=[2, 3, 4], dtype="float32")
output = paddle.sort(x=input, axis=1)
exe = fluid.Executor(self.place)
data = np.array(
[[[5, 8, 9, 5], [0, 0, 1, 7], [6, 9, 2, 4]],
[[5, 2, 4, 2], [4, 7, 7, 9], [1, 7, 0, 6]]],
dtype='float32')
result, = exe.run(feed={'input': data}, fetch_list=[output[0]])
np_result = np.sort(result, axis=1)
self.assertEqual((result == np_result).all(), True)


class TestSortOnGPU(TestSortOnCPU):
def init_place(self):
if core.is_compiled_with_cuda():
self.place = core.CUDAPlace(0)
else:
self.place = core.CPUPlace()


class TestSortDygraph(unittest.TestCase):
def setUp(self):
self.input_data = np.random.rand(10, 10)
if core.is_compiled_with_cuda():
self.place = core.CUDAPlace(0)
else:
self.place = core.CPUPlace()

def test_api_0(self):
with imperative.guard(self.place):
var_x = imperative.to_variable(self.input_data)
out = paddle.sort(var_x)
self.assertEqual((np.sort(self.input_data) == out[0].numpy()).all(),
True)

def test_api_1(self):
with imperative.guard(self.place):
var_x = imperative.to_variable(self.input_data)
out = paddle.sort(var_x, axis=-1)
self.assertEqual(
(np.sort(
self.input_data, axis=-1) == out[0].numpy()).all(),
True)
Loading