"""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      _SCREAMING_SNAKE_CASE							:



   '''simple docstring'''



   def __init__(	self ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE=99 ,				_SCREAMING_SNAKE_CASE=13 ,				_SCREAMING_SNAKE_CASE=16 ,				_SCREAMING_SNAKE_CASE=7 ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=False ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=2 ,				_SCREAMING_SNAKE_CASE=32 ,				_SCREAMING_SNAKE_CASE=4 ,				_SCREAMING_SNAKE_CASE=4 ,				_SCREAMING_SNAKE_CASE=30 ,				_SCREAMING_SNAKE_CASE=0 ,				_SCREAMING_SNAKE_CASE=1 ,				_SCREAMING_SNAKE_CASE=2 ,				_SCREAMING_SNAKE_CASE=None ,				):


      '''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	):


      '''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 ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				):


      '''simple docstring'''

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

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

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

      __lowerCamelCase    		=					model(_SCREAMING_SNAKE_CASE	)['last_hidden_state']
      __lowerCamelCase    		=					model(_SCREAMING_SNAKE_CASE ,				past_key_values=_SCREAMING_SNAKE_CASE	)['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(_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				atol=1E-3	)







   def    _A				(	self	):


      '''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      _SCREAMING_SNAKE_CASE							(			UpperCAmelCase    ,				UpperCAmelCase    ,				UpperCAmelCase    ,				unittest.TestCase				):



   '''simple docstring'''



   __UpperCAmelCase	:							Union[str, Any]    =    (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
   __UpperCAmelCase	:							Any    =    (SpeechaTextaForCausalLM,) if is_torch_available() else ()
   __UpperCAmelCase	:							Union[str, Any]    =    {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
   __UpperCAmelCase	:							int    =    True
   __UpperCAmelCase	:							Union[str, Any]    =    False







   def    _A				(	self ,				):


      '''simple docstring'''

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







   def    _A				(	self	):


      '''simple docstring'''

      pass







   def    _A				(	self	):


      '''simple docstring'''

      pass







   def    _A				(	self	):


      '''simple docstring'''

      pass







   def    _A				(	self	):


      '''simple docstring'''

      self.config_tester.run_common_tests()







   def    _A				(	self	):


      '''simple docstring'''

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







   def    _A				(	self	):


      '''simple docstring'''

      return







"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE	:		str   =   logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE	:		Any   =   {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}






class      _SCREAMING_SNAKE_CASE							(			UpperCAmelCase				):



   '''simple docstring'''



   __UpperCAmelCase	:							Tuple    =    "opt"
   __UpperCAmelCase	:							Any    =    ["past_key_values"]







   def __init__(	self ,				_SCREAMING_SNAKE_CASE=50272 ,				_SCREAMING_SNAKE_CASE=768 ,				_SCREAMING_SNAKE_CASE=12 ,				_SCREAMING_SNAKE_CASE=3072 ,				_SCREAMING_SNAKE_CASE=2048 ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=False ,				_SCREAMING_SNAKE_CASE=None ,				_SCREAMING_SNAKE_CASE=0.1 ,				_SCREAMING_SNAKE_CASE=0.0 ,				_SCREAMING_SNAKE_CASE=12 ,				_SCREAMING_SNAKE_CASE="relu" ,				_SCREAMING_SNAKE_CASE=0.0 ,				_SCREAMING_SNAKE_CASE=0.02 ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=1 ,				_SCREAMING_SNAKE_CASE=2 ,				_SCREAMING_SNAKE_CASE=2 ,				_SCREAMING_SNAKE_CASE=True ,				_SCREAMING_SNAKE_CASE=True ,				**_SCREAMING_SNAKE_CASE ,				):


      '''simple docstring'''

      super().__init__(
          pad_token_id=_SCREAMING_SNAKE_CASE ,				bos_token_id=_SCREAMING_SNAKE_CASE ,				eos_token_id=_SCREAMING_SNAKE_CASE ,				**_SCREAMING_SNAKE_CASE ,				)
      __lowerCamelCase    		=					vocab_size
      __lowerCamelCase    		=					max_position_embeddings
      __lowerCamelCase    		=					num_attention_heads
      __lowerCamelCase    		=					word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
      __lowerCamelCase    		=					ffn_dim
      __lowerCamelCase    		=					hidden_size
      __lowerCamelCase    		=					num_hidden_layers
      __lowerCamelCase    		=					dropout
      __lowerCamelCase    		=					attention_dropout
      __lowerCamelCase    		=					activation_function
      __lowerCamelCase    		=					init_std
      __lowerCamelCase    		=					layerdrop
      __lowerCamelCase    		=					use_cache
      __lowerCamelCase    		=					do_layer_norm_before
      # We keep these variables at `True` for backward compatibility.
      __lowerCamelCase    		=					enable_bias
      __lowerCamelCase    		=					layer_norm_elementwise_affine

      # Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
      # with checkpoints that have been fine-tuned before transformers v4.20.1
      # see https://github.com/facebookresearch/metaseq/pull/164
      __lowerCamelCase    		=					_remove_final_layer_norm






