Skip to content

[PyTorch] Centralize TE↔torch binary boundary in a single facade file - #21

Open
pggPL wants to merge 4 commits into
mainfrom
torch_facade
Open

[PyTorch] Centralize TE↔torch binary boundary in a single facade file#21
pggPL wants to merge 4 commits into
mainfrom
torch_facade

Conversation

@pggPL

@pggPL pggPL commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Description

Centralize the entire binary boundary between Transformer Engine and PyTorch in
the pytorch extension into a single facade file, so that:

  1. switching the concrete tensor type from classic at::Tensor to
    torch::stable::Tensor (LibTorch stable ABI) becomes a one-line compile-flag
    change in one place instead of editing ~40 files, and
  2. there is a lint-enforced guarantee that the only TE↔torch binary
    communication happens through that file.

This is the opposite direction to the dispersed stable-ABI migration: instead of
spreading the stable ABI across every file, it collapses the TE↔torch ABI surface
to one auditable file.

Type of change

  • Code refactoring
  • Infra/Build change

Changes

  • csrc/torch_backend.{h,cpp} — the facade: the only place allowed to include
    libtorch/ATen/c10 headers or name at::/c10::/c10d::/torch:: symbols. It
    re-exports torch as using aliases (Tensor, ScalarType, Device, Stream,
    ProcessGroup, PhiloxCudaState, ...), dtype/device constants, and factory/op
    wrappers (via using-declarations with identical semantics), with a compile
    switch (-DTE_WITH_STABLE_ABI) toward torch::stable::Tensor.
  • All of pytorch/csrc (quantizer, type_converters, extensions.h, pybind.h,
    every extensions/*.cpp incl. multi_tensor/*, common.{h,cpp}) migrated to the
    facade spellings; tensor method calls unchanged.
  • qa/L0_pytorch_lint/check_torch_boundary.py + torch_boundary_allowlist.txt
    — a lint guard (wired into L0_pytorch_lint/test.sh) that fails if any file
    other than the facade touches the torch ABI. The allowlist is empty: nothing is
    grandfathered.

Verification

  • pip install -e . on an RTX-Ada te_pytorch container compiles with 0 errors.
  • The transformer_engine_torch extension loads (170 symbols) and a te.LayerNorm
    GPU forward runs correctly.
  • Boundary guard passes with 0 pending files.

Checklist:

  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

🤖 Generated with Claude Code

pggPL and others added 4 commits July 17, 2026 15:58
Introduce a single facade file that is the only place in the PyTorch
extension allowed to include libtorch/ATen/c10 headers or name at::/c10::/
torch:: symbols. It exposes torch types as `using` aliases (Tensor, Device,
ScalarType, Stream, ProcessGroup, PhiloxCudaState, ...) and torch ops as free
functions (GetATenDType, GetTransformerEngineDType, new_cuda_tensor,
getCurrentCUDAStream), with a compile switch (-DTE_WITH_STABLE_ABI) toward
torch::stable::Tensor.

- torch_backend.h / torch_backend.cpp: the facade (usings + methods).
- common.h / common.cpp: migrated to route through the facade (torch-token-free).
- qa/L0_pytorch_lint/check_torch_boundary.py + torch_boundary_allowlist.txt:
  lint guard enforcing that only the facade touches the torch ABI; unmigrated
  files are grandfathered via an allowlist that only shrinks.
- wired the guard into qa/L0_pytorch_lint/test.sh.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Migrate every file under transformer_engine/pytorch/csrc to talk to the
PyTorch ABI only through the torch_backend.h facade: no file other than
torch_backend.{h,cpp} names at::/c10::/c10d::/torch:: symbols or includes
libtorch/ATen/c10 headers anymore.

- Expanded torch_backend.h with the full re-export vocabulary (type aliases,
  dtype/device constants, factory/op re-exports via using-declarations with
  identical semantics, CUDA helpers, c10d collective option structs, and a
  torch::indexing alias).
- Migrated all 34 remaining csrc files (quantizer, type_converters, extensions.h,
  pybind.h, and every extensions/*.cpp incl. multi_tensor/*) to the facade
  spellings; tensor method calls and non-torch code unchanged.
- Emptied qa/L0_pytorch_lint/torch_boundary_allowlist.txt: nothing is
  grandfathered; the boundary guard now passes with 0 pending files.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Follow-up fixes surfaced by building the pytorch extension:

- common.h: std::to_string(ArrayRef<T>) lives in namespace std, so the
  ArrayRef alias must be fully qualified (transformer_engine::pytorch::ArrayRef).
- torch_backend.h: drop the bare at::device()/at::dtype() re-exports -- their
  names collide with the ubiquitous `device`/`dtype` locals/params. Call sites
  now build options explicitly: TensorOptions().device(...).dtype(...).
- Convert all free device()/dtype() option-builder calls accordingly
  (allocate, cast, comm_gemm_overlap, nvshmem_comm, permutation, quantizer, router).
- Qualify facade aliases in the global-namespace CommOverlap* classes in
  extensions.h (they sit outside transformer_engine::pytorch).
- Add `using namespace transformer_engine::pytorch;` to the TUs with
  global/anonymous-namespace code (attention.cpp mha_fill, comm_gemm_overlap.cpp
  method defs, pybind.cpp PYBIND11_MODULE) so the facade names resolve there.

Verified on the workstation (te_pytorch): full `pip install -e .` compiles with
0 errors; the transformer_engine_torch extension loads (170 symbols) and a
te.LayerNorm GPU forward runs correctly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
GetATenDType, GetTransformerEngineDType and new_cuda_tensor sit on the
per-tensor Python<->C++ marshalling path. Define them `inline` in
torch_backend.h instead of out-of-line in torch_backend.cpp so they stay
inlinable at call sites, matching the pre-facade codegen (the dtype maps were
`inline` in common.h before this branch). torch_backend.cpp is now an empty TU.

Verified on the workstation: full `pip install -e .` compiles with 0 errors;
extension loads (170 symbols) and te.LayerNorm GPU forward runs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant