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_CONTAINER_H_ 18 #define MINDSPORE_CORE_IR_DTYPE_CONTAINER_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 // TypeRefKey type 38 39 // List 40 class MS_CORE_API List : public Object { 41 public: List()42 List() : Object(kObjectTypeList) {} List(const std::initializer_list<TypePtr> & objs)43 List(const std::initializer_list<TypePtr> &objs) 44 : Object(kObjectTypeList, false), elements_(objs.begin(), objs.end()) {} 45 // Shadow copy; List(const TypePtrList & obj)46 explicit List(const TypePtrList &obj) : Object(kObjectTypeList, false), elements_(obj) {} ~List()47 ~List() override {} 48 MS_DECLARE_PARENT(List, Object) 49 50 const TypePtr operator[](size_t dim) const; generic_type_id()51 TypeId generic_type_id() const override { return kObjectTypeList; } 52 TypePtr DeepCopy() const override; 53 54 bool operator==(const Type &other) const override; size()55 std::size_t size() const { return elements_.size(); } elements()56 TypePtrList elements() const { return elements_; } ToReprString()57 std::string ToReprString() const override { return "list_"; } ToString()58 std::string ToString() const override { return DumpContent(false); } DumpText()59 std::string DumpText() const override { return DumpContent(true); }; 60 61 private: 62 std::string DumpContent(bool is_dumptext) const; 63 TypePtrList elements_; 64 }; 65 using ListPtr = std::shared_ptr<List>; 66 67 using ClassAttrVector = std::vector<std::pair<std::string, TypePtr>>; 68 69 class MS_CORE_API Class : public Object { 70 public: Class()71 Class() : Object(kObjectTypeClass), tag_(Named("Class")) {} 72 Class(const Named &tag, const ClassAttrVector &attributes, const std::unordered_map<std::string, ValuePtr> &methods); ~Class()73 ~Class() override {} MS_DECLARE_PARENT(Class,Object)74 MS_DECLARE_PARENT(Class, Object) 75 76 TypeId generic_type_id() const override { return kObjectTypeClass; } 77 78 bool operator==(const Type &other) const override; 79 TypePtr DeepCopy() const override; ToString()80 std::string ToString() const override { return DumpContent(false); } DumpText()81 std::string DumpText() const override { return DumpContent(true); }; set_value(const std::unordered_map<std::string,ValuePtr> & v)82 void set_value(const std::unordered_map<std::string, ValuePtr> &v) { attributes_value_ = v; } 83 tag()84 Named tag() { return tag_; } GetValue()85 std::unordered_map<std::string, ValuePtr> GetValue() { return attributes_value_; } methods()86 std::unordered_map<std::string, ValuePtr> methods() { return methods_; } GetAttributes()87 ClassAttrVector &GetAttributes() { return attributes_; } 88 89 ClassAttrVector attributes_; 90 91 private: 92 std::string DumpContent(bool is_dumptext) const; 93 Named tag_; 94 std::unordered_map<std::string, ValuePtr> methods_; 95 // For AbstractClass build value 96 std::unordered_map<std::string, ValuePtr> attributes_value_; 97 }; 98 using ClassPtr = std::shared_ptr<Class>; 99 100 class MS_CORE_API Tuple : public Object { 101 public: Tuple()102 Tuple() : Object(kObjectTypeTuple) {} 103 // usage : Tuple t = {std::make_shared<Bool>(), std::make_shared<Int>(32)}; Tuple(const std::initializer_list<TypePtr> & objs)104 Tuple(const std::initializer_list<TypePtr> &objs) 105 : Object(kObjectTypeTuple, false), elements_(objs.begin(), objs.end()) {} 106 107 // Shadow copy Tuple(const TypePtrList & objs)108 explicit Tuple(const TypePtrList &objs) : Object(kObjectTypeTuple, false), elements_(objs.begin(), objs.end()) {} 109 ~Tuple()110 ~Tuple() override {} MS_DECLARE_PARENT(Tuple,Object)111 MS_DECLARE_PARENT(Tuple, Object) 112 113 TypeId generic_type_id() const override { return kObjectTypeTuple; } 114 TypePtr DeepCopy() const override; 115 ToReprString()116 std::string ToReprString() const override { return "tuple_"; } ToString()117 std::string ToString() const override { return DumpContent(false); } DumpText()118 std::string DumpText() const override { return DumpContent(true); }; 119 const TypePtr operator[](size_t dim) const; 120 bool operator==(const Type &other) const override; 121 elements()122 TypePtrList elements() const { return elements_; } size()123 std::size_t size() const { return elements_.size(); } 124 125 private: 126 std::string DumpContent(bool is_dumptext) const; 127 TypePtrList elements_; 128 }; 129 using TuplePtr = std::shared_ptr<Tuple>; 130 131 class MS_CORE_API Dictionary : public Object { 132 public: Dictionary()133 Dictionary() : Object(kObjectTypeDictionary) {} Dictionary(const std::vector<std::pair<std::string,TypePtr>> & key_values)134 explicit Dictionary(const std::vector<std::pair<std::string, TypePtr>> &key_values) 135 : Object(kObjectTypeDictionary, false), key_values_(key_values) {} 136 ~Dictionary()137 ~Dictionary() override {} MS_DECLARE_PARENT(Dictionary,Object)138 MS_DECLARE_PARENT(Dictionary, Object) 139 140 TypeId generic_type_id() const override { return kObjectTypeDictionary; } 141 142 bool operator==(const Type &other) const override; 143 TypePtr DeepCopy() const override; ToString()144 std::string ToString() const override { return DumpContent(false); } DumpText()145 std::string DumpText() const override { return DumpContent(true); }; 146 147 private: 148 std::string DumpContent(bool is_dumptext) const; 149 std::vector<std::pair<std::string, TypePtr>> key_values_; 150 }; 151 using DictionaryPtr = std::shared_ptr<Dictionary>; 152 } // namespace mindspore 153 154 #endif // MINDSPORE_CORE_IR_DTYPE_CONTAINER_H_ 155