Skip to content

CMake compilation should use C++17 for PyTorch 2? #229

@GericoVi

Description

@GericoVi

Hi, I'm trying to compile the code on Windows using MSVC 2019. It seems that PyTorch 2 requires C++17 now, so if you want to build this repo for that using CMake I think line 3 in CMakeLists.txt should be changed from set(CMAKE_CXX_STANDARD 14) to set(CMAKE_CXX_STANDARD 17). The former is fine when linking PyTorch 1.8.0 for example but doesn't work for PyTorch 2.2.2 for example unless C++17 is used.

Presumably, the compilation on the python side doesn't use the CMakeLists.txt and provides it's own compiler arguments to use C++17? But when compiling it manually this seems to come up.

However, leaving set(CMAKE_CXX_STANDARD 17) but trying to compile with PyTorch 1.8.0 does not seem to work. With many of the same error but an example snippet is:

C:\Users\utcgv3g21\Desktop\libtorch-win-shared-with-deps-1.8.0+cpu\libtorch\include\c10\util\C++17.h(303): error C2872: 'std': ambiguous symbol
C:\Users\utcgv3g21\Desktop\libtorch-win-shared-with-deps-1.8.0+cpu\libtorch\include\c10/util/variant.h(2796): note: could be 'std'
C:\Users\utcgv3g21\Desktop\libtorch-win-shared-with-deps-1.8.0+cpu\libtorch\include\c10/util/C++17.h(303): note: or       'std'

Note that the same thing is true when trying to compile on MacOS with CMake (like in this example). In this case the error is pretty clear and suggests C++17:

[ 50%] Building CXX object CMakeFiles/hello-world.dir/main.cpp.o
In file included from /Users/gericovidanes/Repos/torch_cpp_test/main.cpp:1:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/script.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3:
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/ATen.h:4:2: error: C++17 or later compatible compiler is required to use ATen.
#error C++17 or later compatible compiler is required to use ATen.
 ^
In file included from /Users/gericovidanes/Repos/torch_cpp_test/main.cpp:1:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/script.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/ATen.h:7:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/Context.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/CPUGeneratorImpl.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/core/Generator.h:11:
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/Exception.h:118:34: error: no template named 'variant' in namespace 'std'
  using warning_variant_t = std::variant<UserWarning, DeprecationWarning>;
                            ~~~~~^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/Exception.h:121:7: error: unknown type name 'warning_variant_t'
      warning_variant_t type,
      ^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/Exception.h:127:7: error: unknown type name 'warning_variant_t'
      warning_variant_t type,
      ^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/Exception.h:133:7: error: unknown type name 'warning_variant_t'
      warning_variant_t type,
      ^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/Exception.h:139:3: error: unknown type name 'warning_variant_t'
  warning_variant_t type() const;
  ^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/Exception.h:146:3: error: unknown type name 'warning_variant_t'
  warning_variant_t type_;
  ^
In file included from /Users/gericovidanes/Repos/torch_cpp_test/main.cpp:1:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/script.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/ATen.h:7:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/Context.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/CPUGeneratorImpl.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/core/Generator.h:12:
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/C++17.h:27:2: error: You need C++17 to compile PyTorch
#error You need C++17 to compile PyTorch
 ^
In file included from /Users/gericovidanes/Repos/torch_cpp_test/main.cpp:1:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/script.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/ATen.h:7:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/Context.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/CPUGeneratorImpl.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/core/Generator.h:13:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/intrusive_ptr.h:4:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:5:
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/in_place.h:7:12: error: no member named 'in_place' in namespace 'std'
using std::in_place;
      ~~~~~^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/in_place.h:8:12: error: no member named 'in_place_index_t' in namespace 'std'
using std::in_place_index_t;
      ~~~~~^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/in_place.h:9:12: error: no member named 'in_place_t' in namespace 'std'
using std::in_place_t;
      ~~~~~^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/in_place.h:10:12: error: no member named 'in_place_type_t' in namespace 'std'
using std::in_place_type_t;
      ~~~~~^
In file included from /Users/gericovidanes/Repos/torch_cpp_test/main.cpp:1:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/script.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/ATen.h:7:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/Context.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/CPUGeneratorImpl.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/core/Generator.h:13:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/intrusive_ptr.h:4:
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:86:23: error: unknown type name 'in_place_t'
  explicit MaybeOwned(in_place_t, Args&&... args)
                      ^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:178:27: error: unknown type name 'in_place_t'
  static MaybeOwned owned(in_place_t, Args&&... args) {
                          ^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:130:12: error: no template named 'is_nothrow_move_constructible_v' in namespace 'std'; did you mean 'is_nothrow_move_constructible'?
      std::is_nothrow_move_constructible_v<T>&&
      ~~~~~^
/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/include/c++/v1/__type_traits/is_nothrow_move_constructible.h:27:29: note: 'is_nothrow_move_constructible' declared here
struct _LIBCPP_TEMPLATE_VIS is_nothrow_move_constructible
                            ^
In file included from /Users/gericovidanes/Repos/torch_cpp_test/main.cpp:1:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/script.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/ATen.h:7:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/Context.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/CPUGeneratorImpl.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/core/Generator.h:13:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/intrusive_ptr.h:4:
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:130:46: error: expected '(' for function-style cast or type construction
      std::is_nothrow_move_constructible_v<T>&&
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:131:16: error: no template named 'is_nothrow_move_assignable_v' in namespace 'std'; did you mean 'is_nothrow_move_assignable'?
          std::is_nothrow_move_assignable_v<borrow_type>)
          ~~~~~^
/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/include/c++/v1/__type_traits/is_nothrow_move_assignable.h:24:29: note: 'is_nothrow_move_assignable' declared here
struct _LIBCPP_TEMPLATE_VIS is_nothrow_move_assignable
                            ^
In file included from /Users/gericovidanes/Repos/torch_cpp_test/main.cpp:1:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/script.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/ATen.h:7:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/Context.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/CPUGeneratorImpl.h:3:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/ATen/core/Generator.h:13:
In file included from /Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/intrusive_ptr.h:4:
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:131:57: error: expected '(' for function-style cast or type construction
          std::is_nothrow_move_assignable_v<borrow_type>)
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
/Users/gericovidanes/miniforge3/envs/pytorch2.2/lib/python3.11/site-packages/torch/include/c10/util/MaybeOwned.h:141:12: error: no template named 'is_nothrow_move_assignable_v' in namespace 'std'; did you mean 'is_nothrow_move_assignable'?
      std::is_nothrow_move_assignable_v<T>&& std::is_nothrow_move_assignable_v<
      ~~~~~^
/Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk/usr/include/c++/v1/__type_traits/is_nothrow_move_assignable.h:24:29: note: 'is_nothrow_move_assignable' declared here
struct _LIBCPP_TEMPLATE_VIS is_nothrow_move_assignable
                            ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make[2]: *** [CMakeFiles/hello-world.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/hello-world.dir/all] Error 2
make: *** [all] Error 2

This is perhaps a PyTorch issue? But maybe a conditional in the CMakeLists.txt that uses either 14 or 17 depending on the libtorch version could work? Or just exposing this as a CMake variable?

Any thoughts on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions