Skip to content

Commit

Permalink
[VM] check DLManagedTensor for conditions to construct NDArray (#11504)
Browse files Browse the repository at this point in the history
* check DLManagedTensor for contiguous and alignment to construct correct NDArray

* correction from the reviewer

* update error description for incontiguous DLTensors

* small update

Co-authored-by: Valery Chernov <[email protected]>
  • Loading branch information
vvchernov and Valery Chernov authored Jun 1, 2022
1 parent 89c0235 commit 24b93f5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/runtime/ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ NDArray NDArray::Empty(ShapeTuple shape, DLDataType dtype, Device dev, Optional<
}

NDArray NDArray::FromExternalDLTensor(const DLTensor& dl_tensor) {
ICHECK(::tvm::runtime::IsContiguous(dl_tensor))
<< "External DLTensor is not contiguous. It does not support for now";
ICHECK(::tvm::runtime::IsContiguous(dl_tensor)) << "External DLTensor must be contiguous.";
ICHECK(IsAligned(dl_tensor)) << "Data in DLTensor is not aligned as required by NDArray";
NDArray::Container* data = new NDArray::Container();

Expand All @@ -224,7 +223,7 @@ NDArray NDArray::FromExternalDLTensor(const DLTensor& dl_tensor) {

NDArray NDArray::NewFromDLTensor(DLTensor* tensor, const Device& dev) {
ICHECK(::tvm::runtime::IsContiguous(*tensor))
<< "DLTensor is not contiguous. It does not support for now";
<< "DLTensor is not contiguous. Copying from non-contiguous data is currently not supported";
std::vector<int64_t> shape;
for (int64_t i = 0; i < tensor->ndim; i++) {
shape.push_back(tensor->shape[i]);
Expand All @@ -240,6 +239,9 @@ NDArray NDArray::FromDLPack(DLManagedTensor* tensor) {
data->SetDeleter(Internal::DLPackDeleter);
// fill up content.
data->manager_ctx = tensor;
ICHECK(::tvm::runtime::IsContiguous(tensor->dl_tensor)) << "DLManagedTensor must be contiguous.";
ICHECK(IsAligned(tensor->dl_tensor))
<< "Data in DLManagedTensor is not aligned as required by NDArray";
data->dl_tensor = tensor->dl_tensor;
// update shape_
std::vector<ShapeTuple::index_type> shape;
Expand Down

0 comments on commit 24b93f5

Please sign in to comment.