Skip to content

Commit 206e2a9

Browse files
yonigozlanBernardZach
authored andcommitted
rename all test_processing_*.py to test_processor_*.py (huggingface#33878)
* rename all test_processing_*.py to test_processor_*.py ans fix duplicate test processor paligemma * fix copies * fix broken tests * fix-copies * fix test processor bridgetower
1 parent a9c310c commit 206e2a9

14 files changed

+46
-170
lines changed

src/transformers/models/omdet_turbo/modeling_omdet_turbo.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from dataclasses import dataclass
2222
from functools import lru_cache
2323
from pathlib import Path
24-
from typing import Optional, Tuple, Union
24+
from typing import List, Optional, Tuple, Union
2525

2626
import torch
2727
import torch.nn.functional as F
@@ -208,7 +208,10 @@ def load_cuda_kernels():
208208

209209
# Copied from transformers.models.deformable_detr.modeling_deformable_detr.multi_scale_deformable_attention
210210
def multi_scale_deformable_attention(
211-
value: Tensor, value_spatial_shapes: Tensor, sampling_locations: Tensor, attention_weights: Tensor
211+
value: Tensor,
212+
value_spatial_shapes: Union[Tensor, List[Tuple]],
213+
sampling_locations: Tensor,
214+
attention_weights: Tensor,
212215
) -> Tensor:
213216
batch_size, _, num_heads, hidden_dim = value.shape
214217
_, num_queries, num_heads, num_levels, num_points, _ = sampling_locations.shape

tests/models/blip/test_processor_blip.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_unstructured_kwargs_batched(self):
152152
self.skip_processor_without_typed_kwargs(processor)
153153

154154
input_str = ["lower newer", "upper older longer string"]
155-
image_input = self.prepare_image_inputs() * 2
155+
image_input = self.prepare_image_inputs(batch_size=2)
156156
inputs = processor(
157157
text=input_str,
158158
images=image_input,

tests/models/blip_2/test_processor_blip_2.py

+1-28
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import pytest
1919

20-
from transformers.testing_utils import require_torch, require_vision
20+
from transformers.testing_utils import require_vision
2121
from transformers.utils import is_vision_available
2222

2323
from ...test_processing_common import ProcessorTesterMixin
@@ -139,30 +139,3 @@ def test_model_input_names(self):
139139

140140
# For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
141141
self.assertCountEqual(list(inputs.keys()), ["input_ids", "pixel_values", "attention_mask"])
142-
143-
@require_torch
144-
@require_vision
145-
def test_unstructured_kwargs_batched(self):
146-
if "image_processor" not in self.processor_class.attributes:
147-
self.skipTest(f"image_processor attribute not present in {self.processor_class}")
148-
image_processor = self.get_component("image_processor")
149-
tokenizer = self.get_component("tokenizer")
150-
if not tokenizer.pad_token:
151-
tokenizer.pad_token = "[TEST_PAD]"
152-
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor)
153-
self.skip_processor_without_typed_kwargs(processor)
154-
155-
input_str = ["lower newer", "upper older longer string"]
156-
image_input = self.prepare_image_inputs() * 2
157-
inputs = processor(
158-
text=input_str,
159-
images=image_input,
160-
return_tensors="pt",
161-
crop_size={"height": 214, "width": 214},
162-
size={"height": 214, "width": 214},
163-
padding="longest",
164-
max_length=76,
165-
)
166-
self.assertEqual(inputs["pixel_values"].shape[2], 214)
167-
168-
self.assertEqual(len(inputs["input_ids"][0]), 11)

tests/models/bridgetower/test_processing_bridgetower.py tests/models/bridgetower/test_processor_bridgetower.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
import tempfile
1616
import unittest
1717

18-
import numpy as np
19-
2018
from transformers.testing_utils import require_torch, require_vision
2119
from transformers.utils import is_vision_available
2220

2321
from ...test_processing_common import ProcessorTesterMixin
2422

2523

2624
if is_vision_available():
27-
from PIL import Image
28-
2925
from transformers import (
3026
AutoProcessor,
3127
BridgeTowerImageProcessor,
@@ -35,7 +31,7 @@
3531

3632

3733
@require_vision
38-
class Blip2ProcessorTest(ProcessorTesterMixin, unittest.TestCase):
34+
class BridgeTowerProcessorTest(ProcessorTesterMixin, unittest.TestCase):
3935
processor_class = BridgeTowerProcessor
4036

4137
def setUp(self):
@@ -57,17 +53,6 @@ def get_image_processor(self, **kwargs):
5753
def tearDown(self):
5854
shutil.rmtree(self.tmpdirname)
5955

60-
def prepare_image_inputs(self):
61-
"""This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True,
62-
or a list of PyTorch tensors if one specifies torchify=True.
63-
"""
64-
65-
image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)]
66-
67-
image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs]
68-
69-
return image_inputs
70-
7156
# Some kwargs tests are overriden from common tests to handle shortest_edge
7257
# and size_divisor behaviour
7358

@@ -149,7 +134,7 @@ def test_unstructured_kwargs_batched(self):
149134
self.skip_processor_without_typed_kwargs(processor)
150135

151136
input_str = ["lower newer", "upper older longer string"]
152-
image_input = self.prepare_image_inputs() * 2
137+
image_input = self.prepare_image_inputs(batch_size=2)
153138
inputs = processor(
154139
text=input_str,
155140
images=image_input,

tests/models/donut/test_processing_donut.py tests/models/donut/test_processor_donut.py

-31
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
import unittest
1919

2020
from transformers import DonutImageProcessor, DonutProcessor, XLMRobertaTokenizerFast
21-
from transformers.testing_utils import (
22-
require_torch,
23-
require_vision,
24-
)
2521

2622
from ...test_processing_common import ProcessorTesterMixin
2723

@@ -65,30 +61,3 @@ def test_token2json(self):
6561
actual_json = self.processor.token2json(sequence)
6662

6763
self.assertDictEqual(actual_json, expected_json)
68-
69-
@require_torch
70-
@require_vision
71-
def test_unstructured_kwargs_batched(self):
72-
if "image_processor" not in self.processor_class.attributes:
73-
self.skipTest(f"image_processor attribute not present in {self.processor_class}")
74-
image_processor = self.get_component("image_processor")
75-
tokenizer = self.get_component("tokenizer")
76-
if not tokenizer.pad_token:
77-
tokenizer.pad_token = "[TEST_PAD]"
78-
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor)
79-
self.skip_processor_without_typed_kwargs(processor)
80-
81-
input_str = ["lower newer", "upper older longer string"]
82-
image_input = self.prepare_image_inputs() * 2
83-
inputs = processor(
84-
text=input_str,
85-
images=image_input,
86-
return_tensors="pt",
87-
crop_size={"height": 214, "width": 214},
88-
size={"height": 214, "width": 214},
89-
padding="longest",
90-
max_length=76,
91-
)
92-
self.assertEqual(inputs["pixel_values"].shape[2], 214)
93-
94-
self.assertEqual(len(inputs["input_ids"][0]), 7)

tests/models/musicgen_melody/test_processor_musicgen_melody.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def floats_list(shape, scale=1.0, rng=None, name=None):
5050
@require_torch
5151
@require_sentencepiece
5252
@require_torchaudio
53-
# Copied from tests.models.musicgen.test_processing_musicgen.MusicgenProcessorTest with Musicgen->MusicgenMelody, Encodec->MusicgenMelody, padding_mask->attention_mask, input_values->input_features
53+
# Copied from tests.models.musicgen.test_processor_musicgen.MusicgenProcessorTest with Musicgen->MusicgenMelody, Encodec->MusicgenMelody, padding_mask->attention_mask, input_values->input_features
5454
class MusicgenMelodyProcessorTest(unittest.TestCase):
5555
def setUp(self):
5656
# Ignore copy

tests/models/paligemma/test_processing_paligemma.py

-84
This file was deleted.

tests/models/paligemma/test_processor_paligemma.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@
1616
import tempfile
1717
import unittest
1818

19-
from transformers import GemmaTokenizer
19+
from transformers import GemmaTokenizer, PaliGemmaProcessor
2020
from transformers.testing_utils import get_tests_dir, require_torch, require_vision
2121
from transformers.utils import is_vision_available
2222

2323
from ...test_processing_common import ProcessorTesterMixin
2424

2525

2626
if is_vision_available():
27-
from transformers import (
28-
PaliGemmaProcessor,
29-
SiglipImageProcessor,
30-
is_vision_available,
31-
)
27+
from transformers import SiglipImageProcessor
3228

3329
SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model")
3430

@@ -61,3 +57,37 @@ def test_image_seq_length(self):
6157
text=input_str, images=image_input, return_tensors="pt", max_length=112, padding="max_length"
6258
)
6359
self.assertEqual(len(inputs["input_ids"][0]), 112 + 14)
60+
61+
def test_text_with_image_tokens(self):
62+
image_processor = self.get_component("image_processor")
63+
tokenizer = self.get_component("tokenizer")
64+
65+
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor)
66+
text_multi_images = "<image><image><bos>Dummy text!"
67+
text_single_image = "<image><bos>Dummy text!"
68+
text_no_image = "Dummy text!"
69+
70+
image = self.prepare_image_inputs()
71+
72+
out_noimage = processor(text=text_no_image, images=image, return_tensors="np")
73+
out_singlimage = processor(text=text_single_image, images=image, return_tensors="np")
74+
for k in out_noimage:
75+
self.assertTrue(out_noimage[k].tolist() == out_singlimage[k].tolist())
76+
77+
out_multiimages = processor(text=text_multi_images, images=[image, image], return_tensors="np")
78+
out_noimage = processor(text=text_no_image, images=[[image, image]], return_tensors="np")
79+
80+
# We can't be sure what is users intention, whether user want "one text + two images" or user forgot to add the second text
81+
with self.assertRaises(ValueError):
82+
out_noimage = processor(text=text_no_image, images=[image, image], return_tensors="np")
83+
84+
for k in out_noimage:
85+
self.assertTrue(out_noimage[k].tolist() == out_multiimages[k].tolist())
86+
87+
text_batched = ["Dummy text!", "Dummy text!"]
88+
text_batched_with_image = ["<image><bos>Dummy text!", "<image><bos>Dummy text!"]
89+
out_images = processor(text=text_batched_with_image, images=[image, image], return_tensors="np")
90+
out_noimage_nested = processor(text=text_batched, images=[[image], [image]], return_tensors="np")
91+
out_noimage = processor(text=text_batched, images=[image, image], return_tensors="np")
92+
for k in out_noimage:
93+
self.assertTrue(out_noimage[k].tolist() == out_images[k].tolist() == out_noimage_nested[k].tolist())

0 commit comments

Comments
 (0)