Skip to content
Merged
Changes from 1 commit
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
39 changes: 33 additions & 6 deletions ramalama/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,39 @@ 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

def parse_props(path):
with open(path) as file:
return {key: int(value) for key, _, value in (line.partition(' ') for line in file)}

def kfd_gpus():
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

for i, (np, props) in enumerate(kfd_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 = parse_props(np + f'/mem_banks/{bank}/properties')
# 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
Preview

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 > 1073741824 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