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.

abstract_value.h 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 PIPELINE_STATIC_ANALYSIS_ABSTRACT_VALUE_H_
  19. #define PIPELINE_STATIC_ANALYSIS_ABSTRACT_VALUE_H_
  20. #include <utility>
  21. #include <vector>
  22. #include <string>
  23. #include <unordered_map>
  24. #include <memory>
  25. #include "utils/log_adapter.h"
  26. #include "utils/hashing.h"
  27. #include "ir/base.h"
  28. #include "ir/dtype.h"
  29. #include "ir/value.h"
  30. #include "ir/meta_tensor.h"
  31. #include "pipeline/static_analysis/dshape.h"
  32. namespace mindspore {
  33. namespace abstract {
  34. class AbstractBase;
  35. using AbstractBasePtrList = std::vector<AbstractBasePtr>;
  36. // The base class for abstract value. The abstract value is used in inferring
  37. // to express the type, shape, and value of the real value.
  38. class AbstractBase : public Base {
  39. public:
  40. explicit AbstractBase(const ValuePtr &value = nullptr, const TypePtr &type = kAnyType,
  41. const BaseShapePtr &shape = kNoShape)
  42. : value_(value), type_(type), shape_(shape) {}
  43. ~AbstractBase() override = default;
  44. MS_DECLARE_PARENT(AbstractBase, Base)
  45. std::size_t hash() const override { return tid(); }
  46. std::string ToString() const override;
  47. virtual bool operator==(const AbstractBase &other) const;
  48. void set_value(const ValuePtr &value) { value_ = value; }
  49. void set_type(const TypePtr &type) { type_ = type; }
  50. void set_shape(const BaseShapePtr &shape) { shape_ = shape; }
  51. void set_value_desc(const std::string &desc) { value_desc_ = desc; }
  52. const std::string &value_desc() const { return value_desc_; }
  53. ValuePtr GetValueTrack() const { return value_; }
  54. TypePtr GetTypeTrack() const { return type_; }
  55. BaseShapePtr GetShapeTrack() const { return shape_; }
  56. // Try build a real value from an abstract value. If the value cannot be built,
  57. // a default value (AnyValue) is returned.
  58. ValuePtr BuildValue() const;
  59. virtual TypePtr BuildType() const = 0;
  60. virtual BaseShapePtr BuildShape() const { return kNoShape; }
  61. virtual AbstractBasePtr Clone() const = 0;
  62. virtual AbstractBasePtr Broaden() const;
  63. virtual AbstractBasePtr Join(const AbstractBasePtr &) { return shared_from_base<AbstractBase>(); }
  64. friend std::ostream &operator<<(std::ostream &os, const std::shared_ptr<AbstractBase> &a) {
  65. os << a->ToString();
  66. return os;
  67. }
  68. protected:
  69. // default implementation, it can be overwritten by subclass;
  70. virtual ValuePtr RealBuildValue() const { return kAnyValue; }
  71. private:
  72. ValuePtr value_;
  73. TypePtr type_;
  74. BaseShapePtr shape_;
  75. std::string value_desc_; // store initial value description for error report
  76. };
  77. class AbstractScalar : public AbstractBase {
  78. public:
  79. AbstractScalar() : AbstractBase(kAnyValue, kAnyType) {}
  80. explicit AbstractScalar(const ValuePtr &value, const TypePtr &type) : AbstractBase(value, type) {}
  81. explicit AbstractScalar(const ValuePtr &value) : AbstractBase(value, value->type()) {}
  82. explicit AbstractScalar(int value) : AbstractBase(MakeValue(value), kInt32) {}
  83. explicit AbstractScalar(float value) : AbstractBase(MakeValue(value), kFloat32) {}
  84. explicit AbstractScalar(double value) : AbstractBase(MakeValue(value), kFloat64) {}
  85. explicit AbstractScalar(bool value) : AbstractBase(MakeValue(value), kBool) {}
  86. explicit AbstractScalar(const std::string &value) : AbstractBase(MakeValue(value), kString) {}
  87. explicit AbstractScalar(const TypePtr &type) : AbstractBase(kAnyValue, type) {}
  88. ~AbstractScalar() override = default;
  89. MS_DECLARE_PARENT(AbstractScalar, AbstractBase)
  90. std::size_t hash() const override { return hash_combine({tid(), GetValueTrack()->hash(), GetTypeTrack()->hash()}); }
  91. TypePtr BuildType() const override { return GetTypeTrack(); }
  92. AbstractBasePtr Clone() const override {
  93. return std::make_shared<AbstractScalar>(GetValueTrack(), GetTypeTrack()->Clone());
  94. }
  95. AbstractBasePtr Broaden() const override;
  96. AbstractBasePtr Join(const AbstractBasePtr &other) override;
  97. };
  98. using AbstractScalarPtr = std::shared_ptr<AbstractScalar>;
  99. class AbstractType : public AbstractBase {
  100. public:
  101. explicit AbstractType(const TypePtr &type) : AbstractBase(type, kTypeType) {
  102. if (type == nullptr) {
  103. MS_LOG(EXCEPTION) << "type is nullptr";
  104. }
  105. }
  106. ~AbstractType() override = default;
  107. MS_DECLARE_PARENT(AbstractType, AbstractBase)
  108. std::string ToString() const override;
  109. bool operator==(const AbstractBase &other) const override;
  110. TypePtr BuildType() const override { return std::make_shared<TypeType>(); }
  111. AbstractBasePtr Clone() const override;
  112. AbstractBasePtr Broaden() const override { return Clone(); }
  113. };
  114. using AbstractTypePtr = std::shared_ptr<AbstractType>;
  115. class AbstractError : public AbstractBase {
  116. public:
  117. explicit AbstractError(const StringImmPtr &err, const AnfNodePtr &node) : AbstractBase(err), node_(node) {
  118. if (err == nullptr || node == nullptr) {
  119. MS_LOG(EXCEPTION) << "err or node is nullptr";
  120. }
  121. }
  122. ~AbstractError() override = default;
  123. MS_DECLARE_PARENT(AbstractError, AbstractBase)
  124. TypePtr BuildType() const override { return std::make_shared<Problem>(); }
  125. AbstractBasePtr Broaden() const override { return Clone(); }
  126. AbstractBasePtr Clone() const override {
  127. return std::make_shared<AbstractError>(GetValueTrack()->cast<StringImmPtr>(), node_);
  128. }
  129. std::string ToString() const override;
  130. private:
  131. // Origin node been specialized to AbstractError, for debug purpose only.
  132. const AnfNodePtr node_;
  133. };
  134. class Evaluator;
  135. using EvaluatorPtr = std::shared_ptr<Evaluator>;
  136. class AnalysisEngine;
  137. using AnalysisEnginePtr = std::shared_ptr<AnalysisEngine>;
  138. class AbstractFunction;
  139. using AbstractFunctionPtr = std::shared_ptr<AbstractFunction>;
  140. class AbstractFuncAtom;
  141. using AbstractFuncAtomPtr = std::shared_ptr<AbstractFuncAtom>;
  142. using AbstractFuncAtomPtrList = std::vector<AbstractFuncAtomPtr>;
  143. class AbstractFunction : public AbstractBase {
  144. public:
  145. AbstractFunction() = default;
  146. ~AbstractFunction() override = default;
  147. MS_DECLARE_PARENT(AbstractFunction, AbstractBase)
  148. // If there is exactly one possible function, return it. Otherwise, raise an Exception.
  149. // Caller should ensure the uniqueness.
  150. virtual AbstractFunctionPtr GetUnique() = 0;
  151. TypePtr BuildType() const override { return std::make_shared<Function>(); }
  152. AbstractBasePtr Clone() const override { return Copy(); }
  153. // For Function, no need to broaden.
  154. AbstractBasePtr Broaden() const override {
  155. return const_cast<AbstractFunction *>(this)->shared_from_base<AbstractFunction>();
  156. }
  157. virtual AbstractFunctionPtr Copy() const = 0;
  158. AbstractBasePtr Join(const AbstractBasePtr &other) final;
  159. virtual AbstractFunctionPtr Join(const AbstractFunctionPtr &other) = 0;
  160. virtual void Visit(std::function<void(const AbstractFuncAtomPtr &)>) const = 0;
  161. bool operator==(const AbstractBase &other) const final;
  162. virtual bool operator==(const AbstractFunction &other) const = 0;
  163. static AbstractFunctionPtr MakeAbstractFunction(const AbstractFuncAtomPtrList &func_list);
  164. virtual EvaluatorPtr GetEvaluator(AnalysisEnginePtr engine) = 0;
  165. virtual AnfNodePtr tracking_id() const { return nullptr; }
  166. virtual void set_tracking_id(AnfNodePtr) {}
  167. virtual AnalysisContextPtr context() const { return nullptr; }
  168. };
  169. using AbstractFunctionPtrList = std::vector<AbstractFunctionPtr>;
  170. // Represents a key-value pair used in function's parameters.
  171. class AbstractKeywordArg : public AbstractBase {
  172. public:
  173. AbstractKeywordArg(const std::string &key, const AbstractBasePtr &argument) : arg_name_(key), arg_value_(argument) {}
  174. ~AbstractKeywordArg() override = default;
  175. MS_DECLARE_PARENT(AbstractKeywordArg, AbstractBase)
  176. TypePtr BuildType() const override;
  177. AbstractBasePtr Clone() const override;
  178. AbstractBasePtr Broaden() const override;
  179. std::size_t hash() const override;
  180. bool operator==(const AbstractKeywordArg &other) const;
  181. bool operator==(const AbstractBase &other) const override;
  182. std::string get_key() const { return arg_name_; }
  183. AbstractBasePtr get_arg() const { return arg_value_; }
  184. std::string ToString() const override;
  185. protected:
  186. ValuePtr RealBuildValue() const override;
  187. private:
  188. std::string arg_name_;
  189. AbstractBasePtr arg_value_;
  190. };
  191. using AbstractKeywordArgPtr = std::shared_ptr<AbstractKeywordArg>;
  192. class AbstractTensor : public AbstractBase {
  193. public:
  194. // only element_ and value, shape track are valid member, type track are unknown.
  195. explicit AbstractTensor(const AbstractBasePtr &element, const BaseShapePtr &shape = std::make_shared<Shape>())
  196. : AbstractBase(kAnyValue), element_(element) {
  197. if (element == nullptr) {
  198. MS_LOG(EXCEPTION) << "element is nullptr";
  199. }
  200. if (element->isa<AbstractTensor>()) {
  201. MS_LOG(EXCEPTION) << "element type error";
  202. }
  203. set_shape(shape);
  204. }
  205. AbstractTensor(const TypePtr &element_type, const std::vector<int> &shape)
  206. : AbstractBase(kAnyValue), element_(std::make_shared<AbstractScalar>(kAnyValue, element_type)) {
  207. if (element_type == nullptr) {
  208. MS_LOG(EXCEPTION) << "element_type is nullptr";
  209. }
  210. set_shape(std::make_shared<Shape>(shape));
  211. }
  212. explicit AbstractTensor(const tensor::TensorPtr &tensor)
  213. : AbstractBase(tensor), element_(std::make_shared<AbstractScalar>(kAnyValue, tensor->Dtype())) {
  214. if (tensor == nullptr) {
  215. MS_LOG(EXCEPTION) << "tensor is nullptr";
  216. }
  217. set_shape(std::make_shared<Shape>(tensor->shape()));
  218. }
  219. ~AbstractTensor() override = default;
  220. MS_DECLARE_PARENT(AbstractTensor, AbstractBase)
  221. TypePtr BuildType() const override;
  222. BaseShapePtr BuildShape() const override;
  223. AbstractBasePtr Clone() const override;
  224. AbstractBasePtr Broaden() const override;
  225. AbstractBasePtr BroadenWithShape() const;
  226. AbstractBasePtr Join(const AbstractBasePtr &other) final;
  227. bool operator==(const AbstractTensor &other) const;
  228. bool operator==(const AbstractBase &other) const override;
  229. ShapePtr shape() const;
  230. std::string ToString() const override;
  231. const AbstractBasePtr element() const { return element_; }
  232. std::size_t hash() const override {
  233. auto value = GetValueTrack();
  234. auto hash_sum = hash_combine(tid(), element_->hash());
  235. if (value != nullptr) {
  236. auto tensor = value->cast<tensor::TensorPtr>();
  237. if (tensor != nullptr) {
  238. hash_sum = hash_combine(hash_sum, IntToSize(tensor->DataSize()));
  239. }
  240. }
  241. return hash_sum;
  242. }
  243. private:
  244. AbstractBasePtr element_;
  245. };
  246. using AbstractTensorPtr = std::shared_ptr<AbstractTensor>;
  247. using AbstractTensorPtrList = std::vector<AbstractTensorPtr>;
  248. class AbstractSequeue : public AbstractBase {
  249. public:
  250. explicit AbstractSequeue(const AbstractBasePtrList &elements) : elements_(elements) {}
  251. ~AbstractSequeue() override = default;
  252. MS_DECLARE_PARENT(AbstractSequeue, AbstractBase)
  253. TypePtrList ElementsType() const;
  254. BaseShapePtrList ElementsShape() const;
  255. AbstractBasePtrList ElementsClone() const;
  256. AbstractBasePtrList ElementsBroaden() const;
  257. template <typename T>
  258. ValuePtr ElementsBuildValue() const;
  259. template <typename T>
  260. AbstractBasePtr ElementsJoin(const AbstractBasePtr &other);
  261. std::size_t size() const { return elements_.size(); }
  262. const AbstractBasePtrList &elements() const { return elements_; }
  263. std::size_t hash() const override;
  264. std::string ToString() const override;
  265. const AbstractBasePtr operator[](const std::size_t &dim) const;
  266. protected:
  267. AbstractBasePtrList elements_;
  268. };
  269. using AbstractSequeuePtr = std::shared_ptr<AbstractSequeue>;
  270. class AbstractTuple : public AbstractSequeue {
  271. public:
  272. explicit AbstractTuple(const AbstractBasePtrList &elements) : AbstractSequeue(elements) {}
  273. ~AbstractTuple() override = default;
  274. MS_DECLARE_PARENT(AbstractTuple, AbstractSequeue)
  275. TypePtr BuildType() const override { return std::make_shared<Tuple>(ElementsType()); }
  276. BaseShapePtr BuildShape() const override { return std::make_shared<TupleShape>(ElementsShape()); }
  277. AbstractBasePtr Clone() const override { return std::make_shared<AbstractTuple>(ElementsClone()); }
  278. AbstractBasePtr Broaden() const override { return std::make_shared<AbstractTuple>(ElementsBroaden()); }
  279. AbstractBasePtr Join(const AbstractBasePtr &other) override { return ElementsJoin<AbstractTuple>(other); }
  280. std::string ToString() const override { return type_name() + "(" + AbstractSequeue::ToString() + ")"; }
  281. bool operator==(const AbstractTuple &other) const;
  282. bool operator==(const AbstractBase &other) const override;
  283. protected:
  284. ValuePtr RealBuildValue() const override { return ElementsBuildValue<ValueTuple>(); }
  285. };
  286. using AbstractTuplePtr = std::shared_ptr<AbstractTuple>;
  287. class AbstractList : public AbstractSequeue {
  288. public:
  289. explicit AbstractList(const AbstractBasePtrList &elements) : AbstractSequeue(elements) {}
  290. ~AbstractList() override = default;
  291. MS_DECLARE_PARENT(AbstractList, AbstractSequeue)
  292. TypePtr BuildType() const override { return std::make_shared<List>(ElementsType()); }
  293. BaseShapePtr BuildShape() const override { return std::make_shared<ListShape>(ElementsShape()); }
  294. AbstractBasePtr Clone() const override { return std::make_shared<AbstractList>(ElementsClone()); }
  295. AbstractBasePtr Broaden() const override { return std::make_shared<AbstractList>(ElementsBroaden()); }
  296. AbstractBasePtr Join(const AbstractBasePtr &other) override { return ElementsJoin<AbstractList>(other); }
  297. std::string ToString() const override { return type_name() + "[" + AbstractSequeue::ToString() + "]"; }
  298. bool operator==(const AbstractList &other) const;
  299. bool operator==(const AbstractBase &other) const override;
  300. protected:
  301. ValuePtr RealBuildValue() const override { return ElementsBuildValue<ValueList>(); }
  302. };
  303. using AbstractListPtr = std::shared_ptr<AbstractList>;
  304. class AbstractClass : public AbstractBase {
  305. public:
  306. AbstractClass(const Named &tag, const std::vector<AbstractAttribute> &attributes,
  307. const std::unordered_map<std::string, ValuePtr> &methods)
  308. : attributes_(attributes), tag_(tag), methods_(methods) {}
  309. ~AbstractClass() override = default;
  310. MS_DECLARE_PARENT(AbstractClass, AbstractBase)
  311. TypePtr BuildType() const override;
  312. bool operator==(const AbstractClass &other) const;
  313. bool operator==(const AbstractBase &other) const override;
  314. const std::vector<AbstractAttribute> &attributes() const { return attributes_; }
  315. std::unordered_map<std::string, ValuePtr> methods() { return methods_; }
  316. AbstractBasePtr GetAttribute(const std::string &name);
  317. ValuePtr GetMethod(const std::string &name);
  318. AbstractBasePtr Clone() const override;
  319. AbstractBasePtr Broaden() const override;
  320. std::string ToString() const override;
  321. Named tag() const { return tag_; }
  322. std::size_t hash() const override;
  323. protected:
  324. ValuePtr RealBuildValue() const override;
  325. private:
  326. std::vector<AbstractAttribute> attributes_;
  327. Named tag_;
  328. std::unordered_map<std::string, ValuePtr> methods_;
  329. };
  330. using AbstractClassPtr = std::shared_ptr<AbstractClass>;
  331. class AbstractDictionary : public AbstractBase {
  332. public:
  333. explicit AbstractDictionary(const std::vector<AbstractAttribute> &key_values) : key_values_(key_values) {}
  334. ~AbstractDictionary() override = default;
  335. MS_DECLARE_PARENT(AbstractDictionary, AbstractBase)
  336. TypePtr BuildType() const override;
  337. bool operator==(const AbstractDictionary &other) const;
  338. bool operator==(const AbstractBase &other) const override;
  339. AbstractBasePtr Clone() const override;
  340. AbstractBasePtr Broaden() const override;
  341. std::string ToString() const override;
  342. std::size_t hash() const override;
  343. std::size_t size() const { return key_values_.size(); }
  344. const std::vector<AbstractAttribute> &elements() const { return key_values_; }
  345. std::vector<AbstractAttribute> key_values_;
  346. protected:
  347. ValuePtr RealBuildValue() const override;
  348. };
  349. using AbstractDictionaryPtr = std::shared_ptr<AbstractDictionary>;
  350. class AbstractSlice : public AbstractBase {
  351. public:
  352. AbstractSlice(const AbstractBasePtr &start, const AbstractBasePtr &stop, const AbstractBasePtr &step)
  353. : start_(start), stop_(stop), step_(step) {}
  354. ~AbstractSlice() override = default;
  355. MS_DECLARE_PARENT(AbstractSlice, AbstractBase)
  356. TypePtr BuildType() const override;
  357. bool operator==(const AbstractSlice &other) const;
  358. bool operator==(const AbstractBase &other) const override;
  359. AbstractBasePtr Clone() const override;
  360. AbstractBasePtr Broaden() const override;
  361. std::string ToString() const override;
  362. std::size_t hash() const override;
  363. AbstractBasePtr start() const { return start_; }
  364. AbstractBasePtr stop() const { return stop_; }
  365. AbstractBasePtr step() const { return step_; }
  366. protected:
  367. ValuePtr RealBuildValue() const override;
  368. private:
  369. AbstractBasePtr start_;
  370. AbstractBasePtr stop_;
  371. AbstractBasePtr step_;
  372. };
  373. using AbstractSlicePtr = std::shared_ptr<AbstractSlice>;
  374. class AbstractJTagged : public AbstractBase {
  375. public:
  376. explicit AbstractJTagged(const AbstractBasePtr &element) : element_(element) {}
  377. ~AbstractJTagged() override = default;
  378. MS_DECLARE_PARENT(AbstractJTagged, AbstractBase)
  379. TypePtr BuildType() const override;
  380. AbstractBasePtr Clone() const override { return std::make_shared<AbstractJTagged>(element_->Clone()); }
  381. AbstractBasePtr Broaden() const override { return std::make_shared<AbstractJTagged>(element_->Broaden()); }
  382. AbstractBasePtr Join(const AbstractBasePtr &other) override;
  383. bool operator==(const AbstractJTagged &other) const;
  384. bool operator==(const AbstractBase &other) const override;
  385. std::string ToString() const override;
  386. AbstractBasePtr element() { return element_; }
  387. std::size_t hash() const override { return hash_combine(tid(), element_->hash()); }
  388. private:
  389. AbstractBasePtr element_;
  390. };
  391. using AbstractJTaggedPtr = std::shared_ptr<AbstractJTagged>;
  392. class AbstractNone : public AbstractBase {
  393. public:
  394. AbstractNone() : AbstractBase() { set_type(std::make_shared<TypeNone>()); }
  395. ~AbstractNone() override = default;
  396. MS_DECLARE_PARENT(AbstractNone, AbstractBase)
  397. TypePtr BuildType() const override { return std::make_shared<TypeNone>(); }
  398. bool operator==(const AbstractNone &other) const;
  399. bool operator==(const AbstractBase &other) const override;
  400. AbstractBasePtr Clone() const override { return std::make_shared<AbstractNone>(); }
  401. std::string ToString() const override;
  402. protected:
  403. ValuePtr RealBuildValue() const override;
  404. };
  405. using AbstractNonePtr = std::shared_ptr<AbstractNone>;
  406. // the un assigned state value for variable, which means the variable is not assigned
  407. class AbstractNull : public AbstractBase {
  408. public:
  409. AbstractNull() : AbstractBase(kNull) { set_type(std::make_shared<TypeNull>()); }
  410. ~AbstractNull() override = default;
  411. MS_DECLARE_PARENT(AbstractNull, AbstractBase)
  412. TypePtr BuildType() const override { return std::make_shared<TypeNull>(); }
  413. bool operator==(const AbstractNull &other) const;
  414. bool operator==(const AbstractBase &other) const override;
  415. AbstractBasePtr Clone() const override { return std::make_shared<AbstractNull>(); }
  416. std::string ToString() const override;
  417. };
  418. using AbstractNullPtr = std::shared_ptr<AbstractNull>;
  419. class AbstractEllipsis : public AbstractBase {
  420. public:
  421. AbstractEllipsis() : AbstractBase(kEllipsis) { set_type(std::make_shared<Ellipsis>()); }
  422. ~AbstractEllipsis() override = default;
  423. MS_DECLARE_PARENT(AbstractEllipsis, AbstractBase)
  424. TypePtr BuildType() const override { return std::make_shared<Ellipsis>(); }
  425. bool operator==(const AbstractEllipsis &other) const;
  426. bool operator==(const AbstractBase &other) const override;
  427. AbstractBasePtr Clone() const override { return std::make_shared<AbstractEllipsis>(); }
  428. std::string ToString() const override;
  429. };
  430. using AbstractEllipsisPtr = std::shared_ptr<AbstractEllipsis>;
  431. class AbstractRefKey : public AbstractBase {
  432. public:
  433. AbstractRefKey() : AbstractBase() { set_type(std::make_shared<RefKeyType>()); }
  434. ~AbstractRefKey() override = default;
  435. MS_DECLARE_PARENT(AbstractRefKey, AbstractBase)
  436. TypePtr BuildType() const override { return std::make_shared<RefKeyType>(); }
  437. bool operator==(const AbstractRefKey &other) const;
  438. bool operator==(const AbstractBase &other) const override;
  439. AbstractBasePtr Clone() const override { return std::make_shared<AbstractRefKey>(); }
  440. std::string ToString() const override;
  441. };
  442. using AbstractRefKeyPtr = std::shared_ptr<AbstractRefKey>;
  443. class AbstractRef : public AbstractBase {
  444. public:
  445. AbstractRef(const AbstractBasePtr &ref_key, const AbstractBasePtr &ref_value, const AbstractBasePtr &ref_origin)
  446. : ref_key_(ref_key), ref_(ref_value), ref_origin_(ref_origin) {
  447. set_type(std::make_shared<RefType>());
  448. }
  449. ~AbstractRef() override = default;
  450. MS_DECLARE_PARENT(AbstractRef, AbstractBase)
  451. TypePtr BuildType() const override;
  452. bool operator==(const AbstractRef &other) const;
  453. bool operator==(const AbstractBase &other) const override;
  454. AbstractBasePtr Clone() const override {
  455. return std::make_shared<AbstractRef>(ref_key_->Clone(), ref_->Clone(), ref_origin_->Clone());
  456. }
  457. std::string ToString() const override;
  458. AbstractBasePtr ref() { return ref_; }
  459. AbstractBasePtr ref_origin() { return ref_origin_; }
  460. AbstractBasePtr ref_key() { return ref_key_; }
  461. AbstractBasePtr Broaden() const override {
  462. return std::make_shared<AbstractRef>(ref_key_->Broaden(), ref_->Broaden(), ref_origin_->Broaden());
  463. }
  464. std::size_t hash() const override {
  465. return ref_key_->hash() ^ ref_->hash() ^ ref_origin_->hash() ^ (std::hash<uint32_t>{}(this->tid()) << 1);
  466. }
  467. private:
  468. AbstractBasePtr ref_key_;
  469. AbstractBasePtr ref_;
  470. AbstractBasePtr ref_origin_;
  471. };
  472. using AbstractRefPtr = std::shared_ptr<AbstractRef>;
  473. struct AbstractBasePtrListHasher {
  474. std::size_t operator()(const AbstractBasePtrList &args_spec_list) const;
  475. };
  476. struct AbstractBasePtrListEqual {
  477. bool operator()(const AbstractBasePtrList &lhs, const AbstractBasePtrList &rhs) const;
  478. };
  479. std::size_t AbstractBasePtrListHash(const AbstractBasePtrList &args_spec_list);
  480. bool AbstractBasePtrListDeepEqual(const AbstractBasePtrList &lhs, const AbstractBasePtrList &rhs);
  481. } // namespace abstract
  482. } // namespace mindspore
  483. #endif // PIPELINE_STATIC_ANALYSIS_ABSTRACT_VALUE_H_