qonnx icon indicating copy to clipboard operation
qonnx copied to clipboard

Add input quantization qkeras converter

Open jurevreca12 opened this issue 1 year ago • 1 comments

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.

jurevreca12 avatar Aug 26 '24 08:08 jurevreca12

Would anyone who is more familiar with the QKeras -> QONNX conversion flow be able to review this? @jmduarte @vloncar @jmitrevs

maltanar avatar Dec 20 '24 08:12 maltanar