pass_manager.cc 20.9 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.
 */

Zhi committed
20 21 22 23 24
/*!
 * Copyright (c) 2019 by Contributors
 * \file src/relay/pass/pass_manager.cc
 * \brief Relay pass manager implementation.
 */
25
#include <dmlc/thread_local.h>
Zhi committed
26
#include <tvm/relay/expr_functor.h>
27
#include <tvm/relay/transform.h>
28 29 30 31 32
#include <tvm/runtime/device_api.h>

#include <algorithm>
#include <stack>
#include <unordered_set>
Zhi committed
33 34 35

namespace tvm {
namespace relay {
36
namespace transform {
Zhi committed
37 38 39

using tvm::IRPrinter;

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
namespace {

// TODO(zhiics) Maybe we can use PackedFunc here so that parameters can be
// handled because we need to register the pass for Python invocation anyway.
Pass GetPass(const std::string& pass_name) {
  if (pass_name == "InferType") {
    return InferType();
  } else if (pass_name == "AlterOpLayout") {
    return AlterOpLayout();
  } else if (pass_name == "CanonicalizeOps") {
    return CanonicalizeOps();
  } else if (pass_name == "CombineParallelConv2d") {
    return CombineParallelConv2D();
  } else if (pass_name == "DeadCodeElimination") {
    return DeadCodeElimination();
  } else if (pass_name == "EliminateCommonSubexpr") {
    return DeadCodeElimination();
  } else if (pass_name == "FoldConstant") {
    return FoldConstant();
  } else if (pass_name == "BackwardFoldScaleAxis") {
    return FoldScaleAxis();
  } else if (pass_name == "ForwardFoldScaleAxis") {
    return FoldScaleAxis();
  } else if (pass_name == "FoldScaleAxis") {
    return FoldScaleAxis();
  } else if (pass_name == "PartialEvaluate") {
    return SimplifyInference();
  } else if (pass_name == "SimplifyInference") {
    return SimplifyInference();
  } else if (pass_name == "ToANormalForm") {
    return ToANormalForm();
  } else if (pass_name == "ToGraphNormalForm") {
    return ToGraphNormalForm();
  } else {
    LOG(FATAL) << pass_name << " has not been registered yet." << "\n";
    return Pass(nullptr);
76
  }
77
}
78

79
}  // namespace
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

struct RelayPassContextThreadLocalEntry {
  /*! \brief The default pass context. */
  PassContext default_context;

  /*! \brief The current pass context. */
  std::stack<PassContext> context_stack;

  RelayPassContextThreadLocalEntry() {
    default_context = PassContext(make_node<PassContextNode>());
  }
};

/*! \brief Thread local store to hold the pass context. */
typedef dmlc::ThreadLocalStore<RelayPassContextThreadLocalEntry>
    RelayPassContextThreadLocalStore;

void PassContext::EnterWithScope() {
  RelayPassContextThreadLocalEntry* entry =
      RelayPassContextThreadLocalStore::Get();
  entry->context_stack.push(*this);
}

void PassContext::ExitWithScope() {
  RelayPassContextThreadLocalEntry* entry =
      RelayPassContextThreadLocalStore::Get();
  CHECK(!entry->context_stack.empty());
  CHECK(entry->context_stack.top().same_as(*this));
  entry->context_stack.pop();
}

PassContext PassContext::Current() {
  RelayPassContextThreadLocalEntry* entry =
      RelayPassContextThreadLocalStore::Get();
  if (!entry->context_stack.empty()) {
    return entry->context_stack.top();
  } else {
    return entry->default_context;
  }
}

121 122 123 124
PassContext PassContext::Create() {
  return PassContext(make_node<PassContextNode>());
}

Zhi committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
class ModulePass;

/*!
 * \brief Module-level passes are designed to implement global
 * analysis/optimizations, i.e. interprocedural optimizations (IPO), etc. Passes
 * at this level have the full control of a given Relay program including
 * addition and deletion of functions.
 */
class ModulePassNode : public PassNode {
 public:
  /* \brief The pass meta data.*/
  PassInfo pass_info;

  /*! \brief The pass function sketches the real optimization. For example,
   * we may need to perform dead code elimination on the module level. We could
   * implement the algorithm in the `pass_func` and let it run on a module. It
   * will then remove the dead code including the unused functions in the module.
   */
  runtime::TypedPackedFunc<Module(Module, PassContext)> pass_func;

  ModulePassNode() = default;

  void VisitAttrs(tvm::AttrVisitor* v) final {
    v->Visit("pass_info", &pass_info);
  }

  /*!
152
   * \brief Run a module pass on given pass context.
Zhi committed
153
   *
154 155
   * \param mod The module that an optimization pass is applied on.
   * \param mod The context that an optimization pass executes on.
Zhi committed
156 157 158
   *
   * \return Return the updated module.
   */
159
  Module operator()(const Module& mod, const PassContext& pass_ctx) const final;
Zhi committed
160 161 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 195 196

  /*!
   * \brief Get the pass information/meta data.
   */
  PassInfo Info() const { return pass_info; }

  TVM_DLL static ModulePass make(
      runtime::TypedPackedFunc<Module(Module, PassContext)> pass_func,
      PassInfo pass_info);

  static constexpr const char* _type_key = "relay.ModulePass";
  TVM_DECLARE_NODE_TYPE_INFO(ModulePassNode, PassNode);
};

RELAY_DEFINE_NODE_REF(ModulePass, ModulePassNode, Pass);

class FunctionPass;

/*!
 * \brief Function-level passes are used to implement various global
 * optimizations for a given Relay module. It fetches one function at a time
 * from the function list in the module for optimization.
 *
 * Note that the scope of passes at this level is a Relay function. Therefore,
 * we cannot add or delete a function through these passes as they are not aware
 * of the global information.
 */
class FunctionPassNode : public PassNode {
 public:
  /* \brief The pass meta data.*/
  PassInfo pass_info;

  /*! \brief The packed pass function sketches the real optimization. For
   * instance, we can implement a pass that works on a Relay function as a
   * `pass_func` and let it run on a given module. The same `pass_func` will
   * then be applied on each function in the module.
   */
197
  runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func;
Zhi committed
198 199 200 201 202 203 204 205

  FunctionPassNode() = default;

  void VisitAttrs(tvm::AttrVisitor* v) final {
    v->Visit("pass_info", &pass_info);
  }

  /*!
206
   * \brief Run a function pass on given pass context.
Zhi committed
207
   *
208 209
   * \param mod The module that an optimization pass is applied on.
   * \param mod The context that an optimization pass executes on.
Zhi committed
210 211 212
   *
   * \return Return the updated module.
   */
213
  Module operator()(const Module& mod, const PassContext& pass_ctx) const final;
Zhi committed
214 215 216 217 218 219 220

  /*!
   * \brief Get the pass information/meta data.
   */
  PassInfo Info() const { return pass_info; }

  TVM_DLL static FunctionPass make(
221
      runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func,
Zhi committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      PassInfo pass_info);

  static constexpr const char* _type_key = "relay.FunctionPass";
  TVM_DECLARE_NODE_TYPE_INFO(FunctionPassNode, PassNode);

 private:
  /*
   * \brief Check if a function should be skipped for optimization.
   *
   * \param func The target function to be checked.
   *
   * \return Return true if the function will be skipped, otherwise false.
   */
  bool SkipFunction(const Function& func) const;
};

RELAY_DEFINE_NODE_REF(FunctionPass, FunctionPassNode, Pass);

/*!
241
 * \brief The SequentialNode contains a set of passes that transform Relay
Zhi committed
242 243 244 245 246 247
 * programs from one AST to another semantically equivalent one.
 *
 * One example of this level of pass is that the pass manager needs to correctly
 * perform a host of optimizations with a given optimization level and disabled
 * passes.
 */
248
class SequentialNode : public PassNode {
Zhi committed
249 250 251 252
 public:
  /* \brief The pass meta data.*/
  PassInfo pass_info;

253 254
  /*! \brief A list of passes that used to compose a sequential pass. */
  tvm::Array<Pass> passes;
Zhi committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  void VisitAttrs(tvm::AttrVisitor* v) final {
    v->Visit("pass_info", &pass_info);
    v->Visit("passes", &passes);
  }

  /*!
   * \brief Get the pass information/meta data.
   */
  PassInfo Info() const { return pass_info; }

  /*!
   * \brief Add a pass to the pass list.
   *
   * \param pass The candidate pass to be added.
   */
  void AddPass(const Pass& pass) {
    passes.push_back(pass);
  }

  /*!
275 276 277 278 279 280
   * \brief Check if a pass is enabled.
   *
   * \param pass_name The name of an optimization/analysis pass.
   *
   * \return true if the pass is enabled. Otherwise, false.
   */
281
  bool PassEnabled(const std::string& pass_name) const;
282 283

  /*!
Zhi committed
284 285 286 287 288 289 290 291 292 293 294 295 296
   * \brief Resolve the pass dependency. It globs all required passes by
   *        a given pass and executes them.
   *
   * \param mod The module that an optimization pass runs on.
   *
   * \return The updated module after resolving pass dependencies.
   *
   * TODO(zhiics) Build a dependency graph among the passes using provided
   * metadata, i.e. required_passes. Likely, we can have a data structure, i.e.
   * PassInfo, to store the relevant information including the parent passes.
   */
  void ResolveDependency(const Module& mod);

297 298 299 300
  std::unordered_set<std::string> DisabledPasses(
      const Array<tvm::Expr>& disabled) const;

  std::unordered_set<std::string> RequiredPasses(
301
      const Array<tvm::Expr>& required) const;
Zhi committed
302 303 304 305 306 307 308

  /*!
   * \brief Perform optimizations on a series of passes. The aforementioned
   *        typical pass manager jobs could be done by it. This function could
   *        be overloaded to focus on different metrics, i.e. performance,
   *        memory footprint, etc.
   *
309 310
   * \param mod The module that these passes are applied on.
   * \param pass_ctx The context that these passes execute on.
Zhi committed
311 312 313
   *
   * \return Return the updated module.
   */
314
  Module operator()(const Module& mod, const PassContext& pass_ctx) const final;
Zhi committed
315

316 317
  static constexpr const char* _type_key = "relay.Sequential";
  TVM_DECLARE_NODE_TYPE_INFO(SequentialNode, PassNode);
Zhi committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
};

PassInfo PassInfoNode::make(int opt_level, std::string name,
                            tvm::Array<tvm::Expr> required) {
  auto pass_info = make_node<PassInfoNode>();
  pass_info->opt_level = opt_level;
  pass_info->name = std::move(name);
  pass_info->required = std::move(required);
  return PassInfo(pass_info);
}

ModulePass ModulePassNode::make(
    runtime::TypedPackedFunc<Module(Module, PassContext)> pass_func,
    PassInfo pass_info) {
  auto n = make_node<ModulePassNode>();
  n->pass_func = std::move(pass_func);
  n->pass_info = std::move(pass_info);
  return ModulePass(n);
}

// Module -> Module optimizations.
339 340
Module ModulePassNode::operator()(const Module& mod,
                                  const PassContext& pass_ctx) const {
Zhi committed
341
  PassInfo pass_info = Info();
342 343
  DLOG(INFO) << "Executing module pass : " << pass_info->name
             << " with opt level: " << pass_info->opt_level << "\n";
344

Zhi committed
345
  CHECK(mod.defined());
346 347 348 349 350 351 352 353 354 355 356 357
  Module updated_mod = mod;
  // Execute the required passes in a DFS way.
  // TODO(zhiics) We may need to pass validation to detect the cyclic
  // dependency.
  for (const auto& it : pass_info->required) {
    const auto* name = it.as<tvm::ir::StringImm>();
    CHECK(name);
    auto pass = GetPass(name->value);
    updated_mod = pass(updated_mod, pass_ctx);
  }

  updated_mod = pass_func(updated_mod, pass_ctx);
Zhi committed
358 359 360 361 362
  CHECK(updated_mod.defined());
  return updated_mod;
}

FunctionPass FunctionPassNode::make(
363
    runtime::TypedPackedFunc<Function(Function, Module, PassContext)> pass_func,
Zhi committed
364 365 366 367 368 369 370 371 372
    PassInfo pass_info) {
  auto n = make_node<FunctionPassNode>();
  n->pass_func = std::move(pass_func);
  n->pass_info = std::move(pass_info);
  return FunctionPass(n);
}

// Perform Module -> Module optimizations at the Function level.
// TODO(zhiics) Check and handle the required passes.
373 374
Module FunctionPassNode::operator()(const Module& mod,
                                    const PassContext& pass_ctx) const {
Zhi committed
375 376
  PassInfo pass_info = Info();
  CHECK(mod.defined());
377 378
  DLOG(INFO) << "Executing module pass : " << pass_info->name
             << " with opt level: " << pass_info->opt_level << "\n";
379 380 381 382 383 384 385 386 387 388 389 390 391

  Module updated_mod = mod;
  // Execute the required passes in a DFS way.
  // TODO(zhiics) We may need to pass validation to detect the cyclic
  // dependency.
  for (const auto& it : pass_info->required) {
    const auto* name = it.as<tvm::ir::StringImm>();
    CHECK(name);
    auto pass = GetPass(name->value);
    updated_mod = pass(updated_mod, pass_ctx);
  }

  Module new_mod = ModuleNode::make({}, mod->type_definitions);
392 393
  // Execute the pass function and return a new module.
  for (const auto& it : mod->functions) {
394 395 396
    auto updated_func = SkipFunction(it.second)
                            ? it.second
                            : pass_func(it.second, updated_mod, pass_ctx);
397
    new_mod->Add(it.first, updated_func);
Zhi committed
398 399
  }

400
  return new_mod;
Zhi committed
401 402 403 404 405 406 407 408 409 410
}

// TODO(zhiics) Create an enum attribute for FunctionNode
// enum Attribute {kPrimitive, kSkipOptimization}
bool FunctionPassNode::SkipFunction(const Function& func) const {
  NodeRef res = FunctionGetAttr(func, "SkipOptimization");
  const ir::IntImm* pval = res.as<ir::IntImm>();
  return pval && pval->value != 0;
}

411
Sequential::Sequential(tvm::Array<Pass> passes, PassInfo pass_info) {
412
  auto n = make_node<SequentialNode>();
Zhi committed
413 414
  n->passes = std::move(passes);
  n->pass_info = std::move(pass_info);
415 416 417
  node_ = std::move(n);
}

418 419 420 421 422 423
Sequential::Sequential(tvm::Array<Pass> passes, std::string name) {
  auto n = make_node<SequentialNode>();
  n->passes = std::move(passes);
  PassInfo pass_info = PassInfoNode::make(2, std::move(name), {});
  n->pass_info = std::move(pass_info);
  node_ = std::move(n);
Zhi committed
424 425
}

426 427
const SequentialNode* Sequential::operator->() const {
  return static_cast<const SequentialNode*>(this->node_.get());
Zhi committed
428 429
}

430
void SequentialNode::ResolveDependency(const Module& mod) {
Zhi committed
431 432 433 434 435 436 437 438
  // TODO(zhiics) Implement it.
  // 1. Consider the required passes for each pass.
  // 2. Only resolve the enabled passes.
  // 3. Build a dependency graph. Probably we need to update the pass list.
  LOG(FATAL) << "Pass dependency has not been resolved yet."
             << "\n";
}

439 440 441
std::unordered_set<std::string> SequentialNode::DisabledPasses(
    const Array<tvm::Expr>& disabled) const {
  std::unordered_set<std::string> ret;
Zhi committed
442 443
  for (const auto& it : disabled) {
    const auto* str = it.as<tvm::ir::StringImm>();
444
    CHECK(str) << "Disabled pass name must be string.";
445
    ret.emplace(str->value);
Zhi committed
446 447 448 449
  }
  return ret;
}

450 451 452 453 454
std::unordered_set<std::string> SequentialNode::RequiredPasses(
    const Array<tvm::Expr>& required) const {
  std::unordered_set<std::string> ret;
  for (const auto& it : required) {
    const auto* str = it.as<tvm::ir::StringImm>();
455
    CHECK(str) << "Required pass name must be string.";
456 457 458 459 460
    ret.emplace(str->value);
  }
  return ret;
}

461
bool SequentialNode::PassEnabled(const std::string& pass_name) const {
462 463
  PassContext ctx = PassContext::Current();

464
  auto required = RequiredPasses(ctx->required_pass);
465
  auto disabled = DisabledPasses(ctx->disabled_pass);
466 467 468 469 470 471 472 473

  if (disabled.count(pass_name)) {
    return false;
  }

  if (required.count(pass_name)) {
    return true;
  }
474 475 476 477

  const Pass pass = GetPass(pass_name);
  PassInfo info = pass->Info();
  return ctx->opt_level >= info->opt_level;
478 479 480 481
}

// TODO(zhiics): we currenlty only sequentially execute each pass in
// a Sequential without the consideration of their orders. The phase
482
// ordering problem needs to be handled in the future.
483 484 485 486 487
Module SequentialNode::operator()(const Module& module,
                                  const PassContext& pass_ctx) const {
  Module mod = module;
  for (const Pass& pass : passes) {
    CHECK(pass.defined()) << "Found undefined pass for optimization.";
488

489
    PassInfo info = pass->Info();
490
    const auto& pass_name = info->name;
491 492 493
    // Execute the pass if it is enabled.
    if (PassEnabled(pass_name)) {
      mod = pass(mod, pass_ctx);
494 495 496
    }
  }
  return mod;
Zhi committed
497 498 499 500 501 502 503 504 505 506 507 508
}

Pass CreateModulePass(
    const runtime::TypedPackedFunc<Module(Module, PassContext)>& pass_func,
    int opt_level,
    const std::string& name,
    const tvm::Array<tvm::Expr>& required) {
  PassInfo pass_info = PassInfoNode::make(opt_level, name, required);
  return ModulePassNode::make(pass_func, pass_info);
}

Pass CreateFunctionPass(
509
    const runtime::TypedPackedFunc<Function(Function, Module, PassContext)>& pass_func,
Zhi committed
510 511 512 513 514 515 516 517 518
    int opt_level,
    const std::string& name,
    const tvm::Array<tvm::Expr>& required) {
  PassInfo pass_info = PassInfoNode::make(opt_level, name, required);
  return FunctionPassNode::make(pass_func, pass_info);
}

TVM_REGISTER_NODE_TYPE(PassInfoNode);

519
TVM_REGISTER_API("relay._transform.PassInfo")
520
.set_body_typed(PassInfoNode::make);
Zhi committed
521

522
TVM_REGISTER_API("relay._transform.Info")
Zhi committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
.set_body([](TVMArgs args, TVMRetValue* ret) {
  Pass pass = args[0];
  *ret = pass->Info();
});

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<PassInfoNode>([](const PassInfoNode* node,
                                tvm::IRPrinter* p) {
  p->stream << "The meta data of the pass: ";
  p->stream << "pass name: " << node->name;
  p->stream << "opt_level: " << node->opt_level;
  p->stream << "required passes: [" << "\n";
  for (const auto& it : node->required) {
    const auto* str = it.as<tvm::ir::StringImm>();
    p->stream << str->value << ", ";
  }
  p->stream << "]\n";
});

TVM_REGISTER_NODE_TYPE(ModulePassNode);

544
TVM_REGISTER_API("relay._transform.CreateModulePass")
545
.set_body_typed(CreateModulePass);
Zhi committed
546

547
TVM_REGISTER_API("relay._transform.RunPass")
Zhi committed
548
.set_body([](TVMArgs args, TVMRetValue* ret) {
549 550 551
  Pass pass = args[0];
  Module mod = args[1];
  *ret = pass(mod);
Zhi committed
552 553 554 555 556
});

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<ModulePassNode>([](const ModulePassNode* node,
                                 tvm::IRPrinter* p) {
557 558 559
  const PassInfo info = node->Info();
  p->stream << "Run Module pass: " << info->name
            << " at the optimization level " << info->opt_level;
Zhi committed
560 561 562 563
});

TVM_REGISTER_NODE_TYPE(FunctionPassNode);

564
TVM_REGISTER_API("relay._transform.CreateFunctionPass")
565
.set_body_typed(CreateFunctionPass);
Zhi committed
566 567 568 569

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<FunctionPassNode>([](const FunctionPassNode* node,
                                   tvm::IRPrinter* p) {
570 571 572
  const PassInfo info = node->Info();
  p->stream << "Run Function pass: " << info->name
            << " at the optimization level " << info->opt_level;
Zhi committed
573 574
});

575
TVM_REGISTER_NODE_TYPE(SequentialNode);
Zhi committed
576

577
TVM_REGISTER_API("relay._transform.Sequential")
Zhi committed
578 579 580 581 582 583
.set_body([](TVMArgs args, TVMRetValue* ret) {
  tvm::Array<Pass> passes = args[0];
  int opt_level = args[1];
  std::string name = args[2];
  tvm::Array<tvm::Expr> required = args[3];
  PassInfo pass_info = PassInfoNode::make(opt_level, name, required);
584
  *ret = Sequential(passes, pass_info);
Zhi committed
585 586 587
});

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
588 589
.set_dispatch<SequentialNode>([](const SequentialNode* node,
                                 tvm::IRPrinter* p) {
590 591 592
  const PassInfo info = node->Info();
  p->stream << "Run Sequential pass: " << info->name
            << " at the optimization level " << info->opt_level << ". ";
Zhi committed
593 594
  p->stream << "The passes will be executed are: [";
  for (const auto& it : node->passes) {
595 596
    const PassInfo pass_info = it->Info();
    p->stream << pass_info->name << " ";
Zhi committed
597 598 599 600 601 602
  }
  p->stream << "]";
});

TVM_REGISTER_NODE_TYPE(PassContextNode);

603
TVM_REGISTER_API("relay._transform.PassContext")
604
.set_body([](TVMArgs args, TVMRetValue* ret) {
605
  auto pctx = PassContext::Create();
606 607 608 609
  int opt_level = args[0];
  int fallback_device = args[1];
  tvm::Array<tvm::Expr> required = args[2];
  tvm::Array<tvm::Expr> disabled = args[3];
610 611 612 613 614
  pctx->opt_level = opt_level;
  pctx->fallback_device = fallback_device;
  pctx->required_pass = std::move(required);
  pctx->disabled_pass = std::move(disabled);
  *ret = pctx;
615
});
Zhi committed
616 617 618

TVM_STATIC_IR_FUNCTOR_REGISTER(IRPrinter, vtable)
.set_dispatch<PassContextNode>([](const PassContextNode* node,
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
                               tvm::IRPrinter* p) {
  p->stream << "Pass context information: " << "\n";
  p->stream << "\topt_level: " << node->opt_level << "\n";
  p->stream << "\tfallback device: " << runtime::DeviceName(node->opt_level)
            << "\n";

  p->stream << "\trequired passes: [" << node->opt_level;
  for (const auto& it : node->required_pass) {
    p->stream << it << " ";
  }
  p->stream << "]\n";

  p->stream << "\tdisabled passes: [" << node->opt_level;
  for (const auto& it : node->disabled_pass) {
    p->stream << it << " ";
  }
  p->stream << "]";
Zhi committed
636 637
});

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
class PassContext::Internal {
 public:
  static void EnterScope(PassContext pass_ctx) {
    pass_ctx.EnterWithScope();
  }

  static void ExitScope(PassContext pass_ctx) {
    pass_ctx.ExitWithScope();
  }
};

TVM_REGISTER_API("relay._transform.GetCurrentPassContext")
.set_body_typed(PassContext::Current);

TVM_REGISTER_API("relay._transform.EnterPassContext")
.set_body_typed(PassContext::Internal::EnterScope);

TVM_REGISTER_API("relay._transform.ExitPassContext")
.set_body_typed(PassContext::Internal::ExitScope);

658
}  // namespace transform
Zhi committed
659 660
}  // namespace relay
}  // namespace tvm