• 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 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 <unordered_map>
25 #include <list>
26 
27 #include "abstract/abstract_value.h"
28 #include "ir/meta_func_graph.h"
29 
30 namespace mindspore {
31 namespace abstract {
32 class AnalysisContext;
33 using AnalysisContextWeakPtr = std::weak_ptr<AnalysisContext>;
34 using ArgsSpecToAnalysisContextMap =
35   std::unordered_map<AbstractBasePtrList, AnalysisContextWeakPtr, AbstractBasePtrListHasher, AbstractBasePtrListEqual>;
36 
37 // AnalysisContext will be stored in Config in AnalysisCache.
38 class AnalysisContext {
39  public:
AnalysisContext(const AnalysisContextPtr & parent,const FuncGraphPtr & fg,const AbstractBasePtrList & args_spec_list)40   AnalysisContext(const AnalysisContextPtr &parent, const FuncGraphPtr &fg, const AbstractBasePtrList &args_spec_list)
41       : parent_(parent), func_graph_(fg), args_spec_list_(args_spec_list) {
42     if (parent_ != nullptr) {
43       extant_context_cache_ = parent_->extant_context_cache_;
44     }
45   }
46   ~AnalysisContext() = default;
47 
48   // Extend this context with values for another graph.
49   AnalysisContextPtr NewContext(const FuncGraphPtr &func_graph, const AbstractBasePtrList &args_spec_list);
50 
51   // Return a context restricted to a graph and its parent.
52   AnalysisContextPtr FindOwnOrParentContext(const FuncGraphPtr &graph);
53   bool operator==(const AnalysisContext &other) const;
54   std::size_t hash();
55   static AnalysisContextPtr DummyContext();
56   bool IsDummyContext();
func_graph()57   FuncGraphPtr func_graph() const { return func_graph_; }
parent()58   AnalysisContextPtr parent() const { return parent_; }
59   std::string ToString() const;
60   AnalysisContextPtr SpecializeKey() const;
args_spec_list()61   AbstractBasePtrList args_spec_list() { return args_spec_list_; }
62   static void ClearContext();
63   static AnalysisContextPtr CreateContext(const AnalysisContextPtr &parent, const FuncGraphPtr &fg,
64                                           const AbstractBasePtrList &args_spec_list);
65 
66  private:
67   AnalysisContextPtr parent_;
68   FuncGraphPtr func_graph_;
69   AbstractBasePtrList args_spec_list_;
70   // Record all created context for each func graph.
71   // `extant_context_cache_` is copied from its parent context.
72   std::unordered_map<FuncGraphPtr, AnalysisContextWeakPtr> extant_context_cache_;
73   // Record all created child contexts from this context.
74   // Like: key: [func_graph & arguments], value: [child_context]
75   std::unordered_map<FuncGraphPtr, ArgsSpecToAnalysisContextMap> children_cache_;
76 
77   // There may may be shared_ptr loop like:
78   // FuncGraphAbstactClosur->AnalysisContext->children_cache_->ArgsSpec->FuncGraphAbstactClosur.
79   // For break the loop, using all_context_ to clear context_.
80   static std::list<AnalysisContextPtr> all_context_;
81 };
82 
83 struct ContextHasher {
operatorContextHasher84   std::size_t operator()(const AnalysisContextPtr &t) const {
85     std::size_t hash = t->hash();
86     return hash;
87   }
88 };
89 
90 struct ContextEqual {
operatorContextEqual91   bool operator()(const AnalysisContextPtr &lhs, const AnalysisContextPtr &rhs) const { return *lhs == *rhs; }
92 };
93 
94 extern const AnalysisContextPtr kDummyAnalysisContext;
95 }  // namespace abstract
96 }  // namespace mindspore
97 #endif  // MINDSPORE_CORE_ABSTRACT_ANALYSIS_CONTEXT_H_
98