Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/runtime/ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,30 @@ struct NDArray::Internal {

NDArray NDArray::CreateView(ShapeTuple shape, DLDataType dtype) {
ICHECK(data_ != nullptr);
ICHECK(get_mutable()->dl_tensor.strides == nullptr) << "Can only create view for compact tensor";

const DLTensor& orig = get_mutable()->dl_tensor;
ICHECK(IsContiguous()) << "Can only create view for compact tensor, but found strides " <<
[&orig]() {
std::stringstream ss;
ss << "[";
for (int i = 0; i < orig.ndim; i++) {
if (i) ss << ", ";
ss << orig.strides[i];
}
ss << "]";
return ss.str();
}() << ", for shape "
<< [&]() {
std::stringstream ss;
ss << "[";
for (int i = 0; i < orig.ndim; i++) {
if (i) ss << ", ";
ss << orig.shape[i];
}
ss << "]";
return ss.str();
}();

NDArray ret = Internal::Create(shape, dtype, get_mutable()->dl_tensor.device);
ret.get_mutable()->dl_tensor.byte_offset = this->get_mutable()->dl_tensor.byte_offset;
size_t curr_size = GetDataSize(this->get_mutable()->dl_tensor);
Expand Down
13 changes: 13 additions & 0 deletions tests/python/unittest/test_runtime_dlpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,18 @@ def test_from_dlpack_shape_one():
tvm.testing.assert_allclose(c.numpy(), a.numpy() + b.numpy())


@tvm.testing.requires_package("torch")
def test_from_dlpack_strided():
import torch
from torch.utils.dlpack import to_dlpack

rows = 1
inp = torch.randn(rows, 16)
a = tvm.runtime.ndarray.from_dlpack(to_dlpack(inp))
view = a._create_view((2, 8))

np.testing.assert_equal(inp.numpy().reshape(2, 8), view.numpy())


if __name__ == "__main__":
tvm.testing.main()