diff --git a/python/paddle/fluid/tests/unittests/ipu/op_test_ipu.py b/python/paddle/fluid/tests/unittests/ipu/op_test_ipu.py index becaaa4173ae7c5b4c3d8356278cde02d90d2e90..90850b56aa657aeb55f83c75e799b31cc5ff6be2 100644 --- a/python/paddle/fluid/tests/unittests/ipu/op_test_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/op_test_ipu.py @@ -67,9 +67,6 @@ class IPUTest(unittest.TestCase): random.seed(cls.SEED) paddle.seed(cls.SEED) - # Enable paddle static graph mode - paddle.enable_static() - @classmethod def tearDownClass(cls): """Restore random seeds""" @@ -86,43 +83,37 @@ class IPUTest(unittest.TestCase): if flag.upper() in ['1', "TRUE"]: return True - # Decorator for static graph building - def static_graph(builder): - def wrapper(self, *args, **kwargs): - self.scope = paddle.static.Scope() - self.main_prog = paddle.static.Program() - self.startup_prog = paddle.static.Program() - self.main_prog.random_seed = self.SEED - self.startup_prog.random_seed = self.SEED - with paddle.static.scope_guard(self.scope): - with paddle.utils.unique_name.guard( - paddle.utils.unique_name.generate('')): - with paddle.static.program_guard(self.main_prog, - self.startup_prog): - builder(self, *args, **kwargs) +@unittest.skipIf(not paddle.is_compiled_with_ipu(), + "core is not compiled with IPU") +class IPUD2STest(IPUTest): - return wrapper - - # Cast a fp32 model to a full-fp16 model @classmethod - def cast_model_to_fp16(cls, main_program): - amp_list = paddle.static.amp.CustomOpLists() - amp_list.unsupported_list = {} - to_fp16_var_names = paddle.static.amp.cast_model_to_fp16( - main_program, amp_list, use_fp16_guard=False) - paddle.static.amp.cast_parameters_to_fp16( - paddle.CPUPlace(), - main_program, - to_fp16_var_names=to_fp16_var_names) + def setUpClass(cls): + super().setUpClass() + + # Disable paddle static graph mode + paddle.disable_static() + + def tearDown(self): + # Manual reset when using ipumodel + if self.use_ipumodel(): + paddle.framework.core.IpuBackend.get_instance().reset() +@unittest.skipIf(not paddle.is_compiled_with_ipu(), + "core is not compiled with IPU") class IPUOpTest(IPUTest): + """Base Class for single op unit tests using static graph on IPU. + """ @classmethod def setUpClass(cls): super().setUpClass() + # Enable paddle static graph mode + paddle.enable_static() + # Items that a op_tester needs cls.main_prog: paddle.static.Program = None cls.startup_prog: paddle.static.Program = None @@ -166,6 +157,36 @@ class IPUOpTest(IPUTest): self.is_training = False self.epoch = 1 + # Decorator for static graph building + def static_graph(builder): + + def wrapper(self, *args, **kwargs): + self.scope = paddle.static.Scope() + self.main_prog = paddle.static.Program() + self.startup_prog = paddle.static.Program() + self.main_prog.random_seed = self.SEED + self.startup_prog.random_seed = self.SEED + with paddle.static.scope_guard(self.scope): + with paddle.utils.unique_name.guard( + paddle.utils.unique_name.generate('')): + with paddle.static.program_guard(self.main_prog, + self.startup_prog): + builder(self, *args, **kwargs) + + return wrapper + + # Cast a fp32 model to a full-fp16 model + @classmethod + def cast_model_to_fp16(cls, main_program): + amp_list = paddle.static.amp.CustomOpLists() + amp_list.unsupported_list = {} + to_fp16_var_names = paddle.static.amp.cast_model_to_fp16( + main_program, amp_list, use_fp16_guard=False) + paddle.static.amp.cast_parameters_to_fp16( + paddle.CPUPlace(), + main_program, + to_fp16_var_names=to_fp16_var_names) + def run_op_test(self, exec_mode, ipu_strategy=None): # NOTE: some op has no inputs # if len(self.feed_list) == 0 or len(self.fetch_list) == 0: diff --git a/python/paddle/fluid/tests/unittests/ipu/test_activation_ops_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_activation_ops_ipu.py index 3c5a90afced72cef52151533d2e614ab438e1c71..97ee7a45e001ca118cf7b55d613722cda2f97fd7 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_activation_ops_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_activation_ops_ipu.py @@ -21,8 +21,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_affine_channel_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_affine_channel_op_ipu.py index 09a251585b381e12f06b503633dbc274269e1e97..836b99099ffe07085438656d61972626434b823c 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_affine_channel_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_affine_channel_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_arg_max_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_arg_max_op_ipu.py index 3612656cea354007db3456b74511d991061b6eff..078e744ae507d3e8b54b527f2e1e7a6da2607831 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_arg_max_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_arg_max_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_arg_min_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_arg_min_op_ipu.py index 181f2017173b44b1cf7121746fbdab841ec5b147..30c604901e877b75c2d3a48d7ac11d7beeff0adc 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_arg_min_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_arg_min_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_argsort_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_argsort_op_ipu.py index c1b585513d8b18be2cedeed5e38017e75932ab49..3f19da43c71c35adc102e14ccf1dcb421e8f19b7 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_argsort_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_argsort_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py index af03480fbf698dcd99aa864d972ce8f557415156..93cdaf018b400dc8557c70d6f516a3801c1fb2cc 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_avg_shard_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_avg_shard_ipu.py index 3f45bf485b8178ab0aac39b2e82230d732d1cf9e..a3be5458ad83f14cfe90e660d53de750d4bd9ef5 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_avg_shard_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_avg_shard_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_batch_norm_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_batch_norm_op_ipu.py index 2d2d3315439306efe10de393a926fdba9dd9dee4..08e5049a790ebfed3f4f543b94f893bd8c6cba41 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_batch_norm_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_batch_norm_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_binary_cross_entropy_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_binary_cross_entropy_op_ipu.py index 121755226ec3429bd42aaf53f4f24b615749663d..113412b8341105cb797d3093823ee1cc41269145 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_binary_cross_entropy_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_binary_cross_entropy_op_ipu.py @@ -21,8 +21,6 @@ from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest import paddle.nn.functional as F -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_bmm_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_bmm_op_ipu.py index 5a08774c236c2da3250ba966a8fb7a26f6c7fd3f..8ea20cebf07a3d6022c6d10a47ffecec6a4f767e 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_bmm_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_bmm_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_cast_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_cast_op_ipu.py index f361b779bb30b0ebfbaaeafaff6835360a810407..6799f4141a416178c5ac9d5865902f993b82c164 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_cast_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_cast_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_clip_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_clip_op_ipu.py index c61685e4a5e30a581789ae18caa348ed085699f0..a221ad617671d75d544a9b1240f3678efe35c2d2 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_clip_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_clip_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_concat_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_concat_op_ipu.py index d0160551b93bd09c26e0fc947ec4cb2049076fa4..733a5291cf50bd52c76c8ffa7ca1d527c569014c 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_concat_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_concat_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py index 64fdcc26636cf61183fe41adc713e70d51614cf3..6136bf34ffb671579934cb7447aff5e8958b5027 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py index 8fe7ee53ca2a85b7b778e54a4f2c2b0474f20a88..3fac45bbbd904869aca47086d31ca85b4bdf72a2 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py @@ -20,8 +20,6 @@ import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") class TestBase(IPUOpTest): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py index 5168a6db339dc7343a9dd396509e909109c27ca9..23ba121a07f2e12464ea732b15126cbf3d262a8b 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py @@ -12,16 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import tempfile import unittest import numpy as np import paddle -from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest - -SEED = 2022 +from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUD2STest class SimpleLayer(paddle.nn.Layer): @@ -48,22 +44,19 @@ class SimpleLayer(paddle.nn.Layer): return x -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") -class TestBase(IPUOpTest): +class TestBase(IPUD2STest): - @classmethod - def setUpClass(cls): - paddle.disable_static() - cls.save_path = tempfile.TemporaryDirectory() + def setUp(self): + super().setUp() + self.save_path = tempfile.TemporaryDirectory() - @classmethod - def tearDownClass(cls): - cls.save_path.cleanup() + def tearDown(self): + super().tearDown() + self.save_path.cleanup() def _test(self, use_ipu=False): - paddle.seed(SEED) - np.random.seed(SEED) + paddle.seed(self.SEED) + np.random.seed(self.SEED) model = SimpleLayer(use_ipu) specs = [ paddle.static.InputSpec(name="x", @@ -82,7 +75,7 @@ class TestBase(IPUOpTest): self.save_path, 'ipu' if use_ipu else 'cpu') if use_ipu: - device = paddle.set_device('ipu') + paddle.set_device('ipu') ipu_strategy = paddle.static.IpuStrategy() ipu_strategy.set_graph_config(num_ipus=1, is_training=True, @@ -92,15 +85,15 @@ class TestBase(IPUOpTest): ipu_strategy.set_optimizer(optim) data = data.astype(np.float16) + epochs = 100 result = [] - for epoch in range(100): + for _ in range(epochs): # ipu only needs call model() to do forward/backward/grad_update pred, loss = model(data, label) if not use_ipu: loss.backward() optim.step() optim.clear_grad() - result.append(loss) if use_ipu: @@ -108,11 +101,10 @@ class TestBase(IPUOpTest): paddle.save(model.state_dict(), model_path) paddle.save(optim.state_dict(), optim_path) - model.set_state_dict(paddle.load(model_path)) optim.set_state_dict(paddle.load(optim_path)) - for epoch in range(100): + for _ in range(epochs): # ipu only needs call model() to do forward/backward/grad_update pred, loss = model(data, label) if not use_ipu: @@ -130,7 +122,6 @@ class TestBase(IPUOpTest): def test_training(self): cpu_loss = self._test(False).flatten() ipu_loss = self._test(True).flatten() - self.assertTrue(np.allclose(ipu_loss, cpu_loss, atol=1e-2)) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py index 4cc9baea9f4b6c4b27114d6642f6ae64c6ed2214..7b581de222819a9ed69c7c6484aa5627fc4b0e17 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py @@ -12,21 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - import tempfile import unittest import numpy as np import paddle from paddle.fluid.dygraph.dygraph_to_static.program_translator import ProgramCache -from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest +from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUD2STest from paddle.jit import to_static from paddle.optimizer.lr import LRScheduler from functools import partial -SEED = 2022 - class SimpleLayer(paddle.nn.Layer): @@ -64,12 +60,9 @@ class SimpleLayer(paddle.nn.Layer): return x -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") -class TestBase(IPUOpTest): +class TestBase(IPUD2STest): def setUp(self): - paddle.disable_static() self.set_op_attrs() self.set_data_feed() @@ -87,14 +80,14 @@ class TestBase(IPUOpTest): use_identity_loss=use_ipu) def _test(self, use_ipu=False): - paddle.seed(SEED) - np.random.seed(SEED) + paddle.seed(self.SEED) + np.random.seed(self.SEED) model = self.create_model(use_ipu) optim = paddle.optimizer.Adam(learning_rate=0.01, parameters=model.parameters()) if use_ipu: - device = paddle.set_device('ipu') + paddle.set_device('ipu') ipu_strategy = paddle.static.IpuStrategy() ipu_strategy.set_graph_config(num_ipus=1, is_training=True, @@ -102,15 +95,15 @@ class TestBase(IPUOpTest): enable_manual_shard=False) ipu_strategy.set_optimizer(optim) + epochs = 100 result = [] - for epoch in range(100): + for _ in range(epochs): # ipu only needs call model() to do forward/backward/grad_update pred, loss = model(self.data, self.label) if not use_ipu: loss.backward() optim.step() optim.clear_grad() - result.append(loss) if use_ipu: @@ -121,23 +114,22 @@ class TestBase(IPUOpTest): def test_training(self): ipu_loss = self._test(True).flatten() cpu_loss = self._test(False).flatten() - self.assertTrue(np.allclose(ipu_loss, cpu_loss, atol=1e-4)) class TestSaveLoad(TestBase): - @classmethod - def setUpClass(cls): - cls.save_path = tempfile.TemporaryDirectory() + def setUp(self): + super().setUp() + self.save_path = tempfile.TemporaryDirectory() - @classmethod - def tearDownClass(cls): - cls.save_path.cleanup() + def tearDown(self): + super().tearDown() + self.save_path.cleanup() def _test(self, use_ipu=False): - paddle.seed(SEED) - np.random.seed(SEED) + paddle.seed(self.SEED) + np.random.seed(self.SEED) model = self.create_model(use_ipu) optim = paddle.optimizer.Adam(learning_rate=0.01, parameters=model.parameters()) @@ -147,7 +139,7 @@ class TestSaveLoad(TestBase): self.save_path, 'ipu' if use_ipu else 'cpu') if use_ipu: - device = paddle.set_device('ipu') + paddle.set_device('ipu') ipu_strategy = paddle.static.IpuStrategy() ipu_strategy.set_graph_config(num_ipus=1, is_training=True, @@ -155,15 +147,15 @@ class TestSaveLoad(TestBase): enable_manual_shard=False) ipu_strategy.set_optimizer(optim) + epochs = 100 result = [] - for epoch in range(100): + for _ in range(epochs): # ipu only needs call model() to do forward/backward/grad_update pred, loss = model(self.data, self.label) if not use_ipu: loss.backward() optim.step() optim.clear_grad() - result.append(loss) if use_ipu: @@ -171,18 +163,16 @@ class TestSaveLoad(TestBase): paddle.save(model.state_dict(), model_path) paddle.save(optim.state_dict(), optim_path) - model.set_state_dict(paddle.load(model_path)) optim.set_state_dict(paddle.load(optim_path)) - for epoch in range(100): + for _ in range(epochs): # ipu only needs call model() to do forward/backward/grad_update pred, loss = model(self.data, self.label) if not use_ipu: loss.backward() optim.step() optim.clear_grad() - result.append(loss) if use_ipu: @@ -191,9 +181,7 @@ class TestSaveLoad(TestBase): return np.array(result) -@unittest.skipIf(not paddle.is_compiled_with_ipu(), - "core is not compiled with IPU") -class TestPatch(IPUOpTest): +class TestPatch(IPUD2STest): def setUp(cls): paddle.disable_static()