Repository navigation
2.3.11
2.3.11
Important
Starting with onnx2tf v2.4.0, tf_converter will be deprecated and the default backend will be switched to flatbuffer_direct. With the v2.3.3 update, all backward compatible conversion options have been migrated to flatbuffer_direct, so I will only be doing minor bug fixes until April. If you provide us with ONNX sample models, I will consider incorporating them into flatbuffer_direct. I'll incorporate ai-edge-quantizer when I feel like it, but that will probably be about 10 years from now.
Summary
This PR brings the feat-torch7 improvements into main as the 2.3.11 update for the PyTorch export path.
The main focus of this branch is exporter robustness around layout bridges, rank-3 reshape/transpose edge cases, and Dynamo ONNX post-processing. In practice, it makes the generated PyTorch package, TorchScript / ExportedProgram artifacts, and sanitized Dynamo ONNX outputs much more stable for models that mix channel-first runtime values with channel-last logical layouts.
What this PR improves
1. Native PyTorch export canonicalization is more robust
- Fixes rank-3 reshape materialization when the runtime tensor is effectively channel-first but the logical tensor metadata is channel-last.
- Preserves required NHWC materialization before rank-3 reshapes instead of collapsing it back to an unsafe alias during later rewrite passes.
- Restores correct public output resize target shapes in generated packages.
- Improves rank-3 transpose bridge handling in generated PyTorch code.
2. ExportedProgram rewrite passes are now safer
- Prevents
_rewrite_generated_model_source_for_exported_program()from undoing already-correct materializations for tensors that must stay in channel-last form before reshape. - Keeps the generated
model.pysemantically aligned with the original ONNX / lowered IR instead of reintroducing layout mismatches during artifact export.
3. Better handling for channel-last GAP / conv and gated bridge patterns
- Repairs channel-last global-average-pooling outputs before they are consumed by following convolution blocks.
- Adds canonicalization for channel-first hardsigmoid gate patterns so that bridge-heavy code is simplified without breaking downstream binary alignment.
- Improves bridge folding around GAP, conv, unary, and binary patterns that previously produced fragile generated code.
4. Dynamo ONNX sanitization is stronger and less shape-fragile
- Replaces rank-4-only output-shape repair with rank-agnostic static-shape recovery.
- Preserves output metadata for rank-3 transpose outputs and unary outputs that were previously at risk of losing valid shape information.
- Expands transpose/unary/binary/reduce-mean bridge folding so the exported ONNX graph is cleaner and carries more reliable boundary metadata.
- Adds folding for
Mul + Add + Clip -> HardSigmoidstyle patterns.
5. Regression coverage is significantly expanded
This branch adds focused tests for:
- runtime channel-first aliases feeding rank-3 reshapes
- ExportedProgram rewrite safety for already-materialized NHWC aliases
- rank-3 transpose output-shape preservation in sanitized Dynamo ONNX
- unary / binary / reduce-mean layout-bridge folding
- GAP-to-conv channel-last repair
- hardsigmoid bridge folding
- model-backed regressions such as
yolox_soutput-shape preservation and existing bread helper behavior
Real regressions addressed by this branch
yolox_s.onnx
The generated *_dynamo.onnx could lose its final output shape metadata after sanitization. This branch preserves the expected output shape and adds regression coverage for it.
iat_llie_180x320.onnx
The generated PyTorch package could drift badly from ONNX because a required NHWC materialization was rewritten back into a raw channel-first alias before a rank-3 reshape.
After the fix, the conversion command below now passes the ONNX/PyTorch comparison:
python -m onnx2tf -i iat_llie_180x320.onnx -o /tmp/iat_llie_fix2_run -tb flatbuffer_direct -cotof -fdopt -fdots -fdodo -fdoepObserved result after the fix:
max_abs=4.76837e-07rmse=1.40854e-07cosine=1pass=True
Validation
I validated the branch with targeted exporter / sanitizer regressions, including:
pytest -q tests/test_pytorch_exporter.py -k 'runtime_cf_alias_before_rank3_reshape or exported_program_preserves_rank3_reshape_materialize_without_model_ir or preserves_transpose_rank3_output_shape or preserves_atan_rank4_output_shape'pytest -q tests/test_pytorch_exporter.py -k 'exported_program_preserves_rank3_reshape_materialize_without_model_ir or runtime_cf_alias_before_rank3_reshape or preserves_transpose_rank3_output_shape or preserves_atan_rank4_output_shape or preserves_yolox_output_shape_when_model_is_available or helper_postprocess_is_noop_for_bread_nonfm_when_model_is_available'Both validation sets passed locally, and the branch also includes the version bump to 2.3.11 in:
onnx2tf/__init__.pypyproject.tomlREADME.md
Notes for reviewers
Most of the code volume in this PR comes from two areas:
- broader layout-bridge folding / shape-repair support inside
onnx2tf/tflite_builder/pytorch_exporter.py - the matching regression suite added to
tests/test_pytorch_exporter.py
The intention is not to change the overall export architecture, but to make the existing PyTorch export and Dynamo ONNX cleanup pipeline much more reliable for real-world bridge-heavy models.
What's Changed
- Improve PyTorch exporter layout bridge rewrites and Dynamo ONNX sanitization by @PINTO0309 in #913
Full Changelog: 2.3.10...2.3.11