blob: 006348c0047c0e9f2bea0816770a87218eb687e0 [file] [log] [blame]
Tim Halle6ccd872020-11-09 16:46:37 +00001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
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
Tim Halle6ccd872020-11-09 16:46:37 +000028
Dwight Lidman9b43f842020-12-08 17:56:44 +010029
Tim Halle6ccd872020-11-09 16:46:37 +000030class DebugDatabase:
31 NULLREF = -1
32 show_warnings = False
33
34 SOURCE_TABLE = "source"
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010035 _sourceUID: Dict[Any, int] = {}
Tim Halle6ccd872020-11-09 16:46:37 +000036 _sourceHeaders = ["id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010037 _sourceTable: List[List[Union[float, int, str]]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000038
39 OPTIMISED_TABLE = "optimised"
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010040 _optimisedUID: Dict[Any, Tuple[int, int]] = {}
Tim Halle6ccd872020-11-09 16:46:37 +000041 _optimisedHeaders = ["id", "source_id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010042 _optimisedTable: List[List[Union[float, int, str]]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000043
44 QUEUE_TABLE = "queue"
45 _queueHeaders = ["offset", "cmdstream_id", "optimised_id"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010046 _queueTable: List[List[int]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000047
48 STREAM_TABLE = "cmdstream"
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010049 _streamUID: Dict[Any, int] = {}
Tim Halle6ccd872020-11-09 16:46:37 +000050 _streamHeaders = ["id", "file_offset"]
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010051 _streamTable: List[List[int]] = []
Tim Halle6ccd872020-11-09 16:46:37 +000052
53 @classmethod
54 def add_source(cls, op: Operation):
55 assert isinstance(op, Operation)
56 uid = len(cls._sourceUID)
57 cls._sourceUID[op] = uid
58 ofm_shape = numeric_util.full_shape(3, op.outputs[0].shape, 1)
59 cls._sourceTable.append(
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010060 [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 +000061 )
62
63 @classmethod
64 def add_optimised(cls, parent: Operation, op: Operation):
65 assert isinstance(parent, Operation) and isinstance(op, Operation)
66 if op not in cls._optimisedUID:
67 if parent not in cls._sourceUID:
68 # The the parent wasn't in the source network try to look it
69 # up in the optimised network and use that op's source parent.
70 if parent in cls._optimisedUID:
71 src_uid = cls._optimisedUID[parent][1]
72 else:
73 if DebugDatabase.show_warnings:
74 print("Debug Database: Associated parent '{0}' not in network".format(parent.type))
75 src_uid = DebugDatabase.NULLREF
76 else:
77 src_uid = cls._sourceUID[parent]
78 uid = len(cls._optimisedUID)
79 cls._optimisedUID[op] = (uid, src_uid)
Patrik Gustavssoncc6915c2020-12-22 09:16:50 +010080 ofm_shape = numeric_util.full_shape(3, op.outputs[0].shape, 1)
Tim Halle6ccd872020-11-09 16:46:37 +000081 cls._optimisedTable.append(
erik.andersson@arm.com606063f2021-01-19 11:24:43 +010082 [
83 uid,
84 src_uid,
85 str(op.type),
86 op.kernel.width,
87 op.kernel.height,
88 ofm_shape[-2],
89 ofm_shape[-3],
90 ofm_shape[-1],
91 ]
Tim Halle6ccd872020-11-09 16:46:37 +000092 )
93
94 @classmethod
95 def add_stream(cls, key):
96 if key not in cls._streamUID:
97 uid = len(cls._streamUID)
98 cls._streamUID[key] = uid
99 return uid
100
101 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100102 def set_stream_offset(cls, key, file_offset: int):
Tim Halle6ccd872020-11-09 16:46:37 +0000103 assert key in cls._streamUID
104 uid = cls._streamUID[key]
105 cls._streamTable.append([uid, file_offset])
106
107 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100108 def add_command(cls, stream_id: int, offset: int, op: Operation):
Tim Halle6ccd872020-11-09 16:46:37 +0000109 assert stream_id < len(cls._streamUID)
110 assert op in cls._optimisedUID, "Optimised operator must exist before code generation"
111 optimised_id = cls._optimisedUID[op][0]
112 cls._queueTable.append([offset, stream_id, optimised_id])
113
114 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100115 def _write_table(cls, root: xml.Element, name: str, headers: List[str], table):
Tim Halle6ccd872020-11-09 16:46:37 +0000116 # Convert table to CSV
117 out = io.StringIO()
118 writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC)
119 writer.writerow(headers)
120 writer.writerows(table)
121
122 # Package table into XML output
123 table = xml.SubElement(root, "table", {"name": name})
124 table.text = xml.CDATA(out.getvalue())
125
126 @classmethod
erik.andersson@arm.com606063f2021-01-19 11:24:43 +0100127 def write(cls, file_path: str, input_file: str, output_file: str):
Tim Halle6ccd872020-11-09 16:46:37 +0000128 root = xml.Element("debug", {"source": input_file, "optimised": output_file})
129
130 cls._write_table(root, cls.SOURCE_TABLE, cls._sourceHeaders, cls._sourceTable)
131 cls._write_table(root, cls.OPTIMISED_TABLE, cls._optimisedHeaders, cls._optimisedTable)
132 cls._write_table(root, cls.QUEUE_TABLE, cls._queueHeaders, cls._queueTable)
133 cls._write_table(root, cls.STREAM_TABLE, cls._streamHeaders, cls._streamTable)
134
135 xml.ElementTree(root).write(file_path, encoding="utf-8", xml_declaration=True, pretty_print=True)