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.2 kB

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