Take ownership of tensor data ptr

MoBaT: <p>Hey!</p>
<p>I currently have most of my application abstracted into “modules”. My current problem is that when I get a <code>at::Tensor tensor</code> from the neural network output in C++, I want to be able to take ownership of the <code>tensor.data_ptr()</code> using my own internal C++ class and delete the interface <code>at::Tensor</code>. Is this possible?</p>

albanD: <p>Hi,</p>
<p>This is not possible as of today because you wouldn’t know how to free that memory.<br>
Is there a specific reason why you can keep the Tensor object around to keep the buffer alive?</p>

LeviViana: <p>This is an interesting point. I’ve been using global variables or copies as workaround.</p>

MoBaT: <p>I architected my codebase so that modules don’t know the data types of other modules it may reference. The only structs shared between modules are my own internal structs.</p>
<p>So I have a struct called ManagedMemory which handles memory management. So what I usually do is instantiate a <code>ManagedMemory(dataSize, dataPtr, memoryType=cuda/cpu)</code> inside the ModulePytorch from the inference output and then send that through pubsub to another module. ManagedMemory is now the struct which owns the data.</p>
<p>Just a thought which may work (tell me if this is doable):</p>
<blockquote>
<p>Maybe an alternative would be to up the refCount artificially by 1 and then pass in a custom function into my ManagedMemory which will decrease the refCount when it’s not being used anymore.<br>
<code>ManagedMemory(dataSize, dataPtr, memoryType=cuda/cpu, [&amp;]() { // Decrease Tensor Refcount })</code><br>
Then that means the data is still managed by Module PyTorch but will have a floating pointer until the refCount falls to 0.</p>
</blockquote>
<p>I just don’t like the idea of doing this. I’m more of favor of telling tensors to release ownership to the pointers.</p>

albanD: <blockquote>
<p>I just don’t like the idea of doing this. I’m more of favor of telling tensors to release ownership to the pointers.</p>
</blockquote>
<p>The problem is not with the ownership but with how will you know how to free the memory?<br>
We use different allocators depending on what you do (regular cpu memory, regular gpu memory, shared cpu memory, paged cpu memory,…). And you won’t be able to call just <code>free</code> or <code>delete</code> on the pointer to clean it properly!</p>
<p>Do you think it would be possible for your ManagedMemory to have an optional <code>Tensor</code> field that will make sure it keep the dataPtr alive? And when the <code>ManagedMemory</code> is freed, the Tensor will go away and free the memory.</p>

MoBaT: <p>Yeah, but if we know the type of memory, the size, and have the pointer, should I be able to just call a release() and it will automatically give up ownership of whatever memory type it was allocated with? It will be up to me as the developer to now do the proper de-allocations elsewhere depending on what the tensor memory type was. A little unsafe but I guess that’s what happens when you go against the grain.</p>
<p>But yeah, I can add some more code to my ManagedMemory to do that and make the tensor live until the duration of the ManagedMemory. Not doing the optional Tensor field method (because the point of ManagedMemory was to have basic dependencies and nothing additional) but some other way.</p>
<p>Thanks for helping me out albanD, it’s really appreciated.</p>

tom: <p>What you describe is a bit like DLPack (DLManagedTensor on the C side), maybe that can help you achieve what you need.<br>
In particular <code>DLManagedTensor* toDLPack(const Tensor&amp; src);</code>, from <code>ATen/DLConvertor.h</code> (mind the <code>o</code> at the end).</p>
<p>Best regards</p>
<p>Thomas</p>

MoBaT: <p>Still couldn’t figure out how to take ownership of the pointer since they store the Tensor internally, but I did what I mentioned above and modified by ManagedMemory to have a custom deallocation func.</p>
<pre><code class="lang-auto">{
        at::Tensor a = at::randn({50, 50, 50});
        int tensorDataSize = std::accumulate(a.sizes().begin(), a.sizes().end(), 1, std::multiplies&lt;int&gt;()) * sizeof(float);
        DLManagedTensor * dlPackTensor = at::toDLPack(a);
        ManagedMemory managedMemory((char *) dlPackTensor-&gt;dl_tensor.data, tensorDataSize, CPU, true);
        managedMemory.deallocationFunc = [&amp;]() { dlPackTensor-&gt;deleter(dlPackTensor); };
}
</code></pre>

CodeForDuty: <p>I am working on this also. But I think the tensor implementation would have its way to reuse the memory. it would not be efficient to just call delete to free the memory.<br>
What I think is to keep a tensor to keep the ref of that memory.</p>

albanD: <p>Hi,</p>
<p>I would recommend using DLPack protocol for sharing this kind of Data if you’re implementing something like that.</p>