






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 	lowercase_      :






      '''simple docstring'''




      def __init__(   self   :		Dict				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		Optional[Any]=99				,      __UpperCAmelCase   :		Tuple=13				,      __UpperCAmelCase   :		Optional[Any]=16				,      __UpperCAmelCase   :		int=7				,      __UpperCAmelCase   :		str=True				,      __UpperCAmelCase   :		List[Any]=True				,      __UpperCAmelCase   :		Optional[int]=True				,      __UpperCAmelCase   :		Union[str, Any]=False				,      __UpperCAmelCase   :		Optional[int]=True				,      __UpperCAmelCase   :		Tuple=2				,      __UpperCAmelCase   :		Optional[Any]=32				,      __UpperCAmelCase   :		Optional[int]=4				,      __UpperCAmelCase   :		Any=4				,      __UpperCAmelCase   :		Tuple=30				,      __UpperCAmelCase   :		int=0				,      __UpperCAmelCase   :		Dict=1				,      __UpperCAmelCase   :		Any=2				,      __UpperCAmelCase   :		Optional[Any]=None				,      )					->Optional[int]:




       """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    __lowerCAmelCase      (   self   :		Optional[Any]						)					->List[str]:




       """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    __lowerCAmelCase      (   self   :		Any				,      __UpperCAmelCase   :		Optional[int]				,      __UpperCAmelCase   :		Union[str, Any]				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		int				,      )					->Union[str, Any]:




       """simple docstring"""


       a							=   True
       a							=   SpeechaTextaDecoder(config=__UpperCAmelCase						).to(__UpperCAmelCase						).eval()
       a							=   input_ids[:2]

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

       self.parent.assertTrue(len(__UpperCAmelCase						) == len(__UpperCAmelCase						)						)
       self.parent.assertTrue(len(__UpperCAmelCase						) == len(__UpperCAmelCase						) + 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(__UpperCAmelCase						)

       a							=   model(__UpperCAmelCase						)['''last_hidden_state''']
       a							=   model(__UpperCAmelCase				,      past_key_values=__UpperCAmelCase						)['''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(__UpperCAmelCase				,      __UpperCAmelCase				,      atol=1e-3						)







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




       """simple docstring"""


       a							=   self.prepare_config_and_inputs()
       (
           (
           a
       )						,  (
           a
       )						,  (
           a
       )						,  (
           a
       )						,  
       )							=   config_and_inputs

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






@require_torch
class 	lowercase_      (			lowercase				,						lowercase				,						lowercase				,						unittest.TestCase     ):






      '''simple docstring'''




      __snake_case  						=						(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
      __snake_case  						=						(SpeechaTextaForCausalLM,) if is_torch_available() else ()
      __snake_case  						=						{'''text-generation''': SpeechaTextaForCausalLM} if is_torch_available() else {}
      __snake_case  						=						True
      __snake_case  						=						False







      def    __lowerCAmelCase      (   self   :		List[Any]				,      )					->int:




       """simple docstring"""


       a							=   SpeechaTextaStandaloneDecoderModelTester(self				,      is_training=__UpperCAmelCase						)
       a							=   ConfigTester(self				,      config_class=__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		Dict						)					->Tuple:




       """simple docstring"""


       pass







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




       """simple docstring"""


       pass







      def    __lowerCAmelCase      (   self   :		List[Any]						)					->str:




       """simple docstring"""


       pass







      def    __lowerCAmelCase      (   self   :		str						)					->Any:




       """simple docstring"""


       self.config_tester.run_common_tests()







      def    __lowerCAmelCase      (   self   :		int						)					->Dict:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past(*__UpperCAmelCase						)







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




       """simple docstring"""


       return










def 		_a					(   a				:float				)							->    float:
 return -num if num < 0 else num
def 		_a					(   a				:list[int]				)							->    int:
 if len(a				) == 0:
  raise ValueError('''abs_min() arg is an empty sequence'''				)
 a							=   x[0]
 for i in x:
  if abs_val(a				) < abs_val(a				):
   a							=   i
 return j
def 		_a					(   a				:list[int]				)							->    int:
 if len(a				) == 0:
  raise ValueError('''abs_max() arg is an empty sequence'''				)
 a							=   x[0]
 for i in x:
  if abs(a				) > abs(a				):
   a							=   i
 return j
def 		_a					(   a				:list[int]				)							->    int:
 if len(a				) == 0:
  raise ValueError('''abs_max_sort() arg is an empty sequence'''				)
 return sorted(a			,							key=a				)[-1]
def 		_a					(   )							->    int:
 assert abs_val(0				) == 0
 assert abs_val(34				) == 34
 assert abs_val(-100_000_000_000				) == 100_000_000_000

 a							=   [-3, -1, 2, -11]
 assert abs_max(a				) == -11
 assert abs_max_sort(a				) == -11
 assert abs_min(a				) == -1


if __name__ == "__main__":
     import doctest

     doctest.testmod()

     test_abs_val()
     print(abs_val(-34))  # --> 34


