-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add max_lengh parametrisation to encode #1518
Draft
pavel-esir
wants to merge
1
commit into
openvinotoolkit:master
Choose a base branch
from
pavel-esir:parameterize_encode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+97
−15
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -97,6 +97,8 @@ class Tokenizer::TokenizerImpl { | |||||
// this flag holds the current state value of the CompiledModel. | ||||||
bool m_add_special_tokens = true; | ||||||
bool m_skip_special_tokens = true; | ||||||
int m_max_pad_length = std::numeric_limits<int>::max(); | ||||||
int m_max_trunc_length = std::numeric_limits<int>::max(); | ||||||
bool m_older_than_24_5 = false; | ||||||
|
||||||
int64_t m_pad_token_id = -1; | ||||||
|
@@ -109,17 +111,47 @@ class Tokenizer::TokenizerImpl { | |||||
|
||||||
std::string m_chat_template = {}; | ||||||
|
||||||
std::pair<int, int> get_padding_values(std::string padding_mode, size_t max_length) { | ||||||
if (padding_mode == "truncate") { | ||||||
return {max_length, std::numeric_limits<int32_t>::max()}; | ||||||
} else if (padding_mode == "longest") { | ||||||
return {std::numeric_limits<int32_t>::max(), std::numeric_limits<int32_t>::max()}; | ||||||
} else if (padding_mode == "max_length") { | ||||||
return {std::numeric_limits<int32_t>::max(), max_length}; | ||||||
} else if (padding_mode == "do_not_pad") { | ||||||
// bahves exactly as longest | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// TODO: need to find a way to disable padding automatically so that it will match to HF. | ||||||
return {std::numeric_limits<int32_t>::max(), std::numeric_limits<int32_t>::max()}; | ||||||
} else { | ||||||
OPENVINO_THROW("Unknown padding mode: " + padding_mode); | ||||||
} | ||||||
} | ||||||
|
||||||
void set_state_if_necessary(CircularBufferQueueElementGuard<ov::InferRequest>& infer_request_guard, const ov::AnyMap& params) { | ||||||
bool add_special_tokens_flag = m_add_special_tokens; | ||||||
bool skip_special_tokens_flag = m_skip_special_tokens; | ||||||
|
||||||
size_t max_length_val; | ||||||
std::string padding_mode_val; | ||||||
|
||||||
ov::genai::utils::read_anymap_param(params, add_special_tokens.name(), add_special_tokens_flag); | ||||||
ov::genai::utils::read_anymap_param(params, skip_special_tokens.name(), skip_special_tokens_flag); | ||||||
ov::genai::utils::read_anymap_param(params, padding_mode.name(), padding_mode_val); | ||||||
ov::genai::utils::read_anymap_param(params, max_length.name(), max_length_val); | ||||||
|
||||||
int max_trunc_length_val = m_max_trunc_length; | ||||||
int max_pad_length_val = m_max_pad_length; | ||||||
|
||||||
if (!padding_mode_val.empty()) { | ||||||
std::tie(max_trunc_length_val, max_pad_length_val) = get_padding_values(padding_mode_val, max_length_val); | ||||||
} | ||||||
|
||||||
// If user requested add_special_tokens mode different from the current one, | ||||||
// need to set state variable. | ||||||
// If requested mode matches the stored state set, then don't touch states. | ||||||
if (add_special_tokens_flag == m_add_special_tokens && skip_special_tokens_flag == m_skip_special_tokens) { | ||||||
if (add_special_tokens_flag == m_add_special_tokens | ||||||
&& skip_special_tokens_flag == m_skip_special_tokens | ||||||
&& max_trunc_length_val == m_max_trunc_length | ||||||
&& max_pad_length_val == m_max_pad_length) { | ||||||
return; | ||||||
} | ||||||
if (m_older_than_24_5) { | ||||||
|
@@ -137,15 +169,26 @@ class Tokenizer::TokenizerImpl { | |||||
ov::Tensor skip_special_tensor = ov::Tensor(ov::element::i32, {1}); | ||||||
*skip_special_tensor.data<int>() = skip_special_tokens_flag; | ||||||
|
||||||
ov::Tensor max_trunc_length_tensor = ov::Tensor(ov::element::i32, {1}); | ||||||
*max_trunc_length_tensor.data<int>() = max_trunc_length_val; | ||||||
ov::Tensor max_pad_length_tensor = ov::Tensor(ov::element::i32, {1}); | ||||||
*max_pad_length_tensor.data<int>() = max_pad_length_val; | ||||||
|
||||||
for (auto& state: infer_request_guard.get().query_state()) { | ||||||
if (state.get_name().find(ov::genai::ADD_SPECIAL_TOKENS_VAR_ID) != std::string::npos) { | ||||||
if (state.get_name().find(add_special_tokens.name()) != std::string::npos) { | ||||||
state.set_state(add_special_tensor); | ||||||
} else if (state.get_name().find(ov::genai::SKIP_SPECIAL_TOKENS_VAR_ID) != std::string::npos) { | ||||||
} else if (state.get_name().find(skip_special_tokens.name()) != std::string::npos) { | ||||||
state.set_state(skip_special_tensor); | ||||||
} else if (state.get_name().find("max_trunc_length") != std::string::npos) { | ||||||
state.set_state(max_trunc_length_tensor); | ||||||
} else if (state.get_name().find("max_pad_length") != std::string::npos) { | ||||||
state.set_state(max_pad_length_tensor); | ||||||
} | ||||||
} | ||||||
m_add_special_tokens = add_special_tokens_flag; | ||||||
m_skip_special_tokens = skip_special_tokens_flag; | ||||||
m_max_trunc_length = max_trunc_length_val; | ||||||
m_max_pad_length = max_pad_length_val; | ||||||
} | ||||||
|
||||||
TokenizerImpl(const std::filesystem::path& models_path, const ov::AnyMap& properties) { | ||||||
|
@@ -625,22 +668,22 @@ Tokenizer::Tokenizer(const std::string& model_str, ov::Tensor& weights_tensor, c | |||||
} | ||||||
|
||||||
TokenizedInputs Tokenizer::encode(const std::string prompt, const ov::AnyMap& tokenization_params) { | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name()}); | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name(), ov::genai::max_length.name(), ov::genai::padding_mode.name()}); | ||||||
return m_pimpl->encode(std::move(prompt), tokenization_params); | ||||||
} | ||||||
|
||||||
TokenizedInputs Tokenizer::encode(std::vector<std::string>& prompts, const ov::AnyMap& tokenization_params) { | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name()}); | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name(), ov::genai::max_length.name(), ov::genai::padding_mode.name()}); | ||||||
return m_pimpl->encode(prompts, tokenization_params); | ||||||
} | ||||||
|
||||||
TokenizedInputs Tokenizer::encode(std::vector<std::string>&& prompts, const ov::AnyMap& tokenization_params) { | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name()}); | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name(), ov::genai::max_length.name(), ov::genai::padding_mode.name()}); | ||||||
return m_pimpl->encode(prompts, tokenization_params); | ||||||
} | ||||||
|
||||||
TokenizedInputs Tokenizer::encode(std::initializer_list<std::string>& text, const ov::AnyMap& tokenization_params) { | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name()}); | ||||||
check_arguments(tokenization_params, {ov::genai::add_special_tokens.name(), ov::genai::max_length.name(), ov::genai::padding_mode.name()}); | ||||||
return encode(std::vector<std::string>(text.begin(), text.end()), tokenization_params); | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,6 +236,26 @@ def test_encode_decode_with_special_tokens_option(add_special_tokens, skip_speci | |
decoded_hf = hf_tokenizer.decode(hf_res[0], skip_special_tokens=skip_special_tokens) | ||
assert decoded_genai == decoded_hf | ||
|
||
prompts = [ | ||
['1+1=', 'What is the previous answer?'] | ||
] | ||
@pytest.mark.precommit | ||
@pytest.mark.nightly | ||
@pytest.mark.parametrize("add_special_tokens", [True, False]) | ||
@pytest.mark.parametrize("max_length", [10, 16, 64, 512]) | ||
@pytest.mark.parametrize("pad_mode", ["truncate", "longest", "max_length", "do_not_pad"]) | ||
@pytest.mark.parametrize("prompt", prompts) | ||
def test_padding(add_special_tokens, max_length, pad_mode, prompt): | ||
import numpy as np | ||
model_descr = get_chat_models_list()[0] | ||
model_id, path, hf_tokenizer, model_opt, ov_pipe = read_model((model_descr[0], model_descr[1] / '_test_chat')) | ||
genai_tokenzier = ov_pipe.get_tokenizer() | ||
|
||
# Calling encode with 'add_special_tokens' will set state flag. | ||
ov_res = genai_tokenzier.encode(prompt, add_special_tokens=add_special_tokens, max_length=max_length, padding_mode=pad_mode).input_ids.data | ||
hf_res = hf_tokenizer(prompt, return_tensors="np", add_special_tokens=add_special_tokens, max_length=max_length, padding=pad_mode)["input_ids"] | ||
assert np.all(ov_res == hf_res) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to test attention mask as well? |
||
|
||
|
||
@pytest.mark.precommit | ||
@pytest.mark.nightly | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is string, but not enum?