test_index.py 2.62 KB
Newer Older
1
2
3
4
import dgl
import dgl.ndarray as nd
from dgl.utils import toindex
import numpy as np
5
import backend as F
VoVAllen's avatar
VoVAllen committed
6
import unittest
7

VoVAllen's avatar
VoVAllen committed
8
@unittest.skipIf(dgl.backend.backend_name == "tensorflow", reason="TF doesn't support inplace update")
9
10
11
12
13
14
15
16
def test_dlpack():
    # test dlpack conversion.
    def nd2th():
        ans = np.array([[1., 1., 1., 1.],
                        [0., 0., 0., 0.],
                        [0., 0., 0., 0.]])
        x = nd.array(np.zeros((3, 4), dtype=np.float32))
        dl = x.to_dlpack()
17
        y = F.zerocopy_from_dlpack(dl)
18
        y[0] = 1
19
20
        print(x)
        print(y)
21
22
23
24
25
26
        assert np.allclose(x.asnumpy(), ans)

    def th2nd():
        ans = np.array([[1., 1., 1., 1.],
                        [0., 0., 0., 0.],
                        [0., 0., 0., 0.]])
27
28
        x = F.zeros((3, 4))
        dl = F.zerocopy_to_dlpack(x)
29
30
        y = nd.from_dlpack(dl)
        x[0] = 1
31
32
        print(x)
        print(y)
33
34
        assert np.allclose(y.asnumpy(), ans)

35
    def th2nd_incontiguous():
36
        x = F.astype(F.tensor([[0, 1], [2, 3]]), F.int64)
37
38
39
40
41
42
        ans = np.array([0, 2])
        y = x[:2, 0]
        # Uncomment this line and comment the one below to observe error
        #dl = dlpack.to_dlpack(y)
        dl = F.zerocopy_to_dlpack(y)
        z = nd.from_dlpack(dl)
43
44
        print(x)
        print(z)
45
46
        assert np.allclose(z.asnumpy(), ans)

47
48
    nd2th()
    th2nd()
49
    th2nd_incontiguous()
50
51
52
53
54
55

def test_index():
    ans = np.ones((10,), dtype=np.int64) * 10
    # from np data
    data = np.ones((10,), dtype=np.int64) * 10
    idx = toindex(data)
56
    y1 = idx.tonumpy()
57
    y2 = F.asnumpy(idx.tousertensor())
58
59
60
61
62
63
64
65
    y3 = idx.todgltensor().asnumpy()
    assert np.allclose(ans, y1)
    assert np.allclose(ans, y2)
    assert np.allclose(ans, y3)

    # from list
    data = [10] * 10
    idx = toindex(data)
66
    y1 = idx.tonumpy()
67
    y2 = F.asnumpy(idx.tousertensor())
68
69
70
71
72
    y3 = idx.todgltensor().asnumpy()
    assert np.allclose(ans, y1)
    assert np.allclose(ans, y2)
    assert np.allclose(ans, y3)

73
    # from dl tensor
74
    data = F.ones((10,), dtype=F.int64) * 10
75
    idx = toindex(data)
76
    y1 = idx.tonumpy()
77
    y2 = F.asnumpy(idx.tousertensor())
78
79
80
81
82
83
84
85
    y3 = idx.todgltensor().asnumpy()
    assert np.allclose(ans, y1)
    assert np.allclose(ans, y2)
    assert np.allclose(ans, y3)

    # from dgl.NDArray
    data = dgl.ndarray.array(np.ones((10,), dtype=np.int64) * 10)
    idx = toindex(data)
86
    y1 = idx.tonumpy()
87
    y2 = F.asnumpy(idx.tousertensor())
88
89
90
91
92
93
94
95
    y3 = idx.todgltensor().asnumpy()
    assert np.allclose(ans, y1)
    assert np.allclose(ans, y2)
    assert np.allclose(ans, y3)

if __name__ == '__main__':
    test_dlpack()
    test_index()