Skip to content

Commit 3835b63

Browse files
fix(backend): detect AMD GPU before setting HSA_OVERRIDE_GFX_VERSION (#785)
Previously, HSA_OVERRIDE_GFX_VERSION=10.3.0 was unconditionally set for all AMD GPUs, which caused suboptimal performance on RDNA 3/4 GPUs (gfx11xx/gfx12xx) that have native ROCm support. Now uses rocminfo to detect all GPUs and only sets the override for systems where the oldest GPU needs it (RDNA 2 and older, gfx10xx and below). Newer GPUs are left untouched. Addresses CodeRabbit review: - Case-insensitive regex matching on lowercased line - Log level changed to INFO for rocminfo failures - Multi-GPU support: iterates all GPUs, uses oldest for decision Fixes #469 Signed-off-by: Amitesh Gupta Signed-off-by: Amitesh Gupta Signed-off-by: singlaamitesh <singlaamitesh@gmail.com>
1 parent da79e37 commit 3835b63

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

backend/app.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import asyncio
44
import logging
55
import os
6+
import re
7+
import subprocess
68
import sys
79
from contextlib import asynccontextmanager
810
from pathlib import Path
@@ -37,8 +39,59 @@ def format(self, record):
3739
logger = logging.getLogger(__name__)
3840

3941
# AMD GPU environment variables must be set before torch import
42+
# Only set HSA_OVERRIDE_GFX_VERSION for older GPUs that need it.
43+
# RDNA 3+ (gfx1100+) and RDNA 4 (gfx1200+) are natively supported by ROCm
44+
# and the override can cause suboptimal performance or errors.
4045
if not os.environ.get("HSA_OVERRIDE_GFX_VERSION"):
41-
os.environ["HSA_OVERRIDE_GFX_VERSION"] = "10.3.0"
46+
try:
47+
result = subprocess.run(
48+
["rocminfo"],
49+
capture_output=True,
50+
text=True,
51+
timeout=5,
52+
)
53+
if result.returncode == 0:
54+
# Collect all GPUs found in rocminfo output
55+
gfx_versions = []
56+
for line in result.stdout.splitlines():
57+
line_lower = line.lower()
58+
if "gfx" in line_lower:
59+
match = re.search(r"(gfx\d+)", line_lower)
60+
if match:
61+
gfx_versions.append(match.group(1))
62+
63+
if gfx_versions:
64+
# Check if any GPU needs the override (RDNA 2 and older)
65+
# Use the oldest GPU (lowest gfx number) for the decision
66+
try:
67+
gfx_nums = []
68+
for v in gfx_versions:
69+
m = re.search(r"\d+", v)
70+
if m:
71+
gfx_nums.append(int(m.group()))
72+
if gfx_nums:
73+
oldest_num = min(gfx_nums)
74+
oldest_gfx = gfx_versions[gfx_nums.index(oldest_num)]
75+
if oldest_num < 1100:
76+
os.environ["HSA_OVERRIDE_GFX_VERSION"] = "10.3.0"
77+
logger.info(
78+
"AMD GPU detected (%s), setting HSA_OVERRIDE_GFX_VERSION=10.3.0 for compatibility. All GPUs: %s",
79+
oldest_gfx,
80+
", ".join(gfx_versions),
81+
)
82+
else:
83+
logger.info(
84+
"AMD GPU detected (%s), native ROCm support available, skipping HSA_OVERRIDE_GFX_VERSION. All GPUs: %s",
85+
oldest_gfx,
86+
", ".join(gfx_versions),
87+
)
88+
except (ValueError, AttributeError) as e:
89+
logger.info("Could not parse GPU version from rocminfo output: %s", e)
90+
except (FileNotFoundError, subprocess.TimeoutExpired, Exception) as e:
91+
logger.info(
92+
"Could not detect AMD GPU via rocminfo, skipping automatic HSA_OVERRIDE_GFX_VERSION configuration: %s",
93+
e,
94+
)
4295
if not os.environ.get("MIOPEN_LOG_LEVEL"):
4396
os.environ["MIOPEN_LOG_LEVEL"] = "4"
4497

0 commit comments

Comments
 (0)