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.

primitive_base.cc 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Copyright 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/primitive_base.h"
  17. #include <utility>
  18. namespace mindspore {
  19. bool Primitive::operator==(const Value &other) const {
  20. if (other.isa<Primitive>()) {
  21. auto other_prim = static_cast<const Primitive &>(other);
  22. return *this == other_prim;
  23. } else {
  24. return false;
  25. }
  26. }
  27. bool Primitive::operator==(const Primitive &other) const {
  28. if (name() != other.name()) {
  29. return false;
  30. }
  31. if (attrs_.size() != other.attrs_.size()) {
  32. return false;
  33. }
  34. auto all = std::all_of(attrs_.begin(), attrs_.end(), [&other](const std::pair<std::string, ValuePtr> &item) -> bool {
  35. if (item.second == nullptr) {
  36. return false;
  37. }
  38. auto iter = other.attrs_.find(item.first);
  39. if (iter == other.attrs_.end()) {
  40. return false;
  41. }
  42. return *item.second == *iter->second;
  43. });
  44. return all;
  45. }
  46. std::string Primitive::GetAttrsText() const {
  47. if (attrs_.empty()) {
  48. return "";
  49. }
  50. std::ostringstream oss;
  51. oss << "[";
  52. bool is_first = true;
  53. for (auto &attr : attrs_) {
  54. if (is_first) {
  55. is_first = false;
  56. } else {
  57. oss << ", ";
  58. }
  59. oss << attr.first << "=" << attr.second->DumpText();
  60. }
  61. oss << "]";
  62. return oss.str();
  63. }
  64. } // namespace mindspore