Skip to content

Commit d3eb360

Browse files
authored
[skip ci] Updated assets/tldr/teaser.ipynb (#2871)
1 parent 67c6709 commit d3eb360

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

assets/tldr/teaser.ipynb

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"outputs": [],
2424
"source": [
2525
"# Install PyTorch-Ignite\n",
26-
"!pip install pytorch-ignite"
26+
"!pip install -q pytorch-ignite"
2727
]
2828
},
2929
{
@@ -45,11 +45,13 @@
4545
"import os\n",
4646
"\n",
4747
"in_colab = \"COLAB_TPU_ADDR\" in os.environ\n",
48-
"with_torch_launch = \"WORLD_SIZE\" in os.environ\n",
48+
"with_torchrun = \"WORLD_SIZE\" in os.environ\n",
4949
"\n",
5050
"if in_colab:\n",
5151
" VERSION = !curl -s https://api.github.com/repos/pytorch/xla/releases/latest | grep -Po '\"tag_name\": \"v\\K.*?(?=\")'\n",
52-
" !pip install cloud-tpu-client==0.10 https://storage.googleapis.com/tpu-pytorch/wheels/torch_xla-{VERSION[0]}-cp37-cp37m-linux_x86_64.whl"
52+
" !pip install --upgrade -q cloud-tpu-client==0.10 torch=={VERSION[0]} torchvision https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{VERSION[0][:-2]}-cp38-cp38-linux_x86_64.whl\n",
53+
"\n",
54+
"!pip list | grep torch"
5355
]
5456
},
5557
{
@@ -88,9 +90,18 @@
8890
"\n",
8991
"\n",
9092
"def get_train_test_datasets(path):\n",
93+
" # - Get train/test datasets\n",
94+
" if idist.get_rank() > 0:\n",
95+
" # Ensure that only rank 0 download the dataset\n",
96+
" idist.barrier()\n",
97+
"\n",
9198
" train_ds = datasets.CIFAR10(root=path, train=True, download=True, transform=train_transform)\n",
9299
" test_ds = datasets.CIFAR10(root=path, train=False, download=False, transform=test_transform)\n",
93100
"\n",
101+
" if idist.get_rank() == 0:\n",
102+
" # Ensure that only rank 0 download the dataset\n",
103+
" idist.barrier()\n",
104+
"\n",
94105
" return train_ds, test_ds\n",
95106
"\n",
96107
"\n",
@@ -105,17 +116,8 @@
105116
"\n",
106117
"def get_dataflow(config):\n",
107118
"\n",
108-
" # - Get train/test datasets\n",
109-
" if idist.get_rank() > 0:\n",
110-
" # Ensure that only rank 0 download the dataset\n",
111-
" idist.barrier()\n",
112-
"\n",
113119
" train_dataset, test_dataset = get_train_test_datasets(config.get(\"data_path\", \".\"))\n",
114120
"\n",
115-
" if idist.get_rank() == 0:\n",
116-
" # Ensure that only rank 0 download the dataset\n",
117-
" idist.barrier()\n",
118-
"\n",
119121
" # Setup data loader also adapted to distributed config: nccl, gloo, xla-tpu\n",
120122
" train_loader = idist.auto_dataloader(\n",
121123
" train_dataset,\n",
@@ -402,10 +404,10 @@
402404
"# --- Single computation device ---\n",
403405
"# $ python main.py\n",
404406
"#\n",
405-
"if __name__ == \"__main__\" and not (in_colab or with_torch_launch):\n",
407+
"if __name__ == \"__main__\" and not (in_colab or with_torchrun):\n",
406408
"\n",
407-
" backend = None # or \"nccl\", \"gloo\", \"xla-tpu\" ...\n",
408-
" nproc_per_node = None # or N to spawn N processes\n",
409+
" backend = None\n",
410+
" nproc_per_node = None\n",
409411
" config = {\n",
410412
" \"model\": \"resnet18\",\n",
411413
" \"dataset\": \"cifar10\",\n",
@@ -416,12 +418,12 @@
416418
"\n",
417419
"\n",
418420
"# --- Multiple GPUs ---\n",
419-
"# $ python -m torch.distributed.launch --nproc_per_node=2 --use_env main.py\n",
421+
"# $ torchrun --nproc_per_node=2 main.py\n",
420422
"#\n",
421-
"if __name__ == \"__main__\" and with_torch_launch:\n",
423+
"if __name__ == \"__main__\" and with_torchrun:\n",
422424
"\n",
423-
" backend = \"nccl\" # or \"nccl\", \"gloo\", \"xla-tpu\" ...\n",
424-
" nproc_per_node = None # or N to spawn N processes\n",
425+
" backend = \"nccl\" # or \"nccl\", \"gloo\"\n",
426+
" nproc_per_node = None\n",
425427
" config = {\n",
426428
" \"model\": \"resnet18\",\n",
427429
" \"dataset\": \"cifar10\",\n",
@@ -435,8 +437,8 @@
435437
"#\n",
436438
"if in_colab:\n",
437439
"\n",
438-
" backend = \"xla-tpu\" # or \"nccl\", \"gloo\", \"xla-tpu\" ...\n",
439-
" nproc_per_node = 8 # or N to spawn N processes\n",
440+
" backend = \"xla-tpu\"\n",
441+
" nproc_per_node = 8\n",
440442
" config = {\n",
441443
" \"model\": \"resnet18\",\n",
442444
" \"dataset\": \"cifar10\",\n",
@@ -465,7 +467,7 @@
465467
],
466468
"metadata": {
467469
"kernelspec": {
468-
"display_name": "Python 3",
470+
"display_name": "Python 3.10.6 64-bit",
469471
"language": "python",
470472
"name": "python3"
471473
},
@@ -479,7 +481,12 @@
479481
"name": "python",
480482
"nbconvert_exporter": "python",
481483
"pygments_lexer": "ipython3",
482-
"version": "3.7.7"
484+
"version": "3.10.6"
485+
},
486+
"vscode": {
487+
"interpreter": {
488+
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
489+
}
483490
}
484491
},
485492
"nbformat": 4,

assets/tldr/teaser.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from ignite.metrics import Accuracy
1616

1717
in_colab = "COLAB_TPU_ADDR" in os.environ
18-
with_torch_launch = "WORLD_SIZE" in os.environ
18+
with_torchrun = "WORLD_SIZE" in os.environ
1919

2020
train_transform = Compose(
2121
[
@@ -31,9 +31,18 @@
3131

3232

3333
def get_train_test_datasets(path):
34+
# - Get train/test datasets
35+
if idist.get_rank() > 0:
36+
# Ensure that only rank 0 download the dataset
37+
idist.barrier()
38+
3439
train_ds = datasets.CIFAR10(root=path, train=True, download=True, transform=train_transform)
3540
test_ds = datasets.CIFAR10(root=path, train=False, download=False, transform=test_transform)
3641

42+
if idist.get_rank() == 0:
43+
# Ensure that only rank 0 download the dataset
44+
idist.barrier()
45+
3746
return train_ds, test_ds
3847

3948

@@ -48,17 +57,8 @@ def get_model(name):
4857

4958
def get_dataflow(config):
5059

51-
# - Get train/test datasets
52-
if idist.get_rank() > 0:
53-
# Ensure that only rank 0 download the dataset
54-
idist.barrier()
55-
5660
train_dataset, test_dataset = get_train_test_datasets(config.get("data_path", "."))
5761

58-
if idist.get_rank() == 0:
59-
# Ensure that only rank 0 download the dataset
60-
idist.barrier()
61-
6262
# Setup data loader also adapted to distributed config: nccl, gloo, xla-tpu
6363
train_loader = idist.auto_dataloader(
6464
train_dataset,
@@ -175,10 +175,10 @@ def evaluate_model():
175175
# --- Single computation device ---
176176
# $ python main.py
177177
#
178-
if __name__ == "__main__" and not (in_colab or with_torch_launch):
178+
if __name__ == "__main__" and not (in_colab or with_torchrun):
179179

180-
backend = None # or "nccl", "gloo", "xla-tpu" ...
181-
nproc_per_node = None # or N to spawn N processes
180+
backend = None
181+
nproc_per_node = None
182182
config = {
183183
"model": "resnet18",
184184
"dataset": "cifar10",
@@ -189,12 +189,12 @@ def evaluate_model():
189189

190190

191191
# --- Multiple GPUs ---
192-
# $ python -m torch.distributed.launch --nproc_per_node=2 --use_env main.py
192+
# $ torchrun --nproc_per_node=2 main.py
193193
#
194-
if __name__ == "__main__" and with_torch_launch:
194+
if __name__ == "__main__" and with_torchrun:
195195

196-
backend = "nccl" # or "nccl", "gloo", "xla-tpu" ...
197-
nproc_per_node = None # or N to spawn N processes
196+
backend = "nccl" # or "nccl", "gloo", ...
197+
nproc_per_node = None
198198
config = {
199199
"model": "resnet18",
200200
"dataset": "cifar10",
@@ -208,8 +208,8 @@ def evaluate_model():
208208
#
209209
if in_colab:
210210

211-
backend = "xla-tpu" # or "nccl", "gloo", "xla-tpu" ...
212-
nproc_per_node = 8 # or N to spawn N processes
211+
backend = "xla-tpu"
212+
nproc_per_node = 8
213213
config = {
214214
"model": "resnet18",
215215
"dataset": "cifar10",

0 commit comments

Comments
 (0)