attrs_test.cc 2.73 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <dmlc/logging.h>
#include <gtest/gtest.h>
#include <tvm/attrs.h>
#include <tvm/ir.h>

namespace tvm {
namespace test {
// test example usage docs
struct TestAttrs : public AttrsNode<TestAttrs> {
  int axis;
  std::string name;
  Expr expr;
  double learning_rate;

  TVM_DECLARE_ATTRS(TestAttrs, "attrs.cpptest.TestAttrs") {
    TVM_ATTR_FIELD(axis)
        .set_default(10)
        .set_lower_bound(1)
        .set_upper_bound(10)
        .describe("axis field");
    TVM_ATTR_FIELD(name)
        .describe("name of the field");
    TVM_ATTR_FIELD(expr)
        .describe("expression field")
        .set_default(make_const(Int(32), 1));
    TVM_ATTR_FIELD(learning_rate)
        .describe("learning_rate")
        .set_default(0.1);
  }
};
}
}

TEST(Attrs, Basic) {
  using namespace tvm;
  using namespace tvm::test;
56
  ObjectPtr<TestAttrs> n = make_object<TestAttrs>();
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  try {
    n->InitBySeq("axis", 10);
    LOG(FATAL) << "bad";
  } catch (const tvm::AttrError& e) {
  }
  try {
    n->InitBySeq("axis", 12, "name", "111");
    LOG(FATAL) << "bad";
  } catch (const tvm::AttrError& e) {
  }

  try {
    n->InitBySeq("axisx", 12, "name", "111");
    LOG(FATAL) << "bad";
  } catch (const tvm::AttrError& e) {
    std::string what = e.what();
    CHECK(what.find("expr : Expr, default=1") != std::string::npos);
    CHECK(what.find("axisx") != std::string::npos);
  }
  n->InitBySeq("learning_rate", Expr(1), "expr", 128, "name", "xx");
  CHECK_EQ(n->learning_rate, 1.0);

  n->InitBySeq("name", "xxx", "expr", 128);
  CHECK_EQ(n->name, "xxx");
  CHECK_EQ(n->axis, 10);
  CHECK_EQ(n->expr.as<tvm::ir::IntImm>()->value, 128);
  // Check docstring
  std::ostringstream os;
  n->PrintDocString(os);
  LOG(INFO) << "docstring\n"<< os.str();
  CHECK(os.str().find("expr : Expr, default=1") != std::string::npos);
}


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