2020-12-17 09:21:00 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-11-17 18:35:17 +00:00
|
|
|
import importlib
|
2020-11-06 14:53:46 +00:00
|
|
|
import platform
|
2020-09-23 04:19:46 +00:00
|
|
|
from unittest import mock
|
2020-11-06 14:53:46 +00:00
|
|
|
|
2020-09-23 04:19:46 +00:00
|
|
|
import pytest
|
2020-11-06 14:53:46 +00:00
|
|
|
import torch
|
|
|
|
|
2020-12-23 23:11:42 +00:00
|
|
|
from pl_examples import _DALI_AVAILABLE
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2020-11-17 18:35:17 +00:00
|
|
|
ARGS_DEFAULT = """
|
2020-12-04 22:32:00 +00:00
|
|
|
--default_root_dir %(tmpdir)s \
|
2020-09-23 04:19:46 +00:00
|
|
|
--max_epochs 1 \
|
|
|
|
--batch_size 32 \
|
|
|
|
--limit_train_batches 2 \
|
|
|
|
--limit_val_batches 2 \
|
|
|
|
"""
|
|
|
|
|
2020-11-17 18:35:17 +00:00
|
|
|
ARGS_GPU = ARGS_DEFAULT + """
|
|
|
|
--gpus 1 \
|
2020-09-23 04:19:46 +00:00
|
|
|
"""
|
|
|
|
|
2020-12-04 22:32:00 +00:00
|
|
|
ARGS_DP = ARGS_DEFAULT + """
|
2020-09-23 04:19:46 +00:00
|
|
|
--gpus 2 \
|
2020-12-04 22:32:00 +00:00
|
|
|
--accelerator dp \
|
|
|
|
"""
|
|
|
|
|
|
|
|
ARGS_DP_AMP = ARGS_DP + """
|
2020-09-23 04:19:46 +00:00
|
|
|
--precision 16 \
|
|
|
|
"""
|
|
|
|
|
2020-12-04 22:32:00 +00:00
|
|
|
ARGS_DDP = ARGS_DEFAULT + """
|
2020-11-17 18:35:17 +00:00
|
|
|
--gpus 2 \
|
2020-12-04 22:32:00 +00:00
|
|
|
--accelerator ddp \
|
2020-11-17 18:35:17 +00:00
|
|
|
--precision 16 \
|
|
|
|
"""
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2020-12-04 22:32:00 +00:00
|
|
|
ARGS_DDP_AMP = ARGS_DEFAULT + """
|
|
|
|
--precision 16 \
|
|
|
|
"""
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2020-12-04 22:32:00 +00:00
|
|
|
|
2021-01-30 10:17:12 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'import_cli', [
|
|
|
|
'pl_examples.basic_examples.simple_image_classifier',
|
|
|
|
'pl_examples.basic_examples.backbone_image_classifier',
|
|
|
|
'pl_examples.basic_examples.autoencoder',
|
|
|
|
]
|
|
|
|
)
|
2020-12-04 22:32:00 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
|
|
|
@pytest.mark.parametrize('cli_args', [ARGS_DP, ARGS_DP_AMP])
|
|
|
|
def test_examples_dp(tmpdir, import_cli, cli_args):
|
|
|
|
|
|
|
|
module = importlib.import_module(import_cli)
|
|
|
|
# update the temp dir
|
|
|
|
cli_args = cli_args % {'tmpdir': tmpdir}
|
|
|
|
|
|
|
|
with mock.patch("argparse._sys.argv", ["any.py"] + cli_args.strip().split()):
|
|
|
|
module.cli_main()
|
2020-09-23 04:19:46 +00:00
|
|
|
|
|
|
|
|
2020-11-17 18:35:17 +00:00
|
|
|
# ToDo: fix this failing example
|
|
|
|
# @pytest.mark.parametrize('import_cli', [
|
2020-11-20 18:10:40 +00:00
|
|
|
# 'pl_examples.basic_examples.simple_image_classifier',
|
|
|
|
# 'pl_examples.basic_examples.backbone_image_classifier',
|
2020-11-17 18:35:17 +00:00
|
|
|
# 'pl_examples.basic_examples.autoencoder',
|
|
|
|
# ])
|
2020-09-23 04:19:46 +00:00
|
|
|
# @pytest.mark.skipif(torch.cuda.device_count() < 2, reason="test requires multi-GPU machine")
|
2020-12-04 22:32:00 +00:00
|
|
|
# @pytest.mark.parametrize('cli_args', [ARGS_DDP, ARGS_DDP_AMP])
|
|
|
|
# def test_examples_ddp(tmpdir, import_cli, cli_args):
|
2020-09-23 04:19:46 +00:00
|
|
|
#
|
2020-11-17 18:35:17 +00:00
|
|
|
# module = importlib.import_module(import_cli)
|
2020-12-04 22:32:00 +00:00
|
|
|
# # update the temp dir
|
|
|
|
# cli_args = cli_args % {'tmpdir': tmpdir}
|
2020-09-23 04:19:46 +00:00
|
|
|
#
|
|
|
|
# with mock.patch("argparse._sys.argv", ["any.py"] + cli_args.strip().split()):
|
2020-11-17 18:35:17 +00:00
|
|
|
# module.cli_main()
|
2020-11-06 14:53:46 +00:00
|
|
|
|
|
|
|
|
2021-01-30 10:17:12 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'import_cli', [
|
|
|
|
'pl_examples.basic_examples.simple_image_classifier',
|
|
|
|
'pl_examples.basic_examples.backbone_image_classifier',
|
|
|
|
'pl_examples.basic_examples.autoencoder',
|
|
|
|
]
|
|
|
|
)
|
2020-11-17 18:35:17 +00:00
|
|
|
@pytest.mark.parametrize('cli_args', [ARGS_DEFAULT])
|
2020-12-04 22:32:00 +00:00
|
|
|
def test_examples_cpu(tmpdir, import_cli, cli_args):
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2020-11-17 18:35:17 +00:00
|
|
|
module = importlib.import_module(import_cli)
|
2020-12-04 22:32:00 +00:00
|
|
|
# update the temp dir
|
|
|
|
cli_args = cli_args % {'tmpdir': tmpdir}
|
2020-09-23 04:19:46 +00:00
|
|
|
|
2020-11-17 18:35:17 +00:00
|
|
|
with mock.patch("argparse._sys.argv", ["any.py"] + cli_args.strip().split()):
|
|
|
|
module.cli_main()
|
2020-11-06 14:53:46 +00:00
|
|
|
|
|
|
|
|
2020-12-23 23:11:42 +00:00
|
|
|
@pytest.mark.skipif(not _DALI_AVAILABLE, reason="Nvidia DALI required")
|
2020-11-06 14:53:46 +00:00
|
|
|
@pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires GPU machine")
|
|
|
|
@pytest.mark.skipif(platform.system() != 'Linux', reason='Only applies to Linux platform.')
|
2020-11-17 18:35:17 +00:00
|
|
|
@pytest.mark.parametrize('cli_args', [ARGS_GPU])
|
2020-12-04 22:32:00 +00:00
|
|
|
def test_examples_mnist_dali(tmpdir, cli_args):
|
2020-11-20 18:10:40 +00:00
|
|
|
from pl_examples.basic_examples.dali_image_classifier import cli_main
|
2020-11-06 14:53:46 +00:00
|
|
|
|
2020-12-04 22:32:00 +00:00
|
|
|
# update the temp dir
|
|
|
|
cli_args = cli_args % {'tmpdir': tmpdir}
|
2020-11-06 14:53:46 +00:00
|
|
|
with mock.patch("argparse._sys.argv", ["any.py"] + cli_args.strip().split()):
|
|
|
|
cli_main()
|