Skip to content

fix(optimize): stop forcing expandable_segments:True before import gptqmodel (#5199) - #5256

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
n-o-t-mugen:fix/5199-optimize-rocm-alloc-conf
Nov 18, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
n-o-t-mugen:fix/5199-optimize-rocm-alloc-conf

Conversation

@n-o-t-mugen

Copy link
Copy Markdown
Contributor

Summary

optimize/_common.set_env() runs before import gptqmodel and set PYTORCH_ALLOC_CONF=expandable_segments:True,max_split_size_mb:1024,garbage_collection_threshold:0.5. GPT-QModel keeps a value the caller already set, so optimize/requant_embed_lm_head.py never got the ROCm default (expandable_segments:False). On ROCm it ran with the configuration that docs/rocm-default-environment.md says corrupts ordinary Torch results after empty_cache() (1,572,864 wrong rows in the issue's repro). It also brought back the max_split_size_mb cap that #5108 removed. #5165 fixed the same problem only in docs/eora/.

Fixes #5199

What Changed

  • optimize/_common.py: set_env() no longer sets PYTORCH_ALLOC_CONF, so import gptqmodel installs the per-platform default. A caller-set value still wins.
  • The repo-wide guard below found the same pinned literal in three more Python files, so it is removed there too:
    • tests/benchmark/benchmark_test.py: assigned it before import gptqmodel, which overrode the default.
    • scripts/eval_model.py and scripts/benchmark_llama_cpp_vs_gptqmodel_gguf.py: used setdefault after gptqmodel was imported, so the line had no effect.
  • No API changes.
  • Out of scope: .github/workflows/unit_tests.yml and unit_tests_reusable.yml still export PYTORCH_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 runs set_env()), then gptqmodel. Both PYTORCH_ALLOC_CONF and PYTORCH_CUDA_ALLOC_CONF must 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 .py files. It rejects any os.environ[...] =, os.environ.setdefault or os.putenv write of expandable_segments:True to PYTORCH_*ALLOC_CONF outside gptqmodel/_alloc_conf.py, unless the write sits under an if that 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:

pytest -q tests/test_rocm_default_env.py -k "requant_tool or no_source_forces"
# test_requant_tool_leaves_the_allocator_default_to_gptqmodel FAILED
#   assert 'expandable_segments:True,max_split_size_mb:1024,garbage_collection_threshold:0.5' == 'expandable_segments:True'
# test_no_source_forces_expandable_segments_outside_alloc_conf FAILED
#   optimize/_common.py:23, scripts/benchmark_llama_cpp_vs_gptqmodel_gguf.py:24,
#   scripts/eval_model.py:30, tests/benchmark/benchmark_test.py:11
# test_requant_tool_keeps_a_caller_alloc_conf PASSED
# 2 failed, 1 passed

Green (after the fix):

pytest -q tests/test_rocm_default_env.py -k "requant_tool or no_source_forces"
# 3 passed, 17 deselected

pytest -q tests/test_requant_embed_lm_head.py tests/test_docs_contract_refs.py \
  tests/test_rocm_default_env.py tests/test_alloc_conf.py \
  tests/test_rocm_alloc_conf_5072.py tests/test_cuda_alloc_conf_4389.py
# 84 passed, 9 skipped, 10 failed. The failures are identical before and after
# the change (see below).

bash format/check.sh   # ruff 0.14.2: All checks passed!

Environment and limits: Windows, CPU-only torch 2.15.0, Python 3.14. pypcre has no Windows wheel, so the run used a local pcre stand-in built on the stdlib re module (not committed).

  • The 10 failures are all in tests/test_requant_embed_lm_head.py. They fail before and after this change because faulthandler.register does not exist on Windows.
  • The 9 skips need ROCm or CUDA.
  • The ROCm corruption repro from the issue was not rerun: no ROCm hardware was available.

Model Support / Module Plan

Not applicable: no model or definition changes.

AWQ onboarding safety

Not applicable: no AWQ changes.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.

Not applicable: one tool-side env default is removed, and the existing gptqmodel._alloc_conf path is unchanged.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

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 capping max_split_size_mb:1024 and setting garbage_collection_threshold:0.5. That matches the default chosen in #4389 and #5108. Users can still set PYTORCH_ALLOC_CONF explicitly.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants