@@ -1144,9 +1144,8 @@ struct server_tokens {
11441144 auto it = map_pos_to_media.find (pos);
11451145 if (it != map_pos_to_media.end ()) {
11461146 return it->second ;
1147- } else {
1148- throw std::runtime_error (" Chunk not found" );
11491147 }
1148+ throw std::runtime_error (" Chunk not found" );
11501149 }
11511150
11521151 void push_back (llama_token tok) {
@@ -1170,7 +1169,7 @@ struct server_tokens {
11701169 map_pos_to_media[start_pos] = std::move (new_chunk);
11711170 } else if (type == MTMD_INPUT_CHUNK_TYPE_TEXT) {
11721171 size_t n_tokens;
1173- auto text_tokens = mtmd_input_chunk_get_tokens_text (chunk, &n_tokens);
1172+ const auto * text_tokens = mtmd_input_chunk_get_tokens_text (chunk, &n_tokens);
11741173 for (size_t i = 0 ; i < n_tokens; ++i) {
11751174 push_back (text_tokens[i]);
11761175 }
@@ -1190,7 +1189,7 @@ struct server_tokens {
11901189 // We could also just check, but this will prevent silently dropping MTMD data.
11911190 GGML_ASSERT (has_mtmd);
11921191 for (auto it = tokens.map_pos_to_media .begin (); it != tokens.map_pos_to_media .end (); ) {
1193- auto chunk = tokens.map_pos_to_media [it->first ].get ();
1192+ auto * chunk = tokens.map_pos_to_media [it->first ].get ();
11941193 mtmd::input_chunk_ptr new_chunk (mtmd_input_chunk_copy (chunk));
11951194 map_pos_to_media[start_pos+it->first ] = std::move (new_chunk);
11961195 }
@@ -1271,33 +1270,42 @@ struct server_tokens {
12711270 }
12721271
12731272 size_t get_common_prefix (const server_tokens & b) const {
1274- size_t max_idx = std::min (tokens.size (), b.tokens .size ());
1273+ const size_t max_idx = std::min (tokens.size (), b.tokens .size ());
1274+
12751275 for (size_t i = 0 ; i < max_idx; ++i) {
1276- auto & ai = tokens[i];
1277- auto & bi = b.tokens [i];
1276+ const llama_token ai = tokens[i];
1277+ const llama_token bi = b.tokens [i];
12781278
12791279 if (ai == LLAMA_TOKEN_NULL && bi == LLAMA_TOKEN_NULL) {
12801280 GGML_ASSERT (has_mtmd);
1281+
12811282 const auto & a_chunk = find_chunk (i);
12821283 const auto & b_chunk = b.find_chunk (i);
1284+
12831285 GGML_ASSERT (a_chunk && b_chunk);
1284- std::string ai_id = mtmd_input_chunk_get_id (a_chunk.get ());
1285- std::string bi_id = mtmd_input_chunk_get_id (b_chunk.get ());
1286- size_t a_pos = mtmd_input_chunk_get_n_pos (a_chunk.get ());
1287- size_t b_pos = mtmd_input_chunk_get_n_pos (b_chunk.get ());
1288- if (ai_id == bi_id && a_pos == b_pos) {
1289- GGML_ASSERT (a_pos > 0 && " Invalid media chunk" ); // should never happen
1290- i += a_pos - 1 ; // will be +1 by the for loop
1286+
1287+ const std::string id_ai = mtmd_input_chunk_get_id (a_chunk.get ());
1288+ const std::string id_bi = mtmd_input_chunk_get_id (b_chunk.get ());
1289+
1290+ const size_t pos_a = mtmd_input_chunk_get_n_pos (a_chunk.get ());
1291+ const size_t pos_b = mtmd_input_chunk_get_n_pos (b_chunk.get ());
1292+
1293+ if (id_ai == id_bi && pos_a == pos_b) {
1294+ GGML_ASSERT (pos_a > 0 && " Invalid media chunk" ); // should never happen
1295+ i += pos_a - 1 ; // will be +1 by the for loop
12911296 continue ;
1292- } else {
1293- return i;
12941297 }
1295- } else if (ai == bi) {
1296- continue ;
1297- } else {
1298+
12981299 return i;
12991300 }
1301+
1302+ if (ai == bi) {
1303+ continue ;
1304+ }
1305+
1306+ return i;
13001307 }
1308+
13011309 return max_idx; // all tokens are equal
13021310 }
13031311
@@ -1308,7 +1316,7 @@ struct server_tokens {
13081316 const int32_t n_vocab = llama_vocab_n_tokens (vocab);
13091317
13101318 for (size_t i = 0 ; i < tokens.size (); ++i) {
1311- auto & t = tokens[i];
1319+ const auto & t = tokens[i];
13121320 if (t == LLAMA_TOKEN_NULL) {
13131321 try {
13141322 const auto & chunk = find_chunk (i);
@@ -1330,8 +1338,8 @@ struct server_tokens {
13301338 mtmd_context * mctx,
13311339 llama_pos n_past,
13321340 int32_t seq_id,
1333- llama_pos & n_pos_out) {
1334- auto & chunk = find_chunk (n_past);
1341+ llama_pos & n_pos_out) const {
1342+ const auto & chunk = find_chunk (n_past);
13351343 const char * name = mtmd_input_chunk_get_type (chunk.get ()) == MTMD_INPUT_CHUNK_TYPE_IMAGE
13361344 ? " image" : " audio" ;
13371345 SRV_INF (" processing %s...\n " , name);
0 commit comments