pytorch / extension-script Goto Github PK
View Code? Open in Web Editor NEWExample repository for custom C++/CUDA operators for TorchScript
Example repository for custom C++/CUDA operators for TorchScript
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.
Hello,
The instructions worked for me right up until make -j
. When I run that, I get:
Scanning dependencies of target warp_perspective
[ 25%] Building CXX object warp_perspective/CMakeFiles/warp_perspective.dir/op.cpp.o
/home/example_app/warp_perspective/op.cpp:23:78: warning: 'torch::jit::RegisterOperators::RegisterOperators(const string&, Implementation&&) [with Implementation = at::Tensor (*)(at::Tensor, at::Tensor); std::string = std::basic_string<char>]' is deprecated [-Wdeprecated-declarations]
torch::jit::RegisterOperators("my_ops::warp_perspective", &warp_perspective);
^
In file included from /libtorch/include/torch/script.h:5:0,
from /home/example_app/warp_perspective/op.cpp:2:
/libtorch/include/torch/csrc/jit/custom_operator.h:28:3: note: declared here
RegisterOperators(const std::string& name, Implementation&& implementation) {
^
[ 50%] Linking CXX shared library libwarp_perspective.so
[ 50%] Built target warp_perspective
Scanning dependencies of target example_app
[ 75%] Building CXX object CMakeFiles/example_app.dir/main.cpp.o
/home/example_app/main.cpp: In function 'int main(int, const char**)':
/home/example_app/main.cpp:14:74: error: conversion from 'torch::jit::script::Module' to non-scalar type 'std::shared_ptr<torch::jit::script::Module>' requested
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(argv[1]);
^
CMakeFiles/example_app.dir/build.make:62: recipe for target 'CMakeFiles/example_app.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/example_app.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/example_app.dir/all' failed
make[1]: *** [CMakeFiles/example_app.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
@goldsborough, is there a chance you could take a look? I'm working on revising the PyTorch tutorials for the 1.3 release, and this is currently how we recommend students run the Extending TorchScript with Custom C++ Operators tutorial. Please let me know if there's any way I can help!
When building C extensions with Cython, it also creates an .so
file, but I don't have to load that manually. Is this planned for TorchScript?
For context, I am exploring to convert all Cython from OpenPifPaf to TorchScript in this branch: https://github.com/vita-epfl/openpifpaf/tree/torchscript-extension
So I am not using the TorchScript modules in a script but in a Python module. This is not one of the use cases in this repo either, right?
The custom layer uses JIT mechanism to invoke on the c++ side, which seems to require registration in scripts, such as:
.# include <torch/script.h>
Torch:: Tensor warp_perspective (torch:: Tensor image, torch:: Tensor warp){
Torch:: Tensor output = torch:: add (image, warp);
Return output. clone ();
}
Static auto registry=
Torch:: jit:: Register Operators ("my_ops:: warp_perspective", & warp_perspective);
This is the application under Ubuntu. Do you have an example under windows?I don't know how to run the example provided in extension-script under Windows.
I tried the following:
### 1. Build on Host
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
first I got
#error You need C++14 to compile PyTorch
So I set c++ 14 in CMakeLists.txt and it worked.
set(CMAKE_CXX_STANDARD 14)
But got error while running the script.py.
With pytorch=1.3.1:
Traceback (most recent call last):
File "test.py", line 4, in <module>
print(torch.ops.my_ops.warp_perspective)
File "/home/miniconda3/lib/python3.7/site-packages/torch/_ops.py", line 61, in __getattr__
op = torch._C._jit_get_operation(qualified_op_name)
RuntimeError: No such operator my_ops::warp_perspective
with pytorch=1.2.0:
Traceback (most recent call last):
File "test.py", line 3, in <module>
torch.ops.load_library("/home/LiRuMei/cpp_pytorch/build/libwarp_perspective.so")
File "/home/anaconda3/envs/torch/lib/python3.7/site-packages/torch/_ops.py", line 106, in load_library
ctypes.CDLL(path)
File "/home/anaconda3/envs/torch/lib/python3.7/ctypes/__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /home/cpp_pytorch/build/libwarp_perspective.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKSs
### 2. Build with Docker
In docker the version is pytorch-nightly-cpu=1.2.0.
I also had an error about
static auto registry =
torch::jit::RegisterOperators("my_ops::warp_perspective", &warp_perspective);
I modified it as in the tutorial:
static auto registry =
torch::RegisterOperators("my_ops::warp_perspective", &warp_perspective);
Then I successfully built the .so, but still got:
Traceback (most recent call last):
File "script.py", line 3, in <module>
torch.ops.load_library("example_app/build/warp_perspective/libwarp_perspective.so")
File "/root/local/miniconda/lib/python3.7/site-packages/torch/_ops.py", line 106, in load_library
ctypes.CDLL(path)
File "/root/local/miniconda/lib/python3.7/ctypes/__init__.py", line 364, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /libtorch/lib/libtorch_cpu.so: undefined symbol: _ZN6caffe28TypeMeta21_typeMetaDataInstanceIcEEPKNS_6detail12TypeMetaDataEv
I think the problem is about the version of pytorch & libtorch, as the Dockerfile you committed a year ago always installed the latest version. So please try again if the Docker still works or indicate the version you used, thanks a lot.
OSError: /home/huang/vsopencv/extension-script/example_app/build/warp_perspective/libwarp_perspective.so: undefined symbol: _ZN3c1017RegisterOperators25checkSchemaAndRegisterOp_ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEONS0_7OptionsE
I changed some code as bellow:
If you got errors like C++ 14 is required
, you can add set(CMAKE_CXX_STANDARD 14)
to example/CMakeLists.txt, like this:
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(example_app)
set(CMAKE_CXX_STANDARD 14)
SET(Torch_DIR /libtorch/share/cmake/Torch)
find_package(Torch REQUIRED)
add_subdirectory(warp_perspective)
add_executable(example_app main.cpp)
target_link_libraries(example_app "${TORCH_LIBRARIES}")
target_link_libraries(example_app -Wl,--no-as-needed warp_perspective)
target_compile_features(example_app PRIVATE cxx_range_for)
torch::jit::RegisterOperators::RegisterOperators
Use torch::RegisterOperators()
instead of torch::jit::RegisterOperator()
, it will be ok.
If the downloading speed of PyTorch is too slow , you can change line 18 in your Dockerfile as follows:
RUN . /activate && conda install pytorch torchvision cpuonly -c pytorch
But when I ran python script.py
, I got the same error as #4
What is the version of pytorch and libtorch used in this project ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.