Repository navigation
fix(optimize): stop forcing expandable_segments:True before import gptqmodel (#5199) - #5256
Merged
Qubitium merged 1 commit intoNov 18, 2026
Conversation
…tqmodel (ModelCloud#5199) optimize/_common.set_env() runs before gptqmodel is imported and set PYTORCH_ALLOC_CONF=expandable_segments:True,max_split_size_mb:1024,... GPT-QModel defers to a caller-set allocator config, so the requantization CLI (optimize/requant_embed_lm_head.py) replaced the ROCm default (expandable_segments:False) with the configuration that corrupts ordinary Torch results after empty_cache() on gfx1201, and restored the max_split_size_mb cap that ModelCloud#5108 removed. ModelCloud#5165 fixed only docs/eora/. Drop the line so import gptqmodel installs the per-platform default. Remove the same pinned literal from scripts/eval_model.py, scripts/benchmark_llama_cpp_vs_gptqmodel_gguf.py, and tests/benchmark/benchmark_test.py. Tests in tests/test_rocm_default_env.py: - a fresh interpreter imports the real tool, then gptqmodel, and must end up with _alloc_conf.default_alloc_conf() (a caller value still wins); - a repo-wide AST guard rejects any os.environ/os.putenv write of expandable_segments:True to PYTORCH_*ALLOC_CONF outside gptqmodel/_alloc_conf.py unless it sits under a HIP/ROCm check.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
optimize/_common.set_env()runs beforeimport gptqmodeland setPYTORCH_ALLOC_CONF=expandable_segments:True,max_split_size_mb:1024,garbage_collection_threshold:0.5. GPT-QModel keeps a value the caller already set, sooptimize/requant_embed_lm_head.pynever got the ROCm default (expandable_segments:False). On ROCm it ran with the configuration thatdocs/rocm-default-environment.mdsays corrupts ordinary Torch results afterempty_cache()(1,572,864 wrong rows in the issue's repro). It also brought back themax_split_size_mbcap that #5108 removed. #5165 fixed the same problem only indocs/eora/.Fixes #5199
What Changed
optimize/_common.py:set_env()no longer setsPYTORCH_ALLOC_CONF, soimport gptqmodelinstalls the per-platform default. A caller-set value still wins.tests/benchmark/benchmark_test.py: assigned it beforeimport gptqmodel, which overrode the default.scripts/eval_model.pyandscripts/benchmark_llama_cpp_vs_gptqmodel_gguf.py: usedsetdefaultaftergptqmodelwas imported, so the line had no effect..github/workflows/unit_tests.ymlandunit_tests_reusable.ymlstill exportPYTORCH_ALLOC_CONF: 'expandable_segments:True'. That is CI configuration and is left for maintainers. The new subprocess tests strip it through the existing_child_env()helper.Tests
New tests in
tests/test_rocm_default_env.py:test_requant_tool_leaves_the_allocator_default_to_gptqmodel: a fresh interpreter imports the real tool (which runsset_env()), thengptqmodel. BothPYTORCH_ALLOC_CONFandPYTORCH_CUDA_ALLOC_CONFmust equal_alloc_conf.default_alloc_conf(). This runs on any platform.test_requant_tool_keeps_a_caller_alloc_conf: control test; an explicit caller value survives.test_no_source_forces_expandable_segments_outside_alloc_conf: an AST guard over the repo's.pyfiles. It rejects anyos.environ[...] =,os.environ.setdefaultoros.putenvwrite ofexpandable_segments:TruetoPYTORCH_*ALLOC_CONFoutsidegptqmodel/_alloc_conf.py, unless the write sits under anifthat checks for HIP/ROCm.Unit tests have been executed and passed, or the reason they are not applicable is documented.
I added a new simple/fast unit test for this change, or documented why that is not applicable.
Red (before the fix), on the parent commit
0275eb40:Green (after the fix):
Environment and limits: Windows, CPU-only torch 2.15.0, Python 3.14.
pypcrehas no Windows wheel, so the run used a localpcrestand-in built on the stdlibremodule (not committed).tests/test_requant_embed_lm_head.py. They fail before and after this change becausefaulthandler.registerdoes not exist on Windows.Model Support / Module Plan
Not applicable: no model or definition changes.
AWQ onboarding safety
Not applicable: no AWQ changes.
Logic Flow Documentation
Not applicable: one tool-side env default is removed, and the existing
gptqmodel._alloc_confpath is unchanged.Review Requirements
Kernel Accuracy Requirements
Not applicable: no kernel changes.
Notes
On CUDA, the requantization tool now runs with GPT-QModel's default (
expandable_segments:True) instead of also cappingmax_split_size_mb:1024and settinggarbage_collection_threshold:0.5. That matches the default chosen in #4389 and #5108. Users can still setPYTORCH_ALLOC_CONFexplicitly.