zhiics commented on a change in pull request #4570: [relay] Relay annotation and partitioning for external compilers URL: https://github.com/apache/incubator-tvm/pull/4570#discussion_r361727260
########## File path: src/relay/pass/partition_graph.cc ########## @@ -0,0 +1,376 @@ +/* + * 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/partition.cc + * + * \brief Partition an input function into multiple functions according based + * on the inserted annotation nodes (i.e. compiler_begin and compiler_end). + * These nodes are used as boundaries to partition the Relay function into + * multiple regions that can be offloaded to different accelerators/backends. + * + * Each of these paritioned functions, a.k.a subgraphs, will be viewed as + * external functions, and they will use the provided compiler for codegen. + */ + +#include <tvm/relay/analysis.h> +#include <tvm/relay/attrs/annotation.h> +#include <tvm/relay/expr.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/relay/transform.h> + +#include <string> +#include <unordered_map> +#include <unordered_set> +#include <utility> +#include <vector> + +namespace tvm { +namespace relay { +namespace partitioning { + +/*! + * \brief The subgraph properties for partitioning. + */ +struct Subgraph { + /*! \brief The subgraph ID. */ + int id; + + /*! \brief The input arguments of this subgraph. */ + std::vector<std::pair<Var, Expr>> args; + + /*! \brief Nodes in this subgraph. */ + std::unordered_set<Expr, ExprHash, ExprEqual> nodes; +}; + +/*! + * \brief The checker that verifies if a Relay program is annotated correctly + * for partitioning. + */ +class AnnotationChecker : public ExprVisitor { + public: + bool Check() { + if (!this->found_start && !this->found_end) { + LOG(WARNING) << "No compiler annotation found"; + } else if (!this->found_start) { + LOG(ERROR) << "compiler_begin annotation is missing"; + return false; + } else if (!this->found_end) { + LOG(ERROR) << "compiler_end annotation is missing"; + return false; + } + return true; + } + + void VisitExpr_(const CallNode* call) final { + auto op_node = call->op.as<OpNode>(); + if (op_node == nullptr || call->attrs.as<CompilerAttrs>() == nullptr) { + return; + } else if (GetRef<Op>(op_node) == Op::Get("annotation.compiler_begin")) { Review comment: Please see #4594, I sent a PR to refactor all places. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services