





'''simple docstring'''


import tempfile
import unittest

from transformers import TaConfig, is_torch_available
from transformers.testing_utils import (
    require_sentencepiece,
    require_tokenizers,
    require_torch,
    slow,
    torch_device,
)

from ...generation.test_utils import GenerationTesterMixin
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel

class       lowerCAmelCase_						:




 def __init__(		self	,   _lowerCAmelCase	,   _lowerCAmelCase=99	,   _lowerCAmelCase=13	,   _lowerCAmelCase=7	,   _lowerCAmelCase=9	,   _lowerCAmelCase=True	,   _lowerCAmelCase=True	,   _lowerCAmelCase=False	,   _lowerCAmelCase=32	,   _lowerCAmelCase=5	,   _lowerCAmelCase=4	,   _lowerCAmelCase=37	,   _lowerCAmelCase=8	,   _lowerCAmelCase=0.1	,   _lowerCAmelCase=0.002	,   _lowerCAmelCase=1	,   _lowerCAmelCase=0	,   _lowerCAmelCase=0	,   _lowerCAmelCase=None	,   _lowerCAmelCase=None	,   )			->							Optional[Any]:
     _lowerCAmelCase			=					parent
     _lowerCAmelCase			=					batch_size
     _lowerCAmelCase			=					encoder_seq_length
     _lowerCAmelCase			=					decoder_seq_length
     # For common tests
     _lowerCAmelCase			=					self.decoder_seq_length
     _lowerCAmelCase			=					is_training
     _lowerCAmelCase			=					use_attention_mask
     _lowerCAmelCase			=					use_labels
     _lowerCAmelCase			=					vocab_size
     _lowerCAmelCase			=					hidden_size
     _lowerCAmelCase			=					num_hidden_layers
     _lowerCAmelCase			=					num_attention_heads
     _lowerCAmelCase			=					d_ff
     _lowerCAmelCase			=					relative_attention_num_buckets
     _lowerCAmelCase			=					dropout_rate
     _lowerCAmelCase			=					initializer_factor
     _lowerCAmelCase			=					eos_token_id
     _lowerCAmelCase			=					pad_token_id
     _lowerCAmelCase			=					decoder_start_token_id
     _lowerCAmelCase			=					None
     _lowerCAmelCase			=					decoder_layers




 def 							_snake_case      (		self						)			->							Optional[Any]:
     return TaConfig.from_pretrained("google/umt5-base"						)




 def 							_snake_case      (		self	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase=None	,   _lowerCAmelCase=None	,   _lowerCAmelCase=None	,   _lowerCAmelCase=None	,   _lowerCAmelCase=None	,   )			->							Optional[Any]:







     if attention_mask is None:
         _lowerCAmelCase			=					input_ids.ne(config.pad_token_id						)
     if decoder_attention_mask is None:
         _lowerCAmelCase			=					decoder_input_ids.ne(config.pad_token_id						)
     if head_mask is None:
         _lowerCAmelCase			=					torch.ones(config.num_hidden_layers	,   config.num_attention_heads	,   device=__lowerCAmelCase						)
     if decoder_head_mask is None:
         _lowerCAmelCase			=					torch.ones(config.num_decoder_layers	,   config.num_attention_heads	,   device=__lowerCAmelCase						)
     if cross_attn_head_mask is None:
         _lowerCAmelCase			=					torch.ones(
             config.num_decoder_layers	,   config.num_attention_heads	,   device=__lowerCAmelCase						)
     return {
         "input_ids": input_ids,
         "decoder_input_ids": decoder_input_ids,
         "attention_mask": attention_mask,
         "decoder_attention_mask": decoder_attention_mask,
         "head_mask": head_mask,
         "decoder_head_mask": decoder_head_mask,
         "cross_attn_head_mask": cross_attn_head_mask,
     }




 def 							_snake_case      (		self						)			->							Union[str, Any]:
     _lowerCAmelCase			=					ids_tensor([self.batch_size, self.encoder_seq_length]	,   self.vocab_size						)
     _lowerCAmelCase			=					ids_tensor([self.batch_size, self.decoder_seq_length]	,   self.vocab_size						)

     # we need to clamp the input ids here to avoid having pad token in between
     # this is because for NllbMoe the position_ids are prepared such that
     # all pad tokens have pos id = 2 and rest are between 2..seq_length
     # and the seq_length here is seq_length - num_pad_tokens
     # but when using past, there is no way of knowing if the past input ids had
     # pad tokens in them, which results in incorrect seq_lenth and which in turn results in
     # position_ids being off by num_pad_tokens in past input
     _lowerCAmelCase			=					input_ids.clamp(self.pad_token_id + 1						)
     _lowerCAmelCase			=					decoder_input_ids.clamp(self.pad_token_id + 1						)

     _lowerCAmelCase			=					self.get_config()
     _lowerCAmelCase			=					config.num_attention_heads
     _lowerCAmelCase			=					self.prepare_inputs_dict(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase						)
     return config, input_dict




 def 							_snake_case      (		self						)			->							Optional[Any]:
     _lowerCAmelCase	,	_lowerCAmelCase			=					self.prepare_config_and_inputs()
     return config, inputs_dict




 def 							_snake_case      (		self						)			->							int:
     return TaConfig(
         vocab_size=166	,   d_model=self.hidden_size	,   d_ff=self.d_ff	,   d_kv=self.hidden_size // self.num_attention_heads	,   num_layers=self.num_hidden_layers	,   num_decoder_layers=self.decoder_layers	,   num_heads=self.num_attention_heads	,   relative_attention_num_buckets=self.relative_attention_num_buckets	,   dropout_rate=self.dropout_rate	,   initializer_factor=self.initializer_factor	,   eos_token_id=self.eos_token_id	,   bos_token_id=self.pad_token_id	,   pad_token_id=self.pad_token_id	,   decoder_start_token_id=self.decoder_start_token_id	,   )




 def 							_snake_case      (		self						)			->							Optional[int]:
     return TaConfig(
         vocab_size=self.vocab_size	,   d_model=self.hidden_size	,   d_ff=self.d_ff	,   d_kv=self.hidden_size // self.num_attention_heads	,   num_layers=self.num_hidden_layers	,   num_decoder_layers=self.decoder_layers	,   num_heads=self.num_attention_heads	,   relative_attention_num_buckets=self.relative_attention_num_buckets	,   dropout_rate=self.dropout_rate	,   initializer_factor=self.initializer_factor	,   eos_token_id=self.eos_token_id	,   bos_token_id=self.pad_token_id	,   pad_token_id=self.pad_token_id	,   decoder_start_token_id=self.decoder_start_token_id	,   )




 def 							_snake_case      (		self	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   )			->							str:
     _lowerCAmelCase			=					UMTaModel(config=__lowerCAmelCase						)
     model.to(__lowerCAmelCase						)
     model.eval()
     _lowerCAmelCase			=					model(
         input_ids=__lowerCAmelCase	,   decoder_input_ids=__lowerCAmelCase	,   attention_mask=__lowerCAmelCase	,   decoder_attention_mask=__lowerCAmelCase	,   )
     _lowerCAmelCase			=					model(input_ids=__lowerCAmelCase	,   decoder_input_ids=__lowerCAmelCase						)
     _lowerCAmelCase			=					result.last_hidden_state
     _lowerCAmelCase			=					result.past_key_values
     _lowerCAmelCase			=					result.encoder_last_hidden_state

     self.parent.assertEqual(encoder_output.size()	,   (self.batch_size, self.encoder_seq_length, self.hidden_size)						)
     self.parent.assertEqual(decoder_output.size()	,   (self.batch_size, self.decoder_seq_length, self.hidden_size)						)
     # There should be `num_layers` key value embeddings stored in decoder_past
     self.parent.assertEqual(len(__lowerCAmelCase						)	,   config.num_layers						)
     # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple
     self.parent.assertEqual(len(decoder_past[0]						)	,   4						)




 def 							_snake_case      (		self	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   _lowerCAmelCase	,   )			->							List[str]:
     _lowerCAmelCase			=					UMTaModel(config=__lowerCAmelCase						).get_decoder().to(__lowerCAmelCase						).eval()
     # first forward pass
     _lowerCAmelCase			=					model(__lowerCAmelCase	,   use_cache=__lowerCAmelCase						)
     _lowerCAmelCase			=					model(__lowerCAmelCase						)
     _lowerCAmelCase			=					model(__lowerCAmelCase	,   use_cache=__lowerCAmelCase						)

     self.parent.assertTrue(len(__lowerCAmelCase						) == len(__lowerCAmelCase						)						)
     self.parent.assertTrue(len(__lowerCAmelCase						) == len(__lowerCAmelCase						) + 1						)

     _lowerCAmelCase	,	_lowerCAmelCase			=					outputs.to_tuple()

     # create hypothetical next token and extent to next_input_ids
     _lowerCAmelCase			=					ids_tensor((self.batch_size, 1)	,   config.vocab_size						)

     # append to next input_ids and
     _lowerCAmelCase			=					torch.cat([input_ids, next_tokens]	,   dim=-1						)

     _lowerCAmelCase			=					model(__lowerCAmelCase						)["last_hidden_state"]
     _lowerCAmelCase			=					model(__lowerCAmelCase	,   past_key_values=__lowerCAmelCase						)["last_hidden_state"]

     # select random slice
     _lowerCAmelCase			=					ids_tensor((1,)	,   output_from_past.shape[-1]						).item()
     _lowerCAmelCase			=					output_from_no_past[:, -1, random_slice_idx].detach()
     _lowerCAmelCase			=					output_from_past[:, 0, random_slice_idx].detach()

     # test that outputs are equal for slice
     self.parent.assertTrue(torch.allclose(__lowerCAmelCase	,   __lowerCAmelCase	,   atol=1E-3						)						)





 def 							_snake_case      (		self	,   _lowerCAmelCase	,   _lowerCAmelCase	,   )			->							Tuple:
     _lowerCAmelCase			=					UMTaModel(config=__lowerCAmelCase						).to(__lowerCAmelCase						).half().eval()
     _lowerCAmelCase			=					model(**__lowerCAmelCase						)["last_hidden_state"]
     self.parent.assertFalse(torch.isnan(__lowerCAmelCase						).any().item()						)







@require_torch
class       lowerCAmelCase_						(						__magic_name__  ,__magic_name__  ,__magic_name__  ,unittest.TestCase						):
 __lowerCamelCase  :							Optional[int]				     =    (
     (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else ()
 )
 __lowerCamelCase  :							int				     =    (UMTaForConditionalGeneration,) if is_torch_available() else ()
 __lowerCamelCase  :							List[Any]				     =    (
     {
         "conversational": UMTaForConditionalGeneration,
         "feature-extraction": UMTaModel,
         "summarization": UMTaForConditionalGeneration,
         "text2text-generation": UMTaForConditionalGeneration,
         "translation": UMTaForConditionalGeneration,
         "question-answering": UMTaForQuestionAnswering,
     }
     if is_torch_available()
     else {}
 )
 __lowerCamelCase  :							Dict				     =    True
 __lowerCamelCase  :							List[str]				     =    False
 __lowerCamelCase  :							Dict				     =    False
 __lowerCamelCase  :							int				     =    True
 __lowerCamelCase  :							Any				     =    True
 # The small UMT5 model needs higher percentages for CPU/MP tests
 __lowerCamelCase  :							Optional[int]				     =    [0.8, 0.9]




 def 							_snake_case      (		self						)			->							Dict:
     _lowerCAmelCase			=					UMTaModelTester(self						)




 @unittest.skip("Test has a segmentation fault on torch 1.8.0"						)
 def 							_snake_case      (		self						)			->							Tuple:
     _lowerCAmelCase			=					self.model_tester.prepare_config_and_inputs()
     _lowerCAmelCase			=					UMTaModel(config_and_inputs[0]						).to(__lowerCAmelCase						)
     with tempfile.TemporaryDirectory() as tmpdirname:
         torch.onnx.export(
             __lowerCAmelCase	,   (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2])	,   f'''{tmpdirname}/t5_test.onnx'''	,   export_params=__lowerCAmelCase	,   opset_version=9	,   input_names=["input_ids", "decoder_input_ids"]	,   )




 @unittest.skipIf(torch_device == "cpu"	,   "Cant do half precision"						)
 def 							_snake_case      (		self						)			->							Tuple:
     _lowerCAmelCase			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model_fpaa_forward(*__lowerCAmelCase						)




 def 							_snake_case      (		self						)			->							Union[str, Any]:
     _lowerCAmelCase			=					["encoder_attentions", "decoder_attentions", "cross_attentions"]
     _lowerCAmelCase			=					self.model_tester.prepare_config_and_inputs()
     _lowerCAmelCase			=					config_and_inputs[0]
     _lowerCAmelCase			=					UMTaForConditionalGeneration(__lowerCAmelCase						).eval()
     model.to(__lowerCAmelCase						)

     _lowerCAmelCase			=					{
         "head_mask": torch.zeros(config.num_layers	,   config.num_heads	,   device=__lowerCAmelCase						),
         "decoder_head_mask": torch.zeros(config.num_decoder_layers	,   config.num_heads	,   device=__lowerCAmelCase						),
         "cross_attn_head_mask": torch.zeros(config.num_decoder_layers	,   config.num_heads	,   device=__lowerCAmelCase						),
     }

     for attn_name, (name, mask) in zip(__lowerCAmelCase	,   head_masking.items()						):
         _lowerCAmelCase			=					{name: mask}
         # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified
         if name == "head_mask":
             _lowerCAmelCase			=					torch.ones(
                 config.num_decoder_layers	,   config.num_heads	,   device=__lowerCAmelCase						)

         _lowerCAmelCase			=					model.generate(
             config_and_inputs[1]["input_ids"]	,   num_beams=1	,   max_length=3	,   output_attentions=__lowerCAmelCase	,   return_dict_in_generate=__lowerCAmelCase	,   **__lowerCAmelCase	,   )
         # We check the state of decoder_attentions and cross_attentions just from the last step
         _lowerCAmelCase			=					out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1]
         self.assertEqual(sum([w.sum().item() for w in attn_weights]						)	,   0.0						)





 @unittest.skip("Does not work on the tiny model as we keep hitting edge cases."						)
 def 							_snake_case      (		self						)			->							Union[str, Any]:
     pass







@require_torch
@require_sentencepiece
@require_tokenizers
class       lowerCAmelCase_						(						unittest.TestCase						):




 @slow
 @unittest.skip(
     "Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged"						)
 def 							_snake_case      (		self						)			->							Optional[int]:
     _lowerCAmelCase			=					UMTaForConditionalGeneration.from_pretrained("google/umt5-small"	,   return_dict=__lowerCAmelCase						).to(__lowerCAmelCase						)
     _lowerCAmelCase			=					AutoTokenizer.from_pretrained("google/umt5-small"	,   use_fast=__lowerCAmelCase	,   legacy=__lowerCAmelCase						)
     _lowerCAmelCase			=					[
         "Bonjour monsieur <extra_id_0> bien <extra_id_1>.",
         "No se como puedo <extra_id_0>.",
         "This is the reason why we <extra_id_0> them.",
         "The <extra_id_0> walks in <extra_id_1>, seats",
         "A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.",
     ]
     _lowerCAmelCase			=					tokenizer(__lowerCAmelCase	,   return_tensors="pt"	,   padding=__lowerCAmelCase						).input_ids
     # fmt: off
     _lowerCAmelCase			=					torch.tensor(
         [
             [ 38530, 210703, 256299, 1410, 256298, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0],
             [   826, 321, 671, 25922, 256299, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0],
             [  1460, 339, 312, 19014, 10620, 758, 256299, 2355,274, 1, 0, 0, 0, 0, 0, 0,0, 0],
             [   517, 256299, 14869, 281, 301, 256298, 275, 119983,1, 0, 0, 0, 0, 0, 0, 0,0, 0],
             [   320, 256299, 14869, 281, 2234, 289, 2275, 333,61391, 289, 256298, 543, 256297, 168714, 329, 256296,274, 1],
         ]						)
     # fmt: on
     torch.testing.assert_allclose(__lowerCAmelCase	,   __lowerCAmelCase						)

     _lowerCAmelCase			=					model.generate(input_ids.to(__lowerCAmelCase						)						)
     _lowerCAmelCase			=					[
         "<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>",
         "<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>",
         "<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>",
         "<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>",
         "<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>",
     ]
     _lowerCAmelCase			=					tokenizer.batch_decode(__lowerCAmelCase						)
     self.assertEqual(__lowerCAmelCase	,   __lowerCAmelCase						)









import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
			from transformers import SpeechaTextFeatureExtractor

_a 						=      random.Random()





def lowerCAmelCase__(__snake_case       ,__snake_case=1.0       ,__snake_case=None       ,__snake_case=None							)				->      List[Any]:







	'''simple docstring'''







	if rng is None:
		lowerCamelCase__								=		global_rng

	lowerCamelCase__								=		[]
	for batch_idx in range(shape[0]							):
		values.append([]							)
		for _ in range(shape[1]							):
			values[-1].append(rng.random() * scale							)

	return values






@require_torch
@require_torchaudio
class 					__A    (    unittest.TestCase ):

			'''simple docstring'''







			def __init__(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase=7       ,  __lowerCAmelCase=4_0_0       ,  __lowerCAmelCase=2_0_0_0       ,  __lowerCAmelCase=2_4       ,  __lowerCAmelCase=2_4       ,  __lowerCAmelCase=0.0       ,  __lowerCAmelCase=1_6_0_0_0       ,  __lowerCAmelCase=True       ,  __lowerCAmelCase=True       ,  ):



				'''simple docstring'''







				lowerCamelCase__								=		parent
				lowerCamelCase__								=		batch_size
				lowerCamelCase__								=		min_seq_length
				lowerCamelCase__								=		max_seq_length
				lowerCamelCase__								=		(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
				lowerCamelCase__								=		feature_size
				lowerCamelCase__								=		num_mel_bins
				lowerCamelCase__								=		padding_value
				lowerCamelCase__								=		sampling_rate
				lowerCamelCase__								=		return_attention_mask
				lowerCamelCase__								=		do_normalize





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return {
				    "feature_size": self.feature_size,
				    "num_mel_bins": self.num_mel_bins,
				    "padding_value": self.padding_value,
				    "sampling_rate": self.sampling_rate,
				    "return_attention_mask": self.return_attention_mask,
				    "do_normalize": self.do_normalize,
				}





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase=False       ,  __lowerCAmelCase=False							):



				'''simple docstring'''







				def _flatten(__lowerCAmelCase							):
					return list(itertools.chain(*__lowerCAmelCase							)							)

				if equal_length:
					lowerCamelCase__								=		[floats_list((self.max_seq_length, self.feature_size)							) for _ in range(self.batch_size							)]
				else:
					# make sure that inputs increase in size
					lowerCamelCase__								=		[
					    floats_list((x, self.feature_size)							)
					    for x in range(self.min_seq_length       ,  self.max_seq_length       ,  self.seq_length_diff							)
					]
				if numpify:
					lowerCamelCase__								=		[np.asarray(__lowerCAmelCase							) for x in speech_inputs]
				return speech_inputs






@require_torch
@require_torchaudio
class 					__A    (    lowerCAmelCase			,				unittest.TestCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						SpeechaTextFeatureExtractor if is_speech_available() else None





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		SpeechaTextFeatureExtractionTester(self							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				self.assertTrue(np.all(np.mean(__lowerCAmelCase       ,  axis=0							) < 1E-3							)							)
				self.assertTrue(np.all(np.abs(np.var(__lowerCAmelCase       ,  axis=0							) - 1							) < 1E-3							)							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
				# create three inputs of length 800, 1000, and 1200
				lowerCamelCase__								=		[floats_list((1, x)							)[0] for x in range(8_0_0       ,  1_4_0_0       ,  2_0_0							)]
				lowerCamelCase__								=		[np.asarray(__lowerCAmelCase							) for speech_input in speech_inputs]

				# Test feature size
				lowerCamelCase__								=		feature_extractor(__lowerCAmelCase       ,  padding=__lowerCAmelCase       ,  return_tensors='''np'''							).input_features
				self.assertTrue(input_features.ndim == 3							)
				self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size							)

				# Test not batched input
				lowerCamelCase__								=		feature_extractor(speech_inputs[0]       ,  return_tensors='''np'''							).input_features
				lowerCamelCase__								=		feature_extractor(np_speech_inputs[0]       ,  return_tensors='''np'''							).input_features
				self.assertTrue(np.allclose(__lowerCAmelCase       ,  __lowerCAmelCase       ,  atol=1E-3							)							)

				# Test batched
				lowerCamelCase__								=		feature_extractor(__lowerCAmelCase       ,  return_tensors='''np'''							).input_features
				lowerCamelCase__								=		feature_extractor(__lowerCAmelCase       ,  return_tensors='''np'''							).input_features
				for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase       ,  __lowerCAmelCase							):
					self.assertTrue(np.allclose(__lowerCAmelCase       ,  __lowerCAmelCase       ,  atol=1E-3							)							)

				# Test 2-D numpy arrays are batched.
				lowerCamelCase__								=		[floats_list((1, x)							)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
				lowerCamelCase__								=		np.asarray(__lowerCAmelCase							)
				lowerCamelCase__								=		feature_extractor(__lowerCAmelCase       ,  return_tensors='''np'''							).input_features
				lowerCamelCase__								=		feature_extractor(__lowerCAmelCase       ,  return_tensors='''np'''							).input_features
				for enc_seq_a, enc_seq_a in zip(__lowerCAmelCase       ,  __lowerCAmelCase							):
					self.assertTrue(np.allclose(__lowerCAmelCase       ,  __lowerCAmelCase       ,  atol=1E-3							)							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
				lowerCamelCase__								=		[floats_list((1, x)							)[0] for x in range(8_0_0       ,  1_4_0_0       ,  2_0_0							)]

				lowerCamelCase__								=		['''longest''', '''max_length''', '''do_not_pad''']
				lowerCamelCase__								=		[None, 1_6, None]
				for max_length, padding in zip(__lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		feature_extractor(
					    __lowerCAmelCase       ,  padding=__lowerCAmelCase       ,  max_length=__lowerCAmelCase       ,  return_attention_mask=__lowerCAmelCase							)
					lowerCamelCase__								=		inputs.input_features
					lowerCamelCase__								=		inputs.attention_mask
					lowerCamelCase__								=		[np.sum(__lowerCAmelCase							) for x in attention_mask]

					self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]							)
					self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]							)
					self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
				lowerCamelCase__								=		[floats_list((1, x)							)[0] for x in range(8_0_0       ,  1_4_0_0       ,  2_0_0							)]

				lowerCamelCase__								=		['''longest''', '''max_length''', '''do_not_pad''']
				lowerCamelCase__								=		[None, 1_6, None]
				for max_length, padding in zip(__lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		feature_extractor(
					    __lowerCAmelCase       ,  max_length=__lowerCAmelCase       ,  padding=__lowerCAmelCase       ,  return_tensors='''np'''       ,  return_attention_mask=__lowerCAmelCase							)
					lowerCamelCase__								=		inputs.input_features
					lowerCamelCase__								=		inputs.attention_mask
					lowerCamelCase__								=		[np.sum(__lowerCAmelCase							) for x in attention_mask]

					self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]							)
					self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6							)
					self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]							)
					self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6							)
					self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
				lowerCamelCase__								=		[floats_list((1, x)							)[0] for x in range(8_0_0       ,  1_4_0_0       ,  2_0_0							)]
				lowerCamelCase__								=		feature_extractor(
				    __lowerCAmelCase       ,  padding='''max_length'''       ,  max_length=4       ,  truncation=__lowerCAmelCase       ,  return_tensors='''np'''       ,  return_attention_mask=__lowerCAmelCase       ,  )
				lowerCamelCase__								=		inputs.input_features
				lowerCamelCase__								=		inputs.attention_mask
				lowerCamelCase__								=		np.sum(attention_mask == 1       ,  axis=1							)

				self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]							)
				self._check_zero_mean_unit_variance(input_features[1]							)
				self._check_zero_mean_unit_variance(input_features[2]							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
				lowerCamelCase__								=		[floats_list((1, x)							)[0] for x in range(8_0_0       ,  1_4_0_0       ,  2_0_0							)]
				lowerCamelCase__								=		feature_extractor(
				    __lowerCAmelCase       ,  padding='''longest'''       ,  max_length=4       ,  truncation=__lowerCAmelCase       ,  return_tensors='''np'''       ,  return_attention_mask=__lowerCAmelCase       ,  )
				lowerCamelCase__								=		inputs.input_features
				lowerCamelCase__								=		inputs.attention_mask
				lowerCamelCase__								=		np.sum(attention_mask == 1       ,  axis=1							)

				self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]							)
				self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]							)
				self._check_zero_mean_unit_variance(input_features[2]							)

				# make sure that if max_length < longest -> then pad to max_length
				self.assertEqual(input_features.shape       ,  (3, 4, 2_4)							)

				lowerCamelCase__								=		[floats_list((1, x)							)[0] for x in range(8_0_0       ,  1_4_0_0       ,  2_0_0							)]
				lowerCamelCase__								=		feature_extractor(
				    __lowerCAmelCase       ,  padding='''longest'''       ,  max_length=1_6       ,  truncation=__lowerCAmelCase       ,  return_tensors='''np'''       ,  return_attention_mask=__lowerCAmelCase       ,  )
				lowerCamelCase__								=		inputs.input_features
				lowerCamelCase__								=		inputs.attention_mask
				lowerCamelCase__								=		np.sum(attention_mask == 1       ,  axis=1							)

				self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]							)
				self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]							)
				self._check_zero_mean_unit_variance(input_features[2]							)

				# make sure that if max_length < longest -> then pad to max_length
				self.assertEqual(input_features.shape       ,  (3, 6, 2_4)							)





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				import torch

				lowerCamelCase__								=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
				lowerCamelCase__								=		np.random.rand(1_0_0       ,  3_2							).astype(np.floataa							)
				lowerCamelCase__								=		np_speech_inputs.tolist()

				for inputs in [py_speech_inputs, np_speech_inputs]:
					lowerCamelCase__								=		feature_extractor.pad([{'''input_features''': inputs}]       ,  return_tensors='''np'''							)
					self.assertTrue(np_processed.input_features.dtype == np.floataa							)
					lowerCamelCase__								=		feature_extractor.pad([{'''input_features''': inputs}]       ,  return_tensors='''pt'''							)
					self.assertTrue(pt_processed.input_features.dtype == torch.floataa							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				from datasets import load_dataset

				lowerCamelCase__								=		load_dataset('''hf-internal-testing/librispeech_asr_dummy'''       ,  '''clean'''       ,  split='''validation'''							)
				# automatic decoding with librispeech
				lowerCamelCase__								=		ds.sort('''id'''							).select(range(__lowerCAmelCase							)							)[:num_samples]['''audio''']

				return [x["array"] for x in speech_samples]





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		np.array([
				    -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
				    -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
				    -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
				]							)
				# fmt: on

				lowerCamelCase__								=		self._load_datasamples(1							)
				lowerCamelCase__								=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
				lowerCamelCase__								=		feature_extractor(__lowerCAmelCase       ,  return_tensors='''pt'''							).input_features
				self.assertEquals(input_features.shape       ,  (1, 5_8_4, 2_4)							)
				self.assertTrue(np.allclose(input_features[0, 0, :3_0]       ,  __lowerCAmelCase       ,  atol=1E-4							)							)



