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.

proto_exporter.h 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright 2021 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_PROTO_EXPORTER_H
  17. #define MINDSPORE_PROTO_EXPORTER_H
  18. #include <map>
  19. #include <string>
  20. #include <vector>
  21. #include "debug/common.h"
  22. #include "debug/data_dump/dump_json_parser.h"
  23. #include "ir/graph_utils.h"
  24. #include "proto/debug_graph.pb.h"
  25. #include "utils/symbolic.h"
  26. #include "utils/trace_base.h"
  27. namespace mindspore {
  28. enum LocDebugDumpMode { kDebugOff = 0, kDebugTopStack = 1, kDebugWholeStack = 2 };
  29. class DebuggerProtoExporter {
  30. public:
  31. DebuggerProtoExporter() {}
  32. ~DebuggerProtoExporter() {}
  33. std::string GetFuncGraphProtoString(const FuncGraphPtr &func_graph,
  34. LocDebugDumpMode dump_location = kDebugWholeStack);
  35. debugger::ModelProto GetFuncGraphProto(const FuncGraphPtr &func_graph);
  36. private:
  37. void InitModelInfo();
  38. void GetOpNodeTypeAndAttrs(const FuncGraphPtr &func_graph, const AnfNodePtr &node, debugger::NodeProto *node_proto);
  39. std::string GetOpNodeInputId(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
  40. const std::map<AnfNodePtr, size_t> &apply_map,
  41. std::map<AnfNodePtr, size_t> *const_map_ptr);
  42. void SetValueToProto(const ValuePtr &attr_value, debugger::ValueProto *value_proto);
  43. void SetScalarToProto(const ScalarPtr &val, debugger::ValueProto *value_proto);
  44. void SetSequenceToProto(const ValueSequencePtr &val, debugger::ValueProto *value_proto);
  45. void SetDictionaryToProto(const ValueDictionaryPtr &val, debugger::ValueProto *value_proto);
  46. void SetNodeOutputType(const AnfNodePtr &node, debugger::TypeProto *type_proto);
  47. void ExportFuncGraph(const FuncGraphPtr &func_graph, debugger::GraphProto *const graph_proto,
  48. LocDebugDumpMode dump_location = kDebugWholeStack);
  49. void ExportParameters(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto);
  50. void ExportCNodes(const FuncGraphPtr &func_graph, debugger::GraphProto *const graph_proto,
  51. std::map<AnfNodePtr, size_t> *const_map_ptr, LocDebugDumpMode dump_location = kDebugWholeStack);
  52. void ExportCNode(const FuncGraphPtr &func_graph, const CNodePtr &node, std::map<AnfNodePtr, size_t> *apply_map_ptr,
  53. std::map<AnfNodePtr, size_t> *const_map_ptr, debugger::GraphProto *const graph_proto,
  54. LocDebugDumpMode dump_location);
  55. void ExportFuncGraphOutput(const FuncGraphPtr &func_graph, const CNodePtr &ret_node,
  56. const std::map<AnfNodePtr, size_t> &apply_map, std::map<AnfNodePtr, size_t> *const_map_ptr,
  57. debugger::GraphProto *graph_proto);
  58. void ExportValueNodes(const std::map<AnfNodePtr, size_t> &const_map, debugger::GraphProto *graph_proto);
  59. static std::string GetConstNodeId(size_t idx) { return std::string("cst") + std::to_string(idx); }
  60. debugger::ModelProto model_;
  61. };
  62. void DumpIRProtoWithSrcInfo(const FuncGraphPtr &func_graph, const std::string &suffix, const std::string &target_dir,
  63. LocDebugDumpMode dump_location = kDebugWholeStack);
  64. } // namespace mindspore
  65. #endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_MINDSPORE_PROTO_EXPORTER_H_