Skip to content

Commit cb59039

Browse files
authored
fixing examples (#6600)
* try Azure * -e * path
1 parent 3a56a60 commit cb59039

File tree

9 files changed

+29
-26
lines changed

9 files changed

+29
-26
lines changed

azure-pipelines.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ jobs:
113113
python -m pytest benchmarks -v --maxfail=2 --durations=0
114114
displayName: 'Testing: benchmarks'
115115
116-
- bash: |
116+
- script: |
117+
set -e
117118
python -m pytest pl_examples -v --maxfail=2 --durations=0
118119
python setup.py install --user --quiet
119120
bash pl_examples/run_ddp-example.sh
120-
cd pl_examples/basic_examples
121-
bash submit_ddp_job.sh
122-
bash submit_ddp2_job.sh
123-
pip uninstall -y pytorch-lightning
121+
# cd pl_examples/basic_examples
122+
# bash submit_ddp_job.sh
123+
# bash submit_ddp2_job.sh
124124
displayName: 'Examples'

pl_examples/basic_examples/submit_ddp2_job.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ source activate $1
2424
# -------------------------
2525

2626
# run script from above
27-
srun python3 image_classifier.py --accelerator 'ddp2' --gpus 2 --num_nodes 2
27+
srun python3 simple_image_classifier.py --accelerator 'ddp2' --gpus 2 --num_nodes 2 --max_epochs 5

pl_examples/basic_examples/submit_ddp_job.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ source activate $1
2424
# -------------------------
2525

2626
# run script from above
27-
srun python3 image_classifier.py --accelerator 'ddp' --gpus 2 --num_nodes 2
27+
srun python3 simple_image_classifier.py --accelerator 'ddp' --gpus 2 --num_nodes 2 --max_epochs 5

tests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
_TEST_ROOT = os.path.dirname(__file__)
2020
_PROJECT_ROOT = os.path.dirname(_TEST_ROOT)
2121
_TEMP_PATH = os.path.join(_PROJECT_ROOT, 'test_temp')
22-
DATASETS_PATH = os.path.join(_PROJECT_ROOT, 'Datasets')
23-
LEGACY_PATH = os.path.join(_PROJECT_ROOT, 'legacy')
22+
PATH_DATASETS = os.path.join(_PROJECT_ROOT, 'Datasets')
23+
PATH_LEGACY = os.path.join(_PROJECT_ROOT, 'legacy')
2424

2525
# todo: this setting `PYTHONPATH` may not be used by other evns like Conda for import packages
2626
if _PROJECT_ROOT not in os.getenv('PYTHONPATH', ""):

tests/base/model_template.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import torch.nn.functional as F
1919

2020
from pytorch_lightning.core.lightning import LightningModule
21+
from tests import PATH_DATASETS
2122
from tests.base.model_optimizers import ConfigureOptimizersPool
2223
from tests.base.model_test_dataloaders import TestDataloaderVariations
2324
from tests.base.model_test_epoch_ends import TestEpochEndVariations
@@ -28,7 +29,7 @@
2829
from tests.base.model_valid_dataloaders import ValDataloaderVariations
2930
from tests.base.model_valid_epoch_ends import ValidationEpochEndVariations
3031
from tests.base.model_valid_steps import ValidationStepVariations
31-
from tests.helpers.datasets import PATH_DATASETS, TrialMNIST
32+
from tests.helpers.datasets import TrialMNIST
3233

3334

3435
class EvalModelTemplate(

tests/checkpointing/test_legacy_checkpoints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import pytest
1919

2020
from pytorch_lightning import Trainer
21-
from tests import LEGACY_PATH
21+
from tests import PATH_LEGACY
2222

23-
LEGACY_CHECKPOINTS_PATH = os.path.join(LEGACY_PATH, 'checkpoints')
23+
LEGACY_CHECKPOINTS_PATH = os.path.join(PATH_LEGACY, 'checkpoints')
2424
CHECKPOINT_EXTENSION = ".ckpt"
2525

2626

tests/helpers/advanced_models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from torch.utils.data import DataLoader
2121

2222
from pytorch_lightning.core.lightning import LightningModule
23+
from tests import PATH_DATASETS
2324
from tests.helpers.datasets import AverageDataset, MNIST, TrialMNIST
2425

2526

@@ -165,7 +166,7 @@ def configure_optimizers(self):
165166
return [opt_g, opt_d], []
166167

167168
def train_dataloader(self):
168-
return DataLoader(TrialMNIST(train=True, download=True), batch_size=16)
169+
return DataLoader(TrialMNIST(root=PATH_DATASETS, train=True, download=True), batch_size=16)
169170

170171

171172
class ParityModuleRNN(LightningModule):
@@ -223,6 +224,7 @@ def configure_optimizers(self):
223224

224225
def train_dataloader(self):
225226
return DataLoader(MNIST(
227+
root=PATH_DATASETS,
226228
train=True,
227229
download=True,
228230
), batch_size=128, num_workers=1)

tests/helpers/datasets.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
from torch import Tensor
2323
from torch.utils.data import Dataset
2424

25-
from tests import _PROJECT_ROOT
26-
27-
#: local path to test datasets
28-
PATH_DATASETS = os.path.join(_PROJECT_ROOT, 'Datasets')
29-
3025

3126
class MNIST(Dataset):
3227
"""
@@ -47,7 +42,7 @@ class MNIST(Dataset):
4742
downloaded again.
4843
4944
Examples:
50-
>>> dataset = MNIST(download=True)
45+
>>> dataset = MNIST(".", download=True)
5146
>>> len(dataset)
5247
60000
5348
>>> torch.bincount(dataset.targets)
@@ -65,7 +60,7 @@ class MNIST(Dataset):
6560

6661
def __init__(
6762
self,
68-
root: str = PATH_DATASETS,
63+
root: str,
6964
train: bool = True,
7065
normalize: tuple = (0.1307, 0.3081),
7166
download: bool = True,
@@ -152,7 +147,7 @@ class TrialMNIST(MNIST):
152147
kwargs: Same as MNIST
153148
154149
Examples:
155-
>>> dataset = TrialMNIST(download=True)
150+
>>> dataset = TrialMNIST(".", download=True)
156151
>>> len(dataset)
157152
300
158153
>>> sorted(set([d.item() for d in dataset.targets]))
@@ -161,15 +156,15 @@ class TrialMNIST(MNIST):
161156
tensor([100, 100, 100])
162157
"""
163158

164-
def __init__(self, num_samples: int = 100, digits: Optional[Sequence] = (0, 1, 2), **kwargs):
159+
def __init__(self, root: str, num_samples: int = 100, digits: Optional[Sequence] = (0, 1, 2), **kwargs):
165160
# number of examples per class
166161
self.num_samples = num_samples
167162
# take just a subset of MNIST dataset
168163
self.digits = sorted(digits) if digits else list(range(10))
169164

170165
self.cache_folder_name = f"digits-{'-'.join(str(d) for d in self.digits)}_nb-{self.num_samples}"
171166

172-
super().__init__(normalize=(0.5, 1.0), **kwargs)
167+
super().__init__(root, normalize=(0.5, 1.0), **kwargs)
173168

174169
@staticmethod
175170
def _prepare_subset(full_data: torch.Tensor, full_targets: torch.Tensor, num_samples: int, digits: Sequence):

tests/helpers/test_datasets.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616
import cloudpickle
1717
import pytest
1818

19+
from tests import PATH_DATASETS
1920
from tests.helpers.datasets import AverageDataset, MNIST, TrialMNIST
2021

2122

22-
@pytest.mark.parametrize('dataset_cls', [MNIST, TrialMNIST, AverageDataset])
23-
def test_pickling_dataset_mnist(tmpdir, dataset_cls):
24-
mnist = dataset_cls()
23+
@pytest.mark.parametrize('dataset_cls,args', [
24+
(MNIST, dict(root=PATH_DATASETS)),
25+
(TrialMNIST, dict(root=PATH_DATASETS)),
26+
(AverageDataset, dict()),
27+
])
28+
def test_pickling_dataset_mnist(tmpdir, dataset_cls, args):
29+
mnist = dataset_cls(**args)
2530

2631
mnist_pickled = pickle.dumps(mnist)
2732
pickle.loads(mnist_pickled)

0 commit comments

Comments
 (0)