1 /** 2 * Copyright 2019-2022 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 <algorithm> 31 #include "utils/hash_map.h" 32 #include "base/base.h" 33 #include "ir/named.h" 34 #include "ir/dtype/type.h" 35 36 namespace mindspore { 37 /// \brief List defines interface for list data type. 38 class MS_CORE_API List final : public Object { 39 public: 40 /// \brief Default constructor for List. List()41 List() : Object(kObjectTypeList) {} 42 43 /// \brief Constructor for List. 44 /// 45 /// \param[in] objs The elements of List. List(const std::initializer_list<TypePtr> & objs)46 List(const std::initializer_list<TypePtr> &objs) 47 : Object(kObjectTypeList, false), elements_(objs.begin(), objs.end()) {} 48 49 /// \brief Shadow copy function for List. 50 /// 51 /// \param[in] obj TypePtrList to be copied. List(const TypePtrList & obj)52 explicit List(const TypePtrList &obj) : Object(kObjectTypeList, false), elements_(obj) {} 53 54 /// \brief Destructor of List. ~List()55 ~List() override {} MS_DECLARE_PARENT(List,Object)56 MS_DECLARE_PARENT(List, Object) 57 58 TypeId generic_type_id() const override { return kObjectTypeList; } 59 TypePtr DeepCopy() const override; ToReprString()60 std::string ToReprString() const override { return "list_"; } ToString()61 std::string ToString() const override { return DumpContent(false); } DumpText()62 std::string DumpText() const override { return DumpContent(true); } 63 64 /// \brief Get type of List element. 65 /// 66 /// \param[in] dim Define the index of List element. 67 /// \return TypePtr of List element. 68 const TypePtr operator[](std::size_t dim) const; 69 70 bool operator==(const Type &other) const override; 71 72 std::size_t hash() const override; 73 74 /// \brief Get the number of elements in this List. 75 /// 76 /// \return The number of elements in this List. size()77 std::size_t size() const { return elements_.size(); } 78 79 /// \brief Get the elements of List object. 80 /// 81 /// \return The elements of List object. elements()82 TypePtrList elements() const { return elements_; } 83 84 /// \brief Set the elements of List object. 85 /// 86 /// \param[in] elements Define the element types to be set. set_elements(TypePtrList && elements)87 void set_elements(TypePtrList &&elements) { elements_ = std::move(elements); } 88 89 /// \brief Determine whether the list is dynamic length. 90 /// 91 /// \return Whether the list is dynamic length. dynamic_len()92 bool dynamic_len() const { return dynamic_len_; } 93 94 /// \brief Set whether the list is dynamic length. 95 /// 96 /// \param[in] dynamic_len bool value indicate whether the sequence is dynamic length. set_dynamic_len(bool dynamic_len)97 void set_dynamic_len(bool dynamic_len) { dynamic_len_ = dynamic_len; } 98 99 /// \brief Get the element type when the list is dynamic length. 100 /// 101 /// \return Whether the list is dynamic length. 102 TypePtr dynamic_element_type() const; 103 104 /// \brief Set the element type when the list is dynamic length. 105 /// 106 /// \param[in] dynamic_element_type type of element for dynamic length list. 107 void set_dynamic_element_type(const TypePtr &dynamic_element_type); 108 109 private: 110 /// \brief Show each element. 111 /// 112 /// \param[in] is_dumptext whether to show each element DumpText 113 /// \return The description of the List object. 114 std::string DumpContent(bool is_dumptext) const; 115 TypePtrList elements_; 116 bool dynamic_len_ = false; 117 TypePtr dynamic_element_type_ = nullptr; 118 }; 119 using ListPtr = std::shared_ptr<List>; 120 121 /// \brief Tuple defines interface for tuple data type. 122 class MS_CORE_API Tuple final : public Object { 123 public: 124 /// \brief Default constructor for Tuple. Tuple()125 Tuple() : Object(kObjectTypeTuple) {} 126 127 /// \brief Constructor for Tuple. 128 /// 129 /// \param[in] objs The elements of Tuple. Tuple(const std::initializer_list<TypePtr> & objs)130 Tuple(const std::initializer_list<TypePtr> &objs) 131 : Object(kObjectTypeTuple, false), elements_(objs.begin(), objs.end()) {} 132 133 /// \brief Shadow copy function for Tuple. 134 /// 135 /// \param[in] objs TypePtrList to be copied. Tuple(const TypePtrList & objs)136 explicit Tuple(const TypePtrList &objs) : Object(kObjectTypeTuple, false), elements_(objs.begin(), objs.end()) {} 137 138 /// \brief Destructor of Tuple. ~Tuple()139 ~Tuple() override {} MS_DECLARE_PARENT(Tuple,Object)140 MS_DECLARE_PARENT(Tuple, Object) 141 142 TypeId generic_type_id() const override { return kObjectTypeTuple; } 143 TypePtr DeepCopy() const override; ToReprString()144 std::string ToReprString() const override { return "tuple_"; } ToString()145 std::string ToString() const override { return DumpContent(false); } DumpText()146 std::string DumpText() const override { return DumpContent(true); } 147 148 /// \brief Get type of Tuple element. 149 /// 150 /// \param[in] dim Define the index of Tuple element. 151 /// \return TypePtr of Tuple element. 152 const TypePtr operator[](std::size_t dim) const; 153 154 bool operator==(const Type &other) const override; 155 156 std::size_t hash() const override; 157 158 /// \brief Get the elements of the Tuple object. 159 /// 160 /// \return The elements of the Tuple object. elements()161 const TypePtrList &elements() const { return elements_; } 162 163 /// \brief Set the elements of Tuple object. 164 /// 165 /// \param[in] elements Define the element types to be set. set_elements(TypePtrList && elements)166 void set_elements(TypePtrList &&elements) { elements_ = std::move(elements); } 167 168 /// \brief Get the number of elements in the Tuple object. 169 /// 170 /// \return The number of elements in the Tuple object. size()171 std::size_t size() const { return elements_.size(); } 172 173 /// \brief Determine whether the tuple is dynamic length. 174 /// 175 /// \return Whether the tuple is dynamic length. dynamic_len()176 bool dynamic_len() const { return dynamic_len_; } 177 178 /// \brief Set whether the tuple is dynamic length. 179 /// 180 /// \param[in] dynamic_len bool value indicate whether the sequence is dynamic length. set_dynamic_len(bool dynamic_len)181 void set_dynamic_len(bool dynamic_len) { dynamic_len_ = dynamic_len; } 182 183 /// \brief Get the element type when the tuple is dynamic length. 184 /// 185 /// \return Whether the tuple is dynamic length. 186 TypePtr dynamic_element_type() const; 187 188 /// \brief Set the element type when the tuple is dynamic length. 189 /// 190 /// \param[in] dynamic_element_type type of element for dynamic length tuple. 191 void set_dynamic_element_type(const TypePtr &dynamic_element_type); 192 193 private: 194 /// \brief Show each element. 195 /// 196 /// \param[in] is_dumptext whether to show each element DumpText 197 /// \return The description of the Tuple object. 198 std::string DumpContent(bool is_dumptext) const; 199 TypePtrList elements_; 200 bool dynamic_len_ = false; 201 TypePtr dynamic_element_type_ = nullptr; 202 }; 203 using TuplePtr = std::shared_ptr<Tuple>; 204 205 /// \brief Dictionary defines interface for dictionary data type. 206 class MS_CORE_API Dictionary final : public Object { 207 public: 208 /// \brief Default constructor for Dictionary. Dictionary()209 Dictionary() : Object(kObjectTypeDictionary) {} 210 211 /// \brief Constructor for Dictionary. 212 /// 213 /// \param[in] key_values The elements of Dictionary. Dictionary(const std::vector<std::pair<ValuePtr,TypePtr>> & key_values)214 explicit Dictionary(const std::vector<std::pair<ValuePtr, TypePtr>> &key_values) 215 : Object(kObjectTypeDictionary, false), key_values_(key_values) {} 216 217 /// \brief Destructor of Dictionary. 218 ~Dictionary() override = default; MS_DECLARE_PARENT(Dictionary,Object)219 MS_DECLARE_PARENT(Dictionary, Object) 220 221 TypeId generic_type_id() const override { return kObjectTypeDictionary; } 222 bool operator==(const Type &other) const override; 223 std::size_t hash() const override; 224 TypePtr DeepCopy() const override; ToString()225 std::string ToString() const override { return DumpContent(false); } DumpText()226 std::string DumpText() const override { return DumpContent(true); } 227 228 /// \brief Get the keys and values. 229 /// 230 /// \return A vector of pairs of ValuePtr and TypePtr. key_values()231 const std::vector<std::pair<ValuePtr, TypePtr>> &key_values() const { return key_values_; } 232 233 private: 234 /// \brief Show each element. 235 /// 236 /// \param[in] is_dumptext whether to show each element DumpText 237 /// \return The description of the Dictionary object. 238 std::string DumpContent(bool) const; 239 std::vector<std::pair<ValuePtr, TypePtr>> key_values_; 240 }; 241 using DictionaryPtr = std::shared_ptr<Dictionary>; 242 } // namespace mindspore 243 244 #endif // MINDSPORE_CORE_IR_DTYPE_CONTAINER_H_ 245