Skip to content

Commit bfbabea

Browse files
committed
move hash to user-define
1 parent 58c4767 commit bfbabea

File tree

3 files changed

+11
-29
lines changed

3 files changed

+11
-29
lines changed

examples/llava/gemma3-cli.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ struct gemma3_context {
8989
ctx_vision.reset(mtmd_init_from_file(clip_path, model, mtmd_context_params{
9090
/* use_gpu */ true,
9191
/* timings */ true,
92-
/* hash */ false,
9392
/* n_threads */ params.cpuparams.n_threads,
9493
/* verbosity */ GGML_LOG_LEVEL_INFO,
9594
}));

examples/llava/mtmd.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ struct mtmd_context {
2929
const mtmd_context_params & ctx_params) :
3030
print_timings (ctx_params.print_timings),
3131
n_threads (ctx_params.n_threads),
32-
image_marker (ctx_params.image_marker),
33-
calc_image_hash(ctx_params.calc_image_hash)
32+
image_marker (ctx_params.image_marker)
3433
{
3534
clip_context_params ctx_clip_params;
3635
ctx_clip_params.use_gpu = ctx_params.use_gpu;
@@ -56,7 +55,7 @@ struct mtmd_image_tokens {
5655
uint32_t ny; // number of tokens in y direction
5756
uint32_t n_tokens() const { return nx * ny; }
5857
clip_image_f32_batch batch_f32; // preprocessed image patches
59-
size_t image_hash = 0; // hash of the image, useful for KV cache tracking
58+
std::string hash; // optional user-defined hash, useful for KV cache tracking
6059
};
6160

6261
mtmd_context * mtmd_init_from_file(const char * mmproj_fname,
@@ -96,16 +95,6 @@ static std::vector<llama_token> mtmd_tokenize_text_internal(
9695
return result;
9796
}
9897

99-
static uint64_t hash_vector_float(const std::vector<float> & vec) {
100-
uint64_t seed = vec.size();
101-
std::hash<float> hasher;
102-
for (float val : vec) {
103-
// inspired by boost::hash_combine
104-
seed ^= hasher(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
105-
}
106-
return seed;
107-
}
108-
10998
int32_t mtmd_tokenize(mtmd_context * ctx,
11099
std::vector<mtmd_input_chunk> & output,
111100
const mtmd_input_text & text,
@@ -170,11 +159,7 @@ int32_t mtmd_tokenize(mtmd_context * ctx,
170159
image_tokens->nx = clip_n_patches(ctx->ctx_clip); // TODO @ngxson : use clip_n_patches_by_image
171160
image_tokens->ny = 1; // TODO
172161
image_tokens->batch_f32 = std::move(batch_f32);
173-
174-
// optionally calculate the hash
175-
if (ctx->calc_image_hash) {
176-
image_tokens->image_hash = hash_vector_float(image_tokens->batch_f32.entries[0]->buf);
177-
}
162+
image_tokens->hash = bitmaps[i_img].hash;
178163

179164
mtmd_input_chunk chunk{
180165
MTMD_INPUT_CHUNK_TYPE_IMAGE,
@@ -207,8 +192,8 @@ size_t mtmd_image_tokens_get_ny(const mtmd_image_tokens * image_tokens) {
207192
return image_tokens->ny;
208193
}
209194

210-
uint64_t mtmd_image_tokens_get_hash(const mtmd_image_tokens * image_tokens) {
211-
return image_tokens->image_hash;
195+
std::string mtmd_image_tokens_get_hash(const mtmd_image_tokens * image_tokens) {
196+
return image_tokens->hash;
212197
}
213198

214199
int32_t mtmd_encode(mtmd_context * ctx, const mtmd_image_tokens * image_tokens) {

examples/llava/mtmd.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct mtmd_bitmap {
3939
uint32_t nx;
4040
uint32_t ny;
4141
std::vector<unsigned char> data;
42+
std::string hash; // optional user-defined hash, useful for KV cache tracking
4243
};
4344

4445
struct mtmd_image_tokens_deleter {
@@ -57,9 +58,6 @@ using mtmd_input_chunks = std::vector<mtmd_input_chunk>;
5758
struct mtmd_context_params {
5859
bool use_gpu = true;
5960
bool print_timings = true;
60-
// calc_image_hash is useful for tracking KV cache
61-
// if not set, mtmd_image_tokens_get_hash will return 0
62-
bool calc_image_hash = false;
6361
int n_threads = 4;
6462
enum ggml_log_level verbosity = GGML_LOG_LEVEL_INFO;
6563
const char * image_marker = "<__image__>";
@@ -100,11 +98,11 @@ MTMD_API int32_t mtmd_tokenize(mtmd_context * ctx,
10098
const std::vector<mtmd_bitmap> & bitmaps);
10199

102100
// access mtmd_image_tokens
103-
MTMD_API size_t mtmd_image_tokens_get_n_tokens(const mtmd_image_tokens * image_tokens);
104-
MTMD_API size_t mtmd_image_tokens_get_nx(const mtmd_image_tokens * image_tokens);
105-
MTMD_API size_t mtmd_image_tokens_get_ny(const mtmd_image_tokens * image_tokens);
106-
MTMD_API uint64_t mtmd_image_tokens_get_hash(const mtmd_image_tokens * image_tokens);
107-
MTMD_API void mtmd_image_tokens_free(mtmd_image_tokens * image_tokens);
101+
MTMD_API size_t mtmd_image_tokens_get_n_tokens(const mtmd_image_tokens * image_tokens);
102+
MTMD_API size_t mtmd_image_tokens_get_nx(const mtmd_image_tokens * image_tokens);
103+
MTMD_API size_t mtmd_image_tokens_get_ny(const mtmd_image_tokens * image_tokens);
104+
MTMD_API std::string mtmd_image_tokens_get_hash(const mtmd_image_tokens * image_tokens);
105+
MTMD_API void mtmd_image_tokens_free(mtmd_image_tokens * image_tokens);
108106

109107
// returns 0 on success
110108
MTMD_API int32_t mtmd_encode(mtmd_context * ctx,

0 commit comments

Comments
 (0)