-
Notifications
You must be signed in to change notification settings - Fork 35
QuantConv2D binarized activations with tf.int32 bitpacked output #611
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
b8b94f0
added function strip_lcedequantize_ops:
simonmaurer fd8f9d0
reformatted using black code style
simonmaurer bfa5590
added pytest module for verifying lce_dequantize_ops
simonmaurer 658e148
fixed larq import errors and renamed unit test function
simonmaurer f860479
fix PyFlakes error due to typo when defining toy_model
simonmaurer 46c0071
using Interpreter from larq_compute_engine.tflite.python.interpreter …
simonmaurer e50143f
reformatted strip_lcedequantize_test.py using black code style
simonmaurer 3be2a3a
added function strip_lcedequantize_ops:
simonmaurer 59d814f
reformatted using black code style
simonmaurer 17e6b46
added pytest module for verifying lce_dequantize_ops
simonmaurer 8b875f5
fixed larq import errors and renamed unit test function
simonmaurer dda14d3
fix PyFlakes error due to typo when defining toy_model
simonmaurer 8c572fa
using Interpreter from larq_compute_engine.tflite.python.interpreter …
simonmaurer 49a9877
reformatted strip_lcedequantize_test.py using black code style
simonmaurer be9b46e
Remove dependency of compute engine interpreter
lgeiger 559ca80
Add bazel target for dequantize test
lgeiger 5230f06
Update strip_lcedequantize_test.py
simonmaurer 79c69dd
Update strip_lcedequantize_test.py
simonmaurer 7cf93f8
Update strip_lcedequantize_test.py
simonmaurer 9b986f1
fixed merge conflict in strip_lcedequantize_test.py
simonmaurer f2ef72c
fix: accidentally added merge indicators
simonmaurer 5e31d80
Update strip_lcedequantize_test.py
simonmaurer 8b518a6
Update strip_lcedequantize_test.py
simonmaurer bafc8d6
Adapt unit test for output type checking
simonmaurer 9e6a268
Update strip_lcedequantize_test.py
simonmaurer ecff8d3
set tf.float32 as parametrized input type
simonmaurer 2c45ed1
Updated strip_lcedequantize_ops() to support more models:
simonmaurer 9a24a91
Unit tests for tf.int8 input/output models
simonmaurer 77ee842
Correction in toy_model_int8_sign
simonmaurer cc44059
Extended Unit tests for test_strip_lcedequantize_ops() to parametrize…
simonmaurer c683131
Clean up using black code style
simonmaurer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import sys | ||
|
||
import larq as lq | ||
import pytest | ||
import tensorflow as tf | ||
|
||
from larq_compute_engine.mlir.python.converter import convert_keras_model | ||
from larq_compute_engine.mlir.python.util import strip_lcedequantize_ops | ||
|
||
|
||
def toy_model_sign(**kwargs): | ||
img = tf.keras.layers.Input(shape=(224, 224, 3)) | ||
x = lq.layers.QuantConv2D( | ||
256, | ||
kernel_size=3, | ||
strides=1, | ||
padding="same", | ||
pad_values=1, | ||
input_quantizer="ste_sign", | ||
kernel_quantizer="ste_sign", | ||
kernel_constraint="weight_clip", | ||
)(img) | ||
x = lq.quantizers.SteSign()(x) | ||
return tf.keras.Model(inputs=img, outputs=x) | ||
|
||
|
||
def quant(x): | ||
return tf.quantization.fake_quant_with_min_max_vars(x, -3.0, 3.0) | ||
|
||
|
||
def toy_model_int8_sign(**kwargs): | ||
img = tf.keras.layers.Input(shape=(224, 224, 3)) | ||
x = quant(img) | ||
x = lq.layers.QuantConv2D( | ||
256, | ||
kernel_size=3, | ||
strides=1, | ||
padding="same", | ||
pad_values=1, | ||
input_quantizer="ste_sign", | ||
kernel_quantizer="ste_sign", | ||
kernel_constraint="weight_clip", | ||
)(img) | ||
x = lq.quantizers.SteSign()(x) | ||
x = quant(x) | ||
return tf.keras.Model(inputs=img, outputs=x) | ||
|
||
|
||
@pytest.mark.parametrize("model_cls", [toy_model_sign, toy_model_int8_sign]) | ||
@pytest.mark.parametrize("inference_input_type", [tf.int8, tf.float32]) | ||
@pytest.mark.parametrize("inference_output_type", [tf.int8, tf.float32]) | ||
def test_strip_lcedequantize_ops( | ||
model_cls, inference_input_type, inference_output_type | ||
): | ||
model_lce = convert_keras_model( | ||
model_cls(), | ||
inference_input_type=inference_input_type, | ||
inference_output_type=inference_output_type, | ||
experimental_default_int8_range=None, | ||
experimental_enable_bitpacked_activations=True, | ||
) | ||
model_lce = strip_lcedequantize_ops(model_lce) | ||
interpreter = tf.lite.Interpreter(model_content=model_lce) | ||
input_details = interpreter.get_input_details() | ||
assert len(input_details) == 1 | ||
assert input_details[0]["dtype"] == inference_input_type.as_numpy_dtype | ||
output_details = interpreter.get_output_details() | ||
assert len(output_details) == 1 | ||
if inference_output_type == tf.float32: | ||
assert output_details[0]["dtype"] == tf.int32.as_numpy_dtype | ||
else: | ||
assert output_details[0]["dtype"] == inference_output_type.as_numpy_dtype | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main([__file__, "-s"])) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean if output inference type is
tf.int8
the dequantize op is not removed?@simonmaurer Is this intended behaviour?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this is intended behavior.
as discussed here:
and when looking at the graph of
tf.float32/tf.int8
models there are onlyLceDequantize
ops when the model hastf.float32
outputs.in other words the
LceDequantize
ops are removed (resulting in the desired tf.int32 outputs) iff the model hastf.float32
outputsUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually it shouldn't differ..as long as there are
LceDequantize
ops as outputs they should be removed to gettf.int32
outputs, no matter whattf.float32/tf.int8
ops are inside the modelsince you've pointed this out: I've removed this check for
tf.int8
models in the unittest function with the last commit, but actually it could (or should) still be in there. not sure though why the MLIR test did not go through fortf.int8
output models..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MLIR failed again for the assertion tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might also want to apply similar changes as done in #635 to this PR, but I don't think this will fix CI either. Looks like the dequantize node is not correctly removed in your example.