Skip to content

Commit c277959

Browse files
authored
[Janus] Fix processor iterating a string prompt character by character (#50044)
* Fix JanusProcessor iterating a str text input character by character Normalize a single string to a one-element list before adding the default system prompt or the image-generation suffix, and add a regression test. * Remove Janus processor test per review * Show full assertion diffs in Janus integration tests * Refresh Janus text expectations for synthetic images
1 parent ca68c9e commit c277959

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎src/transformers/models/janus/processing_janus.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def __call__(
100100
)
101101

102102
generation_mode = output_kwargs["text_kwargs"].pop("generation_mode")
103+
if isinstance(text, str):
104+
text = [text]
105+
103106
if self.use_default_system_prompt and generation_mode == "text":
104107
text = [f"{DEFAULT_SYSTEM_PROMPT}{sample}" for sample in text]
105108
elif generation_mode == "image":

‎tests/models/janus/test_modeling_janus.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ def test_gradient_checkpointing_enable_disable(self):
414414

415415

416416
class JanusIntegrationTest(unittest.TestCase):
417+
maxDiff = None
418+
417419
def setUp(self):
418420
self.model_id = "deepseek-community/Janus-Pro-1B"
419421

@@ -433,7 +435,7 @@ def test_model_text_generation(self):
433435
inputs = processor(images=image, text=prompt, generation_mode="text", return_tensors="pt").to(model.device)
434436

435437
output = model.generate(**inputs, max_new_tokens=20, generation_mode="text", do_sample=False)
436-
EXPECTED_DECODED_TEXT = 'You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.\n\n\nDescribe what do you see here and tell me about the history behind it?\n\nThe image depicts the constellation of Leo, which is part of the zodiac and the constellation' # fmt: skip
438+
EXPECTED_DECODED_TEXT = 'You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.\n\n\nDescribe what do you see here and tell me about the history behind it?\n\nThis image shows a night sky filled with stars, and there are silhouettes of trees at' # fmt: skip
437439
text = processor.decode(output[0], skip_special_tokens=True)
438440
self.assertEqual(
439441
text,
@@ -504,7 +506,7 @@ def test_model_text_generation_with_multi_image(self):
504506
model.device, torch.float16
505507
)
506508

507-
EXPECTED_TEXT_COMPLETION = ["You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.\n\nWhat do these two images and have in common?\n\nThe two images you provided are of the same star constellation. The first image shows the constellation of Leo, and the second image shows the constellation of Ursa Major. Both constellations are part"] # fmt: skip
509+
EXPECTED_TEXT_COMPLETION = ["You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.\n\nWhat do these two images and have in common?\n\nThe image you provided shows a night sky with stars and a constellation, while the second image shows a night sky with stars and a different constellation. Both images feature stars and a dark sky,"] # fmt: skip
508510
generated_ids = model.generate(**inputs, max_new_tokens=40, do_sample=False)
509511
text = processor.batch_decode(generated_ids, skip_special_tokens=True)
510512
self.assertEqual(EXPECTED_TEXT_COMPLETION, text)

0 commit comments

Comments
 (0)