|
| 1 | +from hls4ml.backends.backend import get_backend |
| 2 | +from hls4ml.model.layers import GRU |
| 3 | +from hls4ml.backends.template import LayerConfigTemplate, FunctionCallTemplate |
| 4 | + |
| 5 | +recurrent_include_list = ['nnet_utils/nnet_recurrent.h', 'nnet_utils/nnet_recurrent_stream.h'] |
| 6 | + |
| 7 | +# Shared Matrix Multiplication Template (Dense) |
| 8 | +recr_mult_config_template = '''struct config{index}_mult : nnet::dense_config {{ |
| 9 | + static const unsigned n_in = {n_in}; |
| 10 | + static const unsigned n_out = {n_out}; |
| 11 | + |
| 12 | + static const unsigned rf_pad = {rfpad}; |
| 13 | + static const unsigned bf_pad = {bfpad}; |
| 14 | + static const unsigned reuse_factor = {reuse}; |
| 15 | + static const unsigned reuse_factor_rounded = reuse_factor + rf_pad; |
| 16 | + static const unsigned block_factor = DIV_ROUNDUP(n_in*n_out, reuse_factor); |
| 17 | + static const unsigned block_factor_rounded = block_factor + bf_pad; |
| 18 | + static const unsigned multiplier_factor = MIN(n_in, reuse_factor); |
| 19 | + static const unsigned multiplier_limit = DIV_ROUNDUP(n_in*n_out, multiplier_factor); |
| 20 | + static const unsigned multiplier_scale = multiplier_limit/n_out; |
| 21 | + typedef {accum_t.name} accum_t; |
| 22 | + typedef {bias_t.name} bias_t; |
| 23 | + typedef {weight_t.name} weight_t; |
| 24 | + |
| 25 | + template<class x_T, class y_T> |
| 26 | + using product = nnet::product::{product_type}<x_T, y_T>; |
| 27 | +}};\n''' |
| 28 | + |
| 29 | +# Activation Template |
| 30 | +activ_config_template = '''struct {type}_config{index} : nnet::activ_config {{ |
| 31 | + static const unsigned n_in = {n_in}; |
| 32 | + static const unsigned table_size = {table_size}; |
| 33 | + static const unsigned io_type = nnet::{iotype}; |
| 34 | + static const unsigned reuse_factor = {reuse}; |
| 35 | +}};\n''' |
| 36 | + |
| 37 | +# GRU Template |
| 38 | +gru_config_template = '''struct config{index} : nnet::gru_config {{ |
| 39 | + static const unsigned n_in = {n_in}; |
| 40 | + static const unsigned n_out = {n_out}; |
| 41 | + static const unsigned n_units = {n_units}; |
| 42 | + static const unsigned n_timesteps = {n_timesteps}; |
| 43 | + static const unsigned n_outputs = {n_outputs}; |
| 44 | + static const bool return_sequences = {return_sequences}; |
| 45 | + |
| 46 | + typedef {accum_t.name} accum_t; |
| 47 | + typedef {weight_t.name} weight_t; |
| 48 | + typedef {bias_t.name} bias_t; |
| 49 | + |
| 50 | + typedef {config_mult_x} mult_config_x; |
| 51 | + typedef {config_mult_h} mult_config_h; |
| 52 | + |
| 53 | + typedef {act_t} ACT_CONFIG_T; |
| 54 | + template<class x_T, class y_T, class config_T> |
| 55 | + using activation = nnet::activation::{activation}<x_T, y_T, config_T>; |
| 56 | +
|
| 57 | + typedef {act_recurrent_t} ACT_CONFIG_RECURRENT_T; |
| 58 | + template<class x_T, class y_T, class config_T> |
| 59 | + using activation_recr = nnet::activation::{recurrent_activation}<x_T, y_T, config_T>; |
| 60 | + |
| 61 | + static const unsigned reuse_factor = {reuse}; |
| 62 | + static const bool store_weights_in_bram = false; |
| 63 | +}};\n''' |
| 64 | + |
| 65 | +gru_function_template = 'nnet::gru<{input_t}, {output_t}, {config}>({input}, {output}, {w}, {wr}, {b}, {br});' |
| 66 | + |
| 67 | +class GRUConfigTemplate(LayerConfigTemplate): |
| 68 | + def __init__(self): |
| 69 | + super().__init__(GRU) |
| 70 | + self.gru_template = gru_config_template |
| 71 | + self.act_template = activ_config_template |
| 72 | + self.recr_act_template = activ_config_template |
| 73 | + self.mult_x_template = recr_mult_config_template |
| 74 | + self.mult_h_template = recr_mult_config_template |
| 75 | + |
| 76 | + def format(self, node): |
| 77 | + # Input has shape (n_timesteps, inp_dimensionality) |
| 78 | + # Output / hidden units has shape (1 if !return_sequences else n_timesteps , n_units) |
| 79 | + params = self._default_config_params(node) |
| 80 | + params['n_units'] = node.get_attr('n_out') |
| 81 | + params['n_outputs'] = node.get_attr('n_timesteps') if node.get_attr('return_sequences', False) else '1' |
| 82 | + params['return_sequences'] ='true' if node.get_attr('return_sequences', False) else 'false' |
| 83 | + params['config_mult_x'] = 'config{}_x_mult'.format(node.index) |
| 84 | + params['config_mult_h'] = 'config{}_h_mult'.format(node.index) |
| 85 | + params['act_t'] = '{}_config{}'.format(node.get_attr('activation'), str(node.index) + '_act') |
| 86 | + params['act_recurrent_t'] = '{}_config{}'.format(node.get_attr('recurrent_activation'), str(node.index) + '_rec_act') |
| 87 | + gru_config = self.gru_template.format(**params) |
| 88 | + |
| 89 | + # Activation is on candidate hidden state, dimensionality (1, n_units) |
| 90 | + act_params = self._default_config_params(node) |
| 91 | + act_params['type'] = node.get_attr('activation') |
| 92 | + act_params['n_in'] = node.get_attr('n_out') |
| 93 | + act_params['index'] = str(node.index) + '_act' |
| 94 | + act_config = self.act_template.format(**act_params) |
| 95 | + |
| 96 | + # Recurrent activation is on reset and update gates (therefore x2), dimensionality (1, n_units) |
| 97 | + recr_act_params = self._default_config_params(node) |
| 98 | + recr_act_params['type'] = node.get_attr('recurrent_activation') |
| 99 | + recr_act_params['n_in'] = str(node.get_attr('n_out')) + ' * 2' |
| 100 | + recr_act_params['index'] = str(node.index) + '_rec_act' |
| 101 | + recr_act_config = self.recr_act_template.format(**recr_act_params) |
| 102 | + |
| 103 | + # Multiplication config for matrix multiplications of type Wx (reset, update and candidate states) |
| 104 | + mult_params_x = self._default_config_params(node) |
| 105 | + mult_params_x['n_in'] = node.get_attr('n_in') |
| 106 | + mult_params_x['n_out'] = str(node.get_attr('n_out')) + ' * 3' |
| 107 | + mult_params_x['product_type'] = get_backend('quartus').product_type(node.get_input_variable().type.precision, node.get_weights('weight').type.precision) |
| 108 | + mult_params_x['index'] = str(node.index) + '_x' |
| 109 | + mult_config_x = self.mult_x_template.format(**mult_params_x) |
| 110 | + |
| 111 | + # Multiplication config for matrix multiplications of type Wh (reset, update and candidate states) |
| 112 | + mult_params_h = self._default_config_params(node) |
| 113 | + mult_params_h['n_in'] = node.get_attr('n_out') |
| 114 | + mult_params_h['n_out'] = str(node.get_attr('n_out')) + ' * 3' |
| 115 | + mult_params_h['reuse_factor'] = params['recurrent_reuse_factor'] |
| 116 | + mult_params_h['product_type'] = get_backend('quartus').product_type(node.get_input_variable().type.precision, node.get_weights('recurrent_weight').type.precision) |
| 117 | + mult_params_h['index'] = str(node.index) + '_h' |
| 118 | + mult_config_h = self.mult_h_template.format(**mult_params_h) |
| 119 | + |
| 120 | + return mult_config_x + '\n' + mult_config_h + '\n' + recr_act_config + '\n' + act_config + '\n' + gru_config |
| 121 | + |
| 122 | +class GRUFunctionTemplate(FunctionCallTemplate): |
| 123 | + def __init__(self): |
| 124 | + super().__init__(GRU, include_header=recurrent_include_list) |
| 125 | + self.template = gru_function_template |
| 126 | + |
| 127 | + def format(self, node): |
| 128 | + params = self._default_function_params(node) |
| 129 | + params['w'] = node.get_weights('weight').name |
| 130 | + params['b'] = node.get_weights('bias').name |
| 131 | + params['wr'] = node.get_weights('recurrent_weight').name |
| 132 | + params['br'] = node.get_weights('recurrent_bias').name |
| 133 | + return self.template.format(**params) |
0 commit comments