Skip to content

Commit a0bae25

Browse files
authored
mtmd: add pos_0 to mtmd_image_tokens_get_decoder_pos (breaking change) (ggml-org#22082)
* mtmd: add pos_0 to mtmd_image_tokens_get_decoder_pos * fix build
1 parent c9d98e0 commit a0bae25

5 files changed

Lines changed: 22 additions & 17 deletions

File tree

tests/test-mtmd-c-api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int main(void) {
4242
const mtmd_image_tokens * image_tokens = mtmd_input_chunk_get_tokens_image(chunk);
4343
size_t n_tokens = mtmd_image_tokens_get_n_tokens(image_tokens);
4444
// get position of the last token, which should be (nx - 1, ny - 1)
45-
struct mtmd_decoder_pos pos = mtmd_image_tokens_get_decoder_pos(image_tokens, n_tokens - 1);
45+
struct mtmd_decoder_pos pos = mtmd_image_tokens_get_decoder_pos(image_tokens, 0, n_tokens - 1);
4646
size_t nx = pos.x + 1;
4747
size_t ny = pos.y + 1;
4848
const char * id = mtmd_image_tokens_get_id(image_tokens);

tools/mtmd/mtmd-helper.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks) {
114114
return n_pos;
115115
}
116116

117-
void mtmd_helper_image_get_decoder_pos(const mtmd_image_tokens * chunks, mtmd_decoder_pos * out_pos) {
117+
void mtmd_helper_image_get_decoder_pos(const mtmd_image_tokens * chunks, llama_pos pos_0, mtmd_decoder_pos * out_pos) {
118118
size_t n_tokens = mtmd_image_tokens_get_n_tokens(chunks);
119119
for (size_t i = 0; i < n_tokens; i++) {
120-
out_pos[i] = mtmd_image_tokens_get_decoder_pos(chunks, i);
120+
out_pos[i] = mtmd_image_tokens_get_decoder_pos(chunks, pos_0, i);
121121
}
122122
}
123123

@@ -163,15 +163,15 @@ struct decode_embd_batch {
163163
}
164164

165165
// M-RoPE for image
166-
void set_position_mrope_2d(llama_pos pos_0, const std::vector<mtmd_decoder_pos> & rel_pos, llama_seq_id seq_id) {
166+
void set_position_mrope_2d(const std::vector<mtmd_decoder_pos> & rel_pos, llama_seq_id seq_id) {
167167
GGML_ASSERT(n_pos_per_embd == 4);
168168
GGML_ASSERT(!rel_pos.empty() && (int32_t)rel_pos.size() == batch.n_tokens);
169169
seq_id_0[0] = seq_id;
170170
for (int32_t i = 0; i < batch.n_tokens; i++) {
171-
pos[i ] = pos_0 + rel_pos[i].t;
172-
pos[i + batch.n_tokens ] = pos_0 + rel_pos[i].y;
173-
pos[i + batch.n_tokens * 2] = pos_0 + rel_pos[i].x;
174-
pos[i + batch.n_tokens * 3] = 0; // last pos dim is unused
171+
pos[i ] = rel_pos[i].t;
172+
pos[i + batch.n_tokens ] = rel_pos[i].y;
173+
pos[i + batch.n_tokens * 2] = rel_pos[i].x;
174+
pos[i + batch.n_tokens * 3] = rel_pos[i].z;
175175
}
176176
for (int i = 0; i < batch.n_tokens; i++) {
177177
batch.n_seq_id[i] = 1;
@@ -188,7 +188,7 @@ struct decode_embd_batch {
188188
pos[i ] = pos_0 + i;
189189
pos[i + batch.n_tokens ] = pos_0 + i;
190190
pos[i + batch.n_tokens * 2] = pos_0 + i;
191-
pos[i + batch.n_tokens * 3] = 0; // last pos dim is unused
191+
pos[i + batch.n_tokens * 3] = pos_0 + i;
192192
}
193193
for (int i = 0; i < batch.n_tokens; i++) {
194194
batch.n_seq_id[i] = 1;
@@ -268,8 +268,8 @@ int32_t mtmd_helper_decode_image_chunk(
268268
}
269269
const auto n_tokens = mtmd_image_tokens_get_n_tokens(image_tokens);
270270
std::vector<mtmd_decoder_pos> rel_pos(n_tokens);
271-
mtmd_helper_image_get_decoder_pos(image_tokens, rel_pos.data());
272-
batch_embd.set_position_mrope_2d(n_past, rel_pos, seq_id);
271+
mtmd_helper_image_get_decoder_pos(image_tokens, n_past, rel_pos.data());
272+
batch_embd.set_position_mrope_2d(rel_pos, seq_id);
273273
} else if (chunk_type == MTMD_INPUT_CHUNK_TYPE_AUDIO) {
274274
batch_embd.set_position_mrope_1d(n_past, seq_id);
275275
} else {

tools/mtmd/mtmd-helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ MTMD_API llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks);
4949

5050
// helper to get the list of relative positions corresponding to the embedding tokens, to be used by M-RoPE
5151
// out_pos must have length == mtmd_helper_get_n_tokens(image)
52-
MTMD_API void mtmd_helper_image_get_decoder_pos(const mtmd_image_tokens * image, struct mtmd_decoder_pos * out_pos);
52+
MTMD_API void mtmd_helper_image_get_decoder_pos(const mtmd_image_tokens * image, llama_pos pos_0, struct mtmd_decoder_pos * out_pos);
5353

5454
// helper function that automatically:
5555
// 1. run llama_decode() on text chunks

tools/mtmd/mtmd.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,11 +1246,14 @@ size_t mtmd_image_tokens_get_ny(const mtmd_image_tokens * image_tokens) {
12461246
return image_tokens->ny;
12471247
}
12481248

1249-
mtmd_decoder_pos mtmd_image_tokens_get_decoder_pos(const mtmd_image_tokens * image_tokens, size_t i) {
1249+
mtmd_decoder_pos mtmd_image_tokens_get_decoder_pos(const mtmd_image_tokens * image_tokens, llama_pos pos_0, size_t i) {
12501250
mtmd_decoder_pos pos;
1251-
pos.t = 0;
1252-
pos.x = i % image_tokens->nx;
1253-
pos.y = i / image_tokens->nx;
1251+
// M-RoPE logic
1252+
// TODO: support other types of position encoding if needed
1253+
pos.t = pos_0;
1254+
pos.x = pos_0 + (i % image_tokens->nx);
1255+
pos.y = pos_0 + (i / image_tokens->nx);
1256+
pos.z = 0; // unused for now
12541257
return pos;
12551258
}
12561259

tools/mtmd/mtmd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,13 @@ struct mtmd_decoder_pos {
196196
uint32_t t;
197197
uint32_t x;
198198
uint32_t y;
199+
uint32_t z; // unused for now, reserved for future use
199200
};
200201
// get position for decoder attention, to be used by M-RoPE models
201202
// i is the index of the embedding token, ranging from 0 to mtmd_image_tokens_get_n_tokens() - 1
203+
// pos_0 is the absolute position of the first token
202204
// return relative position (for example, embedding 0 will have position (0, 0, 0); remember to adjust it to the current absolute position)
203-
MTMD_API struct mtmd_decoder_pos mtmd_image_tokens_get_decoder_pos(const mtmd_image_tokens * image_tokens, size_t i);
205+
MTMD_API struct mtmd_decoder_pos mtmd_image_tokens_get_decoder_pos(const mtmd_image_tokens * image_tokens, llama_pos pos_0, size_t i);
204206

205207
// tokenize an input text prompt and a list of bitmaps (images/audio)
206208
// the prompt must have the input image marker (default: "<__media__>") in it

0 commit comments

Comments
 (0)