Skip to content

[GGUF] Reduce peak RAM usage by casting dequantized tensors early during load#45386

Merged
SunMarc merged 5 commits intohuggingface:mainfrom
UsamaKenway:gguf-early-dtype-casting
Apr 20, 2026
Merged

[GGUF] Reduce peak RAM usage by casting dequantized tensors early during load#45386
SunMarc merged 5 commits intohuggingface:mainfrom
UsamaKenway:gguf-early-dtype-casting

Conversation

@UsamaKenway
Copy link
Copy Markdown
Contributor

@UsamaKenway UsamaKenway commented Apr 12, 2026

Optimizes memory usage when loading GGUF models by performing dtype casting immediately after dequantization.

While I was adding the support for Gemma4 in this PR #45296, i noticed this issue that the GGUF tensors are dequantized to float32 by default during the loading process, even if the user intends to load the model in float16 or bfloat16. For large models, this creates a significant RAM spike that can lead to Out Of Memory.

By passing the target torch_dtype directly into the loading utility, we can cast the tensors immediately after dequantization, effectively halving the peak RAM required for the state dict.

Benchmark Results (Gemma 4 26B IT q4_k_m)

I tested the peak RAM (Global Peak RSS) with and without this change using a separate branch for tracking:

- Without this PR (Float32 spike): ~118.7 GB Peak RSS
- With this PR (Early casting):    ~59.4  GB Peak RSS
------------------------------------------------------
Saving:                            ~59.3  GB (50% reduction)
Tests

With the changes

(py312venv) usamakenway@Megatron: RUN_SLOW=1 pytest tests/quantization/ggml/test_ggml.py::GgufModelTests::test_gemma4_26b_it_q4_k_m -s                                                                                                                                                                                                                                                                                                                                                   

[RAM DEBUG] Global Peak RSS (High Water Mark): 2185.59 MB

[RAM DEBUG] Global Peak RSS (High Water Mark): 2197.88 MB

[RAM DEBUG] Global Peak RSS (High Water Mark): 2391.64 MB
Converting and de-quantizing GGUF tensors...: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 658/658 [03:03<00:00,  3.59it/s]

[RAM DEBUG] Global Peak RSS (High Water Mark): 59428.81 MB
Loading weights: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 657/657 [00:00<00:00, 26841.26it/s]
PASSEDtests/quantization/ggml/test_ggml.py::GgufModelTests::test_gemma4_26b_it_q4_k_m [PASSED] 287.34s

Without the changes

(py312venv) usamakenway@Megatron:  RUN_SLOW=1 pytest tests/quantization/ggml/test_ggml.py::GgufModelTests::test_gemma4_26b_it_q4_k_m -s
                                                                                                                                       
tests/quantization/ggml/test_ggml.py::GgufModelTests::test_gemma4_26b_it_q4_k_m 
[RAM DEBUG] Global Peak RSS (High Water Mark): 2259.14 MB

[RAM DEBUG] Global Peak RSS (High Water Mark): 2270.75 MB

[RAM DEBUG] Global Peak RSS (High Water Mark): 2464.60 MB
Converting and de-quantizing GGUF tensors...: 100%|███████████████████████████████████████████████████████████████████████████████| 658/658 [05:46<00:00,  1.90it/s]

[RAM DEBUG] Global Peak RSS (High Water Mark): 118747.33 MB
Loading weights: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 657/657 [00:03<00:00, 195.35it/s]
PASSEDtests/quantization/ggml/test_ggml.py::GgufModelTests::test_gemma4_26b_it_q4_k_m [PASSED] 499.38s

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@Rocketknight1
Copy link
Copy Markdown
Member

cc @SunMarc

Copy link
Copy Markdown
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks, a couple of comments

Comment thread src/transformers/modeling_utils.py Outdated
Comment thread src/transformers/modeling_gguf_pytorch_utils.py Outdated
Signed-off-by: Usama Kenway <usamakenway@gmail.com>
Signed-off-by: Usama Kenway <usamakenway@gmail.com>
Comment thread src/transformers/modeling_utils.py
@UsamaKenway
Copy link
Copy Markdown
Contributor Author

@SunMarc Changes pushed. refactored the logic as suggested . Let me know if you spot anything else or if this is good to go.
Thank you.

@UsamaKenway UsamaKenway requested a review from SunMarc April 15, 2026 08:31
Copy link
Copy Markdown
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Much better thanks a lot !

@SunMarc SunMarc enabled auto-merge April 20, 2026 15:35
@SunMarc SunMarc added this pull request to the merge queue Apr 20, 2026
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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.

Merged via the queue into huggingface:main with commit 7a0d582 Apr 20, 2026
28 checks passed
lvliang-intel pushed a commit to lvliang-intel/transformers that referenced this pull request Apr 21, 2026
…ing load (huggingface#45386)

* [GGUF] cast dequantized tensors to target dtype during load

Signed-off-by: UsamaKenway <usamakenway@gmail.com>

* [GGUF] refac dtype, quantization casting

Signed-off-by: Usama Kenway <usamakenway@gmail.com>

* [GGUF] refac dtype

Signed-off-by: Usama Kenway <usamakenway@gmail.com>

---------

Signed-off-by: UsamaKenway <usamakenway@gmail.com>
Signed-off-by: Usama Kenway <usamakenway@gmail.com>
Co-authored-by: Marc Sun <57196510+SunMarc@users.noreply.github.com>
artem-spector pushed a commit to artem-spector/transformers that referenced this pull request Apr 21, 2026
…ing load (huggingface#45386)

* [GGUF] cast dequantized tensors to target dtype during load

Signed-off-by: UsamaKenway <usamakenway@gmail.com>

* [GGUF] refac dtype, quantization casting

Signed-off-by: Usama Kenway <usamakenway@gmail.com>

* [GGUF] refac dtype

Signed-off-by: Usama Kenway <usamakenway@gmail.com>

---------

Signed-off-by: UsamaKenway <usamakenway@gmail.com>
Signed-off-by: Usama Kenway <usamakenway@gmail.com>
Co-authored-by: Marc Sun <57196510+SunMarc@users.noreply.github.com>
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.

4 participants