!24877 Add api documents for core api

Merge pull request !24877 from LiangZhibo/code_docs_core_api
This commit is contained in:
i-robot 2021-10-14 11:32:45 +00:00 committed by Gitee
commit ec981124d0
6 changed files with 377 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/**
* Copyright 2019 Huawei Technologies Co., Ltd
* Copyright 2019-2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,31 +34,55 @@
#include "ir/dtype/type.h"
namespace mindspore {
// TypeRefKey type
// List
/// \brief List defines interface for list data type.
class MS_CORE_API List : public Object {
public:
/// \brief Default constructor for List.
List() : Object(kObjectTypeList) {}
/// \brief Constructor for List.
///
/// \param[in] objs The elements of List.
List(const std::initializer_list<TypePtr> &objs)
: Object(kObjectTypeList, false), elements_(objs.begin(), objs.end()) {}
// Shadow copy;
/// \brief Shadow copy function for List.
///
/// \param[in] obj TypePtrList to be copied.
explicit List(const TypePtrList &obj) : Object(kObjectTypeList, false), elements_(obj) {}
/// \brief Destructor of List.
~List() override {}
MS_DECLARE_PARENT(List, Object)
/// \brief Get type of List element.
///
/// \param[in] dim Define the index of List element.
/// \return TypePtr of List element.
const TypePtr operator[](size_t dim) const;
TypeId generic_type_id() const override { return kObjectTypeList; }
TypePtr DeepCopy() const override;
bool operator==(const Type &other) const override;
/// \brief Get the number of elements in this List.
///
/// \return The number of elements in this List.
std::size_t size() const { return elements_.size(); }
/// \brief Get the elements of List object.
///
/// \return The elements of List object.
TypePtrList elements() const { return elements_; }
std::string ToReprString() const override { return "list_"; }
std::string ToString() const override { return DumpContent(false); }
std::string DumpText() const override { return DumpContent(true); };
private:
/// \brief Show each element.
///
/// \param[in] is_dumptext whether to show each element DumpText
/// \return The description of the List object.
std::string DumpContent(bool is_dumptext) const;
TypePtrList elements_;
};
@ -66,29 +90,61 @@ using ListPtr = std::shared_ptr<List>;
using ClassAttrVector = std::vector<std::pair<std::string, TypePtr>>;
/// \brief Class defines interface for class data type.
class MS_CORE_API Class : public Object {
public:
/// \brief Constructor for Class.
Class() : Object(kObjectTypeClass), tag_(Named("Class")) {}
/// \brief Constructor for Class.
///
/// \param[in] tag Define the tag of Class object.
/// \param[in] attributes Define the attributes of Class object.
/// \param[in] methods Define the methods of Class object.
Class(const Named &tag, const ClassAttrVector &attributes, const std::unordered_map<std::string, ValuePtr> &methods);
/// \brief Destructor of Class.
~Class() override {}
MS_DECLARE_PARENT(Class, Object)
TypeId generic_type_id() const override { return kObjectTypeClass; }
bool operator==(const Type &other) const override;
TypePtr DeepCopy() const override;
std::string ToString() const override { return DumpContent(false); }
std::string DumpText() const override { return DumpContent(true); };
/// \brief Set attributes value of Class object.
///
/// \param[in] v Define the attributes value to be set.
void set_value(const std::unordered_map<std::string, ValuePtr> &v) { attributes_value_ = v; }
/// \brief Get the tag of Class object.
///
/// \return The tag of Class object.
Named tag() { return tag_; }
/// \brief Get the value of Class object.
///
/// \return The attributes value of Class object.
std::unordered_map<std::string, ValuePtr> GetValue() { return attributes_value_; }
/// \brief Get the methods of Class object.
///
/// \return The methods of Class object.
std::unordered_map<std::string, ValuePtr> methods() { return methods_; }
/// \brief Get the attributes of Class object.
///
/// \return The attributes of Class object.
ClassAttrVector &GetAttributes() { return attributes_; }
ClassAttrVector attributes_;
private:
/// \brief Show each element.
///
/// \param[in] is_dumptext whether to show each element DumpText
/// \return The description of the Class object.
std::string DumpContent(bool is_dumptext) const;
Named tag_;
std::unordered_map<std::string, ValuePtr> methods_;
@ -97,54 +153,88 @@ class MS_CORE_API Class : public Object {
};
using ClassPtr = std::shared_ptr<Class>;
/// \brief Tuple defines interface for tuple data type.
class MS_CORE_API Tuple : public Object {
public:
/// \brief Default constructor for Tuple.
Tuple() : Object(kObjectTypeTuple) {}
// usage : Tuple t = {std::make_shared<Bool>(), std::make_shared<Int>(32)};
/// \brief Constructor for Tuple.
///
/// \param[in] objs The elements of Tuple.
Tuple(const std::initializer_list<TypePtr> &objs)
: Object(kObjectTypeTuple, false), elements_(objs.begin(), objs.end()) {}
// Shadow copy
/// \brief Shadow copy function for Tuple.
///
/// \param[in] obj TypePtrList to be copied.
explicit Tuple(const TypePtrList &objs) : Object(kObjectTypeTuple, false), elements_(objs.begin(), objs.end()) {}
/// \brief Destructor of Tuple.
~Tuple() override {}
MS_DECLARE_PARENT(Tuple, Object)
TypeId generic_type_id() const override { return kObjectTypeTuple; }
TypePtr DeepCopy() const override;
std::string ToReprString() const override { return "tuple_"; }
std::string ToString() const override { return DumpContent(false); }
std::string DumpText() const override { return DumpContent(true); };
std::string DumpText() const override { return DumpContent(true); }
/// \brief Get type of Tuple element.
///
/// \param[in] dim Define the index of Tuple element.
/// \return TypePtr of Tuple element.
const TypePtr operator[](size_t dim) const;
bool operator==(const Type &other) const override;
/// \brief Get the elements of the Tuple object.
///
/// \return The elements of the Tuple object.
TypePtrList elements() const { return elements_; }
/// \brief Get the number of elements in the Tuple object.
///
/// \return The number of elements in the Tuple object.
std::size_t size() const { return elements_.size(); }
private:
/// \brief Show each element.
///
/// \param[in] is_dumptext whether to show each element DumpText
/// \return The description of the Tuple object.
std::string DumpContent(bool is_dumptext) const;
TypePtrList elements_;
};
using TuplePtr = std::shared_ptr<Tuple>;
/// \brief Dictionary defines interface for dictionary data type.
class MS_CORE_API Dictionary : public Object {
public:
/// \brief Default constructor for Dictionary.
Dictionary() : Object(kObjectTypeDictionary) {}
/// \brief Constructor for Dictionary.
///
/// \param[in] objs The elements of Dictionary.
explicit Dictionary(const std::vector<std::pair<std::string, TypePtr>> &key_values)
: Object(kObjectTypeDictionary, false), key_values_(key_values) {}
/// \brief Destructor of Dictionary.
~Dictionary() override {}
MS_DECLARE_PARENT(Dictionary, Object)
TypeId generic_type_id() const override { return kObjectTypeDictionary; }
bool operator==(const Type &other) const override;
TypePtr DeepCopy() const override;
std::string ToString() const override { return DumpContent(false); }
std::string DumpText() const override { return DumpContent(true); };
std::string DumpText() const override { return DumpContent(true); }
private:
/// \brief Show each element.
///
/// \param[in] is_dumptext whether to show each element DumpText
/// \return The description of the Dictionary object.
std::string DumpContent(bool is_dumptext) const;
std::vector<std::pair<std::string, TypePtr>> key_values_;
};

View File

@ -1,5 +1,5 @@
/**
* Copyright 2019 Huawei Technologies Co., Ltd
* Copyright 2019-2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,9 +34,13 @@
#include "ir/dtype/type.h"
namespace mindspore {
/// \brief TypeAnything defines a Type class whose type is Anything.
class MS_CORE_API TypeAnything : public Type {
public:
/// \brief Default constructor for TypeAnything.
TypeAnything() : Type(kMetaTypeAnything) {}
/// \brief Destructor of TypeAnything.
~TypeAnything() override {}
MS_DECLARE_PARENT(TypeAnything, Type)
@ -46,9 +50,13 @@ class MS_CORE_API TypeAnything : public Type {
};
using TypeAnythingPtr = std::shared_ptr<TypeAnything>;
/// \brief TypeNone defines a Type class whose type is None.
class MS_CORE_API TypeNone : public Type {
public:
/// \brief Default constructor for TypeNone.
TypeNone() : Type(kMetaTypeNone) {}
/// \brief Destructor of TypeNone.
~TypeNone() override {}
MS_DECLARE_PARENT(TypeNone, Type)
@ -59,9 +67,13 @@ class MS_CORE_API TypeNone : public Type {
};
using TypeNonePtr = std::shared_ptr<TypeNone>;
/// \brief TypeNull defines a Type class whose type is Null.
class MS_CORE_API TypeNull : public Type {
public:
/// \brief Default constructor for TypeNull.
TypeNull() : Type(kMetaTypeNull) {}
/// \brief Destructor of TypeNull.
~TypeNull() override {}
MS_DECLARE_PARENT(TypeNull, Type)
@ -71,9 +83,13 @@ class MS_CORE_API TypeNull : public Type {
};
using TypeNullPtr = std::shared_ptr<TypeNull>;
/// \brief TypeEllipsis defines a Type class whose type is Ellipsis.
class MS_CORE_API TypeEllipsis : public Type {
public:
/// \brief Default constructor for TypeEllipsis.
TypeEllipsis() : Type(kMetaTypeEllipsis) {}
/// \brief Destructor of TypeEllipsis.
~TypeEllipsis() override {}
MS_DECLARE_PARENT(TypeEllipsis, Type)

View File

@ -1,5 +1,5 @@
/**
* Copyright 2019 Huawei Technologies Co., Ltd
* Copyright 2019-2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,26 +34,42 @@
#include "ir/dtype/type.h"
namespace mindspore {
// Number, abstract class.
/// \brief Number defines an Object class whose type is number.
class MS_CORE_API Number : public Object {
public:
/// \brief Default constructor for Number.
Number() : Object(kObjectTypeNumber), number_type_(kObjectTypeNumber), nbits_(0) {}
/// \brief Constructor for Number.
///
/// \param[in] number_type Define the number type of Number object.
/// \param[in] nbits Define the bit length of Number object.
/// \param[in] is_generic Define whether it is generic for Number object.
Number(const TypeId number_type, const int nbits, bool is_generic = true)
: Object(kObjectTypeNumber, is_generic), number_type_(number_type), nbits_(nbits) {}
/// \brief Destructor of Number.
~Number() override = default;
MS_DECLARE_PARENT(Number, Object)
/// \brief Get the bit length of Number object.
///
/// \return bit length of Number object.
int nbits() const { return nbits_; }
TypeId number_type() const override { return number_type_; }
TypeId type_id() const override { return number_type_; }
TypeId generic_type_id() const override { return kObjectTypeNumber; }
bool operator==(const Type &other) const override;
TypePtr DeepCopy() const override { return std::make_shared<Number>(); }
std::string ToString() const override { return "Number"; }
std::string ToReprString() const override { return "number"; }
std::string DumpText() const override { return "Number"; }
/// \brief Get type name for Number object.
///
/// \param Define the type name.
/// \return The full type name of the Number object.
std::string GetTypeName(const std::string &type_name) const {
std::ostringstream oss;
oss << type_name;
@ -71,9 +87,13 @@ class MS_CORE_API Number : public Object {
using NumberPtr = std::shared_ptr<Number>;
// Bool
/// \brief Bool defines a Number class whose type is boolean.
class MS_CORE_API Bool : public Number {
public:
/// \brief Default constructor for Bool.
Bool() : Number(kNumberTypeBool, 8) {}
/// \brief Destructor of Bool.
~Bool() override = default;
MS_DECLARE_PARENT(Bool, Number)
@ -85,12 +105,21 @@ class MS_CORE_API Bool : public Number {
};
// Int
/// \brief Int defines a Number class whose type is int.
class MS_CORE_API Int : public Number {
public:
/// \brief Default constructor for Int.
Int() : Number(kNumberTypeInt, 0) {}
/// \brief Constructor for Int.
///
/// \param nbits Define the bit length of Int object.
explicit Int(const int nbits);
/// \brief Destructor of Int.
~Int() override = default;
MS_DECLARE_PARENT(Int, Number)
TypeId generic_type_id() const override { return kNumberTypeInt; }
TypePtr DeepCopy() const override {
if (nbits() == 0) {
@ -98,6 +127,7 @@ class MS_CORE_API Int : public Number {
}
return std::make_shared<Int>(nbits());
}
std::string ToString() const override { return GetTypeName("Int"); }
std::string ToReprString() const override { return nbits() == 0 ? "int_" : GetTypeName("int"); }
std::string DumpText() const override {
@ -106,12 +136,20 @@ class MS_CORE_API Int : public Number {
};
// UInt
/// \brief UInt defines a Number class whose type is uint.
class MS_CORE_API UInt : public Number {
public:
/// \brief Default constructor for UInt.
UInt() : Number(kNumberTypeUInt, 0) {}
/// \brief Constructor for UInt.
///
/// \param nbits Define the bit length of UInt object.
explicit UInt(const int nbits);
TypeId generic_type_id() const override { return kNumberTypeUInt; }
/// \brief Destructor of UInt.
~UInt() override {}
MS_DECLARE_PARENT(UInt, Number)
@ -121,6 +159,7 @@ class MS_CORE_API UInt : public Number {
}
return std::make_shared<UInt>(nbits());
}
std::string ToString() const override { return GetTypeName("UInt"); }
std::string ToReprString() const override { return GetTypeName("uint"); }
std::string DumpText() const override {
@ -129,10 +168,18 @@ class MS_CORE_API UInt : public Number {
};
// Float
/// \brief Float defines a Number class whose type is float.
class MS_CORE_API Float : public Number {
public:
/// \brief Default constructor for Float.
Float() : Number(kNumberTypeFloat, 0) {}
/// \brief Constructor for Float.
///
/// \param nbits Define the bit length of Float object.
explicit Float(const int nbits);
/// \brief Destructor of Float.
~Float() override {}
MS_DECLARE_PARENT(Float, Number)
@ -143,6 +190,7 @@ class MS_CORE_API Float : public Number {
}
return std::make_shared<Float>(nbits());
}
std::string ToString() const override { return GetTypeName("Float"); }
std::string ToReprString() const override { return nbits() == 0 ? "float_" : GetTypeName("float"); }
std::string DumpText() const override {
@ -151,10 +199,18 @@ class MS_CORE_API Float : public Number {
};
// Complex
/// \brief Complex defines a Number class whose type is complex.
class MS_CORE_API Complex : public Number {
public:
/// \brief Default constructor for Complex.
Complex() : Number(kNumberTypeComplex, 0) {}
/// \brief Constructor for Complex.
///
/// \param nbits Define the bit length of Complex object.
explicit Complex(const int nbits);
/// \brief Destructor of Complex.
~Complex() override {}
MS_DECLARE_PARENT(Complex, Number)
@ -165,6 +221,7 @@ class MS_CORE_API Complex : public Number {
}
return std::make_shared<Complex>(nbits());
}
std::string ToString() const override { return GetTypeName("Complex"); }
std::string ToReprString() const override { return GetTypeName("complex"); }
std::string DumpText() const override { return std::string("C") + std::to_string(nbits()); }

View File

@ -1,5 +1,5 @@
/**
* Copyright 2019 Huawei Technologies Co., Ltd
* Copyright 2019-2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,9 +27,13 @@
namespace mindspore {
// TypeRefKey type
/// \brief RefKeyType defines an Object class whose type is RefKey.
class MS_CORE_API RefKeyType : public Object {
public:
/// \brief Default constructor for RefKeyType.
RefKeyType() : Object(kObjectTypeRefKey) {}
/// \brief Destructor of RefKeyType.
~RefKeyType() override {}
MS_DECLARE_PARENT(RefKeyType, Object)
@ -40,10 +44,18 @@ class MS_CORE_API RefKeyType : public Object {
};
// TypeRef type
/// \brief RefType defines a TensorType class whose type is Ref.
class MS_CORE_API RefType : public TensorType {
public:
/// \brief Default constructor for RefType.
RefType() : TensorType() {}
/// \brief Constructor for RefType.
///
/// \param[in] subtype Define the TensorType for RefType object to refer to.
explicit RefType(const TensorTypePtr &subtype) : TensorType(subtype->element()) {}
/// \brief Destructor of RefType.
~RefType() override {}
MS_DECLARE_PARENT(RefType, TensorType)

View File

@ -1,5 +1,5 @@
/**
* Copyright 2019 Huawei Technologies Co., Ltd
* Copyright 2019-2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,22 +34,39 @@
#include "ir/dtype/type.h"
namespace mindspore {
/// \brief UndeterminedType defines interface for tensor undetermined data type.
class MS_CORE_API UndeterminedType : public Object {
public:
/// \brief Default constructor for UndeterminedType.
UndeterminedType() : Object(kObjectTypeUndeterminedType) {}
/// \brief Constructor for UndeterminedType.
///
/// \param[in] ele The element of UndeterminedType.
explicit UndeterminedType(const TypePtr &ele)
: Object(kObjectTypeUndeterminedType, kMetaTypeObject, false), element_type_(ele) {}
/// \brief Destructor of UndeterminedType.
~UndeterminedType() override = default;
MS_DECLARE_PARENT(UndeterminedType, Object)
TypeId generic_type_id() const override { return kObjectTypeUndeterminedType; }
/// \brief Get the element of UndeterminedType object.
///
/// \return The element of UndeterminedType object.
const TypePtr element() const { return element_type_; }
/// \brief Set the element of UndeterminedType object.
///
/// \param[in] element_type Define the element type to be set.
void set_element(const TypePtr &element_type) { element_type_ = element_type; }
TypePtr DeepCopy() const override;
std::string ToString() const override;
std::string ToReprString() const override;
std::string DumpText() const override;
bool operator==(const Type &other) const override;
protected:
@ -57,16 +74,32 @@ class MS_CORE_API UndeterminedType : public Object {
};
using MetaTensorTypePtr = std::shared_ptr<UndeterminedType>;
/// \brief TensorType defines interface for tensor data type.
class MS_CORE_API TensorType : public Object {
public:
/// \brief Default constructor for TensorType.
TensorType() : Object(kObjectTypeTensorType, kObjectTypeUndeterminedType) {}
/// \brief Constructor for TensorType.
///
/// \param[in] ele The element of TensorType.
explicit TensorType(const TypePtr &ele)
: Object(kObjectTypeTensorType, kObjectTypeUndeterminedType, false), element_type_(ele) {}
/// \brief Destructor of TensorType.
~TensorType() override = default;
MS_DECLARE_PARENT(TensorType, Object)
TypeId generic_type_id() const override { return kObjectTypeTensorType; }
/// \brief Get the element of TensorType object.
///
/// \return The element of TensorType object.
const TypePtr element() const { return element_type_; }
/// \brief Set the element of TensorType object.
///
/// \param[in] element_type Define the element type to be set.
void set_element(const TypePtr &element_type) { element_type_ = element_type; }
TypePtr DeepCopy() const override;
@ -80,16 +113,32 @@ class MS_CORE_API TensorType : public Object {
};
using TensorTypePtr = std::shared_ptr<TensorType>;
/// \brief RowTensorType defines interface for row tensor data type.
class MS_CORE_API RowTensorType : public Object {
public:
/// \brief Default constructor for RowTensorType.
RowTensorType() : Object(kObjectTypeRowTensorType, kObjectTypeUndeterminedType) {}
/// \brief Constructor for RowTensorType.
///
/// \param[in] ele The element of RowTensorType.
explicit RowTensorType(const TypePtr &ele)
: Object(kObjectTypeRowTensorType, kObjectTypeUndeterminedType, false), element_type_(ele) {}
/// \brief Destructor of RowTensorType.
~RowTensorType() override = default;
MS_DECLARE_PARENT(RowTensorType, Object)
TypeId generic_type_id() const override { return kObjectTypeRowTensorType; }
/// \brief Get the element of RowTensorType object.
///
/// \return The element of RowTensorType object.
const TypePtr element() const { return element_type_; }
/// \brief Set the element of RowTensorType object.
///
/// \param[in] element_type Define the element type to be set.
void set_element(const TypePtr &element_type) { element_type_ = element_type; }
TypePtr DeepCopy() const override;
@ -103,16 +152,32 @@ class MS_CORE_API RowTensorType : public Object {
};
using RowTensorTypePtr = std::shared_ptr<RowTensorType>;
/// \brief SparseTensorType defines interface for sparse tensor data type.
class MS_CORE_API SparseTensorType : public Object {
public:
/// \brief Default constructor for SparseTensorType.
SparseTensorType() : Object(kObjectTypeSparseTensorType, kObjectTypeUndeterminedType) {}
/// \brief Constructor for SparseTensorType.
///
/// \param[in] ele The element of SparseTensorType.
explicit SparseTensorType(const TypePtr &ele)
: Object(kObjectTypeSparseTensorType, kObjectTypeUndeterminedType, false), element_type_(ele) {}
/// \brief Destructor of SparseTensorType.
~SparseTensorType() override = default;
MS_DECLARE_PARENT(SparseTensorType, Object)
TypeId generic_type_id() const override { return kObjectTypeSparseTensorType; }
/// \brief Get the element of SparseTensorType object.
///
/// \return The element of SparseTensorType object.
const TypePtr element() const { return element_type_; }
/// \brief Set the element of SparseTensorType object.
///
/// \param[in] element_type Define the element type to be set.
void set_element(const TypePtr &element_type) { element_type_ = element_type; }
TypePtr DeepCopy() const override;

View File

@ -1,7 +1,7 @@
/**
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
*
* Copyright 2019-2020 Huawei Technologies Co., Ltd
* Copyright 2019-2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -42,6 +42,11 @@ TypeId IntBitsToTypeId(const int nbits);
TypeId UIntBitsToTypeId(const int nbits);
TypeId FloatBitsToTypeId(const int nbits);
TypeId ComplexBitsToTypeId(const int nbits);
/// \brief Get label of the input TypeId.
///
/// \param[in] v Define the input TypeId.
/// \return The label of input TypeId.
MS_CORE_API const std::string &TypeIdLabel(const TypeId &v);
TypeId NormalizeTypeId(const TypeId type_id);
bool IsSameObjectType(const Type &lhs, const Type &rhs);
@ -55,41 +60,119 @@ enum class BitsNum : int {
eBits128 = 128,
};
// Base class for all types
// forward declaration.
/// \brief Type defines an Value class for type.
class MS_CORE_API Type : public Value {
public:
/// \brief Default constructor for Type.
Type() : meta_type_(kMetaTypeType), is_generic_(true) {}
/// \brief Constructor for Type.
///
/// \param[in] t Define TypeId for Type object.
/// \param[in] is_generic Define whether the Type object is generic.
explicit Type(TypeId t, bool is_generic = true) : meta_type_(t), is_generic_(is_generic) {}
/// \brief Destructor of Type.
~Type() override = default;
MS_DECLARE_PARENT(Type, Value)
bool operator==(const Value &other) const override;
/// \brief Show the meta type of the Type object.
///
/// \return The meta type of the Type object.
TypeId meta_type() const { return meta_type_; }
/// \brief Show the type id of the Type object.
///
/// \return The type id of the Type object.
virtual TypeId type_id() const { return meta_type_; }
/// \brief Show the generic type id for the Number object.
///
/// \return The generic type id.
virtual TypeId generic_type_id() const { return kMetaTypeType; }
/// \brief Check whether the input is not the current Type object.
///
/// \param[in] other Define a Value object.
/// \return Check whether the current object and other object are different.
virtual bool operator!=(const Type &other) const { return !(*this == other); }
/// \brief Check whether the input is the current Type object.
///
/// \param[in] other Define a Value object.
/// \return Check whether the current object and other object have the same type id.
virtual bool operator==(const Type &other) const { return this->type_id() == other.type_id(); }
/// \brief Check whether the input is the current Type object.
///
/// \param[in] other Define a TypePtr.
/// \return Check whether the current object and other object are the same.
virtual bool equal(const TypePtr other) const { return *this == *other; }
/// \brief Get the object type of the Type object.
///
/// \return The object type of the Type object.
virtual TypeId object_type() const { return kTypeUnknown; }
/// \brief Get the parent type of the Type object.
///
/// \return The parent type of the Type object.
virtual TypeId parent_type() const { return kTypeUnknown; }
/// \brief Get the number type of the Type object.
///
/// \return The number type of the Type object.
virtual TypeId number_type() const { return kTypeUnknown; }
/// \brief Deep copy the Type object.
///
/// \return The deep copy of the Type object.
virtual TypePtr DeepCopy() const = 0;
/// \brief Clone the Type object.
///
/// \return The clone of the Type object.
virtual TypePtr Clone() const { return DeepCopy(); }
std::size_t hash() const override { return std::hash<int>{}(static_cast<int>(type_id())); }
std::string ToString() const override { return TypeIdLabel(meta_type_); }
/// \brief Get Type object ToReprString description.
///
/// \return The description of Type object.
virtual std::string ToReprString() const { return ToString(); }
/// \brief Get Type object ToReprString description.
///
/// \return The description of Type object.
std::string ReprString() const { return "mindspore." + ToReprString(); }
void dump() const override { std::cout << ToString() << std::endl; }
/// \brief Check whether the Type object is unknown.
///
/// \return whether the Type object is unknown.
bool IsUnknown() const { return (meta_type_ == kMetaTypeType); }
/// \brief Check whether the Type object is generic.
///
/// \return whether the Type object is generic.
bool IsGeneric() const { return is_generic_; }
abstract::AbstractBasePtr ToAbstract() override;
/// \brief Get Type object ToString description.
///
/// \param[in] os The ostream to receive the description
/// \param[in] type The Type object need to show the description
/// \return The ostream with Type object description
friend std::ostream &operator<<(std::ostream &os, const Type &type);
/// \brief Get Type object ToString description.
///
/// \param[in] os The ostream to receive the description
/// \param[in] type The TypePtr need to show the description
/// \return The ostream with Type object description
friend std::ostream &operator<<(std::ostream &os, const TypePtr type);
private:
@ -99,16 +182,28 @@ class MS_CORE_API Type : public Value {
using TypePtrList = std::vector<TypePtr>;
//
// Base class for normal objects
//
/// \brief Type defines an Type class for object.
class MS_CORE_API Object : public Type {
public:
/// \brief Default constructor for Object.
Object() : Type(kMetaTypeObject), object_type_(kMetaTypeObject), parent_type_(kMetaTypeObject) {}
/// \brief Constructor for Object.
///
/// \param[in] object_type Define object type for Object object.
/// \param[in] is_generic Define whether the Object object is generic.
explicit Object(const TypeId object_type, bool is_generic = true)
: Type(kMetaTypeObject, is_generic), object_type_(object_type), parent_type_(kMetaTypeObject) {}
/// \brief Constructor for Object.
///
/// \param[in] object_type Define object type for Object object.
/// \param[in] parent_type Define the parent type for Object object.
/// \param[in] is_generic Define whether the Object object is generic.
explicit Object(const TypeId object_type, const TypeId parent_type, bool is_generic = true)
: Type(kMetaTypeObject, is_generic), object_type_(object_type), parent_type_(parent_type) {}
/// \brief Destructor of Object.
~Object() override = default;
MS_DECLARE_PARENT(Object, Type)
@ -119,7 +214,18 @@ class MS_CORE_API Object : public Type {
bool equal(const TypePtr other) const override;
std::string ToString() const override { return std::string("Object:") + TypeIdLabel(object_type_); }
/// \brief Get Object object ToString description.
///
/// \param[in] os The ostream to receive the description
/// \param[in] obj The Object object need to show the description
/// \return The ostream with Object object description
friend std::ostream &operator<<(std::ostream &os, const Object &obj);
/// \brief Get Object object ToString description.
///
/// \param[in] os The ostream to receive the description
/// \param[in] obj The Object object need to show the description
/// \return The ostream with Object object description
friend std::ostream &operator<<(std::ostream &os, const std::shared_ptr<Object> obj);
private:
@ -140,6 +246,11 @@ const std::unordered_map<TypeId, int> type_priority_map = {
{kNumberTypeInt16, 3}, {kNumberTypeInt32, 4}, {kNumberTypeInt64, 5},
{kNumberTypeFloat16, 6}, {kNumberTypeFloat32, 7}, {kNumberTypeFloat64, 8}};
/// \brief Get TypePtrList description.
///
/// \param[in] os The ostream to receive the description
/// \param[in] types The TypePtrList need to show the description
/// \return The ostream with TypePtrList description
MS_CORE_API std::ostream &operator<<(std::ostream &os, const TypePtrList &types);
} // namespace mindspore