altanh commented on a change in pull request #7677: URL: https://github.com/apache/tvm/pull/7677#discussion_r596283755
########## File path: src/relay/transforms/concretize_like.cc ########## @@ -0,0 +1,160 @@ +/* + * 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/dataflow_matcher.h> +#include <tvm/relay/transform.h> + +#include "pattern_utils.h" + +namespace tvm { +namespace relay { + +class ConcretizeLikeRewrite { + public: + ConcretizeLikeRewrite() { + concrete_map_[Op::Get("reshape_like")] = [](Expr data, Array<Integer> shape, DataType dtype) { + return MakeReshape(data, shape); + }; + concrete_map_[Op::Get("zeros_like")] = [](Expr data, Array<Integer> shape, DataType dtype) { + return MakeZeros(shape, dtype); + }; + concrete_map_[Op::Get("ones_like")] = [](Expr data, Array<Integer> shape, DataType dtype) { + return MakeOnes(shape, dtype); + }; + concrete_map_[Op::Get("collapse_sum_like")] = [](Expr data, Array<Integer> shape, + DataType dtype) { + ICHECK_LE(shape.size(), std::numeric_limits<int64_t>::max()); + static const Op& op = Op::Get("collapse_sum_to"); + auto attrs = make_object<InitOpAttrs>(); + auto cshape = + MakeConstantTensor(DataType::Int(32), {static_cast<int64_t>(shape.size())}, shape); + attrs->shape = shape; + return Call(op, {data, cshape}, Attrs(attrs)); + }; + concrete_map_[Op::Get("broadcast_to_like")] = [](Expr data, Array<Integer> shape, + DataType dtype) { + return MakeBroadCastTo(data, shape); + }; + + for (const auto& pr : concrete_map_) { + if (!op_pat_.defined()) { + op_pat_ = IsExpr(pr.first); + } else { + op_pat_ = op_pat_ || IsExpr(pr.first); + } + } Review comment: 1. thanks for the catch, this was left over from before 2. true, I'm not sure what the code style is for defining static variables like this but I'll try something ---------------------------------------------------------------- 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]
