From ce0f500a3fe50c0c56ebd418f62abfb8f3de7df3 Mon Sep 17 00:00:00 2001 From: huanghui Date: Mon, 7 Mar 2022 10:32:00 +0800 Subject: [PATCH] convert draw_ to call Draw function derectly --- mindspore/ccsrc/debug/draw.cc | 8 --- mindspore/ccsrc/frontend/optimizer/opt.cc | 2 +- .../ccsrc/frontend/optimizer/optimizer.h | 2 +- .../parallel/graph_util/graph_splitter.cc | 3 +- mindspore/ccsrc/pipeline/jit/action.cc | 2 +- .../optimizer/ascend_backend_optimization.cc | 3 +- mindspore/core/ir/func_graph.cc | 2 - mindspore/core/ir/func_graph.h | 4 -- mindspore/core/ir/func_graph_extends.cc | 6 -- tests/ut/python/debug/test_analyze_fail.py | 58 +++++++++++++++++++ 10 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 tests/ut/python/debug/test_analyze_fail.py diff --git a/mindspore/ccsrc/debug/draw.cc b/mindspore/ccsrc/debug/draw.cc index 915412abe4a..eeef993ea35 100644 --- a/mindspore/ccsrc/debug/draw.cc +++ b/mindspore/ccsrc/debug/draw.cc @@ -656,13 +656,5 @@ void ModelDigraph::Edge(const AnfNodePtr &start, const AnfNodePtr &end, int idx, buffer_ << "[arrowhead=vee,"; buffer_ << "]" << std::endl; } - -struct DrawerRegister { - DrawerRegister() { - FuncGraph::set_drawer( - [](const std::string &filename, const FuncGraphPtr &func_graph) { Draw(filename, func_graph); }); - } - ~DrawerRegister() = default; -} drawer_regsiter; } // namespace draw } // namespace mindspore diff --git a/mindspore/ccsrc/frontend/optimizer/opt.cc b/mindspore/ccsrc/frontend/optimizer/opt.cc index fd17df6d8e9..1c006a0d836 100644 --- a/mindspore/ccsrc/frontend/optimizer/opt.cc +++ b/mindspore/ccsrc/frontend/optimizer/opt.cc @@ -308,7 +308,7 @@ bool SubstitutionList::ApplySubstitutionsToIR(const OptimizerPtr &optimizer, con DumpIR(fg_name + ".ir", func_graph); if (MsContext::GetInstance()->get_param(MS_CTX_EXECUTION_MODE) != kPynativeMode) { ExportIR(fg_name + ".dat", func_graph); - func_graph->DumpFuncGraph(fg_name); + draw::Draw(fg_name + ".dot", func_graph); } } #endif diff --git a/mindspore/ccsrc/frontend/optimizer/optimizer.h b/mindspore/ccsrc/frontend/optimizer/optimizer.h index c8ba9db2bc9..59ae7d41dff 100644 --- a/mindspore/ccsrc/frontend/optimizer/optimizer.h +++ b/mindspore/ccsrc/frontend/optimizer/optimizer.h @@ -209,7 +209,7 @@ class Optimizer : public std::enable_shared_from_this { DumpIR(fg_name + ".ir", func_graph); if (MsContext::GetInstance()->get_param(MS_CTX_EXECUTION_MODE) != kPynativeMode) { ExportIR(fg_name + ".dat", func_graph); - func_graph->DumpFuncGraph(fg_name); + draw::Draw(fg_name + ".dot", func_graph); } MS_LOG(DEBUG) << "Dump " << pass_names_[i] << " func graph."; } diff --git a/mindspore/ccsrc/frontend/parallel/graph_util/graph_splitter.cc b/mindspore/ccsrc/frontend/parallel/graph_util/graph_splitter.cc index d4e678608f7..c15faeb8ac4 100644 --- a/mindspore/ccsrc/frontend/parallel/graph_util/graph_splitter.cc +++ b/mindspore/ccsrc/frontend/parallel/graph_util/graph_splitter.cc @@ -26,6 +26,7 @@ #include "base/core_ops.h" #include "mindspore/core/utils/ms_context.h" #include "include/common/utils/anfalgo.h" +#include "debug/draw.h" namespace mindspore { namespace parallel { @@ -220,7 +221,7 @@ void GraphSplitter::DumpDistributedGraph(const InterProcessOpEdgesInfo &comm_edg func_graph_->manager()->SetEdge(user_node, user_node_index, recv_node); } MS_LOG(INFO) << "Cut graph without eliminating nodes."; - func_graph_->DumpFuncGraph("./single_node_graph.dot"); + draw::Draw("single_node_graph.dot", func_graph_); } OperatorLabel GraphSplitter::GetSplitLabel(const AnfNodePtr &node) { diff --git a/mindspore/ccsrc/pipeline/jit/action.cc b/mindspore/ccsrc/pipeline/jit/action.cc index 9b3b8b73752..3a8a60cf50b 100644 --- a/mindspore/ccsrc/pipeline/jit/action.cc +++ b/mindspore/ccsrc/pipeline/jit/action.cc @@ -621,7 +621,7 @@ bool OptimizeAction(const ResourcePtr &res, const std::vector &passes) MS_EXCEPTION_IF_NULL(func_graph); DumpIR(fg_name + ".ir", func_graph); ExportIR(fg_name + ".dat", func_graph); - func_graph->DumpFuncGraph(fg_name); + draw::Draw(fg_name + ".dot", func_graph); MS_LOG(DEBUG) << "Dump " << fg_name << " func graph."; } #endif diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc b/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc index 6dbb9842d5d..d825f4925bd 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc +++ b/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc @@ -164,6 +164,7 @@ #include "include/common/utils/context/graph_kernel_flags.h" #include "debug/anf_ir_dump.h" #include "debug/dump_proto.h" +#include "debug/draw.h" #ifdef ENABLE_DUMP_IR #include "debug/rdr/running_data_recorder.h" #endif @@ -491,7 +492,7 @@ void AscendBackendOptimization(const std::shared_ptr &kern std::string file_name = "hwopt_d_end_graph_" + std::to_string(kernel_graph->graph_id()) + ".ir"; DumpIR(file_name, kernel_graph, true, kWholeStack); DumpIRProto(kernel_graph, "after_hwopt_" + std::to_string(kernel_graph->graph_id())); - kernel_graph->DumpFuncGraph("hwopt_d_end"); + draw::Draw("hwopt_d_end.dot", kernel_graph); } #endif PROF_END(ascend_backend_optimization); diff --git a/mindspore/core/ir/func_graph.cc b/mindspore/core/ir/func_graph.cc index 9995bb8baef..08e9339bb0f 100644 --- a/mindspore/core/ir/func_graph.cc +++ b/mindspore/core/ir/func_graph.cc @@ -802,6 +802,4 @@ api::FuncGraphPtr api::FuncGraph::GetFuncGraphFromAnfNode(const AnfNodePtr &inpu auto fg = GetValueNode(input); return fg; } - -FuncGraph::Drawer FuncGraph::drawer_ = nullptr; } // namespace mindspore diff --git a/mindspore/core/ir/func_graph.h b/mindspore/core/ir/func_graph.h index f51cf88ade2..5be4d9c2e91 100644 --- a/mindspore/core/ir/func_graph.h +++ b/mindspore/core/ir/func_graph.h @@ -289,8 +289,6 @@ class MS_CORE_API FuncGraph : public deprecated::api::FuncGraph, public FuncGrap std::size_t hash() const override { return std::hash{}(this); } - void DumpFuncGraph(const std::string &path = "./func_graph.dot"); - bool operator==(const Value &other) const override { if (other.isa()) { return &other == this; @@ -342,7 +340,6 @@ class MS_CORE_API FuncGraph : public deprecated::api::FuncGraph, public FuncGrap bool stub() const { return stub_; } void set_stub(bool stub) { stub_ = stub; } - static void set_drawer(const Drawer &drawer) { drawer_ = drawer; } std::shared_ptr switch_input() const { return switch_input_; } void set_switch_input(const std::shared_ptr &switch_input) { switch_input_ = switch_input; } std::shared_ptr switch_layer_input() const { return switch_layer_input_; } @@ -437,7 +434,6 @@ class MS_CORE_API FuncGraph : public deprecated::api::FuncGraph, public FuncGrap // CNode order which relates to origin code order. OrderedSet order_; bool stub_; - static Drawer drawer_; // Design switch_input and switch_layer_input as a ptr to // share between derived backpropagator and cloned graphs. std::shared_ptr switch_input_; diff --git a/mindspore/core/ir/func_graph_extends.cc b/mindspore/core/ir/func_graph_extends.cc index 30d699840d8..3e70210a006 100644 --- a/mindspore/core/ir/func_graph_extends.cc +++ b/mindspore/core/ir/func_graph_extends.cc @@ -73,12 +73,6 @@ void FuncGraph::set_output(const AnfNodePtr &value, bool force_new_ret) { input0->set_abstract(f); } -void FuncGraph::DumpFuncGraph(const std::string &path) { - if (drawer_) { - drawer_(path + ".dot", shared_from_base()); - } -} - void FuncGraph::GenerateVarParams(const FuncGraphPtr &specialized_graph, int variable_args_count, int pos_args_input_count, std::vector *specialized_parameter_list, mindspore::HashMap *repl_nodes) const { diff --git a/tests/ut/python/debug/test_analyze_fail.py b/tests/ut/python/debug/test_analyze_fail.py new file mode 100644 index 00000000000..77c0317c163 --- /dev/null +++ b/tests/ut/python/debug/test_analyze_fail.py @@ -0,0 +1,58 @@ +# Copyright 2022 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. +# ============================================================================== +""" +Watchpoints test script for dump analyze_fail.dat when infer failed. +""" +# pylint: disable=too-many-function-args +import os +import pytest +import mindspore +from mindspore import ops, Tensor, nn +from tests.security_utils import security_off_wrap + + +@security_off_wrap +def test_infer_fail_generate_analyze_fail_dat(): + """ + Feature: test dump analyze_fail.dat. + Description: test dump analyze_fail.dat if infer failed. + Expectation: success. + """ + + class Net(nn.Cell): + def __init__(self): + super().__init__() + self.add = ops.Add() + self.sub = ops.Sub() + self.mul = ops.Mul() + self.div = ops.Div() + + def func(self, x, y): + return self.div(x, y) + + def construct(self, x, y): + a = self.sub(x, 1) + b = self.add(a, y) + c = self.mul(b, self.func(a, a, b)) + return c + + input1 = Tensor(3, mindspore.float32) + input2 = Tensor(2, mindspore.float32) + net = Net() + + with pytest.raises(TypeError) as excinfo: + net(input1, input2) + assert "rank_0/om/analyze_fail.dat" in str(excinfo.value) + assert os.path.exists("./rank_0/om/analyze_fail.dat") is True