test_integration.py 16.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
import dgl
import dgl.graphbolt as gb
import dgl.sparse as dglsp
import torch


def test_integration_link_prediction():
    torch.manual_seed(926)

    indptr = torch.tensor([0, 0, 1, 3, 6, 8, 10])
    indices = torch.tensor([5, 3, 3, 3, 3, 4, 4, 0, 5, 4])

    matrix_a = dglsp.from_csc(indptr, indices)
14
    seeds = torch.t(torch.stack(matrix_a.coo()))
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
    node_feature_data = torch.tensor(
        [
            [0.9634, 0.2294],
            [0.6172, 0.7865],
            [0.2109, 0.1089],
            [0.8672, 0.2276],
            [0.5503, 0.8223],
            [0.5160, 0.2486],
        ]
    )
    edge_feature_data = torch.tensor(
        [
            [0.5123, 0.1709, 0.6150],
            [0.1476, 0.1902, 0.1314],
            [0.2582, 0.5203, 0.6228],
            [0.3708, 0.7631, 0.2683],
            [0.2126, 0.7878, 0.7225],
            [0.7885, 0.3414, 0.5485],
            [0.4088, 0.8200, 0.1851],
            [0.0056, 0.9469, 0.4432],
            [0.8972, 0.7511, 0.3617],
            [0.5773, 0.2199, 0.3366],
        ]
    )

40
    item_set = gb.ItemSet(seeds, names="seeds")
41
    graph = gb.fused_csc_sampling_graph(indptr, indices)
42
43
44
45
46
47
48
49
50

    node_feature = gb.TorchBasedFeature(node_feature_data)
    edge_feature = gb.TorchBasedFeature(edge_feature_data)
    features = {
        ("node", None, "feat"): node_feature,
        ("edge", None, "feat"): edge_feature,
    }
    feature_store = gb.BasicFeatureStore(features)
    datapipe = gb.ItemSampler(item_set, batch_size=4)
51
    datapipe = datapipe.sample_uniform_negative(graph, 2)
52
53
54
55
56
57
    fanouts = torch.LongTensor([1])
    datapipe = datapipe.sample_neighbor(graph, [fanouts, fanouts], replace=True)
    datapipe = datapipe.transform(gb.exclude_seed_edges)
    datapipe = datapipe.fetch_feature(
        feature_store, node_feature_keys=["feat"], edge_feature_keys=["feat"]
    )
58
    dataloader = gb.DataLoader(
59
60
61
62
        datapipe,
    )
    expected = [
        str(
63
64
65
66
67
68
69
70
71
72
73
74
75
76
            """MiniBatch(seeds=tensor([[5, 1],
                        [3, 2],
                        [3, 2],
                        [3, 3],
                        [5, 0],
                        [5, 0],
                        [3, 3],
                        [3, 0],
                        [3, 5],
                        [3, 3],
                        [3, 3],
                        [3, 4]]),
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 1, 2, 2, 2, 3], dtype=torch.int32),
                                                                         indices=tensor([0, 5, 4], dtype=torch.int32),
77
                                                           ),
78
                                               original_row_node_ids=tensor([5, 1, 3, 2, 0, 4]),
79
                                               original_edge_ids=None,
80
                                               original_column_node_ids=tensor([5, 1, 3, 2, 0, 4]),
81
                            ),
82
83
                            SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 1, 1, 1, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([5, 4], dtype=torch.int32),
84
                                                           ),
85
                                               original_row_node_ids=tensor([5, 1, 3, 2, 0, 4]),
86
                                               original_edge_ids=None,
87
                                               original_column_node_ids=tensor([5, 1, 3, 2, 0, 4]),
88
89
90
                            )],
          node_features={'feat': tensor([[0.5160, 0.2486],
                                [0.6172, 0.7865],
91
                                [0.8672, 0.2276],
92
93
94
                                [0.2109, 0.1089],
                                [0.9634, 0.2294],
                                [0.5503, 0.8223]])},
95
96
97
          labels=tensor([1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.]),
          input_nodes=tensor([5, 1, 3, 2, 0, 4]),
          indexes=tensor([0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3]),
98
99
          edge_features=[{},
                        {}],
100
101
102
103
104
105
106
107
108
109
110
111
112
          compacted_seeds=tensor([[0, 1],
                                  [2, 3],
                                  [2, 3],
                                  [2, 2],
                                  [0, 4],
                                  [0, 4],
                                  [2, 2],
                                  [2, 4],
                                  [2, 0],
                                  [2, 2],
                                  [2, 2],
                                  [2, 5]]),
          blocks=[Block(num_src_nodes=6, num_dst_nodes=6, num_edges=3),
113
                 Block(num_src_nodes=6, num_dst_nodes=6, num_edges=2)],
114
       )"""
115
116
        ),
        str(
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            """MiniBatch(seeds=tensor([[3, 3],
                        [4, 3],
                        [4, 4],
                        [0, 4],
                        [3, 1],
                        [3, 5],
                        [4, 2],
                        [4, 5],
                        [4, 4],
                        [4, 3],
                        [0, 1],
                        [0, 5]]),
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 0, 0, 0, 1, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([4, 0], dtype=torch.int32),
131
                                                           ),
132
                                               original_row_node_ids=tensor([3, 4, 0, 1, 5, 2]),
133
                                               original_edge_ids=None,
134
                                               original_column_node_ids=tensor([3, 4, 0, 1, 5, 2]),
135
                            ),
136
137
                            SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 0, 0, 0, 1, 2, 3], dtype=torch.int32),
                                                                         indices=tensor([4, 4, 0], dtype=torch.int32),
138
                                                           ),
139
                                               original_row_node_ids=tensor([3, 4, 0, 1, 5, 2]),
140
                                               original_edge_ids=None,
141
                                               original_column_node_ids=tensor([3, 4, 0, 1, 5, 2]),
142
143
144
145
                            )],
          node_features={'feat': tensor([[0.8672, 0.2276],
                                [0.5503, 0.8223],
                                [0.9634, 0.2294],
146
                                [0.6172, 0.7865],
147
                                [0.5160, 0.2486],
148
                                [0.2109, 0.1089]])},
149
          labels=tensor([1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.]),
150
          input_nodes=tensor([3, 4, 0, 1, 5, 2]),
151
          indexes=tensor([0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3]),
152
153
          edge_features=[{},
                        {}],
154
155
156
157
158
159
160
161
162
163
164
165
166
          compacted_seeds=tensor([[0, 0],
                                  [1, 0],
                                  [1, 1],
                                  [2, 1],
                                  [0, 3],
                                  [0, 4],
                                  [1, 5],
                                  [1, 4],
                                  [1, 1],
                                  [1, 0],
                                  [2, 3],
                                  [2, 4]]),
          blocks=[Block(num_src_nodes=6, num_dst_nodes=6, num_edges=2),
167
                 Block(num_src_nodes=6, num_dst_nodes=6, num_edges=3)],
168
       )"""
169
170
        ),
        str(
171
172
173
174
175
176
            """MiniBatch(seeds=tensor([[5, 5],
                        [4, 5],
                        [5, 0],
                        [5, 4],
                        [4, 0],
                        [4, 1]]),
177
178
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 0, 1, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([1, 0], dtype=torch.int32),
179
                                                           ),
180
                                               original_row_node_ids=tensor([5, 4, 0, 1]),
181
                                               original_edge_ids=None,
182
                                               original_column_node_ids=tensor([5, 4, 0, 1]),
183
                            ),
184
185
                            SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 0, 1, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([1, 0], dtype=torch.int32),
186
                                                           ),
187
                                               original_row_node_ids=tensor([5, 4, 0, 1]),
188
                                               original_edge_ids=None,
189
                                               original_column_node_ids=tensor([5, 4, 0, 1]),
190
191
                            )],
          node_features={'feat': tensor([[0.5160, 0.2486],
192
193
194
                                [0.5503, 0.8223],
                                [0.9634, 0.2294],
                                [0.6172, 0.7865]])},
195
          labels=tensor([1., 1., 0., 0., 0., 0.]),
196
          input_nodes=tensor([5, 4, 0, 1]),
197
          indexes=tensor([0, 1, 0, 0, 1, 1]),
198
199
          edge_features=[{},
                        {}],
200
201
202
203
204
205
          compacted_seeds=tensor([[0, 0],
                                  [1, 0],
                                  [0, 2],
                                  [0, 1],
                                  [1, 2],
                                  [1, 3]]),
206
207
          blocks=[Block(num_src_nodes=4, num_dst_nodes=4, num_edges=2),
                 Block(num_src_nodes=4, num_dst_nodes=4, num_edges=2)],
208
       )"""
209
210
211
212
213
214
215
216
217
218
219
220
        ),
    ]
    for step, data in enumerate(dataloader):
        assert expected[step] == str(data), print(data)


def test_integration_node_classification():
    torch.manual_seed(926)

    indptr = torch.tensor([0, 0, 1, 3, 6, 8, 10])
    indices = torch.tensor([5, 3, 3, 3, 3, 4, 4, 0, 5, 4])

221
    seeds = torch.tensor([5, 1, 2, 4, 3, 0])
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
    node_feature_data = torch.tensor(
        [
            [0.9634, 0.2294],
            [0.6172, 0.7865],
            [0.2109, 0.1089],
            [0.8672, 0.2276],
            [0.5503, 0.8223],
            [0.5160, 0.2486],
        ]
    )
    edge_feature_data = torch.tensor(
        [
            [0.5123, 0.1709, 0.6150],
            [0.1476, 0.1902, 0.1314],
            [0.2582, 0.5203, 0.6228],
            [0.3708, 0.7631, 0.2683],
            [0.2126, 0.7878, 0.7225],
            [0.7885, 0.3414, 0.5485],
            [0.4088, 0.8200, 0.1851],
            [0.0056, 0.9469, 0.4432],
            [0.8972, 0.7511, 0.3617],
            [0.5773, 0.2199, 0.3366],
        ]
    )

247
    item_set = gb.ItemSet(seeds, names="seeds")
248
    graph = gb.fused_csc_sampling_graph(indptr, indices)
249
250
251
252
253
254
255
256

    node_feature = gb.TorchBasedFeature(node_feature_data)
    edge_feature = gb.TorchBasedFeature(edge_feature_data)
    features = {
        ("node", None, "feat"): node_feature,
        ("edge", None, "feat"): edge_feature,
    }
    feature_store = gb.BasicFeatureStore(features)
257
    datapipe = gb.ItemSampler(item_set, batch_size=2)
258
259
260
261
262
    fanouts = torch.LongTensor([1])
    datapipe = datapipe.sample_neighbor(graph, [fanouts, fanouts], replace=True)
    datapipe = datapipe.fetch_feature(
        feature_store, node_feature_keys=["feat"], edge_feature_keys=["feat"]
    )
263
    dataloader = gb.DataLoader(
264
265
266
267
        datapipe,
    )
    expected = [
        str(
268
269
270
            """MiniBatch(seeds=tensor([5, 1]),
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([2, 0], dtype=torch.int32),
271
                                                           ),
272
                                               original_row_node_ids=tensor([5, 1, 4]),
273
                                               original_edge_ids=None,
274
                                               original_column_node_ids=tensor([5, 1]),
275
                            ),
276
277
                            SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([0, 0], dtype=torch.int32),
278
                                                           ),
279
                                               original_row_node_ids=tensor([5, 1]),
280
                                               original_edge_ids=None,
281
                                               original_column_node_ids=tensor([5, 1]),
282
283
284
285
286
                            )],
          node_features={'feat': tensor([[0.5160, 0.2486],
                                [0.6172, 0.7865],
                                [0.5503, 0.8223]])},
          labels=None,
287
          input_nodes=tensor([5, 1, 4]),
288
          indexes=None,
289
290
          edge_features=[{},
                        {}],
291
292
293
          compacted_seeds=None,
          blocks=[Block(num_src_nodes=3, num_dst_nodes=2, num_edges=2),
                 Block(num_src_nodes=2, num_dst_nodes=2, num_edges=2)],
294
       )"""
295
296
        ),
        str(
297
298
299
            """MiniBatch(seeds=tensor([2, 4]),
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 2, 3, 3], dtype=torch.int32),
                                                                         indices=tensor([2, 1, 2], dtype=torch.int32),
300
                                                           ),
301
                                               original_row_node_ids=tensor([2, 4, 3, 0]),
302
                                               original_edge_ids=None,
303
                                               original_column_node_ids=tensor([2, 4, 3, 0]),
304
                            ),
305
306
                            SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([2, 3], dtype=torch.int32),
307
                                                           ),
308
                                               original_row_node_ids=tensor([2, 4, 3, 0]),
309
                                               original_edge_ids=None,
310
                                               original_column_node_ids=tensor([2, 4]),
311
                            )],
312
          node_features={'feat': tensor([[0.2109, 0.1089],
313
                                [0.5503, 0.8223],
314
                                [0.8672, 0.2276],
315
316
                                [0.9634, 0.2294]])},
          labels=None,
317
          input_nodes=tensor([2, 4, 3, 0]),
318
          indexes=None,
319
320
          edge_features=[{},
                        {}],
321
322
323
          compacted_seeds=None,
          blocks=[Block(num_src_nodes=4, num_dst_nodes=4, num_edges=3),
                 Block(num_src_nodes=4, num_dst_nodes=2, num_edges=2)],
324
       )"""
325
326
        ),
        str(
327
328
329
            """MiniBatch(seeds=tensor([3, 0]),
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 1], dtype=torch.int32),
                                                                         indices=tensor([0], dtype=torch.int32),
330
                                                           ),
331
                                               original_row_node_ids=tensor([3, 0]),
332
                                               original_edge_ids=None,
333
                                               original_column_node_ids=tensor([3, 0]),
334
                            ),
335
336
                            SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 1], dtype=torch.int32),
                                                                         indices=tensor([0], dtype=torch.int32),
337
                                                           ),
338
                                               original_row_node_ids=tensor([3, 0]),
339
                                               original_edge_ids=None,
340
                                               original_column_node_ids=tensor([3, 0]),
341
                            )],
342
          node_features={'feat': tensor([[0.8672, 0.2276],
343
344
                                [0.9634, 0.2294]])},
          labels=None,
345
          input_nodes=tensor([3, 0]),
346
          indexes=None,
347
348
          edge_features=[{},
                        {}],
349
350
351
          compacted_seeds=None,
          blocks=[Block(num_src_nodes=2, num_dst_nodes=2, num_edges=1),
                 Block(num_src_nodes=2, num_dst_nodes=2, num_edges=1)],
352
       )"""
353
354
355
        ),
    ]
    for step, data in enumerate(dataloader):
356
        assert expected[step] == str(data), print(step, data)