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 Lixiaoquan via TVM Discuss <no...@discuss.tvm.ai> on 2020/05/30 08:33:38 UTC

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output


Current TypePattern requires type in pattern matches type in expr exactly. Is there a way to match only according to element type. For example, match a fp16 *add*?





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/1) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/b4fb4a78777b3a52cfdc29cea58b373ac989ceb5a5a6f6eaf1c0c0befca8c152).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by Matthew Brookhart via TVM Discuss <no...@discuss.tvm.ai>.

What do you guys thing? Which would be easier to use?





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/11) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/476fd7ef81feb94a16420185bfa93b32bfbd8ac64b2eeafd181d834592654028).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by "Cody H. Yu via TVM Discuss" <no...@discuss.tvm.ai>.

I personally like `ShapePattern` and `DTypePattern` more as they are more straightforward in the pattern language, but I'd also like to see other's opinions.

cc @zhiics @tqchen @masahi





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/12) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/0ac78cedd5a278a31cc00c1d4794d7717679bb729dab0f2c33502d36be50259c).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by Matthew Brookhart via TVM Discuss <no...@discuss.tvm.ai>.

https://github.com/apache/incubator-tvm/pull/5760





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/15) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/a8318cd47e0e4d035c39d4d59e4d2896638eee0f1590612faadde0e171aee016).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

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

I would also agree to add these two patterns instead of tricking the matching APIs/rules





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/13) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/bed79dc7ba35001af7c9301673a87ac8fc29d34b94776fcb2943f928395b0e38).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by Matthew Brookhart via TVM Discuss <no...@discuss.tvm.ai>.

Yeah, it's a bit complicated. The current pattern uses the tvm::ir::Type* classes, so you can match  number of versions of types, but as this question reveals, we may want a finer granularity on some types.

Unfortunately, since we're using a lower level Type object, we won't be able to embed Pattern-specific things into that Type (like wildcard or AnyShape)

I think our options are to add rules like above, or extend the pattern language to include things like "ShapePattern" and "DTypePattern" at a more granular level.





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/10) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/045b1a8a84d05cd65b70a01c065f3e7b9768cbffcb10c9360d226565679f74e7).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by "Cody H. Yu via TVM Discuss" <no...@discuss.tvm.ai>.

Make sense. The ideal interface for this case would be leveraging has_type as other type matching. We may need new patterns like AnyShape, or support Wildcard in tensor shapes (seems much harder to me).





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/9) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/7f0b6e2bbe5cd96d96d5295408f143796b460d51a51fab638f20ab5096090708).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by Matthew Brookhart via TVM Discuss <no...@discuss.tvm.ai>.

My apologies! I somehow missed this last week.

Yeah, the current TypePattern is matching the full type via StructuralEqual. One possibility to clean this up slightly is to add a rule of:
1) If it's a TensorType
2) and the pattern's shape is ()
3) only check the dtype

That would only take a handful of lines in the C++ matcher to implement. It's a little specialized, but easier that doing it with callbacks.





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/8) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/54f205b3e3e79cc4470950cb91ab5dfc207314384b12ae53244073c607615e54).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by "Cody H. Yu via TVM Discuss" <no...@discuss.tvm.ai>.

`has_type` is not necessary to match the final expression. It can also be used like the following to check the type of an op:

```python
in1 = wildcard()
in2 = wildcard()
pat = is_op('add')(in1, in2).has_type(relay.TensorType((10, 10), 'float32'))

x = relay.var('x', shape=(10, 10), dtype='float32')
pat.match(relay.add(x, x))
```

This can be extended to a more complex pattern like

```python
in1 = wildcard()
in2 = wildcard()
add = is_op('add')(in1, in2).has_type(relay.TensorType((10, 10), 'float32'))
mul = is_op('multiply')(add, add)
```

In this case, you only match the the type of `add` but do not care `mul`, although in this case `mul` must be `float32`, too.





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/2) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/3027e8d81e00b34102ed7ce473e4174be0fd8d1228d50220f9492c2b8fad3f5c).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

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

I'm afraid I haven't made my point clear.  By using has_type, we can match to a specific dtype, but the shape of tensor is also fixed. By using wildcard(), we can match to (**AnyShape**, **AnyType**).  How to achieve something like has_type((**AnyShape**, 'float32'))?
```
in1 = wildcard()
in2 = wildcard()
pat = is_op('add')(in1, in2).has_type(relay.TensorType((10, 10), 'float32'))

x = relay.var('x', shape=(10, 10), dtype='float32')
pat.match(relay.add(x, x))
```
Thanks





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/5) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/b9345d84e5dd4935dc572de4ade3aa61db9de9184ec637b34bc5df8962da94df).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by "Cody H. Yu via TVM Discuss" <no...@discuss.tvm.ai>.

Of course. Shape is a part of the type.





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/4) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/6a61a8549723d4f0245c35a4be8f5511e2f44e4a60758b308929c3ad3134d3f1).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by Matthew Brookhart via TVM Discuss <no...@discuss.tvm.ai>.

I'll throw together a PR with those extensions. Thanks!





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/14) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/728f6145f25bf8c5ce28a570ab80c2b1c222cab6c3e92da41da9386f9dd4bc0e).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

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

Thanks a lot for this detail sample!





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/7) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/011c799cc49d73b6d74ae5e5301ed7f276c8c9ae74b39b99cc023ae9f9a6b479).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

Posted by "Cody H. Yu via TVM Discuss" <no...@discuss.tvm.ai>.

I see your point and it seems fair.

In the current implementation, one solution I can think of is leveraging the `check` function:

```python
import tvm
from tvm import relay
from tvm.relay.dataflow_pattern import *

def check(pre):
    return (pre.args[0].checked_type.dtype == 'float32' and
            pre.args[1].checked_type.dtype == 'float32')

pat = is_op('add')(wildcard(), wildcard())

x = relay.var('x', shape=(10, 10), dtype='float32')
out = relay.add(x, x)
func = relay.Function([x], out)
mod = tvm.IRModule()
mod['main'] = func
mod = relay.transform.InferType()(mod)

print(pat.partition(mod['main'].body, check=check))
```

In short, you can implement a check function which does any forms of checking by looking into the matched subgraph.

In case you only need to know if matching or not and do not want to partition the graph, you can use `rewrite` to mimic the above functionality:

```python
import tvm
from tvm import relay
from tvm.relay.dataflow_pattern import *

class MyCallback(DFPatternCallback):
    def __init__(self):
        self.in1 = wildcard()
        self.in2 = wildcard()
        self.pattern = is_op('add')(self.in1, self.in2)
        self.match = False

    def callback(self, pre, post, node_map):
        if (node_map[self.in1][0].checked_type.dtype == 'float32' and
            node_map[self.in2][0].checked_type.dtype == 'float32'):
            self.match = True
        return post


x = relay.var('x', shape=(10, 10), dtype='float32')
out = relay.add(x, x)
func = relay.Function([x], out)
mod = tvm.IRModule()
mod['main'] = func
mod = relay.transform.InferType()(mod)

callback = MyCallback()
rewrite(callback, mod['main'].body)
print(callback.match)
```

When matching the pattern, `rewrite` will call the callback function for graph mutation. You can of course add any checks here and maintain your own "match" status.

While the above solutions are working, there are definitely imperfect, especially when the pattern is complex. In long term, we may want to support partial type matching in the pattern language.

cc @mbrookhart





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/6) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/718dc81d65775478e665cb3058af7b1e63259f83066a372388f9677b39fec672).

[TVM Discuss] [Questions] [PattenLang]How to match op according to element type of input and output

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

Can it match shape other than (10,10)?





---
[Visit Topic](https://discuss.tvm.ai/t/pattenlang-how-to-match-op-according-to-element-type-of-input-and-output/6846/3) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/88e97fd877f9f31ebb90fa98bbf2fb7000a177f659d77f0ca894fad1539a6dcd).