• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ir/dtype/container.h"
18 #include <string>
19 #include <cstdlib>
20 #include <algorithm>
21 #include "utils/log_adapter.h"
22 
23 namespace mindspore {
DumpTypeVector(const std::vector<TypePtr> & elements,bool is_dumptext)24 static std::string DumpTypeVector(const std::vector<TypePtr> &elements, bool is_dumptext) {
25   std::ostringstream oss;
26   bool begin = true;
27   size_t cnt = 0;
28   // write 'Tuple[Bool, Bool, Bool, Int, Float, Float]' as 'Tuple[Bool...3, Int, Float...2]'
29   for (size_t i = 0; i < elements.size(); ++i) {
30     TypePtr elem = elements[i];
31     cnt += 1;
32     bool print = false;
33     if (i + 1 < elements.size()) {
34       TypePtr next = elements[i + 1];
35       if (*elem != *next) {
36         print = true;
37       }
38     } else {
39       // encounter last element
40       print = true;
41     }
42     if (!print) {
43       continue;
44     }
45     if (!begin) {
46       oss << ",";
47     } else {
48       begin = false;
49     }
50     oss << (is_dumptext ? elem->DumpText() : elem->ToString());
51     if (cnt > 1) {
52       oss << "*" << cnt;
53     }
54     cnt = 0;
55   }
56   return oss.str();
57 }
58 
DeepCopy() const59 TypePtr List::DeepCopy() const {
60   if (IsGeneric()) {
61     return std::make_shared<List>();
62   } else {
63     TypePtrList elements;
64     (void)std::transform(elements_.begin(), elements_.end(), std::back_inserter(elements),
65                          [](const TypePtr &ele) { return ele->DeepCopy(); });
66     auto copy = std::make_shared<List>(elements);
67     return copy;
68   }
69 }
70 
operator [](std::size_t dim) const71 const TypePtr List::operator[](std::size_t dim) const {
72   if (dim >= size()) {
73     MS_LOG(EXCEPTION) << "Out of the size of the List.";
74   }
75   return elements_[dim];
76 }
77 
operator ==(const Type & other) const78 bool List::operator==(const Type &other) const {
79   if (!IsSameObjectType(*this, other)) {
80     return false;
81   }
82   const List &other_list = static_cast<const List &>(other);
83   if (elements_.size() != other_list.elements_.size()) {
84     return false;
85   }
86   for (size_t i = 0; i < elements_.size(); ++i) {
87     if (*elements_[i] != *other_list.elements_[i]) {
88       return false;
89     }
90   }
91   return true;
92 }
93 
Class(const Named & tag,const ClassAttrVector & attributes,const std::unordered_map<std::string,ValuePtr> & methods)94 Class::Class(const Named &tag, const ClassAttrVector &attributes,
95              const std::unordered_map<std::string, ValuePtr> &methods)
96     : Object(kObjectTypeClass, false), attributes_(attributes), tag_(tag), methods_(methods) {}
97 
DumpContent(bool is_dumptext) const98 std::string List::DumpContent(bool is_dumptext) const {
99   std::ostringstream buffer;
100   if (IsGeneric()) {
101     buffer << "List";
102   } else {
103     buffer << "List[";
104     buffer << DumpTypeVector(elements_, is_dumptext);
105     buffer << "]";
106   }
107   return buffer.str();
108 }
109 
operator ==(const Type & other) const110 bool Class::operator==(const Type &other) const {
111   // Class is cached for each pyobj in ParseDataClass, so ClassPtr is one by one map to pyobj.
112   return &other == this;
113 }
114 
DeepCopy() const115 TypePtr Class::DeepCopy() const {
116   if (IsGeneric()) {
117     return std::make_shared<Class>();
118   } else {
119     auto copy = std::make_shared<Class>(tag_, attributes_, methods_);
120     return copy;
121   }
122 }
123 
DumpContent(bool is_dumptext) const124 std::string Class::DumpContent(bool is_dumptext) const {
125   std::ostringstream buffer;
126   if (IsGeneric()) {
127     buffer << "Cls";
128   } else {
129     bool begin = true;
130     buffer << "Cls." << tag_ << "[";
131     for (auto &attr : attributes_) {
132       if (!begin) {
133         buffer << ", ";
134       } else {
135         begin = false;
136       }
137       auto sub_content = is_dumptext ? attr.second->DumpText() : attr.second->ToString();
138       buffer << attr.first << ":" << sub_content;
139     }
140     buffer << "]";
141   }
142   return buffer.str();
143 }
144 
DeepCopy() const145 TypePtr Tuple::DeepCopy() const {
146   if (IsGeneric()) {
147     return std::make_shared<Tuple>();
148   } else {
149     TypePtrList elements;
150     (void)std::transform(elements_.begin(), elements_.end(), std::back_inserter(elements),
151                          [](const TypePtr &ele) { return ele->DeepCopy(); });
152     auto copy = std::make_shared<Tuple>(elements);
153     return copy;
154   }
155 }
156 
operator ==(const Type & other) const157 bool Tuple::operator==(const Type &other) const {
158   if (!IsSameObjectType(*this, other)) {
159     return false;
160   }
161   auto other_tuple = static_cast<const Tuple &>(other);
162   if (elements_.size() != other_tuple.elements_.size()) {
163     return false;
164   }
165   for (size_t i = 0; i < elements_.size(); ++i) {
166     if (*elements_[i] != *other_tuple.elements_[i]) {
167       return false;
168     }
169   }
170   return true;
171 }
172 
operator [](std::size_t dim) const173 const TypePtr Tuple::operator[](std::size_t dim) const {
174   if (dim >= size()) {
175     MS_LOG(EXCEPTION) << "Out of the size of the tuple.";
176   }
177   return elements_[dim];
178 }
179 
DumpContent(bool is_dumptext) const180 std::string Tuple::DumpContent(bool is_dumptext) const {
181   std::ostringstream buffer;
182   if (IsGeneric()) {
183     buffer << "Tuple";
184   } else {
185     buffer << "Tuple[";
186     buffer << DumpTypeVector(elements_, is_dumptext);
187     buffer << "]";
188   }
189   return buffer.str();
190 }
191 
DeepCopy() const192 TypePtr Dictionary::DeepCopy() const {
193   if (IsGeneric()) {
194     return std::make_shared<Dictionary>();
195   } else {
196     std::vector<std::pair<std::string, TypePtr>> kv;
197     (void)std::transform(
198       key_values_.begin(), key_values_.end(), std::back_inserter(kv),
199       [](const std::pair<std::string, TypePtr> &item) { return std::make_pair(item.first, item.second->DeepCopy()); });
200     return std::make_shared<Dictionary>(kv);
201   }
202 }
203 
DumpKeyVector(std::vector<std::string> keys)204 std::string DumpKeyVector(std::vector<std::string> keys) {
205   std::ostringstream buffer;
206   for (auto key : keys) {
207     buffer << key << ",";
208   }
209   return buffer.str();
210 }
211 
DumpContent(bool) const212 std::string Dictionary::DumpContent(bool) const {
213   std::ostringstream buffer;
214   std::vector<std::string> keys;
215   std::vector<TypePtr> values;
216   for (const auto &kv : key_values_) {
217     keys.push_back(kv.first);
218     values.push_back(kv.second);
219   }
220   if (IsGeneric()) {
221     buffer << "Dictionary";
222   } else {
223     buffer << "Dictionary[";
224     buffer << "[" << DumpKeyVector(keys) << "],";
225     buffer << "[" << DumpTypeVector(values, false) << "]";
226     buffer << "]";
227   }
228   return buffer.str();
229 }
230 
operator ==(const mindspore::Type & other) const231 bool Dictionary::operator==(const mindspore::Type &other) const {
232   if (!IsSameObjectType(*this, other)) {
233     return false;
234   }
235 
236   const auto &other_dict = static_cast<const Dictionary &>(other);
237   if (key_values_.size() != other_dict.key_values_.size()) {
238     return false;
239   }
240   for (size_t index = 0; index < key_values_.size(); index++) {
241     if (key_values_[index].first != other_dict.key_values_[index].first) {
242       return false;
243     }
244     if (*key_values_[index].second != *other_dict.key_values_[index].second) {
245       return false;
246     }
247   }
248   return true;
249 }
250 }  // namespace mindspore
251