Skip to content

Commit

Permalink
add check that tokenizer is available (#1415)
Browse files Browse the repository at this point in the history
Port #1413 

CVS-158884
  • Loading branch information
pavel-esir authored Dec 20, 2024
1 parent 1f149a6 commit 79b70c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/cpp/src/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,16 @@ class Tokenizer::TokenizerImpl {

void setupTokenizer(const std::pair<std::shared_ptr<ov::Model>, std::shared_ptr<ov::Model>>& models, const ov::AnyMap& properties) {
auto [ov_tokenizer, ov_detokenizer] = models;
OPENVINO_ASSERT(ov_tokenizer || ov_detokenizer, "Neither tokenizer nor detokenzier models were provided");

m_older_than_24_5 = ov_tokenizer->get_rt_info().count("openvino_tokenizers_version") != 1;
auto core = get_core_singleton();
std::string device = "CPU"; // only CPU is supported for now

std::string version_str;
utils::read_rt_info(ov_tokenizer != nullptr ? ov_tokenizer: ov_detokenizer , "openvino_tokenizers_version", version_str);
// Saving IR version was added only in 24.5, so if it's empty, then it's older than 24.5
m_older_than_24_5 = version_str.empty();

if (ov_tokenizer) {
ov::pass::Manager manager;
manager.register_pass<MakeCombineSegmentsSatateful>();
Expand Down Expand Up @@ -207,7 +213,8 @@ class Tokenizer::TokenizerImpl {
if (m_tokenizer) {
// TODO CVS-150630: Empty strings sporadically can fail, therefore use nonempty string for warmup.
encode("non empty string").input_ids;
if (m_detokenizer)
}
if (m_detokenizer) {
decode({1, 33, 199, 42, 42});
}

Expand Down Expand Up @@ -354,6 +361,9 @@ class Tokenizer::TokenizerImpl {
}

TokenizedInputs encode(std::string prompt, const ov::AnyMap& tokenization_params = {}) {
OPENVINO_ASSERT(m_ireq_queue_tokenizer, "Either openvino_tokenizer.xml was not provided or it was not loaded correctly. "
"Tokenizer::encode is not available");

bool add_special_tokens_flag = true;
ov::genai::utils::read_anymap_param(tokenization_params, add_special_tokens.name(), add_special_tokens_flag);

Expand All @@ -370,6 +380,8 @@ class Tokenizer::TokenizerImpl {
}

TokenizedInputs encode(std::vector<std::string>& prompts, const ov::AnyMap& tokenization_params = {}) {
OPENVINO_ASSERT(m_ireq_queue_tokenizer, "Either openvino_tokenizer.xml was not provided or it was not loaded correctly. "
"Tokenizer::encode is not available");
TokenizedInputs unpadded;
{
bool add_special_tokens_flag = true;
Expand Down
3 changes: 2 additions & 1 deletion tests/python_tests/test_generate_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,8 @@ def run_perf_metrics_collection(model_descr, generation_config: Dict, prompt: st
def test_perf_metrics(model_descr, generation_config, prompt):
import time
start_time = time.perf_counter()
perf_metrics = run_perf_metrics_collection(read_model(model_descr), generation_config, prompt)
# To check prefill exclusion we need long initial prompt.
perf_metrics = run_perf_metrics_collection(read_model(model_descr), generation_config, prompt * 1000)
total_time = (time.perf_counter() - start_time) * 1000

# Check that load time is adequate.
Expand Down

0 comments on commit 79b70c4

Please sign in to comment.