You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

container.py 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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."""
  16. GIGABYTES = 1024 * 1024 * 1024
  17. class HWTSContainer:
  18. """
  19. HWTS output container.
  20. Args:
  21. split_list (list): The split list of metadata in HWTS output file.
  22. """
  23. def __init__(self, split_list):
  24. self._op_name = ''
  25. self._duration = None
  26. self._status = split_list[0]
  27. self._task_id = split_list[6]
  28. self._cycle_counter = float(split_list[7])
  29. self._stream_id = split_list[8]
  30. @property
  31. def status(self):
  32. """Get the status of the operator, i.e. Start or End."""
  33. return self._status
  34. @property
  35. def task_id(self):
  36. """Get the task id of the operator."""
  37. return self._task_id
  38. @property
  39. def cycle_counter(self):
  40. """Get the cycle counter."""
  41. return self._cycle_counter
  42. @property
  43. def stream_id(self):
  44. """Get the stream id of the operator."""
  45. return self._stream_id
  46. @property
  47. def op_name(self):
  48. """Get the name of the operator."""
  49. return self._op_name
  50. @op_name.setter
  51. def op_name(self, name):
  52. """Set the name of the operator."""
  53. self._op_name = name
  54. @property
  55. def duration(self):
  56. """Get the duration of the operator execution."""
  57. return self._duration
  58. @duration.setter
  59. def duration(self, value):
  60. """Set the duration of the operator execution."""
  61. self._duration = value
  62. class TimelineContainer:
  63. """
  64. A container of operator computation metadata.
  65. Args:
  66. split_list (list): The split list of metadata in op_compute output file.
  67. """
  68. def __init__(self, split_list):
  69. self._op_name = split_list[0]
  70. self._stream_id = str(split_list[1])
  71. self._start_time = float(split_list[2])
  72. self._duration = float(split_list[3])
  73. self._pid = None
  74. if len(split_list) == 5:
  75. self._pid = int(split_list[4])
  76. @property
  77. def op_name(self):
  78. """Get the name of the operator."""
  79. return self._op_name
  80. @property
  81. def stream_id(self):
  82. """Get the stream id of the operator."""
  83. return self._stream_id
  84. @property
  85. def start_time(self):
  86. """Get the execution start time of the operator."""
  87. return self._start_time
  88. @property
  89. def duration(self):
  90. """Get the duration of the operator execution."""
  91. return self._duration
  92. @property
  93. def pid(self):
  94. """Get the pid of the operator execution."""
  95. return self._pid
  96. class MemoryGraph:
  97. """
  98. A container for graph.
  99. Args:
  100. graph_proto (proto): Graph proto, defined in profiler module.
  101. """
  102. def __init__(self, graph_proto):
  103. self._graph_proto = graph_proto
  104. self.graph_id = graph_proto.graph_id
  105. self.static_mem = graph_proto.static_mem / GIGABYTES
  106. self.fp_start = None
  107. self.bp_end = None
  108. self.lines = []
  109. self.nodes = {}
  110. self.breakdowns = []
  111. def to_dict(self):
  112. """Convert Graph to dict."""
  113. graph = {
  114. 'graph_id': self.graph_id,
  115. 'static_mem': self.static_mem,
  116. 'nodes': self.nodes,
  117. 'fp_start': self.fp_start,
  118. 'bp_end': self.bp_end,
  119. 'lines': self.lines,
  120. 'breakdowns': self.breakdowns
  121. }
  122. return graph
  123. class MemoryNode:
  124. """
  125. A container for node.
  126. Args:
  127. node_proto (proto): Node proto.
  128. """
  129. def __init__(self, node_proto):
  130. self._node_proto = node_proto
  131. self.node_id = node_proto.node_id
  132. self.name = node_proto.node_name
  133. self.fullname = ""
  134. self.input_ids = list(node_proto.input_tensor_id)
  135. self.output_ids = list(node_proto.output_tensor_id)
  136. self.workspace_ids = list(node_proto.workspace_tensor_id)
  137. self.inputs = []
  138. self.outputs = []
  139. self.workspaces = []
  140. self.allocations = 0
  141. self.deallocations = 0
  142. self.size = 0
  143. self.mem_change = 0
  144. def to_dict(self):
  145. """Convert Node to dict."""
  146. node = {
  147. 'name': self.name,
  148. 'fullname': self.fullname,
  149. 'node_id': self.node_id,
  150. 'allocations': self.allocations,
  151. 'size': self.size,
  152. 'allocated': self.mem_change,
  153. 'inputs': self.inputs,
  154. 'outputs': self.outputs
  155. }
  156. return node
  157. class MemoryTensor:
  158. """
  159. A container for tensor.
  160. Args:
  161. tensor_proto (proto): Tensor proto.
  162. """
  163. def __init__(self, tensor_proto):
  164. self._tensor_proto = tensor_proto
  165. self.tensor_id = tensor_proto.tensor_id
  166. self.life_long = tensor_proto.life_long
  167. self.life_start = tensor_proto.life_start
  168. self.life_end = tensor_proto.life_end
  169. self.size = tensor_proto.size / GIGABYTES
  170. self.type = tensor_proto.type
  171. self.shape = ""
  172. self.format = ""
  173. self.dtype = ""
  174. self.source_node = ""
  175. self.name = ""
  176. def to_dict(self):
  177. """Convert Tensor to a dict."""
  178. tensor = {
  179. 'tensor_name': self.name,
  180. 'tensor_id': self.tensor_id,
  181. 'size': self.size,
  182. 'type': self.type,
  183. 'shape': self.shape,
  184. 'format': self.format,
  185. 'data_type': self.dtype,
  186. 'life_long': self.life_long,
  187. 'life_start': self.life_start,
  188. 'life_end': self.life_end
  189. }
  190. return tensor