!17520 Optimize exception info when compile net failed

From: @irmo
Reviewed-by: @ginfung,@zh_qh
Signed-off-by: @zh_qh
This commit is contained in:
mindspore-ci-bot 2021-06-05 12:04:28 +08:00 committed by Gitee
commit e5a189a4f0
9 changed files with 30 additions and 21 deletions

View File

@ -122,7 +122,7 @@ class AnalyzedFuncGraphExporter : public AnfExporter {
AnalyzedFuncGraphExporter() : AnfExporter(true, false) {}
~AnalyzedFuncGraphExporter() override = default;
void ExportFuncGraph(const std::string &filename, const std::vector<abstract::AnfNodeConfigPtr> &node_cfgs);
bool ExportFuncGraph(const std::string &filename, const std::vector<abstract::AnfNodeConfigPtr> &node_cfgs);
void ExportOneFuncGraph(std::ofstream &ofs, const FuncGraphPtr &func_graph);
void OutputCNodes(std::ofstream &ofs, const std::vector<AnfNodePtr> &nodes, const FuncGraphPtr &func_graph);
@ -156,9 +156,9 @@ std::unordered_map<FuncGraphPtr, TaggedNodeMap> CalcTaggedFuncGraphs() {
return tagged_func_graphs;
}
void OutputAnalyzedGraphWithType(const string &file_path) {
bool OutputAnalyzedGraphWithType(const string &file_path) {
AnalyzedFuncGraphExporter exporter;
exporter.ExportFuncGraph(file_path, GetCNodeDebugStack());
return exporter.ExportFuncGraph(file_path, GetCNodeDebugStack());
}
std::string AnalyzedFuncGraphExporter::GetNodeType(const AnfNodePtr &node) {
@ -420,11 +420,11 @@ void AnalyzedFuncGraphExporter::ExportOneFuncGraph(std::ofstream &ofs, const Fun
ofs << "}\n";
}
void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename,
bool 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;
return false;
}
context_map_.clear();
@ -433,7 +433,7 @@ void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename,
std::ofstream ofs(filename);
if (!ofs.is_open()) {
MS_LOG(ERROR) << "Open file '" << filename << "' failed!";
return;
return false;
}
param_index = 1;
@ -484,6 +484,7 @@ void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename,
ofs << "# num of total function graphs: " << context_map_.size() << "\n";
ofs.close();
return true;
}
void GetEvalStackInfo(std::ostringstream &oss) {
@ -505,8 +506,12 @@ void GetEvalStackInfo(std::ostringstream &oss) {
file_name = realpath.value();
}
OutputAnalyzedGraphWithType(file_name);
oss << "\nThe function call stack (See file '" << file_name << "' for more details):\n";
auto ret = OutputAnalyzedGraphWithType(file_name);
oss << "\nThe function call stack";
if (ret) {
oss << " (See file '" << file_name << "' for more details)";
}
oss << ":\n";
int index = 0;
std::string last_location_info = "";

View File

@ -101,7 +101,7 @@ AnfNodePtr FunctionBlock::ReadVariable(const std::string &var) {
}
return node;
}
// Get var from predecessor block ,if can't get the make a resolve node to it
// Get var from predecessor block ,if can't get then make a resolve node to it
if (matured_) {
// If only one predecessor block, read the definition of var from it.
if (prev_blocks_.size() == 1) {
@ -317,7 +317,7 @@ void FunctionBlock::Mature() {
matured_ = true;
}
// Force the conditIon node to bool using bool operation
// Force the condition node to bool using bool operation
CNodePtr FunctionBlock::ForceToBoolNode(const AnfNodePtr &cond) {
TraceGuard trace_guard(std::make_shared<TraceForceBool>(cond->debug_info()));
CNodePtr op_apply_node = func_graph()->NewCNodeInOrder({MakeResolveOperation(NAMED_PRIMITIVE_BOOL), cond});

View File

@ -1863,9 +1863,9 @@ FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr) {
MS_LOG(EXCEPTION) << "Current graph cast failed from " << cell_ptr->ToString();
}
TraceGuard guard(current_graph->debug_info()->location());
auto func_graph = std::make_shared<FuncGraph>();
func_graph->debug_info()->set_name(current_graph->debug_info()->name() + "_wrapper");
func_graph->debug_info()->set_location(current_graph->debug_info()->location());
// Copy all parameters information
for (auto &para : current_graph->parameters()) {
@ -1874,6 +1874,7 @@ FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr) {
auto name = orig_param->name();
param->set_name(name);
param->debug_info()->set_name(name);
param->debug_info()->set_location(param->debug_info()->location());
}
func_graph->set_has_vararg(current_graph->has_vararg());
func_graph->set_has_kwarg(current_graph->has_kwarg());
@ -1892,6 +1893,7 @@ FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr) {
}
auto unpacking = func_graph->has_vararg() || func_graph->has_kwarg();
TraceGuard guard(current_graph->get_return()->debug_info()->location());
if (!unpacking) {
std::vector<AnfNodePtr> inputs;
inputs.emplace_back(NewValueNode(cell_ptr));

View File

@ -296,7 +296,6 @@ AnfNodePtr ResolveSymbol(const FuncGraphManagerPtr &manager, const NameSpacePtr
symbol_resolver.Resolve();
py::object obj = symbol_resolver.result();
AnfNodePtr resolved_node = ResolveObjectAndAddToManager(manager, obj, node);
TraceManager::ClearParseOrResolveDebugInfo();
return resolved_node;
}

View File

@ -267,8 +267,7 @@ AbstractBasePtr AnalysisEngine::GetCNodeOperatorAbstract(const CNodePtr &cnode,
auto maybe_func_eval_result = func_conf->ObtainEvalResult();
AbstractBasePtr maybe_func = maybe_func_eval_result->abstract();
if (maybe_func == nullptr) {
MS_LOG(EXCEPTION) << "No abstract, func_conf: " << func_conf->ToString()
<< " NodeInfo: " << trace::GetDebugInfo(cnode->debug_info());
MS_LOG(EXCEPTION) << "No abstract, func_conf: " << func_conf->ToString();
}
return maybe_func;
}
@ -283,7 +282,8 @@ EvalResultPtr AnalysisEngine::EvalCNode(const CNodePtr &cnode, const AnfNodeConf
}
AbstractFunctionPtr func = dyn_cast<AbstractFunction>(maybe_func);
if (func == nullptr) {
MS_LOG(EXCEPTION) << "Not AbstractFunction: " << maybe_func->ToString() << ".";
MS_LOG(ERROR) << "Can not cast to a AbstractFunction: " << maybe_func->ToString() << ".";
MS_EXCEPTION(ValueError) << "This may be not defined, and it can't be a operator. Please check code.";
}
ConfigPtrList args_conf_list;

View File

@ -93,7 +93,10 @@ class TraceGuard {
TraceGuard(const DebugInfoPtr &debug_info, const TraceInfoPtr &trace_info) {
TraceManager::DebugTrace(debug_info, trace_info);
}
~TraceGuard() { TraceManager::EndTrace(); }
~TraceGuard() {
TraceManager::EndTrace();
TraceManager::ClearParseOrResolveDebugInfo();
}
};
class TraceContext {

View File

@ -344,11 +344,11 @@ class Cell(Cell_):
default_args += 1
if len(inputs) < positional_args:
raise ValueError(
raise TypeError(
f"The function construct need {positional_args} positional argument, but only provided {len(inputs)}.")
if len(inputs) > positional_args + default_args:
raise ValueError(
raise TypeError(
f"The function construct need {positional_args} positional argument and {default_args} default "
f"argument, but provided {len(inputs)}")

View File

@ -87,7 +87,7 @@ def test_l1_regularizer07():
try:
l1_regularizer = Net_l1_regularizer(scale)
l1_regularizer()
except ValueError:
except TypeError:
assert True

View File

@ -292,9 +292,9 @@ def test_call_none_in_if():
return ret
net = Net()
with pytest.raises(RuntimeError) as err:
with pytest.raises(ValueError) as err:
net(Tensor([1, 2, 3], mstype.float32))
assert "Not AbstractFunction: AbstractNone(Value: None)" in str(err.value)
assert "not defined" in str(err.value)
assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(291)" in \
str(err.value)
assert "ret = self.func(x)" in str(err.value)