Skip to content

Commit

Permalink
[Target] Creating Target from JSON-like Configuration (apache#6218)
Browse files Browse the repository at this point in the history
* [Target] Creating Target from JSON-like Configuration

* Address comments from Cody

* fix unittest

* More testcases as suggested by @comaniac
  • Loading branch information
junrushao authored and Trevor Morris committed Sep 2, 2020
1 parent 560ad39 commit cbae698
Show file tree
Hide file tree
Showing 5 changed files with 516 additions and 383 deletions.
46 changes: 43 additions & 3 deletions include/tvm/target/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <tvm/target/target_kind.h>

#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -62,6 +63,13 @@ class TargetNode : public Object {
v->Visit("attrs", &attrs);
}

/*!
* \brief Get an entry from attrs of the target
* \tparam TObjectRef Type of the attribute
* \param attr_key The name of the attribute key
* \param default_value The value returned if the key is not present
* \return An optional, NullOpt if not found, otherwise the value found
*/
template <typename TObjectRef>
Optional<TObjectRef> GetAttr(
const std::string& attr_key,
Expand All @@ -75,15 +83,19 @@ class TargetNode : public Object {
return default_value;
}
}

/*!
* \brief Get an entry from attrs of the target
* \tparam TObjectRef Type of the attribute
* \param attr_key The name of the attribute key
* \param default_value The value returned if the key is not present
* \return An optional, NullOpt if not found, otherwise the value found
*/
template <typename TObjectRef>
Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
}

/*! \brief Get the keys for this target as a vector of string */
TVM_DLL std::vector<std::string> GetKeys() const;

/*! \brief Get the keys for this target as an unordered_set of string */
TVM_DLL std::unordered_set<std::string> GetLibs() const;

Expand All @@ -93,6 +105,26 @@ class TargetNode : public Object {
private:
/*! \brief Internal string repr. */
mutable std::string str_repr_;
/*!
* \brief Parsing TargetNode::attrs from a list of raw strings
* \param obj The attribute to be parsed
* \param info The runtime type information for parsing
* \return The attribute parsed
*/
ObjectRef ParseAttr(const ObjectRef& obj, const TargetKindNode::ValueTypeInfo& info) const;
/*!
* \brief Parsing TargetNode::attrs from a list of raw strings
* \param options The raw string of fields to be parsed
* \return The attributes parsed
*/
Map<String, ObjectRef> ParseAttrsFromRaw(const std::vector<std::string>& options) const;
/*!
* \brief Serialize the attributes of a target to raw string
* \param attrs The attributes to be converted to string
* \return The string converted, NullOpt if attrs is empty
*/
Optional<String> StringifyAttrsToRaw(const Map<String, ObjectRef>& attrs) const;

friend class Target;
};

Expand All @@ -103,10 +135,18 @@ class TargetNode : public Object {
class Target : public ObjectRef {
public:
Target() {}
/*! \brief Constructor from ObjectPtr */
explicit Target(ObjectPtr<Object> n) : ObjectRef(n) {}
/*!
* \brief Create a Target using a JSON-like configuration
* \param config The JSON-like configuration
* \return The target created
*/
TVM_DLL static Target FromConfig(const Map<String, ObjectRef>& config);
/*!
* \brief Create a Target given a string
* \param target_str the string to parse
* \return The target created
*/
TVM_DLL static Target Create(const String& target_str);
/*!
Expand Down
20 changes: 5 additions & 15 deletions include/tvm/target/target_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ struct ValueTypeInfoMaker;

class Target;

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

template <typename>
class TargetKindAttrMap;

Expand All @@ -67,36 +64,29 @@ class TargetKindNode : public Object {
v->Visit("default_keys", &default_keys);
}

Map<String, ObjectRef> ParseAttrsFromRaw(const std::vector<std::string>& options) const;

Optional<String> StringifyAttrsToRaw(const Map<String, ObjectRef>& attrs) const;

static constexpr const char* _type_key = "TargetKind";
TVM_DECLARE_FINAL_OBJECT_INFO(TargetKindNode, Object);

private:
/*! \brief Return the index stored in attr registry */
uint32_t AttrRegistryIndex() const { return index_; }
/*! \brief Return the name stored in attr registry */
String AttrRegistryName() const { return name; }
/*! \brief Stores the required type_key and type_index of a specific attr of a target */
struct ValueTypeInfo {
String type_key;
uint32_t type_index;
std::unique_ptr<ValueTypeInfo> key;
std::unique_ptr<ValueTypeInfo> val;
};

uint32_t AttrRegistryIndex() const { return index_; }
String AttrRegistryName() const { return name; }
/*! \brief Perform schema validation */
void ValidateSchema(const Map<String, ObjectRef>& config) const;
/*! \brief Verify if the obj is consistent with the type info */
void VerifyTypeInfo(const ObjectRef& obj, const TargetKindNode::ValueTypeInfo& info) const;
/*! \brief A hash table that stores the type information of each attr of the target key */
std::unordered_map<String, ValueTypeInfo> key2vtype_;
/*! \brief A hash table that stores the default value of each attr of the target key */
std::unordered_map<String, ObjectRef> key2default_;
/*! \brief Index used for internal lookup of attribute registry */
uint32_t index_;
friend void TargetValidateSchema(const Map<String, ObjectRef>&);
friend class Target;
friend class TargetNode;
friend class TargetKind;
template <typename, typename>
friend class AttrRegistry;
Expand Down
Loading

0 comments on commit cbae698

Please sign in to comment.