Exemplo n.º 1
0
    def format(self, node):
        # Separable master config
        params = {}
        params['index'] = node.index
        params['depthwise_config'] = 'config{}_depthwise'.format(node.index)
        params['pointwise_config'] = 'config{}_pointwise'.format(node.index)
        sep_config = self.template.format(**params)

        # Depthwise config
        params = self._default_config_params(node)
        params['n_filt'] = params['n_chan'] # In depthwise step n_chan == n_filt
        params['dilation'] = node.get_attr('dilation', 1)
        params['nzeros'] = node.get_weights('depthwise').nzeros
        params['index'] = str(node.index) + '_depthwise'
        params['weight_t'] = node.get_weights('depthwise').type

        params['config_t'] = 'config{}_depthwise_mult'.format(node.index)
        depthwise_config = self.depthwise_template.format(**params)

        # Depthwise mult config
        mult_params = self._default_config_params(node)
        mult_params['index'] = str(node.index) + '_depthwise'
        mult_params['n_in'] = node.get_attr('n_chan') * node.get_attr('filt_height') * node.get_attr('filt_width')
        mult_params['n_out'] = node.get_attr('n_chan')
        mult_params['weight_t'] = node.get_weights('depthwise').type
        mult_params['product_type'] = get_backend('vivado').product_type(node.get_input_variable().type.precision, node.get_weights('depthwise').type.precision)
        depthwise_mult_config = self.depthwise_mult_template.format(**mult_params)

        # Pointwise config
        params = self._default_config_params(node)
        if node.get_attr('data_format') == 'channels_last':
            params['in_height'] = node.get_output_variable().shape[0]
            params['in_width'] = node.get_output_variable().shape[1]
        else:
            params['in_height'] = node.get_output_variable().shape[1]
            params['in_width'] = node.get_output_variable().shape[2]

        params['filt_height'] = params['filt_width'] = 1
        params['stride_height'] = params['stride_width'] = 1
        params['dilation'] = node.get_attr('dilation', 1)
        params['nzeros'] = node.get_weights('pointwise').nzeros
        params['index'] = str(node.index) + '_pointwise'
        params['weight_t'] = node.get_weights('pointwise').type
        params['min_height'] = params['in_height']
        params['min_width'] = params['in_width']
        params['instructions'] = '0'

        params['config_t'] = 'config{}_pointwise_mult'.format(node.index)
        pointwise_config = self.pointwise_template.format(**params)

        # Pointwise mult config
        mult_params = self._default_config_params(node)
        mult_params['index'] = str(node.index) + '_pointwise'
        mult_params['n_in'] = node.get_attr('n_chan')
        mult_params['n_out'] = node.get_attr('n_filt')
        mult_params['weight_t'] = node.get_weights('pointwise').type
        mult_params['product_type'] = get_backend('vivado').product_type(node.get_input_variable().type.precision, node.get_weights('pointwise').type.precision)
        pointwise_mult_config = self.pointwise_mult_template.format(**mult_params)

        return depthwise_mult_config + '\n' + depthwise_config + '\n' + pointwise_mult_config + '\n' + pointwise_config + '\n' + sep_config
Exemplo n.º 2
0
    def format(self, node):
        params = self._default_config_params(node)
        params['nzeros'] = node.get_weights('weight').nzeros
        params['nonzeros'] = node.get_weights('weight').nonzeros
        params['product_type'] = get_backend('vivado').product_type(node.get_input_variable().type.precision, node.get_weights('weight').type.precision)

        return self.template.format(**params)
Exemplo n.º 3
0
    def format(self, node):
        params = self._default_config_params(node)
        params['n_in'] = node.get_input_variable().size_cpp()
        params['product_type'] = get_backend('quartus').product_type(
            node.get_input_variable().type.precision,
            node.get_weights('scale').type.precision)

        return self.template.format(**params)
Exemplo n.º 4
0
 def format(self, node):
     inp1 = node.get_input_variable(node.inputs[0])
     inp2 = node.get_input_variable(node.inputs[1])
     params = node._default_config_params()
     params['n_out'] = 1
     params['n_in'] = inp1.shape[0]
     params['product_type'] = get_backend('vivado').product_type(inp1.type.precision, inp2.type.precision)
     
     return self.template.format(**params)
Exemplo n.º 5
0
    def format(self, node):
        params = self._default_config_params(node)
        params['dilation'] = node.get_attr('dilation', 1)
        params['nzeros'] = node.get_weights('weight').nzeros

        params['config_t'] = 'config{}_mult'.format(node.index)
        conv_config = self.template.format(**params)

        mult_params = self._default_config_params(node)
        mult_params['n_in'] = node.get_attr('n_chan') * node.get_attr('filt_width')
        mult_params['n_out'] = node.get_attr('n_filt')
        mult_params['product_type'] = get_backend('vivado').product_type(node.get_input_variable().type.precision, node.get_weights('weight').type.precision)
        mult_config = self.mult_template.format(**mult_params)

        return mult_config + '\n' + conv_config
Exemplo n.º 6
0
    def format(self, node):

        params = self._default_config_params(node)

        params['n_in'] = node.get_input_variable().dim_names[1]
        params['n_sequence'] = node.get_input_variable().dim_names[0]
        if node.get_attr('return_sequences'):
            params['n_sequence_out'] = node.get_output_variable().dim_names[0]
            params['n_state'] = node.get_output_variable().dim_names[1]
            params['n_out'] = node.get_output_variable().dim_names[1]
        else:
            params['n_sequence_out'] = 1
            params['n_state'] = node.get_output_variable().dim_names[0]
            params['n_out'] = node.get_output_variable().dim_names[0]
        params['config_mult_t1'] = 'config{}_1'.format(node.index)
        params['config_mult_t2'] = 'config{}_2'.format(node.index)
        params['recr_act_t'] = '{}_config{}_recr'.format(node.get_attr('recurrent_activation'), node.index)
        params['act_t'] = '{}_config{}'.format(node.get_attr('activation'), node.index)
        params['strategy'] = node.get_attr('strategy')
        params['static'] = 'true' if node.attributes['static'] else 'false'
        params['recr_type'] = node.class_name.lower()
        params['RECR_TYPE'] = node.class_name

        if node.class_name=='LSTM':
            n_recr_mult = 4
        else: #GRU
            n_recr_mult = 3

        recr_config = self.template.format(**params)

        act_params = self._default_config_params(node)
        recr_act_params = self._default_config_params(node)

        act_params['type'] = node.get_attr('activation')
        recr_act_params['type'] = node.get_attr('recurrent_activation')
        if node.get_attr('return_sequences'):
            act_params['n_in'] = node.get_output_variable().dim_names[1]
            recr_act_params['n_in'] = node.get_output_variable().dim_names[1] + ' * %i'%(n_recr_mult-1)
        else:
            act_params['n_in'] = node.get_output_variable().dim_names[0]
            recr_act_params['n_in'] = node.get_output_variable().dim_names[0] + ' * %i'%(n_recr_mult-1)

        act_config = self.act_template.format(**act_params)
        recr_act_config = self.recr_act_template.format(**recr_act_params)

        mult_params1 = self._default_config_params(node)
        mult_params2 = self._default_config_params(node)

        mult_params1['n_in'] = node.get_input_variable().dim_names[1]
        if node.get_attr('return_sequences'):
            mult_params1['n_out'] = node.get_output_variable().dim_names[1] + ' * %i'%n_recr_mult
        else:
            mult_params1['n_out'] = node.get_output_variable().dim_names[0] + ' * %i'%n_recr_mult
        mult_params1['product_type'] = get_backend('vivado').product_type(node.get_input_variable().type.precision, node.get_weights('weight').type.precision)
        mult_params1['reuse'] = params['reuse']
        mult_params1['index'] = str(node.index) + '_1'
        mult_params1['nzeros'] = node.get_weights('weight').nzeros
        mult_params1['nonzeros'] = node.get_weights('weight').nonzeros
        if node.get_attr('return_sequences'):
            mult_params2['n_in'] = node.get_output_variable().dim_names[1]
            mult_params2['n_out'] = node.get_output_variable().dim_names[1] + ' * %i'%n_recr_mult
        else:
            mult_params2['n_in'] = node.get_output_variable().dim_names[0]
            mult_params2['n_out'] = node.get_output_variable().dim_names[0] + ' * %i'%n_recr_mult
        mult_params2['product_type'] = get_backend('vivado').product_type(node.get_input_variable().type.precision, node.get_weights('recurrent_weight').type.precision)
        mult_params2['reuse'] = node.attributes['recurrent_reuse_factor']
        mult_params2['index'] = str(node.index) + '_2'
        mult_params2['nzeros'] = node.get_weights('recurrent_weight').nzeros
        mult_params2['nonzeros'] = node.get_weights('recurrent_weight').nonzeros

        mult_config1 = self.mult1_template.format(**mult_params1)
        mult_config2 = self.mult2_template.format(**mult_params2)

        return mult_config1 + '\n' + mult_config2 + '\n' + recr_act_config + '\n' + act_config + '\n' + recr_config