erik.andersson@arm.com | 460c689 | 2021-02-24 14:38:09 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 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. |
| 16 | import csv |
| 17 | import io |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 18 | from typing import Any |
| 19 | from typing import Dict |
| 20 | from typing import List |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 21 | from typing import Tuple |
| 22 | from typing import Union |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 23 | |
| 24 | import lxml.etree as xml |
| 25 | |
| 26 | from . import numeric_util |
| 27 | from .operation import Operation |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 28 | from .shape4d import Shape4D |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 29 | |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 30 | |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 31 | class DebugDatabase: |
| 32 | NULLREF = -1 |
| 33 | show_warnings = False |
| 34 | |
| 35 | SOURCE_TABLE = "source" |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 36 | _sourceUID: Dict[Any, int] = {} |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 37 | _sourceHeaders = ["id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"] |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 38 | _sourceTable: List[List[Union[float, int, str]]] = [] |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 39 | |
| 40 | OPTIMISED_TABLE = "optimised" |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 41 | _optimisedUID: Dict[Any, Tuple[int, int]] = {} |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 42 | _optimisedHeaders = ["id", "source_id", "operator", "kernel_w", "kernel_h", "ofm_w", "ofm_h", "ofm_d"] |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 43 | _optimisedTable: List[List[Union[float, int, str]]] = [] |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 44 | |
| 45 | QUEUE_TABLE = "queue" |
| 46 | _queueHeaders = ["offset", "cmdstream_id", "optimised_id"] |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 47 | _queueTable: List[List[int]] = [] |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 48 | |
| 49 | STREAM_TABLE = "cmdstream" |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 50 | _streamUID: Dict[Any, int] = {} |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 51 | _streamHeaders = ["id", "file_offset"] |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 52 | _streamTable: List[List[int]] = [] |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 53 | |
| 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.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 61 | [uid, str(op.type), op.kernel.width, op.kernel.height, ofm_shape[-2], ofm_shape[-3], ofm_shape[-1]] |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 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 Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 81 | if len(op.ofm_shapes) == 0: |
| 82 | ofm_shape = Shape4D(op.outputs[0].shape) |
| 83 | else: |
| 84 | ofm_shape = op.ofm_shapes[0] |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 85 | cls._optimisedTable.append( |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 86 | [ |
| 87 | uid, |
| 88 | src_uid, |
| 89 | str(op.type), |
| 90 | op.kernel.width, |
| 91 | op.kernel.height, |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 92 | ofm_shape.width, |
| 93 | ofm_shape.height, |
| 94 | ofm_shape.depth, |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 95 | ] |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 96 | ) |
| 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.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 106 | def set_stream_offset(cls, key, file_offset: int): |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 107 | assert key in cls._streamUID |
| 108 | uid = cls._streamUID[key] |
| 109 | cls._streamTable.append([uid, file_offset]) |
| 110 | |
| 111 | @classmethod |
erik.andersson@arm.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 112 | def add_command(cls, stream_id: int, offset: int, op: Operation): |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 113 | 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.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 119 | def _write_table(cls, root: xml.Element, name: str, headers: List[str], table): |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 120 | # 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.com | 606063f | 2021-01-19 11:24:43 +0100 | [diff] [blame] | 131 | def write(cls, file_path: str, input_file: str, output_file: str): |
Tim Hall | e6ccd87 | 2020-11-09 16:46:37 +0000 | [diff] [blame] | 132 | 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) |