From d695a40591986f9f7fe11eb156a62196429894ed Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sat, 14 Feb 2026 02:44:34 -0800 Subject: [PATCH] Suppress HIP amdgpu.ids stderr noise during causal_conv1d check (#4052) * Suppress HIP libdrm stderr noise in causal_conv1d probe * Broaden HIP libdrm stderr suppression for early ROCm startup --------- Co-authored-by: Daniel Hanchen --- unsloth/__init__.py | 21 ++++++++++------- unsloth/import_fixes.py | 52 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/unsloth/__init__.py b/unsloth/__init__.py index a505e89ad4..990e4b915f 100644 --- a/unsloth/__init__.py +++ b/unsloth/__init__.py @@ -29,6 +29,7 @@ from .import_fixes import ( fix_message_factory_issue, check_fbgemm_gpu_version, disable_broken_causal_conv1d, + _suppress_hip_libdrm_ids_noise, torchvision_compatibility_check, fix_diffusers_warnings, fix_huggingface_hub, @@ -106,7 +107,8 @@ del PackageNotFoundError, importlib_version # Try importing PyTorch and check version try: - import torch + with _suppress_hip_libdrm_ids_noise(): + import torch except ModuleNotFoundError: raise ImportError( "Unsloth: Pytorch is not installed. Go to https://pytorch.org/.\n" @@ -115,14 +117,15 @@ except ModuleNotFoundError: except: raise -from unsloth_zoo.device_type import ( - is_hip, - get_device_type, - DEVICE_TYPE, - DEVICE_TYPE_TORCH, - DEVICE_COUNT, - ALLOW_PREQUANTIZED_MODELS, -) +with _suppress_hip_libdrm_ids_noise(): + from unsloth_zoo.device_type import ( + is_hip, + get_device_type, + DEVICE_TYPE, + DEVICE_TYPE_TORCH, + DEVICE_COUNT, + ALLOW_PREQUANTIZED_MODELS, + ) # Fix other issues from .import_fixes import ( diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py index d037903bec..0cac361888 100644 --- a/unsloth/import_fixes.py +++ b/unsloth/import_fixes.py @@ -16,6 +16,7 @@ import os import importlib.abc import importlib.machinery import importlib.util +import contextlib from pathlib import Path from importlib.metadata import version as importlib_version from packaging.version import Version as TrueVersion @@ -1175,6 +1176,54 @@ _CAUSAL_CONV1D_PREFIX = "causal_conv1d" _CAUSAL_CONV1D_BLOCKER_SENTINEL = "_unsloth_causal_conv1d_blocker" +def _is_rocm_torch_build() -> bool: + try: + return "rocm" in str(importlib_version("torch")).lower() + except Exception: + return False + + +@contextlib.contextmanager +def _suppress_stderr_fd(): + saved_stderr_fd = None + devnull_fd = None + redirected = False + try: + saved_stderr_fd = os.dup(2) + devnull_fd = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull_fd, 2) + redirected = True + except Exception: + redirected = False + + try: + yield + finally: + if redirected and saved_stderr_fd is not None: + try: + os.dup2(saved_stderr_fd, 2) + except Exception: + pass + if devnull_fd is not None: + try: + os.close(devnull_fd) + except Exception: + pass + if saved_stderr_fd is not None: + try: + os.close(saved_stderr_fd) + except Exception: + pass + + +def _suppress_hip_libdrm_ids_noise(): + # ROCm/libdrm can emit amdgpu.ids missing errors via low-level fd=2 writes. + # Python-level stderr filters cannot intercept those writes. + if not _is_rocm_torch_build(): + return contextlib.nullcontext() + return _suppress_stderr_fd() + + def _is_causal_conv1d_name(module_name: str) -> bool: return module_name == _CAUSAL_CONV1D_PREFIX or module_name.startswith( _CAUSAL_CONV1D_PREFIX + "." @@ -1307,7 +1356,8 @@ def disable_broken_causal_conv1d(): return try: - import causal_conv1d # noqa: F401 + with _suppress_hip_libdrm_ids_noise(): + import causal_conv1d # noqa: F401 return except Exception as error: