| 60 | } |
| 61 | |
| 62 | bool Qwen3_5VL::insert(chat_meta_info_t& meta_info, lm_uniform_input_t& input, std::function<bool()> is_cancelled) { |
| 63 | // preprocess |
| 64 | constexpr int image_soft_token_id = 248056; |
| 65 | this->profiler_list[TKOEN_ENCODE_TIME].start(); |
| 66 | std::string templated_text; |
| 67 | if (input.messages.empty() && input.prompt.empty()) { |
| 68 | header_print("WARNING", "No messages or prompt provided"); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | constexpr bool DEBUG_IMAGE_PREPROCESS = false; |
| 73 | qwen3_5vl_image_payload_t image_payload; |
| 74 | image_payload.num_images = 0; |
| 75 | if (input.images.size() > 0) { |
| 76 | |
| 77 | |
| 78 | // header_print("info", "Processing images..."); |
| 79 | |
| 80 | // time_utils::time_point preprocess_start = time_utils::now(); |
| 81 | for(const auto& img_str : input.images){ |
| 82 | qwen3_5vl_image_t image = this->load_image(img_str); |
| 83 | |
| 84 | preprocess_image(image, image_payload._data__processed); |
| 85 | // Push the image AFTER preprocessing so grid_h and grid_w are set |
| 86 | image_payload.images.push_back(image); |
| 87 | image_payload.num_images++; |
| 88 | } |
| 89 | } |
| 90 | if (!input.messages.empty()) { // already a formated messages, usually from REST API |
| 91 | json qwenvl_message = json::array(); |
| 92 | for (const auto& item : input.messages) { |
| 93 | if (!item.contains("images")) { |
| 94 | qwenvl_message.push_back(item); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | json newContent = json::array(); |
| 99 | for (const auto& img : item["images"]) { |
| 100 | newContent.push_back({ |
| 101 | {"type", "image"}, |
| 102 | {"image", img} |
| 103 | }); |
| 104 | } |
| 105 | newContent.push_back({ |
| 106 | {"type", "text"}, |
| 107 | {"text", item["content"]} |
| 108 | }); |
| 109 | |
| 110 | json newItem = { |
| 111 | {"role", item["role"]}, |
| 112 | {"content", newContent} |
| 113 | }; |
| 114 | |
| 115 | qwenvl_message.push_back(newItem); |
| 116 | } |
| 117 | templated_text = this->apply_chat_template(qwenvl_message, input.tools); |
| 118 | int total_images = 0; |
| 119 | for (auto& message : qwenvl_message) { |
no test coverage detected