






'''simple docstring'''





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  :  Union[str, Any]					,  __lowerCamelCase  :  int					,  __lowerCamelCase  :  Optional[Any]=99					,  __lowerCamelCase  :  Optional[int]=13					,  __lowerCamelCase  :  Optional[int]=16					,  __lowerCamelCase  :  str=7					,  __lowerCamelCase  :  Tuple=True					,  __lowerCamelCase  :  Dict=True					,  __lowerCamelCase  :  List[Any]=True					,  __lowerCamelCase  :  int=False					,  __lowerCamelCase  :  Any=True					,  __lowerCamelCase  :  str=2					,  __lowerCamelCase  :  List[str]=32					,  __lowerCamelCase  :  int=4					,  __lowerCamelCase  :  Union[str, Any]=4					,  __lowerCamelCase  :  Optional[Any]=30					,  __lowerCamelCase  :  int=0					,  __lowerCamelCase  :  Dict=1					,  __lowerCamelCase  :  Union[str, Any]=2					,  __lowerCamelCase  :  Dict=None					,  )				->				Optional[int]:

     '''simple docstring'''




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

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

     lowerCamelCase__	=  None
     lowerCamelCase__	=  decoder_seq_length
     lowerCamelCase__	=  2
     lowerCamelCase__	=  1

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

     '''simple docstring'''




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

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

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

     lowerCamelCase__	=  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  a__					(       self  :  int					,  __lowerCamelCase  :  Dict					,  __lowerCamelCase  :  Tuple					,  __lowerCamelCase  :  Optional[Any]					,  __lowerCamelCase  :  Dict					,  )				->				Any:

     '''simple docstring'''




     lowerCamelCase__	=  True
     lowerCamelCase__	=  SpeechaTextaDecoder(config=__lowerCamelCase  ).to(__lowerCamelCase  ).eval()
     lowerCamelCase__	=  input_ids[:2]

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

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

     lowerCamelCase__	=  outputs["past_key_values"]

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

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

     print(__lowerCamelCase  )

     lowerCamelCase__	=  model(__lowerCamelCase  )["last_hidden_state"]
     lowerCamelCase__	=  model(__lowerCamelCase					,  past_key_values=__lowerCamelCase  )["last_hidden_state"]

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

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




    def  a__					(       self  :  Optional[int]  )				->				Optional[int]:

     '''simple docstring'''




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

     lowerCamelCase__	=  {
         "input_ids": input_ids,
         "attention_mask": attention_mask,
     }
     return config, inputs_dict
@require_torch
class 					lowercase							(				_lowerCamelCase			,							_lowerCamelCase			,							_lowerCamelCase			,							unittest.TestCase   ):


    '''simple docstring'''

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

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

     '''simple docstring'''




     lowerCamelCase__	=  SpeechaTextaStandaloneDecoderModelTester(self					,  is_training=__lowerCamelCase  )
     lowerCamelCase__	=  ConfigTester(self					,  config_class=__lowerCamelCase  )

    def  a__					(       self  :  int  )				->				Any:

     '''simple docstring'''




     pass

    def  a__					(       self  :  int  )				->				Dict:

     '''simple docstring'''




     pass

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

     '''simple docstring'''




     pass

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

     '''simple docstring'''




     self.config_tester.run_common_tests()

    def  a__					(       self  :  List[str]  )				->				str:

     '''simple docstring'''




     lowerCamelCase__	=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past(*__lowerCamelCase  )




    def  a__					(       self  :  Dict  )				->				List[str]:

     '''simple docstring'''




     return









'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_keras_nlp_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__A				:				Union[str, Any]					=		{
    """configuration_gpt2""": ["""GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPT2Config""", """GPT2OnnxConfig"""],
    """tokenization_gpt2""": ["""GPT2Tokenizer"""],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A				:				Tuple					=		["""GPT2TokenizerFast"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A				:				Optional[int]					=		[
     """GPT2_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """GPT2DoubleHeadsModel""",
     """GPT2ForQuestionAnswering""",
     """GPT2ForSequenceClassification""",
     """GPT2ForTokenClassification""",
     """GPT2LMHeadModel""",
     """GPT2Model""",
     """GPT2PreTrainedModel""",
     """load_tf_weights_in_gpt2""",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A				:				Optional[Any]					=		[
     """TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """TFGPT2DoubleHeadsModel""",
     """TFGPT2ForSequenceClassification""",
     """TFGPT2LMHeadModel""",
     """TFGPT2MainLayer""",
     """TFGPT2Model""",
     """TFGPT2PreTrainedModel""",
 ]

try:
 if not is_keras_nlp_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A				:				Any					=		["""TFGPT2Tokenizer"""]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A				:				Tuple					=		["""FlaxGPT2LMHeadModel""", """FlaxGPT2Model""", """FlaxGPT2PreTrainedModel"""]

if TYPE_CHECKING:
 from .configuration_gpta import GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTaConfig, GPTaOnnxConfig
 from .tokenization_gpta import GPTaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_gpta_fast import GPTaTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_gpta import (
      GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
      GPTaDoubleHeadsModel,
      GPTaForQuestionAnswering,
      GPTaForSequenceClassification,
      GPTaForTokenClassification,
      GPTaLMHeadModel,
      GPTaModel,
      GPTaPreTrainedModel,
      load_tf_weights_in_gpta,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_gpta import (
      TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFGPTaDoubleHeadsModel,
      TFGPTaForSequenceClassification,
      TFGPTaLMHeadModel,
      TFGPTaMainLayer,
      TFGPTaModel,
      TFGPTaPreTrainedModel,
  )

 try:
  if not is_keras_nlp_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_gpta_tf import TFGPTaTokenizer

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel, FlaxGPTaPreTrainedModel

else:
 import sys

 __A				:				Tuple					=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

