larq / larq/compute-engine

Int8 default ranges break when a bconv is followed by a normal conv.

Open
#421 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
258
Forks
35
PR merge metrics
No merged PRs in 30d

Description

Observed behaviour

When converting this model...

model = tf.keras.models.Sequential([
    tf.keras.Input((32, 32, 3)),
    lq.layers.QuantConv2D(
        32,
        (3, 3),
        input_quantizer="ste_sign",
        kernel_quantizer="ste_sign",
        padding="same",
        pad_values=1.0,
        use_bias=False
    ),
    tf.keras.layers.Conv2D(32, (3, 3)),
])
converted_model = lce.convert_keras_model(model, experimental_default_int8_range=(-3, 3))

...we obtain the following converted model, with extra dequantise and quantise nodes around the Conv2D:

image

Expected behaviour

We expect there to be no dequantise or quantise nodes in a converted model when the experimental_default_int8_range argument is used.

If the QuantConv2D is replaced by a normal Conv2D we get:

model = tf.keras.models.Sequential([
    tf.keras.Input((32, 32, 3)),
    tf.keras.layers.Conv2D(
        32, (3, 3), padding="same", use_bias=False
    ),
    tf.keras.layers.Conv2D(32, (3, 3)),
])
converted_model = lce.convert_keras_model(model, experimental_default_int8_range=(-3, 3))
image

Similarly, if the Conv2D is replaced with a QuantConv2D we get:

model = tf.keras.models.Sequential([
    tf.keras.Input((32, 32, 3)),
    lq.layers.QuantConv2D(
        32,
        (3, 3),
        input_quantizer="ste_sign",
        kernel_quantizer="ste_sign",
        padding="same",
        pad_values=1.0,
        use_bias=False
    ),
    lq.layers.QuantConv2D(
        32,
        (3, 3),
        input_quantizer="ste_sign",
        kernel_quantizer="ste_sign",
        padding="same",
        pad_values=1.0,
        use_bias=False
    ),
])
converted_model = lce.convert_keras_model(model, experimental_default_int8_range=(-3, 3))
image

So there is something specifically going wrong with the QuantConv2D > Conv2D combination.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the Sequential models shown and lce.convert_keras_model using experimental_default_int8_range=(-3, 3), then compare the converted graphs for QuantConv2D followed by Conv2D. Trace the conversion path for this layer combination; done means the mixed model no longer contains the extra dequantise and quantise nodes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, keras, python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.