From 2368955eebcf1553f13a2f365de7bcc9f3379ad0 Mon Sep 17 00:00:00 2001 From: Bairong Date: Fri, 11 Dec 2020 22:12:13 +0800 Subject: [PATCH] improve dumpIR --- mindspore/ccsrc/debug/anf_ir_dump.cc | 17 ++++++++++++----- mindspore/ccsrc/debug/anf_ir_dump.h | 3 ++- mindspore/ccsrc/pipeline/jit/pipeline.cc | 8 ++++++-- mindspore/core/utils/info.cc | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/mindspore/ccsrc/debug/anf_ir_dump.cc b/mindspore/ccsrc/debug/anf_ir_dump.cc index bd38fb3e313..3a749fc3912 100644 --- a/mindspore/ccsrc/debug/anf_ir_dump.cc +++ b/mindspore/ccsrc/debug/anf_ir_dump.cc @@ -30,6 +30,7 @@ #include "pipeline/jit/base.h" #include "debug/common.h" #include "debug/trace.h" +#include "utils/trace_base.h" namespace mindspore { const std::string ToShortString(const TypeId &typeId) { @@ -344,7 +345,8 @@ void DumpShape(const AnfNodePtr &nd, const FuncGraphPtr &sub_graph, const std::s } void DumpCNode(const CNodePtr &nd, const FuncGraphPtr &sub_graph, OrderedMap *const para_map, - const std::shared_ptr &gsub, bool dump_full_name = false, bool dump_location = false) { + const std::shared_ptr &gsub, bool dump_full_name = false, + LocDumpMode dump_location = kOff) { if (nd == nullptr || sub_graph == nullptr || para_map == nullptr || gsub == nullptr) { return; } @@ -383,19 +385,24 @@ void DumpCNode(const CNodePtr &nd, const FuncGraphPtr &sub_graph, OrderedMapbuffer << " : (" << nd->fullname_with_scope() << ")" << std::endl; } - if (dump_location) { + if (dump_location == kTopStack) { if (label_manage::GetGlobalTraceLabelType() == label_manage::TraceLabelType::kWithUniqueId) { gsub->buffer << trace::GetDebugInfo(nd->debug_info(), " # ", kSourceLineTipDiscard) << "#" << label_manage::Label(nd->debug_info()) << "\n"; } else { gsub->buffer << trace::GetDebugInfo(nd->debug_info(), " # ", kSourceLineTipDiscard) << "\n"; } + } else if (dump_location == kWholeStack) { + auto traces = mindspore::trace::GetSourceLineList(nd); + for (auto &trace : traces) { + gsub->buffer << " # " << trace; + } } } void DumpIRInSubgraph(const std::vector &nodes, OrderedMap *para_map, OrderedMap> *const sub_graphs, - bool dump_full_name = false, bool dump_location = false) { + bool dump_full_name = false, LocDumpMode dump_location = kOff) { if (para_map == nullptr || sub_graphs == nullptr) { return; } @@ -489,7 +496,7 @@ std::string AddGlobalId(const std::string &filename) { } #ifdef ENABLE_DUMP_IR -void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name, bool dump_location) { +void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_full_name, LocDumpMode dump_location) { if (graph == nullptr) { return; } @@ -529,7 +536,7 @@ void DumpIR(const std::string &filename, const FuncGraphPtr &graph, bool dump_fu ChangeFileMode(realpath.value(), S_IRUSR); } #else -void DumpIR(const std::string &, const FuncGraphPtr &, bool, bool) { +void DumpIR(const std::string &, const FuncGraphPtr &, bool, LocDumpMode) { 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 5d225859205..df6eb37723d 100644 --- a/mindspore/ccsrc/debug/anf_ir_dump.h +++ b/mindspore/ccsrc/debug/anf_ir_dump.h @@ -22,9 +22,10 @@ #include "ir/anf.h" 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, - bool dump_location = false); + LocDumpMode dump_location = kOff); void PrintInputAndOutputInferType(std::ostringstream &buffer, const AnfNodePtr &nd); const std::string ToShortString(const TypeId &typeId); } // namespace mindspore diff --git a/mindspore/ccsrc/pipeline/jit/pipeline.cc b/mindspore/ccsrc/pipeline/jit/pipeline.cc index bb2cc4910f5..08c465417b3 100644 --- a/mindspore/ccsrc/pipeline/jit/pipeline.cc +++ b/mindspore/ccsrc/pipeline/jit/pipeline.cc @@ -644,7 +644,7 @@ void Pipeline::Run() { FuncGraphPtr user_graph = nullptr; WITH(MsProfile::GetProfile())[&user_graph, this]() { - int64_t i = 0; + size_t i = 0; for (auto &action : actions_) { #ifdef ENABLE_TIMELINE DumpTime &dump_time = DumpTime::GetInstance(); @@ -687,7 +687,11 @@ void Pipeline::Run() { // generate IR file in dot format, which can be converted to svg file using graphviz dot command draw::Draw(base_name + ".dot", graph); // generate IR file in human readable format - DumpIR(base_name + ".ir", graph); + if (i == actions_.size() - 1) { + DumpIR(base_name + ".ir", graph, false, kWholeStack); + } else { + DumpIR(base_name + ".ir", graph, false, kTopStack); + } // generate IR file in a heavily commented format, which can also be reloaded ExportIR(base_name + ".dat", std::to_string(i), graph); } diff --git a/mindspore/core/utils/info.cc b/mindspore/core/utils/info.cc index 6ab3afb0127..f42d8020d71 100644 --- a/mindspore/core/utils/info.cc +++ b/mindspore/core/utils/info.cc @@ -47,7 +47,7 @@ std::string HighLightLine(const std::string &line, int col_begin, int col_end, S // print the file name, line no and column no, and part of the content std::string Location::ToString(SourceLineTip tip) { std::stringstream debug_info_ss; - debug_info_ss << " In file " << file_name_ << "(" << line_ << ")" << std::endl; + debug_info_ss << "In file " << file_name_ << "(" << line_ << ")" << std::endl; if (line_ <= 0) { return debug_info_ss.str(); }