





'''simple docstring'''





import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


UpperCAmelCase_  							=				random.Random()

if is_torch_available():
  import torch




def 	lowerCAmelCase_					(						__UpperCAmelCase:    Tuple	,     __UpperCAmelCase:    Tuple=1.0	,     __UpperCAmelCase:    Dict=None	,     __UpperCAmelCase:    int=None   )   ->			str:
    if rng is None:
        UpperCamelCase__							:       Optional[Any]      =   global_rng

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

    return values






class 					lowercase__ (			unittest.TestCase       ):

   '''simple docstring'''


   def __init__(					self,	__magic_name__,	__magic_name__=7,	__magic_name__=400,	__magic_name__=2000,	__magic_name__=1,	__magic_name__=0.0,	__magic_name__=16000,	__magic_name__=True,	__magic_name__=True,	)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       Any      =   parent
       UpperCamelCase__							:       Optional[int]      =   batch_size
       UpperCamelCase__							:       Union[str, Any]      =   min_seq_length
       UpperCamelCase__							:       Dict      =   max_seq_length
       UpperCamelCase__							:       Tuple      =   (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       UpperCamelCase__							:       str      =   feature_size
       UpperCamelCase__							:       Optional[int]      =   padding_value
       UpperCamelCase__							:       List[str]      =   sampling_rate
       UpperCamelCase__							:       List[str]      =   return_attention_mask
       UpperCamelCase__							:       int      =   do_normalize




   def    UpperCamelCase__						(					self							)				->   Union[str, Any]:






       """simple docstring"""
       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    UpperCamelCase__						(					self,	__magic_name__=False,	__magic_name__=False							)				->   Tuple:






       """simple docstring"""
       def _flatten(__magic_name__							):
           return list(itertools.chain(*A__							)							)

       if equal_length:
           UpperCamelCase__							:       Dict      =   floats_list((self.batch_size, self.max_seq_length)							)
       else:
           # make sure that inputs increase in size
           UpperCamelCase__							:       Union[str, Any]      =   [
               _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:
           UpperCamelCase__							:       Optional[int]      =   [np.asarray(A__							) for x in speech_inputs]

       return speech_inputs






@require_torch
@require_torchaudio
class 					lowercase__ (			__lowerCamelCase    ,       unittest.TestCase       ):

   '''simple docstring'''


   a		:							int     	=	ASTFeatureExtractor




   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__							:       Optional[Any]      =   ASTFeatureExtractionTester(self							)




   def    UpperCamelCase__						(					self							)				->   Any:






       """simple docstring"""
       # Tests that all call wrap to encode_plus and batch_encode_plus
       UpperCamelCase__							:       Optional[int]      =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
       # create three inputs of length 800, 1000, and 1200
       UpperCamelCase__							:       List[Any]      =   [floats_list((1, x)							)[0] for x in range(800,	1400,	200							)]
       UpperCamelCase__							:       Optional[Any]      =   [np.asarray(A__							) for speech_input in speech_inputs]

       # Test not batched input
       UpperCamelCase__							:       Tuple      =   feat_extract(speech_inputs[0],	return_tensors='''np'''							).input_values
       UpperCamelCase__							:       Tuple      =   feat_extract(np_speech_inputs[0],	return_tensors='''np'''							).input_values
       self.assertTrue(np.allclose(A__,	A__,	atol=1E-3							)							)

       # Test batched
       UpperCamelCase__							:       Tuple      =   feat_extract(A__,	padding=A__,	return_tensors='''np'''							).input_values
       UpperCamelCase__							:       Tuple      =   feat_extract(A__,	padding=A__,	return_tensors='''np'''							).input_values
       for enc_seq_a, enc_seq_a in zip(A__,	A__							):
           self.assertTrue(np.allclose(A__,	A__,	atol=1E-3							)							)

       # Test 2-D numpy arrays are batched.
       UpperCamelCase__							:       int      =   [floats_list((1, x)							)[0] for x in (800, 800, 800)]
       UpperCamelCase__							:       List[str]      =   np.asarray(A__							)
       UpperCamelCase__							:       Union[str, Any]      =   feat_extract(A__,	return_tensors='''np'''							).input_values
       UpperCamelCase__							:       Optional[Any]      =   feat_extract(A__,	return_tensors='''np'''							).input_values
       for enc_seq_a, enc_seq_a in zip(A__,	A__							):
           self.assertTrue(np.allclose(A__,	A__,	atol=1E-3							)							)




   @require_torch
   def    UpperCamelCase__						(					self							)				->   Optional[Any]:






       """simple docstring"""
       import torch

       UpperCamelCase__							:       Union[str, Any]      =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
       UpperCamelCase__							:       Tuple      =   np.random.rand(100							).astype(np.floataa							)
       UpperCamelCase__							:       Tuple      =   np_speech_inputs.tolist()

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




   def    UpperCamelCase__						(					self,	__magic_name__							)				->   Tuple:






       """simple docstring"""
       from datasets import load_dataset

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

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




   @require_torch
   def    UpperCamelCase__						(					self							)				->   str:






       """simple docstring"""
       # fmt: off
       UpperCamelCase__							:       Optional[Any]      =   torch.tensor(
           [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
            -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
            -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
            -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869]							)
       # fmt: on

       UpperCamelCase__							:       Any      =   self._load_datasamples(1							)
       UpperCamelCase__							:       Tuple      =   ASTFeatureExtractor()
       UpperCamelCase__							:       Dict      =   feature_extractor(A__,	return_tensors='''pt'''							).input_values
       self.assertEquals(input_values.shape,	(1, 1024, 128)							)
       self.assertTrue(torch.allclose(input_values[0, 0, :30],	A__,	atol=1E-4							)							)








import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
  import os

  import jax.numpy as jnp
  from jax import jit

  from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
  from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

  UpperCAmelCase_  							=				'0.12'  # assumed parallelism: 8


if is_torch_available():
  import torch




def 	lowerCAmelCase_					(						__UpperCAmelCase:    Dict	,     __UpperCAmelCase:    Tuple	,     __UpperCAmelCase:    Optional[Any]=None   )   ->			Union[str, Any]:
    if rng is None:
        UpperCamelCase__							:       Tuple      =   random.Random()

    UpperCamelCase__							:       List[str]      =   1
    for dim in shape:
        total_dims *= dim

    UpperCamelCase__							:       Tuple      =   []
    for _ in range(__UpperCAmelCase   ):
        values.append(rng.randint(0	,     vocab_size - 1   )   )

    UpperCamelCase__							:       Any      =   np.array(__UpperCAmelCase	,     dtype=jnp.intaa   ).reshape(__UpperCAmelCase   )

    return output




def 	lowerCAmelCase_					(						__UpperCAmelCase:    Optional[int]	,     __UpperCAmelCase:    Optional[int]=None   )   ->			Any:
    UpperCamelCase__							:       Any      =   ids_tensor(__UpperCAmelCase	,     vocab_size=2	,     rng=__UpperCAmelCase   )
    # make sure that at least one token is attended to for each batch
    UpperCamelCase__							:       Tuple      =   1
    return attn_mask






@require_flax
class 					lowercase__ :

   '''simple docstring'''


   a		:							Optional[int]     	=	None
   a		:							Union[str, Any]     	=	()




   def    UpperCamelCase__						(					self							)				->   str:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__							:       Tuple      =   self.model_tester.prepare_config_and_inputs_for_common()

       # cut to half length & take max batch_size 3
       UpperCamelCase__							:       str      =   2
       UpperCamelCase__							:       Dict      =   inputs['''input_ids'''].shape[-1] // 2
       UpperCamelCase__							:       int      =   inputs['''input_ids'''][:max_batch_size, :sequence_length]

       UpperCamelCase__							:       Tuple      =   jnp.ones_like(__magic_name__							)
       UpperCamelCase__							:       Any      =   attention_mask[:max_batch_size, :sequence_length]

       # generate max 5 tokens
       UpperCamelCase__							:       List[str]      =   input_ids.shape[-1] + 5
       if config.eos_token_id is not None and config.pad_token_id is None:
           # hack to allow generate for models such as GPT2 as is done in `generate()`
           UpperCamelCase__							:       Tuple      =   config.eos_token_id
       return config, input_ids, attention_mask, max_length




   @is_pt_flax_cross_test
   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Optional[int]      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       Optional[int]      =   False
       UpperCamelCase__							:       Optional[int]      =   max_length
       UpperCamelCase__							:       Dict      =   0

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       str      =   model_class(__magic_name__							)

           UpperCamelCase__							:       int      =   model_class.__name__[4:]  # Skip the "Flax" at the beginning
           UpperCamelCase__							:       List[str]      =   getattr(__magic_name__,	__magic_name__							)
           UpperCamelCase__							:       List[Any]      =   pt_model_class(__magic_name__							).eval()
           UpperCamelCase__							:       int      =   load_flax_weights_in_pytorch_model(__magic_name__,	flax_model.params							)

           UpperCamelCase__							:       Optional[int]      =   flax_model.generate(__magic_name__							).sequences
           UpperCamelCase__							:       Union[str, Any]      =   pt_model.generate(torch.tensor(__magic_name__,	dtype=torch.long							)							)

           if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
               UpperCamelCase__							:       Optional[Any]      =   flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

           self.assertListEqual(pt_generation_outputs.numpy().tolist(),	flax_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   Optional[int]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       List[Any]      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       Dict      =   False
       UpperCamelCase__							:       List[str]      =   max_length

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       Dict      =   model_class(__magic_name__							)

           UpperCamelCase__							:       List[Any]      =   model.generate(__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       int      =   jit(model.generate							)
           UpperCamelCase__							:       Optional[Any]      =   jit_generate(__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   Optional[int]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Union[str, Any]      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       Optional[int]      =   True
       UpperCamelCase__							:       List[str]      =   max_length

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       Dict      =   model_class(__magic_name__							)

           UpperCamelCase__							:       Dict      =   model.generate(__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       Optional[Any]      =   jit(model.generate							)
           UpperCamelCase__							:       Optional[int]      =   jit_generate(__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Optional[Any]      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       List[str]      =   False
       UpperCamelCase__							:       Dict      =   max_length
       UpperCamelCase__							:       str      =   2

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       Optional[int]      =   model_class(__magic_name__							)

           UpperCamelCase__							:       List[Any]      =   model.generate(__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       str      =   jit(model.generate							)
           UpperCamelCase__							:       int      =   jit_generate(__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   Union[str, Any]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Any      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       Tuple      =   False
       UpperCamelCase__							:       Union[str, Any]      =   max_length
       UpperCamelCase__							:       Optional[Any]      =   2
       UpperCamelCase__							:       List[Any]      =   2

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       List[str]      =   model_class(__magic_name__							)

           UpperCamelCase__							:       List[str]      =   model.generate(__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[0],	input_ids.shape[0] * config.num_return_sequences							)




   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Optional[int]      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       List[str]      =   True
       UpperCamelCase__							:       Any      =   max_length
       UpperCamelCase__							:       Union[str, Any]      =   0.8
       UpperCamelCase__							:       Dict      =   10
       UpperCamelCase__							:       Any      =   0.3
       UpperCamelCase__							:       str      =   1
       UpperCamelCase__							:       Union[str, Any]      =   8
       UpperCamelCase__							:       Union[str, Any]      =   9

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       int      =   model_class(__magic_name__							)

           UpperCamelCase__							:       Union[str, Any]      =   model.generate(__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       Any      =   jit(model.generate							)
           UpperCamelCase__							:       List[Any]      =   jit_generate(__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   List[Any]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Optional[int]      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       Any      =   max_length
       UpperCamelCase__							:       str      =   1
       UpperCamelCase__							:       Dict      =   8
       UpperCamelCase__							:       List[Any]      =   9

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       Union[str, Any]      =   model_class(__magic_name__							)

           UpperCamelCase__							:       Optional[int]      =   model.generate(__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       Dict      =   jit(model.generate							)
           UpperCamelCase__							:       int      =   jit_generate(__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   Optional[int]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       int      =   self._get_input_ids_and_config()
       UpperCamelCase__							:       Dict      =   max_length
       UpperCamelCase__							:       List[Any]      =   2
       UpperCamelCase__							:       Dict      =   1
       UpperCamelCase__							:       int      =   8
       UpperCamelCase__							:       List[Any]      =   9

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       Union[str, Any]      =   model_class(__magic_name__							)

           UpperCamelCase__							:       Union[str, Any]      =   model.generate(__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       Optional[Any]      =   jit(model.generate							)
           UpperCamelCase__							:       Any      =   jit_generate(__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   Union[str, Any]:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Optional[Any]      =   self._get_input_ids_and_config()

       # pad attention mask on the left
       UpperCamelCase__							:       Optional[int]      =   attention_mask.at[(0, 0)].set(0							)

       UpperCamelCase__							:       Tuple      =   False
       UpperCamelCase__							:       str      =   max_length

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       int      =   model_class(__magic_name__							)

           UpperCamelCase__							:       List[Any]      =   model.generate(__magic_name__,	attention_mask=__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       str      =   jit(model.generate							)
           UpperCamelCase__							:       Optional[Any]      =   jit_generate(__magic_name__,	attention_mask=__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   Tuple:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       List[str]      =   self._get_input_ids_and_config()

       # pad attention mask on the left
       UpperCamelCase__							:       Any      =   attention_mask.at[(0, 0)].set(0							)

       UpperCamelCase__							:       Any      =   True
       UpperCamelCase__							:       Union[str, Any]      =   max_length

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       Union[str, Any]      =   model_class(__magic_name__							)

           UpperCamelCase__							:       Optional[Any]      =   model.generate(__magic_name__,	attention_mask=__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       Union[str, Any]      =   jit(model.generate							)
           UpperCamelCase__							:       str      =   jit_generate(__magic_name__,	attention_mask=__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)




   def    UpperCamelCase__						(					self							)				->   int:






       """simple docstring"""
       UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__     ,UpperCamelCase__							:       Optional[int]      =   self._get_input_ids_and_config()

       # pad attention mask on the left
       UpperCamelCase__							:       Union[str, Any]      =   attention_mask.at[(0, 0)].set(0							)

       UpperCamelCase__							:       Optional[Any]      =   2
       UpperCamelCase__							:       List[str]      =   max_length

       for model_class in self.all_generative_model_classes:
           UpperCamelCase__							:       int      =   model_class(__magic_name__							)

           UpperCamelCase__							:       Any      =   model.generate(__magic_name__,	attention_mask=__magic_name__							).sequences
           self.assertEqual(generation_outputs.shape[-1],	__magic_name__							)

           UpperCamelCase__							:       Tuple      =   jit(model.generate							)
           UpperCamelCase__							:       int      =   jit_generate(__magic_name__,	attention_mask=__magic_name__							).sequences

           self.assertListEqual(generation_outputs.tolist(),	jit_generation_outputs.tolist()							)






@require_flax
class 					lowercase__ (			unittest.TestCase       ):

   '''simple docstring'''


   def    UpperCamelCase__						(					self							)				->   Tuple:






       """simple docstring"""
       UpperCamelCase__							:       List[str]      =   AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert'''							)
       UpperCamelCase__							:       Any      =   FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''							)

       UpperCamelCase__							:       int      =   '''Hello world'''
       UpperCamelCase__							:       Tuple      =   tokenizer(__magic_name__,	return_tensors='''np'''							).input_ids

       # typos are quickly detected (the correct argument is `do_sample`)
       with self.assertRaisesRegex(__magic_name__,	'''do_samples'''							):
           model.generate(__magic_name__,	do_samples=__magic_name__							)

       # arbitrary arguments that will not be used anywhere are also not accepted
       with self.assertRaisesRegex(__magic_name__,	'''foo'''							):
           UpperCamelCase__							:       Optional[Any]      =   {'''foo''': '''bar'''}
           model.generate(__magic_name__,	**__magic_name__							)

