1 /**
2 * Copyright 2019-2020 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/value.h"
18
19 #include <algorithm>
20 #include <memory>
21 #include <cmath>
22 #include <cfloat>
23
24 #include "utils/convert_utils_base.h"
25
26 namespace mindspore {
operator [](const std::size_t & dim) const27 const ValuePtr ValueSequeue::operator[](const std::size_t &dim) const {
28 if (dim >= size()) {
29 MS_LOG(EXCEPTION) << "List index [" << dim << "] is out of range [" << size() << "].";
30 }
31 return elements_[dim];
32 }
33
erase(size_t idx)34 bool ValueSequeue::erase(size_t idx) {
35 if (idx < size()) {
36 (void)elements_.erase(elements_.begin() + SizeToInt(idx));
37 return true;
38 } else {
39 return false;
40 }
41 }
42
operator ==(const Value & other) const43 bool BoolImm::operator==(const Value &other) const {
44 if (other.isa<BoolImm>()) {
45 auto other_ = static_cast<const BoolImm &>(other);
46 return *this == other_;
47 } else {
48 return false;
49 }
50 }
operator ==(const BoolImm & other) const51 bool BoolImm::operator==(const BoolImm &other) const { return v_ == other.v_; }
52
operator ==(const Value & other) const53 bool Int8Imm::operator==(const Value &other) const {
54 if (other.isa<Int8Imm>()) {
55 auto other_ = static_cast<const Int8Imm &>(other);
56 return *this == other_;
57 } else {
58 return false;
59 }
60 }
operator ==(const Int8Imm & other) const61 bool Int8Imm::operator==(const Int8Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const62 bool Int16Imm::operator==(const Value &other) const {
63 if (other.isa<Int16Imm>()) {
64 auto other_ = static_cast<const Int16Imm &>(other);
65 return *this == other_;
66 } else {
67 return false;
68 }
69 }
operator ==(const Int16Imm & other) const70 bool Int16Imm::operator==(const Int16Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const71 bool Int32Imm::operator==(const Value &other) const {
72 if (other.isa<Int32Imm>()) {
73 auto other_ = static_cast<const Int32Imm &>(other);
74 return *this == other_;
75 } else {
76 return false;
77 }
78 }
operator ==(const Int32Imm & other) const79 bool Int32Imm::operator==(const Int32Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const80 bool Int64Imm::operator==(const Value &other) const {
81 if (other.isa<Int64Imm>()) {
82 auto other_ = static_cast<const Int64Imm &>(other);
83 return *this == other_;
84 } else {
85 return false;
86 }
87 }
operator ==(const Int64Imm & other) const88 bool Int64Imm::operator==(const Int64Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const89 bool UInt8Imm::operator==(const Value &other) const {
90 if (other.isa<UInt8Imm>()) {
91 auto other_ = static_cast<const UInt8Imm &>(other);
92 return *this == other_;
93 } else {
94 return false;
95 }
96 }
operator ==(const UInt8Imm & other) const97 bool UInt8Imm::operator==(const UInt8Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const98 bool UInt16Imm::operator==(const Value &other) const {
99 if (other.isa<UInt16Imm>()) {
100 auto other_ = static_cast<const UInt16Imm &>(other);
101 return *this == other_;
102 } else {
103 return false;
104 }
105 }
operator ==(const UInt16Imm & other) const106 bool UInt16Imm::operator==(const UInt16Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const107 bool UInt32Imm::operator==(const Value &other) const {
108 if (other.isa<UInt32Imm>()) {
109 auto other_ = static_cast<const UInt32Imm &>(other);
110 return *this == other_;
111 } else {
112 return false;
113 }
114 }
operator ==(const UInt32Imm & other) const115 bool UInt32Imm::operator==(const UInt32Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const116 bool UInt64Imm::operator==(const Value &other) const {
117 if (other.isa<UInt64Imm>()) {
118 auto other_ = static_cast<const UInt64Imm &>(other);
119 return *this == other_;
120 } else {
121 return false;
122 }
123 }
operator ==(const UInt64Imm & other) const124 bool UInt64Imm::operator==(const UInt64Imm &other) const { return v_ == other.v_; }
operator ==(const Value & other) const125 bool FP32Imm::operator==(const Value &other) const {
126 if (other.isa<FP32Imm>()) {
127 auto other_ = static_cast<const FP32Imm &>(other);
128 return *this == other_;
129 } else {
130 return false;
131 }
132 }
operator ==(const FP32Imm & other) const133 bool FP32Imm::operator==(const FP32Imm &other) const {
134 if (std::isinf(v_) && std::isinf(other.v_)) {
135 return true;
136 }
137 return fabs(v_ - other.v_) < FLT_EPSILON;
138 }
operator ==(const Value & other) const139 bool FP64Imm::operator==(const Value &other) const {
140 if (other.isa<FP64Imm>()) {
141 auto other_ = static_cast<const FP64Imm &>(other);
142 return *this == other_;
143 } else {
144 return false;
145 }
146 }
operator ==(const Value & other) const147 bool ValueSequeue::operator==(const Value &other) const {
148 if (other.isa<ValueSequeue>()) {
149 auto other_ = static_cast<const ValueSequeue &>(other);
150 return *this == other_;
151 } else {
152 return false;
153 }
154 }
operator ==(const ValueSequeue & other) const155 bool ValueSequeue::operator==(const ValueSequeue &other) const {
156 if (other.elements_.size() != elements_.size()) {
157 return false;
158 }
159 return std::equal(elements_.begin(), elements_.end(), other.elements_.begin(),
160 [](const ValuePtr &lhs, const ValuePtr &rhs) { return *lhs == *rhs; });
161 }
162
ToString() const163 std::string ValueSequeue::ToString() const {
164 std::ostringstream buffer;
165 bool begin = true;
166 for (auto &attr : elements_) {
167 if (!begin) {
168 buffer << ", ";
169 } else {
170 begin = false;
171 }
172 MS_EXCEPTION_IF_NULL(attr);
173 buffer << attr->ToString();
174 }
175 return buffer.str();
176 }
177
DumpText() const178 std::string ValueSequeue::DumpText() const {
179 std::ostringstream oss;
180 for (size_t i = 0; i < elements_.size(); ++i) {
181 MS_EXCEPTION_IF_NULL(elements_[i]);
182 oss << (i > 0 ? ", " : "") << elements_[i]->DumpText();
183 }
184 return oss.str();
185 }
186
operator ==(const FP64Imm & other) const187 bool FP64Imm::operator==(const FP64Imm &other) const {
188 if (std::isinf(v_) && std::isinf(other.v_)) {
189 return true;
190 }
191 return fabs(v_ - other.v_) < DBL_EPSILON;
192 }
operator ==(const Value & other) const193 bool StringImm::operator==(const Value &other) const {
194 if (other.isa<StringImm>()) {
195 auto other_ = static_cast<const StringImm &>(other);
196 return *this == other_;
197 } else {
198 return false;
199 }
200 }
operator ==(const StringImm & other) const201 bool StringImm::operator==(const StringImm &other) const { return str_ == other.str_; }
202
operator ==(const Value & other) const203 bool AnyValue::operator==(const Value &other) const {
204 if (other.isa<AnyValue>()) {
205 return true;
206 } else {
207 return false;
208 }
209 }
210
hash() const211 std::size_t ValueSlice::hash() const {
212 MS_EXCEPTION_IF_NULL(start_);
213 MS_EXCEPTION_IF_NULL(stop_);
214 MS_EXCEPTION_IF_NULL(step_);
215 return hash_combine({tid(), start_->hash(), stop_->hash(), step_->hash()});
216 }
217
operator ==(const Value & other) const218 bool ValueSlice::operator==(const Value &other) const {
219 if (other.isa<ValueSlice>()) {
220 auto other_ = static_cast<const ValueSlice &>(other);
221 return *this == other_;
222 } else {
223 return false;
224 }
225 }
226
operator ==(const ValueSlice & other) const227 bool ValueSlice::operator==(const ValueSlice &other) const {
228 MS_EXCEPTION_IF_NULL(start_);
229 MS_EXCEPTION_IF_NULL(stop_);
230 MS_EXCEPTION_IF_NULL(step_);
231 return (*start_ == *other.start_ && *stop_ == *other.stop_ && *step_ == *other.step_);
232 }
233
ToString() const234 std::string ValueSlice::ToString() const {
235 MS_EXCEPTION_IF_NULL(start_);
236 MS_EXCEPTION_IF_NULL(stop_);
237 MS_EXCEPTION_IF_NULL(step_);
238 std::ostringstream buffer;
239 buffer << "Slice[";
240 buffer << start_->ToString() << " : ";
241 buffer << stop_->ToString() << " : ";
242 buffer << step_->ToString();
243 buffer << "]";
244 return buffer.str();
245 }
246
hash() const247 std::size_t KeywordArg::hash() const {
248 MS_EXCEPTION_IF_NULL(value_);
249 return hash_combine({tid(), std::hash<std::string>{}(key_), value_->hash()});
250 }
251
operator ==(const Value & other) const252 bool KeywordArg::operator==(const Value &other) const {
253 if (other.isa<KeywordArg>()) {
254 auto other_ = static_cast<const KeywordArg &>(other);
255 return *this == other_;
256 } else {
257 return false;
258 }
259 }
260
operator ==(const KeywordArg & other) const261 bool KeywordArg::operator==(const KeywordArg &other) const { return (other.key_ == key_ && *other.value_ == *value_); }
262
ToString() const263 std::string KeywordArg::ToString() const {
264 std::ostringstream buffer;
265 buffer << "KeywordArg[";
266 buffer << "key : " << key_;
267 MS_EXCEPTION_IF_NULL(value_);
268 buffer << ", value : " << value_->ToString();
269 buffer << "]";
270 return buffer.str();
271 }
272
operator [](const std::string & key) const273 const ValuePtr ValueDictionary::operator[](const std::string &key) const {
274 auto it = std::find_if(key_values_.begin(), key_values_.end(),
275 [key](const std::pair<std::string, ValuePtr> &item) { return item.first == key; });
276 if (it == key_values_.end()) {
277 MS_LOG(EXCEPTION) << "The key " << key << " is not in the map";
278 }
279 return it->second;
280 }
281
operator ==(const Value & other) const282 bool ValueDictionary::operator==(const Value &other) const {
283 if (other.isa<ValueDictionary>()) {
284 auto other_ = static_cast<const ValueDictionary &>(other);
285 return *this == other_;
286 } else {
287 return false;
288 }
289 }
290
operator ==(const ValueDictionary & other) const291 bool ValueDictionary::operator==(const ValueDictionary &other) const {
292 if (key_values_.size() != other.key_values_.size()) {
293 return false;
294 }
295 for (size_t index = 0; index < key_values_.size(); index++) {
296 if (key_values_[index].first != other.key_values_[index].first) {
297 return false;
298 }
299 if (!(*key_values_[index].second == *other.key_values_[index].second)) {
300 return false;
301 }
302 }
303 return true;
304 }
305
operator ==(const Value & other) const306 bool UMonad::operator==(const Value &other) const { return other.isa<UMonad>(); }
307 const ValuePtr kUMonad = std::make_shared<UMonad>();
308
operator ==(const Value & other) const309 bool IOMonad::operator==(const Value &other) const { return other.isa<IOMonad>(); }
310 const ValuePtr kIOMonad = std::make_shared<IOMonad>();
311 } // namespace mindspore
312