Repository navigation
Add --list-devices --json with PCI ids and device memory - #1118
Merged
Merged
Conversation
audiocpp_cli and audiocpp_server --list-devices --json print one JSON document (schema_version 1) on stdout: for each ggml device, the --backend/--device pair that selects it, registry, ggml name and description, type, PCI bus id, free/total memory as exact integers, and caps. Unknown values are null. The plain listing is unchanged and still creates no GPU context. On CUDA/HIP, reading device properties creates the primary context, so only --json pays for it. While listing, a ggml abort handler keeps a failed device query from writing a gdb backtrace to stdout. docs/usage.md documents the fields, what free memory means per backend, the context cost and the failure behavior.
Owner
|
Thanks @Etherll PR merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
audiocpp_cli --list-devices --jsonandaudiocpp_server --list-devices --jsonprint one JSON document on stdout. For each device it gives the--backend/--devicepair that selects it, the PCI bus id, free and total memory, and the ggml caps. Tools that run audio.cpp as a subprocess can use it to pick a device and check that a model fits before loading it. It works the same on every backend ggml has (CUDA, HIP, Vulkan, Metal, CPU), with nonvidia-smiorrocm-smineeded.The text listing is byte-identical to before, and plain
--list-devicesstill creates no GPU context.Output
From a CUDA + Vulkan build on an RTX 5090 with a Ryzen 9 9950X3D (its Raphael iGPU shows up under Vulkan):
cudavulkanvulkancpuOne entry in full (the real output is one line):
{ "backend": "cuda", "registry": "CUDA", "index": 0, "name": "CUDA0", "description": "NVIDIA GeForce RTX 5090", "type": "gpu", "pci_bus_id": "0000:01:00.0", "memory": {"free_bytes": 29778771968, "total_bytes": 33743044608}, "caps": {"async": true, "host_buffer": true, "buffer_from_host_ptr": false, "events": true} }The same GPU listed by CUDA and by Vulkan has the same
pci_bus_id, so a caller can match them up.What changed
BackendDeviceInfogainsbackend_option(the--backendvalue),device_nameanddescription. It also gainspci_bus_id,memoryandcaps, which onlylist_backend_devices(true)fills. The defaultlist_backend_devices()does what it did before.print_backend_devices_jsonwrites the document. The CLI reuses its existing--jsonflag (the one--list-loadersreads). The server checks--jsononly next to--list-devices. Before this PR,--list-devices --jsonignored the flag and printed the text listing, so a caller can detect an older binary by stdout not starting with{(the docs say so).memory_snapshotholds the rule bothquery_backend_memoryoverloads already applied (no total, or free above total, means not available). The overloads and the listing share it, and the overloads behave exactly as before. The listing builds its snapshot from the memoryggml_backend_dev_get_propsalready read, so each device is queried once.print_backend_devices_jsonsets a ggml abort callback that prints the message to stderr (see "If a device cannot be queried" below), then restores the previous one.docs/usage.mdcovers the fields and what free memory means on each backend. The server README's "Per-model devices" and the README's CLI list link to it.Choices that differ from a plain dump of ggml's props:
json::Valueas--list-loaders --jsonis.json::Valueobjects areunordered_maps, so key order would vary. Its numbers are doubles, which cJSON prints with%1.15g, so integers are exact only below 10^15. Strings still go throughjson::stringify_string. The output is one line, like--list-loaders --json.null.backendis null for registries--backendcan't select (BLAS, RPC).pci_bus_idis null when ggml has none.memoryis null when the device reports no total, as BLAS does, or a free figure above its total. That uses the same rule asBackendMemorySnapshot::available, which the server's memory guard already relies on. ggml itself falls back to an estimate in two places, and the docs name both: the CPU (replaced, see below) and Vulkan withoutVK_EXT_memory_budget, where ggml reports the heap size as free. audio.cpp can't tell that case apart through ggml's API, so the docs say free equals total there whatever is in use.ggml-cpu.cpp). That would be an invented number, so the CPU entry usesavailable_host_memory_bytes()instead. That isMemAvailableon Linux,ullAvailPhyson Windows, and free + inactive + purgeable pages on macOS. It is the same host figure--min-free-memory-mbchecks. The total is still ggml's physical RAM.indexis per registry, like--device. CLI--deviceand serverdevice/models[].deviceboth reachinit_backend→find_device_by_backend_type→ggml_backend_reg_dev_get(reg, index). That is the same enumeration the listing walks. The new test opens every listed device throughinit_backendwith its listed backend and index, and checks that it gets back the same ggml device object.What "free" means
Except on Metal, free memory is device-wide: it includes what other processes hold.
total_bytesfree_bytescudaMemGetInfo/hipMemGetInfo. On NVIDIA this isnvidia-smi'smemory.totalminusmemory.reserved.GGML_CUDA_ENABLE_UNIFIED_MEMORYis set (any value,0included), useMemAvailableinstead, against the device's own total.VK_EXT_memory_budgetbudget minus usage. Without that extension, ggml reports the heap size, so free == total whatever is in use.recommendedMaxWorkingSetSizecurrentAllocatedSize, which counts only this process's allocationsDoes
--jsoncreate a GPU context?On CUDA and HIP, yes. That's why only
--jsonpays the cost.ggml_cuda_initand registry init:cudaGetDeviceProperties,cuDeviceGetAttribute,cudaDeviceGetPCIBusId. None of these creates a context. ggml has two exceptions: compute capability 12.1 devices andGGML_CUDA_P2P.ggml_backend_cuda_device_get_propscallsget_memory, which runscudaSetDevice+cudaMemGetInfoand so creates the primary context (ggml-cuda.cu:5602-5660). Asking for the PCI id throughget_propstherefore costs a context too.Measured on an RTX 5090 (driver 615.78, CUDA 13.4) with
GGML_DISABLE_VULKAN=1. Time is the median of 10 runs. VRAM was read withnvidia-smiwhile gdb held the process atexit().--list-devices--query-compute-apps)--list-devices --jsonOn a Vulkan-only build,
--jsonand text both took 47 ms, and--jsonheld no VRAM. ggml-vulkan reads memory from theVkPhysicalDevicewithout creating aVkDevice.Side effects:
free_bytesis read after the listing's own context exists, so it is about 500 MiB lower thannvidia-smishows once the process exits. Any later audio.cpp process needs a context too, so this is the figure to check a model against. In a CUDA + Vulkan build, Vulkan's figure for the same GPU is read after the CUDA context exists, so the two match.cudaMemGetInfoper CUDA device, checked under gdb.If a device cannot be queried
If a CUDA or HIP device can't give the process a context (an exclusive-process GPU another process holds, or a GPU too full for a new context), ggml's
CUDA_CHECKaborts the process, and the other devices are not listed either. ggml has no non-aborting way to read the PCI id or memory, so this PR doesn't try to recover. Instead:--list-devicesstill works in that state, anddocs/usage.mdsays so.Reporting
memory: nullfor just that device would need ggml's CUDAget_memoryto stop aborting. That is a GGML change, so it belongs in its own PR (see follow-ups).Testing
Linux (CachyOS), RTX 5090 (driver 615.78) + Raphael iGPU (RADV), Ryzen 9 9950X3D, GCC 16.2, CUDA 13.4, CMake 4.4. Builds use
AUDIOCPP_MODEL_SET=core, because device listing doesn't touch model code:--list-devicesvsorigin/main, CLI and server--list-devices --json: exit 0, one line,python3 -I -m json.toolparses itbackend_device_resolution_testctest(all 61 tests in the core build)cudaMemGetInforeturns 46 or 2):--jsonexit 134 with 0 bytes on stdout; text exit 0CUDA0andVulkan0are both0000:01:00.0, andnvidia-smishows00000000:01:00.0. The iGPU is0000:74:00.0.nvidia-smi's 32607 MiB. CUDA reports 33743044608, exactly NVML's total minus reserved (34190917632 − 447873024).cudaMemGetInfoleaves out the 428 MiB the driver reserves.MemTotal(31870488 kB). Free tracksMemAvailable.GGML_VK_VISIBLE_DEVICES=1makesVulkan0the iGPU.=1,0givesVulkan0the iGPU andVulkan1the 5090. The test passes in each case, opening every listed backend/index.CUDA_VISIBLE_DEVICES=0matches unset.CUDA_VISIBLE_DEVICES=''drops the CUDA entry. There is only one NVIDIA GPU here, so CUDA reordering is untested.CUDA_VISIBLE_DEVICES='', the test's existing first check fails ("CUDA registry exposes no devices"). That check is unchanged frommain.--backendvalue makes the test fail with "CPU:0 backend must not be null".LD_PRELOADshim makescudaMemGetInforeturncudaErrorDevicesUnavailable(46, what an exclusive-process GPU in use returns) or out of memory (2). Without the abort callback, stdout held 1457-1517 bytes of gdb output. With it, stdout is empty, the error is on stderr, and the plain listing is unaffected.Run commands:
New tests in
test_backend_device_resolution.cpp:backend/indexopens that exact device throughinit_backend.--backendcan select (CPU, CUDA/MUSA, ROCm, Vulkan, MTL) has the matchingbackendvalue, checked against a table in the test, and the rest arenull. The CPU entry always has memory.pci_bus_idand total memory match ggml.nulls.Not tested here: macOS/Metal, Windows, ROCm/HIP and MUSA. They use the same
ggml_backend_dev_get_props/ggml_backend_dev_memorycalls, and HIP and MUSA share the CUDA registry code. Linux CI runs the new tests on CPU and Vulkan. macOS CI (Metal off, ggml BLAS on by default on Apple) runs them with a BLAS registry, which covers the null-backend and null-memory paths. Windows CI doesn't run this test.Follow-ups (not in this PR)
GET /v1/deviceson the server, built on the same device list./v1/models.get_memoryreports failure instead of aborting (the listing would then printmemory: nullfor that device), and a way to read the CUDA/HIP PCI id without creating a context.get_propsalways queries memory.