question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

data-interchange description of DLPack

See original GitHub issue

The implementation section of the data_interchange document contains some incorrect statements:

  1. The __dlpack__ method will produce a PyCapsule containing a DLPackManagedTensor, …

The DLPackManagedTensor should be DLManagedTensor consistent with dlpack.h and the DLPack diagram.

I also think it would be beneficial to make it more precise:

The __dlpack__ method will produce a PyCapsule object containing a reference to DLPackManagedTensor struct, …

  1. The consumer must set the PyCapsule name to "used_dltensor" , and call the deleter of the DLPackManagedTensor when it no longer needs the data.

The DLPackManagedTensor here also should be DLManagedTensor, but more importantly, the consumer should not be calling the deleter function. The DLManagedTensor.deleter must be called by the PyCapsule_Destructor function provided by the producer.

I also would like to call the mandatory nature of capsule renaming from "dltensor" to "used_dltensor" into question. With proper memory management the capsule can be consumed more than once, creating multiple views into the same allocation.

I would therefore like to propose to change that statement from a requirement to a recommendation.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sebergcommented, Nov 16, 2021

I also would like to call the mandatory nature of capsule renaming from “dltensor” to “used_dltensor” into question. With proper memory management the capsule can be consumed more than once, creating multiple views into the same allocation.

That may be OK, but you have to be very clear about the fact that a producer must produce a fresh capsule every time, because consumers may rename the capsule and “steal” ownership of the DLManagedTensor! And there can only be one owner for it (either the capsule or the importer). Currently, everyone renames the capsule and this is not an issue (there is no reason to forget it). You also may have to be clear, that this means it is only OK for an import function that calls __dlpack__ explicitly (just in case).

With the above exception, what should be added is something like: The consumer must take ownership of the DLManangedTensor by renaming the capsule to “used_dltensor” (note that there can only be a single owner of the DLManagedTensor, renaming the capsule transfers that ownership from the capsule to your object). After taking ownership by renaming the capsule, the consumer must call the deleter function when it does not need the memory anymore.

1reaction
sebergcommented, Nov 16, 2021

The docs seem to require adopting 2, while I find 1 simpler, so if we agree that both are viable ways to manage DLManagedTensor lifetime, I am asking that docs do not mandate 2 over 1.

I agree they do, but getting the mix right is a bit tricky, although probably possible.

It may help to split it out into what you have to do as a producer and as a consumer.

The producer must always export a new DLManagedTensor and a new capsule, because the consumer may rename the capsule and steal ownership. (To me, violating this seems tempting if you do not use the renaming option for consumers.)

Also, a producer must set up the capsule so that it calls DLManagedTensor.delete if and only if its name is dltensor.

The consumer must either:

  • rename the capsule (transferring ownership) and then call the deleter when done. (the capsule itself is irrelevant and may be deleted as soon as it is renamed)
  • ensure that it is the “only” consumer and keep ownership with the capsule, e.g. by only ever accepting capsules returned by __dlpack__(). (The “only” could be violated in certain cases.)

However, while I agree that the second thing should work and is simpler. It is tricky!

For example, if, say pytorch accepts a bare capsule, and you expose myobj.base to be the capsule, then pytorch.from_dlpack(myobj.base) would steal your ownership!

And because I think it is legal to import from the capsule directly (as in some libraries probably may be doing it), you would have to add another requirement: If you do not rename the capsule, you must ensure the the capsule is not available through public API. And that last part is tricky…

Of course you could also hope that nobody does pytorch.from_dlpack(myobj.base) or that nobody allows importing the capsules explicitly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data interchange mechanisms — Python array API standard ...
Data interchange mechanisms¶. This section discusses the mechanism to convert one type of array into another. As discussed in the assumptions-dependencies ...
Read more >
Python Specification for DLPack - DMLC
The Python specification for DLPack is a part of the Python array API standard. More details about the spec can be found under...
Read more >
Implement improved DLPack support #55090 - GitHub
DLPack is a mechanism to exchange data with other tensor/array libraries. PyTorch currently has DLPack support in torch.utils.dlpack ...
Read more >
DLPack — Neural Network Libraries 1.32.0 documentation
DLPack¶. Via a DLPack capsule, you can borrow a tensor from external software as a nnabla.NdArray and can share a NdArray to external...
Read more >
Interoperability — CuPy 11.4.0 documentation
See the mpi4py website for more information. ... PyTorch also supports zero-copy data exchange through DLPack (see DLPack below):.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found