| 3643 | } |
| 3644 | |
| 3645 | bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_image_f32_batch * imgs_c_ptr, float * vec) { |
| 3646 | const clip_image_f32_batch & imgs = *imgs_c_ptr; |
| 3647 | int batch_size = imgs.entries.size(); |
| 3648 | |
| 3649 | // TODO @ngxson : implement batch size > 1 as a loop |
| 3650 | // we don't need true batching support because the cgraph will gonna be big anyway |
| 3651 | if (batch_size != 1) { |
| 3652 | return false; // only support batch size of 1 |
| 3653 | } |
| 3654 | |
| 3655 | // build the inference graph |
| 3656 | ctx->debug_print_tensors.clear(); |
| 3657 | ggml_backend_sched_reset(ctx->sched.get()); |
| 3658 | ggml_cgraph * gf = clip_image_build_graph(ctx, imgs); |
| 3659 | ggml_backend_sched_alloc_graph(ctx->sched.get(), gf); |
| 3660 | |
| 3661 | // set inputs |
| 3662 | const auto & model = ctx->model; |
| 3663 | const auto & hparams = model.hparams; |
| 3664 | |
| 3665 | const int image_size_width = imgs.entries[0]->nx; |
| 3666 | const int image_size_height = imgs.entries[0]->ny; |
| 3667 | |
| 3668 | const int patch_size = hparams.patch_size; |
| 3669 | const int num_patches = ((image_size_width / patch_size) * (image_size_height / patch_size)); |
| 3670 | const int n_pos = num_patches + (model.class_embedding ? 1 : 0); |
| 3671 | const int pos_w = image_size_width / patch_size; |
| 3672 | const int pos_h = image_size_height / patch_size; |
| 3673 | |
| 3674 | const bool use_window_attn = hparams.n_wa_pattern > 0; // for qwen2.5vl |
| 3675 | |
| 3676 | auto get_inp_tensor = [&gf](const char * name) { |
| 3677 | ggml_tensor * inp = ggml_graph_get_tensor(gf, name); |
| 3678 | if (inp == nullptr) { |
| 3679 | GGML_ABORT("Failed to get tensor %s", name); |
| 3680 | } |
| 3681 | if (!(inp->flags & GGML_TENSOR_FLAG_INPUT)) { |
| 3682 | GGML_ABORT("Tensor %s is not an input tensor", name); |
| 3683 | } |
| 3684 | return inp; |
| 3685 | }; |
| 3686 | |
| 3687 | auto set_input_f32 = [&get_inp_tensor](const char * name, std::vector<float> & values) { |
| 3688 | ggml_tensor * cur = get_inp_tensor(name); |
| 3689 | GGML_ASSERT(cur->type == GGML_TYPE_F32); |
| 3690 | GGML_ASSERT(ggml_nelements(cur) == (int64_t)values.size()); |
| 3691 | ggml_backend_tensor_set(cur, values.data(), 0, ggml_nbytes(cur)); |
| 3692 | }; |
| 3693 | |
| 3694 | auto set_input_i32 = [&get_inp_tensor](const char * name, std::vector<int32_t> & values) { |
| 3695 | ggml_tensor * cur = get_inp_tensor(name); |
| 3696 | GGML_ASSERT(cur->type == GGML_TYPE_I32); |
| 3697 | GGML_ASSERT(ggml_nelements(cur) == (int64_t)values.size()); |
| 3698 | ggml_backend_tensor_set(cur, values.data(), 0, ggml_nbytes(cur)); |
| 3699 | }; |
| 3700 | |
| 3701 | // set input pixel values |
| 3702 | if (!imgs.is_audio) { |
no test coverage detected