blob: f52cd023823dfc0078b0d83d45917d5a164478f1 [file] [log] [blame]
Johan Alfven31947ad2024-04-04 15:50:08 +02001# SPDX-FileCopyrightText: Copyright 2020-2022, 2024 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] = {}
William Isakssone4d2f212024-02-10 15:54:44 +010037 _sourceHeaders = ["id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d", "ext_key"]
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(
Johan Alfven31947ad2024-04-04 15:50:08 +020061 [
62 uid,
63 str(op.type),
64 op.kernel.width,
65 op.kernel.height,
66 ofm_shape[-2],
67 ofm_shape[-3],
68 ofm_shape[-1],
69 op.op_index,
70 ]
Tim Halle6ccd872020-11-09 16:46:37 +000071 )
72
wilisa01ce7d65c2022-11-07 11:52:27 +000073 # Ops are added when their type changes, and after optimisation. If an op was already
wilisa0179a89042022-11-02 17:18:43 +000074 # added before optimisation was finished it will only be added again if it's entry
75 # has changed in any way from it's previous entry.
Tim Halle6ccd872020-11-09 16:46:37 +000076 @classmethod
77 def add_optimised(cls, parent: Operation, op: Operation):
78 assert isinstance(parent, Operation) and isinstance(op, Operation)
wilisa0179a89042022-11-02 17:18:43 +000079 if parent not in cls._sourceUID:
80 # If the parent wasn't in the source network try to look it
81 # up in the optimised network and use that op's source parent.
82 if parent in cls._optimisedUID:
83 src_uid = cls._optimisedUID[parent][1]
84 else:
85 if DebugDatabase.show_warnings:
86 print("Debug Database: Associated parent '{0}' not in network".format(parent.type))
87 src_uid = DebugDatabase.NULLREF
88 else:
89 src_uid = cls._sourceUID[parent]
90
91 # correction for missing shapes
92 if len(op.ofm_shapes) == 0:
93 ofm_shape = Shape4D(op.outputs[0].shape)
94 else:
95 ofm_shape = op.ofm_shapes[0]
96
97 next_uid = len(cls._optimisedTable) # required because no longer 1:1 UID->table correspondence
98 opt_uid = cls._optimisedUID.get(op, (next_uid, 0))[0] # already seen or next uid (if not seen)
99
100 opt_table_entry = [
101 opt_uid,
102 src_uid,
103 str(op.type),
104 op.kernel.width,
105 op.kernel.height,
106 ofm_shape.width,
107 ofm_shape.height,
108 ofm_shape.depth,
109 ]
110
Tim Halle6ccd872020-11-09 16:46:37 +0000111 if op not in cls._optimisedUID:
wilisa0179a89042022-11-02 17:18:43 +0000112 # optimised op does not exist
113 cls._optimisedUID[op] = (next_uid, src_uid)
114 cls._optimisedTable.append(opt_table_entry)
115 else:
116 # optimised op already exists
117 existing_entry = cls._optimisedTable[
118 cls._optimisedUID[op][0]
119 ] # Existing entry is where the 'op' object was last inserted
120 if opt_table_entry != existing_entry:
121 # only add again if it's changed in any way
122 opt_table_entry[0] = next_uid # give it a new unique id (required)
123 cls._optimisedUID[op] = (next_uid, src_uid)
124 cls._optimisedTable.append(opt_table_entry)
Tim Halle6ccd872020-11-09 16:46:37 +0000125
126 @classmethod
127 def add_stream(cls, key):
128 if key not in cls._streamUID:
129 uid = len(cls._streamUID)
130 cls._streamUID[key] = uid
131 return uid
132
133 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100134 def set_stream_offset(cls, key, file_offset: int):
Tim Halle6ccd872020-11-09 16:46:37 +0000135 assert key in cls._streamUID
136 uid = cls._streamUID[key]
137 cls._streamTable.append([uid, file_offset])
138
139 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100140 def add_command(cls, stream_id: int, offset: int, op: Operation):
Tim Halle6ccd872020-11-09 16:46:37 +0000141 assert stream_id < len(cls._streamUID)
142 assert op in cls._optimisedUID, "Optimised operator must exist before code generation"
143 optimised_id = cls._optimisedUID[op][0]
144 cls._queueTable.append([offset, stream_id, optimised_id])
145
146 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100147 def _write_table(cls, root: xml.Element, name: str, headers: List[str], table):
Tim Halle6ccd872020-11-09 16:46:37 +0000148 # Convert table to CSV
149 out = io.StringIO()
150 writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC)
151 writer.writerow(headers)
152 writer.writerows(table)
153
154 # Package table into XML output
155 table = xml.SubElement(root, "table", {"name": name})
156 table.text = xml.CDATA(out.getvalue())
157
158 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100159 def write(cls, file_path: str, input_file: str, output_file: str):
Tim Halle6ccd872020-11-09 16:46:37 +0000160 root = xml.Element("debug", {"source": input_file, "optimised": output_file})
161
162 cls._write_table(root, cls.SOURCE_TABLE, cls._sourceHeaders, cls._sourceTable)
163 cls._write_table(root, cls.OPTIMISED_TABLE, cls._optimisedHeaders, cls._optimisedTable)
164 cls._write_table(root, cls.QUEUE_TABLE, cls._queueHeaders, cls._queueTable)
165 cls._write_table(root, cls.STREAM_TABLE, cls._streamHeaders, cls._streamTable)
166
167 xml.ElementTree(root).write(file_path, encoding="utf-8", xml_declaration=True, pretty_print=True)