-
Notifications
You must be signed in to change notification settings - Fork 56
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
Improve handling of trailing optional inputs in pattern matching #1948
base: main
Are you sure you want to change the base?
Conversation
@justinchuby re. our discussion |
❌ 14 Tests Failed:
View the full list of 3 ❄️ flaky tests
To view more test analytics, go to the Test Analytics Dashboard |
f"Input nums mismatch. {len(node.inputs)} vs {len(pattern_node.inputs)}" | ||
) | ||
# Inputs are padded with Nones to match against pattern | ||
to_match = itertools.zip_longest(node.inputs, pattern_node.inputs, fillvalue=None) |
Check failure
Code scanning / lintrunner
MYPY/assignment Error
I think most of the time a user would want Non-Nones as default. I agree having to check for None for every input can be tedious. Given this I think specifically declaring something can be None is more intentional and less error prone. |
Pattern-variables are allowed in places where an optional input is expected. If the input is present, the pattern-variable will be bound to the corresponding ir.Value. Otherwise, the pattern-variable will be bound to None.
This PR extends this convention to handle trailing optional inputs better: specifically,
SomeOp(x)
andSomeOp(x, None)
are treated equivalently during the pattern-match. If the pattern isSomeOp(pattern_x, pattern_y)
, thenpattern_y
will be bound to None in both cases, and the pattern-match will succeed.Side note: A consequence of allowing pattern-variables to be bound to None is that the corresponding guard/rewrite function will have to handle the case where the pattern-variable is None correctly. This is not an issue for correctly-typed models (where we can assume that a non-optional input will never be None). However, to protect against incorrectly-typed models, we may end up having to add tedious
x is not None
checks before accessing x's fields/attributes. It may be useful to perhaps allow the user to flip the convention for a given rule (as to whether pattern-variables are allowed to be bound to None). Or, ensure a model is type-valid before applying optimizations. (Or, a quick hack might be to catch these exceptions in the rewrite-engine and skip that particular rewrite, which is probably a good idea anyway.)