Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input quantization qkeras converter #138

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

jurevreca12
Copy link

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.

jvreca added 19 commits March 22, 2024 15:23
Fixed rounding_mode specifier in convert_quantized_bits
Commented out assertion on non-representability
…ay_and_auto_po2_scale

Merging changes from rounding_mode_new.
…nto HEAD

Merge changes from the new converter
@jurevreca12 jurevreca12 mentioned this pull request Aug 26, 2024
…ed up by the normal converter flow.

Added an extra InferShape transform
@maltanar
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants