From 4d0de5f73a0715ac9a63de5fa461af48d3abbb10 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 8 Apr 2025 17:15:37 +0100 Subject: [PATCH] :rotating_light: :rotating_light: Setup -> setupclass conversion (#37282) * More limited setup -> setupclass conversion * make fixup * Trigger tests * Fixup UDOP * Missed a spot * tearDown -> tearDownClass where appropriate * Couple more class fixes * Fixups for UDOP and VisionTextDualEncoder * Ignore errors when removing the tmpdir, in case it already got cleaned up somewhere * CLIP fixes * More correct classmethods * Wav2Vec2Bert fixes * More methods become static * More class methods * More class methods * Revert changes for integration tests / modeling files * Use a different tempdir for tests that actually write to it * Remove addClassCleanup and just use teardownclass * Remove changes in modeling files * Cleanup get_processor_dict() for got_ocr2 * Fix regression on Wav2Vec2BERT test that was masked by this before * Rework tests that modify the tmpdir * make fix-copies * revert clvp modeling test changes * Fix CLIP processor test * make fix-copies --- .../models/altclip/test_processor_altclip.py | 13 ++-- tests/models/aria/test_processor_aria.py | 2 +- .../aya_vision/test_processor_aya_vision.py | 17 ++-- tests/models/blip/test_processor_blip.py | 27 ++++--- tests/models/blip_2/test_processor_blip_2.py | 27 ++++--- .../bridgetower/test_processor_bridgetower.py | 12 +-- .../chameleon/test_processor_chameleon.py | 9 ++- .../test_processor_chinese_clip.py | 69 +++++++++------- tests/models/clip/test_processor_clip.py | 69 +++++++++------- .../models/colpali/test_processing_colpali.py | 12 +-- tests/models/donut/test_processor_donut.py | 11 +-- tests/models/emu3/test_processor_emu3.py | 9 ++- tests/models/fuyu/test_processor_fuyu.py | 6 +- tests/models/gemma3/test_processing_gemma3.py | 17 ++-- tests/models/git/test_processor_git.py | 27 ++++--- .../got_ocr2/test_processor_got_ocr2.py | 14 ++-- .../test_processor_grounding_dino.py | 78 +++++++++++-------- .../models/idefics/test_processor_idefics.py | 29 +++---- .../idefics2/test_processor_idefics2.py | 32 ++++---- .../idefics3/test_processor_idefics3.py | 2 +- .../test_processor_instructblip.py | 25 +++--- .../test_processor_instructblipvideo.py | 25 +++--- .../models/kosmos2/test_processor_kosmos2.py | 27 ++++--- .../layoutxlm/test_processor_layoutxlm.py | 74 ++++++++++-------- tests/models/llava/test_processor_llava.py | 17 ++-- .../llava_next/test_processor_llava_next.py | 12 +-- .../test_processor_llava_next_video.py | 17 ++-- .../test_processor_llava_onevision.py | 5 +- tests/models/mllama/test_processor_mllama.py | 30 +++---- tests/models/myt5/test_tokenization_myt5.py | 5 +- .../omdet_turbo/test_processor_omdet_turbo.py | 35 +++++---- tests/models/owlv2/test_processor_owlv2.py | 14 ++-- .../paligemma/test_processor_paligemma.py | 12 +-- .../pix2struct/test_processor_pix2struct.py | 27 ++++--- .../pop2piano/test_processor_pop2piano.py | 51 ++++++------ .../qwen2_5_vl/test_processor_qwen2_5_vl.py | 12 +-- .../qwen2_audio/test_processor_qwen2_audio.py | 26 ++++--- .../qwen2_vl/test_processor_qwen2_vl.py | 12 +-- tests/models/sam/test_processor_sam.py | 21 ++--- .../test_processing_shieldgemma2.py | 17 ++-- .../models/smolvlm/test_processor_smolvlm.py | 2 +- .../test_processor_speech_to_text.py | 37 +++++---- .../speecht5/test_processor_speecht5.py | 35 +++++---- tests/models/trocr/test_processor_trocr.py | 42 +++++----- tests/models/udop/test_processor_udop.py | 73 +++++++++-------- ...test_processor_vision_text_dual_encoder.py | 61 ++++++++------- .../wav2vec2/test_processor_wav2vec2.py | 57 ++++++++------ .../test_processor_wav2vec2_bert.py | 59 ++++++++------ tests/test_tokenization_common.py | 2 +- 49 files changed, 740 insertions(+), 574 deletions(-) diff --git a/tests/models/altclip/test_processor_altclip.py b/tests/models/altclip/test_processor_altclip.py index f9ac0398e1..f498ce4aa8 100644 --- a/tests/models/altclip/test_processor_altclip.py +++ b/tests/models/altclip/test_processor_altclip.py @@ -26,15 +26,16 @@ from ...test_processing_common import ProcessorTesterMixin class AltClipProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = AltCLIPProcessor - def setUp(self): - self.model_id = "BAAI/AltCLIP" - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.model_id = "BAAI/AltCLIP" + cls.tmpdirname = tempfile.mkdtemp() image_processor = CLIPImageProcessor() - tokenizer = XLMRobertaTokenizer.from_pretrained(self.model_id) + tokenizer = XLMRobertaTokenizer.from_pretrained(cls.model_id) - processor = self.processor_class(image_processor, tokenizer) + processor = cls.processor_class(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return XLMRobertaTokenizer.from_pretrained(self.model_id, **kwargs) diff --git a/tests/models/aria/test_processor_aria.py b/tests/models/aria/test_processor_aria.py index 57833e7a6a..836563e109 100644 --- a/tests/models/aria/test_processor_aria.py +++ b/tests/models/aria/test_processor_aria.py @@ -87,7 +87,7 @@ class AriaProcessorTest(ProcessorTesterMixin, unittest.TestCase): @classmethod def tearDownClass(cls): - shutil.rmtree(cls.tmpdirname) + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_kwargs_overrides_default_image_processor_kwargs(self): if "image_processor" not in self.processor_class.attributes: diff --git a/tests/models/aya_vision/test_processor_aya_vision.py b/tests/models/aya_vision/test_processor_aya_vision.py index 93632884f7..7e6e080b71 100644 --- a/tests/models/aya_vision/test_processor_aya_vision.py +++ b/tests/models/aya_vision/test_processor_aya_vision.py @@ -37,8 +37,9 @@ if is_vision_available(): class AyaVisionProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = AyaVisionProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = GotOcr2ImageProcessor( do_resize=True, @@ -52,16 +53,17 @@ class AyaVisionProcessorTest(ProcessorTesterMixin, unittest.TestCase): do_convert_rgb=True, ) tokenizer = AutoTokenizer.from_pretrained("CohereForAI/aya-vision-8b", padding_side="left") - processor_kwargs = self.prepare_processor_dict() + processor_kwargs = cls.prepare_processor_dict() processor = AyaVisionProcessor.from_pretrained( "CohereForAI/aya-vision-8b", image_processor=image_processor, tokenizer=tokenizer, **processor_kwargs, ) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - def prepare_processor_dict(self): + @staticmethod + def prepare_processor_dict(): return {"patch_size": 10, "img_size": 20} def get_tokenizer(self, **kwargs): @@ -73,8 +75,9 @@ class AyaVisionProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs) - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) # todo: yoni, fix this test @unittest.skip("Chat template has long system prompt") diff --git a/tests/models/blip/test_processor_blip.py b/tests/models/blip/test_processor_blip.py index aa63855da4..6ff81e991a 100644 --- a/tests/models/blip/test_processor_blip.py +++ b/tests/models/blip/test_processor_blip.py @@ -31,15 +31,16 @@ if is_vision_available(): class BlipProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = BlipProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = BlipImageProcessor() tokenizer = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel") processor = BlipProcessor(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -47,19 +48,21 @@ class BlipProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_save_load_pretrained_additional_features(self): - processor = BlipProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = BlipProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) - processor = BlipProcessor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = BlipProcessor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, PreTrainedTokenizerFast) diff --git a/tests/models/blip_2/test_processor_blip_2.py b/tests/models/blip_2/test_processor_blip_2.py index 7eb5bedc2b..5d125dc57e 100644 --- a/tests/models/blip_2/test_processor_blip_2.py +++ b/tests/models/blip_2/test_processor_blip_2.py @@ -31,15 +31,16 @@ if is_vision_available(): class Blip2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = Blip2Processor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = BlipImageProcessor() tokenizer = GPT2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model") processor = Blip2Processor(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -47,19 +48,21 @@ class Blip2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_save_load_pretrained_additional_features(self): - processor = Blip2Processor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = Blip2Processor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) - processor = Blip2Processor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = Blip2Processor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, PreTrainedTokenizerFast) diff --git a/tests/models/bridgetower/test_processor_bridgetower.py b/tests/models/bridgetower/test_processor_bridgetower.py index 2ccfde803e..4b3bb0716f 100644 --- a/tests/models/bridgetower/test_processor_bridgetower.py +++ b/tests/models/bridgetower/test_processor_bridgetower.py @@ -34,15 +34,16 @@ if is_vision_available(): class BridgeTowerProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = BridgeTowerProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = BridgeTowerImageProcessor() tokenizer = RobertaTokenizerFast.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-itc") processor = BridgeTowerProcessor(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -50,8 +51,9 @@ class BridgeTowerProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) # Some kwargs tests are overriden from common tests to handle shortest_edge # and size_divisor behaviour diff --git a/tests/models/chameleon/test_processor_chameleon.py b/tests/models/chameleon/test_processor_chameleon.py index 2e0951dcf1..2256d1cb0c 100644 --- a/tests/models/chameleon/test_processor_chameleon.py +++ b/tests/models/chameleon/test_processor_chameleon.py @@ -33,11 +33,12 @@ SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model") class ChameleonProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = ChameleonProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = ChameleonImageProcessor() tokenizer = LlamaTokenizer(vocab_file=SAMPLE_VOCAB) tokenizer.pad_token_id = 0 tokenizer.sep_token_id = 1 - processor = self.processor_class(image_processor=image_processor, tokenizer=tokenizer) - processor.save_pretrained(self.tmpdirname) + processor = cls.processor_class(image_processor=image_processor, tokenizer=tokenizer) + processor.save_pretrained(cls.tmpdirname) diff --git a/tests/models/chinese_clip/test_processor_chinese_clip.py b/tests/models/chinese_clip/test_processor_chinese_clip.py index e433c38f78..3c2a2247c5 100644 --- a/tests/models/chinese_clip/test_processor_chinese_clip.py +++ b/tests/models/chinese_clip/test_processor_chinese_clip.py @@ -36,8 +36,9 @@ if is_vision_available(): class ChineseCLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = ChineseCLIPProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() vocab_tokens = [ "[UNK]", @@ -59,8 +60,8 @@ class ChineseCLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): "t", "shirt", ] - self.vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"]) - with open(self.vocab_file, "w", encoding="utf-8") as vocab_writer: + cls.vocab_file = os.path.join(cls.tmpdirname, VOCAB_FILES_NAMES["vocab_file"]) + with open(cls.vocab_file, "w", encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) image_processor_map = { @@ -73,39 +74,44 @@ class ChineseCLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): "image_std": [0.26862954, 0.26130258, 0.27577711], "do_convert_rgb": True, } - self.image_processor_file = os.path.join(self.tmpdirname, FEATURE_EXTRACTOR_NAME) - with open(self.image_processor_file, "w", encoding="utf-8") as fp: + cls.image_processor_file = os.path.join(cls.tmpdirname, FEATURE_EXTRACTOR_NAME) + with open(cls.image_processor_file, "w", encoding="utf-8") as fp: json.dump(image_processor_map, fp) - tokenizer = self.get_tokenizer() - image_processor = self.get_image_processor() + tokenizer = cls.get_tokenizer() + image_processor = cls.get_image_processor() processor = ChineseCLIPProcessor(tokenizer=tokenizer, image_processor=image_processor) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - def get_tokenizer(self, **kwargs): - return BertTokenizer.from_pretrained(self.tmpdirname, **kwargs) + @classmethod + def get_tokenizer(cls, **kwargs): + return BertTokenizer.from_pretrained(cls.tmpdirname, **kwargs) - def get_rust_tokenizer(self, **kwargs): - return BertTokenizerFast.from_pretrained(self.tmpdirname, **kwargs) + @classmethod + def get_rust_tokenizer(cls, **kwargs): + return BertTokenizerFast.from_pretrained(cls.tmpdirname, **kwargs) - def get_image_processor(self, **kwargs): - return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname, **kwargs) + @classmethod + def get_image_processor(cls, **kwargs): + return ChineseCLIPImageProcessor.from_pretrained(cls.tmpdirname, **kwargs) - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() image_processor = self.get_image_processor() - processor_slow = ChineseCLIPProcessor(tokenizer=tokenizer_slow, image_processor=image_processor) - processor_slow.save_pretrained(self.tmpdirname) - processor_slow = ChineseCLIPProcessor.from_pretrained(self.tmpdirname, use_fast=False) + with tempfile.TemporaryDirectory() as tmpdir: + processor_slow = ChineseCLIPProcessor(tokenizer=tokenizer_slow, image_processor=image_processor) + processor_slow.save_pretrained(tmpdir) + processor_slow = ChineseCLIPProcessor.from_pretrained(self.tmpdirname, use_fast=False) - processor_fast = ChineseCLIPProcessor(tokenizer=tokenizer_fast, image_processor=image_processor) - processor_fast.save_pretrained(self.tmpdirname) - processor_fast = ChineseCLIPProcessor.from_pretrained(self.tmpdirname) + processor_fast = ChineseCLIPProcessor(tokenizer=tokenizer_fast, image_processor=image_processor) + processor_fast.save_pretrained(tmpdir) + processor_fast = ChineseCLIPProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor_slow.tokenizer.get_vocab(), tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab(), tokenizer_fast.get_vocab()) @@ -119,15 +125,18 @@ class ChineseCLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): self.assertIsInstance(processor_fast.image_processor, ChineseCLIPImageProcessor) def test_save_load_pretrained_additional_features(self): - processor = ChineseCLIPProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = ChineseCLIPProcessor( + tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor() + ) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(cls_token="(CLS)", sep_token="(SEP)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False) + tokenizer_add_kwargs = self.get_tokenizer(cls_token="(CLS)", sep_token="(SEP)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False) - processor = ChineseCLIPProcessor.from_pretrained( - self.tmpdirname, cls_token="(CLS)", sep_token="(SEP)", do_normalize=False - ) + processor = ChineseCLIPProcessor.from_pretrained( + tmpdir, cls_token="(CLS)", sep_token="(SEP)", do_normalize=False + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, BertTokenizerFast) diff --git a/tests/models/clip/test_processor_clip.py b/tests/models/clip/test_processor_clip.py index 7d7ea25b70..1b6eed7534 100644 --- a/tests/models/clip/test_processor_clip.py +++ b/tests/models/clip/test_processor_clip.py @@ -36,19 +36,20 @@ if is_vision_available(): class CLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = CLIPProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() vocab = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l", "w", "r", "t", "low", "er", "lowest", "newer", "wider", "", "<|startoftext|>", "<|endoftext|>"] # fmt: skip vocab_tokens = dict(zip(vocab, range(len(vocab)))) merges = ["#version: 0.2", "l o", "lo w", "e r", ""] - self.special_tokens_map = {"unk_token": ""} + cls.special_tokens_map = {"unk_token": ""} - self.vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"]) - self.merges_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["merges_file"]) - with open(self.vocab_file, "w", encoding="utf-8") as fp: + cls.vocab_file = os.path.join(cls.tmpdirname, VOCAB_FILES_NAMES["vocab_file"]) + cls.merges_file = os.path.join(cls.tmpdirname, VOCAB_FILES_NAMES["merges_file"]) + with open(cls.vocab_file, "w", encoding="utf-8") as fp: fp.write(json.dumps(vocab_tokens) + "\n") - with open(self.merges_file, "w", encoding="utf-8") as fp: + with open(cls.merges_file, "w", encoding="utf-8") as fp: fp.write("\n".join(merges)) image_processor_map = { @@ -60,34 +61,39 @@ class CLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): "image_mean": [0.48145466, 0.4578275, 0.40821073], "image_std": [0.26862954, 0.26130258, 0.27577711], } - self.image_processor_file = os.path.join(self.tmpdirname, IMAGE_PROCESSOR_NAME) - with open(self.image_processor_file, "w", encoding="utf-8") as fp: + cls.image_processor_file = os.path.join(cls.tmpdirname, IMAGE_PROCESSOR_NAME) + with open(cls.image_processor_file, "w", encoding="utf-8") as fp: json.dump(image_processor_map, fp) - def get_tokenizer(self, **kwargs): - return CLIPTokenizer.from_pretrained(self.tmpdirname, **kwargs) + @classmethod + def get_tokenizer(cls, **kwargs): + return CLIPTokenizer.from_pretrained(cls.tmpdirname, **kwargs) - def get_rust_tokenizer(self, **kwargs): - return CLIPTokenizerFast.from_pretrained(self.tmpdirname, **kwargs) + @classmethod + def get_rust_tokenizer(cls, **kwargs): + return CLIPTokenizerFast.from_pretrained(cls.tmpdirname, **kwargs) - def get_image_processor(self, **kwargs): - return CLIPImageProcessor.from_pretrained(self.tmpdirname, **kwargs) + @classmethod + def get_image_processor(cls, **kwargs): + return CLIPImageProcessor.from_pretrained(cls.tmpdirname, **kwargs) - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname) def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() image_processor = self.get_image_processor() - processor_slow = CLIPProcessor(tokenizer=tokenizer_slow, image_processor=image_processor) - processor_slow.save_pretrained(self.tmpdirname) - processor_slow = CLIPProcessor.from_pretrained(self.tmpdirname, use_fast=False) + with tempfile.TemporaryDirectory() as tmpdir: + processor_slow = CLIPProcessor(tokenizer=tokenizer_slow, image_processor=image_processor) + processor_slow.save_pretrained(tmpdir) + processor_slow = CLIPProcessor.from_pretrained(tmpdir, use_fast=False) - processor_fast = CLIPProcessor(tokenizer=tokenizer_fast, image_processor=image_processor) - processor_fast.save_pretrained(self.tmpdirname) - processor_fast = CLIPProcessor.from_pretrained(self.tmpdirname) + processor_fast = CLIPProcessor(tokenizer=tokenizer_fast, image_processor=image_processor) + processor_fast.save_pretrained(tmpdir) + processor_fast = CLIPProcessor.from_pretrained(tmpdir) self.assertEqual(processor_slow.tokenizer.get_vocab(), tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab(), tokenizer_fast.get_vocab()) @@ -101,15 +107,18 @@ class CLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): self.assertIsInstance(processor_fast.image_processor, CLIPImageProcessor) def test_save_load_pretrained_additional_features(self): - processor = CLIPProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = CLIPProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = CLIPTokenizer.from_pretrained(tmpdir, bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = CLIPImageProcessor.from_pretrained( + tmpdir, do_normalize=False, padding_value=1.0 + ) - processor = CLIPProcessor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = CLIPProcessor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, CLIPTokenizerFast) diff --git a/tests/models/colpali/test_processing_colpali.py b/tests/models/colpali/test_processing_colpali.py index 42592460fa..709b5cf079 100644 --- a/tests/models/colpali/test_processing_colpali.py +++ b/tests/models/colpali/test_processing_colpali.py @@ -27,16 +27,18 @@ SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model") class ColPaliProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = ColPaliProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = SiglipImageProcessor.from_pretrained("google/siglip-so400m-patch14-384") image_processor.image_seq_length = 0 tokenizer = GemmaTokenizer(SAMPLE_VOCAB, keep_accents=True) processor = PaliGemmaProcessor(image_processor=image_processor, tokenizer=tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) @require_torch @require_vision diff --git a/tests/models/donut/test_processor_donut.py b/tests/models/donut/test_processor_donut.py index ddc63a0add..272f1fd823 100644 --- a/tests/models/donut/test_processor_donut.py +++ b/tests/models/donut/test_processor_donut.py @@ -25,16 +25,17 @@ class DonutProcessorTest(ProcessorTesterMixin, unittest.TestCase): from_pretrained_id = "naver-clova-ix/donut-base" processor_class = DonutProcessor - def setUp(self): - self.processor = DonutProcessor.from_pretrained(self.from_pretrained_id) - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.processor = DonutProcessor.from_pretrained(cls.from_pretrained_id) + cls.tmpdirname = tempfile.mkdtemp() image_processor = DonutImageProcessor() - tokenizer = XLMRobertaTokenizerFast.from_pretrained(self.from_pretrained_id) + tokenizer = XLMRobertaTokenizerFast.from_pretrained(cls.from_pretrained_id) processor = DonutProcessor(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def test_token2json(self): expected_json = { diff --git a/tests/models/emu3/test_processor_emu3.py b/tests/models/emu3/test_processor_emu3.py index bdecd62ec2..90696b17b4 100644 --- a/tests/models/emu3/test_processor_emu3.py +++ b/tests/models/emu3/test_processor_emu3.py @@ -31,8 +31,9 @@ if is_vision_available(): class Emu3ProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = Emu3Processor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = Emu3ImageProcessor() extra_special_tokens = extra_special_tokens = { "image_token": "", @@ -46,10 +47,10 @@ class Emu3ProcessorTest(ProcessorTesterMixin, unittest.TestCase): ) tokenizer.pad_token_id = 0 tokenizer.sep_token_id = 1 - processor = self.processor_class( + processor = cls.processor_class( image_processor=image_processor, tokenizer=tokenizer, chat_template="dummy_template" ) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def prepare_processor_dict(self): return { diff --git a/tests/models/fuyu/test_processor_fuyu.py b/tests/models/fuyu/test_processor_fuyu.py index d3ce39fa89..763e283670 100644 --- a/tests/models/fuyu/test_processor_fuyu.py +++ b/tests/models/fuyu/test_processor_fuyu.py @@ -36,8 +36,6 @@ class FuyuProcessingTest(ProcessorTesterMixin, unittest.TestCase): @classmethod def setUpClass(cls): cls.tmpdirname = tempfile.mkdtemp() - # Ensure tempdir is cleaned up even if there's a failure - cls.addClassCleanup(lambda tempdir=cls.tmpdirname: rmtree(tempdir)) image_processor = FuyuImageProcessor() tokenizer = AutoTokenizer.from_pretrained("adept/fuyu-8b") @@ -49,6 +47,10 @@ class FuyuProcessingTest(ProcessorTesterMixin, unittest.TestCase): bus_image_url = "https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/bus.png" cls.bus_image_pil = Image.open(io.BytesIO(requests.get(bus_image_url).content)) + @classmethod + def tearDownClass(cls): + rmtree(cls.tmpdirname) + def get_processor(self): image_processor = FuyuImageProcessor() tokenizer = AutoTokenizer.from_pretrained("adept/fuyu-8b") diff --git a/tests/models/gemma3/test_processing_gemma3.py b/tests/models/gemma3/test_processing_gemma3.py index e72ca9c2bf..e583ca6db2 100644 --- a/tests/models/gemma3/test_processing_gemma3.py +++ b/tests/models/gemma3/test_processing_gemma3.py @@ -34,8 +34,9 @@ SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model") class Gemma3ProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = Gemma3Processor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() gemma3_image_processor_kwargs = { "do_pan_and_scan": True, "pan_and_scan_min_crop_size": 256, @@ -52,15 +53,17 @@ class Gemma3ProcessorTest(ProcessorTesterMixin, unittest.TestCase): "eoi_token": "", } tokenizer = GemmaTokenizer(SAMPLE_VOCAB, keep_accents=True, extra_special_tokens=extra_special_tokens) - processor_kwargs = self.prepare_processor_dict() + processor_kwargs = cls.prepare_processor_dict() processor = Gemma3Processor(image_processor=image_processor, tokenizer=tokenizer, **processor_kwargs) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) # TODO: raushan or arthur: add the real chat template - def prepare_processor_dict(self): + @staticmethod + def prepare_processor_dict(): return { "chat_template": "{{ bos_token }}\n{%- if messages[0]['role'] == 'system' -%}\n {%- set first_user_prefix = messages[0]['content'][0]['text'] + '\n\n' -%}\n {%- set loop_messages = messages[1:] -%}\n{%- else -%}\n {%- set first_user_prefix = \"\" -%}\n {%- set loop_messages = messages -%}\n{%- endif -%}\n{%- for message in loop_messages -%}\n {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}\n {{ raise_exception(\"Conversation roles must alternate user/assistant/user/assistant/...\") }}\n {%- endif -%}\n {%- if (message['role'] == 'assistant') -%}\n {%- set role = \"model\" -%}\n {%- else -%}\n {%- set role = message['role'] -%}\n {%- endif -%}\n {{ '' + role + '\n' + (first_user_prefix if loop.first else \"\") }}\n {%- if message['content'] is string -%}\n {{ message['content'] | trim }}\n {%- elif message['content'] is iterable -%}\n {%- for item in message['content'] -%}\n {%- if item['type'] == 'image' -%}\n {{ '' }}\n {%- elif item['type'] == 'text' -%}\n {{ item['text'] | trim }}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{ raise_exception(\"Invalid content type\") }}\n {%- endif -%}\n {{ '\n' }}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {{'model\n'}}\n{%- endif -%}\n", "image_seq_length": 3, } # fmt: skip diff --git a/tests/models/git/test_processor_git.py b/tests/models/git/test_processor_git.py index bc62454cef..c15301a587 100644 --- a/tests/models/git/test_processor_git.py +++ b/tests/models/git/test_processor_git.py @@ -31,8 +31,9 @@ if is_vision_available(): class GitProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = GitProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = CLIPImageProcessor() tokenizer = BertTokenizer.from_pretrained( @@ -41,7 +42,7 @@ class GitProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor = GitProcessor(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -49,19 +50,21 @@ class GitProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_save_load_pretrained_additional_features(self): - processor = GitProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = GitProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) - processor = GitProcessor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = GitProcessor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, PreTrainedTokenizerFast) diff --git a/tests/models/got_ocr2/test_processor_got_ocr2.py b/tests/models/got_ocr2/test_processor_got_ocr2.py index 530d8234ce..3e7e7cb054 100644 --- a/tests/models/got_ocr2/test_processor_got_ocr2.py +++ b/tests/models/got_ocr2/test_processor_got_ocr2.py @@ -31,14 +31,15 @@ if is_vision_available(): class GotOcr2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = GotOcr2Processor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = GotOcr2ImageProcessor() tokenizer = PreTrainedTokenizerFast.from_pretrained("stepfun-ai/GOT-OCR-2.0-hf") - processor_kwargs = self.prepare_processor_dict() + processor_kwargs = {} processor = GotOcr2Processor(image_processor, tokenizer, **processor_kwargs) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -46,8 +47,9 @@ class GotOcr2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_ocr_queries(self): processor = self.get_processor() diff --git a/tests/models/grounding_dino/test_processor_grounding_dino.py b/tests/models/grounding_dino/test_processor_grounding_dino.py index d527853b1e..0b0174de45 100644 --- a/tests/models/grounding_dino/test_processor_grounding_dino.py +++ b/tests/models/grounding_dino/test_processor_grounding_dino.py @@ -44,12 +44,13 @@ class GroundingDinoProcessorTest(ProcessorTesterMixin, unittest.TestCase): from_pretrained_id = "IDEA-Research/grounding-dino-base" processor_class = GroundingDinoProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() vocab_tokens = ["[UNK]","[CLS]","[SEP]","[PAD]","[MASK]","want","##want","##ed","wa","un","runn","##ing",",","low","lowest"] # fmt: skip - self.vocab_file = os.path.join(self.tmpdirname, VOCAB_FILES_NAMES["vocab_file"]) - with open(self.vocab_file, "w", encoding="utf-8") as vocab_writer: + cls.vocab_file = os.path.join(cls.tmpdirname, VOCAB_FILES_NAMES["vocab_file"]) + with open(cls.vocab_file, "w", encoding="utf-8") as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens])) image_processor_map = { @@ -62,21 +63,21 @@ class GroundingDinoProcessorTest(ProcessorTesterMixin, unittest.TestCase): "rescale_factor": 1 / 255, "do_pad": True, } - self.image_processor_file = os.path.join(self.tmpdirname, IMAGE_PROCESSOR_NAME) - with open(self.image_processor_file, "w", encoding="utf-8") as fp: + cls.image_processor_file = os.path.join(cls.tmpdirname, IMAGE_PROCESSOR_NAME) + with open(cls.image_processor_file, "w", encoding="utf-8") as fp: json.dump(image_processor_map, fp) image_processor = GroundingDinoImageProcessor() - tokenizer = BertTokenizer.from_pretrained(self.from_pretrained_id) + tokenizer = BertTokenizer.from_pretrained(cls.from_pretrained_id) processor = GroundingDinoProcessor(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - self.batch_size = 7 - self.num_queries = 5 - self.embed_dim = 5 - self.seq_length = 5 + cls.batch_size = 7 + cls.num_queries = 5 + cls.embed_dim = 5 + cls.seq_length = 5 def prepare_text_inputs(self, batch_size: Optional[int] = None): labels = ["a cat", "remote control"] @@ -92,21 +93,24 @@ class GroundingDinoProcessorTest(ProcessorTesterMixin, unittest.TestCase): return [labels] return [labels, labels_longer] + [labels] * (batch_size - 2) + @classmethod # Copied from tests.models.clip.test_processor_clip.CLIPProcessorTest.get_tokenizer with CLIP->Bert - def get_tokenizer(self, **kwargs): - return BertTokenizer.from_pretrained(self.tmpdirname, **kwargs) + def get_tokenizer(cls, **kwargs): + return BertTokenizer.from_pretrained(cls.tmpdirname, **kwargs) + @classmethod # Copied from tests.models.clip.test_processor_clip.CLIPProcessorTest.get_rust_tokenizer with CLIP->Bert - def get_rust_tokenizer(self, **kwargs): - return BertTokenizerFast.from_pretrained(self.tmpdirname, **kwargs) + def get_rust_tokenizer(cls, **kwargs): + return BertTokenizerFast.from_pretrained(cls.tmpdirname, **kwargs) + @classmethod # Copied from tests.models.clip.test_processor_clip.CLIPProcessorTest.get_image_processor with CLIP->GroundingDino - def get_image_processor(self, **kwargs): - return GroundingDinoImageProcessor.from_pretrained(self.tmpdirname, **kwargs) + def get_image_processor(cls, **kwargs): + return GroundingDinoImageProcessor.from_pretrained(cls.tmpdirname, **kwargs) - # Copied from tests.models.clip.test_processor_clip.CLIPProcessorTest.tearDown - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def get_fake_grounding_dino_output(self): torch.manual_seed(42) @@ -147,13 +151,14 @@ class GroundingDinoProcessorTest(ProcessorTesterMixin, unittest.TestCase): tokenizer_fast = self.get_rust_tokenizer() image_processor = self.get_image_processor() - processor_slow = GroundingDinoProcessor(tokenizer=tokenizer_slow, image_processor=image_processor) - processor_slow.save_pretrained(self.tmpdirname) - processor_slow = GroundingDinoProcessor.from_pretrained(self.tmpdirname, use_fast=False) + with tempfile.TemporaryDirectory() as tmpdir: + processor_slow = GroundingDinoProcessor(tokenizer=tokenizer_slow, image_processor=image_processor) + processor_slow.save_pretrained(tmpdir) + processor_slow = GroundingDinoProcessor.from_pretrained(tmpdir, use_fast=False) - processor_fast = GroundingDinoProcessor(tokenizer=tokenizer_fast, image_processor=image_processor) - processor_fast.save_pretrained(self.tmpdirname) - processor_fast = GroundingDinoProcessor.from_pretrained(self.tmpdirname) + processor_fast = GroundingDinoProcessor(tokenizer=tokenizer_fast, image_processor=image_processor) + processor_fast.save_pretrained(tmpdir) + processor_fast = GroundingDinoProcessor.from_pretrained(tmpdir) self.assertEqual(processor_slow.tokenizer.get_vocab(), tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab(), tokenizer_fast.get_vocab()) @@ -168,15 +173,20 @@ class GroundingDinoProcessorTest(ProcessorTesterMixin, unittest.TestCase): # Copied from tests.models.clip.test_processor_clip.CLIPProcessorTest.test_save_load_pretrained_additional_features with CLIP->GroundingDino,GroundingDinoTokenizer->BertTokenizer def test_save_load_pretrained_additional_features(self): - processor = GroundingDinoProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = GroundingDinoProcessor( + tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor() + ) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = BertTokenizer.from_pretrained(tmpdir, bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = GroundingDinoImageProcessor.from_pretrained( + tmpdir, do_normalize=False, padding_value=1.0 + ) - processor = GroundingDinoProcessor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = GroundingDinoProcessor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, BertTokenizerFast) diff --git a/tests/models/idefics/test_processor_idefics.py b/tests/models/idefics/test_processor_idefics.py index 062b578a68..e161549166 100644 --- a/tests/models/idefics/test_processor_idefics.py +++ b/tests/models/idefics/test_processor_idefics.py @@ -43,17 +43,18 @@ if is_vision_available(): class IdeficsProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = IdeficsProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = IdeficsImageProcessor(return_tensors="pt") tokenizer = LlamaTokenizerFast.from_pretrained("HuggingFaceM4/tiny-random-idefics") processor = IdeficsProcessor(image_processor, tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - self.input_keys = ["pixel_values", "input_ids", "attention_mask", "image_attention_mask"] + cls.input_keys = ["pixel_values", "input_ids", "attention_mask", "image_attention_mask"] def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -61,8 +62,9 @@ class IdeficsProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def prepare_prompts(self): """This function prepares a list of PIL images""" @@ -107,15 +109,16 @@ class IdeficsProcessorTest(ProcessorTesterMixin, unittest.TestCase): return prompts def test_save_load_pretrained_additional_features(self): - processor = IdeficsProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = IdeficsProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) - processor = IdeficsProcessor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = IdeficsProcessor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, PreTrainedTokenizerFast) diff --git a/tests/models/idefics2/test_processor_idefics2.py b/tests/models/idefics2/test_processor_idefics2.py index f0b06470fe..99373005c9 100644 --- a/tests/models/idefics2/test_processor_idefics2.py +++ b/tests/models/idefics2/test_processor_idefics2.py @@ -41,38 +41,39 @@ if is_vision_available(): class Idefics2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = Idefics2Processor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() processor = Idefics2Processor.from_pretrained("HuggingFaceM4/idefics2-8b", image_seq_len=2) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - self.image1 = Image.open( + cls.image1 = Image.open( BytesIO( requests.get( "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" ).content ) ) - self.image2 = Image.open( + cls.image2 = Image.open( BytesIO(requests.get("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg").content) ) - self.image3 = Image.open( + cls.image3 = Image.open( BytesIO( requests.get( "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg" ).content ) ) - self.bos_token = processor.tokenizer.bos_token - self.image_token = processor.image_token.content - self.fake_image_token = processor.fake_image_token.content + cls.bos_token = processor.tokenizer.bos_token + cls.image_token = processor.image_token.content + cls.fake_image_token = processor.fake_image_token.content - self.bos_token_id = processor.tokenizer.convert_tokens_to_ids(self.bos_token) - self.image_token_id = processor.tokenizer.convert_tokens_to_ids(self.image_token) - self.fake_image_token_id = processor.tokenizer.convert_tokens_to_ids(self.fake_image_token) - self.image_seq_len = processor.image_seq_len + cls.bos_token_id = processor.tokenizer.convert_tokens_to_ids(cls.bos_token) + cls.image_token_id = processor.tokenizer.convert_tokens_to_ids(cls.image_token) + cls.fake_image_token_id = processor.tokenizer.convert_tokens_to_ids(cls.fake_image_token) + cls.image_seq_len = processor.image_seq_len def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -83,8 +84,9 @@ class Idefics2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs) - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_process_interleaved_images_prompts_no_image_splitting(self): tokenizer = self.get_tokenizer() diff --git a/tests/models/idefics3/test_processor_idefics3.py b/tests/models/idefics3/test_processor_idefics3.py index c64e596bee..5ff0eff946 100644 --- a/tests/models/idefics3/test_processor_idefics3.py +++ b/tests/models/idefics3/test_processor_idefics3.py @@ -104,7 +104,7 @@ class Idefics3ProcessorTest(ProcessorTesterMixin, unittest.TestCase): @classmethod def tearDownClass(cls): - shutil.rmtree(cls.tmpdirname) + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_process_interleaved_images_prompts_no_image_splitting(self): processor = self.get_processor() diff --git a/tests/models/instructblip/test_processor_instructblip.py b/tests/models/instructblip/test_processor_instructblip.py index ffec4b0111..6675390e0b 100644 --- a/tests/models/instructblip/test_processor_instructblip.py +++ b/tests/models/instructblip/test_processor_instructblip.py @@ -38,8 +38,9 @@ if is_vision_available(): class InstructBlipProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = InstructBlipProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = BlipImageProcessor() tokenizer = GPT2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model") @@ -47,7 +48,7 @@ class InstructBlipProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor = InstructBlipProcessor(image_processor, tokenizer, qformer_tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -58,8 +59,9 @@ class InstructBlipProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_qformer_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).qformer_tokenizer - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_save_load_pretrained_additional_features(self): processor = InstructBlipProcessor( @@ -67,14 +69,15 @@ class InstructBlipProcessorTest(ProcessorTesterMixin, unittest.TestCase): image_processor=self.get_image_processor(), qformer_tokenizer=self.get_qformer_tokenizer(), ) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) - processor = InstructBlipProcessor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = InstructBlipProcessor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, PreTrainedTokenizerFast) diff --git a/tests/models/instructblipvideo/test_processor_instructblipvideo.py b/tests/models/instructblipvideo/test_processor_instructblipvideo.py index d613d87822..3e65811b73 100644 --- a/tests/models/instructblipvideo/test_processor_instructblipvideo.py +++ b/tests/models/instructblipvideo/test_processor_instructblipvideo.py @@ -39,8 +39,9 @@ if is_vision_available(): class InstructBlipVideoProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = InstructBlipVideoProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = InstructBlipVideoImageProcessor() tokenizer = GPT2Tokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model") @@ -48,7 +49,7 @@ class InstructBlipVideoProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor = InstructBlipVideoProcessor(image_processor, tokenizer, qformer_tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -59,8 +60,9 @@ class InstructBlipVideoProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_qformer_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).qformer_tokenizer - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_save_load_pretrained_additional_features(self): processor = InstructBlipVideoProcessor( @@ -68,14 +70,15 @@ class InstructBlipVideoProcessorTest(ProcessorTesterMixin, unittest.TestCase): image_processor=self.get_image_processor(), qformer_tokenizer=self.get_qformer_tokenizer(), ) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) - processor = InstructBlipVideoProcessor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = InstructBlipVideoProcessor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, PreTrainedTokenizerFast) diff --git a/tests/models/kosmos2/test_processor_kosmos2.py b/tests/models/kosmos2/test_processor_kosmos2.py index 9a300719bc..587db26ef1 100644 --- a/tests/models/kosmos2/test_processor_kosmos2.py +++ b/tests/models/kosmos2/test_processor_kosmos2.py @@ -57,8 +57,9 @@ SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model") class Kosmos2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = Kosmos2Processor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = CLIPImageProcessor(do_center_crop=False) @@ -67,7 +68,7 @@ class Kosmos2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): fast_tokenizer = XLMRobertaTokenizerFast(__slow_tokenizer=slow_tokenizer) processor = Kosmos2Processor(image_processor, fast_tokenizer) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) # We override this method to take the fast tokenizer by default def get_component(self, attribute, **kwargs): @@ -92,8 +93,9 @@ class Kosmos2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_image_procesor_load_save_reload(self): # make sure load from Hub repo. -> save -> reload locally work @@ -105,15 +107,16 @@ class Kosmos2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): assert image_processor.to_json_string() == reloaded_image_processor.to_json_string() def test_save_load_pretrained_additional_features(self): - processor = Kosmos2Processor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = Kosmos2Processor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) + processor.save_pretrained(tmpdir) - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0) - processor = Kosmos2Processor.from_pretrained( - self.tmpdirname, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 - ) + processor = Kosmos2Processor.from_pretrained( + tmpdir, bos_token="(BOS)", eos_token="(EOS)", do_normalize=False, padding_value=1.0 + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, PreTrainedTokenizerFast) diff --git a/tests/models/layoutxlm/test_processor_layoutxlm.py b/tests/models/layoutxlm/test_processor_layoutxlm.py index 278d46d2fc..57872eda80 100644 --- a/tests/models/layoutxlm/test_processor_layoutxlm.py +++ b/tests/models/layoutxlm/test_processor_layoutxlm.py @@ -46,40 +46,46 @@ class LayoutXLMProcessorTest(ProcessorTesterMixin, unittest.TestCase): rust_tokenizer_class = LayoutXLMTokenizerFast processor_class = LayoutXLMProcessor - def setUp(self): + @classmethod + def setUpClass(cls): image_processor_map = { "do_resize": True, "size": 224, "apply_ocr": True, } - self.tmpdirname = tempfile.mkdtemp() - self.feature_extraction_file = os.path.join(self.tmpdirname, FEATURE_EXTRACTOR_NAME) - with open(self.feature_extraction_file, "w", encoding="utf-8") as fp: + cls.tmpdirname = tempfile.mkdtemp() + cls.feature_extraction_file = os.path.join(cls.tmpdirname, FEATURE_EXTRACTOR_NAME) + with open(cls.feature_extraction_file, "w", encoding="utf-8") as fp: fp.write(json.dumps(image_processor_map) + "\n") # taken from `test_tokenization_layoutxlm.LayoutXLMTokenizationTest.test_save_pretrained` - self.tokenizer_pretrained_name = "hf-internal-testing/tiny-random-layoutxlm" + cls.tokenizer_pretrained_name = "hf-internal-testing/tiny-random-layoutxlm" - tokenizer = self.get_tokenizer() - image_processor = self.get_image_processor() + tokenizer = cls.get_tokenizer() + image_processor = cls.get_image_processor() processor = LayoutXLMProcessor(tokenizer=tokenizer, image_processor=image_processor) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) - def get_tokenizer(self, **kwargs) -> PreTrainedTokenizer: - return self.tokenizer_class.from_pretrained(self.tokenizer_pretrained_name, **kwargs) + @classmethod + def get_tokenizer(cls, **kwargs) -> PreTrainedTokenizer: + return cls.tokenizer_class.from_pretrained(cls.tokenizer_pretrained_name, **kwargs) - def get_rust_tokenizer(self, **kwargs) -> PreTrainedTokenizerFast: - return self.rust_tokenizer_class.from_pretrained(self.tokenizer_pretrained_name, **kwargs) + @classmethod + def get_rust_tokenizer(cls, **kwargs) -> PreTrainedTokenizerFast: + return cls.rust_tokenizer_class.from_pretrained(cls.tokenizer_pretrained_name, **kwargs) - def get_tokenizers(self, **kwargs) -> list[PreTrainedTokenizerBase]: - return [self.get_tokenizer(**kwargs), self.get_rust_tokenizer(**kwargs)] + @classmethod + def get_tokenizers(cls, **kwargs) -> list[PreTrainedTokenizerBase]: + return [cls.get_tokenizer(**kwargs), cls.get_rust_tokenizer(**kwargs)] - def get_image_processor(self, **kwargs): - return LayoutLMv2ImageProcessor.from_pretrained(self.tmpdirname, **kwargs) + @classmethod + def get_image_processor(cls, **kwargs): + return LayoutLMv2ImageProcessor.from_pretrained(cls.tmpdirname, **kwargs) - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) def test_save_load_pretrained_default(self): image_processor = self.get_image_processor() @@ -87,8 +93,9 @@ class LayoutXLMProcessorTest(ProcessorTesterMixin, unittest.TestCase): for tokenizer in tokenizers: processor = LayoutXLMProcessor(image_processor=image_processor, tokenizer=tokenizer) - processor.save_pretrained(self.tmpdirname) - processor = LayoutXLMProcessor.from_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor.save_pretrained(tmpdir) + processor = LayoutXLMProcessor.from_pretrained(tmpdir) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer.get_vocab()) self.assertIsInstance(processor.tokenizer, (LayoutXLMTokenizer, LayoutXLMTokenizerFast)) @@ -97,21 +104,22 @@ class LayoutXLMProcessorTest(ProcessorTesterMixin, unittest.TestCase): self.assertIsInstance(processor.image_processor, LayoutLMv2ImageProcessor) def test_save_load_pretrained_additional_features(self): - processor = LayoutXLMProcessor(image_processor=self.get_image_processor(), tokenizer=self.get_tokenizer()) - processor.save_pretrained(self.tmpdirname) + with tempfile.TemporaryDirectory() as tmpdir: + processor = LayoutXLMProcessor(image_processor=self.get_image_processor(), tokenizer=self.get_tokenizer()) + processor.save_pretrained(tmpdir) - # slow tokenizer - tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") - image_processor_add_kwargs = self.get_image_processor(do_resize=False, size=30) + # slow tokenizer + tokenizer_add_kwargs = self.get_tokenizer(bos_token="(BOS)", eos_token="(EOS)") + image_processor_add_kwargs = self.get_image_processor(do_resize=False, size=30) - processor = LayoutXLMProcessor.from_pretrained( - self.tmpdirname, - use_fast=False, - bos_token="(BOS)", - eos_token="(EOS)", - do_resize=False, - size=30, - ) + processor = LayoutXLMProcessor.from_pretrained( + tmpdir, + use_fast=False, + bos_token="(BOS)", + eos_token="(EOS)", + do_resize=False, + size=30, + ) self.assertEqual(processor.tokenizer.get_vocab(), tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer, LayoutXLMTokenizer) diff --git a/tests/models/llava/test_processor_llava.py b/tests/models/llava/test_processor_llava.py index a411430a1e..56c854f4a4 100644 --- a/tests/models/llava/test_processor_llava.py +++ b/tests/models/llava/test_processor_llava.py @@ -34,14 +34,15 @@ if is_torch_available: class LlavaProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = LlavaProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = CLIPImageProcessor(do_center_crop=False) tokenizer = LlamaTokenizerFast.from_pretrained("huggyllama/llama-7b") - processor_kwargs = self.prepare_processor_dict() + processor_kwargs = cls.prepare_processor_dict() processor = LlavaProcessor(image_processor, tokenizer, **processor_kwargs) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -49,10 +50,12 @@ class LlavaProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def tearDown(self): - shutil.rmtree(self.tmpdirname) + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.tmpdirname, ignore_errors=True) - def prepare_processor_dict(self): + @staticmethod + def prepare_processor_dict(): return { "chat_template": "{% for message in messages %}{% if message['role'] != 'system' %}{{ message['role'].upper() + ': '}}{% endif %}{# Render all images first #}{% for content in message['content'] | selectattr('type', 'equalto', 'image') %}{{ '\n' }}{% endfor %}{# Render all text next #}{% if message['role'] != 'assistant' %}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{{ content['text'] + ' '}}{% endfor %}{% else %}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{% generation %}{{ content['text'] + ' '}}{% endgeneration %}{% endfor %}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ 'ASSISTANT:' }}{% endif %}", "patch_size": 3, diff --git a/tests/models/llava_next/test_processor_llava_next.py b/tests/models/llava_next/test_processor_llava_next.py index af14578411..4a14f5ab77 100644 --- a/tests/models/llava_next/test_processor_llava_next.py +++ b/tests/models/llava_next/test_processor_llava_next.py @@ -34,14 +34,15 @@ if is_vision_available(): class LlavaNextProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = LlavaNextProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = LlavaNextImageProcessor() tokenizer = LlamaTokenizerFast.from_pretrained("huggyllama/llama-7b") - processor_kwargs = self.prepare_processor_dict() + processor_kwargs = cls.prepare_processor_dict() processor = LlavaNextProcessor(image_processor, tokenizer, **processor_kwargs) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return LlavaNextProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -49,7 +50,8 @@ class LlavaNextProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_image_processor(self, **kwargs): return LlavaNextProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor - def prepare_processor_dict(self): + @staticmethod + def prepare_processor_dict(): return { "chat_template": "{% for message in messages %}{% if message['role'] != 'system' %}{{ message['role'].upper() + ': '}}{% endif %}{# Render all images first #}{% for content in message['content'] | selectattr('type', 'equalto', 'image') %}{{ '\n' }}{% endfor %}{# Render all text next #}{% if message['role'] != 'assistant' %}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{{ content['text'] + ' '}}{% endfor %}{% else %}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{% generation %}{{ content['text'] + ' '}}{% endgeneration %}{% endfor %}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ 'ASSISTANT:' }}{% endif %}", "patch_size": 3, diff --git a/tests/models/llava_next_video/test_processor_llava_next_video.py b/tests/models/llava_next_video/test_processor_llava_next_video.py index aa2b3045a5..affa95cc04 100644 --- a/tests/models/llava_next_video/test_processor_llava_next_video.py +++ b/tests/models/llava_next_video/test_processor_llava_next_video.py @@ -35,17 +35,18 @@ if is_torch_available: class LlavaNextVideoProcessorTest(ProcessorTesterMixin, unittest.TestCase): processor_class = LlavaNextVideoProcessor - def setUp(self): - self.tmpdirname = tempfile.mkdtemp() + @classmethod + def setUpClass(cls): + cls.tmpdirname = tempfile.mkdtemp() image_processor = LlavaNextImageProcessor() video_processor = LlavaNextVideoImageProcessor() tokenizer = LlamaTokenizerFast.from_pretrained("llava-hf/LLaVA-NeXT-Video-7B-hf") - processor_kwargs = self.prepare_processor_dict() + processor_kwargs = cls.prepare_processor_dict() processor = LlavaNextVideoProcessor( video_processor=video_processor, image_processor=image_processor, tokenizer=tokenizer, **processor_kwargs ) - processor.save_pretrained(self.tmpdirname) + processor.save_pretrained(cls.tmpdirname) def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -56,7 +57,8 @@ class LlavaNextVideoProcessorTest(ProcessorTesterMixin, unittest.TestCase): def get_video_processor(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).video_processor - def prepare_processor_dict(self): + @classmethod + def prepare_processor_dict(cls): return { "chat_template": "{% for message in messages %}{{'<|im_start|>' + message['role'] + ' '}}{# Render all images first #}{% for content in message['content'] | selectattr('type', 'equalto', 'image') %}{{ '' }}{% endfor %}{# Render all video then #}{% for content in message['content'] | selectattr('type', 'equalto', 'video') %}{{ '