first commit
Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
This commit is contained in:
0
tests/models/seamless_m4t/__init__.py
Normal file
0
tests/models/seamless_m4t/__init__.py
Normal file
@@ -0,0 +1,350 @@
|
||||
# Copyright 2023 HuggingFace Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import itertools
|
||||
import os
|
||||
import random
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import numpy as np
|
||||
from datasets import load_dataset
|
||||
|
||||
from transformers import SeamlessM4TFeatureExtractor, is_speech_available
|
||||
from transformers.testing_utils import check_json_file_has_correct_format, require_torch
|
||||
from transformers.utils.import_utils import is_torch_available
|
||||
|
||||
from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
global_rng = random.Random()
|
||||
|
||||
|
||||
# Copied from tests.models.whisper.test_feature_extraction_whisper.floats_list
|
||||
def floats_list(shape, scale=1.0, rng=None, name=None):
|
||||
"""Creates a random float32 tensor"""
|
||||
if rng is None:
|
||||
rng = global_rng
|
||||
|
||||
values = []
|
||||
for batch_idx in range(shape[0]):
|
||||
values.append([])
|
||||
for _ in range(shape[1]):
|
||||
values[-1].append(rng.random() * scale)
|
||||
|
||||
return values
|
||||
|
||||
|
||||
@require_torch
|
||||
class SeamlessM4TFeatureExtractionTester:
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=7,
|
||||
min_seq_length=400,
|
||||
max_seq_length=2000,
|
||||
feature_size=10,
|
||||
padding_value=0.0,
|
||||
sampling_rate=4_000,
|
||||
return_attention_mask=True,
|
||||
do_normalize=True,
|
||||
stride=2,
|
||||
):
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
self.min_seq_length = min_seq_length
|
||||
self.max_seq_length = max_seq_length
|
||||
self.seq_length_diff = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
|
||||
self.padding_value = padding_value
|
||||
self.sampling_rate = sampling_rate
|
||||
self.return_attention_mask = return_attention_mask
|
||||
self.do_normalize = do_normalize
|
||||
self.feature_size = feature_size
|
||||
self.stride = stride
|
||||
self.num_mel_bins = feature_size
|
||||
|
||||
def prepare_feat_extract_dict(self):
|
||||
return {
|
||||
"feature_size": self.feature_size,
|
||||
"num_mel_bins": self.num_mel_bins,
|
||||
"padding_value": self.padding_value,
|
||||
"sampling_rate": self.sampling_rate,
|
||||
"stride": self.stride,
|
||||
"return_attention_mask": self.return_attention_mask,
|
||||
"do_normalize": self.do_normalize,
|
||||
}
|
||||
|
||||
# Copied from tests.models.whisper.test_feature_extraction_whisper.WhisperFeatureExtractionTester.prepare_inputs_for_common
|
||||
def prepare_inputs_for_common(self, equal_length=False, numpify=False):
|
||||
def _flatten(list_of_lists):
|
||||
return list(itertools.chain(*list_of_lists))
|
||||
|
||||
if equal_length:
|
||||
speech_inputs = [floats_list((self.max_seq_length, self.feature_size)) for _ in range(self.batch_size)]
|
||||
else:
|
||||
# make sure that inputs increase in size
|
||||
speech_inputs = [
|
||||
floats_list((x, self.feature_size))
|
||||
for x in range(self.min_seq_length, self.max_seq_length, self.seq_length_diff)
|
||||
]
|
||||
if numpify:
|
||||
speech_inputs = [np.asarray(x) for x in speech_inputs]
|
||||
return speech_inputs
|
||||
|
||||
|
||||
@require_torch
|
||||
class SeamlessM4TFeatureExtractionTest(SequenceFeatureExtractionTestMixin, unittest.TestCase):
|
||||
feature_extraction_class = SeamlessM4TFeatureExtractor if is_speech_available() else None
|
||||
|
||||
def setUp(self):
|
||||
self.feat_extract_tester = SeamlessM4TFeatureExtractionTester(self)
|
||||
|
||||
def test_feat_extract_from_and_save_pretrained(self):
|
||||
feat_extract_first = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
saved_file = feat_extract_first.save_pretrained(tmpdirname)[0]
|
||||
check_json_file_has_correct_format(saved_file)
|
||||
feat_extract_second = self.feature_extraction_class.from_pretrained(tmpdirname)
|
||||
|
||||
dict_first = feat_extract_first.to_dict()
|
||||
dict_second = feat_extract_second.to_dict()
|
||||
self.assertDictEqual(dict_first, dict_second)
|
||||
|
||||
def test_feat_extract_to_json_file(self):
|
||||
feat_extract_first = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
json_file_path = os.path.join(tmpdirname, "feat_extract.json")
|
||||
feat_extract_first.to_json_file(json_file_path)
|
||||
feat_extract_second = self.feature_extraction_class.from_json_file(json_file_path)
|
||||
|
||||
dict_first = feat_extract_first.to_dict()
|
||||
dict_second = feat_extract_second.to_dict()
|
||||
self.assertEqual(dict_first, dict_second)
|
||||
|
||||
def test_call_numpy(self):
|
||||
# Tests that all call wrap to encode_plus and batch_encode_plus
|
||||
feature_extractor = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
|
||||
# create three inputs of length 800, 1000, and 1200
|
||||
speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
|
||||
np_speech_inputs = [np.asarray(speech_input) for speech_input in speech_inputs]
|
||||
|
||||
# Test feature size
|
||||
input_features = feature_extractor(np_speech_inputs, padding=True, return_tensors="np").input_features
|
||||
self.assertTrue(input_features.ndim == 3)
|
||||
self.assertTrue(input_features.shape[0] == 3)
|
||||
self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size * feature_extractor.stride)
|
||||
|
||||
# Test not batched input
|
||||
encoded_sequences_1 = feature_extractor(speech_inputs[0], return_tensors="np").input_features
|
||||
encoded_sequences_2 = feature_extractor(np_speech_inputs[0], return_tensors="np").input_features
|
||||
self.assertTrue(np.allclose(encoded_sequences_1, encoded_sequences_2, atol=1e-3))
|
||||
|
||||
# Test batched
|
||||
encoded_sequences_1 = feature_extractor(speech_inputs, return_tensors="np").input_features
|
||||
encoded_sequences_2 = feature_extractor(np_speech_inputs, return_tensors="np").input_features
|
||||
for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2):
|
||||
self.assertTrue(np.allclose(enc_seq_1, enc_seq_2, atol=1e-3))
|
||||
|
||||
# Test 2-D numpy arrays are batched.
|
||||
speech_inputs = [floats_list((1, x))[0] for x in (800, 800, 800)]
|
||||
np_speech_inputs = np.asarray(speech_inputs)
|
||||
encoded_sequences_1 = feature_extractor(speech_inputs, return_tensors="np").input_features
|
||||
encoded_sequences_2 = feature_extractor(np_speech_inputs, return_tensors="np").input_features
|
||||
for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2):
|
||||
self.assertTrue(np.allclose(enc_seq_1, enc_seq_2, atol=1e-3))
|
||||
|
||||
def test_call_with_padded_input_not_multiple_of_stride(self):
|
||||
# same as test_call_numpy but with stride=6 and pad_to_multiple_of=8
|
||||
# the input sizes 800, 1400 and 200 are a multiple of pad_to_multiple_of but not a multiple of stride
|
||||
# therefore remainder = num_frames % self.stride will not be zero and must be subtracted from num_frames
|
||||
stride = 6
|
||||
pad_to_multiple_of = 8
|
||||
|
||||
feature_extractor_args = self.feat_extract_tester.prepare_feat_extract_dict()
|
||||
feature_extractor_args["stride"] = stride
|
||||
feature_extractor = self.feature_extraction_class(**feature_extractor_args)
|
||||
|
||||
speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
|
||||
np_speech_inputs = [np.asarray(speech_input) for speech_input in speech_inputs]
|
||||
|
||||
# Test feature size and attention mask size
|
||||
output = feature_extractor(np_speech_inputs, pad_to_multiple_of=pad_to_multiple_of, return_tensors="np")
|
||||
input_features = output.input_features
|
||||
self.assertTrue(input_features.ndim == 3)
|
||||
self.assertTrue(input_features.shape[0] == 3)
|
||||
self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size * feature_extractor.stride)
|
||||
# same as test_attention_mask
|
||||
attention_mask = output.attention_mask
|
||||
self.assertTrue(attention_mask.ndim == 2)
|
||||
self.assertTrue(attention_mask.shape[0] == 3)
|
||||
self.assertTrue(attention_mask.shape[-1] == input_features.shape[1])
|
||||
|
||||
# Test not batched input
|
||||
encoded_sequences_1 = feature_extractor(
|
||||
speech_inputs[0], pad_to_multiple_of=pad_to_multiple_of, return_tensors="np"
|
||||
).input_features
|
||||
encoded_sequences_2 = feature_extractor(
|
||||
np_speech_inputs[0], pad_to_multiple_of=pad_to_multiple_of, return_tensors="np"
|
||||
).input_features
|
||||
self.assertTrue(np.allclose(encoded_sequences_1, encoded_sequences_2, atol=1e-3))
|
||||
|
||||
# Test batched
|
||||
encoded_sequences_1 = feature_extractor(
|
||||
speech_inputs, pad_to_multiple_of=pad_to_multiple_of, return_tensors="np"
|
||||
).input_features
|
||||
encoded_sequences_2 = feature_extractor(
|
||||
np_speech_inputs, pad_to_multiple_of=pad_to_multiple_of, return_tensors="np"
|
||||
).input_features
|
||||
for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2):
|
||||
self.assertTrue(np.allclose(enc_seq_1, enc_seq_2, atol=1e-3))
|
||||
|
||||
# Test 2-D numpy arrays are batched.
|
||||
speech_inputs = [floats_list((1, x))[0] for x in (800, 800, 800)]
|
||||
np_speech_inputs = np.asarray(speech_inputs)
|
||||
encoded_sequences_1 = feature_extractor(
|
||||
speech_inputs, pad_to_multiple_of=pad_to_multiple_of, return_tensors="np"
|
||||
).input_features
|
||||
encoded_sequences_2 = feature_extractor(
|
||||
np_speech_inputs, pad_to_multiple_of=pad_to_multiple_of, return_tensors="np"
|
||||
).input_features
|
||||
for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2):
|
||||
self.assertTrue(np.allclose(enc_seq_1, enc_seq_2, atol=1e-3))
|
||||
|
||||
def test_call_without_attention_mask(self):
|
||||
feature_extractor_args = self.feat_extract_tester.prepare_feat_extract_dict()
|
||||
feature_extractor = self.feature_extraction_class(**feature_extractor_args)
|
||||
|
||||
# create three inputs of length 800, 1000, and 1200
|
||||
speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
|
||||
np_speech_inputs = [np.asarray(speech_input) for speech_input in speech_inputs]
|
||||
|
||||
# Test attention mask when passing no attention mask to forward call
|
||||
output = feature_extractor(np_speech_inputs, padding=True, return_tensors="np", return_attention_mask=False)
|
||||
self.assertTrue("attention_mask" not in output)
|
||||
|
||||
# Test attention mask when no attention mask by default
|
||||
feature_extractor_args["return_attention_mask"] = False
|
||||
feature_extractor = self.feature_extraction_class(**feature_extractor_args)
|
||||
output = feature_extractor(np_speech_inputs, padding=True, return_tensors="np", return_attention_mask=False)
|
||||
self.assertTrue("attention_mask" not in output)
|
||||
|
||||
def test_attention_mask(self):
|
||||
# test attention mask has the right output shape
|
||||
feature_extractor_args = self.feat_extract_tester.prepare_feat_extract_dict()
|
||||
|
||||
feature_extractor = self.feature_extraction_class(**feature_extractor_args)
|
||||
# create three inputs of length 800, 1000, and 1200
|
||||
speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
|
||||
np_speech_inputs = [np.asarray(speech_input) for speech_input in speech_inputs]
|
||||
|
||||
# Test attention mask when passing it to forward call
|
||||
output = feature_extractor(np_speech_inputs, padding=True, return_tensors="np")
|
||||
input_features = output.input_features
|
||||
|
||||
attention_mask = output.attention_mask
|
||||
self.assertTrue(attention_mask.ndim == 2)
|
||||
self.assertTrue(attention_mask.shape[0] == 3)
|
||||
self.assertTrue(attention_mask.shape[-1] == input_features.shape[1])
|
||||
|
||||
@require_torch
|
||||
def test_call_torch(self):
|
||||
import torch
|
||||
|
||||
# Tests that all call wrap to encode_plus and batch_encode_plus
|
||||
feature_extractor = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
|
||||
# create three inputs of length 800, 1000, and 1200
|
||||
speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
|
||||
pt_speech_inputs = [torch.tensor(speech_input) for speech_input in speech_inputs]
|
||||
|
||||
# Test feature size
|
||||
input_features = feature_extractor(pt_speech_inputs, padding=True, return_tensors="pt").input_features
|
||||
self.assertTrue(input_features.ndim == 3)
|
||||
self.assertTrue(input_features.shape[0] == 3)
|
||||
self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size * feature_extractor.stride)
|
||||
|
||||
# Test not batched input
|
||||
encoded_sequences_1 = feature_extractor(speech_inputs[0], return_tensors="pt").input_features
|
||||
encoded_sequences_2 = feature_extractor(pt_speech_inputs[0], return_tensors="pt").input_features
|
||||
torch.testing.assert_close(encoded_sequences_1, encoded_sequences_2, rtol=1e-3, atol=1e-3)
|
||||
|
||||
# Test batched
|
||||
encoded_sequences_1 = feature_extractor(speech_inputs, return_tensors="pt").input_features
|
||||
encoded_sequences_2 = feature_extractor(pt_speech_inputs, return_tensors="pt").input_features
|
||||
for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2):
|
||||
torch.testing.assert_close(enc_seq_1, enc_seq_2, rtol=1e-3, atol=1e-3)
|
||||
|
||||
# Test 2-D numpy arrays are batched.
|
||||
speech_inputs = [floats_list((1, x))[0] for x in (800, 800, 800)]
|
||||
pt_speech_inputs = torch.tensor(speech_inputs)
|
||||
encoded_sequences_1 = feature_extractor(speech_inputs, return_tensors="pt").input_features
|
||||
encoded_sequences_2 = feature_extractor(pt_speech_inputs, return_tensors="pt").input_features
|
||||
for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2):
|
||||
torch.testing.assert_close(enc_seq_1, enc_seq_2, rtol=1e-3, atol=1e-3)
|
||||
|
||||
@require_torch
|
||||
# Copied from tests.models.whisper.test_feature_extraction_whisper.WhisperFeatureExtractionTest.test_double_precision_pad
|
||||
def test_double_precision_pad(self):
|
||||
import torch
|
||||
|
||||
feature_extractor = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
|
||||
np_speech_inputs = np.random.rand(100, 32).astype(np.float64)
|
||||
py_speech_inputs = np_speech_inputs.tolist()
|
||||
|
||||
for inputs in [py_speech_inputs, np_speech_inputs]:
|
||||
np_processed = feature_extractor.pad([{"input_features": inputs}], return_tensors="np")
|
||||
self.assertTrue(np_processed.input_features.dtype == np.float32)
|
||||
pt_processed = feature_extractor.pad([{"input_features": inputs}], return_tensors="pt")
|
||||
self.assertTrue(pt_processed.input_features.dtype == torch.float32)
|
||||
|
||||
def _load_datasample(self, id):
|
||||
ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
||||
# automatic decoding with librispeech
|
||||
speech_sample = ds.sort("id")[id]["audio"]["array"]
|
||||
|
||||
return torch.from_numpy(speech_sample).unsqueeze(0)
|
||||
|
||||
def test_integration(self):
|
||||
# fmt: off
|
||||
EXPECTED_INPUT_FEATURES = torch.tensor(
|
||||
[
|
||||
-1.5621, -1.4236, -1.3335, -1.3991, -1.2881, -1.1133, -0.9710, -0.8895,
|
||||
-0.8280, -0.7376, -0.7194, -0.6896, -0.6849, -0.6788, -0.6545, -0.6610,
|
||||
-0.6566, -0.5738, -0.5252, -0.5533, -0.5887, -0.6116, -0.5971, -0.4956,
|
||||
-0.2881, -0.1512, 0.0299, 0.1762, 0.2728, 0.2236
|
||||
]
|
||||
)
|
||||
# fmt: on
|
||||
|
||||
input_speech = self._load_datasample(10)
|
||||
feature_extractor = SeamlessM4TFeatureExtractor()
|
||||
input_features = feature_extractor(input_speech, return_tensors="pt").input_features
|
||||
|
||||
feature_extractor(input_speech, return_tensors="pt").input_features[0, 5, :30]
|
||||
self.assertEqual(input_features.shape, (1, 279, 160))
|
||||
torch.testing.assert_close(input_features[0, 5, :30], EXPECTED_INPUT_FEATURES, rtol=1e-4, atol=1e-4)
|
||||
|
||||
def test_zero_mean_unit_variance_normalization_trunc_np_longest(self):
|
||||
feat_extract = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
|
||||
audio = self._load_datasample(1)
|
||||
audio = ((audio - audio.min()) / (audio.max() - audio.min())) * 65535 # Rescale to [0, 65535] to show issue
|
||||
audio = feat_extract.zero_mean_unit_var_norm([audio], attention_mask=None)[0]
|
||||
|
||||
self.assertTrue((audio.mean() < 1e-3).all())
|
||||
self.assertTrue(((audio.var() - 1).abs() < 1e-3).all())
|
||||
994
tests/models/seamless_m4t/test_modeling_seamless_m4t.py
Normal file
994
tests/models/seamless_m4t/test_modeling_seamless_m4t.py
Normal file
@@ -0,0 +1,994 @@
|
||||
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Testing suite for the PyTorch SeamlessM4T model."""
|
||||
|
||||
import copy
|
||||
import tempfile
|
||||
import unittest
|
||||
from functools import cached_property
|
||||
|
||||
import pytest
|
||||
|
||||
from transformers import SeamlessM4TConfig, is_speech_available, is_torch_available
|
||||
from transformers.testing_utils import require_speech, require_torch, slow, torch_device
|
||||
from transformers.trainer_utils import set_seed
|
||||
|
||||
from ...test_configuration_common import ConfigTester
|
||||
from ...test_modeling_common import (
|
||||
ModelTesterMixin,
|
||||
floats_tensor,
|
||||
ids_tensor,
|
||||
random_attention_mask,
|
||||
)
|
||||
from ...test_pipeline_mixin import PipelineTesterMixin
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers import (
|
||||
SeamlessM4TForSpeechToSpeech,
|
||||
SeamlessM4TForSpeechToText,
|
||||
SeamlessM4TForTextToSpeech,
|
||||
SeamlessM4TForTextToText,
|
||||
SeamlessM4TModel,
|
||||
)
|
||||
|
||||
if is_speech_available():
|
||||
from transformers import SeamlessM4TProcessor
|
||||
|
||||
|
||||
class SeamlessM4TModelTester:
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
input_modality="speech",
|
||||
batch_size=2,
|
||||
seq_length=4,
|
||||
is_training=True,
|
||||
use_input_mask=True,
|
||||
use_token_type_ids=True,
|
||||
use_labels=True,
|
||||
hidden_act="gelu",
|
||||
hidden_dropout_prob=0.1,
|
||||
attention_probs_dropout_prob=0.1,
|
||||
initializer_range=0.02,
|
||||
max_new_tokens=None,
|
||||
num_labels=3,
|
||||
num_choices=4,
|
||||
scope=None,
|
||||
vocab_size=20,
|
||||
t2u_vocab_size=20,
|
||||
hidden_size=6,
|
||||
num_hidden_layers=2,
|
||||
intermediate_size=6,
|
||||
max_position_embeddings=256,
|
||||
encoder_layers=2,
|
||||
decoder_layers=2,
|
||||
encoder_ffn_dim=6,
|
||||
decoder_ffn_dim=6,
|
||||
t2u_encoder_layers=2,
|
||||
t2u_decoder_layers=2,
|
||||
t2u_encoder_ffn_dim=6,
|
||||
t2u_decoder_ffn_dim=6,
|
||||
num_heads=2,
|
||||
vocoder_num_spkrs=5,
|
||||
vocoder_num_langs=5,
|
||||
upsample_initial_channel=32,
|
||||
unit_embed_dim=25,
|
||||
spkr_embed_dim=6,
|
||||
lang_embed_dim=6,
|
||||
num_conv_pos_embeddings=8,
|
||||
unit_hifi_gan_vocab_size=20,
|
||||
t2u_num_langs=0,
|
||||
t2u_max_new_tokens=25,
|
||||
t2u_offset_tgt_lang=0,
|
||||
vocoder_offset=0,
|
||||
):
|
||||
self.parent = parent
|
||||
self.input_modality = input_modality
|
||||
|
||||
self.batch_size = batch_size
|
||||
self.seq_length = seq_length
|
||||
self.is_training = is_training
|
||||
self.use_input_mask = use_input_mask
|
||||
self.use_token_type_ids = use_token_type_ids
|
||||
self.hidden_act = hidden_act
|
||||
self.hidden_dropout_prob = hidden_dropout_prob
|
||||
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
||||
self.initializer_range = initializer_range
|
||||
self.num_labels = num_labels
|
||||
self.num_choices = num_choices
|
||||
self.scope = scope
|
||||
|
||||
self.vocab_size = vocab_size
|
||||
self.t2u_vocab_size = t2u_vocab_size
|
||||
self.hidden_size = hidden_size
|
||||
self.num_hidden_layers = num_hidden_layers
|
||||
self.intermediate_size = intermediate_size
|
||||
self.max_position_embeddings = max_position_embeddings
|
||||
self.encoder_layers = encoder_layers
|
||||
self.decoder_layers = decoder_layers
|
||||
self.encoder_ffn_dim = encoder_ffn_dim
|
||||
self.decoder_ffn_dim = decoder_ffn_dim
|
||||
self.t2u_encoder_layers = t2u_encoder_layers
|
||||
self.t2u_decoder_layers = t2u_decoder_layers
|
||||
self.t2u_encoder_ffn_dim = t2u_encoder_ffn_dim
|
||||
self.t2u_decoder_ffn_dim = t2u_decoder_ffn_dim
|
||||
self.num_heads = num_heads
|
||||
self.num_attention_heads = num_heads
|
||||
|
||||
self.vocoder_num_spkrs = vocoder_num_spkrs
|
||||
self.vocoder_num_langs = vocoder_num_langs
|
||||
self.upsample_initial_channel = upsample_initial_channel
|
||||
self.unit_embed_dim = unit_embed_dim
|
||||
self.spkr_embed_dim = spkr_embed_dim
|
||||
self.num_conv_pos_embeddings = num_conv_pos_embeddings
|
||||
self.lang_embed_dim = lang_embed_dim
|
||||
|
||||
self.max_new_tokens = max_new_tokens
|
||||
|
||||
self.unit_hifi_gan_vocab_size = unit_hifi_gan_vocab_size
|
||||
self.t2u_num_langs = t2u_num_langs
|
||||
self.t2u_max_new_tokens = t2u_max_new_tokens
|
||||
self.t2u_offset_tgt_lang = t2u_offset_tgt_lang
|
||||
self.vocoder_offset = vocoder_offset
|
||||
|
||||
def prepare_config_and_inputs(self):
|
||||
if self.input_modality == "text":
|
||||
inputs = ids_tensor([self.batch_size, self.seq_length], self.vocab_size - 1)
|
||||
else:
|
||||
inputs = ids_tensor([self.batch_size, self.seq_length, 160], self.vocab_size - 1).float()
|
||||
|
||||
input_mask = None
|
||||
if self.use_input_mask:
|
||||
input_mask = random_attention_mask([self.batch_size, self.seq_length])
|
||||
|
||||
decoder_input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size - 1)
|
||||
|
||||
lm_labels = ids_tensor([self.batch_size, self.seq_length], self.num_labels)
|
||||
|
||||
config = self.get_config()
|
||||
|
||||
return config, inputs, decoder_input_ids, input_mask, lm_labels
|
||||
|
||||
def get_config(self):
|
||||
return SeamlessM4TConfig(
|
||||
hidden_act=self.hidden_act,
|
||||
hidden_dropout_prob=self.hidden_dropout_prob,
|
||||
attention_probs_dropout_prob=self.attention_probs_dropout_prob,
|
||||
initializer_range=self.initializer_range,
|
||||
vocab_size=self.vocab_size,
|
||||
t2u_vocab_size=self.t2u_vocab_size,
|
||||
hidden_size=self.hidden_size,
|
||||
speech_encoder_layers=self.num_heads,
|
||||
speech_encoder_intermediate_size=self.intermediate_size,
|
||||
max_position_embeddings=self.max_position_embeddings,
|
||||
encoder_layers=self.encoder_layers,
|
||||
decoder_layers=self.decoder_layers,
|
||||
encoder_ffn_dim=self.encoder_ffn_dim,
|
||||
decoder_ffn_dim=self.decoder_ffn_dim,
|
||||
t2u_encoder_layers=self.t2u_encoder_layers,
|
||||
t2u_decoder_layers=self.t2u_decoder_layers,
|
||||
t2u_encoder_ffn_dim=self.t2u_encoder_ffn_dim,
|
||||
t2u_decoder_ffn_dim=self.t2u_decoder_ffn_dim,
|
||||
num_attention_heads=self.num_heads,
|
||||
encoder_attention_heads=self.num_heads,
|
||||
decoder_attention_heads=self.num_heads,
|
||||
t2u_encoder_attention_heads=self.num_heads,
|
||||
t2u_decoder_attention_heads=self.num_heads,
|
||||
speech_encoder_attention_heads=self.num_heads,
|
||||
unit_hifigan_vocab_vise=self.t2u_vocab_size,
|
||||
vocoder_num_spkrs=self.vocoder_num_spkrs,
|
||||
vocoder_num_langs=self.vocoder_num_langs,
|
||||
upsample_initial_channel=self.upsample_initial_channel,
|
||||
unit_embed_dim=self.unit_embed_dim,
|
||||
spkr_embed_dim=self.spkr_embed_dim,
|
||||
num_conv_pos_embeddings=self.num_conv_pos_embeddings,
|
||||
lang_embed_dim=self.lang_embed_dim,
|
||||
max_new_tokens=self.max_new_tokens,
|
||||
unit_hifi_gan_vocab_size=self.unit_hifi_gan_vocab_size,
|
||||
t2u_num_langs=self.t2u_num_langs,
|
||||
t2u_max_new_tokens=self.t2u_max_new_tokens,
|
||||
t2u_offset_tgt_lang=self.t2u_offset_tgt_lang,
|
||||
vocoder_offset=self.vocoder_offset,
|
||||
)
|
||||
|
||||
def prepare_config_and_inputs_for_decoder(self):
|
||||
(
|
||||
config,
|
||||
input_ids,
|
||||
decoder_input_ids,
|
||||
input_mask,
|
||||
lm_labels,
|
||||
) = self.prepare_config_and_inputs()
|
||||
|
||||
config.is_decoder = True
|
||||
|
||||
encoder_hidden_states = floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
|
||||
encoder_attention_mask = ids_tensor([self.batch_size, self.seq_length], vocab_size=2)
|
||||
|
||||
return (
|
||||
config,
|
||||
input_ids,
|
||||
decoder_input_ids,
|
||||
input_mask,
|
||||
lm_labels,
|
||||
encoder_hidden_states,
|
||||
encoder_attention_mask,
|
||||
)
|
||||
|
||||
def create_and_check_model(self, config, input_ids, decoder_input_ids, input_mask, labels):
|
||||
model = SeamlessM4TModel(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
if self.input_modality == "text":
|
||||
result = model(input_ids=input_ids, attention_mask=input_mask, decoder_input_ids=decoder_input_ids)
|
||||
result = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
|
||||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size))
|
||||
else:
|
||||
result = model(input_features=input_ids, attention_mask=input_mask, decoder_input_ids=decoder_input_ids)
|
||||
result = model(input_features=input_ids, decoder_input_ids=decoder_input_ids)
|
||||
self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size))
|
||||
|
||||
decoder_output = result.logits
|
||||
decoder_past = result.past_key_values
|
||||
encoder_output = result.encoder_last_hidden_state
|
||||
|
||||
if self.input_modality == "text":
|
||||
seq_length = self.seq_length
|
||||
else:
|
||||
# if speech, expected length has been subsampled.
|
||||
seq_length = model._compute_sub_sample_lengths_from_attention_mask(input_mask).max().item()
|
||||
|
||||
self.parent.assertEqual(encoder_output.size(), (self.batch_size, seq_length, self.hidden_size))
|
||||
self.parent.assertEqual(decoder_output.size(), (self.batch_size, decoder_input_ids.shape[1], self.vocab_size))
|
||||
# There should be `num_layers` key value embeddings stored in decoder_past
|
||||
self.parent.assertEqual(len(decoder_past), config.decoder_layers)
|
||||
|
||||
def create_and_check_decoder_model_past_large_inputs(
|
||||
self,
|
||||
config,
|
||||
input_ids,
|
||||
decoder_input_ids,
|
||||
input_mask,
|
||||
lm_labels,
|
||||
encoder_hidden_states,
|
||||
encoder_attention_mask,
|
||||
):
|
||||
config.is_decoder = True
|
||||
model = SeamlessM4TModel(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
# make sure no pad token in decoder_input_ids
|
||||
decoder_input_ids = torch.clamp(decoder_input_ids, config.pad_token_id + 1)
|
||||
|
||||
# first forward pass
|
||||
outputs = model(
|
||||
input_ids, decoder_input_ids=decoder_input_ids, decoder_attention_mask=input_mask, use_cache=True
|
||||
)
|
||||
past_key_values = outputs.past_key_values
|
||||
|
||||
# create hypothetical multiple next token and extent to next_input_ids
|
||||
next_tokens = ids_tensor((self.batch_size, 3), config.vocab_size)
|
||||
next_mask = ids_tensor((self.batch_size, 3), vocab_size=2)
|
||||
|
||||
# append to next input_ids and
|
||||
next_input_ids = torch.cat([decoder_input_ids, next_tokens], dim=-1)
|
||||
next_attention_mask = torch.cat([input_mask, next_mask], dim=-1)
|
||||
|
||||
output_from_no_past = model(
|
||||
input_ids,
|
||||
decoder_input_ids=next_input_ids,
|
||||
decoder_attention_mask=next_attention_mask,
|
||||
output_hidden_states=True,
|
||||
)
|
||||
output_from_no_past = output_from_no_past["decoder_hidden_states"][0]
|
||||
output_from_past = model(
|
||||
input_ids,
|
||||
decoder_input_ids=next_tokens,
|
||||
decoder_attention_mask=next_attention_mask,
|
||||
past_key_values=past_key_values,
|
||||
output_hidden_states=True,
|
||||
)["decoder_hidden_states"][0]
|
||||
|
||||
# select random slice
|
||||
random_slice_idx = ids_tensor((1,), output_from_past.shape[-1]).item()
|
||||
output_from_no_past_slice = output_from_no_past[:, -3:, random_slice_idx].detach()
|
||||
output_from_past_slice = output_from_past[:, :, random_slice_idx].detach()
|
||||
|
||||
self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1])
|
||||
|
||||
# test that outputs are equal for slice
|
||||
self.parent.assertTrue(torch.allclose(output_from_past_slice, output_from_no_past_slice, atol=1e-3))
|
||||
|
||||
def prepare_config_and_inputs_for_common(self):
|
||||
config_and_inputs = self.prepare_config_and_inputs()
|
||||
(
|
||||
config,
|
||||
input_ids,
|
||||
decoder_input_ids,
|
||||
input_mask,
|
||||
lm_labels,
|
||||
) = config_and_inputs
|
||||
|
||||
input_name = "input_ids" if self.input_modality == "text" else "input_features"
|
||||
|
||||
inputs_dict = {
|
||||
input_name: input_ids,
|
||||
"attention_mask": input_mask,
|
||||
"decoder_input_ids": decoder_input_ids,
|
||||
"labels": lm_labels,
|
||||
}
|
||||
return config, inputs_dict
|
||||
|
||||
|
||||
@require_torch
|
||||
class SeamlessM4TModelWithSpeechInputTest(ModelTesterMixin, unittest.TestCase):
|
||||
is_encoder_decoder = True
|
||||
test_missing_keys = False
|
||||
|
||||
test_resize_embeddings = False
|
||||
|
||||
all_model_classes = (
|
||||
(
|
||||
SeamlessM4TModel,
|
||||
SeamlessM4TForSpeechToSpeech,
|
||||
SeamlessM4TForSpeechToText,
|
||||
)
|
||||
if is_torch_available()
|
||||
else ()
|
||||
)
|
||||
# Doesn't run generation tests. Custom generation method with a different interface
|
||||
all_generative_model_classes = ()
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = SeamlessM4TModelTester(self, input_modality="speech")
|
||||
self.config_tester = ConfigTester(self, config_class=SeamlessM4TConfig)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
@slow
|
||||
def test_model_from_pretrained(self):
|
||||
model_name = "facebook/hf-seamless-m4t-medium"
|
||||
model = SeamlessM4TModel.from_pretrained(model_name)
|
||||
self.assertIsNotNone(model)
|
||||
|
||||
@unittest.skip(reason="SeamlessM4TSpeechEncoder doesn't have an embedding layer")
|
||||
def test_inputs_embeds(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(reason="SeamlessM4TSpeechEncoder doesn't have an embedding layer")
|
||||
def test_inputs_embeds_matches_input_ids(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(
|
||||
reason="Expected missing keys serve when using SeamlessM4TForXXX.from_pretrained from a checkpoint saved by SeamlessM4TModel.save_pretrained."
|
||||
)
|
||||
def test_model_weights_reload_no_missing_tied_weights(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(reason="SeamlessM4TModel can takes input_ids or input_features")
|
||||
def test_forward_signature(self):
|
||||
pass
|
||||
|
||||
@pytest.mark.xfail(reason="This architecture seems to not compute gradients for some layer.")
|
||||
def test_training_gradient_checkpointing(self):
|
||||
super().test_training_gradient_checkpointing()
|
||||
|
||||
@pytest.mark.xfail(reason="This architecture seems to not compute gradients for some layer.")
|
||||
def test_training_gradient_checkpointing_use_reentrant_false(self):
|
||||
super().test_training_gradient_checkpointing_use_reentrant_false()
|
||||
|
||||
@pytest.mark.xfail(reason="This architecture seems to not compute gradients for some layer.")
|
||||
def test_training_gradient_checkpointing_use_reentrant_true(self):
|
||||
super().test_training_gradient_checkpointing_use_reentrant_true()
|
||||
|
||||
@unittest.skip(
|
||||
reason="This architecture has tied weights by default and there is no way to remove it, check: https://github.com/huggingface/transformers/pull/31771#issuecomment-2210915245"
|
||||
)
|
||||
def test_load_save_without_tied_weights(self):
|
||||
pass
|
||||
|
||||
def test_attention_outputs(self):
|
||||
# expected length is subsampled so need to change a bit this test
|
||||
if not self.has_attentions:
|
||||
self.skipTest(reason="Model does not output attentions")
|
||||
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
config.return_dict = True
|
||||
|
||||
seq_len = getattr(self.model_tester, "seq_length", None)
|
||||
decoder_seq_length = getattr(self.model_tester, "decoder_seq_length", seq_len)
|
||||
encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", seq_len)
|
||||
decoder_key_length = getattr(self.model_tester, "decoder_key_length", decoder_seq_length)
|
||||
encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length)
|
||||
# no more chunk_length test
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
inputs_dict["output_attentions"] = True
|
||||
inputs_dict["output_hidden_states"] = False
|
||||
config.return_dict = True
|
||||
model = model_class._from_config(config, attn_implementation="eager")
|
||||
config = model.config
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
|
||||
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||
|
||||
# check that output_attentions also work using config
|
||||
del inputs_dict["output_attentions"]
|
||||
config.output_attentions = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
|
||||
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||
|
||||
self.assertListEqual(
|
||||
list(attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
|
||||
)
|
||||
out_len = len(outputs)
|
||||
|
||||
if self.is_encoder_decoder:
|
||||
correct_outlen = 5
|
||||
|
||||
# loss is at first position
|
||||
if "labels" in inputs_dict:
|
||||
correct_outlen += 1 # loss is added to beginning
|
||||
if "past_key_values" in outputs:
|
||||
correct_outlen += 1 # past_key_values have been returned
|
||||
|
||||
self.assertEqual(out_len, correct_outlen)
|
||||
|
||||
# decoder attentions
|
||||
decoder_attentions = outputs.decoder_attentions
|
||||
self.assertIsInstance(decoder_attentions, (list, tuple))
|
||||
self.assertEqual(len(decoder_attentions), self.model_tester.num_hidden_layers)
|
||||
self.assertListEqual(
|
||||
list(decoder_attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, decoder_seq_length, decoder_key_length],
|
||||
)
|
||||
|
||||
# cross attentions
|
||||
cross_attentions = outputs.cross_attentions
|
||||
self.assertIsInstance(cross_attentions, (list, tuple))
|
||||
self.assertEqual(len(cross_attentions), self.model_tester.num_hidden_layers)
|
||||
|
||||
sub_sampled_length = (
|
||||
model._compute_sub_sample_lengths_from_attention_mask(inputs_dict["attention_mask"]).max().item()
|
||||
)
|
||||
self.assertListEqual(
|
||||
list(cross_attentions[0].shape[-3:]),
|
||||
[
|
||||
self.model_tester.num_attention_heads,
|
||||
decoder_seq_length,
|
||||
sub_sampled_length,
|
||||
],
|
||||
)
|
||||
|
||||
# Check attention is always last and order is fine
|
||||
inputs_dict["output_attentions"] = True
|
||||
inputs_dict["output_hidden_states"] = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
|
||||
if hasattr(self.model_tester, "num_hidden_states_types"):
|
||||
added_hidden_states = self.model_tester.num_hidden_states_types
|
||||
elif self.is_encoder_decoder:
|
||||
added_hidden_states = 2
|
||||
else:
|
||||
added_hidden_states = 1
|
||||
self.assertEqual(out_len + added_hidden_states, len(outputs))
|
||||
|
||||
self_attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
|
||||
|
||||
self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers)
|
||||
self.assertListEqual(
|
||||
list(self_attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length],
|
||||
)
|
||||
|
||||
def _prepare_config_and_inputs_for_retain_grad_hidden_states_attentions(self):
|
||||
# Layerdrop can skip layers and return None attentions. Disable it for this test.
|
||||
config, inputs_dict = super()._prepare_config_and_inputs_for_retain_grad_hidden_states_attentions()
|
||||
config.speech_encoder_layerdrop = 0.0
|
||||
config.encoder_layerdrop = 0.0
|
||||
config.decoder_layerdrop = 0.0
|
||||
return config, inputs_dict
|
||||
|
||||
|
||||
@require_torch
|
||||
class SeamlessM4TModelWithTextInputTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
is_encoder_decoder = True
|
||||
test_missing_keys = False
|
||||
|
||||
test_resize_embeddings = True
|
||||
|
||||
all_model_classes = (
|
||||
(
|
||||
SeamlessM4TModel,
|
||||
SeamlessM4TForTextToSpeech,
|
||||
SeamlessM4TForTextToText,
|
||||
)
|
||||
if is_torch_available()
|
||||
else ()
|
||||
)
|
||||
# Doesn't run generation tests. Has custom generation method with a different interface
|
||||
all_generative_model_classes = ()
|
||||
pipeline_model_mapping = (
|
||||
{
|
||||
"automatic-speech-recognition": SeamlessM4TForSpeechToText,
|
||||
"feature-extraction": SeamlessM4TModel,
|
||||
"text-to-audio": SeamlessM4TForTextToSpeech,
|
||||
}
|
||||
if is_torch_available()
|
||||
else {}
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = SeamlessM4TModelTester(self, input_modality="text")
|
||||
self.config_tester = ConfigTester(self, config_class=SeamlessM4TConfig)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
@slow
|
||||
def test_model_from_pretrained(self):
|
||||
model_name = "facebook/hf-seamless-m4t-medium"
|
||||
model = SeamlessM4TModel.from_pretrained(model_name)
|
||||
self.assertIsNotNone(model)
|
||||
|
||||
@unittest.skip(
|
||||
reason="Expected missing keys serve when using SeamlessM4TForXXX.from_pretrained from a checkpoint saved by SeamlessM4TModel.save_pretrained."
|
||||
)
|
||||
def test_model_weights_reload_no_missing_tied_weights(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(reason="SeamlessM4TModel can take input_ids or input_features")
|
||||
def test_forward_signature(self):
|
||||
pass
|
||||
|
||||
def test_decoder_model_past_with_large_inputs(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs_for_decoder()
|
||||
self.model_tester.create_and_check_decoder_model_past_large_inputs(*config_and_inputs)
|
||||
|
||||
@pytest.mark.xfail(reason="This architecture seems to not compute gradients for some layer.")
|
||||
def test_training_gradient_checkpointing(self):
|
||||
super().test_training_gradient_checkpointing()
|
||||
|
||||
@pytest.mark.xfail(reason="This architecture seems to not compute gradients for some layer.")
|
||||
def test_training_gradient_checkpointing_use_reentrant_false(self):
|
||||
super().test_training_gradient_checkpointing_use_reentrant_false()
|
||||
|
||||
@pytest.mark.xfail(reason="This architecture seems to not compute gradients for some layer.")
|
||||
def test_training_gradient_checkpointing_use_reentrant_true(self):
|
||||
super().test_training_gradient_checkpointing_use_reentrant_true()
|
||||
|
||||
@unittest.skip(
|
||||
reason="In training model, the first encoder layer is sometimes skipped. Training is not supported yet, so the test is ignored."
|
||||
)
|
||||
def test_retain_grad_hidden_states_attentions(self):
|
||||
pass
|
||||
|
||||
@unittest.skip(
|
||||
reason="This architecture has tied weights by default and there is no way to remove it, check: https://github.com/huggingface/transformers/pull/31771#issuecomment-2210915245"
|
||||
)
|
||||
def test_load_save_without_tied_weights(self):
|
||||
pass
|
||||
|
||||
|
||||
@require_torch
|
||||
class SeamlessM4TGenerationTest(unittest.TestCase):
|
||||
# test that non-standard generation works
|
||||
# test generation of: SeamlessM4TModel, SeamlessM4TForSpeechToSpeech, SeamlessM4TForSpeechToText, SeamlessM4TForTextToSpeech
|
||||
|
||||
def setUp(self):
|
||||
self.audio_model_tester = SeamlessM4TModelTester(self, input_modality="speech")
|
||||
self.text_model_tester = SeamlessM4TModelTester(self, input_modality="text")
|
||||
self.tmpdirname = tempfile.mkdtemp()
|
||||
|
||||
def update_generation(self, model):
|
||||
lang_code_to_id = {
|
||||
"fra": 4,
|
||||
"eng": 4,
|
||||
}
|
||||
|
||||
generation_config = copy.deepcopy(model.generation_config)
|
||||
|
||||
generation_config.__setattr__("text_decoder_lang_to_code_id", lang_code_to_id)
|
||||
generation_config.__setattr__("t2u_lang_code_to_id", lang_code_to_id)
|
||||
generation_config.__setattr__("vocoder_lang_code_to_id", lang_code_to_id)
|
||||
|
||||
generation_config._from_model_config = False
|
||||
|
||||
model.generation_config = generation_config
|
||||
|
||||
def prepare_text_input(self):
|
||||
config, inputs, decoder_input_ids, input_mask, lm_labels = self.text_model_tester.prepare_config_and_inputs()
|
||||
|
||||
input_dict = {
|
||||
"input_ids": inputs,
|
||||
"attention_mask": input_mask,
|
||||
"tgt_lang": "eng",
|
||||
"num_beams": 2,
|
||||
"do_sample": True,
|
||||
}
|
||||
|
||||
return config, input_dict
|
||||
|
||||
def prepare_speech_input(self):
|
||||
config, inputs, decoder_input_ids, input_mask, lm_labels = self.audio_model_tester.prepare_config_and_inputs()
|
||||
|
||||
input_dict = {
|
||||
"input_features": inputs,
|
||||
"attention_mask": input_mask,
|
||||
"tgt_lang": "fra",
|
||||
"num_beams": 2,
|
||||
"do_sample": True,
|
||||
}
|
||||
|
||||
return config, input_dict
|
||||
|
||||
def prepare_speech_and_text_input(self):
|
||||
config, inputs, decoder_input_ids, input_mask, lm_labels = self.audio_model_tester.prepare_config_and_inputs()
|
||||
|
||||
input_speech = {
|
||||
"input_features": inputs,
|
||||
"attention_mask": input_mask,
|
||||
"tgt_lang": "fra",
|
||||
"num_beams": 2,
|
||||
"do_sample": True,
|
||||
}
|
||||
|
||||
config, inputs, decoder_input_ids, input_mask, lm_labels = self.text_model_tester.prepare_config_and_inputs()
|
||||
|
||||
input_text = {
|
||||
"input_ids": inputs,
|
||||
"attention_mask": input_mask,
|
||||
"tgt_lang": "eng",
|
||||
"num_beams": 2,
|
||||
"do_sample": True,
|
||||
}
|
||||
return config, input_speech, input_text
|
||||
|
||||
def factory_generation_speech_test(self, model, inputs):
|
||||
set_seed(42)
|
||||
output = model.generate(**inputs)
|
||||
return output
|
||||
|
||||
def test_speech_generation(self):
|
||||
config, input_speech, input_text = self.prepare_speech_and_text_input()
|
||||
|
||||
model = SeamlessM4TModel(config=config)
|
||||
self.update_generation(model)
|
||||
model.save_pretrained(self.tmpdirname)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
output_original_text = self.factory_generation_speech_test(model, input_text)
|
||||
output_original_speech = self.factory_generation_speech_test(model, input_speech)
|
||||
|
||||
state_dict = model.state_dict()
|
||||
|
||||
text_model = SeamlessM4TForTextToSpeech.from_pretrained(self.tmpdirname)
|
||||
self.update_generation(text_model)
|
||||
text_model.to(torch_device)
|
||||
text_model.eval()
|
||||
|
||||
output_text = self.factory_generation_speech_test(model, input_text)
|
||||
|
||||
speech_model = SeamlessM4TForSpeechToSpeech.from_pretrained(self.tmpdirname)
|
||||
self.update_generation(speech_model)
|
||||
speech_model.to(torch_device)
|
||||
speech_model.eval()
|
||||
|
||||
for name, tensor in speech_model.state_dict().items():
|
||||
right_tensor = state_dict.get(name)
|
||||
self.assertEqual(tensor.tolist(), right_tensor.tolist(), f"Tensor {name}")
|
||||
|
||||
output_speech = self.factory_generation_speech_test(model, input_speech)
|
||||
|
||||
# test same text output from input text
|
||||
self.assertListEqual(output_original_text[0].ravel().tolist(), output_text[0].ravel().tolist())
|
||||
self.assertListEqual(output_original_text[1].ravel().tolist(), output_text[1].ravel().tolist())
|
||||
|
||||
# test same speech output from input text
|
||||
# assertTrue because super long list makes this hang in case of failure
|
||||
self.assertTrue(
|
||||
output_original_speech[0].ravel().tolist() == output_speech[0].ravel().tolist(),
|
||||
"Speech generated was different",
|
||||
)
|
||||
self.assertTrue(
|
||||
output_original_speech[1].ravel().tolist() == output_speech[1].ravel().tolist(),
|
||||
"Speech generated was different",
|
||||
)
|
||||
|
||||
def test_text_generation(self):
|
||||
config, input_speech, input_text = self.prepare_speech_and_text_input()
|
||||
|
||||
# to return speech
|
||||
input_speech["generate_speech"] = False
|
||||
input_text["generate_speech"] = False
|
||||
|
||||
model = SeamlessM4TModel(config=config)
|
||||
self.update_generation(model)
|
||||
model.save_pretrained(self.tmpdirname)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
output_original_text = self.factory_generation_speech_test(model, input_text)
|
||||
output_original_speech = self.factory_generation_speech_test(model, input_speech)
|
||||
|
||||
# other models don't need it
|
||||
input_speech.pop("generate_speech")
|
||||
input_text.pop("generate_speech")
|
||||
|
||||
state_dict = model.state_dict()
|
||||
|
||||
text_model = SeamlessM4TForTextToText.from_pretrained(self.tmpdirname)
|
||||
self.update_generation(text_model)
|
||||
text_model.to(torch_device)
|
||||
text_model.eval()
|
||||
|
||||
for name, tensor in text_model.state_dict().items():
|
||||
right_tensor = state_dict.get(name)
|
||||
self.assertEqual(tensor.tolist(), right_tensor.tolist())
|
||||
|
||||
output_text = self.factory_generation_speech_test(text_model, input_text)
|
||||
|
||||
speech_model = SeamlessM4TForSpeechToText.from_pretrained(self.tmpdirname)
|
||||
|
||||
for name, tensor in speech_model.state_dict().items():
|
||||
right_tensor = state_dict.get(name)
|
||||
self.assertEqual(tensor.tolist(), right_tensor.tolist(), f"Tensor {name}")
|
||||
|
||||
self.update_generation(speech_model)
|
||||
speech_model.to(torch_device)
|
||||
speech_model.eval()
|
||||
|
||||
output_speech = self.factory_generation_speech_test(speech_model, input_speech)
|
||||
|
||||
# test same text output from input text
|
||||
self.assertListEqual(output_original_text[0].ravel().tolist(), output_text.ravel().tolist())
|
||||
|
||||
# test same speech output from input text
|
||||
self.assertListEqual(output_original_speech[0].ravel().tolist(), output_speech.ravel().tolist())
|
||||
|
||||
def test_generation(self):
|
||||
config, input_speech, input_text = self.prepare_speech_and_text_input()
|
||||
|
||||
input_speech["num_beams"] = 3
|
||||
input_speech["do_sample"] = True
|
||||
input_speech["num_return_sequences"] = 3
|
||||
|
||||
input_text["num_beams"] = 3
|
||||
input_text["do_sample"] = True
|
||||
input_text["num_return_sequences"] = 3
|
||||
|
||||
for model_class in [SeamlessM4TForSpeechToSpeech, SeamlessM4TForSpeechToText, SeamlessM4TModel]:
|
||||
model = model_class(config=config)
|
||||
self.update_generation(model)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
output = model.generate(**input_speech)
|
||||
output = output[0] if isinstance(output, tuple) else output
|
||||
|
||||
self.assertEqual(output.shape[0], 3 * input_speech["input_features"].shape[0])
|
||||
|
||||
for model_class in [SeamlessM4TForTextToSpeech, SeamlessM4TForTextToText, SeamlessM4TModel]:
|
||||
model = model_class(config=config)
|
||||
self.update_generation(model)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
output = model.generate(**input_text)
|
||||
|
||||
output = output[0] if isinstance(output, tuple) else output
|
||||
|
||||
self.assertEqual(output.shape[0], 3 * input_text["input_ids"].shape[0])
|
||||
|
||||
|
||||
@require_torch
|
||||
class SeamlessM4TModelIntegrationTest(unittest.TestCase):
|
||||
repo_id = "facebook/hf-seamless-m4t-medium"
|
||||
|
||||
def assertListAlmostEqual(self, list1, list2, tol=1e-3):
|
||||
self.assertEqual(len(list1), len(list2))
|
||||
for a, b in zip(list1, list2):
|
||||
self.assertAlmostEqual(a, b, delta=tol)
|
||||
|
||||
@cached_property
|
||||
def processor(self):
|
||||
return SeamlessM4TProcessor.from_pretrained(self.repo_id)
|
||||
|
||||
@cached_property
|
||||
def input_text(self):
|
||||
# corresponds to "C'est un test." with seamlessM4T_medium checkpoint
|
||||
|
||||
input_ids = torch.tensor([[256057, 152, 248116, 354, 159, 7356, 248075, 3]]) # fmt: skip
|
||||
|
||||
input_ids = input_ids.to(torch_device)
|
||||
|
||||
attention_mask = torch.ones_like(input_ids).to(torch_device)
|
||||
|
||||
inputs = {
|
||||
"attention_mask": attention_mask,
|
||||
"input_ids": input_ids,
|
||||
}
|
||||
|
||||
return inputs
|
||||
|
||||
@cached_property
|
||||
def input_audio(self):
|
||||
set_seed(42)
|
||||
seq_len = 20000
|
||||
sampling_rate = 16000
|
||||
input_features = torch.rand((2, seq_len))
|
||||
|
||||
return self.processor(audio=[input_features.tolist()], sampling_rate=sampling_rate, return_tensors="pt").to(
|
||||
torch_device
|
||||
)
|
||||
|
||||
def factory_test_task(self, class1, class2, inputs, class1_kwargs, class2_kwargs):
|
||||
model1 = class1.from_pretrained(self.repo_id).to(torch_device)
|
||||
model2 = class2.from_pretrained(self.repo_id).to(torch_device)
|
||||
|
||||
set_seed(42)
|
||||
output_1 = model1.generate(**inputs, **class1_kwargs)
|
||||
set_seed(42)
|
||||
output_2 = model2.generate(**inputs, **class2_kwargs)
|
||||
|
||||
for key in output_1:
|
||||
if isinstance(output_1[key], torch.Tensor):
|
||||
if len(output_1[key].shape) == 0:
|
||||
self.assertEqual(output_1[key].item(), output_2[key].item())
|
||||
else:
|
||||
self.assertListAlmostEqual(output_1[key].squeeze().tolist(), output_2[key].squeeze().tolist())
|
||||
|
||||
@slow
|
||||
def test_to_eng_text(self):
|
||||
model = SeamlessM4TModel.from_pretrained(self.repo_id).to(torch_device)
|
||||
|
||||
# test text - tgt lang: eng
|
||||
|
||||
expected_text_tokens = [3, 256047, 3291, 248116, 248066, 9, 7356, 248075, 3] # fmt: skip
|
||||
|
||||
# fmt: off
|
||||
expected_unit_tokens = [
|
||||
2,10051,8980,8212,949,1270,4311,1123,5918,2333,5311,3882,2415,5284,1123,612,8816,6370,5386,7334,4345,5645,
|
||||
9437,5748,1378,9818,4319,7968,7375,2909,9119,5151,8728,5335,3896,4013,8939,8885,6048,9530,3167,5833,1072,693,
|
||||
431,9867,364,7909,4608,5938,1889,9984,7947,4944,6171,3767,9861,9169,1187,8365,4571,7635,7784,7635,800,2393,
|
||||
32,5380,5852,8289,2530,2762,1833,2056,3553,4641,3553,5683,370,2288,1344,1518,7534,703,8359,7699,2
|
||||
]
|
||||
# fmt: on
|
||||
|
||||
expected_wav_slice = [-3e-05, -0.0004, -0.00037, -0.00013, -6e-05, 0.00012, -0.00016, 0.00025, 7e-05, -3e-05] # fmt: skip
|
||||
|
||||
set_seed(42)
|
||||
output = model.generate(**self.input_text, num_beams=1, tgt_lang="eng", return_intermediate_token_ids=True)
|
||||
|
||||
self.assertListEqual(expected_text_tokens, output.sequences.squeeze().tolist())
|
||||
# FOR NOW, only first units correspondence
|
||||
self.assertListEqual(expected_unit_tokens[:10], output.unit_sequences.squeeze().tolist()[:10])
|
||||
|
||||
self.assertListAlmostEqual(expected_wav_slice, output.waveform.squeeze().tolist()[50:60])
|
||||
|
||||
@slow
|
||||
def test_to_swh_text(self):
|
||||
model = SeamlessM4TModel.from_pretrained(self.repo_id).to(torch_device)
|
||||
|
||||
# test text - tgt lang: swh
|
||||
|
||||
expected_text_tokens = [3, 256168, 1665, 188589, 7040, 248075, 3] # fmt: skip
|
||||
|
||||
# fmt: off
|
||||
expected_unit_tokens = [
|
||||
2,10071,5729,9995,3089,7546,1204,1721,2532,4340,5623,3496,432,7730,9096,7677,3143,8211,6447,8399,4248,3565,
|
||||
4529,7700,9308,217,6476,3485,9667,3194,8476,4923,5593,1148,4466,7416,4872,463,4872,253,2348,4640,3450,2133,
|
||||
6318,2806,817,7613,2698,6563,8712,8344,9286,6878,6387,4281,6387,640,6387,3200,640,8355,640,6708,979,1738,2
|
||||
]
|
||||
# fmt: on
|
||||
|
||||
expected_wav_slice = [1e-05, -7e-05, -4e-05, -4e-05, -6e-05, -9e-05, -0.0001, -2e-05, -7e-05, -2e-05] # fmt: skip
|
||||
|
||||
set_seed(42)
|
||||
output = model.generate(**self.input_text, num_beams=1, tgt_lang="swh", return_intermediate_token_ids=True)
|
||||
|
||||
self.assertListEqual(expected_text_tokens, output.sequences.squeeze().tolist())
|
||||
self.assertListEqual(expected_unit_tokens[:10], output.unit_sequences.squeeze().tolist()[:10])
|
||||
|
||||
self.assertListAlmostEqual(expected_wav_slice, output.waveform.squeeze().tolist()[50:60])
|
||||
|
||||
@require_speech
|
||||
@slow
|
||||
def test_to_rus_speech(self):
|
||||
model = SeamlessM4TModel.from_pretrained(self.repo_id).to(torch_device)
|
||||
|
||||
# test audio - tgt lang: rus
|
||||
|
||||
expected_text_tokens = [3, 256147, 1197, 73565, 3413, 537, 233331, 248075, 3] # fmt: skip
|
||||
|
||||
# fmt: off
|
||||
expected_unit_tokens = [
|
||||
2, 10067, 5729, 4798, 9631, 8378, 4446, 2393, 6901, 5983, 2817, 4629, 8532, 1991, 2931, 8576, 8857, 5936, 4317,
|
||||
9000, 7740, 7995, 1225, 5980, 6094, 1420, 5373, 8771, 6600, 4487, 7029, 3630, 6740, 4870, 1483, 3003, 5585, 5511,
|
||||
7465, 3222, 32, 6272, 1950, 3120, 5368, 639, 3713, 5935, 7943, 567, 6129, 6822, 1226, 5063, 9878, 7756, 8825, 1078, 5943,
|
||||
457, 9282, 9668, 817, 7613, 2698, 6563, 8712, 8704, 9286, 8704, 6387, 4281, 6387, 640, 3200, 6387, 640, 8355, 6708, 979, 1738, 2
|
||||
]
|
||||
# fmt: on
|
||||
|
||||
expected_wav_slice = [0.00013, 0.00012, 0.00014, 3e-05, 0.0, -6e-05, -0.00018, -0.00016, -0.00021, -0.00018] # fmt: skip
|
||||
|
||||
set_seed(42)
|
||||
output = model.generate(**self.input_audio, num_beams=1, tgt_lang="rus", return_intermediate_token_ids=True)
|
||||
|
||||
self.assertListEqual(expected_text_tokens, output.sequences.squeeze().tolist())
|
||||
self.assertListEqual(expected_unit_tokens[:10], output.unit_sequences.squeeze().tolist()[:10])
|
||||
|
||||
self.assertListAlmostEqual(expected_wav_slice, output.waveform.squeeze().tolist()[50:60])
|
||||
|
||||
@slow
|
||||
def test_text_to_text_model(self):
|
||||
kwargs1 = {"tgt_lang": "eng", "return_intermediate_token_ids": True, "generate_speech": False}
|
||||
kwargs2 = {
|
||||
"tgt_lang": "eng",
|
||||
"output_hidden_states": True,
|
||||
"return_dict_in_generate": True,
|
||||
"output_scores": True,
|
||||
}
|
||||
self.factory_test_task(SeamlessM4TModel, SeamlessM4TForTextToText, self.input_text, kwargs1, kwargs2)
|
||||
|
||||
@require_speech
|
||||
@slow
|
||||
def test_speech_to_text_model(self):
|
||||
kwargs1 = {"tgt_lang": "eng", "return_intermediate_token_ids": True, "generate_speech": False}
|
||||
kwargs2 = {
|
||||
"tgt_lang": "eng",
|
||||
"output_hidden_states": True,
|
||||
"return_dict_in_generate": True,
|
||||
"output_scores": True,
|
||||
}
|
||||
self.factory_test_task(SeamlessM4TModel, SeamlessM4TForSpeechToText, self.input_audio, kwargs1, kwargs2)
|
||||
|
||||
@require_speech
|
||||
@slow
|
||||
def test_speech_to_speech_model(self):
|
||||
kwargs1 = {"tgt_lang": "eng", "return_intermediate_token_ids": True}
|
||||
self.factory_test_task(SeamlessM4TModel, SeamlessM4TForSpeechToSpeech, self.input_audio, kwargs1, kwargs1)
|
||||
|
||||
@slow
|
||||
def test_text_to_speech_model(self):
|
||||
kwargs1 = {"tgt_lang": "eng", "return_intermediate_token_ids": True}
|
||||
|
||||
self.factory_test_task(SeamlessM4TModel, SeamlessM4TForTextToSpeech, self.input_text, kwargs1, kwargs1)
|
||||
103
tests/models/seamless_m4t/test_processing_seamless_m4t.py
Normal file
103
tests/models/seamless_m4t/test_processing_seamless_m4t.py
Normal file
@@ -0,0 +1,103 @@
|
||||
# Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from transformers import SeamlessM4TFeatureExtractor, SeamlessM4TProcessor
|
||||
from transformers.models.seamless_m4t import (
|
||||
SeamlessM4TTokenizer,
|
||||
SeamlessM4TTokenizerFast,
|
||||
)
|
||||
from transformers.testing_utils import require_torch
|
||||
|
||||
from .test_feature_extraction_seamless_m4t import floats_list
|
||||
|
||||
|
||||
@require_torch
|
||||
class SeamlessM4TProcessorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.checkpoint = "facebook/hf-seamless-m4t-medium"
|
||||
self.tmpdirname = tempfile.mkdtemp()
|
||||
|
||||
def get_tokenizer(self, **kwargs):
|
||||
return SeamlessM4TTokenizer.from_pretrained(self.checkpoint, **kwargs)
|
||||
|
||||
def get_feature_extractor(self, **kwargs):
|
||||
return SeamlessM4TFeatureExtractor.from_pretrained(self.checkpoint, **kwargs)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmpdirname)
|
||||
|
||||
def test_save_load_pretrained_default(self):
|
||||
tokenizer = self.get_tokenizer()
|
||||
feature_extractor = self.get_feature_extractor()
|
||||
|
||||
processor = SeamlessM4TProcessor(tokenizer=tokenizer, feature_extractor=feature_extractor)
|
||||
|
||||
processor.save_pretrained(self.tmpdirname)
|
||||
processor = SeamlessM4TProcessor.from_pretrained(self.tmpdirname)
|
||||
|
||||
self.assertEqual(processor.tokenizer.get_vocab(), tokenizer.get_vocab())
|
||||
tokenizer_instance = isinstance(processor.tokenizer, (SeamlessM4TTokenizerFast, SeamlessM4TTokenizer))
|
||||
self.assertTrue(tokenizer_instance)
|
||||
|
||||
self.assertEqual(processor.feature_extractor.to_json_string(), feature_extractor.to_json_string())
|
||||
self.assertIsInstance(processor.feature_extractor, SeamlessM4TFeatureExtractor)
|
||||
|
||||
# Copied from test.models.whisper.test_processing_whisper.WhisperProcessorTest.test_feature_extractor with Whisper->SeamlessM4T
|
||||
def test_feature_extractor(self):
|
||||
feature_extractor = self.get_feature_extractor()
|
||||
tokenizer = self.get_tokenizer()
|
||||
|
||||
processor = SeamlessM4TProcessor(tokenizer=tokenizer, feature_extractor=feature_extractor)
|
||||
|
||||
raw_speech = floats_list((3, 1000))
|
||||
|
||||
input_feat_extract = feature_extractor(raw_speech, return_tensors="np")
|
||||
input_processor = processor(audio=raw_speech, return_tensors="np")
|
||||
|
||||
for key in input_feat_extract:
|
||||
self.assertAlmostEqual(input_feat_extract[key].sum(), input_processor[key].sum(), delta=1e-2)
|
||||
|
||||
# Copied from test.models.whisper.test_processing_whisper.WhisperProcessorTest.test_tokenizer with Whisper->SeamlessM4T
|
||||
def test_tokenizer(self):
|
||||
feature_extractor = self.get_feature_extractor()
|
||||
tokenizer = self.get_tokenizer()
|
||||
|
||||
processor = SeamlessM4TProcessor(tokenizer=tokenizer, feature_extractor=feature_extractor)
|
||||
|
||||
input_str = "This is a test string"
|
||||
|
||||
encoded_processor = processor(text=input_str)
|
||||
|
||||
encoded_tok = tokenizer(input_str)
|
||||
|
||||
for key in encoded_tok:
|
||||
self.assertListEqual(encoded_tok[key], encoded_processor[key])
|
||||
|
||||
# Copied from test.models.whisper.test_processing_whisper.WhisperProcessorTest.test_tokenizer_decode with Whisper->SeamlessM4T
|
||||
def test_tokenizer_decode(self):
|
||||
feature_extractor = self.get_feature_extractor()
|
||||
tokenizer = self.get_tokenizer()
|
||||
|
||||
processor = SeamlessM4TProcessor(tokenizer=tokenizer, feature_extractor=feature_extractor)
|
||||
|
||||
predicted_ids = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]
|
||||
|
||||
decoded_processor = processor.batch_decode(predicted_ids)
|
||||
decoded_tok = tokenizer.batch_decode(predicted_ids)
|
||||
|
||||
self.assertListEqual(decoded_tok, decoded_processor)
|
||||
438
tests/models/seamless_m4t/test_tokenization_seamless_m4t.py
Normal file
438
tests/models/seamless_m4t/test_tokenization_seamless_m4t.py
Normal file
@@ -0,0 +1,438 @@
|
||||
# Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import unittest
|
||||
|
||||
from transformers import (
|
||||
AddedToken,
|
||||
BatchEncoding,
|
||||
SeamlessM4TTokenizer,
|
||||
is_torch_available,
|
||||
)
|
||||
from transformers.testing_utils import (
|
||||
get_tests_dir,
|
||||
nested_simplify,
|
||||
require_sentencepiece,
|
||||
require_tokenizers,
|
||||
require_torch,
|
||||
)
|
||||
|
||||
from ...test_tokenization_common import TokenizerTesterMixin
|
||||
|
||||
|
||||
SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model")
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
from transformers.models.m2m_100.modeling_m2m_100 import shift_tokens_right
|
||||
|
||||
EN_CODE = 256047
|
||||
RO_CODE = 256145
|
||||
|
||||
SMALL_TRAINING_CORPUS = [
|
||||
["This is the first sentence.", "This is the second one."],
|
||||
["This sentence (contains #) over symbols and numbers 12 3.", "But not this one."],
|
||||
]
|
||||
|
||||
|
||||
@require_sentencepiece
|
||||
@require_tokenizers
|
||||
class SeamlessM4TTokenizationTest(TokenizerTesterMixin, unittest.TestCase):
|
||||
from_pretrained_id = "facebook/hf-seamless-m4t-medium"
|
||||
tokenizer_class = SeamlessM4TTokenizer
|
||||
test_rust_tokenizer = True
|
||||
|
||||
integration_expected_tokens = ['▁This', '▁is', '▁a', '▁test', '▁', '😊', '▁I', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fals', 'é', '.', '▁生活', '的', '真', '<unk>', '是', '▁Hi', '▁Hello', '▁Hi', '▁Hello', '▁Hello', '<s>', '▁hi', '<s>', 'th', 'ere', '▁The', '▁following', '▁string', '▁should', '▁be', '▁properly', '▁enc', 'od', 'ed', ':', '▁Hello', '.', '▁But', '▁ir', 'd', '▁and', '▁ปี', '▁ir', 'd', '▁ด', '▁Hey', '▁how', '▁are', '▁you', '▁doing'] # fmt: skip
|
||||
integration_expected_token_ids = [9680, 248, 9, 7356, 248059, 253515, 117, 1398, 79519, 108, 855, 45299, 248079, 540, 3423, 248, 52428, 248132, 248075, 182892, 248506, 249573, 1, 249221, 2867, 94124, 2867, 94124, 94124, 2, 435, 2, 419, 275, 1617, 45893, 191422, 12516, 280, 242514, 12025, 129, 76, 248144, 94124, 248075, 9062, 528, 248072, 540, 99681, 528, 248072, 34744, 27426, 11657, 2442, 1259, 34512] # fmt: skip
|
||||
expected_tokens_from_ids = ['▁This', '▁is', '▁a', '▁test', '▁', '😊', '▁I', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fals', 'é', '.', '▁生活', '的', '真', '<unk>', '是', '▁Hi', '▁Hello', '▁Hi', '▁Hello', '▁Hello', '<s>', '▁hi', '<s>', 'th', 'ere', '▁The', '▁following', '▁string', '▁should', '▁be', '▁properly', '▁enc', 'od', 'ed', ':', '▁Hello', '.', '▁But', '▁ir', 'd', '▁and', '▁ปี', '▁ir', 'd', '▁ด', '▁Hey', '▁how', '▁are', '▁you', '▁doing'] # fmt: skip
|
||||
integration_expected_decoded_text = "This is a test 😊 I was born in 92000, and this is falsé. 生活的真<unk>是 Hi Hello Hi Hello Hello<s> hi<s>there The following string should be properly encoded: Hello. But ird and ปี ird ด Hey how are you doing"
|
||||
|
||||
def test_batch_encode_plus_batch_sequence_length(self):
|
||||
# Override the parent test because SeamlessM4T uses padding=True by default
|
||||
# Tests that all encoded values have the correct size
|
||||
tokenizer = self.get_tokenizer(do_lower_case=False)
|
||||
sequences = [
|
||||
"Testing batch encode plus",
|
||||
"Testing batch encode plus with different sequence lengths",
|
||||
"Testing batch encode plus with different sequence lengths correctly pads",
|
||||
]
|
||||
|
||||
# For SeamlessM4T, encode with explicit padding=False for individual sequences too
|
||||
encoded_sequences = [tokenizer(sequence, padding=False) for sequence in sequences]
|
||||
encoded_sequences_batch = tokenizer(sequences, padding=False)
|
||||
self.assertListEqual(encoded_sequences, self.convert_batch_to_list_format(encoded_sequences_batch))
|
||||
|
||||
def test_padding_to_multiple_of(self):
|
||||
tokenizers = self.get_tokenizers()
|
||||
for tokenizer in tokenizers:
|
||||
with self.subTest(f"{tokenizer.__class__.__name__}"):
|
||||
if tokenizer.pad_token is None:
|
||||
self.skipTest(reason="No padding token.")
|
||||
else:
|
||||
empty_tokens = tokenizer("", padding=True, pad_to_multiple_of=8)
|
||||
normal_tokens = tokenizer("This is a sample input", padding=True, pad_to_multiple_of=8)
|
||||
for key, value in empty_tokens.items():
|
||||
self.assertEqual(len(value) % 8, 0, f"BatchEncoding.{key} is not multiple of 8")
|
||||
for key, value in normal_tokens.items():
|
||||
self.assertEqual(len(value) % 8, 0, f"BatchEncoding.{key} is not multiple of 8")
|
||||
|
||||
# default to padding=True so need to precise which padding is called
|
||||
normal_tokens = tokenizer("This", pad_to_multiple_of=8, padding=False)
|
||||
for key, value in normal_tokens.items():
|
||||
self.assertNotEqual(len(value) % 8, 0, f"BatchEncoding.{key} is not multiple of 8")
|
||||
|
||||
# Should also work with truncation
|
||||
normal_tokens = tokenizer("This", padding=True, truncation=True, pad_to_multiple_of=8)
|
||||
for key, value in normal_tokens.items():
|
||||
self.assertEqual(len(value) % 8, 0, f"BatchEncoding.{key} is not multiple of 8")
|
||||
|
||||
# truncation to something which is not a multiple of pad_to_multiple_of raises an error
|
||||
self.assertRaises(
|
||||
ValueError,
|
||||
tokenizer.__call__,
|
||||
"This",
|
||||
padding=True,
|
||||
truncation=True,
|
||||
max_length=12,
|
||||
pad_to_multiple_of=8,
|
||||
)
|
||||
|
||||
@require_torch
|
||||
def test_prepare_seq2seq_batch(self):
|
||||
if not self.test_seq2seq:
|
||||
self.skipTest(reason="test_seq2seq is set to False")
|
||||
|
||||
tokenizers = self.get_tokenizers()
|
||||
for tokenizer in tokenizers:
|
||||
with self.subTest(f"{tokenizer.__class__.__name__}"):
|
||||
# Longer text that will definitely require truncation.
|
||||
src_text = [
|
||||
" UN Chief Says There Is No Military Solution in Syria",
|
||||
" Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for"
|
||||
" Syria is that 'there is no military solution' to the nearly five-year conflict and more weapons"
|
||||
" will only worsen the violence and misery for millions of people.",
|
||||
]
|
||||
tgt_text = [
|
||||
"Şeful ONU declară că nu există o soluţie militară în Siria",
|
||||
"Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al"
|
||||
' Rusiei pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi'
|
||||
" că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.",
|
||||
]
|
||||
try:
|
||||
batch = tokenizer.prepare_seq2seq_batch(
|
||||
src_texts=src_text,
|
||||
tgt_texts=tgt_text,
|
||||
max_length=3,
|
||||
max_target_length=10,
|
||||
return_tensors="pt",
|
||||
src_lang="eng",
|
||||
tgt_lang="ron",
|
||||
pad_to_multiple_of=None,
|
||||
)
|
||||
except NotImplementedError:
|
||||
self.skipTest(reason="Encountered NotImplementedError when calling prepare_seq2seq_batch")
|
||||
self.assertEqual(batch.input_ids.shape[1], 3)
|
||||
self.assertEqual(batch.labels.shape[1], 10)
|
||||
|
||||
# TODO: not working for tgt_text
|
||||
# max_target_length will default to max_length if not specified
|
||||
batch = tokenizer.prepare_seq2seq_batch(
|
||||
src_texts=src_text,
|
||||
tgt_texts=tgt_text,
|
||||
max_length=4,
|
||||
return_tensors="pt",
|
||||
pad_to_multiple_of=None,
|
||||
)
|
||||
self.assertEqual(batch.input_ids.shape[1], 4)
|
||||
self.assertEqual(batch.labels.shape[1], 4)
|
||||
|
||||
batch_encoder_only = tokenizer.prepare_seq2seq_batch(
|
||||
src_texts=src_text,
|
||||
max_length=4,
|
||||
max_target_length=10,
|
||||
return_tensors="pt",
|
||||
pad_to_multiple_of=None,
|
||||
)
|
||||
self.assertEqual(batch_encoder_only.input_ids.shape[1], 4)
|
||||
self.assertEqual(batch_encoder_only.attention_mask.shape[1], 4)
|
||||
self.assertNotIn("decoder_input_ids", batch_encoder_only)
|
||||
|
||||
# Copied from tests.models.nllb.test_tokenization_nllb.NllbTokenizationTest.test_special_tokens_initialization
|
||||
def test_special_tokens_initialization(self):
|
||||
for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
|
||||
with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"):
|
||||
added_tokens = [AddedToken("<special>", lstrip=True)]
|
||||
|
||||
tokenizer_r = self.get_tokenizer(pretrained_name, additional_special_tokens=added_tokens, **kwargs)
|
||||
r_output = tokenizer_r.encode("Hey this is a <special> token")
|
||||
|
||||
special_token_id = tokenizer_r.encode("<special>", add_special_tokens=False)[0]
|
||||
|
||||
self.assertTrue(special_token_id in r_output)
|
||||
|
||||
def test_training_new_tokenizer(self):
|
||||
# This feature only exists for fast tokenizers
|
||||
if not self.test_rust_tokenizer:
|
||||
self.skipTest(reason="test_rust_tokenizer is set to False")
|
||||
|
||||
tokenizer = self.get_tokenizer()
|
||||
new_tokenizer = tokenizer.train_new_from_iterator(SMALL_TRAINING_CORPUS, 100)
|
||||
|
||||
# Test we can use the new tokenizer with something not seen during training
|
||||
inputs = new_tokenizer(["This is the first sentence", "This sentence is different 🤗."])
|
||||
self.assertEqual(len(inputs["input_ids"]), 2)
|
||||
decoded_input = new_tokenizer.decode(inputs["input_ids"][0], skip_special_tokens=True)
|
||||
expected_result = "This is the first sentence"
|
||||
|
||||
if tokenizer.backend_tokenizer.normalizer is not None:
|
||||
expected_result = tokenizer.backend_tokenizer.normalizer.normalize_str(expected_result)
|
||||
self.assertEqual(expected_result, decoded_input)
|
||||
|
||||
# We check that the parameters of the tokenizer remained the same
|
||||
# Check we have the same number of added_tokens for both pair and non-pair inputs.
|
||||
# make sure it has the same prefix tokens first
|
||||
new_tokenizer.tgt_lang = tokenizer.tgt_lang
|
||||
tokenizer.tgt_lang = tokenizer.tgt_lang
|
||||
self.assertEqual(tokenizer.num_special_tokens_to_add(False), new_tokenizer.num_special_tokens_to_add(False))
|
||||
self.assertEqual(tokenizer.num_special_tokens_to_add(True), new_tokenizer.num_special_tokens_to_add(True))
|
||||
|
||||
# Check we have the correct max_length for both pair and non-pair inputs.
|
||||
self.assertEqual(tokenizer.max_len_single_sentence, new_tokenizer.max_len_single_sentence)
|
||||
self.assertEqual(tokenizer.max_len_sentences_pair, new_tokenizer.max_len_sentences_pair)
|
||||
|
||||
# Assert the set of special tokens match as we didn't ask to change them
|
||||
self.assertSequenceEqual(
|
||||
tokenizer.all_special_tokens,
|
||||
new_tokenizer.all_special_tokens,
|
||||
)
|
||||
|
||||
self.assertDictEqual(tokenizer.special_tokens_map, new_tokenizer.special_tokens_map)
|
||||
|
||||
|
||||
@require_torch
|
||||
@require_sentencepiece
|
||||
@require_tokenizers
|
||||
class SeamlessM4TDistilledIntegrationTest(unittest.TestCase):
|
||||
checkpoint_name = "facebook/hf-seamless-m4t-medium"
|
||||
src_text = [
|
||||
" UN Chief Says There Is No Military Solution in Syria",
|
||||
""" Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""",
|
||||
]
|
||||
tgt_text = [
|
||||
"Şeful ONU declară că nu există o soluţie militară în Siria",
|
||||
"Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei"
|
||||
' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor'
|
||||
" face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.",
|
||||
]
|
||||
|
||||
expected_src_tokens = [256047, 16297, 134408, 8165, 248066, 14734, 950, 1135, 105721, 3573, 83, 27352, 108, 49486, 3] # fmt: skip
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.tokenizer: SeamlessM4TTokenizer = SeamlessM4TTokenizer.from_pretrained(
|
||||
cls.checkpoint_name, src_lang="eng", tgt_lang="ron"
|
||||
)
|
||||
# cls.pad_token_id = 1
|
||||
return cls
|
||||
|
||||
def setUp(self):
|
||||
# Some tests may change source/target language and not reset
|
||||
self.tokenizer.src_lang = "eng"
|
||||
self.tokenizer.set_tgt_lang_special_tokens(self.tokenizer.tgt_lang)
|
||||
|
||||
def test_int_remove_extra_whitespaces(self):
|
||||
# make sure the extra spaces are eaten. Since the sample vocab does not have
|
||||
# `______`. sentencepiece.NormalizerSpec.remove_extra_whitespaces attribute is set to False
|
||||
|
||||
input_ids = self.tokenizer.encode(" . Hello")
|
||||
self.assertEqual(input_ids, [3, 256145, 81, 94124, 3])
|
||||
tokens = self.tokenizer.tokenize(" . Hello")
|
||||
self.assertEqual(tokens, ["▁.", "▁Hello"])
|
||||
|
||||
# `'▁'` is also a whitespace
|
||||
input_ids = self.tokenizer.encode("▁He is not")
|
||||
self.assertEqual(input_ids, [3, 256145, 1808, 248, 2294, 3])
|
||||
tokens = self.tokenizer.tokenize("▁He is not")
|
||||
|
||||
self.assertEqual(tokens, ["▁He", "▁is", "▁not"]) # no extra space added
|
||||
|
||||
input_ids = self.tokenizer.encode("▁He is not<s> ▁He")
|
||||
self.assertEqual(input_ids, [3, 256145, 1808, 248, 2294, 2, 1808, 3])
|
||||
tokens = self.tokenizer.tokenize("▁He is not<s> ▁He")
|
||||
self.assertEqual(tokens, ["▁He", "▁is", "▁not", "<s>", "▁He"]) # spaces are eaten by spm + our strip
|
||||
# make sure that the output after the extra id is the same as if
|
||||
# extra_id was not there
|
||||
input_ids = self.tokenizer.encode("▁He is not ▁He")
|
||||
self.assertEqual(input_ids, [3, 256145, 1808, 248, 2294, 1808, 3])
|
||||
tokens = self.tokenizer.tokenize("▁He is not ▁He")
|
||||
self.assertEqual(tokens, ["▁He", "▁is", "▁not", "▁He"]) # spaces are eaten by spm even if not start
|
||||
|
||||
def test_language_codes(self):
|
||||
self.assertEqual(self.tokenizer.convert_tokens_to_ids("__ace_Latn__"), 256002)
|
||||
self.assertEqual(self.tokenizer.convert_tokens_to_ids("__shn__"), 256152)
|
||||
self.assertEqual(self.tokenizer.convert_tokens_to_ids("__eng__"), 256047)
|
||||
self.assertEqual(self.tokenizer.convert_tokens_to_ids("__fra__"), 256057)
|
||||
self.assertEqual(self.tokenizer.convert_tokens_to_ids("__quy__"), 256144)
|
||||
|
||||
def test_tokenizer_tgt_lang(self):
|
||||
ids = self.tokenizer(self.src_text, src_lang="fra").input_ids[0]
|
||||
self.assertListEqual(self.expected_src_tokens[1:], ids[1 : len(self.expected_src_tokens)])
|
||||
self.assertEqual(256057, ids[0])
|
||||
|
||||
rest_ids = ids[len(self.expected_src_tokens) :]
|
||||
self.assertListEqual([0] * len(rest_ids), rest_ids)
|
||||
|
||||
ids = self.tokenizer(self.src_text, src_lang="__shn__").input_ids[0]
|
||||
self.assertListEqual(self.expected_src_tokens[1:], ids[1 : len(self.expected_src_tokens)])
|
||||
self.assertEqual(256152, ids[0])
|
||||
|
||||
# Copied from tests.models.nllb.test_tokenization_nllb.NllbDistilledIntegrationTest.test_enro_tokenizer_decode_ignores_language_codes
|
||||
def test_enro_tokenizer_decode_ignores_language_codes(self):
|
||||
self.assertIn(RO_CODE, self.tokenizer.all_special_ids)
|
||||
generated_ids = [RO_CODE, 4254, 98068, 112923, 39072, 3909, 713, 102767, 26, 17314, 35642, 14683, 33118, 2022, 66987, 2, 256047] # fmt: skip
|
||||
|
||||
result = self.tokenizer.decode(generated_ids, skip_special_tokens=True)
|
||||
expected_romanian = self.tokenizer.decode(generated_ids[1:], skip_special_tokens=True)
|
||||
self.assertEqual(result, expected_romanian)
|
||||
self.assertNotIn(self.tokenizer.eos_token, result)
|
||||
|
||||
def test_enro_tokenizer_truncation(self):
|
||||
src_text = ["this is gunna be a long sentence " * 20]
|
||||
assert isinstance(src_text[0], str)
|
||||
desired_max_length = 10
|
||||
ids = self.tokenizer(src_text, max_length=desired_max_length, truncation=True).input_ids[0]
|
||||
self.assertEqual(ids[-1], 3)
|
||||
self.assertEqual(ids[0], EN_CODE)
|
||||
self.assertEqual(len(ids), desired_max_length)
|
||||
|
||||
@require_torch
|
||||
def test_enro_tokenizer_prepare_batch(self):
|
||||
batch = self.tokenizer(
|
||||
self.src_text,
|
||||
text_target=self.tgt_text,
|
||||
padding=True,
|
||||
truncation=True,
|
||||
max_length=len(self.expected_src_tokens),
|
||||
pad_to_multiple_of=None,
|
||||
return_tensors="pt",
|
||||
)
|
||||
batch["decoder_input_ids"] = shift_tokens_right(
|
||||
batch["labels"], self.tokenizer.pad_token_id, self.tokenizer.convert_tokens_to_ids("__ron__")
|
||||
)
|
||||
|
||||
self.assertIsInstance(batch, BatchEncoding)
|
||||
|
||||
self.assertEqual((2, 15), batch.input_ids.shape)
|
||||
self.assertEqual((2, 15), batch.attention_mask.shape)
|
||||
result = batch.input_ids.tolist()[0]
|
||||
self.assertListEqual(self.expected_src_tokens, result)
|
||||
self.assertEqual(RO_CODE, batch.decoder_input_ids[0, 0]) # EOS
|
||||
# Test that special tokens are reset
|
||||
self.assertEqual(self.tokenizer.prefix_tokens, [EN_CODE])
|
||||
self.assertEqual(self.tokenizer.suffix_tokens, [self.tokenizer.eos_token_id])
|
||||
|
||||
def test_seq2seq_max_length(self):
|
||||
batch = self.tokenizer(
|
||||
self.src_text, padding=True, truncation=True, max_length=3, return_tensors="pt", pad_to_multiple_of=None
|
||||
)
|
||||
targets = self.tokenizer(
|
||||
text_target=self.tgt_text, padding=True, truncation=True, max_length=10, return_tensors="pt"
|
||||
)
|
||||
labels = targets["input_ids"]
|
||||
batch["decoder_input_ids"] = shift_tokens_right(
|
||||
labels,
|
||||
self.tokenizer.pad_token_id,
|
||||
decoder_start_token_id=self.tokenizer.convert_tokens_to_ids(self.tokenizer.tgt_lang),
|
||||
)
|
||||
|
||||
self.assertEqual(batch.input_ids.shape[1], 3)
|
||||
self.assertEqual(batch.decoder_input_ids.shape[1], 10)
|
||||
|
||||
@require_torch
|
||||
def test_tokenizer_translation(self):
|
||||
inputs = self.tokenizer._build_translation_inputs(
|
||||
"A test", return_tensors="pt", src_lang="eng", tgt_lang="fra"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
nested_simplify(inputs),
|
||||
{
|
||||
# A, test, EOS, en_XX
|
||||
"input_ids": [[256047, 70, 7356, 3]],
|
||||
"attention_mask": [[1, 1, 1, 1]],
|
||||
# ar_AR
|
||||
"forced_bos_token_id": 256057,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@require_sentencepiece
|
||||
@require_tokenizers
|
||||
class CommonSpmIntegrationTests(unittest.TestCase):
|
||||
"""
|
||||
A class that regroups important test to make sure that we properly handle the special tokens.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
tokenizer = SeamlessM4TTokenizer.from_pretrained(SAMPLE_VOCAB)
|
||||
tokenizer.add_special_tokens({"additional_special_tokens": [AddedToken("<s>", rstrip=False, lstrip=False)]})
|
||||
cls.tokenizer = tokenizer
|
||||
return cls
|
||||
|
||||
def setUp(self):
|
||||
self.tokenizer.set_tgt_lang_special_tokens(self.tokenizer.tgt_lang)
|
||||
|
||||
def test_add_dummy_prefix(self):
|
||||
# make sure `'▁'` is prepended properly
|
||||
input_ids = self.tokenizer.encode(". Hello")
|
||||
self.assertEqual(input_ids, [3, 1, 8, 5, 157, 87, 21, 3])
|
||||
|
||||
tokens = self.tokenizer.tokenize(". Hello")
|
||||
self.assertEqual(tokens, ["▁", ".", "▁He", "ll", "o"])
|
||||
|
||||
tokens = self.tokenizer.tokenize("")
|
||||
self.assertEqual(tokens, [])
|
||||
|
||||
tokens = self.tokenizer.tokenize(" ")
|
||||
self.assertEqual(tokens, [])
|
||||
|
||||
tokens = self.tokenizer.tokenize("▁")
|
||||
self.assertEqual(tokens, [])
|
||||
|
||||
def test_character_after_special_token(self):
|
||||
# Make sure that `tokenizer.tokenize` is similar to
|
||||
# adding the equivalent special token to the vocab
|
||||
input_ids = self.tokenizer.encode("Hey <s>I")
|
||||
self.assertEqual(input_ids, [3, 1, 157, 31, 2, 101, 3])
|
||||
|
||||
tokens = self.tokenizer.tokenize("<s>I")
|
||||
self.assertEqual(tokens, ["<s>", "I"])
|
||||
|
||||
input_ids = self.tokenizer.encode("Hello, <s>,")
|
||||
self.assertEqual(input_ids, [3, 1, 157, 87, 21, 4, 2, 4, 3])
|
||||
tokens = self.tokenizer.tokenize("Hello, <s>,")
|
||||
self.assertEqual(tokens, ["▁He", "ll", "o", ",", "<s>", ","])
|
||||
|
||||
def test_special_tokens_strip(self):
|
||||
input_ids = self.tokenizer.encode(" <s> ,")
|
||||
self.assertEqual(input_ids, [3, 1, 2, 8, 4, 3])
|
||||
tokens = self.tokenizer.tokenize(" <s> ,")
|
||||
# spaces are eaten by rstrip / lstrip + normalizer
|
||||
self.assertEqual(tokens, ["<s>", "▁", ","])
|
||||
|
||||
input_ids = self.tokenizer.encode("No <s> He")
|
||||
self.assertEqual(input_ids, [3, 1, 285, 2, 157, 3])
|
||||
tokens = self.tokenizer.tokenize("No <s> ▁He")
|
||||
self.assertEqual(tokens, ["▁No", "<s>", "▁He"]) # spaces are eaten by rstrip / lstrip
|
||||
Reference in New Issue
Block a user