Running examples from PyTorch C++ API

I’m trying to get more familiar with ATen by playing around with it directly.

I would like to run the examples from the cppdocs, for example

#include <ATen/ATen.h>

at::Tensor a = at::ones({2, 2}, at::kInt);
at::Tensor b = at::randn({2, 2});
auto c = a + b.to(at::kInt);

I’m not super familiar with C++, so what I did was create a new C++ project my_project such that I get the following folder structure:

+ pytorch/
|
+ my_project/

and in my_project/ create a main.cpp:

#include "../pytorch/aten/src/ATen/ATen.h"

int main() {
  at::Tensor a = at::ones({2, 2}, at::kInt);
  at::Tensor b = at::randn({2, 2});
  auto c = a + b.to(at::kInt);
  return 0;
}

The include seems to be successful in that it finds the folder, however when I try to build my_project I get the following error:

In file included from .../aten_debug/main.cpp:2:
.../aten_debug/../pytorch/aten/src/ATen/ATen.h:7:10: fatal error: 'c10/core/Allocator.h' file not found`.

What do I have to do to get this to work? Note please that pytorch isn’t build, it’s just the repository, since I’m working on a laptop. I have the possibility to use a remote server with built PyTorch, but I’m not sure whether this is the problem or just some trivial misunderstanding due to my lacking C++ knowledge.

You could follow this tutorial, which explains how to write the CMakeLists.txt, setup the general project, and build it, as it seems you are trying to use a manual setup, which is missing some includes.