Developer Tools

PyTorch patches export bug for subgraph tensor constants in invoke_subgraph

Nested inline tensor constants were breaking export, verification, and ONNX pipelines.

Deep Dive

PyTorch's latest commit resolves a thorny export issue introduced when nested_compile_region captures an inline tensor constant and replays it. During export promotion to repeated_subgraph0, the constant surfaces as a buffer named repeated_subgraph0._tensor_constant0 owned by the subgraph submodule — not the top-level state_dict. That shape broke the verifier, named_buffers, decomposition, and the ONNX dynamo exporter.

The patch touches five key modules: _export/verifier.py and export/exported_program.py now recognise the subgraph-scoped buffer shape and resolve it through the right state container. _subclasses/functional_tensor.py skips view-replay sync for lifted constants with no tracker entry (they’re owned by the inner subgraph tracer). _higher_order_ops/invoke_subgraph.py preserves kwargs through the non-strict export wrapper and reuses the wrapped GraphModule when its captured buffers are FunctionalTensors. Finally, onnx/_internal/exporter/_core.py materializes nested tensor-constant get_attr nodes as ONNX initializers so the exported model is fully self-contained.

Key Points
  • Fixes export failures when nested_compile_region lifts inline tensor constants into subgraph buffers.
  • Updates 5 core files: verifier, program, functional tensor, invoke_subgraph, and ONNX exporter.
  • ONNX exports now materialise nested tensor-constant nodes as initializers for self-contained models.

Why It Matters

Ensures reliable export of complex PyTorch models with graph breaks to ONNX and other formats.