Skip to content

Implement pixel unshuffle block for lfm2vl #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: xsn/lfm2_vl
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions tools/mtmd/clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,45 @@ struct clip_graph {
return cur;
}

// lfm2vl
static ggml_tensor * build_pixel_unshuffle_block(ggml_context * ctx, ggml_tensor * x, const int factor) {
// [n, w, h, c]
const int64_t n = x->ne[3];
int64_t w = x->ne[2];
int64_t h = x->ne[1];
const int64_t c = x->ne[0];

if (factor == 1) {
return x;
}

GGML_ASSERT(factor > 1 && (factor & (factor - 1)) == 0); // factor must be power of two for GGML_PAD
GGML_ASSERT(n == 1); // only support batch size of 1
GGML_ASSERT(w > 0 && h > 0); // width and height must be positive

// pad w and h to factor
const int64_t pad_w = GGML_PAD(w, factor) - w;
const int64_t pad_h = GGML_PAD(h, factor) - h;

if (pad_w || pad_h) {
x = ggml_pad(ctx, x, 0, pad_h, pad_w, 0);
w += pad_w;
h += pad_h;
}

// unshuffle h
x = ggml_view_3d(ctx, x, c * factor, h / factor, w, x->nb[1] * factor, x->nb[2], 0);
x = ggml_permute(ctx, x, 0, 2, 1, 3);
x = ggml_cont(ctx, x);

// unshuffle w
x = ggml_view_3d(ctx, x, c * factor * factor, w / factor, h / factor, x->nb[1] * factor, x->nb[2], 0);
x = ggml_permute(ctx, x, 0, 2, 1, 3);
x = ggml_cont(ctx, x);

return x;
}

};

static ggml_cgraph * clip_image_build_graph(clip_ctx * ctx, const clip_image_f32_batch & imgs) {
Expand Down