Skip to content
Merged
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
22 changes: 22 additions & 0 deletions src/core/ggml_extend.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "core/ggml_extend.h"

#include <cmath>
#include <cstring>
#include <stdexcept>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/core/ggml_extend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions src/model/vae/minimax_h3_vae.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
Loading