From cfd8906b29aa770ddd54719ad1fd3d4dfe1f6ecd Mon Sep 17 00:00:00 2001 From: leejet Date: Sat, 5 Dec 2026 20:35:57 +0800 Subject: [PATCH 1/3] refactor(dgl): unify model operation timing logs --- docs/development/dgl.md | 8 ++++++++ src/conditioning/conditioner.hpp | 25 ------------------------- src/conditioning/llm_conditioner.cpp | 8 -------- src/dgl/bindings/registration.cpp | 4 ++++ src/dgl/executor.cpp | 9 +++++++++ src/dgl/models/detail_detector.cpp | 5 +---- src/dgl/models/upscaler.cpp | 21 +++++++-------------- src/dgl/ops/sampling.cpp | 2 -- src/dgl/registry.h | 1 + src/model/vae/ltx_audio_vae.hpp | 6 +----- src/model/vae/minimax_h3_audio_vae.hpp | 18 ++---------------- src/model/vae/vae.hpp | 6 ------ 12 files changed, 33 insertions(+), 80 deletions(-) diff --git a/docs/development/dgl.md b/docs/development/dgl.md index cb1059b02f..67e563d9e7 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 38084593b9..6ec14be85f 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 #include #include #include @@ -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 hidden_states; // [n_token, hidden_size] or [n_token, hidden_size + hidden_size2] sd::Tensor 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 hidden_states; sd::Tensor 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 hidden_states; // [N, n_token, 4096] sd::Tensor pooled; // [768,] @@ -1243,8 +1234,6 @@ struct FluxCLIPEmbedder : public Conditioner { chunk_hidden_states = sd::Tensor::zeros({4096, static_cast(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 t5_attn_mask = sd::Tensor::from_vector(t5_attn_mask_vec); sd::Tensor 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::from_vector(t5_tokens); auto t5_weight_tensor = sd::Tensor::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 prompt_attn_range; prompt_attn_range.first = static_cast(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 01380a1d7e..286e1df5f3 100644 --- a/src/conditioning/llm_conditioner.cpp +++ b/src/conditioning/llm_conditioner.cpp @@ -1,7 +1,6 @@ #include "conditioning/llm_conditioner.h" #include -#include #include #include #include @@ -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 f21c4f18fb..ebfc5f41b7 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 6fb51e325b..f51640f9d7 100644 --- a/src/dgl/executor.cpp +++ b/src/dgl/executor.cpp @@ -1,6 +1,7 @@ #include "executor.h" #include "context.h" +#include #include #include "core/util.h" @@ -226,6 +227,7 @@ namespace sd::dgl { if (operation.resource()) failure = SD_STATUS_LOAD_FAILED; state_.events.check_cancelled(); + 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()) { @@ -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(elapsed).count() + : std::chrono::duration(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 6bee3312ab..6a0508055f 100644 --- a/src/dgl/models/detail_detector.cpp +++ b/src/dgl/models/detail_detector.cpp @@ -346,7 +346,6 @@ 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 raw = detector->compute(n_threads, input.tensor); detector->runner_end(); if (raw.empty()) { @@ -354,9 +353,7 @@ namespace sd::dgl::models { return {}; } 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 169c30dd48..153a701bf5 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 input_tensor = sd_image_to_tensor(input_image); - sd::Tensor 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 f04053922a..c2d41dd043 100644 --- a/src/dgl/ops/sampling.cpp +++ b/src/dgl/ops/sampling.cpp @@ -56,7 +56,6 @@ namespace sd::dgl::ops { Tensor run_sample(const Tensor& input, const std::vector& 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 53da434215..b0bc17c035 100644 --- a/src/dgl/registry.h +++ b/src/dgl/registry.h @@ -59,6 +59,7 @@ namespace sd::dgl { std::function prepare; ModelUse model_use = ModelUse::Automatic; std::optional 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 631b8c5a4f..b83d3333ac 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 decode(int n_threads, const sd::Tensor& 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 d83b992e86..021ea0a88d 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 encode(int n_threads, const sd::Tensor& 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 decode(int n_threads, const sd::Tensor& 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 6c72d9be55..68e88d4db5 100644 --- a/src/model/vae/vae.hpp +++ b/src/model/vae/vae.hpp @@ -247,7 +247,6 @@ struct VAE : public GGMLRunner { bool circular_x, bool circular_y, bool auto_end) { - int64_t t0 = ggml_time_ms(); tiling_params = resolve_tiling_params(tiling_params); sd::Tensor input = x; sd::Tensor output; @@ -294,8 +293,6 @@ struct VAE : public GGMLRunner { 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 @@ struct VAE : public GGMLRunner { bool circular_y, bool silent, bool auto_end) { - int64_t t0 = ggml_time_ms(); tiling_params = resolve_tiling_params(tiling_params); sd::Tensor input = x; sd::Tensor output; @@ -368,8 +364,6 @@ struct VAE : public GGMLRunner { 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); } From 5db60e0bc988936f9770842084106bf0b9622ec1 Mon Sep 17 00:00:00 2001 From: leejet Date: Sat, 5 Dec 2026 20:37:21 +0800 Subject: [PATCH 2/3] format code --- src/dgl/executor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dgl/executor.cpp b/src/dgl/executor.cpp index f51640f9d7..1a8e869173 100644 --- a/src/dgl/executor.cpp +++ b/src/dgl/executor.cpp @@ -228,8 +228,8 @@ namespace sd::dgl { failure = SD_STATUS_LOAD_FAILED; state_.events.check_cancelled(); 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_); + 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); From 15400c62c518bdf6d3aa4949d05b6dccb1b9c30b Mon Sep 17 00:00:00 2001 From: leejet Date: Sat, 5 Dec 2026 22:18:15 +0800 Subject: [PATCH 3/3] fix --- src/dgl/models/detail_detector.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/dgl/models/detail_detector.cpp b/src/dgl/models/detail_detector.cpp index 6a0508055f..ab5defa7a7 100644 --- a/src/dgl/models/detail_detector.cpp +++ b/src/dgl/models/detail_detector.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "core/image_geometry.h" @@ -348,10 +349,8 @@ namespace sd::dgl::models { LetterboxInput input = make_letterbox_input(image, params.input_size); sd::Tensor 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)", detections.size()); for (size_t i = 0; i < detections.size(); ++i) {