Skip to content

[BUG][ROCm] #5165 (fix of #5121) incomplete: optimize/_common.set_env() still forces PYTORCH_ALLOC_CONF=expandable_segments:True (the documented corrupting config) before import gptqmodel - requant_embed_lm_head on ROCm: plain matmul+empty_cache 1.57M wrong rows #5199

Closed

Description

@Qubitium

Found on main @ 396b5f0, AMD Radeon AI PRO R9700 (gfx1201), torch 2.16.0.dev+rocm10.1.

What is wrong

#5165 (the fix for #5121 item 1) removed PYTORCH_ALLOC_CONF="expandable_segments:True" from the four docs/eora/ scripts. The reason given was that on ROCm expandable_segments:True plus torch.cuda.empty_cache() corrupts ordinary Torch results (#4072, docs/rocm-default-environment.md).

The repository's quantization CLI tools still install that same configuration on every platform. optimize/_common.py:17-26:

def set_env() -> None:
    """Configure process defaults before importing numerical libraries."""
    ...
    os.environ.setdefault(
        "PYTORCH_ALLOC_CONF",
        "expandable_segments:True,max_split_size_mb:1024,garbage_collection_threshold:0.5",
    )

optimize/requant_embed_lm_head.py:35 calls set_env() at import time, before gptqmodel is imported. GPT-QModel's ROCm default (expandable_segments:False) defers to a caller-set PYTORCH_ALLOC_CONF, so it keeps this value and prints no warning. The tool then requantizes with the configuration that docs/rocm-default-environment.md calls corrupting, and quantization calls empty_cache() throughout. This also brings back the max_split_size_mb cap that #5108 (#5072) just removed from the ROCm default.

Repro (pure Torch after the tool's env setup, no quantization)

import os, sys
mode = sys.argv[1]
if mode == "optimize":
    from optimize._common import set_env      # what optimize/requant_embed_lm_head.py does first
    set_env()
if mode in ("optimize", "gptqmodel"):
    import gptqmodel                          # ROCm default defers to the value set above
import torch
print("PYTORCH_ALLOC_CONF =", os.environ.get("PYTORCH_ALLOC_CONF"))
torch.manual_seed(0)
a = torch.randn(2097152, 64, device="cuda"); b = torch.randn(64, 16, device="cuda")
ref = (a @ b).cpu()
bad = 0
for rep in range(3):
    junk = [torch.empty(512 << 20, dtype=torch.uint8, device="cuda") for _ in range(4)]
    del junk
    torch.cuda.empty_cache()
    out = (a @ b).cpu()
    bad += int(((out - ref).abs().amax(1) > 1e-3).sum())
print("bad rows", bad)

Run from the repo root on gfx1201:

plain       PYTORCH_ALLOC_CONF = None                                                                   bad rows 0
gptqmodel   PYTORCH_ALLOC_CONF = expandable_segments:False                                              bad rows 0
optimize    PYTORCH_ALLOC_CONF = expandable_segments:True,max_split_size_mb:1024,garbage_collection_threshold:0.5   bad rows 1572864

Suggested fix

Drop the PYTORCH_ALLOC_CONF line from set_env(). import gptqmodel already installs the right per-platform default: expandable_segments:True on CUDA, expandable_segments:False on ROCm. If the tool really needs its own value, set it only when the host is not ROCm (check torch.version.hip after import, or the same running_on_rocm() probe _alloc_conf uses). A repo-wide test should reject any expandable_segments:True literal outside _alloc_conf.py that is not guarded for ROCm. The #5165 fix only covered docs/eora/.

Severity

Medium to high: silent result corruption when the shipped requantization CLI runs on ROCm. The fix is one line.

🤖 Generated with Claude Code

Activity

  1. n-o-t-mugen commented on Nov 18, 2026

    @n-o-t-mugen
    Contributor

    Agent claim released: agent-34732ec3ea49 finished this issue.

    Pull request: #5256

    i'd like to work on this. starting now: dropping the forced PYTORCH_ALLOC_CONF from optimize/_common.set_env() and adding a repo-wide guard test against unguarded expandable_segments:True literals

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions