You need C++14 to compile PyTorch

I followed the instruction of pytorch/extension-script to register custom c++ operator.

But I changed the version of PyTorch because my network speed is poor. My Dockerfile is as follows:

FROM ubuntu:xenial

RUN apt-get update  -y \
  && apt-get install -y git cmake vim make wget gnupg build-essential software-properties-common gdb zip

# Install OpenCV
RUN apt-get install -y libopencv-dev

# Install Miniconda
RUN wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh \
  && chmod +x miniconda.sh \
  && ./miniconda.sh -b -p ~/local/miniconda \
  && rm ./miniconda.sh

# Symlink the Miniconda activation script to /activate
RUN ln -s ~/local/miniconda/bin/activate /activate
# Install PyTorch
RUN . /activate && conda install pytorch torchvision cpuonly -c pytorch

# Download LibTorch
RUN wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
RUN unzip libtorch-shared-with-deps-latest.zip && rm libtorch-shared-with-deps-latest.zip

WORKDIR /home

I changed this line:
RUN . /activate && conda install pytorch torchvision cpuonly -c pytorch

When I run : make -j, I got following error:

(base) root@4c6b54e7fed4:/home/example_app/build# make -j
Scanning dependencies of target warp_perspective
[ 25%] Building CXX object warp_perspective/CMakeFiles/warp_perspective.dir/op.cpp.o
In file included from /libtorch/include/c10/util/ArrayRef.h:19:0,
                 from /libtorch/include/c10/core/MemoryFormat.h:5,
                 from /libtorch/include/ATen/core/TensorBody.h:5,
                 from /libtorch/include/ATen/Tensor.h:11,
                 from /libtorch/include/ATen/Context.h:4,
                 from /libtorch/include/ATen/ATen.h:5,
                 from /libtorch/include/torch/csrc/api/include/torch/types.h:3,
                 from /libtorch/include/torch/script.h:3,
                 from /home/example_app/warp_perspective/op.cpp:2:
/libtorch/include/c10/util/C++17.h:20:2: error: #error You need C++14 to compile PyTorch
 #error You need C++14 to compile PyTorch
  ^
/home/example_app/warp_perspective/op.cpp: In function 'at::Tensor warp_perspective(at::Tensor, at::Tensor)':
/home/example_app/warp_perspective/op.cpp:54:48: warning: 'T* at::Tensor::data() const [with T = float]' is deprecated [-Wdeprecated-declarations]
                     /*data=*/image.data<float>());
                                                ^
In file included from /libtorch/include/ATen/Tensor.h:11:0,
                 from /libtorch/include/ATen/Context.h:4,
                 from /libtorch/include/ATen/ATen.h:5,
                 from /libtorch/include/torch/csrc/api/include/torch/types.h:3,
                 from /libtorch/include/torch/script.h:3,
                 from /home/example_app/warp_perspective/op.cpp:2:
/libtorch/include/ATen/core/TensorBody.h:322:7: note: declared here
   T * data() const {
       ^
/home/example_app/warp_perspective/op.cpp:58:46: warning: 'T* at::Tensor::data() const [with T = float]' is deprecated [-Wdeprecated-declarations]
                    /*data=*/warp.data<float>());
                                              ^
In file included from /libtorch/include/ATen/Tensor.h:11:0,
                 from /libtorch/include/ATen/Context.h:4,
                 from /libtorch/include/ATen/ATen.h:5,
                 from /libtorch/include/torch/csrc/api/include/torch/types.h:3,
                 from /libtorch/include/torch/script.h:3,
                 from /home/example_app/warp_perspective/op.cpp:2:
/libtorch/include/ATen/core/TensorBody.h:322:7: note: declared here
   T * data() const {
       ^
/home/example_app/warp_perspective/op.cpp: At global scope:
/home/example_app/warp_perspective/op.cpp:69:78: error: no matching function for call to 'torch::jit::RegisterOperators::RegisterOperators(const char [25], at::Tensor (*)(at::Tensor, at::Tensor))'
   torch::jit::RegisterOperators("my_ops::warp_perspective", &warp_perspective);
                                                                              ^
In file included from /libtorch/include/torch/script.h:6:0,
                 from /home/example_app/warp_perspective/op.cpp:2:
/libtorch/include/torch/csrc/jit/custom_operator.h:20:3: note: candidate: torch::jit::RegisterOperators::RegisterOperators(std::vector<torch::jit::Operator>)
   RegisterOperators(std::vector<Operator> operators) {
   ^
/libtorch/include/torch/csrc/jit/custom_operator.h:20:3: note:   candidate expects 1 argument, 2 provided
/libtorch/include/torch/csrc/jit/custom_operator.h:17:3: note: candidate: constexpr torch::jit::RegisterOperators::RegisterOperators()
   RegisterOperators() = default;
   ^
/libtorch/include/torch/csrc/jit/custom_operator.h:17:3: note:   candidate expects 0 arguments, 2 provided
/libtorch/include/torch/csrc/jit/custom_operator.h:16:18: note: candidate: constexpr torch::jit::RegisterOperators::RegisterOperators(const torch::jit::RegisterOperators&)
 struct TORCH_API RegisterOperators {
                  ^
/libtorch/include/torch/csrc/jit/custom_operator.h:16:18: note:   candidate expects 1 argument, 2 provided
/libtorch/include/torch/csrc/jit/custom_operator.h:16:18: note: candidate: constexpr torch::jit::RegisterOperators::RegisterOperators(torch::jit::RegisterOperators&&)
/libtorch/include/torch/csrc/jit/custom_operator.h:16:18: note:   candidate expects 1 argument, 2 provided
warp_perspective/CMakeFiles/warp_perspective.dir/build.make:62: recipe for target 'warp_perspective/CMakeFiles/warp_perspective.dir/op.cpp.o' failed
make[2]: *** [warp_perspective/CMakeFiles/warp_perspective.dir/op.cpp.o] Error 1
CMakeFiles/Makefile2:122: recipe for target 'warp_perspective/CMakeFiles/warp_perspective.dir/all' failed
make[1]: *** [warp_perspective/CMakeFiles/warp_perspective.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

How can I fix this problem? Looking forward to your replay.

I just had a similar problem

Your c++ compiler needs to support c++14, so if you have a version that does not support c++14 then you will have to upgrade to one that does.

To check compiler version
in a terminal type gcc --version

Looking at this page https://gcc.gnu.org/projects/cxx-status.html I am guessing if you compiler is not >= version 4.9 then you will need to upgrade it

If your compiler does support c++14 then you need to set a flag to -std=c++14 to tell the compiler to use c++14

if building from source I think you would do the following

in CMakeCache.txt file find CMAKE_CXX_FLAGS:STRING= and set it to CMAKE_CXX_FLAGS:STRING=-std=c++14
or CMAKE_CXX_FLAGS:STRING=-std=c++1y

or take a look at this

1 Like

When building cutome extention module use the setup.py, you can specify:

extra_compile_args={'cxx': ['-std=c++14'], 'nvcc', [some_args]}

I had similar issues compiling torchvision. I was using a compiler that supported C++14 and the CMakeLists.txt file had the correct flags set. The issue was with a CUDA flag that was getting set automatically. More info available at the issue I opened here: Failing to compile torchvision C++ API from source (>C++14 compiler required, which is installed) · Issue #4175 · pytorch/vision · GitHub