Fix Edge Case: Shared Layer Output Between Model Output and Internal Layers #2407
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Edge Case: Shared Layer Output Between Model Output and Internal Layers
🎯 Summary
This PR addresses a specific edge case that occurs when converting TensorFlow models to ONNX. It improves the handling of cases where a layer's output is used simultaneously as both the input to another layer and the model's final output.
🔧 Changes
Edge case handling: Resolves the case where a layer's output is used as both the model output and the input to an internal layer.
Improved Transpose output logic:
Uses identity nodes to handle output branching.
Differentiates between model output consumers and internal consumers.
Introduces a two-step process: edge case handling → regular case handling.
Optimizer order optimization: Moves remove_identity to the front of the optimizer chain.
Improved error messages: Provides clearer error messages during graph output validation.
Technical Details
Before:
When a layer's output is connected to both the model output and internal layers, the transpose operation fails, causing the graph structure to break.
After:
Output is branched through identity nodes.
Transpose is applied only to the model output consumer.
Internal consumers maintain the original output.
🔍 Files Changed
tf2onnx/tfonnx.py - Major improvements to the transpose_outputs function.
tf2onnx/graph.py - Improved error messages and code cleanup.
tf2onnx/optimizer/init.py - Changed optimizer order.
Test model file
This file is a test model created by extracting part of the edge case I encountered.
https://drive.google.com/file/d/10ChR5OS4k6az1yG13vdbiassrsxYUkuJ/view?usp=sharing