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.cc 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/primitive.h"
  17. #include <utility>
  18. #include "abstract/abstract_function.h"
  19. namespace mindspore {
  20. static std::string MakeId() {
  21. // Use atomic to make id generator thread safe.
  22. static std::atomic<uint64_t> last_id{1};
  23. return "P" + std::to_string(last_id.fetch_add(1, std::memory_order_relaxed));
  24. }
  25. Primitive::Primitive(const std::string &name, const bool is_base, const PrimType prim_type)
  26. : Named(name),
  27. is_base_(is_base),
  28. has_signature_(false),
  29. prim_type_(prim_type),
  30. record_evaluate_add_attr_(false),
  31. is_const_prim_(false),
  32. id_(MakeId()) {}
  33. Primitive::Primitive(const std::string &name, const std::unordered_map<std::string, ValuePtr> &attrs)
  34. : Named(name),
  35. is_base_(true),
  36. has_signature_(false),
  37. prim_type_(kPrimTypeBuiltIn),
  38. record_evaluate_add_attr_(false),
  39. is_const_prim_(false),
  40. id_(MakeId()) {
  41. for (auto &attr : attrs) {
  42. attrs_[attr.first] = attr.second;
  43. }
  44. }
  45. Primitive::Primitive(const Primitive &prim)
  46. : Named(prim),
  47. attrs_(prim.attrs_),
  48. instance_name_(prim.instance_name_),
  49. is_base_(prim.is_base_),
  50. has_signature_(prim.has_signature_),
  51. prim_type_(prim.prim_type_),
  52. record_evaluate_add_attr_(false),
  53. is_const_prim_(false),
  54. id_(prim.id_) {}
  55. abstract::AbstractBasePtr Primitive::ToAbstract() {
  56. return std::make_shared<abstract::PrimitiveAbstractClosure>(shared_from_base<Primitive>(), nullptr);
  57. }
  58. bool Primitive::operator==(const Value &other) const {
  59. if (other.isa<Primitive>()) {
  60. auto other_prim = static_cast<const Primitive &>(other);
  61. return *this == other_prim;
  62. } else {
  63. return false;
  64. }
  65. }
  66. bool Primitive::operator==(const Primitive &other) const {
  67. if (name() != other.name()) {
  68. return false;
  69. }
  70. if (attrs_.size() != other.attrs_.size()) {
  71. return false;
  72. }
  73. auto all = std::all_of(attrs_.begin(), attrs_.end(), [&other](const std::pair<std::string, ValuePtr> &item) -> bool {
  74. if (item.second == nullptr) {
  75. return false;
  76. }
  77. auto iter = other.attrs_.find(item.first);
  78. if (iter == other.attrs_.end()) {
  79. return false;
  80. }
  81. return *item.second == *iter->second;
  82. });
  83. return all;
  84. }
  85. std::string Primitive::GetAttrsText() const {
  86. if (attrs_.empty()) {
  87. return "";
  88. }
  89. std::ostringstream oss;
  90. oss << "[";
  91. bool is_first = true;
  92. for (auto &attr : attrs_) {
  93. if (is_first) {
  94. is_first = false;
  95. } else {
  96. oss << ", ";
  97. }
  98. oss << attr.first << "=" << attr.second->DumpText();
  99. }
  100. oss << "]";
  101. return oss.str();
  102. }
  103. } // namespace mindspore