Rate this Page

Template Classclass_#

Inheritance Relationships#

Base Type#

  • publictorch::detail::class_base

Class Documentation#

template<classCurClass>
classclass_:publictorch::detail::class_base#

Entry point for custom C++ class registration.

To register a C++ class in PyTorch, instantiatetorch::class_ with the desired class as the template parameter. Typically, this instantiation should be done in the initialization of a global variable, so that the class will be made available on dynamic library loading without any additional API calls needed. For example, to register a class named Foo, you might create a global variable like so:

staticautoregister_foo=torch::class_<Foo>("myclasses","Foo").def("myMethod",&Foo::myMethod).def("lambdaMethod",[](constc10::intrusive_ptr<Foo>&self){// Do something with `self`});
In addition to registering the class, this registration also chainsdef() calls to register methods.myMethod() is registered with a pointer to the Foo class’smyMethod() method.lambdaMethod() is registered with a C++ lambda expression.

Public Functions

inlineexplicitclass_(conststd::string&namespaceName,conststd::string&className,std::stringdoc_string="")#

This constructor actually registers the class type.

String argumentnamespaceName is an identifier for the namespace you would like this class to appear in. String argumentclassName is the name you would like to see this class exposed as in Python and TorchScript. For example, if you passfoo as the namespace name andBar as the className, the class will appear astorch.classes.foo.Bar in Python and TorchScript

template<typename...Types>
inlineclass_&def(torch::detail::types<void,Types...>,std::stringdoc_string="",std::initializer_list<arg>default_args={})#

def() can be used in conjunction withtorch::init() to register a constructor for a given C++ class type.

For example, passingtorch::init<int,std::string>() would register a two-argument constructor taking anint and astd::string as argument.

template<typenameFunc,typename...ParameterTypes>
inlineclass_&def(InitLambda<Func,c10::guts::typelist::typelist<ParameterTypes...>>init,std::stringdoc_string="",std::initializer_list<arg>default_args={})#
template<typenameFunc>
inlineclass_&def(std::stringname,Funcf,std::stringdoc_string="",std::initializer_list<arg>default_args={})#

This is the normal method registration API.

name is the name that the method will be made accessible by in Python and TorchScript.f is a callable object that defines the method. Typicallyf will either be a pointer to a method onCurClass, or a lambda expression that takes ac10::intrusive_ptr<CurClass> as the first argument (emulating athis argument in a C++ method.)

Examples:

// Exposes method `foo` on C++ class `Foo` as `call_foo()` in// Python and TorchScript.def("call_foo",&Foo::foo)// Exposes the given lambda expression as method `call_lambda()`// in Python and TorchScript..def("call_lambda",[](constc10::intrusive_ptr<Foo>&self){// do something})

template<typenameFunc>
inlineclass_&def_static(std::stringname,Funcfunc,std::stringdoc_string="")#

Method registration API for static methods.

template<typenameGetterFunc,typenameSetterFunc>
inlineclass_&def_property(conststd::string&name,GetterFuncgetter_func,SetterFuncsetter_func,std::stringdoc_string="")#

Property registration API for properties with both getter and setter functions.

template<typenameGetterFunc>
inlineclass_&def_property(conststd::string&name,GetterFuncgetter_func,std::stringdoc_string="")#

Property registration API for properties with only getter function.

template<typenameT>
inlineclass_&def_readwrite(conststd::string&name,TCurClass::*field)#

Property registration API for properties with read-write access.

template<typenameT>
inlineclass_&def_readonly(conststd::string&name,TCurClass::*field)#

Property registration API for properties with read-only access.

inlineclass_&_def_unboxed(conststd::string&name,std::function<void(jit::Stack&)>func,c10::FunctionSchemaschema,std::stringdoc_string="")#

This is an unsafe method registration API added for adding custom JIT backend support via custom C++ classes.

It is not for general purpose use.

template<typenameGetStateFn,typenameSetStateFn>
inlineclass_&def_pickle(GetStateFn&&get_state,SetStateFn&&set_state)#

def_pickle() is used to define exactly what state gets serialized or deserialized for a given instance of a custom C++ class in Python or TorchScript.

This protocol is equivalent to the Pickle concept of__getstate__ and__setstate__ from Python (https://docs.python.org/2/library/pickle.html#object.__getstate__)

Currently, both theget_state andset_state callables must be C++ lambda expressions. They should have the following signatures, whereCurClass is the class you’re registering andT1 is some object that encapsulates the state of the object.

__getstate__(intrusive_ptr<CurClass>)->T1__setstate__(T2)->intrusive_ptr<CurClass>
T1 must be an object that is convertible to IValue by the same rules for custom op/method registration.

For the common case, T1 == T2. T1 can also be a subtype of T2. An example where it makes sense for T1 and T2 to differ is ifsetstate handles legacy formats in a backwards compatible way.

Example:

.def_pickle(// __getstate__[](constc10::intrusive_ptr<MyStackClass<std::string>>&self){returnself->stack_;},[](std::vector<std::string>state){// __setstate__returnc10::make_intrusive<MyStackClass<std::string>>(std::vector<std::string>{"i","was","deserialized"});})