Skip to content

Commit 36667c8

Browse files
authored
clip : revert the change of BOI/EOI token for GLM-edge (⚠️ breaking change) (#13259)
1 parent 3bf785f commit 36667c8

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

tools/llava/clip-impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
#define TN_MM_PROJECTOR "mm.model.fc.weight" // idefics3
7676
#define TN_MM_PATCH_MERGER "mm.patch_merger.weight" // mistral small 3.1
7777
#define TN_TOK_IMG_BREAK "v.token_embd.img_break" // pixtral
78+
#define TN_TOK_GLM_BOI "adapter.boi" // glm-edge (these embeddings are not in text model)
79+
#define TN_TOK_GLM_EOI "adapter.eoi" // glm-edge (these embeddings are not in text model)
7880

7981
// mimicpmv
8082
#define TN_MINICPMV_POS_EMBD_K "resampler.pos_embed_k"

tools/llava/clip.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,11 @@ struct clip_vision_model {
249249
struct ggml_tensor * mm_4_w = nullptr;
250250
struct ggml_tensor * mm_4_b = nullptr;
251251

252-
//GLMV-Edge projection
252+
// GLMV-Edge projection
253253
struct ggml_tensor * mm_model_adapter_conv_w = nullptr;
254254
struct ggml_tensor * mm_model_adapter_conv_b = nullptr;
255+
struct ggml_tensor * mm_glm_tok_boi = nullptr;
256+
struct ggml_tensor * mm_glm_tok_eoi = nullptr;
255257

256258
// MobileVLM projection
257259
struct ggml_tensor * mm_model_mlp_1_w = nullptr;
@@ -1559,6 +1561,13 @@ static ggml_cgraph * clip_image_build_graph_legacy(clip_ctx * ctx, const clip_im
15591561
embeddings = ggml_mul(ctx0, embeddings,x);
15601562
embeddings = ggml_mul_mat(ctx0, model.mm_model_mlp_3_w, embeddings);
15611563
}
1564+
// arrangement of BOI/EOI token embeddings
1565+
// note: these embeddings are not present in text model, hence we cannot process them as text tokens
1566+
// see: https://huggingface.co/THUDM/glm-edge-v-2b/blob/main/siglip.py#L53
1567+
{
1568+
embeddings = ggml_concat(ctx0, model.mm_glm_tok_boi, embeddings, 1); // BOI
1569+
embeddings = ggml_concat(ctx0, embeddings, model.mm_glm_tok_eoi, 1); // EOI
1570+
}
15621571
}
15631572

15641573
else if (ctx->proj_type == PROJECTOR_TYPE_QWEN2VL) {
@@ -1972,12 +1981,14 @@ struct clip_model_loader {
19721981
{
19731982
vision_model.mm_model_adapter_conv_w = get_tensor(string_format(TN_GLM_ADAPER_CONV, "weight"));
19741983
vision_model.mm_model_adapter_conv_b = get_tensor(string_format(TN_GLM_ADAPER_CONV, "bias"));
1975-
vision_model.mm_model_mlp_0_w = get_tensor(string_format(TN_GLM_ADAPTER_LINEAR,"weight"));
1976-
vision_model.mm_model_ln_q_w = get_tensor(string_format(TN_GLM_ADAPTER_NORM_1,"weight"));
1977-
vision_model.mm_model_ln_q_b = get_tensor(string_format(TN_GLM_ADAPTER_NORM_1,"bias"));
1978-
vision_model.mm_model_mlp_1_w = get_tensor(string_format(TN_GLM_ADAPTER_D_H_2_4H,"weight"));
1979-
vision_model.mm_model_mlp_2_w = get_tensor(string_format(TN_GLM_ADAPTER_GATE,"weight"));
1980-
vision_model.mm_model_mlp_3_w = get_tensor(string_format(TN_GLM_ADAPTER_D_4H_2_H,"weight"));
1984+
vision_model.mm_model_mlp_0_w = get_tensor(string_format(TN_GLM_ADAPTER_LINEAR, "weight"));
1985+
vision_model.mm_model_ln_q_w = get_tensor(string_format(TN_GLM_ADAPTER_NORM_1, "weight"));
1986+
vision_model.mm_model_ln_q_b = get_tensor(string_format(TN_GLM_ADAPTER_NORM_1, "bias"));
1987+
vision_model.mm_model_mlp_1_w = get_tensor(string_format(TN_GLM_ADAPTER_D_H_2_4H, "weight"));
1988+
vision_model.mm_model_mlp_2_w = get_tensor(string_format(TN_GLM_ADAPTER_GATE, "weight"));
1989+
vision_model.mm_model_mlp_3_w = get_tensor(string_format(TN_GLM_ADAPTER_D_4H_2_H, "weight"));
1990+
vision_model.mm_glm_tok_boi = get_tensor(string_format(TN_TOK_GLM_BOI, "weight"));
1991+
vision_model.mm_glm_tok_eoi = get_tensor(string_format(TN_TOK_GLM_EOI, "weight"));
19811992
} break;
19821993
case PROJECTOR_TYPE_QWEN2VL:
19831994
case PROJECTOR_TYPE_QWEN25VL:
@@ -2948,6 +2959,7 @@ int clip_n_output_tokens(const struct clip_ctx * ctx, struct clip_image_f32 * im
29482959

29492960
if (ctx->proj_type == PROJECTOR_TYPE_LDP || ctx->proj_type == PROJECTOR_TYPE_LDPV2 || ctx->proj_type == PROJECTOR_TYPE_GLM_EDGE) {
29502961
n_patches /= 4;
2962+
n_patches += 2; // for BOI and EOI token embeddings
29512963
} else if (ctx->proj_type == PROJECTOR_TYPE_MINICPMV) {
29522964
if (ctx->minicpmv_version == 2) {
29532965
n_patches = 96;

tools/llava/mtmd.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,6 @@ int32_t mtmd_tokenize(mtmd_context * ctx,
189189
marker_modified = "<start_of_image>" + ctx->image_marker + "<end_of_image>";
190190
string_replace_all(prompt_modified, ctx->image_marker, marker_modified);
191191

192-
} else if (proj_type == PROJECTOR_TYPE_GLM_EDGE) {
193-
// <|begin_of_image|> ... (image embeddings) ... <|end_of_image|>
194-
marker_modified = "<|begin_of_image|>" + ctx->image_marker + "<|end_of_image|>";
195-
string_replace_all(prompt_modified, ctx->image_marker, marker_modified);
196-
197192
} else if (proj_type == PROJECTOR_TYPE_IDEFICS3) {
198193
// https://github.com/huggingface/transformers/blob/a42ba80fa520c784c8f11a973ca9034e5f859b79/src/transformers/models/idefics3/processing_idefics3.py#L192-L215
199194
marker_modified = "<fake_token_around_image><global-img>" + ctx->image_marker + "<fake_token_around_image>";
@@ -213,6 +208,7 @@ int32_t mtmd_tokenize(mtmd_context * ctx,
213208
}
214209

215210
// llava-1.5, llava-1.6, Yi-VL, Yi-34B, granite: don't need to add prefix and suffix
211+
// for glm-edge, BOI and EOI token's embeddings are not present in the text model
216212

217213
std::vector<std::string> parts = string_split_str(prompt_modified, ctx->image_marker);
218214
output.clear();

0 commit comments

Comments
 (0)