Skip to content

Commit d4bb7c1

Browse files
Test add_special_tokens properly (#1586)
- In HF `tiny-random-phi3` add_special_tokens works, while for `Qwen2-0.5B-Instruct` it does not work even in HF. - Use `"katuni4ka/tiny-random-phi3"` instead of `"Qwen/Qwen2-0.5B-Instruct"` for special tokens testing. --------- Co-authored-by: Ilya Lavrenov <ilya.lavrenov@intel.com>
1 parent ca6f5cb commit d4bb7c1

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

tests/python_tests/test_continuous_batching.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_cb_streamer_vs_return_vs_stateful(prompt):
117117
@pytest.mark.parametrize("model_descr", get_chat_models_list())
118118
@pytest.mark.precommit
119119
def test_chat_scenario_vs_stateful(model_descr, generation_config_kwargs: Dict):
120-
model_id, models_path, hf_tokenizer, opt_model, ov_pipe = read_model((model_descr[0], model_descr[1] / '_test_chat'))
120+
model_id, models_path, hf_tokenizer, opt_model, ov_pipe = read_model((model_descr[0], model_descr[1]))
121121
cb_pipe = get_continuous_batching(models_path)
122122

123123
ov_pipe.start_chat()

tests/python_tests/test_llm_pipeline.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def test_chat_scenario(model_descr, generation_config_kwargs: Dict):
129129
chat_history_hf = []
130130
chat_history_ov = []
131131

132-
model_id, path, tokenizer, opt_model, ov_pipe = read_model((model_descr[0], model_descr[1] / '_test_chat'))
132+
model_id, path, tokenizer, opt_model, ov_pipe = read_model((model_descr[0], model_descr[1]))
133133

134134
ov_generation_config = GenerationConfig(**generation_config_kwargs)
135135
hf_generation_config = convert_to_hf(opt_model.generation_config, ov_generation_config)

tests/python_tests/test_tokenizer.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_apply_chat_template(model_tmp_path, chat_config: Tuple[str, Dict]):
192192
@pytest.mark.nightly
193193
def test_set_chat_template():
194194
model_descr = get_chat_models_list()[0]
195-
model_id, path, hf_tokenizer, opt_model, ov_pipe = read_model((model_descr[0], model_descr[1] / '_test_chat'))
195+
model_id, path, hf_tokenizer, opt_model, ov_pipe = read_model((model_descr[0], model_descr[1]))
196196

197197
prompt = "how are you?"
198198
dummy_conversation = [
@@ -223,24 +223,36 @@ def test_set_chat_template():
223223
]
224224
@pytest.mark.precommit
225225
@pytest.mark.nightly
226-
@pytest.mark.parametrize("add_special_tokens", [True, False])
227-
@pytest.mark.parametrize("skip_special_tokens", [True, False])
228226
@pytest.mark.parametrize("prompt", prompts)
229-
def test_encode_decode_with_special_tokens_option(add_special_tokens, skip_special_tokens, prompt):
227+
def test_encode_decode_with_special_tokens_option(prompt):
230228
import numpy as np
231-
model_descr = get_chat_models_list()[0]
232-
model_id, path, hf_tokenizer, model_opt, ov_pipe = read_model((model_descr[0], model_descr[1] / '_test_chat'))
229+
model_descr = get_models_list()[0]
230+
model_id, path, hf_tokenizer, model_opt, ov_pipe = read_model((model_descr[0], model_descr[1]))
233231
ov_tokenzier = ov_pipe.get_tokenizer()
234232

235233
# Calling encode with 'add_special_tokens' will set state flag.
236-
ov_res = ov_tokenzier.encode(prompt, add_special_tokens=add_special_tokens).input_ids.data
237-
hf_res = hf_tokenizer(prompt, return_tensors="np", add_special_tokens=add_special_tokens)["input_ids"]
238-
assert np.all(ov_res == hf_res)
234+
ov_res_add_spec = ov_tokenzier.encode(prompt, add_special_tokens=True).input_ids.data
235+
ov_res_no_spec = ov_tokenzier.encode(prompt, add_special_tokens=False).input_ids.data
236+
hf_res_add_spec = hf_tokenizer(prompt, return_tensors="np", add_special_tokens=True)["input_ids"]
237+
hf_res_no_spec = hf_tokenizer(prompt, return_tensors="np", add_special_tokens=False)["input_ids"]
238+
assert np.all(ov_res_add_spec == hf_res_add_spec)
239+
assert np.all(ov_res_no_spec == hf_res_no_spec)
240+
241+
# Check that add_special_tokens flag indeed made any difference
242+
assert ov_res_add_spec.size != ov_res_no_spec.size
243+
assert hf_res_add_spec.size != hf_res_no_spec.size
239244

240245
# Decode with 'skip_special_tokens'
241-
decoded_genai = ov_tokenzier.decode(ov_res, skip_special_tokens=skip_special_tokens)[0]
242-
decoded_hf = hf_tokenizer.decode(hf_res[0], skip_special_tokens=skip_special_tokens)
243-
assert decoded_genai == decoded_hf
246+
decoded_genai_skip_spec = ov_tokenzier.decode(hf_res_add_spec, skip_special_tokens=True)[0]
247+
decoded_genai_no_skip = ov_tokenzier.decode(hf_res_add_spec, skip_special_tokens=False)[0]
248+
decoded_hf_skip_spec = hf_tokenizer.decode(hf_res_add_spec[0], skip_special_tokens=True)
249+
decoded_hf_no_skip = hf_tokenizer.decode(hf_res_add_spec[0], skip_special_tokens=False)
250+
assert decoded_genai_skip_spec == decoded_hf_skip_spec
251+
assert decoded_genai_no_skip == decoded_hf_no_skip
252+
253+
# Check that skip_special_tokens indeed made any difference
254+
assert decoded_genai_skip_spec != decoded_genai_no_skip
255+
assert decoded_hf_skip_spec != decoded_hf_no_skip
244256

245257

246258
@pytest.mark.precommit

0 commit comments

Comments
 (0)