1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "debug/rdr/graph_recorder.h"
17 #include <fstream>
18 #include <utility>
19 #include <optional>
20
21 #include "mindspore/core/base/base.h"
22 #include "mindspore/core/utils/log_adapter.h"
23 #include "include/common/debug/anf_ir_dump.h"
24 #include "include/common/debug/anf_dump_utils.h"
25 #include "include/common/debug/dump_proto.h"
26 #include "include/common/debug/rdr/recorder_manager.h"
27 #include "mindspore/core/utils/file_utils.h"
28
29 namespace mindspore {
30 namespace protobuf {
31 #ifdef ENABLE_DUMP_IR
DumpIRProto(const std::string & real_path,const FuncGraphPtr & func_graph)32 void DumpIRProto(const std::string &real_path, const FuncGraphPtr &func_graph) {
33 if (func_graph == nullptr) {
34 MS_LOG(ERROR) << "Func graph is nullptr.";
35 return;
36 }
37
38 // write to pb file
39 std::ofstream ofs(real_path);
40 if (!ofs.is_open()) {
41 MS_LOG(ERROR) << "Open file '" << real_path << "' failed!";
42 return;
43 }
44 ofs << GetFuncGraphProtoString(func_graph);
45 ofs.close();
46 // set file mode to read only by user
47 ChangeFileMode(real_path, S_IRUSR);
48 }
49 #else
50 void DumpIRProto(const std::string &, const FuncGraphPtr &) {
51 static bool is_printed = false;
52 if (!is_printed) {
53 is_printed = true;
54 MS_LOG(WARNING) << "The functionality of dumping function graph IR in protobuf format is disabled, "
55 << "please recompile source to enable it. See help of building script.";
56 }
57 }
58 #endif
59 } // namespace protobuf
60
Export()61 void GraphRecorder::Export() {
62 bool save_flag = false;
63 auto tmp_realpath = GetFileRealPath();
64 if (!tmp_realpath.has_value()) {
65 return;
66 }
67 std::string realpath = tmp_realpath.value();
68 if (graph_type_.find(".dat") != std::string::npos) {
69 save_flag = true;
70 AnfDumpHandler::DumpDat(realpath, func_graph_);
71 }
72 if (graph_type_.find(".ir") != std::string::npos) {
73 save_flag = true;
74 std::string realpath_ir = realpath + ".ir";
75 if (dump_graph_info_.dump_mode <= static_cast<int>(kWholeStack) &&
76 dump_graph_info_.dump_mode >= static_cast<int>(kOff)) {
77 LocDumpMode dump_mode = LocDumpMode(dump_graph_info_.dump_mode);
78 DumpIRForRDR(realpath_ir, func_graph_, dump_graph_info_.dump_full_name, dump_mode);
79 } else {
80 MS_LOG(WARNING) << "Unknown save graph LocDumpMode: " << dump_graph_info_.dump_mode
81 << ", it must be in the range [0,2].";
82 }
83 }
84 if (graph_type_.find(".pb") != std::string::npos) {
85 save_flag = true;
86 protobuf::DumpIRProto(realpath + ".pb", func_graph_); // save *.pb file
87 }
88 if (!save_flag) {
89 MS_LOG(WARNING) << "Unknown save graph type: " << graph_type_;
90 }
91 }
92
93 namespace RDR {
RecordAnfGraph(const SubModuleId module,const std::string & name,const FuncGraphPtr & graph,const DumpGraphParams & info,const std::string & file_type)94 bool RecordAnfGraph(const SubModuleId module, const std::string &name, const FuncGraphPtr &graph,
95 const DumpGraphParams &info, const std::string &file_type) {
96 auto ms_context = MsContext::GetInstance();
97 MS_EXCEPTION_IF_NULL(ms_context);
98 if (!mindspore::RecorderManager::Instance().RdrEnable() ||
99 ms_context->get_param<int>(MS_CTX_EXECUTION_MODE) == kPynativeMode) {
100 return false;
101 }
102 std::string submodule_name = std::string(GetSubModuleName(module));
103 GraphRecorderPtr graph_recorder = std::make_shared<GraphRecorder>(submodule_name, name, graph, file_type);
104 graph_recorder->SetDumpFlag(info);
105 bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(graph_recorder));
106 return ans;
107 }
108 } // namespace RDR
109 } // namespace mindspore
110