Skip to content

Commit 56823cd

Browse files
committed
add comparing templated strings
1 parent 910d7e6 commit 56823cd

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

tests/openvino/test_exporters_cli.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,33 @@ def test_exporters_cli_tokenizers_chat_template(self, model_type):
422422
else:
423423
rt_info_chat_template = tokenizer_model.get_rt_info("chat_template")
424424
if not model_test_config.get("processor_chat_template"):
425-
ref_chat_template = AutoTokenizer.from_pretrained(
425+
tokenizer = AutoTokenizer.from_pretrained(
426426
tmpdir, trust_remote_code=remote_code
427-
).chat_template
427+
)
428428
else:
429-
ref_chat_template = AutoProcessor.from_pretrained(
429+
tokenizer = AutoProcessor.from_pretrained(
430430
tmpdir, trust_remote_code=remote_code
431-
).chat_template
432-
self.assertEqual(rt_info_chat_template, ref_chat_template)
431+
)
432+
ref_chat_template = tokenizer.chat_template
433+
self.assertEqual(rt_info_chat_template.value, ref_chat_template)
433434
if not model_test_config.get("simplified_chat_template", False):
434435
self.assertFalse(tokenizer_model.has_rt_info("simplified_chat_template"))
435436
else:
436-
simplified_rt_chat_template = tokenizer_model.get_rt_info("simplified_chat_template")
437+
simplified_rt_chat_template = tokenizer_model.get_rt_info("simplified_chat_template").value
437438
self.assertTrue(rt_info_chat_template in COMPLEX_CHAT_TEMPLATES)
438-
self.assertEqual(simplified_rt_chat_template, COMPLEX_CHAT_TEMPLATES[rt_info_chat_template])
439+
self.assertEqual(simplified_rt_chat_template, COMPLEX_CHAT_TEMPLATES[rt_info_chat_template.value])
440+
# there are some difference in content key for conversation templates, simplified templates align to use common
441+
if not "llava" in model_type:
442+
origin_history_messages = [{"role": "system", "content": "You are a friendly chatbot who always responds in the style of a pirate"}, {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, {"role": "assistant", "content": " There is no specific limit for how many helicopters a human can eat in one sitting, but it is not recommended to consume large quantities of helicopters."}, {"role": "user", "content": "Why is it not recommended?"}]
443+
else:
444+
origin_history_messages = [{"role": "system", "content": [{"type": "text", "text": "You are a friendly chatbot who always responds in the style of a pirate"}]}, {"role": "user", "content": [{"type": "text", "text": "How many helicopters can a human eat in one sitting?"}]}, {"role": "assistant", "content": [{"type": "text", "text": "There is no specific limit for how many helicopters a human can eat in one sitting, but it is not recommended to consume large quantities of helicopters."}]}, {"role": "user", "content": [{"type": "text", "text": "Why is it not recommended?"}]}]
445+
history_messages = [{"role": "system", "content": "You are a friendly chatbot who always responds in the style of a pirate"}, {"role": "user", "content": "How many helicopters can a human eat in one sitting?"}, {"role": "assistant", "content": "There is no specific limit for how many helicopters a human can eat in one sitting, but it is not recommended to consume large quantities of helicopters."}, {"role": "user", "content": "Why is it not recommended?"}]
446+
reference_input_text_no_gen_prompt = tokenizer.apply_chat_template(origin_history_messages, add_generation_prompt=False, chat_template=ref_chat_template, tokenize=False)
447+
simplified_input_text_no_gen_prompt = tokenizer.apply_chat_template(history_messages, add_generation_prompt=False, chat_template=simplified_rt_chat_template, tokenize=False)
448+
self.assertEqual(reference_input_text_no_gen_prompt, simplified_input_text_no_gen_prompt, f"Expected text:\n{reference_input_text_no_gen_prompt}\nSimplified text:\n{simplified_input_text_no_gen_prompt}")
449+
reference_input_text_gen_prompt = tokenizer.apply_chat_template(origin_history_messages, chat_template=ref_chat_template, add_generation_prompt=True, tokenize=False)
450+
simplified_input_text_gen_prompt = tokenizer.apply_chat_template(history_messages, add_generation_prompt=True, chat_template=simplified_rt_chat_template, tokenize=False)
451+
self.assertEqual(reference_input_text_gen_prompt, simplified_input_text_gen_prompt, f"Expected text:\n{reference_input_text_gen_prompt}\nSimplified text:\n{simplified_input_text_gen_prompt}")
439452

440453
@parameterized.expand(SUPPORTED_ARCHITECTURES)
441454
def test_exporters_cli_fp16(self, task: str, model_type: str):

0 commit comments

Comments
 (0)