|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * Copyright (c) 2019 by Contributors |
| 22 | + * \file canonicalize_expr.cc |
| 23 | + * \brief Canonicalize an expression to make operator fusion more efficient. |
| 24 | + */ |
| 25 | +#include <tvm/relay/pass.h> |
| 26 | +#include <tvm/relay/expr_functor.h> |
| 27 | +#include <tvm/relay/attrs/nn.h> |
| 28 | +#include <tvm/relay/transform.h> |
| 29 | +#include "pattern_util.h" |
| 30 | +#include "pass_util.h" |
| 31 | + |
| 32 | +namespace tvm { |
| 33 | +namespace relay { |
| 34 | + |
| 35 | +// This pass finds upcast that is referred by multiple elemwise/broadcast operators, and creates a |
| 36 | +// copy of it in each branch such that after fusion the previous function have output with fewer |
| 37 | +// bits. |
| 38 | +class ExprCanonicalizer : public ExprMutator { |
| 39 | + public: |
| 40 | + Expr VisitExpr_(const CallNode* call) { |
| 41 | + static auto fpattern = Op::GetAttr<TOpPattern>("TOpPattern"); |
| 42 | + |
| 43 | + if (const OpNode* opnode = call->op.as<OpNode>()) { |
| 44 | + auto pattern = fpattern[GetRef<Op>(opnode)]; |
| 45 | + if (pattern <= kBroadcast) { |
| 46 | + Array<Expr> call_args = call->args; |
| 47 | + bool unchanged = true; |
| 48 | + for (size_t i = 0; i < call_args.size(); ++i) { |
| 49 | + Expr arg = call_args[i]; |
| 50 | + Expr new_arg = GetNewCallArg(arg); |
| 51 | + if (!arg.same_as(new_arg)) { |
| 52 | + call_args.Set(i, new_arg); |
| 53 | + unchanged = false; |
| 54 | + } |
| 55 | + } |
| 56 | + if (unchanged) { |
| 57 | + return GetRef<Expr>(call); |
| 58 | + } |
| 59 | + return CallNode::make(call->op, call_args, call->attrs, call->type_args); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + Expr new_expr = ExprMutator::VisitExpr_(call); |
| 64 | + return new_expr; |
| 65 | + } |
| 66 | + |
| 67 | + private: |
| 68 | + std::unordered_map<const Node*, size_t> ref_counter_; |
| 69 | + |
| 70 | + Expr GetNewCallArg(const Expr& e) { |
| 71 | + // if e is a upcast and ref count > 1, create an copy; otherwise call the default visitor |
| 72 | + |
| 73 | + static auto& cast = Op::Get("cast"); |
| 74 | + Expr new_expr = this->VisitExpr(e); |
| 75 | + |
| 76 | + if (const CallNode* call = e.as<CallNode>()) { |
| 77 | + if (call->op.same_as(cast)) { |
| 78 | + auto attrs = call->attrs.as<CastAttrs>(); |
| 79 | + const auto* from_type = call->args[0]->type_as<TensorTypeNode>(); |
| 80 | + CHECK(from_type); |
| 81 | + |
| 82 | + if (from_type->dtype.bits() < attrs->dtype.bits()) { |
| 83 | + if (++ref_counter_[call] > 1) { |
| 84 | + const CallNode* new_call = new_expr.as<CallNode>(); |
| 85 | + CHECK(new_call); |
| 86 | + CHECK(new_call->op.same_as(cast)); |
| 87 | + return CallNode::make(new_call->op, new_call->args, new_call->attrs, |
| 88 | + new_call->type_args); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return new_expr; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +Expr CanonicalizeExpr(const Expr& e) { |
| 98 | + return ExprCanonicalizer().Mutate(e); |
| 99 | +} |
| 100 | + |
| 101 | +TVM_REGISTER_API("relay._ir_pass.canonicalize_expr") |
| 102 | +.set_body_typed(CanonicalizeExpr); |
| 103 | + |
| 104 | +namespace transform { |
| 105 | + |
| 106 | +Pass CanonicalizeExpr() { |
| 107 | + runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func = |
| 108 | + [=](Function f, Module m, PassContext pc) { |
| 109 | + return Downcast<Function>(CanonicalizeExpr(f)); |
| 110 | + }; |
| 111 | + return CreateFunctionPass(pass_func, 3, "CanonicalizeExpr", |
| 112 | + {ir::StringImm::make("InferType")}); |
| 113 | +} |
| 114 | + |
| 115 | +TVM_REGISTER_API("relay._transform.CanonicalizeExpr") |
| 116 | +.set_body_typed(CanonicalizeExpr); |
| 117 | + |
| 118 | +} // namespace transform |
| 119 | + |
| 120 | +} // namespace relay |
| 121 | +} // namespace tvm |
0 commit comments