Const correctness and thread-safety of torch::from_blob with external memory

lmichals: <p>In some C++ integrations, I often need to <strong>wrap externally managed buffers</strong> into <code>torch::Tensor</code> objects <em>without copying data</em> so they can be used for inference or transformations directly on CPU/GPU. The typical pattern uses:</p>
<pre><code class="lang-auto">torch::Tensor t = torch::from_blob(void* data, IntArrayRef sizes, const TensorOptions&amp; options);
</code></pre>
<p>However, <code>from_blob</code> only accepts a <strong>non-const pointer</strong>, which means we must cast away constness even when the memory is immutable:</p>
<pre><code class="lang-auto">auto options = torch::TensorOptions().dtype(dtype).device(device).pinned_memory(true);
auto t = torch::from_blob(const_cast&lt;void*&gt;(block.ptr()), block.size(), block.stride(), options);
</code></pre>
<h4><a name="p-476049-const-correctness-1" class="anchor" href="#p-476049-const-correctness-1"></a><strong>Const correctness</strong></h4>
<p>There’s currently no way to express that a tensor’s data should be <strong>read-only</strong>.<br>
This forces all uses of external memory to go through a <code>const_cast</code>, which is error-prone and semantically incorrect when the source data must not be modified.</p>
<p>Even if the user only performs read-only operations, the compiler cannot enforce this, and developers must rely on assumptions about PyTorch internals.</p>
<h4><a name="p-476049-thread-safety-2" class="anchor" href="#p-476049-thread-safety-2"></a><strong>Thread-safety</strong></h4>
<p>Frameworks used at CERN, like CMSSW, process data concurrently across threads.<br>
If multiple threads access the same external buffer (declared <code>const</code>) via <code>from_blob</code>, we need strong guarantees that PyTorch will <strong>never write</strong> to that memory, even for internal bookkeeping or temporary aliasing. And a temporal copy operation would not be the solution where performance is crucial.</p>
<p>Without const correctness, it’s hard to ensure the use of <code>from_blob</code> remains safe in multi-threaded or read-only contexts.</p>
<p><strong>Questions:</strong></p>
<ol>
<li>Is the current <code>void*</code> signature of <code>from_blob</code> purely historical, or is there an internal reason preventing a <code>const void*</code> overload? I can see this would need internal effort in the framework from <a href="https://github.com/pytorch/pytorch/issues/97856" class="inline-onebox" rel="noopener nofollow ugc">make tensor data const correct · Issue #97856 · pytorch/pytorch · GitHub</a>, but there is no activity since 202,4 and I would not expect it to converge any time soon</li>
<li>Can PyTorch <strong>guarantee</strong> that the memory passed to <code>from_blob</code> is it not written to unless an explicit in-place operation is invoked?</li>
<li>Would the team consider adding support for a <strong>read-only tensor.</strong> If not possible short-term, is there at least a <strong>guarantee or documentation</strong> that read-only use (no mutations) of such tensors is safe and thread-safe?</li>
</ol>
<p>Thanks in advance.</p>
<p>Regards,<br>
Lukasz</p>