canonicalize_ops.cc 2.7 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

20 21 22 23 24
/*!
 * \file canonicalize_ops.cc
 * \brief Canonicalize special operators to basic operators.
    This can simplify latter analysis. (e.g. Expand bias_add to expand_dims and broadcast_add.)
 */
Zhi committed
25
#include <tvm/relay/analysis.h>
26
#include <tvm/relay/expr_functor.h>
27
#include <tvm/relay/op.h>
28
#include <tvm/relay/attrs/nn.h>
29
#include <tvm/relay/transform.h>
30 31 32 33 34
#include "pattern_util.h"

namespace tvm {
namespace relay {

35
class BiasAddSimplifier : public ExprRewriter {
36
 public:
37 38
  BiasAddSimplifier() : bias_add_op_(Op::Get("nn.bias_add")) {}

39 40
  Expr Rewrite_(const CallNode* n, const Expr& post) override {
    auto new_n = post;
41
    if (n->op == bias_add_op_) {
42 43 44 45
      Call call = Downcast<Call>(new_n);
      CHECK_EQ(call->args.size(), 2);
      const BiasAddAttrs* param = call->attrs.as<BiasAddAttrs>();

46
      auto ttype = n->args[0]->type_as<TensorTypeNode>();
47
      size_t n_dim = ttype->shape.size();
48 49 50 51 52
      int axis = param->axis;
      if (axis < 0) {
        axis += n_dim;
      }
      Expr expanded_bias = ExpandBiasToMatchAxis(call->args[1], n_dim, {axis});
53 54 55 56 57 58
      Expr ret = Add(call->args[0], expanded_bias);
      ret->checked_type_ = n->checked_type_;
      return ret;
    }
    return new_n;
  }
59 60 61 62

 private:
  // Cache the bias_add for equivalence checking.
  const Op& bias_add_op_;
63 64 65
};

Expr CanonicalizeOps(const Expr& e) {
66 67
  auto rewriter = BiasAddSimplifier();
  return PostOrderRewrite(e, &rewriter);
68 69
}

70 71 72
namespace transform {

Pass CanonicalizeOps() {
73 74
  runtime::TypedPackedFunc<Function(Function, IRModule, PassContext)> pass_func =
    [=](Function f, IRModule m, PassContext pc) {
75 76 77
    return Downcast<Function>(CanonicalizeOps(f));
  };
  return CreateFunctionPass(pass_func, 3, "CanonicalizeOps",
78
                            {tir::StringImmNode::make("InferType")});
79 80
}

81
TVM_REGISTER_GLOBAL("relay._transform.CanonicalizeOps")
82 83 84 85
.set_body_typed(CanonicalizeOps);

}  // namespace transform

86 87
}  // namespace relay
}  // namespace tvm