"vscode:/vscode.git/clone" did not exist on "9f00ec44eb10927a7ed8468d6998295b2795ba24"
test_inplace_update.py 7.95 KB
Newer Older
Lingfan Yu's avatar
Lingfan Yu committed
1
2
3
4
import numpy as np
import scipy.sparse as sp
import dgl
import dgl.function as fn
5
import backend as F
Lingfan Yu's avatar
Lingfan Yu committed
6
7
8
9
10
11
12
13
14
15
16
17

D = 5

def generate_graph():
    g = dgl.DGLGraph()
    g.add_nodes(10)
    # 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)
18
19
    g.ndata['f'] = F.randn((10, D))
    g.edata['e'] = F.randn((17, D))
Lingfan Yu's avatar
Lingfan Yu committed
20
21
22
    return g

def test_inplace_recv():
23
24
    u = F.tensor([0, 0, 0, 3, 4, 9])
    v = F.tensor([1, 2, 3, 9, 9, 0])
Lingfan Yu's avatar
Lingfan Yu committed
25
26
27
28
29

    def message_func(edges):
        return {'m' : edges.src['f'] + edges.dst['f']}

    def reduce_func(nodes):
30
        return {'f' : F.sum(nodes.mailbox['m'], 1)}
Lingfan Yu's avatar
Lingfan Yu committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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

    def _test(apply_func):
        g = generate_graph()
        f = g.ndata['f']

        # one out place run to get result
        g.send((u, v), message_func)
        g.recv([0,1,2,3,9], reduce_func, apply_func)
        result = g.get_n_repr()['f']

        # inplace deg bucket run
45
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
46
47
48
49
50
        g.ndata['f'] = v1
        g.send((u, v), message_func)
        g.recv([0,1,2,3,9], reduce_func, apply_func, inplace=True)
        r1 = g.get_n_repr()['f']
        # check result
51
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
52
        # check inplace
53
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
54
55

        # inplace e2v
56
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
57
58
59
60
61
        g.ndata['f'] = v1
        g.send((u, v), message_func)
        g.recv([0,1,2,3,9], fn.sum(msg='m', out='f'), apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
62
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
63
        # check inplace
64
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
65
66
67
68
69
70
71

    # test send_and_recv with apply_func
    _test(apply_func)
    # test send_and_recv without apply_func
    _test(None)

def test_inplace_snr():
72
73
    u = F.tensor([0, 0, 0, 3, 4, 9])
    v = F.tensor([1, 2, 3, 9, 9, 0])
Lingfan Yu's avatar
Lingfan Yu committed
74
75
76
77
78

    def message_func(edges):
        return {'m' : edges.src['f']}

    def reduce_func(nodes):
79
        return {'f' : F.sum(nodes.mailbox['m'], 1)}
Lingfan Yu's avatar
Lingfan Yu committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93

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

    def _test(apply_func):
        g = generate_graph()
        f = g.ndata['f']

        # an out place run to get result
        g.send_and_recv((u, v), fn.copy_src(src='f', out='m'),
                fn.sum(msg='m', out='f'), apply_func)
        result = g.ndata['f']

        # inplace deg bucket
94
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
95
96
97
98
        g.ndata['f'] = v1
        g.send_and_recv((u, v), message_func, reduce_func, apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
99
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
100
        # check inplace
101
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
102
103

        # inplace v2v spmv
104
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
105
106
107
108
109
        g.ndata['f'] = v1
        g.send_and_recv((u, v), fn.copy_src(src='f', out='m'),
                        fn.sum(msg='m', out='f'), apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
110
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
111
        # check inplace
112
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
113
114

        # inplace e2v spmv
115
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
116
117
118
119
120
        g.ndata['f'] = v1
        g.send_and_recv((u, v), message_func,
                        fn.sum(msg='m', out='f'), apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
121
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
122
        # check inplace
123
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
124
125
126
127
128
129
130

    # test send_and_recv with apply_func
    _test(apply_func)
    # test send_and_recv without apply_func
    _test(None)

def test_inplace_push():
131
    nodes = F.tensor([0, 3, 4, 9])
Lingfan Yu's avatar
Lingfan Yu committed
132
133
134
135
136

    def message_func(edges):
        return {'m' : edges.src['f']}

    def reduce_func(nodes):
137
        return {'f' : F.sum(nodes.mailbox['m'], 1)}
Lingfan Yu's avatar
Lingfan Yu committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151

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

    def _test(apply_func):
        g = generate_graph()
        f = g.ndata['f']

        # an out place run to get result
        g.push(nodes,
               fn.copy_src(src='f', out='m'), fn.sum(msg='m', out='f'), apply_func)
        result = g.ndata['f']

        # inplace deg bucket
152
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
153
154
155
156
        g.ndata['f'] = v1
        g.push(nodes, message_func, reduce_func, apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
157
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
158
        # check inplace
159
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
160
161

        # inplace v2v spmv
162
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
163
164
165
166
167
        g.ndata['f'] = v1
        g.push(nodes, fn.copy_src(src='f', out='m'),
               fn.sum(msg='m', out='f'), apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
168
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
169
        # check inplace
170
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
171
172

        # inplace e2v spmv
173
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
174
175
176
177
178
        g.ndata['f'] = v1
        g.push(nodes,
               message_func, fn.sum(msg='m', out='f'), apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
179
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
180
        # check inplace
181
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
182
183
184
185
186
187
188

    # test send_and_recv with apply_func
    _test(apply_func)
    # test send_and_recv without apply_func
    _test(None)

def test_inplace_pull():
189
    nodes = F.tensor([1, 2, 3, 9])
Lingfan Yu's avatar
Lingfan Yu committed
190
191
192
193
194

    def message_func(edges):
        return {'m' : edges.src['f']}

    def reduce_func(nodes):
195
        return {'f' : F.sum(nodes.mailbox['m'], 1)}
Lingfan Yu's avatar
Lingfan Yu committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209

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

    def _test(apply_func):
        g = generate_graph()
        f = g.ndata['f']

        # an out place run to get result
        g.pull(nodes,
               fn.copy_src(src='f', out='m'), fn.sum(msg='m', out='f'), apply_func)
        result = g.ndata['f']

        # inplace deg bucket
210
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
211
212
213
214
        g.ndata['f'] = v1
        g.pull(nodes, message_func, reduce_func, apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
215
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
216
        # check inplace
217
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
218
219

        # inplace v2v spmv
220
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
221
222
223
224
225
        g.ndata['f'] = v1
        g.pull(nodes, fn.copy_src(src='f', out='m'),
               fn.sum(msg='m', out='f'), apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
226
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
227
        # check inplace
228
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
229
230

        # inplace e2v spmv
231
        v1 = F.clone(f)
Lingfan Yu's avatar
Lingfan Yu committed
232
233
234
235
236
        g.ndata['f'] = v1
        g.pull(nodes,
               message_func, fn.sum(msg='m', out='f'), apply_func, inplace=True)
        r1 = g.ndata['f']
        # check result
237
        assert F.allclose(r1, result)
Lingfan Yu's avatar
Lingfan Yu committed
238
        # check inplace
239
        assert F.allclose(v1, r1)
Lingfan Yu's avatar
Lingfan Yu committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

    # test send_and_recv with apply_func
    _test(apply_func)
    # test send_and_recv without apply_func
    _test(None)

def test_inplace_apply():
    def apply_node_func(nodes):
        return {'f': nodes.data['f'] * 2}

    def apply_edge_func(edges):
        return {'e': edges.data['e'] * 2}

    g = generate_graph()
    nodes = [1, 2, 3, 9]
    nf = g.ndata['f']
    # out place run
    g.apply_nodes(apply_node_func, nodes)
    new_nf = g.ndata['f']
    # in place run
    g.ndata['f'] = nf
    g.apply_nodes(apply_node_func, nodes, inplace=True)
    # check results correct and in place
263
    assert F.allclose(nf, new_nf)
Lingfan Yu's avatar
Lingfan Yu committed
264
265
266
    # test apply all nodes, should not be done in place
    g.ndata['f'] = nf
    g.apply_nodes(apply_node_func, inplace=True)
267
    assert F.allclose(nf, g.ndata['f']) == False
Lingfan Yu's avatar
Lingfan Yu committed
268
269
270
271
272
273
274
275
276
277

    edges = [3, 5, 7, 10]
    ef = g.edata['e']
    # out place run
    g.apply_edges(apply_edge_func, edges)
    new_ef = g.edata['e']
    # in place run
    g.edata['e'] = ef
    g.apply_edges(apply_edge_func, edges, inplace=True)
    g.edata['e'] = ef
278
    assert F.allclose(ef, new_ef)
Lingfan Yu's avatar
Lingfan Yu committed
279
280
281
    # test apply all edges, should not be done in place
    g.edata['e'] == ef
    g.apply_edges(apply_edge_func, inplace=True)
282
    assert F.allclose(ef, g.edata['e']) == False
Lingfan Yu's avatar
Lingfan Yu committed
283
284
285
286
287
288
289

if __name__ == '__main__':
    test_inplace_recv()
    test_inplace_snr()
    test_inplace_push()
    test_inplace_pull()
    test_inplace_apply()