• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
3  *
4  * Copyright 2019-2022 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 
19 #ifndef MINDSPORE_CORE_ABSTRACT_ANALYSIS_CONTEXT_H_
20 #define MINDSPORE_CORE_ABSTRACT_ANALYSIS_CONTEXT_H_
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 #include <utility>
26 #include <unordered_map>
27 
28 #include "mindapi/base/macros.h"
29 #include "utils/hashing.h"
30 #include "ir/func_graph.h"
31 #include "abstract/abstract_value.h"
32 
33 namespace mindspore {
34 namespace abstract {
35 //
36 // AnalysisContext represents the context that a func graph is called.
37 // - parent context: determines the free variables used by the func graph;
38 // - func graph: the func graph be called in current context;
39 // - argument list: the argument abstracts used to call the func graph.
40 //
41 class MS_CORE_API AnalysisContext : public std::enable_shared_from_this<AnalysisContext> {
42  public:
43   ~AnalysisContext() = default;
44 
parent()45   AnalysisContextPtr parent() const { return parent_ == nullptr ? nullptr : parent_->shared_from_this(); }
func_graph()46   const FuncGraphPtr &func_graph() const { return func_graph_; }
args_abs_list()47   const AbstractBasePtrList &args_abs_list() const { return args_abs_list_; }
48 
49   // Extend this context with values for another graph.
50   // Not call directly. Call NewContext(AnalysisContextPtr, FuncGraphPtr, AbstractBasePtrList) instead.
51   AnalysisContextPtr NewContext(const FuncGraphPtr &fg, const AbstractBasePtrList &args_abs_list);
52 
53   AnalysisContextPtr GetCachedContext(const FuncGraphPtr &fg, const AbstractBasePtrList &args_abs_list);
54 
55   // Return a context restricted to a graph and its parent.
56   AnalysisContextPtr FindOwnOrParentContext(FuncGraph *fg);
57 
58   std::string ToString() const;
59 
60   // Return current root dummy context.
61   static AnalysisContextPtr DummyContext();
62 
63   // Create a new root dummy context.
64   static AnalysisContextPtr NewDummyContext();
65 
66   // Clear all contexts to release resources.
67   static void ClearContext();
68 
69  protected:
70   // Make constructor protected to prevent creating an isolated context object.
AnalysisContext(AnalysisContext * parent,const FuncGraphPtr & fg,const AbstractBasePtrList & args_abs_list)71   AnalysisContext(AnalysisContext *parent, const FuncGraphPtr &fg, const AbstractBasePtrList &args_abs_list)
72       : parent_(parent), func_graph_(fg), args_abs_list_(args_abs_list) {}
73 
74  private:
75   // Clear to release resources.
76   void Clear();
77 
78   // Find context from the given func graph.
79   AnalysisContext *FindContext(const FuncGraphPtr &fg);
80 
81   // Create a new context instance.
82   static AnalysisContextPtr CreateContext(AnalysisContext *parent, const FuncGraphPtr &fg,
83                                           const AbstractBasePtrList &args_abs_list);
84 
85   using ChildKey = std::pair<FuncGraphPtr, AbstractBasePtrList>;
86 
87   struct ChildHash {
88     std::size_t operator()(const ChildKey &key) const noexcept;
89   };
90 
91   struct ChildEqual {
92     bool operator()(const ChildKey &a, const ChildKey &b) const noexcept;
93   };
94 
95   // Parent context, use raw pointer to avoid cycle reference.
96   AnalysisContext *parent_;
97 
98   // Func graph for current context.
99   FuncGraphPtr func_graph_;
100 
101   // Func graph arguments in current context.
102   AbstractBasePtrList args_abs_list_;
103 
104   // Children contexts discriminated by func_graph & arguments.
105   std::unordered_map<ChildKey, AnalysisContextPtr, ChildHash, ChildEqual> children_;
106 
107   // Root dummy contexts.
108   static std::vector<AnalysisContextPtr> dummy_contexts_;
109 };
110 }  // namespace abstract
111 }  // namespace mindspore
112 #endif  // MINDSPORE_CORE_ABSTRACT_ANALYSIS_CONTEXT_H_
113