Unsupported operation: some elements of the input tensor and the written-to tensor refer to a single memory location

digiamm: <p>Hy guys, I am having a runtime error with <code>libtorch</code>. I am getting a video stream and I want to get the probability out for my classification problem. The program runs for the first 4/5 frames and then it crashes.</p>
<p>This is what I am doing:</p>
<pre><code class="lang-auto">void MyClass::prob(const cv::Mat&amp; img, const bool cuda){
    torch::Tensor img_tensor = torch::from_blob(img.clone().data, { 1, 1, img.rows, img.cols }, torch::kByte);
    img_tensor = img_tensor.to(torch::kFloat) / 255;
    
    bool use_cuda = cuda &amp;&amp; torch::cuda::is_available();
    torch::DeviceType device_type;
    if (use_cuda)
        device_type = torch::kCUDA;
    else
        device_type = torch::kCPU;
    
    torch::Device device(device_type);
    _model-&gt;to(device);
    img_tensor = img_tensor.set_requires_grad(false);

    auto out = _model-&gt;forward(img_tensor.to(device));
    _mProb = out[0].squeeze(0); // writing to a field [H, W] 
}
</code></pre>
<p>It seems that the error refers to a <code>to</code> function, input tensor and memory location. I just started with <code>pytorch</code> and I’ve seen examples that seems to do the same. Don’t know what can be wrong. I attach the error trace that may be interesting:</p>
<pre><code class="lang-auto">what():  unsupported operation: some elements of the input tensor and the written-to tensor refer to a single memory location. Please clone() the tensor before performing the operation.
Exception raised from assert_no_partial_overlap at ../aten/src/ATen/MemoryOverlap.cpp:75 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;) + 0x69 (0x7f9798b88b89 in /home/ldg/source/libraries/libtorch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0xb24bb2 (0x7f9788759bb2 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #2: at::TensorIterator::compute_mem_overlaps(at::TensorIteratorConfig const&amp;) + 0xc5 (0x7f9788c425e5 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #3: at::TensorIterator::build(at::TensorIteratorConfig&amp;) + 0x4e (0x7f9788c4b4ce in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #4: at::TensorIterator::TensorIterator(at::TensorIteratorConfig&amp;) + 0xdd (0x7f9788c4bc9d in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #5: &lt;unknown function&gt; + 0xd3f173 (0x7f9788974173 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #6: at::native::copy_(at::Tensor&amp;, at::Tensor const&amp;, bool) + 0x53 (0x7f9788976093 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #7: at::Tensor&amp; c10::Dispatcher::callWithDispatchKey&lt;at::Tensor&amp;, at::Tensor&amp;, at::Tensor const&amp;, bool&gt;(c10::TypedOperatorHandle&lt;at::Tensor&amp; (at::Tensor&amp;, at::Tensor const&amp;, bool)&gt; const&amp;, c10::DispatchKey, at::Tensor&amp;, at::Tensor const&amp;, bool) const + 0x1e7 (0x7f97891665d7 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #8: at::Tensor::copy_(at::Tensor const&amp;, bool) const + 0xcd (0x7f978928e0ed in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #9: &lt;unknown function&gt; + 0x32cecd8 (0x7f978af03cd8 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #10: at::Tensor&amp; c10::Dispatcher::callWithDispatchKey&lt;at::Tensor&amp;, at::Tensor&amp;, at::Tensor const&amp;, bool&gt;(c10::TypedOperatorHandle&lt;at::Tensor&amp; (at::Tensor&amp;, at::Tensor const&amp;, bool)&gt; const&amp;, c10::DispatchKey, at::Tensor&amp;, at::Tensor const&amp;, bool) const + 0x1e7 (0x7f97891665d7 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #11: at::Tensor::copy_(at::Tensor const&amp;, bool) const + 0xcd (0x7f978928e0ed in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #12: at::native::to(at::Tensor const&amp;, c10::ScalarType, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) + 0x2ff (0x7f9788c2039f in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
</code></pre>
<p>I really appreciate some help,<br>
Thanks for your time</p>

ptrblck: <p>Did you try to add the suggested <code>clone()</code> operation to the code?<br>
E.g. start at the beginning by adding it to the <code>torch::from_blob()</code> operation and try to isolate the line of code, which raises the error.</p>

digiamm: <p>Thanks for the suggestion, I’ve just add <code>clone()</code> and seems going well.</p>