Skip to content

Add --list-devices --json with PCI ids and device memory - #1118

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
Etherll:Etherll/list-devices-json
Oct 27, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
Etherll:Etherll/list-devices-json

Conversation

@Etherll

@Etherll Etherll commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Summary

audiocpp_cli --list-devices --json and audiocpp_server --list-devices --json print one JSON document on stdout. For each device it gives the --backend/--device pair 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 no nvidia-smi or rocm-smi needed.

The text listing is byte-identical to before, and plain --list-devices still 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):

backend index name type pci_bus_id total_bytes free_bytes
cuda 0 CUDA0 gpu 0000:01:00.0 33743044608 29778771968
vulkan 0 Vulkan0 gpu 0000:01:00.0 34190917632 29778771968
vulkan 1 Vulkan1 igpu 0000:74:00.0 16854560768 16825069568
cpu 0 CPU cpu null 32635379712 17498980352

One 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

  • BackendDeviceInfo gains backend_option (the --backend value), device_name and description. It also gains pci_bus_id, memory and caps, which only list_backend_devices(true) fills. The default list_backend_devices() does what it did before.
  • print_backend_devices_json writes the document. The CLI reuses its existing --json flag (the one --list-loaders reads). The server checks --json only next to --list-devices. Before this PR, --list-devices --json ignored 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_snapshot holds the rule both query_backend_memory overloads 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 memory ggml_backend_dev_get_props already read, so each device is queried once.
  • While it queries devices, print_backend_devices_json sets a ggml abort callback that prints the message to stderr (see "If a device cannot be queried" below), then restores the previous one.
  • Docs: a "Listing Devices" section in docs/usage.md covers 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:

  • The JSON is written by hand, not through json::Value as --list-loaders --json is. json::Value objects are unordered_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 through json::stringify_string. The output is one line, like --list-loaders --json.
  • Unknown is null. backend is null for registries --backend can't select (BLAS, RPC). pci_bus_id is null when ggml has none. memory is null when the device reports no total, as BLAS does, or a free figure above its total. That uses the same rule as BackendMemorySnapshot::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 without VK_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.
  • CPU free memory. Off Windows, ggml's CPU device reports free == total ("free system memory is ill-defined", ggml-cpu.cpp). That would be an invented number, so the CPU entry uses available_host_memory_bytes() instead. That is MemAvailable on Linux, ullAvailPhys on Windows, and free + inactive + purgeable pages on macOS. It is the same host figure --min-free-memory-mb checks. The total is still ggml's physical RAM.
  • index is per registry, like --device. CLI --device and server device / models[].device both reach init_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 through init_backend with 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.

Backend total_bytes free_bytes
CUDA, HIP cudaMemGetInfo / hipMemGetInfo. On NVIDIA this is nvidia-smi's memory.total minus memory.reserved. Free from the same call, read after the listing created its own context. Linux integrated GPUs, and any device while GGML_CUDA_ENABLE_UNIFIED_MEMORY is set (any value, 0 included), use MemAvailable instead, against the device's own total.
Vulkan Device-local heaps; every heap on an integrated GPU VK_EXT_memory_budget budget minus usage. Without that extension, ggml reports the heap size, so free == total whatever is in use.
Metal recommendedMaxWorkingSetSize That minus currentAllocatedSize, which counts only this process's allocations
CPU Physical RAM Available host memory (see above)

Does --json create a GPU context?

On CUDA and HIP, yes. That's why only --json pays the cost.

  • The text listing runs ggml_cuda_init and registry init: cudaGetDeviceProperties, cuDeviceGetAttribute, cudaDeviceGetPCIBusId. None of these creates a context. ggml has two exceptions: compute capability 12.1 devices and GGML_CUDA_P2P.
  • ggml_backend_cuda_device_get_props calls get_memory, which runs cudaSetDevice + cudaMemGetInfo and so creates the primary context (ggml-cuda.cu:5602-5660). Asking for the PCI id through get_props therefore 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 with nvidia-smi while gdb held the process at exit().

Wall time VRAM held by the process
--list-devices 50 ms none (not in --query-compute-apps)
--list-devices --json 192 ms 498 MiB, released at exit

On a Vulkan-only build, --json and text both took 47 ms, and --json held no VRAM. ggml-vulkan reads memory from the VkPhysicalDevice without creating a VkDevice.

Side effects:

  • CUDA free_bytes is read after the listing's own context exists, so it is about 500 MiB lower than nvidia-smi shows 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.
  • Memory is read once per device: one cudaMemGetInfo per 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_CHECK aborts 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:

  • The listing sets a ggml abort callback that writes the message to stderr. ggml's default handler forks gdb, and the gdb backtrace goes to stdout, so without the callback a caller would read ~1.5 KB of gdb text instead of JSON.
  • The document is built before anything is written, so a caller sees a non-zero exit (134) and empty stdout, never partial JSON.
  • The plain --list-devices still works in that state, and docs/usage.md says so.

Reporting memory: null for just that device would need ggml's CUDA get_memory to 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:

cmake -S . -B build-cpu -G Ninja -DCMAKE_BUILD_TYPE=Release -DAUDIOCPP_MODEL_SET=core -DENGINE_BUILD_TESTS=ON
cmake -S . -B build-vulkan -G Ninja -DCMAKE_BUILD_TYPE=Release -DAUDIOCPP_MODEL_SET=core -DENGINE_BUILD_TESTS=ON -DENGINE_ENABLE_VULKAN=ON
cmake -S . -B build-cuda-vulkan -G Ninja -DCMAKE_BUILD_TYPE=Release -DAUDIOCPP_MODEL_SET=core -DENGINE_BUILD_TESTS=ON \
  -DENGINE_ENABLE_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=120 -DENGINE_ENABLE_VULKAN=ON
cmake --build <dir> --target audiocpp_cli --target audiocpp_server --target backend_device_resolution_test
Check CPU Vulkan CUDA + Vulkan
Text --list-devices vs origin/main, CLI and server identical identical identical
--list-devices --json: exit 0, one line, python3 -I -m json.tool parses it ✓ ✓ ✓
Nothing but the JSON on stdout (ggml banners on stderr) ✓ ✓ ✓
backend_device_resolution_test pass pass pass
ctest (all 61 tests in the core build) pass
CUDA context failure injected (cudaMemGetInfo returns 46 or 2): --json exit 134 with 0 bytes on stdout; text exit 0 ✓ (CLI and server)
  • PCI id: CUDA0 and Vulkan0 are both 0000:01:00.0, and nvidia-smi shows 00000000:01:00.0. The iGPU is 0000:74:00.0.
  • Total memory: Vulkan reports 34190917632, exactly nvidia-smi's 32607 MiB. CUDA reports 33743044608, exactly NVML's total minus reserved (34190917632 − 447873024). cudaMemGetInfo leaves out the 428 MiB the driver reserves.
  • CPU memory: total 32635379712 equals MemTotal (31870488 kB). Free tracks MemAvailable.
  • Device visibility:
    • GGML_VK_VISIBLE_DEVICES=1 makes Vulkan0 the iGPU.
    • =1,0 gives Vulkan0 the iGPU and Vulkan1 the 5090. The test passes in each case, opening every listed backend/index.
    • CUDA_VISIBLE_DEVICES=0 matches unset. CUDA_VISIBLE_DEVICES='' drops the CUDA entry. There is only one NVIDIA GPU here, so CUDA reordering is untested.
    • With CUDA_VISIBLE_DEVICES='', the test's existing first check fails ("CUDA registry exposes no devices"). That check is unchanged from main.
  • Text format test: adding a trailing space to the text output makes the new test fail.
  • Backend mapping test: making the CPU registry map to no --backend value makes the test fail with "CPU:0 backend must not be null".
  • Failure injection: an LD_PRELOAD shim makes cudaMemGetInfo return cudaErrorDevicesUnavailable (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:

build-cuda-vulkan/bin/audiocpp_cli --list-devices --json | python3 -I -m json.tool
build-cuda-vulkan/bin/audiocpp_server --list-devices --json | python3 -I -m json.tool
GGML_VK_VISIBLE_DEVICES=1,0 build-cuda-vulkan/bin/backend_device_resolution_test
ctest --test-dir build-cpu --output-on-failure

New tests in test_backend_device_resolution.cpp:

  • The text listing is checked byte for byte against what ggml reports.
  • The JSON lists the same devices in the same order as the text.
  • Each backend/index opens that exact device through init_backend.
  • Every registry --backend can select (CPU, CUDA/MUSA, ROCm, Vulkan, MTL) has the matching backend value, checked against a table in the test, and the rest are null. The CPU entry always has memory.
  • pci_bus_id and total memory match ggml.
  • A synthetic case covers exact integers beyond 2^53, escaped strings and nulls.

Not tested here: macOS/Metal, Windows, ROCm/HIP and MUSA. They use the same ggml_backend_dev_get_props / ggml_backend_dev_memory calls, 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/devices on the server, built on the same device list.
  • Per-model or per-process memory in /v1/models.
  • Any change to the text format.
  • A GGML PR so CUDA get_memory reports failure instead of aborting (the listing would then print memory: null for that device), and a way to read the CUDA/HIP PCI id without creating a context. get_props always queries memory.

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.
@0xShug0
0xShug0 merged commit bdf78b9 into 0xShug0:main Oct 27, 2026
12 checks passed
@0xShug0

0xShug0 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @Etherll PR merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants