• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021-2022 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 "include/backend/debug/data_dump/dump_utils.h"
17 #include <dirent.h>
18 #ifdef ENABLE_DEBUGGER
19 #include <sys/stat.h>
20 #endif
21 #include <map>
22 #include <vector>
23 #include <stack>
24 #include <queue>
25 #include <algorithm>
26 
27 #include "runtime/device/ms_device_shape_transfer.h"
28 #include "utils/ms_context.h"
29 #include "include/backend/debug/data_dump/dump_json_parser.h"
30 #include "include/backend/anf_runtime_algorithm.h"
31 #include "include/common/utils/anfalgo.h"
32 #include "runtime/device/kernel_runtime_manager.h"
33 #include "include/common/utils/utils.h"
34 #include "include/common/debug/common.h"
35 #include "runtime/graph_scheduler/device_tensor_store.h"
36 #include "mindspore/core/utils/file_utils.h"
37 
38 using mindspore::runtime::DeviceTensorStore;
39 
40 namespace mindspore {
41 static std::vector<std::string> g_overflow_operators;
42 
ConvertPhysicalDeviceId(uint32_t device_id)43 uint32_t ConvertPhysicalDeviceId(uint32_t device_id) {
44   auto context = MsContext::GetInstance();
45   MS_EXCEPTION_IF_NULL(context);
46   auto device_target = context->get_param<std::string>(MS_CTX_DEVICE_TARGET);
47   auto kernel_runtime = device::KernelRuntimeManager::Instance().GetSingleKernelRuntime(device_target, device_id);
48   MS_EXCEPTION_IF_NULL(kernel_runtime);
49   return kernel_runtime->device_id();
50 }
51 
GenerateDumpPath(uint32_t graph_id,uint32_t rank_id,bool is_cst)52 std::string GenerateDumpPath(uint32_t graph_id, uint32_t rank_id, bool is_cst) {
53   auto &dump_json_parser = DumpJsonParser::GetInstance();
54   std::string net_name = dump_json_parser.net_name();
55   std::string iterator = std::to_string(dump_json_parser.cur_dump_iter());
56   std::string dump_path = dump_json_parser.path();
57   if (dump_path.back() != '/') {
58     dump_path += "/";
59   }
60   if (is_cst) {
61     dump_path += ("rank_" + std::to_string(rank_id) + "/" + net_name + "/" + std::to_string(graph_id) + "/constants/");
62   } else {
63     dump_path +=
64       ("rank_" + std::to_string(rank_id) + "/" + net_name + "/" + std::to_string(graph_id) + "/" + iterator + "/");
65   }
66   return dump_path;
67 }
68 
GetFileKernelName(NotNull<std::string * > kernel_name)69 void GetFileKernelName(NotNull<std::string *> kernel_name) {
70   const std::string strsrc_to_replace[4] = {"/", "\\", ".", " "};
71   const std::string strdst = "_";
72   for (const std::string strsrc : strsrc_to_replace) {
73     std::string::size_type pos = 0;
74     std::string::size_type srclen = strsrc.size();
75     std::string::size_type dstlen = strdst.size();
76     while ((pos = kernel_name->find(strsrc, pos)) != std::string::npos) {
77       kernel_name->replace(pos, srclen, strdst);
78       pos += dstlen;
79     }
80   }
81 }
82 
GetDumpIntShape(const AnfNodePtr & node,size_t index,NotNull<ShapeVector * > const int_shapes,bool trans_flag)83 void GetDumpIntShape(const AnfNodePtr &node, size_t index, NotNull<ShapeVector *> const int_shapes, bool trans_flag) {
84   if (trans_flag) {
85     MS_EXCEPTION_IF_NULL(node);
86     if (IsValueNode<None>(node)) {
87       return;
88     }
89     *int_shapes = trans::GetRuntimePaddingShape(node, index);
90   } else {
91     *int_shapes = AnfAlgo::GetOutputDeviceShape(node, index);
92   }
93 }
94 
GetParameterInfo(const AnfNodePtr & node,NotNull<ShapeVector * > const int_shapes,NotNull<TypeId * > const host_type,NotNull<TypeId * > const device_type)95 const DeviceTensorPtr GetParameterInfo(const AnfNodePtr &node, NotNull<ShapeVector *> const int_shapes,
96                                        NotNull<TypeId *> const host_type, NotNull<TypeId *> const device_type) {
97   const auto &device_tensors = DeviceTensorStore::GetInstance().Fetch(node.get());
98   if (device_tensors.size() < 1) {
99     return nullptr;
100   }
101   auto device_addr = device_tensors[0];
102   MS_EXCEPTION_IF_NULL(device_addr);
103   auto &dump_json_parser = DumpJsonParser::GetInstance();
104   bool trans_flag = dump_json_parser.trans_flag();
105   auto ref_node = device_addr->GetNodeIndex().first;
106   MS_EXCEPTION_IF_NULL(ref_node);
107   GetDumpIntShape(ref_node, kParameterOutputIndex, int_shapes, trans_flag);
108   *host_type = common::AnfAlgo::GetOutputInferDataType(ref_node, kParameterOutputIndex);
109   *device_type = AnfAlgo::GetOutputDeviceDataType(ref_node, kParameterOutputIndex);
110   return device_addr;
111 }
112 
DumpMemToFile(const std::string & file_path,const device::DeviceAddress & addr,const ShapeVector & int_shapes,const TypeId & type,bool trans_flag)113 void DumpMemToFile(const std::string &file_path, const device::DeviceAddress &addr, const ShapeVector &int_shapes,
114                    const TypeId &type, bool trans_flag) {
115   auto format = kOpFormat_DEFAULT;
116   auto ret = addr.DumpMemToFile(file_path, format, int_shapes, type, trans_flag);
117   if (!ret) {
118     MS_LOG(ERROR) << "DumpMemToFile Failed: flag:" << trans_flag << ", path:" << file_path << ", host_format:" << format
119                   << ".!";
120   }
121 }
122 
DumpToFile(const std::string & file_name,const std::string & dump_str)123 void DumpToFile(const std::string &file_name, const std::string &dump_str) {
124   if (dump_str.empty()) {
125     MS_LOG(ERROR) << "Failed to dump empty tensor data.";
126     return;
127   }
128 
129   auto real_path = Common::CreatePrefixPath(file_name);
130   if (!real_path.has_value()) {
131     MS_LOG(ERROR) << "CreatePrefixPath failed.";
132     return;
133   }
134   std::string real_path_str = real_path.value();
135   ChangeFileMode(real_path_str, S_IWUSR);
136   std::ofstream file(real_path_str, std::ofstream::out | std::ofstream::trunc);
137   if (!file.is_open()) {
138     MS_LOG(EXCEPTION) << "Open file " << real_path_str << "failed: " << ErrnoToString(errno);
139   }
140   file << dump_str;
141   if (file.bad()) {
142     file.close();
143     MS_LOG(EXCEPTION) << "Dump string to file " << real_path_str << " failed: " << ErrnoToString(errno);
144   }
145   file.close();
146   ChangeFileMode(real_path_str, S_IRUSR);
147 }
148 }  // namespace mindspore
149