ggml: fix mem_hip incorrectly reporting zero memory for AMD APUs (#8735)

AMD APUs (eg. MI300A) report zero VRAM due to unified CPU/GPU memory.
In this case, GPU-accessible memory is exposed via GTT.
This commit is contained in:
Javier Cacheiro 2025-12-14 01:48:20 +01:00
parent 4ff8a691bc
commit cfa6102c54

View file

@ -461,6 +461,8 @@ int ggml_hip_get_device_memory(const char *id, size_t *free, size_t *total) {
const std::string drmTotalMemoryFile = "mem_info_vram_total";
const std::string drmUsedMemoryFile = "mem_info_vram_used";
const std::string drmUeventPCISlotLabel = "PCI_SLOT_NAME=";
const std::string drmGttTotalMemoryFile = "mem_info_gtt_total";
const std::string drmGttUsedMemoryFile = "mem_info_gtt_used";
glob_t glob_result;
glob(drmDeviceGlob.c_str(), GLOB_NOSORT, NULL, &glob_result);
@ -497,7 +499,29 @@ int ggml_hip_get_device_memory(const char *id, size_t *free, size_t *total) {
totalFileStream >> memory;
*total = memory;
std::string usedFile = dir + "/" + drmUsedMemoryFile;
// AMD APUs report zero VRAM due to unified CPU/GPU memory
// GPU-accessible memory is exposed via GTT
if (memory == 0) {
std::string gttTotalFile = dir + "/" + drmGttTotalMemoryFile;
std::ifstream gttTotalFileStream(gttTotalFile.c_str());
if (!gttTotalFileStream.is_open()) {
GGML_LOG_DEBUG("%s Failed to read sysfs node %s\n", __func__, gttTotalFile.c_str());
file.close();
globfree(&glob_result);
return 1;
}
gttTotalFileStream >> memory;
*total = memory;
}
std::string usedFile;
if (*total == 0) {
usedFile = dir + "/" + drmGttUsedMemoryFile;
} else {
usedFile = dir + "/" + drmUsedMemoryFile;
}
std::ifstream usedFileStream(usedFile.c_str());
if (!usedFileStream.is_open()) {
GGML_LOG_DEBUG("%s Failed to read sysfs node %s\n", __func__, usedFile.c_str());
@ -526,4 +550,4 @@ int ggml_hip_get_device_memory(const char *id, size_t *free, size_t *total) {
} // extern "C"
#endif // #ifdef _WIN32
#endif // #ifdef _WIN32