Compare commits

...
11 changed files with 252 additions and 279 deletions
+18 -21
View File
@@ -91,34 +91,34 @@ void log_print(enum sd_log_level_t level, const char* log, sd_log_level_t min_le
switch (level) {
case SD_LOG_DEBUG:
tag_color = 37;
level_str = "DEBUG";
level_str = "D";
break;
case SD_LOG_VERBOSE:
tag_color = 37;
level_str = "VERBOSE";
level_str = "V";
break;
case SD_LOG_INFO:
tag_color = 34;
level_str = "INFO";
level_str = "I";
break;
case SD_LOG_WARN:
tag_color = 35;
level_str = "WARN";
level_str = "W";
break;
case SD_LOG_ERROR:
tag_color = 31;
level_str = "ERROR";
level_str = "E";
break;
default:
tag_color = 33;
level_str = "?????";
level_str = "?";
break;
}
if (color) {
fprintf(out_stream, "\033[%d;1m[%-7s]\033[0m ", tag_color, level_str);
fprintf(out_stream, "\033[%d;1m[%s]\033[0m ", tag_color, level_str);
} else {
fprintf(out_stream, "[%-7s] ", level_str);
fprintf(out_stream, "[%s] ", level_str);
}
fflush(out_stream);
print_utf8(out_stream, log);
@@ -131,18 +131,15 @@ void example_log_printf(sd_log_level_t level, const char* file, int line, const
va_list args;
va_start(args, format);
static char log_buffer[LOG_BUFFER_SIZE + 1];
int written = snprintf(log_buffer, LOG_BUFFER_SIZE, "%s:%-4d - ", sd_basename(file).c_str(), line);
if (written >= 0 && written < static_cast<int>(LOG_BUFFER_SIZE)) {
vsnprintf(log_buffer + written, LOG_BUFFER_SIZE - written, format, args);
}
size_t len = strlen(log_buffer);
if (len == 0 || log_buffer[len - 1] != '\n') {
strncat(log_buffer, "\n", LOG_BUFFER_SIZE - len);
}
log_print(level, log_buffer, log_level, log_color);
char log_buffer[LOG_BUFFER_SIZE + 1] = {};
vsnprintf(log_buffer, sizeof(log_buffer), format, args);
va_end(args);
std::string message = log_buffer;
while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) {
message.pop_back();
}
message += " - " + sd_basename(file) + ":" + std::to_string(line) + '\n';
log_print(level, message.c_str(), log_level, log_color);
}
+31
View File
@@ -410,6 +410,20 @@ Field types:
| `queue_position` | `integer` |
| `result` | `object \| null` |
| `error` | `object \| null` |
| `preview` | `object \| null` |
`preview` sub-fields:
| Field | Type | Notes |
| --- | --- | --- |
| `preview.pass` | `integer` | Sampling pass number, starting at 1 within the job |
| `preview.step` | `integer` | Positive logical sample step within this pass |
| `preview.total_steps` | `integer` | Actual sample steps in this pass, after schedule and strength adjustments |
| `preview.b64_json` | `string` | Base64-encoded PNG of the current preview frame |
The preview is updated in place as sampling progresses; poll `GET /sdcpp/v1/jobs/{id}` to retrieve the latest frame. Only the most recent preview is retained.
`step / total_steps` describes the current sampling pass, not overall job completion. A new batch item, high/low-noise stage, or highres pass increments `pass` and restarts `step`. Samplers with multiple denoiser evaluations per logical step may update a preview more than once at the same step. The frame and its pass/step metadata are published together.
### Endpoints
@@ -437,6 +451,7 @@ Top-level fields:
| `loras` | `array<object>` | Available LoRA entries |
| `upscalers` | `array<object>` | Available highres upscalers, built-in and model-backed |
| `upscale` | `boolean` | Whether a compatible RGB ESRGAN model is available for `POST /sdcpp/v1/upscale` |
| `preview_modes` | `array<string>` | Available preview modes, e.g. `["none", "proj", "tae", "vae"]` |
| `limits` | `object` | Shared queue and size limits |
`model`
@@ -598,6 +613,7 @@ Fields returned in `features_by_mode.img_gen`:
- `cache`
- `cancel_queued`
- `cancel_generating`
- `preview`
Fields returned in `features_by_mode.vid_gen`:
@@ -610,6 +626,7 @@ Fields returned in `features_by_mode.vid_gen`:
- `cache`
- `cancel_queued`
- `cancel_generating`
- `preview`
#### `POST /sdcpp/v1/img_gen`
@@ -782,6 +799,9 @@ Example:
"scm_mask": "",
"scm_policy_dynamic": true,
"preview": "none",
"preview_interval": 1,
"output_format": "png",
"output_compression": 100
}
@@ -847,6 +867,8 @@ Top-level scalar fields:
| `control_strength` | `number` |
| `ip_adapter_strength` | `number` |
| `embed_image_metadata` | `boolean` |
| `preview` | `string` |
| `preview_interval` | `integer` |
Image fields:
@@ -934,6 +956,10 @@ When omitted, backend defaults apply to these fields:
- `sample_params.flow_shift`
- `sample_params.guidance.img_cfg`
### Preview Interval Semantics
`preview_interval` controls the period (in sample steps) at which preview frames are generated. The default is `1` (every step). Any non-positive values will be clamped to `1`. Note that if `preview` is set to `"none"`, no previews are produced regardless of the interval.
### Completion Result
Example completed job:
@@ -1130,6 +1156,9 @@ Example:
"scm_mask": "",
"scm_policy_dynamic": true,
"preview": "none",
"preview_interval": 1,
"output_format": "webm",
"output_compression": 100
}
@@ -1181,6 +1210,8 @@ Top-level scalar fields:
| `fps` | `integer` |
| `moe_boundary` | `number` |
| `vace_strength` | `number` |
| `preview` | `string` |
| `preview_interval` | `integer` |
Image and frame fields:
+65
View File
@@ -10,6 +10,55 @@
#include "common/media_io.h"
#include "common/resource_owners.hpp"
static void preview_callback(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data) {
(void)is_noisy;
if (frame_count <= 0 || frames == nullptr || frames[0].data == nullptr) {
return;
}
AsyncGenerationJob* job = static_cast<AsyncGenerationJob*>(data);
const sd_image_t& frame = frames[0];
const sd_preview_info_t info = sd_get_preview_info();
auto image_bytes = encode_image_to_vector(EncodedImageFormat::PNG,
frame.data,
frame.width,
frame.height,
frame.channel,
"",
80);
if (image_bytes.empty()) {
return;
}
std::string b64 = base64_encode(image_bytes);
std::lock_guard<std::mutex> lock(job->preview_mutex);
job->preview_b64 = std::move(b64);
job->preview_step = step < 0 ? -step : step;
job->preview_pass = info.sample_pass;
job->preview_total_steps = info.total_steps;
}
static void clear_preview_callback() {
sd_set_preview_callback(nullptr, PREVIEW_NONE, 1, false, false, nullptr);
}
static void set_preview_callback(AsyncGenerationJob& job) {
preview_t mode = str_to_preview(job.kind == AsyncJobKind::ImgGen
? job.img_gen.preview_mode.c_str()
: job.vid_gen.preview_mode.c_str());
if (mode == PREVIEW_COUNT || mode == PREVIEW_NONE) {
return;
}
int interval = job.kind == AsyncJobKind::ImgGen
? job.img_gen.preview_interval
: job.vid_gen.preview_interval;
if (interval <= 0) {
interval = 1;
}
sd_set_preview_callback(preview_callback, mode, interval, true, false, &job);
}
const char* async_job_kind_name(AsyncJobKind kind) {
switch (kind) {
case AsyncJobKind::ImgGen:
@@ -161,6 +210,18 @@ json make_async_job_json(const AsyncJobManager& manager, const AsyncGenerationJo
result["error"] = nullptr;
}
if (job.status == AsyncJobStatus::Generating) {
std::lock_guard<std::mutex> lock(job.preview_mutex);
if (!job.preview_b64.empty()) {
result["preview"] = {
{"pass", job.preview_pass},
{"step", job.preview_step},
{"total_steps", job.preview_total_steps},
{"b64_json", job.preview_b64},
};
}
}
return result;
}
@@ -174,12 +235,14 @@ bool execute_img_gen_job(ServerRuntime& runtime,
{
std::lock_guard<std::mutex> lock(*runtime.sd_ctx_mutex);
set_preview_callback(job);
sd_image_t* raw_results = nullptr;
int num_results = 0;
if (!generate_image(runtime.sd_ctx, &params, &raw_results, &num_results)) {
raw_results = nullptr;
num_results = 0;
}
clear_preview_callback();
results.adopt(raw_results, num_results);
}
@@ -247,10 +310,12 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
{
std::lock_guard<std::mutex> lock(*runtime.sd_ctx_mutex);
set_preview_callback(job);
sd_image_t* raw_results = nullptr;
if (!generate_video(runtime.sd_ctx, &params, &raw_results, &num_results, &generated_audio, &output_fps)) {
raw_results = nullptr;
}
clear_preview_callback();
results.adopt(raw_results, num_results);
}
+5
View File
@@ -44,6 +44,11 @@ struct AsyncGenerationJob {
int result_fps = 0;
std::string error_code;
std::string error_message;
mutable std::mutex preview_mutex;
std::string preview_b64;
int preview_pass = 0;
int preview_step = 0;
int preview_total_steps = 0;
};
struct AsyncJobManager {
+15
View File
@@ -205,6 +205,7 @@ static json make_img_gen_features_json() {
{"cache", true},
{"cancel_queued", true},
{"cancel_generating", false},
{"preview", true},
};
}
@@ -219,9 +220,18 @@ static json make_vid_gen_features_json() {
{"cache", true},
{"cancel_queued", true},
{"cancel_generating", false},
{"preview", true},
};
}
static json make_preview_modes_json() {
json modes = json::array();
for (int i = 0; i < PREVIEW_COUNT; ++i) {
modes.push_back(sd_preview_name((preview_t)i));
}
return modes;
}
static json make_capabilities_json(ServerRuntime& runtime) {
refresh_lora_cache(runtime);
refresh_upscaler_cache(runtime);
@@ -396,6 +406,7 @@ static json make_capabilities_json(ServerRuntime& runtime) {
result["loras"] = available_loras;
result["upscalers"] = available_upscalers;
result["upscale"] = have_upscaler_models;
result["preview_modes"] = make_preview_modes_json();
return result;
}
@@ -418,6 +429,8 @@ static bool parse_img_gen_request(const json& body,
if (!assign_output_options(request, output_format, output_compression, true, error_message)) {
return false;
}
request.preview_mode = body.value("preview", std::string("none"));
request.preview_interval = body.value("preview_interval", 1);
// Intentionally disable prompt-embedded LoRA tag parsing for server APIs.
if (!request.gen_params.resolve_and_validate(IMG_GEN, "", runtime.ctx_params->hires_upscalers_dir, true)) {
error_message = "invalid generation parameters";
@@ -445,6 +458,8 @@ static bool parse_vid_gen_request(const json& body,
if (!assign_output_options(request, output_format, output_compression, error_message)) {
return false;
}
request.preview_mode = body.value("preview", std::string("none"));
request.preview_interval = body.value("preview_interval", 1);
// Intentionally disable prompt-embedded LoRA tag parsing for server APIs.
if (!request.gen_params.resolve_and_validate(VID_GEN, "", runtime.ctx_params->hires_upscalers_dir, true)) {
error_message = "invalid generation parameters";
+4
View File
@@ -66,6 +66,8 @@ struct ImgGenJobRequest {
SDGenerationParams gen_params;
std::string output_format = "png";
int output_compression = 100;
std::string preview_mode = "none";
int preview_interval = 1;
sd_img_gen_params_t to_sd_img_gen_params_t() {
return gen_params.to_sd_img_gen_params_t();
@@ -76,6 +78,8 @@ struct VidGenJobRequest {
SDGenerationParams gen_params;
std::string output_format = "webm";
int output_compression = 100;
std::string preview_mode = "none";
int preview_interval = 1;
sd_vid_gen_params_t to_sd_vid_gen_params_t() {
return gen_params.to_sd_vid_gen_params_t();
+9
View File
@@ -461,6 +461,11 @@ 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);
@@ -469,6 +474,10 @@ SD_API void sd_set_progress_callback(sd_progress_cb_t cb, void* data);
// 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();
+28 -10
View File
@@ -360,12 +360,13 @@ int32_t sd_get_num_physical_cores() {
static sd_progress_cb_t sd_progress_cb = nullptr;
void* sd_progress_cb_data = nullptr;
static sd_preview_cb_t sd_preview_cb = nullptr;
static void* sd_preview_cb_data = nullptr;
preview_t sd_preview_mode = PREVIEW_NONE;
int sd_preview_interval = 1;
bool sd_preview_denoised = true;
bool sd_preview_noisy = false;
static sd_preview_info_t sd_preview_info = {};
static sd_preview_cb_t sd_preview_cb = nullptr;
static void* sd_preview_cb_data = nullptr;
preview_t sd_preview_mode = PREVIEW_NONE;
int sd_preview_interval = 1;
bool sd_preview_denoised = true;
bool sd_preview_noisy = false;
static sd_graph_eval_callback_t sd_backend_eval_cb = nullptr;
static void* sd_backend_eval_cb_data = nullptr;
@@ -644,9 +645,14 @@ void* sd_log_cb_data = nullptr;
static void sd_log_dispatch(sd_log_level_t level, const std::string& origin, const std::string& text) {
if (sd_log_cb == nullptr)
return;
std::string message = origin + " - " + text;
if (message.back() != '\n') {
message += '\n';
std::string message = text;
while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) {
message.pop_back();
}
if (origin == "ggml") {
message = origin + ": " + message + '\n';
} else {
message += " - " + origin + '\n';
}
sd_log_cb(level, message.c_str(), sd_log_cb_data);
}
@@ -656,7 +662,7 @@ void log_printf(sd_log_level_t level, const char* file, int line, const char* fo
va_start(args, format);
std::string message = sd_vformat(format, args);
va_end(args);
std::string origin = sd_format("%s:%-4d", sd_basename(file).c_str(), line);
std::string origin = sd_format("%s:%d", sd_basename(file).c_str(), line);
sd_log_dispatch(level, origin, message);
}
@@ -691,6 +697,7 @@ void sd_set_progress_callback(sd_progress_cb_t cb, void* data) {
sd_progress_cb_data = data;
}
void sd_set_preview_callback(sd_preview_cb_t cb, preview_t mode, int interval, bool denoised, bool noisy, void* data) {
sd_preview_info = {};
sd_preview_cb = cb;
sd_preview_cb_data = data;
sd_preview_mode = mode;
@@ -704,6 +711,17 @@ void sd_set_backend_eval_callback(sd_graph_eval_callback_t cb, void* data) {
sd_backend_eval_cb_data = data;
}
void sd_begin_preview_pass(int total_steps) {
if (sd_preview_cb != nullptr) {
++sd_preview_info.sample_pass;
sd_preview_info.total_steps = total_steps;
}
}
sd_preview_info_t sd_get_preview_info() {
return sd_preview_info;
}
sd_preview_cb_t sd_get_preview_callback() {
return sd_preview_cb;
}
+1
View File
@@ -101,6 +101,7 @@ std::vector<std::pair<std::string, float>> split_quotation_attention(
sd_progress_cb_t sd_get_progress_callback();
void* sd_get_progress_callback_data();
void sd_begin_preview_pass(int total_steps);
sd_preview_cb_t sd_get_preview_callback();
void* sd_get_preview_callback_data();
preview_t sd_get_preview_mode();
+2 -1
View File
@@ -2373,7 +2373,8 @@ sd::Tensor<float> StableDiffusionGGML::sample(const std::shared_ptr<DiffusionMod
noise *= eta;
}
int64_t last_progress_us = ggml_time_us();
int64_t last_progress_us = ggml_time_us();
sd_begin_preview_pass(static_cast<int>(steps));
SamplePreviewContext preview = prepare_sample_preview_context();
sd::Tensor<float> processed_init_latent = denoiser->process_latent_in(init_latent);
+74 -247
View File
@@ -8,60 +8,51 @@
#include "core/util.h"
#include "ggml.h"
static int sd_tiling_calc_num_tiles(int dimension, int tile_size, float target_overlap_factor) {
if (dimension <= tile_size) {
return 1;
} else if (dimension < 2 * tile_size) {
return 2;
} else if (dimension == 2 * tile_size) {
return 3;
} else {
float target_num_tiles = 1.0f + (dimension - tile_size) / ((1.0f - target_overlap_factor) * tile_size);
int num_tiles_lower = static_cast<int>(std::floor(target_num_tiles));
int num_tiles_upper = static_cast<int>(std::ceil(target_num_tiles));
int num_tiles_min = 1 + (dimension - 2) / (tile_size - 1); // positive adjacent overlap
int num_tiles_max = 2 * dimension / tile_size - 1; // no triple overlap under Bresenham placement
num_tiles_lower = std::clamp(num_tiles_lower, num_tiles_min, num_tiles_max);
num_tiles_upper = std::clamp(num_tiles_upper, num_tiles_min, num_tiles_max);
auto overlap_error = [target_num_tiles](int num_tiles) -> float {
return std::abs(1.0f / (num_tiles - 1.0f) - 1.0f / (target_num_tiles - 1.0f));
}; // (dimension - tile_size) / tile_size factors out
return (overlap_error(num_tiles_upper) < overlap_error(num_tiles_lower)) ? num_tiles_upper : num_tiles_lower; // use lower if tie
struct TileSpan {
int offset;
int overlap_before;
int overlap_after;
};
static std::vector<TileSpan> sd_tiling_plan_axis(int dimension,
int tile_size,
float target_overlap,
bool circular) {
if (tile_size >= dimension) {
return {{0, 0, 0}};
}
}
static float sd_tiling_calc_average_stride_factor(int dimension, int tile_size, int num_tiles) {
return static_cast<float>(dimension - tile_size) / static_cast<float>(tile_size * (num_tiles - 1));
}
static void sd_tiling_calc_tiles(int& num_tiles_dim,
float& tile_overlap_factor_dim,
int small_dim,
int tile_size,
const float tile_overlap_factor,
bool circular) {
int tile_overlap = static_cast<int>(tile_size * tile_overlap_factor);
int non_tile_overlap = tile_size - tile_overlap;
if (circular) {
// circular means the last and first tile are overlapping (wraping around)
num_tiles_dim = small_dim / non_tile_overlap;
if (num_tiles_dim < 1) {
num_tiles_dim = 1;
}
tile_overlap_factor_dim = (tile_size - small_dim / num_tiles_dim) / (float)tile_size;
// if single tile and tile_overlap_factor is not 0, add one to ensure we have at least two overlapping tiles
if (num_tiles_dim == 1 && tile_overlap_factor_dim > 0) {
num_tiles_dim++;
tile_overlap_factor_dim = 0.5;
}
} else {
num_tiles_dim = sd_tiling_calc_num_tiles(small_dim, tile_size, tile_overlap_factor);
tile_overlap_factor_dim = (num_tiles_dim == 1) ? 0 : (1.0f - sd_tiling_calc_average_stride_factor(small_dim, tile_size, num_tiles_dim));
const int extent = circular ? dimension : dimension - tile_size;
int intervals = extent;
if (tile_size > 1) {
const float target = extent / ((1.0f - target_overlap) * tile_size);
// Adjacent tiles must overlap, but three tiles must not cover the same point.
const int min_intervals = 1 + (extent - 1) / (tile_size - 1);
const int max_intervals = std::max(1, static_cast<int>(2LL * extent / tile_size));
const int lower = std::clamp(static_cast<int>(std::floor(target)), min_intervals, max_intervals);
const int upper = std::clamp(static_cast<int>(std::ceil(target)), min_intervals, max_intervals);
auto error = [&](int count) {
return std::abs(1.0f / count - 1.0f / target);
};
intervals = error(upper) < error(lower) ? upper : lower;
}
const int num_tiles = circular ? intervals : intervals + 1;
auto position = [&](int index) -> int {
return static_cast<int>(int64_t(index) * extent / intervals);
};
std::vector<TileSpan> tiles;
tiles.reserve(num_tiles);
for (int i = 0; i < num_tiles; ++i) {
const int offset = position(i);
// Keep wrapped neighbors in the same unwrapped coordinate space.
const int previous = i > 0 ? position(i - 1) : position(num_tiles - 1) - dimension;
const int next = i + 1 < num_tiles ? position(i + 1) : dimension;
const int before = i > 0 || circular ? previous + tile_size - offset : 0;
const int after = i + 1 < num_tiles || circular ? offset + tile_size - next : 0;
tiles.push_back({offset, before, after});
}
return tiles;
}
static int64_t sd_tensor_plane_size(const sd::Tensor<float>& tensor) {
@@ -96,62 +87,10 @@ static void sd_tensor_merge_2d(const sd::Tensor<float>& input,
sd::Tensor<float>* output,
int x,
int y,
int overlap_x,
int overlap_y,
bool circular_x,
bool circular_y,
int x_skip,
int y_skip) {
GGML_ASSERT(output != nullptr);
int64_t width = input.shape()[0];
int64_t height = input.shape()[1];
int64_t img_width = output->shape()[0];
int64_t img_height = output->shape()[1];
int64_t input_plane = sd_tensor_plane_size(input);
int64_t output_plane = sd_tensor_plane_size(*output);
int64_t plane_count = input.numel() / input_plane;
GGML_ASSERT(output->numel() / output_plane == plane_count);
// unclamped -> expects x in the range [0-1]
auto smootherstep_f32 = [](const float x) -> float {
GGML_ASSERT(x >= 0.f && x <= 1.f);
return x * x * x * (x * (6.0f * x - 15.0f) + 10.0f);
};
for (int iy = y_skip; iy < height; iy++) {
for (int ix = x_skip; ix < width; ix++) {
int64_t src_xy = ix + width * iy;
int64_t ox = (x + ix) % img_width;
int64_t oy = (y + iy) % img_height;
int64_t dst_xy = ox + img_width * oy;
for (int64_t plane = 0; plane < plane_count; ++plane) {
float new_value = input[plane * input_plane + src_xy];
if (overlap_x > 0 || overlap_y > 0) {
float old_value = (*output)[plane * output_plane + dst_xy];
const float x_f_0 = (circular_x || (overlap_x > 0 && x > 0)) ? (ix - x_skip) / float(overlap_x) : 1.f;
const float x_f_1 = (circular_x || (overlap_x > 0 && x < (img_width - width))) ? (width - ix) / float(overlap_x) : 1.f;
const float y_f_0 = (circular_y || (overlap_y > 0 && y > 0)) ? (iy - y_skip) / float(overlap_y) : 1.f;
const float y_f_1 = (circular_y || (overlap_y > 0 && y < (img_height - height))) ? (height - iy) / float(overlap_y) : 1.f;
const float x_f = std::min(std::min(x_f_0, x_f_1), 1.f);
const float y_f = std::min(std::min(y_f_0, y_f_1), 1.f);
(*output)[plane * output_plane + dst_xy] =
old_value + new_value * smootherstep_f32(y_f) * smootherstep_f32(x_f);
} else {
(*output)[plane * output_plane + dst_xy] = new_value;
}
}
}
}
}
static void sd_tensor_merge_2d_non_circular(const sd::Tensor<float>& input,
sd::Tensor<float>* output,
int x,
int y,
int overlap_left,
int overlap_right,
int overlap_top,
int overlap_bottom) {
int overlap_left,
int overlap_right,
int overlap_top,
int overlap_bottom) {
GGML_ASSERT(output != nullptr);
int64_t in_width = input.shape()[0];
@@ -164,8 +103,8 @@ static void sd_tensor_merge_2d_non_circular(const sd::Tensor<float>& input,
GGML_ASSERT(output->numel() == plane_count * out_size);
GGML_ASSERT(x >= 0 && y >= 0);
GGML_ASSERT(x + in_width <= out_width);
GGML_ASSERT(y + in_height <= out_height);
GGML_ASSERT(x < out_width && in_width <= out_width);
GGML_ASSERT(y < out_height && in_height <= out_height);
GGML_ASSERT(overlap_left >= 0 && overlap_right >= 0);
GGML_ASSERT(overlap_top >= 0 && overlap_bottom >= 0);
@@ -191,7 +130,7 @@ static void sd_tensor_merge_2d_non_circular(const sd::Tensor<float>& input,
x_f = static_cast<float>(in_width - ix) / overlap_right;
}
float x_weight = smootherstep_f32(std::clamp(x_f, 0.0f, 1.0f));
(*output)[plane * out_size + out_width * (y + iy) + (x + ix)] += x_weight * y_weight * input[plane * in_size + in_width * iy + ix];
(*output)[plane * out_size + out_width * ((y + iy) % out_height) + ((x + ix) % out_width)] += x_weight * y_weight * input[plane * in_size + in_width * iy + ix];
}
}
}
@@ -223,13 +162,8 @@ sd::Tensor<float> process_tiles_2d(const sd::Tensor<float>& input,
int scale_in = decode ? 1 : scale;
int scale_out = decode ? scale : 1;
int num_tiles_x;
float tile_overlap_factor_x;
sd_tiling_calc_tiles(num_tiles_x, tile_overlap_factor_x, small_width, p_tile_size_w, tile_overlap_factor, circular_x);
int num_tiles_y;
float tile_overlap_factor_y;
sd_tiling_calc_tiles(num_tiles_y, tile_overlap_factor_y, small_height, p_tile_size_h, tile_overlap_factor, circular_y);
const auto tiles_x = sd_tiling_plan_axis(small_width, p_tile_size_w, tile_overlap_factor, circular_x);
const auto tiles_y = sd_tiling_plan_axis(small_height, p_tile_size_h, tile_overlap_factor, circular_y);
int tile_width = std::min(p_tile_size_w, small_width);
int tile_height = std::min(p_tile_size_h, small_height);
@@ -238,144 +172,37 @@ sd::Tensor<float> process_tiles_2d(const sd::Tensor<float>& input,
int output_tile_width = tile_width * scale_out;
int output_tile_height = tile_height * scale_out;
int num_tiles = num_tiles_x * num_tiles_y;
int tile_count = 1;
float last_time = 0.0f;
const int num_tiles = static_cast<int>(tiles_x.size() * tiles_y.size());
int tile_count = 0;
if (!silent) {
LOG_VERBOSE("num tiles : %d, %d ", num_tiles_x, num_tiles_y);
LOG_VERBOSE("optimal overlap : %f, %f (targeting %f)", tile_overlap_factor_x, tile_overlap_factor_y, tile_overlap_factor);
LOG_VERBOSE("num tiles : %d, %d ", static_cast<int>(tiles_x.size()), static_cast<int>(tiles_y.size()));
LOG_VERBOSE("processing %i tiles", num_tiles);
pretty_progress(0, num_tiles, 0.0f);
}
if (circular_x || circular_y) {
int tile_overlap_x = static_cast<int32_t>(p_tile_size_w * tile_overlap_factor_x);
int non_tile_overlap_x = p_tile_size_w - tile_overlap_x;
int tile_overlap_y = static_cast<int32_t>(p_tile_size_h * tile_overlap_factor_y);
int non_tile_overlap_y = p_tile_size_h - tile_overlap_y;
bool last_y = false;
bool last_x = false;
for (int y = 0; y < small_height && !last_y; y += non_tile_overlap_y) {
int dy = 0;
if (!circular_y && y + tile_height >= small_height) {
int original_y = y;
y = small_height - tile_height;
dy = original_y - y;
if (decode) {
dy *= scale;
}
last_y = true;
for (const auto& y : tiles_y) {
for (const auto& x : tiles_x) {
int64_t t1 = ggml_time_ms();
auto input_tile = sd_tensor_split_2d(input, input_tile_width, input_tile_height, x.offset * scale_in, y.offset * scale_in);
auto output_tile = on_processing(input_tile);
if (output_tile.empty()) {
return {};
}
for (int x = 0; x < small_width && !last_x; x += non_tile_overlap_x) {
int dx = 0;
if (!circular_x && x + tile_width >= small_width) {
int original_x = x;
x = small_width - tile_width;
dx = original_x - x;
if (decode) {
dx *= scale;
}
last_x = true;
}
int x_in = decode ? x : scale * x;
int y_in = decode ? y : scale * y;
int x_out = decode ? x * scale : x;
int y_out = decode ? y * scale : y;
int overlap_x_out = decode ? tile_overlap_x * scale : tile_overlap_x;
int overlap_y_out = decode ? tile_overlap_y * scale : tile_overlap_y;
int64_t t1 = ggml_time_ms();
auto input_tile = sd_tensor_split_2d(input, input_tile_width, input_tile_height, x_in, y_in);
auto output_tile = on_processing(input_tile);
if (output_tile.empty()) {
return {};
}
GGML_ASSERT(output_tile.shape()[0] == output_tile_width && output_tile.shape()[1] == output_tile_height);
if (output.empty()) {
std::vector<int64_t> output_shape = output_tile.shape();
output_shape[0] = output_width;
output_shape[1] = output_height;
output = sd::Tensor<float>::zeros(std::move(output_shape));
}
sd_tensor_merge_2d(output_tile, &output, x_out, y_out, overlap_x_out, overlap_y_out, circular_x, circular_y, dx, dy);
if (!silent) {
int64_t t2 = ggml_time_ms();
last_time = (t2 - t1) / 1000.0f;
pretty_progress(tile_count, num_tiles, last_time);
}
tile_count++;
GGML_ASSERT(output_tile.shape()[0] == output_tile_width && output_tile.shape()[1] == output_tile_height);
if (output.empty()) {
std::vector<int64_t> output_shape = output_tile.shape();
output_shape[0] = output_width;
output_shape[1] = output_height;
output = sd::Tensor<float>::zeros(std::move(output_shape));
}
last_x = false;
}
} else {
for (int j = 0; j < num_tiles_y; ++j) {
int y = 0;
int overlap_top = 0;
int overlap_bottom = 0;
if (num_tiles_y > 1) {
y = j * (small_height - tile_height) / (num_tiles_y - 1);
if (j > 0) {
int y_prev = (j - 1) * (small_height - tile_height) / (num_tiles_y - 1);
overlap_top = y_prev + tile_height - y;
}
if (j < num_tiles_y - 1) {
int y_next = (j + 1) * (small_height - tile_height) / (num_tiles_y - 1);
overlap_bottom = y + tile_height - y_next;
}
}
for (int i = 0; i < num_tiles_x; ++i) {
int x = 0;
int overlap_left = 0;
int overlap_right = 0;
if (num_tiles_x > 1) {
x = i * (small_width - tile_width) / (num_tiles_x - 1);
if (i > 0) {
int x_prev = (i - 1) * (small_width - tile_width) / (num_tiles_x - 1);
overlap_left = x_prev + tile_width - x;
}
if (i < num_tiles_x - 1) {
int x_next = (i + 1) * (small_width - tile_width) / (num_tiles_x - 1);
overlap_right = x + tile_width - x_next;
}
}
int64_t t1 = ggml_time_ms();
auto input_tile = sd_tensor_split_2d(input, input_tile_width, input_tile_height, x * scale_in, y * scale_in);
auto output_tile = on_processing(input_tile);
if (output_tile.empty()) {
return {};
}
GGML_ASSERT(output_tile.shape()[0] == output_tile_width && output_tile.shape()[1] == output_tile_height);
if (output.empty()) {
std::vector<int64_t> output_shape = output_tile.shape();
output_shape[0] = output_width;
output_shape[1] = output_height;
output = sd::Tensor<float>::zeros(std::move(output_shape));
}
sd_tensor_merge_2d_non_circular(
output_tile, &output,
x * scale_out, y * scale_out,
overlap_left * scale_out,
overlap_right * scale_out,
overlap_top * scale_out,
overlap_bottom * scale_out);
if (!silent) {
last_time = (ggml_time_ms() - t1) / 1000.0f;
pretty_progress(tile_count, num_tiles, last_time);
}
tile_count++;
sd_tensor_merge_2d(output_tile, &output,
x.offset * scale_out, y.offset * scale_out,
x.overlap_before * scale_out, x.overlap_after * scale_out,
y.overlap_before * scale_out, y.overlap_after * scale_out);
++tile_count;
if (!silent) {
pretty_progress(tile_count, num_tiles, (ggml_time_ms() - t1) / 1000.0f);
}
}
}
if (!silent && tile_count < num_tiles) {
pretty_progress(num_tiles, num_tiles, last_time);
}
if (output.empty()) {
return {};
}
return output;
}