Shortcuts

Class Library

Page Contents

Class Documentation

class torch::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&) = delete
Library &operator=(const Library&) = delete
Library(Library&&) = default
Library &operator=(Library&&) = default
template<typename Schema>
Library &def(Schema &&raw_schema) &

Declare an operator with a schema, but don’t provide any implementations for it.

You’re 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) -> Tensor");
}
Parameters
  • raw_schema: 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.

template<typename NameOrSchema, typename Func>
Library &def(NameOrSchema &&raw_name_or_schema, Func &&raw_f) &

Define an operator for a schema and then register an implementation for it.

This is typically what you would use if you aren’t planning on making use of the dispatcher to structure your operator implementation. It’s 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: 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: 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<typename Name, typename Func>
Library &impl(Name name, Func &&raw_f) &

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: The name of the operator to implement. Do NOT provide schema here.

  • raw_f: 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<typename Name, typename Func>
Library &impl_UNBOXED(Name name, Func *raw_f) &
Library &def(detail::SelectiveStr<false>) &
Library &def(detail::SelectiveStr<true> raw_schema) &
template<typename Func>
Library &def(detail::SelectiveStr<false>, Func &&raw_f) &
template<typename Func>
Library &def(detail::SelectiveStr<true> raw_name_or_schema, Func &&raw_f) &
template<typename Func>
Library &impl(detail::SelectiveStr<false>, Func &&raw_f) &
template<typename Dispatch, typename Func>
Library &impl(detail::SelectiveStr<false>, Dispatch &&key, Func &&raw_f) &
template<typename Func>
Library &impl_UNBOXED(detail::SelectiveStr<false> name, Func *raw_f) &
template<typename Func>
Library &impl(detail::SelectiveStr<true> name, Func &&raw_f) &
template<typename Dispatch, typename Func>
Library &impl(detail::SelectiveStr<true> name, Dispatch &&key, Func &&raw_f) &
template<typename Func>
Library &impl_UNBOXED(detail::SelectiveStr<true> name, Func *raw_f) &
template<typename Func>
Library &fallback(Func &&raw_f) &

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

template<class CurClass>
torch::class_<CurClass> class_(const std::string &className)
template<class CurClass>
class_<CurClass> class_(const std::string &className)

Friends

friend class detail::TorchLibraryInit

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources