test_ffi.py 1.38 KB
Newer Older
1
import os
2
3
4
5
6
import unittest

import backend as F

import dgl
7
8
import numpy as np
import pytest
9

10

11
@unittest.skipIf(os.name == "nt", reason="Cython only works on linux")
12
13
14
def test_cython():
    import dgl._ffi._cy3.core

15
16

@pytest.mark.parametrize("arg", [1, 2.3])
17
18
19
def test_callback(arg):
    def cb(x):
        return x + 1
20

21
22
23
    ret = dgl._api_internal._TestPythonCallback(cb, arg)
    assert ret == arg + 1

24
25

@pytest.mark.parametrize("dtype", [F.float32, F.float64, F.int32, F.int64])
26
27
28
def _test_callback_array(dtype):
    def cb(x):
        return F.to_dgl_nd(F.from_dgl_nd(x) + 1)
29

30
    arg = F.copy_to(F.tensor([1, 2, 3], dtype=dtype), F.ctx())
31
32
33
    ret = F.from_dgl_nd(
        dgl._api_internal._TestPythonCallback(cb, F.to_dgl_nd(arg))
    )
34
35
    assert np.allclose(F.asnumpy(ret), F.asnumpy(arg) + 1)

36
37

@pytest.mark.parametrize("arg", [1, 2.3])
38
39
40
def test_callback_thread(arg):
    def cb(x):
        return x + 1
41

42
43
44
    ret = dgl._api_internal._TestPythonCallbackThread(cb, arg)
    assert ret == arg + 1

45
46

@pytest.mark.parametrize("dtype", [F.float32, F.float64, F.int32, F.int64])
47
48
49
def _test_callback_array_thread(dtype):
    def cb(x):
        return F.to_dgl_nd(F.from_dgl_nd(x) + 1)
50

51
    arg = F.copy_to(F.tensor([1, 2, 3], dtype=dtype), F.ctx())
52
53
54
    ret = F.from_dgl_nd(
        dgl._api_internal._TestPythonCallbackThread(cb, F.to_dgl_nd(arg))
    )
55
    assert np.allclose(F.asnumpy(ret), F.asnumpy(arg) + 1)