Commit 73a954a for stable-diffusion.cpp
commit 73a954a85420bc7a1b09a04198a231c72a67e7be
Author: leejet <leejet714@gmail.com>
Date: Sat Dec 5 22:35:19 2026 +0800
refactor(dgl): unify model operation timing logs (#2532)
diff --git a/docs/development/dgl.md b/docs/development/dgl.md
index cb1059b..67e563d 100644
--- a/docs/development/dgl.md
+++ b/docs/development/dgl.md
@@ -63,6 +63,14 @@ step. The resource plan therefore accounts for diffusion weights during
`sample`. Each sampling call owns its prediction history and reports sampling
events through the run's shared progress dispatcher.
+For built-ins registered with duration logging, the executor logs one INFO
+message per successful call. Durations below one second use milliseconds and
+durations of one second or more use seconds. Both use two decimal places, as in
+`vae_encode completed in 842.30 ms` and `sample completed in 6.41 s`.
+The duration covers the operation after argument evaluation, including any
+on-demand weight preparation, preprocessing and postprocessing within the
+call. A cache hit reports the time spent serving that invocation.
+
## What the context retains
A context owns backend settings, memory budgets and the model cache. A
diff --git a/src/conditioning/conditioner.hpp b/src/conditioning/conditioner.hpp
index 3808459..6ec14be 100644
--- a/src/conditioning/conditioner.hpp
+++ b/src/conditioning/conditioner.hpp
@@ -1,7 +1,6 @@
#ifndef __SD_CONDITIONING_CONDITIONER_HPP__
#define __SD_CONDITIONING_CONDITIONER_HPP__
-#include <cinttypes>
#include <cmath>
#include <functional>
#include <iomanip>
@@ -325,7 +324,6 @@ struct FrozenCLIPEmbedderWithCustomWords : public Conditioner {
int height,
bool zero_out_masked = false,
const sd::TextualInversion* inversion = nullptr) {
- int64_t t0 = ggml_time_ms();
sd::Tensor<float> hidden_states; // [n_token, hidden_size] or [n_token, hidden_size + hidden_size2]
sd::Tensor<float> pooled;
@@ -399,9 +397,6 @@ struct FrozenCLIPEmbedderWithCustomWords : public Conditioner {
}
}
}
- int64_t t1 = ggml_time_ms();
- LOG_VERBOSE("computing condition graph completed, taking %" PRId64 " ms", t1 - t0);
-
chunk_hidden_states = sd::apply_token_weights(std::move(chunk_hidden_states), chunk_weights);
if (zero_out_masked) {
@@ -783,7 +778,6 @@ struct SD3CLIPEmbedder : public Conditioner {
auto& t5_weights = token_and_weights[2].second;
size_t chunk_len = 77;
- int64_t t0 = ggml_time_ms();
sd::Tensor<float> hidden_states;
sd::Tensor<float> pooled;
@@ -943,8 +937,6 @@ struct SD3CLIPEmbedder : public Conditioner {
pooled = sd::ops::concat(pooled_l, pooled_g, 0); // [768 + 1280]
}
- int64_t t1 = ggml_time_ms();
- LOG_INFO("computing condition graph completed, taking %" PRId64 " ms", t1 - t0);
if (zero_out_masked) {
chunk_hidden_states.fill_(0.0f);
}
@@ -1180,7 +1172,6 @@ struct FluxCLIPEmbedder : public Conditioner {
auto& t5_tokens = token_and_weights[1].first;
auto& t5_weights = token_and_weights[1].second;
- int64_t t0 = ggml_time_ms();
sd::Tensor<float> hidden_states; // [N, n_token, 4096]
sd::Tensor<float> pooled; // [768,]
@@ -1243,8 +1234,6 @@ struct FluxCLIPEmbedder : public Conditioner {
chunk_hidden_states = sd::Tensor<float>::zeros({4096, static_cast<int64_t>(chunk_len)});
}
- int64_t t1 = ggml_time_ms();
- LOG_INFO("computing condition graph completed, taking %" PRId64 " ms", t1 - t0);
if (!hidden_states.empty()) {
hidden_states = sd::ops::concat(hidden_states, chunk_hidden_states, 1);
} else {
@@ -1453,7 +1442,6 @@ struct T5CLIPEmbedder : public Conditioner {
auto& t5_weights = std::get<1>(token_and_weights);
auto& t5_attn_mask_vec = std::get<2>(token_and_weights);
- int64_t t0 = ggml_time_ms();
sd::Tensor<float> t5_attn_mask = sd::Tensor<float>::from_vector(t5_attn_mask_vec);
sd::Tensor<float> hidden_states;
@@ -1488,9 +1476,6 @@ struct T5CLIPEmbedder : public Conditioner {
chunk_hidden_states.masked_fill_(chunk_mask_tensor < 0.0f, 0.0f);
}
- int64_t t1 = ggml_time_ms();
- LOG_INFO("computing condition graph completed, taking %" PRId64 " ms", t1 - t0);
-
if (!hidden_states.empty()) {
hidden_states = sd::ops::concat(hidden_states, chunk_hidden_states, 1);
} else {
@@ -1793,8 +1778,6 @@ struct AnimaConditioner : public Conditioner {
SDCondition get_learned_condition(int n_threads,
const ConditionerParams& conditioner_params) override {
- int64_t t0 = ggml_time_ms();
-
auto tokenized = tokenize(conditioner_params.text, conditioner_params.raw, conditioner_params.system_prompt);
auto& qwen_tokens = std::get<0>(tokenized);
auto& qwen_weights = std::get<1>(tokenized);
@@ -1819,9 +1802,6 @@ struct AnimaConditioner : public Conditioner {
auto t5_ids_tensor = sd::Tensor<int32_t>::from_vector(t5_tokens);
auto t5_weight_tensor = sd::Tensor<float>::from_vector(t5_weights);
- int64_t t1 = ggml_time_ms();
- LOG_INFO("computing condition graph completed, taking %" PRId64 " ms", t1 - t0);
-
SDCondition result;
result.c_crossattn = std::move(hidden_states);
result.c_t5_ids = std::move(t5_ids_tensor);
@@ -2417,8 +2397,6 @@ struct LTXAVEmbedder : public Conditioner {
SDCondition get_learned_condition(int n_threads,
const ConditionerParams& conditioner_params) override {
- int64_t t0 = ggml_time_ms();
-
std::string prompt = conditioner_params.raw ? std::string{} : sd::prompt_templates::with_system("", conditioner_params.system_prompt, sd::prompt_templates::RoleFormat::Gemma);
std::pair<int, int> prompt_attn_range;
prompt_attn_range.first = static_cast<int>(prompt.size());
@@ -2436,9 +2414,6 @@ struct LTXAVEmbedder : public Conditioner {
if (prepared.first.empty() || prepared.second.empty())
return {};
- int64_t t1 = ggml_time_ms();
- LOG_VERBOSE("computing LTXAV condition graph completed, taking %" PRId64 " ms", t1 - t0);
-
SDCondition result;
result.c_crossattn = std::move(prepared.first);
result.extra_c_crossattns.push_back(std::move(prepared.second));
diff --git a/src/conditioning/llm_conditioner.cpp b/src/conditioning/llm_conditioner.cpp
index 01380a1..286e1df 100644
--- a/src/conditioning/llm_conditioner.cpp
+++ b/src/conditioning/llm_conditioner.cpp
@@ -1,7 +1,6 @@
#include "conditioning/llm_conditioner.h"
#include <algorithm>
-#include <cinttypes>
#include <cmath>
#include <iomanip>
#include <iterator>
@@ -711,8 +710,6 @@ struct LLMEmbedder : public Conditioner {
return result;
};
- int64_t t0 = ggml_time_ms();
-
if (sd_version_is_minimax_h3(version)) {
prompt = system_prefix("");
prompt_template_encode_start_idx = 0;
@@ -1397,9 +1394,6 @@ struct LLMEmbedder : public Conditioner {
hidden_states = sd::ops::concat(bos, tail, 1);
}
- int64_t t1 = ggml_time_ms();
- LOG_INFO("computing condition graph completed, taking %" PRId64 " ms", t1 - t0);
-
SDCondition result;
result.c_crossattn = std::move(hidden_states);
return result;
@@ -1521,8 +1515,6 @@ struct LLMEmbedder : public Conditioner {
extra_hidden_states_vec.push_back(std::move(extra_hidden_states));
}
- int64_t t1 = ggml_time_ms();
- LOG_INFO("computing condition graph completed, taking %" PRId64 " ms", t1 - t0);
SDCondition result;
result.c_crossattn = std::move(hidden_states);
result.extra_c_crossattns = std::move(extra_hidden_states_vec);
diff --git a/src/dgl/bindings/registration.cpp b/src/dgl/bindings/registration.cpp
index f21c4f1..ebfc5f4 100644
--- a/src/dgl/bindings/registration.cpp
+++ b/src/dgl/bindings/registration.cpp
@@ -787,6 +787,10 @@ namespace sd::dgl {
for (const auto* name : {"controlnet::apply", "ip_adapter::apply", "pulid::apply"})
registry.at(name).model_use = Operation::ModelUse::Capture;
registry.at("preview").model_use = Operation::ModelUse::Preview;
+ for (const auto* name : {"audio_vae_decode", "audio_vae_encode", "detections::detect", "encode_audio", "encode_prompt", "encode_vision",
+ "ip_adapter::project", "photomaker::apply", "photomaker::encode_images", "photomaker::encode_prompt",
+ "relayvsr::decode", "sample", "sefi::sample", "upscale", "upscale_latent", "vae_decode", "vae_encode"})
+ registry.at(name).log_duration = true;
}
} // namespace sd::dgl
diff --git a/src/dgl/executor.cpp b/src/dgl/executor.cpp
index 6fb51e3..1a8e869 100644
--- a/src/dgl/executor.cpp
+++ b/src/dgl/executor.cpp
@@ -1,6 +1,7 @@
#include "executor.h"
#include "context.h"
+#include <chrono>
#include <stdexcept>
#include "core/util.h"
@@ -226,8 +227,9 @@ namespace sd::dgl {
if (operation.resource())
failure = SD_STATUS_LOAD_FAILED;
state_.events.check_cancelled();
- Value value = operation.resource() ? (state_.plan ? state_.plan->load_resource(expr.get(), *bound.operation, args) : state_.context.load(*bound.operation, args, true))
- : operation.execute(args, state_);
+ const auto start = operation.log_duration ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{};
+ Value value = operation.resource() ? (state_.plan ? state_.plan->load_resource(expr.get(), *bound.operation, args) : state_.context.load(*bound.operation, args, true))
+ : operation.execute(args, state_);
if (operation.resource()) {
if (state_.plan)
state_.plan->resource_loaded(expr.get(), value);
@@ -239,6 +241,13 @@ namespace sd::dgl {
: args.at(operation.output.same_type_as).type();
if (!accepts(output_type, value.type()))
throw std::runtime_error("built-in function '" + expr->name + "' returned the wrong type");
+ if (operation.log_duration) {
+ const auto elapsed = std::chrono::steady_clock::now() - start;
+ const bool show_seconds = elapsed >= std::chrono::seconds(1);
+ const double duration = show_seconds ? std::chrono::duration<double>(elapsed).count()
+ : std::chrono::duration<double, std::milli>(elapsed).count();
+ LOG_INFO("%s completed in %.2f %s", bound.operation->first.c_str(), duration, show_seconds ? "s" : "ms");
+ }
return value;
} catch (const std::bad_alloc&) {
throw;
diff --git a/src/dgl/models/detail_detector.cpp b/src/dgl/models/detail_detector.cpp
index 6bee331..ab5defa 100644
--- a/src/dgl/models/detail_detector.cpp
+++ b/src/dgl/models/detail_detector.cpp
@@ -3,6 +3,7 @@
#include <algorithm>
#include <cmath>
#include <limits>
+#include <stdexcept>
#include <utility>
#include "core/image_geometry.h"
@@ -346,17 +347,12 @@ namespace sd::dgl::models {
Model::ExecutionScope scope(*this);
scope.require_ready();
LetterboxInput input = make_letterbox_input(image, params.input_size);
- int64_t start = ggml_time_ms();
sd::Tensor<float> raw = detector->compute(n_threads, input.tensor);
detector->runner_end();
- if (raw.empty()) {
- LOG_ERROR("YOLOv8 detector inference failed");
- return {};
- }
+ if (raw.empty())
+ throw std::runtime_error("YOLOv8 detector inference failed");
auto detections = decode_detections(raw, detector->config, input, image, params);
- LOG_INFO("ADetailer detected %zu object(s), taking %.2fs",
- detections.size(),
- (ggml_time_ms() - start) / 1000.f);
+ LOG_INFO("ADetailer detected %zu object(s)", detections.size());
for (size_t i = 0; i < detections.size(); ++i) {
const auto& detection = detections[i];
std::string object = "class_" + std::to_string(detection.class_id);
diff --git a/src/dgl/models/upscaler.cpp b/src/dgl/models/upscaler.cpp
index 169c30d..153a701 100644
--- a/src/dgl/models/upscaler.cpp
+++ b/src/dgl/models/upscaler.cpp
@@ -92,25 +92,18 @@ namespace sd::dgl::models {
sd::Image ImageUpscaler::upscale(sd::Image input_image) {
sd::ParallelScope tensor_scope(tensor_executor.get());
- sd::Image upscaled_image = {0, 0, 0, nullptr};
- const int scale = get_scale();
- int output_width = (int)input_image.width * scale;
- int output_height = (int)input_image.height * scale;
+ const int scale = get_scale();
+ int output_width = (int)input_image.width * scale;
+ int output_height = (int)input_image.height * scale;
LOG_INFO("upscaling from (%i x %i) to (%i x %i)",
input_image.width, input_image.height, output_width, output_height);
- sd::Tensor<float> input_tensor = sd_image_to_tensor(input_image);
- sd::Tensor<float> upscaled;
- int64_t t0 = ggml_time_ms();
- upscaled = upscale_tensor(input_tensor);
+ auto input_tensor = sd_image_to_tensor(input_image);
+ auto upscaled = upscale_tensor(input_tensor);
if (upscaled.empty()) {
- return upscaled_image;
+ return {};
}
- sd::Image upscaled_data = tensor_to_sd_image(upscaled);
- int64_t t3 = ggml_time_ms();
- LOG_INFO("input_image_tensor upscaled, taking %.2fs", (t3 - t0) / 1000.0f);
- upscaled_image = upscaled_data;
- return upscaled_image;
+ return tensor_to_sd_image(upscaled);
}
} // namespace sd::dgl::models
diff --git a/src/dgl/ops/sampling.cpp b/src/dgl/ops/sampling.cpp
index f040539..c2d41dd 100644
--- a/src/dgl/ops/sampling.cpp
+++ b/src/dgl/ops/sampling.cpp
@@ -56,7 +56,6 @@ namespace sd::dgl::ops {
Tensor<float> run_sample(const Tensor<float>& input, const std::vector<float>& sigmas, PredictionExecution& prediction_run, const SamplingSolver& solver, const SamplingOptions& options, RunEvents& events) {
validate_mask(input, options);
- const int64_t sampling_start = ggml_time_ms();
LOG_INFO("sampling started: method=%s, steps=%zu", options.method.c_str(), sigmas.size() - 1);
auto& model = prediction_run.dit();
const auto& parameterization = prediction_run.parameterization();
@@ -96,7 +95,6 @@ namespace sd::dgl::ops {
if (options.finish.value_or(false) && !terminal_zero)
output = parameterization.finish(sigmas.back(), output);
observation.end(output);
- LOG_INFO("sampling completed, taking %.2fs", (ggml_time_ms() - sampling_start) / 1000.0);
return output;
}
diff --git a/src/dgl/registry.h b/src/dgl/registry.h
index 53da434..b0bc17c 100644
--- a/src/dgl/registry.h
+++ b/src/dgl/registry.h
@@ -59,6 +59,7 @@ namespace sd::dgl {
std::function<PreparedResource(Values&)> prepare;
ModelUse model_use = ModelUse::Automatic;
std::optional<SDBackendModule> backend_module;
+ bool log_duration = false;
bool resource() const { return bool(prepare); }
};
diff --git a/src/model/vae/ltx_audio_vae.hpp b/src/model/vae/ltx_audio_vae.hpp
index 631b8c5..b83d333 100644
--- a/src/model/vae/ltx_audio_vae.hpp
+++ b/src/model/vae/ltx_audio_vae.hpp
@@ -1016,7 +1016,6 @@ namespace LTXV {
sd::Tensor<float> decode(int n_threads,
const sd::Tensor<float>& latent_tensor) override {
- int64_t t0 = ggml_time_ms();
auto get_graph = [&]() -> ggml_cgraph* {
auto latent = make_input(latent_tensor);
ggml_tensor* bwe_skip_filter = config.has_bwe ? make_input(bwe_skip_filter_tensor) : nullptr;
@@ -1026,10 +1025,7 @@ namespace LTXV {
ggml_build_forward_expand(gf, waveform);
return gf;
};
- auto result = restore_trailing_singleton_dims(GGMLRunner::compute(get_graph, n_threads, false), 4);
- int64_t t1 = ggml_time_ms();
- LOG_INFO("ltx audio vae decode completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
- return result;
+ return restore_trailing_singleton_dims(GGMLRunner::compute(get_graph, n_threads, false), 4);
}
int output_sample_rate() const override {
diff --git a/src/model/vae/minimax_h3_audio_vae.hpp b/src/model/vae/minimax_h3_audio_vae.hpp
index d83b992..021ea0a 100644
--- a/src/model/vae/minimax_h3_audio_vae.hpp
+++ b/src/model/vae/minimax_h3_audio_vae.hpp
@@ -458,7 +458,6 @@ namespace MiniMaxH3 {
sd::Tensor<float> encode(int n_threads,
const sd::Tensor<float>& waveform) override {
- int64_t t0 = ggml_time_ms();
auto get_graph = [&]() -> ggml_cgraph* {
auto input = make_input(waveform);
auto runner_ctx = get_context();
@@ -467,18 +466,11 @@ namespace MiniMaxH3 {
ggml_build_forward_expand(graph, latent);
return graph;
};
- auto result = restore_trailing_singleton_dims(
- GGMLRunner::compute(get_graph, n_threads, false),
- 4);
- int64_t t1 = ggml_time_ms();
- LOG_INFO("MiniMax-H3 audio VAE encode completed, taking %.2fs",
- (t1 - t0) / 1000.f);
- return result;
+ return restore_trailing_singleton_dims(GGMLRunner::compute(get_graph, n_threads, false), 4);
}
sd::Tensor<float> decode(int n_threads,
const sd::Tensor<float>& latent_tensor) override {
- int64_t t0 = ggml_time_ms();
auto get_graph = [&]() -> ggml_cgraph* {
auto latent = make_input(latent_tensor);
auto runner_ctx = get_context();
@@ -487,13 +479,7 @@ namespace MiniMaxH3 {
ggml_build_forward_expand(graph, waveform);
return graph;
};
- auto result = restore_trailing_singleton_dims(
- GGMLRunner::compute(get_graph, n_threads, false),
- 4);
- int64_t t1 = ggml_time_ms();
- LOG_INFO("MiniMax-H3 audio VAE decode completed, taking %.2fs",
- (t1 - t0) / 1000.f);
- return result;
+ return restore_trailing_singleton_dims(GGMLRunner::compute(get_graph, n_threads, false), 4);
}
};
diff --git a/src/model/vae/vae.hpp b/src/model/vae/vae.hpp
index 6c72d9b..68e88d4 100644
--- a/src/model/vae/vae.hpp
+++ b/src/model/vae/vae.hpp
@@ -247,7 +247,6 @@ public:
bool circular_x,
bool circular_y,
bool auto_end) {
- int64_t t0 = ggml_time_ms();
tiling_params = resolve_tiling_params(tiling_params);
sd::Tensor<float> input = x;
sd::Tensor<float> output;
@@ -294,8 +293,6 @@ public:
LOG_ERROR("vae encode compute failed");
return {};
}
- int64_t t1 = ggml_time_ms();
- LOG_VERBOSE("computing vae encode graph completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
return std::move(output);
}
@@ -317,7 +314,6 @@ public:
bool circular_y,
bool silent,
bool auto_end) {
- int64_t t0 = ggml_time_ms();
tiling_params = resolve_tiling_params(tiling_params);
sd::Tensor<float> input = x;
sd::Tensor<float> output;
@@ -368,8 +364,6 @@ public:
if (scale_input) {
scale_tensor_to_0_1(&output);
}
- int64_t t1 = ggml_time_ms();
- LOG_INFO("computing vae decode graph completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
return std::move(output);
}