expr_builder.cc 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright 2020 Huawei Technologies Co., Ltd
 *
 * Licensed 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.
 */
#include <sstream>
L
LuoYin 已提交
17
#include <tvm/operation.h>
18 19 20
#include "base/expr_builder.h"

namespace akg {
L
LuoYin 已提交
21 22 23 24 25 26 27 28 29 30 31 32
air::Expr UTExprBuilder::IntImm(int64_t value, air::DataType dtype) {
  return air::IntImm::make(dtype, value);
}

air::Expr UTExprBuilder::UIntImm(uint64_t value, air::DataType dtype) {
  return air::ir::UIntImm::make(dtype, value);
}

air::Expr UTExprBuilder::BoolImm(bool value) {
  return air::ir::UIntImm::make(air::Bool(), value ? 1 : 0);
}

33 34
air::Array<air::Expr> UTExprBuilder::CreateShape(const std::vector<int32_t> &shapes) {
  air::Array<air::Expr> res;
35
  for (int32_t shape : shapes) {
36
    air::Integer imm = air::IntImm::make(air::Int(32), shape);
37 38 39 40 41
    res.push_back(imm);
  }
  return res;
}

42 43
air::Var UTExprBuilder::CreateVar(const std::string &name) {
  return air::Var(name);
44 45
}

46 47
air::Array<air::Expr> UTExprBuilder::CreateVars(const std::vector<std::string> &names) {
  air::Array<air::Expr> vars;
48 49 50 51 52 53
  for (const std::string &name : names) {
    vars.push_back(std::move(CreateVar(name)));
  }
  return vars;
}

L
LuoYin 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
air::Region UTExprBuilder::CreateRegion(const std::vector<int32_t> &shapes) {
  air::Region region;
  for (int32_t shape : shapes) {
    region.push_back(CreateRange(0, shape));
  }
  return region;
}

air::Region UTExprBuilder::CreateRegion(const air::Array<air::Expr> &shapes) {
  air::Region region;
  for (const air::Expr &shape : shapes) {
    region.push_back(air::Range::make_by_min_extent(IntImm(0), shape));
  }
  return region;
}

air::Range UTExprBuilder::CreateRange(int32_t min, int32_t max) {
  air::Integer imm_min = air::IntImm::make(air::Int(32), min);
  air::Integer imm_max = air::IntImm::make(air::Int(32), max);
  return air::Range(std::move(imm_min), std::move(imm_max));
}

76
air::Operation UTExprBuilder::PlaceholderOpNode(
77 78
    const std::string &name,
    const std::vector<int32_t> &shapes,
79 80 81
    air::DataType dtype) {
  air::Array<air::Expr> expr_shapes = CreateShape(shapes);
  return air::PlaceholderOpNode::make(name, expr_shapes, dtype);
82 83
}

84
air::Expr UTExprBuilder::TensorElement(
85 86 87
    const std::string &name,
    const std::vector<int32_t> &shapes,
    const std::vector<std::string> &axis_names,
88 89
    air::DataType dtype) {
  return air::ir::Call::make(
90 91 92
      dtype,                                   // type
      name,                                    // name
      CreateVars(axis_names),                  // args
93
      air::ir::Call::Halide,                  // call_type
94 95 96 97
      PlaceholderOpNode(name, shapes, dtype),  // func,
      0);                                      // value_index
}

L
LuoYin 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
air::Expr UTExprBuilder::ElementOf(
    const air::Operation &op,
    const std::vector<std::string> &axis_names) {
  if (op->template IsInstance<air::PlaceholderOpNode>()) {
    return ElementOfPlaceholderOp(op, axis_names);
  } else {
    CHECK(false);
    return air::ir::Any::make();
  }
}

air::Expr UTExprBuilder::ElementOfPlaceholderOp(
    const air::Operation &op,
    const std::vector<std::string> &axis_names) {
  const air::PlaceholderOpNode *node = op.as<const air::PlaceholderOpNode>();
  CHECK(node);
  return air::ir::Call::make(
      node->dtype,
      node->name,
      CreateVars(axis_names),
      air::ir::Call::Halide,
      op,
      0);
}
air::Expr UTExprBuilder::CreateCall(
  const air::ir::FunctionRef func,
  air::Array<air::Expr> args,
  air::ir::Call::CallType call_type,
  int value_index) {
  air::DataType type = air::Float(16);
  const air::OperationNode *node_op = func.as<air::OperationNode>();
  CHECK(node_op);
  std::string name = node_op->name;
  const air::PlaceholderOpNode *node_placeholder = func.as<air::PlaceholderOpNode>();
  if (node_placeholder != nullptr) {
    type = node_placeholder->dtype;
  }
  return air::ir::Call::make(type, name, args, call_type, func, value_index);
}

air::Tensor UTExprBuilder::CreateTensorByPlaceholder(const air::Operation op) {
  const air::PlaceholderOpNode *node = op.as<air::PlaceholderOpNode>();
  CHECK(node);
  return air::TensorNode::make(
      node->shape,
      node->dtype,
      op,
      0);
}

148 149 150 151 152 153 154 155 156 157 158
UTTensorElementHelper::UTTensorElementHelper(const std::vector<int32_t> &shapes,
                                             const std::string &axis_name_prefix)
    : shapes_(shapes), axis_name_prefix_(axis_name_prefix) {
  std::stringstream ss;
  for (size_t i = 0; i < shapes_.size(); i++) {
    ss << axis_name_prefix_ << i;
    axis_names_.push_back(ss.str());
    ss.str("");
  }
}

159
air::Expr UTTensorElementHelper::Elem(const std::string &name,
L
LuoYin 已提交
160 161
                                      uint32_t dim,
                                      air::DataType dtype) const {
162 163 164 165 166 167 168 169
  uint32_t start = shapes_.size() - dim;
  return UTExprBuilder::TensorElement(
      name,
      std::vector<int32_t>(shapes_.begin() + start, shapes_.end()),
      std::vector<std::string>(axis_names_.begin() + start, axis_names_.end()),
      dtype);
}
}  // namespace akg