Skip to content

Commit

Permalink
[Bugfix] Allow pinning of empty tensor (#6044)
Browse files Browse the repository at this point in the history
  • Loading branch information
nv-dlasalle committed Jul 25, 2023
1 parent b400af4 commit c08192c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions python/dgl/backend/pytorch/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,13 @@ def check_is_view(input):


def zerocopy_to_dgl_ndarray_for_write(input):
assert input.is_contiguous(), (
"Cannot convert non-contiguous tensors "
"to dgl ndarray for write. Call .to_contiguous() first."
)
check_is_view(input)
if input.numel() > 0:
# only check non-empty tensors
assert input.is_contiguous(), (
"Cannot convert non-contiguous tensors "
"to dgl ndarray for write. Call .to_contiguous() first."
)
check_is_view(input)
return zerocopy_to_dgl_ndarray(input)


Expand Down
4 changes: 4 additions & 0 deletions tests/python/pytorch/utils/test_pin_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def test_pin_view():
with pytest.raises(dgl.DGLError):
dgl.utils.pin_memory_inplace(v)

# make sure an empty view does not generate an error
u = t[10:10]
u = dgl.utils.pin_memory_inplace(u)


@pytest.mark.skipif(
F._default_context_str == "cpu", reason="Need gpu for this test."
Expand Down

0 comments on commit c08192c

Please sign in to comment.