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

Apply weight compression after model save to reduce peak RAM during export #878

Merged

Conversation

nikita-savelyevv
Copy link
Collaborator

@nikita-savelyevv nikita-savelyevv commented Aug 22, 2024

What does this PR do?

Currently during OV model export with compression, compression is applied right after the model is converted. Because of this, we can't deallocated the memory required for the conversion before the compression start.

This PR introduces an intermediate full precision model saving step. Model compression is applied after this step allowing to save some memory. For INT8 compression the improvement is estimated to be at least 20%. Please see the figures below for "before" and "after" for INT8 compression of llama-2-7b-hf model.

Memory figures were obtained with https://github.com/openvinotoolkit/nncf/blob/develop/tools/memory_monitor.py

Memory improvement is only for data-free compression via optimum-cli. For export via Python API, i.e. from_pretrained(..., export=True) the memory is already handled efficiently with intermediate saving. The same for data-aware compression via optimum-cli.

Before After
system_memory_usage_from-zero system_memory_usage_from-zero
Model Peak Mem. Before (MiB) Peak Mem. After (MiB) Time Before (sec.) Time After (sec.)
opt-350m, INT8 1789 1461 (-19%) 20 20 (+0%)
opt-350m, INT4 1734 1466 (-15%) 28 29 (+3%)
tiny-llama-1.1b, INT8 4370 3888 (-11%) 33 32 (-3%)
tiny-llama-1.1b, INT4 4005 3798 (-5%) 63 62 (-2%)
open-llama-3b, INT8 11203 7822 (-30%) 48 55 (+15%)
open-llama-3b, INT4 9750 7822 (-20%) 150 158 (+5%)
llama2-7b-hf, INT8 21108 14551 (-31%) 80 94 (+18%)
llama2-7b-hf, INT4 18039 14531 (-19%) 276 292 (+6%)

For some unknown reason the results for tiny-llama-1.1b are unexpected. There is no yet an explanation for that. For other models there is noticeable memory saving with the cost of longer conversion due to intermediate model saving.

Related tickets
147935

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

@nikita-savelyevv nikita-savelyevv force-pushed the compress-after-model-saving branch from aab15e1 to 3ffacd3 Compare August 22, 2024 16:07
@nikita-savelyevv nikita-savelyevv marked this pull request as ready for review August 26, 2024 12:46
@nikita-savelyevv
Copy link
Collaborator Author

@eaidova @AlexKoff88 please take a look

@nikita-savelyevv nikita-savelyevv changed the title Apply weight compression after model is saved to save RAM Apply weight compression after model save to reduce peak RAM during export Aug 26, 2024
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

optimum/exporters/openvino/__main__.py Outdated Show resolved Hide resolved
@eaidova
Copy link
Collaborator

eaidova commented Aug 30, 2024

@IlyasMoutawwakil @echarlaix could you please merge?

@echarlaix echarlaix merged commit b5998f2 into huggingface:main Aug 30, 2024
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants