-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathservable.cpp
More file actions
296 lines (268 loc) · 15.8 KB
/
servable.cpp
File metadata and controls
296 lines (268 loc) · 15.8 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//*****************************************************************************
// Copyright 2025 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
#include "../../../logging.hpp"
#include "../../../status.hpp"
#pragma warning(push)
#pragma warning(disable : 4005 4309 6001 6385 6386 6326 6011 4005 4456 6246)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include "mediapipe/framework/calculator_graph.h"
#pragma GCC diagnostic pop
#pragma warning(pop)
#include "../../../config.hpp"
#include "../../../http_payload.hpp"
#include "../../../mediapipe_internal/mediapipe_utils.hpp"
#include "../../apis/openai_completions.hpp"
#include "../../text_utils.hpp"
#include "../../../tokenize/tokenize_parser.hpp"
#if (PYTHON_DISABLE == 0)
#include "../../py_jinja_template_processor.hpp"
#endif
#include "servable.hpp"
namespace ovms {
absl::Status VisualLanguageModelLegacyServable::loadRequest(std::shared_ptr<GenAiServableExecutionContext>& executionContext, const ovms::HttpPayload& payload) {
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Request body: {}", payload.body);
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Request uri: {}", payload.uri);
// Parsed JSON is not guaranteed to be valid, we may reach this point via multipart content type request with no valid JSON parser
if (payload.parsedJson->HasParseError()) {
return absl::InvalidArgumentError("Non-json request received in text generation calculator");
}
if (payload.uri == "/v3/chat/completions" || payload.uri == "/v3/v1/chat/completions") {
executionContext->endpoint = Endpoint::CHAT_COMPLETIONS;
} else if (TokenizeParser::isTokenizeEndpoint(payload.uri)) {
executionContext->endpoint = Endpoint::TOKENIZE;
} else {
return absl::InvalidArgumentError("Wrong endpoint. VLM Servable allowed only on /v3/chat/completions endpoint or /v3/tokenize");
}
executionContext->payload = payload;
return absl::OkStatus();
}
// Node resources interface start
std::shared_ptr<GenAiServableExecutionContext> VisualLanguageModelLegacyServable::createExecutionContext() {
return std::make_shared<VisualLanguageModelLegacyServableExecutionContext>();
}
std::shared_ptr<GenAiServableProperties> VisualLanguageModelLegacyServable::getProperties() {
return properties;
}
absl::Status VisualLanguageModelLegacyServable::parseRequest(std::shared_ptr<GenAiServableExecutionContext>& executionContext) {
auto legacyExecutionContext = std::static_pointer_cast<VisualLanguageModelLegacyServableExecutionContext>(executionContext);
if (legacyExecutionContext->payload.client->isDisconnected()) {
return absl::CancelledError();
}
legacyExecutionContext->baseGenerationConfig = properties->baseGenerationConfig;
legacyExecutionContext->apiHandler = std::make_shared<OpenAIChatCompletionsHandler>(*legacyExecutionContext->payload.parsedJson,
legacyExecutionContext->endpoint,
std::chrono::system_clock::now(),
getProperties()->tokenizer,
getProperties()->toolParserName,
getProperties()->reasoningParserName);
auto& config = ovms::Config::instance();
auto status = executionContext->apiHandler->parseRequest(getProperties()->maxTokensLimit, getProperties()->bestOfLimit, getProperties()->maxModelLength, config.getServerSettings().allowedLocalMediaPath, config.getServerSettings().allowedMediaDomains);
if (!status.ok()) {
SPDLOG_LOGGER_ERROR(llm_calculator_logger, "Failed to parse request: {}", status.message());
return status;
}
if (legacyExecutionContext->apiHandler->isStream()) {
legacyExecutionContext->lastStreamerCallbackOutput = ""; // initialize with empty string
}
auto callback = [& executionInProgress = legacyExecutionContext->executionInProgress,
&mutex = legacyExecutionContext->mutex,
&lastStreamerCallbackOutput = legacyExecutionContext->lastStreamerCallbackOutput,
&clientDisconnected = legacyExecutionContext->clientDisconnected,
streamMode = legacyExecutionContext->apiHandler->isStream()](std::string text) {
SPDLOG_LOGGER_TRACE(llm_calculator_logger, "Streamer callback executed with text: [{}]", text);
if (clientDisconnected.load()) {
executionInProgress.notify_one();
return ov::genai::StreamingStatus::CANCEL;
}
if (streamMode) {
std::lock_guard<std::mutex> lock(mutex);
lastStreamerCallbackOutput += text;
executionInProgress.notify_one();
}
return ov::genai::StreamingStatus::RUNNING;
};
ov::AnyMap streamerConfig;
if (legacyExecutionContext->apiHandler->getOutputParser() != nullptr &&
(legacyExecutionContext->apiHandler->getOutputParser()->requiresStreamingWithSpecialTokens())) {
streamerConfig.insert(ov::genai::skip_special_tokens(false));
}
legacyExecutionContext->textStreamer = std::make_shared<ov::genai::TextStreamer>(getProperties()->tokenizer, callback, streamerConfig);
legacyExecutionContext->generationConfigBuilder = std::make_shared<GenerationConfigBuilder>(getProperties()->baseGenerationConfig,
getProperties()->toolParserName,
getProperties()->enableToolGuidedGeneration,
getProperties()->decodingMethod);
legacyExecutionContext->generationConfigBuilder->parseConfigFromRequest(legacyExecutionContext->apiHandler->getRequest());
legacyExecutionContext->generationConfigBuilder->adjustConfigForDecodingMethod();
try {
legacyExecutionContext->generationConfigBuilder->validateStructuredOutputConfig(getProperties()->tokenizer);
} catch (const std::exception& e) {
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Tool guided generation will not be applied due to JSON schema validation failure: {}", e.what());
legacyExecutionContext->generationConfigBuilder->unsetStructuredOutputConfig();
}
auto adapterStatus = applyLoraAdapter(executionContext);
if (!adapterStatus.ok()) {
return adapterStatus;
}
return absl::OkStatus();
}
absl::Status VisualLanguageModelLegacyServable::scheduleExecution(std::shared_ptr<GenAiServableExecutionContext>& executionContext) {
auto legacyExecutionContext = std::static_pointer_cast<VisualLanguageModelLegacyServableExecutionContext>(executionContext);
std::weak_ptr<VisualLanguageModelLegacyServableExecutionContext> weakContext = legacyExecutionContext;
legacyExecutionContext->payload.client->registerDisconnectionCallback([weakContext]() {
if (auto context = weakContext.lock()) {
context->signalDisconnection();
}
});
if (legacyExecutionContext->payload.client->isDisconnected()) {
legacyExecutionContext->signalDisconnection();
return absl::CancelledError();
}
properties->legacyExecutor->addRequest(legacyExecutionContext);
return absl::OkStatus();
}
absl::Status VisualLanguageModelLegacyServable::readCompleteExecutionResults(std::shared_ptr<GenAiServableExecutionContext>& executionContext) {
auto legacyExecutionContext = std::static_pointer_cast<VisualLanguageModelLegacyServableExecutionContext>(executionContext);
if (legacyExecutionContext->payload.client->isDisconnected()) {
return absl::CancelledError();
}
legacyExecutionContext->finished.wait();
if (!legacyExecutionContext->success) {
return absl::InvalidArgumentError("Request processing failed, check its correctness.");
}
return absl::OkStatus();
}
absl::Status VisualLanguageModelLegacyServable::prepareCompleteResponse(std::shared_ptr<GenAiServableExecutionContext>& executionContext) {
auto legacyExecutionContext = std::static_pointer_cast<VisualLanguageModelLegacyServableExecutionContext>(executionContext);
if (legacyExecutionContext->payload.client->isDisconnected()) {
return absl::CancelledError();
}
executionContext->response = executionContext->apiHandler->serializeUnaryResponse(legacyExecutionContext->results);
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Complete unary response: {}", executionContext->response);
return absl::OkStatus();
}
absl::Status VisualLanguageModelLegacyServable::readPartialExecutionResults(std::shared_ptr<GenAiServableExecutionContext>& executionContext) {
return absl::OkStatus();
}
absl::Status VisualLanguageModelLegacyServable::preparePartialResponse(std::shared_ptr<GenAiServableExecutionContext>& executionContext) {
auto legacyExecutionContext = std::static_pointer_cast<VisualLanguageModelLegacyServableExecutionContext>(executionContext);
if (legacyExecutionContext->payload.client->isDisconnected()) {
return absl::CancelledError();
}
std::string lastTextChunk;
auto generationStatus = legacyExecutionContext->finished.wait_for(std::chrono::nanoseconds::zero());
{
std::unique_lock lock(legacyExecutionContext->mutex);
while (executionContext->lastStreamerCallbackOutput.size() == 0 && generationStatus != std::future_status::ready) {
SPDLOG_LOGGER_TRACE(llm_executor_logger, "Waiting for partial data...");
auto cvStatus = legacyExecutionContext->executionInProgress.wait_for(lock, std::chrono::milliseconds(10));
generationStatus = legacyExecutionContext->finished.wait_for(std::chrono::nanoseconds::zero());
if (cvStatus == std::cv_status::timeout && generationStatus == std::future_status::ready) {
SPDLOG_LOGGER_TRACE(llm_executor_logger, "Race condition avoided - notification was missed but recovered with timeout");
}
}
lastTextChunk = executionContext->lastStreamerCallbackOutput;
executionContext->lastStreamerCallbackOutput = "";
}
if (generationStatus != std::future_status::ready) { // continue
if (lastTextChunk.size() > 0) {
std::string serializedChunk = executionContext->apiHandler->serializeStreamingChunk(lastTextChunk, ov::genai::GenerationFinishReason::NONE);
if (!serializedChunk.empty()) {
executionContext->response = wrapTextInServerSideEventMessage(serializedChunk);
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Generated subsequent streaming response: {}", executionContext->response);
}
}
executionContext->sendLoopbackSignal = true;
} else { // finish generation
if (!legacyExecutionContext->success) {
return absl::InvalidArgumentError("Request processing failed, check its correctness.");
}
OVMS_PROFILE_SCOPE("Generation of last streaming response");
executionContext->textStreamer->end();
// if streamer::put returned a value, streamer::end() result will not contain it, so we add it manually
if (!executionContext->lastStreamerCallbackOutput.empty()) {
lastTextChunk = lastTextChunk + executionContext->lastStreamerCallbackOutput;
}
std::string serializedChunk = executionContext->apiHandler->serializeStreamingChunk(lastTextChunk, ov::genai::GenerationFinishReason::STOP);
if (!serializedChunk.empty()) {
executionContext->response = wrapTextInServerSideEventMessage(serializedChunk);
}
executionContext->apiHandler->setPromptTokensUsage(legacyExecutionContext->results.perf_metrics.get_num_input_tokens());
executionContext->apiHandler->setCompletionTokensUsage(legacyExecutionContext->results.perf_metrics.get_num_generated_tokens());
if (executionContext->apiHandler->getStreamOptions().includeUsage)
executionContext->response += wrapTextInServerSideEventMessage(executionContext->apiHandler->serializeStreamingUsageChunk());
executionContext->response += wrapTextInServerSideEventMessage("[DONE]");
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Generated complete streaming response: {}", lastTextChunk);
executionContext->sendLoopbackSignal = false;
return absl::OkStatus();
}
return absl::OkStatus();
}
absl::Status VisualLanguageModelLegacyServable::prepareInputs(std::shared_ptr<GenAiServableExecutionContext>& executionContext) {
auto vlmExecutionContext = std::static_pointer_cast<VisualLanguageModelLegacyServableExecutionContext>(executionContext);
if (vlmExecutionContext->apiHandler == nullptr) {
return absl::Status(absl::StatusCode::kInvalidArgument, "API handler is not initialized");
}
if (executionContext->endpoint == Endpoint::CHAT_COMPLETIONS) {
ov::genai::ChatHistory& chatHistory = vlmExecutionContext->apiHandler->getChatHistory();
for (size_t i = 0; i < chatHistory.size(); i++) {
const auto& message = chatHistory[i];
if (message["content"].as_string().value_or("").find("<ov_genai_image_") != std::string::npos) {
return absl::InvalidArgumentError("Message contains restricted <ov_genai_image> tag");
}
}
const ImageHistory& imageHistory = vlmExecutionContext->apiHandler->getImageHistory();
size_t imageIndex = 0;
std::unordered_map<size_t, std::string> imageTags;
for (const auto& image : imageHistory) {
const auto& [chatTurnIndex, imageTensor] = image;
std::string imageTag = "<ov_genai_image_" + std::to_string(imageIndex++) + ">\n";
imageTags[chatTurnIndex] = imageTags[chatTurnIndex] + imageTag;
vlmExecutionContext->inputImages.push_back(imageTensor);
}
for (const auto& [chatTurnIndex, imageTagString] : imageTags) {
std::string messageContent = chatHistory[chatTurnIndex]["content"].as_string().value_or("");
chatHistory[chatTurnIndex]["content"] = imageTagString + messageContent;
}
constexpr bool add_generation_prompt = true; // confirm it should be hardcoded
auto toolsStatus = vlmExecutionContext->apiHandler->parseToolsToJsonContainer();
if (!toolsStatus.ok()) {
return toolsStatus.status();
}
const auto& tools = toolsStatus.value();
auto chatTemplateKwargsStatus = vlmExecutionContext->apiHandler->parseChatTemplateKwargsToJsonContainer();
if (!chatTemplateKwargsStatus.ok()) {
return chatTemplateKwargsStatus.status();
}
const auto& chatTemplateKwargs = chatTemplateKwargsStatus.value();
vlmExecutionContext->inputText = properties->tokenizer.apply_chat_template(chatHistory, add_generation_prompt, {}, tools, chatTemplateKwargs);
} else {
return absl::InvalidArgumentError("Unsupported endpoint");
}
// Below logic is used only for the statistics and debugging purposes and does not affect the model execution.
SPDLOG_LOGGER_TRACE(llm_calculator_logger, "VLM input text: {}", vlmExecutionContext->inputText);
bool encodeAddSpecialTokens = false; // assuming chat template application added special tokens
ov::Tensor inputTextIds = getProperties()->tokenizer.encode(vlmExecutionContext->inputText, ov::genai::add_special_tokens(encodeAddSpecialTokens)).input_ids;
vlmExecutionContext->apiHandler->setPromptTokensUsage(inputTextIds.get_size());
SPDLOG_LOGGER_TRACE(llm_calculator_logger, "{}", getPromptTokensString(inputTextIds));
return absl::OkStatus();
}
} // namespace ovms