Skip to content

Latest commit

 

History

History
 
 

README.md

Generated C++ code from the PyTorch C++ docs

This tree is produced by codegen/generate.py from the documentation in this repository (_sources/**, llms-full.txt, _coverage/**). It mirrors the documented PyTorch C++ API as a buildable codebase.

Provenance: this repository is auto-generated from pytorch/pytorch docs/cpp/source. Doc content changes belong upstream; only the generator and generated layout are maintained here.

Layout

  • include/ — API-surface stub headers mirroring the real LibTorch header paths recorded under _coverage/ (169 stubs generated).
  • include/torch/**/*.hpp — header-only template stubs (nn::Module, data::Dataset, OrderedDict).
  • src/ — non-template reference implementations compiled into the generated_stubs static library.
  • examples/<module>/ — one runnable example per documentation page (52 examples). Each example has a single main() and doubles as a compile-test / smoke test.
  • coverage_report.md — diff of the generated include/ tree against the 166 documented headers in _coverage/.

File-type conventions

Extension Role
.h Public API surface stubs, umbrella headers, stable-ABI / C-compatible interface
.hpp Template-heavy, header-only implementations (nn/data templates, options)
.cpp Non-template implementations and every compilable example program
.cu CUDA-only kernels/examples, built only when a CUDA compiler is found

.c and .hh files are deliberately not generated: neither extension is used anywhere in the documented PyTorch C++ API.

Build

./validate.sh /path/to/libtorch        # or: cmake -S . -B build -DCMAKE_PREFIX_PATH=...
cmake --build build -j$(nproc)

The build follows the documented LibTorch CMake pattern (find_package(Torch REQUIRED), ${TORCH_CXX_FLAGS}, C++20, MSVC DLL copy). CUDA .cu targets are only configured when check_language(CUDA) finds NVCC.

Testing

Every example is registered with CTest and runs as a smoke test once the tree is built against LibTorch:

./validate.sh /path/to/libtorch
ctest --test-dir build --output-on-failure -j$(nproc)

Examples that exercise APIs missing from the installed LibTorch are skipped at configure time via feature checks: stable/* needs torch::stable ops (PyTorch ≥ 2.8), cuda/* needs CUDA-enabled headers, and xpu/* needs XPU-enabled headers. The CI matrix uses the LibTorch 2.7.1 wheels, where those three groups are skipped.

Generator unit tests (stdlib unittest, no LibTorch needed):

python3 -m unittest codegen.test_generate -v   # from the repo root

Regenerating

python3 ../codegen/generate.py

Curated examples (hand-written, doc-derived) are preserved on re-run; the generator only fills gaps for pages without a curated example. The generator is idempotent: re-running it never changes the checked-in tree, a property the CI workflows verify.

CI / releases

  • .github/workflows/ci.yml — codegen unit tests, a regeneration idempotency check, a skeleton configure without LibTorch, LibTorch builds of every example on Linux/Windows/macOS followed by the CTest smoke suite, and end-to-end runs of the curated examples/minimal and examples/mnist tutorial apps.
  • .github/workflows/release.yml — tag pushes (v*) rerun the full build + test matrix and publish a GitHub release with the packaged generated tree and per-platform example binaries.