Skip to content

Commit

Permalink
[RUNTIME] Allow non-nullable ObjectRef, introduce Optional<T>.
Browse files Browse the repository at this point in the history
We use ObjectRef and their sub-classes extensively throughout our codebase.
Each of ObjectRef's sub-classes are nullable, which means they can hold nullptr
as their values.

While in some places we need nullptr as an alternative value. The implicit support
for nullptr in all ObjectRef creates additional burdens for the developer
to explicitly check defined in many places of the codebase.

Moreover, it is unclear from the API's intentional point of view whether
we want a nullable object or not-null version(many cases we want the later).

Borrowing existing wisdoms from languages like Rust. We propose to
introduce non-nullable ObjectRef, and Optional<T> container that
represents a nullable variant.

To keep backward compatiblity, we will start by allowing most ObjectRef to be nullable.
However, we should start to use Optional<T> as the type in places where
we know nullable is a requirement. Gradually, we will move most of the ObjectRef
to be non-nullable and use Optional<T> in the nullable cases.

Such explicitness in typing can help reduce the potential problems
in our codebase overall.

Changes in this PR:
- Introduce _type_is_nullable attribute to ObjectRef
- Introduce Optional<T>
- Change String to be non-nullable.
- Change the API of function->GetAttr to return Optional<T>
  • Loading branch information
tqchen committed Apr 12, 2020
1 parent e4b80bd commit 5fa3274
Show file tree
Hide file tree
Showing 38 changed files with 393 additions and 85 deletions.
2 changes: 2 additions & 0 deletions include/tvm/ir/attrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ namespace tvm {
*/
template<typename TObjectRef>
inline TObjectRef NullValue() {
static_assert(TObjectRef::_type_is_nullable,
"Can only value for nullable types");
return TObjectRef(ObjectPtr<Object>(nullptr));
}

Expand Down
67 changes: 63 additions & 4 deletions include/tvm/ir/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,47 @@ class FloatImm : public PrimExpr {
TVM_DEFINE_OBJECT_REF_METHODS(FloatImm, PrimExpr, FloatImmNode);
};

/*!
* \brief Boolean constant.
*
* This reference type is useful to add additional compile-time
* type checks and helper functions for Integer equal comparisons.
*/
class Bool : public IntImm {
public:
explicit Bool(bool value)
: IntImm(DataType::Bool(), value) {
}
Bool operator!() const {
return Bool((*this)->value == 0);
}
operator bool() const {
return (*this)->value != 0;
}

TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Bool, IntImm, IntImmNode);
};

// Overload operators to make sure we have the most fine grained types.
inline Bool operator||(const Bool& a, bool b) {
return Bool(a.operator bool() || b);
}
inline Bool operator||(bool a, const Bool& b) {
return Bool(a || b.operator bool());
}
inline Bool operator||(const Bool& a, const Bool& b) {
return Bool(a.operator bool() || b.operator bool());
}
inline Bool operator&&(const Bool& a, bool b) {
return Bool(a.operator bool() && b);
}
inline Bool operator&&(bool a, const Bool& b) {
return Bool(a && b.operator bool());
}
inline Bool operator&&(const Bool& a, const Bool& b) {
return Bool(a.operator bool() && b.operator bool());
}

/*!
* \brief Container of constant int that adds more constructors.
*
Expand Down Expand Up @@ -346,10 +387,10 @@ class Integer : public IntImm {
* \tparam Enum The enum type.
* \param value The enum value.
*/
template<typename ENum,
typename = typename std::enable_if<std::is_enum<ENum>::value>::type>
explicit Integer(ENum value) : Integer(static_cast<int>(value)) {
static_assert(std::is_same<int, typename std::underlying_type<ENum>::type>::value,
template<typename Enum,
typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
explicit Integer(Enum value) : Integer(static_cast<int>(value)) {
static_assert(std::is_same<int, typename std::underlying_type<Enum>::type>::value,
"declare enum to be enum int to use visitor");
}
/*!
Expand All @@ -368,6 +409,24 @@ class Integer : public IntImm {
<< " Trying to reference a null Integer";
return (*this)->value;
}
// comparators
Bool operator==(int other) const {
if (data_ == nullptr) return Bool(false);
return Bool((*this)->value == other);
}
Bool operator!=(int other) const {
return !(*this == other);
}
template<typename Enum,
typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
Bool operator==(Enum other) const {
return *this == static_cast<int>(other);
}
template<typename Enum,
typename = typename std::enable_if<std::is_enum<Enum>::value>::type>
Bool operator!=(Enum other) const {
return *this != static_cast<int>(other);
}
};

/*! \brief range over one dimension */
Expand Down
19 changes: 13 additions & 6 deletions include/tvm/ir/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <tvm/ir/expr.h>
#include <tvm/ir/attrs.h>
#include <tvm/runtime/container.h>
#include <type_traits>
#include <string>

Expand Down Expand Up @@ -90,25 +91,31 @@ class BaseFuncNode : public RelayExprNode {
* \code
*
* void GetAttrExample(const BaseFunc& f) {
* Integer value = f->GetAttr<Integer>("AttrKey", 0);
* auto value = f->GetAttr<Integer>("AttrKey", 0);
* }
*
* \endcode
*/
template<typename TObjectRef>
TObjectRef GetAttr(const std::string& attr_key,
TObjectRef default_value = NullValue<TObjectRef>()) const {
Optional<TObjectRef> GetAttr(
const std::string& attr_key,
Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
static_assert(std::is_base_of<ObjectRef, TObjectRef>::value,
"Can only call GetAttr with ObjectRef types.");
if (!attrs.defined()) return default_value;
auto it = attrs->dict.find(attr_key);
if (it != attrs->dict.end()) {
return Downcast<TObjectRef>((*it).second);
return Downcast<Optional<TObjectRef>>((*it).second);
} else {
return default_value;
}
}

// variant that uses TObjectRef to enable implicit conversion to default value.
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 Check whether the function has an non-zero integer attr.
*
Expand All @@ -129,7 +136,7 @@ class BaseFuncNode : public RelayExprNode {
* \endcode
*/
bool HasNonzeroAttr(const std::string& attr_key) const {
return GetAttr<Integer>(attr_key, 0)->value != 0;
return GetAttr<Integer>(attr_key) != 0;
}

static constexpr const char* _type_key = "BaseFunc";
Expand Down
1 change: 0 additions & 1 deletion include/tvm/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ using runtime::make_object;
using runtime::PackedFunc;
using runtime::TVMArgs;
using runtime::TVMRetValue;
using runtime::String;

} // namespace tvm
#endif // TVM_NODE_NODE_H_
131 changes: 127 additions & 4 deletions include/tvm/runtime/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ class StringObj : public Object {
*/
class String : public ObjectRef {
public:
/*!
* \brief Construct an empty string.
*/
String() : String(std::string()) {}
/*!
* \brief Construct a new String object
*
Expand Down Expand Up @@ -467,9 +471,6 @@ class String : public ObjectRef {
*/
size_t size() const {
const auto* ptr = get();
if (ptr == nullptr) {
return 0;
}
return ptr->size;
}

Expand Down Expand Up @@ -524,7 +525,7 @@ class String : public ObjectRef {
/*! \return the internal StringObj pointer */
const StringObj* get() const { return operator->(); }

TVM_DEFINE_OBJECT_REF_METHODS(String, ObjectRef, StringObj);
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(String, ObjectRef, StringObj);

private:
/*!
Expand Down Expand Up @@ -610,7 +611,129 @@ struct PackedFuncValueConverter<::tvm::runtime::String> {
}
};

/*!
* \brief Optional container that to represent to a Nullable variant of T.
* \tparam T The original ObjectRef.
*
* \code
*
* Optional<String> opt0 = nullptr;
* Optional<String> opt1 = String("xyz");
* CHECK(opt0 == nullptr);
* CHECK(opt1 == "xyz");
*
* \endcode
*/
template<typename T>
class Optional : public ObjectRef {
public:
using ContainerType = typename T::ContainerType;
static_assert(std::is_base_of<ObjectRef, T>::value,
"Optional is only defined for ObjectRef.");
// default constructors.
Optional() = default;
Optional(const Optional<T>&) = default;
Optional(Optional<T>&&) = default;
Optional<T>& operator=(const Optional<T>&) = default;
Optional<T>& operator=(Optional<T>&&) = default;
/*!
* \brief Construct from an ObjectPtr
* whose type already matches the ContainerType.
* \param ptr
*/
explicit Optional(ObjectPtr<Object> ptr) : ObjectRef(ptr) {}
// nullptr handling.
// disallow implicit conversion as 0 can be implicitly converted to nullptr_t
explicit Optional(std::nullptr_t) {}
Optional<T>& operator=(std::nullptr_t) {
data_ = nullptr;
return *this;
}
// normal value handling.
Optional(T other) // NOLINT(*)
: ObjectRef(std::move(other)) {
}
Optional<T>& operator=(T other) {
ObjectRef::operator=(std::move(other));
return *this;
}
// delete the int constructor
// since Optional<Integer>(0) is ambiguious
// 0 can be implicitly casted to nullptr_t
Optional(int val) = delete;
Optional<T>& operator=(int val) = delete;
/*!
* \return A not-null container value in the optional.
* \note This function performs not-null checking.
*/
T value() const {
CHECK(data_ != nullptr);
return T(data_);
}
/*! \return Whether the container is not nullptr.*/
explicit operator bool() const {
return *this != nullptr;
}
// operator overloadings
bool operator==(std::nullptr_t) const {
return data_ == nullptr;
}
bool operator!=(std::nullptr_t) const {
return data_ != nullptr;
}
auto operator==(const Optional<T>& other) const {
// support case where sub-class returns a symbolic ref type.
using RetType = decltype(value() == other.value());
if (same_as(other)) return RetType(true);
if (*this != nullptr && other != nullptr) {
return value() == other.value();
} else {
// one of them is nullptr.
return RetType(false);
}
}
auto operator!=(const Optional<T>& other) const {
return !(*this == other);
}
auto operator==(const T& other) const {
using RetType = decltype(value() == other);
if (same_as(other)) return RetType(true);
if (*this != nullptr) return value() == other;
return RetType(false);
}
auto operator!=(const T& other) const {
return !(*this == other);
}
template<typename U>
auto operator==(const U& other) const {
using RetType = decltype(value() == other);
if (*this == nullptr) return RetType(false);
return value() == other;
}
template<typename U>
auto operator!=(const U& other) const {
return !(*this == other);
}
static constexpr bool _type_is_nullable = true;
};

template<typename T>
struct PackedFuncValueConverter<Optional<T>> {
static Optional<T> From(const TVMArgValue& val) {
if (val.type_code() == kTVMNullptr) return Optional<T>(nullptr);
return PackedFuncValueConverter<T>::From(val);
}
static Optional<T> From(const TVMRetValue& val) {
if (val.type_code() == kTVMNullptr) return Optional<T>(nullptr);
return PackedFuncValueConverter<T>::From(val);
}
};

} // namespace runtime

// expose the functions to the root namespace.
using runtime::String;
using runtime::Optional;
} // namespace tvm

namespace std {
Expand Down
Loading

0 comments on commit 5fa3274

Please sign in to comment.