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

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.

Most of the warnings are all of the same type (mainly C4624) so I give an example of each:

|Warning|C26110|Caller failing to hold lock ‘lock’ before calling function ‘std::lock_guardstd::mutex::~lock_guardstd::mutex’.|cstorchlib|C:\Packages\libtorch1.8.0-debug-cuda10.2\include\ATen\Context.h|318||

|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||

|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||

|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||

|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||

|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||

|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||

|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||

|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||

|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||

|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||

Are any of these warnings a potential problem and why did I start getting them when upgrading to a newer version of libtorch?

I have the same issue with 1.8.3. Any solutions?

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:

c10/util/Optional.h(183): warning C4624: 'c10::constexpr_storage_t<T>': destructor was implicitly defined as deleted
        with
        [
            T=at::Tensor
        ]

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

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

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

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

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

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

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

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

c10/util/Optional.h(395): warning C4624: 'c10::trivially_copyable_optimization_optional_base<T>': destructor was implicitly defined as deleted
        with
        [
            T=at::Tensor
        ]

Then I look into the source file, the related lines are:

template <class T>
union constexpr_storage_t {
  unsigned char dummy_;
  T value_;

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

  template <class... Args>
  constexpr constexpr_storage_t(Args&&... args)
      : value_(constexpr_forward<Args>(args)...) {}

  ~constexpr_storage_t() = default;
};

and

template <class T>
struct trivially_copyable_optimization_optional_base {
  bool init_;
  constexpr_storage_t<T> storage_;

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

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

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

  template <class... Args>
  explicit constexpr trivially_copyable_optimization_optional_base(
      in_place_t,
      Args&&... args)
      : init_(true), storage_(constexpr_forward<Args>(args)...) {}

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

  ~trivially_copyable_optimization_optional_base() = default;
};

Hi…
I have the same problem.
Is it solved?

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.

Hi all.

Warnings are not Errors. So don’t worry about that while your build was successfull.

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.

In addition, given the constant evolution (nightly-build) of the project, it is possible that warnings disappear and others appear depending on developments.

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).

Cheers!