Contributing¶
Running the tests¶
The unit tests need pytest and psutil and no hardware; vendor outputs are replayed from fixtures.
pip install -e . pytest
python -m pytest tests -q
With uv, nothing needs to be installed in the environment:
uv run --no-project --with pytest --with psutil python -m pytest tests -q
One test is macOS-only (it compares the IOKit and ioreg paths on the same machine) and is skipped elsewhere.
Hardware test scripts¶
vendor_devices_tests/test_<vendor>.py queries one backend on the current machine and prints the devices and the JSON. The scripts add the checkout to sys.path, so they run from a clone without installing:
python vendor_devices_tests/test_amd.py
If a backend marked untested in Backends produces correct output on your hardware, open an issue with the script output, the vendor tool version and the device name, so the status can be updated.
Adding a backend¶
A backend is one module with two classes and a parser. Use xpustat/moorethreads.py as the smallest example and xpustat/apple.py as an example with a fast path and a fallback.
- The stat class. Subclass
DeviceMixinfromxpustat/_device.py, setvendorto the key and_index_attrto the name of the attribute holding the physical index (_sub_index_attras well when devices are addressed as card:chip). Store at leastname,mem_used_mbandmem_total_mbas integers andprocessesas a list ofProcessInfo. Fields the tool cannot report areNone, not guessed.to_dict()lists the vendor fields plusmem_free_mb. - The collection class. Hold the list, implement
to_dict,__len__,__iter__,__getitem__and__repr__, and anew_query()classmethod.new_querychecksshutil.whichfor the tool, runs it withsubprocess.run(..., capture_output=True, timeout=...), catchesFileNotFoundErrorandsubprocess.TimeoutExpired, and returns an empty collection on any failure. If the tool needs several calls, submit them to aThreadPoolExecutorso they run concurrently. - The parser. A module-level function that takes the tool's text and returns a list of stat objects, with no subprocess inside, so it can be tested from a fixture. Skip malformed entries rather than raising.
- Registration. Add the module to
xpustat/xpu.py: the import, theAnyDeviceStatunion, a keyword argument with aNonedefault onXPUStatCollection.__init__,all_devices,to_dictand the_BACKENDSlist; then the exports inxpustat/__init__.py. - CLI. Add a colour and a label of at most six characters to
_VENDORSinxpustat/cli.pyand a branch in_fieldsreturning the index string, name, architecture tag, memory, utilization, temperature, power and processes. - Framework mapping. Add the vendor to
TORCH_DEVICE_TYPEandVISIBLE_DEVICES_ENVinxpustat/_device.py, withNoneand an empty tuple when there is no PyTorch backend or no visible-devices variable. - Tests and docs. A parser test from a captured output in
tests/test_vendors.py, theto_dictkey intests/test_xpu.py, a script invendor_devices_tests/, a row in the README table and a section in Backends stating the command, the fields and what is not reported.
Keep the existing JSON keys and constructor signatures unchanged; new fields are added, not renamed.
Plugin backends¶
A vendor can live outside this repository. Expose the collection class as an entry point:
[project.entry-points."xpustat.backends"]
acme = "xpustat_acme:AcmeStatCollection"
or register it at runtime with xpustat.register_backend("acme", AcmeStatCollection). The key must be a lowercase identifier that is not a built-in vendor. The contract is the one in the steps above: AcmeStatCollection([]) is empty, AcmeStatCollection.new_query() never raises, instances support len(), iteration, indexing and to_dict() with one list under a key of your choice, and each device subclasses xpustat.DeviceMixin with vendor and _index_attr set. Plugins get a generic CLI label (the first six letters of the key, upper case); add the key to TORCH_DEVICE_TYPE, VISIBLE_DEVICES_ENV, RAY_RESOURCE and K8S_RESOURCE from your package if the vendor has them. A plugin that fails to import is skipped; set XPUSTAT_DEBUG=1 to see why.
Conventions¶
- Vendor tools are the source of truth. When a tool does not report a value, the field is
Noneor 0 and the documentation says so; the value is not estimated. - Backends never raise out of
new_query. A broken vendor tool on one machine must not hide the other vendors. - Query cost matters because watch mode and the Prometheus exporter call
query_allrepeatedly. Prefer one tool invocation, run independent invocations concurrently, and prefer an in-process library over a subprocess when one exists.