stable-diffusion.cpp icon indicating copy to clipboard operation
stable-diffusion.cpp copied to clipboard

[Feature] Add abort/cancellation mechanism for interruptible generation

Open shakfu opened this issue 3 weeks ago • 2 comments

Feature Summary

Currently there's no way to abort/cancel an in-progress generate_image() or generate_video() call from the calling code.

Detailed Description

Summary

Currently there's no way to abort/cancel an in-progress generate_image() or generate_video() call from the calling code. This makes it impossible to handle user interrupts (Ctrl+C) or implement timeouts in applications using the library.

Problem

The progress callback is defined as:

typedef void (*sd_progress_cb_t)(int step, int steps, float time, void* data);

Since it returns void, there's no way for the caller to signal "please stop" back to the library.

The underlying ggml library has an abort callback mechanism (ggml_abort_callback returning bool), but this isn't exposed through the stable-diffusion.cpp API.

Suggested Solutions

Any of these would work:

  1. Change progress callback to return bool - Return false to continue, true to abort:
typedef bool (*sd_progress_cb_t)(int step, int steps, float time, void* data);
  1. Add a separate abort callback - Expose ggml's mechanism:
typedef bool (*sd_abort_cb_t)(void* data);

SD_API void sd_set_abort_callback(sd_abort_cb_t cb, void* data);
  1. Add explicit cancel function - Set a flag that's checked during iteration:
SD_API void sd_cancel(sd_ctx_t* sd_ctx);

Use Case

I'm developing Python bindings for stable-diffusion.cpp. Without an abort mechanism, the only option is to kill the process.

Related user-reported issue

Alternatives you considered

No response

Additional context

No response

shakfu avatar Dec 03 '25 13:12 shakfu

I think ggml mechanism to abort during graph compute only works in CPU backends? But we could easily make it interrupt between calls to the forward pass of the diffusion model, it just wouldn't help much in cases where each forward pass takes forever.

stduhpf avatar Dec 03 '25 14:12 stduhpf

SD_API void sd_set_abort_callback(sd_abort_cb_t cb, void* data);

I'd avoid that name in any case, because ggml_set_abort_callback does something entirely different:

https://github.com/ggml-org/ggml/blob/2d3876d5/src/ggml.c#L205 https://github.com/ggml-org/ggml/blob/master/include/ggml.h#L339

The unrelated bool (*ggml_abort_callback)(void *) pointer is indeed just used by a few backends (CPU and Metal).

Anyway, IMHO an asynchronous cancellation function is likely the most flexible approach, as it can be triggered from a separate thread or from the existing progress or preview callbacks.

wbruna avatar Dec 03 '25 23:12 wbruna