How to Build/Debug (full) LibTorch Sources on Windows?

  c++, libtorch, visual-studio

The link below is an excellent description of how to download the prebuilt libtorch windows binaries and integrate them into Visual Studio.

https://towardsdatascience.com/setting-up-a-c-project-in-visual-studio-2019-with-libtorch-1-6-ad8a0e49e82c

I downloaded both the Debug and Release distributions for pytorch 1.8.1. I was surprised to see many duplicated directories/files in the two expanded zip files. The zip files both include the headers and some source. I was tempted to merge the two hierarchies, just keeping separate bin and lib folders, but some of the header files were not identical.

During the compile of my example project, I ran into a compiler error, that I fixed per the “SBW” comment line below.

template<bool Condition, class ThenCallback>
decltype(auto) if_constexpr(ThenCallback&& thenCallback) {
#if defined(__cpp_if_constexpr)
  // If we have C++17, just use it's "if constexpr" feature instead of wrapping it.
  // This will give us better error messages.
  if constexpr(Condition) {
    if constexpr (detail::function_takes_identity_argument<ThenCallback>::value) {
      // SBW 2021.05.12 Disambiguate std.
      // https://github.com/pytorch/pytorch/pull/53490/files/2ef3c80214c798afdf165d677fc04025b28166d7
      // return std::forward<ThenCallback>(thenCallback)(detail::_identity());
      return ::std::forward<ThenCallback>(thenCallback)(detail::_identity());
    } else {
      // return std::forward<ThenCallback>(thenCallback)();
      return ::std::forward<ThenCallback>(thenCallback)();
    }
  }
#else
  // C++14 implementation of if constexpr
  return if_constexpr<Condition>(std::forward<ThenCallback>(thenCallback), [] (auto) {});
#endif
}

Debugging an example project is an excellent way to learn about a new library, so one of the first things I did for my Debug build was to add copying of the .pdb files to the Post-Build step described in the link above.

My current project involves integrating libtorch models and optimizers with existing home-grown deep learning code. We had started this work a couple years ago with TensorFlow, but it stalled when we discovered the TF c++ api doesn’t support model training.

We have a custom optimizer that requires the calculation of the Jacobian during the training phase, which is not natively supported by libtorch, so I needed to learn about the autograd library. While debugging, I quickly found myself in a stack without accompanying source code – the Windows binaries downloads only include a subset of the source.

How do I get a full set of sources?

Source: Windows Questions C++

LEAVE A COMMENT