#ifndef __EXAMPLES_COMMON_COMMON_H__ #define __EXAMPLES_COMMON_COMMON_H__ #include "options.h" #include #include #include #include #include #include #include "log.h" #include "resource_owners.hpp" #include "stable-diffusion.h" // The command-line argument at `index` as UTF-8 (on Windows, read again from // the UTF-16 command line: argv itself is in the ANSI code page). std::string argv_to_utf8(int index, const char** argv); #define SAFE_STR(s) ((s) ? (s) : "") #define BOOL_STR(b) ((b) ? "true" : "false") extern const char* const modes_str[]; #define SD_ALL_MODES_STR "img_gen, vid_gen, convert, upscale, metadata, dgl" enum SDMode { IMG_GEN, VID_GEN, CONVERT, UPSCALE, METADATA, DGL, MODE_COUNT }; struct StringOption { std::string short_name; std::string long_name; std::string desc; int concat; OptionTarget target; }; struct IntOption { std::string short_name; std::string long_name; std::string desc; OptionTarget target; }; struct FloatOption { std::string short_name; std::string long_name; std::string desc; OptionTarget target; }; struct BoolOption { std::string short_name; std::string long_name; std::string desc; bool keep_true; OptionTarget target; }; struct ManualFunction { std::function _func; ManualFunction() = default; ManualFunction(std::function func) : _func(std::move(func)) { } template ManualFunction(F func) : _func(make_function(func)) { } int operator()(int argc, const char** argv, int index, bool& valid) const { return _func(argc, argv, index, valid); } private: template static std::function make_function(F func) { if constexpr (std::is_invocable_v) { return func; } else { return [func](int argc, const char** argv, int index, bool&) { return func(argc, argv, index); }; } } }; struct ManualOption { std::string short_name; std::string long_name; std::string desc; ManualFunction cb; }; struct ArgOptions { std::vector string_options; std::vector int_options; std::vector float_options; std::vector bool_options; std::vector manual_options; static std::string wrap_text(const std::string& text, size_t width, size_t indent); void print() const; }; void add_log_options(ArgOptions& options, sd_log_level_t& level); bool parse_options(int argc, const char** argv, const std::vector& options_list); bool decode_base64_image(const std::string& encoded_input, int target_channels, int expected_width, int expected_height, SDImageOwner& out_image); struct SDContextParams { std::optional n_threads; std::optional conditioning_cache_size; std::string model_path; std::string clip_l_path; std::string clip_g_path; std::string clip_vision_path; std::string t5xxl_path; std::string llm_path; std::string llm_vision_path; std::string tokenizer; std::string diffusion_model_path; std::string high_noise_diffusion_model_path; std::string uncond_diffusion_model_path; std::string embeddings_connectors_path; std::string vae_path; std::optional vae_format; std::string audio_vae_path; std::string audio_encoder_path; std::string taesd_path; std::string esrgan_path; std::string control_net_path; std::string ip_adapter_path; std::string motion_module_path; std::string embedding_dir; std::string photo_maker_path; std::string pulid_weights_path; sd_type_t wtype = SD_TYPE_COUNT; std::string tensor_type_rules; std::string lora_model_dir = "."; std::string hires_upscalers_dir; std::map embedding_map; std::vector embedding_vec; std::optional rng_type; std::optional sampler_rng_type; bool offload_params_to_cpu = false; std::optional max_vram; std::optional disable_prefetch; std::optional disable_segmented_compute; std::optional eager_load; std::optional batched_cfg; std::optional backend; std::optional params_backend; std::optional split_mode; std::optional model_args; std::optional auto_fit; std::optional rpc_servers; bool list_devices = false; std::string effective_backend; std::string effective_params_backend; std::optional enable_mmap; bool control_net_cpu = false; bool clip_on_cpu = false; bool vae_on_cpu = false; std::optional flash_attn_mode; std::optional conv_direct_mode; std::optional sage_attn; std::optional diffusion_conv_direct; std::optional vae_conv_direct; std::optional taesd_preview_only; std::optional prediction; std::optional lora_apply_mode; std::optional force_sdxl_vae_conv_scale; std::optional linear_scale; std::optional attn_scale; ArgOptions get_options(); bool print_devices() const; void build_embedding_map(); void prepare_backend_assignments(); bool resolve(SDMode mode); bool validate(SDMode mode); bool resolve_and_validate(SDMode mode); std::string to_string() const; SDOptPtr to_context_opt(); bool configure_dgl(sd_dgl_ctx_t* ctx) const; bool register_rpc_devices() const; }; struct SDGenerationParams { // User-facing input fields. std::string prompt; std::string negative_prompt; std::string ad_model_path; std::string ad_prompt; std::string ad_negative_prompt; std::string extra_ad_args; std::optional clip_skip; std::optional width; std::optional height; std::optional batch_count; std::optional qwen_image_layers; std::optional seed; std::optional strength; std::optional control_strength; std::optional ip_adapter_strength; bool embed_image_metadata = true; std::string init_image_path; std::string end_image_path; std::string mask_image_path; std::string control_image_path; std::optional control_type; std::vector ref_image_paths; std::vector ref_video_paths; std::vector ref_video_audio_paths; std::vector ref_audio_paths; std::string control_video_path; SDSampleParams sample_params; SDSampleParams high_noise_sample_params; std::string extra_sample_args; std::string high_noise_extra_sample_args; std::optional> skip_layers; std::optional> high_noise_skip_layers; std::optional> custom_sigmas; std::string cache_mode; std::string cache_option; std::string scm_mask; std::optional scm_policy_dynamic; SDCacheParams cache_params{}; std::optional moe_boundary; std::optional video_frames; std::optional fps; std::optional vace_strength; SDTilingParams vae_tiling_params; std::string extra_tiling_args; int upscale_repeats = 1; std::optional upscale_tile_size; bool circular = false; std::optional circular_x; std::optional circular_y; std::optional hires_enabled; std::optional hires_upscaler; std::string hires_upscaler_model_path; std::optional hires_scale; std::optional hires_width; std::optional hires_height; std::optional hires_steps; std::optional hires_denoising_strength; std::optional hires_upscale_tile_size; std::optional> hires_custom_sigmas; std::map lora_map; std::map high_noise_lora_map; // Derived and normalized fields. std::string prompt_with_lora; // for metadata record only std::vector lora_vec; std::string resolved_hires_upscaler; // Owned execution payload. SDImageOwner init_image; SDImageOwner end_image; std::vector ref_images; std::map prompt_images; std::vector> ref_videos; std::vector ref_video_audios; std::vector ref_audios; SDImageOwner mask_image; SDImageOwner control_image; SDImageOwner ip_adapter_image; std::vector control_frames; // Media views used while populating the opaque options. std::vector ref_image_views; std::vector> ref_video_frame_views; std::vector ref_video_views; std::vector ref_audio_views; std::vector control_frame_views; SDGenerationParams(); SDGenerationParams(const SDGenerationParams& other) = default; SDGenerationParams& operator=(const SDGenerationParams& other) = default; SDGenerationParams(SDGenerationParams&& other) noexcept = default; SDGenerationParams& operator=(SDGenerationParams&& other) noexcept = default; ArgOptions get_options(); bool from_json_str(const std::string& json_str, const std::function& lora_path_resolver = {}); bool parse_prompt_images_json(const std::string& json_str); bool initialize_cache_params(); bool extract_and_remove_lora(const std::string& lora_model_dir); bool width_and_height_are_set() const; void set_width_and_height_if_unset(int w, int h); int get_requested_width() const; int get_requested_height() const; bool resolve(const std::string& lora_model_dir, const std::string& hires_upscalers_dir, bool strict = false, bool validate_missing_loras = false); bool validate(SDMode mode); bool resolve_and_validate(SDMode mode, const std::string& lora_model_dir, const std::string& hires_upscalers_dir, bool strict = false); SDOptPtr to_image_opt(); SDOptPtr to_video_opt(); std::string to_string() const; }; std::string version_string(); #endif // __EXAMPLES_COMMON_COMMON_H__