-
Notifications
You must be signed in to change notification settings - Fork 64
Pattern match on default attributes? #2012
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
Comments
Thanks. So a node with default and without default is store differently in graph. They are semantically the same but syntactically different. We need to decide what the behavior of the matcher should be. |
Well, it can be outside of matcher, if one could force defaults for But as I found, it may be not formalized at all!
I wonder, how it handled in |
I would rather put defaults handler outside of matcher, with function like |
I had the same problem. For now I solved it through a def target_pattern(op, x, w):
return op.Conv(x, w, _outputs=["y"])
def replacement_pattern(op, x, w, **__):
# Naïve pattern
return op.Identity(op.Conv(x, w))
def condition_fn(*_, y, **__):
ir_node = y.producer()
return (dilatation := ir_node.attributes.get("dilatation", None)) and dilatation.value == [1,1]
rule = RewriteRule(target_pattern, replacement_pattern, condition_fn) I tried the following def target_pattern(op, x, w, dilatation=[1,1]):
return op.Conv(x, w, dilatation=dilatation) But unfortunately the condition was never triggered. Let me know if there is a simpler solution. |
@Johansmm hey, yes, it is better then my force/strip_defaults! |
But @leshabirukov what do you think of my suggestion? Let the user define the default value in the target function: def target_pattern(op, x, w, dilations = [1,1]):
return op.Conv(x, w, dilations=dilations) Then, |
@Johansmm, I hesitate here. I agree, it is neat, understandable semantics. Questions to think about: Was defaults supposed to be ancored to the onnx standart? I mean is it ok to set defaulted Implementation can unveil some other issues, I can't say how to exactly parse following:
|
@leshabirukov I agree with you that it might be better to infer the default values of the schema, but I don't think it is the purpose of onnxscript to hardcode all the parameters that are not defined in the ONNX schemas. What I like about my solution is that in def target_pattern(op, x, w, dilations = "hello_world"):
return op.Conv(x, w, dilations=dilations)
def replacement_pattern(op, x, w, dilations):
if dilations == "hello_world":
# Remove dilations
y = op.Conv(x, w)
else:
# Include dilations
y = op.Conv(x, w, dilations=dilations)
return y I'm not sure it's a good solution, but at least I think the user has more control. |
Meh. I hesitate to judge, because I am not one, who will implement it. Behind nice and convenient interface of python-function-as-pattern, I sence a lot of support work, making it consistent with Python rules and user's expectation. And mechanics that already work sometimes fail: |
@leshabirukov and how about including your idea about e.g.: def target_pattern(op, x, w):
return op.Conv(x, w, force_defaults=True)
def replacement_pattern(op, x, w, dilations, **__):
# Naïve pattern
return op.Identity(op.Conv(x, w, dilations=dilations))
rule = RewriteRule(target_pattern, replacement_pattern) I could work on this, but I would like to define a workable solution. |
@Johansmm, let my groans not discourage you! I have no question to proposed syntax, it is nice and clear, it is implementation, my concerns about. If you or somebody manage to made it smoothly, it would be powerful addition to rewriter. Still, I would take a pause and gather more usecases, good thing As about |
Problem: rewrite
Conv
nodes, but only withdilations
==[1,1]. Issue - default value. To check dilation, I need include it in pattern, but this cause pattern match to fail on nodes with default dilations.Probably related: #1627
Possible fix: substitute defaults on deserialization, I suspect, such a function already exists.
rewr_c.py.txt
The text was updated successfully, but these errors were encountered: