Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/development/dgl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 0 additions & 25 deletions src/conditioning/conditioner.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef __SD_CONDITIONING_CONDITIONER_HPP__
#define __SD_CONDITIONING_CONDITIONER_HPP__

#include <cinttypes>
#include <cmath>
#include <functional>
#include <iomanip>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,]

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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));
Expand Down
8 changes: 0 additions & 8 deletions src/conditioning/llm_conditioner.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "conditioning/llm_conditioner.h"

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <iomanip>
#include <iterator>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/dgl/bindings/registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 11 additions & 2 deletions src/dgl/executor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "executor.h"
#include "context.h"

#include <chrono>
#include <stdexcept>

#include "core/util.h"
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Comment thread
leejet marked this conversation as resolved.
Resolved
}
return value;
} catch (const std::bad_alloc&) {
throw;
Expand Down
12 changes: 4 additions & 8 deletions src/dgl/models/detail_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <cmath>
#include <limits>
#include <stdexcept>
#include <utility>

#include "core/image_geometry.h"
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 7 additions & 14 deletions src/dgl/models/upscaler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/dgl/ops/sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/dgl/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
};
Expand Down
6 changes: 1 addition & 5 deletions src/model/vae/ltx_audio_vae.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
18 changes: 2 additions & 16 deletions src/model/vae/minimax_h3_audio_vae.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
};

Expand Down
6 changes: 0 additions & 6 deletions src/model/vae/vae.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> input = x;
sd::Tensor<float> output;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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<float> input = x;
sd::Tensor<float> output;
Expand Down Expand Up @@ -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);
}

Expand Down
Loading