Skip to content
Open
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
2 changes: 1 addition & 1 deletion ggml
Submodule ggml updated 1 files
+67 −56 src/ggml-cuda/concat.cu
83 changes: 32 additions & 51 deletions src/model/vae/hunyuan_vae.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, int, int> kernel_size;
Expand Down Expand Up @@ -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<int64_t>(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<int64_t>(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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down