feat: add HardSwish activation function#1406
Conversation
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
…zation Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
feat: add HardSwish activation function
|
Hey, thanks for looking into this. I would still be happy to accept a baseline implementation like this, but let me think about it. In the meantime, why did you decide to make it UnsignedInt8? Another reason why we don't provide a default quantizer is related to this. HardSwish requires a signed quantizer, but most of the negative range is "wasted". To overcome this, the most intuitive solution would be to have Considering that Also, I don't think setting Looking forward to hearing back from you while we think about whether or not to add this feature. In any case, many thanks for the contribution. |
Also, please remove Since you are working with FINN, you may be interested to hear that we are planning to have a programmatic quantization flow for FINN in the next release. |
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
feat: add QuantHardSwish with signed quantization for baseline
|
HI @Giuseppe5 Changes Made
Current StateThe implementation provides a baseline using Regarding Asymmetric QuantizationI understand your concern about hardware-specific requirements. Option A – Baseline onlyKeep the current symmetric signed implementation as a starting point that users can customize. Option B – Add asymmetric variantAdd an example/alternative using Next StepsWhich approach would you prefer? Thanks again for your guidance! |
|
I have one more question. For my research purposes, I also want to implement a quantized version of MaxPool2d and AdaptiveMaxPool2d. Would you be able to add this? Are you interested? I wanted to do this for my quantized yolov8-obb also. |
Can you tell us more about your QuantMaxPool use-case? MaxPool is quantization-invariant, so it's non-obvious to use why this would be needed. Our current position is that this is non-necessary / interesting, but we are open to being convinced. Please note, that Brevitas is designed to be completely compatible with out-of-source extensions, (for example, everything in If you find something that is difficult to do out-of-source, please raise this to our attention.
I prefer option A. @Giuseppe5, do you agree / disagree? |
|
Thank you for the quick response. Yes, MaxPool is quantization-invariant, so it usually does not make sense to quantize it. I also appreciate your reminder about the brevitas_examples. I have a one question:
Also, thank you for considering my pull request for review. I hope it will be merged. |
Giuseppe5
left a comment
There was a problem hiding this comment.
As far as I know, PTQ is currently available only for ImageNet models
This is not correct. We have implemented PTQ pipelines as examples for torchvision, stable diffusion, and LLM models.
For what concerns the PTQ algorithms themselves, there is nothing preventing their application on any model with any task.
The most important part is understanding the concepts behind how each algorithm works (most of them require having a calibration dataset and iterating through it once per model or once per layer, but please take this as general directive and look at our existing pipelines for examples).
The main reason why we do not have a PTQ pipeline for object detection is that it takes time, there is no general benchmark (unlike imagenet for torchvision), and there are multiple libraries we would need to add and study to make sure everything works as intended.
We are not saying it is not important but it is not currently a priority.
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
fix: change act_quant to Uint8ActPerTensorFloat
|
Ok, I will start implementing PTQ for object detection algorithms. |
|
@Giuseppe5 |
Giuseppe5
left a comment
There was a problem hiding this comment.
Overall this looks good to me, except for the two small changes above.
Please check our contribution guideline to see how to sign your commits:
https://github.com/Xilinx/brevitas/blob/master/CONTRIBUTING.md
Once all these is handled, we can merge, unless @nickfraser has some other comments
Thanks for the contribution.
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
feat: add HardSwish activation function
|
Hi @Giuseppe5 and @nickfraser , I have don the necessary changes. Thank you for the guidance. |
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
chore: remove unused function
|
Hi @Giuseppe5 and @nickfraser, I removed unused function. |
Giuseppe5
left a comment
There was a problem hiding this comment.
Just two remaining changes, mostly related to comments.
If you want, I can also do that myself.
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Signed-off-by: Suraj Karki <suraj.karki@uni-bielefeld.de>
Feat/hardswish: Update QuantHardSwish comments and tests for unsigned quantization
|
Hi @Giuseppe5 and @nickfraser, |
|
Hi @nickfraser and @Giuseppe5 |
|
One question to @nickfraser |
|
Hello @nickfraser and @Giuseppe5 |
Reason for this PR
This change adds support for QuantHardSwish, a quantized version of the HardSwish activation function, to Brevitas. Currently, Brevitas lacks support for quantized HardSwish, limiting its applicability to networks that use this activation. I am currently doing my research with brevitas and FINN for quantization-aware training and deploying object detection algorithms on FPGA. Currently, I am using the quantized ReLU activation function for YOLOv8, but I want to try Hardswish, which is better than ReLU in some scenarios.
This addition enables quantization-aware training (QAT) and post-training quantization (PTQ) for networks using HardSwish activation. This addition could potentially enhance the existing quantized activation functions within the Brevitas ecosystem.
Changes Made in this PR
New QuantHardSwish Layer (quant_activation.py)
Graph Quantization Support (quantize.py)
FlexML Target Support (flexml.py)
QuantTensor Functional Support (torch_handler.py)
Comprehensive Test Suite (test_act.py)
Public API Export (init.py)
Design Decisions:
Implementation Approach:
Testing Summary
Tests Performed:
Risk Highlight
Checklist
devbranch.