mtmd : add const in various places - #28307
Conversation
Mark `mtmd_context` as `const` in: - mtmd_bitmap_init_lazy - mtmd_tokenize - mtmd_tokenize_from_parts - mtmd_helper_support_video - mtmd_helper_bitmap_init_from_file - mtmd_helper_bitmap_init_from_buf - mtmd_helper_video_init - mtmd_helper_video_init_from_buf - mtmd_helper_model_can_chat The tokenization functions in particular are useful to have marked `const`, as that allows more easily telling the compiler that we can safely tokenize from multiple threads (`mtmd_tokenize` is already documented as thread-safe, this just reifies that in the signature).
Mark the `bitmaps` and `parts` pointers in `mtmd_tokenize` and `mtmd_tokenize_from_parts` as `const`. This allows more easily calling these with immutable arrays / vectors.
|
/bot review |
Automated code reviewReview of PR #28307 (mtmd: add
|
| struct mtmd_helper_video { | ||
| mtmd_context * mctx; | ||
| const mtmd_context * mctx; |
There was a problem hiding this comment.
This is actually unused, so we could also just remove it. I kept it for now in case the code wants to use it in the future.
There was a problem hiding this comment.
it's ok I think, mtmd_helper_video should be thread-safe (multiple video helper can use the same mctx)
There was a problem hiding this comment.
ah I misread it, let's remove it then
ngxson
left a comment
There was a problem hiding this comment.
looks reasonable. I was a bit worry if I will need to modify something inside mctx in the future, but it's true that having const make it clear which APIs are thread-safe
let's wait for bot review to see if we miss anything
|
bot review: point 1 is optional, can be a follow-up fix if you want (preprocessor must be thread-safe by design) let's address point 2 here |
|
Coolio, I've marked |
* mtmd : mark context as const in more methods Mark `mtmd_context` as `const` in: - mtmd_bitmap_init_lazy - mtmd_tokenize - mtmd_tokenize_from_parts - mtmd_helper_support_video - mtmd_helper_bitmap_init_from_file - mtmd_helper_bitmap_init_from_buf - mtmd_helper_video_init - mtmd_helper_video_init_from_buf - mtmd_helper_model_can_chat The tokenization functions in particular are useful to have marked `const`, as that allows more easily telling the compiler that we can safely tokenize from multiple threads (`mtmd_tokenize` is already documented as thread-safe, this just reifies that in the signature). * mtmd : mark tokenization input pointer as const Mark the `bitmaps` and `parts` pointers in `mtmd_tokenize` and `mtmd_tokenize_from_parts` as `const`. This allows more easily calling these with immutable arrays / vectors. * mtmd : mark llama_context as const in mtmd_helper_model_can_chat
* mtmd : mark context as const in more methods Mark `mtmd_context` as `const` in: - mtmd_bitmap_init_lazy - mtmd_tokenize - mtmd_tokenize_from_parts - mtmd_helper_support_video - mtmd_helper_bitmap_init_from_file - mtmd_helper_bitmap_init_from_buf - mtmd_helper_video_init - mtmd_helper_video_init_from_buf - mtmd_helper_model_can_chat The tokenization functions in particular are useful to have marked `const`, as that allows more easily telling the compiler that we can safely tokenize from multiple threads (`mtmd_tokenize` is already documented as thread-safe, this just reifies that in the signature). * mtmd : mark tokenization input pointer as const Mark the `bitmaps` and `parts` pointers in `mtmd_tokenize` and `mtmd_tokenize_from_parts` as `const`. This allows more easily calling these with immutable arrays / vectors. * mtmd : mark llama_context as const in mtmd_helper_model_can_chat
Overview
Mark
mtmd_contextasconstin:mtmd_bitmap_init_lazymtmd_tokenizemtmd_tokenize_from_partsmtmd_helper_support_videomtmd_helper_bitmap_init_from_filemtmd_helper_bitmap_init_from_bufmtmd_helper_video_initmtmd_helper_video_init_from_bufmtmd_helper_model_can_chatAnd mark the
bitmapsandpartsarray pointers inmtmd_tokenizeandmtmd_tokenize_from_partsasconst.The tokenization functions in particular are useful to have marked
const, as that allows more easily telling the compiler that we can safely tokenize from multiple threads (mtmd_tokenizeis already documented as thread-safe, this just reifies that in the signature). Having the array pointers beconstallows more easily calling these with immutable arrays / vectors.CC @ngxson
Additional information
This would be useful for the
llama.cppRust bindings, as it would allow us to make various methods use the correct Rust&Tor&mut Treferences as necessary.Requirements