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.

anf_ir_utils.h 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright 2019 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. #ifndef MINDSPORE_CCSRC_DEBUG_ANF_IR_UTILS_H_
  17. #define MINDSPORE_CCSRC_DEBUG_ANF_IR_UTILS_H_
  18. #include <string>
  19. #include <vector>
  20. #include <fstream>
  21. #include <map>
  22. #include <memory>
  23. #include <unordered_map>
  24. #include <unordered_set>
  25. #include <algorithm>
  26. #include "ir/anf.h"
  27. #include "ir/func_graph.h"
  28. #include "ir/meta_func_graph.h"
  29. #include "pipeline/jit/parse/python_adapter.h"
  30. #include "pipeline/jit/parse/resolve.h"
  31. #include "frontend/operator/composite/composite.h"
  32. #include "utils/symbolic.h"
  33. #include "utils/ordered_map.h"
  34. #include "utils/ordered_set.h"
  35. #include "utils/utils.h"
  36. namespace mindspore {
  37. struct ParamPtrEqual {
  38. bool operator()(AnfNodePtr const &t1, AnfNodePtr const &t2) const {
  39. const ParameterPtr param1 = dyn_cast<Parameter>(t1);
  40. const ParameterPtr param2 = dyn_cast<Parameter>(t2);
  41. if (param1 == nullptr || param2 == nullptr) {
  42. return false;
  43. }
  44. return *param1 == *param2;
  45. }
  46. };
  47. struct ParamPtrHasher {
  48. std::size_t operator()(AnfNodePtr const &param) const {
  49. const ParameterPtr parameter = dyn_cast<Parameter>(param);
  50. if (parameter == nullptr) {
  51. return 0;
  52. }
  53. std::size_t hash = std::hash<std::string>()(parameter->name());
  54. return hash;
  55. }
  56. };
  57. class AnfExporter {
  58. public:
  59. explicit AnfExporter(const std::string &id, bool export_used = true, bool check_integrity = false)
  60. : param_index(-1), id_(id), export_used_(export_used), check_integrity_(check_integrity) {
  61. func_graph_set.clear();
  62. exported.clear();
  63. }
  64. virtual ~AnfExporter() {}
  65. void ExportFuncGraph(const std::string &filename, const FuncGraphPtr &func_graph);
  66. void ExportFuncGraph(const std::string &filename, const std::vector<TaggedGraph> &graphs);
  67. protected:
  68. virtual std::string GetNodeType(const AnfNodePtr &nd);
  69. int GetParamIndex(const FuncGraphPtr &func_graph, const AnfNodePtr &param, bool throw_excp = true);
  70. int GetParamIndexFromExported(const AnfNodePtr &param);
  71. std::string DumpObject(const py::object &obj, const std::string &category) const;
  72. std::string GetValueNodeText(const FuncGraphPtr &func_graph, const ValueNodePtr &node);
  73. std::string GetMultitypeFuncGraphText(const prim::MultitypeFuncGraphPtr &mt_func_graph);
  74. std::string GetSymbolicKeyInstanceText(const FuncGraphPtr &func_graph, const SymbolicKeyInstancePtr &sym_inst);
  75. std::string GetSequenceText(const FuncGraphPtr &func_graph, const ValuePtr &value);
  76. std::string GetValueText(const FuncGraphPtr &func_graph, const ValuePtr &value);
  77. std::string GetOtherValueText(const FuncGraphPtr &func_graph, const ValuePtr &value);
  78. std::string GetPrimitiveText(const PrimitivePtr &prim);
  79. std::string GetDictText(const FuncGraphPtr &func_graph, const ValuePtr &value);
  80. std::string GetNameSpaceText(const parse::NameSpacePtr &ns);
  81. std::string GetMetaFuncGraphText(const MetaFuncGraphPtr &meta_func_graph);
  82. std::string GetAnfNodeText(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
  83. const std::map<AnfNodePtr, int> &apply_map);
  84. virtual void ExportOneFuncGraph(std::ofstream &ofs, const FuncGraphPtr &func_graph);
  85. void OutputParameters(std::ofstream &ofs, const std::vector<AnfNodePtr> &parameters,
  86. OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual> *param_map);
  87. void OutputStatementComment(std::ofstream &ofs, const CNodePtr &node);
  88. virtual void OutputCNodes(std::ofstream &ofs, const std::vector<AnfNodePtr> &nodes, const FuncGraphPtr &func_graph);
  89. void OutputOrderList(std::ofstream &ofs, const FuncGraphPtr &func_graph);
  90. void OutputIsolateNodes(std::ofstream &ofs, const FuncGraphPtr &func_graph);
  91. int param_index;
  92. OrderedSet<FuncGraphPtr> func_graph_set{};
  93. OrderedMap<FuncGraphPtr, OrderedMap<AnfNodePtr, int, ParamPtrHasher, ParamPtrEqual>> exported;
  94. std::string id_;
  95. bool export_used_ = true; // whether export function graphs used in current exporting function graph
  96. bool check_integrity_ = false; // whether check integrity or not, when dumping ir for loading, must set it to true
  97. TaggedNodeMap tagged_cnodes_;
  98. abstract::AnfNodeConfigPtr node_cfg_ = nullptr;
  99. };
  100. void ExportIR(const std::string &filename, const std::string &id, const FuncGraphPtr &func_graph);
  101. void ExportIR(const std::string &filename, const std::vector<TaggedGraph> &graphs);
  102. std::vector<FuncGraphPtr> ImportIR(const std::string &filename);
  103. } // namespace mindspore
  104. #endif // MINDSPORE_CCSRC_DEBUG_ANF_IR_UTILS_H_