



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






						'''simple docstring'''







						def __init__(						self   :      Any						,						__lowerCamelCase   :      Optional[int]						,						__lowerCamelCase   :      Any=99						,						__lowerCamelCase   :      str=13						,						__lowerCamelCase   :      Dict=16						,						__lowerCamelCase   :      Tuple=7						,						__lowerCamelCase   :      List[Any]=True						,						__lowerCamelCase   :      Any=True						,						__lowerCamelCase   :      Tuple=True						,						__lowerCamelCase   :      str=False						,						__lowerCamelCase   :      str=True						,						__lowerCamelCase   :      int=2						,						__lowerCamelCase   :      Optional[Any]=32						,						__lowerCamelCase   :      Optional[Any]=4						,						__lowerCamelCase   :      Union[str, Any]=4						,						__lowerCamelCase   :      Optional[int]=30						,						__lowerCamelCase   :      Optional[int]=0						,						__lowerCamelCase   :      Dict=1						,						__lowerCamelCase   :      str=2						,						__lowerCamelCase   :      Union[str, Any]=None						,						)   ->					str:
									A				:       Any														=    parent
									A				:       Optional[Any]														=    batch_size
									A				:       Union[str, Any]														=    decoder_seq_length
									# For common tests
									A				:       int														=    self.decoder_seq_length
									A				:       str														=    is_training
									A				:       int														=    use_attention_mask
									A				:       List[Any]														=    use_labels

									A				:       Any														=    vocab_size
									A				:       Dict														=    d_model
									A				:       Any														=    d_model
									A				:       List[str]														=    decoder_layers
									A				:       int														=    decoder_layers
									A				:       int														=    decoder_ffn_dim
									A				:       Any														=    decoder_attention_heads
									A				:       Any														=    decoder_attention_heads
									A				:       Dict														=    eos_token_id
									A				:       int														=    bos_token_id
									A				:       List[Any]														=    pad_token_id
									A				:       Dict														=    decoder_start_token_id
									A				:       Optional[int]														=    use_cache
									A				:       List[str]														=    max_position_embeddings

									A				:       Optional[int]														=    None
									A				:       Tuple														=    decoder_seq_length
									A				:       Optional[int]														=    2
									A				:       List[str]														=    1







						def       SCREAMING_SNAKE_CASE__		(						self   :      Optional[int]			)   ->					str:
									A				:       Optional[int]														=    ids_tensor([self.batch_size, self.decoder_seq_length]						,						self.vocab_size			)

									A				:       List[str]														=    None
									if self.use_attention_mask:
												A				:       Optional[int]														=    ids_tensor([self.batch_size, self.decoder_seq_length]						,						vocab_size=2			)

									A				:       Union[str, Any]														=    None
									if self.use_labels:
												A				:       List[Any]														=    ids_tensor([self.batch_size, self.decoder_seq_length]						,						self.vocab_size			)

									A				:       Union[str, Any]														=    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       SCREAMING_SNAKE_CASE__		(						self   :      str						,						__lowerCamelCase   :      Any						,						__lowerCamelCase   :      List[Any]						,						__lowerCamelCase   :      int						,						__lowerCamelCase   :      Dict						,						)   ->					List[str]:
									A				:       Union[str, Any]														=    True
									A				:       Dict														=    SpeechaTextaDecoder(config=_UpperCAmelCase			).to(_UpperCAmelCase			).eval()
									A				:       Tuple														=    input_ids[:2]

									input_ids[input_ids == 0] += 1
									# first forward pass
									A				:       Union[str, Any]														=    model(_UpperCAmelCase						,						use_cache=_UpperCAmelCase			)
									A				:       int														=    model(_UpperCAmelCase			)
									A				:       List[Any]														=    model(_UpperCAmelCase						,						use_cache=_UpperCAmelCase			)

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

									A				:       Optional[Any]														=    outputs["past_key_values"]

									# create hypothetical next token and extent to next_input_ids
									A				:       Union[str, Any]														=    ids_tensor((2, 1)						,						config.vocab_size - 1			) + 1

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

									print(_UpperCAmelCase			)

									A				:       List[str]														=    model(_UpperCAmelCase			)["last_hidden_state"]
									A				:       str														=    model(_UpperCAmelCase						,						past_key_values=_UpperCAmelCase			)["last_hidden_state"]

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

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







						def       SCREAMING_SNAKE_CASE__		(						self   :      Union[str, Any]			)   ->					Any:
									A				:       Any														=    self.prepare_config_and_inputs()
									(
									    (
									    A       
									)							,			(
									    A       
									)							,			(
									    A       
									)							,			(
									    A       
									)							,			       
									)				:       Tuple														=    config_and_inputs

									A				:       Dict														=    {
									    "input_ids": input_ids,
									    "attention_mask": attention_mask,
									}
									return config, inputs_dict







@require_torch
class 						lowerCamelCase_     (			lowerCamelCase__    ,lowerCamelCase__    ,lowerCamelCase__    ,unittest.TestCase    ):






						'''simple docstring'''
						a__ =	(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
						a__ =	(SpeechaTextaForCausalLM,) if is_torch_available() else ()
						a__ =	{"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
						a__ =	True
						a__ =	False







						def       SCREAMING_SNAKE_CASE__		(						self   :      int						,						)   ->					int:
									A				:       int														=    SpeechaTextaStandaloneDecoderModelTester(self						,						is_training=_UpperCAmelCase			)
									A				:       Optional[Any]														=    ConfigTester(self						,						config_class=_UpperCAmelCase			)







						def       SCREAMING_SNAKE_CASE__		(						self   :      Any			)   ->					List[Any]:
									pass







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







						def       SCREAMING_SNAKE_CASE__		(						self   :      int			)   ->					Optional[int]:
									pass







						def       SCREAMING_SNAKE_CASE__		(						self   :      Union[str, Any]			)   ->					Optional[Any]:
									self.config_tester.run_common_tests()







						def       SCREAMING_SNAKE_CASE__		(						self   :      Optional[Any]			)   ->					List[Any]:
									A				:       List[str]														=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_decoder_model_past(*_UpperCAmelCase			)







						def       SCREAMING_SNAKE_CASE__		(						self   :      List[Any]			)   ->					Any:
									return



from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available






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




      """simple docstring"""

      if is_torch_available():
            import torch

            if isinstance(SCREAMING_SNAKE_CASE   ,   torch.Tensor   ):
                  return True

      return isinstance(SCREAMING_SNAKE_CASE   ,   np.ndarray   )







class lowerCAmelCase_		(					lowerCamelCase__			):

 '''simple docstring'''

 def 		UpperCamelCase__	(      self   ):
       snake_case_			= fields(self   )

       # Safety and consistency checks
       if not len(_UpperCAmelCase   ):
             raise ValueError(F'''{self.__class__.__name__} has no fields.'''   )

       snake_case_			= getattr(self						, class_fields[0].name   )
       snake_case_			= all(getattr(self						, field.name   ) is None for field in class_fields[1:]   )

       if other_fields_are_none and isinstance(_UpperCAmelCase						, _UpperCAmelCase   ):
             for key, value in first_field.items():
                   snake_case_			= value
       else:
             for field in class_fields:
                   snake_case_			= getattr(self						, field.name   )
                   if v is not None:
                         snake_case_			= v



 def __delitem__(      self						, *_UpperCAmelCase						, **_UpperCAmelCase   ):
       raise Exception(F'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.'''   )



 def 		UpperCamelCase__	(      self						, *_UpperCAmelCase						, **_UpperCAmelCase   ):
       raise Exception(F'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.'''   )



 def 		UpperCamelCase__	(      self						, *_UpperCAmelCase						, **_UpperCAmelCase   ):
       raise Exception(F'''You cannot use ``pop`` on a {self.__class__.__name__} instance.'''   )



 def 		UpperCamelCase__	(      self						, *_UpperCAmelCase						, **_UpperCAmelCase   ):
       raise Exception(F'''You cannot use ``update`` on a {self.__class__.__name__} instance.'''   )



 def __getitem__(      self						, _UpperCAmelCase   ):
       if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ):
             snake_case_			= dict(self.items()   )
             return inner_dict[k]
       else:
             return self.to_tuple()[k]



 def __setattr__(      self						, _UpperCAmelCase						, _UpperCAmelCase   ):
       if name in self.keys() and value is not None:
             # Don't call self.__setitem__ to avoid recursion errors
             super().__setitem__(_UpperCAmelCase						, _UpperCAmelCase   )
       super().__setattr__(_UpperCAmelCase						, _UpperCAmelCase   )



 def __setitem__(      self						, _UpperCAmelCase						, _UpperCAmelCase   ):
       # Will raise a KeyException if needed
       super().__setitem__(_UpperCAmelCase						, _UpperCAmelCase   )
       # Don't call self.__setattr__ to avoid recursion errors
       super().__setattr__(_UpperCAmelCase						, _UpperCAmelCase   )





 def 		UpperCamelCase__	(      self   ):
       return tuple(self[k] for k in self.keys()   )