test_specialization.py 19.6 KB
Newer Older
1
import torch as th
2
3
import numpy as np
import scipy.sparse as sp
4
5
import dgl
import dgl.function as fn
6
import utils as U
7

Minjie Wang's avatar
Minjie Wang committed
8
9
D = 5

10
def generate_graph():
11
    g = dgl.DGLGraph()
Minjie Wang's avatar
Minjie Wang committed
12
    g.add_nodes(10)
13
14
15
16
17
18
    # create a graph where 0 is the source and 9 is the sink
    for i in range(1, 9):
        g.add_edge(0, i)
        g.add_edge(i, 9)
    # add a back flow from 9 to 0
    g.add_edge(9, 0)
19
20
21
    g.set_n_repr({'f1' : th.randn(10,), 'f2' : th.randn(10, D)})
    weights = th.randn(17,)
    g.set_e_repr({'e1': weights, 'e2': th.unsqueeze(weights, 1)})
22
23
    return g

24
def test_v2v_update_all():
25
    def _test(fld):
26
27
        def message_func(edges):
            return {'m' : edges.src[fld]}
28

29
30
31
        def message_func_edge(edges):
            if len(edges.src[fld].shape) == 1:
                return {'m' : edges.src[fld] * edges.data['e1']}
32
            else:
33
                return {'m' : edges.src[fld] * edges.data['e2']}
34

35
36
        def reduce_func(nodes):
            return {fld : th.sum(nodes.mailbox['m'], 1)}
37

38
39
        def apply_func(nodes):
            return {fld : 2 * nodes.data[fld]}
40
41
        g = generate_graph()
        # update all
42
        v1 = g.ndata[fld]
Minjie Wang's avatar
Minjie Wang committed
43
        g.update_all(fn.copy_src(src=fld, out='m'), fn.sum(msg='m', out=fld), apply_func)
44
        v2 = g.ndata[fld]
45
        g.set_n_repr({fld : v1})
Minjie Wang's avatar
Minjie Wang committed
46
        g.update_all(message_func, reduce_func, apply_func)
47
        v3 = g.ndata[fld]
48
        assert U.allclose(v2, v3)
49
        # update all with edge weights
50
        v1 = g.ndata[fld]
Minjie Wang's avatar
Minjie Wang committed
51
52
        g.update_all(fn.src_mul_edge(src=fld, edge='e1', out='m'),
                fn.sum(msg='m', out=fld), apply_func)
53
        v2 = g.ndata[fld]
54
        g.set_n_repr({fld : v1})
Minjie Wang's avatar
Minjie Wang committed
55
56
        g.update_all(fn.src_mul_edge(src=fld, edge='e2', out='m'),
                fn.sum(msg='m', out=fld), apply_func)
57
        v3 = g.ndata[fld]
58
        g.set_n_repr({fld : v1})
Minjie Wang's avatar
Minjie Wang committed
59
        g.update_all(message_func_edge, reduce_func, apply_func)
60
        v4 = g.ndata[fld]
61
62
        assert U.allclose(v2, v3)
        assert U.allclose(v3, v4)
63
64
65
66
67
    # test 1d node features
    _test('f1')
    # test 2d node features
    _test('f2')

68
def test_v2v_snr():
Minjie Wang's avatar
Minjie Wang committed
69
70
    u = th.tensor([0, 0, 0, 3, 4, 9])
    v = th.tensor([1, 2, 3, 9, 9, 0])
71
    def _test(fld):
72
73
        def message_func(edges):
            return {'m' : edges.src[fld]}
74

75
76
77
        def message_func_edge(edges):
            if len(edges.src[fld].shape) == 1:
                return {'m' : edges.src[fld] * edges.data['e1']}
78
            else:
79
                return {'m' : edges.src[fld] * edges.data['e2']}
80

81
82
        def reduce_func(nodes):
            return {fld : th.sum(nodes.mailbox['m'], 1)}
83

84
85
        def apply_func(nodes):
            return {fld : 2 * nodes.data[fld]}
86
87
        g = generate_graph()
        # send and recv
88
89
        v1 = g.ndata[fld]
        g.send_and_recv((u, v), fn.copy_src(src=fld, out='m'),
Minjie Wang's avatar
Minjie Wang committed
90
                fn.sum(msg='m', out=fld), apply_func)
91
        v2 = g.ndata[fld]
92
        g.set_n_repr({fld : v1})
93
94
        g.send_and_recv((u, v), message_func, reduce_func, apply_func)
        v3 = g.ndata[fld]
95
        assert U.allclose(v2, v3)
96
        # send and recv with edge weights
97
98
        v1 = g.ndata[fld]
        g.send_and_recv((u, v), fn.src_mul_edge(src=fld, edge='e1', out='m'),
Minjie Wang's avatar
Minjie Wang committed
99
                fn.sum(msg='m', out=fld), apply_func)
100
        v2 = g.ndata[fld]
101
        g.set_n_repr({fld : v1})
102
        g.send_and_recv((u, v), fn.src_mul_edge(src=fld, edge='e2', out='m'),
Minjie Wang's avatar
Minjie Wang committed
103
                fn.sum(msg='m', out=fld), apply_func)
104
        v3 = g.ndata[fld]
105
        g.set_n_repr({fld : v1})
106
107
        g.send_and_recv((u, v), message_func_edge, reduce_func, apply_func)
        v4 = g.ndata[fld]
108
109
        assert U.allclose(v2, v3)
        assert U.allclose(v3, v4)
110
111
112
113
    # test 1d node features
    _test('f1')
    # test 2d node features
    _test('f2')
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

def test_v2v_pull():
    nodes = th.tensor([1, 2, 3, 9])
    def _test(fld):
        def message_func(edges):
            return {'m' : edges.src[fld]}

        def message_func_edge(edges):
            if len(edges.src[fld].shape) == 1:
                return {'m' : edges.src[fld] * edges.data['e1']}
            else:
                return {'m' : edges.src[fld] * edges.data['e2']}

        def reduce_func(nodes):
            return {fld : th.sum(nodes.mailbox['m'], 1)}

        def apply_func(nodes):
            return {fld : 2 * nodes.data[fld]}
        g = generate_graph()
        # send and recv
        v1 = g.ndata[fld]
        g.pull(nodes, fn.copy_src(src=fld, out='m'), fn.sum(msg='m', out=fld), apply_func)
        v2 = g.ndata[fld]
        g.ndata[fld] = v1
        g.pull(nodes, message_func, reduce_func, apply_func)
        v3 = g.ndata[fld]
        assert U.allclose(v2, v3)
        # send and recv with edge weights
        v1 = g.ndata[fld]
        g.pull(nodes, fn.src_mul_edge(src=fld, edge='e1', out='m'),
               fn.sum(msg='m', out=fld), apply_func)
        v2 = g.ndata[fld]
        g.ndata[fld] = v1
        g.pull(nodes, fn.src_mul_edge(src=fld, edge='e2', out='m'),
               fn.sum(msg='m', out=fld), apply_func)
        v3 = g.ndata[fld]
        g.ndata[fld] = v1
        g.pull(nodes, message_func_edge, reduce_func, apply_func)
        v4 = g.ndata[fld]
        assert U.allclose(v2, v3)
        assert U.allclose(v3, v4)
    # test 1d node features
    _test('f1')
    # test 2d node features
    _test('f2')

161
def test_v2v_update_all_multi_fn():
162
163
    def message_func(edges):
        return {'m2': edges.src['f2']}
164

165
166
    def message_func_edge(edges):
        return {'m2': edges.src['f2'] * edges.data['e2']}
167

168
    def reduce_func(nodes):
169
        return {'v1': th.sum(nodes.mailbox['m2'], 1)}
170
171

    g = generate_graph()
Minjie Wang's avatar
Minjie Wang committed
172
    g.set_n_repr({'v1' : th.zeros((10,)), 'v2' : th.zeros((10,))})
173
174
    fld = 'f2'

175
    g.update_all(message_func, reduce_func)
176
    v1 = g.ndata['v1']
177

Minjie Wang's avatar
Minjie Wang committed
178
    # 1 message, 2 reduces
179
    g.update_all(fn.copy_src(src=fld, out='m'), [fn.sum(msg='m', out='v2'), fn.sum(msg='m', out='v3')])
180
181
    v2 = g.ndata['v2']
    v3 = g.ndata['v3']
182
183
    assert U.allclose(v1, v2)
    assert U.allclose(v1, v3)
184
185
186

    # update all with edge weights, 2 message, 3 reduces
    g.update_all([fn.src_mul_edge(src=fld, edge='e1', out='m1'), fn.src_mul_edge(src=fld, edge='e2', out='m2')],
Minjie Wang's avatar
Minjie Wang committed
187
                 [fn.sum(msg='m1', out='v1'), fn.sum(msg='m2', out='v2'), fn.sum(msg='m1', out='v3')],
Minjie Wang's avatar
Minjie Wang committed
188
                 None)
189
190
191
    v1 = g.ndata['v1']
    v2 = g.ndata['v2']
    v3 = g.ndata['v3']
192
193
    assert U.allclose(v1, v2)
    assert U.allclose(v1, v3)
194
195

    # run UDF with single message and reduce
Minjie Wang's avatar
Minjie Wang committed
196
    g.update_all(message_func_edge, reduce_func, None)
197
    v2 = g.ndata['v2']
198
    assert U.allclose(v1, v2)
199

200
def test_v2v_snr_multi_fn():
201
202
203
    u = th.tensor([0, 0, 0, 3, 4, 9])
    v = th.tensor([1, 2, 3, 9, 9, 0])

204
205
    def message_func(edges):
        return {'m2': edges.src['f2']}
206

207
208
    def message_func_edge(edges):
        return {'m2': edges.src['f2'] * edges.data['e2']}
209

210
    def reduce_func(nodes):
211
        return {'v1' : th.sum(nodes.mailbox['m2'], 1)}
212
213

    g = generate_graph()
Minjie Wang's avatar
Minjie Wang committed
214
215
    g.set_n_repr({'v1' : th.zeros((10, D)), 'v2' : th.zeros((10, D)),
        'v3' : th.zeros((10, D))})
216
217
    fld = 'f2'

218
    g.send_and_recv((u, v), message_func, reduce_func)
219
    v1 = g.ndata['v1']
220

Minjie Wang's avatar
Minjie Wang committed
221
    # 1 message, 2 reduces
222
    g.send_and_recv((u, v),
Minjie Wang's avatar
Minjie Wang committed
223
224
225
            fn.copy_src(src=fld, out='m'),
            [fn.sum(msg='m', out='v2'), fn.sum(msg='m', out='v3')],
            None)
226
227
    v2 = g.ndata['v2']
    v3 = g.ndata['v3']
228
229
    assert U.allclose(v1, v2)
    assert U.allclose(v1, v3)
230
231

    # send and recv with edge weights, 2 message, 3 reduces
232
    g.send_and_recv((u, v),
233
                    [fn.src_mul_edge(src=fld, edge='e1', out='m1'), fn.src_mul_edge(src=fld, edge='e2', out='m2')],
Minjie Wang's avatar
Minjie Wang committed
234
                    [fn.sum(msg='m1', out='v1'), fn.sum(msg='m2', out='v2'), fn.sum(msg='m1', out='v3')],
Minjie Wang's avatar
Minjie Wang committed
235
                    None)
236
237
238
    v1 = g.ndata['v1']
    v2 = g.ndata['v2']
    v3 = g.ndata['v3']
239
240
    assert U.allclose(v1, v2)
    assert U.allclose(v1, v3)
241
242

    # run UDF with single message and reduce
243
    g.send_and_recv((u, v), message_func_edge,
Minjie Wang's avatar
Minjie Wang committed
244
            reduce_func, None)
245
    v2 = g.ndata['v2']
246
    assert U.allclose(v1, v2)
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
def test_e2v_update_all_multi_fn():
    def _test(fld):
        def message_func(edges):
            return {'m1' : edges.src[fld] + edges.dst[fld],
                    'm2' : edges.src[fld] * edges.dst[fld]}

        def reduce_func(nodes):
            return {fld : th.sum(nodes.mailbox['m1'] + nodes.mailbox['m2'], 1)}

        def apply_func(nodes):
            return {fld : 2 * nodes.data[fld]}

        def apply_func_2(nodes):
            return {fld : 2 * nodes.data['r1'] + 2 * nodes.data['r2']}

        g = generate_graph()
        # update all
        v1 = g.get_n_repr()[fld]
        # no specialization
        g.update_all(message_func, reduce_func, apply_func)
        v2 = g.get_n_repr()[fld]

        # user break reduce func into 2 builtin
        g.set_n_repr({fld : v1})
        g.update_all(message_func,
                     [fn.sum(msg='m1', out='r1'), fn.sum(msg='m2', out='r2')],
                     apply_func_2)
        v3 = g.get_n_repr()[fld]

        assert th.allclose(v2, v3)

    # test 1d node features
    _test('f1')
    # test 2d node features
    _test('f2')

def test_e2v_snr_multi_fn():
    u = th.tensor([0, 0, 0, 3, 4, 9])
    v = th.tensor([1, 2, 3, 9, 9, 0])
    def _test(fld):
        def message_func(edges):
            return {'m1' : edges.src[fld] + edges.dst[fld],
                    'm2' : edges.src[fld] * edges.dst[fld]}

        def reduce_func(nodes):
            return {fld : th.sum(nodes.mailbox['m1'] + nodes.mailbox['m2'], 1)}

        def apply_func(nodes):
            return {fld : 2 * nodes.data[fld]}

        def apply_func_2(nodes):
            return {fld : 2 * nodes.data['r1'] + 2 * nodes.data['r2']}

        g = generate_graph()
        # send_and_recv
        v1 = g.get_n_repr()[fld]
        # no specialization
        g.send_and_recv((u, v), message_func, reduce_func, apply_func)
        v2 = g.get_n_repr()[fld]

        # user break reduce func into 2 builtin
        g.set_n_repr({fld : v1})
        g.send_and_recv((u, v), message_func,
                        [fn.sum(msg='m1', out='r1'), fn.sum(msg='m2', out='r2')],
                        apply_func_2)
        v3 = g.get_n_repr()[fld]

        assert th.allclose(v2, v3)

    # test 1d node features
    _test('f1')
    # test 2d node features
    _test('f2')

def test_e2v_recv_multi_fn():
    u = th.tensor([0, 0, 0, 3, 4, 9])
    v = th.tensor([1, 2, 3, 9, 9, 0])
    def _test(fld):
        def message_func(edges):
            return {'m1' : edges.src[fld] + edges.dst[fld],
                    'm2' : edges.src[fld] * edges.dst[fld]}

        def reduce_func(nodes):
            return {fld : th.sum(nodes.mailbox['m1'] + nodes.mailbox['m2'], 1)}

        def apply_func(nodes):
            return {fld : 2 * nodes.data[fld]}

        def apply_func_2(nodes):
            return {fld : 2 * nodes.data['r1'] + 2 * nodes.data['r2']}

        g = generate_graph()
        # recv
        v1 = g.get_n_repr()[fld]
        # no specialization
        g.send((u, v), message_func)
        g.recv([0,1,2,3,9], reduce_func, apply_func)
        v2 = g.get_n_repr()[fld]

        # user break reduce func into 2 builtin
        g.set_n_repr({fld : v1})
        g.send((u, v), message_func)
        g.recv([0,1,2,3,9],
               [fn.sum(msg='m1', out='r1'), fn.sum(msg='m2', out='r2')],
               apply_func_2)
        v3 = g.get_n_repr()[fld]

        assert th.allclose(v2, v3)

    # test 1d node features
    _test('f1')
    # test 2d node features
    _test('f2')

362
def test_update_all_multi_fallback():
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
    # create a graph with zero in degree nodes
    g = dgl.DGLGraph()
    g.add_nodes(10)
    for i in range(1, 9):
        g.add_edge(0, i)
        g.add_edge(i, 9)
    g.ndata['h'] = th.randn(10, D)
    g.edata['w1'] = th.randn(16,)
    g.edata['w2'] = th.randn(16, D)
    def _mfunc_hxw1(edges):
        return {'m1' : edges.src['h'] * th.unsqueeze(edges.data['w1'], 1)}
    def _mfunc_hxw2(edges):
        return {'m2' : edges.src['h'] * edges.data['w2']}
    def _rfunc_m1(nodes):
        return {'o1' : th.sum(nodes.mailbox['m1'], 1)}
    def _rfunc_m2(nodes):
        return {'o2' : th.sum(nodes.mailbox['m2'], 1)}
    def _rfunc_m1max(nodes):
        return {'o3' : th.max(nodes.mailbox['m1'], 1)[0]}
    def _afunc(nodes):
        ret = {}
        for k, v in nodes.data.items():
            if k.startswith('o'):
                ret[k] = 2 * v
        return ret
    # compute ground truth
    g.update_all(_mfunc_hxw1, _rfunc_m1, _afunc)
    o1 = g.ndata.pop('o1')
    g.update_all(_mfunc_hxw2, _rfunc_m2, _afunc)
    o2 = g.ndata.pop('o2')
    g.update_all(_mfunc_hxw1, _rfunc_m1max, _afunc)
    o3 = g.ndata.pop('o3')
    # v2v spmv
    g.update_all(fn.src_mul_edge(src='h', edge='w1', out='m1'),
                 fn.sum(msg='m1', out='o1'),
                 _afunc)
    assert U.allclose(o1, g.ndata.pop('o1'))
    # v2v fallback to e2v
    g.update_all(fn.src_mul_edge(src='h', edge='w2', out='m2'),
                 fn.sum(msg='m2', out='o2'),
                 _afunc)
    assert U.allclose(o2, g.ndata.pop('o2'))
    # v2v fallback to degree bucketing
    g.update_all(fn.src_mul_edge(src='h', edge='w1', out='m1'),
                 fn.max(msg='m1', out='o3'),
                 _afunc)
    assert U.allclose(o3, g.ndata.pop('o3'))
    # multi builtins, both v2v spmv
    g.update_all([fn.src_mul_edge(src='h', edge='w1', out='m1'), fn.src_mul_edge(src='h', edge='w1', out='m2')],
                 [fn.sum(msg='m1', out='o1'), fn.sum(msg='m2', out='o2')],
                 _afunc)
    assert U.allclose(o1, g.ndata.pop('o1'))
    assert U.allclose(o1, g.ndata.pop('o2'))
    # multi builtins, one v2v spmv, one fallback to e2v
    g.update_all([fn.src_mul_edge(src='h', edge='w1', out='m1'), fn.src_mul_edge(src='h', edge='w2', out='m2')],
                 [fn.sum(msg='m1', out='o1'), fn.sum(msg='m2', out='o2')],
                 _afunc)
    assert U.allclose(o1, g.ndata.pop('o1'))
    assert U.allclose(o2, g.ndata.pop('o2'))
    # multi builtins, one v2v spmv, one fallback to e2v, one fallback to degree-bucketing
    g.update_all([fn.src_mul_edge(src='h', edge='w1', out='m1'),
                  fn.src_mul_edge(src='h', edge='w2', out='m2'),
                  fn.src_mul_edge(src='h', edge='w1', out='m3')],
                 [fn.sum(msg='m1', out='o1'),
                  fn.sum(msg='m2', out='o2'),
                  fn.max(msg='m3', out='o3')],
                 _afunc)
    assert U.allclose(o1, g.ndata.pop('o1'))
    assert U.allclose(o2, g.ndata.pop('o2'))
    assert U.allclose(o3, g.ndata.pop('o3'))

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

def test_pull_multi_fallback():
    # create a graph with zero in degree nodes
    g = dgl.DGLGraph()
    g.add_nodes(10)
    for i in range(1, 9):
        g.add_edge(0, i)
        g.add_edge(i, 9)
    g.ndata['h'] = th.randn(10, D)
    g.edata['w1'] = th.randn(16,)
    g.edata['w2'] = th.randn(16, D)
    def _mfunc_hxw1(edges):
        return {'m1' : edges.src['h'] * th.unsqueeze(edges.data['w1'], 1)}
    def _mfunc_hxw2(edges):
        return {'m2' : edges.src['h'] * edges.data['w2']}
    def _rfunc_m1(nodes):
        return {'o1' : th.sum(nodes.mailbox['m1'], 1)}
    def _rfunc_m2(nodes):
        return {'o2' : th.sum(nodes.mailbox['m2'], 1)}
    def _rfunc_m1max(nodes):
        return {'o3' : th.max(nodes.mailbox['m1'], 1)[0]}
    def _afunc(nodes):
        ret = {}
        for k, v in nodes.data.items():
            if k.startswith('o'):
                ret[k] = 2 * v
        return ret
    # nodes to pull
    def _pull_nodes(nodes):
        # compute ground truth
        g.pull(nodes, _mfunc_hxw1, _rfunc_m1, _afunc)
        o1 = g.ndata.pop('o1')
        g.pull(nodes, _mfunc_hxw2, _rfunc_m2, _afunc)
        o2 = g.ndata.pop('o2')
        g.pull(nodes, _mfunc_hxw1, _rfunc_m1max, _afunc)
        o3 = g.ndata.pop('o3')
        # v2v spmv
        g.pull(nodes, fn.src_mul_edge(src='h', edge='w1', out='m1'),
                     fn.sum(msg='m1', out='o1'),
                     _afunc)
        assert U.allclose(o1, g.ndata.pop('o1'))
        # v2v fallback to e2v
        g.pull(nodes, fn.src_mul_edge(src='h', edge='w2', out='m2'),
                     fn.sum(msg='m2', out='o2'),
                     _afunc)
        assert U.allclose(o2, g.ndata.pop('o2'))
        # v2v fallback to degree bucketing
        g.pull(nodes, fn.src_mul_edge(src='h', edge='w1', out='m1'),
                     fn.max(msg='m1', out='o3'),
                     _afunc)
        assert U.allclose(o3, g.ndata.pop('o3'))
        # multi builtins, both v2v spmv
        g.pull(nodes,
               [fn.src_mul_edge(src='h', edge='w1', out='m1'), fn.src_mul_edge(src='h', edge='w1', out='m2')],
               [fn.sum(msg='m1', out='o1'), fn.sum(msg='m2', out='o2')],
               _afunc)
        assert U.allclose(o1, g.ndata.pop('o1'))
        assert U.allclose(o1, g.ndata.pop('o2'))
        # multi builtins, one v2v spmv, one fallback to e2v
        g.pull(nodes,
               [fn.src_mul_edge(src='h', edge='w1', out='m1'), fn.src_mul_edge(src='h', edge='w2', out='m2')],
               [fn.sum(msg='m1', out='o1'), fn.sum(msg='m2', out='o2')],
               _afunc)
        assert U.allclose(o1, g.ndata.pop('o1'))
        assert U.allclose(o2, g.ndata.pop('o2'))
        # multi builtins, one v2v spmv, one fallback to e2v, one fallback to degree-bucketing
        g.pull(nodes,
               [fn.src_mul_edge(src='h', edge='w1', out='m1'),
                fn.src_mul_edge(src='h', edge='w2', out='m2'),
                fn.src_mul_edge(src='h', edge='w1', out='m3')],
               [fn.sum(msg='m1', out='o1'),
                fn.sum(msg='m2', out='o2'),
                fn.max(msg='m3', out='o3')],
               _afunc)
        assert U.allclose(o1, g.ndata.pop('o1'))
        assert U.allclose(o2, g.ndata.pop('o2'))
        assert U.allclose(o3, g.ndata.pop('o3'))
    # test#1: non-0deg nodes
    nodes = [1, 2, 9]
    _pull_nodes(nodes)
    # test#2: 0deg nodes + non-0deg nodes
    nodes = [0, 1, 2, 9]
    _pull_nodes(nodes)

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
def test_spmv_3d_feat():
    def src_mul_edge_udf(edges):
        return {'sum': edges.src['h'] * edges.data['h'].unsqueeze(1).unsqueeze(1)}

    def sum_udf(nodes):
        return {'h': nodes.mailbox['sum'].sum(1)}

    n = 100
    p = 0.1
    a = sp.random(n, n, p, data_rvs=lambda n: np.ones(n))
    g = dgl.DGLGraph(a)
    m = g.number_of_edges()

    # test#1: v2v with adj data
    h = th.randn((n, 5, 5))
    e = th.randn((m,))

    g.ndata['h'] = h
    g.edata['h'] = e
    g.update_all(message_func=fn.src_mul_edge('h', 'h', 'sum'), reduce_func=fn.sum('sum', 'h')) # 1
    ans = g.ndata['h']

    g.ndata['h'] = h
    g.edata['h'] = e
    g.update_all(message_func=src_mul_edge_udf, reduce_func=fn.sum('sum', 'h')) # 2
    assert U.allclose(g.ndata['h'], ans)

    g.ndata['h'] = h
    g.edata['h'] = e
    g.update_all(message_func=src_mul_edge_udf, reduce_func=sum_udf) # 3
    assert U.allclose(g.ndata['h'], ans)

    # test#2: e2v
    def src_mul_edge_udf(edges):
        return {'sum': edges.src['h'] * edges.data['h']}

    h = th.randn((n, 5, 5))
    e = th.randn((m, 5, 5))

    g.ndata['h'] = h
    g.edata['h'] = e
    g.update_all(message_func=fn.src_mul_edge('h', 'h', 'sum'), reduce_func=fn.sum('sum', 'h')) # 1
    ans = g.ndata['h']

    g.ndata['h'] = h
    g.edata['h'] = e
    g.update_all(message_func=src_mul_edge_udf, reduce_func=fn.sum('sum', 'h')) # 2
    assert U.allclose(g.ndata['h'], ans)

    g.ndata['h'] = h
    g.edata['h'] = e
    g.update_all(message_func=src_mul_edge_udf, reduce_func=sum_udf) # 3
    assert U.allclose(g.ndata['h'], ans)

572
if __name__ == '__main__':
573
574
    test_v2v_update_all()
    test_v2v_snr()
575
    test_v2v_pull()
576
577
578
579
580
    test_v2v_update_all_multi_fn()
    test_v2v_snr_multi_fn()
    test_e2v_update_all_multi_fn()
    test_e2v_snr_multi_fn()
    test_e2v_recv_multi_fn()
581
582
    test_update_all_multi_fallback()
    test_pull_multi_fallback()
583
    test_spmv_3d_feat()