






"""simple docstring"""


import unittest

from transformers import (
    MODEL_FOR_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
    TextGenerationPipeline,
    logging,
    pipeline,
)
from transformers.testing_utils import (
    CaptureLogger,
    is_pipeline_test,
    require_accelerate,
    require_tf,
    require_torch,
    require_torch_gpu,
    require_torch_or_tf,
)

from .test_pipelines_common import ANY


@is_pipeline_test
@require_torch_or_tf
class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   A_						:	List[Any]						 = MODEL_FOR_CAUSAL_LM_MAPPING
   A_						:	Dict						 = TF_MODEL_FOR_CAUSAL_LM_MAPPING


   @require_torch
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     __A									= pipeline(task='''text-generation''',       model='''sshleifer/tiny-ctrl''',       framework='''pt'''  )
     # Using `do_sample=False` to force deterministic output
     __A									= text_generator('''This is a test''',       do_sample=_lowerCamelCase  )
     self.assertEqual(
         _lowerCamelCase,       [
             {
                 '''generated_text''': (
                     '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.'''
                     ''' oscope. FiliFili@@'''
                 )
             }
         ],       )

     __A									= text_generator(['''This is a test''', '''This is a second test''']  )
     self.assertEqual(
         _lowerCamelCase,       [
             [
                 {
                     '''generated_text''': (
                         '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.'''
                         ''' oscope. FiliFili@@'''
                     )
                 }
             ],
             [
                 {
                     '''generated_text''': (
                         '''This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy'''
                         ''' oscope. oscope. FiliFili@@'''
                     )
                 }
             ],
         ],       )

     __A									= text_generator('''This is a test''',       do_sample=_lowerCamelCase,       num_return_sequences=2,       return_tensors=_lowerCamelCase  )
     self.assertEqual(
         _lowerCamelCase,       [
             {'''generated_token_ids''': ANY(_lowerCamelCase  )},
             {'''generated_token_ids''': ANY(_lowerCamelCase  )},
         ],       )
     __A									= text_generator.model.config.eos_token_id
     __A									= '''<pad>'''
     __A									= text_generator(
         ['''This is a test''', '''This is a second test'''],       do_sample=_lowerCamelCase,       num_return_sequences=2,       batch_size=2,       return_tensors=_lowerCamelCase,       )
     self.assertEqual(
         _lowerCamelCase,       [
             [
                 {'''generated_token_ids''': ANY(_lowerCamelCase  )},
                 {'''generated_token_ids''': ANY(_lowerCamelCase  )},
             ],
             [
                 {'''generated_token_ids''': ANY(_lowerCamelCase  )},
                 {'''generated_token_ids''': ANY(_lowerCamelCase  )},
             ],
         ],       )


   @require_tf
   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     __A									= pipeline(task='''text-generation''',       model='''sshleifer/tiny-ctrl''',       framework='''tf'''  )

     # Using `do_sample=False` to force deterministic output
     __A									= text_generator('''This is a test''',       do_sample=_lowerCamelCase  )
     self.assertEqual(
         _lowerCamelCase,       [
             {
                 '''generated_text''': (
                     '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵'''
                     ''' please,'''
                 )
             }
         ],       )

     __A									= text_generator(['''This is a test''', '''This is a second test'''],       do_sample=_lowerCamelCase  )
     self.assertEqual(
         _lowerCamelCase,       [
             [
                 {
                     '''generated_text''': (
                         '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵'''
                         ''' please,'''
                     )
                 }
             ],
             [
                 {
                     '''generated_text''': (
                         '''This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes'''
                         ''' Cannes 閲閲Cannes Cannes Cannes 攵 please,'''
                     )
                 }
             ],
         ],       )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Tuple,       _lowerCamelCase :     Union[str, Any],       _lowerCamelCase :     Tuple,       _lowerCamelCase :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= TextGenerationPipeline(model=_lowerCamelCase,       tokenizer=_lowerCamelCase  )
     return text_generator, ["This is a test", "Another test"]


   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     __A									= '''Hello I believe in'''
     __A									= pipeline('''text-generation''',       model='''hf-internal-testing/tiny-random-gpt2'''  )
     __A									= text_generator(_lowerCamelCase  )
     self.assertEqual(
         _lowerCamelCase,       [{'''generated_text''': '''Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe'''}],       )

     __A									= text_generator(_lowerCamelCase,       stop_sequence=''' fe'''  )
     self.assertEqual(_lowerCamelCase,       [{'''generated_text''': '''Hello I believe in fe'''}]  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any,       _lowerCamelCase :     Optional[int],       _lowerCamelCase :     Any  ):

     '''simple docstring'''
     __A									= text_generator.model
     __A									= text_generator.tokenizer

     __A									= text_generator('''This is a test'''  )
     self.assertEqual(_lowerCamelCase,       [{'''generated_text''': ANY(_lowerCamelCase  )}]  )
     self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test'''  )  )

     __A									= text_generator('''This is a test''',       return_full_text=_lowerCamelCase  )
     self.assertEqual(_lowerCamelCase,       [{'''generated_text''': ANY(_lowerCamelCase  )}]  )
     self.assertNotIn('''This is a test''',       outputs[0]['''generated_text''']  )

     __A									= pipeline(task='''text-generation''',       model=_lowerCamelCase,       tokenizer=_lowerCamelCase,       return_full_text=_lowerCamelCase  )
     __A									= text_generator('''This is a test'''  )
     self.assertEqual(_lowerCamelCase,       [{'''generated_text''': ANY(_lowerCamelCase  )}]  )
     self.assertNotIn('''This is a test''',       outputs[0]['''generated_text''']  )

     __A									= text_generator('''This is a test''',       return_full_text=_lowerCamelCase  )
     self.assertEqual(_lowerCamelCase,       [{'''generated_text''': ANY(_lowerCamelCase  )}]  )
     self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test'''  )  )

     __A									= text_generator(['''This is great !''', '''Something else'''],       num_return_sequences=2,       do_sample=_lowerCamelCase  )
     self.assertEqual(
         _lowerCamelCase,       [
             [{'''generated_text''': ANY(_lowerCamelCase  )}, {'''generated_text''': ANY(_lowerCamelCase  )}],
             [{'''generated_text''': ANY(_lowerCamelCase  )}, {'''generated_text''': ANY(_lowerCamelCase  )}],
         ],       )

     if text_generator.tokenizer.pad_token is not None:
       __A									= text_generator(
           ['''This is great !''', '''Something else'''],       num_return_sequences=2,       batch_size=2,       do_sample=_lowerCamelCase  )
       self.assertEqual(
           _lowerCamelCase,       [
               [{'''generated_text''': ANY(_lowerCamelCase  )}, {'''generated_text''': ANY(_lowerCamelCase  )}],
               [{'''generated_text''': ANY(_lowerCamelCase  )}, {'''generated_text''': ANY(_lowerCamelCase  )}],
           ],       )

     with self.assertRaises(_lowerCamelCase  ):
       __A									= text_generator('''test''',       return_full_text=_lowerCamelCase,       return_text=_lowerCamelCase  )
     with self.assertRaises(_lowerCamelCase  ):
       __A									= text_generator('''test''',       return_full_text=_lowerCamelCase,       return_tensors=_lowerCamelCase  )
     with self.assertRaises(_lowerCamelCase  ):
       __A									= text_generator('''test''',       return_text=_lowerCamelCase,       return_tensors=_lowerCamelCase  )

     # Empty prompt is slighly special
     # it requires BOS token to exist.
     # Special case for Pegasus which will always append EOS so will
     # work even without BOS.
     if (
         text_generator.tokenizer.bos_token_id is not None
         or "Pegasus" in tokenizer.__class__.__name__
         or "Git" in model.__class__.__name__
     ):
       __A									= text_generator(''''''  )
       self.assertEqual(_lowerCamelCase,       [{'''generated_text''': ANY(_lowerCamelCase  )}]  )
     else:
       with self.assertRaises((ValueError, AssertionError)  ):
         __A									= text_generator(''''''  )

     if text_generator.framework == "tf":
       # TF generation does not support max_new_tokens, and it's impossible
       # to control long generation with only max_length without
       # fancy calculation, dismissing tests for now.
       return
     # We don't care about infinite range models.
     # They already work.
     # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly.
     __A									= ['''RwkvForCausalLM''', '''XGLMForCausalLM''', '''GPTNeoXForCausalLM''']
     if (
         tokenizer.model_max_length < 1_00_00
         and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS
     ):
       # Handling of large generations
       with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError)  ):
         text_generator('''This is a test''' * 5_00,       max_new_tokens=20  )

       __A									= text_generator('''This is a test''' * 5_00,       handle_long_generation='''hole''',       max_new_tokens=20  )
       # Hole strategy cannot work
       with self.assertRaises(_lowerCamelCase  ):
         text_generator(
             '''This is a test''' * 5_00,       handle_long_generation='''hole''',       max_new_tokens=tokenizer.model_max_length + 10,       )


   @require_torch
   @require_accelerate
   @require_torch_gpu
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     import torch

     # Classic `model_kwargs`
     __A									= pipeline(
         model='''hf-internal-testing/tiny-random-bloom''',       model_kwargs={'''device_map''': '''auto''', '''torch_dtype''': torch.bfloataa},       )
     self.assertEqual(pipe.model.device,       torch.device(0  )  )
     self.assertEqual(pipe.model.lm_head.weight.dtype,       torch.bfloataa  )
     __A									= pipe('''This is a test'''  )
     self.assertEqual(
         _lowerCamelCase,       [
             {
                 '''generated_text''': (
                     '''This is a test test test test test test test test test test test test test test test test'''
                     ''' test'''
                 )
             }
         ],       )

     # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.)
     __A									= pipeline(model='''hf-internal-testing/tiny-random-bloom''',       device_map='''auto''',       torch_dtype=torch.bfloataa  )
     self.assertEqual(pipe.model.device,       torch.device(0  )  )
     self.assertEqual(pipe.model.lm_head.weight.dtype,       torch.bfloataa  )
     __A									= pipe('''This is a test'''  )
     self.assertEqual(
         _lowerCamelCase,       [
             {
                 '''generated_text''': (
                     '''This is a test test test test test test test test test test test test test test test test'''
                     ''' test'''
                 )
             }
         ],       )

     # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602
     __A									= pipeline(model='''hf-internal-testing/tiny-random-bloom''',       device_map='''auto'''  )
     self.assertEqual(pipe.model.device,       torch.device(0  )  )
     self.assertEqual(pipe.model.lm_head.weight.dtype,       torch.floataa  )
     __A									= pipe('''This is a test'''  )
     self.assertEqual(
         _lowerCamelCase,       [
             {
                 '''generated_text''': (
                     '''This is a test test test test test test test test test test test test test test test test'''
                     ''' test'''
                 )
             }
         ],       )


   @require_torch
   @require_torch_gpu
   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     import torch

     __A									= pipeline(model='''hf-internal-testing/tiny-random-bloom''',       device=0,       torch_dtype=torch.floataa  )
     pipe('''This is a test'''  )


   @require_torch
   @require_accelerate
   @require_torch_gpu
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     import torch

     __A									= pipeline(model='''hf-internal-testing/tiny-random-bloom''',       device_map='''auto''',       torch_dtype=torch.floataa  )
     pipe('''This is a test''',       do_sample=_lowerCamelCase,       top_p=0.5  )






   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= '''Hello world'''
     __A									= pipeline('''text-generation''',       model='''hf-internal-testing/tiny-random-gpt2'''  )
     if text_generator.model.framework == "tf":
       __A									= logging.get_logger('''transformers.generation.tf_utils'''  )
     else:
       __A									= logging.get_logger('''transformers.generation.utils'''  )
     __A									= '''Both `max_new_tokens`'''  # The beggining of the message to be checked in this test

     # Both are set by the user -> log warning
     with CaptureLogger(_lowerCamelCase  ) as cl:
       __A									= text_generator(_lowerCamelCase,       max_length=10,       max_new_tokens=1  )
     self.assertIn(_lowerCamelCase,       cl.out  )

     # The user only sets one -> no warning
     with CaptureLogger(_lowerCamelCase  ) as cl:
       __A									= text_generator(_lowerCamelCase,       max_new_tokens=1  )
     self.assertNotIn(_lowerCamelCase,       cl.out  )

     with CaptureLogger(_lowerCamelCase  ) as cl:
       __A									= text_generator(_lowerCamelCase,       max_length=10  )
     self.assertNotIn(_lowerCamelCase,       cl.out  )












"""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


lowercase_				     =     random.Random()

if is_torch_available():
       import torch



def 					lowerCAmelCase				(    __UpperCamelCase		,       __UpperCamelCase=1.0		,       __UpperCamelCase=None		,       __UpperCamelCase=None ):




  """simple docstring"""







  if rng is None:
    __A									= global_rng

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

  return values


class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   def __init__(	self :     Any,       _lowerCamelCase :     List[str],       _lowerCamelCase :     Any=7,       _lowerCamelCase :     Optional[int]=4_00,       _lowerCamelCase :     Optional[int]=20_00,       _lowerCamelCase :     Dict=1,       _lowerCamelCase :     Optional[Any]=0.0,       _lowerCamelCase :     int=1_60_00,       _lowerCamelCase :     Optional[int]=True,       _lowerCamelCase :     Dict=True,       ):

     '''simple docstring'''
     __A									= parent
     __A									= batch_size
     __A									= min_seq_length
     __A									= max_seq_length
     __A									= (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
     __A									= feature_size
     __A									= padding_value
     __A									= sampling_rate
     __A									= return_attention_mask
     __A									= do_normalize


   def 				_SCREAMING_SNAKE_CASE				(	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 				_SCREAMING_SNAKE_CASE				(	self :     Any,       _lowerCamelCase :     Optional[Any]=False,       _lowerCamelCase :     int=False  ):

     '''simple docstring'''
     def _flatten(_lowerCamelCase :     List[str]  ):
       return list(itertools.chain(*_lowerCamelCase  )  )

     if equal_length:
       __A									= floats_list((self.batch_size, self.max_seq_length)  )
     else:
       # make sure that inputs increase in size
       __A									= [
           _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:
       __A									= [np.asarray(_lowerCamelCase  ) for x in speech_inputs]

     return speech_inputs





@require_torch
@require_torchaudio
class 					snake_case		(							_lowerCAmelCase   ,	unittest.TestCase						):


   '''simple docstring'''

   A_						:	int						 = ASTFeatureExtractor


   def 				_SCREAMING_SNAKE_CASE				(	self :     Tuple  ):

     '''simple docstring'''
     __A									= ASTFeatureExtractionTester(self  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     # Tests that all call wrap to encode_plus and batch_encode_plus
     __A									= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
     # create three inputs of length 800, 1000, and 1200
     __A									= [floats_list((1, x)  )[0] for x in range(8_00,       14_00,       2_00  )]
     __A									= [np.asarray(_lowerCamelCase  ) for speech_input in speech_inputs]

     # Test not batched input
     __A									= feat_extract(speech_inputs[0],       return_tensors='''np'''  ).input_values
     __A									= feat_extract(np_speech_inputs[0],       return_tensors='''np'''  ).input_values
     self.assertTrue(np.allclose(_lowerCamelCase,       _lowerCamelCase,       atol=1e-3  )  )

     # Test batched
     __A									= feat_extract(_lowerCamelCase,       padding=_lowerCamelCase,       return_tensors='''np'''  ).input_values
     __A									= feat_extract(_lowerCamelCase,       padding=_lowerCamelCase,       return_tensors='''np'''  ).input_values
     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.
     __A									= [floats_list((1, x)  )[0] for x in (8_00, 8_00, 8_00)]
     __A									= np.asarray(_lowerCamelCase  )
     __A									= feat_extract(_lowerCamelCase,       return_tensors='''np'''  ).input_values
     __A									= feat_extract(_lowerCamelCase,       return_tensors='''np'''  ).input_values
     for enc_seq_a, enc_seq_a in zip(_lowerCamelCase,       _lowerCamelCase  ):
       self.assertTrue(np.allclose(_lowerCamelCase,       _lowerCamelCase,       atol=1e-3  )  )


   @require_torch
   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     import torch

     __A									= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
     __A									= np.random.rand(1_00  ).astype(np.floataa  )
     __A									= np_speech_inputs.tolist()

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


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int],       _lowerCamelCase :     Union[str, Any]  ):

     '''simple docstring'''
     from datasets import load_dataset

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

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






   @require_torch
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     # fmt: off
     __A									= torch.tensor(
         [-0.98_94, -1.27_76, -0.90_66, -1.27_76, -0.93_49, -1.26_09, -1.03_86, -1.27_76,
          -1.15_61, -1.27_76, -1.20_52, -1.27_23, -1.21_90, -1.21_32, -1.27_76, -1.11_33,
          -1.19_53, -1.13_43, -1.15_84, -1.22_03, -1.17_70, -1.24_74, -1.23_81, -1.19_36,
          -0.92_70, -0.83_17, -0.80_49, -0.77_06, -0.75_65, -0.78_69]  )
     # fmt: on

     __A									= self._load_datasamples(1  )
     __A									= ASTFeatureExtractor()
     __A									= feature_extractor(_lowerCamelCase,       return_tensors='''pt'''  ).input_values
     self.assertEquals(input_values.shape,       (1, 10_24, 1_28)  )
     self.assertTrue(torch.allclose(input_values[0, 0, :30],       _lowerCamelCase,       atol=1e-4  )  )




