|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 HuggingFace Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import json |
| 17 | +import shutil |
| 18 | +import tempfile |
| 19 | +import unittest |
| 20 | + |
| 21 | +from transformers.testing_utils import require_av, require_torch, require_vision |
| 22 | +from transformers.utils import is_torch_available, is_vision_available |
| 23 | + |
| 24 | +from ...test_processing_common import ProcessorTesterMixin |
| 25 | + |
| 26 | + |
| 27 | +if is_vision_available(): |
| 28 | + from transformers import ( |
| 29 | + AutoProcessor, |
| 30 | + LlamaTokenizerFast, |
| 31 | + LlavaNextImageProcessor, |
| 32 | + LlavaNextVideoImageProcessor, |
| 33 | + LlavaNextVideoProcessor, |
| 34 | + ) |
| 35 | + |
| 36 | +if is_torch_available: |
| 37 | + import torch |
| 38 | + |
| 39 | + |
| 40 | +@require_vision |
| 41 | +class LlavaNextVideoProcessorTest(ProcessorTesterMixin, unittest.TestCase): |
| 42 | + processor_class = LlavaNextVideoProcessor |
| 43 | + |
| 44 | + def setUp(self): |
| 45 | + self.tmpdirname = tempfile.mkdtemp() |
| 46 | + image_processor = LlavaNextImageProcessor() |
| 47 | + video_processor = LlavaNextVideoImageProcessor() |
| 48 | + tokenizer = LlamaTokenizerFast.from_pretrained("llava-hf/LLaVA-NeXT-Video-7B-hf") |
| 49 | + processor_kwargs = self.prepare_processor_dict() |
| 50 | + |
| 51 | + processor = LlavaNextVideoProcessor( |
| 52 | + video_processor=video_processor, image_processor=image_processor, tokenizer=tokenizer, **processor_kwargs |
| 53 | + ) |
| 54 | + processor.save_pretrained(self.tmpdirname) |
| 55 | + |
| 56 | + def get_tokenizer(self, **kwargs): |
| 57 | + return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer |
| 58 | + |
| 59 | + def get_image_processor(self, **kwargs): |
| 60 | + return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor |
| 61 | + |
| 62 | + def get_video_processor(self, **kwargs): |
| 63 | + return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).video_processor |
| 64 | + |
| 65 | + def prepare_processor_dict(self): |
| 66 | + return { |
| 67 | + "chat_template": "dummy_template", |
| 68 | + "num_additional_image_tokens": 6, |
| 69 | + "vision_feature_select_strategy": "default", |
| 70 | + } |
| 71 | + |
| 72 | + def test_processor_to_json_string(self): |
| 73 | + processor = self.get_processor() |
| 74 | + obj = json.loads(processor.to_json_string()) |
| 75 | + print(processor) |
| 76 | + for key, value in self.prepare_processor_dict().items(): |
| 77 | + # chat_tempalate are tested as a separate test because they are saved in separate files |
| 78 | + if key != "chat_template": |
| 79 | + self.assertEqual(obj[key], value) |
| 80 | + self.assertEqual(getattr(processor, key, None), value) |
| 81 | + |
| 82 | + # Copied from tests.models.llava.test_processor_llava.LlavaProcessorTest.test_chat_template_is_saved |
| 83 | + def test_chat_template_is_saved(self): |
| 84 | + processor_loaded = self.processor_class.from_pretrained(self.tmpdirname) |
| 85 | + processor_dict_loaded = json.loads(processor_loaded.to_json_string()) |
| 86 | + # chat templates aren't serialized to json in processors |
| 87 | + self.assertFalse("chat_template" in processor_dict_loaded.keys()) |
| 88 | + |
| 89 | + # they have to be saved as separate file and loaded back from that file |
| 90 | + # so we check if the same template is loaded |
| 91 | + processor_dict = self.prepare_processor_dict() |
| 92 | + self.assertTrue(processor_loaded.chat_template == processor_dict.get("chat_template", None)) |
| 93 | + |
| 94 | + def tearDown(self): |
| 95 | + shutil.rmtree(self.tmpdirname) |
| 96 | + |
| 97 | + def test_chat_template(self): |
| 98 | + processor = AutoProcessor.from_pretrained("llava-hf/LLaVA-NeXT-Video-7B-hf") |
| 99 | + expected_prompt = "USER: <image>\nWhat is shown in this image? ASSISTANT:" |
| 100 | + |
| 101 | + messages = [ |
| 102 | + { |
| 103 | + "role": "user", |
| 104 | + "content": [ |
| 105 | + {"type": "image"}, |
| 106 | + {"type": "text", "text": "What is shown in this image?"}, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ] |
| 110 | + |
| 111 | + formatted_prompt = processor.apply_chat_template(messages, add_generation_prompt=True) |
| 112 | + self.assertEqual(expected_prompt, formatted_prompt) |
| 113 | + |
| 114 | + @require_av |
| 115 | + def test_chat_template_dict(self): |
| 116 | + processor = AutoProcessor.from_pretrained("llava-hf/LLaVA-NeXT-Video-7B-hf") |
| 117 | + messages = [ |
| 118 | + { |
| 119 | + "role": "user", |
| 120 | + "content": [ |
| 121 | + {"type": "video"}, |
| 122 | + {"type": "text", "text": "What is shown in this video?"}, |
| 123 | + ], |
| 124 | + }, |
| 125 | + ] |
| 126 | + |
| 127 | + formatted_prompt_tokenized = processor.apply_chat_template( |
| 128 | + messages, add_generation_prompt=True, tokenize=True, return_tensors=None |
| 129 | + ) |
| 130 | + expected_output = [[1, 3148, 1001, 29901, 29871, 32000, 13, 5618, 338, 4318, 297, 445, 4863, 29973, 319, 1799, 9047, 13566, 29901]] # fmt: skip |
| 131 | + self.assertListEqual(expected_output, formatted_prompt_tokenized) |
| 132 | + |
| 133 | + out_dict = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True) |
| 134 | + self.assertListEqual(list(out_dict.keys()), ["input_ids", "attention_mask"]) |
| 135 | + |
| 136 | + # add image URL for return dict |
| 137 | + messages[0]["content"][0] = { |
| 138 | + "type": "video", |
| 139 | + "url": "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/720/Big_Buck_Bunny_720_10s_10MB.mp4", |
| 140 | + } |
| 141 | + out_dict_with_video = processor.apply_chat_template( |
| 142 | + messages, add_generation_prompt=True, tokenize=True, return_dict=True |
| 143 | + ) |
| 144 | + self.assertListEqual(list(out_dict_with_video.keys()), ["input_ids", "attention_mask", "pixel_values_videos"]) |
| 145 | + |
| 146 | + @require_torch |
| 147 | + @require_av |
| 148 | + def test_chat_template_dict_torch(self): |
| 149 | + processor = AutoProcessor.from_pretrained("llava-hf/LLaVA-NeXT-Video-7B-hf") |
| 150 | + messages = [ |
| 151 | + { |
| 152 | + "role": "user", |
| 153 | + "content": [ |
| 154 | + { |
| 155 | + "type": "video", |
| 156 | + "url": "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/720/Big_Buck_Bunny_720_10s_10MB.mp4", |
| 157 | + }, |
| 158 | + {"type": "text", "text": "What is shown in this video?"}, |
| 159 | + ], |
| 160 | + }, |
| 161 | + ] |
| 162 | + |
| 163 | + out_dict_tensors = processor.apply_chat_template( |
| 164 | + messages, |
| 165 | + add_generation_prompt=True, |
| 166 | + tokenize=True, |
| 167 | + return_dict=True, |
| 168 | + return_tensors="pt", |
| 169 | + ) |
| 170 | + self.assertListEqual(list(out_dict_tensors.keys()), ["input_ids", "attention_mask", "pixel_values_videos"]) |
| 171 | + self.assertTrue(isinstance(out_dict_tensors["input_ids"], torch.Tensor)) |
0 commit comments