


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




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


										"""simple docstring"""





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

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

										_SCREAMING_SNAKE_CASE     					=None
										_SCREAMING_SNAKE_CASE     					=decoder_seq_length
										_SCREAMING_SNAKE_CASE     					=2
										_SCREAMING_SNAKE_CASE     					=1




						def 							__UpperCamelCase			(    self   : Tuple					)							->       Tuple:


										"""simple docstring"""





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

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

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

										_SCREAMING_SNAKE_CASE     					=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   : str			,      _a   : List[Any]			,      _a   : Union[str, Any]			,      _a   : Union[str, Any]			,      _a   : Optional[int]			,      )							->       str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=True
										_SCREAMING_SNAKE_CASE     					=SpeechaTextaDecoder(config=_a					).to(_a					).eval()
										_SCREAMING_SNAKE_CASE     					=input_ids[:2]

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

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

										_SCREAMING_SNAKE_CASE     					=outputs['''past_key_values''']

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

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

										print(_a					)

										_SCREAMING_SNAKE_CASE     					=model(_a					)['''last_hidden_state''']
										_SCREAMING_SNAKE_CASE     					=model(_a			,      past_key_values=_a					)['''last_hidden_state''']

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

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




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


										"""simple docstring"""





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

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

@require_torch
class   A__							(						UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       unittest.TestCase					):
						UpperCAmelCase     =  (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
						UpperCAmelCase     =  (SpeechaTextaForCausalLM,) if is_torch_available() else ()
						UpperCAmelCase     =  {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
						UpperCAmelCase     =  True
						UpperCAmelCase     =  False




						def 							__UpperCamelCase			(    self   : Optional[int]			,      )							->       int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=SpeechaTextaStandaloneDecoderModelTester(self			,      is_training=_a					)
										_SCREAMING_SNAKE_CASE     					=ConfigTester(self			,      config_class=_a					)




						def 							__UpperCamelCase			(    self   : List[Any]					)							->       Optional[Any]:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase			(    self   : Optional[Any]					)							->       Optional[int]:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase			(    self   : Dict					)							->       Tuple:


										"""simple docstring"""





										pass




						def 							__UpperCamelCase			(    self   : str					)							->       Optional[int]:


										"""simple docstring"""





										self.config_tester.run_common_tests()




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE     					=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_decoder_model_past(*_a					)




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


										"""simple docstring"""





										return



from ..utils import DummyObject, requires_backends

class   A__							(						metaclass=UpperCamelCase__					):
						UpperCAmelCase     =  ["torch", "librosa"]




						def __init__(    self   : Dict			,      *_a   : Union[str, Any]			,      **_a   : List[str]					)							->       Optional[int]:


										"""simple docstring"""





										requires_backends(self			,      ['''torch''', '''librosa''']					)




						@classmethod
						def 							__UpperCamelCase			(    cls   : Dict			,      *_a   : Tuple			,      **_a   : Tuple					)							->       Optional[int]:


										"""simple docstring"""





										requires_backends(cls			,      ['''torch''', '''librosa''']					)




						@classmethod
						def 							__UpperCamelCase			(    cls   : List[Any]			,      *_a   : Any			,      **_a   : List[str]					)							->       Dict:


										"""simple docstring"""





										requires_backends(cls			,      ['''torch''', '''librosa''']					)

class   A__							(						metaclass=UpperCamelCase__					):
						UpperCAmelCase     =  ["torch", "librosa"]




						def __init__(    self   : List[Any]			,      *_a   : int			,      **_a   : int					)							->       Optional[Any]:


										"""simple docstring"""





										requires_backends(self			,      ['''torch''', '''librosa''']					)




						@classmethod
						def 							__UpperCamelCase			(    cls   : str			,      *_a   : Dict			,      **_a   : Optional[Any]					)							->       Union[str, Any]:


										"""simple docstring"""





										requires_backends(cls			,      ['''torch''', '''librosa''']					)




						@classmethod
						def 							__UpperCamelCase			(    cls   : List[str]			,      *_a   : Optional[int]			,      **_a   : Union[str, Any]					)							->       Optional[Any]:


										"""simple docstring"""





										requires_backends(cls			,      ['''torch''', '''librosa''']					)