diff --git a/ggml b/ggml index e9e6fe2c4..a30080c04 160000 --- a/ggml +++ b/ggml @@ -1 +1 @@ -Subproject commit e9e6fe2c4c460eb7e6293c512acd6df777b4a7ac +Subproject commit a30080c041b263044264b4dac3d043b3cdf6653b diff --git a/src/model/vae/hunyuan_vae.hpp b/src/model/vae/hunyuan_vae.hpp index 5953772bc..aa8e51647 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; @@ -102,55 +91,47 @@ namespace Hunyuan { int pad_t = std::get<0>(kernel_size) - 1; auto padded = ggml_ext_cont(ctx, x); if (pad_t > 0) { - auto previous = carry != nullptr && carry->is_continuation() ? carry->take() : nullptr; - if (previous != nullptr) { - 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 = 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]); + }; - auto input = flatten_video_frames(ctx, padded); - auto prev_flat = previous != nullptr ? flatten_video_frames(ctx, previous) : nullptr; - auto prefix = prev_flat; + auto prefix = previous; 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); - } - prefix = prefix != nullptr ? ggml_concat(ctx->ggml_ctx, prefix, first, 1) : first; + 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, 1); - padded = ggml_reshape_4d(ctx->ggml_ctx, padded, x->ne[0], x->ne[1], padded->ne[1], x->ne[3]); + padded = ggml_concat(ctx->ggml_ctx, prefix, input, 2); 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 (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) { - 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); - } + 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, prev_flat, 1, prev_frames - keep_prev, prev_frames, false); - tail = ggml_concat(ctx->ggml_ctx, prev_tail, tail, 1); + 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(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)); + carry->push(tail); } } } + 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 +470,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 +739,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;