test_pin_memory.py 978 Bytes
Newer Older
1
2
import backend as F

3
import dgl
4
import pytest
5
6
7
8
9


@pytest.mark.skipif(
    F._default_context_str == "cpu", reason="Need gpu for this test"
)
10
11
12
13
14
def test_pin_unpin():
    t = F.arange(0, 100, dtype=F.int64, ctx=F.cpu())

    assert not F.is_pinned(t)

15
    if F.backend_name == "pytorch":
16
        nd = dgl.utils.pin_memory_inplace(t)
17
        assert F.is_pinned(t)
18
19
20
21
22
23
        nd.unpin_memory_()
        assert not F.is_pinned(t)
        del nd

        # tensor will be unpinned immediately if the returned ndarray is not saved
        dgl.utils.pin_memory_inplace(t)
24
        assert not F.is_pinned(t)
25
26
27
28
29

        t_pin = t.pin_memory()
        # cannot unpin a tensor that is pinned outside of DGL
        with pytest.raises(dgl.DGLError):
            F.to_dgl_nd(t_pin).unpin_memory_()
30
31
    else:
        with pytest.raises(dgl.DGLError):
32
            # tensorflow and mxnet should throw an error
33
34
            dgl.utils.pin_memory_inplace(t)

35

36
37
if __name__ == "__main__":
    test_pin_unpin()