From 26397472aa79ad29b3426def34f282c9948b1a19 Mon Sep 17 00:00:00 2001 From: YWMditto Date: Sun, 1 May 2022 16:53:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E4=BA=86=20@pytest.mark.torc?= =?UTF-8?q?h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/callbacks/test_checkpoint_callback_torch.py | 5 ++++- .../callbacks/test_load_best_model_callback_torch.py | 2 +- tests/core/callbacks/test_more_evaluate_callback.py | 2 ++ .../torch_callbacks/test_torch_grad_clip_callback.py | 1 + .../torch_callbacks/test_torch_warmup_callback.py | 1 + tests/core/drivers/torch_driver/test_ddp.py | 3 +++ tests/core/drivers/torch_driver/test_dist_utils.py | 2 ++ .../torch_driver/test_initialize_torch_driver.py | 12 ++++++++++++ tests/core/drivers/torch_driver/test_utils.py | 4 ++++ tests/core/log/test_logger_torch.py | 7 +++++++ tests/core/utils/test_distributed.py | 3 ++- tests/core/utils/test_paddle_utils.py | 3 ++- tests/core/utils/test_torch_paddle_utils.py | 3 +++ 13 files changed, 44 insertions(+), 4 deletions(-) diff --git a/tests/core/callbacks/test_checkpoint_callback_torch.py b/tests/core/callbacks/test_checkpoint_callback_torch.py index ca2a3292..acb975d9 100644 --- a/tests/core/callbacks/test_checkpoint_callback_torch.py +++ b/tests/core/callbacks/test_checkpoint_callback_torch.py @@ -70,6 +70,7 @@ def model_and_optimizers(request): return trainer_params +@pytest.mark.torch @pytest.mark.parametrize("driver,device", [("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1)]) # ("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1) @pytest.mark.parametrize("version", [0, 1]) @pytest.mark.parametrize("only_state_dict", [True, False]) @@ -201,6 +202,7 @@ def test_model_checkpoint_callback_1( dist.destroy_process_group() +@pytest.mark.torch @pytest.mark.parametrize("driver,device", [("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1)]) # ("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1) @pytest.mark.parametrize("only_state_dict", [True]) @magic_argv_env_context @@ -292,6 +294,7 @@ def test_model_checkpoint_callback_2( dist.destroy_process_group() +@pytest.mark.torch @pytest.mark.parametrize("driver,device", [("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 0)]) # ("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1) @pytest.mark.parametrize("version", [0, 1]) @pytest.mark.parametrize("only_state_dict", [True, False]) @@ -421,7 +424,7 @@ def test_trainer_checkpoint_callback_1( dist.destroy_process_group() - +@pytest.mark.torch # 通过自己编写 model_save_fn 和 model_load_fn 来测试 huggingface 的 transformers 的模型的保存和加载; @pytest.mark.parametrize("driver,device", [("torch_ddp", [6, 7]), ("torch", 7)]) # ("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1) @pytest.mark.parametrize("version", [0, 1]) diff --git a/tests/core/callbacks/test_load_best_model_callback_torch.py b/tests/core/callbacks/test_load_best_model_callback_torch.py index b042ae0f..726bef87 100644 --- a/tests/core/callbacks/test_load_best_model_callback_torch.py +++ b/tests/core/callbacks/test_load_best_model_callback_torch.py @@ -71,8 +71,8 @@ def model_and_optimizers(request): return trainer_params -# pytest test_load_best_model_callback_torch.py::test_load_best_model_callback -s +@pytest.mark.torch @pytest.mark.parametrize("driver,device", [("torch_ddp", [4, 5]), ("torch", 1), ("torch", "cpu")]) # ("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1) @pytest.mark.parametrize("save_folder", ['save_models', None]) @pytest.mark.parametrize("only_state_dict", [True, False]) diff --git a/tests/core/callbacks/test_more_evaluate_callback.py b/tests/core/callbacks/test_more_evaluate_callback.py index 16ee3e17..0fba0f33 100644 --- a/tests/core/callbacks/test_more_evaluate_callback.py +++ b/tests/core/callbacks/test_more_evaluate_callback.py @@ -95,6 +95,7 @@ def model_and_optimizers(request): return trainer_params +@pytest.mark.torch @pytest.mark.parametrize("driver,device", [("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1)]) # ("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1) @pytest.mark.parametrize("version", [0, 1]) @pytest.mark.parametrize("only_state_dict", [True, False]) @@ -179,6 +180,7 @@ def test_model_more_evaluate_callback_1( dist.destroy_process_group() +@pytest.mark.torch @pytest.mark.parametrize("driver,device", [("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 0)]) # ("torch", "cpu"), ("torch_ddp", [0, 1]), ("torch", 1) @pytest.mark.parametrize("version", [0, 1]) @pytest.mark.parametrize("only_state_dict", [True, False]) diff --git a/tests/core/callbacks/torch_callbacks/test_torch_grad_clip_callback.py b/tests/core/callbacks/torch_callbacks/test_torch_grad_clip_callback.py index 7f2016e2..a365329e 100644 --- a/tests/core/callbacks/torch_callbacks/test_torch_grad_clip_callback.py +++ b/tests/core/callbacks/torch_callbacks/test_torch_grad_clip_callback.py @@ -25,6 +25,7 @@ class CheckClipCallback(Callback): assert np.linalg.norm(param.grad.cpu().view(-1).numpy())<=self.clip_value +@pytest.mark.torch @pytest.mark.parametrize('accumulation_steps', [1, 3, 5]) @pytest.mark.parametrize('fp16', [True, False]) @pytest.mark.parametrize('clip_type', ['norm', 'value']) diff --git a/tests/core/callbacks/torch_callbacks/test_torch_warmup_callback.py b/tests/core/callbacks/torch_callbacks/test_torch_warmup_callback.py index 6367c458..ecdaa1c5 100644 --- a/tests/core/callbacks/torch_callbacks/test_torch_warmup_callback.py +++ b/tests/core/callbacks/torch_callbacks/test_torch_warmup_callback.py @@ -15,6 +15,7 @@ class RecordLrCallback(Callback): self.lrs.append(trainer.driver.optimizers[0].param_groups[0]['lr']) +@pytest.mark.torch @pytest.mark.parametrize('warmup', [5, 0.1]) @pytest.mark.parametrize('schedule', ['constant', 'linear']) @pytest.mark.parametrize('accumulation_steps', [1, 3, 4]) diff --git a/tests/core/drivers/torch_driver/test_ddp.py b/tests/core/drivers/torch_driver/test_ddp.py index 87787fbc..48299bf4 100644 --- a/tests/core/drivers/torch_driver/test_ddp.py +++ b/tests/core/drivers/torch_driver/test_ddp.py @@ -72,6 +72,7 @@ def dataloader_with_randomsampler(dataset, batch_size, shuffle, drop_last, seed= # ############################################################################ +@pytest.mark.torch class TestDDPDriverFunction: """ 测试 TorchDDPDriver 一些简单函数的测试类,基本都是测试能否运行、是否存在 import 错误等问题 @@ -180,6 +181,7 @@ class TestDDPDriverFunction: # ############################################################################ +@pytest.mark.torch class TestSetDistReproDataloader: @classmethod @@ -526,6 +528,7 @@ class TestSetDistReproDataloader: # 测试 save 和 load 相关的功能 # ############################################################################ +@pytest.mark.torch class TestSaveLoad: """ 测试多卡情况下 save 和 load 相关函数的表现 diff --git a/tests/core/drivers/torch_driver/test_dist_utils.py b/tests/core/drivers/torch_driver/test_dist_utils.py index 2d2145c8..6e04af78 100644 --- a/tests/core/drivers/torch_driver/test_dist_utils.py +++ b/tests/core/drivers/torch_driver/test_dist_utils.py @@ -11,6 +11,7 @@ from fastNLP.core.drivers.torch_driver.dist_utils import fastnlp_torch_all_gathe from tests.helpers.utils import re_run_current_cmd_for_torch, magic_argv_env_context +@pytest.mark.torch @magic_argv_env_context def test_fastnlp_torch_all_gather(): os.environ['MASTER_ADDR'] = '127.0.0.1' @@ -59,6 +60,7 @@ def test_fastnlp_torch_all_gather(): dist.destroy_process_group() +@pytest.mark.torch @magic_argv_env_context def test_fastnlp_torch_broadcast_object(): os.environ['MASTER_ADDR'] = '127.0.0.1' diff --git a/tests/core/drivers/torch_driver/test_initialize_torch_driver.py b/tests/core/drivers/torch_driver/test_initialize_torch_driver.py index 3e612964..f62ccd0c 100644 --- a/tests/core/drivers/torch_driver/test_initialize_torch_driver.py +++ b/tests/core/drivers/torch_driver/test_initialize_torch_driver.py @@ -8,12 +8,16 @@ from tests.helpers.utils import magic_argv_env_context import torch + +@pytest.mark.torch def test_incorrect_driver(): model = TorchNormalModel_Classification_1(2, 100) with pytest.raises(ValueError): driver = initialize_torch_driver("paddle", 0, model) + +@pytest.mark.torch @pytest.mark.parametrize( "device", ["cpu", "cuda:0", 0, torch.device("cuda:0")] @@ -31,6 +35,8 @@ def test_get_single_device(driver, device): driver = initialize_torch_driver(driver, device, model) assert isinstance(driver, TorchSingleDriver) + +@pytest.mark.torch @pytest.mark.parametrize( "device", [0, 1] @@ -50,6 +56,8 @@ def test_get_ddp_2(driver, device): assert isinstance(driver, TorchDDPDriver) + +@pytest.mark.torch @pytest.mark.parametrize( "device", [[0, 2, 3], -1] @@ -69,6 +77,8 @@ def test_get_ddp(driver, device): assert isinstance(driver, TorchDDPDriver) + +@pytest.mark.torch @pytest.mark.parametrize( ("driver", "device"), [("torch_ddp", "cpu")] @@ -82,6 +92,8 @@ def test_get_ddp_cpu(driver, device): with pytest.raises(ValueError): driver = initialize_torch_driver(driver, device, model) + +@pytest.mark.torch @pytest.mark.parametrize( "device", [-2, [0, torch.cuda.device_count() + 1, 3], [-2], torch.cuda.device_count() + 1] diff --git a/tests/core/drivers/torch_driver/test_utils.py b/tests/core/drivers/torch_driver/test_utils.py index 4df767b5..97037b71 100644 --- a/tests/core/drivers/torch_driver/test_utils.py +++ b/tests/core/drivers/torch_driver/test_utils.py @@ -9,6 +9,8 @@ from torch.utils.data import DataLoader, BatchSampler from tests.helpers.datasets.torch_data import TorchNormalDataset + +@pytest.mark.torch def test_replace_batch_sampler(): dataset = TorchNormalDataset(10) dataloader = DataLoader(dataset, batch_size=32) @@ -22,6 +24,8 @@ def test_replace_batch_sampler(): assert len(replaced_loader.dataset) == len(dataset) assert replaced_loader.batch_sampler.batch_size == 16 + +@pytest.mark.torch def test_replace_sampler(): dataset = TorchNormalDataset(10) dataloader = DataLoader(dataset, batch_size=32) diff --git a/tests/core/log/test_logger_torch.py b/tests/core/log/test_logger_torch.py index 7c1b96e7..13a758e9 100644 --- a/tests/core/log/test_logger_torch.py +++ b/tests/core/log/test_logger_torch.py @@ -4,6 +4,7 @@ import datetime from pathlib import Path import logging import re +import pytest from fastNLP.envs.env import FASTNLP_LAUNCH_TIME from fastNLP.core import rank_zero_rm @@ -12,6 +13,7 @@ from fastNLP.core.log.logger import logger from tests.helpers.utils import magic_argv_env_context, recover_logger +@pytest.mark.torch # 测试 TorchDDPDriver; @magic_argv_env_context @recover_logger @@ -61,6 +63,7 @@ def test_add_file_ddp_1_torch(): dist.destroy_process_group() +@pytest.mark.torch @magic_argv_env_context @recover_logger def test_add_file_ddp_2_torch(): @@ -111,6 +114,7 @@ def test_add_file_ddp_2_torch(): dist.destroy_process_group() +@pytest.mark.torch @magic_argv_env_context @recover_logger def test_add_file_ddp_3_torch(): @@ -159,6 +163,8 @@ def test_add_file_ddp_3_torch(): dist.barrier() dist.destroy_process_group() + +@pytest.mark.torch @magic_argv_env_context @recover_logger def test_add_file_ddp_4_torch(): @@ -208,6 +214,7 @@ def test_add_file_ddp_4_torch(): dist.destroy_process_group() +@pytest.mark.torch class TestLogger: msg = 'some test log msg' diff --git a/tests/core/utils/test_distributed.py b/tests/core/utils/test_distributed.py index eecbc72a..e4610e00 100644 --- a/tests/core/utils/test_distributed.py +++ b/tests/core/utils/test_distributed.py @@ -1,4 +1,5 @@ import os +import pytest from fastNLP.envs.distributed import rank_zero_call, all_rank_call_context from tests.helpers.utils import re_run_current_cmd_for_torch, Capturing, magic_argv_env_context @@ -30,7 +31,7 @@ class JittorTest: def test_all_rank_run(self): pass - +@pytest.mark.torch class TestTorch: @magic_argv_env_context def test_rank_zero_call(self): diff --git a/tests/core/utils/test_paddle_utils.py b/tests/core/utils/test_paddle_utils.py index 344c0ed9..777b234f 100644 --- a/tests/core/utils/test_paddle_utils.py +++ b/tests/core/utils/test_paddle_utils.py @@ -1,5 +1,5 @@ import unittest - +import pytest import paddle from fastNLP.core.utils.paddle_utils import paddle_to, paddle_move_data_to_device @@ -11,6 +11,7 @@ from fastNLP.core.utils.paddle_utils import paddle_to, paddle_move_data_to_devic # ############################################################################ +@pytest.mark.paddle class PaddleToDeviceTestCase(unittest.TestCase): def test_case(self): tensor = paddle.rand((4, 5)) diff --git a/tests/core/utils/test_torch_paddle_utils.py b/tests/core/utils/test_torch_paddle_utils.py index d5c61e4f..f56fa172 100644 --- a/tests/core/utils/test_torch_paddle_utils.py +++ b/tests/core/utils/test_torch_paddle_utils.py @@ -1,6 +1,7 @@ import unittest import paddle +import pytest import torch from fastNLP.core.utils.torch_paddle_utils import torch_paddle_move_data_to_device @@ -11,6 +12,8 @@ from fastNLP.core.utils.torch_paddle_utils import torch_paddle_move_data_to_devi # ############################################################################ +# @pytest.mark.paddle +# @pytest.mark.torch class TorchPaddleMoveDataToDeviceTestCase(unittest.TestCase): def check_gpu(self, tensor, idx):