|
| 1 | +# Copyright (C) 2025, Advanced Micro Devices, Inc. |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | + |
| 7 | +# * Redistributions of source code must retain the above copyright notice, this |
| 8 | +# list of conditions and the following disclaimer. |
| 9 | + |
| 10 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | + |
| 14 | +# * Neither the name of FINN nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from |
| 16 | +# this software without specific prior written permission. |
| 17 | + |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +import numpy as np |
| 30 | +from qonnx.core.modelwrapper import ModelWrapper |
| 31 | +from qonnx.custom_op.registry import getCustomOp |
| 32 | +from qonnx.util.basic import gen_finn_dt_tensor, roundup_to_integer_multiple |
| 33 | +from typing import Dict |
| 34 | + |
| 35 | +import finn.util.data_packing as dpk |
| 36 | +from finn.util.data_packing import ( |
| 37 | + hexstring2npbytearray, |
| 38 | + pack_innermost_dim_as_hex_string, |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +def to_external_tensor(init, w_dtype): |
| 43 | + """Return an appropriately formatted and packed numpy byte array for given |
| 44 | + external parameter tensor.""" |
| 45 | + |
| 46 | + weight_width = init.shape[1] * w_dtype.bitwidth() |
| 47 | + weight_width_padded = roundup_to_integer_multiple(weight_width, 4) |
| 48 | + hex_init = pack_innermost_dim_as_hex_string(init, w_dtype, weight_width_padded, prefix="0x") |
| 49 | + ext_weight = np.array([], dtype=np.uint8) |
| 50 | + for line in hex_init: |
| 51 | + array_line = [x for x in reversed(hexstring2npbytearray(line, remove_prefix="0x"))] |
| 52 | + ext_weight = np.append(ext_weight, array_line) |
| 53 | + |
| 54 | + return ext_weight |
| 55 | + |
| 56 | + |
| 57 | +def get_driver_shapes(model: ModelWrapper) -> Dict: |
| 58 | + idt = [] |
| 59 | + idma_names = [] |
| 60 | + ishape_normal = [] |
| 61 | + ishape_folded = [] |
| 62 | + ishape_packed = [] |
| 63 | + for idma_ind, graph_in in enumerate(model.graph.input): |
| 64 | + i_tensor_name = graph_in.name |
| 65 | + # get inp tensor properties |
| 66 | + i_tensor_dt = model.get_tensor_datatype(i_tensor_name) |
| 67 | + i_tensor_shape_normal = tuple(model.get_tensor_shape(i_tensor_name)) |
| 68 | + # go down into dataflow partition to get folded shape info etc |
| 69 | + # TODO consider setting these as attributes during dataflow partitioning |
| 70 | + i_consumer = model.find_consumer(i_tensor_name) |
| 71 | + assert ( |
| 72 | + i_consumer.op_type == "StreamingDataflowPartition" |
| 73 | + ), """ |
| 74 | + Ensure CreateDataflowPartition called before driver creation.""" |
| 75 | + first_df_model = ModelWrapper(getCustomOp(i_consumer).get_nodeattr("model")) |
| 76 | + assert ( |
| 77 | + first_df_model.graph.node[0].op_type == "IODMA_hls" |
| 78 | + ), "First partition must hold input IODMA" |
| 79 | + successors = model.find_direct_successors(i_consumer) |
| 80 | + successor_input_num = list(successors[0].input).index(i_consumer.output[0]) |
| 81 | + successor_sdp = getCustomOp(successors[0]) |
| 82 | + successor_df_model = ModelWrapper(successor_sdp.get_nodeattr("model")) |
| 83 | + first_node = successor_df_model.find_consumer( |
| 84 | + successor_df_model.graph.input[successor_input_num].name |
| 85 | + ) |
| 86 | + i_tensor_shape_folded = tuple(getCustomOp(first_node).get_folded_input_shape()) |
| 87 | + # generate dummy folded i/o tensors and their packed versions |
| 88 | + i_tensor_dummy_folded = gen_finn_dt_tensor(i_tensor_dt, i_tensor_shape_folded) |
| 89 | + i_tensor_dummy_packed = dpk.finnpy_to_packed_bytearray(i_tensor_dummy_folded, i_tensor_dt) |
| 90 | + i_tensor_shape_packed = i_tensor_dummy_packed.shape |
| 91 | + # append all input tensor info to relevant lists |
| 92 | + idt.append("DataType['%s']" % i_tensor_dt.name) |
| 93 | + ishape_normal.append(i_tensor_shape_normal) |
| 94 | + ishape_folded.append(i_tensor_shape_folded) |
| 95 | + ishape_packed.append(i_tensor_shape_packed) |
| 96 | + idma_names.append(getCustomOp(i_consumer).get_nodeattr("instance_name")) |
| 97 | + |
| 98 | + odt = [] |
| 99 | + odma_names = [] |
| 100 | + oshape_normal = [] |
| 101 | + oshape_folded = [] |
| 102 | + oshape_packed = [] |
| 103 | + for odma_ind, graph_out in enumerate(model.graph.output): |
| 104 | + o_tensor_name = graph_out.name |
| 105 | + # get inp tensor properties |
| 106 | + o_tensor_dt = model.get_tensor_datatype(o_tensor_name) |
| 107 | + o_tensor_shape_normal = tuple(model.get_tensor_shape(o_tensor_name)) |
| 108 | + # go down into IODMA partition to get folded shape info etc |
| 109 | + # TODO consider setting these as attributes during dataflow partitioning |
| 110 | + o_producer = model.find_producer(o_tensor_name) |
| 111 | + assert ( |
| 112 | + o_producer.op_type == "StreamingDataflowPartition" |
| 113 | + ), """ |
| 114 | + Ensure CreateDataflowPartition called before driver creation.""" |
| 115 | + df_model = ModelWrapper(getCustomOp(o_producer).get_nodeattr("model")) |
| 116 | + assert df_model.graph.node[-1].op_type == "IODMA_hls", "Partition must hold output IODMA" |
| 117 | + predecessors = model.find_direct_predecessors(o_producer) |
| 118 | + predecessor_output_num = list(predecessors[0].output).index(o_producer.input[0]) |
| 119 | + predecessor_sdp = getCustomOp(predecessors[0]) |
| 120 | + predecessor_df_model = ModelWrapper(predecessor_sdp.get_nodeattr("model")) |
| 121 | + last_node = predecessor_df_model.find_producer( |
| 122 | + predecessor_df_model.graph.output[predecessor_output_num].name |
| 123 | + ) |
| 124 | + o_tensor_shape_folded = tuple(getCustomOp(last_node).get_folded_output_shape()) |
| 125 | + o_tensor_dummy_folded = gen_finn_dt_tensor(o_tensor_dt, o_tensor_shape_folded) |
| 126 | + o_tensor_dummy_packed = dpk.finnpy_to_packed_bytearray(o_tensor_dummy_folded, o_tensor_dt) |
| 127 | + o_tensor_shape_packed = o_tensor_dummy_packed.shape |
| 128 | + # append all output tensor info to relevant lists |
| 129 | + odt.append("DataType['%s']" % o_tensor_dt.name) |
| 130 | + oshape_normal.append(o_tensor_shape_normal) |
| 131 | + oshape_folded.append(o_tensor_shape_folded) |
| 132 | + oshape_packed.append(o_tensor_shape_packed) |
| 133 | + odma_names.append(getCustomOp(o_producer).get_nodeattr("instance_name")) |
| 134 | + |
| 135 | + return { |
| 136 | + "idt": idt, |
| 137 | + "idma_names": idma_names, |
| 138 | + "ishape_normal": ishape_normal, |
| 139 | + "ishape_folded": ishape_folded, |
| 140 | + "ishape_packed": ishape_packed, |
| 141 | + "odt": odt, |
| 142 | + "odma_names": odma_names, |
| 143 | + "oshape_normal": oshape_normal, |
| 144 | + "oshape_folded": oshape_folded, |
| 145 | + "oshape_packed": oshape_packed, |
| 146 | + } |
0 commit comments