Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fix NDArray ToDLPack Bug #13698

Merged
merged 2 commits into from Dec 21, 2018
Merged

Fix NDArray ToDLPack Bug #13698

merged 2 commits into from Dec 21, 2018

Conversation

wkcn
Copy link
Member

@wkcn wkcn commented Dec 20, 2018

Description

Fix Issue #13658
The variable shape in DLTensor is pointed to the TShape instance in tblob_ in NDArray.
However, the NDArray instance in NDArrayDLManager is different from the original NDArray instance.

Test Case written by @jermainewang

import mxnet as mx
import numpy as np
import tvm

def foo():
  x = mx.nd.array([0, 5], dtype='int64')
  dl = x.to_dlpack_for_read()
  return tvm.nd.from_dlpack(dl)

for i in range(10):
  y = foo()
  y.asnumpy()

Command:

for i in {0..100}; do echo $i && python3 t.py || break ; done

The test is passed more than 300 times on Ubuntu 16.04.

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • Tiny Changes. The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Fix NDArray::ToDLPack

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@@ -330,11 +330,10 @@ struct NDArrayDLManager {
};

DLManagedTensor* NDArray::ToDLPack() const {
CHECK(!is_none()) << "NDArray is not initialized";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm. Such check should never fail as ToDLPack will only be called after wait_to_read or wait_to_write so the NDArray should always be initialized.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But when users use C++ API directly, they may forget to call WaitToRead or WaitToWrite before calling ToDLPack.

@jermainewang
Copy link
Contributor

LGTM. Thanks @wkcn .

@tqchen tqchen merged commit f211588 into apache:master Dec 21, 2018
rondogency pushed a commit to rondogency/incubator-mxnet that referenced this pull request Jan 9, 2019
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants