mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-11 03:24:30 +00:00
feat(server): report generation stages independently of previews (#2303)
Co-authored-by: Maxime Dréan <maximedrean@proton.me>
This commit is contained in:
1 parent
b43ce4badd
commit
fc350be2ce
12 files changed
+259
-11
No files matched your search
@@ -76,6 +76,16 @@ Recipes declaring `video_frames` also switch the form to Video Generation;
|
||||
other recipes switch to Image Generation. Generation is enabled after the
|
||||
selected recipe is loaded. Changing a selection does not reload the model
|
||||
until you click Load.
|
||||
|
||||
The generation status bar shows the current stage and elapsed time even when
|
||||
Preview Mode is `none`. During sampling it shows completed steps for the current
|
||||
pass; prompt encoding and VAE decoding show an activity indicator when their
|
||||
total work is unknown. Sampling reaching 100% does not mean the job is finished:
|
||||
decoding, ADetailer, and output encoding may still follow.
|
||||
The canvas keeps the submitted output aspect ratio, and stage changes keep the
|
||||
same layout. Editing the next request's resolution does not resize the current
|
||||
preview or result.
|
||||
|
||||
The server can also start with the existing model arguments shown below;
|
||||
existing API clients then work without selecting a recipe. The UI automatically
|
||||
selects **Command-line model (all features)** and uses the startup generation
|
||||
|
||||
@@ -775,6 +775,7 @@ Field types:
|
||||
| `queue_position` | `integer` |
|
||||
| `result` | `object \| null` |
|
||||
| `error` | `object \| null` |
|
||||
| `progress` | `object` |
|
||||
| `preview` | `object \| null` |
|
||||
|
||||
`created`, `started`, and `completed` are integer Unix timestamps in
|
||||
@@ -782,6 +783,57 @@ Field types:
|
||||
the elapsed generation time in seconds, including output encoding and excluding
|
||||
queue time.
|
||||
|
||||
`progress` is available independently of the preview setting, for both image
|
||||
and video jobs. Continue polling until `status` is terminal: a sampling pass
|
||||
can finish before decoding, ADetailer, or output encoding finishes.
|
||||
|
||||
| Field | Type | Notes |
|
||||
| --- | --- | --- |
|
||||
| `progress.stage` | `string` | Current execution stage; see below |
|
||||
| `progress.detail` | `string` | Stage detail, or an empty string |
|
||||
| `progress.pass` | `integer` | Sampling pass number within the job; zero before sampling |
|
||||
| `progress.completed` | `integer \| null` | Completed units in the current stage/pass |
|
||||
| `progress.total` | `integer \| null` | Actual total units, or null when unknown |
|
||||
| `progress.unit` | `string \| null` | `steps` for sampling, `images` for image output encoding; null when uncounted |
|
||||
| `progress.stage_elapsed_ms` | `integer` | Elapsed time in the current stage/detail/pass |
|
||||
| `progress.elapsed_ms` | `integer` | Job execution time, excluding queue time; zero before execution |
|
||||
|
||||
Stages include `queued`, `waiting` (waiting for the execution lock), `preparing`,
|
||||
`conditioning`, `sampling`, `vae_encode`, `vae_decode`, `audio_decode`,
|
||||
`upscaling`, `adetailer`, and `encoding`. Stages may repeat or be skipped.
|
||||
Details include `positive_prompt` / `negative_prompt` / `image_condition` for
|
||||
conditioning, `high_noise` / `low_noise` for two-stage sampling, and `image` /
|
||||
`video` for output encoding. During ADetailer, `detail` identifies the inner
|
||||
stage, such as `conditioning` or `sampling`. Clients should tolerate unknown
|
||||
stage, detail, and unit values.
|
||||
|
||||
Sampling counts completed logical steps using the actual schedule, including
|
||||
strength adjustments. Each new sampling pass increments `pass` and starts at
|
||||
zero, including batch items, highres passes, high/low-noise passes, and ADetailer
|
||||
passes. The ratio is **current-pass progress**, not overall job completion.
|
||||
The progress snapshot is published atomically. Preview metadata still describes
|
||||
the preview frame, which may be older than the current progress.
|
||||
|
||||
Unknown totals are represented by null counts, not an estimated percentage.
|
||||
For example, prompt encoding can report:
|
||||
|
||||
```json
|
||||
"progress": {
|
||||
"stage": "conditioning",
|
||||
"detail": "positive_prompt",
|
||||
"pass": 0,
|
||||
"completed": null,
|
||||
"total": null,
|
||||
"unit": null,
|
||||
"stage_elapsed_ms": 3200,
|
||||
"elapsed_ms": 4100
|
||||
}
|
||||
```
|
||||
|
||||
Elapsed times freeze when a job completes, fails, or is cancelled; the last
|
||||
stage is retained for inspection. Use `status` to determine the final outcome.
|
||||
Elapsed time alone does not indicate that computation is advancing.
|
||||
|
||||
`preview` sub-fields:
|
||||
|
||||
| Field | Type | Notes |
|
||||
|
||||
@@ -92,6 +92,63 @@ static void set_preview_callback(AsyncGenerationJob& job) {
|
||||
sd_set_preview_callback(preview_callback, mode, interval, true, false, &job);
|
||||
}
|
||||
|
||||
static void update_job_progress(AsyncGenerationJob& job, const char* stage, const char* detail = "", int sample_pass = -1, int64_t completed = 0, int64_t total = 0, const char* unit = "") {
|
||||
std::lock_guard<std::mutex> lock(job.progress_mutex);
|
||||
auto& progress = job.progress;
|
||||
if (sample_pass < 0) {
|
||||
sample_pass = progress.sample_pass;
|
||||
}
|
||||
if (progress.stage != stage || progress.detail != detail || progress.sample_pass != sample_pass) {
|
||||
progress.stage_started = std::chrono::steady_clock::now();
|
||||
}
|
||||
progress.stage = stage;
|
||||
progress.detail = detail;
|
||||
progress.sample_pass = sample_pass;
|
||||
progress.completed = completed;
|
||||
progress.total = total;
|
||||
progress.unit = unit;
|
||||
}
|
||||
|
||||
static void finish_job_progress(AsyncGenerationJob& job) {
|
||||
std::lock_guard<std::mutex> lock(job.progress_mutex);
|
||||
job.progress.finished = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
class JobCallbacksGuard {
|
||||
sd_ctx_t* ctx_;
|
||||
AsyncGenerationJob& job_;
|
||||
bool detailing_ = false;
|
||||
|
||||
static void progress_callback(const sd_generation_progress_t* progress, void* data) {
|
||||
auto& guard = *static_cast<JobCallbacksGuard*>(data);
|
||||
update_job_progress(guard.job_, guard.detailing_ ? "adetailer" : progress->stage,
|
||||
guard.detailing_ ? progress->stage : progress->detail,
|
||||
progress->sample_pass, progress->completed, progress->total, progress->unit);
|
||||
}
|
||||
|
||||
public:
|
||||
JobCallbacksGuard(sd_ctx_t* ctx, AsyncGenerationJob& job)
|
||||
: ctx_(ctx), job_(job) {
|
||||
clear_preview_callback();
|
||||
set_preview_callback(job);
|
||||
sd_set_generation_progress_callback(ctx_, progress_callback, this);
|
||||
}
|
||||
|
||||
~JobCallbacksGuard() {
|
||||
sd_set_generation_progress_callback(ctx_, nullptr, nullptr);
|
||||
clear_preview_callback();
|
||||
}
|
||||
|
||||
void begin_detailing() {
|
||||
clear_preview_callback();
|
||||
detailing_ = true;
|
||||
update_job_progress(job_, "adetailer");
|
||||
}
|
||||
|
||||
JobCallbacksGuard(const JobCallbacksGuard&) = delete;
|
||||
JobCallbacksGuard& operator=(const JobCallbacksGuard&) = delete;
|
||||
};
|
||||
|
||||
const char* async_job_kind_name(AsyncJobKind kind) {
|
||||
switch (kind) {
|
||||
case AsyncJobKind::ImgGen:
|
||||
@@ -186,6 +243,7 @@ bool cancel_queued_job(AsyncJobManager& manager, AsyncGenerationJob& job) {
|
||||
job.result_fps = 0;
|
||||
job.error_code = "cancelled";
|
||||
job.error_message = "job cancelled by client";
|
||||
finish_job_progress(job);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -200,6 +258,24 @@ json make_async_job_json(const AsyncJobManager& manager, const AsyncGenerationJo
|
||||
result["completed"] = job.completed_at == 0 ? json(nullptr) : json(job.completed_at);
|
||||
result["queue_position"] = 0;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(job.progress_mutex);
|
||||
const auto& progress = job.progress;
|
||||
const auto end = progress.finished == std::chrono::steady_clock::time_point{}
|
||||
? std::chrono::steady_clock::now()
|
||||
: progress.finished;
|
||||
result["progress"] = {
|
||||
{"stage", progress.stage},
|
||||
{"detail", progress.detail},
|
||||
{"pass", progress.sample_pass},
|
||||
{"completed", progress.total > 0 ? json(progress.completed) : json(nullptr)},
|
||||
{"total", progress.total > 0 ? json(progress.total) : json(nullptr)},
|
||||
{"unit", progress.unit.empty() ? json(nullptr) : json(progress.unit)},
|
||||
{"stage_elapsed_ms", std::chrono::duration_cast<std::chrono::milliseconds>(end - progress.stage_started).count()},
|
||||
{"elapsed_ms", job.started_at == 0 ? 0 : std::max<int64_t>(0, (job.completed_at == 0 ? unix_timestamp_now_ms() : job.completed_at) - job.started_at)},
|
||||
};
|
||||
}
|
||||
|
||||
if (job.status == AsyncJobStatus::Queued) {
|
||||
size_t position = 1;
|
||||
for (const auto& queued_id : manager.queue) {
|
||||
@@ -278,17 +354,19 @@ bool execute_img_gen_job(ServerRuntime& runtime,
|
||||
return false;
|
||||
}
|
||||
JobCancellationMonitor cancellation(runtime.sd_ctx, job);
|
||||
set_preview_callback(job);
|
||||
JobCallbacksGuard callbacks(runtime.sd_ctx, job);
|
||||
sd_image_generation_result_t* generated = nullptr;
|
||||
generate_image(runtime.sd_ctx, ¶ms, &generated);
|
||||
results.adopt(generated, runtime.ctx_params->lora_model_dir);
|
||||
clear_preview_callback();
|
||||
if (job.cancel_requested.load()) {
|
||||
return false;
|
||||
}
|
||||
if (!results.empty() && !apply_server_adetailer(runtime, job.img_gen, params, results)) {
|
||||
error_message = "ADetailer failed";
|
||||
return false;
|
||||
if (!results.empty() && job.img_gen.adetailer_enabled && !runtime.default_gen_params->ad_model_path.empty()) {
|
||||
callbacks.begin_detailing();
|
||||
if (!apply_server_adetailer(runtime, job.img_gen, params, results)) {
|
||||
error_message = "ADetailer failed";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,6 +383,7 @@ bool execute_img_gen_job(ServerRuntime& runtime,
|
||||
encoded_format = EncodedImageFormat::WEBP;
|
||||
}
|
||||
|
||||
update_job_progress(job, "encoding", "image", -1, 0, num_results, "images");
|
||||
for (int i = 0; i < num_results; ++i) {
|
||||
if (results[i].data == nullptr) {
|
||||
continue;
|
||||
@@ -325,6 +404,7 @@ bool execute_img_gen_job(ServerRuntime& runtime,
|
||||
}
|
||||
output_images.push_back(base64_encode(image_bytes));
|
||||
output_info.push_back(json::parse(results.generation_info(i)));
|
||||
update_job_progress(job, "encoding", "image", -1, i + 1, num_results, "images");
|
||||
}
|
||||
|
||||
if (output_images.empty()) {
|
||||
@@ -356,7 +436,7 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
|
||||
return false;
|
||||
}
|
||||
JobCancellationMonitor cancellation(runtime.sd_ctx, job);
|
||||
set_preview_callback(job);
|
||||
JobCallbacksGuard callbacks(runtime.sd_ctx, job);
|
||||
sd_video_generation_result_t* generated = nullptr;
|
||||
if (generate_video(runtime.sd_ctx, ¶ms, &generated)) {
|
||||
output_fps = generated->fps;
|
||||
@@ -367,7 +447,6 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
|
||||
if (job.vid_gen.gen_params.embed_image_metadata)
|
||||
str_params = results.generation_info(0);
|
||||
}
|
||||
clear_preview_callback();
|
||||
}
|
||||
|
||||
num_results = results.count();
|
||||
@@ -377,6 +456,7 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
|
||||
return false;
|
||||
}
|
||||
|
||||
update_job_progress(job, "encoding", "video");
|
||||
std::vector<uint8_t> video_bytes = create_video_from_sd_images_to_vector(job.vid_gen.output_format,
|
||||
results.data(),
|
||||
num_results,
|
||||
@@ -425,6 +505,7 @@ void async_job_worker(ServerRuntime& runtime) {
|
||||
job = it->second;
|
||||
job->status = AsyncJobStatus::Generating;
|
||||
job->started_at = unix_timestamp_now_ms();
|
||||
update_job_progress(*job, "waiting");
|
||||
}
|
||||
|
||||
std::vector<std::string> output_images;
|
||||
@@ -459,6 +540,7 @@ void async_job_worker(ServerRuntime& runtime) {
|
||||
}
|
||||
|
||||
job->completed_at = unix_timestamp_now_ms();
|
||||
finish_job_progress(*job);
|
||||
if (job->cancel_requested.load()) {
|
||||
job->status = AsyncJobStatus::Cancelled;
|
||||
job->error_code = "cancelled";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
@@ -29,6 +30,17 @@ enum class AsyncJobStatus {
|
||||
const char* async_job_kind_name(AsyncJobKind kind);
|
||||
const char* async_job_status_name(AsyncJobStatus status);
|
||||
|
||||
struct AsyncJobProgress {
|
||||
std::string stage = "queued";
|
||||
std::string detail;
|
||||
std::string unit;
|
||||
int sample_pass = 0;
|
||||
int64_t completed = 0;
|
||||
int64_t total = 0;
|
||||
std::chrono::steady_clock::time_point stage_started = std::chrono::steady_clock::now();
|
||||
std::chrono::steady_clock::time_point finished{};
|
||||
};
|
||||
|
||||
struct AsyncGenerationJob {
|
||||
std::string id;
|
||||
AsyncJobKind kind = AsyncJobKind::ImgGen;
|
||||
@@ -47,6 +59,8 @@ struct AsyncGenerationJob {
|
||||
int result_fps = 0;
|
||||
std::string error_code;
|
||||
std::string error_message;
|
||||
mutable std::mutex progress_mutex;
|
||||
AsyncJobProgress progress;
|
||||
mutable std::mutex preview_mutex;
|
||||
std::string preview_b64;
|
||||
int preview_pass = 0;
|
||||
|
||||
+1
-1
Submodule examples/server/frontend updated: fa93429c47...d3d83992ff.
Reference in new issue
Block a user