|
| 1 | +// BEGIN class |
| 2 | +// This header is all you need to do the C++ portions of this |
| 3 | +// tutorial |
| 4 | +#include <torch/script.h> |
| 5 | +// This header is what defines the custom class registration |
| 6 | +// behavior specifically. script.h already includes this, but |
| 7 | +// we include it here so you know it exists in case you want |
| 8 | +// to look at the API or implementation. |
| 9 | +#include <torch/custom_class.h> |
| 10 | + |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +template <class T> |
| 15 | +struct MyStackClass : torch::CustomClassHolder { |
| 16 | + std::vector<T> stack_; |
| 17 | + MyStackClass(std::vector<T> init) : stack_(init.begin(), init.end()) {} |
| 18 | + |
| 19 | + void push(T x) { |
| 20 | + stack_.push_back(x); |
| 21 | + } |
| 22 | + T pop() { |
| 23 | + auto val = stack_.back(); |
| 24 | + stack_.pop_back(); |
| 25 | + return val; |
| 26 | + } |
| 27 | + |
| 28 | + c10::intrusive_ptr<MyStackClass> clone() const { |
| 29 | + return c10::make_intrusive<MyStackClass>(stack_); |
| 30 | + } |
| 31 | + |
| 32 | + void merge(const c10::intrusive_ptr<MyStackClass>& c) { |
| 33 | + for (auto& elem : c->stack_) { |
| 34 | + push(elem); |
| 35 | + } |
| 36 | + } |
| 37 | +}; |
| 38 | +// END class |
| 39 | + |
| 40 | +#ifdef NO_PICKLE |
| 41 | + |
| 42 | +// BEGIN binding |
| 43 | +// Notice a few things: |
| 44 | +// - We pass the class to be registered as a template parameter to |
| 45 | +// `torch::class_`. In this instance, we've passed the |
| 46 | +// specialization of the MyStackClass class ``MyStackClass<std::string>``. |
| 47 | +// In general, you cannot register a non-specialized template |
| 48 | +// class. For non-templated classes, you can just pass the |
| 49 | +// class name directly as the template parameter. |
| 50 | +// - The arguments passed to the constructor make up the "qualified name" |
| 51 | +// of the class. In this case, the registered class will appear in |
| 52 | +// Python and C++ as `torch.classes.my_classes.MyStackClass`. We call |
| 53 | +// the first argument the "namespace" and the second argument the |
| 54 | +// actual class name. |
| 55 | +static auto testStack = |
| 56 | + torch::class_<MyStackClass<std::string>>("my_classes", "MyStackClass") |
| 57 | + // The following line registers the contructor of our MyStackClass |
| 58 | + // class that takes a single `std::vector<std::string>` argument, |
| 59 | + // i.e. it exposes the C++ method `MyStackClass(std::vector<T> init)`. |
| 60 | + // Currently, we do not support registering overloaded |
| 61 | + // constructors, so for now you can only `def()` one instance of |
| 62 | + // `torch::init`. |
| 63 | + .def(torch::init<std::vector<std::string>>()) |
| 64 | + // The next line registers a stateless (i.e. no captures) C++ lambda |
| 65 | + // function as a method. Note that a lambda function must take a |
| 66 | + // `c10::intrusive_ptr<YourClass>` (or some const/ref version of that) |
| 67 | + // as the first argument. Other arguments can be whatever you want. |
| 68 | + .def("top", [](const c10::intrusive_ptr<MyStackClass<std::string>>& self) { |
| 69 | + return self->stack_.back(); |
| 70 | + }) |
| 71 | + // The following four lines expose methods of the MyStackClass<std::string> |
| 72 | + // class as-is. `torch::class_` will automatically examine the |
| 73 | + // argument and return types of the passed-in method pointers and |
| 74 | + // expose these to Python and TorchScript accordingly. Finally, notice |
| 75 | + // that we must take the *address* of the fully-qualified method name, |
| 76 | + // i.e. use the unary `&` operator, due to C++ typing rules. |
| 77 | + .def("push", &MyStackClass<std::string>::push) |
| 78 | + .def("pop", &MyStackClass<std::string>::pop) |
| 79 | + .def("clone", &MyStackClass<std::string>::clone) |
| 80 | + .def("merge", &MyStackClass<std::string>::merge); |
| 81 | +// END binding |
| 82 | + |
| 83 | +#else |
| 84 | + |
| 85 | +// BEGIN pickle_binding |
| 86 | +static auto testStack = |
| 87 | + torch::class_<MyStackClass<std::string>>("my_classes", "MyStackClass") |
| 88 | + .def(torch::init<std::vector<std::string>>()) |
| 89 | + .def("top", [](const c10::intrusive_ptr<MyStackClass<std::string>>& self) { |
| 90 | + return self->stack_.back(); |
| 91 | + }) |
| 92 | + .def("push", &MyStackClass<std::string>::push) |
| 93 | + .def("pop", &MyStackClass<std::string>::pop) |
| 94 | + .def("clone", &MyStackClass<std::string>::clone) |
| 95 | + .def("merge", &MyStackClass<std::string>::merge) |
| 96 | + // class_<>::def_pickle allows you to define the serialization |
| 97 | + // and deserialization methods for your C++ class. |
| 98 | + // Currently, we only support passing stateless lambda functions |
| 99 | + // as arguments to def_pickle |
| 100 | + .def_pickle( |
| 101 | + // __getstate__ |
| 102 | + // This function defines what data structure should be produced |
| 103 | + // when we serialize an instance of this class. The function |
| 104 | + // must take a single `self` argument, which is an intrusive_ptr |
| 105 | + // to the instance of the object. The function can return |
| 106 | + // any type that is supported as a return value of the TorchScript |
| 107 | + // custom operator API. In this instance, we've chosen to return |
| 108 | + // a std::vector<std::string> as the salient data to preserve |
| 109 | + // from the class. |
| 110 | + [](const c10::intrusive_ptr<MyStackClass<std::string>>& self) |
| 111 | + -> std::vector<std::string> { |
| 112 | + return self->stack_; |
| 113 | + }, |
| 114 | + // __setstate__ |
| 115 | + // This function defines how to create a new instance of the C++ |
| 116 | + // class when we are deserializing. The function must take a |
| 117 | + // single argument of the same type as the return value of |
| 118 | + // `__getstate__`. The function must return an intrusive_ptr |
| 119 | + // to a new instance of the C++ class, initialized however |
| 120 | + // you would like given the serialized state. |
| 121 | + [](std::vector<std::string> state) |
| 122 | + -> c10::intrusive_ptr<MyStackClass<std::string>> { |
| 123 | + // A convenient way to instantiate an object and get an |
| 124 | + // intrusive_ptr to it is via `make_intrusive`. We use |
| 125 | + // that here to allocate an instance of MyStackClass<std::string> |
| 126 | + // and call the single-argument std::vector<std::string> |
| 127 | + // constructor with the serialized state. |
| 128 | + return c10::make_intrusive<MyStackClass<std::string>>(std::move(state)); |
| 129 | + }); |
| 130 | +// END pickle_binding |
| 131 | + |
| 132 | +// BEGIN free_function |
| 133 | +c10::intrusive_ptr<MyStackClass<std::string>> manipulate_instance(const c10::intrusive_ptr<MyStackClass<std::string>>& instance) { |
| 134 | + instance->pop(); |
| 135 | + return instance; |
| 136 | +} |
| 137 | + |
| 138 | +static auto instance_registry = torch::RegisterOperators().op( |
| 139 | +torch::RegisterOperators::options() |
| 140 | + .schema( |
| 141 | + "foo::manipulate_instance(__torch__.torch.classes.my_classes.MyStackClass x) -> __torch__.torch.classes.my_classes.MyStackClass Y") |
| 142 | + .catchAllKernel<decltype(manipulate_instance), &manipulate_instance>()); |
| 143 | +// END free_function |
| 144 | + |
| 145 | +#endif |
0 commit comments