|
| 1 | +################################################################################### |
| 2 | +# Copyright (C) 2025, Advanced Micro Devices, Inc. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: BSD-3-Clause |
| 6 | +# |
| 7 | +# Copyright for portions of this file is held by AMD and Microsoft under |
| 8 | +# MIT license as part of project Brainsmith. |
| 9 | +# All other copyright is held by AMD and is provided under BSD-3-Clause license. |
| 10 | +# |
| 11 | +################################################################################### |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import warnings |
| 15 | +from qonnx.core.datatype import DataType |
| 16 | + |
| 17 | +from finn.custom_op.fpgadataflow.hwcustomop import HWCustomOp |
| 18 | + |
| 19 | + |
| 20 | +class Crop(HWCustomOp): |
| 21 | + """Abstraction layer for Crop layers.""" |
| 22 | + |
| 23 | + def __init__(self, onnx_node, **kwargs): |
| 24 | + super().__init__(onnx_node, **kwargs) |
| 25 | + |
| 26 | + def get_nodeattr_types(self): |
| 27 | + my_attrs = { |
| 28 | + "DataType": ("s", True, ""), |
| 29 | + "ImgDim": ("ints", True, []), # [h, w] |
| 30 | + "NumChannels": ("i", True, 0), |
| 31 | + "CropNorth": ("i", True, []), |
| 32 | + "CropSouth": ("i", True, []), |
| 33 | + "CropWest": ("i", True, []), |
| 34 | + "CropEast": ("i", True, []), |
| 35 | + "SIMD": ("i", False, 1), |
| 36 | + "numInputVectors": ("ints", False, []), |
| 37 | + } |
| 38 | + my_attrs.update(super().get_nodeattr_types()) |
| 39 | + return my_attrs |
| 40 | + |
| 41 | + def get_normal_input_shape(self, ind=0): |
| 42 | + num_vec = self.get_nodeattr("numInputVectors") |
| 43 | + h, w = self.get_nodeattr("ImgDim") |
| 44 | + if h == 0: |
| 45 | + img_dim = [w] |
| 46 | + else: |
| 47 | + img_dim = [h, w] |
| 48 | + ch = self.get_nodeattr("NumChannels") |
| 49 | + return num_vec + img_dim + [ch] if num_vec != [0] else img_dim + [ch] |
| 50 | + |
| 51 | + def get_normal_output_shape(self, ind=0): |
| 52 | + num_vec = self.get_nodeattr("numInputVectors") |
| 53 | + height, width = self.get_nodeattr("ImgDim") |
| 54 | + ch = self.get_nodeattr("NumChannels") |
| 55 | + crop_north = self.get_nodeattr("CropNorth") |
| 56 | + crop_east = self.get_nodeattr("CropEast") |
| 57 | + crop_west = self.get_nodeattr("CropWest") |
| 58 | + crop_south = self.get_nodeattr("CropSouth") |
| 59 | + owidth = width - (crop_west + crop_east) |
| 60 | + oheight = height - (crop_north + crop_south) |
| 61 | + if oheight == 0: |
| 62 | + o_img_dim = [owidth] |
| 63 | + else: |
| 64 | + o_img_dim = [oheight, owidth] |
| 65 | + return num_vec + o_img_dim + [ch] if num_vec != [0] else o_img_dim + [ch] |
| 66 | + |
| 67 | + def execute_node(self, context, graph): |
| 68 | + node = self.onnx_node |
| 69 | + h, w = self.get_nodeattr("ImgDim") |
| 70 | + crop_north = self.get_nodeattr("CropNorth") |
| 71 | + crop_east = self.get_nodeattr("CropEast") |
| 72 | + crop_west = self.get_nodeattr("CropWest") |
| 73 | + crop_south = self.get_nodeattr("CropSouth") |
| 74 | + inp = context[node.input[0]] |
| 75 | + if len(inp.shape) == 3: |
| 76 | + cropped_slice = inp[crop_north : h - crop_south, crop_west : w - crop_east, :] |
| 77 | + elif len(inp.shape) == 2: |
| 78 | + cropped_slice = inp[crop_west : w - crop_east, :] |
| 79 | + elif len(inp.shape) == 4: |
| 80 | + cropped_slice = inp[:, crop_north : h - crop_south, crop_west : w - crop_east, :] |
| 81 | + else: |
| 82 | + raise Exception("Crop execute node currently only supports 2D - 4D input tensors.") |
| 83 | + assert cropped_slice.shape == tuple(self.get_normal_output_shape()) |
| 84 | + context[node.output[0]] = cropped_slice |
| 85 | + |
| 86 | + def get_input_datatype(self, ind=0): |
| 87 | + return DataType[self.get_nodeattr("DataType")] |
| 88 | + |
| 89 | + def infer_node_datatype(self, model): |
| 90 | + node = self.onnx_node |
| 91 | + dt = model.get_tensor_datatype(node.input[0]) |
| 92 | + if dt != self.get_input_datatype(): |
| 93 | + warn_str = ( |
| 94 | + f"data_type changing for {node.name}: {str(self.get_input_datatype())} -> {str(dt)}" |
| 95 | + ) |
| 96 | + warnings.warn(warn_str) |
| 97 | + self.set_nodeattr("DataType", dt.name) |
| 98 | + |
| 99 | + def get_instream_width(self, ind=0): |
| 100 | + ibits = self.get_input_datatype().bitwidth() |
| 101 | + simd = self.get_nodeattr("SIMD") |
| 102 | + return ibits * simd |
| 103 | + |
| 104 | + def get_outstream_width(self, ind=0): |
| 105 | + obits = self.get_output_datatype().bitwidth() |
| 106 | + simd = self.get_nodeattr("SIMD") |
| 107 | + return obits * simd |
| 108 | + |
| 109 | + def get_output_datatype(self, ind=0): |
| 110 | + return DataType[self.get_nodeattr("DataType")] |
| 111 | + |
| 112 | + def get_folded_output_shape(self, ind=0): |
| 113 | + normal_oshape = list(self.get_normal_output_shape()) |
| 114 | + simd = self.get_nodeattr("SIMD") |
| 115 | + assert normal_oshape[-1] % simd == 0, "Innermost dimension must be divisible by SIMD" |
| 116 | + fold = int(normal_oshape[-1] / simd) |
| 117 | + folded_oshape = normal_oshape[:-1] + [fold, simd] |
| 118 | + return tuple(folded_oshape) |
| 119 | + |
| 120 | + def get_folded_input_shape(self, ind=0): |
| 121 | + normal_ishape = list(self.get_normal_input_shape()) |
| 122 | + simd = self.get_nodeattr("SIMD") |
| 123 | + assert normal_ishape[-1] % simd == 0, "Innermost dimension must be divisible by SIMD" |
| 124 | + fold = int(normal_ishape[-1] / simd) |
| 125 | + folded_ishape = normal_ishape[:-1] + [fold, simd] |
| 126 | + return tuple(folded_ishape) |
| 127 | + |
| 128 | + def get_exp_cycles(self): |
| 129 | + simd = self.get_nodeattr("SIMD") |
| 130 | + num_vec = self.get_nodeattr("numInputVectors") |
| 131 | + height, width = self.get_nodeattr("ImgDim") |
| 132 | + ch = self.get_nodeattr("NumChannels") |
| 133 | + if height == 0: |
| 134 | + # pretend that height is 1 for code generation |
| 135 | + height = 1 |
| 136 | + |
| 137 | + return ( |
| 138 | + np.prod(num_vec) * height * width * (ch // simd) |
| 139 | + if num_vec != [0] |
| 140 | + else height * width * (ch // simd) |
| 141 | + ) |
0 commit comments