model-optimization icon indicating copy to clipboard operation
model-optimization copied to clipboard

Support for Conv1D Quantization

Open madarax64 opened this issue 5 years ago • 31 comments

Hello, It seems as though Conv1D layer quantization is not yet supported? I get an error while trying to quantize a model containing Conv1D layers..is this going to be supported at some point?

madarax64 avatar Apr 15 '20 19:04 madarax64

@nutsiepully : would you be able to add this?

alanchiao avatar Apr 16 '20 23:04 alanchiao

Sure. I'll take a look.

nutsiepully avatar Apr 28 '20 06:04 nutsiepully

Hello @alanchiao @nutsiepully , I wanted to ask if there was any progress on this? Thanks

madarax64 avatar Jun 24 '20 23:06 madarax64

Hi @madarax64,

Haven't started working on this yet. We are planning another release in the next 1-2 months and this should be part of it.

nutsiepully avatar Jun 24 '20 23:06 nutsiepully

Hi @nutsiepully, Okay, great. Thanks for the prompt response. Closing this since it should be GA by the next release. Thanks!

madarax64 avatar Jun 25 '20 05:06 madarax64

You're welcome.

I'm just reopening it to make sure it stays open until I actually fix it, and so I don't forget :)

nutsiepully avatar Jun 25 '20 06:06 nutsiepully

@nutsiepully Haha, okay :)

madarax64 avatar Jun 25 '20 20:06 madarax64

Hi @nutsiepully I just noticed there's a new release out (v0.4.0). Has this been fixed in this new release? Thanks

madarax64 avatar Jul 31 '20 06:07 madarax64

Hi @nutsiepully I'm also interested in Conv1D and SeparableConv1D. Thanks!

marno1d avatar Sep 13 '20 01:09 marno1d

Hi @marno1d,

I just pushed a new release yesterday which has support for SeparableConv1D. Full support for Conv1D is still pending, but you can do some work around it by using custom quantization.

nutsiepully avatar Sep 14 '20 20:09 nutsiepully

Hi @nutsiepully, Great work! Can you provide an example of this please? Thanks

madarax64 avatar Sep 18 '20 15:09 madarax64

https://github.com/tensorflow/model-optimization/blob/48c08d13629ff062ce1720d53a035bbfa0331b83/tensorflow_model_optimization/python/core/quantization/keras/default_8bit/quantize_numerical_test.py#L112

https://github.com/tensorflow/model-optimization/blob/48c08d13629ff062ce1720d53a035bbfa0331b83/tensorflow_model_optimization/python/core/quantization/keras/default_8bit/quantize_numerical_test.py#L135

nutsiepully avatar Sep 22 '20 17:09 nutsiepully

Hi @marno1d,

I just pushed a new release yesterday which has support for SeparableConv1D. Full support for Conv1D is still pending, but you can do some work around it by using custom quantization.

Hi @nutsiepully , could you elaborate on the workaround for Conv1D? Also is there any, at least approximate, ETA for the Conv1D support?

PiotrowskiD avatar Sep 29 '20 16:09 PiotrowskiD

Adding support for Conv1D standalone should be fairly simple. Conv1D internally uses the Conv2D op which is basically supported through training and conversion. You just need to write a test to verify the numerics, and also update the registry entry for it.

Conv1D with BatchNorm and that handling would need some more work - The transforms would need to be updated to match Conv1D and ensure the FQ placement is correct through conversion.

I haven't found the time to dig into it further. Happy to review a PR though.

nutsiepully avatar Sep 29 '20 21:09 nutsiepully

Does the "workaround" for Conv1D involve defining a subclass of QuantizationConfig as explained in this guide?

https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide#experiment_with_quantization

jennakwon06 avatar Oct 01 '20 04:10 jennakwon06

@jennakwon06 - Yes. For Conv1D, it should be the same as the Conv2D option. Internally there's just a conv2d op, no conv1d op.

nutsiepully avatar Oct 01 '20 05:10 nutsiepully

I see. So I take that as I can reuse the QuantizationConfig of Conv2D for Conv1D.

We do have Conv1D then BatchNorm though. So would QuantizationConfig of Conv1D have to account for that?

jennakwon06 avatar Oct 01 '20 05:10 jennakwon06

No, it won't be able to. You'll have to write custom transforms for that, similar to the Conv2D BatchNorm transforms. And you would have to ensure that the converter is able to handle that.

nutsiepully avatar Oct 01 '20 06:10 nutsiepully

I see. Thank you for the reply.

That must be subclassing tensorflow_model_optimization.python.core.quantization.keras.graph_transformations.transforms.Transform, as done here https://github.com/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/python/core/quantization/keras/default_8bit/default_8bit_transforms.py#L171 , correct? I will take a shot at implementing this.

jennakwon06 avatar Oct 01 '20 15:10 jennakwon06

Yes, you can use the Conv2D one as a guide. Perhaps, even start with modifying it. Might just work with that.

Thanks!

nutsiepully avatar Oct 01 '20 18:10 nutsiepully

Hi @jennakwon06, can you update?

teijeong avatar Apr 14 '21 09:04 teijeong

Hey, I am also having issues to quantize my model. I want to deploy on the edge tpu, so I just set up a simple timeseries model to test it out. Any feedback on that? Thanks

marbortoli avatar Jul 22 '21 08:07 marbortoli

It would be very useful for me as well to have conv1d and also 1d pooling supported, should be very simple to implement and would save time and make the embedded solutions more complete. This makes a lot of sense as in embedded systems that needs the quantization many cases has 1d time series of sensor measurement.

LevLavy avatar Aug 10 '21 10:08 LevLavy

Any updates on this?

aqibsaeed avatar Dec 25 '21 22:12 aqibsaeed

It is the middle of 2022 and still no updates... :(

choesy avatar May 30 '22 09:05 choesy

@Xhark - do we have any plans to add support for this?

Others, please try the rest of the bug to use custom quantization for support.

nutsiepully avatar Jun 05 '22 22:06 nutsiepully

Any update on this?

jjmattathil avatar Aug 27 '22 10:08 jjmattathil

2023, is there any plan for supporting conv1D quantization...?

tng1605 avatar May 16 '23 12:05 tng1605

Hey, the workaround is to use 2D convolution. But keep one dimension at 1.

choesy avatar May 16 '23 13:05 choesy

Thanks, @choesy! Your workaround worked. I'll elaborate here in case it helps someone else.

You can insert Reshape() layers into the model and then replace your Conv1D or MaxPool1D layers with their 2D equivalents (which are QAT-supported).

If you've already tried the "post-training quantization" option (i.e. NOT quantization-aware training) to convert your model, you'll see that method is doing the Reshape() + 2D operation behind the scenes. The first Netron image below is from my original model: image

The second is after the "post-training quantization" option. Notice the extra Reshape() layers and the ways the convolution kernels have changed. image

If you can rewrite your model to use this Reshape() + 2D operation trick, then you can apply quantization-aware-training. I didn't even need to retrain my model from scratch, I just figured out how to reshape the pretrained weights as I loaded them in. It's a little painful to get all the shaping figured out, but it does allow you to move forward.

EClemMarq avatar May 16 '23 21:05 EClemMarq