mbrookhart commented on a change in pull request #6052:
URL: https://github.com/apache/incubator-tvm/pull/6052#discussion_r454650310
##########
File path: include/tvm/relay/dataflow_matcher.h
##########
@@ -42,11 +42,16 @@ class DFPatternCallback;
class DFPatternCallbackNode : public Object {
public:
/*! \brief Pattern this callback matches */
- DFPattern pattern_;
+ DFPattern pattern;
/*! \brief Function to call when finding a matched expression */
- PackedFunc function_;
+ PackedFunc function;
+ /*! \brief Require InferType to be run before the callback */
+ bool require_type;
Review comment:
https://tvm.apache.org/docs/contribute/code_guide.html
https://google.github.io/styleguide/cppguide.html#Variable_Names
Why the move away from the Google Style Guide convention? You seem to use
the var_name_ convention in simplify_expr.cc.
##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file src/relay/transforms/simplify_expr.cc
+ * \brief A pass for simplifying the Relay expression.
+ */
+
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+#include <tvm/relay/dataflow_matcher.h>
+#include <tvm/support/logging.h>
+#include "../op/tensor/transform.h"
+
+namespace tvm {
+namespace relay {
+
+static Op reshape_op = Op::Get("reshape");
+static Op reverse_reshape_op = Op::Get("contrib_reverse_reshape");
+
+/*!
+ * \brief SimplifyReshape matches the pattern of consecutive reshape or
reverse_reshape ops,
+ * and merges into one reshape op.
+ */
+class SimplifyReshape {
+ public:
+ SimplifyReshape() {
+ x_ = WildcardPattern(make_object<WildcardPatternNode>());
+ auto reshape1 = AltPattern(ExprPattern(reshape_op),
ExprPattern(reverse_reshape_op));
+ auto reshape2 = AltPattern(ExprPattern(reshape_op),
ExprPattern(reverse_reshape_op));
+ pattern_ = CallPattern(reshape1, {CallPattern(reshape2, {x_}, Attrs{},
{})}, Attrs{}, {});
+ }
+
+ Expr callback(const Expr& pre, const Expr& post, const Map<DFPattern,
Array<Expr>>& node_map) {
+ auto x = node_map[x_][0];
+ bool const_shape = true;
+ Array<Integer> newshape;
+ for (auto dim : Downcast<TensorType>(pre->checked_type())->shape) {
+ if (dim.as<IntImmNode>() == nullptr) {
+ const_shape = false;
+ break;
+ }
+ newshape.push_back(Downcast<Integer>(dim));
+ }
+ if (const_shape) {
+ return MakeReshape(x, newshape);
+ }
+ return post;
+ }
+
+ DFPattern pattern() const { return pattern_; }
+
+ private:
+ /*! \brief Pattern input */
+ DFPattern x_;
+ /*! \brief Pattern for consecutive reshape or reverse_reshape ops */
+ DFPattern pattern_;
+};
+
+/*!
+ * \brief ExprSimplifier simplifies the Relay expression.
+ */
+class ExprSimplifier {
+ public:
+ ExprSimplifier() {
+ auto reshape_func = [this](TVMArgs args, TVMRetValue* rv) {
+ Expr pre = args[0];
+ Expr post = args[1];
+ Map<DFPattern, Array<Expr>> node_map = args[2];
+ *rv = simplify_reshape_.callback(pre, post, node_map);
+ };
+ callbacks_.push_back(DFPatternCallback(simplify_reshape_.pattern(),
PackedFunc(reshape_func),
Review comment:
Maybe have SimplifyReshape directly inherit DFPatternCallback? You could
fold this directly into that and keep it out of the main Simplifier.
##########
File path: tests/python/relay/test_dataflow_pattern.py
##########
@@ -599,6 +599,7 @@ def test_rewrite():
class TestRewrite(DFPatternCallback):
def __init__(self):
+ super(TestRewrite, self).__init__()
Review comment:
Thanks for updating these!
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]