Skip to content

feat: add sd_device_memory to query device memory - #2483

Open
shiwano wants to merge 1 commit into
leejet:masterfrom
shiwano:sd-device-memory
Open

shiwano wants to merge 1 commit into
leejet:masterfrom
shiwano:sd-device-memory

Conversation

@shiwano

@shiwano shiwano commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Applications using stable-diffusion.h can query free and total memory for each device listed by sd_list_devices.

  • Add sd_device_memory(index, &free_bytes, &total_bytes). The index follows the sd_list_devices order, and the values come from ggml_backend_dev_memory.
  • It returns false for an out-of-range index or a null output pointer.
  • Like sd_list_devices, it calls ggml_backend_load_all() when no device is registered, so dynamic-backend builds work without listing devices first.

Related Issue / Discussion

Closes #2482.

Additional Information

Tested a C caller against the current implementation with the CUDA 12.8 build's ggml libraries.

  • Queried index 0 before calling sd_list_devices. The query succeeded.
  • Enumerated CUDA0 (RTX 5090) and CPU. Both queries succeeded with a positive total and free bytes at or below the total.
  • Index 2, the first index past the two listed devices, returned false.
  • Passing null for either output pointer or for both returned false.

Checklist

Summary by CodeRabbit

  • New Features
    • Added an API to retrieve a device’s free and total memory in bytes by its listed index. It returns an error result for an invalid index or missing output values.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Adds sd_device_memory, a public C API that returns free and total memory for a device by its sd_list_devices index.

Changes: Device Memory Query API

Layer / File(s) Summary
Declare and implement device memory query
include/stable-diffusion.h, src/core/util.cpp
The public declaration documents the device index and failure conditions. The implementation checks output pointers and index, loads backends if no devices are registered, and retrieves the selected device’s memory values.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Feature

Suggested reviewers: leejet, fszontagh

Merge Risk: Minimal · up to 1c92d

This adds a small, additive C API for querying device memory. Existing APIs are unchanged and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a new public function, sd_device_memory, that queries device memory. It is short, specific, and follows the conventional commit style. The changeset adds this function…
Linked Issues check Passed [#2482] The issue asks for a public C API that reports free and total memory for a device in sd_list_devices order. The whole-PR summary shows `bool sd_device_memory(size_t index, size_t* free_bytes…
Out of Scope Changes check Passed The whole-PR summary lists only two changed files: include/stable-diffusion.h (one added declaration) and src/core/util.cpp (the added function). Both changes implement the requested `sd_device_me…
Description check Passed The description covers the change well. The Summary states that the PR adds sd_device_memory(index, &free_bytes, &total_bytes), that the index follows sd_list_devices order, that values come from …
Full details: Title check

Explanation

The title names the main change: a new public function, sd_device_memory, that queries device memory. It is short, specific, and follows the conventional commit style. The changeset adds this function to include/stable-diffusion.h and src/core/util.cpp, which matches the title.

Full details: Linked Issues check

Explanation

[#2482] The issue asks for a public C API that reports free and total memory for a device in sd_list_devices order. The whole-PR summary shows bool sd_device_memory(size_t index, size_t* free_bytes, size_t* total_bytes) added to include/stable-diffusion.h (+4 lines) and implemented in src/core/util.cpp (+14 lines). The summary states that the function returns false for a null output pointer or an out-of-range index, and that it loads backend devices when none are registered before checking the index. The PR description states that the values come from ggml_backend_dev_memory. The author reports tests with a C caller: a query before sd_list_devices succeeded, CUDA0 and CPU returned positive totals with free bytes at or below total, index 2 returned false, and null pointers returned false. Limit: the repository diff tool could not read the changed blobs, and the head file view of src/core/util.cpp was truncated before the new function. The function body was therefore not read directly. The assessment relies on the supplied summary and PR description.

Full details: Out of Scope Changes check

Explanation

The whole-PR summary lists only two changed files: include/stable-diffusion.h (one added declaration) and src/core/util.cpp (the added function). Both changes implement the requested sd_device_memory API. No unrelated changes appear in the summary. The raw diff could not be read, so this conclusion relies on the summary's file list and line counts.

Full details: Description check

Explanation

The description covers the change well. The Summary states that the PR adds sd_device_memory(index, &free_bytes, &total_bytes), that the index follows sd_list_devices order, that values come from ggml_backend_dev_memory, and that null pointers or out-of-range indices return false. It also explains the backend-loading behavior. The Related Issue section links issue #2482. The Additional Information section lists concrete test results from a CUDA 12.8 build, including queries before sd_list_devices, device enumeration, the out-of-range index check, and null-pointer checks. The checklist is filled in. The template's required sections are all present and substantive.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a292c2da-ed3a-4a1b-b8f9-4d5a0258eec4
Commits

Reviewing files that changed from the base of the PR and between f93d45b and 1c92d3e.

Files selected for processing: 2

  • include/stable-diffusion.h
  • src/core/util.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

[Feature] C API: query free and total memory of a listed device

1 participant