comaniac commented on a change in pull request #7731: URL: https://github.com/apache/tvm/pull/7731#discussion_r600673972
########## File path: src/relay/transforms/concretize_like.cc ########## @@ -0,0 +1,182 @@ +/* + * 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 concretize_like.cc + * \brief Converts `*_like` operators to their explicit shape equivalent (e.g. `zeros_like(x, y)` to + * `zeros(x, y.shape)`), when the target shape is concrete. This removes unnecessary dependencies + * and can enable more opportunities for operator fusion. + */ + +#include <tvm/relay/transform.h> + +#include "pattern_utils.h" +#include "simplify_expr.h" + +namespace tvm { +namespace relay { + +class ConcretizeLikeRewrite : public DFPatternRewrite { + public: + explicit ConcretizeLikeRewrite(const Op& op) { + ICHECK(op->num_inputs == 1 || op->num_inputs == 2) + << "ConcretizeLike does not handle operators that aren't unary or binary, got: " << op; + like_pat_ = IsWildcard(); + data_pat_ = IsWildcard(); + if (op->num_inputs == 1) { + pattern_ = IsExpr(op)({like_pat_}); + } else { + pattern_ = IsExpr(op)({data_pat_, like_pat_}); + } + require_type_ = true; + } + + virtual bool Check(const Expr& pre, const Expr& post, + const Map<DFPattern, Array<Expr>>& node_map) const { + const CallNode* call_node = pre.as<CallNode>(); + ICHECK(call_node); + + if (!call_node->checked_type_.defined()) { + // TODO(@altanh): maybe because of the input being rewritten? Review comment: You could manually assign the `checked_type_` of input to the new created node in the rewrite callback. ########## File path: src/relay/transforms/concretize_like.cc ########## @@ -0,0 +1,182 @@ +/* + * 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 concretize_like.cc + * \brief Converts `*_like` operators to their explicit shape equivalent (e.g. `zeros_like(x, y)` to + * `zeros(x, y.shape)`), when the target shape is concrete. This removes unnecessary dependencies + * and can enable more opportunities for operator fusion. + */ + +#include <tvm/relay/transform.h> + +#include "pattern_utils.h" +#include "simplify_expr.h" + +namespace tvm { +namespace relay { + +class ConcretizeLikeRewrite : public DFPatternRewrite { + public: + explicit ConcretizeLikeRewrite(const Op& op) { + ICHECK(op->num_inputs == 1 || op->num_inputs == 2) + << "ConcretizeLike does not handle operators that aren't unary or binary, got: " << op; + like_pat_ = IsWildcard(); + data_pat_ = IsWildcard(); + if (op->num_inputs == 1) { + pattern_ = IsExpr(op)({like_pat_}); + } else { + pattern_ = IsExpr(op)({data_pat_, like_pat_}); + } + require_type_ = true; + } + + virtual bool Check(const Expr& pre, const Expr& post, + const Map<DFPattern, Array<Expr>>& node_map) const { + const CallNode* call_node = pre.as<CallNode>(); + ICHECK(call_node); + + if (!call_node->checked_type_.defined()) { + // TODO(@altanh): maybe because of the input being rewritten? + return false; + } + + const TensorTypeNode* like_ty = call_node->checked_type().as<TensorTypeNode>(); + ICHECK(like_ty) << "got non-Tensor *_like call type " << PrettyPrint(call_node->checked_type()); + + return true; + } + + virtual Expr Concretize(const Map<DFPattern, Array<Expr>>& node_map, Array<Integer> shape, + DataType dtype) const = 0; + + Expr Callback(const Expr& pre, const Expr& post, + const Map<DFPattern, Array<Expr>>& node_map) const override { + if (!Check(pre, post, node_map)) { + return post; + } + + const TensorTypeNode* like_ty = pre->checked_type().as<TensorTypeNode>(); + Array<Integer> cshape; + for (const auto& dim : like_ty->shape) { + if (const auto* imm = dim.as<IntImmNode>()) { + cshape.push_back(Integer(GetRef<IntImm>(imm))); + } else { + return post; Review comment: Add a comment saying like we do nothing here when the reference shape is not static. ########## File path: src/relay/transforms/simplify_expr.cc ########## @@ -22,44 +22,37 @@ * \brief A pass for simplifying the Relay expression. */ +#include "simplify_expr.h" + #include <tvm/relay/dataflow_matcher.h> #include <tvm/relay/expr.h> #include <tvm/relay/expr_functor.h> #include <tvm/relay/transform.h> #include <tvm/runtime/logging.h> +#include <utility> + #include "../op/tensor/transform.h" #include "pattern_utils.h" namespace tvm { namespace relay { -class SimplifyPattern { - public: - virtual Expr callback(const Expr& pre, const Expr& post, - const Map<DFPattern, Array<Expr>>& node_map) const = 0; - - DFPattern pattern() const { return pattern_; } - - protected: - /*! \brief Pattern for rewriting */ - DFPattern pattern_; -}; - /*! * \brief SimplifyReshape matches the pattern of consecutive reshape or reverse_reshape ops, * and merges into one reshape op. */ -class SimplifyReshape : public SimplifyPattern { +class SimplifyReshape : public DFPatternRewrite { public: SimplifyReshape() { x_ = IsWildcard(); auto reshape1 = IsOp("reshape") || IsOp("contrib_reverse_reshape"); auto reshape2 = IsOp("reshape") || IsOp("contrib_reverse_reshape"); pattern_ = reshape1({reshape2({x_})}); + require_type_ = true; Review comment: I think it should be fine to set this variable as true by default in `DFPatternRewrite` so that you could get rid of all of these statements. The case that we don't need type checking should be rare. ########## File path: src/relay/transforms/concretize_like.cc ########## @@ -0,0 +1,182 @@ +/* + * 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 concretize_like.cc + * \brief Converts `*_like` operators to their explicit shape equivalent (e.g. `zeros_like(x, y)` to + * `zeros(x, y.shape)`), when the target shape is concrete. This removes unnecessary dependencies + * and can enable more opportunities for operator fusion. + */ + +#include <tvm/relay/transform.h> + +#include "pattern_utils.h" +#include "simplify_expr.h" + +namespace tvm { +namespace relay { + +class ConcretizeLikeRewrite : public DFPatternRewrite { + public: + explicit ConcretizeLikeRewrite(const Op& op) { + ICHECK(op->num_inputs == 1 || op->num_inputs == 2) + << "ConcretizeLike does not handle operators that aren't unary or binary, got: " << op; + like_pat_ = IsWildcard(); + data_pat_ = IsWildcard(); + if (op->num_inputs == 1) { + pattern_ = IsExpr(op)({like_pat_}); + } else { + pattern_ = IsExpr(op)({data_pat_, like_pat_}); + } + require_type_ = true; + } + + virtual bool Check(const Expr& pre, const Expr& post, + const Map<DFPattern, Array<Expr>>& node_map) const { + const CallNode* call_node = pre.as<CallNode>(); + ICHECK(call_node); + + if (!call_node->checked_type_.defined()) { + // TODO(@altanh): maybe because of the input being rewritten? + return false; + } + + const TensorTypeNode* like_ty = call_node->checked_type().as<TensorTypeNode>(); + ICHECK(like_ty) << "got non-Tensor *_like call type " << PrettyPrint(call_node->checked_type()); + + return true; + } + + virtual Expr Concretize(const Map<DFPattern, Array<Expr>>& node_map, Array<Integer> shape, + DataType dtype) const = 0; + + Expr Callback(const Expr& pre, const Expr& post, + const Map<DFPattern, Array<Expr>>& node_map) const override { + if (!Check(pre, post, node_map)) { + return post; + } Review comment: Because of this use case, I'm wondering if `Check` should always be safe to return (i.e., no `ICHECK`)? -- 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]
