Skip to content

Commit 1ac5a61

Browse files
authored
feat: support custom upscale tile size (#896)
1 parent d939f6e commit 1ac5a61

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

esrgan.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ struct ESRGAN : public GGMLRunner {
156156

157157
ESRGAN(ggml_backend_t backend,
158158
bool offload_params_to_cpu,
159+
int tile_size = 128,
159160
const String2TensorStorage& tensor_storage_map = {})
160161
: GGMLRunner(backend, offload_params_to_cpu) {
161-
// rrdb_net will be created in load_from_file
162+
this->tile_size = tile_size;
162163
}
163164

164165
std::string get_desc() override {

examples/cli/main.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,8 @@ struct SDGenerationParams {
10791079
std::string pm_id_embed_path;
10801080
float pm_style_strength = 20.f;
10811081

1082-
int upscale_repeats = 1;
1082+
int upscale_repeats = 1;
1083+
int upscale_tile_size = 128;
10831084

10841085
std::map<std::string, float> lora_map;
10851086
std::map<std::string, float> high_noise_lora_map;
@@ -1176,6 +1177,10 @@ struct SDGenerationParams {
11761177
"--upscale-repeats",
11771178
"Run the ESRGAN upscaler this many times (default: 1)",
11781179
&upscale_repeats},
1180+
{"",
1181+
"--upscale-tile-size",
1182+
"tile size for ESRGAN upscaling (default: 128)",
1183+
&upscale_tile_size},
11791184
};
11801185

11811186
options.float_options = {
@@ -1635,6 +1640,10 @@ struct SDGenerationParams {
16351640
return false;
16361641
}
16371642

1643+
if (upscale_tile_size < 1) {
1644+
return false;
1645+
}
1646+
16381647
if (mode == UPSCALE) {
16391648
if (init_image_path.length() == 0) {
16401649
fprintf(stderr, "error: upscale mode needs an init image (--init-img)\n");
@@ -1720,6 +1729,7 @@ struct SDGenerationParams {
17201729
<< " control_strength: " << control_strength << ",\n"
17211730
<< " seed: " << seed << ",\n"
17221731
<< " upscale_repeats: " << upscale_repeats << ",\n"
1732+
<< " upscale_tile_size: " << upscale_tile_size << ",\n"
17231733
<< "}";
17241734
free(sample_params_str);
17251735
free(high_noise_sample_params_str);
@@ -2336,7 +2346,8 @@ int main(int argc, const char* argv[]) {
23362346
upscaler_ctx_t* upscaler_ctx = new_upscaler_ctx(ctx_params.esrgan_path.c_str(),
23372347
ctx_params.offload_params_to_cpu,
23382348
ctx_params.diffusion_conv_direct,
2339-
ctx_params.n_threads);
2349+
ctx_params.n_threads,
2350+
gen_params.upscale_tile_size);
23402351

23412352
if (upscaler_ctx == nullptr) {
23422353
printf("new_upscaler_ctx failed\n");

stable-diffusion.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ typedef struct upscaler_ctx_t upscaler_ctx_t;
347347
SD_API upscaler_ctx_t* new_upscaler_ctx(const char* esrgan_path,
348348
bool offload_params_to_cpu,
349349
bool direct,
350-
int n_threads);
350+
int n_threads,
351+
int tile_size);
351352
SD_API void free_upscaler_ctx(upscaler_ctx_t* upscaler_ctx);
352353

353354
SD_API sd_image_t upscale(upscaler_ctx_t* upscaler_ctx,

upscaler.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ struct UpscalerGGML {
99
std::shared_ptr<ESRGAN> esrgan_upscaler;
1010
std::string esrgan_path;
1111
int n_threads;
12-
bool direct = false;
12+
bool direct = false;
13+
int tile_size = 128;
1314

1415
UpscalerGGML(int n_threads,
15-
bool direct = false)
16+
bool direct = false,
17+
int tile_size = 128)
1618
: n_threads(n_threads),
17-
direct(direct) {
19+
direct(direct),
20+
tile_size(tile_size) {
1821
}
1922

2023
bool load_from_file(const std::string& esrgan_path,
@@ -51,7 +54,7 @@ struct UpscalerGGML {
5154
backend = ggml_backend_cpu_init();
5255
}
5356
LOG_INFO("Upscaler weight type: %s", ggml_type_name(model_data_type));
54-
esrgan_upscaler = std::make_shared<ESRGAN>(backend, offload_params_to_cpu, model_loader.get_tensor_storage_map());
57+
esrgan_upscaler = std::make_shared<ESRGAN>(backend, offload_params_to_cpu, tile_size, model_loader.get_tensor_storage_map());
5558
if (direct) {
5659
esrgan_upscaler->set_conv2d_direct_enabled(true);
5760
}
@@ -113,14 +116,15 @@ struct upscaler_ctx_t {
113116
upscaler_ctx_t* new_upscaler_ctx(const char* esrgan_path_c_str,
114117
bool offload_params_to_cpu,
115118
bool direct,
116-
int n_threads) {
119+
int n_threads,
120+
int tile_size) {
117121
upscaler_ctx_t* upscaler_ctx = (upscaler_ctx_t*)malloc(sizeof(upscaler_ctx_t));
118122
if (upscaler_ctx == nullptr) {
119123
return nullptr;
120124
}
121125
std::string esrgan_path(esrgan_path_c_str);
122126

123-
upscaler_ctx->upscaler = new UpscalerGGML(n_threads, direct);
127+
upscaler_ctx->upscaler = new UpscalerGGML(n_threads, direct, tile_size);
124128
if (upscaler_ctx->upscaler == nullptr) {
125129
return nullptr;
126130
}

0 commit comments

Comments
 (0)