diff --git a/src/core/ggml_extend.cpp b/src/core/ggml_extend.cpp index 9a119f9eb..a60af1b01 100644 --- a/src/core/ggml_extend.cpp +++ b/src/core/ggml_extend.cpp @@ -1,6 +1,7 @@ #include "core/ggml_extend.h" #include +#include #include #include #include @@ -914,6 +915,27 @@ ggml_tensor* ggml_ext_group_norm(GGMLRunnerContext* runner_ctx, return x; } +bool ggml_ext_group_norm_batches_dim3(ggml_backend_t backend) { + if (backend == nullptr) { + return false; + } + auto device = ggml_backend_get_device(backend); + auto reg = device != nullptr ? ggml_backend_dev_backend_reg(device) : nullptr; + if (reg == nullptr) { + return false; + } + // CPU loops over dim 3; CUDA / ROCm / MUSA and Vulkan launch num_groups * ne3 groups; Metal dispatches + // (num_groups, ne3) threadgroups since leejet/ggml a70d9e12. + static const char* const per_slice[] = {"CPU", "CUDA", "ROCm", "MUSA", "MTL", "Vulkan"}; + const char* name = ggml_backend_reg_name(reg); + for (const char* ok : per_slice) { + if (name != nullptr && std::strcmp(name, ok) == 0) { + return true; + } + } + return false; +} + ggml_tensor* ggml_ext_timestep_embedding(GGMLRunnerContext* runner_ctx, ggml_tensor* timesteps, int dim, diff --git a/src/core/ggml_extend.h b/src/core/ggml_extend.h index c3d1c42fb..dab70b53b 100644 --- a/src/core/ggml_extend.h +++ b/src/core/ggml_extend.h @@ -220,6 +220,10 @@ ggml_tensor* ggml_ext_group_norm(GGMLRunnerContext* runner_ctx, int num_groups = 32, float eps = 1e-6f); +// True when GROUP_NORM normalises every dim-3 slice, so frames / batch items may share one op. SYCL, OpenCL +// and unverified backends cover only slice 0. +bool ggml_ext_group_norm_batches_dim3(ggml_backend_t backend); + ggml_tensor* ggml_ext_timestep_embedding(GGMLRunnerContext* runner_ctx, ggml_tensor* timesteps, int dim, diff --git a/src/model/vae/minimax_h3_vae.hpp b/src/model/vae/minimax_h3_vae.hpp index 1adefbfad..8c5121c64 100644 --- a/src/model/vae/minimax_h3_vae.hpp +++ b/src/model/vae/minimax_h3_vae.hpp @@ -178,6 +178,9 @@ namespace MiniMaxH3VAE { const int64_t planes = x->ne[3]; GGML_ASSERT(planes % num_channels == 0); const int64_t batch = planes / num_channels; + if (!ggml_ext_group_norm_batches_dim3(ctx->backend)) { + return forward_per_item(ctx, x, batch); + } if (frames == 1) { x = ggml_reshape_4d(c, ggml_ext_cont(ctx, x), width, height, num_channels, batch); x = GroupNorm::forward(ctx, x); @@ -192,6 +195,29 @@ namespace MiniMaxH3VAE { x = ggml_cont(c, ggml_permute(c, x, 0, 2, 1, 3)); return ggml_reshape_4d(c, x, width, height, frames, planes); } + + private: + // One GROUP_NORM per frame and batch item, for backends whose GroupNorm covers only dim-3 slice 0. + ggml_tensor* forward_per_item(GGMLRunnerContext* ctx, ggml_tensor* x, int64_t batch) { + auto c = ctx->ggml_ctx; + ggml_tensor* result = nullptr; + for (int64_t t = 0; t < x->ne[2]; ++t) { + auto frame = ggml_ext_slice(ctx, x, 2, t, t + 1); + frame = ggml_cont(c, frame); + frame = ggml_reshape_4d(c, frame, frame->ne[0], frame->ne[1], num_channels, batch); + ggml_tensor* normed = nullptr; + for (int64_t b = 0; b < batch; ++b) { + auto item = batch == 1 ? frame + : ggml_view_4d(c, frame, frame->ne[0], frame->ne[1], frame->ne[2], 1, + frame->nb[1], frame->nb[2], frame->nb[3], b * frame->nb[3]); + item = GroupNorm::forward(ctx, item); + normed = normed == nullptr ? item : ggml_concat(c, normed, item, 3); + } + frame = ggml_reshape_4d(c, normed, frame->ne[0], frame->ne[1], 1, num_channels * batch); + result = result == nullptr ? frame : ggml_concat(c, result, frame, 2); + } + return result; + } }; struct Downsample3D : public GGMLBlock {