Skip to content

fix(gpt_oss): free quantized expert tensors per-layer to reduce peak memory#2149

Open
stanley1208 wants to merge 2 commits intoNVIDIA-NeMo:mainfrom
stanley1208:fix/gpt-oss-dequant-free-quantized
Open

fix(gpt_oss): free quantized expert tensors per-layer to reduce peak memory#2149
stanley1208 wants to merge 2 commits intoNVIDIA-NeMo:mainfrom
stanley1208:fix/gpt-oss-dequant-free-quantized

Conversation

@stanley1208
Copy link
Copy Markdown
Contributor

@stanley1208 stanley1208 commented May 6, 2026

Summary

Step 1 of #1274.

_dequantize_block_scale_tensors was holding every layer's quantized blocks/scales alive until the dequant loop finished, doubling peak memory during GPT-OSS load. This pops each entry from the local mapping right before dequantizing. No behavior change.

for layer_name in list(layer_name_to_quantized_weights.keys()):
    quantized_dict = layer_name_to_quantized_weights.pop(layer_name)
    dequantized_weights = self._convert_moe_packed_tensors(
        quantized_dict["blocks"], quantized_dict["scales"]
    )
    del quantized_dict
    state_dict[layer_name] = dequantized_weights

Originally I had a Step 2 too (CPU dequant fallback), but @akoumpa flagged the runtime cost on large models and that it doesn't help on DGX Spark unified memory. Dropped.

Won't fully close the 2× 48GB GPT-OSS 20B OOM on its own, dequantized BF16 tensors still accumulate in state_dict. I'll measure post-merge and come back with a streaming approach if needed.

Tests

26 existing tests in tests/unit_tests/models/gpt_oss/test_gptoss_state_dict_adapter.py pass. New test_dequantize_frees_quantized_between_iterations uses weakref to assert layer N's tensors are GC-able when layer N+1 starts. Confirmed it fails without the fix.

Refs #1274

@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented May 6, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Pop each layer's blocks/scales out of layer_name_to_quantized_weights
before producing its dequantized output, so the quantized side is freed
per-iteration instead of being pinned for the full loop. Reduces peak
memory during GPT-OSS load. Behavioral contract is unchanged.

Adds a regression test using weakrefs to verify mid-loop GC.

Refs NVIDIA-NeMo#1274

Signed-off-by: stanley1208 <stanley.mei08@gmail.com>
@akoumpa
Copy link
Copy Markdown
Contributor

akoumpa commented May 6, 2026

/ok to test b471951

@akoumpa
Copy link
Copy Markdown
Contributor

akoumpa commented May 6, 2026

Hi @stanley1208 ,

sorry to be bothering you with linting, but can you run

ruff format nemo_automodel/components/models/gpt_oss/state_dict_adapter.py

commit + push? so that the lint does not compalin.

Thank you & sorry for the trouble.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-customer Waiting on the original author to respond label May 6, 2026
Signed-off-by: stanley1208 <stanley.mei08@gmail.com>
@akoumpa
Copy link
Copy Markdown
Contributor

akoumpa commented May 7, 2026

/ok to test 16fded5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-request waiting-on-customer Waiting on the original author to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants