





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							:







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






     '''simple docstring'''





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

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

     lowercase__    =  None
     lowercase__    =  decoder_seq_length
     lowercase__    =  2
     lowercase__    =  1







  def   UpperCAmelCase     (			self  :List[Any]					):






     '''simple docstring'''





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

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

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

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






     '''simple docstring'''





     lowercase__    =  True
     lowercase__    =  SpeechaTextaDecoder(config=_lowercase					).to(_lowercase					).eval()
     lowercase__    =  input_ids[:2]

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

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

     lowercase__    =  outputs["past_key_values"]

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

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

     print(_lowercase					)

     lowercase__    =  model(_lowercase					)["last_hidden_state"]
     lowercase__    =  model(_lowercase    ,							past_key_values=_lowercase					)["last_hidden_state"]

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

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







  def   UpperCAmelCase     (			self  :List[str]					):






     '''simple docstring'''





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

     lowercase__    =  {
         "input_ids": input_ids,
         "attention_mask": attention_mask,
     }
     return config, inputs_dict





@require_torch
class 					lowerCAmelCase							(   lowercase_							,							lowercase_							,							lowercase_							,							unittest.TestCase					):
  __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   UpperCAmelCase     (			self  :Dict    ,							):






     '''simple docstring'''





     lowercase__    =  SpeechaTextaStandaloneDecoderModelTester(self    ,							is_training=_lowercase					)
     lowercase__    =  ConfigTester(self    ,							config_class=_lowercase					)







  def   UpperCAmelCase     (			self  :Union[str, Any]					):






     '''simple docstring'''





     pass







  def   UpperCAmelCase     (			self  :int					):






     '''simple docstring'''





     pass







  def   UpperCAmelCase     (			self  :List[Any]					):






     '''simple docstring'''





     pass







  def   UpperCAmelCase     (			self  :Tuple					):






     '''simple docstring'''





     self.config_tester.run_common_tests()







  def   UpperCAmelCase     (			self  :Optional[int]					):






     '''simple docstring'''





     lowercase__    =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past(*_lowercase					)







  def   UpperCAmelCase     (			self  :Union[str, Any]					):






     '''simple docstring'''





     return













from typing import TYPE_CHECKING

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


_snake_case				= {"""configuration_regnet""": ["""REGNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RegNetConfig"""]}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _snake_case				= [
      """REGNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """RegNetForImageClassification""",
      """RegNetModel""",
      """RegNetPreTrainedModel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _snake_case				= [
      """TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFRegNetForImageClassification""",
      """TFRegNetModel""",
      """TFRegNetPreTrainedModel""",
  ]

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _snake_case				= [
      """FlaxRegNetForImageClassification""",
      """FlaxRegNetModel""",
      """FlaxRegNetPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_regnet import REGNET_PRETRAINED_CONFIG_ARCHIVE_MAP, RegNetConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_regnet import (
        REGNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        RegNetForImageClassification,
        RegNetModel,
        RegNetPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_regnet import (
        TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFRegNetForImageClassification,
        TFRegNetModel,
        TFRegNetPreTrainedModel,
    )

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_regnet import (
        FlaxRegNetForImageClassification,
        FlaxRegNetModel,
        FlaxRegNetPreTrainedModel,
    )


else:
  import sys

  _snake_case				= _LazyModule(__name__, globals()["""__file__"""], _import_structure)






