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

[TorchFX] SmoothQuant algorithm implementation #2875

Merged
merged 4 commits into from
Aug 16, 2024

Conversation

daniil-lyakhov
Copy link
Collaborator

@daniil-lyakhov daniil-lyakhov commented Aug 6, 2024

Changes

TorchFX SmoothQuant backend implementation

  • module_insertion_transformation_builder is introduced
  • Transformation requires names for new modules and nodes
  • vit_b_16 is introduced in the conformance tests

Reason for changes

To improve metrics of quantized models: swin_v2_s and vit_b_16

  • To insert SQ multiply nodes to the graph
  • To make node names human-readable and consistent
  • To check sq algorithm E2E

Related tickets

#2766

Tests

  • Smooth quant test template is implemented for TorchfX backed
  • Conformance test: post_training_quantization/446/ is successfull
  • Test models check SQ multiplies for swin_v2_s and vit_b_16 models

@github-actions github-actions bot added NNCF PT Pull requests that updates NNCF PyTorch experimental NNCF OpenVINO Pull requests that updates NNCF OpenVINO NNCF PTQ Pull requests that updates NNCF PTQ labels Aug 6, 2024
@daniil-lyakhov daniil-lyakhov force-pushed the dl/fx/sq branch 2 times, most recently from 06f948a to 3ce7cb0 Compare August 8, 2024 18:15
@daniil-lyakhov daniil-lyakhov marked this pull request as ready for review August 9, 2024 09:16
@daniil-lyakhov daniil-lyakhov requested a review from a team as a code owner August 9, 2024 09:16
@daniil-lyakhov daniil-lyakhov force-pushed the dl/fx/sq branch 7 times, most recently from 756481a to ff48d0a Compare August 13, 2024 12:38
Swin transformer conformance test

FXSQMultiply

Refereces update
nncf/experimental/torch/fx/statistics/aggregator.py Outdated Show resolved Hide resolved
nncf/experimental/torch/fx/transformations.py Outdated Show resolved Hide resolved
nncf/experimental/torch/fx/transformations.py Outdated Show resolved Hide resolved
Comment on lines +42 to +48
class FXSQMultiply(torch.nn.Module):
def __init__(self, scale: torch.Tensor):
super().__init__()
self._scale_value = scale

def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.mul(x, self._scale_value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just pass a value into the model transformer? From my side, the algo backend should be only the bridge between the algorithm and backend specifics. But not the place for the critical structures that affect other parts of the pipeline.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is made for the sake of flexibility and is adopted from the torch backend: we can reuse one module_insertion_transformation_builder to insert both multiplies for the SQ, biases for channel alignment and convert nodes for the weights compression algo.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. But in this case, model transformers start to be dependant on the algorithms and their internal classes, not only from transformer commands. This is the main issue for me.

tests/torch/fx/test_smooth_quant.py Outdated Show resolved Hide resolved
@daniil-lyakhov daniil-lyakhov requested a review from KodiaqQ August 14, 2024 11:53
@KodiaqQ KodiaqQ merged commit 7744ebf into openvinotoolkit:develop Aug 16, 2024
13 checks passed
AlexanderDokuchaev pushed a commit to AlexanderDokuchaev/nncf that referenced this pull request Aug 23, 2024
### Changes

TorchFX SmoothQuant backend implementation
*  module_insertion_transformation_builder is introduced
* Transformation requires names for new modules and nodes
* vit_b_16 is introduced in the conformance tests
### Reason for changes

To improve metrics of quantized models: swin_v2_s and vit_b_16
* To insert SQ multiply nodes to the graph
* To make node names human-readable and consistent
* To check sq algorithm E2E

### Related tickets

openvinotoolkit#2766

### Tests

* Smooth quant test template is implemented for TorchfX backed
* Conformance test: post_training_quantization/446/ is successfull
* Test models check SQ multiplies for swin_v2_s and vit_b_16 models
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental NNCF OpenVINO Pull requests that updates NNCF OpenVINO NNCF PT Pull requests that updates NNCF PyTorch NNCF PTQ Pull requests that updates NNCF PTQ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants