

import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

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


if is_torch_available():
       import torch

       from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
           SpeechaTextaDecoder,
           SpeechaTextaForCausalLM,
       )







@require_torch
class 		__magic_name__     :

   '''simple docstring'''




   def __init__(							self,       lowercase_,       lowercase_=99,       lowercase_=13,       lowercase_=16,       lowercase_=7,       lowercase_=True,       lowercase_=True,       lowercase_=True,       lowercase_=False,       lowercase_=True,       lowercase_=2,       lowercase_=32,       lowercase_=4,       lowercase_=4,       lowercase_=30,       lowercase_=0,       lowercase_=1,       lowercase_=2,       lowercase_=None,       )				->       List[str]:






     """simple docstring"""






     a__    =parent
     a__    =batch_size
     a__    =decoder_seq_length
     # For common tests
     a__    =self.decoder_seq_length
     a__    =is_training
     a__    =use_attention_mask
     a__    =use_labels

     a__    =vocab_size
     a__    =d_model
     a__    =d_model
     a__    =decoder_layers
     a__    =decoder_layers
     a__    =decoder_ffn_dim
     a__    =decoder_attention_heads
     a__    =decoder_attention_heads
     a__    =eos_token_id
     a__    =bos_token_id
     a__    =pad_token_id
     a__    =decoder_start_token_id
     a__    =use_cache
     a__    =max_position_embeddings

     a__    =None
     a__    =decoder_seq_length
     a__    =2
     a__    =1



   def 					_UpperCAmelCase					(							self			)				->       Any:






     """simple docstring"""






     a__    =ids_tensor([self.batch_size, self.decoder_seq_length],       self.vocab_size			)

     a__    =None
     if self.use_attention_mask:
       a__    =ids_tensor([self.batch_size, self.decoder_seq_length],       vocab_size=2			)

     a__    =None
     if self.use_labels:
       a__    =ids_tensor([self.batch_size, self.decoder_seq_length],       self.vocab_size			)

     a__    =SpeechaTextaConfig(
         vocab_size=self.vocab_size,       d_model=self.d_model,       decoder_layers=self.decoder_layers,       decoder_ffn_dim=self.decoder_ffn_dim,       decoder_attention_heads=self.decoder_attention_heads,       eos_token_id=self.eos_token_id,       bos_token_id=self.bos_token_id,       use_cache=self.use_cache,       pad_token_id=self.pad_token_id,       decoder_start_token_id=self.decoder_start_token_id,       max_position_embeddings=self.max_position_embeddings,       )

     return (
         config,
         input_ids,
         attention_mask,
         lm_labels,
     )



   def 					_UpperCAmelCase					(							self,       lowercase_,       lowercase_,       lowercase_,       lowercase_,       )				->       Dict:






     """simple docstring"""






     a__    =True
     a__    =SpeechaTextaDecoder(config=lowercase_			).to(lowercase_			).eval()
     a__    =input_ids[:2]

     input_ids[input_ids == 0] += 1
     # first forward pass
     a__    =model(lowercase_,       use_cache=lowercase_			)
     a__    =model(lowercase_			)
     a__    =model(lowercase_,       use_cache=lowercase_			)

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

     a__    =outputs['past_key_values']

     # create hypothetical next token and extent to next_input_ids
     a__    =ids_tensor((2, 1),       config.vocab_size - 1			) + 1

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

     print(lowercase_			)

     a__    =model(lowercase_			)['last_hidden_state']
     a__    =model(lowercase_,       past_key_values=lowercase_			)['last_hidden_state']

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

     # test that outputs are equal for slice
     assert torch.allclose(lowercase_,       lowercase_,       atol=1E-3			)



   def 					_UpperCAmelCase					(							self			)				->       Optional[Any]:






     """simple docstring"""






     a__    =self.prepare_config_and_inputs()
     (
         a__				
     )    =config_and_inputs

     a__    ={
         'input_ids': input_ids,
         'attention_mask': attention_mask,
     }
     return config, inputs_dict







@require_torch
class 		__magic_name__     (				snake_case__						,			snake_case__						,			snake_case__						,			unittest.TestCase				):

   '''simple docstring'''




   lowerCamelCase__					:      Optional[Any]        =  (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
   lowerCamelCase__					:      Optional[Any]        =  (SpeechaTextaForCausalLM,) if is_torch_available() else ()
   lowerCamelCase__					:      Optional[Any]        =  {'text-generation': SpeechaTextaForCausalLM} if is_torch_available() else {}
   lowerCamelCase__					:      Any        =  True
   lowerCamelCase__					:      str        =  False



   def 					_UpperCAmelCase					(							self,       )				->       Optional[Any]:






     """simple docstring"""






     a__    =SpeechaTextaStandaloneDecoderModelTester(self,       is_training=lowercase_			)
     a__    =ConfigTester(self,       config_class=lowercase_			)



   def 					_UpperCAmelCase					(							self			)				->       Any:






     """simple docstring"""






     pass



   def 					_UpperCAmelCase					(							self			)				->       Tuple:






     """simple docstring"""






     pass



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






     """simple docstring"""






     pass



   def 					_UpperCAmelCase					(							self			)				->       Tuple:






     """simple docstring"""






     self.config_tester.run_common_tests()



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






     """simple docstring"""






     a__    =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past(*lowercase_			)



   def 					_UpperCAmelCase					(							self			)				->       str:






     """simple docstring"""






     return



import numpy as np







def _a					(			SCREAMING_SNAKE_CASE						:     Union[str, Any]						)       ->       List[str]:

 """simple docstring"""






 __lowerCAmelCase:  Union[str, Any]        = np.exp(SCREAMING_SNAKE_CASE						)

 # Add up the all the exponentials
 __lowerCAmelCase:  Dict        = np.sum(SCREAMING_SNAKE_CASE						)

 # Divide every exponent by the sum of all exponents
 __lowerCAmelCase:  int        = exponent_vector / sum_of_exponents

 return softmax_vector


if __name__ == "__main__":
  print(softmax((0,)))





