Skip to content

Commit 497e20c

Browse files
committed
Only enumerate ROCm-capable AMD GPUs
Discover AMD graphics devices using AMDKFD topology instead of enumerating the PCIe bus. This interface exposes a lot more information about potential devices, allowing RamaLama to filter out unsupported devices. Currently, devices older than GFX9 are filtered, as they are no longer supported by ROCm. Signed-off-by: Leorize <[email protected]>
1 parent 83a75f1 commit 497e20c

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

ramalama/common.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,39 @@ def check_ascend():
447447
def check_rocm_amd():
448448
gpu_num = 0
449449
gpu_bytes = 0
450-
for i, fp in enumerate(sorted(glob.glob('/sys/bus/pci/devices/*/mem_info_vram_total'))):
451-
with open(fp, 'r') as file:
452-
content = int(file.read())
453-
if content > 1073741824 and content > gpu_bytes:
454-
gpu_bytes = content
450+
451+
def parse_props(path):
452+
with open(path) as file:
453+
return {key: value for key, _, value in (line.rstrip().partition(' ') for line in file)}
454+
455+
def kfd_gpus():
456+
for np in sorted(glob.glob('/sys/devices/virtual/kfd/kfd/topology/nodes/*')):
457+
props = parse_props(np + '/properties')
458+
459+
# Skip CPUs
460+
if props['gfx_target_version'] == '0':
461+
continue
462+
463+
yield np, props
464+
465+
for i, (np, props) in enumerate(kfd_gpus()):
466+
gfx_target_version = int(props['gfx_target_version'])
467+
# Radeon GPUs older than gfx900 are not supported by ROCm (e.g. Polaris)
468+
if gfx_target_version < 90000:
469+
continue
470+
471+
mem_banks_count = int(props['mem_banks_count'])
472+
mem_bytes = 0
473+
for bank in range(mem_banks_count):
474+
bank_props = parse_props(np + f'/mem_banks/{bank}/properties')
475+
# See /usr/include/linux/kfd_sysfs.h for possible heap types
476+
match bank_props['heap_type']:
477+
# Count public and private framebuffer memory as VRAM
478+
case '1' | '2':
479+
mem_bytes += int(bank_props['size_in_bytes'])
480+
481+
if mem_bytes > 1073741824 and mem_bytes > gpu_bytes:
482+
gpu_bytes = mem_bytes
455483
gpu_num = i
456484

457485
if gpu_bytes:

0 commit comments

Comments
 (0)