pytorch/pytorch
PyTorch is a layered system: the c10 core library and ATen tensor library (C++), the autograd engine in torch/csrc/autograd, and the Python frontend package `torch` bound through torch._C (torch_python). Build is PEP 517 via scikit-build-core: pyproject.toml declares the backend, CMakeLists.txt is the native build entry point, and cmake/Codegen.cmake runs torchgen over native_functions.yaml to emit ATen headers/sources plus autograd and Python binding sources. Operator dispatch crosses the Python/C++ boundary through OpOverloadPacket/OpOverload in torch/_ops.py into the C++ dispatcher. CI is a large GitHub Actions matrix of reusable _binary-build/_binary-test workflows gated by a job-filter workflow; test selection uses target determination. Packaging installs CMake artifacts under the `torch` wheel directory with an extensive exclusion list to keep the wheel lean.
- The build backend is scikit-build-core (build-backend = "scikit_build_core.build"); PEP 517 builds run CMake, and requires-python is >=3.10.
- setup.py is a deprecated shim: `install`/`develop` forward to `pip install --no-build-isolation`, all other commands fail, and the file is scheduled for removal in PyTorch 2.18.
- CMakeLists.txt is the native build entry point driven by scikit-build-core; its header comment documents the ordered stages: EnvVarForwarding, PreBuildSteps (git submodules + pinned NCCL fetch), Dependencies.cmake, codegen via cmake/Codegen.cmake (torchgen over native_functions.yaml) and tools/setup_helpers/generate_code.py, then add_subdirectory of torch/headeronly, c10 and caffe2.
- `import torch` loads native deps before Python bindings: _load_global_deps() ctypes.CDLLs libtorch_global_deps.so with RTLD_GLOBAL, inspects /proc/self/maps for libcudart, then _preload_cuda_deps preloads cublasLt/cublas/cudnn/nvrtc/cudart/cupti/nccl etc. from site-packages before `from torch._C import *`.
- On Windows, _load_dll_libraries() adds DLL directories, preloads vcruntime/msvcp DLLs, and LoadLibraryExW-loads every *.dll under torch/lib before importing torch._C.
- torchgen/gen.py main() is the codegen CLI invoked at build time: it parses aten/src/ATen/native/native_functions.yaml and native/tags.yaml and emits ATen headers/sources, dispatcher registrations, and the AOTInductor C shim, with flags like --rocm/--mps/--xpu/--mtia and --per-operator-headers.
- The wheel ships packages torch, torchgen and functorch, with CMake artifacts installed into install-dir "torch"; a long exclude list strips static archives (*.a), C++ sources, third-party cmake configs, docs and dev files from the wheel.
- Version and runtime dependencies are dynamic metadata: tools/metadata version provider resolves PYTORCH_BUILD_VERSION/PKG-INFO/version.txt, and the dependencies provider injects CUDA/ROCm/XPU pip deps from PYTORCH_EXTRA_INSTALL_REQUIREMENTS.
- Console scripts are torchrun (-> torch.distributed.run:main) and torchfrtrace (-> flight recorder trace); the torch.distributed.backends entry-point group registers mpi/gloo/nccl/ucc/xccl/nccl4py backend factories.
- The Python op surface is assembled at import: _C._initExtension(torch_shm_manager path) runs first, then every callable in _C._VariableFunctions is copied into torch's globals (appending to __all__), followed by submodule imports (cpu, cuda, xpu, mps, mtia, distributed, nn, optim, fx-related) and _C._init_names.
- Native dependencies are vendored as git submodules under third_party/ (pybind11, googletest, protobuf, gloo, NNPACK, pthreadpool, benchmark, and more).
- CI is GitHub Actions: trunk.yml runs on pushes to main/release/*/landchecks plus a nightly cron, fronted by job-filter.yml and target_determination.yml (with llm_td_retrieval) that gate which reusable _binary-build/_binary-test workflows execute; pull.yml does the same for PRs.
- Editable installs use scikit-build-core redirect mode: Python files resolve from the source tree, CMake artifacts from the build tree, and rebuild-on-import is off by default (opt-in via SKBUILD_EDITABLE_REBUILD).