@@ -29,7 +29,7 @@ state in a member variable.
2929 #include <vector>
3030
3131 template <class T>
32- struct MyStackClass : torch::jit:: CustomClassHolder {
32+ struct MyStackClass : torch::CustomClassHolder {
3333 std::vector<T> stack_;
3434 MyStackClass(std::vector<T> init) : stack_(init.begin(), init.end()) {}
3535
@@ -63,7 +63,7 @@ There are several things to note:
6363 is to ensure consistent lifetime management of the object instances between languages
6464 (C++, Python and TorchScript).
6565- The second thing to notice is that the user-defined class must inherit from
66- ``torch::jit:: CustomClassHolder ``. This ensures that everything is set up to handle
66+ ``torch::CustomClassHolder ``. This ensures that everything is set up to handle
6767 the lifetime management system previously mentioned.
6868
6969Now let's take a look at how we will make this class visible to TorchScript, a process called
@@ -73,24 +73,24 @@ Now let's take a look at how we will make this class visible to TorchScript, a p
7373
7474 // Notice a few things:
7575 // - We pass the class to be registered as a template parameter to
76- // `torch::jit:: class_`. In this instance, we've passed the
76+ // `torch::class_`. In this instance, we've passed the
7777 // specialization of the MyStackClass class ``MyStackClass<std::string>``.
7878 // In general, you cannot register a non-specialized template
7979 // class. For non-templated classes, you can just pass the
8080 // class name directly as the template parameter.
81- // - The single parameter to ``torch::jit:: class_()`` is a
81+ // - The single parameter to ``torch::class_()`` is a
8282 // string indicating the name of the class. This is the name
8383 // the class will appear as in both Python and TorchScript.
8484 // For example, our MyStackClass class would appear as ``torch.classes.MyStackClass``.
8585 static auto testStack =
86- torch::jit:: class_<MyStackClass<std::string>>("MyStackClass")
86+ torch::class_<MyStackClass<std::string>>("MyStackClass")
8787 // The following line registers the contructor of our MyStackClass
8888 // class that takes a single `std::vector<std::string>` argument,
8989 // i.e. it exposes the C++ method `MyStackClass(std::vector<T> init)`.
9090 // Currently, we do not support registering overloaded
9191 // constructors, so for now you can only `def()` one instance of
92- // `torch::jit:: init`.
93- .def(torch::jit:: init<std::vector<std::string>>())
92+ // `torch::init`.
93+ .def(torch::init<std::vector<std::string>>())
9494 // The next line registers a stateless (i.e. no captures) C++ lambda
9595 // function as a method. Note that a lambda function must take a
9696 // `c10::intrusive_ptr<YourClass>` (or some const/ref version of that)
@@ -99,7 +99,7 @@ Now let's take a look at how we will make this class visible to TorchScript, a p
9999 return self->stack_.back();
100100 })
101101 // The following four lines expose methods of the MyStackClass<std::string>
102- // class as-is. `torch::jit:: class_` will automatically examine the
102+ // class as-is. `torch::class_` will automatically examine the
103103 // argument and return types of the passed-in method pointers and
104104 // expose these to Python and TorchScript accordingly. Finally, notice
105105 // that we must take the *address* of the fully-qualified method name,
@@ -307,7 +307,7 @@ Let's populate ``infer.cpp`` with the following:
307307 # include <memory>
308308
309309 int main(int argc, const char* argv[]) {
310- torch::jit:: script::Module module;
310+ torch::script::Module module;
311311 try {
312312 // Deserialize the ScriptModule from a file using torch::jit::load ().
313313 module = torch::jit::load(" foo.pt" );
@@ -394,6 +394,31 @@ And now we can run our exciting C++ binary:
394394
395395Incredible!
396396
397+ Moving Custom Classes To/From IValues
398+ -------------------------------------
399+
400+ It's also possible that you may need to move custom classes into or out of
401+ ` ` IValue` ` s, such as when you take or return ` ` IValue` ` s from TorchScript methods
402+ or you want to instantiate a custom class attribute in C++. For creating an
403+ ` ` IValue` ` from a custom C++ class instance:
404+
405+ - ` ` torch::make_custom_class<T>()` ` provides an API similar to c10::intrusive_ptr<T>
406+ in that it will take whatever set of arguments you provide to it, call the constructor
407+ of T that matches that set of arguments, and wrap that instance up and return it.
408+ However, instead of returning just a pointer to a custom class object, it returns
409+ an ` ` IValue` ` wrapping the object. You can then pass this ` ` IValue` ` directly to
410+ TorchScript.
411+ - In the event that you already have an ` ` intrusive_ptr` ` pointing to your class, you
412+ can directly construct an IValue from it using the constructor ` ` IValue(intrusive_ptr<T>)` ` .
413+
414+ For converting ` ` IValue` ` s back to custom classes:
415+
416+ - ` ` IValue::toCustomClass<T>()` ` will return an ` ` intrusive_ptr<T>` ` pointing to the
417+ custom class that the ` ` IValue` ` contains. Internally, this function is checking
418+ that ` ` T` ` is registered as a custom class and that the ` ` IValue` ` does in fact contain
419+ a custom class. You can check whether the ` ` IValue` ` contains a custom class manually by
420+ calling ` ` isCustomClass()` ` .
421+
397422Defining Serialization/Deserialization Methods for Custom C++ Classes
398423---------------------------------------------------------------------
399424
@@ -422,7 +447,7 @@ an attribute, you'll get the following error:
422447.. code-block:: shell
423448
424449 $ python export_attr.py
425- RuntimeError: Cannot serialize custom bound C++ class __torch__.torch.classes.MyStackClass. Please define serialization methods via torch::jit::pickle_ for this class. (pushIValueImpl at ../torch/csrc/jit/pickler.cpp:128)
450+ RuntimeError: Cannot serialize custom bound C++ class __torch__.torch.classes.MyStackClass. Please define serialization methods via def_pickle for this class. (pushIValueImpl at ../torch/csrc/jit/pickler.cpp:128)
426451
427452This is because TorchScript cannot automatically figure out what information
428453save from your C++ class. You must specify that manually. The way to do that
@@ -441,8 +466,8 @@ Here is an example of how we can update the registration code for our
441466.. code-block:: cpp
442467
443468 static auto testStack =
444- torch::jit:: class_<MyStackClass<std::string>>("MyStackClass")
445- .def(torch::jit:: init<std::vector<std::string>>())
469+ torch::class_<MyStackClass<std::string>>("MyStackClass")
470+ .def(torch::init<std::vector<std::string>>())
446471 .def("top", [](const c10::intrusive_ptr<MyStackClass<std::string>>& self) {
447472 return self->stack_.back();
448473 })
0 commit comments