使用自定义 C++ 类扩展 TorchScript

原文:https://pytorch.org/tutorials/advanced/torch_script_custom_classes.html

本教程是自定义运算符教程的后续教程,并介绍了我们为将 C++ 类同时绑定到 TorchScript 和 Python 而构建的 API。 该 API 与pybind11非常相似,如果您熟悉该系统,则大多数概念都将转移过来。

用 C++ 实现和绑定类

在本教程中,我们将定义一个简单的 C++ 类,该类在成员变量中保持持久状态。

  1. // This header is all you need to do the C++ portions of this
  2. // tutorial
  3. #include <torch/script.h>
  4. // This header is what defines the custom class registration
  5. // behavior specifically. script.h already includes this, but
  6. // we include it here so you know it exists in case you want
  7. // to look at the API or implementation.
  8. #include <torch/custom_class.h>
  9. #include <string>
  10. #include <vector>
  11. template <class T>
  12. struct MyStackClass : torch::CustomClassHolder {
  13. std::vector<T> stack_;
  14. MyStackClass(std::vector<T> init) : stack_(init.begin(), init.end()) {}
  15. void push(T x) {
  16. stack_.push_back(x);
  17. }
  18. T pop() {
  19. auto val = stack_.back();
  20. stack_.pop_back();
  21. return val;
  22. }
  23. c10::intrusive_ptr<MyStackClass> clone() const {
  24. return c10::make_intrusive<MyStackClass>(stack_);
  25. }
  26. void merge(const c10::intrusive_ptr<MyStackClass>& c) {
  27. for (auto& elem : c->stack_) {
  28. push(elem);
  29. }
  30. }
  31. };

有几件事要注意:

  • torch/custom_class.h是您需要使用自定义类扩展 TorchScript 的标头。
  • 注意,无论何时使用自定义类的实例,我们都通过c10::intrusive_ptr<>的实例来实现。 可以将intrusive_ptr视为类似于std::shared_ptr的智能指针,但是引用计数直接存储在对象中,而不是单独的元数据块(如std::shared_ptr中所做的。torch::Tensor内部使用相同的指针类型 ;和自定义类也必须使用此指针类型,以便我们可以一致地管理不同的对象类型。
  • 注意的第二件事是用户定义的类必须继承torch::CustomClassHolder。 这样可以确保自定义类具有存储引用计数的空间。

现在让我们看一下如何使该类对 TorchScript 可见,该过程称为绑定该类:

  1. // Notice a few things:
  2. // - We pass the class to be registered as a template parameter to
  3. // `torch::class_`. In this instance, we've passed the
  4. // specialization of the MyStackClass class ``MyStackClass<std::string>``.
  5. // In general, you cannot register a non-specialized template
  6. // class. For non-templated classes, you can just pass the
  7. // class name directly as the template parameter.
  8. // - The arguments passed to the constructor make up the "qualified name"
  9. // of the class. In this case, the registered class will appear in
  10. // Python and C++ as `torch.classes.my_classes.MyStackClass`. We call
  11. // the first argument the "namespace" and the second argument the
  12. // actual class name.
  13. TORCH_LIBRARY(my_classes, m) {
  14. m.class_<MyStackClass<std::string>>("MyStackClass")
  15. // The following line registers the contructor of our MyStackClass
  16. // class that takes a single `std::vector<std::string>` argument,
  17. // i.e. it exposes the C++ method `MyStackClass(std::vector<T> init)`.
  18. // Currently, we do not support registering overloaded
  19. // constructors, so for now you can only `def()` one instance of
  20. // `torch::init`.
  21. .def(torch::init<std::vector<std::string>>())
  22. // The next line registers a stateless (i.e. no captures) C++ lambda
  23. // function as a method. Note that a lambda function must take a
  24. // `c10::intrusive_ptr<YourClass>` (or some const/ref version of that)
  25. // as the first argument. Other arguments can be whatever you want.
  26. .def("top", [](const c10::intrusive_ptr<MyStackClass<std::string>>& self) {
  27. return self->stack_.back();
  28. })
  29. // The following four lines expose methods of the MyStackClass<std::string>
  30. // class as-is. `torch::class_` will automatically examine the
  31. // argument and return types of the passed-in method pointers and
  32. // expose these to Python and TorchScript accordingly. Finally, notice
  33. // that we must take the *address* of the fully-qualified method name,
  34. // i.e. use the unary `&` operator, due to C++ typing rules.
  35. .def("push", &MyStackClass<std::string>::push)
  36. .def("pop", &MyStackClass<std::string>::pop)
  37. .def("clone", &MyStackClass<std::string>::clone)
  38. .def("merge", &MyStackClass<std::string>::merge)
  39. ;
  40. }

使用 CMake 将示例构建为 C++ 项目

现在,我们将使用 CMake 构建系统来构建上述 C++ 代码。 首先,将到目前为止介绍的所有 C++ 代码放入class.cpp文件中。 然后,编写一个简单的CMakeLists.txt文件并将其放在同一目录中。 CMakeLists.txt应该是这样的:

  1. cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
  2. project(custom_class)
  3. find_package(Torch REQUIRED)
  4. # Define our library target
  5. add_library(custom_class SHARED class.cpp)
  6. set(CMAKE_CXX_STANDARD 14)
  7. # Link against LibTorch
  8. target_link_libraries(custom_class "${TORCH_LIBRARIES}")

另外,创建一个build目录。 您的文件树应如下所示:

  1. custom_class_project/
  2. class.cpp
  3. CMakeLists.txt
  4. build/

我们假设您已经按照上一教程中所述的相同方式设置了环境。 继续并调用cmake,然后进行构建项目:

  1. $ cd build
  2. $ cmake -DCMAKE_PREFIX_PATH="$(python -c 'import torch.utils; print(torch.utils.cmake_prefix_path)')" ..
  3. -- The C compiler identification is GNU 7.3.1
  4. -- The CXX compiler identification is GNU 7.3.1
  5. -- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc
  6. -- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc -- works
  7. -- Detecting C compiler ABI info
  8. -- Detecting C compiler ABI info - done
  9. -- Detecting C compile features
  10. -- Detecting C compile features - done
  11. -- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++
  12. -- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++ -- works
  13. -- Detecting CXX compiler ABI info
  14. -- Detecting CXX compiler ABI info - done
  15. -- Detecting CXX compile features
  16. -- Detecting CXX compile features - done
  17. -- Looking for pthread.h
  18. -- Looking for pthread.h - found
  19. -- Looking for pthread_create
  20. -- Looking for pthread_create - not found
  21. -- Looking for pthread_create in pthreads
  22. -- Looking for pthread_create in pthreads - not found
  23. -- Looking for pthread_create in pthread
  24. -- Looking for pthread_create in pthread - found
  25. -- Found Threads: TRUE
  26. -- Found torch: /torchbind_tutorial/libtorch/lib/libtorch.so
  27. -- Configuring done
  28. -- Generating done
  29. -- Build files have been written to: /torchbind_tutorial/build
  30. $ make -j
  31. Scanning dependencies of target custom_class
  32. [ 50%] Building CXX object CMakeFiles/custom_class.dir/class.cpp.o
  33. [100%] Linking CXX shared library libcustom_class.so
  34. [100%] Built target custom_class

您会发现,构建目录中现在有一个动态库文件。 在 Linux 上,它可能名为libcustom_class.so。 因此,文件树应如下所示:

  1. custom_class_project/
  2. class.cpp
  3. CMakeLists.txt
  4. build/
  5. libcustom_class.so

从 Python 和 TorchScript 使用 C++ 类

现在我们已经将我们的类及其注册编译为.so文件,我们可以将.so加载到 Python 中并进行尝试。 这是一个演示脚本的脚本:

  1. import torch
  2. # `torch.classes.load_library()` allows you to pass the path to your .so file
  3. # to load it in and make the custom C++ classes available to both Python and
  4. # TorchScript
  5. torch.classes.load_library("build/libcustom_class.so")
  6. # You can query the loaded libraries like this:
  7. print(torch.classes.loaded_libraries)
  8. # prints {'/custom_class_project/build/libcustom_class.so'}
  9. # We can find and instantiate our custom C++ class in python by using the
  10. # `torch.classes` namespace:
  11. #
  12. # This instantiation will invoke the MyStackClass(std::vector<T> init)
  13. # constructor we registered earlier
  14. s = torch.classes.my_classes.MyStackClass(["foo", "bar"])
  15. # We can call methods in Python
  16. s.push("pushed")
  17. assert s.pop() == "pushed"
  18. # Returning and passing instances of custom classes works as you'd expect
  19. s2 = s.clone()
  20. s.merge(s2)
  21. for expected in ["bar", "foo", "bar", "foo"]:
  22. assert s.pop() == expected
  23. # We can also use the class in TorchScript
  24. # For now, we need to assign the class's type to a local in order to
  25. # annotate the type on the TorchScript function. This may change
  26. # in the future.
  27. MyStackClass = torch.classes.my_classes.MyStackClass
  28. @torch.jit.script
  29. def do_stacks(s: MyStackClass): # We can pass a custom class instance
  30. # We can instantiate the class
  31. s2 = torch.classes.my_classes.MyStackClass(["hi", "mom"])
  32. s2.merge(s) # We can call a method on the class
  33. # We can also return instances of the class
  34. # from TorchScript function/methods
  35. return s2.clone(), s2.top()
  36. stack, top = do_stacks(torch.classes.my_classes.MyStackClass(["wow"]))
  37. assert top == "wow"
  38. for expected in ["wow", "mom", "hi"]:
  39. assert stack.pop() == expected

使用自定义类保存,加载和运行 TorchScript 代码

我们还可以在使用 libtorch 的 C++ 进程中使用自定义注册的 C++ 类。 举例来说,让我们定义一个简单的nn.Module,它实例化并调用MyStackClass类上的方法:

  1. import torch
  2. torch.classes.load_library('build/libcustom_class.so')
  3. class Foo(torch.nn.Module):
  4. def __init__(self):
  5. super().__init__()
  6. def forward(self, s: str) -> str:
  7. stack = torch.classes.my_classes.MyStackClass(["hi", "mom"])
  8. return stack.pop() + s
  9. scripted_foo = torch.jit.script(Foo())
  10. print(scripted_foo.graph)
  11. scripted_foo.save('foo.pt')

我们文件系统中的foo.pt现在包含我们刚刚定义的序列化 TorchScript 程序。

现在,我们将定义一个新的 CMake 项目,以展示如何加载此模型及其所需的.so文件。 有关如何执行此操作的完整说明,请查看在 C++ 中加载 TorchScript 模型的教程。

与之前类似,让我们创建一个包含以下内容的文件结构:

  1. cpp_inference_example/
  2. infer.cpp
  3. CMakeLists.txt
  4. foo.pt
  5. build/
  6. custom_class_project/
  7. class.cpp
  8. CMakeLists.txt
  9. build/

请注意,我们已经复制了序列化的foo.pt文件以及上面custom_class_project的源代码树。 我们将把custom_class_project作为依赖项添加到此 C++ 项目中,以便可以将自定义类构建到二进制文件中。

让我们用以下内容填充infer.cpp

  1. #include <torch/script.h>
  2. #include <iostream>
  3. #include <memory>
  4. int main(int argc, const char* argv[]) {
  5. torch::jit::Module module;
  6. try {
  7. // Deserialize the ScriptModule from a file using torch::jit::load().
  8. module = torch::jit::load("foo.pt");
  9. }
  10. catch (const c10::Error& e) {
  11. std::cerr << "error loading the model\n";
  12. return -1;
  13. }
  14. std::vector<c10::IValue> inputs = {"foobarbaz"};
  15. auto output = module.forward(inputs).toString();
  16. std::cout << output->string() << std::endl;
  17. }

同样,让我们​​定义CMakeLists.txt文件:

  1. cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
  2. project(infer)
  3. find_package(Torch REQUIRED)
  4. add_subdirectory(custom_class_project)
  5. # Define our library target
  6. add_executable(infer infer.cpp)
  7. set(CMAKE_CXX_STANDARD 14)
  8. # Link against LibTorch
  9. target_link_libraries(infer "${TORCH_LIBRARIES}")
  10. # This is where we link in our libcustom_class code, making our
  11. # custom class available in our binary.
  12. target_link_libraries(infer -Wl,--no-as-needed custom_class)

您知道练习:cd buildcmakemake

  1. $ cd build
  2. $ cmake -DCMAKE_PREFIX_PATH="$(python -c 'import torch.utils; print(torch.utils.cmake_prefix_path)')" ..
  3. -- The C compiler identification is GNU 7.3.1
  4. -- The CXX compiler identification is GNU 7.3.1
  5. -- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc
  6. -- Check for working C compiler: /opt/rh/devtoolset-7/root/usr/bin/cc -- works
  7. -- Detecting C compiler ABI info
  8. -- Detecting C compiler ABI info - done
  9. -- Detecting C compile features
  10. -- Detecting C compile features - done
  11. -- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++
  12. -- Check for working CXX compiler: /opt/rh/devtoolset-7/root/usr/bin/c++ -- works
  13. -- Detecting CXX compiler ABI info
  14. -- Detecting CXX compiler ABI info - done
  15. -- Detecting CXX compile features
  16. -- Detecting CXX compile features - done
  17. -- Looking for pthread.h
  18. -- Looking for pthread.h - found
  19. -- Looking for pthread_create
  20. -- Looking for pthread_create - not found
  21. -- Looking for pthread_create in pthreads
  22. -- Looking for pthread_create in pthreads - not found
  23. -- Looking for pthread_create in pthread
  24. -- Looking for pthread_create in pthread - found
  25. -- Found Threads: TRUE
  26. -- Found torch: /local/miniconda3/lib/python3.7/site-packages/torch/lib/libtorch.so
  27. -- Configuring done
  28. -- Generating done
  29. -- Build files have been written to: /cpp_inference_example/build
  30. $ make -j
  31. Scanning dependencies of target custom_class
  32. [ 25%] Building CXX object custom_class_project/CMakeFiles/custom_class.dir/class.cpp.o
  33. [ 50%] Linking CXX shared library libcustom_class.so
  34. [ 50%] Built target custom_class
  35. Scanning dependencies of target infer
  36. [ 75%] Building CXX object CMakeFiles/infer.dir/infer.cpp.o
  37. [100%] Linking CXX executable infer
  38. [100%] Built target infer

现在我们可以运行令人兴奋的 C++ 二进制文件:

  1. $ ./infer
  2. momfoobarbaz

难以置信!

将自定义类移入或移出IValue

也可能需要将自定义类从自定义 C++ 类实例移入或移出IValue, such as when you take or return IValues from TorchScript methods or you want to instantiate a custom class attribute in C++. For creating an IValue:

  • torch::make_custom_class<T>()提供类似于c10::intrusive_ptr<T>的 API,因为它将采用您提供给它的任何参数集,调用与该参数集匹配的T的构造器,并包装该实例,然后返回。 但是,它不仅返回指向自定义类对象的指针,还返回包装对象的IValue。 然后,您可以将此IValue直接传递给 TorchScript。
  • 如果您已经有一个指向类的intrusive_ptr,则可以使用构造器IValue(intrusive_ptr<T>)直接从其构造IValue

要将IValue转换回自定义类:

  • IValue::toCustomClass<T>()将返回一个intrusive_ptr<T>,指向IValue包含的自定义类。 在内部,此函数正在检查T是否已注册为自定义类,并且IValue实际上确实包含一个自定义类。 您可以通过调用isCustomClass()来手动检查IValue是否包含自定义类。

为自定义 C++ 类定义序列化/反序列化方法

如果您尝试将具有自定义绑定 C++ 类的ScriptModule保存为属性,则会出现以下错误:

  1. # export_attr.py
  2. import torch
  3. torch.classes.load_library('build/libcustom_class.so')
  4. class Foo(torch.nn.Module):
  5. def __init__(self):
  6. super().__init__()
  7. self.stack = torch.classes.my_classes.MyStackClass(["just", "testing"])
  8. def forward(self, s: str) -> str:
  9. return self.stack.pop() + s
  10. scripted_foo = torch.jit.script(Foo())
  11. scripted_foo.save('foo.pt')
  12. loaded = torch.jit.load('foo.pt')
  13. print(loaded.stack.pop())
  1. $ python export_attr.py
  2. RuntimeError: Cannot serialize custom bound C++ class __torch__.torch.classes.my_classes.MyStackClass. Please define serialization methods via def_pickle for this class. (pushIValueImpl at ../torch/csrc/jit/pickler.cpp:128)

这是因为 TorchScript 无法自动找出 C++ 类中保存的信息。 您必须手动指定。 这样做的方法是使用class_上的特殊def_pickle方法在类上定义__getstate____setstate__方法。

注意

TorchScript 中__getstate____setstate__的语义与 Python pickle模块的语义相同。 您可以阅读更多有关如何使用这些方法的信息。

这是def_pickle调用的示例,我们可以将其添加到MyStackClass的注册中以包括序列化方法:

  1. // class_<>::def_pickle allows you to define the serialization
  2. // and deserialization methods for your C++ class.
  3. // Currently, we only support passing stateless lambda functions
  4. // as arguments to def_pickle
  5. .def_pickle(
  6. // __getstate__
  7. // This function defines what data structure should be produced
  8. // when we serialize an instance of this class. The function
  9. // must take a single `self` argument, which is an intrusive_ptr
  10. // to the instance of the object. The function can return
  11. // any type that is supported as a return value of the TorchScript
  12. // custom operator API. In this instance, we've chosen to return
  13. // a std::vector<std::string> as the salient data to preserve
  14. // from the class.
  15. [](const c10::intrusive_ptr<MyStackClass<std::string>>& self)
  16. -> std::vector<std::string> {
  17. return self->stack_;
  18. },
  19. // __setstate__
  20. // This function defines how to create a new instance of the C++
  21. // class when we are deserializing. The function must take a
  22. // single argument of the same type as the return value of
  23. // `__getstate__`. The function must return an intrusive_ptr
  24. // to a new instance of the C++ class, initialized however
  25. // you would like given the serialized state.
  26. [](std::vector<std::string> state)
  27. -> c10::intrusive_ptr<MyStackClass<std::string>> {
  28. // A convenient way to instantiate an object and get an
  29. // intrusive_ptr to it is via `make_intrusive`. We use
  30. // that here to allocate an instance of MyStackClass<std::string>
  31. // and call the single-argument std::vector<std::string>
  32. // constructor with the serialized state.
  33. return c10::make_intrusive<MyStackClass<std::string>>(std::move(state));
  34. });

注意

我们在 Pickle API 中采用与pybind11不同的方法。pybind11作为传递给class_::def()的特殊函数pybind11::pickle(),为此我们有一个单独的方法def_pickle。 这是因为torch::jit::pickle这个名称已经被使用了,我们不想引起混淆。

以这种方式定义(反)序列化行为后,脚本现在可以成功运行:

  1. $ python ../export_attr.py
  2. testing

定义接受或返回绑定 C++ 类的自定义运算符

定义自定义 C++ 类后,您还可以将该类用作自变量或从自定义运算符返回(即自由函数)。 假设您具有以下自由函数:

  1. c10::intrusive_ptr<MyStackClass<std::string>> manipulate_instance(const c10::intrusive_ptr<MyStackClass<std::string>>& instance) {
  2. instance->pop();
  3. return instance;
  4. }

您可以在TORCH_LIBRARY块中运行以下代码来注册它:

  1. m.def(
  2. "foo::manipulate_instance(__torch__.torch.classes.my_classes.MyStackClass x) -> __torch__.torch.classes.my_classes.MyStackClass Y",
  3. manipulate_instance
  4. );

有关注册 API 的更多详细信息,请参见自定义操作教程

完成此操作后,您可以像以下示例一样使用操作:

  1. class TryCustomOp(torch.nn.Module):
  2. def __init__(self):
  3. super(TryCustomOp, self).__init__()
  4. self.f = torch.classes.my_classes.MyStackClass(["foo", "bar"])
  5. def forward(self):
  6. return torch.ops.foo.manipulate_instance(self.f)

注意

注册使用 C++ 类作为参数的运算符时,要求已注册自定义类。 您可以通过确保自定义类注册和您的自由函数定义在同一TORCH_LIBRARY块中,并确保自定义类注册位于第一位来强制实现此操作。 将来,我们可能会放宽此要求,以便可以按任何顺序进行注册。

总结

本教程向您介绍了如何向 TorchScript(以及扩展为 Python)公开 C++ 类,如何注册其方法,如何从 Python 和 TorchScript 使用该类以及如何使用该类保存和加载代码以及运行该代码。 在独立的 C++ 过程中。 现在,您可以使用与第三方 C++ 库连接的 C++ 类扩展 TorchScript 模型,或实现需要 Python,TorchScript 和 C++ 之间的界线平滑融合的任何其他用例。

与往常一样,如果您遇到任何问题或疑问,可以使用我们的论坛GitHub ISSUE 进行联系。 另外,我们的常见问题解答(FAQ)页面可能包含有用的信息。