-
Notifications
You must be signed in to change notification settings - Fork 66
2301 fold pad into conv #2363
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
base: main
Are you sure you want to change the base?
2301 fold pad into conv #2363
Conversation
Convert 'auto_pad' attribute into a list of explicit pads.
4b9b69b
to
19b0418
Compare
- Pad ∘ Conv -> Conv | ||
- Pad ∘ ConvInteger -> ConvInteger | ||
|
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.
Is this Conv ∘ Pad and ConvInteger ∘ Pad?
) | ||
elif constant_value.const_value.numpy().item() != 0: | ||
return check_result.fail(f"{constant_value.name} must be equal to 0.") | ||
axes = list(range(x_rank)) |
Check warning
Code scanning / CodeQL
Variable defined multiple times Warning
redefined
"""Replaces ``Pad(ConvInteger(x))`` with ``ConvInteger(x)``.""" | ||
|
||
def __init__(self, as_function: bool = False): | ||
super(FusePadConv, self).__init__(name="FusePadConvInteger", as_function=as_function) |
Check failure
Code scanning / CodeQL
First argument to super() is not enclosing class Error
return new_pads | ||
|
||
|
||
def read_conv_attributes(ir_conv: ir.Node) -> dict[str, typing.Sequence[int] | str]: |
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.
I just added some methods you can use to read attributes: https://onnx.ai/ir-py/api/generated/onnx_ir._graph_containers.Attributes.html#onnx_ir._graph_containers.Attributes.get_int
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2363 +/- ##
==========================================
+ Coverage 70.15% 70.42% +0.26%
==========================================
Files 196 198 +2
Lines 24687 24943 +256
Branches 2645 2683 +38
==========================================
+ Hits 17320 17566 +246
- Misses 6454 6459 +5
- Partials 913 918 +5 ☔ View full report in Codecov by Sentry. |
if (kernel_shape := ir_conv.attributes.get("kernel_shape", None)) is not None: | ||
attributes["kernel_shape"] = kernel_shape.as_ints() | ||
else: | ||
attributes["kernel_shape"] = ir_conv.inputs[1].shape[2:] |
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.
if (kernel_shape := ir_conv.attributes.get("kernel_shape", None)) is not None: | |
attributes["kernel_shape"] = kernel_shape.as_ints() | |
else: | |
attributes["kernel_shape"] = ir_conv.inputs[1].shape[2:] | |
attributes["kernel_shape"] = ir_conv.attributes.get_ints("kernel_shape", ir_conv.inputs[1].shape[2:]) |
if (kernel_shape := ir_conv.attributes.get("kernel_shape", None)) is not None: | ||
attributes["kernel_shape"] = kernel_shape.as_ints() | ||
else: | ||
attributes["kernel_shape"] = ir_conv.inputs[1].shape[2:] |
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.
What if the shape of ir_conv.inputs[1]
is not known? Just making sure it is checked
conv_attr: typing.Mapping[str, ir.Attr] = cnode.attributes.copy() | ||
if "pads" in conv_attr: | ||
new_pads = [x + y for x, y in zip(conv_attr["pads"].as_ints(), new_pads)] | ||
conv_attr["pads"] = ir.convenience.convert_attribute("pads", new_pads) |
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.
conv_attr["pads"] = ir.convenience.convert_attribute("pads", new_pads) | |
conv_attr.add(ir.AttrInt64s("pads", new_pads)) |
new_pads = pad_pads[2:x_rank] + pad_pads[x_rank + 2 :] | ||
|
||
# Replace conv pads = new + old | ||
conv_attr: typing.Mapping[str, ir.Attr] = cnode.attributes.copy() |
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.
conv_attr: typing.Mapping[str, ir.Attr] = cnode.attributes.copy() | |
conv_attr = cnode.attributes.copy() |
pnode = pad.producer() | ||
cnode = conv.producer() |
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.
pnode = pad.producer() | |
cnode = conv.producer() | |
pad_node = pad.producer() | |
conv_node = conv.producer() |
avoid abbreviations unless well known
opset_imports=opset_imports, | ||
name="model", | ||
), | ||
ir_version=9, |
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.
ir_version=9, | |
ir_version=10, |
nit: prefer 10 but doesn't matter too much
conv_attributes: typing.Mapping[str, ir.Attr] | None = None, | ||
opset_imports: typing.Mapping[str, int] = {"": 20}, | ||
) -> ir.Model: | ||
tape = ir.tape.Tape() |
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.
This is a great example of using the tape module, thanks!
pad_inputs: typing.Sequence[ir.TensorProtocol | ir.Value | None], | ||
pad_attributes: typing.Mapping[str, ir.Attr] | None = None, | ||
conv_attributes: typing.Mapping[str, ir.Attr] | None = None, | ||
opset_imports: typing.Mapping[str, int] = {"": 20}, |
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.
Using dictionaries as default values is not recommended. Since this field is not changed by callers, I suggest we set it as a constant inside the method
self, | ||
op_type: str, | ||
input_shape: ir.Shape, | ||
weight_shape: typing.Sequence[int], |
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.
weight_shape: typing.Sequence[int], | |
weight_shape: Sequence[int], |
prefer importing the classes from the typing module: https://google.github.io/styleguide/pyguide.html#2241-exemptions
Fuses Pad nodes into the following nodes (Conv, ConvInteger)
(#2301)