diff --git a/tests/core/dataloaders/paddle_dataloader/test_fdl.py b/tests/core/dataloaders/paddle_dataloader/test_fdl.py index 83e40610..484b0daa 100644 --- a/tests/core/dataloaders/paddle_dataloader/test_fdl.py +++ b/tests/core/dataloaders/paddle_dataloader/test_fdl.py @@ -1,10 +1,12 @@ import pytest +import numpy as np from fastNLP.core.dataloaders.paddle_dataloader.fdl import PaddleDataLoader from fastNLP.core.dataset import DataSet -from paddle.io import Dataset, DataLoader -import numpy as np -import paddle +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + from paddle.io import Dataset, DataLoader + import paddle class RandomDataset(Dataset): diff --git a/tests/core/drivers/paddle_driver/test_dist_utils.py b/tests/core/drivers/paddle_driver/test_dist_utils.py index 8b136b3c..da40ad78 100644 --- a/tests/core/drivers/paddle_driver/test_dist_utils.py +++ b/tests/core/drivers/paddle_driver/test_dist_utils.py @@ -14,9 +14,10 @@ from fastNLP.core.drivers.paddle_driver.dist_utils import ( ) from fastNLP.core.drivers.paddle_driver.fleet_launcher import FleetLauncher from tests.helpers.utils import magic_argv_env_context - -import paddle -import paddle.distributed as dist +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle + import paddle.distributed as dist @pytest.mark.paddle class TestDistUtilsTools: diff --git a/tests/core/drivers/paddle_driver/test_fleet.py b/tests/core/drivers/paddle_driver/test_fleet.py index 40bbe95e..a184bb11 100644 --- a/tests/core/drivers/paddle_driver/test_fleet.py +++ b/tests/core/drivers/paddle_driver/test_fleet.py @@ -13,10 +13,11 @@ from tests.helpers.models.paddle_model import PaddleNormalModel_Classification_1 from tests.helpers.datasets.paddle_data import PaddleNormalDataset, PaddleRandomMaxDataset from tests.helpers.utils import magic_argv_env_context from fastNLP.core import rank_zero_rm - -import paddle -import paddle.distributed as dist -from paddle.io import DataLoader, BatchSampler +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle + import paddle.distributed as dist + from paddle.io import DataLoader, BatchSampler def generate_driver(num_labels, feature_dimension, device=[0,1], fp16=False, output_from_new_proc="only_error"): paddle_model = PaddleNormalModel_Classification_1(num_labels, feature_dimension) diff --git a/tests/core/drivers/paddle_driver/test_initialize_paddle_driver.py b/tests/core/drivers/paddle_driver/test_initialize_paddle_driver.py index e27f2e0c..e339bbcc 100644 --- a/tests/core/drivers/paddle_driver/test_initialize_paddle_driver.py +++ b/tests/core/drivers/paddle_driver/test_initialize_paddle_driver.py @@ -5,8 +5,9 @@ from fastNLP.core.drivers.paddle_driver.initialize_paddle_driver import initiali from fastNLP.envs import get_gpu_count from tests.helpers.models.paddle_model import PaddleNormalModel_Classification_1 from tests.helpers.utils import magic_argv_env_context - -import paddle +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle @pytest.mark.paddle def test_incorrect_driver(): diff --git a/tests/core/drivers/paddle_driver/test_single_device.py b/tests/core/drivers/paddle_driver/test_single_device.py index 326e102a..a00a41f5 100644 --- a/tests/core/drivers/paddle_driver/test_single_device.py +++ b/tests/core/drivers/paddle_driver/test_single_device.py @@ -8,10 +8,12 @@ from tests.helpers.datasets.paddle_data import PaddleNormalDataset, PaddleRandom from tests.helpers.datasets.torch_data import TorchNormalDataset from tests.helpers.models.torch_model import TorchNormalModel_Classification_1 from fastNLP.core import rank_zero_rm - -import paddle -from paddle.io import DataLoader, BatchSampler -import torch +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE, _NEED_IMPORT_TORCH +if _NEED_IMPORT_PADDLE: + import paddle + from paddle.io import DataLoader, BatchSampler +if _NEED_IMPORT_TORCH: + import torch ############################################################################ # diff --git a/tests/core/drivers/paddle_driver/test_utils.py b/tests/core/drivers/paddle_driver/test_utils.py index 8db4de2d..4b683c1e 100644 --- a/tests/core/drivers/paddle_driver/test_utils.py +++ b/tests/core/drivers/paddle_driver/test_utils.py @@ -7,9 +7,10 @@ from fastNLP.core.drivers.paddle_driver.utils import ( replace_sampler, ) from fastNLP.core.samplers import RandomBatchSampler, RandomSampler - -import paddle -from paddle.io import DataLoader, BatchSampler +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle + from paddle.io import DataLoader, BatchSampler from tests.helpers.datasets.paddle_data import PaddleNormalDataset diff --git a/tests/core/drivers/torch_driver/test_single_device.py b/tests/core/drivers/torch_driver/test_single_device.py index 29d1fe8e..8c761a95 100644 --- a/tests/core/drivers/torch_driver/test_single_device.py +++ b/tests/core/drivers/torch_driver/test_single_device.py @@ -8,10 +8,12 @@ from tests.helpers.datasets.torch_data import TorchNormalDataset, TorchArgMaxDat from tests.helpers.datasets.paddle_data import PaddleNormalDataset from tests.helpers.models.paddle_model import PaddleNormalModel_Classification_1 from fastNLP.core import rank_zero_rm - -import torch -from torch.utils.data import DataLoader, BatchSampler -import paddle +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE, _NEED_IMPORT_TORCH +if _NEED_IMPORT_TORCH: + import torch + from torch.utils.data import DataLoader, BatchSampler +if _NEED_IMPORT_PADDLE: + import paddle def dataloader_with_randombatchsampler(dataset, batch_size, shuffle, drop_last): """ diff --git a/tests/core/metrics/test_accuracy_paddle.py b/tests/core/metrics/test_accuracy_paddle.py index 2d1e59fd..0dc65f1f 100644 --- a/tests/core/metrics/test_accuracy_paddle.py +++ b/tests/core/metrics/test_accuracy_paddle.py @@ -1,12 +1,14 @@ import os import pytest -import paddle -import paddle.distributed -import paddle.distributed.fleet.base.role_maker as role_maker -import paddle.distributed.fleet as fleet from fastNLP.core.metrics import Accuracy from fastNLP.core.drivers.paddle_driver.fleet_launcher import FleetLauncher +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle + import paddle.distributed + import paddle.distributed.fleet.base.role_maker as role_maker + import paddle.distributed.fleet as fleet ############################################################################ diff --git a/tests/core/utils/test_paddle_utils.py b/tests/core/utils/test_paddle_utils.py index e3cb2329..ba9dcf79 100644 --- a/tests/core/utils/test_paddle_utils.py +++ b/tests/core/utils/test_paddle_utils.py @@ -1,7 +1,9 @@ import pytest -import paddle from fastNLP.core.utils.paddle_utils import paddle_to, paddle_move_data_to_device +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle ############################################################################ diff --git a/tests/helpers/datasets/paddle_data.py b/tests/helpers/datasets/paddle_data.py index 17b2d310..0fa8ee83 100644 --- a/tests/helpers/datasets/paddle_data.py +++ b/tests/helpers/datasets/paddle_data.py @@ -1,7 +1,10 @@ -import paddle -from paddle.io import Dataset import numpy as np +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle + from paddle.io import Dataset + class PaddleNormalDataset(Dataset): def __init__(self, num_of_data=1000): diff --git a/tests/helpers/models/paddle_model.py b/tests/helpers/models/paddle_model.py index efa8c0ce..7a897235 100644 --- a/tests/helpers/models/paddle_model.py +++ b/tests/helpers/models/paddle_model.py @@ -1,5 +1,7 @@ -import paddle -import paddle.nn as nn +from fastNLP.envs.imports import _NEED_IMPORT_PADDLE +if _NEED_IMPORT_PADDLE: + import paddle + import paddle.nn as nn class PaddleNormalModel_Classification_1(paddle.nn.Layer): """