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.

meta_func_graph.h 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 MINDSPORE_CCSRC_IR_META_FUNC_GRAPH_H_
  19. #define MINDSPORE_CCSRC_IR_META_FUNC_GRAPH_H_
  20. #include <unordered_map>
  21. #include <string>
  22. #include <map>
  23. #include <memory>
  24. #include <vector>
  25. #include <algorithm>
  26. #include "pybind11/pybind11.h"
  27. #include "ir/dtype.h"
  28. #include "ir/anf.h"
  29. #include "ir/func_graph.h"
  30. #include "pipeline/static_analysis/abstract_value.h"
  31. namespace py = pybind11;
  32. namespace mindspore {
  33. // namespace to support intermediate representation definition
  34. // Graph generator.
  35. // Can be called with a pipeline's resources and a list of argument types to
  36. // generate a graph corresponding to these types.
  37. class MetaFuncGraph : public FuncGraphBase {
  38. public:
  39. explicit MetaFuncGraph(const std::string &name) : name_(name) { cache_.clear(); }
  40. ~MetaFuncGraph() override = default;
  41. MS_DECLARE_PARENT(MetaFuncGraph, FuncGraphBase);
  42. abstract::AbstractBasePtr MakeAbstractClosure(const AnfNodePtr &anf_node);
  43. // Return normalized versions of the arguments.
  44. // By default, this returns args unchanged.
  45. virtual abstract::AbstractBasePtrList NormalizeArgs(const abstract::AbstractBasePtrList &args_spec_list) const {
  46. return args_spec_list;
  47. }
  48. const std::vector<Signature> &signatures() const { return signatures_; }
  49. void set_signatures(const std::vector<Signature> &signatures) { signatures_ = signatures; }
  50. // Generate a Graph for the given abstract arguments.
  51. virtual FuncGraphPtr GenerateFuncGraph(const abstract::AbstractBasePtrList &args_spec_list) {
  52. TypePtrList types;
  53. (void)std::transform(args_spec_list.begin(), args_spec_list.end(), std::back_inserter(types),
  54. [](const AbstractBasePtr &arg) -> TypePtr {
  55. MS_EXCEPTION_IF_NULL(arg);
  56. return arg->BuildType();
  57. });
  58. // filter unsafe characters in log print since name_ is from outside
  59. auto iter = cache_.find(types);
  60. if (iter == cache_.end()) {
  61. FuncGraphPtr fg = GenerateFromTypes(types);
  62. MS_EXCEPTION_IF_NULL(fg);
  63. MS_LOG(INFO) << "MetaFuncgraph: cache miss for types: " << mindspore::ToString(args_spec_list)
  64. << ", g: " << fg->ToString();
  65. cache_[types] = fg;
  66. return fg;
  67. } else {
  68. MS_LOG(DEBUG) << "MetaFuncgraph: cache hit for types: " << mindspore::ToString(args_spec_list)
  69. << ", g: " << iter->second->ToString();
  70. return iter->second;
  71. }
  72. }
  73. // Generate a Graph for this type signature.
  74. virtual FuncGraphPtr GenerateFromTypes(const TypePtrList &) {
  75. MS_LOG(EXCEPTION) << "Undefine the method of generating graph from types.";
  76. }
  77. std::string name() { return name_; }
  78. std::string ToString() const override { return name_; }
  79. std::size_t hash() const override { return tid(); }
  80. virtual bool operator==(const MetaFuncGraph &other) const { return &other == this; }
  81. bool operator==(const Value &other) const override {
  82. if (other.isa<MetaFuncGraph>()) {
  83. return &other == this;
  84. } else {
  85. return false;
  86. }
  87. }
  88. const bool parse_info_ = true;
  89. protected:
  90. template <typename Derived>
  91. std::shared_ptr<Derived> shared_from_base() {
  92. return std::static_pointer_cast<Derived>(shared_from_this());
  93. }
  94. std::string name_;
  95. std::vector<Signature> signatures_;
  96. std::unordered_map<TypePtrList, FuncGraphPtr, TypeListHasher, TypeListEqual> cache_;
  97. };
  98. using MetaFuncGraphPtr = std::shared_ptr<MetaFuncGraph>;
  99. } // namespace mindspore
  100. #endif // MINDSPORE_CCSRC_IR_META_FUNC_GRAPH_H_