| 765 | } |
| 766 | |
| 767 | int32_t mtmd_encode(mtmd_context * ctx, const mtmd_image_tokens * image_tokens) { |
| 768 | clip_ctx * ctx_clip = ctx->ctx_v; |
| 769 | if (!ctx_clip) { |
| 770 | LOG_ERR("%s: this API does not support non-vision input, please use mtmd_encode_chunk instead\n", __func__); |
| 771 | return 1; |
| 772 | } |
| 773 | int n_mmproj_embd = clip_n_mmproj_embd(ctx_clip); |
| 774 | ctx->image_embd_v.resize(image_tokens->n_tokens() * n_mmproj_embd); |
| 775 | bool ok = false; |
| 776 | |
| 777 | if (clip_is_llava(ctx_clip) || clip_is_minicpmv(ctx_clip) || clip_is_glm(ctx_clip)) { |
| 778 | // TODO @ngxson : llava does not support batched encoding ; this should be fixed inside clip_image_batch_encode() |
| 779 | const auto & entries = image_tokens->batch_f32.entries; |
| 780 | for (size_t i = 0; i < entries.size(); i++) { |
| 781 | int n_tokens_per_image = clip_n_output_tokens(ctx_clip, entries[i].get()); |
| 782 | ok = clip_image_encode( |
| 783 | ctx_clip, |
| 784 | ctx->n_threads, |
| 785 | entries[i].get(), |
| 786 | ctx->image_embd_v.data() + i*n_mmproj_embd*n_tokens_per_image); |
| 787 | } |
| 788 | } else { |
| 789 | ok = clip_image_batch_encode( |
| 790 | ctx_clip, |
| 791 | ctx->n_threads, |
| 792 | &image_tokens->batch_f32, |
| 793 | ctx->image_embd_v.data()); |
| 794 | } |
| 795 | |
| 796 | return ok ? 0 : 1; |
| 797 | } |
| 798 | |
| 799 | float * mtmd_get_output_embd(mtmd_context * ctx) { |
| 800 | return ctx->image_embd_v.data(); |
no test coverage detected