pytorch / extension-cpp

C++ extensions in PyTorch
1.02k stars 214 forks source link

Custom C++ extension : torch/torch.h not found #22

Closed adarsh-kr closed 6 years ago

adarsh-kr commented 6 years ago

I am following this tutorial to create a C++ extension for Pytorch. My C++ code is giving following error :

test.cpp:3:10: fatal error: torch/torch.h: No such file or directory
 #include <torch/torch.h>

How to get torch.h header file ? Is there some pytorch-dev version ?

goldsborough commented 6 years ago

Hi, please provide more information about your environment. What version of PyTorch are you using?

adarsh-kr commented 6 years ago

I am using torch==0.4.1. I was initially running g++ compressionLayer.cc which gave me the above mentioned error.

I located the torch.h file and added the path during compilation. g++ -I /home/adarsh/.local/lib/python3.6/site-packages/torch/lib/include/ compressionLayer.cc, which compiled but then I am getting following error :

/tmp/ccIqciqg.o: In function `at::infer_type(at::Tensor const&)':
compressionLayer.cc:(.text+0x241): undefined reference to `at::Error::Error(at::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccIqciqg.o: In function `at::UndefinedTensor::singleton()':
compressionLayer.cc:(.text._ZN2at15UndefinedTensor9singletonEv[_ZN2at15UndefinedTensor9singletonEv]+0x7): undefined reference to `at::UndefinedTensor::_singleton'
collect2: error: ld returned 1 exit status
Makefile:6: recipe for target 'compressionLayer.o' failed
make: *** [compressionLayer.o] Error 1

CompressionLayer.cc is this :

#include<torch/torch.h>
#include<iostream>

at::Tensor d_sigmoid(at::Tensor z) {
        at::Tensor  s = at::sigmoid(z);
        return (1 - s) * s;
}
int main()
{}
goldsborough commented 6 years ago

What commands did you run to arrive at this compiler error? Please provide as much context as possible for me to help.

adarsh-kr commented 6 years ago

@goldsborough I have added all the info in the above comment. Please let me know if you have any questions.

goldsborough commented 6 years ago

You are not supposed to compile this code manually like this. Have you seen the setup.py and torch.utils.cpp_extensions.load instructions in the tutorial? I think it is explained quite well how to build an extension using the means we provide.

adarsh-kr commented 6 years ago

Can u pls tell me how build to this ?

goldsborough commented 6 years ago

It is explained in the tutorial here: https://pytorch.org/tutorials/advanced/cpp_extension.html#building-with-setuptools

adarsh-kr commented 6 years ago

My bad. I skipped that. Thanks