test_pickle.py 5.48 KB
Newer Older
Gan Quan's avatar
Gan Quan committed
1
import dgl
2
import dgl.contrib as contrib
Gan Quan's avatar
Gan Quan committed
3
4
5
from dgl.frame import Frame, FrameRef, Column
from dgl.graph_index import create_graph_index
from dgl.utils import toindex
6
7
import backend as F
import dgl.function as fn
Gan Quan's avatar
Gan Quan committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import pickle
import io

def _reconstruct_pickle(obj):
    f = io.BytesIO()
    pickle.dump(obj, f)
    f.seek(0)
    obj = pickle.load(f)
    f.close()

    return obj

def test_pickling_index():
    i = toindex([1, 2, 3])
    i.tousertensor()
    i.todgltensor() # construct a dgl tensor which is unpicklable

    i2 = _reconstruct_pickle(i)

27
    assert F.array_equal(i2.tousertensor(), i.tousertensor())
Gan Quan's avatar
Gan Quan committed
28
29
30
31
32
33
34
35
36
37
38
39
40


def test_pickling_graph_index():
    gi = create_graph_index()
    gi.add_nodes(3)
    src_idx = toindex([0, 0])
    dst_idx = toindex([1, 2])
    gi.add_edges(src_idx, dst_idx)

    gi2 = _reconstruct_pickle(gi)

    assert gi2.number_of_nodes() == gi.number_of_nodes()
    src_idx2, dst_idx2, _ = gi2.edges()
41
42
    assert F.array_equal(src_idx.tousertensor(), src_idx2.tousertensor())
    assert F.array_equal(dst_idx.tousertensor(), dst_idx2.tousertensor())
Gan Quan's avatar
Gan Quan committed
43
44
45


def test_pickling_frame():
46
47
    x = F.randn((3, 7))
    y = F.randn((3, 5))
Gan Quan's avatar
Gan Quan committed
48
49
50
51

    c = Column(x)

    c2 = _reconstruct_pickle(c)
52
    assert F.allclose(c.data, c2.data)
Gan Quan's avatar
Gan Quan committed
53
54
55
56

    fr = Frame({'x': x, 'y': y})

    fr2 = _reconstruct_pickle(fr)
57
58
    assert F.allclose(fr2['x'].data, x)
    assert F.allclose(fr2['y'].data, y)
Gan Quan's avatar
Gan Quan committed
59
60
61
62
63

    fr = Frame()


def _assert_is_identical(g, g2):
64
65
    assert g.is_multigraph == g2.is_multigraph
    assert g.is_readonly == g2.is_readonly
Gan Quan's avatar
Gan Quan committed
66
67
68
    assert g.number_of_nodes() == g2.number_of_nodes()
    src, dst = g.all_edges()
    src2, dst2 = g2.all_edges()
69
70
    assert F.array_equal(src, src2)
    assert F.array_equal(dst, dst2)
Gan Quan's avatar
Gan Quan committed
71
72
73
74

    assert len(g.ndata) == len(g2.ndata)
    assert len(g.edata) == len(g2.edata)
    for k in g.ndata:
75
        assert F.allclose(g.ndata[k], g2.ndata[k])
Gan Quan's avatar
Gan Quan committed
76
    for k in g.edata:
77
        assert F.allclose(g.edata[k], g2.edata[k])
Gan Quan's avatar
Gan Quan committed
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def _assert_is_identical_nodeflow(nf1, nf2):
    assert nf1.is_multigraph == nf2.is_multigraph
    assert nf1.is_readonly == nf2.is_readonly
    assert nf1.number_of_nodes() == nf2.number_of_nodes()
    src, dst = nf1.all_edges()
    src2, dst2 = nf2.all_edges()
    assert F.array_equal(src, src2)
    assert F.array_equal(dst, dst2)

    assert nf1.num_layers == nf2.num_layers
    for i in range(nf1.num_layers):
        assert nf1.layer_size(i) == nf2.layer_size(i)
        assert nf1.layers[i].data.keys() == nf2.layers[i].data.keys()
        for k in nf1.layers[i].data:
            assert F.allclose(nf1.layers[i].data[k], nf2.layers[i].data[k])
    assert nf1.num_blocks == nf2.num_blocks
    for i in range(nf1.num_blocks):
        assert nf1.block_size(i) == nf2.block_size(i)
        assert nf1.blocks[i].data.keys() == nf2.blocks[i].data.keys()
        for k in nf1.blocks[i].data:
            assert F.allclose(nf1.blocks[i].data[k], nf2.blocks[i].data[k])
Gan Quan's avatar
Gan Quan committed
100
101
102
103
104
105
106
107

def _global_message_func(nodes):
    return {'x': nodes.data['x']}

def test_pickling_graph():
    # graph structures and frames are pickled
    g = dgl.DGLGraph()
    g.add_nodes(3)
108
109
    src = F.tensor([0, 0])
    dst = F.tensor([1, 2])
Gan Quan's avatar
Gan Quan committed
110
111
    g.add_edges(src, dst)

112
113
114
115
    x = F.randn((3, 7))
    y = F.randn((3, 5))
    a = F.randn((2, 6))
    b = F.randn((2, 4))
Gan Quan's avatar
Gan Quan committed
116
117
118
119
120
121
122
123

    g.ndata['x'] = x
    g.ndata['y'] = y
    g.edata['a'] = a
    g.edata['b'] = b

    # registered functions are pickled
    g.register_message_func(_global_message_func)
124
    reduce_func = fn.sum('x', 'x')
Gan Quan's avatar
Gan Quan committed
125
126
127
128
129
130
131
132
133
134
135
136
    g.register_reduce_func(reduce_func)

    # custom attributes should be pickled
    g.foo = 2

    new_g = _reconstruct_pickle(g)

    _assert_is_identical(g, new_g)
    assert new_g.foo == 2
    assert new_g._message_func == _global_message_func
    assert isinstance(new_g._reduce_func, type(reduce_func))
    assert new_g._reduce_func._name == 'sum'
137
    assert new_g._reduce_func.reduce_op == F.sum
Gan Quan's avatar
Gan Quan committed
138
139
140
141
142
143
    assert new_g._reduce_func.msg_field == 'x'
    assert new_g._reduce_func.out_field == 'x'

    # test batched graph with partial set case
    g2 = dgl.DGLGraph()
    g2.add_nodes(4)
144
145
    src2 = F.tensor([0, 1])
    dst2 = F.tensor([2, 3])
Gan Quan's avatar
Gan Quan committed
146
147
    g2.add_edges(src2, dst2)

148
149
150
151
    x2 = F.randn((4, 7))
    y2 = F.randn((3, 5))
    a2 = F.randn((2, 6))
    b2 = F.randn((2, 4))
Gan Quan's avatar
Gan Quan committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

    g2.ndata['x'] = x2
    g2.nodes[[0, 1, 3]].data['y'] = y2
    g2.edata['a'] = a2
    g2.edata['b'] = b2

    bg = dgl.batch([g, g2])

    bg2 = _reconstruct_pickle(bg)

    _assert_is_identical(bg, bg2)
    new_g, new_g2 = dgl.unbatch(bg2)
    _assert_is_identical(g, new_g)
    _assert_is_identical(g2, new_g2)

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    # readonly graph
    g = dgl.DGLGraph([(0, 1), (1, 2)], readonly=True)
    new_g = _reconstruct_pickle(g)
    _assert_is_identical(g, new_g)

    # multigraph
    g = dgl.DGLGraph([(0, 1), (0, 1), (1, 2)], multigraph=True)
    new_g = _reconstruct_pickle(g)
    _assert_is_identical(g, new_g)

    # readonly multigraph
    g = dgl.DGLGraph([(0, 1), (0, 1), (1, 2)], multigraph=True, readonly=True)
    new_g = _reconstruct_pickle(g)
    _assert_is_identical(g, new_g)

182
183
184
185
186
187
188
189
190
def test_pickling_nodeflow():
    elist = [(0, 1), (1, 2), (2, 3), (3, 0)]
    g = dgl.DGLGraph(elist, readonly=True)
    g.ndata['x'] = F.randn((4, 5))
    g.edata['y'] = F.randn((4, 3))
    nf = contrib.sampling.sampler.create_full_nodeflow(g, 5)
    nf.copy_from_parent()  # add features
    new_nf = _reconstruct_pickle(nf)
    _assert_is_identical_nodeflow(nf, new_nf)
Gan Quan's avatar
Gan Quan committed
191
192
193
194
195
196

if __name__ == '__main__':
    test_pickling_index()
    test_pickling_graph_index()
    test_pickling_frame()
    test_pickling_graph()
197
    test_pickling_nodeflow()