siju-samuel commented on a change in pull request #5134: [RELAY] Add MergeCompilerRegions pass URL: https://github.com/apache/incubator-tvm/pull/5134#discussion_r396873069
########## 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/pass/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 <tvm/relay/analysis.h> +#include <tvm/relay/attrs/annotation.h> +#include <tvm/relay/expr.h> +#include <tvm/ir/error.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/relay/transform.h> + +#include <string> +#include <unordered_map> +#include <unordered_set> +#include <vector> + +#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<Function>(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<Function>(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<Expr>(func_); + } else { + return mutated_expr; + } + } + + /*! \brief This function adds compiler ends to nodes nodes that Review comment: nodes nodes -> nodes ---------------------------------------------------------------- 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
