• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_RESOLVE_H_
18 #define MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_RESOLVE_H_
19 
20 #include <memory>
21 #include <string>
22 #include "ir/anf.h"
23 #include "ir/manager.h"
24 #include "pipeline/jit/parse/python_adapter.h"
25 #include "pipeline/jit/parse/parse_base.h"
26 #include "abstract/abstract_value.h"
27 #include "utils/log_adapter.h"
28 
29 // forward declaration of ResourceBase
30 namespace mindspore {
31 namespace pipeline {
32 class ResourceBase;
33 using ResourceBasePtr = std::shared_ptr<ResourceBase>;
34 }  // namespace pipeline
35 }  // namespace mindspore
36 
37 namespace mindspore {
38 namespace parse {
39 // NameSpace class for resolving python code.
40 class NameSpace : public Named {
41  public:
42   NameSpace(const std::string &module, const py::object &obj, const py::object &module_obj = py::object())
Named(module)43       : Named(module), module_(module), obj_(obj), module_obj_(module_obj) {}
44   ~NameSpace() override = default;
45   MS_DECLARE_PARENT(NameSpace, Named);
46 
obj()47   py::object obj() { return obj_; }
module_obj()48   py::object module_obj() { return module_obj_; }
module()49   std::string module() { return module_; }
ToAbstract()50   abstract::AbstractBasePtr ToAbstract() override {
51     return std::make_shared<abstract::AbstractScalar>(shared_from_base<NameSpace>(), std::make_shared<External>());
52   }
53 
54  private:
55   // namespace of the module
56   std::string module_;
57   // namespace object
58   py::object obj_;
59   // module object
60   py::object module_obj_;
61 };
62 using NameSpacePtr = std::shared_ptr<NameSpace>;
63 
64 // Symbol in NameSpace or Class which shall be resolved.
65 class Symbol : public Named {
66  public:
Symbol(const std::string & symbol)67   explicit Symbol(const std::string &symbol) : Named(symbol), symbol_(symbol) {}
Symbol(const std::string & symbol,const std::string & name)68   Symbol(const std::string &symbol, const std::string &name) : Named(name), symbol_(symbol) {}
69 
70   ~Symbol() override = default;
71   MS_DECLARE_PARENT(Symbol, Named);
72 
symbol()73   std::string symbol() { return symbol_; }
ToAbstract()74   abstract::AbstractBasePtr ToAbstract() override {
75     return std::make_shared<abstract::AbstractScalar>(shared_from_base<Symbol>(), std::make_shared<External>());
76   }
77 
78  private:
79   std::string symbol_;
80 };
81 using SymbolPtr = std::shared_ptr<Symbol>;
82 
83 class Script : public Named {
84  public:
Script(const std::string & script)85   explicit Script(const std::string &script) : Named(script), script_(script) {}
Script(const std::string & script,const std::string & name)86   Script(const std::string &script, const std::string &name) : Named(name), script_(script) {}
87 
88   ~Script() override = default;
89   MS_DECLARE_PARENT(Script, Named);
90 
script()91   std::string script() { return script_; }
ToAbstract()92   abstract::AbstractBasePtr ToAbstract() override {
93     return std::make_shared<abstract::AbstractScript>(shared_from_base<Script>());
94   }
ToString()95   std::string ToString() const override { return "`" + name() + "`"; }
96 
97  private:
98   std::string script_;
99 };
100 using ScriptPtr = std::shared_ptr<Script>;
101 
102 // PyObjectWrapper class wrappers resolved python object for further processing.
103 class PyObjectWrapper : public Named {
104  public:
Named(name)105   explicit PyObjectWrapper(const py::object &obj, const std::string &name = "Python object") : Named(name), obj_(obj) {}
106   ~PyObjectWrapper() override = default;
107   MS_DECLARE_PARENT(PyObjectWrapper, Named);
obj()108   py::object obj() { return obj_; }
109 
110  private:
111   // the object that needs to be resolved
112   py::object obj_;
113 };
114 
115 // InterpretedObject class wrappers interpreted python object.
116 class InterpretedObject : public PyObjectWrapper {
117  public:
118   explicit InterpretedObject(const py::object &obj, const std::string &name = "Interpreted object")
PyObjectWrapper(obj,name)119       : PyObjectWrapper(obj, name) {}
120   ~InterpretedObject() override = default;
121   MS_DECLARE_PARENT(InterpretedObject, PyObjectWrapper);
122   abstract::AbstractBasePtr ToAbstract() override;
123 };
124 
125 // ClassObject class wrappers dataclass
126 class ClassObject : public PyObjectWrapper {
127  public:
128   explicit ClassObject(const py::object &obj, const std::string &name = "Python dataclass")
PyObjectWrapper(obj,name)129       : PyObjectWrapper(obj, name) {}
130   ~ClassObject() override = default;
131   MS_DECLARE_PARENT(ClassObject, PyObjectWrapper);
132   abstract::AbstractBasePtr ToAbstract() override;
133 };
134 
135 // ClassType class wrappers class name in python
136 class ClassType : public PyObjectWrapper {
137  public:
138   explicit ClassType(const py::object &obj, const std::string &name = "Python class type")
PyObjectWrapper(obj,name)139       : PyObjectWrapper(obj, name) {}
140   ~ClassType() override = default;
141   MS_DECLARE_PARENT(ClassType, PyObjectWrapper);
142   abstract::AbstractBasePtr ToAbstract() override;
143 };
144 using ClassTypePtr = std::shared_ptr<ClassType>;
145 
146 // SymbolResolver class for resolving symbol extracted from AnfNode.
147 class SymbolResolver {
148  public:
SymbolResolver(const NameSpacePtr & name_space,const SymbolPtr & symbol,const AnfNodePtr & node)149   SymbolResolver(const NameSpacePtr &name_space, const SymbolPtr &symbol, const AnfNodePtr &node)
150       : namespace_(name_space), symbol_(symbol), resolved_node_(node) {}
151 
152   ~SymbolResolver() = default;
153 
154   // resolve symbol in namespace and save it in result_;
155   bool Resolve();
156 
get_namespace()157   NameSpacePtr get_namespace() { return namespace_; }
158 
symbol()159   SymbolPtr symbol() { return symbol_; }
160 
result()161   const py::object &result() { return result_; }
162 
resolved_node()163   AnfNodePtr resolved_node() { return resolved_node_; }
164 
165  private:
166   // namespace where the symbol locates
167   NameSpacePtr namespace_;
168   // the symbol that needs to be resovled
169   SymbolPtr symbol_;
170   // the node that has been resolved
171   AnfNodePtr resolved_node_;
172   // Resolve result
173   py::object result_;
174 };
175 using SymbolResolverPtr = std::shared_ptr<SymbolResolver>;
176 // Resolve symbol in namespace.
177 AnfNodePtr ResolveSymbol(const FuncGraphManagerPtr &manager, const NameSpacePtr &name_space, const SymbolPtr &symbol,
178                          const AnfNodePtr &node);
179 
180 // Resolve Cell with attr name.
181 AnfNodePtr ResolveCellwithAttr(const FuncGraphManagerPtr &manager, const NameSpacePtr &name_space,
182                                const SymbolPtr &symbol, const AnfNodePtr &node, const AnfNodePtr &attr);
183 
184 // Resolve one graph which normally is the root graph. FuncGraph shall be managed by res->manager().
185 bool ResolveFuncGraph(const FuncGraphPtr &func_graph, const pipeline::ResourceBasePtr &res, bool use_profile = true);
186 
187 // Resolve all graphs in manager which is defined outside of pipeline::Resource.
188 // Mainly used for test cases or resolve graphs which will not be managed by manager.
189 bool ResolveAll(const FuncGraphManagerPtr &manager);
190 
191 }  // namespace parse
192 }  // namespace mindspore
193 
194 #endif  // MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_RESOLVE_H_
195