From 722eb2ec5a6daf67b31c5cae4b632e4f8aa51efd Mon Sep 17 00:00:00 2001 From: Islam Amin Date: Thu, 18 Feb 2021 13:16:03 -0500 Subject: [PATCH] ascend graph dump trigger at data dump --- .../ccsrc/backend/session/ascend_session.cc | 27 +++ .../ccsrc/backend/session/gpu_session.cc | 23 ++- mindspore/ccsrc/debug/anf_ir_dump.cc | 8 +- mindspore/ccsrc/debug/anf_ir_dump.h | 2 +- mindspore/ccsrc/debug/common.h | 2 +- .../ccsrc/debug/data_dump/dump_json_parser.cc | 22 +++ .../ccsrc/debug/data_dump/dump_json_parser.h | 1 + .../ccsrc/debug/debugger/debug_graph.proto | 1 + .../ccsrc/debug/debugger/proto_exporter.cc | 154 +++++++++++------- .../ccsrc/debug/debugger/proto_exporter.h | 73 +++++++++ .../debug/debugger/proto_exporter_stub.h | 29 ++++ mindspore/ccsrc/debug/dump_proto.cc | 32 ++-- 12 files changed, 299 insertions(+), 75 deletions(-) create mode 100644 mindspore/ccsrc/debug/debugger/proto_exporter.h create mode 100644 mindspore/ccsrc/debug/debugger/proto_exporter_stub.h diff --git a/mindspore/ccsrc/backend/session/ascend_session.cc b/mindspore/ccsrc/backend/session/ascend_session.cc index 16bde676b7..deb225bde1 100644 --- a/mindspore/ccsrc/backend/session/ascend_session.cc +++ b/mindspore/ccsrc/backend/session/ascend_session.cc @@ -65,6 +65,11 @@ #include "debug/data_dump/e2e_dump_util.h" #include "debug/anf_ir_dump.h" #include "debug/dump_proto.h" +#ifdef ENABLE_DEBUGGER +#include "debug/debugger/proto_exporter.h" +#else +#include "debug/debugger/proto_exporter_stub.h" +#endif #include "toolchain/adx_datadump_server.h" #ifdef ENABLE_DUMP_IR #include "debug/rdr/running_data_recorder.h" @@ -802,6 +807,7 @@ void AscendSession::SelectKernel(const KernelGraph &kernel_graph) const { void DumpInit() { auto &json_parser = DumpJsonParser::GetInstance(); json_parser.Parse(); + json_parser.CopyJsonToDir(); if (json_parser.async_dump_enabled()) { if (AdxDataDumpServerInit() != 0) { MS_LOG(EXCEPTION) << "Adx data dump server init failed"; @@ -999,6 +1005,14 @@ void AscendSession::DumpAllGraphs(const std::vector &all_graphs) auto context_ptr = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(context_ptr); bool save_graphs = context_ptr->get_param(MS_CTX_SAVE_GRAPHS_FLAG); + auto &json_parser = DumpJsonParser::GetInstance(); + json_parser.Parse(); + if (!save_graphs && !json_parser.e2e_dump_enabled() && !json_parser.async_dump_enabled()) { + return; + } + auto kernel_runtime = device::KernelRuntimeManager::Instance().GetKernelRuntime(kAscendDevice, device_id_); + MS_EXCEPTION_IF_NULL(kernel_runtime); + uint32_t device_id = kernel_runtime->device_id(); for (auto &graph : all_graphs) { MS_EXCEPTION_IF_NULL(graph); std::string tag = "graph_build"; @@ -1009,6 +1023,19 @@ void AscendSession::DumpAllGraphs(const std::vector &all_graphs) DumpIRProto(graph, "vm_build_" + std::to_string(graph->graph_id())); DumpIR("trace_code_graph", graph, true, kWholeStack); } + std::string final_graph = "trace_code_graph_" + std::to_string(graph->graph_id()); + if (json_parser.e2e_dump_enabled()) { + std::string target_dir = + json_parser.path() + "/" + json_parser.net_name() + "/device_" + std::to_string(device_id) + "/graphs"; + std::string ir_file_path = target_dir + "/" + "ms_output_" + final_graph + ".ir"; + DumpIRProtoWithSrcInfo(graph, final_graph, target_dir, kDebugWholeStack); + DumpIR("trace_code_graph", graph, true, kWholeStack, ir_file_path); + } else if (json_parser.async_dump_enabled()) { + std::string target_dir = json_parser.path() + "/device_" + std::to_string(device_id) + "/graphs"; + std::string ir_file_path = target_dir + "/" + "ms_output_" + final_graph + ".ir"; + DumpIRProtoWithSrcInfo(graph, final_graph, target_dir, kDebugWholeStack); + DumpIR("trace_code_graph", graph, true, kWholeStack, ir_file_path); + } } #endif } diff --git a/mindspore/ccsrc/backend/session/gpu_session.cc b/mindspore/ccsrc/backend/session/gpu_session.cc index 8564de2ec5..8a4153590a 100644 --- a/mindspore/ccsrc/backend/session/gpu_session.cc +++ b/mindspore/ccsrc/backend/session/gpu_session.cc @@ -62,7 +62,14 @@ #include "backend/optimizer/pass/communication_op_fusion.h" #include "backend/optimizer/pass/getitem_tuple.h" #include "common/trans.h" +#include "debug/anf_ir_dump.h" #include "debug/data_dump/e2e_dump_util.h" +#ifdef ENABLE_DEBUGGER +#include "debug/debugger/proto_exporter.h" +#else +#include "debug/debugger/proto_exporter_stub.h" +#endif +#include "debug/data_dump/dump_json_parser.h" #include "debug/tensor_load.h" #include "debug/dump_proto.h" #include "runtime/device/gpu/gpu_kernel_build.h" @@ -103,7 +110,9 @@ void GPUSession::Init(uint32_t device_id) { auto ms_context = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(ms_context); ms_context->set_param(MS_CTX_DEVICE_ID, device_id); - + auto &json_parser = DumpJsonParser::GetInstance(); + // Dump json config file if dump is enabled + json_parser.CopyJsonToDir(); MS_LOG(INFO) << "Set device id " << device_id << " for gpu session."; InitExecutor(kGPUDevice, device_id); } @@ -361,6 +370,11 @@ GraphId GPUSession::CompileGraphImpl(KernelGraphPtr graph) { auto context_ptr = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(context_ptr); bool save_graphs = context_ptr->get_param(MS_CTX_SAVE_GRAPHS_FLAG); + auto runtime_instance = device::KernelRuntimeManager::Instance().GetSingleKernelRuntime(kGPUDevice, device_id_); + MS_EXCEPTION_IF_NULL(runtime_instance); + uint32_t device_id = runtime_instance->device_id(); + auto &json_parser = DumpJsonParser::GetInstance(); + json_parser.Parse(); // Dump .pb graph before graph optimization if (save_graphs) { DumpIRProto(graph, "before_opt_" + std::to_string(graph->graph_id())); @@ -399,6 +413,13 @@ GraphId GPUSession::CompileGraphImpl(KernelGraphPtr graph) { if (save_graphs) { DumpIRProto(graph, "after_opt_" + std::to_string(graph->graph_id())); } + if (json_parser.e2e_dump_enabled()) { + std::string target_dir = + json_parser.path() + "/" + json_parser.net_name() + "/device_" + std::to_string(device_id) + "/graphs"; + std::string ir_file_path = target_dir + "/" + "after_opt_" + std::to_string(graph->graph_id()) + ".ir"; + DumpIRProtoWithSrcInfo(graph, "after_opt_" + std::to_string(graph->graph_id()), target_dir, kDebugWholeStack); + DumpIR("trace_code_graph", graph, true, kWholeStack, ir_file_path); + } // Set graph manager. MS_EXCEPTION_IF_NULL(context_); FuncGraphManagerPtr manager = MakeManager({graph}); diff --git a/mindspore/ccsrc/debug/anf_ir_dump.cc b/mindspore/ccsrc/debug/anf_ir_dump.cc index 4473c6da88..97445e2a63 100644 --- a/mindspore/ccsrc/debug/anf_ir_dump.cc +++ b/mindspore/ccsrc/debug/anf_ir_dump.cc @@ -558,12 +558,16 @@ void GetEnvDumpIrLineLevel(LocDumpMode *dump_location) { } #ifdef ENABLE_DUMP_IR -void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name, LocDumpMode dump_location) { +void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name, LocDumpMode dump_location, + const std::string &target_file) { GetEnvDumpIrLineLevel(&dump_location); if (graph == nullptr) { return; } auto path = pipeline::GetSaveGraphsPathName(AddGlobalId(filename)); + if (!target_file.empty()) { + path = target_file; + } auto realpath = Common::GetRealPath(path); if (!realpath.has_value()) { MS_LOG(ERROR) << "Get real path failed. path=" << path; @@ -642,7 +646,7 @@ void DumpIRForRDR(const std::string &filename, const FuncGraphPtr &graph, bool d } #else -void DumpIR(const std::string &, const FuncGraphPtr &, bool, LocDumpMode) { +void DumpIR(const std::string &, const FuncGraphPtr &, bool, LocDumpMode, const std::string &) { static bool already_printed = false; if (already_printed) { return; diff --git a/mindspore/ccsrc/debug/anf_ir_dump.h b/mindspore/ccsrc/debug/anf_ir_dump.h index 90de5fa22f..47831b071a 100644 --- a/mindspore/ccsrc/debug/anf_ir_dump.h +++ b/mindspore/ccsrc/debug/anf_ir_dump.h @@ -25,7 +25,7 @@ namespace mindspore { enum LocDumpMode { kOff = 0, kTopStack = 1, kWholeStack = 2 }; constexpr char PARALLEL_STRATEGY[] = "strategy"; void DumpIR(const std::string &filename, const FuncGraphPtr &func_graph, bool dump_full_name = false, - LocDumpMode dump_location = kOff); + LocDumpMode dump_location = kOff, const std::string &target_file = ""); void PrintInputAndOutputInferType(std::ostringstream &buffer, const AnfNodePtr &nd); void DumpIRForRDR(const std::string &filename, const FuncGraphPtr &func_graph, bool dump_full_name = false, diff --git a/mindspore/ccsrc/debug/common.h b/mindspore/ccsrc/debug/common.h index e1913af5e4..fefd325d4c 100644 --- a/mindspore/ccsrc/debug/common.h +++ b/mindspore/ccsrc/debug/common.h @@ -38,9 +38,9 @@ class Common { const bool &print_str = true); static bool IsFilenameValid(const std::string &filename, const int &length_limit, const std::string &error_message = ""); + static bool CreateNotExistDirs(const std::string &path); private: - static bool CreateNotExistDirs(const std::string &path); static bool IsEveryFilenameValid(const std::string &path, const int &length_limit, const std::string &error_message); }; } // namespace mindspore diff --git a/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc b/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc index 6ef4471bd8..b9c1ac5e7c 100644 --- a/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc +++ b/mindspore/ccsrc/debug/data_dump/dump_json_parser.cc @@ -112,6 +112,28 @@ void DumpJsonParser::Parse() { JudgeDumpEnabled(); } +void DumpJsonParser::CopyJsonToDir() { + this->Parse(); + if (!IsDumpEnabled()) { + return; + } + auto dump_config_file = Common::GetConfigFile(kMindsporeDumpConfig); + if (!dump_config_file.has_value()) { + MS_LOG(EXCEPTION) << "Get dump config file failed"; + } + std::ifstream json_file(dump_config_file.value()); + if (async_dump_enabled_ || e2e_dump_enabled_) { + auto realpath = Common::GetRealPath(path_ + "/.metadata/data_dump.json"); + if (!realpath.has_value()) { + MS_LOG(ERROR) << "Get real path failed in CopyJsonDir."; + } + std::ofstream json_copy(realpath.value()); + json_copy << json_file.rdbuf(); + json_copy.close(); + ChangeFileMode(realpath.value(), S_IRUSR); + } +} + bool DumpJsonParser::DumpToFile(const std::string &filename, const void *data, size_t len) { if (filename.empty() || data == nullptr || len == 0) { MS_LOG(ERROR) << "Incorrect parameter."; diff --git a/mindspore/ccsrc/debug/data_dump/dump_json_parser.h b/mindspore/ccsrc/debug/data_dump/dump_json_parser.h index b1b8c5ae91..5c002e5a3b 100644 --- a/mindspore/ccsrc/debug/data_dump/dump_json_parser.h +++ b/mindspore/ccsrc/debug/data_dump/dump_json_parser.h @@ -34,6 +34,7 @@ class DumpJsonParser { void Parse(); static bool DumpToFile(const std::string &filename, const void *data, size_t len); + void CopyJsonToDir(); bool NeedDump(const std::string &op_full_name) const; void MatchKernel(const std::string &kernel_name); void PrintUnusedKernel(); diff --git a/mindspore/ccsrc/debug/debugger/debug_graph.proto b/mindspore/ccsrc/debug/debugger/debug_graph.proto index d268c4fd97..a6c2a7e332 100644 --- a/mindspore/ccsrc/debug/debugger/debug_graph.proto +++ b/mindspore/ccsrc/debug/debugger/debug_graph.proto @@ -233,6 +233,7 @@ message NodeProto { // for debugger, full name with scope optional string full_name = 8; + optional string source_address = 9; } // Models diff --git a/mindspore/ccsrc/debug/debugger/proto_exporter.cc b/mindspore/ccsrc/debug/debugger/proto_exporter.cc index 7bf35b3b0c..f07885538a 100644 --- a/mindspore/ccsrc/debug/debugger/proto_exporter.cc +++ b/mindspore/ccsrc/debug/debugger/proto_exporter.cc @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "debug/debugger/proto_exporter.h" #include #include @@ -22,60 +23,39 @@ #include #include +#include "debug/common.h" #include "debug/debugger/debugger.h" +#include "debug/data_dump/dump_json_parser.h" #include "proto/debug_graph.pb.h" #include "ir/graph_utils.h" #include "utils/symbolic.h" +#include "utils/trace_base.h" namespace mindspore { -class DebuggerProtoExporter { - public: - DebuggerProtoExporter() {} - ~DebuggerProtoExporter() {} - - std::string GetFuncGraphProtoString(const FuncGraphPtr &func_graph); - debugger::ModelProto GetFuncGraphProto(const FuncGraphPtr &func_graph); - - private: - void InitModelInfo(); - void GetOpNodeTypeAndAttrs(const FuncGraphPtr &func_graph, const AnfNodePtr &node, debugger::NodeProto *node_proto); - std::string GetOpNodeInputId(const FuncGraphPtr &func_graph, const AnfNodePtr &node, - const std::map &apply_map, - std::map *const_map_ptr); - void SetValueToProto(const ValuePtr &attr_value, debugger::ValueProto *value_proto); - void SetScalarToProto(const ScalarPtr &val, debugger::ValueProto *value_proto); - void SetSequenceToProto(const ValueSequeuePtr &val, debugger::ValueProto *value_proto); - void SetDictionaryToProto(const ValueDictionaryPtr &val, debugger::ValueProto *value_proto); - void SetNodeOutputType(const AnfNodePtr &node, debugger::TypeProto *type_proto); - void SetNodeOutputType(const TypePtr &node, const BaseShapePtr &shape, debugger::TypeProto *type_proto); - - void ExportFuncGraph(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto); - void ExportParameters(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto); - void ExportCNodes(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto, - std::map *const_map_ptr); - void ExportCNode(const FuncGraphPtr &func_graph, const CNodePtr &node, std::map *apply_map_ptr, - std::map *const_map_ptr, debugger::GraphProto *graph_proto); - void ExportFuncGraphOutput(const FuncGraphPtr &func_graph, const CNodePtr &ret_node, - const std::map &apply_map, std::map *const_map_ptr, - debugger::GraphProto *graph_proto); - void ExportValueNodes(const std::map &const_map, debugger::GraphProto *graph_proto); - - static std::string GetConstNodeId(size_t idx) { return std::string("cst") + std::to_string(idx); } - - debugger::ModelProto model_; -}; -void DebuggerProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &shape, - debugger::TypeProto *type_proto) { - if (type_proto == nullptr) { - return; +void CheckIfValidType(const TypePtr &type, debugger::TypeProto *type_proto) { + if (!(type->isa() || type->isa() || type->isa() || type->isa() || + type->isa() || type->isa() || type->isa() || type->isa() || + type->isa() || type->isa() || type->isa() || type->isa() || + type->isa() || type->isa())) { + MS_LOG(EXCEPTION) << "Unknown type: " << type->type_name(); } - if (type == nullptr) { type_proto->set_data_type(debugger::DT_UNDEFINED); } else if (type->isa()) { type_proto->set_data_type(GetDebuggerNumberDataType(type)); - } else if (type->isa()) { + } +} + +void DebuggerProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &shape, + debugger::TypeProto *type_proto) { + if (type_proto == nullptr) { + return; + } + if (type != nullptr) { + CheckIfValidType(type, type_proto); + } + if (type->isa()) { TypePtr elem_type = dyn_cast(type)->element(); type_proto->mutable_tensor_type()->set_elem_type(GetDebuggerNumberDataType(elem_type)); type_proto->set_data_type(debugger::DT_TENSOR); @@ -111,14 +91,10 @@ void DebuggerProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseSha type_proto->set_data_type(debugger::DT_NONE); } else if (type->isa()) { type_proto->set_data_type(debugger::DT_STRING); - } else if (type->isa()) { - // Do Nothing. } else if (type->isa()) { type_proto->set_data_type(debugger::DT_UMONAD); } else if (type->isa()) { type_proto->set_data_type(debugger::DT_IOMONAD); - } else { - MS_LOG(EXCEPTION) << "Unknown type: " << type->type_name(); } } @@ -325,14 +301,15 @@ std::string DebuggerProtoExporter::GetOpNodeInputId(const FuncGraphPtr &, const MS_LOG(EXCEPTION) << "Unknown node type. node is '" << node->ToString() << "'"; } -std::string DebuggerProtoExporter::GetFuncGraphProtoString(const FuncGraphPtr &func_graph) { +std::string DebuggerProtoExporter::GetFuncGraphProtoString(const FuncGraphPtr &func_graph, + LocDebugDumpMode dump_location) { if (func_graph == nullptr) { return ""; } InitModelInfo(); debugger::GraphProto *graph_proto = model_.mutable_graph(); - ExportFuncGraph(func_graph, graph_proto); + ExportFuncGraph(func_graph, graph_proto, dump_location); return model_.SerializeAsString(); } @@ -347,7 +324,8 @@ debugger::ModelProto DebuggerProtoExporter::GetFuncGraphProto(const FuncGraphPtr return model_; } -void DebuggerProtoExporter::ExportFuncGraph(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto) { +void DebuggerProtoExporter::ExportFuncGraph(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto, + LocDebugDumpMode dump_location) { if (func_graph == nullptr || graph_proto == nullptr) { return; } @@ -362,7 +340,7 @@ void DebuggerProtoExporter::ExportFuncGraph(const FuncGraphPtr &func_graph, debu ExportParameters(func_graph, graph_proto); - ExportCNodes(func_graph, graph_proto, &const_map); + ExportCNodes(func_graph, graph_proto, &const_map, dump_location); ExportValueNodes(const_map, graph_proto); } @@ -389,7 +367,7 @@ void DebuggerProtoExporter::ExportParameters(const FuncGraphPtr &func_graph, deb } void DebuggerProtoExporter::ExportCNodes(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto, - std::map *const_map_ptr) { + std::map *const_map_ptr, LocDebugDumpMode dump_location) { if (func_graph == nullptr || graph_proto == nullptr || const_map_ptr == nullptr) { return; } @@ -403,7 +381,7 @@ void DebuggerProtoExporter::ExportCNodes(const FuncGraphPtr &func_graph, debugge } auto cnode = node->cast(); if (cnode != func_graph->get_return()) { - ExportCNode(func_graph, cnode, &apply_map, const_map_ptr, graph_proto); + ExportCNode(func_graph, cnode, &apply_map, const_map_ptr, graph_proto, dump_location); } else { ExportFuncGraphOutput(func_graph, cnode, apply_map, const_map_ptr, graph_proto); } @@ -412,8 +390,8 @@ void DebuggerProtoExporter::ExportCNodes(const FuncGraphPtr &func_graph, debugge void DebuggerProtoExporter::ExportCNode(const FuncGraphPtr &func_graph, const CNodePtr &node, std::map *apply_map_ptr, - std::map *const_map_ptr, - debugger::GraphProto *graph_proto) { + std::map *const_map_ptr, debugger::GraphProto *graph_proto, + LocDebugDumpMode dump_location) { if (func_graph == nullptr || node == nullptr || apply_map_ptr == nullptr || const_map_ptr == nullptr || graph_proto == nullptr) { return; @@ -440,7 +418,14 @@ void DebuggerProtoExporter::ExportCNode(const FuncGraphPtr &func_graph, const CN // add full_name for debugger node_proto->set_full_name(node->fullname_with_scope()); MS_LOG(INFO) << "full_name: " << node->fullname_with_scope(); - + if (dump_location == kDebugWholeStack) { + std::ostringstream buffer; + auto traces = mindspore::trace::GetSourceLineList(node); + for (auto &trace : traces) { + buffer << " # " << trace; + } + node_proto->set_source_address(buffer.str()); + } // process OP inputs for (size_t i = 1; i < inputs.size(); ++i) { debugger::InputProto *input_proto = node_proto->add_input(); @@ -546,4 +531,63 @@ debugger::DataType GetDebuggerNumberDataType(const TypePtr &type) { } } +#ifdef ENABLE_DUMP_IR +void DumpIRProtoWithSrcInfo(const FuncGraphPtr &func_graph, const std::string &suffix, const std::string &target_dir, + LocDebugDumpMode dump_location) { + DebuggerProtoExporter exporter; + std::string graph_proto = exporter.GetFuncGraphProtoString(func_graph, dump_location); + if (func_graph == nullptr) { + MS_LOG(ERROR) << "Func graph is nullptr"; + return; + } + std::string file_path = target_dir + "/" + "ms_output_" + suffix + ".pb"; + bool status = Common::CreateNotExistDirs(target_dir); + if (!status) { + MS_LOG(ERROR) << "Failed at CreateNotExistDirs in dump_proto"; + return; + } + if (file_path.size() > PATH_MAX) { + MS_LOG(ERROR) << "File path " << file_path << " is too long."; + return; + } + char real_path[PATH_MAX] = {0}; + char *real_path_ret = nullptr; +#if defined(_WIN32) || defined(_WIN64) + real_path_ret = _fullpath(real_path, file_path.c_str(), PATH_MAX); +#else + real_path_ret = realpath(file_path.c_str(), real_path); +#endif + if (nullptr == real_path_ret) { + MS_LOG(DEBUG) << "dir " << file_path << " does not exit."; + } else { + std::string path_string = real_path; + if (chmod(common::SafeCStr(path_string), S_IRUSR | S_IWUSR) == -1) { + MS_LOG(ERROR) << "Modify file:" << real_path << " to rw fail."; + return; + } + } + + // write to pb file + std::ofstream ofs(real_path); + if (!ofs.is_open()) { + MS_LOG(ERROR) << "Open file '" << real_path << "' failed!"; + return; + } + ofs << graph_proto; + ofs.close(); + // set file mode to read only by user + ChangeFileMode(file_path, S_IRUSR); +} +#else +void DumpIRProtoWithSrcInfo(const FuncGraphPtr &, const std::string &, const std::string &) { + static bool already_printed = false; + if (already_printed) { + return; + } + already_printed = true; + MS_LOG(WARNING) << "The functionality of dumping function graph IR in protobuf format is disabled," + << "because ENABLE_DEBUGGER option is off" + << "please recompile source to enable it. See help of building script."; +} +#endif } // namespace mindspore diff --git a/mindspore/ccsrc/debug/debugger/proto_exporter.h b/mindspore/ccsrc/debug/debugger/proto_exporter.h new file mode 100644 index 0000000000..6a7c4e2679 --- /dev/null +++ b/mindspore/ccsrc/debug/debugger/proto_exporter.h @@ -0,0 +1,73 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MINDSPORE_PROTO_EXPORTER_H +#define MINDSPORE_PROTO_EXPORTER_H + +#include +#include +#include + +#include "debug/common.h" +#include "debug/data_dump/dump_json_parser.h" +#include "ir/graph_utils.h" +#include "proto/debug_graph.pb.h" +#include "utils/symbolic.h" +#include "utils/trace_base.h" + +namespace mindspore { +enum LocDebugDumpMode { kDebugOff = 0, kDebugTopStack = 1, kDebugWholeStack = 2 }; +class DebuggerProtoExporter { + public: + DebuggerProtoExporter() {} + ~DebuggerProtoExporter() {} + + std::string GetFuncGraphProtoString(const FuncGraphPtr &func_graph, LocDebugDumpMode dump_location = kDebugOff); + debugger::ModelProto GetFuncGraphProto(const FuncGraphPtr &func_graph); + + private: + void InitModelInfo(); + void GetOpNodeTypeAndAttrs(const FuncGraphPtr &func_graph, const AnfNodePtr &node, debugger::NodeProto *node_proto); + std::string GetOpNodeInputId(const FuncGraphPtr &func_graph, const AnfNodePtr &node, + const std::map &apply_map, + std::map *const_map_ptr); + void SetValueToProto(const ValuePtr &attr_value, debugger::ValueProto *value_proto); + void SetScalarToProto(const ScalarPtr &val, debugger::ValueProto *value_proto); + void SetSequenceToProto(const ValueSequeuePtr &val, debugger::ValueProto *value_proto); + void SetDictionaryToProto(const ValueDictionaryPtr &val, debugger::ValueProto *value_proto); + void SetNodeOutputType(const AnfNodePtr &node, debugger::TypeProto *type_proto); + void SetNodeOutputType(const TypePtr &node, const BaseShapePtr &shape, debugger::TypeProto *type_proto); + + void ExportFuncGraph(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto, + LocDebugDumpMode dump_location = kDebugOff); + void ExportParameters(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto); + void ExportCNodes(const FuncGraphPtr &func_graph, debugger::GraphProto *graph_proto, + std::map *const_map_ptr, LocDebugDumpMode dump_location = kDebugOff); + void ExportCNode(const FuncGraphPtr &func_graph, const CNodePtr &node, std::map *apply_map_ptr, + std::map *const_map_ptr, debugger::GraphProto *graph_proto, + LocDebugDumpMode dump_location = kDebugOff); + void ExportFuncGraphOutput(const FuncGraphPtr &func_graph, const CNodePtr &ret_node, + const std::map &apply_map, std::map *const_map_ptr, + debugger::GraphProto *graph_proto); + void ExportValueNodes(const std::map &const_map, debugger::GraphProto *graph_proto); + + static std::string GetConstNodeId(size_t idx) { return std::string("cst") + std::to_string(idx); } + + debugger::ModelProto model_; +}; +void DumpIRProtoWithSrcInfo(const FuncGraphPtr &func_graph, const std::string &suffix, const std::string &target_dir, + LocDebugDumpMode dump_location = kDebugOff); +} // namespace mindspore +#endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_MINDSPORE_PROTO_EXPORTER_H_ diff --git a/mindspore/ccsrc/debug/debugger/proto_exporter_stub.h b/mindspore/ccsrc/debug/debugger/proto_exporter_stub.h new file mode 100644 index 0000000000..21d4ac1a99 --- /dev/null +++ b/mindspore/ccsrc/debug/debugger/proto_exporter_stub.h @@ -0,0 +1,29 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MINDSPORE_PROTO_EXPORTER_STUB_H +#define MINDSPORE_PROTO_EXPORTER_STUB_H + +#include +#include "utils/symbolic.h" + +namespace mindspore { +enum LocDebugDumpMode { kDebugOff = 0, kDebugTopStack = 1, kDebugWholeStack = 2 }; +void DumpIRProtoWithSrcInfo(const FuncGraphPtr &func_graph, const std::string &suffix, const std::string &target_dir, + LocDebugDumpMode dump_location = kDebugOff) { + return; +} +} // namespace mindspore +#endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_MINDSPORE_PROTO_EXPORTER_STUB_H_ diff --git a/mindspore/ccsrc/debug/dump_proto.cc b/mindspore/ccsrc/debug/dump_proto.cc index afffcbece3..e410428928 100644 --- a/mindspore/ccsrc/debug/dump_proto.cc +++ b/mindspore/ccsrc/debug/dump_proto.cc @@ -22,6 +22,7 @@ #include #include +#include "debug/common.h" #include "proto/anf_ir.pb.h" #include "ir/graph_utils.h" #include "utils/ms_context.h" @@ -103,11 +104,26 @@ static irpb::DataType GetNumberDataType(const TypePtr &type) { } } +void CheckIfValidType(const TypePtr &type) { + if (type->isa()) { + MS_LOG(WARNING) << "The type: " << type->type_name(); + } + if (!(type->isa() || type->isa() || type->isa() || type->isa() || + type->isa() || type->isa() || type->isa() || type->isa() || + type->isa() || type->isa() || type->isa() || type->isa() || + type->isa() || type->isa() || type->isa() || + type->isa() || type->isa())) { + MS_LOG(EXCEPTION) << "Unknown type: " << type->type_name(); + } +} + void ProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &shape, irpb::TypeProto *type_proto) { if (type_proto == nullptr) { return; } - + if (type != nullptr) { + CheckIfValidType(type); + } if (type == nullptr) { type_proto->set_data_type(irpb::DT_UNDEFINED); } else if (type->isa()) { @@ -122,12 +138,6 @@ void ProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &s type_proto->mutable_tensor_type()->mutable_shape()->add_dim()->set_size(elem); } } - } else if (type->isa()) { - // Do Nothing - } else if (type->isa()) { - // Do Nothing - } else if (type->isa()) { - // Do Nothing } else if (type->isa()) { TuplePtr tuple_type = dyn_cast(type); type_proto->set_data_type(irpb::DT_TUPLE); @@ -154,14 +164,6 @@ void ProtoExporter::SetNodeOutputType(const TypePtr &type, const BaseShapePtr &s type_proto->set_data_type(irpb::DT_NONE); } else if (type->isa()) { type_proto->set_data_type(irpb::DT_STRING); - } else if (type->isa()) { - // Do Nothing. - } else if (type->isa()) { - // Do Nothing. - } else if (type->isa()) { - MS_LOG(WARNING) << "The type: " << type->type_name(); - } else { - MS_LOG(EXCEPTION) << "Unknown type: " << type->type_name(); } }