|
| 1 | +from math import ceil |
| 2 | + |
| 3 | +from hls4ml.backends.backend import get_backend |
| 4 | +from hls4ml.backends.template import FunctionCallTemplate, LayerConfigTemplate |
| 5 | +from hls4ml.model.layers import Einsum |
| 6 | +from hls4ml.utils.transpose_utils import transpose_config_gen |
| 7 | + |
| 8 | +from .reshaping_templates import transpose_config_template |
| 9 | + |
| 10 | +# Shared Dense template |
| 11 | +# Einsum template |
| 12 | + |
| 13 | +einsum_config_template = ''' |
| 14 | +struct config{index} {{ |
| 15 | + typedef config{index}_tpose_inp0 tpose_inp0_config; |
| 16 | + typedef config{index}_tpose_inp1 tpose_inp1_config; |
| 17 | + typedef config{index}_tpose_out tpose_out_conf; |
| 18 | +
|
| 19 | + typedef {accum_t.name} accum_t; |
| 20 | +
|
| 21 | + // Layer Sizes |
| 22 | + static const unsigned n_free0 = {n_free0}; |
| 23 | + static const unsigned n_free1 = {n_free1}; |
| 24 | + static const unsigned n_contract = {n_contract}; |
| 25 | + static const unsigned n_inplace = {n_inplace}; |
| 26 | +
|
| 27 | + // Resource reuse info |
| 28 | + static const unsigned io_type = nnet::{iotype}; |
| 29 | + static const unsigned strategy = nnet::{strategy}; |
| 30 | + static const unsigned reuse_factor = {reuse_factor}; |
| 31 | + static const unsigned multiplier_limit = {multiplier_limit}; |
| 32 | + static const bool store_weights_in_bram = false; // NOT USED |
| 33 | +
|
| 34 | + template <class x_T, class y_T> |
| 35 | + using product = nnet::product::{product_type}<x_T, y_T>; |
| 36 | +}}; |
| 37 | +''' |
| 38 | + |
| 39 | +einsum_function_template = 'nnet::einsum<{input0_t}, {input1_t}, {output_t}, {config}>({input0}, {input1}, {output});' |
| 40 | + |
| 41 | +einsum_include_list = ['nnet_utils/nnet_einsum.h'] |
| 42 | + |
| 43 | + |
| 44 | +class EinsumConfigTemplate(LayerConfigTemplate): |
| 45 | + def __init__(self): |
| 46 | + super().__init__(Einsum) |
| 47 | + self.template = einsum_config_template |
| 48 | + |
| 49 | + def format(self, node: Einsum): |
| 50 | + default_params = self._default_config_params(node) |
| 51 | + |
| 52 | + strategy = node.attributes['strategy'] |
| 53 | + io_type = node.model.config.get_config_value('IOType') |
| 54 | + |
| 55 | + assert io_type == 'io_parallel', 'EinsumDense layer only supports io_parallel for now' |
| 56 | + assert strategy.lower() == 'latency', 'EinsumDense layer only supports Latency strategy for now' |
| 57 | + |
| 58 | + # EinsumDense config |
| 59 | + params = default_params.copy() |
| 60 | + params['strategy'] = strategy |
| 61 | + params['n_free0'] = node.attributes['n_free0'] |
| 62 | + params['n_free1'] = node.attributes['n_free1'] |
| 63 | + params['n_contract'] = node.attributes['n_contract'] |
| 64 | + params['n_inplace'] = node.attributes['n_inplace'] |
| 65 | + inp0_t = node.get_input_variable(node.inputs[0]).type.precision |
| 66 | + inp1_t = node.get_input_variable(node.inputs[1]).type.precision |
| 67 | + params['product_type'] = get_backend('vivado').product_type(inp0_t, inp1_t) |
| 68 | + |
| 69 | + total_mults = params['n_free0'] * params['n_free1'] * params['n_contract'] * params['n_inplace'] |
| 70 | + params['multiplier_limit'] = ceil(total_mults / params['reuse_factor']) |
| 71 | + |
| 72 | + einsum_conf = self.template.format(**params) |
| 73 | + |
| 74 | + # inp/out transpose config |
| 75 | + inp0_shape = node.attributes['inp0_shape'] |
| 76 | + inp1_shape = node.attributes['inp1_shape'] |
| 77 | + out_interpert_shape = node.attributes['out_interpert_shape'] |
| 78 | + inp0_tpose_idxs = node.attributes['inp0_tpose_idxs'] |
| 79 | + inp1_tpose_idxs = node.attributes['inp1_tpose_idxs'] |
| 80 | + out_tpose_idxs = node.attributes['out_tpose_idxs'] |
| 81 | + tpose_inp0_config_name = f'config{node.index}_tpose_inp0' |
| 82 | + tpose_inp1_config_name = f'config{node.index}_tpose_inp1' |
| 83 | + tpose_out_conf_name = f'config{node.index}_tpose_out' |
| 84 | + |
| 85 | + conf = transpose_config_gen(tpose_inp0_config_name, inp0_shape, inp0_tpose_idxs) |
| 86 | + inp0_tpose_conf = transpose_config_template.format(**conf) |
| 87 | + conf = transpose_config_gen(tpose_inp1_config_name, inp1_shape, inp1_tpose_idxs) |
| 88 | + inp1_tpose_conf = transpose_config_template.format(**conf) |
| 89 | + conf = transpose_config_gen(tpose_out_conf_name, out_interpert_shape, out_tpose_idxs) |
| 90 | + out_tpose_conf = transpose_config_template.format(**conf) |
| 91 | + |
| 92 | + return '\n\n'.join((inp0_tpose_conf, inp1_tpose_conf, out_tpose_conf, einsum_conf)) |
| 93 | + |
| 94 | + |
| 95 | +class EinsumFunctionTemplate(FunctionCallTemplate): |
| 96 | + def __init__(self): |
| 97 | + super().__init__(Einsum, include_header=einsum_include_list) |
| 98 | + self.template = einsum_function_template |
| 99 | + |
| 100 | + def format(self, node: Einsum): |
| 101 | + params = {} |
| 102 | + params['config'] = f'config{node.index}' |
| 103 | + params['input0_t'] = node.get_input_variable(node.inputs[0]).type.name |
| 104 | + params['input1_t'] = node.get_input_variable(node.inputs[1]).type.name |
| 105 | + params['output_t'] = node.get_output_variable().type.name |
| 106 | + params['input0'] = node.get_input_variable(node.inputs[0]).name |
| 107 | + params['input1'] = node.get_input_variable(node.inputs[1]).name |
| 108 | + params['output'] = node.get_output_variable().name |
| 109 | + return self.template.format(**params) |
0 commit comments