From bcc46dc6ec7c89a9f61813c14cf6fcfb40b0acc6 Mon Sep 17 00:00:00 2001 From: leejet Date: Tue, 1 Dec 2026 23:11:02 +0800 Subject: [PATCH 1/3] perf(hunyuan_vae): rely on batched CUDA concat --- src/model/vae/hunyuan_vae.hpp | 92 +++++++++++++---------------------- 1 file changed, 33 insertions(+), 59 deletions(-) diff --git a/src/model/vae/hunyuan_vae.hpp b/src/model/vae/hunyuan_vae.hpp index 5953772bc..23428ce5b 100644 --- a/src/model/vae/hunyuan_vae.hpp +++ b/src/model/vae/hunyuan_vae.hpp @@ -61,17 +61,6 @@ namespace Hunyuan { return ggml_reshape_4d(ctx->ggml_ctx, x, width, height, frames, channels * repeats); } - // Fold space so a temporal concat launches once across all channels on CUDA. - static ggml_tensor* flatten_video_frames(GGMLRunnerContext* ctx, ggml_tensor* x) { - return ggml_reshape_3d(ctx->ggml_ctx, ggml_ext_cont(ctx, x), x->ne[0] * x->ne[1], x->ne[2], x->ne[3]); - } - - static ggml_tensor* concat_video_frames(GGMLRunnerContext* ctx, ggml_tensor* a, ggml_tensor* b) { - GGML_ASSERT(a->ne[0] == b->ne[0] && a->ne[1] == b->ne[1]); - auto joined = ggml_concat(ctx->ggml_ctx, flatten_video_frames(ctx, a), flatten_video_frames(ctx, b), 1); - return ggml_reshape_4d(ctx->ggml_ctx, joined, a->ne[0], a->ne[1], joined->ne[1], a->ne[3]); - } - class CausalConv3d : public GGMLBlock { protected: std::tuple kernel_size; @@ -100,57 +89,42 @@ namespace Hunyuan { int pad_w = std::get<2>(kernel_size) / 2; int pad_h = std::get<1>(kernel_size) / 2; int pad_t = std::get<0>(kernel_size) - 1; - auto padded = ggml_ext_cont(ctx, x); + std::vector temporal_frames; + temporal_frames.reserve(x->ne[2] + pad_t); if (pad_t > 0) { - auto previous = carry != nullptr && carry->is_continuation() ? carry->take() : nullptr; - if (previous != nullptr) { + if (carry != nullptr && carry->is_continuation()) { + auto previous = carry->take(); GGML_ASSERT(previous->ne[2] <= pad_t); - GGML_ASSERT(previous->ne[0] == x->ne[0] && previous->ne[1] == x->ne[1]); - } - const int64_t missing = pad_t - (previous != nullptr ? previous->ne[2] : 0); - - auto input = flatten_video_frames(ctx, padded); - auto prev_flat = previous != nullptr ? flatten_video_frames(ctx, previous) : nullptr; - auto prefix = prev_flat; - if (missing > 0) { - auto first = ggml_ext_slice(ctx, input, 1, 0, 1, false); - if (missing > 1) { - first = ggml_repeat_4d(ctx->ggml_ctx, first, first->ne[0], missing, first->ne[2], 1); + for (int64_t frame = 0; frame < previous->ne[2]; frame++) { + temporal_frames.push_back(ggml_ext_slice(ctx, previous, 2, frame, frame + 1)); } - prefix = prefix != nullptr ? ggml_concat(ctx->ggml_ctx, prefix, first, 1) : first; - } - padded = ggml_concat(ctx->ggml_ctx, prefix, input, 1); - padded = ggml_reshape_4d(ctx->ggml_ctx, padded, x->ne[0], x->ne[1], padded->ne[1], x->ne[3]); - - if (carry != nullptr && carry->output != nullptr) { - if (x->ne[2] < pad_t) { - // Build the carry from previous/input directly so the temporary prefix is freed before the next chunk. - const int64_t prev_frames = previous != nullptr ? previous->ne[2] : 0; - const int64_t keep_prev = std::max(0, prev_frames - x->ne[2]); - const int64_t keep_first = pad_t - x->ne[2] - keep_prev; - ggml_tensor* tail = input; - if (keep_first > 0) { - if (x->ne[2] == 1 && keep_prev == 0) { - tail = ggml_repeat_4d(ctx->ggml_ctx, input, input->ne[0], pad_t, input->ne[2], 1); - } else { - auto first = ggml_ext_slice(ctx, input, 1, 0, 1, false); - if (keep_first > 1) { - first = ggml_repeat_4d(ctx->ggml_ctx, first, first->ne[0], keep_first, first->ne[2], 1); - } - tail = ggml_concat(ctx->ggml_ctx, first, tail, 1); - } - } - if (keep_prev > 0) { - auto prev_tail = ggml_ext_slice(ctx, prev_flat, 1, prev_frames - keep_prev, prev_frames, false); - tail = ggml_concat(ctx->ggml_ctx, prev_tail, tail, 1); - } - carry->push(ggml_reshape_4d(ctx->ggml_ctx, tail, x->ne[0], x->ne[1], pad_t, x->ne[3])); - } else { - auto tail = ggml_ext_slice(ctx, x, 2, x->ne[2] - pad_t, x->ne[2], false); - carry->push(ggml_cont(ctx->ggml_ctx, tail)); + for (int64_t frame = previous->ne[2]; frame < pad_t; frame++) { + temporal_frames.push_back(ggml_ext_slice(ctx, x, 2, 0, 1)); + } + } else { + auto first = ggml_ext_slice(ctx, x, 2, 0, 1); + for (int frame = 0; frame < pad_t; frame++) { + temporal_frames.push_back(first); } } } + for (int64_t frame = 0; frame < x->ne[2]; frame++) { + temporal_frames.push_back(ggml_ext_slice(ctx, x, 2, frame, frame + 1)); + } + + if (pad_t > 0 && carry != nullptr && carry->output != nullptr) { + ggml_tensor* next = nullptr; + for (int frame = pad_t; frame > 0; frame--) { + auto item = temporal_frames[temporal_frames.size() - frame]; + next = next == nullptr ? item : ggml_concat(ctx->ggml_ctx, next, item, 2); + } + carry->push(ggml_cont(ctx->ggml_ctx, next)); + } + + ggml_tensor* padded = nullptr; + for (auto frame : temporal_frames) { + padded = padded == nullptr ? frame : ggml_concat(ctx->ggml_ctx, padded, frame, 2); + } auto replicate_pad = [&](ggml_tensor* input, int dim, int left, int right) { if (left > 0) { auto first = ggml_ext_slice(ctx, input, dim, 0, 1); @@ -489,8 +463,8 @@ namespace Hunyuan { x = mean_channel_groups(ctx, x, group_size); if (factor_t == 2) { - h = concat_video_frames(ctx, h_first, h); - x = concat_video_frames(ctx, x_first, x); + h = ggml_concat(ctx->ggml_ctx, h_first, h, 2); + x = ggml_concat(ctx->ggml_ctx, x_first, x, 2); } return ggml_add(ctx->ggml_ctx, h, x); } @@ -758,7 +732,7 @@ namespace Hunyuan { chunk = conv_out->forward(ctx, chunk, &carry); carry.finish(); - output = output == nullptr ? chunk : concat_video_frames(ctx, output, chunk); + output = output == nullptr ? chunk : ggml_concat(ctx->ggml_ctx, output, chunk, 2); carry_input = std::move(carry_output); } return output; From 6eb42be37c26b9aa5700a4a340c69cedf46d536f Mon Sep 17 00:00:00 2001 From: leejet Date: Tue, 1 Dec 2026 23:12:52 +0800 Subject: [PATCH 2/3] update ggml --- ggml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ggml b/ggml index e9e6fe2c4..a30080c04 160000 --- a/ggml +++ b/ggml @@ -1 +1 @@ -Subproject commit e9e6fe2c4c460eb7e6293c512acd6df777b4a7ac +Subproject commit a30080c041b263044264b4dac3d043b3cdf6653b From 08834712fed165d96406beb4d4033dccebb46207 Mon Sep 17 00:00:00 2001 From: leejet Date: Tue, 1 Dec 2026 23:57:55 +0800 Subject: [PATCH 3/3] avoid quadratic copies in temporal padding --- src/model/vae/hunyuan_vae.hpp | 69 +++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/src/model/vae/hunyuan_vae.hpp b/src/model/vae/hunyuan_vae.hpp index 23428ce5b..aa8e51647 100644 --- a/src/model/vae/hunyuan_vae.hpp +++ b/src/model/vae/hunyuan_vae.hpp @@ -89,42 +89,49 @@ namespace Hunyuan { int pad_w = std::get<2>(kernel_size) / 2; int pad_h = std::get<1>(kernel_size) / 2; int pad_t = std::get<0>(kernel_size) - 1; - std::vector temporal_frames; - temporal_frames.reserve(x->ne[2] + pad_t); + auto padded = ggml_ext_cont(ctx, x); if (pad_t > 0) { - if (carry != nullptr && carry->is_continuation()) { - auto previous = carry->take(); - GGML_ASSERT(previous->ne[2] <= pad_t); - for (int64_t frame = 0; frame < previous->ne[2]; frame++) { - temporal_frames.push_back(ggml_ext_slice(ctx, previous, 2, frame, frame + 1)); - } - for (int64_t frame = previous->ne[2]; frame < pad_t; frame++) { - temporal_frames.push_back(ggml_ext_slice(ctx, x, 2, 0, 1)); - } - } else { - auto first = ggml_ext_slice(ctx, x, 2, 0, 1); - for (int frame = 0; frame < pad_t; frame++) { - temporal_frames.push_back(first); - } - } - } - for (int64_t frame = 0; frame < x->ne[2]; frame++) { - temporal_frames.push_back(ggml_ext_slice(ctx, x, 2, frame, frame + 1)); - } + auto input = padded; + auto previous = carry != nullptr && carry->is_continuation() ? carry->take() : nullptr; + const int64_t frames = input->ne[2]; + const int64_t prev_frames = previous != nullptr ? previous->ne[2] : 0; + GGML_ASSERT(prev_frames <= pad_t); + const int64_t missing = pad_t - prev_frames; + + auto repeat_first = [&](int64_t count) { + auto first = ggml_ext_slice(ctx, input, 2, 0, 1, false); + return count == 1 ? first : ggml_repeat_4d(ctx->ggml_ctx, first, first->ne[0], first->ne[1], count, first->ne[3]); + }; - if (pad_t > 0 && carry != nullptr && carry->output != nullptr) { - ggml_tensor* next = nullptr; - for (int frame = pad_t; frame > 0; frame--) { - auto item = temporal_frames[temporal_frames.size() - frame]; - next = next == nullptr ? item : ggml_concat(ctx->ggml_ctx, next, item, 2); + auto prefix = previous; + if (missing > 0) { + auto first = repeat_first(missing); + prefix = previous != nullptr ? ggml_concat(ctx->ggml_ctx, previous, first, 2) : first; + } + padded = ggml_concat(ctx->ggml_ctx, prefix, input, 2); + + if (carry != nullptr && carry->output != nullptr) { + if (frames >= pad_t) { + auto tail = ggml_ext_slice(ctx, x, 2, frames - pad_t, frames, false); + carry->push(ggml_cont(ctx->ggml_ctx, tail)); + } else { + // Avoid retaining the padding buffer across decoder chunks. + const int64_t keep_first = std::min(missing, pad_t - frames); + const int64_t keep_prev = pad_t - frames - keep_first; + auto tail = input; + if (keep_first > 0) { + tail = frames == 1 ? repeat_first(keep_first + 1) + : ggml_concat(ctx->ggml_ctx, repeat_first(keep_first), tail, 2); + } + if (keep_prev > 0) { + auto prev_tail = ggml_ext_slice(ctx, previous, 2, prev_frames - keep_prev, prev_frames, false); + tail = ggml_concat(ctx->ggml_ctx, prev_tail, tail, 2); + } + carry->push(tail); + } } - carry->push(ggml_cont(ctx->ggml_ctx, next)); } - ggml_tensor* padded = nullptr; - for (auto frame : temporal_frames) { - padded = padded == nullptr ? frame : ggml_concat(ctx->ggml_ctx, padded, frame, 2); - } auto replicate_pad = [&](ggml_tensor* input, int dim, int left, int right) { if (left > 0) { auto first = ggml_ext_slice(ctx, input, dim, 0, 1);