refactor: split examples common into header and source (#1393)

This commit is contained in:
leejet 2026-04-06 21:11:57 +08:00 committed by GitHub
parent 359eb8b8de
commit 5bf438d568
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 1970 additions and 1919 deletions

View File

@ -1,6 +1,7 @@
set(TARGET sd-cli)
add_executable(${TARGET}
../common/common.cpp
../common/log.cpp
../common/media_io.cpp
image_metadata.cpp

View File

@ -15,11 +15,13 @@
// #include "preprocessing.hpp"
#include "stable-diffusion.h"
#include "common/common.hpp"
#include "common/common.h"
#include "common/media_io.h"
#include "common/resource_owners.hpp"
#include "image_metadata.h"
namespace fs = std::filesystem;
const char* previews_str[] = {
"none",
"proj",

1740
examples/common/common.cpp Normal file

File diff suppressed because it is too large Load Diff

207
examples/common/common.h Normal file
View File

@ -0,0 +1,207 @@
#ifndef __EXAMPLES_COMMON_COMMON_H__
#define __EXAMPLES_COMMON_COMMON_H__
#include <cmath>
#include <cstdint>
#include <functional>
#include <map>
#include <string>
#include <vector>
#include "log.h"
#include "stable-diffusion.h"
#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"
enum SDMode {
IMG_GEN,
VID_GEN,
CONVERT,
UPSCALE,
METADATA,
MODE_COUNT
};
struct StringOption {
std::string short_name;
std::string long_name;
std::string desc;
std::string* target;
};
struct IntOption {
std::string short_name;
std::string long_name;
std::string desc;
int* target;
};
struct FloatOption {
std::string short_name;
std::string long_name;
std::string desc;
float* target;
};
struct BoolOption {
std::string short_name;
std::string long_name;
std::string desc;
bool keep_true;
bool* target;
};
struct ManualOption {
std::string short_name;
std::string long_name;
std::string desc;
std::function<int(int argc, const char** argv, int index)> cb;
};
struct ArgOptions {
std::vector<StringOption> string_options;
std::vector<IntOption> int_options;
std::vector<FloatOption> float_options;
std::vector<BoolOption> bool_options;
std::vector<ManualOption> manual_options;
static std::string wrap_text(const std::string& text, size_t width, size_t indent);
void print() const;
};
bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& options_list);
struct SDContextParams {
int n_threads = -1;
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 diffusion_model_path;
std::string high_noise_diffusion_model_path;
std::string vae_path;
std::string taesd_path;
std::string esrgan_path;
std::string control_net_path;
std::string embedding_dir;
std::string photo_maker_path;
sd_type_t wtype = SD_TYPE_COUNT;
std::string tensor_type_rules;
std::string lora_model_dir = ".";
std::map<std::string, std::string> embedding_map;
std::vector<sd_embedding_t> embedding_vec;
rng_type_t rng_type = CUDA_RNG;
rng_type_t sampler_rng_type = RNG_TYPE_COUNT;
bool offload_params_to_cpu = false;
bool enable_mmap = false;
bool control_net_cpu = false;
bool clip_on_cpu = false;
bool vae_on_cpu = false;
bool flash_attn = false;
bool diffusion_flash_attn = false;
bool diffusion_conv_direct = false;
bool vae_conv_direct = false;
bool circular = false;
bool circular_x = false;
bool circular_y = false;
bool chroma_use_dit_mask = true;
bool chroma_use_t5_mask = false;
int chroma_t5_mask_pad = 1;
bool qwen_image_zero_cond_t = false;
prediction_t prediction = PREDICTION_COUNT;
lora_apply_mode_t lora_apply_mode = LORA_APPLY_AUTO;
bool force_sdxl_vae_conv_scale = false;
float flow_shift = INFINITY;
ArgOptions get_options();
void build_embedding_map();
bool process_and_check(SDMode mode);
std::string to_string() const;
sd_ctx_params_t to_sd_ctx_params_t(bool vae_decode_only, bool free_params_immediately, bool taesd_preview);
};
struct SDGenerationParams {
std::string prompt;
std::string prompt_with_lora; // for metadata record only
std::string negative_prompt;
int clip_skip = -1; // <= 0 represents unspecified
int width = -1;
int height = -1;
int batch_count = 1;
std::string init_image_path;
std::string end_image_path;
std::string mask_image_path;
std::string control_image_path;
std::vector<std::string> ref_image_paths;
std::string control_video_path;
bool auto_resize_ref_image = true;
bool increase_ref_index = false;
bool embed_image_metadata = true;
std::vector<int> skip_layers = {7, 8, 9};
sd_sample_params_t sample_params;
std::vector<int> high_noise_skip_layers = {7, 8, 9};
sd_sample_params_t high_noise_sample_params;
std::vector<float> custom_sigmas;
std::string cache_mode;
std::string cache_option;
std::string scm_mask;
bool scm_policy_dynamic = true;
sd_cache_params_t cache_params{};
float moe_boundary = 0.875f;
int video_frames = 1;
int fps = 16;
float vace_strength = 1.f;
float strength = 0.75f;
float control_strength = 0.9f;
int64_t seed = 42;
sd_tiling_params_t vae_tiling_params = {false, 0, 0, 0.5f, 0.0f, 0.0f};
// Photo Maker
std::string pm_id_images_dir;
std::string pm_id_embed_path;
float pm_style_strength = 20.f;
int upscale_repeats = 1;
int upscale_tile_size = 128;
std::map<std::string, float> lora_map;
std::map<std::string, float> high_noise_lora_map;
std::vector<sd_lora_t> lora_vec;
SDGenerationParams();
ArgOptions get_options();
bool from_json_str(const std::string& json_str);
void 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_resolved_width() const;
int get_resolved_height() const;
bool process_and_check(SDMode mode, const std::string& lora_model_dir);
std::string to_string() const;
};
std::string version_string();
std::string get_image_params(const SDContextParams& ctx_params, const SDGenerationParams& gen_params, int64_t seed);
#endif // __EXAMPLES_COMMON_COMMON_H__

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
#include "log.h"
#include "media_io.h"
#include "log.h"
#include "resource_owners.hpp"
#include <algorithm>
@ -38,7 +38,6 @@
namespace fs = std::filesystem;
namespace {
#ifdef SD_USE_WEBP
struct WebPFreeDeleter {
void operator()(void* ptr) const {
@ -91,8 +90,8 @@ struct WebPPictureGuard {
bool initialized;
};
using WebPBufferPtr = std::unique_ptr<uint8_t, WebPFreeDeleter>;
using WebPMuxPtr = std::unique_ptr<WebPMux, WebPMuxDeleter>;
using WebPBufferPtr = std::unique_ptr<uint8_t, WebPFreeDeleter>;
using WebPMuxPtr = std::unique_ptr<WebPMux, WebPMuxDeleter>;
using WebPAnimEncoderPtr = std::unique_ptr<WebPAnimEncoder, WebPAnimEncoderDeleter>;
#endif
@ -472,14 +471,14 @@ uint8_t* load_image_common(bool from_memory,
if (from_memory) {
image_path = "memory";
if (image_buffer == nullptr) {
int c = 0;
int c = 0;
image_buffer.reset((uint8_t*)stbi_load_from_memory((const stbi_uc*)image_path_or_bytes, len, &width, &height, &c, expected_channel));
source_channel_count = c;
}
} else {
image_path = image_path_or_bytes;
if (image_buffer == nullptr) {
int c = 0;
int c = 0;
image_buffer.reset((uint8_t*)stbi_load(image_path_or_bytes, &width, &height, &c, expected_channel));
source_channel_count = c;
}
@ -534,8 +533,8 @@ uint8_t* load_image_common(bool from_memory,
memcpy(dst, src, crop_w * expected_channel);
}
width = crop_w;
height = crop_h;
width = crop_w;
height = crop_h;
image_buffer = std::move(cropped_image_buffer);
}
@ -551,8 +550,8 @@ uint8_t* load_image_common(bool from_memory,
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
STBIR_COLORSPACE_SRGB, nullptr);
width = expected_width;
height = expected_height;
width = expected_width;
height = expected_height;
image_buffer = std::move(resized_image_buffer);
}
return image_buffer.release();
@ -570,8 +569,6 @@ void write_u32_le(FILE* f, uint32_t val) {
void write_u16_le(FILE* f, uint16_t val) {
fwrite(&val, 2, 1, f);
}
} // namespace
EncodedImageFormat encoded_image_format_from_path(const std::string& path) {
std::string ext = fs::path(path).extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
@ -797,7 +794,7 @@ int create_mjpg_avi_from_sd_images(const char* filename, sd_image_t* images, int
jpeg_data.clear();
auto write_to_buf = [](void* context, void* data, int size) {
auto* buffer = reinterpret_cast<std::vector<uint8_t>*>(context);
auto* buffer = reinterpret_cast<std::vector<uint8_t>*>(context);
const uint8_t* src = reinterpret_cast<const uint8_t*>(data);
buffer->insert(buffer->end(), src, src + size);
};
@ -886,7 +883,7 @@ int create_animated_webp_from_sd_images(const char* filename, sd_image_t* images
}
const int frame_duration_ms = std::max(1, static_cast<int>(std::lround(1000.0 / static_cast<double>(fps))));
int timestamp_ms = 0;
int timestamp_ms = 0;
for (int i = 0; i < num_images; ++i) {
const sd_image_t& image = images[i];
@ -969,7 +966,7 @@ int create_webm_from_sd_images(const char* filename, sd_image_t* images, int num
return -1;
}
const int width = static_cast<int>(images[0].width);
const int width = static_cast<int>(images[0].width);
const int height = static_cast<int>(images[0].height);
if (width <= 0 || height <= 0) {
fprintf(stderr, "Error: Invalid frame dimensions.\n");

View File

@ -57,6 +57,7 @@ else()
endif()
add_executable(${TARGET}
../common/common.cpp
../common/log.cpp
../common/media_io.cpp
main.cpp

View File

@ -8,10 +8,11 @@
#include <sstream>
#include <vector>
#include <json.hpp>
#include "httplib.h"
#include "stable-diffusion.h"
#include "common/common.hpp"
#include "common/common.h"
#include "common/media_io.h"
#include "common/resource_owners.hpp"
@ -19,6 +20,7 @@
#include "frontend/dist/gen_index_html.h"
#endif
using json = nlohmann::json;
namespace fs = std::filesystem;
// ----------------------- helpers -----------------------

View File

@ -1,4 +1,6 @@
for f in src/*.cpp src/*.h src/*.hpp src/vocab/*.h src/vocab/*.cpp examples/cli/*.cpp examples/common/*.hpp examples/cli/*.h examples/server/*.cpp; do
for f in src/*.cpp src/*.h src/*.hpp src/vocab/*.h src/vocab/*.cpp \
examples/cli/*.cpp examples/cli/*.h examples/server/*.cpp \
examples/common/*.hpp examples/common/*.h examples/common/*.cpp; do
[[ "$f" == vocab* ]] && continue
echo "formatting '$f'"
# if [ "$f" != "stable-diffusion.h" ]; then