DiffusionEngine

stable-diffusion.cpp text-to-image engine with streamed progress and cooperative cancellation.

Create instances with the DiffusionEngine factory functions. Engines are ordinary objects — never singletons — and any number of them can coexist and generate concurrently: each owns its native context (weights + backends) and a dedicated single-threaded dispatcher (SerialExecutor) that serializes loading, every generation and the native release in close.

Cancellation is cooperative at step granularity: cancelling the collector of generate (or abortActiveGeneration / close) latches the native abort flag, and stable-diffusion.cpp stops at its next progress point — after the denoising step (or VAE tile) that is computing finishes. No native image buffer outlives a call, and the engine stays usable afterwards.

Types

Link copied to clipboard
object Companion

Constants and factory functions of DiffusionEngine.

Properties

Link copied to clipboard
open override val activeBackend: ComputeBackend

The backend stable-diffusion.cpp actually runs on (never ComputeBackend.AUTO).

Link copied to clipboard
open override val activeDeviceName: String?

The GPU stable-diffusion.cpp runs on, or null on the CPU (see InferenceEngine.activeDeviceName).

Link copied to clipboard
open override val backendConfig: BackendConfig

The backend configuration this engine was loaded with.

Link copied to clipboard
open override val backendFallbackReason: String?

Why activeBackend is not the requested backend, or null (see InferenceEngine.backendFallbackReason).

Link copied to clipboard
open override val isClosed: Boolean

true once close has been called.

Link copied to clipboard

true while a generation is executing native code on this engine.

Link copied to clipboard

The model files this engine was loaded from.

Link copied to clipboard

Static properties of the loaded model.

Link copied to clipboard

Diffusion-specific construction options.

Link copied to clipboard

true if a GPU device was handed to stable-diffusion.cpp; always false for ComputeBackend.CPU_ONLY.

Functions

Link copied to clipboard

Requests the native abort of the generation currently executing, if any; its generate flow then ends with a terminal DiffusionEvent.Error (InferenceCancelledException) at the next step boundary. Safe from any thread. Queued generations are not affected.

Link copied to clipboard
open override fun close()

Closes the engine: aborts a running generation, rejects queued and future calls with IllegalStateException, frees the native context on the engine thread, and stops that thread. Idempotent and safe from any thread.

Link copied to clipboard
open suspend override fun closeAndAwait()

closes the engine and suspends until its native memory has been freed on the engine thread (after a running call has been aborted and left native code). Idempotent; cancelling the caller stops the wait, not the release. See InferenceEngine.closeAndAwait.

Link copied to clipboard

Generates one image from params as a cold Flow of DiffusionEvents. Each collection runs one generation on the engine thread (collections on the same engine are serialized).

Link copied to clipboard

Runs generate to completion and returns the image. onProgress (optional) receives every DiffusionEvent.Progress on the calling coroutine.