codingutf8 2021 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 orglicenseslicense2 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 copied from tests models whisper testfeatureextractionwhisper floatslist creates a random float32 tensor if rng is none rng globalrng values for batchidx in rangeshape0 values append for in rangeshape1 values1 appendrng random scale return values class wav2vec2featureextractiontesterunittest testcase def init self parent batchsize7 minseqlength400 maxseqlength2000 featuresize1 paddingvalue0 0 samplingrate16000 returnattentionmasktrue donormalizetrue self parent parent self batchsize batchsize self minseqlength minseqlength self maxseqlength maxseqlength self seqlengthdiff self maxseqlength self minseqlength self batchsize 1 self featuresize featuresize self paddingvalue paddingvalue self samplingrate samplingrate self returnattentionmask returnattentionmask self donormalize donormalize def preparefeatextractdictself return featuresize self featuresize paddingvalue self paddingvalue samplingrate self samplingrate returnattentionmask self returnattentionmask donormalize self donormalize def prepareinputsforcommonself equallengthfalse numpifyfalse def flattenlistoflists return listitertools chainlistoflists if equallength speechinputs floatslistself batchsize self maxseqlength else make sure that inputs increase in size speechinputs flattenfloatslistx self featuresize for x in rangeself minseqlength self maxseqlength self seqlengthdiff if numpify speechinputs np asarrayx for x in speechinputs return speechinputs class wav2vec2featureextractiontestsequencefeatureextractiontestmixin unittest testcase featureextractionclass wav2vec2featureextractor def setupself self featextracttester wav2vec2featureextractiontesterself def checkzeromeanunitvarianceself inputvector self asserttruenp allnp meaninputvector axis0 1e3 self asserttruenp allnp absnp varinputvector axis0 1 1e3 def testcallself tests that all call wrap to encodeplus and batchencodeplus featextract self featureextractionclassself featextracttester preparefeatextractdict create three inputs of length 800 1000 and 1200 speechinputs floatslist1 x0 for x in range800 1400 200 npspeechinputs np asarrayspeechinput for speechinput in speechinputs test not batched input encodedsequences1 featextractspeechinputs0 returntensorsnp inputvalues encodedsequences2 featextractnpspeechinputs0 returntensorsnp inputvalues self asserttruenp allcloseencodedsequences1 encodedsequences2 atol1e3 test batched encodedsequences1 featextractspeechinputs returntensorsnp inputvalues encodedsequences2 featextractnpspeechinputs returntensorsnp inputvalues for encseq1 encseq2 in zipencodedsequences1 encodedsequences2 self asserttruenp allcloseencseq1 encseq2 atol1e3 test 2d numpy arrays are batched speechinputs floatslist1 x0 for x in 800 800 800 npspeechinputs np asarrayspeechinputs encodedsequences1 featextractspeechinputs returntensorsnp inputvalues encodedsequences2 featextractnpspeechinputs returntensorsnp inputvalues for encseq1 encseq2 in zipencodedsequences1 encodedsequences2 self asserttruenp allcloseencseq1 encseq2 atol1e3 def testzeromeanunitvariancenormalizationnpself featextract self featureextractionclassself featextracttester preparefeatextractdict speechinputs floatslist1 x0 for x in range800 1400 200 paddings longest maxlength donotpad maxlengths none 1600 none for maxlength padding in zipmaxlengths paddings processed featextractspeechinputs paddingpadding maxlengthmaxlength returntensorsnp inputvalues processed inputvalues self checkzeromeanunitvarianceinputvalues0 800 self asserttrueinputvalues0800 sum 1e6 self checkzeromeanunitvarianceinputvalues1 1000 self asserttrueinputvalues01000 sum 1e6 self checkzeromeanunitvarianceinputvalues2 1200 def testzeromeanunitvariancenormalizationself featextract self featureextractionclassself featextracttester preparefeatextractdict lengths range800 1400 200 speechinputs floatslist1 x0 for x in lengths paddings longest maxlength donotpad maxlengths none 1600 none for maxlength padding in zipmaxlengths paddings processed featextractspeechinputs maxlengthmaxlength paddingpadding inputvalues processed inputvalues self checkzeromeanunitvarianceinputvalues0 800 self checkzeromeanunitvarianceinputvalues1 1000 self checkzeromeanunitvarianceinputvalues2 1200 def testzeromeanunitvariancenormalizationtruncnpmaxlengthself featextract self featureextractionclassself featextracttester preparefeatextractdict speechinputs floatslist1 x0 for x in range800 1400 200 processed featextract speechinputs truncationtrue maxlength1000 paddingmaxlength returntensorsnp inputvalues processed inputvalues self checkzeromeanunitvarianceinputvalues0 800 self checkzeromeanunitvarianceinputvalues1 self checkzeromeanunitvarianceinputvalues2 def testzeromeanunitvariancenormalizationtruncnplongestself featextract self featureextractionclassself featextracttester preparefeatextractdict speechinputs floatslist1 x0 for x in range800 1400 200 processed featextract speechinputs truncationtrue maxlength1000 paddinglongest returntensorsnp inputvalues processed inputvalues self checkzeromeanunitvarianceinputvalues0 800 self checkzeromeanunitvarianceinputvalues1 1000 self checkzeromeanunitvarianceinputvalues2 make sure that if maxlength longest then pad to maxlength self asserttrueinputvalues shape 3 1000 speechinputs floatslist1 x0 for x in range800 1400 200 processed featextract speechinputs truncationtrue maxlength2000 paddinglongest returntensorsnp inputvalues processed inputvalues self checkzeromeanunitvarianceinputvalues0 800 self checkzeromeanunitvarianceinputvalues1 1000 self checkzeromeanunitvarianceinputvalues2 make sure that if maxlength longest then pad to longest self asserttrueinputvalues shape 3 1200 requiretorch def testdoubleprecisionpadself import torch featureextractor self featureextractionclassself featextracttester preparefeatextractdict npspeechinputs np random rand100 astypenp float64 pyspeechinputs npspeechinputs tolist for inputs in pyspeechinputs npspeechinputs npprocessed featureextractor padinputvalues inputs returntensorsnp self asserttruenpprocessed inputvalues dtype np float32 ptprocessed featureextractor padinputvalues inputs returntensorspt self asserttrueptprocessed inputvalues dtype torch float32 slow requiretorch def testpretrainedcheckpointsaresetcorrectlyself this test makes sure that models that are using group norm don t have their feature extractor return the attentionmask for modelid in wav2vec2pretrainedmodelarchivelist config wav2vec2config frompretrainedmodelid featextract wav2vec2featureextractor frompretrainedmodelid only layer feature extraction norm should make use of attentionmask self assertequalfeatextract returnattentionmask config featextractnorm layer coding utf 8 2021 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 copied from tests models whisper test_feature_extraction_whisper floats_list creates a random float32 tensor make sure that inputs increase in size tests that all call wrap to encode_plus and batch_encode_plus create three inputs of length 800 1000 and 1200 test not batched input test batched test 2 d numpy arrays are batched make sure that if max_length longest then pad to max_length make sure that if max_length longest then pad to longest this test makes sure that models that are using group norm don t have their feature extractor return the attention_mask only layer feature extraction norm should make use of attention_mask
import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, Wav2Vec2Config, Wav2Vec2FeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


global_rng = random.Random()



def floats_list(shape, scale=1.0, rng=None, name=None):
    
    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


class Wav2Vec2FeatureExtractionTester(unittest.TestCase):
    def __init__(
        self,
        parent,
        batch_size=7,
        min_seq_length=400,
        max_seq_length=2000,
        feature_size=1,
        padding_value=0.0,
        sampling_rate=16000,
        return_attention_mask=True,
        do_normalize=True,
    ):
        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.feature_size = feature_size
        self.padding_value = padding_value
        self.sampling_rate = sampling_rate
        self.return_attention_mask = return_attention_mask
        self.do_normalize = do_normalize

    def prepare_feat_extract_dict(self):
        return {
            "feature_size": self.feature_size,
            "padding_value": self.padding_value,
            "sampling_rate": self.sampling_rate,
            "return_attention_mask": self.return_attention_mask,
            "do_normalize": self.do_normalize,
        }

    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.batch_size, self.max_seq_length))
        else:
            
            speech_inputs = [
                _flatten(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


class Wav2Vec2FeatureExtractionTest(SequenceFeatureExtractionTestMixin, unittest.TestCase):
    feature_extraction_class = Wav2Vec2FeatureExtractor

    def setUp(self):
        self.feat_extract_tester = Wav2Vec2FeatureExtractionTester(self)

    def _check_zero_mean_unit_variance(self, input_vector):
        self.assertTrue(np.all(np.mean(input_vector, axis=0) < 1e-3))
        self.assertTrue(np.all(np.abs(np.var(input_vector, axis=0) - 1) < 1e-3))

    def test_call(self):
        
        feat_extract = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
        
        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]

        
        encoded_sequences_1 = feat_extract(speech_inputs[0], return_tensors="np").input_values
        encoded_sequences_2 = feat_extract(np_speech_inputs[0], return_tensors="np").input_values
        self.assertTrue(np.allclose(encoded_sequences_1, encoded_sequences_2, atol=1e-3))

        
        encoded_sequences_1 = feat_extract(speech_inputs, return_tensors="np").input_values
        encoded_sequences_2 = feat_extract(np_speech_inputs, return_tensors="np").input_values
        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))

        
        speech_inputs = [floats_list((1, x))[0] for x in (800, 800, 800)]
        np_speech_inputs = np.asarray(speech_inputs)
        encoded_sequences_1 = feat_extract(speech_inputs, return_tensors="np").input_values
        encoded_sequences_2 = feat_extract(np_speech_inputs, return_tensors="np").input_values
        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_zero_mean_unit_variance_normalization_np(self):
        feat_extract = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
        speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]

        paddings = ["longest", "max_length", "do_not_pad"]
        max_lengths = [None, 1600, None]
        for max_length, padding in zip(max_lengths, paddings):
            processed = feat_extract(speech_inputs, padding=padding, max_length=max_length, return_tensors="np")
            input_values = processed.input_values

            self._check_zero_mean_unit_variance(input_values[0][:800])
            self.assertTrue(input_values[0][800:].sum() < 1e-6)
            self._check_zero_mean_unit_variance(input_values[1][:1000])
            self.assertTrue(input_values[0][1000:].sum() < 1e-6)
            self._check_zero_mean_unit_variance(input_values[2][:1200])

    def test_zero_mean_unit_variance_normalization(self):
        feat_extract = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
        lengths = range(800, 1400, 200)
        speech_inputs = [floats_list((1, x))[0] for x in lengths]

        paddings = ["longest", "max_length", "do_not_pad"]
        max_lengths = [None, 1600, None]

        for max_length, padding in zip(max_lengths, paddings):
            processed = feat_extract(speech_inputs, max_length=max_length, padding=padding)
            input_values = processed.input_values

            self._check_zero_mean_unit_variance(input_values[0][:800])
            self._check_zero_mean_unit_variance(input_values[1][:1000])
            self._check_zero_mean_unit_variance(input_values[2][:1200])

    def test_zero_mean_unit_variance_normalization_trunc_np_max_length(self):
        feat_extract = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
        speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
        processed = feat_extract(
            speech_inputs, truncation=True, max_length=1000, padding="max_length", return_tensors="np"
        )
        input_values = processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800])
        self._check_zero_mean_unit_variance(input_values[1])
        self._check_zero_mean_unit_variance(input_values[2])

    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())
        speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
        processed = feat_extract(
            speech_inputs, truncation=True, max_length=1000, padding="longest", return_tensors="np"
        )
        input_values = processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800])
        self._check_zero_mean_unit_variance(input_values[1, :1000])
        self._check_zero_mean_unit_variance(input_values[2])

        
        self.assertTrue(input_values.shape == (3, 1000))

        speech_inputs = [floats_list((1, x))[0] for x in range(800, 1400, 200)]
        processed = feat_extract(
            speech_inputs, truncation=True, max_length=2000, padding="longest", return_tensors="np"
        )
        input_values = processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :800])
        self._check_zero_mean_unit_variance(input_values[1, :1000])
        self._check_zero_mean_unit_variance(input_values[2])

        
        self.assertTrue(input_values.shape == (3, 1200))

    @require_torch
    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).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_values": inputs}], return_tensors="np")
            self.assertTrue(np_processed.input_values.dtype == np.float32)
            pt_processed = feature_extractor.pad([{"input_values": inputs}], return_tensors="pt")
            self.assertTrue(pt_processed.input_values.dtype == torch.float32)

    @slow
    @require_torch
    def test_pretrained_checkpoints_are_set_correctly(self):
        
        
        
        for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
            config = Wav2Vec2Config.from_pretrained(model_id)
            feat_extract = Wav2Vec2FeatureExtractor.from_pretrained(model_id)

            
            
            self.assertEqual(feat_extract.return_attention_mask, config.feat_extract_norm == "layer")