Skip to content
Closed
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
1 change: 0 additions & 1 deletion test/deprecated/legacy_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ set_tests_properties(test_dataloader_keep_order_deprecated PROPERTIES TIMEOUT
120)
set_tests_properties(test_dataloader_unkeep_order_deprecated PROPERTIES TIMEOUT
120)
set_tests_properties(test_reader_reset PROPERTIES TIMEOUT 120)
set_tests_properties(test_split_program PROPERTIES TIMEOUT 120)
set_tests_properties(test_uniform_random_op_deprecated PROPERTIES TIMEOUT 60)

Expand Down
97 changes: 49 additions & 48 deletions test/deprecated/legacy_test/test_reader_reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,55 +42,56 @@ def setUp(self):
self.prepare_data()

def main(self, with_double_buffer):
main_prog = base.Program()
startup_prog = base.Program()

with base.program_guard(main_prog, startup_prog):
image = paddle.static.data(
name='image', shape=[-1] + self.ins_shape, dtype='float32'
)
label = paddle.static.data(
name='label', shape=[-1, 1], dtype='int64'
with paddle.pir_utils.OldIrGuard():
main_prog = base.Program()
startup_prog = base.Program()

with base.program_guard(main_prog, startup_prog):
image = paddle.static.data(
name='image', shape=[-1] + self.ins_shape, dtype='float32'
)
label = paddle.static.data(
name='label', shape=[-1, 1], dtype='int64'
)
data_reader_handle = base.io.PyReader(
feed_list=[image, label],
capacity=16,
iterable=False,
use_double_buffer=with_double_buffer,
)
fetch_list = [image.name, label.name]

place = base.CUDAPlace(0) if self.use_cuda else base.CPUPlace()
exe = base.Executor(place)
exe.run(startup_prog)

data_reader_handle.decorate_sample_list_generator(
paddle.batch(self.prepare_data(), batch_size=self.batch_size)
)
data_reader_handle = base.io.PyReader(
feed_list=[image, label],
capacity=16,
iterable=False,
use_double_buffer=with_double_buffer,
)
fetch_list = [image.name, label.name]

place = base.CUDAPlace(0) if self.use_cuda else base.CPUPlace()
exe = base.Executor(place)
exe.run(startup_prog)

data_reader_handle.decorate_sample_list_generator(
paddle.batch(self.prepare_data(), batch_size=self.batch_size)
)

train_cp = compiler.CompiledProgram(main_prog)

batch_id = 0
pass_count = 0
while pass_count < self.test_pass_num:
data_reader_handle.start()
try:
while True:
data_val, label_val = exe.run(
train_cp, fetch_list=fetch_list, return_numpy=True
)
ins_num = data_val.shape[0]
broadcasted_label = np.ones(
(ins_num,) + tuple(self.ins_shape)
) * label_val.reshape((ins_num, 1))
self.assertEqual(data_val.all(), broadcasted_label.all())
batch_id += 1
except base.core.EOFException:
data_reader_handle.reset()
pass_count += 1
self.assertEqual(pass_count * self.batch_num, batch_id)

self.assertEqual(pass_count, self.test_pass_num)

train_cp = compiler.CompiledProgram(main_prog)

batch_id = 0
pass_count = 0
while pass_count < self.test_pass_num:
data_reader_handle.start()
try:
while True:
data_val, label_val = exe.run(
train_cp, fetch_list=fetch_list, return_numpy=True
)
ins_num = data_val.shape[0]
broadcasted_label = np.ones(
(ins_num,) + tuple(self.ins_shape)
) * label_val.reshape((ins_num, 1))
self.assertEqual(data_val.all(), broadcasted_label.all())
batch_id += 1
except base.core.EOFException:
data_reader_handle.reset()
pass_count += 1
self.assertEqual(pass_count * self.batch_num, batch_id)

self.assertEqual(pass_count, self.test_pass_num)

def test_all(self):
self.main(with_double_buffer=False)
Expand Down
1 change: 1 addition & 0 deletions test/legacy_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ set_tests_properties(test_translated_layer PROPERTIES TIMEOUT 120)
set_tests_properties(test_pir_translated_layer PROPERTIES TIMEOUT 120)
set_tests_properties(test_mean_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_dataloader PROPERTIES TIMEOUT 120)
set_tests_properties(test_reader_reset PROPERTIES TIMEOUT 120)
set_tests_properties(test_pool3d_api PROPERTIES TIMEOUT 120)

set_tests_properties(test_dataset_cifar PROPERTIES TIMEOUT 120)
Expand Down
180 changes: 180 additions & 0 deletions test/legacy_test/test_real_imag_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# 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.

import unittest

import numpy as np
from op_test import OpTest

import paddle
from paddle import base, static
from paddle.pir_utils import test_with_pir_api

numpy_apis = {
"real": np.real,
"imag": np.imag,
}

paddle_apis = {
"real": paddle.real,
"imag": paddle.imag,
}


class TestRealOp(OpTest):
def setUp(self):
# switch to static
paddle.enable_static()
# op test attrs
self.op_type = "real"
self.python_api = paddle.real
self.dtype = np.float64
self.init_input_output()
# backward attrs
self.init_grad_input_output()

def init_input_output(self):
self.inputs = {
'X': np.random.random((20, 5)).astype(self.dtype)
+ 1j * np.random.random((20, 5)).astype(self.dtype)
}
self.outputs = {'Out': numpy_apis[self.op_type](self.inputs['X'])}

def init_grad_input_output(self):
self.grad_out = np.ones((20, 5), self.dtype)
self.grad_x = np.real(self.grad_out) + 1j * np.zeros(
self.grad_out.shape
)

def test_check_output(self):
self.check_output(check_pir=True)

def test_check_grad(self):
self.check_grad(
['X'],
'Out',
user_defined_grads=[self.grad_x],
user_defined_grad_outputs=[self.grad_out],
check_pir=True,
)


class TestImagOp(TestRealOp):
def setUp(self):
# switch to static
paddle.enable_static()
# op test attrs
self.op_type = "imag"
self.python_api = paddle.imag
self.dtype = np.float64
self.init_input_output()
# backward attrs
self.init_grad_input_output()

def init_grad_input_output(self):
self.grad_out = np.ones((20, 5), self.dtype)
self.grad_x = np.zeros(self.grad_out.shape) + 1j * np.real(
self.grad_out
)


class TestRealAPI(unittest.TestCase):
def setUp(self):
# switch to static
paddle.enable_static()
# prepare test attrs
self.api = "real"
self.dtypes = ["complex64", "complex128"]
self.places = [paddle.CPUPlace()]
if paddle.is_compiled_with_cuda():
self.places.append(paddle.CUDAPlace(0))
self._shape = [2, 20, 2, 3]

@test_with_pir_api
def test_in_static_mode(self):
def init_input_output(dtype):
input = np.random.random(self._shape).astype(
dtype
) + 1j * np.random.random(self._shape).astype(dtype)
return {'x': input}, numpy_apis[self.api](input)

for dtype in self.dtypes:
input_dict, np_res = init_input_output(dtype)
for place in self.places:
with static.program_guard(static.Program()):
x = static.data(name="x", shape=self._shape, dtype=dtype)
out = paddle_apis[self.api](x)

exe = static.Executor(place)
out_value = exe.run(feed=input_dict, fetch_list=[out])
np.testing.assert_array_equal(np_res, out_value[0])

def test_in_dynamic_mode(self):
for dtype in self.dtypes:
input = np.random.random(self._shape).astype(
dtype
) + 1j * np.random.random(self._shape).astype(dtype)
np_res = numpy_apis[self.api](input)
for place in self.places:
# it is more convenient to use `guard` than `enable/disable_**` here
with base.dygraph.guard(place):
input_t = paddle.to_tensor(input)
res = paddle_apis[self.api](input_t).numpy()
np.testing.assert_array_equal(np_res, res)
res_t = (
input_t.real().numpy()
if self.api == "real"
else input_t.imag().numpy()
)
np.testing.assert_array_equal(np_res, res_t)

def test_name_argument(self):
with paddle.pir_utils.OldIrGuard():
with static.program_guard(static.Program()):
x = static.data(name="x", shape=self._shape, dtype=self.dtypes[0])
out = paddle_apis[self.api](x, name="real_res")
self.assertTrue("real_res" in out.name)

@test_with_pir_api
def test_dtype_static_error(self):
# in static graph mode
with self.assertRaises(TypeError):
with static.program_guard(static.Program()):
x = static.data(name="x", shape=self._shape, dtype="float32")
out = paddle_apis[self.api](x, name="real_res")

def test_dtype_dygraph_error(self):
# in dynamic mode
with self.assertRaises(RuntimeError):
with base.dygraph.guard():
input = np.random.random(self._shape).astype("float32")
input_t = paddle.to_tensor(input)
res = paddle_apis[self.api](input_t)


class TestImagAPI(TestRealAPI):
def setUp(self):
# switch to static
paddle.enable_static()
# prepare test attrs
self.api = "imag"
self.dtypes = ["complex64", "complex128"]
self.places = [paddle.CPUPlace()]
if paddle.is_compiled_with_cuda():
self.places.append(paddle.CUDAPlace(0))
self._shape = [2, 20, 2, 3]


if __name__ == "__main__":
unittest.main()