You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by cronos via Apache TVM Discuss <no...@discuss.tvm.ai> on 2021/11/18 07:27:38 UTC

[Apache TVM Discuss] [Questions] How to add my custom Relay node to pattern matcher? [EthosU example]


Hello,

I was looking at the Arm EthosU integration in TVM and [noticed that there was a new conv2d Relay operator defined](https://github.com/apache/tvm/blob/main/python/tvm/relay/backend/contrib/ethosu/op/convolution.py#L185). Obviously this operator is only legal/valid for offloading onto the EthosU, [so they decide to replace standard Relay operator patterns inside of a Composite with this single operator](https://github.com/apache/tvm/blob/main/python/tvm/relay/backend/contrib/ethosu/legalize.py#L126).
That makes it easier to define their own Relay->Tir pipeline since they only need to parse one node and all its parameters/attributes.

I was wondering though, how could this (or any) new Relay operator be added into the pattern matching infrastructure?

I guess what I would like is the following
```
#Original Relay module using standard Relay operators
%1 = standard_relay_op_1(...)
%2 = standard_relay_op_2(...)

#Assume I have some custom Relay operators that each match to one of those from above
%1 = my_custom_relay_op_1(...)
%2 = my_custom_relay_op_2(...)

#Now, I further have a third Relay operator which matches exactly to the pattern above
%1 = my_custom_rela_op_3(....)
```

I know in this simple case there is a solution using the already available pattern matcher infrastructure (i.e. defining the pattern to be match of `my_custom_relay_op_3` before the others in the pattern list). But let's say for more complex situations I couldn't just solve it via position in the pattern list.

How do I add `my_custom_relay_op_{1,2}` to the pattern matching infrastructure such that I can define the pattern matching for `my_custom_relay_op_3` to have statements/expression like `is_op('my_custom_relay_op_1')` ?


Thank you all





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-add-my-custom-relay-node-to-pattern-matcher-ethosu-example/11498/1) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/afd28cebad62f95679f29998044d6e6e9d61e5440a9d22e725a44a23655447b7).

[Apache TVM Discuss] [Questions] How to add my custom Relay node to pattern matcher? [EthosU example]

Posted by Mark Shields via Apache TVM Discuss <no...@discuss.tvm.ai>.

cc @mikepapadim, who's reworking FuseOps to be more Composite+patterns like.





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-add-my-custom-relay-node-to-pattern-matcher-ethosu-example/11498/5) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/04a7f178d46bca80bb1e706c5d4f12b41895dfadeb1a966c80f4323868b87ce7).

[Apache TVM Discuss] [Questions] How to add my custom Relay node to pattern matcher? [EthosU example]

Posted by Andrew Reusch via Apache TVM Discuss <no...@discuss.tvm.ai>.

@cron I think you're asking about how to express a situation where a subgraph of two adjacent Relay operators may match two patterns in the graph partitioning logic. could you elaborate on the exact problem you're having? I think the ordering logic should be sufficient to handle this, but it'd be good to understand your use case a bit further.

[quote="cron, post:2, topic:11498"]
Is the ordering of first pattern matching your offloadable and then replacing, within the extracted composite, the native relay operators with your new ethosu.conv2d relay operator a solution to not being able to do what I said before?
[/quote]

It's worth mentioning that in the Ethos-U case, there are additional reasons to legalize into Ethos-U-specific Relay operators. Specifically, information is needed further down the compilation pipeline from that entire subgraph, and it's only valid to drive the Ethos-U compilation pipeline with certain patterns of Relay operators and with certain arguments to those operators. So if I'm understanding your original question correctly, you could view the Ethos-U legalization as more of a sanity-check or input validation step rather than a workaround for a pattern-matcher limitation.

also cc @mbs-octoml @mbrookhart





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-add-my-custom-relay-node-to-pattern-matcher-ethosu-example/11498/4) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/fca59ad872266b5bc3d8227ff30de264c93e73e685ff76735e82b4a9c14d13de).

[Apache TVM Discuss] [Questions] How to add my custom Relay node to pattern matcher? [EthosU example]

Posted by Matt Barrett via Apache TVM Discuss <no...@discuss.tvm.ai>.

Hi,

Apologies for the delayed reply. I don't entirely follow your question on pattern matching - it seems like what you're talking about there is already possible.

Regarding the query about the Ethos-U partitioning/legalizing flow, I think the two routes are essentially equivalent. We partition first (via composite functions) and then legalize, but I agree it would be equally valid to legalize first and then partition. In terms of simplicity, I think both are just as simple. There are fundamentally two pieces of logic that need implementing - something to check whether a given pattern is supported by your offload target, and something to mutate the Relay into your target's Relay 'dialect'.

If you legalize first, then you must do both of these things in the same legalization pass. If you partition first, then you test 'supportedness' in the partitioner and subsequently do the mutation in the partitioned module. The code that needs writing is about the same, just positioned differently.





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-add-my-custom-relay-node-to-pattern-matcher-ethosu-example/11498/3) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/959f7d0989a2ec5712ff5175130133d4220bd65e12087bfb3278b6947cac2f54).

[Apache TVM Discuss] [Questions] How to add my custom Relay node to pattern matcher? [EthosU example]

Posted by cronos via Apache TVM Discuss <no...@discuss.tvm.ai>.

@manupa-arm  @matt-arm 

So maybe I can ask more directly.

Is the ordering of first pattern matching your offloadable and then replacing, within the extracted composite, the native relay operators with your new ethosu.conv2d relay operator a solution to not being able to do what I said before? 

The following would feel like a simpler pipeline
1. Find and replace in the main Relay module the native Relay op pattern which describes your `ethosu.<operator>` (just like you did it in your legalization process)
2. PartitionGraph given that `ethosu.<operator>` is offloadable 
3. Relay-TIR now works like you already have it





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-add-my-custom-relay-node-to-pattern-matcher-ethosu-example/11498/2) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/2d200c2ebea313a19a46c8e50a53fd213da781d929efc8628952740d5042cecf).

[Apache TVM Discuss] [Questions] How to add my custom Relay node to pattern matcher? [EthosU example]

Posted by Lily Orth-Smith via Apache TVM Discuss <no...@discuss.tvm.ai>.

I'm also a bit confused about your question, can you clarify what custom_relay_op_3 is, and its relationship to custom_relay_op_1 and custom_relay_op_2?





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-to-add-my-custom-relay-node-to-pattern-matcher-ethosu-example/11498/6) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/76e26894f659c68665f8974e4841fc9e2bc2e5f68f14fce8e356c4a158bb8a8f).