Add debug info for nodes added by func_graph_builder
This commit is contained in:
parent
01897887e3
commit
1e5ddaa27d
|
@ -495,4 +495,12 @@ bool FuncGraphBuilder::CanConstantFoldFunc(const py::object &obj) {
|
|||
py::object can_constant_fold = python_adapter::CallPyModFn(mod, parse::PYTHON_MOD_CAN_CONSTANT_FOLD, obj);
|
||||
return can_constant_fold.cast<bool>();
|
||||
}
|
||||
|
||||
void FuncGraphBuilder::SetGraphName(const std::string &name) {
|
||||
if (name.empty()) {
|
||||
return;
|
||||
}
|
||||
MS_EXCEPTION_IF_NULL(graph_->debug_info());
|
||||
graph_->debug_info()->set_name(name);
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -106,6 +106,11 @@ class FuncGraphBuilder {
|
|||
/// \return The graph constructed.
|
||||
FuncGraphPtr graph();
|
||||
|
||||
/// \brief Set the name of the func_graph.
|
||||
///
|
||||
/// \param[in] name The func_graph name to set.
|
||||
void SetGraphName(const std::string &name);
|
||||
|
||||
static ValuePtr ConvertPyObjToValue(const py::object &obj);
|
||||
|
||||
static AbstractBasePtr EvalValue(const ValuePtr &value, const AbstractBasePtrList &inputs_abs_list);
|
||||
|
|
|
@ -2047,6 +2047,24 @@ bool GraphBuilder::ReplaceCall(CallNode *call_node, const py::object &old_func)
|
|||
return true;
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::string GetFuncGraphName(const py::object &func, const GraphBuilderPtr &subgraph) {
|
||||
auto func_str = py::cast<std::string>(py::str(func));
|
||||
std::vector<std::string> vec;
|
||||
std::istringstream iss(func_str);
|
||||
std::string str;
|
||||
while (iss >> str) {
|
||||
(void)vec.emplace_back(str);
|
||||
}
|
||||
if (vec.size() <= 1) {
|
||||
return "";
|
||||
}
|
||||
auto func_name = vec[1];
|
||||
std::replace(func_name.begin(), func_name.end(), '.', '_');
|
||||
return func_name + "_" + std::to_string(subgraph->GetGraph()->GetCodeObj()->co_firstlineno);
|
||||
}
|
||||
}
|
||||
|
||||
StopTraceReason MindGraphBuilder::BuildSubGraph(CallNode *call_node, int depth, const py::object &func,
|
||||
const GraphBuilderPtr &subgraph) {
|
||||
InlineReason stat = InlineReason::kInline;
|
||||
|
@ -2074,11 +2092,13 @@ StopTraceReason MindGraphBuilder::BuildSubGraph(CallNode *call_node, int depth,
|
|||
if (CheckConstPyObject(sub_ret->GetVobj()->GetPyObject().ptr())) {
|
||||
call_node->SetVobj(sub_ret->GetVobj());
|
||||
} else {
|
||||
sg->FGBuilder()->SetGraphName(GetFuncGraphName(func, subgraph));
|
||||
sg->FGAddOutput();
|
||||
if (sg->FGBuilder()->graph() == nullptr) {
|
||||
MS_LOG(ERROR) << "subgraph trace null";
|
||||
return StopTraceReason::kTrace_Fail;
|
||||
} else {
|
||||
TraceGuard trace_guard(GetLocation(call_node));
|
||||
auto res = FGBuilder()->AddNode(sg->FGBuilder()->graph(), args);
|
||||
if (res.ptr()) {
|
||||
MS_LOG(INFO) << "add fg node suc: ";
|
||||
|
@ -2545,6 +2565,7 @@ void MindGraphBuilder::FGAddOutput() {
|
|||
py::object MindGraphBuilder::FGAddNode(CallNode *call_node, const py::object &callable_info,
|
||||
const std::vector<py::object> &args, StopTraceReason *stop_reason) {
|
||||
MS_LOG(INFO) << "try add node: " << py::str(callable_info);
|
||||
TraceGuard trace_guard(GetLocation(call_node));
|
||||
auto res = FGBuilder()->AddNode(callable_info, args);
|
||||
if (res.ptr() == nullptr) {
|
||||
MS_LOG(ERROR) << "add node fail";
|
||||
|
@ -3476,5 +3497,12 @@ bool MindGraphBuilder::HandleFuncInWhiteList(const std::string &key, CallNode *n
|
|||
MS_LOG(INFO) << "specialize for " << key;
|
||||
return GetFuncWhiteListMap(true).find(key)->second.infer(n);
|
||||
}
|
||||
|
||||
LocationPtr MindGraphBuilder::GetLocation(CallNode *call_node) const {
|
||||
auto file_name = py::cast<std::string>(graph_->GetCodeObj()->co_filename);
|
||||
auto line_no = call_node->GetLineNo();
|
||||
std::vector<std::string> comments;
|
||||
return std::make_shared<Location>(file_name, line_no, 0, line_no, 0, "", std::move(comments));
|
||||
}
|
||||
} // namespace pijit
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -295,10 +295,23 @@ class GraphBuilder {
|
|||
|
||||
class MindGraphBuilder : public GraphBuilder {
|
||||
public:
|
||||
explicit MindGraphBuilder(const PyFrameObject *f)
|
||||
: GraphBuilder(f), fg_builder_(std::make_shared<FuncGraphBuilder>()) {}
|
||||
explicit MindGraphBuilder(const PyFrameObject *f) : GraphBuilder(f) {
|
||||
std::vector<std::string> comments;
|
||||
auto location = std::make_shared<Location>(py::cast<std::string>(f->f_code->co_filename), f->f_code->co_firstlineno,
|
||||
0, f->f_code->co_firstlineno, 0, "", std::move(comments));
|
||||
TraceGuard trace_guard(location);
|
||||
fg_builder_ = std::make_shared<FuncGraphBuilder>();
|
||||
fg_builder_->SetGraphName(py::cast<std::string>(f->f_code->co_name) + "_" +
|
||||
std::to_string(f->f_code->co_firstlineno));
|
||||
}
|
||||
MindGraphBuilder(GraphBuilder *r, GraphBuilder *p, PyCodeObject *co, PyObject *globals)
|
||||
: GraphBuilder(r, p, co, globals), fg_builder_(std::make_shared<FuncGraphBuilder>()) {}
|
||||
: GraphBuilder(r, p, co, globals) {
|
||||
std::vector<std::string> comments;
|
||||
auto location = std::make_shared<Location>(py::cast<std::string>(co->co_filename), co->co_firstlineno, 0,
|
||||
co->co_firstlineno, 0, "", std::move(comments));
|
||||
TraceGuard trace_guard(location);
|
||||
fg_builder_ = std::make_shared<FuncGraphBuilder>();
|
||||
}
|
||||
bool trace_flag() { return true; }
|
||||
mindspore::FuncGraphBuilderPtr FGBuilder() const { return fg_builder_; }
|
||||
StopTraceReason TraceRun(const std::vector<py::object> &args);
|
||||
|
@ -310,6 +323,8 @@ class MindGraphBuilder : public GraphBuilder {
|
|||
py::object ResolveCallable(CallNode *call_node, StopTraceReason *stop_reason) override;
|
||||
bool WhiteListFuncCheckAndInfer(CallNode *, const py::object &f) override;
|
||||
|
||||
LocationPtr GetLocation(CallNode *call_node) const;
|
||||
|
||||
protected:
|
||||
bool DoGetItem(const Instr &instr) override;
|
||||
bool DoUnary(const Instr &instr) override;
|
||||
|
|
Loading…
Reference in New Issue