From 8f18274f755968ac385b308dbb19a2a60d548290 Mon Sep 17 00:00:00 2001 From: "yuze.zyz" Date: Wed, 28 Jun 2023 09:44:44 +0800 Subject: [PATCH] Add teardown for tests Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/12643554 * add teardown for tests * add teardown for dialog_modeling_trainer,document_grounded_dialog_generate_trainer,document_grounded_dialog_rerank_trainer,document_grounded_dialog_retrieval_trainer,training_args,translation_evaluation_trainer,translation_trainer --- tests/trainers/test_dialog_modeling_trainer.py | 6 ++++++ ..._document_grounded_dialog_rerank_trainer.py | 5 +++++ .../test_image_portrait_stylization_trainer.py | 5 +++++ tests/trainers/test_nerf_recon_acc_trainer.py | 5 +++++ .../trainers/test_ocr_detection_db_trainer.py | 4 ++++ tests/trainers/test_ofa_mmspeech_trainer.py | 18 ++++++++++++------ tests/trainers/test_ofa_trainer.py | 17 +++++++++++------ .../test_plug_finetune_text_generation.py | 1 + ...erring_video_object_segmentation_trainer.py | 4 ++++ tests/trainers/test_team_transfer_trainer.py | 5 +++++ .../trainers/test_tinynas_damoyolo_trainer.py | 5 +++++ .../test_translation_evaluation_trainer.py | 12 ++++++++++++ 12 files changed, 75 insertions(+), 12 deletions(-) diff --git a/tests/trainers/test_dialog_modeling_trainer.py b/tests/trainers/test_dialog_modeling_trainer.py index 900bf904..3378506c 100644 --- a/tests/trainers/test_dialog_modeling_trainer.py +++ b/tests/trainers/test_dialog_modeling_trainer.py @@ -1,5 +1,6 @@ # Copyright (c) Alibaba, Inc. and its affiliates. import os +import shutil import unittest import torch @@ -17,6 +18,11 @@ class TestDialogModelingTrainer(unittest.TestCase): model_id = 'damo/nlp_space_pretrained-dialog-model' output_dir = './dialog_fintune_result' + def tearDown(self): + if os.path.exists(self.output_dir): + shutil.rmtree(self.output_dir) + super().tearDown() + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_trainer_with_model_and_args(self): # download data set diff --git a/tests/trainers/test_document_grounded_dialog_rerank_trainer.py b/tests/trainers/test_document_grounded_dialog_rerank_trainer.py index fad0b55e..168659db 100644 --- a/tests/trainers/test_document_grounded_dialog_rerank_trainer.py +++ b/tests/trainers/test_document_grounded_dialog_rerank_trainer.py @@ -1,5 +1,6 @@ # Copyright (c) Alibaba, Inc. and its affiliates. import os +import shutil import unittest import json @@ -19,6 +20,10 @@ class TestDialogIntentTrainer(unittest.TestCase): def setUp(self): self.model_id = 'DAMO_ConvAI/nlp_convai_ranking_pretrain' + def tearDown(self): + shutil.rmtree('./model') + super().tearDown() + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_trainer_with_model_and_args(self): args = { diff --git a/tests/trainers/test_image_portrait_stylization_trainer.py b/tests/trainers/test_image_portrait_stylization_trainer.py index 6a3c41fa..37b42de6 100644 --- a/tests/trainers/test_image_portrait_stylization_trainer.py +++ b/tests/trainers/test_image_portrait_stylization_trainer.py @@ -1,5 +1,6 @@ # Copyright (c) Alibaba, Inc. and its affiliates. import os +import shutil import unittest import cv2 @@ -20,6 +21,10 @@ class TestImagePortraitStylizationTrainer(unittest.TestCase): self.task = Tasks.image_portrait_stylization self.test_image = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_cartoon.png' + def tearDown(self): + shutil.rmtree('exp_localtoon') + super().tearDown() + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_run_with_model_name(self): model_id = 'damo/cv_unet_person-image-cartoon_compound-models' diff --git a/tests/trainers/test_nerf_recon_acc_trainer.py b/tests/trainers/test_nerf_recon_acc_trainer.py index 4b6c8091..579901af 100644 --- a/tests/trainers/test_nerf_recon_acc_trainer.py +++ b/tests/trainers/test_nerf_recon_acc_trainer.py @@ -1,5 +1,6 @@ # Copyright (c) Alibaba, Inc. and its affiliates. import os +import shutil import unittest from modelscope.msdatasets import MsDataset @@ -10,6 +11,10 @@ from modelscope.utils.test_utils import test_level class TestNeRFReconAccTrainer(unittest.TestCase): + def tearDown(self): + shutil.rmtree('exp_nerf') + super().tearDown() + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_run_with_model_name(self): model_id = 'damo/cv_nerf-3d-reconstruction-accelerate_damo' diff --git a/tests/trainers/test_ocr_detection_db_trainer.py b/tests/trainers/test_ocr_detection_db_trainer.py index 10097fea..136ae410 100644 --- a/tests/trainers/test_ocr_detection_db_trainer.py +++ b/tests/trainers/test_ocr_detection_db_trainer.py @@ -36,6 +36,10 @@ class TestOCRDetectionDBTrainerSingleGPU(unittest.TestCase): self.saved_infer_model = os.path.join(self.saved_dir, 'pytorch_model.pt') + def tearDown(self): + shutil.rmtree(self.saved_dir) + super().tearDown() + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_trainer_finetune_singleGPU(self): diff --git a/tests/trainers/test_ofa_mmspeech_trainer.py b/tests/trainers/test_ofa_mmspeech_trainer.py index 2c4f6307..6ab4ac76 100644 --- a/tests/trainers/test_ofa_mmspeech_trainer.py +++ b/tests/trainers/test_ofa_mmspeech_trainer.py @@ -69,11 +69,17 @@ class TestMMSpeechTrainer(unittest.TestCase): 'metrics': [{'type': 'accuracy'}]}, 'preprocessor': []} + self.WORKSPACE = './workspace/ckpts/asr_recognition' + + def tearDown(self) -> None: + if os.path.exists(self.WORKSPACE): + shutil.rmtree(self.WORKSPACE, ignore_errors=True) + super().tearDown() + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_trainer_std(self): - WORKSPACE = './workspace/ckpts/asr_recognition' - os.makedirs(WORKSPACE, exist_ok=True) - config_file = os.path.join(WORKSPACE, ModelFile.CONFIGURATION) + os.makedirs(self.WORKSPACE, exist_ok=True) + config_file = os.path.join(self.WORKSPACE, ModelFile.CONFIGURATION) with open(config_file, 'w') as writer: json.dump(self.finetune_cfg, writer) @@ -81,7 +87,7 @@ class TestMMSpeechTrainer(unittest.TestCase): args = dict( model=pretrained_model, - work_dir=WORKSPACE, + work_dir=self.WORKSPACE, train_dataset=MsDataset.load( 'aishell1_subset', subset_name='default', @@ -100,8 +106,8 @@ class TestMMSpeechTrainer(unittest.TestCase): self.assertIn( ModelFile.TORCH_MODEL_BIN_FILE, - os.listdir(os.path.join(WORKSPACE, ModelFile.TRAIN_OUTPUT_DIR))) - shutil.rmtree(WORKSPACE) + os.listdir( + os.path.join(self.WORKSPACE, ModelFile.TRAIN_OUTPUT_DIR))) if __name__ == '__main__': diff --git a/tests/trainers/test_ofa_trainer.py b/tests/trainers/test_ofa_trainer.py index f4ca7bcb..cb480744 100644 --- a/tests/trainers/test_ofa_trainer.py +++ b/tests/trainers/test_ofa_trainer.py @@ -69,19 +69,24 @@ class TestOfaTrainer(unittest.TestCase): 'evaluation': {'dataloader': {'batch_size_per_gpu': 4, 'workers_per_gpu': 0}, 'metrics': [{'type': 'accuracy'}]}, 'preprocessor': []} + self.WORKSPACE = './workspace/ckpts/recognition' + + def tearDown(self) -> None: + if os.path.exists(self.WORKSPACE): + shutil.rmtree(self.WORKSPACE, ignore_errors=True) + super().tearDown() @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_trainer_std(self): - WORKSPACE = './workspace/ckpts/recognition' - os.makedirs(WORKSPACE, exist_ok=True) - config_file = os.path.join(WORKSPACE, ModelFile.CONFIGURATION) + os.makedirs(self.WORKSPACE, exist_ok=True) + config_file = os.path.join(self.WORKSPACE, ModelFile.CONFIGURATION) with open(config_file, 'w') as writer: json.dump(self.finetune_cfg, writer, indent=4) pretrained_model = 'damo/ofa_ocr-recognition_scene_base_zh' args = dict( model=pretrained_model, - work_dir=WORKSPACE, + work_dir=self.WORKSPACE, train_dataset=MsDataset.load( 'ocr_fudanvi_zh', subset_name='scene', @@ -100,8 +105,8 @@ class TestOfaTrainer(unittest.TestCase): self.assertIn( ModelFile.TORCH_MODEL_BIN_FILE, - os.listdir(os.path.join(WORKSPACE, ModelFile.TRAIN_OUTPUT_DIR))) - shutil.rmtree(WORKSPACE) + os.listdir( + os.path.join(self.WORKSPACE, ModelFile.TRAIN_OUTPUT_DIR))) if __name__ == '__main__': diff --git a/tests/trainers/test_plug_finetune_text_generation.py b/tests/trainers/test_plug_finetune_text_generation.py index 6d9e0740..4e3523b0 100644 --- a/tests/trainers/test_plug_finetune_text_generation.py +++ b/tests/trainers/test_plug_finetune_text_generation.py @@ -45,6 +45,7 @@ def test_trainer_with_model_and_args(): trainer = build_trainer( name=Trainers.nlp_plug_trainer, default_args=kwargs) trainer.train() + shutil.rmtree(tmp_dir) if __name__ == '__main__': diff --git a/tests/trainers/test_referring_video_object_segmentation_trainer.py b/tests/trainers/test_referring_video_object_segmentation_trainer.py index fb152954..53ab5236 100644 --- a/tests/trainers/test_referring_video_object_segmentation_trainer.py +++ b/tests/trainers/test_referring_video_object_segmentation_trainer.py @@ -60,6 +60,10 @@ class TestImageInstanceSegmentationTrainer(unittest.TestCase): self.max_epochs = max_epochs + def tearDown(self): + shutil.rmtree('./work_dir') + super().tearDown() + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_trainer(self): kwargs = dict( diff --git a/tests/trainers/test_team_transfer_trainer.py b/tests/trainers/test_team_transfer_trainer.py index 0f6b88bb..6012da08 100644 --- a/tests/trainers/test_team_transfer_trainer.py +++ b/tests/trainers/test_team_transfer_trainer.py @@ -1,4 +1,5 @@ import os +import shutil import unittest import json @@ -81,6 +82,10 @@ def train_worker(device_id): class TEAMTransferTrainerTest(unittest.TestCase): + def tearDown(self) -> None: + super().tearDown() + shutil.rmtree('./ckpt') + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_trainer(self): if torch.cuda.device_count() > 0: diff --git a/tests/trainers/test_tinynas_damoyolo_trainer.py b/tests/trainers/test_tinynas_damoyolo_trainer.py index 5dd9e928..d2070bec 100644 --- a/tests/trainers/test_tinynas_damoyolo_trainer.py +++ b/tests/trainers/test_tinynas_damoyolo_trainer.py @@ -1,6 +1,7 @@ # Copyright (c) Alibaba, Inc. and its affiliates. import os +import shutil import unittest from modelscope.hub.snapshot_download import snapshot_download @@ -21,6 +22,10 @@ class TestTinynasDamoyoloTrainerSingleGPU(unittest.TestCase): self.model_id = 'damo/cv_tinynas_object-detection_damoyolo' self.cache_path = _setup() + def tearDown(self) -> None: + super().tearDown() + shutil.rmtree('./workdirs') + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_trainer_from_scratch_singleGPU(self): kwargs = dict( diff --git a/tests/trainers/test_translation_evaluation_trainer.py b/tests/trainers/test_translation_evaluation_trainer.py index 139427da..a070c812 100644 --- a/tests/trainers/test_translation_evaluation_trainer.py +++ b/tests/trainers/test_translation_evaluation_trainer.py @@ -1,8 +1,11 @@ # Copyright (c) Alibaba, Inc. and its affiliates. +import os.path +import shutil import unittest from modelscope.metainfo import Trainers from modelscope.trainers import build_trainer +from modelscope.utils.hub import read_config from modelscope.utils.test_utils import test_level @@ -13,6 +16,15 @@ class TranslationEvaluationTest(unittest.TestCase): self.model_id_large = 'damo/nlp_unite_mup_translation_evaluation_multilingual_large' self.model_id_base = 'damo/nlp_unite_mup_translation_evaluation_multilingual_base' + def tearDown(self) -> None: + cfg_base = read_config(self.model_id_base) + if os.path.exists(cfg_base.train.work_dir): + shutil.rmtree(cfg_base.train.work_dir, ignore_errors=True) + cfg_large = read_config(self.model_id_large) + if os.path.exists(cfg_large.train.work_dir): + shutil.rmtree(cfg_large.train.work_dir, ignore_errors=True) + super().tearDown() + @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') def test_run_with_unite_mup_large(self) -> None: default_args = {'model': self.model_id_large}