Cannot move tensor to CPU in the main function

justanhduc: <p>Hi. When I process an image of size larger than some threshold <code>(512, 512)</code>, when moving the tensor to CPU in the main function, it throws this error</p>
<pre><code class="lang-auto">terminate called after throwing an instance of 'c10::Error'
  what():  CUDA error: invalid argument
Exception raised from copy_kernel_cuda at /pytorch/aten/src/ATen/native/cuda/Copy.cu:200 (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 (0x7ffbbba9cb89 in /home/justanhduc/Documents/libtorch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0x2b1aff3 (0x7ffb52e3fff3 in /home/justanhduc/Documents/libtorch/lib/libtorch_cuda.so)
frame #2: &lt;unknown function&gt; + 0xd42f83 (0x7ffbaa131f83 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #3: &lt;unknown function&gt; + 0xd3ffa1 (0x7ffbaa12efa1 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #4: at::native::copy_(at::Tensor&amp;, at::Tensor const&amp;, bool) + 0x53 (0x7ffbaa130d93 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #5: 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 (0x7ffbaa9212d7 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #6: at::Tensor::copy_(at::Tensor const&amp;, bool) const + 0xcd (0x7ffbaaa48ded in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #7: &lt;unknown function&gt; + 0x32cf9d8 (0x7ffbac6be9d8 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #8: 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 (0x7ffbaa9212d7 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #9: at::Tensor::copy_(at::Tensor const&amp;, bool) const + 0xcd (0x7ffbaaa48ded in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #10: at::native::to(at::Tensor const&amp;, c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) + 0x17ef (0x7ffbaa3d98ff in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #11: &lt;unknown function&gt; + 0x158d5e6 (0x7ffbaa97c5e6 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #12: &lt;unknown function&gt; + 0x15eb7b3 (0x7ffbaa9da7b3 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #13: &lt;unknown function&gt; + 0xaf2d7f (0x7ffba9ee1d7f in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #14: &lt;unknown function&gt; + 0x133045a (0x7ffbaa71f45a in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #15: at::Tensor::to(c10::TensorOptions const&amp;, bool, bool, c10::optional&lt;c10::MemoryFormat&gt;) const + 0x315 (0x7ffbaaa5d045 in /home/justanhduc/Documents/libtorch/lib/libtorch_cpu.so)
frame #16: main() [0x4183ed]
frame #17: __libc_start_main + 0xf0 (0x7ffb4f2cf840 in /lib/x86_64-linux-gnu/libc.so.6)
frame #18: main() [0x416e89]


Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
</code></pre>
<p>However, when I move it to CPU before returning to the main function, it works fine.<br>
I will try to reproduce by a minimal example as the whole project is kinda large, but please let me know if you have experienced it before, and if possible, any solution to this problem.<br>
Thanks in advance!</p>

glaringlee: <p>From the error message, it seems to me that you tensor (image) got destroyed after returning to the main function, the reason is that when doing copy, the address is unknown. I am not 100% percent sure, please double check. And, please provide an example if possible, I can further look into it.</p>

justanhduc: <p>Thanks for the hint. I figured out the problem. I assign <code>input.data()</code> to a pointer and then being to careful, I unconsciously freed this pointer in the destructor of the input class. I am not sure why it affects the output though, but after commenting this part, it works!</p>