Skip to content

pnnx for torch exported program - #6946

Open
AtomAlpaca wants to merge 49 commits into
Tencent:masterfrom
AtomAlpaca:master
Open

AtomAlpaca wants to merge 49 commits into
Tencent:masterfrom
AtomAlpaca:master

Conversation

@AtomAlpaca

Copy link
Copy Markdown
Contributor

Summary

Add pnnx support for PyTorch torch.export.ExportedProgram models saved with torch.export.save().

ep = torch.export.export(model, (x,))
torch.export.save(ep, "model.pt2")

Convert the exported model with:

pnnx model.pt2

Changes

  • Detect legacy ExportedProgram ZIP and PT2 Archive model formats.
  • Parse PT2 archive metadata and JSON without additional third-party dependencies.
  • Read tensor payloads with a restricted pickle implementation.
  • Reconstruct parameters, buffers, constants and tensor views.
  • Load PT2 graphs into the existing pnnx::Graph.
  • Use libtorch Dispatcher schemas to order and validate operator arguments.
  • Lower static and symbolic SymInt, SymFloat and SymBool values.
  • Evaluate exported shape guards and preserve unresolved assertions as pnnx.Assert.
  • Convert bf16 weights correctly when writing ncnn models.
  • Normalize GRU and LSTM operators for the existing pnnx RNN passes.
  • Reject unsupported formats, schemas, opsets, operators, devices, layouts and symbolic expressions explicitly.
  • Add PT2 conversion tests for existing operators and models.
  • Add archive, JSON, schema, weight, malformed-input, dynamic-shape and generated-model tests.
  • Add PT2 CI coverage on Ubuntu, macOS and Windows.
  • Add a PyTorch 2.6–2.12.1 producer-version test matrix.
  • Document PT2 usage and supported format versions.

Supported formats

  • PyTorch 2.6–2.7 legacy ExportedProgram ZIP
  • PyTorch 2.8–2.12.1 PT2 Archive v0
  • Program schema major 8, minors 2, 7, 8, 14, 15, 17 and 20
  • ATen opset 10

Tests

The complete PT2 suite contains 392 tests with TorchVision enabled:

ctest --output-on-failure -L pt2

The producer-version regression subset contains 383 tests:

ctest --output-on-failure -L pt2-regression

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2c514a6d46

ℹ️ 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".

Comment thread tools/pnnx/src/pt2_weights.cpp
Comment thread tools/pnnx/src/pass_ncnn/convert_half_to_float.cpp

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1180fa0369

ℹ️ 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".

Comment thread tools/pnnx/src/pass_level2/nn_ChannelShuffle.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a8255bd5a0

ℹ️ 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".

Comment thread tools/pnnx/src/pt2_graph_lowering.cpp
Comment thread tools/pnnx/src/pt2_weights.cpp Outdated
Comment thread tools/pnnx/src/pass_level2/torchvision_DeformConv2d.cpp

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1194e99365

ℹ️ 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".

Comment thread .github/workflows/pnnx.yml
Comment thread tools/pnnx/src/ir.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 759aeb6793

ℹ️ 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".

Comment thread tools/pnnx/src/pass_level2/F_conv1d.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b1237ed449

ℹ️ 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".

Comment thread tools/pnnx/src/main.cpp Outdated
Comment thread tools/pnnx/src/pt2_graph_lowering.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 869b33aedf

ℹ️ 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".

Comment thread tools/pnnx/src/main.cpp Outdated
Comment thread tools/pnnx/src/pt2_graph_lowering.cpp

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: db8277dde0

ℹ️ 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".

Comment on lines +30 to +31
GraphRewriterPass::write(op, captured_params);
op->params["mode"] = "nearest-exact";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reject nearest-exact instead of silently using nearest

When a PT2 model uses 2-D or 3-D mode='nearest-exact', this rewrite marks it as supported, but pass_ncnn/F_interpolate.cpp explicitly maps nearest-exact to ordinary ncnn nearest interpolation (resize_type=1). Those coordinate rules differ—for example, resizing a spatial dimension from 2 to 3 can select [0,1,1] for nearest-exact versus [0,0,1] for ncnn nearest—so the default conversion succeeds while silently producing incorrect inference results. Implement the exact sampling rule or reject ncnn export for this mode.

Useful? React with 👍 / 👎.

Comment on lines +16 to +18
pnnx.Input input_1 0 1 size
pnnx.Input input_2 0 1 scale_factor
aten::_upsample_nearest_exact2d op_0 3 1 input size scale_factor out

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Capture interpolation arguments for low optimization levels

When either new direct nearest-exact pattern runs with optlevel=0 or optlevel=1, size and scale_factor remain operator inputs because the level-5 constant-expression pass only runs at optlevel>=2. The ncnn F.interpolate rewrites accept a single tensor input and require size or scale_factor as parameters, so this three-input operator survives pass_ncnn and is serialized as the nonexistent ncnn layer F.interpolate while conversion reports success. Match and capture the constant/list arguments during this level-2 rewrite, as the more specific interpolation patterns below do.

Useful? React with 👍 / 👎.

Comment on lines +49 to +53
prim::Constant op_0 0 1 use_input_stats value=%use_input_stats
prim::Constant op_1 0 1 momentum value=*
prim::Constant op_2 0 1 eps value=%eps
prim::Constant op_3 0 1 cudnn_enabled value=*
aten::instance_norm op_4 9 1 input weight bias running_mean running_var use_input_stats momentum eps cudnn_enabled out

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Lower tracked-stat instance normalization before ncnn export

When exporting an evaluated nn.InstanceNorm* with track_running_stats=True, use_input_stats is false and the running mean/variance are tensor attributes, so this new fallback matches and emits F.instance_norm with those inputs. Both ncnn instance-normalization rewrites require running_mean=None and running_var=None; consequently the operator remains unchanged and save_ncnn writes an unsupported F.instance_norm layer while returning success. Either lower the fixed running-stat computation to an ncnn-supported operation or reject this case before producing ncnn files.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c27edc0932

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

std::string message;
if (parameter_from_argument(arg, value, message) != 0)
return fail_node(index, node, message + " for argument " + node.inputs[i].name);
args[i] = Parameter::encode_to_string(value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Emit valid Python for non-finite expression constants

When a symbolic operation combines a runtime value with a concrete non-finite float—for example _operator.eq(x.sum().item(), float('nan')) or torch.sym_max(..., inf)—this expression path encodes the constant as the bare token nan, inf, or -inf. ir.cpp later copies that token verbatim into model_pnnx.py, where it is an undefined Python name, so the generated model raises NameError; encode these constants as float('nan')/float('inf') when expanding expressions, as is already done for direct operator arguments.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant