|
|
|
@ -17,6 +17,7 @@
|
|
|
|
|
#include "debug/trace.h"
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include <vector>
|
|
|
|
@ -194,37 +195,116 @@ void TraceGraphInfer() {
|
|
|
|
|
MS_LOG(INFO) << "\n*************************************************************************************";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OutputAnalysisGraphInfo() {
|
|
|
|
|
MS_LOG(INFO) << "Output analysis graph begin";
|
|
|
|
|
std::unordered_map<FuncGraphPtr, size_t> index_map;
|
|
|
|
|
std::vector<TaggedGraph> tagged_graphs;
|
|
|
|
|
class AnalyzedFuncGraphExporter : public AnfExporter {
|
|
|
|
|
public:
|
|
|
|
|
AnalyzedFuncGraphExporter() : AnfExporter("", true, false) {}
|
|
|
|
|
~AnalyzedFuncGraphExporter() override = default;
|
|
|
|
|
|
|
|
|
|
void ExportFuncGraph(const std::string& filename, const std::vector<abstract::AnfNodeConfigPtr>& node_cfgs);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string GetNodeType(const AnfNodePtr& nd) override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::unordered_map<FuncGraphPtr, TaggedNodeMap> CalcTaggedFuncGraphs() {
|
|
|
|
|
std::unordered_map<FuncGraphPtr, TaggedNodeMap> tagged_func_graphs;
|
|
|
|
|
auto& list = GetCNodeDebugStack();
|
|
|
|
|
for (size_t i = 0; i < list.size(); ++i) {
|
|
|
|
|
auto& node_cfg = list[i];
|
|
|
|
|
auto node_cfg = list[i];
|
|
|
|
|
auto fg = node_cfg->context()->func_graph();
|
|
|
|
|
auto node = node_cfg->node();
|
|
|
|
|
auto idx = tagged_graphs.size();
|
|
|
|
|
std::pair<FuncGraphPtr, size_t> item(fg, idx);
|
|
|
|
|
if (index_map.insert(item).second) {
|
|
|
|
|
tagged_graphs.emplace_back(TaggedGraph(fg, TaggedNodeMap()));
|
|
|
|
|
tagged_func_graphs[fg][node] = i;
|
|
|
|
|
}
|
|
|
|
|
return tagged_func_graphs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OutputAnalyzedGraphWithType() {
|
|
|
|
|
AnalyzedFuncGraphExporter exporter;
|
|
|
|
|
exporter.ExportFuncGraph("analyze_fail.dat", GetCNodeDebugStack());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string AnalyzedFuncGraphExporter::GetNodeType(const AnfNodePtr& node) {
|
|
|
|
|
if (node_cfg_ == nullptr) {
|
|
|
|
|
return AnfExporter::GetNodeType(node);
|
|
|
|
|
}
|
|
|
|
|
auto ctx = node_cfg_->context();
|
|
|
|
|
auto engine = node_cfg_->engine();
|
|
|
|
|
auto cfg = engine->MakeConfig(node, ctx);
|
|
|
|
|
auto abs = engine->cache().GetValue(cfg);
|
|
|
|
|
|
|
|
|
|
if (abs == nullptr) {
|
|
|
|
|
return "Undefined";
|
|
|
|
|
}
|
|
|
|
|
auto dtype = abs->BuildType();
|
|
|
|
|
auto shape = abs->BuildShape();
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
if (dtype != nullptr && abs->isa<abstract::AbstractTensor>() && shape != nullptr) {
|
|
|
|
|
oss << dtype->DumpText() << shape->DumpText();
|
|
|
|
|
} else if (dtype != nullptr) {
|
|
|
|
|
oss << dtype->DumpText();
|
|
|
|
|
} else {
|
|
|
|
|
oss << "Undefined";
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string& filename,
|
|
|
|
|
const std::vector<abstract::AnfNodeConfigPtr>& node_cfgs) {
|
|
|
|
|
if (node_cfgs.empty()) {
|
|
|
|
|
MS_LOG(DEBUG) << "Node configs is empty";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ofstream ofs(filename);
|
|
|
|
|
if (!ofs.is_open()) {
|
|
|
|
|
MS_LOG(ERROR) << "Open file '" << filename << "' failed!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
param_index = 1;
|
|
|
|
|
auto tagged_func_graphs = CalcTaggedFuncGraphs();
|
|
|
|
|
|
|
|
|
|
// first output grapn on the analysis stack
|
|
|
|
|
for (const auto& node_cfg : node_cfgs) {
|
|
|
|
|
auto fg = node_cfg->context()->func_graph();
|
|
|
|
|
// the graph is already output, skip it
|
|
|
|
|
if (exported.find(fg) != exported.end()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
tagged_graphs[index_map[fg]].second[node] = i;
|
|
|
|
|
// set node_cfg info for getting type
|
|
|
|
|
node_cfg_ = node_cfg;
|
|
|
|
|
tagged_cnodes_ = tagged_func_graphs[fg];
|
|
|
|
|
ExportOneFuncGraph(ofs, fg);
|
|
|
|
|
ofs << "\n\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node_cfg_ = nullptr;
|
|
|
|
|
tagged_cnodes_.clear();
|
|
|
|
|
|
|
|
|
|
// print seperator between function graphs on analyzed graph call stack and others
|
|
|
|
|
ofs << "#===============================================================================\n\n\n";
|
|
|
|
|
|
|
|
|
|
// second output other graphs
|
|
|
|
|
while (!func_graph_set.empty()) {
|
|
|
|
|
FuncGraphPtr fg = *func_graph_set.begin();
|
|
|
|
|
ExportOneFuncGraph(ofs, fg);
|
|
|
|
|
ofs << "\n\n";
|
|
|
|
|
(void)func_graph_set.erase(fg);
|
|
|
|
|
}
|
|
|
|
|
ofs << "# num of total funcgraphs: " << exported.size();
|
|
|
|
|
|
|
|
|
|
ExportIR("analyze_fail.dat", tagged_graphs);
|
|
|
|
|
MS_LOG(INFO) << "Output analysis graph *end*";
|
|
|
|
|
ofs.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GetInferStackInfo(std::ostringstream& oss) {
|
|
|
|
|
MS_LOG(INFO) << "Get graph analysis information begin";
|
|
|
|
|
auto& stack = GetCNodeDebugStack();
|
|
|
|
|
auto stack = GetCNodeDebugStack();
|
|
|
|
|
if (stack.empty()) {
|
|
|
|
|
MS_LOG(INFO) << "Length of analysis information stack is empty.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OutputAnalysisGraphInfo();
|
|
|
|
|
OutputAnalyzedGraphWithType();
|
|
|
|
|
oss << "\nThe function call stack:\n";
|
|
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|