1# Copyright 2020-2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15"""The container of metadata used in profiler parser.""" 16GIGABYTES = 1024 * 1024 * 1024 17 18 19class HWTSContainer: 20 """ 21 HWTS output container. 22 23 Args: 24 split_list (list): The split list of metadata in HWTS output file. 25 """ 26 27 def __init__(self, split_list): 28 self._op_name = '' 29 self._duration = None 30 self._status = split_list[0] 31 self._task_id = split_list[6] 32 self._cycle_counter = float(split_list[7]) 33 self._stream_id = split_list[8] 34 35 @property 36 def status(self): 37 """Get the status of the operator, i.e. Start or End.""" 38 return self._status 39 40 @property 41 def task_id(self): 42 """Get the task id of the operator.""" 43 return self._task_id 44 45 @property 46 def cycle_counter(self): 47 """Get the cycle counter.""" 48 return self._cycle_counter 49 50 @property 51 def stream_id(self): 52 """Get the stream id of the operator.""" 53 return self._stream_id 54 55 @property 56 def op_name(self): 57 """Get the name of the operator.""" 58 return self._op_name 59 60 @op_name.setter 61 def op_name(self, name): 62 """Set the name of the operator.""" 63 self._op_name = name 64 65 @property 66 def duration(self): 67 """Get the duration of the operator execution.""" 68 return self._duration 69 70 @duration.setter 71 def duration(self, value): 72 """Set the duration of the operator execution.""" 73 self._duration = value 74 75 76class TimelineContainer: 77 """ 78 A container of operator computation metadata. 79 80 Args: 81 split_list (list): The split list of metadata in op_compute output file. 82 """ 83 84 def __init__(self, split_list): 85 self._op_name = split_list[0] 86 self._stream_id = str(split_list[1]) 87 self._start_time = float(split_list[2]) 88 self._duration = float(split_list[3]) 89 self._pid = None 90 if len(split_list) == 5: 91 self._pid = int(split_list[4]) 92 93 @property 94 def op_name(self): 95 """Get the name of the operator.""" 96 return self._op_name 97 98 @property 99 def stream_id(self): 100 """Get the stream id of the operator.""" 101 return self._stream_id 102 103 @property 104 def start_time(self): 105 """Get the execution start time of the operator.""" 106 return self._start_time 107 108 @property 109 def duration(self): 110 """Get the duration of the operator execution.""" 111 return self._duration 112 113 @property 114 def pid(self): 115 """Get the pid of the operator execution.""" 116 return self._pid 117 118 119class MemoryGraph: 120 """ 121 A container for graph. 122 123 Args: 124 graph_proto (proto): Graph proto, defined in profiler module. 125 """ 126 127 def __init__(self, graph_proto): 128 self._graph_proto = graph_proto 129 self.graph_id = graph_proto.graph_id 130 self.static_mem = graph_proto.static_mem / GIGABYTES 131 self.fp_start = None 132 self.bp_end = None 133 self.lines = [] 134 self.nodes = {} 135 self.breakdowns = [] 136 137 def to_dict(self): 138 """Convert Graph to dict.""" 139 graph = { 140 'graph_id': self.graph_id, 141 'static_mem': self.static_mem, 142 'nodes': self.nodes, 143 'fp_start': self.fp_start, 144 'bp_end': self.bp_end, 145 'lines': self.lines, 146 'breakdowns': self.breakdowns 147 } 148 149 return graph 150 151 152class MemoryNode: 153 """ 154 A container for node. 155 156 Args: 157 node_proto (proto): Node proto. 158 """ 159 160 def __init__(self, node_proto): 161 self._node_proto = node_proto 162 self.node_id = node_proto.node_id 163 self.name = node_proto.node_name 164 self.fullname = "" 165 self.input_ids = list(node_proto.input_tensor_id) 166 self.output_ids = list(node_proto.output_tensor_id) 167 self.workspace_ids = list(node_proto.workspace_tensor_id) 168 self.inputs = [] 169 self.outputs = [] 170 self.workspaces = [] 171 self.allocations = 0 172 self.deallocations = 0 173 self.size = 0 174 self.mem_change = 0 175 176 def to_dict(self): 177 """Convert Node to dict.""" 178 node = { 179 'name': self.name, 180 'fullname': self.fullname, 181 'node_id': self.node_id, 182 'allocations': self.allocations, 183 'size': self.size, 184 'allocated': self.mem_change, 185 'inputs': self.inputs, 186 'outputs': self.outputs 187 } 188 189 return node 190 191 192class MemoryTensor: 193 """ 194 A container for tensor. 195 196 Args: 197 tensor_proto (proto): Tensor proto. 198 """ 199 200 def __init__(self, tensor_proto): 201 self._tensor_proto = tensor_proto 202 self.tensor_id = tensor_proto.tensor_id 203 self.life_long = tensor_proto.life_long 204 self.life_start = tensor_proto.life_start 205 self.life_end = tensor_proto.life_end 206 self.size = tensor_proto.size / GIGABYTES 207 self.type = tensor_proto.type 208 self.shape = "" 209 self.format = "" 210 self.dtype = "" 211 self.source_node = "" 212 self.name = "" 213 214 def to_dict(self): 215 """Convert Tensor to a dict.""" 216 tensor = { 217 'tensor_name': self.name, 218 'tensor_id': self.tensor_id, 219 'size': self.size, 220 'type': self.type, 221 'shape': self.shape, 222 'format': self.format, 223 'data_type': self.dtype, 224 'life_long': self.life_long, 225 'life_start': self.life_start, 226 'life_end': self.life_end 227 } 228 229 return tensor 230