Skip to content

Commit 4114ba1

Browse files
ServeurpersoComXuan Son Nguyen
andauthored
mtmd: fix silent prompt truncation on embedded NUL (ggml-org#25548)
* mtmd: fix silent prompt truncation on embedded NUL mtmd_input_text carried the prompt as a bare const char* with no length, so a NUL byte in message content cut the prompt at the tokenizer boundary and dropped every later message plus the assistant marker, with no log. Add an explicit text_len and thread it through, matching llama_tokenize and the text only path. * cleanup --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
1 parent 0c4fa7a commit 4114ba1

4 files changed

Lines changed: 8 additions & 4 deletions

File tree

tools/mtmd/mtmd-cli.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ static int eval_message(mtmd_cli_context & ctx, common_chat_msg & msg) {
250250
LOG_DBG("formatted_chat.prompt: %s\n", formatted_chat.c_str());
251251

252252
mtmd_input_text text;
253-
text.text = formatted_chat.c_str();
253+
text.text = formatted_chat.data();
254+
text.text_len = formatted_chat.size();
254255
text.add_special = add_bos;
255256
text.parse_special = true;
256257

tools/mtmd/mtmd.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ void mtmd_free(mtmd_context * ctx) {
809809
struct mtmd_tokenizer {
810810
mtmd_context * ctx;
811811

812-
std::string input_text;
812+
std::string input_text; // note: can contain null bytes; do not use c_str()
813813
bool add_special;
814814
bool parse_special;
815815
const llama_vocab * vocab;
@@ -839,9 +839,10 @@ struct mtmd_tokenizer {
839839
size_t n_bitmaps) : ctx(ctx) {
840840
add_special = text->add_special;
841841
parse_special = text->parse_special;
842-
input_text = text->text;
843842
vocab = ctx->vocab;
844843

844+
input_text.assign(text->text, text->text_len);
845+
845846
std::vector<const mtmd_bitmap *> bitmaps(bmps, bmps + n_bitmaps);
846847
auto parts_str = split_text(input_text, ctx->media_marker);
847848
size_t i_bm = 0;

tools/mtmd/mtmd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct mtmd_batch;
6767

6868
struct mtmd_input_text {
6969
const char * text;
70+
size_t text_len;
7071
bool add_special;
7172
bool parse_special;
7273
};

tools/server/server-common.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,8 @@ server_tokens process_mtmd_prompt(mtmd_context * mctx, const std::string & promp
705705
std::vector<server_tokens> inputs;
706706
// multimodal
707707
mtmd_input_text inp_txt = {
708-
prompt.c_str(),
708+
prompt.data(),
709+
prompt.size(),
709710
/* add_special */ true,
710711
/* parse_special */ true,
711712
};

0 commit comments

Comments
 (0)