forked from openvinotoolkit/openvino.genai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs_embedder.cpp
261 lines (225 loc) · 12.1 KB
/
inputs_embedder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (C) 2023-2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "openvino/genai/visual_language/perf_metrics.hpp"
#include "visual_language/inputs_embedder.hpp"
#include "visual_language/clip.hpp"
#include "visual_language/vision_encoder.hpp"
#include "visual_language/embedding_model.hpp"
#include "visual_language/qwen2vl/classes.hpp"
#include "visual_language/phi3_vision/classes.hpp"
#include "visual_language/minicpm/classes.hpp"
#include "visual_language/llava/classes.hpp"
#include "visual_language/llava_next/classes.hpp"
#include "visual_language/internvl_chat/classes.hpp"
#include "utils.hpp"
namespace ov::genai {
// Base InputsEmbedder class
std::pair<ov::Tensor, std::optional<int64_t>> InputsEmbedder::IInputsEmbedder::get_position_ids(const size_t inputs_embeds_size, const size_t history_size) {
ov::Tensor position_ids = ov::Tensor{ov::element::i64, { 1, inputs_embeds_size }};
std::iota(position_ids.data<int64_t>(), position_ids.data<int64_t>() + position_ids.get_size(), history_size);
return {position_ids, std::nullopt};
}
void InputsEmbedder::IInputsEmbedder::start_chat(const std::string& system_message) {
m_is_chat_conversation = true;
if (!m_kv_cache_state.get_state().empty()) {
m_history.clear();
m_kv_cache_state.reset_state();
}
if (system_message.empty()) {
return;
}
m_history = {{{"role", "system"}, {"content", system_message}}};
}
void InputsEmbedder::IInputsEmbedder::update_chat_history(const std::string& decoded_results, const ov::genai::GenerationStatus generation_finish_status) {
m_kv_cache_state.num_tokens_to_trim = 0;
if (generation_finish_status == ov::genai::GenerationStatus::CANCEL) {
// If chat generation process was cancelled by user, let's rollback to previous state of history
m_history.pop_back();
std::vector<int64_t>& state = m_kv_cache_state.get_state();
m_kv_cache_state.num_tokens_to_trim = state.size() - m_prev_hist_length;
state.resize(m_prev_hist_length);
m_kv_cache_state.reset_mem_state = state.empty();
} else {
// Tail of chat template is missing in KV cache.
// Find the tail to concatenate it with the next input prompt.
m_history.push_back({{"role", "assistant"}, {"content", decoded_results}});
}
}
void InputsEmbedder::IInputsEmbedder::finish_chat() {
m_is_chat_conversation = false;
m_history.clear();
m_kv_cache_state.reset_state();
}
InputsEmbedder::IInputsEmbedder::IInputsEmbedder(
const VLMConfig& vlm_config,
const std::filesystem::path& model_dir,
const std::string& device,
const ov::AnyMap device_config) :
m_vlm_config{vlm_config},
m_vision_encoder(VisionEncoder::create(model_dir, m_vlm_config.model_type, device, device_config)),
m_embedding(model_dir, m_vlm_config.scale_emb, device, device_config),
m_tokenizer{model_dir, device_config} { }
InputsEmbedder::IInputsEmbedder::IInputsEmbedder(
const VLMConfig& vlm_config,
const ModelsMap& models_map,
const Tokenizer& tokenizer,
const std::filesystem::path& config_dir_path,
const std::string& device,
const ov::AnyMap device_config) :
m_vlm_config{vlm_config},
m_vision_encoder(VisionEncoder::create(
utils::get_model_weights_pair(models_map, "vision_embeddings").first,
utils::get_model_weights_pair(models_map, "vision_embeddings").second,
config_dir_path,
m_vlm_config.model_type,
device,
device_config
)),
m_embedding(
utils::get_model_weights_pair(models_map, "text_embeddings").first,
utils::get_model_weights_pair(models_map, "text_embeddings").second,
m_vlm_config.scale_emb,
device,
device_config
),
m_tokenizer(tokenizer) { }
ov::Tensor InputsEmbedder::IInputsEmbedder::apply_chat_template_tokenize(const std::string& prompt, ov::genai::VLMPerfMetrics& metrics) {
if (m_is_chat_conversation) {
m_history.push_back({{"role", "user"}, {"content", prompt}});
constexpr bool add_generation_prompt = true;
std::string new_templated_chat_history;
new_templated_chat_history = m_tokenizer.apply_chat_template(m_history, add_generation_prompt);
auto start_tokenizer_time = std::chrono::steady_clock::now();
ov::Tensor new_chat_tokens = m_tokenizer.encode(new_templated_chat_history, ov::genai::add_special_tokens(false)).input_ids;
auto end_tokenizer_time = std::chrono::steady_clock::now();
metrics.raw_metrics.tokenization_durations.emplace_back(PerfMetrics::get_microsec(end_tokenizer_time - start_tokenizer_time));
return new_chat_tokens;
} else {
ov::Tensor encoded_input_ids;
auto start_tokenizer_time = std::chrono::steady_clock::now();
if (m_apply_chat_template) {
std::string templated_prompt;
ChatHistory history({{{"role", "user"}, {"content", prompt}}});
constexpr bool add_generation_prompt = true;
templated_prompt = m_tokenizer.apply_chat_template(history, add_generation_prompt);
encoded_input_ids = m_tokenizer.encode(templated_prompt, ov::genai::add_special_tokens(false)).input_ids;
} else {
encoded_input_ids = m_tokenizer.encode(prompt).input_ids;
}
auto end_tokenizer_time = std::chrono::steady_clock::now();
metrics.raw_metrics.tokenization_durations.emplace_back(PerfMetrics::get_microsec(end_tokenizer_time - start_tokenizer_time));
return encoded_input_ids;
}
}
ov::Tensor InputsEmbedder::IInputsEmbedder::update_history(const ov::Tensor& new_chat_tokens) {
ov::Tensor encoded_inputs;
if (m_is_chat_conversation) {
ov::genai::align_kv_cache_and_history(new_chat_tokens, m_kv_cache_state);
encoded_inputs = get_chat_encoded_input(new_chat_tokens, m_kv_cache_state).input_ids;
} else {
encoded_inputs = new_chat_tokens;
}
return encoded_inputs;
}
ov::Tensor InputsEmbedder::IInputsEmbedder::get_encoded_input_ids(const std::string& prompt, ov::genai::VLMPerfMetrics& metrics) {
const auto new_chat_tokens = apply_chat_template_tokenize(prompt, metrics);
auto new_input_ids = update_history(new_chat_tokens);
m_prev_hist_length = m_kv_cache_state.get_state().size();
m_kv_cache_state.add_inputs(new_input_ids);
return new_input_ids;
}
std::vector<ov::Tensor> InputsEmbedder::IInputsEmbedder::to_single_image_tensors(const std::vector<ov::Tensor>& images) {
std::vector<ov::Tensor> single_image_tensors;
for (const auto& image : images) {
ov::Tensor reshaped_image = image;
ov::Shape image_shape = image.get_shape();
switch (image_shape.size()) {
case 3:
reshaped_image.set_shape({1, image_shape.at(0), image_shape.at(1), image_shape.at(2)});
break;
case 4: break;
default: OPENVINO_THROW("Input image must have [NHWC] or [HWC] layout, given image shape is ", image_shape);
}
ov::Shape reshaped_image_shape = reshaped_image.get_shape();
for (size_t batch_idx = 0; batch_idx < reshaped_image_shape.at(0); ++batch_idx) {
ov::Tensor single_image{
reshaped_image.get_element_type(),
{1, reshaped_image_shape.at(1), reshaped_image_shape.at(2), reshaped_image_shape.at(3)},
reshaped_image.data<uint8_t>() + batch_idx * reshaped_image_shape.at(1) * reshaped_image_shape.at(2) * reshaped_image_shape.at(3)
};
single_image_tensors.push_back(std::move(single_image));
}
}
return single_image_tensors;
}
/// Public InputsEmbedder class
InputsEmbedder::InputsEmbedder(const std::filesystem::path& model_dir,
const std::string& device,
const ov::AnyMap device_config) {
auto vlm_config = utils::from_config_json_if_exists<VLMConfig>(model_dir, "config.json");
if (vlm_config.model_type == VLMModelType::MINICPM) {
m_impl = std::make_shared<InputsEmbedderMiniCPM>(vlm_config, model_dir, device, device_config);
} else if (vlm_config.model_type == VLMModelType::LLAVA) {
m_impl = std::make_shared<InputsEmbedderLLaVA>(vlm_config, model_dir, device, device_config);
} else if (vlm_config.model_type == VLMModelType::LLAVA_NEXT) {
m_impl = std::make_shared<InputsEmbedderLLaVANext>(vlm_config, model_dir, device, device_config);
} else if (vlm_config.model_type == VLMModelType::INTERNVL_CHAT) {
m_impl = std::make_shared<InputsEmbedderInternVLChat>(vlm_config, model_dir, device, device_config);
} else if (vlm_config.model_type == VLMModelType::PHI3_V) {
m_impl = std::make_shared<InputsEmbedderPhi3V>(vlm_config, model_dir, device, device_config);
} else if (vlm_config.model_type == VLMModelType::QWEN2_VL) {
m_impl = std::make_shared<InputsEmbedderQwen2VL>(vlm_config, model_dir, device, device_config);
} else {
OPENVINO_THROW("Unsupported model type in VLM InputsEmbedder class. Please, create feature request on new model support");
}
}
InputsEmbedder::InputsEmbedder(const ModelsMap& models_map,
const Tokenizer& tokenizer,
const std::filesystem::path& config_dir_path,
const std::string& device,
const ov::AnyMap device_config) {
auto vlm_config = utils::from_config_json_if_exists<VLMConfig>(config_dir_path, "config.json");
if (vlm_config.model_type == VLMModelType::MINICPM) {
m_impl = std::make_shared<InputsEmbedderMiniCPM>(vlm_config, models_map, tokenizer, config_dir_path, device, device_config);
} else if (vlm_config.model_type == VLMModelType::LLAVA) {
m_impl = std::make_shared<InputsEmbedderLLaVA>(vlm_config, models_map, tokenizer, config_dir_path, device, device_config);
} else if (vlm_config.model_type == VLMModelType::LLAVA_NEXT) {
m_impl = std::make_shared<InputsEmbedderLLaVANext>(vlm_config, models_map, tokenizer, config_dir_path, device, device_config);
} else if (vlm_config.model_type == VLMModelType::INTERNVL_CHAT) {
m_impl = std::make_shared<InputsEmbedderInternVLChat>(vlm_config, models_map, tokenizer, config_dir_path, device, device_config);
// } else if (vlm_config.model_type == VLMModelType::PHI3_V) {
// m_impl = std::make_shared<InputsEmbedderPhi3V>(vlm_config, models_map, tokenizer, config_dir_path, device, device_config);
} else if (vlm_config.model_type == VLMModelType::QWEN2_VL) {
m_impl = std::make_shared<InputsEmbedderQwen2VL>(vlm_config, models_map, tokenizer, config_dir_path, device, device_config);
} else {
OPENVINO_THROW("Unsupported model type in VLM InputsEmbedder class. Please, create feature request on new model support");
}
}
ov::Tensor InputsEmbedder::get_inputs_embeds(const std::string& prompt, const std::vector<ov::Tensor>& images, ov::genai::VLMPerfMetrics& metrics) {
return m_impl->get_inputs_embeds(prompt, images, metrics);
}
std::pair<ov::Tensor, std::optional<int64_t>> InputsEmbedder::get_position_ids(const size_t inputs_embeds_size, const size_t history_size) {
return m_impl->get_position_ids(inputs_embeds_size, history_size);
}
EmbeddingsModel InputsEmbedder::get_embedding_model() const {
return m_impl->get_embedding_model();
}
ov::genai::utils::KVCacheState& InputsEmbedder::get_kv_cache_state() {
return m_impl->get_kv_cache_state();
}
Tokenizer InputsEmbedder::get_tokenizer() const {
return m_impl->get_tokenizer();
}
void InputsEmbedder::start_chat(const std::string& system_message) {
return m_impl->start_chat(system_message);
}
void InputsEmbedder::update_chat_history(const std::string& decoded_results, const ov::genai::GenerationStatus generation_finish_status) {
return m_impl->update_chat_history(decoded_results, generation_finish_status);
}
void InputsEmbedder::set_apply_chat_template_status(bool apply_chat_template) {
return m_impl->set_apply_chat_template_status(apply_chat_template);
}
void InputsEmbedder::finish_chat() {
return m_impl->finish_chat();
}
} // namespace ov::genai