Repository navigation
Rewriter: fix cast_constant_of_shape for values outside the target range - #3153
Open
Lim Jooyoung (mmporong) wants to merge 1 commit into
Open
Lim Jooyoung (mmporong) wants to merge 1 commit into
Lim Jooyoung (mmporong) wants to merge 1 commit into
Conversation
fused_cast_constant_of_shape converted the scalar with .item() and built a new tensor from the Python value, which raises OverflowError for values outside the target range (for example 300 or -1 cast to UINT8), so the whole rewrite or optimize pass failed. Cast the numpy array instead, which wraps the value the same way the Cast op does at runtime.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fused_cast_constant_of_shapereads theConstantOfShapevalue with.item()and builds the new tensor from that Python scalar. When the value does not fit theCasttarget type, NumPy 2 raisesOverflowError(for example300or-1cast toUINT8, or200cast toINT8), sorewriter.rewrite()andoptimizer.optimize()fail on the whole model.Castitself runs fine on such a graph: ONNX Runtime turns300into44and-1into255.This casts the NumPy array (
astypeto the target dtype) instead of going through a Python scalar, so the fused constant holds the same value thatCastproduces.np.errstate(invalid="ignore")keeps NumPy from warning on float values outside an integer range. Values inside the range are unchanged.This touches the replacement function only; open PR #3145 adds a
checkto the same rule for string targets and reference attributes, so the two do not overlap in code.Testing:
_cast_constant_of_shape_test.py(int32 300 -> UINT8,int32 -1 -> UINT8,int64 200 -> INT8) checks that the rule applies and that the fused value equals the NumPy cast. All three raiseOverflowErroronmainand pass with this change.onnxscript/rewriter/rules/common: 352 passed, 1 skipped (349 before, plus the 3 new cases).-1.5 -> UINT8,3.7 -> INT32,1e10 -> INT32),70000 -> INT16and2.5 -> FLOAT16. All 8 match.ruff checkandruff format --check(0.15.8, from the lintrunner requirements) pass on both files. I did not run the full lintrunner (mypy, etc.).