fastmachinelearning / qonnx

QONNX: Arbitrary-Precision Quantized Neural Networks in ONNX
https://qonnx.readthedocs.io/
Apache License 2.0
124 stars 39 forks source link

Add input quantization qkeras converter #138

Open jurevreca12 opened 2 months ago

jurevreca12 commented 2 months ago

Updates the QKeras to QONNX converter to also take care of quantizers that are present at the input of the neural network. This handles cases such as the following:

x = x_in = tf.keras.layers.Input(shape=3)
x = qkeras.QActivation(
    qkeras.quantized_bits(bits=4, integer=3, keep_negative=True)  # input quantizer
)(x)
x = qkeras.QDense(
    4,
    kernel_quantizer=qkeras.quantized_bits(
        bits=4, integer=3, keep_negative=True, alpha=[0.5, 0.5, 1, 0.5]
    ),
)(x)
x = qkeras.QActivation(qkeras.quantized_relu(bits=3, integer=3))(x)
x = qkeras.QDense(
    1,
    kernel_quantizer=qkeras.quantized_bits(
        bits=4, integer=3, keep_negative=True, alpha=[0.5]
    ),
)(x)
x = qkeras.QActivation(qkeras.quantized_relu(bits=3, integer=3))(x)
model = tf.keras.Model(inputs=[x_in], outputs=[x])

This defines a 4-bit signed integer as an input. Previous versions of the qkeras2onnx converter would have completely ignored this input quantizer, as the converter works around dense/conv layers.

Changes to the code are handled by a special function _add_input_quantizer in src/qonnx/converters/keras.py.

This PR follow PR: #137 and should be merged after it.

This PR should not affect users that do not use input quantizers as it only changed the output onnx graph if a quantizer is present at any input tensor.