Why do I get almost 1000 warnings when compiling with libtorch?

username: Anders_G
cooked: <p>I create a project using libtorch 1.6 about 6-8 months ago (visual studio c++). Since then I’ve been upgrading the project as new versions of torch are releasad. However, since 1.8 I get very many warnings as I compile the code. Around 1000 of them, and I always find it a bit unsettling with warnings since it often is an indication that something bad might happen.</p>
<p>Most of the warnings are all of the same type (mainly C4624) so I give an example of each:</p>
<p>|Warning|C26110|Caller failing to hold lock ‘lock’ before calling function ‘std::lock_guard<a>std::mutex</a>::~lock_guard<a>std::mutex</a>’.|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\ATen\Context.h|318||</p>
<p>|Warning|C26439|This kind of function may not throw. Declare it ‘noexcept’ (f.6).|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\ATen\core\TensorBody.h|206||</p>
<p>|Warning|C26451|Arithmetic overflow: Using operator ‘-’ on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator ‘-’ to avoid overflow (io.2).|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\ATen\core\MT19937RNGEngine.h|159||</p>
<p>|Warning|C26478|Don’t use std::move on constant variables. (es.56).|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\torch\csrc\api\include\torch\nn\modules\container\sequential.h|120||</p>
<p>|Warning|C26495|Variable ‘at::indexing::TensorIndex::integer_’ is uninitialized. Always initialize a member variable (type.6).|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\ATen\TensorIndexing.h|130||</p>
<p>|Warning|C26812|The enum type ‘c10::DispatchKeySet::Full’ is unscoped. Prefer ‘enum class’ over ‘enum’ (Enum.3).|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\c10\core\DispatchKeySet.h|44||</p>
<p>|Warning|C26817|Potentially expensive copy of variable ‘d’ in range-for loop. Consider making it a const reference (es.71).|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\ATen\core\jit_type.h|420||</p>
<p>|Warning|C4067|unexpected tokens following preprocessor directive - expected a newline|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\c10\macros\Macros.h|189||</p>
<p>|Warning|C4624|‘c10::trivially_copyable_optimization_optional_base’: destructor was implicitly defined as deleted|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\c10\util\Optional.h|387||</p>
<p>|Warning|C6308|‘realloc’ might return null pointer: assigning null pointer to ‘outOfLineStorage_’, which is passed as an argument to ‘realloc’, will cause the original memory block to be leaked.|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\c10\core\impl\SizesAndStrides.h|276||</p>
<p>|Warning|C6385|Reading invalid data from ‘device_guard_impl_registry’:  the readable size is ‘104’ bytes, but ‘2048’ bytes may be read.|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\c10\core\impl\DeviceGuardImplInterface.h|219||</p>
<p>Are any of these warnings a potential problem and why did I start getting them when upgrading to a newer version of libtorch?</p>

username: Willem
cooked: <p>I have the same issue with 1.8.3. Any solutions?</p>

username: zfzhang
cooked: <p>I met ~70 similar repeated warnings when compiling a C++ CUDA extension with Pytorch 1.9.1 in Windows using VC 2019. The message is:</p>
<pre><code class="lang-auto">c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=at::Tensor
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=at::Generator
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=c10::impl::InlineDeviceGuard&lt;c10::impl::VirtualGuardImpl&gt;
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt;&gt;
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=std::vector&lt;c10::ShapeSymbol,std::allocator&lt;c10::ShapeSymbol&gt;&gt;
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=std::vector&lt;c10::optional&lt;c10::Stride&gt;,std::allocator&lt;c10::optional&lt;c10::Stride&gt;&gt;&gt;
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=c10::QualifiedName
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=c10::impl::VirtualGuardImpl
        ]

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=c10::OperatorName
        ]

c10/util/Optional.h(395): warning C4624: 'c10::trivially_copyable_optimization_optional_base&lt;T&gt;': destructor was implicitly defined as deleted
        with
        [
            T=at::Tensor
        ]
</code></pre>
<p>Then I look into the source file, the related lines are:</p>
<pre><code class="lang-auto">template &lt;class T&gt;
union constexpr_storage_t {
  unsigned char dummy_;
  T value_;

  constexpr constexpr_storage_t(trivial_init_t) noexcept : dummy_(){};

  template &lt;class... Args&gt;
  constexpr constexpr_storage_t(Args&amp;&amp;... args)
      : value_(constexpr_forward&lt;Args&gt;(args)...) {}

  ~constexpr_storage_t() = default;
};
</code></pre>
<p>and</p>
<pre><code class="lang-auto">template &lt;class T&gt;
struct trivially_copyable_optimization_optional_base {
  bool init_;
  constexpr_storage_t&lt;T&gt; storage_;

  constexpr trivially_copyable_optimization_optional_base() noexcept
      : init_(false), storage_(trivial_init) {}

  explicit constexpr trivially_copyable_optimization_optional_base(const T&amp; v)
      : init_(true), storage_(v) {}

  explicit constexpr trivially_copyable_optimization_optional_base(T&amp;&amp; v)
      : init_(true), storage_(constexpr_move(v)) {}

  template &lt;class... Args&gt;
  explicit constexpr trivially_copyable_optimization_optional_base(
      in_place_t,
      Args&amp;&amp;... args)
      : init_(true), storage_(constexpr_forward&lt;Args&gt;(args)...) {}

  template &lt;
      class U,
      class... Args,
      TR2_OPTIONAL_REQUIRES(std::is_constructible&lt;T, std::initializer_list&lt;U&gt;&gt;)&gt;
  constexpr explicit trivially_copyable_optimization_optional_base(
      in_place_t,
      std::initializer_list&lt;U&gt; il,
      Args&amp;&amp;... args)
      : init_(true), storage_(il, std::forward&lt;Args&gt;(args)...) {}

  ~trivially_copyable_optimization_optional_base() = default;
};
</code></pre>

username: Hari_Kishan
cooked: <p>Hi…<br>
I have the same problem.<br>
Is it solved?</p>

username: kys
cooked: <p>Hello,I use libtorch-1.8-cpu-debug version, vscode and Visual Studio 2019 in Windows System.I also get the same warnings,and I execute **.exe(this is a .exe after cmake --build .) in  vscode terminal,there is nothing output,but I go to open using cmd,and go to the path corresponding to **.exe,and execute **.exe,I get the correct output.Hope it helps you.</p>

username: MaxC2
cooked: <p>Hi all.</p>
<p>Warnings are not Errors. So don’t worry about that while your build was successfull.</p>
<p>In kind of libtorch project, it’s difficult to produce a zero-warning code, in particular when it implements its own tools which are not standard, for better portability of the code.</p>
<p>In addition, given the constant evolution (nightly-build) of the project, it is possible that warnings disappear and others appear depending on developments.</p>
<p>All compilers have their own pragma to hide all or part of the warnings. For those using Visual Studio, you can even specify warning numbers to hide (/wd[num] - for warning disabled in C/C++ / Advanced Settings). If your own code is clean and you want a cleaner build, you can also reduce the level of warnings (/W0 → /W4 … default at /W3 in C/C++ / General).</p>
<p>Cheers!</p>