expr.cc 10.2 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 25 26 27 28 29 30 31 32 33
/*!
 *  Copyright (c) 2018 by Contributors
 * \file src/tvm/ir/expr.cc
 * \brief The expression AST nodes of Relay.
 */
#include <tvm/relay/expr.h>

namespace tvm {
namespace relay {

using tvm::IRPrinter;
using namespace tvm::runtime;

Constant ConstantNode::make(runtime::NDArray data) {
34
  NodePtr<ConstantNode> n = make_node<ConstantNode>();
35 36 37 38
  n->data = std::move(data);
  return Constant(n);
}

39 40
TVM_REGISTER_NODE_TYPE(ConstantNode);

41
TVM_REGISTER_API("relay._make.Constant")
42
.set_body_typed(ConstantNode::make);
43 44

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
45
.set_dispatch<ConstantNode>([](const ConstantNode* node, tvm::IRPrinter* p) {
46 47 48 49
    const PackedFunc* fprint = Registry::Get("relay._constant_repr");
    CHECK(fprint) << "unable to find printing function for constants";
    std::string data = (*fprint)(GetRef<Constant>(node));
    p->stream << "Constant(" << data << ")";
50
  });
51 52 53 54 55

TensorType ConstantNode::tensor_type() const {
  auto dtype = TVMType2Type(data->dtype);
  Array<tvm::Expr> shape;
  for (int i = 0; i < data->ndim; i++) {
56 57 58 59
    CHECK_LE(data->shape[i], std::numeric_limits<int32_t>::max());
    CHECK_GE(data->shape[i], std::numeric_limits<int32_t>::min());
    shape.push_back(
        tvm::ir::IntImm::make(Int(32), data->shape[i]));
60 61 62 63 64 65
  }

  return TensorTypeNode::make(shape, dtype);
}

Tuple TupleNode::make(tvm::Array<relay::Expr> fields) {
66
  NodePtr<TupleNode> n = make_node<TupleNode>();
67 68 69 70
  n->fields = std::move(fields);
  return Tuple(n);
}

71 72
TVM_REGISTER_NODE_TYPE(TupleNode);

73
TVM_REGISTER_API("relay._make.Tuple")
74
.set_body_typed(TupleNode::make);
75 76

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
77
.set_dispatch<TupleNode>([](const TupleNode* node, tvm::IRPrinter* p) {
78 79
    p->stream << "Tuple(" << node->fields << ")";
  });
80

81 82

Var VarNode::make(Id vid, Type type_annotation) {
83
  NodePtr<VarNode> n = make_node<VarNode>();
84
  n->vid = std::move(vid);
85
  n->type_annotation = std::move(type_annotation);
86 87 88
  return Var(n);
}

89 90 91 92 93 94
Var VarNode::make(std::string name_hint, Type type_annotation) {
  NodePtr<IdNode> n = make_node<IdNode>();
  n->name_hint = std::move(name_hint);
  return VarNode::make(Id(n), type_annotation);
}

95 96
TVM_REGISTER_NODE_TYPE(VarNode);

97
TVM_REGISTER_API("relay._make.Var")
98
.set_body_typed(static_cast<Var (*)(std::string, Type)>(VarNode::make));
99 100

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
101
.set_dispatch<VarNode>([](const VarNode* node, tvm::IRPrinter* p) {
102
    p->stream << "Var(" << node->name_hint();
103 104
    if (node->type_annotation.defined()) {
      p->stream << ", ty=";
105
      p->Print(node->type_annotation);
106 107
    }
    p->stream << ")";
108
  });
109 110

GlobalVar GlobalVarNode::make(std::string name_hint) {
111
  NodePtr<GlobalVarNode> n = make_node<GlobalVarNode>();
112 113 114 115
  n->name_hint = std::move(name_hint);
  return GlobalVar(n);
}

116 117
TVM_REGISTER_NODE_TYPE(GlobalVarNode);

118
TVM_REGISTER_API("relay._make.GlobalVar")
119
.set_body_typed(GlobalVarNode::make);
120 121

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
122
.set_dispatch<GlobalVarNode>([](const GlobalVarNode* node, tvm::IRPrinter* p) {
123 124
    p->stream << "GlobalVar(" << node->name_hint << ")";
  });
125 126


127 128
Function FunctionNode::make(tvm::Array<Var> params,
                            Expr body,
129
                            Type ret_type,
130 131
                            tvm::Array<TypeVar> type_params,
                            tvm::Attrs attrs) {
132
  NodePtr<FunctionNode> n = make_node<FunctionNode>();
133 134
  CHECK(params.defined());
  CHECK(type_params.defined());
135 136
  n->params = std::move(params);
  n->body = std::move(body);
137
  n->ret_type = std::move(ret_type);
138
  n->type_params = std::move(type_params);
139
  n->attrs = std::move(attrs);
140 141 142
  return Function(n);
}

143
FuncType FunctionNode::func_type_annotation() const {
144 145
  Array<Type> param_types;
  for (auto param : this->params) {
146 147 148
    Type param_type = (param->type_annotation.defined()) ? param->type_annotation
      : IncompleteTypeNode::make(Kind::kType);
    param_types.push_back(param_type);
149
  }
150 151 152 153

  Type ret_type = (this->ret_type.defined()) ? this->ret_type
    : IncompleteTypeNode::make(Kind::kType);
  return FuncTypeNode::make(param_types, ret_type, this->type_params, {});
154 155
}

156 157 158 159 160 161
bool FunctionNode::IsPrimitive() const {
  NodeRef res = FunctionGetAttr(GetRef<Function>(this), "Primitive");
  const ir::IntImm* pval = res.as<ir::IntImm>();
  return pval && pval->value != 0;
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
NodeRef FunctionGetAttr(const Function& func, const std::string& key) {
  if (!func->attrs.defined()) { return NodeRef(); }

  const DictAttrsNode* dict_attrs = func->attrs.as<DictAttrsNode>();
  CHECK(dict_attrs);
  auto it = dict_attrs->dict.find(key);
  if (it != dict_attrs->dict.end()) {
    return (*it).second;
  } else {
    return NodeRef();
  }
}

Function FunctionSetAttr(const Function& func, const std::string& key, const NodeRef& data) {
  const DictAttrsNode* dattrs = func->attrs.as<DictAttrsNode>();
  Attrs func_attrs;
  if (dattrs) {
    Map<std::string, NodeRef> dict = dattrs->dict;
    dict.Set(key, data);
    func_attrs = DictAttrsNode::make(dict);
  } else {
    Map<std::string, NodeRef> dict = {{key, data}};
    func_attrs = DictAttrsNode::make(dict);
  }

  return FunctionNode::make(
    func->params,
    func->body,
    func->ret_type,
    func->type_params,
    func_attrs);
}

195 196
TVM_REGISTER_NODE_TYPE(FunctionNode);

197
TVM_REGISTER_API("relay._make.Function")
198
.set_body_typed(FunctionNode::make);
199 200

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
201 202
.set_dispatch<FunctionNode>([](const FunctionNode* node,
                                   tvm::IRPrinter* p) {
203
      p->stream << "FunctionNode(" << node->params << ", " << node->ret_type
204 205
                << ", " << node->body << ", " << node->type_params << ", "
                << node->attrs << ")";
206 207 208 209
});

Call CallNode::make(Expr op, Array<Expr> args, Attrs attrs,
                    Array<Type> type_args) {
210
  NodePtr<CallNode> n = make_node<CallNode>();
211 212 213 214 215 216 217
  n->op = std::move(op);
  n->args = std::move(args);
  n->attrs = std::move(attrs);
  n->type_args = std::move(type_args);
  return Call(n);
}

218 219
TVM_REGISTER_NODE_TYPE(CallNode);

220
TVM_REGISTER_API("relay._make.Call")
221
.set_body_typed(CallNode::make);
222 223

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
224
.set_dispatch<CallNode>([](const CallNode* node, tvm::IRPrinter* p) {
雾雨魔理沙 committed
225 226
    p->stream << "CallNode(" << node->op << ", " << node->args << ", "
              << node->attrs << ", " << node->type_args << ")";
227 228
});

229
Let LetNode::make(Var var, Expr value, Expr body) {
230
  NodePtr<LetNode> n = make_node<LetNode>();
231 232 233 234 235 236
  n->var = std::move(var);
  n->value = std::move(value);
  n->body = std::move(body);
  return Let(n);
}

237 238
TVM_REGISTER_NODE_TYPE(LetNode);

239
TVM_REGISTER_API("relay._make.Let")
240
.set_body_typed(LetNode::make);
241 242

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
243
.set_dispatch<LetNode>([](const LetNode* node, tvm::IRPrinter* p) {
244
  p->stream << "LetNode(" << node->var << ", " << node->value
245
            << ", " << node->body << ")";
246 247 248
});

If IfNode::make(Expr cond, Expr true_branch, Expr false_branch) {
249
  NodePtr<IfNode> n = make_node<IfNode>();
250 251 252 253 254 255
  n->cond = std::move(cond);
  n->true_branch = std::move(true_branch);
  n->false_branch = std::move(false_branch);
  return If(n);
}

256 257
TVM_REGISTER_NODE_TYPE(IfNode);

258 259
TVM_REGISTER_API("relay._make.If")
.set_body_typed(IfNode::make);
260 261

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
262
.set_dispatch<IfNode>([](const IfNode* node, tvm::IRPrinter* p) {
263
  p->stream << "IfNode(" << node->cond << ", " << node->true_branch
264
            << ", " << node->false_branch << ")";
265 266
});

267 268 269 270 271 272 273
TupleGetItem TupleGetItemNode::make(Expr tuple, int index) {
  NodePtr<TupleGetItemNode> n = make_node<TupleGetItemNode>();
  n->tuple = std::move(tuple);
  n->index = index;
  return TupleGetItem(n);
}

274 275
TVM_REGISTER_NODE_TYPE(TupleGetItemNode);

276 277
TVM_REGISTER_API("relay._make.TupleGetItem")
.set_body_typed(TupleGetItemNode::make);
278 279 280 281 282 283

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<TupleGetItemNode>([](const TupleGetItemNode* node, tvm::IRPrinter* p) {
  p->stream << "TupleGetItemNode(" << node->tuple << ", " << node->index << ")";
});

284 285 286 287 288
RefCreate RefCreateNode::make(Expr value) {
  NodePtr<RefCreateNode> n = make_node<RefCreateNode>();
  n->value = std::move(value);
  return RefCreate(n);
}
289

290 291
TVM_REGISTER_NODE_TYPE(RefCreateNode);

292 293
TVM_REGISTER_API("relay._make.RefCreate")
.set_body_typed(RefCreateNode::make);
294 295 296 297 298 299 300 301 302 303 304 305

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<RefCreateNode>([](const RefCreateNode* node, tvm::IRPrinter* p) {
  p->stream << "RefCreateNode(" << node->value << ")";
});

RefRead RefReadNode::make(Expr ref) {
  NodePtr<RefReadNode> n = make_node<RefReadNode>();
  n->ref = std::move(ref);
  return RefRead(n);
}

306 307
TVM_REGISTER_NODE_TYPE(RefReadNode);

308
TVM_REGISTER_API("relay._make.RefRead")
309
.set_body_typed(RefReadNode::make);
310 311 312 313

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<RefReadNode>([](const RefReadNode* node, tvm::IRPrinter* p) {
  p->stream << "RefReadNode(" << node->ref << ")";
314 315
});

316 317 318 319 320 321 322
RefWrite RefWriteNode::make(Expr ref, Expr value) {
  NodePtr<RefWriteNode> n = make_node<RefWriteNode>();
  n->ref = std::move(ref);
  n->value = std::move(value);
  return RefWrite(n);
}

323 324
TVM_REGISTER_NODE_TYPE(RefWriteNode);

325
TVM_REGISTER_API("relay._make.RefWrite")
326
.set_body_typed(RefWriteNode::make);
327 328 329 330 331 332 333

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<RefWriteNode>([](const RefWriteNode* node, tvm::IRPrinter* p) {
  p->stream << "RefWriteNode(" << node->ref << ", " << node->value << ")";
});

TVM_REGISTER_API("relay._expr.TempExprRealize")
334
.set_body_typed<Expr(TempExpr)>([](TempExpr temp) {
335
  return temp->Realize();
336
});
337

338 339
}  // namespace relay
}  // namespace tvm