-
Notifications
You must be signed in to change notification settings - Fork 72
Cast-cast elimination #2368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cast-cast elimination #2368
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -51,22 +51,30 @@ | |||||
class CastCast(orp.RewriteRuleClassBase): | ||||||
"""Replaces ``Cast(Cast(X, ...), to=to)`` by ``Cast(X, to=to)``.""" | ||||||
|
||||||
_allowed_tensor_types: ClassVar = { | ||||||
ir.DataType.FLOAT, | ||||||
ir.DataType.FLOAT16, | ||||||
ir.DataType.BFLOAT16, | ||||||
ir.DataType.DOUBLE, | ||||||
} | ||||||
# Simplify "cast type1 => type2 => type3" to "cast type1 => type3". | ||||||
# This rule is not valid for all combinations of types: e.g., | ||||||
# it is not valid for float32 => float16 => float32 or float32 => int32 => string. | ||||||
# TODO: fill out the list of allowed combinations: the following is just a couple | ||||||
# that shows up in practice where it is valid | ||||||
_allowed_type2_type3: ClassVar = frozenset( | ||||||
{ | ||||||
(ir.DataType.FLOAT, ir.DataType.FLOAT16), | ||||||
(ir.DataType.FLOAT, ir.DataType.BFLOAT16), | ||||||
} | ||||||
) | ||||||
|
||||||
def pattern(self, op, x, to, to_ignored): | ||||||
return op.Cast(op.Cast(x, to=to_ignored), to=to) | ||||||
|
||||||
def check(self, context, x: ir.Value, to: ir.Attr, to_ignored: ir.Attr) -> orp.MatchResult: | ||||||
check_result = orp.MatchResult() | ||||||
if to.as_int() not in self._allowed_tensor_types: | ||||||
return check_result.fail(f"Output type {to.as_int()} is not allowed") | ||||||
if to_ignored.as_int() not in self._allowed_tensor_types: | ||||||
return check_result.fail(f"Ignored type {to_ignored.as_int()} is not allowed") | ||||||
type2 = to_ignored.as_int() | ||||||
type3 = to.as_int() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (type2, type3) not in self._allowed_type2_type3: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe when type2==type3 or when both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first one is already covered by redundant cast elimination. The second one is what I think too (at least for float types; something similar for int types too may hold), but am keeping it simple here. (Do these methods is_floating_point / itemsize exist in ir.DataType?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes the methods exist. I think it’s pretty simple to implement |
||||||
return check_result.fail( | ||||||
f"Intermediate cast elimination not recognized as valid from {type2} to {type3}. " | ||||||
f"Cast-Cast rule may be incomplete for this combination." | ||||||
) | ||||||
return check_result | ||||||
|
||||||
def rewrite(self, op, x: ir.Value, to: ir.Attr, to_ignored: ir.Attr): | ||||||
|
@@ -284,7 +292,7 @@ | |||||
""" | ||||||
return orp.RewriteRuleSet( | ||||||
[ | ||||||
# cast_cast_rule, # Might have precision issues. | ||||||
cast_cast_rule, | ||||||
cast_identity_rule, | ||||||
expand_identity_rule, | ||||||
reshape_reshape_rule, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.