multigpu_node_classification.ipynb 28.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2ppSJal9At7-"
      },
      "source": [
        "# Multi-GPU Node Classification\n",
        "\n",
        "This tutorial shows how to train a multi-layer GraphSAGE for node classification on the `ogbn-products` dataset provided by [Open Graph\n",
        "Benchmark (OGB)](https://ogb.stanford.edu/). The dataset contains around 2.4 million nodes and 62 million edges.\n",
        "\n",
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/dmlc/dgl/blob/master/notebooks/stochastic_training/multigpu_node_classification.ipynb)\n",
        "[![GitHub](https://img.shields.io/badge/-View%20on%20GitHub-181717?logo=github&logoColor=ffffff)](https://github.com/dmlc/dgl/blob/master/notebooks/stochastic_training/multigpu_node_classification.ipynb)\n",
        "\n",
        "By the end of this tutorial, you will be able to\n",
        "\n",
        "- Train a GNN model for node classification on multiple GPUs with DGL's neighbor sampling components. After learning how to use multiple GPUs, you will\n",
        "be able to extend it to other scenarios such as link prediction."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mzZKrVVk6Y_8"
      },
      "source": [
        "## Install DGL package and other dependencies"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "QTCc1RrD_5Id"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
            "Looking in links: https://data.dgl.ai/wheels-test/cu121/repo.html\n",
            "Requirement already satisfied: dgl in /localscratch/dgl-3/python (2.1)\n",
            "Requirement already satisfied: numpy>=1.14.0 in /usr/local/lib/python3.10/dist-packages (from dgl) (1.24.4)\n",
            "Requirement already satisfied: scipy>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from dgl) (1.11.4)\n",
            "Requirement already satisfied: networkx>=2.1 in /usr/local/lib/python3.10/dist-packages (from dgl) (2.6.3)\n",
            "Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from dgl) (2.31.0)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from dgl) (4.66.1)\n",
            "Requirement already satisfied: psutil>=5.8.0 in /usr/local/lib/python3.10/dist-packages (from dgl) (5.9.4)\n",
            "Requirement already satisfied: torchdata>=0.5.0 in /usr/local/lib/python3.10/dist-packages (from dgl) (0.7.0a0)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->dgl) (3.3.2)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->dgl) (3.6)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->dgl) (1.26.18)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->dgl) (2023.11.17)\n",
            "Requirement already satisfied: torch>=2 in /usr/local/lib/python3.10/dist-packages (from torchdata>=0.5.0->dgl) (2.2.0a0+81ea7a4)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch>=2->torchdata>=0.5.0->dgl) (3.13.1)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from torch>=2->torchdata>=0.5.0->dgl) (4.8.0)\n",
            "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=2->torchdata>=0.5.0->dgl) (1.12)\n",
            "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=2->torchdata>=0.5.0->dgl) (3.1.2)\n",
            "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch>=2->torchdata>=0.5.0->dgl) (2023.12.0)\n",
            "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=2->torchdata>=0.5.0->dgl) (2.1.3)\n",
            "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=2->torchdata>=0.5.0->dgl) (1.3.0)\n",
            "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
            "\u001b[0m\n",
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n",
            "Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
            "Requirement already satisfied: torchmetrics in /usr/local/lib/python3.10/dist-packages (1.3.0.post0)\n",
            "Requirement already satisfied: multiprocess in /usr/local/lib/python3.10/dist-packages (0.70.16)\n",
            "Requirement already satisfied: numpy>1.20.0 in /usr/local/lib/python3.10/dist-packages (from torchmetrics) (1.24.4)\n",
            "Requirement already satisfied: packaging>17.1 in /usr/local/lib/python3.10/dist-packages (from torchmetrics) (23.2)\n",
            "Requirement already satisfied: torch>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from torchmetrics) (2.2.0a0+81ea7a4)\n",
            "Requirement already satisfied: lightning-utilities>=0.8.0 in /usr/local/lib/python3.10/dist-packages (from torchmetrics) (0.10.1)\n",
            "Requirement already satisfied: dill>=0.3.8 in /usr/local/lib/python3.10/dist-packages (from multiprocess) (0.3.8)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from lightning-utilities>=0.8.0->torchmetrics) (68.2.2)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from lightning-utilities>=0.8.0->torchmetrics) (4.8.0)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->torchmetrics) (3.13.1)\n",
            "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->torchmetrics) (1.12)\n",
            "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->torchmetrics) (2.6.3)\n",
            "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->torchmetrics) (3.1.2)\n",
            "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->torchmetrics) (2023.12.0)\n",
            "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.10.0->torchmetrics) (2.1.3)\n",
            "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.10.0->torchmetrics) (1.3.0)\n",
            "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
            "\u001b[0m\n",
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n",
            "DGL installed!\n"
          ]
        }
      ],
      "source": [
        "# Install required packages.\n",
        "import os\n",
        "import torch\n",
        "os.environ['TORCH'] = torch.__version__\n",
        "os.environ['DGLBACKEND'] = \"pytorch\"\n",
        "\n",
        "# Install the CUDA version. If you want to install CPU version, please\n",
        "# refer to https://www.dgl.ai/pages/start.html.\n",
        "!pip install --pre dgl -f https://data.dgl.ai/wheels-test/cu121/repo.html\n",
        "!pip install torchmetrics multiprocess\n",
        "\n",
        "try:\n",
        "    import dgl\n",
        "    import dgl.graphbolt as gb\n",
        "    installed = True\n",
        "except ImportError as error:\n",
        "    installed = False\n",
        "    print(error)\n",
        "print(\"DGL installed!\" if installed else \"DGL not found!\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "q7GrcJTnZQjt"
      },
      "source": [
        "## Defining Neighbor Sampler and Data Loader in DGL\n",
        "\n",
        "The major difference from the previous tutorial is that we will use `DistributedItemSampler` instead of `ItemSampler` to sample mini-batches of nodes. `DistributedItemSampler` is a distributed version of `ItemSampler` that works with `DistributedDataParallel`. It is implemented as a wrapper around `ItemSampler` and will sample the same minibatch on all replicas. It also supports dropping the last non-full minibatch to avoid the need for padding.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "eel0Wn_aEYAd"
      },
      "outputs": [],
      "source": [
        "def create_dataloader(graph, features, itemset, device, is_train):\n",
        "    datapipe = gb.DistributedItemSampler(\n",
        "        item_set=itemset,\n",
        "        batch_size=1024,\n",
        "        drop_last=is_train,\n",
        "        shuffle=is_train,\n",
        "        drop_uneven_inputs=is_train,\n",
        "    )\n",
        "    datapipe = datapipe.copy_to(device, extra_attrs=[\"seed_nodes\"])\n",
        "    # Now that we have moved to device, sample_neighbor and fetch_feature steps\n",
        "    # will be executed on GPUs.\n",
        "    datapipe = datapipe.sample_neighbor(graph, [10, 10, 10])\n",
        "    datapipe = datapipe.fetch_feature(features, node_feature_keys=[\"feat\"])\n",
        "    return gb.DataLoader(datapipe)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uswPlvOLF1IX"
      },
      "source": [
        "## Weighted reduction across GPUs\n",
        "\n",
        "As the different GPUs might process differing numbers of data points, we define a function to compute the exact average of values such as loss or accuracy in a\n",
        "weighted manner.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "VXP0hmzVGKnp"
      },
      "outputs": [],
      "source": [
        "import torch.distributed as dist\n",
        "\n",
        "def weighted_reduce(tensor, weight, dst=0):\n",
        "    ########################################################################\n",
        "    # (HIGHLIGHT) Collect accuracy and loss values from sub-processes and\n",
        "    # obtain overall average values.\n",
        "    #\n",
        "    # `torch.distributed.reduce` is used to reduce tensors from all the\n",
        "    # sub-processes to a specified process, ReduceOp.SUM is used by default.\n",
        "    #\n",
        "    # Because the GPUs may have differing numbers of processed items, we\n",
        "    # perform a weighted mean to calculate the exact loss and accuracy.\n",
        "    ########################################################################\n",
        "    dist.reduce(tensor=tensor, dst=dst)\n",
        "    weight = torch.tensor(weight, device=tensor.device)\n",
        "    dist.reduce(tensor=weight, dst=dst)\n",
        "    return tensor / weight"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fV6epnRxbZl4"
      },
      "source": [
        "## Defining Model\n",
        "Let’s consider training a 3-layer GraphSAGE with neighbor sampling. The model can be written as follows:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "ft9Ldg-yEsa5"
      },
      "outputs": [],
      "source": [
        "from torch import nn\n",
        "import torch.nn.functional as F\n",
        "from dgl.nn import SAGEConv\n",
        "\n",
        "class SAGE(nn.Module):\n",
        "    def __init__(self, in_size, hidden_size, out_size):\n",
        "        super().__init__()\n",
        "        self.layers = nn.ModuleList()\n",
        "        # Three-layer GraphSAGE-mean.\n",
        "        self.layers.append(SAGEConv(in_size, hidden_size, \"mean\"))\n",
        "        self.layers.append(SAGEConv(hidden_size, hidden_size, \"mean\"))\n",
        "        self.layers.append(SAGEConv(hidden_size, out_size, \"mean\"))\n",
        "        self.dropout = nn.Dropout(0.5)\n",
        "        self.hidden_size = hidden_size\n",
        "        self.out_size = out_size\n",
        "        # Set the dtype for the layers manually.\n",
        "        self.float()\n",
        "\n",
        "    def forward(self, blocks, x):\n",
        "        hidden_x = x\n",
        "        for layer_idx, (layer, block) in enumerate(zip(self.layers, blocks)):\n",
        "            hidden_x = layer(block, hidden_x)\n",
        "            is_last_layer = layer_idx == len(self.layers) - 1\n",
        "            if not is_last_layer:\n",
        "                hidden_x = F.relu(hidden_x)\n",
        "                hidden_x = self.dropout(hidden_x)\n",
        "        return hidden_x"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CjuvDKDVGbPW"
      },
      "source": [
        "## Evaluation function\n",
        "\n",
        "The evaluation function can be used to calculate the validation accuracy during training or the testing accuracy at the end of the training. The difference from\n",
        "the previous tutorial is that we need to return the number of items processed\n",
        "by each GPU to take a weighted average."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "j4djoX9tG7Ib"
      },
      "outputs": [],
      "source": [
        "import torchmetrics.functional as MF\n",
        "import tqdm\n",
        "\n",
        "@torch.no_grad()\n",
        "def evaluate(rank, model, graph, features, itemset, num_classes, device):\n",
        "    model.eval()\n",
        "    y = []\n",
        "    y_hats = []\n",
        "    dataloader = create_dataloader(\n",
        "        graph,\n",
        "        features,\n",
        "        itemset,\n",
        "        device,\n",
        "        is_train=False,\n",
        "    )\n",
        "\n",
        "    for data in tqdm.tqdm(dataloader) if rank == 0 else dataloader:\n",
        "        blocks = data.blocks\n",
        "        x = data.node_features[\"feat\"]\n",
        "        y.append(data.labels)\n",
        "        y_hats.append(model.module(blocks, x))\n",
        "\n",
        "    res = MF.accuracy(\n",
        "        torch.cat(y_hats),\n",
        "        torch.cat(y),\n",
        "        task=\"multiclass\",\n",
        "        num_classes=num_classes,\n",
        "    )\n",
        "\n",
        "    return res.to(device), sum(y_i.size(0) for y_i in y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kN5BbnR4HSU2"
      },
      "source": [
        "## Training Loop\n",
        "\n",
        "The training loop is almost identical to the previous tutorial. In this tutorial, we explicitly disable uneven inputs coming from the dataloader, however, the Join Context Manager could be used to train possibly with incomplete batches at the end of epochs. Please refer to [this tutorial](https://pytorch.org/tutorials/advanced/generic_join.html) for more information."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "bdOceP3yH-eI"
      },
      "outputs": [],
      "source": [
        "import time\n",
        "\n",
        "def train(\n",
        "    rank,\n",
        "    graph,\n",
        "    features,\n",
        "    train_set,\n",
        "    valid_set,\n",
        "    num_classes,\n",
        "    model,\n",
        "    device,\n",
        "):\n",
        "    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n",
        "    # Create training data loader.\n",
        "    dataloader = create_dataloader(\n",
        "        graph,\n",
        "        features,\n",
        "        train_set,\n",
        "        device,\n",
        "        is_train=True,\n",
        "    )\n",
        "\n",
        "    for epoch in range(5):\n",
        "        epoch_start = time.time()\n",
        "\n",
        "        model.train()\n",
        "        total_loss = torch.tensor(0, dtype=torch.float, device=device)\n",
        "        num_train_items = 0\n",
        "        for data in tqdm.tqdm(dataloader) if rank == 0 else dataloader:\n",
        "            # The input features are from the source nodes in the first\n",
        "            # layer's computation graph.\n",
        "            x = data.node_features[\"feat\"]\n",
        "\n",
        "            # The ground truth labels are from the destination nodes\n",
        "            # in the last layer's computation graph.\n",
        "            y = data.labels\n",
        "\n",
        "            blocks = data.blocks\n",
        "\n",
        "            y_hat = model(blocks, x)\n",
        "\n",
        "            # Compute loss.\n",
        "            loss = F.cross_entropy(y_hat, y)\n",
        "\n",
        "            optimizer.zero_grad()\n",
        "            loss.backward()\n",
        "            optimizer.step()\n",
        "\n",
        "            total_loss += loss.detach() * y.size(0)\n",
        "            num_train_items += y.size(0)\n",
        "\n",
        "        # Evaluate the model.\n",
        "        if rank == 0:\n",
        "            print(\"Validating...\")\n",
        "        acc, num_val_items = evaluate(\n",
        "            rank,\n",
        "            model,\n",
        "            graph,\n",
        "            features,\n",
        "            valid_set,\n",
        "            num_classes,\n",
        "            device,\n",
        "        )\n",
        "        total_loss = weighted_reduce(total_loss, num_train_items)\n",
        "        acc = weighted_reduce(acc * num_val_items, num_val_items)\n",
        "\n",
        "        # We synchronize before measuring the epoch time.\n",
        "        torch.cuda.synchronize()\n",
        "        epoch_end = time.time()\n",
        "        if rank == 0:\n",
        "            print(\n",
        "                f\"Epoch {epoch:05d} | \"\n",
        "                f\"Average Loss {total_loss.item():.4f} | \"\n",
        "                f\"Accuracy {acc.item():.4f} | \"\n",
        "                f\"Time {epoch_end - epoch_start:.4f}\"\n",
        "            )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mA-Xu37uIHc4"
      },
      "source": [
        "## Defining Training and Evaluation Procedures\n",
        "\n",
        "The following code defines the main function for each process. It is similar to the previous tutorial except that we need to initialize a distributed training context with `torch.distributed` and wrap the model with `torch.nn.parallel.DistributedDataParallel`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "sW__HeslIMTT"
      },
      "outputs": [],
      "source": [
        "def run(rank, world_size, devices, dataset):\n",
        "    # Set up multiprocessing environment.\n",
        "    device = devices[rank]\n",
        "    torch.cuda.set_device(device)\n",
        "    dist.init_process_group(\n",
        "        backend=\"nccl\",  # Use NCCL backend for distributed GPU training\n",
        "        init_method=\"tcp://127.0.0.1:12345\",\n",
        "        world_size=world_size,\n",
        "        rank=rank,\n",
        "    )\n",
        "\n",
        "    # Pin the graph and features in-place to enable GPU access.\n",
        "    graph = dataset.graph.pin_memory_()\n",
        "    features = dataset.feature.pin_memory_()\n",
        "    train_set = dataset.tasks[0].train_set\n",
        "    valid_set = dataset.tasks[0].validation_set\n",
        "    num_classes = dataset.tasks[0].metadata[\"num_classes\"]\n",
        "\n",
        "    in_size = features.size(\"node\", None, \"feat\")[0]\n",
        "    hidden_size = 256\n",
        "    out_size = num_classes\n",
        "\n",
        "    # Create GraphSAGE model. It should be copied onto a GPU as a replica.\n",
        "    model = SAGE(in_size, hidden_size, out_size).to(device)\n",
        "    model = nn.parallel.DistributedDataParallel(model)\n",
        "\n",
        "    # Model training.\n",
        "    if rank == 0:\n",
        "        print(\"Training...\")\n",
        "    train(\n",
        "        rank,\n",
        "        graph,\n",
        "        features,\n",
        "        train_set,\n",
        "        valid_set,\n",
        "        num_classes,\n",
        "        model,\n",
        "        device,\n",
        "    )\n",
        "\n",
        "    # Test the model.\n",
        "    if rank == 0:\n",
        "        print(\"Testing...\")\n",
        "    test_set = dataset.tasks[0].test_set\n",
        "    test_acc, num_test_items = evaluate(\n",
        "        rank,\n",
        "        model,\n",
        "        graph,\n",
        "        features,\n",
        "        itemset=test_set,\n",
        "        num_classes=num_classes,\n",
        "        device=device,\n",
        "    )\n",
        "    test_acc = weighted_reduce(test_acc * num_test_items, num_test_items)\n",
        "\n",
        "    if rank == 0:\n",
        "        print(f\"Test Accuracy {test_acc.item():.4f}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qMzt0aBFIfbS"
      },
      "source": [
        "## Spawning Trainer Processes\n",
        "\n",
        "The following code spawns a process for each GPU and calls the run function defined above."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "5Dt95eSVIiyM"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Training with 1 gpus.\n",
            "The dataset is already preprocessed.\n",
            "Training...\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "192it [00:09, 21.32it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Validating...\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "39it [00:00, 78.32it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Epoch 00000 | Average Loss 1.2953 | Accuracy 0.8556 | Time 9.5520\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "192it [00:03, 61.08it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Validating...\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "39it [00:00, 79.10it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Epoch 00001 | Average Loss 0.5859 | Accuracy 0.8788 | Time 3.6609\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "192it [00:03, 62.82it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Validating...\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "39it [00:00, 80.55it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Epoch 00002 | Average Loss 0.4858 | Accuracy 0.8852 | Time 3.5646\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "192it [00:03, 60.34it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Validating...\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "39it [00:00, 44.41it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Epoch 00003 | Average Loss 0.4407 | Accuracy 0.8920 | Time 4.0852\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "192it [00:03, 58.87it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Validating...\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "39it [00:00, 78.52it/s]\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Epoch 00004 | Average Loss 0.4122 | Accuracy 0.8943 | Time 3.7938\n",
            "Testing...\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "2162it [00:24, 89.75it/s]"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Test Accuracy 0.7514\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\n"
          ]
        }
      ],
      "source": [
        "import torch.multiprocessing as mp\n",
        "\n",
        "def main():\n",
        "    if not torch.cuda.is_available():\n",
        "        print(\"No GPU found!\")\n",
        "        return\n",
        "\n",
        "    devices = [\n",
        "        torch.device(f\"cuda:{i}\") for i in range(torch.cuda.device_count())\n",
        "    ][:1]\n",
        "    world_size = len(devices)\n",
        "\n",
        "    print(f\"Training with {world_size} gpus.\")\n",
        "\n",
        "    # Load and preprocess dataset.\n",
        "    dataset = gb.BuiltinDataset(\"ogbn-products\").load()\n",
        "\n",
        "    # Thread limiting to avoid resource competition.\n",
        "    os.environ[\"OMP_NUM_THREADS\"] = str(mp.cpu_count() // 2 // world_size)\n",
        "\n",
        "    if world_size > 1:\n",
        "        # The following launch method is not supported in a notebook.\n",
        "        mp.set_sharing_strategy(\"file_system\")\n",
        "        mp.spawn(\n",
        "            run,\n",
        "            args=(world_size, devices, dataset),\n",
        "            nprocs=world_size,\n",
        "            join=True,\n",
        "        )\n",
        "    else:\n",
        "        run(0, 1, devices, dataset)\n",
        "\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    main()"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}