junrushao1994 commented on a change in pull request #5962: URL: https://github.com/apache/incubator-tvm/pull/5962#discussion_r449807345
########## File path: src/ansor/compute_dag.cc ########## @@ -0,0 +1,505 @@ +/* + * 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 ansor/compute_dag.cc + * \brief Compute declaration graph and its related analysis tools. + */ + +#include "compute_dag.h" + +#include <tvm/runtime/registry.h> +#include <tvm/te/operation.h> +#include <tvm/te/schedule.h> +#include <tvm/te/schedule_pass.h> +#include <tvm/tir/stmt_functor.h> + +#include <algorithm> +#include <queue> +#include <unordered_map> +#include <unordered_set> +#include <vector> + +#include "loop_state.h" +#include "utils.h" + +namespace tvm { +namespace ansor { + +using namespace tvm::tir; + +TVM_REGISTER_NODE_TYPE(ComputeDAGNode); + +// Topo-sort ops from tensors according to their read-write relations. +// Results are stored in ops +void TopoSortOps(const Array<te::Tensor>& tensors, Array<te::Operation>* ops) { + std::unordered_map<const te::OperationNode*, int> degree; + std::unordered_map<const te::OperationNode*, std::vector<const te::OperationNode*> > edge_set; + std::unordered_map<const te::OperationNode*, int> priority; + std::unordered_set<const te::OperationNode*> visited; + + // traverse to build edge_set and count degree + std::vector<const te::OperationNode*> stack; + stack.reserve(tensors.size()); + for (const auto& x : tensors) { + stack.push_back(x->op.operator->()); + } + + int ct = 0; + while (!stack.empty()) { + const te::OperationNode* op = stack.back(); + stack.pop_back(); + if (visited.count(op)) { + continue; + } + + priority[op] = ct; + ct++; + visited.insert(op); + + if (op->IsInstance<te::PlaceholderOpNode>()) { + degree[op] = 0; + } else if (auto cop = GetRef<te::Operation>(op).as<te::ComputeOpNode>()) { + const Array<te::Tensor>& input_tensors = cop->InputTensors(); + degree[op] = input_tensors.size(); + for (const auto& ten : input_tensors) { + edge_set[ten->op.operator->()].push_back(op); + stack.push_back(ten->op.operator->()); + } + } else { + LOG(FATAL) << "Unsupported op " << GetRef<te::Operation>(op); + } + } + + // topo sort + ops->clear(); + + using Item = std::pair<const te::OperationNode*, int>; + auto cmp = [](const Item& left, const Item& right) { return left.second < right.second; }; + std::priority_queue<Item, std::vector<Item>, decltype(cmp)> queue(cmp); + for (const auto& iter : degree) { + if (iter.second == 0) { + queue.push(Item(iter.first, priority[iter.first])); + } + } + + ops->reserve(degree.size()); + while (!queue.empty()) { + Item item = queue.top(); + queue.pop(); + ops->push_back(GetRef<te::Operation>(item.first)); + for (const auto& dst : edge_set[item.first]) { + degree[dst] -= 1; + if (degree[dst] == 0) { + queue.push(Item(dst, priority[dst])); + } + } + } +} + +// Estimate number of float operations in an expression +class FlopEstimator : public ExprFunctor<double(const PrimExpr& n)> { + public: + double EstimateFlop(const Array<te::Operation>& ops) { + double ret = 0; + for (const auto& op : ops) { + if (auto pop = op.as<te::ComputeOpNode>()) { + double num_element = AxisLengthProd(pop->axis); + if (num_element == -1) { + fail = true; + break; + } + double op_per_element = 0; + for (const auto& x : pop->body) { + op_per_element += VisitExpr(x); + } + ret += num_element * op_per_element; + } else if (op->IsInstance<te::PlaceholderOpNode>()) { + {} // do nothing + } else { + LOG(FATAL) << "Invalid op type " << op; + } + } + + return fail ? -1 : ret; + } + + double VisitExpr_(const ReduceNode* op) final { + uint64_t num_iter = 1; + for (const auto& x : op->axis) { + if (auto imm = x->dom->extent.as<IntImmNode>()) { + num_iter *= imm->value; + } else { + fail = true; + num_iter = -1; + } + } + double body_flop = 0; + for (size_t i = 0; i < op->combiner->result.size(); ++i) { + body_flop += VisitExpr(op->combiner->result[i]); + body_flop += VisitExpr(op->source[i]); + } + return num_iter * body_flop; + } + + double VisitExpr_(const FloatImmNode* op) final { return 0.0; } Review comment: it's not part of flops imo ---------------------------------------------------------------- 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]
