1 /** 2 * Copyright 2019 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CORE_IR_DTYPE_NUMBER_H_ 18 #define MINDSPORE_CORE_IR_DTYPE_NUMBER_H_ 19 20 #include <cstddef> 21 #include <iostream> 22 #include <initializer_list> 23 #include <map> 24 #include <memory> 25 #include <utility> 26 #include <sstream> 27 #include <string> 28 #include <vector> 29 #include <type_traits> 30 #include <unordered_map> 31 #include <algorithm> 32 #include "base/base.h" 33 #include "ir/named.h" 34 #include "ir/dtype/type.h" 35 36 namespace mindspore { 37 // Number, abstract class. 38 class MS_CORE_API Number : public Object { 39 public: Number()40 Number() : Object(kObjectTypeNumber), number_type_(kObjectTypeNumber), nbits_(0) {} 41 Number(const TypeId number_type, const int nbits, bool is_generic = true) Object(kObjectTypeNumber,is_generic)42 : Object(kObjectTypeNumber, is_generic), number_type_(number_type), nbits_(nbits) {} 43 ~Number() override = default; MS_DECLARE_PARENT(Number,Object)44 MS_DECLARE_PARENT(Number, Object) 45 46 int nbits() const { return nbits_; } 47 number_type()48 TypeId number_type() const override { return number_type_; } type_id()49 TypeId type_id() const override { return number_type_; } generic_type_id()50 TypeId generic_type_id() const override { return kObjectTypeNumber; } 51 52 bool operator==(const Type &other) const override; DeepCopy()53 TypePtr DeepCopy() const override { return std::make_shared<Number>(); } ToString()54 std::string ToString() const override { return "Number"; } ToReprString()55 std::string ToReprString() const override { return "number"; } DumpText()56 std::string DumpText() const override { return "Number"; } GetTypeName(const std::string & type_name)57 std::string GetTypeName(const std::string &type_name) const { 58 std::ostringstream oss; 59 oss << type_name; 60 if (nbits() != 0) { 61 oss << nbits(); 62 } 63 return oss.str(); 64 } 65 66 private: 67 const TypeId number_type_; 68 const int nbits_; 69 }; 70 71 using NumberPtr = std::shared_ptr<Number>; 72 73 // Bool 74 class MS_CORE_API Bool : public Number { 75 public: Bool()76 Bool() : Number(kNumberTypeBool, 8) {} 77 ~Bool() override = default; MS_DECLARE_PARENT(Bool,Number)78 MS_DECLARE_PARENT(Bool, Number) 79 80 TypeId generic_type_id() const override { return kNumberTypeBool; } DeepCopy()81 TypePtr DeepCopy() const override { return std::make_shared<Bool>(); } ToString()82 std::string ToString() const override { return "Bool"; } ToReprString()83 std::string ToReprString() const override { return "bool_"; } DumpText()84 std::string DumpText() const override { return "Bool"; } 85 }; 86 87 // Int 88 class MS_CORE_API Int : public Number { 89 public: Int()90 Int() : Number(kNumberTypeInt, 0) {} 91 explicit Int(const int nbits); 92 ~Int() override = default; MS_DECLARE_PARENT(Int,Number)93 MS_DECLARE_PARENT(Int, Number) 94 TypeId generic_type_id() const override { return kNumberTypeInt; } DeepCopy()95 TypePtr DeepCopy() const override { 96 if (nbits() == 0) { 97 return std::make_shared<Int>(); 98 } 99 return std::make_shared<Int>(nbits()); 100 } ToString()101 std::string ToString() const override { return GetTypeName("Int"); } ToReprString()102 std::string ToReprString() const override { return nbits() == 0 ? "int_" : GetTypeName("int"); } DumpText()103 std::string DumpText() const override { 104 return nbits() == 0 ? std::string("Int") : std::string("I") + std::to_string(nbits()); 105 } 106 }; 107 108 // UInt 109 class MS_CORE_API UInt : public Number { 110 public: UInt()111 UInt() : Number(kNumberTypeUInt, 0) {} 112 explicit UInt(const int nbits); generic_type_id()113 TypeId generic_type_id() const override { return kNumberTypeUInt; } 114 ~UInt()115 ~UInt() override {} MS_DECLARE_PARENT(UInt,Number)116 MS_DECLARE_PARENT(UInt, Number) 117 118 TypePtr DeepCopy() const override { 119 if (nbits() == 0) { 120 return std::make_shared<UInt>(); 121 } 122 return std::make_shared<UInt>(nbits()); 123 } ToString()124 std::string ToString() const override { return GetTypeName("UInt"); } ToReprString()125 std::string ToReprString() const override { return GetTypeName("uint"); } DumpText()126 std::string DumpText() const override { 127 return nbits() == 0 ? std::string("UInt") : std::string("U") + std::to_string(nbits()); 128 } 129 }; 130 131 // Float 132 class MS_CORE_API Float : public Number { 133 public: Float()134 Float() : Number(kNumberTypeFloat, 0) {} 135 explicit Float(const int nbits); ~Float()136 ~Float() override {} MS_DECLARE_PARENT(Float,Number)137 MS_DECLARE_PARENT(Float, Number) 138 139 TypeId generic_type_id() const override { return kNumberTypeFloat; } DeepCopy()140 TypePtr DeepCopy() const override { 141 if (nbits() == 0) { 142 return std::make_shared<Float>(); 143 } 144 return std::make_shared<Float>(nbits()); 145 } ToString()146 std::string ToString() const override { return GetTypeName("Float"); } ToReprString()147 std::string ToReprString() const override { return nbits() == 0 ? "float_" : GetTypeName("float"); } DumpText()148 std::string DumpText() const override { 149 return nbits() == 0 ? std::string("Float") : std::string("F") + std::to_string(nbits()); 150 } 151 }; 152 153 // Complex 154 class MS_CORE_API Complex : public Number { 155 public: Complex()156 Complex() : Number(kNumberTypeComplex64, 64, false) {} 157 explicit Complex(const int nbits); ~Complex()158 ~Complex() override {} MS_DECLARE_PARENT(Complex,Number)159 MS_DECLARE_PARENT(Complex, Number) 160 161 TypeId generic_type_id() const override { return kNumberTypeComplex64; } DeepCopy()162 TypePtr DeepCopy() const override { return std::make_shared<Complex>(nbits()); } ToString()163 std::string ToString() const override { return GetTypeName("Complex"); } ToReprString()164 std::string ToReprString() const override { return GetTypeName("complex"); } DumpText()165 std::string DumpText() const override { return std::string("C") + std::to_string(nbits()); } 166 }; 167 168 inline const TypePtr kBool = std::make_shared<Bool>(); 169 inline const TypePtr kInt8 = std::make_shared<Int>(static_cast<int>(BitsNum::eBits8)); 170 inline const TypePtr kInt16 = std::make_shared<Int>(static_cast<int>(BitsNum::eBits16)); 171 inline const TypePtr kInt32 = std::make_shared<Int>(static_cast<int>(BitsNum::eBits32)); 172 inline const TypePtr kInt64 = std::make_shared<Int>(static_cast<int>(BitsNum::eBits64)); 173 inline const TypePtr kUInt8 = std::make_shared<UInt>(static_cast<int>(BitsNum::eBits8)); 174 inline const TypePtr kUInt16 = std::make_shared<UInt>(static_cast<int>(BitsNum::eBits16)); 175 inline const TypePtr kUInt32 = std::make_shared<UInt>(static_cast<int>(BitsNum::eBits32)); 176 inline const TypePtr kUInt64 = std::make_shared<UInt>(static_cast<int>(BitsNum::eBits64)); 177 inline const TypePtr kFloat16 = std::make_shared<Float>(static_cast<int>(BitsNum::eBits16)); 178 inline const TypePtr kFloat32 = std::make_shared<Float>(static_cast<int>(BitsNum::eBits32)); 179 inline const TypePtr kFloat64 = std::make_shared<Float>(static_cast<int>(BitsNum::eBits64)); 180 inline const TypePtr kInt = std::make_shared<Int>(); 181 inline const TypePtr kUInt = std::make_shared<UInt>(); 182 inline const TypePtr kFloat = std::make_shared<Float>(); 183 inline const TypePtr kNumber = std::make_shared<Number>(); 184 inline const TypePtr kComplex64 = std::make_shared<Complex>(static_cast<int>(BitsNum::eBits64)); 185 inline const TypePtr kComplex128 = std::make_shared<Complex>(static_cast<int>(BitsNum::eBits128)); 186 } // namespace mindspore 187 188 #endif // MINDSPORE_CORE_IR_DTYPE_NUMBER_H_ 189