You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Version: 2.1.0 (ships with torch 2.1.0) Type: Pure Python SPM target: Bundled in the Python framework (top-level package alongside torch) Auto-included by: torch (build-time tooling — packaged into the wheel) Total Python modules: 62
PyTorch's code-generation toolchain. Used at upstream torch BUILD time to generate C++ operator dispatch tables, autograd bindings, lazy-tensor IR, and ATen kernel registrations from native_functions.yaml. NOT used at runtime — the generated .cpp files are already baked into the iOS torch binaries. It ships in the bundle because it's part of the torch wheel layout and writing a custom out-of-tree op may eventually need it.
If your IDE flags torchgen as unused: it's correct. Safe to ignore unless you're authoring custom ops.
Note: Despite the name suggesting it lives under torch/, on disk torchgen/ is a separate top-level package at app_packages/site-packages/torchgen/. Import as from torchgen import ..., NOT from torch.torchgen import ....
Modules
Top-level
Module
What it does
torchgen.__init__
Empty package marker — see module docstring for BC disclaimer
torchgen.gen
Main entry point — generates ATen operator dispatch tables from YAML
torchgen.gen_aoti_c_shim
Generates Ahead-of-Time Inductor C shim wrappers
torchgen.gen_backend_stubs
Generates dispatch stubs for out-of-tree backends
torchgen.gen_functionalization_type
Generates functionalization pass kernels
torchgen.gen_lazy_tensor
Generates lazy-tensor IR nodes + lowerings
torchgen.gen_schema_utils
Helpers for parsing JIT schema strings
torchgen.gen_vmap_plumbing
Generates vmap (vectorizing-map) plumbing
torchgen.model
Core data classes — NativeFunction, Argument, Return, DispatchKey, BackendIndex
torchgen.context
Codegen-context tracking (current op being generated, error reporting)
torchgen.local
Thread-local codegen state
torchgen.code_template
Jinja-like $variable template engine for emitting C++
torchgen.native_function_generation
Auto-derives composite kernels from primitives
torchgen.yaml_utils
YAML loader with custom constructors for torch types
Runtime no-op. Nothing in torchgen is loaded by import torch at app launch. It's static tooling.
No native deps. Pure Python; no .so, no Cython. Safe on any iOS device.
Don't run gen.py on device. It writes .cpp and .h files — the iOS app sandbox can write inside Documents, but you have no C++ compiler to consume the output.
Standalone example
You almost never invoke torchgen directly. The one realistic use is inspecting the parsed operator model:
fromtorchgen.modelimportNativeFunction, Locationfromtorchgen.yaml_utilsimportYamlLoaderimportyaml, importlib.resourcesasir# Load the bundled native_functions.yamlnf_path=ir.files("torchgen") /"packaged/ATen/native/native_functions.yaml"docs=yaml.load(nf_path.read_text(), Loader=YamlLoader)
print(f"{len(docs)} native functions defined in ATen")
print("First:", docs[0]["func"])
For everything else, prefer the runtime APIs in torch.*.
See also
docs/pytorch.md — the runtime torch package this generator targets