kfeng123 commented on PR #15350: URL: https://github.com/apache/tvm/pull/15350#issuecomment-1645258393
> btw, does the dataflow pattern support recursion (loop) at all? Surprisingly, the answer is yes. Indeed, I was thinking to add the support for recursion, only to find that the existing dataflow pattern language has alreadly (silently) support it. But currently recursion may only implemented in C++, not python. The following is an example taken from the [pre-RFC](https://discuss.tvm.apache.org/t/on-the-applications-of-the-composition-of-dataflow-patterns/15344?u=kfeng123) doc: ``` python TVM_REGISTER_GLOBAL("relay.dataflow_pattern.my_pattern") .set_body_typed([]() { DFPattern dense_pattern = IsOp("nn.dense")({IsWildcard(), IsWildcard()}); ObjectPtr<CallPatternNode> the_pattern_ptr = make_object<CallPatternNode>(); the_pattern_ptr->op = IsOp("cast"); the_pattern_ptr->args.clear(); CallPattern the_pattern = CallPattern(the_pattern_ptr); AltPattern or_pattern{the_pattern, dense_pattern}; the_pattern_ptr->args.push_back(or_pattern); //LOG(INFO) << PrettyPrint(the_pattern); // TODO: BUG! return the_pattern; }); ``` This simple pattern matches a nn.dense followed by an arbitrary number of cast. You can test this pattern via the following python code: ``` python class TheRewrite(DFPatternCallback): def __init__(self): super(TheRewrite, self).__init__(rewrite_once = True) pattern = tvm.get_global_func("relay.dataflow_pattern.my_pattern")() self.pattern = pattern def callback(self, pre, post, node_map): return relay.nn.relu(post) mod = create_model() # define a model the_rewrite = TheRewrite() out = rewrite(the_rewrite, mod["main"]) ``` Another application of recursion is PR [#15362](https://github.com/apache/tvm/pull/15362), which I do not know how to achieve without recursion. That PR is useful, and can really improve the computational graph for some quantized models. I would like to examine the pattern matching code further in the following days. If I find any bug with pattern recursion, I would like to raise PRs. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
