Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ramalama/amdkfd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""utilities for working with AMDKFD driver"""

import glob


def parse_props(path):
"""Returns a dict corresponding to a KFD properties file"""
with open(path) as file:
return {key: int(value) for key, _, value in (line.partition(' ') for line in file)}


def gpus():
"""Yields GPU nodes within KFD topology and their properties"""
for np in sorted(glob.glob('/sys/devices/virtual/kfd/kfd/topology/nodes/*')):
props = parse_props(np + '/properties')

# Skip CPUs
if props['gfx_target_version'] == 0:
continue

yield np, props
27 changes: 21 additions & 6 deletions ramalama/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import urllib.error
from typing import List

import ramalama.amdkfd as amdkfd
import ramalama.console as console
from ramalama.http_client import HttpClient
from ramalama.logger import logger
Expand All @@ -35,6 +36,8 @@

DEFAULT_IMAGE = "quay.io/ramalama/ramalama"

MIN_VRAM_BYTES = 1073741824 # 1GiB


_engine = -1 # -1 means cached variable not set yet
_nvidia = -1 # -1 means cached variable not set yet
Expand Down Expand Up @@ -447,12 +450,24 @@ def check_ascend():
def check_rocm_amd():
gpu_num = 0
gpu_bytes = 0
for i, fp in enumerate(sorted(glob.glob('/sys/bus/pci/devices/*/mem_info_vram_total'))):
with open(fp, 'r') as file:
content = int(file.read())
if content > 1073741824 and content > gpu_bytes:
gpu_bytes = content
gpu_num = i
for i, (np, props) in enumerate(amdkfd.gpus()):
# Radeon GPUs older than gfx900 are not supported by ROCm (e.g. Polaris)
if props['gfx_target_version'] < 90000:
continue

mem_banks_count = int(props['mem_banks_count'])
mem_bytes = 0
for bank in range(mem_banks_count):
bank_props = amdkfd.parse_props(np + f'/mem_banks/{bank}/properties')
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Path construction via string concatenation can be fragile; prefer os.path.join(np, 'mem_banks', str(bank), 'properties').

Suggested change
bank_props = amdkfd.parse_props(np + f'/mem_banks/{bank}/properties')
bank_props = amdkfd.parse_props(os.path.join(np, 'mem_banks', str(bank), 'properties'))

Copilot uses AI. Check for mistakes.
# See /usr/include/linux/kfd_sysfs.h for possible heap types
#
# Count public and private framebuffer memory as VRAM
if bank_props['heap_type'] in [1, 2]:
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Magic numbers 1 and 2 for heap types are unclear; consider defining named constants or an enum for readability and future maintenance.

Suggested change
if bank_props['heap_type'] in [1, 2]:
if bank_props['heap_type'] in [HEAP_TYPE_PUBLIC, HEAP_TYPE_PRIVATE]:

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is correct we should try and take this is another PR @alaviss . I have no idea what 1 and 2 mean.

mem_bytes += int(bank_props['size_in_bytes'])

if mem_bytes > MIN_VRAM_BYTES and mem_bytes > gpu_bytes:
gpu_bytes = mem_bytes
gpu_num = i

if gpu_bytes:
os.environ["HIP_VISIBLE_DEVICES"] = str(gpu_num)
Expand Down
Loading