comaniac commented on a change in pull request #5030: [RELAY] Added a AnnotatedRegion utility class URL: https://github.com/apache/incubator-tvm/pull/5030#discussion_r396604369
########## File path: src/relay/analysis/annotated_region_set.cc ########## @@ -0,0 +1,239 @@ +/* + * 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. + */ + +#include "annotated_region_set.h" + +#include <tvm/relay/expr.h> +#include <tvm/ir/error.h> + +#include <algorithm> +#include <unordered_map> +#include <vector> + + +namespace tvm { +namespace relay { + +AnnotatedRegion AnnotatedRegionSetNode::GetRegion(const Expr& expr) const { + for (auto candidate : regions_) { + if (candidate->nodes.find(expr) != candidate->nodes.end()) { + return candidate; + } + } + return AnnotatedRegion(nullptr); +} + +void AnnotatedRegionSetNode::MergeRegions(AnnotatedRegion region1, + AnnotatedRegion region2) { + if (region1 == region2) { + return; + } + + // Merge region 2 to region 1 and erase region 2. + region1->nodes.insert(region2->nodes.begin(), region2->nodes.end()); + for (auto arg : region2->ins) { + region1->ins.push_back(arg); + } + for (auto out : region2->outs) { + region1->outs.push_back(out); + } + // if any of the outputs of 2 are inputs of 1, they become internal nodes + // so remove them from outs/args + std::vector<Expr> args_to_remove; + for (const auto& arg : region1->ins) { + auto call = Downcast<Call>(arg); + auto it = std::find(region2->outs.begin(), region2->outs.end(), call->args[0]); + if (it != region2->outs.end()) { + args_to_remove.push_back(arg); + region1->outs.remove(*it); + } + } + for (const auto& arg : args_to_remove) { + region1->ins.remove(arg); + } + regions_.erase(region2); +} + +void AnnotatedRegionSetNode::AddToRegion(AnnotatedRegion region, const Expr& expr) { + auto region2 = GetRegion(expr); + if (region2.defined()) { + MergeRegions(region, region2); + } else { + region->nodes.insert(expr); + } +} + +AnnotatedRegion AnnotatedRegionSetNode::MakeRegion() { + auto ret = regions_.emplace(AnnotatedRegion()); + return *ret.first; +} + +class AnnotatedRegionSet::Creator : public ExprVisitor { + public: + Creator(const Op ®ion_begin_op, const Op ®ion_end_op) : + begin_op_(region_begin_op), end_op_(region_end_op) {} + + AnnotatedRegionSet Create(const Expr &expr) { + VisitExpr(expr); + return std::move(region_set_); + } + + void VisitExpr_(const CallNode *call) { + auto op_node = call->op.as<OpNode>(); + + if (op_node == nullptr || call->attrs.as<CompilerAttrs>() == nullptr) { + // Propagate region to arguments + auto region = region_set_->GetRegion(GetRef<Call>(call)); + if (region.defined()) { + for (auto arg : call->args) { + region_set_->AddToRegion(region, arg); + } + } + } else if (call->op == begin_op_) { + // The annotation node is inserted on edge so it must have only one argument. + CHECK_EQ(call->args.size(), 1U); + + auto region = region_set_->GetRegion(GetRef<Call>(call)); + if (!region.defined()) { + throw Error(ErrorBuilder() + << "Cannot find the corresponding region for start annotation:\n" + << AsText(GetRef<Call>(call), false)); + } + region->ins.push_back(GetRef<Call>(call)); + } else { + CHECK_EQ(call->op, end_op_); + // The annotation node is inserted on edge so it must have only one argument. + CHECK_EQ(call->args.size(), 1U); + + // Check if the argument already belongs to a region + auto region = region_set_->GetRegion(call->args[0]); + if (!region.defined()) { + region = region_set_->MakeRegion(); + region->nodes.insert(call->args[0]); + region->id = region_id_++; Review comment: We should maintain this ID in `MakeRegion`. ---------------------------------------------------------------- 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] With regards, Apache Git Services
