Skip to content

Commit 9d2b2cb

Browse files
committed
Sync mtmd: add mtmd_image_tokens_get_decoder_pos() API (#21851)
Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent 701195e commit 9d2b2cb

1 file changed

Lines changed: 100 additions & 15 deletions

File tree

llama_cpp/mtmd_cpp.py

Lines changed: 100 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,59 @@ class mtmd_input_chunk_type(enum.IntEnum):
8282
mtmd_context_p = NewType("mtmd_context_p", int)
8383
mtmd_context_p_ctypes = c_void_p
8484

85-
# struct mtmd_bitmap;
85+
# // represents raw image data, layout is RGBRGBRGB...
86+
# // length of data must be nx * ny * 3
87+
# struct mtmd_bitmap {
88+
# uint32_t nx;
89+
# uint32_t ny;
90+
# std::vector<unsigned char> data;
91+
# std::string id; // optional user-defined id, for ex: can be set to image hash, useful for KV cache tracking
92+
# bool is_audio = false; // true if the bitmap is audio
93+
# };
8694
mtmd_bitmap_p = NewType("mtmd_bitmap_p", int)
8795
mtmd_bitmap_p_ctypes = c_void_p
8896

89-
# struct mtmd_image_tokens;
97+
# struct mtmd_image_tokens {
98+
# uint32_t nx; // number of tokens in x direction
99+
# uint32_t ny; // number of tokens in y direction
100+
# bool use_mrope_pos = false; // use M-RoPE position counting (the whole image is 1 temporal position)
101+
# uint32_t n_tokens() const { return nx * ny; }
102+
# clip_image_f32_batch batch_f32; // preprocessed image patches
103+
# std::string id; // optional user-defined ID, useful for KV cache tracking
104+
# mtmd_image_tokens clone() {
105+
# return mtmd_image_tokens{
106+
# nx,
107+
# ny,
108+
# use_mrope_pos,
109+
# batch_f32.clone(),
110+
# id
111+
# };
112+
# }
113+
# };
90114
mtmd_image_tokens_p = NewType("mtmd_image_tokens_p", int)
91115
mtmd_image_tokens_p_ctypes = c_void_p
92116

93-
# struct mtmd_input_chunk;
117+
# struct mtmd_audio_tokens {
118+
# uint32_t n_tokens; // number of tokens
119+
# clip_image_f32_batch batch_f32; // preprocessed image patches
120+
# std::string id; // optional user-defined ID, useful for KV cache tracking
121+
# mtmd_audio_tokens clone() {
122+
# return mtmd_audio_tokens{
123+
# n_tokens,
124+
# batch_f32.clone(),
125+
# id
126+
# };
127+
# }
128+
# };
129+
mtmd_audio_tokens_p = NewType("mtmd_audio_tokens_p", int)
130+
mtmd_audio_tokens_p_ctypes = c_void_p
131+
132+
# struct mtmd_input_chunk {
133+
# mtmd_input_chunk_type type;
134+
# std::vector<llama_token> tokens_text;
135+
# mtmd_image_tokens_ptr tokens_image;
136+
# mtmd_audio_tokens_ptr tokens_audio;
137+
# };
94138
mtmd_input_chunk_p = NewType("mtmd_input_chunk_p", int)
95139
mtmd_input_chunk_p_ctypes = c_void_p
96140

@@ -487,18 +531,6 @@ def mtmd_input_chunk_free(chunk: mtmd_input_chunk_p):
487531
def mtmd_image_tokens_get_n_tokens(image_tokens: mtmd_image_tokens_p) -> c_size_t:
488532
...
489533

490-
# MTMD_API size_t mtmd_image_tokens_get_nx (const mtmd_image_tokens * image_tokens);
491-
@ctypes_function_mtmd(
492-
"mtmd_image_tokens_get_nx", [mtmd_image_tokens_p_ctypes], c_size_t)
493-
def mtmd_image_tokens_get_nx(image_tokens: mtmd_image_tokens_p) -> c_size_t:
494-
...
495-
496-
# MTMD_API size_t mtmd_image_tokens_get_ny (const mtmd_image_tokens * image_tokens);
497-
@ctypes_function_mtmd(
498-
"mtmd_image_tokens_get_ny", [mtmd_image_tokens_p_ctypes], c_size_t)
499-
def mtmd_image_tokens_get_ny(image_tokens: mtmd_image_tokens_p) -> c_size_t:
500-
...
501-
502534
# MTMD_API const char * mtmd_image_tokens_get_id (const mtmd_image_tokens * image_tokens); // TODO: deprecate
503535
@ctypes_function_mtmd(
504536
"mtmd_image_tokens_get_id", [mtmd_image_tokens_p_ctypes], c_char_p)
@@ -513,6 +545,59 @@ def mtmd_image_tokens_get_n_pos(image_tokens: mtmd_image_tokens_p) -> c_int32:
513545
"""number of temporal positions (equals to max(t,h,w) for M-RoPE; equals to n_tokens otherwise)"""
514546
...
515547

548+
# DEPRECATED(MTMD_API size_t mtmd_image_tokens_get_nx(const mtmd_image_tokens * image_tokens),
549+
# "use mtmd_image_tokens_get_decoder_pos() instead");
550+
@ctypes_function_mtmd(
551+
"mtmd_image_tokens_get_nx", [mtmd_image_tokens_p_ctypes], c_size_t)
552+
def mtmd_image_tokens_get_nx(image_tokens: mtmd_image_tokens_p) -> c_size_t:
553+
"""
554+
use mtmd_image_tokens_get_decoder_pos() instead
555+
"""
556+
...
557+
558+
# DEPRECATED(MTMD_API size_t mtmd_image_tokens_get_ny(const mtmd_image_tokens * image_tokens),
559+
# "use mtmd_image_tokens_get_decoder_pos() instead");
560+
@ctypes_function_mtmd(
561+
"mtmd_image_tokens_get_ny", [mtmd_image_tokens_p_ctypes], c_size_t)
562+
def mtmd_image_tokens_get_ny(image_tokens: mtmd_image_tokens_p) -> c_size_t:
563+
"""
564+
use mtmd_image_tokens_get_decoder_pos() instead
565+
"""
566+
...
567+
568+
# struct mtmd_decoder_pos {
569+
# uint32_t t;
570+
# uint32_t x;
571+
# uint32_t y;
572+
# };
573+
class mtmd_decoder_pos(Structure):
574+
_fields_ = [
575+
("t", c_uint32),
576+
("x", c_uint32),
577+
("y", c_uint32),
578+
]
579+
580+
if TYPE_CHECKING:
581+
t: c_uint32
582+
x: c_uint32
583+
y: c_uint32
584+
585+
# // get position for decoder attention, to be used by M-RoPE models
586+
# // i is the index of the embedding token, ranging from 0 to mtmd_image_tokens_get_n_tokens() - 1
587+
# // return relative position (for example, embedding 0 will have position (0, 0, 0);
588+
# // remember to adjust it to the current absolute position)
589+
# MTMD_API struct mtmd_decoder_pos mtmd_image_tokens_get_decoder_pos(const mtmd_image_tokens * image_tokens, size_t i);
590+
@ctypes_function_mtmd(
591+
"mtmd_image_tokens_get_decoder_pos", [mtmd_image_tokens_p_ctypes, c_size_t], mtmd_decoder_pos)
592+
def mtmd_image_tokens_get_decoder_pos(image_tokens: mtmd_image_tokens_p, i: c_size_t) -> mtmd_decoder_pos:
593+
"""
594+
get position for decoder attention, to be used by M-RoPE models
595+
i is the index of the embedding token, ranging from 0 to mtmd_image_tokens_get_n_tokens() - 1
596+
return relative position (for example, embedding 0 will have position (0, 0, 0);
597+
remember to adjust it to the current absolute position)
598+
"""
599+
...
600+
516601
# // tokenize an input text prompt and a list of bitmaps (images/audio)
517602
# // the prompt must have the input image marker (default: "<__media__>") in it
518603
# // the default marker is defined by mtmd_default_marker()

0 commit comments

Comments
 (0)