relay_transform_sequential.cc 3.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 *
 *   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 <gtest/gtest.h>
#include <topi/generic/injective.h>
22
#include <tvm/node/structural_equal.h>
23
#include <tvm/driver/driver_api.h>
24
#include <tvm/relay/expr.h>
25
#include <tvm/ir/module.h>
Zhi committed
26
#include <tvm/relay/analysis.h>
27 28 29 30
#include <tvm/relay/transform.h>
#include <tvm/relay/type.h>
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/registry.h>
31
#include <tvm/te/operation.h>
32 33 34 35 36 37 38 39

TVM_REGISTER_GLOBAL("schedule")
    .set_body([](tvm::TVMArgs args, tvm::TVMRetValue* rv) {
      *rv = topi::generic::schedule_injective(args[0], args[1]);
    });

TEST(Relay, Sequential) {
  using namespace tvm;
40
  auto tensor_type = relay::TensorType({1, 2, 3}, DataType::Float(32));
41 42 43 44
  auto c_data =
      tvm::runtime::NDArray::Empty({1, 2, 3}, {kDLFloat, 32, 1}, {kDLCPU, 0});

  // Create a function for optimization.
45 46 47
  auto c = relay::Constant(c_data);
  auto a = relay::Var("a", tensor_type);
  auto x = relay::Var("x", tensor_type);
48
  auto add_op = relay::Op::Get("add");
49 50 51 52 53
  auto y = relay::Call(add_op, {c, c});
  y = relay::Call(add_op, {x, y});
  auto z = relay::Call(add_op, {y, c});
  auto z1 = relay::Call(add_op, {y, c});
  auto z2 = relay::Call(add_op, {z, z1});
54
  // Let expression and varaible a should be dead-code eliminated.
55
  auto z3 = relay::Let(a, c, z2);
56
  relay::Function func =
57
      relay::Function(relay::FreeVars(z3), z3, relay::Type(), {});
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

  // Get schedule
  auto reg = tvm::runtime::Registry::Get("relay.op._Register");
  auto sch = tvm::runtime::Registry::Get("schedule");
  if (!reg || !sch) {
    LOG(FATAL) << "Register/schedule is not defined.";
  }

  (*reg)("add", "FTVMSchedule", *sch, 10);

  // Run sequential passes.
  tvm::Array<relay::transform::Pass> pass_seqs{
      relay::transform::InferType(),
      relay::transform::DeadCodeElimination(),
      relay::transform::EliminateCommonSubexpr(),
      relay::transform::AlterOpLayout()
  };
  relay::transform::Pass seq = relay::transform::Sequential(pass_seqs);
76
  auto mod = IRModule::FromExpr(func);
77 78 79 80 81 82 83 84 85 86
  auto pass_ctx = relay::transform::PassContext::Create();
  pass_ctx->opt_level = 3;
  pass_ctx->fallback_device = 1;
  {
    tvm::With<relay::transform::PassContext> ctx_scope(pass_ctx);
    tvm::With<tvm::Target> tctx(tvm::Target::Create("llvm"));
    mod = seq(mod);
  }

  CHECK(mod.defined());
87
  auto entry_func = mod->GetGlobalVar("main");
88
  CHECK(entry_func.defined());
89
  relay::Function f = Downcast<relay::Function>(mod->Lookup("main"));
90 91 92
  CHECK(f.defined());

  // Expected function
93 94 95 96 97 98
  auto c1 = relay::Constant(c_data);
  auto x1 = relay::Var("x", tensor_type);
  auto y1 = relay::Call(add_op, {c1, c1});
  y1 = relay::Call(add_op, {x1, y1});
  auto zz = relay::Call(add_op, {y1, c1});
  zz = relay::Call(add_op, {zz, zz});
99
  relay::Function expected_func =
100
      relay::Function(relay::FreeVars(zz), zz, relay::Type(), {});
101 102

  // Infer type for the expected function.
103
  auto mod1 = IRModule::FromExpr(expected_func);
Zhi committed
104
  mod1 = relay::transform::InferType()(mod1);
105
  auto expected = mod1->Lookup("main");
106
  CHECK(tvm::StructuralEqual()(f, expected));
107 108 109 110 111 112 113
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  testing::FLAGS_gtest_death_test_style = "threadsafe";
  return RUN_ALL_TESTS();
}