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.

symbolic.h 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MINDSPORE_CCSRC_UTILS_SYMBOLIC_H_
  19. #define MINDSPORE_CCSRC_UTILS_SYMBOLIC_H_
  20. #include <unordered_map>
  21. #include <memory>
  22. #include <algorithm>
  23. #include <utility>
  24. #include <string>
  25. #include "ir/anf.h"
  26. #include "pipeline/static_analysis/abstract_value.h"
  27. namespace mindspore {
  28. class SymbolicKeyInstance : public Value {
  29. public:
  30. SymbolicKeyInstance(const AnfNodePtr &node, const abstract::AbstractBasePtr &abstract)
  31. : node_(node), abstract_(abstract) {}
  32. ~SymbolicKeyInstance() override = default;
  33. MS_DECLARE_PARENT(SymbolicKeyInstance, Value);
  34. AnfNodePtr node() const { return node_; }
  35. abstract::AbstractBasePtr abstract() const { return abstract_; }
  36. bool operator==(const SymbolicKeyInstance &other) const {
  37. return (*node_ == *other.node_) && (*abstract_ == *other.abstract_);
  38. }
  39. std::size_t hash() const override { return std::hash<AnfNodePtr>{}(node_); }
  40. friend std::ostream &operator<<(std::ostream &os, const std::shared_ptr<SymbolicKeyInstance> &inst) {
  41. if (inst == nullptr) {
  42. os << "[Key]["
  43. << "Invalid symbolic key instance"
  44. << "]";
  45. } else {
  46. os << "[Key][" << inst->node_->type_name() << "]" << inst->node_->ToString();
  47. }
  48. return os;
  49. }
  50. std::string ToString() const override {
  51. return node_ == nullptr ? "Invalid node" : "[Key][" + node_->type_name() + "]" + node_->ToString();
  52. }
  53. bool operator==(const Value &other) const override {
  54. if (other.isa<SymbolicKeyInstance>()) {
  55. auto other_ = static_cast<const SymbolicKeyInstance &>(other);
  56. return *this == other_;
  57. } else {
  58. return false;
  59. }
  60. }
  61. abstract::AbstractBasePtr ToAbstract() override {
  62. return std::make_shared<abstract::AbstractScalar>(shared_from_base<SymbolicKeyInstance>(),
  63. std::make_shared<SymbolicKeyType>());
  64. }
  65. private:
  66. AnfNodePtr node_;
  67. abstract::AbstractBasePtr abstract_;
  68. };
  69. using SymbolicKeyInstancePtr = std::shared_ptr<SymbolicKeyInstance>;
  70. struct SymbolicKeyInstanceHash {
  71. std::size_t operator()(const SymbolicKeyInstancePtr s) const {
  72. if (s == nullptr) {
  73. return 0;
  74. }
  75. return s->abstract()->hash();
  76. }
  77. };
  78. struct SymbolicKeyInstanceEqual {
  79. bool operator()(const SymbolicKeyInstancePtr lhs, const SymbolicKeyInstancePtr rhs) const {
  80. if (lhs == nullptr || rhs == nullptr) {
  81. return false;
  82. }
  83. MS_EXCEPTION_IF_NULL(lhs->node());
  84. MS_EXCEPTION_IF_NULL(rhs->node());
  85. MS_EXCEPTION_IF_NULL(lhs->abstract());
  86. MS_EXCEPTION_IF_NULL(rhs->abstract());
  87. return (*lhs->node() == *rhs->node()) && (*lhs->abstract() == *rhs->abstract());
  88. }
  89. };
  90. using EnvInstanceContentsMap =
  91. std::unordered_map<SymbolicKeyInstancePtr, Any, SymbolicKeyInstanceHash, SymbolicKeyInstanceEqual>;
  92. // Environment mapping keys to values.
  93. // Keys are SymbolicKeyInstances, which represent nodes in the graph along
  94. // with inferred properties.
  95. class EnvInstance : public Value {
  96. public:
  97. friend std::ostream &operator<<(std::ostream &out, const std::shared_ptr<EnvInstance> &env);
  98. explicit EnvInstance(const EnvInstanceContentsMap &contents = {}) : contents_(contents) {}
  99. ~EnvInstance() override = default;
  100. MS_DECLARE_PARENT(EnvInstance, Value);
  101. abstract::AbstractBasePtr ToAbstract() override {
  102. return std::make_shared<abstract::AbstractScalar>(shared_from_base<EnvInstance>(), std::make_shared<EnvType>());
  103. }
  104. bool operator==(const EnvInstance &other) const;
  105. bool operator==(const Value &other) const override;
  106. EnvInstance(const EnvInstance &v) : Value(v), contents_(v.contents_) {}
  107. EnvInstance(EnvInstance &&v) = default;
  108. EnvInstance &operator=(EnvInstance &&src) noexcept {
  109. if (&src != this) {
  110. contents_ = src.contents_;
  111. }
  112. return *this;
  113. };
  114. // Get the sensitivity list for the given key
  115. const Any &Get(const SymbolicKeyInstancePtr &key, const Any &def) const {
  116. auto iterator = contents_.find(key);
  117. if (iterator != contents_.end()) {
  118. return iterator->second;
  119. }
  120. return def;
  121. }
  122. // Set a value for the given key.
  123. EnvInstance Set(const SymbolicKeyInstancePtr &key, const Any &value) const {
  124. EnvInstance rval(contents_);
  125. rval.contents_[key] = value;
  126. return rval;
  127. }
  128. // Add two EnvInstances.
  129. EnvInstance Add(const EnvInstance &other) const {
  130. EnvInstance rval(contents_);
  131. for (auto iter_other : other.contents_) {
  132. auto item_self = contents_.find(iter_other.first);
  133. if (item_self != contents_.end()) {
  134. MS_LOG(DEBUG) << "Need to use add";
  135. } else {
  136. rval.contents_[iter_other.first] = iter_other.second;
  137. }
  138. }
  139. return rval;
  140. }
  141. size_t Len() const { return contents_.size(); }
  142. std::size_t hash() const override {
  143. // deterministic characteristic of member variables.
  144. return Len();
  145. }
  146. const bool parse_info_ = true;
  147. private:
  148. EnvInstanceContentsMap contents_;
  149. };
  150. using EnvInstancePtr = std::shared_ptr<EnvInstance>;
  151. extern std::shared_ptr<EnvInstance> newenv;
  152. } // namespace mindspore
  153. #endif // MINDSPORE_CCSRC_UTILS_SYMBOLIC_H_