1 /** 2 * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). 3 * 4 * Copyright 2019-2020 Huawei Technologies Co., Ltd 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #ifndef MINDSPORE_CORE_IR_DTYPE_TYPE_H_ 20 #define MINDSPORE_CORE_IR_DTYPE_TYPE_H_ 21 22 #include <cstddef> 23 #include <iostream> 24 #include <initializer_list> 25 #include <map> 26 #include <memory> 27 #include <utility> 28 #include <sstream> 29 #include <string> 30 #include <vector> 31 #include <type_traits> 32 #include <unordered_map> 33 #include <algorithm> 34 35 #include "base/base.h" 36 #include "ir/named.h" 37 #include "ir/dtype/type_id.h" 38 39 namespace mindspore { 40 41 TypeId IntBitsToTypeId(const int nbits); 42 TypeId UIntBitsToTypeId(const int nbits); 43 TypeId FloatBitsToTypeId(const int nbits); 44 TypeId ComplexBitsToTypeId(const int nbits); 45 MS_CORE_API const std::string &TypeIdLabel(const TypeId &v); 46 TypeId NormalizeTypeId(const TypeId type_id); 47 bool IsSameObjectType(const Type &lhs, const Type &rhs); 48 size_t GetTypeByte(const TypePtr &type_ptr); 49 50 enum class BitsNum : int { 51 eBits8 = 8, 52 eBits16 = 16, 53 eBits32 = 32, 54 eBits64 = 64, 55 eBits128 = 128, 56 }; 57 58 // Base class for all types 59 // forward declaration. 60 class MS_CORE_API Type : public Value { 61 public: Type()62 Type() : meta_type_(kMetaTypeType), is_generic_(true) {} meta_type_(t)63 explicit Type(TypeId t, bool is_generic = true) : meta_type_(t), is_generic_(is_generic) {} 64 ~Type() override = default; 65 MS_DECLARE_PARENT(Type, Value) 66 67 bool operator==(const Value &other) const override; meta_type()68 TypeId meta_type() const { return meta_type_; } 69 type_id()70 virtual TypeId type_id() const { return meta_type_; } generic_type_id()71 virtual TypeId generic_type_id() const { return kMetaTypeType; } 72 73 virtual bool operator!=(const Type &other) const { return !(*this == other); } 74 virtual bool operator==(const Type &other) const { return this->type_id() == other.type_id(); } equal(const TypePtr other)75 virtual bool equal(const TypePtr other) const { return *this == *other; } 76 object_type()77 virtual TypeId object_type() const { return kTypeUnknown; } parent_type()78 virtual TypeId parent_type() const { return kTypeUnknown; } number_type()79 virtual TypeId number_type() const { return kTypeUnknown; } 80 virtual TypePtr DeepCopy() const = 0; Clone()81 virtual TypePtr Clone() const { return DeepCopy(); } 82 hash()83 std::size_t hash() const override { return std::hash<int>{}(static_cast<int>(type_id())); } 84 ToString()85 std::string ToString() const override { return TypeIdLabel(meta_type_); } ToReprString()86 virtual std::string ToReprString() const { return ToString(); } ReprString()87 std::string ReprString() const { return "mindspore." + ToReprString(); } dump()88 void dump() const override { std::cout << ToString() << std::endl; } IsUnknown()89 bool IsUnknown() const { return (meta_type_ == kMetaTypeType); } IsGeneric()90 bool IsGeneric() const { return is_generic_; } 91 abstract::AbstractBasePtr ToAbstract() override; 92 friend std::ostream &operator<<(std::ostream &os, const Type &type); 93 friend std::ostream &operator<<(std::ostream &os, const TypePtr type); 94 95 private: 96 TypeId meta_type_; 97 bool is_generic_; 98 }; 99 100 using TypePtrList = std::vector<TypePtr>; 101 102 // 103 // Base class for normal objects 104 // 105 class MS_CORE_API Object : public Type { 106 public: Object()107 Object() : Type(kMetaTypeObject), object_type_(kMetaTypeObject), parent_type_(kMetaTypeObject) {} 108 explicit Object(const TypeId object_type, bool is_generic = true) Type(kMetaTypeObject,is_generic)109 : Type(kMetaTypeObject, is_generic), object_type_(object_type), parent_type_(kMetaTypeObject) {} 110 explicit Object(const TypeId object_type, const TypeId parent_type, bool is_generic = true) Type(kMetaTypeObject,is_generic)111 : Type(kMetaTypeObject, is_generic), object_type_(object_type), parent_type_(parent_type) {} 112 ~Object() override = default; MS_DECLARE_PARENT(Object,Type)113 MS_DECLARE_PARENT(Object, Type) 114 115 TypeId object_type() const override { return object_type_; } parent_type()116 TypeId parent_type() const override { return parent_type_; } type_id()117 TypeId type_id() const override { return object_type_; } generic_type_id()118 TypeId generic_type_id() const override { return kMetaTypeObject; } 119 bool equal(const TypePtr other) const override; ToString()120 std::string ToString() const override { return std::string("Object:") + TypeIdLabel(object_type_); } 121 122 friend std::ostream &operator<<(std::ostream &os, const Object &obj); 123 friend std::ostream &operator<<(std::ostream &os, const std::shared_ptr<Object> obj); 124 125 private: 126 const TypeId object_type_; 127 const TypeId parent_type_; 128 }; 129 130 // 131 // TypeId name map 132 // 133 const std::unordered_map<TypeId, std::string> type_name_map = { 134 {kNumberTypeBool, "bool_"}, {kNumberTypeInt8, "int8"}, {kNumberTypeUInt8, "uint8"}, 135 {kNumberTypeInt16, "int16"}, {kNumberTypeInt32, "int32"}, {kNumberTypeInt64, "int64"}, 136 {kNumberTypeFloat16, "float16"}, {kNumberTypeFloat32, "float32"}, {kNumberTypeFloat64, "float64"}}; 137 138 const std::unordered_map<TypeId, int> type_priority_map = { 139 {kNumberTypeBool, 0}, {kNumberTypeUInt8, 1}, {kNumberTypeInt8, 2}, 140 {kNumberTypeInt16, 3}, {kNumberTypeInt32, 4}, {kNumberTypeInt64, 5}, 141 {kNumberTypeFloat16, 6}, {kNumberTypeFloat32, 7}, {kNumberTypeFloat64, 8}}; 142 143 MS_CORE_API std::ostream &operator<<(std::ostream &os, const TypePtrList &types); 144 } // namespace mindspore 145 146 #endif // MINDSPORE_CORE_IR_DTYPE_TYPE_H_ 147