mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-09-25 04:32:29 +00:00
fix: reuse graph cut plans across CFG passes (#1943)
This commit is contained in:
parent
80bac2d5fc
commit
d8fb10c029
@ -537,10 +537,12 @@ namespace sd::ggml_graph_cut {
|
|||||||
return signature;
|
return signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool plan_matches_graph(ggml_cgraph* gf, const Plan& plan) {
|
static bool plan_matches_graph(ggml_cgraph* gf,
|
||||||
|
const Plan& plan,
|
||||||
|
const std::vector<uint64_t>& layout) {
|
||||||
GGML_ASSERT(gf != nullptr);
|
GGML_ASSERT(gf != nullptr);
|
||||||
if (plan.leaf_names.size() != static_cast<size_t>(gf->n_leafs) ||
|
if (plan.leaf_names.size() != static_cast<size_t>(gf->n_leafs) ||
|
||||||
plan.layout != graph_layout(gf, false)) {
|
plan.layout != layout) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < gf->n_leafs; ++i) {
|
for (int i = 0; i < gf->n_leafs; ++i) {
|
||||||
@ -558,6 +560,11 @@ namespace sd::ggml_graph_cut {
|
|||||||
return cut_markers == plan.cut_markers;
|
return cut_markers == plan.cut_markers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool plan_matches_graph(ggml_cgraph* gf, const Plan& plan) {
|
||||||
|
GGML_ASSERT(gf != nullptr);
|
||||||
|
return plan_matches_graph(gf, plan, graph_layout(gf, false));
|
||||||
|
}
|
||||||
|
|
||||||
ggml_tensor* output_tensor(ggml_cgraph* gf, const Segment& segment, size_t output_index) {
|
ggml_tensor* output_tensor(ggml_cgraph* gf, const Segment& segment, size_t output_index) {
|
||||||
GGML_ASSERT(gf != nullptr);
|
GGML_ASSERT(gf != nullptr);
|
||||||
if (output_index >= segment.output_node_indices.size()) {
|
if (output_index >= segment.output_node_indices.size()) {
|
||||||
@ -938,20 +945,26 @@ namespace sd::ggml_graph_cut {
|
|||||||
GGML_ASSERT(gf != nullptr);
|
GGML_ASSERT(gf != nullptr);
|
||||||
GGML_ASSERT(cache != nullptr);
|
GGML_ASSERT(cache != nullptr);
|
||||||
|
|
||||||
if (cache->graph_cut_plan.available &&
|
const auto layout = graph_layout(gf, false);
|
||||||
plan_matches_graph(gf, cache->graph_cut_plan)) {
|
auto& plans = cache->graph_cut_plans;
|
||||||
return cache->graph_cut_plan;
|
for (auto it = plans.begin(); it != plans.end(); ++it) {
|
||||||
|
if (it->available && plan_matches_graph(gf, *it, layout)) {
|
||||||
|
plans.splice(plans.begin(), plans, it);
|
||||||
|
return plans.front();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t t_plan_begin = ggml_time_ms();
|
int64_t t_plan_begin = ggml_time_ms();
|
||||||
Plan plan = build_plan(backend, gf, params_tensor_set, log_desc);
|
plans.push_front(build_plan(backend, gf, params_tensor_set, log_desc));
|
||||||
cache->graph_cut_plan = plan;
|
if (plans.size() > PlanCache::MAX_PLANS) {
|
||||||
|
plans.pop_back();
|
||||||
|
}
|
||||||
if (log_desc != nullptr) {
|
if (log_desc != nullptr) {
|
||||||
LOG_INFO("%s build cached graph cut plan done (taking %lld ms)",
|
LOG_INFO("%s build cached graph cut plan done (taking %lld ms)",
|
||||||
log_desc,
|
log_desc,
|
||||||
ggml_time_ms() - t_plan_begin);
|
ggml_time_ms() - t_plan_begin);
|
||||||
}
|
}
|
||||||
return plan;
|
return plans.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sd::ggml_graph_cut
|
} // namespace sd::ggml_graph_cut
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
@ -48,7 +49,8 @@ namespace sd::ggml_graph_cut {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct PlanCache {
|
struct PlanCache {
|
||||||
Plan graph_cut_plan;
|
static constexpr size_t MAX_PLANS = 4;
|
||||||
|
std::list<Plan> graph_cut_plans;
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr const char* GGML_RUNNER_CUT_PREFIX = "ggml_runner_cut:";
|
static constexpr const char* GGML_RUNNER_CUT_PREFIX = "ggml_runner_cut:";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user