blob: bd45b468225fd3ee7231107a4dda3a3a20c7fd1e [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2020-2021 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
64 @classmethod
65 def add_optimised(cls, parent: Operation, op: Operation):
66 assert isinstance(parent, Operation) and isinstance(op, Operation)
67 if op not in cls._optimisedUID:
68 if parent not in cls._sourceUID:
69 # The the parent wasn't in the source network try to look it
70 # up in the optimised network and use that op's source parent.
71 if parent in cls._optimisedUID:
72 src_uid = cls._optimisedUID[parent][1]
73 else:
74 if DebugDatabase.show_warnings:
75 print("Debug Database: Associated parent '{0}' not in network".format(parent.type))
76 src_uid = DebugDatabase.NULLREF
77 else:
78 src_uid = cls._sourceUID[parent]
79 uid = len(cls._optimisedUID)
80 cls._optimisedUID[op] = (uid, src_uid)
Patrik Gustavsson3a269202021-01-21 08:28:55 +010081 if len(op.ofm_shapes) == 0:
82 ofm_shape = Shape4D(op.outputs[0].shape)
83 else:
84 ofm_shape = op.ofm_shapes[0]
Tim Halle6ccd872020-11-09 16:46:37 +000085 cls._optimisedTable.append(
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010086 [
87 uid,
88 src_uid,
89 str(op.type),
90 op.kernel.width,
91 op.kernel.height,
Patrik Gustavsson3a269202021-01-21 08:28:55 +010092 ofm_shape.width,
93 ofm_shape.height,
94 ofm_shape.depth,
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010095 ]
Tim Halle6ccd872020-11-09 16:46:37 +000096 )
97
98 @classmethod
99 def add_stream(cls, key):
100 if key not in cls._streamUID:
101 uid = len(cls._streamUID)
102 cls._streamUID[key] = uid
103 return uid
104
105 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100106 def set_stream_offset(cls, key, file_offset: int):
Tim Halle6ccd872020-11-09 16:46:37 +0000107 assert key in cls._streamUID
108 uid = cls._streamUID[key]
109 cls._streamTable.append([uid, file_offset])
110
111 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100112 def add_command(cls, stream_id: int, offset: int, op: Operation):
Tim Halle6ccd872020-11-09 16:46:37 +0000113 assert stream_id < len(cls._streamUID)
114 assert op in cls._optimisedUID, "Optimised operator must exist before code generation"
115 optimised_id = cls._optimisedUID[op][0]
116 cls._queueTable.append([offset, stream_id, optimised_id])
117
118 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100119 def _write_table(cls, root: xml.Element, name: str, headers: List[str], table):
Tim Halle6ccd872020-11-09 16:46:37 +0000120 # Convert table to CSV
121 out = io.StringIO()
122 writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC)
123 writer.writerow(headers)
124 writer.writerows(table)
125
126 # Package table into XML output
127 table = xml.SubElement(root, "table", {"name": name})
128 table.text = xml.CDATA(out.getvalue())
129
130 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100131 def write(cls, file_path: str, input_file: str, output_file: str):
Tim Halle6ccd872020-11-09 16:46:37 +0000132 root = xml.Element("debug", {"source": input_file, "optimised": output_file})
133
134 cls._write_table(root, cls.SOURCE_TABLE, cls._sourceHeaders, cls._sourceTable)
135 cls._write_table(root, cls.OPTIMISED_TABLE, cls._optimisedHeaders, cls._optimisedTable)
136 cls._write_table(root, cls.QUEUE_TABLE, cls._queueHeaders, cls._queueTable)
137 cls._write_table(root, cls.STREAM_TABLE, cls._streamHeaders, cls._streamTable)
138
139 xml.ElementTree(root).write(file_path, encoding="utf-8", xml_declaration=True, pretty_print=True)