Skip to content

Keep the saturate attribute when constant folding rewrites CastLike to Cast - #3075

Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
microsoft:mainfrom
MohammedAlkindi:fix/castlike-fold-saturate
Open

Mohammed Alkindi (MohammedAlkindi) wants to merge 1 commit into
microsoft:mainfrom
MohammedAlkindi:fix/castlike-fold-saturate

Conversation

@MohammedAlkindi

Copy link
Copy Markdown
Contributor

The CastLike partial evaluator rewrites CastLike to Cast without its saturate attribute, so saturate=0 silently becomes the default 1. For a float8 target, values out of range then clamp to ±448 instead of becoming NaN. ONNX Runtime on CastLike <saturate = 0> to float8e4m3fn with input [1000, 1, -1, 0.3]:

original   [nan, 1, -1, 0.3125]
optimized  [448, 1, -1, 0.3125]

The rewrite now forwards saturate, which is what ONNX's CastLike function body passes on to Cast, including an @sat reference inside a function. round_mode is not forwarded, matching that body; one test checks it stays off the Cast.

Three of the four new tests fail before the change. onnxscript/optimizer: 78 passed, and the optimized models give the same ONNX Runtime output as the originals for saturate=0, round_mode="down" and the @sat function. ruff check and ruff format --check are clean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

1 participant