• 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/data_dump/dump_utils.h"
17 #include <map>
18 #include <vector>
19 #include <algorithm>
20 
21 #include "common/trans.h"
22 #include "utils/ms_context.h"
23 #include "debug/anf_ir_utils.h"
24 #include "debug/data_dump/dump_json_parser.h"
25 #include "backend/session/anf_runtime_algorithm.h"
26 #include "runtime/device/kernel_runtime_manager.h"
27 
28 namespace mindspore {
ConvertPhysicalDeviceId(uint32_t device_id)29 uint32_t ConvertPhysicalDeviceId(uint32_t device_id) {
30   auto context = MsContext::GetInstance();
31   MS_EXCEPTION_IF_NULL(context);
32   auto device_target = context->get_param<std::string>(MS_CTX_DEVICE_TARGET);
33   auto kernel_runtime = device::KernelRuntimeManager::Instance().GetSingleKernelRuntime(device_target, device_id);
34   MS_EXCEPTION_IF_NULL(kernel_runtime);
35   return kernel_runtime->device_id();
36 }
37 
GenerateDumpPath(uint32_t graph_id,uint32_t rank_id)38 std::string GenerateDumpPath(uint32_t graph_id, uint32_t rank_id) {
39   auto &dump_json_parser = DumpJsonParser::GetInstance();
40   std::string net_name = dump_json_parser.net_name();
41   std::string iterator = std::to_string(dump_json_parser.cur_dump_iter());
42   std::string dump_path = dump_json_parser.path();
43   if (dump_path.back() != '/') {
44     dump_path += "/";
45   }
46   dump_path += ("rank_" + std::to_string(rank_id) + "/" + net_name + "/" + std::to_string(graph_id) + "/" + iterator);
47   return dump_path;
48 }
49 
GetFileKernelName(NotNull<std::string * > kernel_name)50 void GetFileKernelName(NotNull<std::string *> kernel_name) {
51   const std::string strsrc = "/";
52   const std::string strdst = "--";
53   std::string::size_type pos = 0;
54   std::string::size_type srclen = strsrc.size();
55   std::string::size_type dstlen = strdst.size();
56   while ((pos = kernel_name->find(strsrc, pos)) != std::string::npos) {
57     kernel_name->replace(pos, srclen, strdst);
58     pos += dstlen;
59   }
60 }
61 
SetConstNodeId(const AnfNodePtr & node,std::map<std::string,size_t> * const_map)62 void SetConstNodeId(const AnfNodePtr &node, std::map<std::string, size_t> *const_map) {
63   MS_EXCEPTION_IF_NULL(node);
64   if (!node->isa<ValueNode>()) {
65     return;
66   }
67   std::string node_name = GetKernelNodeName(node);
68   MS_EXCEPTION_IF_NULL(const_map);
69   auto iter = const_map->find(node_name);
70   if (iter == const_map->end()) {
71     auto const_idx = const_map->size() + 1;
72     (*const_map)[node_name] = const_idx;
73   }
74 }
75 
GetCNodeConstantId(const CNodePtr & node,std::map<std::string,size_t> * const_map)76 void GetCNodeConstantId(const CNodePtr &node, std::map<std::string, size_t> *const_map) {
77   MS_EXCEPTION_IF_NULL(node);
78   auto &inputs = node->inputs();
79   if (inputs.empty()) {
80     MS_LOG(EXCEPTION) << "Inputs of apply node is empty";
81   }
82   AnfNodePtr op = inputs[0];
83 
84   // CNode/ConstGraph/Const/Parameter
85   MS_EXCEPTION_IF_NULL(op);
86   if (op->isa<CNode>() || IsValueNode<FuncGraph>(op) || op->isa<Parameter>()) {
87     MS_LOG(WARNING) << "Operator must be a primitive.";
88   } else {
89     // process OP inputs
90     for (size_t i = 1; i < inputs.size(); ++i) {
91       SetConstNodeId(inputs[i], const_map);
92     }
93   }
94 }
95 
GetConstantId(const session::KernelGraph * graph,std::map<std::string,size_t> * const_map)96 void GetConstantId(const session::KernelGraph *graph, std::map<std::string, size_t> *const_map) {
97   MS_EXCEPTION_IF_NULL(graph);
98   std::vector<AnfNodePtr> nodes = TopoSort(graph->get_return(), SuccIncoming, AlwaysInclude);
99   for (const AnfNodePtr &node : nodes) {
100     MS_EXCEPTION_IF_NULL(node);
101     if (!node->isa<CNode>()) {
102       continue;
103     }
104     auto cnode = node->cast<CNodePtr>();
105     MS_EXCEPTION_IF_NULL(cnode);
106     if (cnode != graph->get_return()) {
107       GetCNodeConstantId(cnode, const_map);
108     } else {
109       SetConstNodeId(cnode->input(1), const_map);
110     }
111   }
112 }
113 
GetDumpIntShape(const AnfNodePtr & node,size_t index,NotNull<ShapeVector * > int_shapes,bool trans_flag)114 void GetDumpIntShape(const AnfNodePtr &node, size_t index, NotNull<ShapeVector *> int_shapes, bool trans_flag) {
115   if (trans_flag) {
116     *int_shapes = trans::GetRuntimePaddingShape(node, index);
117   } else {
118     auto shape = AnfAlgo::GetOutputDeviceShape(node, index);
119     (void)std::transform(shape.begin(), shape.end(), std::back_inserter(*int_shapes),
120                          [](size_t inner_item) { return SizeToInt(inner_item); });
121   }
122 }
123 
DumpMemToFile(const std::string & file_path,const device::DeviceAddress & addr,const ShapeVector & int_shapes,const TypeId & type,bool trans_flag)124 void DumpMemToFile(const std::string &file_path, const device::DeviceAddress &addr, const ShapeVector &int_shapes,
125                    const TypeId &type, bool trans_flag) {
126   auto format = kOpFormat_DEFAULT;
127   auto ret = addr.DumpMemToFile(file_path, format, int_shapes, type, trans_flag);
128   if (!ret) {
129     MS_LOG(ERROR) << "DumpMemToFile Failed: flag:" << trans_flag << ", path:" << file_path << ", host_format:" << format
130                   << ".!";
131   }
132 }
133 
GetTimeStamp()134 uint64_t GetTimeStamp() {
135   auto cur_sys_time = std::chrono::system_clock::now();
136   uint64_t timestamp = std::chrono::duration_cast<std::chrono::microseconds>(cur_sys_time.time_since_epoch()).count();
137   return timestamp;
138 }
139 
GetOpNameWithoutScope(const std::string & fullname_with_scope)140 std::string GetOpNameWithoutScope(const std::string &fullname_with_scope) {
141   const std::string separator("--");
142   std::size_t found = fullname_with_scope.rfind(separator);
143   std::string op_name;
144   if (found != std::string::npos) {
145     op_name = fullname_with_scope.substr(found + separator.length());
146   }
147   return op_name;
148 }
149 }  // namespace mindspore
150