• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 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 
17 #include "src/common/draw/drawer.h"
18 #ifdef ENABLE_DRAW
19 #include <iomanip>
20 #include <sstream>
21 #include <cstdlib>
22 #include "src/common/file_utils.h"
23 #include "src/common/draw/adapter_graphs/sub_graph_kernel_adapter_graph.h"
24 #include "src/common/draw/adapter_graphs/compile_result_adapter_graph.h"
25 #endif
26 
27 namespace mindspore::lite {
28 constexpr char kDefaultDrawDIR[] = "./graphs";
29 #ifdef ENABLE_DRAW
Reset()30 inline void Drawer::Reset() { count_ = 0; }
31 
Init()32 void Drawer::Init() {
33   auto ret = CreateDir(kDefaultDrawDIR);
34   if (ret != RET_OK) {
35     MS_LOG(WARNING) << "Create draw directory failed, disable draw.";
36     enabled_ = false;
37   }
38   if (enabled_) {
39     base_dir_ = RealPath(kDefaultDrawDIR);
40     if (base_dir_.empty()) {
41       MS_LOG(WARNING) << kDefaultDrawDIR << " is invalid: " << base_dir_ << ", disable draw.";
42       enabled_ = false;
43     }
44   }
45   Reset();
46 }
47 
GetNextFileName(const std::string & name)48 std::string Drawer::GetNextFileName(const std::string &name) {
49   std::ostringstream oss;
50   oss << std::setw(3) << std::setfill('0') << count_++ << '-' << name << ".dot";
51   return oss.str();
52 }
53 
SaveDotFile(const std::string & dot_name,const std::string & dot_content)54 inline bool Drawer::SaveDotFile(const std::string &dot_name, const std::string &dot_content) {
55   auto fname = GetNextFileName(dot_name);
56   auto write_path = lite::WriteStrToFile(this->base_dir_, fname, dot_content);
57   if (write_path.empty()) {
58     MS_LOG(ERROR) << "Save dot-file failed, path: " << this->base_dir_ << ", fname: " << fname;
59     return false;
60   } else {
61     MS_LOG(INFO) << "Save dot-file successfully, path: " << write_path;
62     return true;
63   }
64 }
65 
Draw(const kernel::SubGraphKernel * graph,const std::string & name)66 void Drawer::Draw(const kernel::SubGraphKernel *graph, const std::string &name) {
67   if (!enabled_) {
68     return;
69   }
70   auto gv_graph = lite::CreateGVGraph(graph);
71   if (gv_graph == nullptr) {
72     MS_LOG(ERROR) << "Create gv_graph failed.";
73     return;
74   }
75   (void)SaveDotFile(name, gv_graph->Code());
76 }
77 #ifdef ENABLE_CLOUD_INFERENCE
Draw(const CompileResult * graph,const std::string & name)78 void Drawer::Draw(const CompileResult *graph, const std::string &name) {
79   if (!enabled_) {
80     return;
81   }
82   auto gv_graph = lite::CreateGVGraph(graph);
83   if (gv_graph == nullptr) {
84     MS_LOG(ERROR) << "Create gv_graph failed.";
85     return;
86   }
87   (void)SaveDotFile(name, gv_graph->Code());
88 }
89 #endif
90 #else
91 #define WARNLOG                                                                                           \
92   MS_LOG(WARNING) << "Drawer is not enabled, please set env 'export MSLITE_EXPORT_COMPUTE_IR=on; export " \
93                   << kDrawDIREnvKey << "=/path/to/draw_dir' to enable drawer."
94 
Reset()95 inline void Drawer::Reset() { WARNLOG; }
96 
Init()97 void Drawer::Init() { WARNLOG; }
98 
SaveDotFile(const std::string & dot_name,const std::string & dot_content)99 inline bool Drawer::SaveDotFile(const std::string &dot_name, const std::string &dot_content) {
100   WARNLOG;
101   return false;
102 }
103 
Draw(const kernel::SubGraphKernel * graph,const std::string & name)104 void Drawer::Draw(const kernel::SubGraphKernel *graph, const std::string &name) { WARNLOG; }
105 #ifdef ENABLE_CLOUD_INFERENCE
Draw(const CompileResult * graph,const std::string & name)106 void Drawer::Draw(const CompileResult *graph, const std::string &name) { WARNLOG; }
107 #endif
108 #endif
109 }  // namespace mindspore::lite
110