icemelon9 commented on a change in pull request #6052: URL: https://github.com/apache/incubator-tvm/pull/6052#discussion_r454668508
########## 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: The reason that I didn't inherit directly from `DFPatternCallback` is because you need to create the pattern somewhere else as it's required in the `DFPatternCallback` constructor. ---------------------------------------------------------------- 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]
