wkcn closed pull request #13663:  [MXNET-1266] Filling the strides of DLPack
URL: https://github.com/apache/incubator-mxnet/pull/13663
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/ndarray/ndarray.cc b/src/ndarray/ndarray.cc
index 081d4e75932..8a8b0cd16bf 100644
--- a/src/ndarray/ndarray.cc
+++ b/src/ndarray/ndarray.cc
@@ -327,6 +327,7 @@ NDArray NDArray::data_ndarray() const {
 struct NDArrayDLManager {
     NDArray handle;  // ref NDArray
     DLManagedTensor tensor;
+    std::unique_ptr<int64_t> strides;
 };
 
 DLManagedTensor* NDArray::ToDLPack() const {
@@ -334,6 +335,19 @@ DLManagedTensor* NDArray::ToDLPack() const {
   dlmanager->handle = *this;
   if (!is_none()) {
     dlmanager->tensor.dl_tensor = data().dltensor();
+    // create strides because the strides is nullptr for MXNet NDArray.
+    // MXNet only support compact tensor now.
+    DLTensor &dltensor = dlmanager->tensor.dl_tensor;
+    const int& ndim = dltensor.ndim;
+    if (dltensor.strides == nullptr && ndim >= 1) {
+      int64_t* strides = new int64_t[ndim];
+      strides[ndim - 1] = 1;
+      for (int i = ndim - 2; i >= 0; --i) {
+        strides[i] = dltensor.shape[i + 1] * strides[i + 1];
+      }
+      dlmanager->strides.reset(strides);
+      dltensor.strides = strides;
+    }
   }
   dlmanager->tensor.manager_ctx = dlmanager;
   dlmanager->tensor.deleter = [](DLManagedTensor* dlmanager){


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to