Repository navigation
Fix bitsandbytes ROCm GPU arch and warp size detection on Windows - #6127
Conversation
bitsandbytes resolves the ROCm GPU architecture (and warp size on 0.49.x) by shelling out to rocminfo / hipinfo.exe via PATH at import time. On Windows neither tool is normally on PATH (AMD torch wheels ship hipInfo.exe into the venv Scripts dir, only on PATH while activated), so every `import bitsandbytes` logs an ERROR and WARNING, ROCM_GPU_ARCH degrades to unknown, and the 0.49.x warp size defaults to 64, which is wrong on RDNA (wave 32) and silently disables pre-quantized 4-bit models via ALLOW_PREQUANTIZED_MODELS. Install a one-shot MetaPathFinder before unsloth_zoo is imported (the first bitsandbytes import on ROCm) that swaps get_rocm_gpu_arch and get_rocm_warpsize for torch-device-properties-first implementations right after bitsandbytes.cuda_specs executes, before cextension reads them. Falls back to running hipInfo.exe by absolute path (venv Scripts, conda Scripts, HIP SDK / AMD installer dirs). Repairs the constants in place when bitsandbytes was imported first. Strict no-op on non-Windows, non-ROCm builds, missing bitsandbytes, and versions that fix this upstream. Opt out with UNSLOTH_DISABLE_BNB_ROCM_FIX=1. Proposed upstream in bitsandbytes-foundation/bitsandbytes#1969; shipped here so all bitsandbytes versions are covered. Verified on gfx1151 Strix Halo, Windows 11, torch 2.11.0+rocm7.13.0 against bitsandbytes main, 0.49.2, and a torch-props-fixed variant.
There was a problem hiding this comment.
Code Review
This pull request introduces a fix for bitsandbytes ROCm architecture and warp-size detection on Windows by installing a custom import hook to patch bitsandbytes.cuda_specs at import time, replacing subprocess-based detection with a torch-device-properties-first approach. It also adds a drift test to ensure compatibility with future bitsandbytes updates. A review comment suggests defensively decoding props.gcnArchName in case it is returned as a bytes object in certain PyTorch builds, preventing potential failures in architecture detection.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| except Exception: | ||
| pass | ||
| for hipinfo_path in _iter_hipinfo_paths(): | ||
| match = re.search(r"gcnArchName:\s+gfx([a-zA-Z\d]+)", _run_hipinfo(hipinfo_path)) |
There was a problem hiding this comment.
In some PyTorch builds or custom environments, props.gcnArchName can be returned as a bytes object rather than a str. Calling str(props.gcnArchName) on a bytes object results in a string representation like "b'gfx90a'", which will fail the subsequent .startswith("gfx") check. Defensively decoding the attribute if it is an instance of bytes ensures robust architecture detection.
arch = props.gcnArchName.decode("utf-8", errors="ignore") if isinstance(props.gcnArchName, bytes) else str(props.gcnArchName)
arch = arch.split(":")[0].strip()Comment and docstring pass only. AST comparison with docstrings stripped confirms every definition is identical to the version the 12 scenario suite ran against, and the suite plus the drift test pass unchanged on the edited files.
Simulation testing caught a regression in the one-shot design: importlib.reload(bitsandbytes.cuda_specs) re-resolves the spec through sys.meta_path, so with the finder already removed the reload reinstalled the unpatched upstream detector and the Windows ROCm noise returned. Keep the finder on sys.meta_path permanently, matching the lifecycle of the existing causal_conv1d and vllm import blockers. The finder matches a single module name and patching stays idempotent via the sentinel flags, so repeat hits are no-ops. Validated on gfx1151 Windows 11: 22 simulation scenarios (conda and embedded layouts, Program Files scan ordering, paths with spaces and unicode, hanging probe timeout, lru-wrapped and C-function helper shapes, reload, failed-import retry, threads, spawn, dormant finder, Studio PATH coexistence, early fix-block ordering, bnb 0.45.5 / 0.47.0 / 0.49.2 / main / upstream-fixed) plus the original 12 scenario suite, CPU-torch and stale-HIP_PATH sandboxes, Python 3.10 to 3.13 gates, and a WSL Linux leg proving byte-identical Linux behavior with and without the fix, with and without rocminfo on PATH.
Problem
On Windows ROCm,
import bitsandbytesshells out torocminfo(bnb 0.47.0 to 0.49.2) orhipinfo.exe(bnb main, since bitsandbytes-foundation/bitsandbytes#1846) via PATH to detect the GPU architecture at import time. Neither tool is normally on PATH: the HIP SDK does not add its bin directory, and AMD's PyTorch wheels shiphipInfo.exeinto the venvScriptsdirectory, which is only on PATH while the venv is activated. Running throughuv run, invokingpython.exedirectly, or embedding the interpreter all hit this. Every import then logs:and
ROCM_GPU_ARCHdegrades to"unknown".On bnb 0.49.0 to 0.49.2 it is worse:
get_rocm_warpsize()defaults to 64 on failure, which is wrong for every Windows ROCm GPU (RDNA is wave 32). That mis-gates 4-bit blocksize selection inside bitsandbytes and flipsALLOW_PREQUANTIZED_MODELSto False throughdevice_type.py, silently disabling pre-quantized 4-bit models on Windows AMD boxes.I proposed the fix upstream in bitsandbytes-foundation/bitsandbytes#1969, but shipping it here covers every bitsandbytes version users already have installed.
Fix
New
fix_bitsandbytes_rocm_arch_detection()inimport_fixes.py, called from the early fix block of_gpu_init.py. It has to run there: on ROCm the first bitsandbytes import happens insideimport unsloth_zoo(device_type.pyimports bitsandbytes at module scope), long before the late fix block.MetaPathFinderfor exactlybitsandbytes.cuda_specs. The module executes normally, thenget_rocm_gpu_arch(andget_rocm_warpsizewhere present) are swapped beforecextension.pycalls them at module scope. The noise is prevented rather than suppressed and the constants are computed correctly from the start. The finder stays installed (same lifecycle as the existing causal_conv1d and vllm blockers) soimportlib.reload(bitsandbytes.cuda_specs)re-applies the patch; it matches a single module name and repeat hits are idempotent.torch.cuda.get_device_properties(0)first (gcnArchName,warp_size): no subprocess, PATH independent. The fallback runshipInfo.exeby absolute path (PATH, venv scripts dir, condaScripts,HIP_PATH/ROCM_PATH, AMD installer dirs) with a timeout and no console window. The last resort returns upstream's own defaults, quietly.ROCM_WARP_SIZE_64infunctional.py/nn/modules.py/backends/cuda/ops.py.UNSLOTH_DISABLE_BNB_ROCM_FIX=1.Compatibility
_gpu_init).Testing
Verified on a gfx1151 (Strix Halo) Windows 11 machine, torch 2.11.0+rocm7.13.0, with the venv not activated so
hipinfo.exeis not PATH resolvable. 12 scenarios against three bitsandbytes trees (main, 0.49.2, and a torch-props-fixed variant), each in a fresh interpreter:unknownarch, and the wrongROCM_WARP_SIZE_64 = Trueon 0.49.2.ROCM_GPU_ARCH = "gfx1151",ROCM_WARP_SIZE_64 = False(including the copy inbackends/cuda/ops.py), zero ERROR / WARNING records captured.subprocess.runproves the arch comes from torch device properties; blindingtorch.cuda.is_availableproves the absolute-pathhipInfo.exefallback also returnsgfx1151.Follow-up simulation matrix (all passing):
sys.executable, brokensysconfig),Program Files\AMD\ROCmscan with newest-version-first ordering, hipInfo paths containing spaces and unicode, a hanging probe binary (bounded by the timeout), zero-byte probe binaries.lru_cachewrapped broken helper (unwrapped and patched), a C-function helper (declined), an unrecognized rewrite (declined by design, covered by the new drift test).importlib.reload(bitsandbytes.cuda_specs)re-applies the patch (this run caught that the original one-shot finder regressed on reload, fixed by keeping the finder installed), failed-import-then-retry, two threads importing concurrently, a multiprocessing spawn child, and a dormant finder leaving unrelated imports untouched.darwinandlinuxgates install nothing, a real CPU-torch venv installs nothing, a staleHIP_PATHon CPU torch stays value-identical to upstream, and Python 3.10 through 3.13 gates all pass in isolated uv venvs._gpu_initearly fix-block call order, Studio's PATH prepend, andUNSLOTH_ENABLE_LOGGING=1all work unchanged.