blob: c1389e51f9a30966ffb2c3befe2050dddbfd6ec9 [file] [log] [blame]
wilisa0179a89042022-11-02 17:18:43 +00001# SPDX-FileCopyrightText: Copyright 2020-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Tim Halle6ccd872020-11-09 16:46:37 +00002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16import csv
17import io
Dwight Lidman9b43f842020-12-08 17:56:44 +010018from typing import Any
19from typing import Dict
20from typing import List
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010021from typing import Tuple
22from typing import Union
Tim Halle6ccd872020-11-09 16:46:37 +000023
24import lxml.etree as xml
25
26from . import numeric_util
27from .operation import Operation
Patrik Gustavsson3a269202021-01-21 08:28:55 +010028from .shape4d import Shape4D
Tim Halle6ccd872020-11-09 16:46:37 +000029
Dwight Lidman9b43f842020-12-08 17:56:44 +010030
Tim Halle6ccd872020-11-09 16:46:37 +000031class DebugDatabase:
32 NULLREF = -1
33 show_warnings = False
34
35 SOURCE_TABLE = "source"
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010036 _sourceUID: Dict[Any, int] = {}
Tim Halle6ccd872020-11-09 16:46:37 +000037 _sourceHeaders = ["id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010038 _sourceTable: List[List[Union[float, int, str]]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000039
40 OPTIMISED_TABLE = "optimised"
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010041 _optimisedUID: Dict[Any, Tuple[int, int]] = {}
Tim Halle6ccd872020-11-09 16:46:37 +000042 _optimisedHeaders = ["id", "source_id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010043 _optimisedTable: List[List[Union[float, int, str]]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000044
45 QUEUE_TABLE = "queue"
46 _queueHeaders = ["offset", "cmdstream_id", "optimised_id"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010047 _queueTable: List[List[int]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000048
49 STREAM_TABLE = "cmdstream"
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010050 _streamUID: Dict[Any, int] = {}
Tim Halle6ccd872020-11-09 16:46:37 +000051 _streamHeaders = ["id", "file_offset"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010052 _streamTable: List[List[int]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000053
54 @classmethod
55 def add_source(cls, op: Operation):
56 assert isinstance(op, Operation)
57 uid = len(cls._sourceUID)
58 cls._sourceUID[op] = uid
59 ofm_shape = numeric_util.full_shape(3, op.outputs[0].shape, 1)
60 cls._sourceTable.append(
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010061 [uid, str(op.type), op.kernel.width, op.kernel.height, ofm_shape[-2], ofm_shape[-3], ofm_shape[-1]]
Tim Halle6ccd872020-11-09 16:46:37 +000062 )
63
wilisa0179a89042022-11-02 17:18:43 +000064 # Ops are added when their type changes, and after optimisation. If an op was already
65 # added before optimisation was finished it will only be added again if it's entry
66 # has changed in any way from it's previous entry.
Tim Halle6ccd872020-11-09 16:46:37 +000067 @classmethod
68 def add_optimised(cls, parent: Operation, op: Operation):
69 assert isinstance(parent, Operation) and isinstance(op, Operation)
wilisa0179a89042022-11-02 17:18:43 +000070 if parent not in cls._sourceUID:
71 # If the parent wasn't in the source network try to look it
72 # up in the optimised network and use that op's source parent.
73 if parent in cls._optimisedUID:
74 src_uid = cls._optimisedUID[parent][1]
75 else:
76 if DebugDatabase.show_warnings:
77 print("Debug Database: Associated parent '{0}' not in network".format(parent.type))
78 src_uid = DebugDatabase.NULLREF
79 else:
80 src_uid = cls._sourceUID[parent]
81
82 # correction for missing shapes
83 if len(op.ofm_shapes) == 0:
84 ofm_shape = Shape4D(op.outputs[0].shape)
85 else:
86 ofm_shape = op.ofm_shapes[0]
87
88 next_uid = len(cls._optimisedTable) # required because no longer 1:1 UID->table correspondence
89 opt_uid = cls._optimisedUID.get(op, (next_uid, 0))[0] # already seen or next uid (if not seen)
90
91 opt_table_entry = [
92 opt_uid,
93 src_uid,
94 str(op.type),
95 op.kernel.width,
96 op.kernel.height,
97 ofm_shape.width,
98 ofm_shape.height,
99 ofm_shape.depth,
100 ]
101
Tim Halle6ccd872020-11-09 16:46:37 +0000102 if op not in cls._optimisedUID:
wilisa0179a89042022-11-02 17:18:43 +0000103 # optimised op does not exist
104 cls._optimisedUID[op] = (next_uid, src_uid)
105 cls._optimisedTable.append(opt_table_entry)
106 else:
107 # optimised op already exists
108 existing_entry = cls._optimisedTable[
109 cls._optimisedUID[op][0]
110 ] # Existing entry is where the 'op' object was last inserted
111 if opt_table_entry != existing_entry:
112 # only add again if it's changed in any way
113 opt_table_entry[0] = next_uid # give it a new unique id (required)
114 cls._optimisedUID[op] = (next_uid, src_uid)
115 cls._optimisedTable.append(opt_table_entry)
Tim Halle6ccd872020-11-09 16:46:37 +0000116
117 @classmethod
118 def add_stream(cls, key):
119 if key not in cls._streamUID:
120 uid = len(cls._streamUID)
121 cls._streamUID[key] = uid
122 return uid
123
124 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100125 def set_stream_offset(cls, key, file_offset: int):
Tim Halle6ccd872020-11-09 16:46:37 +0000126 assert key in cls._streamUID
127 uid = cls._streamUID[key]
128 cls._streamTable.append([uid, file_offset])
129
130 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100131 def add_command(cls, stream_id: int, offset: int, op: Operation):
Tim Halle6ccd872020-11-09 16:46:37 +0000132 assert stream_id < len(cls._streamUID)
133 assert op in cls._optimisedUID, "Optimised operator must exist before code generation"
134 optimised_id = cls._optimisedUID[op][0]
135 cls._queueTable.append([offset, stream_id, optimised_id])
136
137 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100138 def _write_table(cls, root: xml.Element, name: str, headers: List[str], table):
Tim Halle6ccd872020-11-09 16:46:37 +0000139 # Convert table to CSV
140 out = io.StringIO()
141 writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC)
142 writer.writerow(headers)
143 writer.writerows(table)
144
145 # Package table into XML output
146 table = xml.SubElement(root, "table", {"name": name})
147 table.text = xml.CDATA(out.getvalue())
148
149 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100150 def write(cls, file_path: str, input_file: str, output_file: str):
Tim Halle6ccd872020-11-09 16:46:37 +0000151 root = xml.Element("debug", {"source": input_file, "optimised": output_file})
152
153 cls._write_table(root, cls.SOURCE_TABLE, cls._sourceHeaders, cls._sourceTable)
154 cls._write_table(root, cls.OPTIMISED_TABLE, cls._optimisedHeaders, cls._optimisedTable)
155 cls._write_table(root, cls.QUEUE_TABLE, cls._queueHeaders, cls._queueTable)
156 cls._write_table(root, cls.STREAM_TABLE, cls._streamHeaders, cls._streamTable)
157
158 xml.ElementTree(root).write(file_path, encoding="utf-8", xml_declaration=True, pretty_print=True)