Error while running simple pytorch code in the C++ front end

username: johndoe2648
cooked: <p>Hi guys,</p>
<p>I am new to PyTorch or any other neural network package. I was running the example program of printing out a torch tensor in the installation page. I am able to successfully build it. But, while running the ‘make’ command, I am getting error. The details are given below.</p>
<p>I have installed (copied and unzipped) Pytorch C++ frontend in Centos 6.9. It has GNU C++ compiler 4.4.7, and I use a separately installed CMake of version 3.15.0.</p>
<p>Then, I wrote the code ‘first_example.cpp’</p>
<blockquote>
<p><span class="hashtag-raw">#include</span> &lt;torch/torch.h&gt;<br>
<span class="hashtag-raw">#include</span> </p>
<p>int main() {<br>
torch::Tensor tensor = torch::rand({2, 3});<br>
std::cout &lt;&lt; tensor &lt;&lt; std::endl;<br>
}</p>
</blockquote>
<p>I compiled it using the CMakeLists.txt:</p>
<blockquote>
<p>cmake_minimum_required(VERSION 3.0 FATAL_ERROR)<br>
project(example-app)</p>
<p>find_package(Torch REQUIRED)</p>
<p>add_executable(first_example first_example.cpp)<br>
target_link_libraries(example-app “${TORCH_LIBRARIES}”)<br>
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)</p>
</blockquote>
<p>with command:</p>
<blockquote>
<p>/full/path/to/cmake -DCMAKE_PREFIX_PATH=full/path/to/libtorch …</p>
</blockquote>
<p>This compiled OK.</p>
<blockquote>
<p>– The C compiler identification is GNU 4.4.7<br>
– The CXX compiler identification is GNU 4.4.7<br>
– Check for working C compiler: /usr/bin/cc<br>
– Check for working C compiler: /usr/bin/cc – works<br>
– Detecting C compiler ABI info<br>
– Detecting C compiler ABI info - done<br>
– Detecting C compile features<br>
– Detecting C compile features - done<br>
– Check for working CXX compiler: /usr/bin/c++<br>
– Check for working CXX compiler: /usr/bin/c++ – works<br>
– Detecting CXX compiler ABI info<br>
– Detecting CXX compiler ABI info - done<br>
– Detecting CXX compile features<br>
– Detecting CXX compile features - done<br>
– Looking for pthread.h<br>
– Looking for pthread.h - found<br>
– Performing Test CMAKE_HAVE_LIBC_PTHREAD<br>
– Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed<br>
– Looking for pthread_create in pthreads<br>
– Looking for pthread_create in pthreads - not found<br>
– Looking for pthread_create in pthread<br>
– Looking for pthread_create in pthread - found<br>
– Found Threads: TRUE<br>
– Found torch: /export/apps/pytorch/libtorch/lib/libtorch.so<br>
– Configuring done<br>
– Generating done<br>
– Build files have been written to: /export/apps/pytorch/libtorch/examples_dir/build</p>
</blockquote>
<p>Now, when I run the ‘make’ command, I get the following error.</p>
<blockquote>
<p>In file included from /export/apps/pytorch/libtorch/include/c10/core/Backend.h:5,<br>
from /export/apps/pytorch/libtorch/include/c10/core/Layout.h:3,<br>
from /export/apps/pytorch/libtorch/include/ATen/core/Tensor.h:4,<br>
from /export/apps/pytorch/libtorch/include/ATen/Tensor.h:2,<br>
from /export/apps/pytorch/libtorch/include/ATen/Context.h:4,<br>
from /export/apps/pytorch/libtorch/include/ATen/ATen.h:5,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/types.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/all.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/torch.h:3,<br>
from /export/apps/pytorch/libtorch/examples_dir/first_example.cpp:1:<br>
/export/apps/pytorch/libtorch/include/c10/core/TensorTypeIdRegistration.h:16:18: error: atomic: No such file or directory<br>
In file included from /export/apps/pytorch/libtorch/include/c10/core/Device.h:3,<br>
from /export/apps/pytorch/libtorch/include/c10/core/Allocator.h:6,<br>
from /export/apps/pytorch/libtorch/include/ATen/ATen.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/types.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/all.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/torch.h:3,<br>
from /export/apps/pytorch/libtorch/examples_dir/first_example.cpp:1:<br>
/export/apps/pytorch/libtorch/include/c10/core/DeviceType.h:34: error: ‘constexpr’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/core/DeviceType.h:35: error: ‘constexpr’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/core/DeviceType.h:36: error: ‘constexpr’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/core/DeviceType.h:37: error: ‘constexpr’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/core/DeviceType.h:38: error: ‘constexpr’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/core/DeviceType.h:41: error: expected constructor, destructor, or type conversion before ‘int’<br>
/export/apps/pytorch/libtorch/include/c10/core/DeviceType.h:44: error: ‘COMPILE_TIME_MAX_DEVICE_TYPES’ was not declared in this scope<br>
In file included from /export/apps/pytorch/libtorch/include/c10/core/Device.h:5,<br>
from /export/apps/pytorch/libtorch/include/c10/core/Allocator.h:6,<br>
from /export/apps/pytorch/libtorch/include/ATen/ATen.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/types.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/all.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/torch.h:3,<br>
from /export/apps/pytorch/libtorch/examples_dir/first_example.cpp:1:<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:71: error: expected ‘;’ before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:75: error: expected ‘;’ before ‘const’<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:75: error: expected ‘;’ before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:80: error: expected ‘;’ before ‘const’<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:80: error: expected ‘;’ before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:83: error: expected ‘;’ before ‘}’ token<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:28: error: looser throw specifier for ‘virtual c10::Error::~Error()’<br>
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/…/…/…/…/include/c++/4.4.7/exception:63: error:   overriding ‘virtual std::exception::~exception() throw ()’<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:49: error: ‘nullptr’ was not declared in this scope<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:57: error: ‘nullptr’ was not declared in this scope<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:86: error: expected nested-name-specifier before ‘handler_t’<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:86: error: using-declaration for non-member at class scope<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:86: error: expected ‘;’ before ‘=’ token<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:86: error: expected unqualified-id before ‘=’ token<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:95: error: ‘handler_t’ has not been declared<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:102: error: ‘handler_t’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/util/Exception.h:109: error: ‘c10::Error::Error’ names constructor<br>
In file included from /export/apps/pytorch/libtorch/include/c10/core/Allocator.h:6,<br>
from /export/apps/pytorch/libtorch/include/ATen/ATen.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/types.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader_options.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/base.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader/stateful.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data/dataloader.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/data.h:3,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/all.h:4,<br>
from /export/apps/pytorch/libtorch/include/torch/csrc/api/include/torch/torch.h:3,<br>
from /export/apps/pytorch/libtorch/examples_dir/first_example.cpp:1:<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:18: error: expected nested-name-specifier before ‘DeviceIndex’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:18: error: ‘DeviceIndex’ has not been declared<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:18: error: expected ‘;’ before ‘=’ token<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:18: error: expected unqualified-id before ‘=’ token<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:30: error: variable ‘c10::Device c10::final’ has initializer but incomplete type<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:31: error: expected primary-expression before ‘using’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:31: error: expected ‘}’ before ‘using’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:31: error: expected ‘,’ or ‘;’ before ‘using’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:35: error: expected ‘)’ before ‘type’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:45: error: expected unqualified-id before ‘const’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:45: error: expected ‘)’ before ‘const’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:49: error: expected initializer before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:55: error: expected initializer before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:60: error: variable or field ‘set_index’ declared void<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:60: error: ‘DeviceIndex’ was not declared in this scope<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:65: error: expected initializer before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:70: error: ‘DeviceIndex’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:75: error: expected initializer before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:80: error: expected initializer before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:85: error: expected initializer before ‘noexcept’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:89: error: expected unqualified-id before ‘private’<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:91: error: ‘DeviceIndex’ does not name a type<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h: In function ‘void c10::validate()’:<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:93: error: ‘index_’ was not declared in this scope<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:95: error: ‘is_cpu’ was not declared in this scope<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:95: error: ‘index_’ was not declared in this scope<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h: At global scope:<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:102: error: expected ‘,’ or ‘…’ before ‘&amp;’ token<br>
/export/apps/pytorch/libtorch/include/c10/core/Device.h:104: error: expected declaration before ‘}’ token<br>
make[2]: *** [CMakeFiles/first_example.dir/first_example.cpp.o] Error 1<br>
make[1]: *** [CMakeFiles/first_example.dir/all] Error 2<br>
make: *** [all] Error 2</p>
</blockquote>
<p>I also compiled with the g++ command as follows:</p>
<blockquote>
<p>g++ -std=c++0x -I /export/apps/pytorch/libtorch/include/torch/csrc/api/include/ first_example.cpp</p>
</blockquote>
<p>I get the same error as above.</p>
<p>I am not sure what the issue is. Can anyone help?</p>

username: tom
cooked: <p>It’s always good to look at the very first error, in your case <code>/export/apps/pytorch/libtorch/include/c10/core/TensorTypeIdRegistration.h:16:18: error: atomic: No such file or directory</code>. So gcc 4.4 does not have support for <code>#include &lt;atomic&gt;</code>, you need to find a newer gcc.</p>
<p>Best regards</p>
<p>Thomas</p>

username: johndoe2648
cooked: <p>Thank you very much tom, and sorry about the delayed response.</p>
<p>As you suggested, installing a newer version of GCC might partially resolve the issue. However, I have few doubts too regarding that.</p>
<ol>
<li>The current version of GCC is 9.1. Should I go for the latest version, or is there an upper bound to the GCC compiler I should use?</li>
<li>Does PyTorch C++ frontend have dependencies on versions of MPI and/or any other packages that you are aware of? Is there a documentation or blog that you know which is informative on the matter.</li>
</ol>
<p>Furthermore, the reason I have chosen this path is because I do not have GPU card. But, I do have a cluster that runs on Rocks Cluster. As mentioned in the <a href="https://pytorch.org/cppdocs/frontend.html" rel="nofollow noopener">link</a> (under the Philosophy heading), the C++ frontend exploits multi-threading and Python does not.</p>

username: tom
cooked: <p>I have built PyTorch successfully with gcc 4.9 - gcc 8. There is a list in the <a href="https://github.com/pytorch/pytorch/#from-source" rel="nofollow noopener">README</a>.</p>
<p>PyTorch will make use of multiple threads all right for its ops, it will be very specific circumstances when it’s insufficient.</p>
<p>Best regards</p>
<p>Thomas</p>

username: johndoe2648
cooked: <p>Thank you again Tom.</p>