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

[NNCF]: Optimized memory footprint by removing redundant collected statistics #2563

Closed
wants to merge 35 commits into from

Conversation

AdiKsOnDev
Copy link
Contributor

@AdiKsOnDev AdiKsOnDev commented Mar 8, 2024

Changes

Made changes in:

  • nncf/common/tensor_statistics/statistic_point.py
  • nncf/quantization/algorithms/pipeline.py

Focus on the way I used newly created remove_statistic_point() inside pipeline.py, to see if it's up to the expectations

Reason for changes

Save space by removing "unused" statistic points associated with an algorithm

Related tickets

120377

Tests

tests/common/test_statistic_points.py was added:

  • Test removing associated statistical points
  • Test removing from an empty container

Closes #2557

TODO: Integrate the method into the pipeline
@AdiKsOnDev AdiKsOnDev requested a review from a team as a code owner March 8, 2024 12:26
@github-actions github-actions bot added NNCF Common Pull request that updates NNCF Common NNCF PTQ Pull requests that updates NNCF PTQ labels Mar 8, 2024
@alexsu52 alexsu52 requested a review from kshpv March 8, 2024 12:31
Copy link

codecov bot commented Mar 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 52.32%. Comparing base (8ad77dc) to head (aefd170).
Report is 3 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##           develop    #2563       +/-   ##
============================================
- Coverage    77.91%   52.32%   -25.59%     
============================================
  Files          494      494               
  Lines        45387    45387               
============================================
- Hits         35363    23749    -11614     
- Misses       10024    21638    +11614     
Files Coverage Δ
nncf/common/tensor_statistics/statistic_point.py 93.10% <100.00%> (+1.61%) ⬆️
nncf/quantization/algorithms/algorithm.py 100.00% <100.00%> (ø)
nncf/quantization/algorithms/pipeline.py 93.50% <100.00%> (-1.17%) ⬇️

... and 276 files with indirect coverage changes

Flag Coverage Δ
COMMON 44.22% <77.77%> (?)
ONNX 34.65% <100.00%> (?)
TENSORFLOW 30.11% <22.22%> (+<0.01%) ⬆️
TORCH ?

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

Components Coverage Δ
common 87.60% <100.00%> (-0.72%) ⬇️
torch 32.25% <ø> (-61.24%) ⬇️
tensorflow 93.74% <ø> (ø)
onnx 93.07% <ø> (+93.07%) ⬆️
openvino 0.00% <ø> (-25.77%) ⬇️
ptq 48.47% <100.00%> (-4.58%) ⬇️

Copy link
Collaborator

@kshpv kshpv left a comment

Choose a reason for hiding this comment

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

Thank you for looking into the issue!
General recommendation: I suggest beginning with Test-Driven Development (TDD) by initially crafting a test case.

  1. Add multiple statistical points.
  2. Subsequently remove some of them/all. Don't forget about edge cases.
  3. Concluding with a final verification to ensure the function operates as intended.

nncf/common/tensor_statistics/statistic_point.py Outdated Show resolved Hide resolved
nncf/common/tensor_statistics/statistic_point.py Outdated Show resolved Hide resolved
@github-actions github-actions bot removed the NNCF PTQ Pull requests that updates NNCF PTQ label Mar 8, 2024
@AdiKsOnDev
Copy link
Contributor Author

AdiKsOnDev commented Mar 8, 2024

@kshpv Are any actions expected from me any further?

@kshpv
Copy link
Collaborator

kshpv commented Mar 8, 2024

@kshpv Is any action expected from me any further?

Could you add a test on this functionality? You can create a file test_statistic_points.py with a test and put it in tests/common/

Also, it would be beneficial if you provide the memory usage before and after your changes for some of NNCF examples

Thank you!

@AdiKsOnDev
Copy link
Contributor Author

Yes, absolutely! I shall request a re-review for another PR tomorrow (hopefully) with a test for this and I'll give the results of a benchmark before/after the change

Thank you for responding so quickly!

@AdiKsOnDev
Copy link
Contributor Author

Benchmark Examples

@kshpv

Before

before

After

Screenshot_20240309_161303

@AdiKsOnDev AdiKsOnDev requested a review from kshpv March 9, 2024 12:22
@AdiKsOnDev
Copy link
Contributor Author

@KodiaqQ Are any actions expected from me any further?

@nikita-malininn
Copy link
Collaborator

Benchmark Examples

@kshpv

Before

...

After

...

Hi, @AdiKsOnDev. Thank you for your contribution.
I wonder to understand, why the performance of the model was decreased after the changes?

@AdiKsOnDev
Copy link
Contributor Author

Benchmark Examples

@kshpv

Before

...

After

...

Hi, @AdiKsOnDev. Thank you for your contribution.
I wonder to understand, why the performance of the model was decreased after the changes?

Hi, can it be that in the remove_statistical_points() the loop that's doing all the work is taking too long?

remove_statistic_point() is now integrated in the pipeline
@github-actions github-actions bot added the NNCF PTQ Pull requests that updates NNCF PTQ label Mar 11, 2024
@kshpv
Copy link
Collaborator

kshpv commented Mar 28, 2024

@AdiKsOnDev Hi!
Are you going to continue work on this?
I think a fixing the precommit is the last part that you need to do to finish the PR.

@AdiKsOnDev
Copy link
Contributor Author

@AdiKsOnDev Hi!
Are you going to continue work on this?
I think a fixing the precommit is the last part that you need to do to finish the PR.

Hello! I remember this, don't worry I am still extremely eager to finish, but I have a huge workload on me right now. I shall continue working this weekend

Very sorry for the silence

@kshpv
Copy link
Collaborator

kshpv commented Mar 28, 2024

@AdiKsOnDev Hi!
Are you going to continue work on this?
I think a fixing the precommit is the last part that you need to do to finish the PR.

Hello! I remember this, don't worry I am still extremely eager to finish, but I have a huge workload on me right now. I shall continue working this weekend

Very sorry for the silence

Ok Then!
If you have any question, don't hesistate to ask :)

@AdiKsOnDev
Copy link
Contributor Author

AdiKsOnDev commented Mar 31, 2024

@kshpv @KodiaqQ For some reason (that is unknown to me, lol) algorithm in the loop provided below is not able to call the .algorithm_key() I made. (Throws TypeError: 'str' object is not callable)
HOWEVER, I am able to use algorithm._algorithm_key BUT it ends up failing the following test (Along with many others):

FAILED tests/onnx/quantization/test_bias_correction.py::TestONNXBCAlgorithm::test_update_bias[MultipleConvTestModel-ref_biases0] - KeyError: '/Relu_1'

This test only passes when I just pass the algorithm object itself, but in that case there is no point in .remove_statiistic_points() because it doesn't end up removing the algorithm (I think)

for algorithm in pipeline_step[:-1]:
    current_model = algorithm.apply(current_model, current_graph, step_statistics)
    current_graph = NNCFGraphFactory.create(current_model)
    step_statistics.remove_statistic_points(algorithm)
                                                                                       
current_model = pipeline_step[-1].apply(current_model, current_graph, step_statistics)
step_statistics.remove_statistic_points(pipeline_step[-1])

@AdiKsOnDev
Copy link
Contributor Author

@kshpv Following up on the above^

@AdiKsOnDev
Copy link
Contributor Author

@KodiaqQ

@kshpv
Copy link
Collaborator

kshpv commented Apr 2, 2024

@kshpv @KodiaqQ For some reason (that is unknown to me, lol) algorithm in the loop provided below is not able to call the .algorithm_key() I made. (Throws TypeError: 'str' object is not callable) HOWEVER, I am able to use algorithm._algorithm_key BUT it ends up failing the following test (Along with many others):

FAILED tests/onnx/quantization/test_bias_correction.py::TestONNXBCAlgorithm::test_update_bias[MultipleConvTestModel-ref_biases0] - KeyError: '/Relu_1'

This test only passes when I just pass the algorithm object itself, but in that case there is no point in .remove_statiistic_points() because it doesn't end up removing the algorithm (I think)

for algorithm in pipeline_step[:-1]:
    current_model = algorithm.apply(current_model, current_graph, step_statistics)
    current_graph = NNCFGraphFactory.create(current_model)
    step_statistics.remove_statistic_points(algorithm)
                                                                                       
current_model = pipeline_step[-1].apply(current_model, current_graph, step_statistics)
step_statistics.remove_statistic_points(pipeline_step[-1])

algorithm_key is decorated method by property decorator. So syntaxis to get a property value is the following:
algorithm.algorithm_key

Please, fix this and then it looks like it will work

@AdiKsOnDev
Copy link
Contributor Author

AdiKsOnDev commented Apr 2, 2024

@kshpv @KodiaqQ For some reason (that is unknown to me, lol) algorithm in the loop provided below is not able to call the .algorithm_key() I made. (Throws TypeError: 'str' object is not callable) HOWEVER, I am able to use algorithm._algorithm_key BUT it ends up failing the following test (Along with many others):

FAILED tests/onnx/quantization/test_bias_correction.py::TestONNXBCAlgorithm::test_update_bias[MultipleConvTestModel-ref_biases0] - KeyError: '/Relu_1'

This test only passes when I just pass the algorithm object itself, but in that case there is no point in .remove_statiistic_points() because it doesn't end up removing the algorithm (I think)

for algorithm in pipeline_step[:-1]:
    current_model = algorithm.apply(current_model, current_graph, step_statistics)
    current_graph = NNCFGraphFactory.create(current_model)
    step_statistics.remove_statistic_points(algorithm)
                                                                                       
current_model = pipeline_step[-1].apply(current_model, current_graph, step_statistics)
step_statistics.remove_statistic_points(pipeline_step[-1])

algorithm_key is decorated method by property decorator. So syntaxis to get a property value is the following: algorithm.algorithm_key

Please, fix this and then it looks like it will work

@kshpv I've tried this, but I still get this error:

FAILED tests/onnx/quantization/test_bias_correction.py::TestONNXBCAlgorithm::test_update_bias[MultipleConvTestModel-ref_biases0] - KeyError: '/Relu_1'

@AdiKsOnDev
Copy link
Contributor Author

@kshpv UPDATE On the above:
I think it was just a local error, you can approve the git workflows now, let's see if it runs properly on the cloud (Should run fine)

@AdiKsOnDev
Copy link
Contributor Author

@kshpv Nope, fails here as well :/ Any ideas?

@kshpv
Copy link
Collaborator

kshpv commented Apr 3, 2024

Hello @AdiKsOnDev!
Could you rebase on the lateset develop branch? It seems that the recent PRs should fix your red precommit issue.

Copy link
Collaborator

@kshpv kshpv left a comment

Choose a reason for hiding this comment

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

Precommit is green. Code looks okay. Please, apply last comments and I approve

nncf/quantization/algorithms/algorithm.py Show resolved Hide resolved
tests/common/test_statistic_points.py Outdated Show resolved Hide resolved
@kshpv kshpv self-requested a review April 3, 2024 13:31
@AdiKsOnDev
Copy link
Contributor Author

Hello once again! Just wanted to confirm, is this PR ready for being merged or is something more expected from my end?
If the work is done here, I'd like to start working on some other issue while this one is getting merged

Thanks

@kshpv
Copy link
Collaborator

kshpv commented Apr 8, 2024

Hello @AdiKsOnDev!
I conducted some experiments with your changes to assess their impact on memory consumption during post-training quantization. Unfortunately, for the current pipeline (MinMax + FastBiasCorrection/BiasCorrection), these adjustments do not seem to have any significant influence. This lack of impact stems from the fact that the statistics are already optimized, making these changes negligible.

So, I am not sure about merging these changes yet.

Initially, I had high hopes that this PR would significantly reduce memory usage. However, after experimenting and crunching the numbers, it has become apparent that the impact is small.

I am disappointed that things did not pan out as I hoped, but I want to express my gratitude for your hard work. I hope you gain some experience from it, and I encourage you to keep contributing to the enhancement of NNCF.

@AdiKsOnDev
Copy link
Contributor Author

Yes, the experience was very interesting so it's perfectly fine :P
Thanks for the collaboration either way!

@kshpv
Copy link
Collaborator

kshpv commented Apr 8, 2024

So, thank you @AdiKsOnDev again! I am closing the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NNCF Common Pull request that updates NNCF Common NNCF PTQ Pull requests that updates NNCF PTQ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Good First Issue][NNCF]: Optimize memory footprint by removing redundant collected statistics
4 participants