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.

resolve.h 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_RESOLVE_H_
  17. #define MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_RESOLVE_H_
  18. #include <memory>
  19. #include <string>
  20. #include "ir/anf.h"
  21. #include "ir/manager.h"
  22. #include "pipeline/jit/parse/python_adapter.h"
  23. #include "pipeline/jit/parse/parse_base.h"
  24. #include "abstract/abstract_value.h"
  25. #include "utils/log_adapter.h"
  26. // forward declaration of ResourceBase
  27. namespace mindspore {
  28. namespace pipeline {
  29. class ResourceBase;
  30. using ResourceBasePtr = std::shared_ptr<ResourceBase>;
  31. } // namespace pipeline
  32. } // namespace mindspore
  33. namespace mindspore {
  34. namespace parse {
  35. // NameSpace class for resolving python code.
  36. class NameSpace : public Named {
  37. public:
  38. NameSpace(const std::string &module, const py::object &obj) : Named(module), module_(module), obj_(obj) {}
  39. ~NameSpace() override = default;
  40. MS_DECLARE_PARENT(NameSpace, Named);
  41. py::object obj() { return obj_; }
  42. std::string module() { return module_; }
  43. abstract::AbstractBasePtr ToAbstract() override {
  44. return std::make_shared<abstract::AbstractScalar>(shared_from_base<NameSpace>(), std::make_shared<External>());
  45. }
  46. private:
  47. // namespace of the module
  48. std::string module_;
  49. // namespace object
  50. py::object obj_;
  51. };
  52. using NameSpacePtr = std::shared_ptr<NameSpace>;
  53. // Symbol in NameSpace or Class which shall be resolved.
  54. class Symbol : public Named {
  55. public:
  56. explicit Symbol(const std::string &symbol) : Named(symbol), symbol_(symbol) {}
  57. explicit Symbol(const std::string &symbol, const std::string &name) : Named(name), symbol_(symbol) {}
  58. ~Symbol() override = default;
  59. MS_DECLARE_PARENT(Symbol, Named);
  60. std::string symbol() { return symbol_; }
  61. abstract::AbstractBasePtr ToAbstract() override {
  62. return std::make_shared<abstract::AbstractScalar>(shared_from_base<Symbol>(), std::make_shared<External>());
  63. }
  64. private:
  65. std::string symbol_;
  66. };
  67. using SymbolPtr = std::shared_ptr<Symbol>;
  68. // PyObjectWrapper class wrappers resolved python object for further processing.
  69. class PyObjectWrapper : public Named {
  70. public:
  71. explicit PyObjectWrapper(const py::object &obj, const std::string &name = "Python object") : Named(name), obj_(obj) {}
  72. ~PyObjectWrapper() override = default;
  73. MS_DECLARE_PARENT(PyObjectWrapper, Named);
  74. py::object obj() { return obj_; }
  75. private:
  76. // the object that needs to be resolved
  77. py::object obj_;
  78. };
  79. // ClassObject class wrappers dataclass
  80. class ClassObject : public PyObjectWrapper {
  81. public:
  82. explicit ClassObject(const py::object &obj, const std::string &name = "Python dataclass")
  83. : PyObjectWrapper(obj, name) {}
  84. ~ClassObject() override = default;
  85. MS_DECLARE_PARENT(ClassObject, PyObjectWrapper);
  86. abstract::AbstractBasePtr ToAbstract() override;
  87. };
  88. // ClassType class wrappers class name in python
  89. class ClassType : public PyObjectWrapper {
  90. public:
  91. explicit ClassType(const py::object &obj, const std::string &name = "Python class type")
  92. : PyObjectWrapper(obj, name) {}
  93. ~ClassType() override = default;
  94. MS_DECLARE_PARENT(ClassType, PyObjectWrapper);
  95. abstract::AbstractBasePtr ToAbstract() override;
  96. };
  97. // SymbolResolver class for resolving symbol extracted from AnfNode.
  98. class SymbolResolver {
  99. public:
  100. SymbolResolver(const NameSpacePtr &name_space, const SymbolPtr &symbol, const AnfNodePtr &node)
  101. : namespace_(name_space), symbol_(symbol), resolved_node_(node) {}
  102. ~SymbolResolver() = default;
  103. // resolve symbol in namespace and save it in result_;
  104. bool Resolve();
  105. NameSpacePtr get_namespace() { return namespace_; }
  106. SymbolPtr symbol() { return symbol_; }
  107. py::object &result() { return result_; }
  108. AnfNodePtr resolved_node() { return resolved_node_; }
  109. // Resolve result
  110. py::object result_;
  111. private:
  112. // namespace where the symbol locates
  113. NameSpacePtr namespace_;
  114. // the symbol that needs to be resovled
  115. SymbolPtr symbol_;
  116. // the node that has been resolved
  117. AnfNodePtr resolved_node_;
  118. };
  119. using SymbolResolverPtr = std::shared_ptr<SymbolResolver>;
  120. // Resolve symbol in namespace.
  121. AnfNodePtr ResolveSymbol(const FuncGraphManagerPtr &manager, const NameSpacePtr &name_space, const SymbolPtr &symbol,
  122. const AnfNodePtr &node);
  123. // Resolve Cell with attr name.
  124. AnfNodePtr ResolveCellwithAttr(const FuncGraphManagerPtr &manager, const NameSpacePtr &name_space,
  125. const SymbolPtr &symbol, const AnfNodePtr &node, const std::string &attr);
  126. // Resolve one graph which normally is the root graph. FuncGraph shall be managed by res->manager().
  127. bool ResolveFuncGraph(const FuncGraphPtr &func_graph, const pipeline::ResourceBasePtr &res, bool use_profile = true);
  128. // Resolve all graphs in manager which is defined outside of pipeline::Resource.
  129. // Mainly used for test cases or resolve graphs which will not be managed by manager.
  130. bool ResolveAll(const FuncGraphManagerPtr &manager);
  131. } // namespace parse
  132. } // namespace mindspore
  133. #endif // MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_RESOLVE_H_