# Extension points — pytorch/pytorch

> Canonical page: https://codevetter.com/unpack/pytorch/extension-points

# Extension points — pytorch/pytorch

Scanned commit 5711f1a4586cb0a3dbb3d67538df814db48f94f4 (2026-09-22). Generated by codevetter codevetter 1.12.0 on 2026-09-22.

PyTorch is built around registries. Operators extend via native_functions.yaml + torchgen (in-tree) or torch.library (Library.define/impl, custom_op, register_fake, register_autograd) and appear lazily under torch.ops.<ns>. Compiler backends plug into torch.compile through the torch_dynamo_backends entry-point group or @register_backend. Distributed backends register via the torch.distributed.backends entry-point group. Device vendors use _register_device_module/Privateuse1. Interop is through __torch_function__ (overrides.py) and __torch_dispatch__ modes (_python_dispatch.py). Serialization accepts custom location tags via register_package, and C++ extensions JIT-build via cpp_extension.
- In-tree op extension: declare `func:` schema + per-backend `dispatch:` entries in native_functions.yaml, implement the kernel in aten/src/ATen/native/*.cpp, and torchgen generates C++/Python bindings — the documented 'modern mechanism for adding operators'. — [aten/src/ATen/native/README.md#L1-27](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/aten/src/ATen/native/README.md#L1-L27) [aten/src/ATen/native/native_functions.yaml](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/aten/src/ATen/native/native_functions.yaml)
- torch.library.Library(ns, kind in DEF/IMPL/FRAGMENT, dispatch_key) registers new operator schemas (define()) or overrides kernels for a specific dispatch key (impl()); reserved namespaces are rejected and registration is finalized via weakref cleanup. — [torch/library.py#L212-328](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/library.py#L212-L328)
- Higher-level Python op APIs exist alongside Library: torch.library.custom_op, register_fake (meta/fake kernels for compile), register_autograd, and impl_abstract — the supported path for making custom ops work under torch.compile. — [torch/library.py#L978-1322](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/library.py#L978-L1322)
- torch.ops namespaces are lazily materialized: _OpNamespace.__getattr__ creates OpOverloadPackets on demand, and each packet resolves overloads via torch._C._get_operation_overload — so newly registered ops are reachable as torch.ops.ns.op without code changes. — [torch/_ops.py#L1284-1331](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/_ops.py#L1284-L1331) [torch/_ops.py#L1417-1456](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/_ops.py#L1417-L1456)
- Compile backends plug in two ways: the @register_backend decorator populating _BACKENDS/_COMPILER_FNS/_BACKEND_TAGS, or external packages exposing entry points in the 'torch_dynamo_backends' group discoverable by list_backends(). — [torch/_dynamo/backends/registry.py#L1-120](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/_dynamo/backends/registry.py#L1-L120)
- Distributed backends are entry-point plugins: pyproject registers mpi/gloo/nccl/nccl-legacy/nccl2/nccl-lazy/ucc/xccl/nccl4py factories under group 'torch.distributed.backends', and init_process_group accepts 'third-party plugin' backend strings and device-qualified specs like 'cpu:gloo,cuda:custom_backend'. — [pyproject.toml#L390-399](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/pyproject.toml#L390-L399) [torch/distributed/distributed_c10d.py#L2397-2417](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/distributed/distributed_c10d.py#L2397-L2417) [torch/distributed/distributed_c10d.py#L2467-2472](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/distributed/distributed_c10d.py#L2467-L2472)
- Out-of-tree device runtimes use torch._register_device_module(device_type, module) which installs the module as torch.<device_type> in sys.modules and the torch namespace. — [torch/__init__.py#L3341-3358](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/__init__.py#L3341-L3358)
- __torch_function__ overrides are handled in torch/overrides.py (with a non-overridable allowlist and performance warnings about changing this file); __torch_dispatch__ modes live in torch/utils/_python_dispatch.py. — [torch/overrides.py#L1-18](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/overrides.py#L1-L18) [torch/utils/_python_dispatch.py](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/utils/_python_dispatch.py)
- Serialization is extensible: user extensions register custom location tags plus tagging/deserialization methods via torch.serialization.register_package, and the safe-globals API (add_safe_globals/safe_globals) extends what weights_only load accepts. — [torch/serialization.py#L365-366](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/serialization.py#L365-L366) [torch/serialization.py#L283-341](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/serialization.py#L283-L341)
- JIT C++/CUDA extension path: torch.utils.cpp_extension.load()/load_inline() write ninja files, compile objects and produce loadable extension modules — the out-of-tree kernel authoring route. — [torch/utils/cpp_extension.py#L1880-2162](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/utils/cpp_extension.py#L1880-L2162) [torch/utils/cpp_extension.py#L2374-2559](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/utils/cpp_extension.py#L2374-L2559)
- Inductor's compile_fx accepts a decompositions dict (else select_decomp_table) and config_patches (with per-device '<device>.<key>' routing), making decomposition and config the knob surface for backend behavior. — [torch/_inductor/compile_fx.py#L3117-3167](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torch/_inductor/compile_fx.py#L3117-L3167)
- AOTInductor C shim is extensible: torchgen's --extend-aoti-c-shim generates c shims for out-of-tree ATen ops from an out-of-tree native_functions.yaml into torch/csrc/inductor/aoti_torch/generated/extend. — [torchgen/gen.py#L2999-3008](https://github.com/pytorch/pytorch/blob/5711f1a4586cb0a3dbb3d67538df814db48f94f4/torchgen/gen.py#L2999-L3008)

Every claim links to its cited files at the pinned commit. Citations identify source evidence; they do not establish runtime correctness. Independent CodeVetter scan — not reviewed by upstream maintainers.

## Public product links

- [CodeVetter](https://codevetter.com/)
- [Download](https://codevetter.com/download)
- [Documentation](https://codevetter.com/docs/)
- [Source](https://github.com/Codevetter/codevetter)
