CastCast rewrite changes rounding for DOUBLE inputs by removing the FLOAT intermediate
维护者通常 2 天内回复
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- python
调研方向
Find the CastCast rewrite rule in onnxscript.rewriter.rules.common and read how it determines whether the intermediate cast can be removed. Add focused tests for the reported DOUBLE rounding boundary and for supported mixed precision source types, then run the relevant rewrite tests. Done means the rewrite preserves both casts for DOUBLE or unknown source types while still collapsing the supported combinations; an open linked pull request already addresses this issue.
由索引模型根据 Issue 内容生成。
描述
Description
The CastCast rewrite checks the intermediate/output dtype pair but ignores the source dtype. It currently treats both Cast(Cast(X, FLOAT), FLOAT16) and Cast(Cast(X, FLOAT), BFLOAT16) as equivalent to a direct cast for every source type.
That is not true for wider sources: rounding to FLOAT can land exactly on a lower-precision midpoint, so the second rounding can differ from a direct cast. This silently changes valid model outputs.
Reproduction
Reproduced locally on main f51083c, Python 3.12.16, macOS arm64, using the ONNX reference evaluator:
import numpy as np
import onnx
import onnx.reference
import onnx_ir as ir
from onnxscript.rewriter.rules.common import _basic_rules
h = onnx.helper
model = h.make_model(
h.make_graph(
[
h.make_node("Cast", ["X"], ["M"], to=onnx.TensorProto.FLOAT),
h.make_node("Cast", ["M"], ["Y"], to=onnx.TensorProto.FLOAT16),
],
"rounding",
[h.make_tensor_value_info("X", onnx.TensorProto.DOUBLE, [1])],
[h.make_tensor_value_info("Y", onnx.TensorProto.FLOAT16, [1])],
),
opset_imports=[h.make_opsetid("", 18)],
ir_version=10,
)
rewritten = ir.from_proto(model)
print(_basic_rules.cast_cast_rule.apply_to_model(rewritten)) # 1
feeds = {"X": np.array([1.00048828125 + 2**-30], dtype=np.float64)}
for candidate in (model, ir.to_proto(rewritten)):
y = onnx.reference.ReferenceEvaluator(candidate).run(None, feeds)[0]
print(y, y.view(np.uint16))
Original: [1.] [15360]
Rewritten: [1.001] [15361]
NumPy independently gives the same distinction for x.astype(np.float32).astype(np.float16) versus x.astype(np.float16).
This is semantic correctness evidence from the reference evaluator, not a GPU/performance claim. In my local ONNX Runtime CPU check, both graphs returned the lower value; that runtime result does not independently demonstrate this rewrite's numerical difference.
Proposed fix
Restrict elimination to source dtypes for which widening to FLOAT is exact, retaining the intended mixed-precision cases (FLOAT, FLOAT16 and BFLOAT16). Preserve both casts for DOUBLE, unproven source types, or missing source-type information. Add exact-result tests around rounding boundaries plus positive tests that the supported mixed-precision combinations still collapse.
I checked open PRs and searched issues/PRs for CastCast and cast rounding/precision; I did not find a competing fix for this source-type guard.
- 主要语言
- Python
- 星标
- 456
- 派生
- 159
- 平均合并
- 3 天 5 小时
- 30 天内合并 PR
- 23
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/onnxscript 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
microsoft/onnxscript#3200 ·
维护者通常 2 天内回复
-
`fuse_skip_rms_normalization` fuses a float16 residual into a norm whose scale is float32, which ONNX Runtime rejects可能已有人在做 @justinchuby 于 8 天前认领。 未关闭module: optimizer
难度 2/5 1-3 小时 新手友好度 76/100
microsoft/onnxscript#3155 · 3 条评论 · 1 个 reaction · 已指派 2 人 ·
维护者通常 2 天内回复
-
Constant folding uses onnx's plain reference ops, never onnx.reference.ops_optimized可能已有人在做 @todkarsant 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/onnxscript#3003 · 3 条评论 ·
维护者通常 2 天内回复
-
CI : Pin github actions to full commit SHA可能已有人在做 @Aniketsy 于 193 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
microsoft/onnxscript#2903 · 6 条评论 ·
维护者通常 2 天内回复
-
Partial rotary fusion sets `rotary_embedding_dim` without `num_heads`, which ONNX Runtime requires可能已有人在做 @justinchuby 于 3 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 25/100
microsoft/onnxscript#3185 ·
维护者通常 2 天内回复
查看 microsoft/onnxscript 的全部 Issue
相似的 Issue
-
fleet size/m
难度 2/5 1-3 小时 新手友好度 78/100
sipyourdrink-ltd/bernstein#7548 ·
维护者通常 1 天内回复
-
good first issue help wanted level 1 wocs
难度 2/5 1-3 小时 新手友好度 82/100
GianlucaBave/murmury#73 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
G-nn-r/lg_heatpump#52 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
category:codebase:gpi category:codebase:handle type:bug
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复