From commits-return-9498-archive-asf-public=cust-asf.ponee.io@tvm.apache.org Wed Mar 25 17:58:20 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 0483A18065D for ; Wed, 25 Mar 2020 18:58:19 +0100 (CET) Received: (qmail 39865 invoked by uid 500); 25 Mar 2020 17:58:19 -0000 Mailing-List: contact commits-help@tvm.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tvm.apache.org Delivered-To: mailing list commits@tvm.apache.org Received: (qmail 39856 invoked by uid 99); 25 Mar 2020 17:58:19 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Mar 2020 17:58:19 +0000 From: GitBox To: commits@tvm.apache.org Subject: [GitHub] [incubator-tvm] comaniac commented on a change in pull request #5134: [RELAY] Add MergeCompilerRegions pass Message-ID: <158515909928.14149.1272460825514837292.gitbox@gitbox.apache.org> References: In-Reply-To: Date: Wed, 25 Mar 2020 17:58:19 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit comaniac commented on a change in pull request #5134: [RELAY] Add MergeCompilerRegions pass URL: https://github.com/apache/incubator-tvm/pull/5134#discussion_r398044599 ########## File path: src/relay/transforms/merge_compiler_regions.cc ########## @@ -0,0 +1,352 @@ +/* + * 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/merge_compiler_regions.cc + * + * \brief After operators have been annotated with the targets that support + * them, this pass creates regions of the operators for each target. It + * is guaranteed that the regions will have a topological ordering so that + * no data dependency issues exist. + * + * This pass only introduces annotations to indicate the regions. + * partition_graph must subsequently be called to lift these regions out + * as external functions. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "../analysis/annotated_region_set.h" + + +namespace tvm { +namespace relay { +namespace partitioning { + +// Cache compiler_begin and compiler_end annotation ops for equivalence check to +// reduce registry lookup overhead. +static const Op& compiler_begin_op = Op::Get("annotation.compiler_begin"); +static const Op& compiler_end_op = Op::Get("annotation.compiler_end"); + +/*! \brief This is a pre-requisite pass to merge-supported pass. + * The AnnotateRestDefault pass will put "default" Compiler Annotations to + * nodes that are not annotated already. This is there to ensure that the + * user will not leave un-annotated nodes MergeCompilerRegions pass is run. + * Why? Because, MergeCompilerRegions pass assumes every node to be annotated. + */ +class AnnotateRestDefault : public ExprMutator { + public: + explicit AnnotateRestDefault(const Expr& expr) { + regions_ = AnnotatedRegionSet::Create(expr, compiler_begin_op, compiler_end_op); + } + + Expr Annotate(const Expr& expr) { + // Its a function that is being passed on to annotate + func_ = Downcast(expr); + + // Corner Case CC1 : If the last node does not belong + // to a region nede to add a compiler_end + auto region = regions_->GetRegion(func_->body); + auto mutated_expr = this->VisitExpr(expr); + if (!region.defined()) { + func_ = Downcast(mutated_expr); + // CC1 : add that compiler end after mutation + auto body = AddCompilerEnd_(func_->body); + func_ = Function(func_->params, body, + body->checked_type_, {}, DictAttrs()); + return Downcast(func_); + } else { + return mutated_expr; + } + } + + /*! \brief This function adds compiler ends to nodes that + * have a region AND they should not be arguments of the + * original function + * \param expr + * \return expr + */ + Expr AddCompilerEnd(const Expr& expr) { + auto region = regions_->GetRegion(expr); + auto visited_expr = VisitExpr(expr); + + // The compiler ends are added to nodes that does have a region + // AND they should not be arguments of the original function + if (!region.defined() && + std::find(func_->params.begin(), + func_->params.end(), visited_expr) + == func_->params.end()) { + return AddCompilerEnd_(visited_expr); + } else { + return visited_expr; + } + } + + Expr AddCompilerEnd_(const Expr& expr) { + const auto* end_op = + runtime::Registry::Get("relay.op.annotation._make.compiler_end"); + CHECK(end_op); + Expr end = (*end_op)(expr, target_); + return end; + } + + Expr VisitExpr_(const CallNode* call) final { + auto op_node = call->op.as(); + auto ret = GetRef(call); + + Array args; + + // Add compiler ends if the parent is supported + for (auto arg : call->args) { + args.push_back(AddCompilerEnd(arg)); + } + + if (op_node == nullptr || call->attrs.as() == nullptr) { + // Skip annotatation ops, only add default compiler to actual compute nodes + + auto region = regions_->GetRegion(ret); + if (!region.defined()) { + // if the current node does not belong to annotated region + // annotate the all incoming edges (args) + // with "default" compile_begin and compiler_end annotations. + tvm::Array compiler_begins; + for (auto arg : args) { + const auto* begin_op = + runtime::Registry::Get("relay.op.annotation._make.compiler_begin"); + CHECK(begin_op); + Expr begin = (*begin_op)(arg, target_); + compiler_begins.push_back(begin); + } + Expr update_call = Call(call->op, compiler_begins, call->attrs); + return update_call; + } else { + return Call(call->op, args, call->attrs); + } + } else { + return Call(call->op, args, call->attrs); + } + }; + + Expr VisitExpr_(const TupleNode *op) { + auto new_e = ExprMutator::VisitExpr_(op); + auto tup = Downcast(new_e); + Array new_fields; + for (auto field : tup->fields) { + new_fields.push_back(AddCompilerEnd(field)); + } + return Tuple(new_fields); + } + + Expr VisitExpr_(const TupleGetItemNode *op) { + auto new_e = ExprMutator::VisitExpr_(op); + auto get = Downcast(new_e); + return TupleGetItem( + AddCompilerEnd(get->tuple), + get->index); + } + + Expr VisitExpr_(const LetNode *op) { + auto new_e = ExprMutator::VisitExpr_(op); + auto let = Downcast(new_e); + return Let( + let->var, + AddCompilerEnd(let->value), + AddCompilerEnd(let->body)); + } + + Expr VisitExpr_(const IfNode *op) { + auto new_e = ExprMutator::VisitExpr_(op); + auto iff = Downcast(new_e); + return If( + AddCompilerEnd(iff->cond), + AddCompilerEnd(iff->true_branch), + AddCompilerEnd(iff->false_branch)); + } + + Expr VisitExpr_(const RefCreateNode *op) { + auto new_e = ExprMutator::VisitExpr_(op); + auto create = Downcast(new_e); + return RefCreate(AddCompilerEnd(create->value)); + } + + Expr VisitExpr_(const RefReadNode *op) { + auto new_e = ExprMutator::VisitExpr_(op); + auto read = Downcast(new_e); + return RefRead(AddCompilerEnd(read->ref)); + } + + Expr VisitExpr_(const RefWriteNode *op) { + auto new_e = ExprMutator::VisitExpr_(op); + auto write = Downcast(new_e); + return RefWriteNode::make( + AddCompilerEnd(write->ref), + AddCompilerEnd(write->value)); + } + + private: + AnnotatedRegionSet regions_; + const std::string target_ = "default"; + Function func_; +}; + +class MergeAnnotations : public ExprMutator { + public: + explicit MergeAnnotations(AnnotatedRegionSet regions) : regions_(regions) {} + + Expr VisitExpr_(const CallNode* call) final { + if (call->op == compiler_begin_op) { + if (call->args[0]->IsInstance()) { + auto arg = Downcast(call->args[0]); + if (arg->op == compiler_end_op) { + auto region1 = regions_->GetRegion(GetRef(call)); + auto region2 = regions_->GetRegion(arg); + if (region1 == region2) { + return ExprMutator::VisitExpr(arg->args[0]); + } + } + } + } + return ExprMutator::VisitExpr_(call); + } + + private: + AnnotatedRegionSet regions_; +}; + +class RegionMerger : public ExprVisitor { + public: + explicit RegionMerger(AnnotatedRegionSet regions) : regions_(regions) {} + + void VisitExpr_(const CallNode* call) final { + if (call->op == compiler_end_op) { + auto region = regions_->GetRegion(GetRef(call)); + // set the region target + auto compiler_attrs = call->attrs.as(); + region_targets_[region->GetID()] = compiler_attrs->compiler; + std::vector mergeable_regions; + // first look at the region args to determine the parent regions + for (const auto& arg : region->GetInputs()) { + // all args should be begin annotations + auto begin = Downcast(arg); + // the arguments of the begin annotations will be in the parent regions + auto parent_region = regions_->GetRegion(begin->args[0]); + // if there is no parent region, move on + if (!parent_region.defined()) continue; + // merge the parent region if it hasn't been done already + if (merged_regions_.find(parent_region->GetID()) == merged_regions_.end()) { + VisitExpr(begin->args[0]); + } + mergeable_regions.push_back(parent_region); + } + // region_restrictions_[region->id] = std::unordered_set(); + auto& region_restrictions = region_restrictions_[region->GetID()]; + for (const auto& parent_region : mergeable_regions) { + auto parent_restrictions = region_restrictions_[parent_region->GetID()]; + // add all the parent restrictions to the current region + region_restrictions.insert(parent_restrictions.begin(), + parent_restrictions.end()); + } + for (const auto& parent_region : mergeable_regions) { + bool merged = false; + // check the parent region has the same target + if (region_targets_[parent_region->GetID()] == compiler_attrs->compiler) { + // check the parent region isn't in the restrictions + if (region_restrictions.find(parent_region->GetID()) == region_restrictions.end()) { + // merge the parent region into the current region + regions_->MergeRegions(region, parent_region); + // update the restrictions of all other regions to reflect the change in id + for (const auto& r : regions_) { + auto& restrictions = region_restrictions_[r->GetID()]; + if (restrictions.find(parent_region->GetID()) != restrictions.end()) { + restrictions.erase(parent_region->GetID()); + restrictions.insert(region->GetID()); + } + } + merged = true; + } + } + // if the parent wasn't merged, add it as a restriction to the current region + if (!merged) + region_restrictions.insert(parent_region->GetID()); + } + merged_regions_.insert(region->GetID()); + } + ExprVisitor::VisitExpr_(call); + } + + private: + AnnotatedRegionSet regions_; + std::unordered_set merged_regions_; + std::map> region_restrictions_; + std::map region_targets_; +}; + + +Expr MergeCompilerRegions(const Expr& expr) { + // Annotate the nodes that are not annotated, if any. + AnnotateRestDefault anno_default(expr); + auto expr_all_annotated = anno_default.Annotate(expr); + + // Create regions using the annotations. + AnnotatedRegionSet regions = AnnotatedRegionSet::Create(expr_all_annotated, + compiler_begin_op, compiler_end_op); + + // By now, all the nodes have some sort of annotation. + // Region merger is ExprVisitor that will update the + // AnnotatedRegionSet. + RegionMerger merger(regions); + merger.VisitExpr(expr_all_annotated); + + // This will use updated (merged) + // AnnotatedRegionSet : regions_withdefault to remove annotations + // within each region Review comment: Unclear, especially the "regions_withdefault" part. ---------------------------------------------------------------- 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: users@infra.apache.org With regards, Apache Git Services