node_classification.py 14.5 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
"""
This script trains and tests a GraphSAGE model for node classification
on large graphs using GraphBolt dataloader.

Paper: [Inductive Representation Learning on Large Graphs]
(https://arxiv.org/abs/1706.02216)

Unlike previous dgl examples, we've utilized the newly defined dataloader
from GraphBolt. This example will help you grasp how to build an end-to-end
training pipeline using GraphBolt.

Before reading this example, please familiar yourself with graphsage node
classification by reading the example in the
`examples/core/graphsage/node_classification.py`. This introduction,
[A Blitz Introduction to Node Classification with DGL]
(https://docs.dgl.ai/tutorials/blitz/1_introduction.html), might be helpful.

If you want to train graphsage on a large graph in a distributed fashion,
please read the example in the `examples/distributed/graphsage/`.

This flowchart describes the main functional sequence of the provided example:
main

├───> OnDiskDataset pre-processing

├───> Instantiate SAGE model

├───> train
│     │
│     ├───> Get graphbolt dataloader (HIGHLIGHT)
│     │
│     └───> Training loop
│           │
│           ├───> SAGE.forward
│           │
│           └───> Validation set evaluation

38
└───> All nodes set inference & Test set evaluation
39
40
"""
import argparse
41
import time
42
43
44
45
46
47
48

import dgl.graphbolt as gb
import dgl.nn as dglnn
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchmetrics.functional as MF
49
from tqdm import tqdm
50
51


52
def create_dataloader(
53
    graph, features, itemset, batch_size, fanout, device, num_workers, job
54
):
55
56
57
58
    """
    [HIGHLIGHT]
    Get a GraphBolt version of a dataloader for node classification tasks.
    This function demonstrates how to utilize functional forms of datapipes in
59
60
    GraphBolt. For a more detailed tutorial, please read the examples in
    `dgl/notebooks/graphbolt/walkthrough.ipynb`.
61
    Alternatively, you can create a datapipe using its class constructor.
62
63
64

    Parameters
    ----------
65
    job : one of ["train", "evaluate", "infer"]
66
67
        The stage where dataloader is created, with options "train", "evaluate"
        and "infer".
68
    Other parameters are explicated in the comments below.
69
70
71
72
73
74
75
    """

    ############################################################################
    # [Step-1]:
    # gb.ItemSampler()
    # [Input]:
    # 'itemset': The current dataset. (e.g. `train_set` or `valid_set`)
76
    # 'batch_size': Specify the number of samples to be processed together,
77
78
79
    # referred to as a 'mini-batch'. (The term 'mini-batch' is used here to
    # indicate a subset of the entire dataset that is processed together. This
    # is in contrast to processing the entire dataset, known as a 'full batch'.)
80
    # 'job': Determines whether data should be shuffled. (Shuffling is
81
82
83
84
85
86
87
88
89
    # generally used only in training to improve model generalization. It's
    # not used in validation and testing as the focus there is to evaluate
    # performance rather than to learn from the data.)
    # [Output]:
    # An ItemSampler object for handling mini-batch sampling.
    # [Role]:
    # Initialize the ItemSampler to sample mini-batche from the dataset.
    ############################################################################
    datapipe = gb.ItemSampler(
90
        itemset, batch_size=batch_size, shuffle=(job == "train")
91
92
93
94
    )

    ############################################################################
    # [Step-2]:
95
96
97
98
99
100
101
102
103
104
105
106
107
    # self.copy_to()
    # [Input]:
    # 'device': The device to copy the data to.
    # 'extra_attrs': The extra attributes to copy.
    # [Output]:
    # A CopyTo object to copy the data to the specified device. Copying here
    # ensures that the rest of the operations run on the GPU.
    ############################################################################
    if args.storage_device != "cpu":
        datapipe = datapipe.copy_to(device=device, extra_attrs=["seed_nodes"])

    ############################################################################
    # [Step-3]:
108
109
110
    # self.sample_neighbor()
    # [Input]:
    # 'graph': The network topology for sampling.
111
112
    # '[-1] or fanout': Number of neighbors to sample per node. In
    # training or validation, the length of `fanout` should be equal to the
113
114
    # number of layers in the model. In inference, this parameter is set to
    # [-1], indicating that all neighbors of a node are sampled.
115
116
117
118
119
    # [Output]:
    # A NeighborSampler object to sample neighbors.
    # [Role]:
    # Initialize a neighbor sampler for sampling the neighborhoods of nodes.
    ############################################################################
120
    datapipe = datapipe.sample_neighbor(
121
        graph, fanout if job != "infer" else [-1]
122
    )
123
124

    ############################################################################
125
    # [Step-4]:
126
127
128
129
130
131
132
133
    # self.fetch_feature()
    # [Input]:
    # 'features': The node features.
    # 'node_feature_keys': The keys of the node features to be fetched.
    # [Output]:
    # A FeatureFetcher object to fetch node features.
    # [Role]:
    # Initialize a feature fetcher for fetching features of the sampled
134
135
    # subgraphs. This step is skipped in inference because features are updated
    # as a whole during it, thus storing features in minibatch is unnecessary.
136
    ############################################################################
137
138
    if job != "infer":
        datapipe = datapipe.fetch_feature(features, node_feature_keys=["feat"])
139
140

    ############################################################################
141
    # [Step-5]:
142
143
144
145
146
147
    # self.copy_to()
    # [Input]:
    # 'device': The device to copy the data to.
    # [Output]:
    # A CopyTo object to copy the data to the specified device.
    ############################################################################
148
149
    if args.storage_device == "cpu":
        datapipe = datapipe.copy_to(device=device)
150
151

    ############################################################################
152
    # [Step-6]:
153
    # gb.DataLoader()
154
155
    # [Input]:
    # 'datapipe': The datapipe object to be used for data loading.
156
    # 'num_workers': The number of processes to be used for data loading.
157
    # [Output]:
158
    # A DataLoader object to handle data loading.
159
160
161
    # [Role]:
    # Initialize a multi-process dataloader to load the data in parallel.
    ############################################################################
162
    dataloader = gb.DataLoader(datapipe, num_workers=num_workers)
163
164
165
166
167

    # Return the fully-initialized DataLoader object.
    return dataloader


168
169
170
171
172
173
174
175
176
177
178
179
class SAGE(nn.Module):
    def __init__(self, in_size, hidden_size, out_size):
        super().__init__()
        self.layers = nn.ModuleList()
        # Three-layer GraphSAGE-mean.
        self.layers.append(dglnn.SAGEConv(in_size, hidden_size, "mean"))
        self.layers.append(dglnn.SAGEConv(hidden_size, hidden_size, "mean"))
        self.layers.append(dglnn.SAGEConv(hidden_size, out_size, "mean"))
        self.dropout = nn.Dropout(0.5)
        self.hidden_size = hidden_size
        self.out_size = out_size
        # Set the dtype for the layers manually.
180
        self.set_layer_dtype(torch.float32)
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

    def set_layer_dtype(self, _dtype):
        for layer in self.layers:
            for param in layer.parameters():
                param.data = param.data.to(_dtype)

    def forward(self, blocks, x):
        hidden_x = x
        for layer_idx, (layer, block) in enumerate(zip(self.layers, blocks)):
            hidden_x = layer(block, hidden_x)
            is_last_layer = layer_idx == len(self.layers) - 1
            if not is_last_layer:
                hidden_x = F.relu(hidden_x)
                hidden_x = self.dropout(hidden_x)
        return hidden_x

197
    def inference(self, graph, features, dataloader, device):
198
199
200
        """Conduct layer-wise inference to get all the node embeddings."""
        feature = features.read("node", None, "feat")

201
202
203
204
205
        buffer_device = torch.device("cpu")
        # Enable pin_memory for faster CPU to GPU data transfer if the
        # model is running on a GPU.
        pin_memory = buffer_device != device

206
207
208
209
210
211
        for layer_idx, layer in enumerate(self.layers):
            is_last_layer = layer_idx == len(self.layers) - 1

            y = torch.empty(
                graph.total_num_nodes,
                self.out_size if is_last_layer else self.hidden_size,
212
                dtype=torch.float32,
213
214
                device=buffer_device,
                pin_memory=pin_memory,
215
            )
216
            feature = feature.to(device)
217

218
            for step, data in tqdm(enumerate(dataloader)):
219
                x = feature[data.input_nodes]
220
                hidden_x = layer(data.blocks[0], x)  # len(blocks) = 1
221
222
223
224
                if not is_last_layer:
                    hidden_x = F.relu(hidden_x)
                    hidden_x = self.dropout(hidden_x)
                # By design, our output nodes are contiguous.
225
226
227
                y[data.seed_nodes[0] : data.seed_nodes[-1] + 1] = hidden_x.to(
                    buffer_device
                )
228
229
230
231
232
233
234
235
236
237
238
            feature = y

        return y


@torch.no_grad()
def layerwise_infer(
    args, graph, features, test_set, all_nodes_set, model, num_classes
):
    model.eval()
    dataloader = create_dataloader(
239
240
241
242
243
244
245
246
        graph=graph,
        features=features,
        itemset=all_nodes_set,
        batch_size=4 * args.batch_size,
        fanout=[-1],
        device=args.device,
        num_workers=args.num_workers,
        job="infer",
247
    )
248
    pred = model.inference(graph, features, dataloader, args.device)
249
250
251
252
253
254
255
256
257
258
259
    pred = pred[test_set._items[0]]
    label = test_set._items[1].to(pred.device)

    return MF.accuracy(
        pred,
        label,
        task="multiclass",
        num_classes=num_classes,
    )


260
261
262
263
264
265
@torch.no_grad()
def evaluate(args, model, graph, features, itemset, num_classes):
    model.eval()
    y = []
    y_hats = []
    dataloader = create_dataloader(
266
267
268
269
270
271
272
273
        graph=graph,
        features=features,
        itemset=itemset,
        batch_size=args.batch_size,
        fanout=args.fanout,
        device=args.device,
        num_workers=args.num_workers,
        job="evaluate",
274
275
    )

276
    for step, data in tqdm(enumerate(dataloader), "Evaluating"):
277
278
        x = data.node_features["feat"]
        y.append(data.labels)
279
        y_hats.append(model(data.blocks, x))
280

281
    return MF.accuracy(
282
283
284
285
286
287
288
289
290
291
        torch.cat(y_hats),
        torch.cat(y),
        task="multiclass",
        num_classes=num_classes,
    )


def train(args, graph, features, train_set, valid_set, num_classes, model):
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    dataloader = create_dataloader(
292
293
294
295
296
297
298
299
        graph=graph,
        features=features,
        itemset=train_set,
        batch_size=args.batch_size,
        fanout=args.fanout,
        device=args.device,
        num_workers=args.num_workers,
        job="train",
300
301
    )

302
    for epoch in range(args.epochs):
303
        t0 = time.time()
304
305
        model.train()
        total_loss = 0
306
        for step, data in tqdm(enumerate(dataloader), "Training"):
307
308
309
310
311
312
313
314
            # The input features from the source nodes in the first layer's
            # computation graph.
            x = data.node_features["feat"]

            # The ground truth labels from the destination nodes
            # in the last layer's computation graph.
            y = data.labels

315
            y_hat = model(data.blocks, x)
316
317
318
319
320
321
322
323
324
325

            # Compute loss.
            loss = F.cross_entropy(y_hat, y)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            total_loss += loss.item()

326
        t1 = time.time()
327
328
329
330
        # Evaluate the model.
        acc = evaluate(args, model, graph, features, valid_set, num_classes)
        print(
            f"Epoch {epoch:05d} | Loss {total_loss / (step + 1):.4f} | "
331
            f"Accuracy {acc.item():.4f} | Time {t1 - t0:.4f}"
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
        )


def parse_args():
    parser = argparse.ArgumentParser(
        description="A script trains and tests a GraphSAGE model "
        "for node classification using GraphBolt dataloader."
    )
    parser.add_argument(
        "--epochs", type=int, default=10, help="Number of training epochs."
    )
    parser.add_argument(
        "--lr",
        type=float,
        default=0.0005,
        help="Learning rate for optimization.",
    )
    parser.add_argument(
350
        "--batch-size", type=int, default=1024, help="Batch size for training."
351
352
353
354
    )
    parser.add_argument(
        "--num-workers",
        type=int,
355
        default=0,
356
357
358
359
360
        help="Number of workers for data loading.",
    )
    parser.add_argument(
        "--fanout",
        type=str,
361
        default="10,10,10",
362
        help="Fan-out of neighbor sampling. It is IMPORTANT to keep len(fanout)"
363
        " identical with the number of layers in your model. Default: 10,10,10",
364
    )
365
    parser.add_argument(
366
367
368
369
370
        "--mode",
        default="pinned-cuda",
        choices=["cpu-cpu", "cpu-cuda", "pinned-cuda", "cuda-cuda"],
        help="Dataset storage placement and Train device: 'cpu' for CPU and RAM,"
        " 'pinned' for pinned memory in RAM, 'cuda' for GPU and GPU memory.",
371
    )
372
373
374
375
    return parser.parse_args()


def main(args):
376
    if not torch.cuda.is_available():
377
378
379
        args.mode = "cpu-cpu"
    print(f"Training in {args.mode} mode.")
    args.storage_device, args.device = args.mode.split("-")
380
381
    args.device = torch.device(args.device)

382
    # Load and preprocess dataset.
383
    print("Loading data...")
384
385
    dataset = gb.BuiltinDataset("ogbn-products").load()

386
387
388
389
    # Move the dataset to the selected storage.
    graph = dataset.graph.to(args.storage_device)
    features = dataset.feature.to(args.storage_device)

390
391
    train_set = dataset.tasks[0].train_set
    valid_set = dataset.tasks[0].validation_set
392
393
    test_set = dataset.tasks[0].test_set
    all_nodes_set = dataset.all_nodes_set
394
395
396
397
    args.fanout = list(map(int, args.fanout.split(",")))

    num_classes = dataset.tasks[0].metadata["num_classes"]

398
    in_size = features.size("node", None, "feat")[0]
399
    hidden_size = 256
400
401
402
    out_size = num_classes

    model = SAGE(in_size, hidden_size, out_size)
403
404
    assert len(args.fanout) == len(model.layers)
    model = model.to(args.device)
405
406
407
408
409
410
411

    # Model training.
    print("Training...")
    train(args, graph, features, train_set, valid_set, num_classes, model)

    # Test the model.
    print("Testing...")
412
413
414
415
416
417
418
419
    test_acc = layerwise_infer(
        args,
        graph,
        features,
        test_set,
        all_nodes_set,
        model,
        num_classes,
420
    )
421
    print(f"Test accuracy {test_acc.item():.4f}")
422
423
424
425
426


if __name__ == "__main__":
    args = parse_args()
    main(args)