Memory leaks in libtorch?

ericrhenry: <p>Valgrind is my go-to for wrangling possible memory leaks. It is a beautiful piece of software, but is unfortunately (and necessarily) imperfect. I just ran a libtorch-based application through a relatively brief optimization of a CNN model, and it generated a fair number of loss records. Fortunately, all of them appear to be of the “possibly lost” variety (as opposed to “definitely lost”); I was using all of the available leak-check-heuristics available to valgrind. Many of these records reflect allocations embedded in pthread-related activities, which may just suggest that threads are not being thoroughly cleaned up on exit.<br>
However, there are a number of records which, at least going by the traceback, don’t reflect an allocation related to thread creation. I just quote the largest of these here, and I’ve cut out a lot of intermediate traceback. (Apologies for the messy output.)</p>
<p>==4165== 1,441,792 bytes in 1 blocks are possibly lost in loss record 13,570 of 13,578<br>
==4165==    at 0x4C307EF: operator new(unsigned long) (vg_replace_malloc.c:344)<br>
==4165==    by 0x12BE8C16: void std::vector&lt;mkldnn_primitive*, std::allocator&lt;mkldnn_primitive*&gt; &gt;::_M_range_insert&lt;__gnu_cxx::__normal_iterator&lt;mkldnn_primitive* const*, std::vector&lt;mkldnn_primitive*, std::allocator&lt;mkldnn_primitive*&gt; &gt; &gt; &gt;(__gnu_cxx::__normal_iterator&lt;mkldnn_primitive**, std::vector&lt;mkldnn_primitive*, std::allocator&lt;mkldnn_primitive*&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;mkldnn_primitive* const*, std::vector&lt;mkldnn_primitive*, std::allocator&lt;mkldnn_primitive*&gt; &gt; &gt;, __gnu_cxx::<em><em>normal_iterator&lt;mkldnn_primitive* const*, std::vector&lt;mkldnn_primitive*, std::allocator&lt;mkldnn_primitive*&gt; &gt; &gt;, std::forward_iterator_tag) (in /usr/lib64/libtorch_cpu.so)<br>
…<br>
…<br>
==4165==    by 0x103E262C: c10::detail::wrap_kernel_functor_unboxed</em>&lt;c10::detail::WrapRuntimeKernelFunctor</em>&lt;std::tuple&lt;at::Tensor, at::Tensor, at::Tensor&gt; (<em>)(at::Tensor const&amp;, at::Tensor const&amp;, at::Tensor const&amp;, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, std::array&lt;bool, 3ul&gt;), std::tuple&lt;at::Tensor, at::Tensor, at::Tensor&gt;, c10::guts::typelist::typelist&lt;at::Tensor const&amp;, at::Tensor const&amp;, at::Tensor const&amp;, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, std::array&lt;bool, 3ul&gt; &gt; &gt;, std::tuple&lt;at::Tensor, at::Tensor, at::Tensor&gt; (at::Tensor const&amp;, at::Tensor const&amp;, at::Tensor const&amp;, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, std::array&lt;bool, 3ul&gt;)&gt;::call(c10::OperatorKernel</em>, at::Tensor const&amp;, at::Tensor const&amp;, at::Tensor const&amp;, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, std::array&lt;bool, 3ul&gt;) (in /usr/lib64/libtorch_cpu.so)<br>
==4165==    by 0x11D0AE43: torch::autograd::VariableType::mkldnn_convolution_backward(at::Tensor const&amp;, at::Tensor const&amp;, at::Tensor const&amp;, c10::ArrayRef, c10::ArrayRef, c10::ArrayRef, long, std::array&lt;bool, 3ul&gt;) (in /usr/lib64/libtorch_cpu.so)</p>
<p>Of course, “possibly lost” in valgrind may just mean that the allocation has been “hidden” in some way that the available heuristics can’t penetrate. (In my experience, this kind of hiding does not occur in shared_ptr or similar constructs.) Therefore, I wouldn’t expect any kind of detailed interpretation of this, but I was wondering if libtorch (v. 1.5, specifically) has been subjected to any kind of careful memory-leak vetting, whether by valgrind or some other checker.</p>
<p>Part of my interest is perhaps my just being a neurotic purist, but that has gotten me to where some quite complex applications, involving hundreds of millions of alloc/free pairs, result in no loss records of any kind (which automatically triggers a fist-pump on my part <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> ).</p>
<p>Thanks,<br>
Eric</p>

ptrblck: <p>Thanks for the analysis and I think that (same as with your last check) it would be worth creating an issue on GitHub to track it.</p>

ericrhenry: <p>Done (<span class="hashtag">#39522</span>). Triaged, and initial response was a (probable) no. Unfortunately, for my application (long-running open-ended interactive environment), even the possibility of accumulating leaks is a serious concern. If nobody else is going to investigate this, I may have to find a way to do it myself (which I realize is fully within the spirit of open source software).<br>
As always, thanks for weighing in.</p>