From 24843fcb57571daadc7fc7b8f026c8757b5a9cfa Mon Sep 17 00:00:00 2001 From: Qi Li Date: Thu, 16 Jun 2022 11:39:29 +0800 Subject: [PATCH] [Cherry-pick] Fix ut tempfile v23 (#43387) * fix unit test temp file, test=develop (#43155) * add cleanup code, test=develop (#43305) --- .../tests/unittests/ipu/test_weight_decay_ipu.py | 9 ++++++++- .../paddle/fluid/tests/unittests/test_load_op_xpu.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_weight_decay_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_weight_decay_ipu.py index 5e652ce4833..b0d55d42809 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_weight_decay_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_weight_decay_ipu.py @@ -14,7 +14,9 @@ import unittest +import os import numpy as np +import tempfile import paddle import paddle.static from paddle.fluid.tests.unittests.ipu.op_test_ipu import IPUOpTest @@ -29,6 +31,11 @@ class TestBase(IPUOpTest): self.set_data_feed() self.set_feed_attr() self.set_attrs() + self.temp_dir = tempfile.TemporaryDirectory() + self.model_path = os.path.join(self.temp_dir.name, "weight_decay") + + def tearDown(self): + self.temp_dir.cleanup() def set_atol(self): self.atol = 1e-6 @@ -83,7 +90,7 @@ class TestBase(IPUOpTest): place = paddle.CPUPlace() exe = paddle.static.Executor(place) exe.run(startup_prog) - paddle.static.save(main_prog, "weight_decay") + paddle.static.save(main_prog, self.model_path) if run_ipu: feed_list = [image.name] diff --git a/python/paddle/fluid/tests/unittests/test_load_op_xpu.py b/python/paddle/fluid/tests/unittests/test_load_op_xpu.py index a5af6871be4..f89ea988d53 100644 --- a/python/paddle/fluid/tests/unittests/test_load_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/test_load_op_xpu.py @@ -16,6 +16,8 @@ from __future__ import print_function import unittest import numpy as np +import os +import tempfile from op_test import OpTest, randomize_probability import paddle.fluid as fluid import paddle.fluid.layers as layers @@ -29,6 +31,8 @@ class TestLoadOpXpu(unittest.TestCase): """ def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + self.model_path = os.path.join(self.temp_dir.name, "model") self.ones = np.ones((4, 4)).astype('float32') main_prog = fluid.Program() start_prog = fluid.Program() @@ -44,14 +48,17 @@ class TestLoadOpXpu(unittest.TestCase): exe = fluid.Executor(fluid.XPUPlace(0)) exe.run(start_prog) fluid.io.save_persistables( - exe, dirname="./model", main_program=main_prog) + exe, dirname=self.model_path, main_program=main_prog) + + def tearDown(self): + self.temp_dir.cleanup() def test_load_xpu(self): main_prog = fluid.Program() start_prog = fluid.Program() with fluid.program_guard(main_prog, start_prog): var = layers.create_tensor(dtype='float32') - layers.load(var, file_path='./model/w') + layers.load(var, file_path=self.model_path + '/w') exe = fluid.Executor(fluid.XPUPlace(0)) exe.run(start_prog) -- GitLab