未验证 提交 afd0c1db 编写于 作者: F freeliuzc 提交者: GitHub

Use tempfile to place all the temporary files. (#43392)

    使用 tempfile 替换临时文件,保证在单测结束后,所有临时文件都会被正常的删除,避免占用磁盘文件。
    此 PR 仅涉及单测修改,不影响现有功能。
    develop 分支修改在 PR 43376
上级 ed859054
...@@ -17,13 +17,21 @@ from __future__ import print_function ...@@ -17,13 +17,21 @@ from __future__ import print_function
import os import os
import sys import sys
import time import time
import tempfile
import subprocess import subprocess
import unittest import unittest
import numpy as np import numpy as np
import paddle import paddle
class TestDirectory(unittest.TestCase): class TestDirectory(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def get_import_command(self, module): def get_import_command(self, module):
paths = module.split('.') paths = module.split('.')
if len(paths) == 1: if len(paths) == 1:
...@@ -76,7 +84,7 @@ class TestDirectory(unittest.TestCase): ...@@ -76,7 +84,7 @@ class TestDirectory(unittest.TestCase):
'paddle.static.nn.spectral_norm', 'paddle.static.nn.embedding' 'paddle.static.nn.spectral_norm', 'paddle.static.nn.embedding'
] ]
import_file = 'run_import_modules.py' import_file = os.path.join(self.temp_dir.name, 'run_import_modules.py')
with open(import_file, "w") as wb: with open(import_file, "w") as wb:
for module in new_directory: for module in new_directory:
...@@ -137,7 +145,8 @@ class TestDirectory(unittest.TestCase): ...@@ -137,7 +145,8 @@ class TestDirectory(unittest.TestCase):
'paddle.declarative.spectral_norm', 'paddle.declarative.embedding' 'paddle.declarative.spectral_norm', 'paddle.declarative.embedding'
] ]
import_file = 'run_old_import_modules.py' import_file = os.path.join(self.temp_dir.name,
'run_old_import_modules.py')
with open(import_file, "w") as wb: with open(import_file, "w") as wb:
cmd_context_count = """ cmd_context_count = """
......
...@@ -176,4 +176,5 @@ class TestDygraphGNN(unittest.TestCase): ...@@ -176,4 +176,5 @@ class TestDygraphGNN(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -12,8 +12,11 @@ ...@@ -12,8 +12,11 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import tempfile
import unittest import unittest
import numpy as np import numpy as np
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.static import InputSpec from paddle.static import InputSpec
...@@ -157,6 +160,10 @@ class TestNetWithNonTensorSpec(unittest.TestCase): ...@@ -157,6 +160,10 @@ class TestNetWithNonTensorSpec(unittest.TestCase):
self.out_num = 16 self.out_num = 16
self.x_spec = paddle.static.InputSpec([-1, 16], name='x') self.x_spec = paddle.static.InputSpec([-1, 16], name='x')
self.x = paddle.randn([4, 16]) self.x = paddle.randn([4, 16])
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
...@@ -179,7 +186,7 @@ class TestNetWithNonTensorSpec(unittest.TestCase): ...@@ -179,7 +186,7 @@ class TestNetWithNonTensorSpec(unittest.TestCase):
self.check_result(specs, 'list') self.check_result(specs, 'list')
def check_result(self, specs, path): def check_result(self, specs, path):
path = './net_non_tensor_' + path path = os.path.join(self.temp_dir.name, './net_non_tensor_', path)
net = NetWithNonTensorSpec(self.in_num, self.out_num) net = NetWithNonTensorSpec(self.in_num, self.out_num)
net.eval() net.eval()
...@@ -215,7 +222,7 @@ class TestNetWithNonTensorSpec(unittest.TestCase): ...@@ -215,7 +222,7 @@ class TestNetWithNonTensorSpec(unittest.TestCase):
net = paddle.jit.to_static(net, input_spec=specs) net = paddle.jit.to_static(net, input_spec=specs)
net.eval() net.eval()
path = './net_twice' path = os.path.join(self.temp_dir.name, './net_twice')
# NOTE: check input_specs_compatible # NOTE: check input_specs_compatible
new_specs = [self.x_spec, True, "bn", 10] new_specs = [self.x_spec, True, "bn", 10]
...@@ -259,6 +266,10 @@ class TestNetWithNonTensorSpecWithPrune(unittest.TestCase): ...@@ -259,6 +266,10 @@ class TestNetWithNonTensorSpecWithPrune(unittest.TestCase):
self.y_spec = paddle.static.InputSpec([16], name='y') self.y_spec = paddle.static.InputSpec([16], name='y')
self.x = paddle.randn([4, 16]) self.x = paddle.randn([4, 16])
self.y = paddle.randn([16]) self.y = paddle.randn([16])
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
...@@ -266,7 +277,7 @@ class TestNetWithNonTensorSpecWithPrune(unittest.TestCase): ...@@ -266,7 +277,7 @@ class TestNetWithNonTensorSpecWithPrune(unittest.TestCase):
def test_non_tensor_with_prune(self): def test_non_tensor_with_prune(self):
specs = [self.x_spec, self.y_spec, True] specs = [self.x_spec, self.y_spec, True]
path = './net_non_tensor_prune_' path = os.path.join(self.temp_dir.name, './net_non_tensor_prune_')
net = NetWithNonTensorSpecPrune(self.in_num, self.out_num) net = NetWithNonTensorSpecPrune(self.in_num, self.out_num)
net.eval() net.eval()
......
...@@ -12,14 +12,18 @@ ...@@ -12,14 +12,18 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import paddle import os
import unittest import json
import numpy import numpy
import paddle.nn.functional as F import unittest
import tempfile import tempfile
import warnings import warnings
import json
import os import paddle
import paddle.nn.functional as F
from paddle.fluid.framework import _enable_legacy_dygraph
_enable_legacy_dygraph()
class SimpleNet(paddle.nn.Layer): class SimpleNet(paddle.nn.Layer):
......
...@@ -14,6 +14,9 @@ ...@@ -14,6 +14,9 @@
from __future__ import print_function from __future__ import print_function
import os
import numpy as np
import tempfile
import unittest import unittest
import paddle.fluid as fluid import paddle.fluid as fluid
...@@ -21,12 +24,11 @@ import paddle.fluid.framework as framework ...@@ -21,12 +24,11 @@ import paddle.fluid.framework as framework
import paddle.fluid.optimizer as optimizer import paddle.fluid.optimizer as optimizer
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.compat as cpt import paddle.compat as cpt
import numpy as np
from paddle.fluid.backward import append_backward from paddle.fluid.backward import append_backward
from paddle.fluid.framework import Program, program_guard, convert_np_dtype_to_dtype_ from paddle.fluid.framework import Program, program_guard, convert_np_dtype_to_dtype_
import paddle import paddle
from paddle.io import Dataset from paddle.io import Dataset
import numpy
paddle.enable_static() paddle.enable_static()
...@@ -1121,9 +1123,15 @@ class TestMasterWeightSaveForFP16(unittest.TestCase): ...@@ -1121,9 +1123,15 @@ class TestMasterWeightSaveForFP16(unittest.TestCase):
Master weights will be saved by optimizer::state_dict. Master weights will be saved by optimizer::state_dict.
''' '''
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
def tearDown(self):
self.temp_dir.cleanup()
def check_with_opt_state_dict(self, use_save_load=True): def check_with_opt_state_dict(self, use_save_load=True):
paddle.seed(100) paddle.seed(100)
numpy.random.seed(100) np.random.seed(100)
class SimpleNet(paddle.nn.Layer): class SimpleNet(paddle.nn.Layer):
def __init__(self, input_size, output_size): def __init__(self, input_size, output_size):
...@@ -1147,8 +1155,8 @@ class TestMasterWeightSaveForFP16(unittest.TestCase): ...@@ -1147,8 +1155,8 @@ class TestMasterWeightSaveForFP16(unittest.TestCase):
self.num_samples = num_samples self.num_samples = num_samples
def __getitem__(self, idx): def __getitem__(self, idx):
data = numpy.random.random([input_size]).astype('float16') data = np.random.random([input_size]).astype('float16')
label = numpy.random.random([output_size]).astype('float16') label = np.random.random([output_size]).astype('float16')
return data, label return data, label
def __len__(self): def __len__(self):
...@@ -1182,10 +1190,12 @@ class TestMasterWeightSaveForFP16(unittest.TestCase): ...@@ -1182,10 +1190,12 @@ class TestMasterWeightSaveForFP16(unittest.TestCase):
optimizer.clear_grad(set_to_zero=False) optimizer.clear_grad(set_to_zero=False)
if use_save_load and i == 5: if use_save_load and i == 5:
paddle.save(model.state_dict(), "model.pdparams") model_path = os.path.join(self.temp_dir.name, "model.pdparams")
paddle.save(optimizer.state_dict(), "opt.pdopt") optimizer_path = os.path.join(self.temp_dir.name, "opt.pdopt")
model.set_state_dict(paddle.load("model.pdparams")) paddle.save(model.state_dict(), model_path)
optimizer.set_state_dict(paddle.load("opt.pdopt")) paddle.save(optimizer.state_dict(), optimizer_path)
model.set_state_dict(paddle.load(model_path))
optimizer.set_state_dict(paddle.load(optimizer_path))
return loss.numpy() return loss.numpy()
......
...@@ -12,13 +12,14 @@ ...@@ -12,13 +12,14 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import paddle import os
import unittest import json
import numpy as np import numpy as np
import unittest
import tempfile import tempfile
import warnings import warnings
import json
import os import paddle
class SimpleNet(paddle.nn.Layer): class SimpleNet(paddle.nn.Layer):
......
...@@ -13,12 +13,14 @@ ...@@ -13,12 +13,14 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import tempfile
import unittest
import numpy as np import numpy as np
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import unittest
import paddle.nn as nn import paddle.nn as nn
import os
class SimpleFCLayer(nn.Layer): class SimpleFCLayer(nn.Layer):
...@@ -189,11 +191,15 @@ class TestTracedLayerSaveInferenceModel(unittest.TestCase): ...@@ -189,11 +191,15 @@ class TestTracedLayerSaveInferenceModel(unittest.TestCase):
"""test save_inference_model will automaticlly create non-exist dir""" """test save_inference_model will automaticlly create non-exist dir"""
def setUp(self): def setUp(self):
self.save_path = "./nonexist_dir/fc" self.temp_dir = tempfile.TemporaryDirectory()
self.save_path = os.path.join(self.temp_dir.name, "./nonexist_dir/fc")
import shutil import shutil
if os.path.exists(os.path.dirname(self.save_path)): if os.path.exists(os.path.dirname(self.save_path)):
shutil.rmtree(os.path.dirname(self.save_path)) shutil.rmtree(os.path.dirname(self.save_path))
def tearDown(self):
self.temp_dir.cleanup()
def test_mkdir_when_input_path_non_exist(self): def test_mkdir_when_input_path_non_exist(self):
if fluid.framework.in_dygraph_mode(): if fluid.framework.in_dygraph_mode():
return return
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册