Example #1
0
    def get_attrs(self, node):
        rank = len(self.get_value_info_shape(node.input[0]))
        nspatial_dim = (rank - 2)

        from furiosa_sdk_quantizer.frontend.onnx.quantizer.utils import attribute_to_kwargs
        attrs = attribute_to_kwargs(node.attribute)
        ceil_mode = attrs.get('ceil_mode', 0)
        dilations = attrs.get('dilations', [
            1,
        ] * nspatial_dim)
        kernel_shape = attrs['kernel_shape']
        strides = attrs.get('strides', [
            1,
        ] * nspatial_dim)
        pads = attrs.get('pads', [
            0,
        ] * nspatial_dim * 2)

        return {
            'ceil_mode': ceil_mode,
            'dilations': dilations,
            'kernel_shape': kernel_shape,
            'pads': pads,
            'strides': strides
        }
Example #2
0
    def _quantize_pad_constant(self, node):
        mode = attribute_to_kwargs(node.attribute)['mode']

        if mode != b'constant':
            return

        try:
            w_init = self.initializer[node.input[2]]
        except IndexError:
            name = '%s_constant_value' % node.input[0]
            node.input.append(name)

            vi = make_tensor_value_info(name=name,
                                        elem_type=onnx.TensorProto.FLOAT,
                                        shape=[])
            self.model.graph.input.append(vi)
            self.value_info.update({name: vi})

            w_init = make_tensor(name=name,
                                 data_type=onnx.TensorProto.FLOAT,
                                 dims=[],
                                 vals=[float(0)])
            self.model.graph.initializer.append(w_init)
            self.initializer.update({name: w_init})

        self._quantize_weight_per_layer(w_init)
    def get_attrs(self, node):
        from furiosa_sdk_quantizer.frontend.onnx.quantizer.utils import attribute_to_kwargs
        attrs = attribute_to_kwargs(node.attribute)

        if node.op_type == 'ReduceL1':
            p = 1
        elif node.op_type == 'ReduceL2':
            p = 2
        else:
            raise Exception()

        return {'axis': int(attrs['axes'][0]), 'p': int(p)}
Example #4
0
    def get_attrs(self, mid_node):
        from furiosa_sdk_quantizer.frontend.onnx.quantizer.utils import attribute_to_kwargs
        attrs = attribute_to_kwargs(mid_node.attribute)
        dilations = attrs.get('dilations', [1])
        group = attrs.get('group', 1)
        kernel_shape = attrs['kernel_shape']
        pads = attrs.get('pads', [0, 0])
        strides = attrs.get('strides', [1])

        return {'dilations': [*dilations, 1],
                'group': group,
                'kernel_shape': [*kernel_shape, 1],
                'pads': [pads[0], 0, pads[1], 0],
                'strides': [strides[0], 1]}
Example #5
0
    def get_bn_params(self, node):
        scale = self.get_initializer_array(node.input[1])
        if all(v == 0. for v in scale):
            warnings.warn(f'BatchNormalization.scale is a zero tensor: {node.input[1]}')

        B = self.get_initializer_array(node.input[2])
        mean = self.get_initializer_array(node.input[3])
        var = self.get_initializer_array(node.input[4])

        from furiosa_sdk_quantizer.frontend.onnx.quantizer.utils import attribute_to_kwargs
        attrs = attribute_to_kwargs(node.attribute)
        eps = attrs.get('epsilon', 1e-05)

        return scale, B, mean, var, eps
Example #6
0
    def _stack_quant_node(
            self,
            inputs: List[str],
            outputs: List[str],
            op_type: str,
            attributes: Optional[List[onnx.AttributeProto]] = None) -> None:

        # make quantized node proto
        attr_kwargs = {}
        if attributes:
            attr_kwargs = attribute_to_kwargs(attributes)

        quant_node = make_node(op_type, inputs, outputs, **attr_kwargs)

        # stack quantized node
        self._quant_node.update({outputs[0]: quant_node})
Example #7
0
    def _make_integer_arithmetic_operator(node,
                                          node_i0,
                                          node_i1,
                                          node_o0,
                                          node_i2=None):
        quant_op_type = 'QLinear%s' % node.op_type

        quant_inputs = [*node_i0.input, *node_i1.input, *node_o0.input[1:]]

        if node_i2:
            quant_inputs.append(node_i2.input[0])

        quant_outputs = [node_o0.output[0]]

        attributes = node.attribute
        attr_kwargs = {}
        if attributes:
            attr_kwargs = attribute_to_kwargs(attributes)

        quant_node = make_node(quant_op_type, quant_inputs, quant_outputs,
                               **attr_kwargs)

        return quant_node
Example #8
0
    def get_pad_mode(self, node_attr):
        from furiosa_sdk_quantizer.frontend.onnx.quantizer.utils import attribute_to_kwargs

        return attribute_to_kwargs(node_attr).get('mode',
                                                  'constant').decode("utf-8")