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 7.1 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright 2019-2021 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 final : public Named {
  37. public:
  38. NameSpace(const std::string &module, const py::object &obj, const py::object &module_obj = py::object())
  39. : Named(module), module_(module), obj_(obj), module_obj_(module_obj) {}
  40. ~NameSpace() override = default;
  41. MS_DECLARE_PARENT(NameSpace, Named);
  42. py::object obj() { return obj_; }
  43. py::object module_obj() { return module_obj_; }
  44. std::string module() { return module_; }
  45. abstract::AbstractBasePtr ToAbstract() override {
  46. return std::make_shared<abstract::AbstractScalar>(shared_from_base<NameSpace>(), std::make_shared<External>());
  47. }
  48. private:
  49. // namespace of the module
  50. std::string module_;
  51. // namespace object
  52. py::object obj_;
  53. // module object
  54. py::object module_obj_;
  55. };
  56. using NameSpacePtr = std::shared_ptr<NameSpace>;
  57. // Symbol in NameSpace or Class which shall be resolved.
  58. class Symbol final : public Named {
  59. public:
  60. explicit Symbol(const std::string &symbol) : Named(symbol), symbol_(symbol) {}
  61. Symbol(const std::string &symbol, const std::string &name) : Named(name), symbol_(symbol) {}
  62. ~Symbol() override = default;
  63. MS_DECLARE_PARENT(Symbol, Named);
  64. std::string symbol() { return symbol_; }
  65. abstract::AbstractBasePtr ToAbstract() override {
  66. return std::make_shared<abstract::AbstractScalar>(shared_from_base<Symbol>(), std::make_shared<External>());
  67. }
  68. private:
  69. std::string symbol_;
  70. };
  71. using SymbolPtr = std::shared_ptr<Symbol>;
  72. class Script final : public Named {
  73. public:
  74. explicit Script(const std::string &script) : Named(script), script_(script) {}
  75. Script(const std::string &script, const std::string &name) : Named(name), script_(script) {}
  76. ~Script() override = default;
  77. MS_DECLARE_PARENT(Script, Named);
  78. std::string script() { return script_; }
  79. abstract::AbstractBasePtr ToAbstract() override {
  80. return std::make_shared<abstract::AbstractScript>(shared_from_base<Script>());
  81. }
  82. std::string ToString() const override { return "`" + name() + "`"; }
  83. private:
  84. std::string script_;
  85. };
  86. using ScriptPtr = std::shared_ptr<Script>;
  87. // PyObjectWrapper class wrappers resolved python object for further processing.
  88. class PyObjectWrapper : public Named {
  89. public:
  90. explicit PyObjectWrapper(const py::object &obj, const std::string &name = "Python object") : Named(name), obj_(obj) {}
  91. ~PyObjectWrapper() override = default;
  92. MS_DECLARE_PARENT(PyObjectWrapper, Named);
  93. py::object obj() { return obj_; }
  94. private:
  95. // the object that needs to be resolved
  96. py::object obj_;
  97. };
  98. // InterpretedObject class wrappers interpreted python object.
  99. class InterpretedObject final : public PyObjectWrapper {
  100. public:
  101. explicit InterpretedObject(const py::object &obj, const std::string &name = "null")
  102. : PyObjectWrapper(obj, "InterpretedObject: '" + name + "'") {}
  103. ~InterpretedObject() override = default;
  104. MS_DECLARE_PARENT(InterpretedObject, PyObjectWrapper);
  105. abstract::AbstractBasePtr ToAbstract() override {
  106. return std::make_shared<abstract::AbstractScalar>(shared_from_base<InterpretedObject>(),
  107. std::make_shared<External>());
  108. }
  109. };
  110. using InterpretedObjectPtr = std::shared_ptr<InterpretedObject>;
  111. // ClassObject class wrappers dataclass
  112. class ClassObject final : public PyObjectWrapper {
  113. public:
  114. explicit ClassObject(const py::object &obj, const std::string &name = "Python dataclass")
  115. : PyObjectWrapper(obj, name) {}
  116. ~ClassObject() override = default;
  117. MS_DECLARE_PARENT(ClassObject, PyObjectWrapper);
  118. abstract::AbstractBasePtr ToAbstract() override;
  119. };
  120. // ClassType class wrappers class name in python
  121. class ClassType final : public PyObjectWrapper {
  122. public:
  123. explicit ClassType(const py::object &obj, const std::string &name = "Python class type")
  124. : PyObjectWrapper(obj, name) {}
  125. ~ClassType() override = default;
  126. MS_DECLARE_PARENT(ClassType, PyObjectWrapper);
  127. abstract::AbstractBasePtr ToAbstract() override;
  128. };
  129. using ClassTypePtr = std::shared_ptr<ClassType>;
  130. // SymbolResolver class for resolving symbol extracted from AnfNode.
  131. class SymbolResolver {
  132. public:
  133. SymbolResolver(const NameSpacePtr &name_space, const SymbolPtr &symbol, const AnfNodePtr &node)
  134. : namespace_(name_space), symbol_(symbol), resolved_node_(node) {}
  135. ~SymbolResolver() = default;
  136. // resolve symbol in namespace and save it in result_;
  137. bool Resolve();
  138. NameSpacePtr get_namespace() { return namespace_; }
  139. SymbolPtr symbol() { return symbol_; }
  140. const py::object &result() { return result_; }
  141. AnfNodePtr resolved_node() { return resolved_node_; }
  142. private:
  143. // namespace where the symbol locates
  144. NameSpacePtr namespace_;
  145. // the symbol that needs to be resovled
  146. SymbolPtr symbol_;
  147. // the node that has been resolved
  148. AnfNodePtr resolved_node_;
  149. // Resolve result
  150. py::object result_;
  151. };
  152. using SymbolResolverPtr = std::shared_ptr<SymbolResolver>;
  153. // Resolve symbol in namespace.
  154. AnfNodePtr ResolveSymbol(const FuncGraphManagerPtr &manager, const NameSpacePtr &name_space, const SymbolPtr &symbol,
  155. const AnfNodePtr &node);
  156. // Resolve Cell with attr name.
  157. AnfNodePtr ResolveCellwithAttr(const FuncGraphManagerPtr &manager, const NameSpacePtr &name_space,
  158. const SymbolPtr &symbol, const AnfNodePtr &node, const AnfNodePtr &attr);
  159. // Resolve one graph which normally is the root graph. FuncGraph shall be managed by res->manager().
  160. bool ResolveFuncGraph(const FuncGraphPtr &func_graph, const pipeline::ResourceBasePtr &res, bool use_profile = true);
  161. // Resolve all graphs in manager which is defined outside of pipeline::Resource.
  162. // Mainly used for test cases or resolve graphs which will not be managed by manager.
  163. bool ResolveAll(const FuncGraphManagerPtr &manager);
  164. } // namespace parse
  165. } // namespace mindspore
  166. #endif // MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_RESOLVE_H_