|
|
|
|
@ -16,10 +16,8 @@ import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
from op_test import OpTest
|
|
|
|
|
import paddle
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
import paddle.fluid.core as core
|
|
|
|
|
from paddle.fluid.op import Operator
|
|
|
|
|
from paddle.fluid import Program, program_guard
|
|
|
|
|
from paddle import Program, program_guard
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_randperm_out(n, data_np):
|
|
|
|
|
@ -36,8 +34,11 @@ def error_msg(data_np):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_dtype(dtype_str):
|
|
|
|
|
dtype_str_list = ["int32", "int64"]
|
|
|
|
|
dtype_num_list = [2, 3]
|
|
|
|
|
dtype_str_list = ["int32", "int64", "float32", "float64"]
|
|
|
|
|
dtype_num_list = [
|
|
|
|
|
core.VarDesc.VarType.INT32, core.VarDesc.VarType.INT64,
|
|
|
|
|
core.VarDesc.VarType.FP32, core.VarDesc.VarType.FP64
|
|
|
|
|
]
|
|
|
|
|
assert dtype_str in dtype_str_list, dtype_str + \
|
|
|
|
|
" should in " + str(dtype_str_list)
|
|
|
|
|
return dtype_num_list[dtype_str_list.index(dtype_str)]
|
|
|
|
|
@ -50,8 +51,6 @@ class TestRandpermOp(OpTest):
|
|
|
|
|
self.op_type = "randperm"
|
|
|
|
|
self.n = 200
|
|
|
|
|
self.dtype = "int64"
|
|
|
|
|
self.device = None
|
|
|
|
|
self.seed = 0
|
|
|
|
|
|
|
|
|
|
self.inputs = {}
|
|
|
|
|
self.outputs = {"Out": np.zeros((self.n)).astype(self.dtype)}
|
|
|
|
|
@ -59,8 +58,6 @@ class TestRandpermOp(OpTest):
|
|
|
|
|
self.attrs = {
|
|
|
|
|
"n": self.n,
|
|
|
|
|
"dtype": convert_dtype(self.dtype),
|
|
|
|
|
"device": self.device,
|
|
|
|
|
"seed": self.seed,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def init_attrs(self):
|
|
|
|
|
@ -75,100 +72,60 @@ class TestRandpermOp(OpTest):
|
|
|
|
|
check_randperm_out(self.n, out_np), msg=error_msg(out_np))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermOp_attr_n(TestRandpermOp):
|
|
|
|
|
""" Test randperm op for attr n. """
|
|
|
|
|
|
|
|
|
|
class TestRandpermOpN(TestRandpermOp):
|
|
|
|
|
def init_attrs(self):
|
|
|
|
|
self.n = 10000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermOp_attr_int32(TestRandpermOp):
|
|
|
|
|
""" Test randperm op for attr int32 dtype. """
|
|
|
|
|
|
|
|
|
|
class TestRandpermOpInt32(TestRandpermOp):
|
|
|
|
|
def init_attrs(self):
|
|
|
|
|
self.dtype = "int32"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermOp_attr_device_cpu(TestRandpermOp):
|
|
|
|
|
""" Test randperm op for cpu device. """
|
|
|
|
|
|
|
|
|
|
class TestRandpermOpFloat32(TestRandpermOp):
|
|
|
|
|
def init_attrs(self):
|
|
|
|
|
self.device = "cpu"
|
|
|
|
|
self.dtype = "float32"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermOp_attr_device_gpu(TestRandpermOp):
|
|
|
|
|
""" Test randperm op for gpu device. """
|
|
|
|
|
|
|
|
|
|
class TestRandpermOpFloat64(TestRandpermOp):
|
|
|
|
|
def init_attrs(self):
|
|
|
|
|
self.device = "gpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermOp_attr_seed(TestRandpermOp):
|
|
|
|
|
""" Test randperm op for attr seed. """
|
|
|
|
|
|
|
|
|
|
def init_attrs(self):
|
|
|
|
|
self.seed = 10
|
|
|
|
|
self.dtype = "float64"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermOpError(unittest.TestCase):
|
|
|
|
|
""" Test randperm op for raise error. """
|
|
|
|
|
|
|
|
|
|
def test_errors(self):
|
|
|
|
|
main_prog = Program()
|
|
|
|
|
start_prog = Program()
|
|
|
|
|
with program_guard(main_prog, start_prog):
|
|
|
|
|
with program_guard(Program(), Program()):
|
|
|
|
|
self.assertRaises(ValueError, paddle.randperm, -3)
|
|
|
|
|
self.assertRaises(TypeError, paddle.randperm, 10, 'int8')
|
|
|
|
|
|
|
|
|
|
def test_Variable():
|
|
|
|
|
out = np.arange(10)
|
|
|
|
|
paddle.randperm(n=10, out=out)
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_Variable)
|
|
|
|
|
class TestRandpermAPI(unittest.TestCase):
|
|
|
|
|
def test_out(self):
|
|
|
|
|
n = 10
|
|
|
|
|
place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda(
|
|
|
|
|
) else paddle.CPUPlace()
|
|
|
|
|
with program_guard(Program(), Program()):
|
|
|
|
|
x1 = paddle.randperm(n)
|
|
|
|
|
x2 = paddle.randperm(n, 'float32')
|
|
|
|
|
|
|
|
|
|
def test_value():
|
|
|
|
|
paddle.randperm(n=-3)
|
|
|
|
|
exe = paddle.Executor(place)
|
|
|
|
|
res = exe.run(fetch_list=[x1, x2])
|
|
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, test_value)
|
|
|
|
|
self.assertEqual(res[0].dtype, np.int64)
|
|
|
|
|
self.assertEqual(res[1].dtype, np.float32)
|
|
|
|
|
self.assertTrue(check_randperm_out(n, res[0]))
|
|
|
|
|
self.assertTrue(check_randperm_out(n, res[1]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermOp_attr_out(unittest.TestCase):
|
|
|
|
|
""" Test randperm op for attr out. """
|
|
|
|
|
|
|
|
|
|
def test_attr_tensor_API(self):
|
|
|
|
|
startup_program = fluid.Program()
|
|
|
|
|
train_program = fluid.Program()
|
|
|
|
|
with fluid.program_guard(train_program, startup_program):
|
|
|
|
|
n = 10
|
|
|
|
|
data_1 = fluid.layers.fill_constant([n], "int64", 3)
|
|
|
|
|
paddle.randperm(n=n, out=data_1)
|
|
|
|
|
|
|
|
|
|
data_2 = paddle.randperm(n=n, dtype="int32", device="cpu")
|
|
|
|
|
|
|
|
|
|
place = fluid.CPUPlace()
|
|
|
|
|
if fluid.core.is_compiled_with_cuda():
|
|
|
|
|
place = fluid.CUDAPlace(0)
|
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
|
|
|
|
|
|
exe.run(startup_program)
|
|
|
|
|
outs = exe.run(train_program, fetch_list=[data_1, data_2])
|
|
|
|
|
|
|
|
|
|
out_np = np.array(outs[0])
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
check_randperm_out(n, out_np), msg=error_msg(out_np))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRandpermDygraphMode(unittest.TestCase):
|
|
|
|
|
def test_check_output(self):
|
|
|
|
|
with fluid.dygraph.guard():
|
|
|
|
|
class TestRandpermImperative(unittest.TestCase):
|
|
|
|
|
def test_out(self):
|
|
|
|
|
with paddle.imperative.guard():
|
|
|
|
|
n = 10
|
|
|
|
|
data_1 = paddle.randperm(n, dtype="int64")
|
|
|
|
|
data_1_np = data_1.numpy()
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
check_randperm_out(n, data_1_np), msg=error_msg(data_1_np))
|
|
|
|
|
|
|
|
|
|
data_2 = paddle.randperm(n, dtype="int32", device="cpu")
|
|
|
|
|
data_2_np = data_2.numpy()
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
check_randperm_out(n, data_2_np), msg=error_msg(data_2_np))
|
|
|
|
|
for dtype in ['int32', np.int64, 'float32', 'float64']:
|
|
|
|
|
data_p = paddle.randperm(n, dtype)
|
|
|
|
|
data_np = data_p.numpy()
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
check_randperm_out(n, data_np), msg=error_msg(data_np))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|