pytorch / extension-cpp

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

The cuda version compiled with setup.py has import error. JIT works. #50

Closed huyaoyu closed 5 years ago

huyaoyu commented 5 years ago

In teneral: the cuda version in cuda/ has import error when compiled and installed by setup.py. The JIT version under cuda/ works.

After cloning the source code, go to the cuda/ directory run python setup.py build_ext && python setup.py install compilation finishes with warnings:

====== Compilation outputs. ======

running build_ext building 'lltm_cuda' extension creating build creating build/temp.linux-x86_64-3.6 x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/TH -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/THC -I/usr/local/cuda-10.1/include -I/usr/include/python3.6m -I/home/yaoyu/p3pt/include/python3.6m -c lltm_cuda.cpp -o build/temp.linux-x86_64-3.6/lltm_cuda.o -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=lltm_cuda -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 /usr/local/cuda-10.1/bin/nvcc -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/TH -I/home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/THC -I/usr/local/cuda-10.1/include -I/usr/include/python3.6m -I/home/yaoyu/p3pt/include/python3.6m -c lltm_cuda_kernel.cu -o build/temp.linux-x86_64-3.6/lltm_cuda_kernel.o -DCUDA_NO_HALF_OPERATORS -DCUDA_NO_HALF_CONVERSIONS -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr --compiler-options '-fPIC' -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=lltm_cuda -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 lltm_cuda_kernel.cu: In lambda function: lltm_cuda_kernel.cu:119:98: warning: ‘c10::ScalarType detail::scalar_type(const at::DeprecatedTypeProperties&)’ is deprecated [-Wdeprecated-declarations] AT_DISPATCH_FLOATING_TYPES(gates.type(), "lltm_forward_cuda", ([&] { ^ /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/ATen/Dispatch.h:78:1: note: declared here inline at::ScalarType scalar_type(const at::DeprecatedTypeProperties &t) { ^~~ lltm_cuda_kernel.cu: In lambda function: lltm_cuda_kernel.cu:152:94: warning: ‘c10::ScalarType detail::scalar_type(const at::DeprecatedTypeProperties&)’ is deprecated [-Wdeprecated-declarations] AT_DISPATCH_FLOATING_TYPES(X.type(), "lltm_forward_cuda", ([&] { ^ /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/ATen/Dispatch.h:78:1: note: declared here inline at::ScalarType scalar_type(const at::DeprecatedTypeProperties &t) { ^~~ creating build/lib.linux-x86_64-3.6 x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.6/lltm_cuda.o build/temp.linux-x86_64-3.6/lltm_cuda_kernel.o -L/usr/local/cuda-10.1/lib64 -lcudart -o build/lib.linux-x86_64-3.6/lltm_cuda.cpython-36m-x86_64-linux-gnu.so

====== End of compilation outputs. ======

When importing lltm_cuda the following error happens

====== Import error. ======

import lltm_cuda

ImportError: /home/yaoyu/p3pt/lib/python3.6/site-packages/lltm_cuda-0.0.0-py3.6-linux-x86_64.egg/lltm_cuda.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN6caffe26detail37_typeMetaDataInstance_preallocated_32E

====== End of import error. ======

Try the jit.py in the cuda/ folder, the compilation outputs are as follows:

====== Compilation outputs from cuda/jit.py. ======

Using /tmp/torch_extensions as PyTorch extensions root... Detected CUDA files, patching ldflags Emitting ninja build file /tmp/torch_extensions/lltm_cuda/build.ninja... Building extension module lltm_cuda... [1/3] c++ -MMD -MF lltm_cuda.o.d -DTORCH_EXTENSION_NAME=lltm_cuda -DTORCH_API_INCLUDE_EXTENSION_H -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/TH -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/THC -isystem /usr/local/cuda-10.1/include -isystem /home/yaoyu/p3pt/include/python3.6m -D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -std=c++11 -c /home/yaoyu/Projects/DeepLearningModels/extension-cpp/cuda/lltm_cuda.cpp -o lltm_cuda.o [2/3] /usr/local/cuda-10.1/bin/nvcc -DTORCH_EXTENSION_NAME=lltm_cuda -DTORCH_API_INCLUDE_EXTENSION_H -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/TH -isystem /home/yaoyu/p3pt/lib/python3.6/site-packages/torch/include/THC -isystem /usr/local/cuda-10.1/include -isystem /home/yaoyu/p3pt/include/python3.6m -D_GLIBCXX_USE_CXX11_ABI=0 -DCUDA_NO_HALF_OPERATORS -DCUDA_NO_HALF_CONVERSIONS -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr --compiler-options '-fPIC' -std=c++11 -c /home/yaoyu/Projects/DeepLearningModels/extension-cpp/cuda/lltm_cuda_kernel.cu -o lltm_cuda_kernel.cuda.o [3/3] c++ lltm_cuda.o lltm_cuda_kernel.cuda.o -shared -L/usr/local/cuda-10.1/lib64 -lcudart -o lltm_cuda.so

====== End of compilation outputs from cuda/jit.py. ======

Then cuda/jit.py will import lltm_cuda automatically, no error happens.

huyaoyu commented 5 years ago

The error happens when doing

import lltm_cuda

before

import torch

Reversing the order of import solves the problem.

soumith commented 5 years ago

cc: @yf225

soumith commented 5 years ago

actually, importing torch first is a requirement, so I'd say this is a non-issue.