Title: Operator Registration — PyTorch main documentation
Description: Operator registration in PyTorch C++ — TORCH_LIBRARY, TORCH_LIBRARY_IMPL for custom operators.
Keywords:
Domain: docs.pytorch.org
{
"@context": "https://schema.org",
"@type": "Article",
"name": "Operator Registration",
"headline": "Operator Registration",
"description": "Operator registration in PyTorch C++ \u2014 TORCH_LIBRARY, TORCH_LIBRARY_IMPL for custom operators.",
"url": "/api/library/registration.html",
"articleBody": "Operator Registration# The library API provides macros and classes for registering custom operators with PyTorch\u2019s dispatcher. Macros# TORCH_LIBRARY# TORCH_LIBRARY(ns, m) \u00a0 static void TORCH_LIBRARY_init_##ns(torch::Library\u0026);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\ static const torch::detail::TorchLibraryInitTORCH_LIBRARY_static_init_##ns( \\ torch::Library::DEF,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\ \u0026TORCH_LIBRARY_init_##ns,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\ C10_STRINGIZE(ns),\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\ std::nullopt,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\ __FILE__,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\ __LINE__);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\ void TORCH_LIBRARY_init_##ns( torch::Library\u0026 m)# Macro for defining a function that will be run at static initialization time to define a library of operators in the namespace ns (must be a valid C++ identifier, no quotes). Use this macro when you want to define a new set of custom operators that do not already exist in PyTorch. Example usage: TORCH_LIBRARY(myops, m) { // m is a torch::Library; methods on it will define // operators in the myops namespace m.def(\"add\", add_impl); } The m argument is bound to a torch::Library that is used to register operators. There may only be one TORCH_LIBRARY() for any given namespace. Example: TORCH_LIBRARY(myops, m) { m.def(\"add(Tensor self, Tensor other) -\u003e Tensor\", \u0026add_impl); m.def(\"mul(Tensor self, Tensor other) -\u003e Tensor\"); m.impl(\"mul\", torch::kCPU, \u0026mul_cpu_impl); m.impl(\"mul\", torch::kCUDA, \u0026mul_cuda_impl); } TORCH_LIBRARY_IMPL# TORCH_LIBRARY_IMPL(ns, k, m) _TORCH_LIBRARY_IMPL(ns, k, m, C10_UID)# Macro for defining a function that will be run at static initialization time to define operator overrides for dispatch key k (must be an unqualified enum member of c10::DispatchKey) in namespace ns (must be a valid C++ identifier, no quotes). Use this macro when you want to implement a preexisting set of custom operators on a new dispatch key (e.g., you want to provide CUDA implementations of already existing operators). One common usage pattern is to use TORCH_LIBRARY() to define schema for all new operators you want to define, and then use several TORCH_LIBRARY_IMPL() blocks to provide implementations of the operator for CPU, CUDA and Autograd. In some cases, you need to define something that applies to all namespaces, not just one namespace (usually a fallback). In that case, use the reserved namespace _, e.g., TORCH_LIBRARY_IMPL(_, XLA, m) { m.fallback(xla_fallback); } Example usage: TORCH_LIBRARY_IMPL(myops, CPU, m) { // m is a torch::Library; methods on it will define // CPU implementations of operators in the myops namespace. // It is NOT valid to call torch::Library::def() // in this context. m.impl(\"add\", add_cpu_impl); } If add_cpu_impl is an overloaded function, use a static_cast to specify which overload you want (by providing the full type). Example: TORCH_LIBRARY_IMPL(myops, XLA, m) { m.impl(\"mul\", \u0026mul_xla_impl); } TORCH_LIBRARY_FRAGMENT# TORCH_LIBRARY_FRAGMENT(ns, m) _TORCH_LIBRARY_FRAGMENT(ns, m, C10_UID)# This macro is a version of TORCH_LIBRARY() that doesn\u2019t enforce that there is only one library (it is a \u201cfragment\u201d). This is used inside the PerOpRegistration.cpp file, as well as in places where all op registrations within the same namespace cannot be easily put into one macro block (this is mostly the case for custom ops in fbcode that were ported from the old API) Example: // In file1.cpp TORCH_LIBRARY(myops, m) { m.def(\"add(Tensor self, Tensor other) -\u003e Tensor\", \u0026add_impl); } // In file2.cpp TORCH_LIBRARY_FRAGMENT(myops, m) { m.def(\"mul(Tensor self, Tensor other) -\u003e Tensor\", \u0026mul_impl); } Classes# Library# class Library# This object provides the API for defining operators and providing implementations at dispatch keys. Typically, a torch::Library is not allocated directly; instead it is created by the TORCH_LIBRARY() or TORCH_LIBRARY_IMPL() macros. Most methods on torch::Library return a reference to itself, supporting method chaining. // Examples: TORCH_LIBRARY(torchvision, m) { // m is a torch::Library m.def(\"roi_align\", ...); ... } TORCH_LIBRARY_IMPL(aten, XLA, m) { // m is a torch::Library m.impl(\"add\", ...); ... } Public Functions Library(const Library\u0026) = delete# Library \u0026operator=(const Library\u0026) = delete# Library(Library\u0026\u0026) = default# Library \u0026operator=(Library\u0026\u0026) = default# ~Library() = default# inline Library \u0026def(c10::FunctionSchema \u0026\u0026s, const std::vector\u003cat::Tag\u003e \u0026tags = {}, _RegisterOrVerify rv = _RegisterOrVerify::REGISTER) \u0026# Declare an operator with a schema, but don\u2019t provide any implementations for it. You\u2019re expected to then provide implementations using the impl() method. All template arguments are inferred. // Example: TORCH_LIBRARY(myops, m) { m.def(\"add(Tensor self, Tensor other) -\u003e Tensor\"); } Parameters: raw_schema \u2013 The schema of the operator to be defined. Typically, this is a const char* string literal, but any type accepted by torch::schema() is accepted here. inline Library \u0026def(const char *raw_schema, const std::vector\u003cat::Tag\u003e \u0026tags = {}, _RegisterOrVerify rv = _RegisterOrVerify::REGISTER) \u0026# inline Library \u0026set_python_module(const char *pymodule, const char *context = \"\")# Declares that for all operators that are subsequently def\u2019ed, their fake impls may be found in the given Python module (pymodule). This registers some help text that is used if the fake impl cannot be found. Args: pymodule: the python module context: We may include this in the error message. inline Library \u0026impl_abstract_pystub(const char *pymodule, const char *context = \"\")# Deprecated; use set_python_module instead. template\u003ctypename NameOrSchema, typename Func\u003einline Library \u0026def(NameOrSchema \u0026\u0026raw_name_or_schema, Func \u0026\u0026raw_f, const std::vector\u003cat::Tag\u003e \u0026tags = {}) \u0026# Define an operator for a schema and then register an implementation for it. This is typically what you would use if you aren\u2019t planning on making use of the dispatcher to structure your operator implementation. It\u2019s roughly equivalent to calling def() and then impl(), but if you omit the schema of the operator, we will infer it from the type of your C++ function. All template arguments are inferred. // Example: TORCH_LIBRARY(myops, m) { m.def(\"add\", add_fn); } Parameters: raw_name_or_schema \u2013 The schema of the operator to be defined, or just the name of the operator if the schema is to be inferred from raw_f. Typically a const char* literal. raw_f \u2013 The C++ function that implements this operator. Any valid constructor of torch::CppFunction is accepted here; typically you provide a function pointer or lambda. template\u003ctypename Name, typename Func\u003einline Library \u0026impl(Name name, Func \u0026\u0026raw_f, _RegisterOrVerify rv = _RegisterOrVerify::REGISTER) \u0026# Register an implementation for an operator. You may register multiple implementations for a single operator at different dispatch keys (see torch::dispatch()). Implementations must have a corresponding declaration (from def()), otherwise they are invalid. If you plan to register multiple implementations, DO NOT provide a function implementation when you def() the operator. // Example: TORCH_LIBRARY_IMPL(myops, CUDA, m) { m.impl(\"add\", add_cuda); } Parameters: name \u2013 The name of the operator to implement. Do NOT provide schema here. raw_f \u2013 The C++ function that implements this operator. Any valid constructor of torch::CppFunction is accepted here; typically you provide a function pointer or lambda. c10::OperatorName _resolve(const char *name) const# template\u003ctypename Name, typename Func\u003einline Library \u0026impl_UNBOXED(Name, Func*) \u0026# inline Library \u0026def(detail::SelectiveStr\u003cfalse\u003e, const std::vector\u003cat::Tag\u003e \u0026tags[[maybe_unused]] = {}) \u0026# inline Library \u0026def(detail::SelectiveStr\u003ctrue\u003e raw_schema, const std::vector\u003cat::Tag\u003e \u0026tags = {}) \u0026# template\u003ctypename Func\u003einline Library \u0026def(detail::SelectiveStr\u003cfalse\u003e, Func\u0026\u0026, const std::vector\u003cat::Tag\u003e \u0026tags[[maybe_unused]] = {}) \u0026# template\u003ctypename Func\u003einline Library \u0026def(detail::SelectiveStr\u003ctrue\u003e raw_name_or_schema, Func \u0026\u0026raw_f, const std::vector\u003cat::Tag\u003e \u0026tags = {}) \u0026# template\u003ctypename Func\u003einline Library \u0026impl(detail::SelectiveStr\u003cfalse\u003e, Func\u0026\u0026) \u0026# template\u003ctypename Dispatch, typename Func\u003einline Library \u0026impl(detail::SelectiveStr\u003cfalse\u003e, Dispatch\u0026\u0026, Func\u0026\u0026) \u0026# template\u003ctypename Func\u003einline Library \u0026impl_UNBOXED(detail::SelectiveStr\u003cfalse\u003e, Func*) \u0026# template\u003ctypename Func\u003einline Library \u0026impl(detail::SelectiveStr\u003ctrue\u003e name, Func \u0026\u0026raw_f) \u0026# template\u003ctypename Dispatch, typename Func\u003einline Library \u0026impl(detail::SelectiveStr\u003ctrue\u003e name, Dispatch \u0026\u0026key, Func \u0026\u0026raw_f) \u0026# template\u003ctypename Func\u003einline Library \u0026impl_UNBOXED(detail::SelectiveStr\u003ctrue\u003e, Func*) \u0026# template\u003ctypename Func\u003einline Library \u0026fallback(Func \u0026\u0026raw_f) \u0026# Register a fallback implementation for all operators which will be used if there is not a specific implementation for an operator available. There MUST be a DispatchKey associated with a fallback; e.g., only call this from TORCH_LIBRARY_IMPL() with namespace _. // Example: TORCH_LIBRARY_IMPL(_, AutogradXLA, m) { // If there is not a kernel explicitly registered // for AutogradXLA, fallthrough to the next // available kernel m.fallback(torch::CppFunction::makeFallthrough()); } // See aten/src/ATen/core/dispatch/backend_fallback_test.cpp // for a full example of boxed fallback Parameters: raw_f \u2013 The function that implements the fallback. Unboxed functions typically do not work as fallback functions, as fallback functions must work for every operator (even though they have varying type signatures). Typical arguments are CppFunction::makeFallthrough() or CppFunction::makeFromBoxedFunction() template\u003cclass CurClass\u003einline torch::class_\u003cCurClass\u003e class_(const std::string \u0026className)# template\u003cclass CurClass\u003einline torch::class_\u003cCurClass\u003e class_(detail::SelectiveStr\u003ctrue\u003e className)# template\u003cclass CurClass\u003einline detail::ClassNotSelected class_(detail::SelectiveStr\u003cfalse\u003e className)# void reset()# template\u003cclass CurClass\u003einline class_\u003cCurClass\u003e class_(const std::string \u0026className)# template\u003cclass CurClass\u003einline class_\u003cCurClass\u003e class_(detail::SelectiveStr\u003ctrue\u003e className)# Friends friend class detail::TorchLibraryInit Example: TORCH_LIBRARY(myops, m) { // Define with implementation m.def(\"add(Tensor self, Tensor other) -\u003e Tensor\", \u0026add_impl); // Define schema only m.def(\"mul(Tensor self, Tensor other) -\u003e Tensor\"); // Provide backend-specific implementations m.impl(\"mul\", torch::kCPU, \u0026mul_cpu_impl); m.impl(\"mul\", torch::kCUDA, \u0026mul_cuda_impl); } CppFunction# class CppFunction Represents a C++ function that implements an operator. Most users won\u2019t interact directly with this class, except via error messages: the constructors this function define the set of permissible \u201cfunction\u201d-like things you can bind via the interface. This class erases the type of the passed in function, but durably records the type via an inferred schema for the function. Public Functions template\u003ctypename Func\u003einline explicit CppFunction(Func *f, std::enable_if_t\u003cc10::guts::is_function_type\u003cFunc\u003e::value, std::nullptr_t\u003e = nullptr) This overload accepts function pointers, e.g., CppFunction(\u0026add_impl) template\u003ctypename FuncPtr\u003einline explicit CppFunction(FuncPtr f, std::enable_if_t\u003cc10::is_compile_time_function_pointer\u003cFuncPtr\u003e::value, std::nullptr_t\u003e = nullptr) This overload accepts compile time function pointers, e.g., CppFunction(TORCH_FN(add_impl)) template\u003ctypename Lambda\u003einline explicit CppFunction(Lambda \u0026\u0026f, std::enable_if_t\u003cc10::guts::is_functor\u003cstd::decay_t\u003cLambda\u003e\u003e::value, std::nullptr_t\u003e = nullptr) This overload accepts lambdas, e.g., CppFunction([](const Tensor\u0026 self) { ... }) ~CppFunction() CppFunction(const CppFunction\u0026) = delete CppFunction \u0026operator=(const CppFunction\u0026) = delete CppFunction(CppFunction\u0026\u0026) noexcept = default CppFunction \u0026operator=(CppFunction\u0026\u0026) = default inline CppFunction \u0026\u0026debug(std::string d) \u0026\u0026 Public Static Functions static inline CppFunction makeFallthrough() This creates a fallthrough function. Fallthrough functions immediately redispatch to the next available dispatch key, but are implemented more efficiently than a hand written function done in the same way. template\u003cc10::BoxedKernel::BoxedKernelFunction *func\u003estatic inline CppFunction makeFromBoxedFunction() Create a function from a boxed kernel function with signature void(const OperatorHandle\u0026, Stack*); i.e., they receive a stack of arguments in a boxed calling convention, rather than in the native C++ calling convention. Boxed functions are typically only used to register backend fallbacks via torch::Library::fallback(). template\u003cc10::BoxedKernel::BoxedKernelFunction_withDispatchKeys *func\u003estatic inline CppFunction makeFromBoxedFunction() template\u003cclass KernelFunctor\u003estatic inline CppFunction makeFromBoxedFunctor(std::unique_ptr\u003cKernelFunctor\u003e kernelFunctor) Create a function from a boxed kernel functor which defines operator()(const OperatorHandle\u0026, DispatchKeySet, Stack*) (receiving arguments from boxed calling convention) and inherits from c10::OperatorKernel. Unlike makeFromBoxedFunction, functions registered in this way can also carry additional state which is managed by the functor; this is useful if you\u2019re writing an adapter to some other implementation, e.g., a Python callable, which is dynamically associated with the registered kernel. template\u003ctypename FuncPtr, std::enable_if_t\u003cc10::guts::is_function_type\u003cFuncPtr\u003e::value, std::nullptr_t\u003e = nullptr\u003estatic inline CppFunction makeFromUnboxedFunction(FuncPtr *f) Create a function from an unboxed kernel function. This is typically used to register common operators. template\u003ctypename FuncPtr, std::enable_if_t\u003cc10::is_compile_time_function_pointer\u003cFuncPtr\u003e::value, std::nullptr_t\u003e = nullptr\u003estatic inline CppFunction makeFromUnboxedFunction(FuncPtr f) Create a function from a compile time unboxed kernel function pointer. This is typically used to register common operators. Compile time function pointers can be used to allow the compiler to optimize (e.g. inline) calls to it. OrderedDict# template\u003ctypename Key, typename Value\u003eclass OrderedDict# An ordered dictionary implementation, akin to Python\u2019s OrderedDict. Public Types using Iterator = typename std::vector\u003cItem\u003e::iterator# using ConstIterator = typename std::vector\u003cItem\u003e::const_iterator# Public Functions explicit OrderedDict(std::string key_description = \"Key\")# Constructs the OrderedDict with a short description of the kinds of keys stored in the OrderedDict. This description is used in error messages thrown by the OrderedDict. OrderedDict(const OrderedDict \u0026other)# Copy constructs this OrderedDict from other. OrderedDict \u0026operator=(const OrderedDict \u0026other)# Assigns items from other to this OrderedDict. OrderedDict(OrderedDict \u0026\u0026other) noexcept = default# OrderedDict \u0026operator=(OrderedDict \u0026\u0026other) noexcept = default# ~OrderedDict() = default# OrderedDict(std::initializer_list\u003cItem\u003e initializer_list)# Constructs a new OrderedDict and pre-populates it with the given Items. const std::string \u0026key_description() const noexcept# Returns the key description string the OrderedDict was constructed with. Item \u0026front()# Returns the very first item in the OrderedDict and throws an exception if it is empty. const Item \u0026front() const# Returns the very first item in the OrderedDict and throws an exception if it is empty. Item \u0026back()# Returns the very last item in the OrderedDict and throws an exception if it is empty. const Item \u0026back() const# Returns the very last item in the OrderedDict and throws an exception if it is empty. Item \u0026operator[](size_t index)# Returns the item at the index-th position in the OrderedDict. Throws an exception if the index is out of bounds. const Item \u0026operator[](size_t index) const# Returns the item at the index-th position in the OrderedDict. Throws an exception if the index is out of bounds. Value \u0026operator[](const Key \u0026key)# Returns the value associated with the given key. Throws an exception if no such key is stored in the OrderedDict. Use find() for a non-throwing way of accessing a value if it is present. const Value \u0026operator[](const Key \u0026key) const# Returns the value associated with the given key. Throws an exception if no such key is stored in the OrderedDict. Use find() for a non-throwing way of accessing a value if it is present. Value *find(const Key \u0026key) noexcept# Returns a pointer to the value associated with the given key, or a nullptr if no such key is stored in the OrderedDict. const Value *find(const Key \u0026key) const noexcept# Returns a pointer to the value associated with the given key, or a nullptr if no such key is stored in the OrderedDict. bool contains(const Key \u0026key) const noexcept# Returns true if the key is present in the OrderedDict. Iterator begin()# Returns an iterator to the first item in the OrderedDict. Iteration is ordered. ConstIterator begin() const# Returns an iterator to the first item in the OrderedDict. Iteration is ordered. Iterator end()# Returns an iterator one past the last item in the OrderedDict. ConstIterator end() const# Returns an iterator one past the last item in the OrderedDict. size_t size() const noexcept# Returns the number of items currently stored in the OrderedDict. bool is_empty() const noexcept# Returns true if the OrderedDict contains no elements. void reserve(size_t requested_capacity)# Resizes internal storage to fit at least requested_capacity items without requiring reallocation. template\u003ctypename K, typename V\u003eValue \u0026insert(K \u0026\u0026key, V \u0026\u0026value)# Inserts a new (key, value) pair into the OrderedDict. Throws an exception if the key is already present. If insertion is successful, immediately returns a reference to the inserted value. Value \u0026insert(Key key, Value \u0026\u0026value)# Inserts a new (key, value) pair into the OrderedDict. Throws an exception if the key is already present. If insertion is successful, immediately returns a reference to the inserted value. void update(OrderedDict \u0026\u0026other)# Inserts all items from other into this OrderedDict. If any key from other is already present in this OrderedDict, an exception is thrown. void update(const OrderedDict \u0026other)# Inserts all items from other into this OrderedDict. If any key from other is already present in this OrderedDict, an exception is thrown. void erase(const Key \u0026key)# Removes the item that has key from this OrderedDict if exists and if it doesn\u2019t an exception is thrown. void clear()# Removes all items from this OrderedDict. const std::vector\u003cItem\u003e \u0026items() const noexcept# Returns the items stored in the OrderedDict. ::std::vector\u003cKey\u003e keys() const# Returns a newly allocated vector and copies all keys from this OrderedDict into the vector. ::std::vector\u003cValue\u003e values() const# Returns a newly allocated vector and copies all values from this OrderedDict into the vector. ::std::vector\u003cstd::pair\u003cKey, Value\u003e\u003e pairs() const# Returns a newly allocated vector and copies all keys and values from this OrderedDict into a vector of std::pair\u003cKey, Value\u003e. Friends template\u003ctypename K, typename V\u003efriend bool operator==(const OrderedDict\u003cK, V\u003e \u0026a, const OrderedDict\u003cK, V\u003e \u0026b)# Returns true if both dicts contain the same keys and values, in the same order. class Item# Public Functions inline Item(Key key, Value value)# Constructs a new item. inline Value \u0026operator*()# Returns a reference to the value. inline const Value \u0026operator*() const# Returns a reference to the value. inline Value *operator-\u003e()# Allows access to the value using the arrow operator. inline const Value *operator-\u003e() const# Allows access to the value using the arrow operator. inline const Key \u0026key() const noexcept# Returns a reference to the key. inline Value \u0026value() noexcept# Returns a reference to the value. inline const Value \u0026value() const noexcept# Returns a reference to the value. inline const std::pair\u003cKey, Value\u003e \u0026pair() const noexcept# Returns a (key, value) pair. Functions# The library API provides builder methods on the Library class for registering operators. See the Library class documentation above for the full API including def(), impl(), and fallback() methods. Dispatch Keys# Common dispatch keys used with torch::dispatch(): torch::kCPU - CPU backend torch::kCUDA - CUDA backend torch::kAutograd - Autograd backend torch::kMeta - Meta tensor backend",
"author": {
"@type": "Organization",
"name": "PyTorch Contributors",
"url": "https://pytorch.org"
},
"image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "/api/library/registration.html"
},
"datePublished": "2023-01-01T00:00:00Z",
"dateModified": "2023-01-01T00:00:00Z"
}
| docsearch:language | en |
| llm:site-type | documentation |
| llm:framework | PyTorch |
| llm:description | Operator registration in PyTorch C++ — TORCH_LIBRARY, TORCH_LIBRARY_IMPL for custom operators. |
| llm:navigation-file | https://pytorch.org/docs/stable/llms.txt |
| llm:sitemap | https://pytorch.org/docs/stable/sitemap.xml |
| llm:version | main |
| llm:project | PyTorch |
| llm:page-type | documentation |
| og:image | https://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png |
| None | 3 |
Links:
Viewport: width=device-width, initial-scale=1