pnnx for torch exported program - #6946
AtomAlpaca wants to merge 49 commits into
Conversation
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
| GraphRewriterPass::write(op, captured_params); | ||
| op->params["mode"] = "nearest-exact"; |
There was a problem hiding this comment.
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 👍 / 👎.
| 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 |
There was a problem hiding this comment.
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 👍 / 👎.
| 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 |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
💡 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); |
There was a problem hiding this comment.
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 👍 / 👎.
Summary
Add pnnx support for PyTorch
torch.export.ExportedProgrammodels saved withtorch.export.save().Convert the exported model with:
Changes
pnnx::Graph.SymInt,SymFloatandSymBoolvalues.pnnx.Assert.Supported formats
Tests
The complete PT2 suite contains 392 tests with TorchVision enabled:
The producer-version regression subset contains 383 tests: