-
Notifications
You must be signed in to change notification settings - Fork 12.5k
mtmd : add support for Qwen2-Audio and SeaLLM-Audio #13760
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fa4fed7
mtmd : add Qwen2-Audio support
ngxson 06145bf
small clean up
ngxson 699684c
update discussion link
ngxson 9c192cf
clarify mtmd_get_output_embd
ngxson 6687d38
clarification in multimodal.md
ngxson c0bd810
fix ultravox bug
ngxson e53a0dc
ggml_cont
ngxson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
mtmd : add Qwen2-Audio support
- Loading branch information
commit fa4fed71a69b7f351194bda20cdef6d365f3d112
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,7 +254,9 @@ struct clip_vision_model { | |
ggml_tensor * post_ln_w; | ||
< A3E2 /td> | ggml_tensor * post_ln_b; | |
|
||
ggml_tensor * projection; | ||
ggml_tensor * projection; // TODO: rename it to fc (fully connected layer) | ||
ggml_tensor * mm_fc_w; | ||
ggml_tensor * mm_fc_b; | ||
|
||
// LLaVA projection | ||
ggml_tensor * mm_input_norm_w = nullptr; | ||
|
@@ -1471,48 +1473,58 @@ struct clip_graph { | |
|
||
cb(cur, "after_transformer", -1); | ||
|
||
// StackAudioFrames | ||
// https://huggingface.co/fixie-ai/ultravox-v0_5-llama-3_2-1b/blob/main/ultravox_model.py | ||
{ | ||
int64_t stride = n_embd * hparams.proj_stack_factor; | ||
int64_t padded_len = GGML_PAD(ggml_nelements(cur), stride); | ||
int64_t pad = padded_len - ggml_nelements(cur); | ||
if (pad > 0) { | ||
cur = ggml_view_1d(ctx0, cur, ggml_nelements(cur), 0); | ||
cur = ggml_pad(ctx0, cur, pad, 0, 0, 0); | ||
if (ctx->proj_type == PROJECTOR_TYPE_ULTRAVOX) { | ||
// StackAudioFrames | ||
// https://huggingface.co/fixie-ai/ultravox-v0_5-llama-3_2-1b/blob/main/ultravox_model.py | ||
{ | ||
int64_t stride = n_embd * hparams.proj_stack_factor; | ||
int64_t padded_len = GGML_PAD(ggml_nelements(cur), stride); | ||
int64_t pad = padded_len - ggml_nelements(cur); | ||
if (pad > 0) { | ||
cur = ggml_view_1d(ctx0, cur, ggml_nelements(cur), 0); | ||
cur = ggml_pad(ctx0, cur, pad, 0, 0, 0); | ||
} | ||
cur = ggml_view_2d(ctx0, cur, stride, padded_len / stride, | ||
ggml_row_size(cur->type, stride), 0); | ||
} | ||
cur = ggml_view_2d(ctx0, cur, stride, padded_len / stride, | ||
ggml_row_size(cur->type, stride), 0); | ||
} | ||
|
||
cb(cur, "after_stacked", -1); | ||
cb(cur, "after_stacked", -1); | ||
|
||
// UltravoxProjector | ||
{ | ||
// pre-norm | ||
cur = ggml_rms_norm(ctx0, cur, 1e-6); | ||
cur = ggml_mul(ctx0, cur, model.mm_norm_pre_w); | ||
// UltravoxProjector | ||
{ | ||
// pre-norm | ||
cur = ggml_rms_norm(ctx0, cur, 1e-6); | ||
cur = ggml_mul(ctx0, cur, model.mm_norm_pre_w); | ||
|
||
// ffn in | ||
cur = ggml_mul_mat(ctx0, model.mm_1_w, cur); | ||
// ffn in | ||
cur = ggml_mul_mat(ctx0, model.mm_1_w, cur); | ||
|
||
// swiglu | ||
{ | ||
int64_t split_point = cur->ne[0] / 2; | ||
ggml_tensor * x0 = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, split_point, cur->ne[1], cur->nb[1], 0)); | ||
ggml_tensor * x1 = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, split_point, cur->ne[1], cur->nb[1], split_point * ggml_element_size(cur))); | ||
// swiglu | ||
{ | ||
int64_t split_point = cur->ne[0] / 2; | ||
ggml_tensor * x0 = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, split_point, cur->ne[1], cur->nb[1], 0)); | ||
ggml_tensor * x1 = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, split_point, cur->ne[1], cur->nb[1], split_point * ggml_element_size(cur))); | ||
|
||
// see SwiGLU in ultravox_model.py, the second half passed through is silu, not the first half | ||
x1 = ggml_silu(ctx0, x1); | ||
cur = ggml_mul(ctx0, x0, x1); | ||
} | ||
|
||
// see SwiGLU in ultravox_model.py, the second half passed through is silu, not the first half | ||
x1 = ggml_silu(ctx0, x1); | ||
cur = ggml_mul(ctx0, x0, x1); | ||
// mid-norm | ||
cur = ggml_rms_norm(ctx0, cur, 1e-6); | ||
cur = ggml_mul(ctx0, cur, model.mm_norm_mid_w); | ||
|
||
// ffn out | ||
cur = ggml_mul_mat(ctx0, model.mm_2_w, cur); | ||
} | ||
|
||
// mid-norm | ||
cur = ggml_rms_norm(ctx0, cur, 1e-6); | ||
cur = ggml_mul(ctx0, cur, model.mm_norm_mid_w); | ||
} else if (ctx->proj_type == PROJECTOR_TYPE_QWEN2A) { | ||
// projector | ||
cur = ggml_mul_mat(ctx0, model.mm_fc_w, cur); | ||
cur = ggml_add(ctx0, cur, model.mm_fc_b); | ||
|
||
// ffn out | ||
cur = ggml_mul_mat(ctx0, model.mm_2_w, cur); | ||
} else { | ||
GGML_ABORT("%s: unknown projector type", __func__); | ||
} | ||
|
||
cb(cur, "projected", -1); | ||
|
@@ -1655,6 +1667,35 @@ struct clip_graph { | |
inpL = cur; | ||
} | ||
|
||
// TODO @ngxson : find a way to move this output of this function | ||
if (ctx->proj_type == PROJECTOR_TYPE_QWEN2A) { | ||
ggml_tensor * cur = inpL; | ||
// trick: use sum_rows and ggml_scale instead of ggml_pool_1d | ||
// because ggml_pool_1d is not supported on some GPU backend | ||
// add padding if number of frames is not divisible by 2 | ||
/* | ||
if (cur->ne[1] % 2 != 0) { | ||
cur = ggml_pad(ctx0, cur, 0, 1, 0, 0); | ||
} | ||
cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], 2, cur->ne[1]/2); // [n_embd, 2, n_frames/2] | ||
cur = ggml_transpose(ctx0, cur); // [2, n_embd, n_frames/2] | ||
// calc mean value | ||
{ | ||
cur = ggml_cast(ctx0, cur, GGML_TYPE_F32); | ||
cur = ggml_sum_rows(ctx0, cur); // [1, n_embd, n_frames/2] | ||
cur = ggml_scale(ctx0, cur, 0.5f); | ||
} | ||
cur = ggml_transpose(ctx0, cur); // [n_embd, 1, n_frames/2] | ||
cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], cur->ne[2]); // [n_embd, n_frames/2] | ||
*/ | ||
cur = ggml_transpose(ctx0, cur); | ||
cur = ggml_cast(ctx0, cur, GGML_TYPE_F32); | ||
cur = ggml_pool_1d(ctx0, cur, GGML_OP_POOL_AVG, 2, 2, 0); | ||
cur = ggml_transpose(ctx0, cur); | ||
cur = ggml_cast(ctx0, cur, GGML_TYPE_F32); | ||
inpL = cur; | ||
} | ||
|
||
// post-layernorm | ||
if (model.post_ln_w) { | ||
inpL = build_norm(inpL, model.post_ln_w, model.post_ln_b, norm_t, eps, -1); | ||
|
@@ -1952,6 +1993,7 @@ static ggml_cgraph * clip_image_build_graph(clip_ctx * ctx, const clip_image_f32 | |
res = graph.build_llama4(); | ||
} break; | ||
case PROJECTOR_TYPE_ULTRAVOX: | ||
case PROJECTOR_TYPE_QWEN2A: | ||
{ | ||
res = graph.build_whisper_enc(); | ||
} break; | ||
|
@@ -2186,8 +2228,10 @@ struct clip_model_loader { | |
}; | ||
} break; | ||
case PROJECTOR_TYPE_ULTRAVOX: | ||
case PROJECTOR_TYPE_QWEN2A: | ||
{ | ||
get_u32(KEY_A_PROJ_STACK_FACTOR, hparams.proj_stack_factor); | ||
bool require_stack = ctx_clip.proj_type == PROJECTOR_TYPE_ULTRAVOX; | ||
get_u32(KEY_A_PROJ_STACK_FACTOR, hparams.proj_stack_factor, require_stack); | ||
if (hparams.n_mel_bins != 128) { | ||
throw std::runtime_error(string_format("%s: only 128 mel bins are supported for ultravox\n", __func__)); | ||
} | ||
|
@@ -2266,7 +2310,7 @@ struct clip_model_loader { | |
return cur; | ||
}; | ||
|
||
auto & vision_model = ctx_clip.vision_model; | ||
auto & vision_model = ctx_clip.vision_model; // TODO: rename this to just "model" | ||
|
||
vision_model.class_embedding = get_tensor(TN_CLASS_EMBD, false); | ||
|
||
|
@@ -2463,6 +2507,15 @@ struct clip_model_loader { | |
vision_model.mm_norm_pre_w = get_tensor(string_format(TN_MM_NORM_PRE, "weight")); | ||
vision_model.mm_norm_mid_w = get_tensor(string_format(TN_MM_NORM_MID, "weight")); | ||
} break; | ||
case PROJECTOR_TYPE_QWEN2A: | ||
{ | ||
vision_model.conv1d_1_w = get_tensor(string_format(TN_CONV1D, 1, "weight")); | ||
vision_model.conv1d_1_b = get_tensor(string_format(TN_CONV1D, 1, "bias")); | ||
vision_model.conv1d_2_w = get_tensor(string_format(TN_CONV1D, 2, "weight")); | ||
vision_model.conv1d_2_b = get_tensor(string_format(TN_CONV1D, 2, "bias")); | ||
vision_model.mm_fc_w = get_tensor(string_format(TN_MM_AUDIO_FC, "weight")); | ||
vision_model.mm_fc_b = get_tensor(string_format(TN_MM_AUDIO_FC, "bias")); | ||
} break; | ||
case PROJECTOR_TYPE_INTERNVL: | ||
{ | ||
vision_model.mm_0_w = get_tensor(string_format(TN_MVLM_PROJ_MLP, 0, "weight")); | ||
|
@@ -3450,6 +3503,10 @@ int clip_n_output_tokens(const struct clip_ctx * ctx, struct clip_image_f32 * im | |
const int proj_stack_factor = ctx->vision_model.hparams.proj_stack_factor; | ||
const int n_len = CLIP_ALIGN(img->nx, proj_stack_factor); | ||
n_patches = n_len / proj_stack_factor / 2; | ||
} else if (ctx->proj_type == PROJECTOR_TYPE_QWEN2A) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this should become a |
||
// divide by 2 because of whisper | ||
// another divide by 2 because of nn.AvgPool1d(2, stride=2) | ||
n_patches = img->nx / 4; | ||
} | ||
|
||
return n_patches; | ||
|
@@ -3850,6 +3907,7 @@ bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_ima | |
case PROJECTOR_TYPE_GEMMA3: | ||
case PROJECTOR_TYPE_IDEFICS3: | ||
case PROJECTOR_TYPE_INTERNVL: | ||
case PROJECTOR_TYPE_QWEN2A: | ||
case PROJECTOR_TYPE_ULTRAVOX: | ||
{ | ||
// do nothing | ||
|
@@ -3910,7 +3968,7 @@ bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_ima | |
const int n_tokens_out = embeddings->ne[1]; | ||
const int expected_n_tokens_out = clip_n_output_tokens(ctx, imgs.entries[0].get()); | ||
if (n_tokens_out != expected_n_tokens_out) { | ||
LOG_ERR("%s: expected %d tokens, got %d\n", __func__, expected_n_tokens_out, n_tokens_out); | ||
LOG_ERR("%s: expected output %d tokens, got %d\n", __func__, expected_n_tokens_out, n_tokens_out); | ||
GGML_ABORT("Invalid number of output tokens"); | ||
} | ||
|
||
|
@@ -3955,6 +4013,8 @@ int clip_n_mmproj_embd(const struct clip_ctx * ctx) { | |
return ctx->vision_model.mm_3_w->ne[1]; | ||
case PROJECTOR_TYPE_LLAMA4: | ||
return ctx->vision_model.mm_model_proj->ne[1]; | ||
case PROJECTOR_TYPE_QWEN2A: | ||
return ctx->vision_model.mm_fc_w->ne[1]; | ||
default: | ||
GGML_ABORT("Unknown projector type"); | ||
} | ||
|
@@ -3991,6 +4051,10 @@ bool clip_has_audio_encoder(const struct clip_ctx * ctx) { | |
return ctx->vision_model.hparams.has_audio; | ||
} | ||
|
||
bool clip_has_whisper_encoder(const struct clip_ctx * ctx) { | ||
return ctx->proj_type == PROJECTOR_TYPE_GEMMA3 || ctx->proj_type == PROJECTOR_TYPE_QWEN2A; | ||
} | ||
|
||
bool clip_encode_float_image (struct clip_ctx * ctx, int n_threads, float * img, int h, int w, float * vec) { | ||
clip_image_f32 clip_img; | ||
clip_img.buf.resize(h * w * 3); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to prefer
ggml_cast
here overggml_cont
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some problem with
ggml_compute_forward_pool_1d
and when looking into the source code, I mistakenly thought that it only supports F32. Changed toggml_cont
in e53a0dcNote: I got this error without cast or cont, maybe we should assert the input to be contiguous: