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

[PTQ] Add support of arbitrary batch size for PTQ #2197

Merged
merged 131 commits into from
Mar 22, 2024

Conversation

kshpv
Copy link
Collaborator

@kshpv kshpv commented Oct 13, 2023

Changes

Add a new advanced bool option for quantization - batchwise_statistics.
When set to True then statistics collection for supported algorithms (see below) are calculated with the assumption that the 0-axis of a tensor is a batch axis.
If the value is False then statistics collection for algorithms is calculated with an assumption that the tensor has no batch axis.
If set to None statistics collection logic adapts based on the batch_size of the provided dataset.

These adjustments in statistical computation apply specifically to MinMax, ChannelAlighnment algorithms.

During the validation of proposed changes on a wide scope of models, some limitations were observed - if a model contains specific operations that output in a way that a tensor batch axis starts to contain no batch meaning anymore, then the statistics after such operations are collected not precisely.

The handling of such cases is introduced and determined by a warning message to a user with a recommendation using batch size = 1 for a specific model or set to False batchwise_statistics option.

The torch sample for mobilenet_v2 was updated with batch_size=128 value with a new recalculated subset_size.
The conformance test was updated with new options batch_size and dynamic_batch_shape.
Calibrate.py was updated with a new option batch_size.

Algorithm support batch_size > 1:

Algorithm Do results depend on batch_size? Comments
MinMax relatively depends Relatively means that results are dependant on the correctness of the utilized assumption that batch lays on the 0-axis. To overcome there is a need to have batch axis determination algorithm
FastBiascCorrection Yes Incorrect statistics calculation with no regarding batch axis in an aggregator. Need to have batch axis determination algorithm
BiasCorrection Yes Incorrect statistics calculation with no regarding batch axis in an aggregator. Need to have batch axis determination algorithm
ChannelAlighnment No Checked on models from conformance test: mobilenet_v2, mobilenet_v3
SmoothQuant No Checked on models from conformance test: levit_128, visformer_small
PostTrainingQuantization Yes Need to have batch axis determination algorithm

Reason for changes

Speeding up statistics collection.
SpeedUp on mobilenet_v2 sample (local measurments):

Backend bs=1 (sec) bs=16 (sec) bs=128 (sec)
Torch 24 4 4
Torch CUDA 20 1 1
OpenVINO 9 4 5
ONNX 17 11 12

Extend usage scenarios.

Related tickets

121650

Tests

Old tests were updated accordingly.
New test added:
test_tensor_collector_batch_size
test_min_max

@github-actions github-actions bot added NNCF PT Pull requests that updates NNCF PyTorch NNCF Common Pull request that updates NNCF Common NNCF OpenVINO Pull requests that updates NNCF OpenVINO NNCF ONNX Pull requests that updates NNCF ONNX labels Oct 13, 2023
@codecov
Copy link

codecov bot commented Oct 13, 2023

Codecov Report

Attention: Patch coverage is 83.61582% with 29 lines in your changes are missing coverage. Please review.

Project coverage is 84.89%. Comparing base (7974023) to head (88653aa).
Report is 2 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2197      +/-   ##
===========================================
- Coverage    91.19%   84.89%   -6.30%     
===========================================
  Files          492      494       +2     
  Lines        45100    45350     +250     
===========================================
- Hits         41127    38502    -2625     
- Misses        3973     6848    +2875     
Files Coverage Δ
nncf/common/graph/utils.py 82.14% <100.00%> (+1.75%) ⬆️
nncf/common/quantization/initialization/range.py 94.38% <100.00%> (-1.68%) ⬇️
nncf/common/tensor_statistics/aggregator.py 98.55% <100.00%> (-1.45%) ⬇️
.../common/tensor_statistics/statistical_functions.py 100.00% <100.00%> (ø)
nncf/experimental/tensor/functions/numeric.py 98.30% <100.00%> (-0.57%) ⬇️
...ncf/experimental/tensor/functions/numpy_numeric.py 82.35% <100.00%> (-11.73%) ⬇️
...ncf/experimental/tensor/functions/torch_numeric.py 98.12% <100.00%> (+0.01%) ⬆️
nncf/onnx/graph/metatypes/groups.py 100.00% <100.00%> (ø)
nncf/onnx/graph/metatypes/onnx_metatypes.py 99.58% <100.00%> (ø)
nncf/onnx/graph/node_utils.py 97.40% <100.00%> (-0.36%) ⬇️
... and 30 more

... and 45 files with indirect coverage changes

Flag Coverage Δ
COMMON 44.15% <28.45%> (+0.22%) ⬆️
ONNX 34.65% <66.10%> (-0.01%) ⬇️
OPENVINO ∅ <ø> (∅)
TENSORFLOW 30.12% <20.33%> (+0.25%) ⬆️
TORCH 65.95% <64.97%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
common 93.14% <100.00%> (-0.63%) ⬇️
torch 93.48% <100.00%> (-0.12%) ⬇️
tensorflow 93.74% <ø> (ø)
onnx 93.02% <100.00%> (-0.03%) ⬇️
openvino 25.75% <25.00%> (-68.33%) ⬇️
ptq 69.89% <73.03%> (-20.33%) ⬇️

@daniil-lyakhov daniil-lyakhov self-requested a review October 16, 2023 09:03
@github-actions github-actions bot added experimental NNCF PTQ Pull requests that updates NNCF PTQ labels Oct 18, 2023
@MaximProshin MaximProshin requested a review from alexsu52 October 23, 2023 09:04
Copy link
Collaborator

@daniil-lyakhov daniil-lyakhov left a comment

Choose a reason for hiding this comment

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

Code looks ok for me, what about tests?

nncf/common/tensor_statistics/aggregator.py Outdated Show resolved Hide resolved
nncf/experimental/common/tensor_statistics/collectors.py Outdated Show resolved Hide resolved
nncf/experimental/common/tensor_statistics/collectors.py Outdated Show resolved Hide resolved
nncf/experimental/common/tensor_statistics/collectors.py Outdated Show resolved Hide resolved
nncf/experimental/common/tensor_statistics/collectors.py Outdated Show resolved Hide resolved
nncf/experimental/common/tensor_statistics/collectors.py Outdated Show resolved Hide resolved
nncf/quantization/algorithms/min_max/openvino_backend.py Outdated Show resolved Hide resolved
nncf/quantization/algorithms/min_max/openvino_backend.py Outdated Show resolved Hide resolved
nncf/quantization/algorithms/min_max/openvino_backend.py Outdated Show resolved Hide resolved
@github-actions github-actions bot removed the NNCF ONNX Pull requests that updates NNCF ONNX label Dec 20, 2023
@kshpv kshpv requested a review from AlexanderDokuchaev March 8, 2024 18:55
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Mar 18, 2024
kshpv added 2 commits March 18, 2024 15:03
…ls bs>1 wit dynamic batch_size at input shape; make validation with bs=1
Copy link
Collaborator

@daniil-lyakhov daniil-lyakhov left a comment

Choose a reason for hiding this comment

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

Minor

nncf/common/tensor_statistics/aggregator.py Outdated Show resolved Hide resolved
nncf/quantization/advanced_parameters.py Outdated Show resolved Hide resolved
nncf/quantization/quantize_model.py Outdated Show resolved Hide resolved
@kshpv kshpv requested a review from alexsu52 March 20, 2024 20:25
Copy link
Contributor

@alexsu52 alexsu52 left a comment

Choose a reason for hiding this comment

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

LGTM. Please provide test results for weights compression.

@kshpv
Copy link
Collaborator Author

kshpv commented Mar 22, 2024

LGTM. Please provide test results for weights compression.

build 35 passed

@alexsu52 alexsu52 merged commit b7ba5ad into openvinotoolkit:develop Mar 22, 2024
11 checks passed
alexsu52 pushed a commit that referenced this pull request Mar 27, 2024
### Changes

Restore total number of iterations for StatisticsAggregator after
#2197

### Reason for changes

N/A

### Related tickets

136892

### Tests

Manually tested on yolov8 sample for OV.
Before:

![image](https://github.com/openvinotoolkit/nncf/assets/32935044/6a1292ef-cf00-4571-bbe7-92acc36b44f4)

After:

![image](https://github.com/openvinotoolkit/nncf/assets/32935044/aa5c1f6d-c4f8-4d6c-86d0-15a34bc9f86e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Public API-impacting changes documentation Improvements or additions to documentation experimental NNCF Common Pull request that updates NNCF Common NNCF ONNX Pull requests that updates NNCF ONNX 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.

8 participants