🌐 AI搜索 & 代理 主页
Skip to content

Commit 6888fcb

Browse files
authored
feat: server add default_gen_params to override default args (#1050)
1 parent 2aecdd5 commit 6888fcb

File tree

3 files changed

+103
-15
lines changed

3 files changed

+103
-15
lines changed

examples/common/common.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,4 +1749,4 @@ uint8_t* load_image_from_memory(const char* image_bytes,
17491749
int expected_height = 0,
17501750
int expected_channel = 3) {
17511751
return load_image_common(true, image_bytes, len, width, height, expected_width, expected_height, expected_channel);
1752-
}
1752+
}

examples/server/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,63 @@ Context Options:
6060
--vae-tile-size tile size for vae tiling, format [X]x[Y] (default: 32x32)
6161
--vae-relative-tile-size relative tile size for vae tiling, format [X]x[Y], in fraction of image size if < 1, in number of tiles per dim if >=1
6262
(overrides --vae-tile-size)
63+
64+
Default Generation Options:
65+
-p, --prompt <string> the prompt to render
66+
-n, --negative-prompt <string> the negative prompt (default: "")
67+
-i, --init-img <string> path to the init image
68+
--end-img <string> path to the end image, required by flf2v
69+
--mask <string> path to the mask image
70+
--control-image <string> path to control image, control net
71+
--control-video <string> path to control video frames, It must be a directory path. The video frames inside should be stored as images in
72+
lexicographical (character) order. For example, if the control video path is
73+
`frames`, the directory contain images such as 00.png, 01.png, ... etc.
74+
--pm-id-images-dir <string> path to PHOTOMAKER input id images dir
75+
--pm-id-embed-path <string> path to PHOTOMAKER v2 id embed
76+
-H, --height <int> image height, in pixel space (default: 512)
77+
-W, --width <int> image width, in pixel space (default: 512)
78+
--steps <int> number of sample steps (default: 20)
79+
--high-noise-steps <int> (high noise) number of sample steps (default: -1 = auto)
80+
--clip-skip <int> ignore last layers of CLIP network; 1 ignores none, 2 ignores one layer (default: -1). <= 0 represents unspecified,
81+
will be 1 for SD1.x, 2 for SD2.x
82+
-b, --batch-count <int> batch count
83+
--video-frames <int> video frames (default: 1)
84+
--fps <int> fps (default: 24)
85+
--timestep-shift <int> shift timestep for NitroFusion models (default: 0). recommended N for NitroSD-Realism around 250 and 500 for
86+
NitroSD-Vibrant
87+
--upscale-repeats <int> Run the ESRGAN upscaler this many times (default: 1)
88+
--upscale-tile-size <int> tile size for ESRGAN upscaling (default: 128)
89+
--cfg-scale <float> unconditional guidance scale: (default: 7.0)
90+
--img-cfg-scale <float> image guidance scale for inpaint or instruct-pix2pix models: (default: same as --cfg-scale)
91+
--guidance <float> distilled guidance scale for models with guidance input (default: 3.5)
92+
--slg-scale <float> skip layer guidance (SLG) scale, only for DiT models: (default: 0). 0 means disabled, a value of 2.5 is nice for sd3.5
93+
medium
94+
--skip-layer-start <float> SLG enabling point (default: 0.01)
95+
--skip-layer-end <float> SLG disabling point (default: 0.2)
96+
--eta <float> eta in DDIM, only for DDIM and TCD (default: 0)
97+
--high-noise-cfg-scale <float> (high noise) unconditional guidance scale: (default: 7.0)
98+
--high-noise-img-cfg-scale <float> (high noise) image guidance scale for inpaint or instruct-pix2pix models (default: same as --cfg-scale)
99+
--high-noise-guidance <float> (high noise) distilled guidance scale for models with guidance input (default: 3.5)
100+
--high-noise-slg-scale <float> (high noise) skip layer guidance (SLG) scale, only for DiT models: (default: 0)
101+
--high-noise-skip-layer-start <float> (high noise) SLG enabling point (default: 0.01)
102+
--high-noise-skip-layer-end <float> (high noise) SLG disabling point (default: 0.2)
103+
--high-noise-eta <float> (high noise) eta in DDIM, only for DDIM and TCD (default: 0)
104+
--strength <float> strength for noising/unnoising (default: 0.75)
105+
--pm-style-strength <float>
106+
--control-strength <float> strength to apply Control Net (default: 0.9). 1.0 corresponds to full destruction of information in init image
107+
--moe-boundary <float> timestep boundary for Wan2.2 MoE model. (default: 0.875). Only enabled if `--high-noise-steps` is set to -1
108+
--vace-strength <float> wan vace strength
109+
--increase-ref-index automatically increase the indices of references images based on the order they are listed (starting with 1).
110+
--disable-auto-resize-ref-image disable auto resize of ref images
111+
-s, --seed RNG seed (default: 42, use random seed for < 0)
112+
--sampling-method sampling method, one of [euler, euler_a, heun, dpm2, dpm++2s_a, dpm++2m, dpm++2mv2, ipndm, ipndm_v, lcm, ddim_trailing,
113+
tcd] (default: euler for Flux/SD3/Wan, euler_a otherwise)
114+
--high-noise-sampling-method (high noise) sampling method, one of [euler, euler_a, heun, dpm2, dpm++2s_a, dpm++2m, dpm++2mv2, ipndm, ipndm_v, lcm,
115+
ddim_trailing, tcd] default: euler for Flux/SD3/Wan, euler_a otherwise
116+
--scheduler denoiser sigma scheduler, one of [discrete, karras, exponential, ays, gits, smoothstep, sgm_uniform, simple, lcm],
117+
default: discrete
118+
--skip-layers layers to skip for SLG steps (default: [7,8,9])
119+
--high-noise-skip-layers (high noise) layers to skip for SLG steps (default: [7,8,9])
120+
-r, --ref-image reference image for Flux Kontext models (can be used multiple times)
121+
--easycache enable EasyCache for DiT models with optional "threshold,start_percent,end_percent" (default: 0.2,0.15,0.95)
63122
```

examples/server/main.cpp

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,21 @@ void print_usage(int argc, const char* argv[], const std::vector<ArgOptions>& op
179179
options_list[0].print();
180180
std::cout << "\nContext Options:\n";
181181
options_list[1].print();
182+
std::cout << "\nDefault Generation Options:\n";
183+
options_list[2].print();
182184
}
183185

184-
void parse_args(int argc, const char** argv, SDSvrParams& svr_params, SDContextParams& ctx_params) {
185-
std::vector<ArgOptions> options_vec = {svr_params.get_options(), ctx_params.get_options()};
186+
void parse_args(int argc, const char** argv, SDSvrParams& svr_params, SDContextParams& ctx_params, SDGenerationParams& default_gen_params) {
187+
std::vector<ArgOptions> options_vec = {svr_params.get_options(), ctx_params.get_options(), default_gen_params.get_options()};
186188

187189
if (!parse_options(argc, argv, options_vec)) {
188190
print_usage(argc, argv, options_vec);
189191
exit(svr_params.normal_exit ? 0 : 1);
190192
}
191193

192-
if (!svr_params.process_and_check() || !ctx_params.process_and_check(IMG_GEN)) {
194+
if (!svr_params.process_and_check() ||
195+
!ctx_params.process_and_check(IMG_GEN) ||
196+
!default_gen_params.process_and_check(IMG_GEN, ctx_params.lora_model_dir)) {
193197
print_usage(argc, argv, options_vec);
194198
exit(1);
195199
}
@@ -298,14 +302,16 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
298302
int main(int argc, const char** argv) {
299303
SDSvrParams svr_params;
300304
SDContextParams ctx_params;
301-
parse_args(argc, argv, svr_params, ctx_params);
305+
SDGenerationParams default_gen_params;
306+
parse_args(argc, argv, svr_params, ctx_params, default_gen_params);
302307

303308
sd_set_log_callback(sd_log_cb, (void*)&svr_params);
304309

305310
if (svr_params.verbose) {
306311
printf("%s", sd_get_system_info());
307312
printf("%s\n", svr_params.to_string().c_str());
308313
printf("%s\n", ctx_params.to_string().c_str());
314+
printf("%s\n", default_gen_params.to_string().c_str());
309315
}
310316

311317
sd_ctx_params_t sd_ctx_params = ctx_params.to_sd_ctx_params_t(false, false, false);
@@ -320,6 +326,23 @@ int main(int argc, const char** argv) {
320326

321327
httplib::Server svr;
322328

329+
svr.set_pre_routing_handler([](const httplib::Request& req, httplib::Response& res) {
330+
std::string origin = req.get_header_value("Origin");
331+
if (origin.empty()) {
332+
origin = "*";
333+
}
334+
res.set_header("Access-Control-Allow-Origin", origin);
335+
res.set_header("Access-Control-Allow-Credentials", "true");
336+
res.set_header("Access-Control-Allow-Methods", "*");
337+
res.set_header("Access-Control-Allow-Headers", "*");
338+
339+
if (req.method == "OPTIONS") {
340+
res.status = 204;
341+
return httplib::Server::HandlerResponse::Handled;
342+
}
343+
return httplib::Server::HandlerResponse::Unhandled;
344+
});
345+
323346
// health
324347
svr.Get("/", [&](const httplib::Request&, httplib::Response& res) {
325348
res.set_content(R"({"ok":true,"service":"sd-cpp-http"})", "application/json");
@@ -390,11 +413,11 @@ int main(int argc, const char** argv) {
390413
out["data"] = json::array();
391414
out["output_format"] = output_format;
392415

393-
SDGenerationParams gen_params;
394-
gen_params.prompt = prompt;
395-
gen_params.width = width;
396-
gen_params.height = height;
397-
gen_params.batch_count = n;
416+
SDGenerationParams gen_params = default_gen_params;
417+
gen_params.prompt = prompt;
418+
gen_params.width = width;
419+
gen_params.height = height;
420+
gen_params.batch_count = n;
398421

399422
if (!sd_cpp_extra_args_str.empty() && !gen_params.from_json_str(sd_cpp_extra_args_str)) {
400423
res.status = 400;
@@ -570,18 +593,24 @@ int main(int argc, const char** argv) {
570593
output_compression = 0;
571594
}
572595

573-
SDGenerationParams gen_params;
574-
gen_params.prompt = prompt;
575-
gen_params.width = width;
576-
gen_params.height = height;
577-
gen_params.batch_count = n;
596+
SDGenerationParams gen_params = default_gen_params;
597+
gen_params.prompt = prompt;
598+
gen_params.width = width;
599+
gen_params.height = height;
600+
gen_params.batch_count = n;
578601

579602
if (!sd_cpp_extra_args_str.empty() && !gen_params.from_json_str(sd_cpp_extra_args_str)) {
580603
res.status = 400;
581604
res.set_content(R"({"error":"invalid sd_cpp_extra_args"})", "application/json");
582605
return;
583606
}
584607

608+
if (!gen_params.process_and_check(IMG_GEN, ctx_params.lora_model_dir)) {
609+
res.status = 400;
610+
res.set_content(R"({"error":"invalid params"})", "application/json");
611+
return;
612+
}
613+
585614
if (svr_params.verbose) {
586615
printf("%s\n", gen_params.to_string().c_str());
587616
}

0 commit comments

Comments
 (0)