[6385267][ONNX][Autocast] Preserve graph-output Cast producers#1943
[6385267][ONNX][Autocast] Preserve graph-output Cast producers#1943gcunhase wants to merge 3 commits into
Conversation
Preserve pre-existing floating Cast nodes when they are valid producers of graph outputs so autocast cleanup does not leave dangling graph outputs. Signed-off-by: Gwenaelle Cunha Sergio <gcunhasergio@nvidia.com>
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughModifies the condition for preserving pre-existing Cast nodes in ChangesCast Preservation Fix
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Warning
CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.
Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/unit/onnx/autocast/test_precisionconverter.py`:
- Around line 91-119: The test currently uses a DOUBLE→DOUBLE→FLOAT cast chain,
which won’t hit the preexisting-cast removal path; update the graph in
test_convert_preserves_cast_chain_graph_output to use an FP32→FP16→FP32 chain
instead. Adjust the input/output tensor types and the two Cast nodes in this
test so the converted_model path exercises _remove_preexisting_casts and
verifies the output-preservation behavior directly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 54296094-8937-434b-b86e-19fe095c922d
📒 Files selected for processing (2)
modelopt/onnx/autocast/precisionconverter.pytests/unit/onnx/autocast/test_precisionconverter.py
cjluo-nv
left a comment
There was a problem hiding this comment.
Bot review — DM the bot to share feedback.
Small, focused bug fix (+37/-3, 2 files) in the ONNX AutoCast PrecisionConverter._remove_preexisting_casts.
What it does: Previously a graph-output Cast was preserved only when both its input and output were graph outputs (the output→cast→output pattern). A valid cast chain like in → Cast → t1 → Cast → t2(output) had its final cast removed and bypassed, leaving t2 dangling and failing the sanity check. The new condition keeps a Cast when its output is a graph output AND the cast target type matches the declared output element type — the correct, broader predicate that still preserves the old chain case.
Correctness: Verified the logic. The change also broadens preservation to casts whose input isn't a graph output (e.g. test_multiple_output_node_casted_to_output, test_casted_input_to_output_model) but those tests assert only check_model, which stays valid whether the cast is kept or removed. The type-match guard prevents keeping mismatched output casts that keep_io_types=True should still repair.
Tests: New test_convert_preserves_cast_chain_graph_output reproduces the reported failure end-to-end through convert_to_mixed_precision (exercises the default keep_io_types=False path). I confirmed via the GraphSanitizer that the DOUBLE casts are converted to FP32 before _remove_preexisting_casts, so the test genuinely hits the fixed branch. It asserts the output is produced by exactly one Cast node and that the model validates.
No licensing changes (existing Apache-licensed file, headers untouched), no new abstractions/subsystems, no prompt-injection in the untrusted PR content. Minor nit (non-blocking): unlike neighboring tests, the new test isn't parametrized over keep_io_types/bf16.
Use an FP32 to FP16 to FP32 Cast chain so the test exercises pre-existing Cast removal directly. Signed-off-by: Gwenaelle Cunha Sergio <gcunhasergio@nvidia.com>
What does this PR do?
Type of change: Bug fix
modelopt/onnx/autocast/precisionconverter.pyso pre-existing floating-pointCastnodes are preserved when they are valid producers of graph outputs.Castonly when the Cast target type matches the declared graph output element type. This avoids dangling graph outputs while preserving existingkeep_io_types=Truebehavior for mismatched output Casts that should be removed and repaired.tests/unit/onnx/autocast/test_precisionconverter.pyfor a valid ONNX model whose graph output is produced by a consecutive Cast chain.Usage
Testing
Exception("Sanity Check Failed")after ONNX validation reported that graph outputt2was not produced by any node.Converted 2/2 nodes (100.00%) to fp16.pytest tests/unit/onnx/autocast/test_precisionconverter.py::test_convert_preserves_cast_chain_graph_output -qBefore your PR is "Ready for review"
Make sure you read and follow Contributor guidelines and your commits are signed (
git commit -s -S).Make sure you read and follow the Security Best Practices (e.g. avoiding hardcoded
trust_remote_code=True,torch.load(..., weights_only=False),pickle, etc.).CONTRIBUTING.md: N/ASummary by CodeRabbit
Bug Fixes
Tests