blob: 696480895d095f355e2fb71667fc6ad235d983cc [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
Tim Halle6ccd872020-11-09 16:46:37 +000021
22import lxml.etree as xml
23
24from . import numeric_util
25from .operation import Operation
Tim Halle6ccd872020-11-09 16:46:37 +000026
Dwight Lidman9b43f842020-12-08 17:56:44 +010027UntypedDict = Dict[Any, Any]
28UntypedList = List[Any]
29
30
Tim Halle6ccd872020-11-09 16:46:37 +000031class DebugDatabase:
32 NULLREF = -1
33 show_warnings = False
34
35 SOURCE_TABLE = "source"
Dwight Lidman9b43f842020-12-08 17:56:44 +010036 _sourceUID: UntypedDict = {}
Tim Halle6ccd872020-11-09 16:46:37 +000037 _sourceHeaders = ["id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"]
Dwight Lidman9b43f842020-12-08 17:56:44 +010038 _sourceTable: UntypedList = []
Tim Halle6ccd872020-11-09 16:46:37 +000039
40 OPTIMISED_TABLE = "optimised"
Dwight Lidman9b43f842020-12-08 17:56:44 +010041 _optimisedUID: UntypedDict = {}
Tim Halle6ccd872020-11-09 16:46:37 +000042 _optimisedHeaders = ["id", "source_id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"]
Dwight Lidman9b43f842020-12-08 17:56:44 +010043 _optimisedTable: UntypedList = []
Tim Halle6ccd872020-11-09 16:46:37 +000044
45 QUEUE_TABLE = "queue"
46 _queueHeaders = ["offset", "cmdstream_id", "optimised_id"]
Dwight Lidman9b43f842020-12-08 17:56:44 +010047 _queueTable: UntypedList = []
Tim Halle6ccd872020-11-09 16:46:37 +000048
49 STREAM_TABLE = "cmdstream"
Dwight Lidman9b43f842020-12-08 17:56:44 +010050 _streamUID: UntypedDict = {}
Tim Halle6ccd872020-11-09 16:46:37 +000051 _streamHeaders = ["id", "file_offset"]
Dwight Lidman9b43f842020-12-08 17:56:44 +010052 _streamTable: UntypedList = []
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(
61 [uid, op.type, op.kernel.width, op.kernel.height, ofm_shape[-2], ofm_shape[-3], ofm_shape[-1]]
62 )
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 Gustavssoncc6915c2020-12-22 09:16:50 +010081 ofm_shape = numeric_util.full_shape(3, op.outputs[0].shape, 1)
Tim Halle6ccd872020-11-09 16:46:37 +000082 cls._optimisedTable.append(
Patrik Gustavssoncc6915c2020-12-22 09:16:50 +010083 [uid, src_uid, op.type, op.kernel.width, op.kernel.height, ofm_shape[-2], ofm_shape[-3], ofm_shape[-1]]
Tim Halle6ccd872020-11-09 16:46:37 +000084 )
85
86 @classmethod
87 def add_stream(cls, key):
88 if key not in cls._streamUID:
89 uid = len(cls._streamUID)
90 cls._streamUID[key] = uid
91 return uid
92
93 @classmethod
94 def set_stream_offset(cls, key, file_offset):
95 assert key in cls._streamUID
96 uid = cls._streamUID[key]
97 cls._streamTable.append([uid, file_offset])
98
99 @classmethod
100 def add_command(cls, stream_id, offset, op: Operation):
101 assert stream_id < len(cls._streamUID)
102 assert op in cls._optimisedUID, "Optimised operator must exist before code generation"
103 optimised_id = cls._optimisedUID[op][0]
104 cls._queueTable.append([offset, stream_id, optimised_id])
105
106 @classmethod
107 def _write_table(cls, root, name, headers, table):
108 # Convert table to CSV
109 out = io.StringIO()
110 writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC)
111 writer.writerow(headers)
112 writer.writerows(table)
113
114 # Package table into XML output
115 table = xml.SubElement(root, "table", {"name": name})
116 table.text = xml.CDATA(out.getvalue())
117
118 @classmethod
119 def write(cls, file_path, input_file, output_file):
120 root = xml.Element("debug", {"source": input_file, "optimised": output_file})
121
122 cls._write_table(root, cls.SOURCE_TABLE, cls._sourceHeaders, cls._sourceTable)
123 cls._write_table(root, cls.OPTIMISED_TABLE, cls._optimisedHeaders, cls._optimisedTable)
124 cls._write_table(root, cls.QUEUE_TABLE, cls._queueHeaders, cls._queueTable)
125 cls._write_table(root, cls.STREAM_TABLE, cls._streamHeaders, cls._streamTable)
126
127 xml.ElementTree(root).write(file_path, encoding="utf-8", xml_declaration=True, pretty_print=True)