Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Target] Introduce Target Id Registry #5838

Merged
merged 3 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions include/tvm/target/target_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ template <typename, typename, typename>
struct ValueTypeInfoMaker;
}

/*! \brief Perform schema validation */
TVM_DLL void TargetValidateSchema(const Map<String, ObjectRef>& config);

template <typename>
class TargetIdAttrMap;

Expand All @@ -51,8 +54,6 @@ class TargetIdNode : public Object {
public:
/*! \brief Name of the target id */
String name;
/*! \brief Perform schema validation */
TVM_DLL void ValidateSchema(const Map<String, ObjectRef>& config) const;
/*! \brief Stores the required type_key and type_index of a specific attr of a target */
struct ValueTypeInfo {
String type_key;
Expand All @@ -67,10 +68,13 @@ class TargetIdNode : public Object {
private:
uint32_t AttrRegistryIndex() const { return index_; }
String AttrRegistryName() const { return name; }
/*! \brief Perform schema validation */
void ValidateSchema(const Map<String, ObjectRef>& config) const;
/*! \brief A hash table that stores the type information of each attr of the target key */
std::unordered_map<String, ValueTypeInfo> key2vtype_;
/*! \brief Index used for internal lookup of attribute registry */
uint32_t index_;
friend void TargetValidateSchema(const Map<String, ObjectRef>&);
friend class TargetId;
template <typename, typename>
friend class AttrRegistry;
Expand Down Expand Up @@ -284,6 +288,8 @@ inline TargetIdRegEntry& TargetIdRegEntry::set_attr(const String& attr_name, con

template <typename ValueType>
inline TargetIdRegEntry& TargetIdRegEntry::add_attr_option(const String& key) {
CHECK(!id_->key2vtype_.count(key))
<< "AttributeError: add_attr_option failed because '" << key << "' has been set once";
id_->key2vtype_[key] = detail::ValueTypeInfoMaker<ValueType>()();
return *this;
}
Expand Down
72 changes: 54 additions & 18 deletions src/target/target_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ namespace tvm {

using TargetIdRegistry = AttrRegistry<TargetIdRegEntry, TargetId>;

TVM_DLL TargetIdRegEntry& TargetIdRegEntry::RegisterOrGet(const String& target_id_name) {
TargetIdRegEntry& TargetIdRegEntry::RegisterOrGet(const String& target_id_name) {
return TargetIdRegistry::Global()->RegisterOrGet(target_id_name);
}

TVM_DLL void TargetIdRegEntry::UpdateAttr(const String& key, TVMRetValue value, int plevel) {
void TargetIdRegEntry::UpdateAttr(const String& key, TVMRetValue value, int plevel) {
TargetIdRegistry::Global()->UpdateAttr(key, id_, value, plevel);
}

TVM_DLL const AttrRegistryMapContainerMap<TargetId>& TargetId::GetAttrMapContainer(
const AttrRegistryMapContainerMap<TargetId>& TargetId::GetAttrMapContainer(
const String& attr_name) {
return TargetIdRegistry::Global()->GetAttrMap(attr_name);
}

TVM_DLL const TargetId& TargetId::Get(const String& target_id_name) {
const TargetId& TargetId::Get(const String& target_id_name) {
const TargetIdRegEntry* reg = TargetIdRegistry::Global()->Get(target_id_name);
CHECK(reg != nullptr) << "TargetId " << target_id_name << " is not registered";
return reg->id_;
Expand Down Expand Up @@ -91,38 +91,74 @@ void VerifyTypeInfo(const ObjectRef& obj, const TargetIdNode::ValueTypeInfo& inf
}
}

TVM_DLL void TargetIdNode::ValidateSchema(const Map<String, ObjectRef>& config) const {
void TargetIdNode::ValidateSchema(const Map<String, ObjectRef>& config) const {
const String kTargetId = "id";
for (const auto& kv : config) {
auto it = key2vtype_.find(kv.first);
const String& name = kv.first;
const ObjectRef& obj = kv.second;
if (name == kTargetId) {
CHECK(obj->IsInstance<StringObj>())
<< "AttributeError: \"id\" is not a string, but its type is " << obj->GetTypeKey();
CHECK(Downcast<String>(obj) == this->name)
<< "AttributeError: \"id\" = " << obj << " is inconsistent with TargetId " << this->name;
continue;
}
auto it = key2vtype_.find(name);
if (it == key2vtype_.end()) {
std::ostringstream os;
os << "AttributeError: Invalid config option, cannot recognize \'" << kv.first
<< "\' candidates are:";
bool is_first = true;
os << "AttributeError: Invalid config option, cannot recognize \'" << name
<< "\'. Candidates are:";
for (const auto& kv : key2vtype_) {
if (is_first) {
is_first = false;
} else {
os << ',';
}
os << ' ' << kv.first;
os << "\n " << kv.first;
}
LOG(FATAL) << os.str();
throw;
}
const auto& obj = kv.second;
const auto& info = it->second;
try {
VerifyTypeInfo(obj, info);
} catch (const tvm::Error& e) {
LOG(FATAL) << "AttributeError: Schema validation failed for TargetId " << name
LOG(FATAL) << "AttributeError: Schema validation failed for TargetId " << this->name
<< ", details:\n"
<< e.what() << "\n"
<< "The given config is:\n"
<< "The config is:\n"
<< config;
throw;
}
}
}

inline String GetId(const Map<String, ObjectRef>& target, const char* name) {
const String kTargetId = "id";
CHECK(target.count(kTargetId)) << "AttributeError: \"id\" does not exist in " << name << "\n"
<< name << " = " << target;
const ObjectRef& obj = target[kTargetId];
CHECK(obj->IsInstance<StringObj>()) << "AttributeError: \"id\" is not a string in " << name
<< ", but its type is " << obj->GetTypeKey() << "\n"
<< name << " = " << target;
return Downcast<String>(obj);
}

void TargetValidateSchema(const Map<String, ObjectRef>& config) {
try {
const String kTargetHost = "target_host";
Map<String, ObjectRef> target = config;
Map<String, ObjectRef> target_host;
String target_id = GetId(target, "target");
String target_host_id;
if (config.count(kTargetHost)) {
target.erase(kTargetHost);
target_host = Downcast<Map<String, ObjectRef>>(config[kTargetHost]);
junrushao marked this conversation as resolved.
Show resolved Hide resolved
target_host_id = GetId(target_host, "target_host");
}
TargetId::Get(target_id)->ValidateSchema(target);
if (!target_host.empty()) {
TargetId::Get(target_host_id)->ValidateSchema(target_host);
}
} catch (const tvm::Error& e) {
LOG(INFO) << e.what();
throw e;
}
}

} // namespace tvm
29 changes: 16 additions & 13 deletions tests/cpp/target_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,41 @@ TEST(TargetId, GetAttrMap) {
}

TEST(TargetId, SchemaValidation) {
tvm::Map<String, ObjectRef> schema;
tvm::Map<String, ObjectRef> target;
{
tvm::Array<String> your_names{"junru", "jian"};
tvm::Map<String, Integer> her_maps{
{"a", 1},
{"b", 2},
};
schema.Set("my_bool", Bool(true));
schema.Set("your_names", your_names);
schema.Set("her_maps", her_maps);
target.Set("my_bool", Bool(true));
target.Set("your_names", your_names);
target.Set("her_maps", her_maps);
target.Set("id", String("TestTargetId"));
}
const TargetId& target_id = tvm::TargetId::Get("TestTargetId");
target_id->ValidateSchema(schema);
TargetValidateSchema(target);
tvm::Map<String, ObjectRef> target_host(target.begin(), target.end());
target.Set("target_host", target_host);
TargetValidateSchema(target);
}

TEST(TargetId, SchemaValidationFail) {
tvm::Map<String, ObjectRef> schema;
tvm::Map<String, ObjectRef> target;
{
tvm::Array<String> your_names{"junru", "jian"};
tvm::Map<String, Integer> her_maps{
{"a", 1},
{"b", 2},
};
schema.Set("my_bool", Bool(true));
schema.Set("your_names", your_names);
schema.Set("her_maps", her_maps);
schema.Set("ok", ObjectRef(nullptr));
target.Set("my_bool", Bool(true));
target.Set("your_names", your_names);
target.Set("her_maps", her_maps);
target.Set("ok", ObjectRef(nullptr));
target.Set("id", String("TestTargetId"));
}
const TargetId& target_id = tvm::TargetId::Get("TestTargetId");
bool failed = false;
try {
target_id->ValidateSchema(schema);
TargetValidateSchema(target);
} catch (...) {
failed = true;
}
Expand Down