|
| 1 | +from hls4ml.model.layers import Conv1D, Conv2D, SeparableConv1D, SeparableConv2D |
| 2 | +from hls4ml.model.optimizer import OptimizerPass |
| 3 | +from hls4ml.model.types import Source |
| 4 | + |
| 5 | + |
| 6 | +class GenerateConvIm2col(OptimizerPass): |
| 7 | + '''Generates tcode for im2col step of 1D/2d convolution''' |
| 8 | + |
| 9 | + # Note, DepthwizeConv1D/2D also matches because it inherits from Conv1D/2D |
| 10 | + def match(self, node): |
| 11 | + return ( |
| 12 | + isinstance(node, (Conv1D, Conv2D, SeparableConv1D, SeparableConv2D)) |
| 13 | + and node.model.config.get_config_value('IOType') == 'io_parallel' |
| 14 | + ) |
| 15 | + |
| 16 | + def transform(self, model, node): |
| 17 | + node_class = node.class_name |
| 18 | + if 'Separable' in node_class: |
| 19 | + if '1D' in node_class: |
| 20 | + self._generate_separable_im2col_1d(node) |
| 21 | + elif '2D' in node_class: |
| 22 | + self._generate_separable_im2col_2d(node) |
| 23 | + else: |
| 24 | + raise Exception(f'Cannot generate instructions for node {node.name} ({node_class})') |
| 25 | + else: |
| 26 | + if '1D' in node_class: |
| 27 | + self._generate_im2col_1d(node) |
| 28 | + elif '2D' in node_class: |
| 29 | + self._generate_im2col_2d(node) |
| 30 | + else: |
| 31 | + raise Exception(f'Cannot generate instructions for node {node.name} ({node_class})') |
| 32 | + |
| 33 | + def _generate_im2col_1d(self, node): |
| 34 | + code_str = node.model.config.backend.generate_conv1d_line_buffer_fn( |
| 35 | + node.get_attr('index'), |
| 36 | + node.get_attr('n_partitions'), |
| 37 | + node.get_input_variable().shape[0], |
| 38 | + node.get_input_variable().shape[1], |
| 39 | + kernel=node.get_attr('filt_width'), |
| 40 | + stride=node.get_attr('stride_width'), |
| 41 | + pad=(node.get_attr('pad_left'), node.get_attr('pad_right')), |
| 42 | + ) |
| 43 | + |
| 44 | + node.set_attr('line_buffer_codegen', Source(code_str)) |
| 45 | + |
| 46 | + def _generate_im2col_2d(self, node): |
| 47 | + code_str = node.model.config.backend.generate_conv2d_line_buffer_fn( |
| 48 | + node.get_attr('index'), |
| 49 | + node.get_attr('n_partitions'), |
| 50 | + node.get_input_variable().shape[0], |
| 51 | + node.get_input_variable().shape[1], |
| 52 | + node.get_input_variable().shape[2], |
| 53 | + kernel=(node.get_attr('filt_height'), node.get_attr('filt_width')), |
| 54 | + stride=(node.get_attr('stride_height'), node.get_attr('stride_width')), |
| 55 | + pad=( |
| 56 | + node.get_attr('pad_top'), |
| 57 | + node.get_attr('pad_bottom'), |
| 58 | + node.get_attr('pad_left'), |
| 59 | + node.get_attr('pad_right'), |
| 60 | + ), |
| 61 | + ) |
| 62 | + |
| 63 | + node.set_attr('line_buffer_codegen', Source(code_str)) |
| 64 | + |
| 65 | + def _generate_separable_im2col_1d(self, node): |
| 66 | + dw_code_str = node.model.config.backend.generate_conv1d_line_buffer_fn( |
| 67 | + str(node.get_attr('index')) + '_dw', |
| 68 | + node.get_attr('n_partitions'), |
| 69 | + node.get_input_variable().shape[0], |
| 70 | + node.get_input_variable().shape[1], |
| 71 | + kernel=node.get_attr('filt_width'), |
| 72 | + stride=node.get_attr('stride_width'), |
| 73 | + pad=(node.get_attr('pad_left'), node.get_attr('pad_right')), |
| 74 | + ) |
| 75 | + |
| 76 | + node.set_attr('dw_line_buffer_codegen', Source(dw_code_str)) |
| 77 | + |
| 78 | + pw_code_str = node.model.config.backend.generate_conv1d_line_buffer_fn( |
| 79 | + str(node.get_attr('index')) + '_pw', |
| 80 | + node.get_attr('n_partitions'), |
| 81 | + node.get_output_variable().shape[0], |
| 82 | + node.get_input_variable().shape[1], |
| 83 | + kernel=1, |
| 84 | + ) |
| 85 | + |
| 86 | + node.set_attr('pw_line_buffer_codegen', Source(pw_code_str)) |
| 87 | + |
| 88 | + def _generate_separable_im2col_2d(self, node): |
| 89 | + dw_code_str = node.model.config.backend.generate_conv2d_line_buffer_fn( |
| 90 | + str(node.get_attr('index')) + '_dw', |
| 91 | + node.get_attr('n_partitions'), |
| 92 | + node.get_input_variable().shape[0], |
| 93 | + node.get_input_variable().shape[1], |
| 94 | + node.get_input_variable().shape[2], |
| 95 | + kernel=(node.get_attr('filt_height'), node.get_attr('filt_width')), |
| 96 | + stride=(node.get_attr('stride_height'), node.get_attr('stride_width')), |
| 97 | + pad=( |
| 98 | + node.get_attr('pad_top'), |
| 99 | + node.get_attr('pad_bottom'), |
| 100 | + node.get_attr('pad_left'), |
| 101 | + node.get_attr('pad_right'), |
| 102 | + ), |
| 103 | + ) |
| 104 | + |
| 105 | + node.set_attr('dw_line_buffer_codegen', Source(dw_code_str)) |
| 106 | + |
| 107 | + pw_code_str = node.model.config.backend.generate_conv2d_line_buffer_fn( |
| 108 | + str(node.get_attr('index')) + '_pw', |
| 109 | + node.get_attr('n_partitions'), |
| 110 | + node.get_output_variable().shape[0], |
| 111 | + node.get_output_variable().shape[1], |
| 112 | + node.get_input_variable().shape[2], |
| 113 | + kernel=(1, 1), |
| 114 | + ) |
| 115 | + |
| 116 | + node.set_attr('pw_line_buffer_codegen', Source(pw_code_str)) |
0 commit comments