pnnx: support PyTorch ExportedProgram .pt2 models - #6959
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1ac380c8cd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (!value_name.empty()) | ||
| continue; |
There was a problem hiding this comment.
Reject or implement user-input mutations
For an ExportedProgram whose signature contains user_input_mutation, this silently drops the mutation output and still reports successful conversion. Calling the generated model therefore does not update the caller's tensor as the exported module does; either emit the corresponding copy-back operation or reject this output kind, as is already done for unsupported buffer mutations.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cd0dbbcc87
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Summary
pnnx model.pt2support for PyTorchExportedProgramarchivesexport_exported_program()helper for PT2 round-trip export.pt2modelUsage
Export a model with PyTorch:
Convert the exported program with pnnx:
The generated PNNX Python model can also be exported to another PT2 package:
python -c "import model_pnnx; model_pnnx.export_exported_program()"An optional input tuple can be passed to
export_exported_program(example_inputs=None)to override the generated example inputs.Implementation
The importer contains a schema-oriented JSON parser and a restricted pickle reader for tensor payloads. It reconstructs PNNX IR directly from the serialized ExportedProgram graph and graph signature.
The existing store-only ZIP implementation is extended to read central directories, data descriptors, ZIP64 offsets, and large tensor files. Archive entry metadata, bounds, duplicate names, encryption state, and CRC are validated before use. Compressed entries are detected and rejected explicitly without adding a decompression dependency.
Parameters, persistent buffers, non-persistent buffers, and tensor constants are restored as PNNX attributes instead of being exposed as runtime inputs. Tensor dtype, shape, stride, storage offset, byte order, and raw storage data are handled by the importer.
Additional graph-rewrite patterns normalize decomposed ExportedProgram graphs for convolution, pooling, interpolation, normalization, RNN, window, scatter/slice-copy, ChannelShuffle,
torch.tril, and selected torchvision operators.The generated
*_pnnx.pymodel validates and reconstructs supported tuple/list input and output structures and providesexport_exported_program()for a PT2-to-PNNX-to-PT2 round trip.No new third-party dependency is introduced.
Tests
Local environment: Windows, MSVC Release, PyTorch 2.5.1 CPU.
Focused tests:
test_exported_program: passedtest_exported_program_ops: passed, including a dynamic-shape reshape casetest_exported_program_pytree_roundtrip: passed, including nested inputs and outputs and PT2 -> PNNX Python -> PT2 -> pnnx conversiontest_exported_program_invalid: passed, including archive, JSON, schema, PyTree, CRC, compression, and mutation diagnosticsPT2 test matrix:
Compatibility samples were checked with:
A representative ExportedProgram and the generated PNNX Python model produced an exact match:
A total of 245 pnnx-to-ncnn PT2 test variants are registered. Their full execution and the multi-platform/version matrix are pending upstream CI.
Current limitations
Related issue
Related to #6366.
This work was developed as part of the 2026 Tencent Rhino-Bird Open Source Program.