Skip to content

Commit df7e385

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

File tree

1 file changed

+94
-9
lines changed

1 file changed

+94
-9
lines changed

tests/openvino/test_exporters_cli.py

+94-9
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,105 @@ 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(
426-
tmpdir, trust_remote_code=remote_code
427-
).chat_template
425+
tokenizer = AutoTokenizer.from_pretrained(tmpdir, trust_remote_code=remote_code)
428426
else:
429-
ref_chat_template = AutoProcessor.from_pretrained(
430-
tmpdir, trust_remote_code=remote_code
431-
).chat_template
432-
self.assertEqual(rt_info_chat_template, ref_chat_template)
427+
tokenizer = AutoProcessor.from_pretrained(tmpdir, trust_remote_code=remote_code)
428+
ref_chat_template = tokenizer.chat_template
429+
self.assertEqual(rt_info_chat_template.value, ref_chat_template)
433430
if not model_test_config.get("simplified_chat_template", False):
434431
self.assertFalse(tokenizer_model.has_rt_info("simplified_chat_template"))
435432
else:
436-
simplified_rt_chat_template = tokenizer_model.get_rt_info("simplified_chat_template")
433+
simplified_rt_chat_template = tokenizer_model.get_rt_info("simplified_chat_template").value
437434
self.assertTrue(rt_info_chat_template in COMPLEX_CHAT_TEMPLATES)
438-
self.assertEqual(simplified_rt_chat_template, COMPLEX_CHAT_TEMPLATES[rt_info_chat_template])
435+
self.assertEqual(simplified_rt_chat_template, COMPLEX_CHAT_TEMPLATES[rt_info_chat_template.value])
436+
# there are some difference in content key for conversation templates, simplified templates align to use common
437+
if not "llava" in model_type:
438+
origin_history_messages = [
439+
{
440+
"role": "system",
441+
"content": "You are a friendly chatbot who always responds in the style of a pirate",
442+
},
443+
{"role": "user", "content": "How many helicopters can a human eat in one sitting?"},
444+
{
445+
"role": "assistant",
446+
"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.",
447+
},
448+
{"role": "user", "content": "Why is it not recommended?"},
449+
]
450+
else:
451+
origin_history_messages = [
452+
{
453+
"role": "system",
454+
"content": [
455+
{
456+
"type": "text",
457+
"text": "You are a friendly chatbot who always responds in the style of a pirate",
458+
}
459+
],
460+
},
461+
{
462+
"role": "user",
463+
"content": [
464+
{"type": "text", "text": "How many helicopters can a human eat in one sitting?"}
465+
],
466+
},
467+
{
468+
"role": "assistant",
469+
"content": [
470+
{
471+
"type": "text",
472+
"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.",
473+
}
474+
],
475+
},
476+
{"role": "user", "content": [{"type": "text", "text": "Why is it not recommended?"}]},
477+
]
478+
history_messages = [
479+
{
480+
"role": "system",
481+
"content": "You are a friendly chatbot who always responds in the style of a pirate",
482+
},
483+
{"role": "user", "content": "How many helicopters can a human eat in one sitting?"},
484+
{
485+
"role": "assistant",
486+
"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.",
487+
},
488+
{"role": "user", "content": "Why is it not recommended?"},
489+
]
490+
reference_input_text_no_gen_prompt = tokenizer.apply_chat_template(
491+
origin_history_messages,
492+
add_generation_prompt=False,
493+
chat_template=ref_chat_template,
494+
tokenize=False,
495+
)
496+
simplified_input_text_no_gen_prompt = tokenizer.apply_chat_template(
497+
history_messages,
498+
add_generation_prompt=False,
499+
chat_template=simplified_rt_chat_template,
500+
tokenize=False,
501+
)
502+
self.assertEqual(
503+
reference_input_text_no_gen_prompt,
504+
simplified_input_text_no_gen_prompt,
505+
f"Expected text:\n{reference_input_text_no_gen_prompt}\nSimplified text:\n{simplified_input_text_no_gen_prompt}",
506+
)
507+
reference_input_text_gen_prompt = tokenizer.apply_chat_template(
508+
origin_history_messages,
509+
chat_template=ref_chat_template,
510+
add_generation_prompt=True,
511+
tokenize=False,
512+
)
513+
simplified_input_text_gen_prompt = tokenizer.apply_chat_template(
514+
history_messages,
515+
add_generation_prompt=True,
516+
chat_template=simplified_rt_chat_template,
517+
tokenize=False,
518+
)
519+
self.assertEqual(
520+
reference_input_text_gen_prompt,
521+
simplified_input_text_gen_prompt,
522+
f"Expected text:\n{reference_input_text_gen_prompt}\nSimplified text:\n{simplified_input_text_gen_prompt}",
523+
)
439524

440525
@parameterized.expand(SUPPORTED_ARCHITECTURES)
441526
def test_exporters_cli_fp16(self, task: str, model_type: str):

0 commit comments

Comments
 (0)