You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

value.cc 9.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include "ir/value.h"
  17. #include <algorithm>
  18. #include <memory>
  19. #include <cmath>
  20. #include <cfloat>
  21. #include "utils/convert_utils_base.h"
  22. namespace mindspore {
  23. const ValuePtr ValueSequeue::operator[](const std::size_t &dim) const {
  24. if (dim >= size()) {
  25. MS_LOG(EXCEPTION) << "List index [" << dim << "] is out of range [" << size() << "].";
  26. }
  27. return elements_[dim];
  28. }
  29. bool ValueSequeue::erase(size_t idx) {
  30. if (idx < size()) {
  31. (void)elements_.erase(elements_.begin() + SizeToInt(idx));
  32. return true;
  33. } else {
  34. return false;
  35. }
  36. }
  37. bool BoolImm::operator==(const Value &other) const {
  38. if (other.isa<BoolImm>()) {
  39. auto other_ = static_cast<const BoolImm &>(other);
  40. return *this == other_;
  41. } else {
  42. return false;
  43. }
  44. }
  45. bool BoolImm::operator==(const BoolImm &other) const { return v_ == other.v_; }
  46. bool Int8Imm::operator==(const Value &other) const {
  47. if (other.isa<Int8Imm>()) {
  48. auto other_ = static_cast<const Int8Imm &>(other);
  49. return *this == other_;
  50. } else {
  51. return false;
  52. }
  53. }
  54. bool Int8Imm::operator==(const Int8Imm &other) const { return v_ == other.v_; }
  55. bool Int16Imm::operator==(const Value &other) const {
  56. if (other.isa<Int16Imm>()) {
  57. auto other_ = static_cast<const Int16Imm &>(other);
  58. return *this == other_;
  59. } else {
  60. return false;
  61. }
  62. }
  63. bool Int16Imm::operator==(const Int16Imm &other) const { return v_ == other.v_; }
  64. bool Int32Imm::operator==(const Value &other) const {
  65. if (other.isa<Int32Imm>()) {
  66. auto other_ = static_cast<const Int32Imm &>(other);
  67. return *this == other_;
  68. } else {
  69. return false;
  70. }
  71. }
  72. bool Int32Imm::operator==(const Int32Imm &other) const { return v_ == other.v_; }
  73. bool Int64Imm::operator==(const Value &other) const {
  74. if (other.isa<Int64Imm>()) {
  75. auto other_ = static_cast<const Int64Imm &>(other);
  76. return *this == other_;
  77. } else {
  78. return false;
  79. }
  80. }
  81. bool Int64Imm::operator==(const Int64Imm &other) const { return v_ == other.v_; }
  82. bool UInt8Imm::operator==(const Value &other) const {
  83. if (other.isa<UInt8Imm>()) {
  84. auto other_ = static_cast<const UInt8Imm &>(other);
  85. return *this == other_;
  86. } else {
  87. return false;
  88. }
  89. }
  90. bool UInt8Imm::operator==(const UInt8Imm &other) const { return v_ == other.v_; }
  91. bool UInt16Imm::operator==(const Value &other) const {
  92. if (other.isa<UInt16Imm>()) {
  93. auto other_ = static_cast<const UInt16Imm &>(other);
  94. return *this == other_;
  95. } else {
  96. return false;
  97. }
  98. }
  99. bool UInt16Imm::operator==(const UInt16Imm &other) const { return v_ == other.v_; }
  100. bool UInt32Imm::operator==(const Value &other) const {
  101. if (other.isa<UInt32Imm>()) {
  102. auto other_ = static_cast<const UInt32Imm &>(other);
  103. return *this == other_;
  104. } else {
  105. return false;
  106. }
  107. }
  108. bool UInt32Imm::operator==(const UInt32Imm &other) const { return v_ == other.v_; }
  109. bool UInt64Imm::operator==(const Value &other) const {
  110. if (other.isa<UInt64Imm>()) {
  111. auto other_ = static_cast<const UInt64Imm &>(other);
  112. return *this == other_;
  113. } else {
  114. return false;
  115. }
  116. }
  117. bool UInt64Imm::operator==(const UInt64Imm &other) const { return v_ == other.v_; }
  118. bool FP32Imm::operator==(const Value &other) const {
  119. if (other.isa<FP32Imm>()) {
  120. auto other_ = static_cast<const FP32Imm &>(other);
  121. return *this == other_;
  122. } else {
  123. return false;
  124. }
  125. }
  126. bool FP32Imm::operator==(const FP32Imm &other) const { return fabs(v_ - other.v_) < FLT_EPSILON; }
  127. bool FP64Imm::operator==(const Value &other) const {
  128. if (other.isa<FP64Imm>()) {
  129. auto other_ = static_cast<const FP64Imm &>(other);
  130. return *this == other_;
  131. } else {
  132. return false;
  133. }
  134. }
  135. bool ValueSequeue::operator==(const Value &other) const {
  136. if (other.isa<ValueSequeue>()) {
  137. auto other_ = static_cast<const ValueSequeue &>(other);
  138. return *this == other_;
  139. } else {
  140. return false;
  141. }
  142. }
  143. bool ValueSequeue::operator==(const ValueSequeue &other) const {
  144. if (other.elements_.size() != elements_.size()) {
  145. return false;
  146. }
  147. return std::equal(elements_.begin(), elements_.end(), other.elements_.begin(),
  148. [](const ValuePtr &lhs, const ValuePtr &rhs) { return *lhs == *rhs; });
  149. }
  150. std::string ValueSequeue::ToString() const {
  151. std::ostringstream buffer;
  152. bool begin = true;
  153. for (auto &attr : elements_) {
  154. if (!begin) {
  155. buffer << ", ";
  156. } else {
  157. begin = false;
  158. }
  159. MS_EXCEPTION_IF_NULL(attr);
  160. buffer << attr->ToString();
  161. }
  162. return buffer.str();
  163. }
  164. std::string ValueSequeue::DumpText() const {
  165. std::ostringstream oss;
  166. for (size_t i = 0; i < elements_.size(); ++i) {
  167. MS_EXCEPTION_IF_NULL(elements_[i]);
  168. oss << (i > 0 ? ", " : "") << elements_[i]->DumpText();
  169. }
  170. return oss.str();
  171. }
  172. bool FP64Imm::operator==(const FP64Imm &other) const { return fabs(v_ - other.v_) < DBL_EPSILON; }
  173. bool StringImm::operator==(const Value &other) const {
  174. if (other.isa<StringImm>()) {
  175. auto other_ = static_cast<const StringImm &>(other);
  176. return *this == other_;
  177. } else {
  178. return false;
  179. }
  180. }
  181. bool StringImm::operator==(const StringImm &other) const { return str_ == other.str_; }
  182. bool RefKey::operator==(const Value &other) const {
  183. if (other.isa<RefKey>()) {
  184. auto other_ = static_cast<const RefKey &>(other);
  185. return *this == other_;
  186. } else {
  187. return false;
  188. }
  189. }
  190. bool RefKey::operator==(const RefKey &other) const { return tag_ == other.tag_; }
  191. bool AnyValue::operator==(const Value &other) const {
  192. if (other.isa<AnyValue>()) {
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. }
  198. const ValuePtr kAnyValue = std::make_shared<AnyValue>();
  199. std::size_t ValueSlice::hash() const {
  200. MS_EXCEPTION_IF_NULL(start_);
  201. MS_EXCEPTION_IF_NULL(stop_);
  202. MS_EXCEPTION_IF_NULL(step_);
  203. return hash_combine({tid(), start_->hash(), stop_->hash(), step_->hash()});
  204. }
  205. bool ValueSlice::operator==(const Value &other) const {
  206. if (other.isa<ValueSlice>()) {
  207. auto other_ = static_cast<const ValueSlice &>(other);
  208. return *this == other_;
  209. } else {
  210. return false;
  211. }
  212. }
  213. bool ValueSlice::operator==(const ValueSlice &other) const {
  214. MS_EXCEPTION_IF_NULL(start_);
  215. MS_EXCEPTION_IF_NULL(stop_);
  216. MS_EXCEPTION_IF_NULL(step_);
  217. return (*start_ == *other.start_ && *stop_ == *other.stop_ && *step_ == *other.step_);
  218. }
  219. std::string ValueSlice::ToString() const {
  220. MS_EXCEPTION_IF_NULL(start_);
  221. MS_EXCEPTION_IF_NULL(stop_);
  222. MS_EXCEPTION_IF_NULL(step_);
  223. std::ostringstream buffer;
  224. buffer << "Slice[";
  225. buffer << start_->ToString() << " : ";
  226. buffer << stop_->ToString() << " : ";
  227. buffer << step_->ToString();
  228. buffer << "]";
  229. return buffer.str();
  230. }
  231. std::size_t KeywordArg::hash() const {
  232. MS_EXCEPTION_IF_NULL(value_);
  233. return hash_combine({tid(), std::hash<std::string>{}(key_), value_->hash()});
  234. }
  235. bool KeywordArg::operator==(const Value &other) const {
  236. if (other.isa<KeywordArg>()) {
  237. auto other_ = static_cast<const KeywordArg &>(other);
  238. return *this == other_;
  239. } else {
  240. return false;
  241. }
  242. }
  243. bool KeywordArg::operator==(const KeywordArg &other) const { return (other.key_ == key_ && *other.value_ == *value_); }
  244. std::string KeywordArg::ToString() const {
  245. std::ostringstream buffer;
  246. buffer << "KeywordArg[";
  247. buffer << "key : " << key_;
  248. MS_EXCEPTION_IF_NULL(value_);
  249. buffer << "value : " << value_->ToString();
  250. buffer << "]";
  251. return buffer.str();
  252. }
  253. const ValuePtr ValueDictionary::operator[](const std::string &key) const {
  254. auto it = std::find_if(key_values_.begin(), key_values_.end(),
  255. [key](const std::pair<std::string, ValuePtr> &item) { return item.first == key; });
  256. if (it == key_values_.end()) {
  257. MS_LOG(EXCEPTION) << "The key " << key << " is not in the map";
  258. }
  259. return it->second;
  260. }
  261. bool ValueDictionary::operator==(const Value &other) const {
  262. if (other.isa<ValueDictionary>()) {
  263. auto other_ = static_cast<const ValueDictionary &>(other);
  264. return *this == other_;
  265. } else {
  266. return false;
  267. }
  268. }
  269. bool ValueDictionary::operator==(const ValueDictionary &other) const {
  270. if (key_values_.size() != other.key_values_.size()) {
  271. return false;
  272. }
  273. for (size_t index = 0; index < key_values_.size(); index++) {
  274. if (key_values_[index].first != other.key_values_[index].first) {
  275. return false;
  276. }
  277. if (!(*key_values_[index].second == *other.key_values_[index].second)) {
  278. return false;
  279. }
  280. }
  281. return true;
  282. }
  283. } // namespace mindspore