test_pin_memory.py 969 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
import backend as F
import dgl
import pytest

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

    assert not F.is_pinned(t)

    if F.backend_name == 'pytorch':
12
        nd = dgl.utils.pin_memory_inplace(t)
13
        assert F.is_pinned(t)
14
15
16
17
18
19
        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)
20
        assert not F.is_pinned(t)
21
22
23
24
25

        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_()
26
27
    else:
        with pytest.raises(dgl.DGLError):
28
            # tensorflow and mxnet should throw an error
29
30
31
32
            dgl.utils.pin_memory_inplace(t)

if __name__ == "__main__":
    test_pin_unpin()