Skip to content

Commit 2acae97

Browse files
authored
Fix boolean padding in ClapFeatureExtractor (#23648) (#49960)
1 parent e487eaf commit 2acae97

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎src/transformers/models/clap/feature_extraction_clap.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def __call__(
263263
self,
264264
raw_speech: np.ndarray | list[float] | list[np.ndarray] | list[list[float]],
265265
truncation: str | None = None,
266-
padding: str | None = None,
266+
padding: bool | str | None = None,
267267
max_length: int | None = None,
268268
sampling_rate: int | None = None,
269269
return_tensors: str | TensorType | None = None,
@@ -284,8 +284,9 @@ def __call__(
284284
If `config.fusion` is set to True, shorter audios also need to return 4 mels, which will just be a
285285
copy of the original mel obtained from the padded audio.
286286
- `rand_trunc` will select a random crop of the mel spectrogram.
287-
padding (`str`, *optional*):
288-
Padding pattern for shorter audio inputs. Three patterns were originally implemented:
287+
padding (`bool`, `str`, *optional*):
288+
Padding pattern for shorter audio inputs. If `True`, defaults to `self.padding`.
289+
Available patterns:
289290
- `repeatpad`: the audio is repeated, and then padded to fit the `max_length`.
290291
- `repeat`: the audio is repeated and then cut to fit the `max_length`
291292
- `pad`: the audio is padded.
@@ -299,7 +300,8 @@ def __call__(
299300
pipeline.
300301
"""
301302
truncation = truncation if truncation is not None else self.truncation
302-
padding = padding if padding else self.padding
303+
if padding is True or padding is None:
304+
padding = self.padding
303305

304306
if sampling_rate is not None:
305307
if sampling_rate != self.sampling_rate:

‎tests/models/clap/test_feature_extraction_clap.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ def test_double_precision_pad(self):
144144
pt_processed = feature_extractor.pad([{"input_features": inputs}], return_tensors="pt")
145145
self.assertTrue(pt_processed.input_features.dtype == torch.float32)
146146

147+
def test_padding_boolean(self):
148+
feature_extractor = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
149+
speech_inputs = floats_list((1, 800))[0]
150+
151+
np.random.seed(0)
152+
out_default = feature_extractor(speech_inputs, return_tensors="np").input_features
153+
np.random.seed(0)
154+
out_bool = feature_extractor(speech_inputs, padding=True, return_tensors="np").input_features
155+
np.random.seed(0)
156+
out_repeatpad = feature_extractor(speech_inputs, padding="repeatpad", return_tensors="np").input_features
157+
158+
self.assertTrue(np.allclose(out_bool, out_default, atol=1e-3))
159+
self.assertTrue(np.allclose(out_bool, out_repeatpad, atol=1e-3))
160+
147161
# Copied from tests.models.whisper.test_feature_extraction_whisper.WhisperFeatureExtractionTest._load_datasamples
148162
def _load_datasamples(self, num_samples):
149163
ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")

0 commit comments

Comments
 (0)