• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_exec_order_recorder.h"
17 #include <fstream>
18 #include <utility>
19 #include "mindspore/core/ir/anf.h"
20 #include "mindspore/core/utils/log_adapter.h"
21 #include "include/backend/anf_runtime_algorithm.h"
22 #include "include/common/debug/rdr/recorder_manager.h"
23 #include "mindspore/core/utils/file_utils.h"
24 
25 namespace mindspore {
26 namespace {
DumpGraphExeOrder(const std::string & filename,const std::vector<CNodePtr> & execution_order)27 bool DumpGraphExeOrder(const std::string &filename, const std::vector<CNodePtr> &execution_order) {
28   ChangeFileMode(filename, S_IRWXU);
29   std::ofstream fout(filename, std::ofstream::app);
30   if (!fout.is_open()) {
31     MS_LOG(WARNING) << "Open file for saving graph exec order failed.";
32     return false;
33   }
34   fout << "================== execution order ==================\n";
35   fout << "execution_order size: " << execution_order.size() << "\n";
36   int i = 0;
37   for (auto &cnode : execution_order) {
38     MS_EXCEPTION_IF_NULL(cnode);
39     fout << i << ":\n";
40     fout << "\t" << cnode->DebugString() << "\n";
41     fout << "\t" << AnfAlgo::GetStreamDistinctionLabel(cnode.get()) << "\n";
42     fout << "\t" << AnfAlgo::GetGraphId(cnode.get()) << "\n";
43     i++;
44   }
45   fout << "================== execution order ==================\n";
46   fout.close();
47   ChangeFileMode(filename, S_IRUSR);
48   return true;
49 }
50 }  // namespace
51 
Export()52 void GraphExecOrderRecorder::Export() {
53   auto realpath = GetFileRealPath();
54   if (!realpath.has_value()) {
55     return;
56   }
57   std::string real_file_path = realpath.value() + ".txt";
58   DumpGraphExeOrder(real_file_path, exec_order_);
59 }
60 
61 namespace RDR {
RecordGraphExecOrder(const SubModuleId module,const std::string & name,const std::vector<CNodePtr> & final_exec_order)62 bool RecordGraphExecOrder(const SubModuleId module, const std::string &name,
63                           const std::vector<CNodePtr> &final_exec_order) {
64   auto ms_context = MsContext::GetInstance();
65   MS_EXCEPTION_IF_NULL(ms_context);
66   if (!mindspore::RecorderManager::Instance().RdrEnable() ||
67       ms_context->get_param<int>(MS_CTX_EXECUTION_MODE) == kPynativeMode) {
68     return false;
69   }
70   std::string submodule_name = std::string(GetSubModuleName(module));
71   GraphExecOrderRecorderPtr graph_exec_order_recorder =
72     std::make_shared<GraphExecOrderRecorder>(submodule_name, name, final_exec_order);
73   bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(graph_exec_order_recorder));
74   return ans;
75 }
76 }  // namespace RDR
77 }  // namespace mindspore
78