| 10 | #include "base64.hpp" |
| 11 | |
| 12 | static bool encode_image_with_clip(clip_ctx * ctx_clip, int n_threads, const clip_image_u8 * img, float * image_embd, int * n_img_pos) { |
| 13 | clip_image_f32 * img_res = make_clip_image_f32(); |
| 14 | if (!clip_image_preprocess(ctx_clip, img, img_res, /*pad2square =*/ true)) { |
| 15 | fprintf(stderr, "%s: unable to preprocess image\n", __func__); |
| 16 | clip_image_f32_free(img_res); |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | *n_img_pos = clip_n_patches(ctx_clip); |
| 21 | |
| 22 | const int64_t t_img_enc_start_us = ggml_time_us(); |
| 23 | bool encoded = clip_image_encode(ctx_clip, n_threads, img_res, image_embd); |
| 24 | clip_image_f32_free(img_res); |
| 25 | if (!encoded) { |
| 26 | fprintf(stderr, "Unable to encode image\n"); |
| 27 | |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | const int64_t t_img_enc_end_us = ggml_time_us(); |
| 32 | float t_img_enc_ms = (t_img_enc_end_us - t_img_enc_start_us) / 1000.0; |
| 33 | |
| 34 | printf("\n%s: image encoded in %8.2f ms by CLIP (%8.2f ms per image patch)\n", __func__, t_img_enc_ms, t_img_enc_ms / *n_img_pos); |
| 35 | |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | bool llava_validate_embed_size(const llama_context * ctx_llama, const clip_ctx * ctx_clip) { |
| 40 | // make sure that the correct mmproj was used, i.e., compare apples to apples |
no test coverage detected