mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +00:00
The values of device health decided on 25 November (3 levels of strain, 15% of the capacity and 25% of the CPU threads per level, a quiet period of 300 s, and 10 s under which two engine events count once) can now be read and changed. Without the new option nothing changes. - C API, additions only: sd_device_health_settings_t, sd_get_device_health_settings() and sd_set_device_health_settings(). - sd-cli and sd-server: --device-health with any of the keys levels, step, cpu-step, quiet and spacing, e.g. --device-health quiet=600. It applies as soon as the options are read, in the DGL mode of sd-cli too; values against the rules stop the program at start and say the rules, and the log names the values used. - The registry refuses waits longer than a day and steps whose product with the levels would not fit in an int. When the values change, every component keeps its state: before, fewer levels could turn a level of strain into an exclusion, and more levels could bring an excluded device back under strain. The same values written again no longer change the revision, which makes the next generation place its models again. - termico.py --segnala reads the levels of its device from /sdcpp/v1/health and from the answers to its reports, and never sends more than the last level of strain: with two levels the critical band is level 2, not an exclusion. Docs: a "Device health" section in docs/guides/backend.md, examples/server/api.md and the server README, the README of scripts/diagnostica_termica, test/README.md and the fork history. test: device_health_test covers the new rules and the C API (135 checks), device_health_option_test is new (30), termico_test covers engines with two levels, one level and none declared (161), and server_guardian_e2e.py starts its second server with --device-health levels=2,step=20,quiet=5 and checks the option in sd-cli (70 checks, three runs in a row with a synthetic SD 1.5 model). Every program of test/ passes, the server tests under ThreadSanitizer too, and 29 broken versions of the new code (registry and C API 12, option 9, termico.py 6, the calls in sd-server and sd-cli 2) all make the tests fail. Not tried: real GPUs, Windows. Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
678 lines
29 KiB
C
678 lines
29 KiB
C
#ifndef __STABLE_DIFFUSION_H__
|
|
#define __STABLE_DIFFUSION_H__
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
#ifndef SD_BUILD_SHARED_LIB
|
|
#define SD_API
|
|
#else
|
|
#ifdef SD_BUILD_DLL
|
|
#define SD_API __declspec(dllexport)
|
|
#else
|
|
#define SD_API __declspec(dllimport)
|
|
#endif
|
|
#endif
|
|
#else
|
|
#if __GNUC__ >= 4
|
|
#define SD_API __attribute__((visibility("default")))
|
|
#else
|
|
#define SD_API
|
|
#endif
|
|
#endif
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// Opaque handles
|
|
|
|
typedef struct sd_tensor_t sd_tensor_t;
|
|
typedef struct sd_ctx_t sd_ctx_t;
|
|
typedef struct sd_result sd_result_t;
|
|
typedef struct sd_opt sd_opt_t;
|
|
typedef struct sd_upscaler sd_upscaler_t;
|
|
typedef struct sd_dgl_ctx_t sd_dgl_ctx_t;
|
|
typedef struct sd_dgl_result_t sd_dgl_result_t;
|
|
typedef struct sd_dgl_value_t sd_dgl_value_t;
|
|
|
|
// Status and diagnostics
|
|
|
|
typedef enum {
|
|
SD_STATUS_OK = 0,
|
|
SD_STATUS_INVALID_ARGUMENT = 1,
|
|
SD_STATUS_UNKNOWN_KEY = 2,
|
|
SD_STATUS_TYPE_MISMATCH = 3,
|
|
SD_STATUS_OUT_OF_RANGE = 4,
|
|
SD_STATUS_UNSUPPORTED = 5,
|
|
SD_STATUS_OUT_OF_MEMORY = 6,
|
|
SD_STATUS_LOAD_FAILED = 7,
|
|
SD_STATUS_GENERATION_FAILED = 8,
|
|
SD_STATUS_BUSY = 9,
|
|
SD_STATUS_CANCELLED = 10,
|
|
} sd_status_t;
|
|
|
|
// Thread-local diagnostic, cleared by successful status/accessor calls.
|
|
// Valid until the next API call; free functions and tensor retain preserve it.
|
|
SD_API const char* sd_get_last_error(void);
|
|
|
|
// Generation metadata
|
|
|
|
// Returns inferred image parameters and infotext as UTF-8 JSON, or {} when no
|
|
// image parameters can be inferred. Does not execute source or read files.
|
|
// Borrowed until the next call to this function on the same thread.
|
|
// Returns NULL on failure; see sd_get_last_error().
|
|
SD_API const char* sd_get_generation_metadata(const char* source);
|
|
|
|
typedef enum {
|
|
SD_METADATA_READ_JSON = 1u << 0,
|
|
SD_METADATA_READ_RAW = 1u << 1,
|
|
SD_METADATA_READ_BRIEF = 1u << 2,
|
|
SD_METADATA_READ_STRUCTURAL = 1u << 3,
|
|
} sd_metadata_read_flags_t;
|
|
|
|
// Reads container metadata without loading a model. Flags may be ORed together;
|
|
// zero selects the default text report. Returns NULL on failure.
|
|
// Borrowed until the next call to this function on the same thread.
|
|
SD_API const char* sd_read_metadata(const char* path, uint32_t flags);
|
|
|
|
typedef enum {
|
|
SD_METADATA_PNG,
|
|
SD_METADATA_JPEG,
|
|
SD_METADATA_XMP,
|
|
SD_METADATA_AVI,
|
|
} sd_metadata_format_t;
|
|
|
|
// Builds PNG iTXt chunks, JPEG COM segments, UTF-8 XMP or an AVI INFO chunk.
|
|
// PNG/JPEG/XMP contain the DGL source and inferred infotext; AVI stores source.
|
|
// Does not write files. Borrowed bytes remain valid until the next call to this
|
|
// function on the same thread. Empty source succeeds with NULL, 0.
|
|
// Output pointers are required and cleared on failure.
|
|
SD_API sd_status_t sd_get_metadata_chunk(const char* source, sd_metadata_format_t format, const void** data_out, size_t* size_out);
|
|
|
|
// File access for generation and DGL contexts
|
|
|
|
typedef enum {
|
|
SD_DGL_FILE_READ,
|
|
SD_DGL_FILE_READ_MODEL,
|
|
SD_DGL_FILE_WRITE,
|
|
} sd_dgl_file_access_t;
|
|
|
|
// Return false to reject the access. The UTF-8 path is valid only during the callback.
|
|
typedef bool (*sd_dgl_file_access_cb_t)(const char* path, sd_dgl_file_access_t access, void* userdata);
|
|
|
|
// Tensors and media
|
|
|
|
typedef int32_t sd_tensor_type_t;
|
|
#define SD_TENSOR_U8 1
|
|
#define SD_TENSOR_I32 2
|
|
#define SD_TENSOR_I64 3
|
|
#define SD_TENSOR_F32 4
|
|
#define SD_TENSOR_F64 5
|
|
|
|
typedef struct {
|
|
uint32_t sample_rate;
|
|
const sd_tensor_t* data;
|
|
} sd_audio_t;
|
|
|
|
typedef struct {
|
|
const sd_tensor_t* data;
|
|
int fps;
|
|
sd_audio_t audio;
|
|
} sd_video_t;
|
|
|
|
// Contiguous dimension first. Images: U8 [C,W,H], audio: F32 [C,N], video: U8 [C,W,H,T].
|
|
// create copies data; data_size must exactly match type and shape. Rank <= 8.
|
|
// Rank zero is a scalar; zero dimensions represent empty tensors.
|
|
SD_API sd_tensor_t* sd_tensor_create(
|
|
sd_tensor_type_t type,
|
|
size_t rank,
|
|
const int64_t* shape,
|
|
const void* data,
|
|
size_t data_size);
|
|
|
|
// retain returns the same immutable handle with one additional ownership reference.
|
|
SD_API sd_tensor_t* sd_tensor_retain(const sd_tensor_t* tensor);
|
|
SD_API void sd_tensor_free(sd_tensor_t* tensor);
|
|
SD_API sd_tensor_type_t sd_tensor_type(const sd_tensor_t* tensor);
|
|
SD_API size_t sd_tensor_rank(const sd_tensor_t* tensor);
|
|
SD_API int64_t sd_tensor_dim(const sd_tensor_t* tensor, size_t axis);
|
|
SD_API size_t sd_tensor_byte_size(const sd_tensor_t* tensor);
|
|
SD_API const void* sd_tensor_data(const sd_tensor_t* tensor);
|
|
|
|
// Saves a NumPy .npy file. NumPy shape is the reverse of sd_tensor shape.
|
|
// Failures use sd_get_last_error().
|
|
SD_API bool sd_tensor_save(const sd_tensor_t* tensor, const char* path);
|
|
|
|
// Options
|
|
|
|
typedef struct {
|
|
const char* name;
|
|
const char* path;
|
|
} sd_embedding_t;
|
|
|
|
typedef struct {
|
|
bool is_high_noise;
|
|
float multiplier;
|
|
const char* path;
|
|
} sd_lora_t;
|
|
|
|
typedef sd_opt_t sd_ctx_opt_t;
|
|
typedef sd_opt_t sd_img_gen_opt_t;
|
|
typedef sd_opt_t sd_vid_gen_opt_t;
|
|
typedef sd_opt_t sd_upscaler_opt_t;
|
|
typedef sd_opt_t sd_upscale_opt_t;
|
|
|
|
// Parameter keys and types are documented in docs/guides/c_api.md.
|
|
// Setters replace values atomically; a failed setter preserves the previous value.
|
|
// Strings, arrays and media descriptors are copied. Tensor storage is retained;
|
|
// callers may free their tensor handles after a successful setter.
|
|
// Do not mutate/free params concurrently with an operation using them.
|
|
// The aliases share one C type; constructors bind their purpose at runtime.
|
|
SD_API sd_ctx_opt_t* sd_ctx_opt_create(void);
|
|
SD_API sd_img_gen_opt_t* sd_img_gen_opt_create(void);
|
|
SD_API sd_vid_gen_opt_t* sd_vid_gen_opt_create(void);
|
|
SD_API sd_upscaler_opt_t* sd_upscaler_opt_create(void);
|
|
SD_API sd_upscale_opt_t* sd_upscale_opt_create(void);
|
|
SD_API void sd_opt_free(sd_opt_t* params);
|
|
|
|
SD_API sd_status_t sd_opt_set_bool(sd_opt_t* params, const char* key, bool value);
|
|
SD_API sd_status_t sd_opt_set_int(sd_opt_t* params, const char* key, int64_t value);
|
|
SD_API sd_status_t sd_opt_set_float(sd_opt_t* params, const char* key, double value);
|
|
SD_API sd_status_t sd_opt_set_string(sd_opt_t* params, const char* key, const char* value);
|
|
SD_API sd_status_t sd_opt_set_tensor(sd_opt_t* params, const char* key, const sd_tensor_t* tensor);
|
|
SD_API sd_status_t sd_opt_set_tensors(
|
|
sd_opt_t* params,
|
|
const char* key,
|
|
const sd_tensor_t* const* tensors,
|
|
size_t count);
|
|
SD_API sd_status_t sd_opt_set_floats(sd_opt_t* params, const char* key, const float* values, size_t count);
|
|
SD_API sd_status_t sd_opt_set_ints(sd_opt_t* params, const char* key, const int32_t* values, size_t count);
|
|
SD_API sd_status_t sd_opt_set_loras(sd_opt_t* params, const char* key, const sd_lora_t* values, size_t count);
|
|
SD_API sd_status_t sd_opt_set_embeddings(
|
|
sd_opt_t* params,
|
|
const char* key,
|
|
const sd_embedding_t* values,
|
|
size_t count);
|
|
SD_API sd_status_t sd_opt_set_audios(sd_opt_t* params, const char* key, const sd_audio_t* values, size_t count);
|
|
SD_API sd_status_t sd_opt_set_videos(sd_opt_t* params, const char* key, const sd_video_t* values, size_t count);
|
|
|
|
// Setters/unset reject keys for another purpose. Unsetting an applicable known
|
|
// absent key succeeds; unknown keys fail. NULL is not a string value.
|
|
SD_API sd_status_t sd_opt_unset(sd_opt_t* params, const char* key);
|
|
|
|
// Context and generation
|
|
|
|
// Output pointers are required and set to NULL on failure. Context creation retains
|
|
// its own configuration; params may be freed immediately after the call returns.
|
|
SD_API sd_status_t sd_ctx_create(const sd_ctx_opt_t* params, sd_ctx_t** ctx_out);
|
|
SD_API void sd_ctx_free(sd_ctx_t* ctx);
|
|
// Borrows the context used for generation, or returns NULL if ctx is NULL.
|
|
// It shares component caches and can run scripts between generation calls.
|
|
// Its lifetime is tied to ctx. Do not pass it to sd_dgl_ctx_free().
|
|
SD_API sd_dgl_ctx_t* sd_ctx_get_dgl_ctx(sd_ctx_t* ctx);
|
|
// Copies the new model configuration into an idle context. The execution
|
|
// settings, callbacks, and DGL component cache belong to the existing context.
|
|
// Returns SD_STATUS_BUSY during generation, DGL execution, or a model probe.
|
|
SD_API sd_status_t sd_ctx_set_model_options(sd_ctx_t* ctx, const sd_ctx_opt_t* params);
|
|
// Applies to generated programs and model probing. Register or clear while idle.
|
|
SD_API sd_status_t sd_ctx_set_file_access_callback(sd_ctx_t* ctx, sd_dgl_file_access_cb_t callback, void* userdata);
|
|
|
|
SD_API sd_status_t sd_generate_image(sd_ctx_t* ctx, const sd_img_gen_opt_t* params, sd_result_t** result_out);
|
|
SD_API sd_status_t sd_generate_video(sd_ctx_t* ctx, const sd_vid_gen_opt_t* params, sd_result_t** result_out);
|
|
|
|
SD_API bool sd_ctx_supports_image_generation(const sd_ctx_t* sd_ctx);
|
|
SD_API bool sd_ctx_supports_video_generation(const sd_ctx_t* sd_ctx);
|
|
|
|
// Probes model metadata lazily; returns a static string, or "Unknown" on error.
|
|
SD_API const char* sd_get_model_version_name(const sd_ctx_t* sd_ctx);
|
|
|
|
enum sd_cancel_mode_t {
|
|
// Stop the current generation as soon as possible.
|
|
SD_CANCEL_ALL,
|
|
// Finish the current image sample, then skip additional batch latents and return completed images.
|
|
SD_CANCEL_NEW_LATENTS,
|
|
// Clear a pending cancellation request.
|
|
SD_CANCEL_RESET
|
|
};
|
|
|
|
SD_API void sd_cancel_generation(sd_ctx_t* sd_ctx, enum sd_cancel_mode_t mode);
|
|
|
|
// Results
|
|
|
|
typedef enum {
|
|
SD_RESULT_INVALID = 0,
|
|
SD_RESULT_IMAGE = 1,
|
|
SD_RESULT_VIDEO = 2,
|
|
} sd_result_kind_t;
|
|
|
|
// Probes the candidate model before replacing the current configuration.
|
|
// Accepts SD_RESULT_IMAGE or SD_RESULT_VIDEO and preserves the current model on failure.
|
|
SD_API sd_status_t sd_ctx_set_model_options_for_generation(sd_ctx_t* ctx, const sd_ctx_opt_t* params, sd_result_kind_t kind);
|
|
|
|
// Results own all outputs independently of ctx and params. Views are read-only and
|
|
// borrowed until sd_result_free(). Retain a tensor to keep its data independently.
|
|
// Media descriptors contain metadata and borrowed immutable tensor handles.
|
|
SD_API sd_result_kind_t sd_result_get_kind(const sd_result_t* result);
|
|
SD_API size_t sd_result_get_image_count(const sd_result_t* result);
|
|
SD_API const sd_tensor_t* sd_result_get_image(const sd_result_t* result, size_t index);
|
|
|
|
// Materializes contiguous U8 [C,W,H,T] storage on first access.
|
|
SD_API const sd_video_t* sd_result_get_video(const sd_result_t* result);
|
|
|
|
// Returns the exact UTF-8 DGL program used for inference. Image programs are per image;
|
|
// video programs are shared by all frame indices. Host inputs must be supplied again to replay.
|
|
SD_API const char* sd_result_get_generation_info(const sd_result_t* result, size_t index);
|
|
SD_API const sd_audio_t* sd_result_get_audio(const sd_result_t* result);
|
|
SD_API int sd_result_get_fps(const sd_result_t* result);
|
|
|
|
SD_API void sd_result_free(sd_result_t* result);
|
|
|
|
// Callbacks and progress
|
|
|
|
enum sd_log_level_t {
|
|
SD_LOG_DEBUG,
|
|
SD_LOG_VERBOSE,
|
|
SD_LOG_INFO,
|
|
SD_LOG_WARN,
|
|
SD_LOG_ERROR
|
|
};
|
|
|
|
typedef void (*sd_log_cb_t)(enum sd_log_level_t level, const char* text, void* data);
|
|
SD_API void sd_set_log_callback(sd_log_cb_t sd_log_cb, void* data);
|
|
|
|
typedef void (*sd_progress_cb_t)(int step, int steps, float time, void* data);
|
|
SD_API void sd_set_progress_callback(sd_progress_cb_t cb, void* data);
|
|
|
|
typedef struct {
|
|
const char* stage;
|
|
const char* detail;
|
|
const char* unit;
|
|
int sample_pass;
|
|
int64_t completed;
|
|
int64_t total;
|
|
} sd_generation_progress_t;
|
|
typedef void (*sd_generation_progress_cb_t)(const sd_generation_progress_t* progress, void* data);
|
|
|
|
// Context-local generation events, independent of preview and the legacy progress callback.
|
|
// Strings and the event are valid only during the synchronous callback. Callbacks must not throw.
|
|
// total == 0 means unknown; sample_pass counts from 1 since callback registration.
|
|
// Register/clear only while the context is idle. Passing NULL clears the callback.
|
|
SD_API void sd_set_generation_progress_callback(sd_ctx_t* ctx, sd_generation_progress_cb_t cb, void* data);
|
|
|
|
typedef struct {
|
|
int sample_pass;
|
|
int total_steps;
|
|
} sd_preview_info_t;
|
|
|
|
// Frames are borrowed during the callback; retain a tensor to keep its snapshot.
|
|
typedef void (*sd_preview_cb_t)(int step, int frame_count, const sd_tensor_t* const* frames, bool is_noisy, void* data);
|
|
|
|
// In each sampling pass, a positive interval previews every Nth denoiser step, while a
|
|
// negative interval previews only completed logical step -interval. Zero previews the final
|
|
// completed step of the first sampling pass (base-resolution or high-noise).
|
|
// Mode is "none", "proj", "tae" or "vae"; invalid input preserves registration.
|
|
SD_API sd_status_t sd_set_preview_callback(
|
|
sd_preview_cb_t cb,
|
|
const char* mode,
|
|
int interval,
|
|
bool denoised,
|
|
bool noisy,
|
|
void* data);
|
|
|
|
// Query from a preview callback. Passes are numbered from 1 since the last
|
|
// sd_set_preview_callback call; total_steps is the actual count in that pass.
|
|
// Both fields are zero before sampling starts.
|
|
SD_API sd_preview_info_t sd_get_preview_info();
|
|
|
|
// Resolves <image:source> markers. The returned U8 [C,W,H] tensor is borrowed
|
|
// and copied before the next callback. Register/clear while ctx is idle.
|
|
typedef bool (*sd_prompt_image_cb_t)(const char* source, void* data, const sd_tensor_t** image_out);
|
|
SD_API void sd_set_prompt_image_callback(sd_ctx_t* ctx, sd_prompt_image_cb_t cb, void* data);
|
|
|
|
struct ggml_tensor;
|
|
|
|
typedef bool (*sd_graph_eval_callback_t)(struct ggml_tensor* t, bool ask, void* user_data);
|
|
SD_API void sd_set_backend_eval_callback(sd_graph_eval_callback_t cb, void* data);
|
|
|
|
// Image upscaling
|
|
|
|
SD_API sd_status_t sd_upscaler_create(const sd_upscaler_opt_t* opt, sd_upscaler_t** out);
|
|
SD_API void sd_upscaler_free(sd_upscaler_t* upscaler);
|
|
SD_API sd_status_t sd_upscale(sd_upscaler_t* upscaler, const sd_upscale_opt_t* opt, sd_result_t** out);
|
|
SD_API int sd_upscaler_get_scale(const sd_upscaler_t* upscaler);
|
|
|
|
// Reads model metadata only; returns 0 if it is not a recognized RGB upscaler.
|
|
SD_API int sd_upscaler_probe_scale(const char* model_path);
|
|
|
|
// DGL (Diffusion Graph Language)
|
|
|
|
SD_API const char* sd_dgl_builtin_catalog(void);
|
|
|
|
// A context shares loaded components across scripts and runs one script at a time.
|
|
// Keep the context alive until all calls using it return.
|
|
SD_API sd_dgl_ctx_t* sd_dgl_ctx_create(void);
|
|
// Creates a context that releases models after their last use in each run.
|
|
SD_API sd_dgl_ctx_t* sd_dgl_ctx_create_once(void);
|
|
SD_API void sd_dgl_ctx_free(sd_dgl_ctx_t* ctx);
|
|
// Sets options on an idle context: max_vram, max_ram, conditioning_cache_size, batched_cfg,
|
|
// n_threads, auto_fit, enable_mmap, eager_load, disable_prefetch, disable_segmented_compute,
|
|
// embed_metadata (default on; controls generation metadata in DGL image/video saves),
|
|
// memory_guard (0-100: weights are released until a device's memory use is under this percent; 0 is off) and
|
|
// disable_backend_fallback (a backend that cannot be initialized fails the load instead of using the CPU).
|
|
// Threading, placement and weight-loading options invalidate cached models when changed.
|
|
SD_API bool sd_dgl_ctx_set(sd_dgl_ctx_t* ctx, const char* key, const char* value);
|
|
|
|
// Releases idle GPU staging buffers that have RAM originals.
|
|
// Returns SD_STATUS_BUSY during DGL execution or generation.
|
|
SD_API sd_status_t sd_dgl_ctx_release_staging(sd_dgl_ctx_t* ctx);
|
|
// Unloads cached model components. The context keeps its settings and callbacks.
|
|
// Subsequent requests reload components as needed. Returns SD_STATUS_BUSY during execution.
|
|
SD_API sd_status_t sd_dgl_ctx_unload_models(sd_dgl_ctx_t* ctx);
|
|
|
|
// Optional per-context callback. A null callback restores unrestricted file access.
|
|
SD_API bool sd_dgl_ctx_set_file_access_callback(sd_dgl_ctx_t* ctx, sd_dgl_file_access_cb_t callback, void* userdata);
|
|
|
|
// Callbacks run synchronously on the executing thread. Set or clear while idle.
|
|
// Pass counts start at 1 in each run. Data supplied by the library is borrowed
|
|
// until the callback returns. Callbacks may request cancellation and must not throw.
|
|
typedef void (*sd_dgl_preview_cb_t)(
|
|
const sd_preview_info_t* info,
|
|
int step,
|
|
int frame_count,
|
|
const sd_tensor_t* const* frames,
|
|
bool is_noisy,
|
|
void* userdata);
|
|
SD_API sd_status_t sd_dgl_ctx_set_progress_callback(sd_dgl_ctx_t* ctx, sd_generation_progress_cb_t callback, void* userdata);
|
|
// Modes: none, proj, tae, vae. Pass NULL as callback to clear.
|
|
// Sampling uses projection previews by default. Positive intervals count
|
|
// completed logical steps; supply preview(decoder=...) for tae or vae decoding.
|
|
SD_API sd_status_t sd_dgl_ctx_set_preview_callback(
|
|
sd_dgl_ctx_t* ctx,
|
|
sd_dgl_preview_cb_t callback,
|
|
const char* mode,
|
|
int interval,
|
|
bool denoised,
|
|
bool noisy,
|
|
void* userdata);
|
|
|
|
typedef struct {
|
|
const char* name;
|
|
const sd_tensor_t* tensor;
|
|
} sd_dgl_tensor_input_t;
|
|
|
|
// Checks syntax and types without creating a context or loading models.
|
|
// Set allow_tensor_inputs to accept named tensor parameters in fn main(name, ...).
|
|
SD_API sd_status_t sd_dgl_check(const char* source, bool allow_tensor_inputs);
|
|
|
|
// Compiles source once per call, binds inputs and executes in the required context.
|
|
// Supply every declared tensor parameter exactly once, or NULL, 0 for no inputs.
|
|
// The run retains tensors as needed; result tensors can share their storage.
|
|
// On failure, *result_out is NULL. Compare the return value with SD_STATUS_OK.
|
|
SD_API sd_status_t sd_dgl_run(
|
|
sd_dgl_ctx_t* ctx,
|
|
const char* source,
|
|
const sd_dgl_tensor_input_t* inputs,
|
|
size_t input_count,
|
|
sd_dgl_result_t** result_out);
|
|
// Thread-safe. Requests cooperative cancellation of the current run only,
|
|
// including during compilation; it is checked before execution begins.
|
|
// An idle context accepts the request without queuing it for the next run.
|
|
SD_API sd_status_t sd_dgl_ctx_cancel(sd_dgl_ctx_t* ctx);
|
|
// Alias for sd_get_last_error().
|
|
SD_API const char* sd_dgl_error(void);
|
|
|
|
SD_API void sd_dgl_result_free(sd_dgl_result_t* result);
|
|
|
|
typedef enum {
|
|
SD_DGL_VALUE_INVALID = 0,
|
|
SD_DGL_VALUE_BOOL,
|
|
SD_DGL_VALUE_INT,
|
|
SD_DGL_VALUE_NUMBER,
|
|
SD_DGL_VALUE_STRING,
|
|
SD_DGL_VALUE_TENSOR,
|
|
SD_DGL_VALUE_LIST,
|
|
SD_DGL_VALUE_DICT,
|
|
SD_DGL_VALUE_OPAQUE = 11,
|
|
SD_DGL_VALUE_VOID = 12,
|
|
SD_DGL_VALUE_BYTES = 13,
|
|
} sd_dgl_value_kind_t;
|
|
|
|
// Structured values, names and tensor handles are borrowed from the result.
|
|
// Retain tensors to use them after the result is freed.
|
|
SD_API const sd_dgl_value_t* sd_dgl_result_root(const sd_dgl_result_t* result);
|
|
SD_API sd_dgl_value_kind_t sd_dgl_value_kind(const sd_dgl_value_t* value);
|
|
SD_API size_t sd_dgl_value_count(const sd_dgl_value_t* value);
|
|
SD_API const sd_dgl_value_t* sd_dgl_value_at(const sd_dgl_value_t* value, size_t index);
|
|
SD_API const char* sd_dgl_value_name(const sd_dgl_value_t* value);
|
|
SD_API const sd_dgl_value_t* sd_dgl_value_field(const sd_dgl_value_t* value, const char* name);
|
|
SD_API sd_status_t sd_dgl_value_get_bool(const sd_dgl_value_t* value, bool* out);
|
|
SD_API sd_status_t sd_dgl_value_get_int(const sd_dgl_value_t* value, int64_t* out);
|
|
SD_API sd_status_t sd_dgl_value_get_number(const sd_dgl_value_t* value, double* out);
|
|
SD_API sd_status_t sd_dgl_value_get_string(const sd_dgl_value_t* value, const char** out, size_t* size);
|
|
// Borrowed immutable bytes, valid until the result is freed. No NUL terminator.
|
|
SD_API sd_status_t sd_dgl_value_get_bytes(const sd_dgl_value_t* value, const void** out, size_t* size);
|
|
SD_API const sd_tensor_t* sd_dgl_value_tensor(const sd_dgl_value_t* value);
|
|
|
|
// Flat tensor convenience access. Scalar values are materialized as tensors on demand.
|
|
SD_API const char* sd_dgl_result_type(const sd_dgl_result_t* result);
|
|
SD_API size_t sd_dgl_result_count(const sd_dgl_result_t* result);
|
|
SD_API const char* sd_dgl_result_name(const sd_dgl_result_t* result, size_t index);
|
|
// Borrowed read-only tensor. Use sd_tensor_retain() to keep it past result free.
|
|
SD_API const sd_tensor_t* sd_dgl_result_tensor(const sd_dgl_result_t* result, const char* name);
|
|
|
|
// Weight types and model conversion
|
|
|
|
// same as enum ggml_type
|
|
enum sd_type_t {
|
|
SD_TYPE_F32 = 0,
|
|
SD_TYPE_F16 = 1,
|
|
SD_TYPE_Q4_0 = 2,
|
|
SD_TYPE_Q4_1 = 3,
|
|
// SD_TYPE_Q4_2 = 4, support has been removed
|
|
// SD_TYPE_Q4_3 = 5, support has been removed
|
|
SD_TYPE_Q5_0 = 6,
|
|
SD_TYPE_Q5_1 = 7,
|
|
SD_TYPE_Q8_0 = 8,
|
|
SD_TYPE_Q8_1 = 9,
|
|
SD_TYPE_Q2_K = 10,
|
|
SD_TYPE_Q3_K = 11,
|
|
SD_TYPE_Q4_K = 12,
|
|
SD_TYPE_Q5_K = 13,
|
|
SD_TYPE_Q6_K = 14,
|
|
SD_TYPE_Q8_K = 15,
|
|
SD_TYPE_IQ2_XXS = 16,
|
|
SD_TYPE_IQ2_XS = 17,
|
|
SD_TYPE_IQ3_XXS = 18,
|
|
SD_TYPE_IQ1_S = 19,
|
|
SD_TYPE_IQ4_NL = 20,
|
|
SD_TYPE_IQ3_S = 21,
|
|
SD_TYPE_IQ2_S = 22,
|
|
SD_TYPE_IQ4_XS = 23,
|
|
SD_TYPE_I8 = 24,
|
|
SD_TYPE_I16 = 25,
|
|
SD_TYPE_I32 = 26,
|
|
SD_TYPE_I64 = 27,
|
|
SD_TYPE_F64 = 28,
|
|
SD_TYPE_IQ1_M = 29,
|
|
SD_TYPE_BF16 = 30,
|
|
// SD_TYPE_Q4_0_4_4 = 31, support has been removed from gguf files
|
|
// SD_TYPE_Q4_0_4_8 = 32,
|
|
// SD_TYPE_Q4_0_8_8 = 33,
|
|
SD_TYPE_TQ1_0 = 34,
|
|
SD_TYPE_TQ2_0 = 35,
|
|
// SD_TYPE_IQ4_NL_4_4 = 36,
|
|
// SD_TYPE_IQ4_NL_4_8 = 37,
|
|
// SD_TYPE_IQ4_NL_8_8 = 38,
|
|
SD_TYPE_MXFP4 = 39, // MXFP4 (1 block)
|
|
SD_TYPE_NVFP4 = 40, // NVFP4 (4 blocks, E4M3 scale)
|
|
SD_TYPE_Q1_0 = 41,
|
|
SD_TYPE_Q2_0 = 42,
|
|
SD_TYPE_F8_E4M3 = 43,
|
|
SD_TYPE_F8_E5M2 = 44,
|
|
SD_TYPE_COUNT = 45,
|
|
};
|
|
|
|
SD_API const char* sd_type_name(enum sd_type_t type);
|
|
SD_API enum sd_type_t str_to_sd_type(const char* str);
|
|
|
|
SD_API bool convert(
|
|
const char* input_path,
|
|
const char* vae_path,
|
|
const char* output_path,
|
|
enum sd_type_t output_type,
|
|
const char* tensor_type_rules,
|
|
bool convert_name);
|
|
|
|
SD_API bool convert_with_components(
|
|
const char* model_path,
|
|
const char* clip_l_path,
|
|
const char* clip_g_path,
|
|
const char* t5xxl_path,
|
|
const char* diffusion_model_path,
|
|
const char* vae_path,
|
|
const char* output_path,
|
|
enum sd_type_t output_type,
|
|
const char* tensor_type_rules,
|
|
bool convert_name,
|
|
int n_threads,
|
|
const sd_lora_t* loras,
|
|
int lora_count);
|
|
|
|
// Importance matrix
|
|
|
|
SD_API bool load_imatrix(const char* imatrix_path);
|
|
SD_API void save_imatrix(const char* imatrix_path);
|
|
SD_API void enable_imatrix_collection(void);
|
|
SD_API void disable_imatrix_collection(void);
|
|
|
|
// Devices and system information
|
|
|
|
// List available ggml backend devices, one `name<TAB>description` per line.
|
|
// The names are the device names accepted by the --backend / --params-backend
|
|
// assignment specs. Returns the number of bytes required, excluding the null
|
|
// terminator. Passing nullptr or buffer_size 0 only queries the required size.
|
|
SD_API size_t sd_list_devices(char* buffer, size_t buffer_size);
|
|
|
|
// Register comma-separated RPC endpoints (host:port) before listing devices.
|
|
// Repeated endpoints are safe; nullptr or an empty string is a no-op.
|
|
// Returns false if RPC is unavailable or an endpoint has no usable devices.
|
|
SD_API bool sd_add_rpc_servers(const char* servers);
|
|
|
|
enum sd_device_type_t {
|
|
SD_DEVICE_TYPE_CPU,
|
|
SD_DEVICE_TYPE_GPU,
|
|
SD_DEVICE_TYPE_IGPU,
|
|
SD_DEVICE_TYPE_ACCEL,
|
|
SD_DEVICE_TYPE_UNKNOWN,
|
|
};
|
|
|
|
typedef struct {
|
|
char name[64]; // device name accepted by the --backend / --params-backend assignment specs
|
|
char description[128];
|
|
enum sd_device_type_t type;
|
|
uint64_t free_memory_bytes; // 0 when the backend does not report memory; for the CPU, the RAM available without swapping
|
|
uint64_t total_memory_bytes; // 0 when the backend does not report memory
|
|
} sd_device_info_t;
|
|
|
|
// Structured device enumeration for host applications.
|
|
SD_API size_t sd_get_device_count(void);
|
|
SD_API bool sd_get_device_info(size_t index, sd_device_info_t* info);
|
|
SD_API const char* sd_device_type_name(enum sd_device_type_t type);
|
|
|
|
typedef struct {
|
|
uint64_t total_bytes; // physical RAM; 0 when unknown
|
|
uint64_t available_bytes; // RAM that can be used without swapping; 0 when unknown
|
|
uint64_t process_bytes; // RAM held by this process (resident set); 0 when unknown
|
|
} sd_system_memory_t;
|
|
|
|
// Current system RAM figures, for hosts that decide when to start work or how to configure a context.
|
|
// Returns false when none of them can be determined on this platform.
|
|
SD_API bool sd_get_system_memory(sd_system_memory_t* info);
|
|
|
|
// Device health. The engine and outside watchers report how the devices, the RAM and the disk are doing: a
|
|
// component under strain offers less of its capacity, one step per level, and the work it gives up goes to the
|
|
// others; an excluded device is never computed on. A level comes back one step after each quiet period. The
|
|
// components are the devices sd_get_device_info() lists, "RAM" and "disk".
|
|
typedef struct {
|
|
char name[64];
|
|
int level; // 0 healthy, 1..strain_levels under strain, strain_levels + 1 excluded
|
|
int strain_levels; // levels of strain before exclusion
|
|
int share_percent; // what the component offers of its capacity
|
|
bool excluded; // never computed on
|
|
bool critical; // a watcher asks to stop all work
|
|
char reason[200]; // why it is not healthy; empty when it is
|
|
int64_t held_ms; // how long the signals that hold this level still last; 0 when none does
|
|
int64_t next_step_ms; // until the level comes down one step if nothing else happens; 0 when none is due
|
|
} sd_device_health_t;
|
|
|
|
// Fills up to capacity entries and returns the number of components; with health NULL or capacity 0 it only
|
|
// counts them.
|
|
SD_API size_t sd_get_device_health(sd_device_health_t* health, size_t capacity);
|
|
// Reports the signal of an outside watcher, named by source; each source holds one signal per component. Level 0
|
|
// withdraws the signal of that source, 1..strain_levels puts the component under strain, strain_levels + 1 excludes
|
|
// it; critical asks to stop all work while the signal lasts, and excludes the component too. A signal reaches the
|
|
// listings of the same card through other backends and ends after duration_seconds, so a watcher repeats it while
|
|
// the condition lasts. Returns false, and changes nothing, when the component is unknown or the signal not valid.
|
|
SD_API bool sd_report_device_pressure(const char* component,
|
|
const char* source,
|
|
int level,
|
|
bool critical,
|
|
const char* reason,
|
|
int duration_seconds);
|
|
// An operator says the machine was checked: the critical signals are withdrawn and the excluded components come
|
|
// back to the last level of strain.
|
|
SD_API void sd_resume_device_health(void);
|
|
|
|
// The values of device health, for the whole process: read them, change some and write them back. The defaults are
|
|
// 3 levels of strain, 15% of the capacity and 25% of the CPU threads per level, a quiet period of 300 s, and 10 s
|
|
// under which two events of the engine on one component count once.
|
|
typedef struct {
|
|
int strain_levels; // levels of strain before exclusion, 1..10
|
|
int step_percent; // capacity a component gives up at each level, at least 1
|
|
int cpu_thread_step_percent; // compute threads the CPU gives up at each level, at least 0
|
|
int quiet_period_seconds; // a level comes back one step after this long without events, 1..86400
|
|
int event_spacing_seconds; // events of the engine on one component closer than this count once, 0..86400
|
|
} sd_device_health_settings_t;
|
|
|
|
SD_API void sd_get_device_health_settings(sd_device_health_settings_t* settings);
|
|
// Returns false, and changes nothing, when a value is out of its range or a step times strain_levels reaches 100.
|
|
// Every component keeps its state: an excluded one stays excluded, and a level of strain above the new last one
|
|
// comes down to it. Writing the same values again changes nothing.
|
|
SD_API bool sd_set_device_health_settings(const sd_device_health_settings_t* settings);
|
|
|
|
// Why a call failed, in finer terms than sd_status_t: a lack of memory, which a lighter configuration may get
|
|
// around, is told apart from a compute backend that failed. sd_ctx_create, sd_ctx_set_model_options*,
|
|
// sd_generate_image and sd_generate_video record the reason on the calling thread when they fail; a call that
|
|
// succeeds resets it to SD_ERROR_NONE.
|
|
enum sd_error_code_t {
|
|
SD_ERROR_NONE,
|
|
SD_ERROR_UNKNOWN, // failed without a more specific cause; the message tells more
|
|
SD_ERROR_INVALID_ARGUMENT, // a null pointer or an unsupported parameter
|
|
SD_ERROR_MODEL_FILE, // a model file is missing, unreadable, incomplete or not recognized
|
|
SD_ERROR_OUT_OF_MEMORY, // system RAM or device memory ran out; a lighter configuration may succeed
|
|
SD_ERROR_BACKEND, // a compute backend could not be initialized or failed while computing
|
|
SD_ERROR_BUSY, // the context is already running another operation
|
|
SD_ERROR_CANCELLED, // stopped on request through sd_cancel_generation(); not a failure of the system
|
|
};
|
|
|
|
// Returns the reason of the latest failed call made by this thread. When message is not null it receives
|
|
// the text of the error (at most message_size - 1 bytes, always terminated).
|
|
SD_API enum sd_error_code_t sd_get_last_error_code(char* message, size_t message_size);
|
|
SD_API const char* sd_error_code_name(enum sd_error_code_t code);
|
|
SD_API int32_t sd_get_num_physical_cores();
|
|
SD_API const char* sd_get_system_info();
|
|
|
|
// Build information
|
|
|
|
SD_API const char* sd_commit(void);
|
|
SD_API const char* sd_version(void);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // __STABLE_DIFFUSION_H__
|