Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CopyFromBytes and CopyToBytes convenience methods to NDArray. Fixed typos. #4970

Merged
merged 10 commits into from
Feb 29, 2020

Conversation

jmorrill
Copy link
Contributor

It is not very intuitive (specially as a beginner) on how to copy a void* to an NDArray. Currently one may have to configure a DLTensor and run an NDArray::CopyTo(...).

This PR adds two member functions to NDArray to copy to and from a void*.

NDArray::CopyFromBytes(const void* data, size_t nbytes)
and
NDArray::CopyToBytes(void* data, size_t nbytes)

Included in this PR are also a few typo fixes.

@jmorrill jmorrill changed the title Added CopyFromBytes and CopyToBytes convenience methods. Fixed typos. Added CopyFromBytes and CopyToBytes convenience methods to NDArray. Fixed typos. Feb 28, 2020
@@ -306,6 +324,26 @@ inline void NDArray::CopyFrom(const NDArray& other) {
CopyFromTo(&(other.get_mutable()->dl_tensor), &(get_mutable()->dl_tensor));
}

inline void NDArray::CopyFromBytes(const void* data, size_t nbytes) {
Copy link
Member

Choose a reason for hiding this comment

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

To avoid duplication, let us make it a non-line member function, and make uses of implementation in the C API here https://github.com/apache/incubator-tvm/blob/master/src/runtime/ndarray.cc#L289

After that, we can safely redirect the C API calls into these C++ APIs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I have it calling the TVMArrayCopyFrom/ToBytes methods.

Are you saying to put the implementation into ndarray.cc ?

Copy link
Contributor Author

@jmorrill jmorrill Feb 28, 2020

Choose a reason for hiding this comment

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

Ok, I misunderstood your original message. Let me fix

@tqchen tqchen self-assigned this Feb 28, 2020
@tqchen tqchen added the status: need update need update based on feedbacks label Feb 28, 2020
* \note The copy may happen asynchronously if it involves a GPU context.
* TVMSynchronize is necessary.
*/
inline void CopyFromBytes(const void* data, size_t nbytes);
Copy link
Member

Choose a reason for hiding this comment

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

Declare as TVM_DLL as it is a member function that is not inlined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed!

void NDArray::CopyToBytes(void* data, size_t nbytes) const {
CHECK(data != nullptr);
CHECK(data_ != nullptr);
CHECK_EQ(TVMArrayCopyToBytes(&get_mutable()->dl_tensor, data, nbytes), 0);
Copy link
Member

Choose a reason for hiding this comment

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

We should move the impl of TVMArrayCopyToBytes to here, and call this function from that function instead. So exceptions can be directly throw out without in C++

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a problem.. Sorry it has taken so many revisions :). At least they are easy changes.

Copy link
Member

Choose a reason for hiding this comment

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

no worries, we always need to iterate to make better versions. Thanks for being patient in improving things

@@ -185,6 +185,38 @@ NDArray NDArray::FromDLPack(DLManagedTensor* tensor) {
return NDArray(GetObjectPtr<Object>(data));
}

void NDArray::CopyToBytes(void* data, size_t nbytes) const {
CHECK(data != nullptr);
Copy link
Member

Choose a reason for hiding this comment

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

Last change: perhaps it is better to bring the common impl in this function, and TVMArrayCopyToBytes to a common function

void ArrayCopyToBytes(DLTensor* handle, void* data, size_t nbytes).

and have both functions call into this common function to avoid impl duplications.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have ArrayCopyFromBytes/ArrayCopyToBytes as the single implementation for the C and C++ API.
Let me know if I missed anything.

@tqchen tqchen merged commit 474c70d into apache:master Feb 29, 2020
@tqchen
Copy link
Member

tqchen commented Feb 29, 2020

Thanks @jmorrill !!

@tqchen tqchen added status: accepted and removed status: need update need update based on feedbacks labels Feb 29, 2020
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Apr 16, 2020
…ixed typos. (apache#4970)

* Added CopyFromBytes and CopyToBytes convenience methods.  Fixed typos.

* Removed unneed argument check

* Use TVMArrayCopyFrom/ToBytes methods

* Moved CopyFrom/ToBytes to ndarray.cc

* CopyToBytes impl was using CopyFromBytes.  Fixed

* changed inline to TVM_DLL

* Used impl from TVMArrayCopyTo/FromBytes into NDArray CopyTo/FromBytes

* Move implementation of all CopyFrom/ToBytes into a common impls

* make arg const

* simplify method impl
zhiics pushed a commit to neo-ai/tvm that referenced this pull request Apr 17, 2020
…ixed typos. (apache#4970)

* Added CopyFromBytes and CopyToBytes convenience methods.  Fixed typos.

* Removed unneed argument check

* Use TVMArrayCopyFrom/ToBytes methods

* Moved CopyFrom/ToBytes to ndarray.cc

* CopyToBytes impl was using CopyFromBytes.  Fixed

* changed inline to TVM_DLL

* Used impl from TVMArrayCopyTo/FromBytes into NDArray CopyTo/FromBytes

* Move implementation of all CopyFrom/ToBytes into a common impls

* make arg const

* simplify method impl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants