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.

analysis_context.h 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef PIPELINE_STATIC_ANALYSIS_ANALYSIS_CONTEXT_H_
  19. #define PIPELINE_STATIC_ANALYSIS_ANALYSIS_CONTEXT_H_
  20. #include <memory>
  21. #include <string>
  22. #include <unordered_map>
  23. #include "pipeline/static_analysis/abstract_value.h"
  24. #include "ir/meta_func_graph.h"
  25. namespace mindspore {
  26. namespace abstract {
  27. // AnalysisContext will be stored in Config in AnalysisCache.
  28. class AnalysisContext {
  29. public:
  30. AnalysisContext(const AnalysisContextPtr &parent, const FuncGraphPtr &fg, const AbstractBasePtrList &args_spec_list)
  31. : parent_(parent), func_graph_(fg), args_spec_list_(args_spec_list) {
  32. if (parent_ != nullptr) {
  33. parent_cache_ = parent_->parent_cache_;
  34. }
  35. }
  36. ~AnalysisContext() = default;
  37. // Helper function to wrapper constructor to save shared_ptr in parent_cache.
  38. AnalysisContextPtr NewContext(AnalysisContextPtr parent, FuncGraphPtr fg, const AbstractBasePtrList &args_spec_list) {
  39. AnalysisContextPtr context_new = std::make_shared<AnalysisContext>(parent, fg, args_spec_list);
  40. // Reference to myself, so use weak_ptr to break reference cycle.
  41. context_new->parent_cache_[fg] = std::weak_ptr<AnalysisContext>(context_new);
  42. return context_new;
  43. }
  44. // Extend this context with values for another graph.
  45. AnalysisContextPtr NewFuncGraphContext(const FuncGraphPtr &func_graph, const AbstractBasePtrList &args_spec_list);
  46. // Return a context restricted to a graph's dependencies.
  47. AnalysisContextPtr Filter(const FuncGraphPtr &graph);
  48. bool operator==(const AnalysisContext &other) const;
  49. std::size_t hash();
  50. static AnalysisContextPtr DummyContext();
  51. FuncGraphPtr func_graph() const { return func_graph_; }
  52. AnalysisContextPtr parent() const { return parent_; }
  53. std::string ToString() const;
  54. AnalysisContextPtr SpecializeKey() const;
  55. AbstractBasePtrList args_spec_list() { return args_spec_list_; }
  56. private:
  57. AnalysisContextPtr parent_;
  58. FuncGraphPtr func_graph_;
  59. AbstractBasePtrList args_spec_list_;
  60. std::unordered_map<FuncGraphPtr, std::weak_ptr<AnalysisContext>> parent_cache_;
  61. };
  62. struct ContextHasher {
  63. std::size_t operator()(const AnalysisContextPtr &t) const {
  64. std::size_t hash = t->hash();
  65. return hash;
  66. }
  67. };
  68. struct ContextEqual {
  69. bool operator()(const AnalysisContextPtr &lhs, const AnalysisContextPtr &rhs) const { return *lhs == *rhs; }
  70. };
  71. extern const AnalysisContextPtr kDummyAnalysisContext;
  72. } // namespace abstract
  73. } // namespace mindspore
  74. #endif // PIPELINE_STATIC_ANALYSIS_ANALYSIS_CONTEXT_H_