mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +00:00
new_sd_ctx(), generate_image() and generate_video() now record the reason of a failure, and sd_get_last_error() returns it with a code: out of memory, model file, backend, invalid argument, busy or cancelled. An application can tell "try again with less memory" from "the file is damaged" instead of reading the log. The state belongs to the calling thread; errors logged by the model loader threads reach the thread that reports the failure. C++ exceptions, std::bad_alloc included, no longer cross the C API. A test with a limited address space showed the process ending with std::terminate when a loader thread could not be created while others were already running. The loader now uses fewer threads, or the calling thread when none could be started, and the loader threads report exceptions as a failed load. sd_get_system_memory() returns total, available and process RAM. The free memory of the CPU device in sd_get_device_info() is the available RAM, because ggml reports the total on Linux and macOS. Checked with a synthetic 2.1 GB SD 1.5 model under a limited address space: the standard configuration fails with out_of_memory, the same context generates once the limit is lifted, and weights on disk let the same request succeed. The Windows branch of system_memory.cpp was only syntax-checked; macOS was not built. Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
698 lines
24 KiB
C
698 lines
24 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
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
enum rng_type_t {
|
|
STD_DEFAULT_RNG,
|
|
CUDA_RNG,
|
|
CPU_RNG,
|
|
RNG_TYPE_COUNT
|
|
};
|
|
|
|
enum sample_method_t {
|
|
EULER_SAMPLE_METHOD,
|
|
EULER_A_SAMPLE_METHOD,
|
|
HEUN_SAMPLE_METHOD,
|
|
DPM2_SAMPLE_METHOD,
|
|
DPMPP2S_A_SAMPLE_METHOD,
|
|
DPMPP2M_SAMPLE_METHOD,
|
|
DPMPP2Mv2_SAMPLE_METHOD,
|
|
IPNDM_SAMPLE_METHOD,
|
|
IPNDM_V_SAMPLE_METHOD,
|
|
LCM_SAMPLE_METHOD,
|
|
DDIM_TRAILING_SAMPLE_METHOD,
|
|
TCD_SAMPLE_METHOD,
|
|
RES_MULTISTEP_SAMPLE_METHOD,
|
|
RES_2S_SAMPLE_METHOD,
|
|
ER_SDE_SAMPLE_METHOD,
|
|
EULER_CFG_PP_SAMPLE_METHOD,
|
|
EULER_A_CFG_PP_SAMPLE_METHOD,
|
|
EULER_GE_SAMPLE_METHOD,
|
|
DPMPP2M_SDE_SAMPLE_METHOD,
|
|
DPMPP2M_SDE_BT_SAMPLE_METHOD,
|
|
LMS_SAMPLE_METHOD,
|
|
SAMPLE_METHOD_COUNT
|
|
};
|
|
|
|
extern SD_API const char* sample_method_to_str[];
|
|
|
|
enum scheduler_t {
|
|
DISCRETE_SCHEDULER,
|
|
KARRAS_SCHEDULER,
|
|
EXPONENTIAL_SCHEDULER,
|
|
AYS_SCHEDULER,
|
|
GITS_SCHEDULER,
|
|
SGM_UNIFORM_SCHEDULER,
|
|
SIMPLE_SCHEDULER,
|
|
SMOOTHSTEP_SCHEDULER,
|
|
KL_OPTIMAL_SCHEDULER,
|
|
LCM_SCHEDULER,
|
|
BONG_TANGENT_SCHEDULER,
|
|
LTX2_SCHEDULER,
|
|
LOGIT_NORMAL_SCHEDULER,
|
|
FLUX2_SCHEDULER,
|
|
FLUX_SCHEDULER,
|
|
BETA_SCHEDULER,
|
|
LLADA_IMAGE_SCHEDULER,
|
|
SCHEDULER_COUNT
|
|
};
|
|
|
|
extern SD_API const char* scheduler_to_str[];
|
|
|
|
enum prediction_t {
|
|
EPS_PRED,
|
|
V_PRED,
|
|
EDM_V_PRED,
|
|
FLOW_PRED,
|
|
FLUX_FLOW_PRED,
|
|
SEFI_FLOW_PRED,
|
|
MINIT2I_FLOW_PRED,
|
|
SENSENOVA_U1_FLOW_PRED,
|
|
PREDICTION_COUNT
|
|
};
|
|
|
|
// 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,
|
|
};
|
|
|
|
enum sd_log_level_t {
|
|
SD_LOG_DEBUG,
|
|
SD_LOG_VERBOSE,
|
|
SD_LOG_INFO,
|
|
SD_LOG_WARN,
|
|
SD_LOG_ERROR
|
|
};
|
|
|
|
enum preview_t {
|
|
PREVIEW_NONE,
|
|
PREVIEW_PROJ,
|
|
PREVIEW_TAE,
|
|
PREVIEW_VAE,
|
|
PREVIEW_COUNT
|
|
};
|
|
|
|
enum lora_apply_mode_t {
|
|
LORA_APPLY_AUTO,
|
|
LORA_APPLY_IMMEDIATELY,
|
|
LORA_APPLY_AT_RUNTIME,
|
|
LORA_APPLY_MODE_COUNT,
|
|
};
|
|
|
|
typedef struct {
|
|
bool enabled;
|
|
bool temporal_tiling;
|
|
// Spatial tile dimensions in image pixels for both encode and decode; 0 uses 256.
|
|
int tile_size_w;
|
|
int tile_size_h;
|
|
float target_overlap;
|
|
// Positive values override tile_size: <= 1 is a dimension fraction, > 1 a target tile count.
|
|
float rel_size_w;
|
|
float rel_size_h;
|
|
const char* extra_tiling_args;
|
|
} sd_tiling_params_t;
|
|
|
|
typedef struct {
|
|
const char* name;
|
|
const char* path;
|
|
} sd_embedding_t;
|
|
|
|
enum sd_vae_format_t {
|
|
SD_VAE_FORMAT_AUTO = -1,
|
|
SD_VAE_FORMAT_FLUX,
|
|
SD_VAE_FORMAT_SD3,
|
|
SD_VAE_FORMAT_FLUX2,
|
|
SD_VAE_FORMAT_WAN,
|
|
SD_VAE_FORMAT_COUNT,
|
|
};
|
|
|
|
typedef struct {
|
|
const char* model_path;
|
|
const char* clip_l_path;
|
|
const char* clip_g_path;
|
|
const char* clip_vision_path;
|
|
const char* t5xxl_path;
|
|
const char* llm_path;
|
|
const char* llm_vision_path;
|
|
const char* diffusion_model_path;
|
|
const char* high_noise_diffusion_model_path;
|
|
const char* uncond_diffusion_model_path;
|
|
const char* embeddings_connectors_path;
|
|
const char* vae_path;
|
|
const char* audio_vae_path;
|
|
const char* audio_encoder_path;
|
|
const char* taesd_path;
|
|
const char* control_net_path;
|
|
const char* ip_adapter_path;
|
|
const char* motion_module_path;
|
|
const sd_embedding_t* embeddings;
|
|
uint32_t embedding_count;
|
|
const char* photo_maker_path;
|
|
const char* pulid_weights_path;
|
|
const char* tensor_type_rules;
|
|
int n_threads;
|
|
enum sd_type_t wtype;
|
|
enum rng_type_t rng_type;
|
|
enum rng_type_t sampler_rng_type;
|
|
enum prediction_t prediction;
|
|
enum lora_apply_mode_t lora_apply_mode;
|
|
bool enable_mmap;
|
|
bool flash_attn;
|
|
bool diffusion_flash_attn;
|
|
bool tae_preview_only;
|
|
bool diffusion_conv_direct;
|
|
bool vae_conv_direct;
|
|
bool force_sdxl_vae_conv_scale;
|
|
enum sd_vae_format_t vae_format;
|
|
const char* max_vram; // Optional per-device GiB budget for managed weights and runner buffers; 0 uses live free VRAM without an explicit budget
|
|
bool disable_prefetch; // Disable asynchronous next-segment weight prefetch
|
|
bool eager_load; // Load all params into the params backend at model-load time instead of lazily on first use
|
|
const char* backend;
|
|
const char* params_backend;
|
|
const char* split_mode; // weight distribution for multi-device modules: layer (default) or row, or per-module assignments e.g. "diffusion=row"
|
|
bool auto_fit;
|
|
const char* rpc_servers;
|
|
const char* model_args;
|
|
bool disable_segmented_compute; // Force monolithic graph execution even when automatic graph cutting would fit memory better
|
|
bool batched_cfg; // Run the conditional and unconditional CFG branches in one batched UNet forward when supported
|
|
float linear_scale; // Override linear input scaling; 0 keeps the model default
|
|
float attn_scale; // Override flash-attention K/V scaling; 0 keeps the model default
|
|
const char* tokenizer; // tokenizer.json path or main=FILE,clip-l=FILE,clip-g=FILE assignments; required for PiD and Lens
|
|
bool sage_attn;
|
|
int conditioning_cache_size; // Maximum cached conditioning entries per context; 0 disables caching (default: 4)
|
|
int memory_guard; // Memory usage percent (device memory, or system RAM on CPU backends) that triggers pressure reduction (weight eviction, prefetch pause, segmented CPU graphs for weights read from disk); 0 disables (default). Auto-fit uses 90 for a CPU-only plan that reads weights from disk
|
|
bool disable_backend_fallback; // Fail context creation instead of falling back to the CPU backend when the requested backend is unavailable
|
|
} sd_ctx_params_t;
|
|
|
|
typedef struct {
|
|
uint32_t sample_rate;
|
|
uint32_t channels;
|
|
uint64_t sample_count;
|
|
float* data;
|
|
} sd_audio_t;
|
|
|
|
typedef struct {
|
|
uint32_t width;
|
|
uint32_t height;
|
|
uint32_t channel;
|
|
uint8_t* data;
|
|
} sd_image_t;
|
|
|
|
typedef struct {
|
|
// Semicolon-separated target=...,key=value rules. NULL preserves defaults.
|
|
const char* rules;
|
|
} sd_image_preprocess_params_t;
|
|
|
|
typedef struct {
|
|
sd_image_t* frames;
|
|
int frame_count;
|
|
int fps;
|
|
sd_audio_t audio;
|
|
} sd_ref_video_t;
|
|
|
|
typedef struct {
|
|
int* layers;
|
|
size_t layer_count;
|
|
float layer_start;
|
|
float layer_end;
|
|
float scale;
|
|
} sd_slg_params_t;
|
|
|
|
typedef struct {
|
|
float txt_cfg;
|
|
float img_cfg;
|
|
float distilled_guidance;
|
|
sd_slg_params_t slg;
|
|
} sd_guidance_params_t;
|
|
|
|
typedef struct {
|
|
sd_guidance_params_t guidance;
|
|
enum scheduler_t scheduler;
|
|
enum sample_method_t sample_method;
|
|
int sample_steps;
|
|
float eta;
|
|
int shifted_timestep;
|
|
float* custom_sigmas;
|
|
int custom_sigmas_count;
|
|
float flow_shift;
|
|
const char* extra_sample_args;
|
|
} sd_sample_params_t;
|
|
|
|
typedef struct {
|
|
sd_image_t* id_images;
|
|
int id_images_count;
|
|
const char* id_embed_path;
|
|
float style_strength;
|
|
} sd_pm_params_t; // photo maker
|
|
|
|
typedef struct {
|
|
const char* id_embedding_path;
|
|
float id_weight;
|
|
} sd_pulid_params_t;
|
|
|
|
enum sd_cache_mode_t {
|
|
SD_CACHE_DISABLED = 0,
|
|
SD_CACHE_EASYCACHE,
|
|
SD_CACHE_UCACHE,
|
|
SD_CACHE_DBCACHE,
|
|
SD_CACHE_TAYLORSEER,
|
|
SD_CACHE_CACHE_DIT,
|
|
SD_CACHE_SPECTRUM,
|
|
};
|
|
|
|
typedef struct {
|
|
enum sd_cache_mode_t mode;
|
|
float reuse_threshold;
|
|
float start_percent;
|
|
float end_percent;
|
|
float error_decay_rate;
|
|
bool use_relative_threshold;
|
|
bool reset_error_on_compute;
|
|
int Fn_compute_blocks;
|
|
int Bn_compute_blocks;
|
|
float residual_diff_threshold;
|
|
int max_warmup_steps;
|
|
int max_cached_steps;
|
|
int max_continuous_cached_steps;
|
|
int taylorseer_n_derivatives;
|
|
int taylorseer_skip_interval;
|
|
const char* scm_mask;
|
|
bool scm_policy_dynamic;
|
|
float spectrum_w;
|
|
int spectrum_m;
|
|
float spectrum_lam;
|
|
int spectrum_window_size;
|
|
float spectrum_flex_window;
|
|
int spectrum_warmup_steps;
|
|
float spectrum_stop_percent;
|
|
} sd_cache_params_t;
|
|
|
|
typedef struct {
|
|
bool is_high_noise;
|
|
float multiplier;
|
|
const char* path;
|
|
} sd_lora_t;
|
|
|
|
enum sd_hires_upscaler_t {
|
|
SD_HIRES_UPSCALER_NONE,
|
|
SD_HIRES_UPSCALER_LATENT,
|
|
SD_HIRES_UPSCALER_LATENT_NEAREST,
|
|
SD_HIRES_UPSCALER_LATENT_NEAREST_EXACT,
|
|
SD_HIRES_UPSCALER_LATENT_ANTIALIASED,
|
|
SD_HIRES_UPSCALER_LATENT_BICUBIC,
|
|
SD_HIRES_UPSCALER_LATENT_BICUBIC_ANTIALIASED,
|
|
SD_HIRES_UPSCALER_LANCZOS,
|
|
SD_HIRES_UPSCALER_NEAREST,
|
|
SD_HIRES_UPSCALER_MODEL,
|
|
SD_HIRES_UPSCALER_COUNT,
|
|
};
|
|
|
|
typedef struct {
|
|
bool enabled;
|
|
enum sd_hires_upscaler_t upscaler;
|
|
const char* model_path;
|
|
float scale;
|
|
int target_width;
|
|
int target_height;
|
|
int steps;
|
|
float denoising_strength;
|
|
int upscale_tile_size;
|
|
float* custom_sigmas;
|
|
int custom_sigmas_count;
|
|
} sd_hires_params_t;
|
|
|
|
typedef struct {
|
|
const sd_lora_t* loras;
|
|
uint32_t lora_count;
|
|
const char* prompt;
|
|
const char* negative_prompt;
|
|
int clip_skip;
|
|
sd_image_t init_image;
|
|
sd_image_t* ref_images;
|
|
int ref_images_count;
|
|
const char* ref_image_args;
|
|
sd_image_t mask_image;
|
|
int width;
|
|
int height;
|
|
sd_sample_params_t sample_params;
|
|
float strength;
|
|
int64_t seed;
|
|
int batch_count;
|
|
sd_image_t control_image;
|
|
float control_strength;
|
|
sd_image_t ip_adapter_image;
|
|
float ip_adapter_strength;
|
|
sd_pm_params_t pm_params;
|
|
sd_pulid_params_t pulid_params;
|
|
sd_tiling_params_t vae_tiling_params;
|
|
sd_cache_params_t cache;
|
|
sd_hires_params_t hires;
|
|
int qwen_image_layers;
|
|
bool circular_x;
|
|
bool circular_y;
|
|
sd_image_preprocess_params_t image_preprocess;
|
|
} sd_img_gen_params_t;
|
|
|
|
typedef struct {
|
|
const sd_lora_t* loras;
|
|
uint32_t lora_count;
|
|
const char* prompt;
|
|
const char* negative_prompt;
|
|
int clip_skip;
|
|
sd_image_t init_image;
|
|
sd_image_t end_image;
|
|
sd_image_t* ref_images;
|
|
int ref_images_count;
|
|
sd_ref_video_t* ref_videos;
|
|
int ref_videos_count;
|
|
sd_audio_t* ref_audios;
|
|
int ref_audios_count;
|
|
sd_image_t* control_frames;
|
|
int control_frames_size;
|
|
int width;
|
|
int height;
|
|
sd_sample_params_t sample_params;
|
|
sd_sample_params_t high_noise_sample_params;
|
|
float moe_boundary;
|
|
float strength;
|
|
int64_t seed;
|
|
int video_frames;
|
|
int fps;
|
|
float vace_strength;
|
|
sd_tiling_params_t vae_tiling_params;
|
|
sd_cache_params_t cache;
|
|
sd_hires_params_t hires;
|
|
bool circular_x;
|
|
bool circular_y;
|
|
sd_image_preprocess_params_t image_preprocess;
|
|
} sd_vid_gen_params_t;
|
|
|
|
typedef struct sd_ctx_t sd_ctx_t;
|
|
struct ggml_tensor;
|
|
|
|
typedef void (*sd_log_cb_t)(enum sd_log_level_t level, const char* text, void* data);
|
|
typedef void (*sd_progress_cb_t)(int step, int steps, float time, void* data);
|
|
typedef void (*sd_preview_cb_t)(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data);
|
|
|
|
typedef struct {
|
|
int sample_pass;
|
|
int total_steps;
|
|
} sd_preview_info_t;
|
|
typedef bool (*sd_graph_eval_callback_t)(struct ggml_tensor* t, bool ask, void* user_data);
|
|
|
|
SD_API void sd_set_log_callback(sd_log_cb_t sd_log_cb, void* data);
|
|
SD_API void sd_set_progress_callback(sd_progress_cb_t cb, 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).
|
|
SD_API void sd_set_preview_callback(sd_preview_cb_t cb, enum preview_t 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();
|
|
SD_API void sd_set_backend_eval_callback(sd_graph_eval_callback_t cb, void* data);
|
|
SD_API int32_t sd_get_num_physical_cores();
|
|
SD_API const char* sd_get_system_info();
|
|
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);
|
|
|
|
// ControlNet hot-swap APIs are not safe to call while generation is in flight.
|
|
SD_API bool sd_ctx_load_control_net(sd_ctx_t* sd_ctx, const char* path);
|
|
SD_API bool sd_ctx_unload_control_net(sd_ctx_t* sd_ctx);
|
|
SD_API bool sd_ctx_has_control_net(const sd_ctx_t* sd_ctx);
|
|
|
|
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 const char* sd_rng_type_name(enum rng_type_t rng_type);
|
|
SD_API enum rng_type_t str_to_rng_type(const char* str);
|
|
SD_API const char* sd_sample_method_name(enum sample_method_t sample_method);
|
|
SD_API enum sample_method_t str_to_sample_method(const char* str);
|
|
SD_API const char* sd_scheduler_name(enum scheduler_t scheduler);
|
|
SD_API enum scheduler_t str_to_scheduler(const char* str);
|
|
SD_API const char* sd_prediction_name(enum prediction_t prediction);
|
|
SD_API enum prediction_t str_to_prediction(const char* str);
|
|
SD_API const char* sd_preview_name(enum preview_t preview);
|
|
SD_API enum preview_t str_to_preview(const char* str);
|
|
SD_API const char* sd_lora_apply_mode_name(enum lora_apply_mode_t mode);
|
|
SD_API enum lora_apply_mode_t str_to_lora_apply_mode(const char* str);
|
|
SD_API const char* sd_hires_upscaler_name(enum sd_hires_upscaler_t upscaler);
|
|
SD_API enum sd_hires_upscaler_t str_to_sd_hires_upscaler(const char* str);
|
|
|
|
SD_API void sd_cache_params_init(sd_cache_params_t* cache_params);
|
|
SD_API void sd_hires_params_init(sd_hires_params_t* hires_params);
|
|
|
|
SD_API void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params);
|
|
SD_API char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params);
|
|
|
|
SD_API sd_ctx_t* new_sd_ctx(const sd_ctx_params_t* sd_ctx_params);
|
|
SD_API void free_sd_ctx(sd_ctx_t* sd_ctx);
|
|
SD_API void free_sd_audio(sd_audio_t* audio);
|
|
|
|
SD_API void sd_sample_params_init(sd_sample_params_t* sample_params);
|
|
SD_API char* sd_sample_params_to_str(const sd_sample_params_t* sample_params);
|
|
|
|
// Requires a loaded context; returns a static string owned by the library, or "Unknown".
|
|
SD_API const char* sd_get_model_version_name(const sd_ctx_t* sd_ctx);
|
|
|
|
SD_API enum sample_method_t sd_get_default_sample_method(const sd_ctx_t* sd_ctx);
|
|
SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx, enum sample_method_t sample_method);
|
|
|
|
SD_API void sd_img_gen_params_init(sd_img_gen_params_t* sd_img_gen_params);
|
|
SD_API char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params);
|
|
SD_API bool generate_image(sd_ctx_t* sd_ctx,
|
|
const sd_img_gen_params_t* sd_img_gen_params,
|
|
sd_image_t** images_out,
|
|
int* num_images_out);
|
|
|
|
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);
|
|
|
|
SD_API void sd_vid_gen_params_init(sd_vid_gen_params_t* sd_vid_gen_params);
|
|
// If non-NULL, fps_out receives the effective encoding frame rate before preview callbacks.
|
|
SD_API bool generate_video(sd_ctx_t* sd_ctx,
|
|
const sd_vid_gen_params_t* sd_vid_gen_params,
|
|
sd_image_t** frames_out,
|
|
int* num_frames_out,
|
|
sd_audio_t** audio_out,
|
|
int* fps_out);
|
|
|
|
typedef struct upscaler_ctx_t upscaler_ctx_t;
|
|
|
|
SD_API upscaler_ctx_t* new_upscaler_ctx(const char* esrgan_path,
|
|
bool direct,
|
|
int n_threads,
|
|
int tile_size,
|
|
const char* backend,
|
|
const char* params_backend);
|
|
SD_API void free_upscaler_ctx(upscaler_ctx_t* upscaler_ctx);
|
|
|
|
SD_API bool upscale(upscaler_ctx_t* upscaler_ctx,
|
|
sd_image_t input_image,
|
|
uint32_t upscale_factor,
|
|
sd_image_t** images_out,
|
|
int* num_images_out);
|
|
|
|
SD_API int get_upscale_factor(upscaler_ctx_t* upscaler_ctx);
|
|
// Reads model metadata only; returns 0 if the file is not a recognized RGB ESRGAN model.
|
|
SD_API int get_upscaler_model_scale(const char* model_path);
|
|
|
|
typedef struct adetailer_ctx_t adetailer_ctx_t;
|
|
|
|
typedef struct {
|
|
const char* prompt;
|
|
const char* negative_prompt;
|
|
const char* extra_ad_args;
|
|
} sd_adetailer_params_t;
|
|
|
|
SD_API adetailer_ctx_t* new_adetailer_ctx(const char* detector_path,
|
|
int n_threads,
|
|
const char* backend,
|
|
const char* params_backend);
|
|
SD_API void free_adetailer_ctx(adetailer_ctx_t* adetailer_ctx);
|
|
SD_API bool adetail_image(adetailer_ctx_t* adetailer_ctx,
|
|
sd_ctx_t* sd_ctx,
|
|
sd_image_t input_image,
|
|
const sd_adetailer_params_t* adetailer_params,
|
|
const sd_img_gen_params_t* inpaint_params,
|
|
sd_image_t** images_out,
|
|
int* num_images_out);
|
|
|
|
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);
|
|
|
|
SD_API bool preprocess_canny(sd_image_t image,
|
|
float high_threshold,
|
|
float low_threshold,
|
|
float weak,
|
|
float strong,
|
|
bool inverse);
|
|
|
|
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);
|
|
|
|
SD_API const char* sd_commit(void);
|
|
SD_API const char* sd_version(void);
|
|
|
|
// 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);
|
|
|
|
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);
|
|
|
|
// Why a call failed. new_sd_ctx, generate_image and 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(char* message, size_t message_size);
|
|
SD_API const char* sd_error_code_name(enum sd_error_code_t code);
|
|
|
|
// for C API, caller needs to call free_sd_images to free the memory after use
|
|
// This helps avoid CRT problems on Windows when memory is allocated in the library but freed in the caller, which may use a different CRT.
|
|
SD_API void free_sd_images(sd_image_t* result_images, int num_images);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // __STABLE_DIFFUSION_H__
|