
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	:							List[Any]							,    SCREAMING_SNAKE_CASE__	:							Dict							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]=99							,    SCREAMING_SNAKE_CASE__	:							Union[str, Any]=13							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]=16							,    SCREAMING_SNAKE_CASE__	:							int=7							,    SCREAMING_SNAKE_CASE__	:							str=True							,    SCREAMING_SNAKE_CASE__	:							str=True							,    SCREAMING_SNAKE_CASE__	:							str=True							,    SCREAMING_SNAKE_CASE__	:							Any=False							,    SCREAMING_SNAKE_CASE__	:							Any=True							,    SCREAMING_SNAKE_CASE__	:							str=2							,    SCREAMING_SNAKE_CASE__	:							Tuple=32							,    SCREAMING_SNAKE_CASE__	:							Optional[int]=4							,    SCREAMING_SNAKE_CASE__	:							List[Any]=4							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]=30							,    SCREAMING_SNAKE_CASE__	:							Optional[int]=0							,    SCREAMING_SNAKE_CASE__	:							Optional[int]=1							,    SCREAMING_SNAKE_CASE__	:							int=2							,    SCREAMING_SNAKE_CASE__	:							List[str]=None							,    )  ->							Optional[int]:
      __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	:							Dict						)  ->							Optional[Any]:
      __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	:							List[str]							,    SCREAMING_SNAKE_CASE__	:							Optional[int]							,    SCREAMING_SNAKE_CASE__	:							Optional[Any]							,    SCREAMING_SNAKE_CASE__	:							Optional[int]							,    SCREAMING_SNAKE_CASE__	:							Tuple							,    )  ->							Dict:
      __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	:							Any						)  ->							Union[str, Any]:
      __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      lowerCAmelCase__  (			__lowercase						,			__lowercase						,			__lowercase						,			unittest.TestCase    ):
  a__ :		List[Any]   						=	(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
  a__ :		Optional[int]   						=	(SpeechaTextaForCausalLM,) if is_torch_available() else ()
  a__ :		Union[str, Any]   						=	{"""text-generation""": SpeechaTextaForCausalLM} if is_torch_available() else {}
  a__ :		Any   						=	True
  a__ :		List[str]   						=	False






  def 				__A (	self	:							Any							,    )  ->							Tuple:
      __lowerCamelCase					      =			SpeechaTextaStandaloneDecoderModelTester(self							,    is_training=SCREAMING_SNAKE_CASE__						)
      __lowerCamelCase					      =			ConfigTester(self							,    config_class=SCREAMING_SNAKE_CASE__						)






  def 				__A (	self	:							Optional[Any]						)  ->							Any:
      pass






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






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






  def 				__A (	self	:							Optional[int]						)  ->							Optional[int]:
      self.config_tester.run_common_tests()






  def 				__A (	self	:							Union[str, Any]						)  ->							Dict:
      __lowerCamelCase					      =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past(*SCREAMING_SNAKE_CASE__						)







  def 				__A (	self	:							Any						)  ->							str:
      return





from ..utils import deprecate
from .attention_processor import (  # noqa: F401
    Attention,
    AttentionProcessor,
    AttnAddedKVProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRALinearLayer,
    LoRAXFormersAttnProcessor,
    SlicedAttnAddedKVProcessor,
    SlicedAttnProcessor,
    XFormersAttnProcessor,
)
from .attention_processor import AttnProcessor as AttnProcessorRename  # noqa: F401


deprecate(
    "cross_attention",
    "0.20.0",
    "Importing from cross_attention is deprecated. Please import from diffusers.models.attention_processor instead.",
    standard_warn=False,
)


SCREAMING_SNAKE_CASE__     :    Optional[int]      							=					AttentionProcessor

class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Tuple							,    *SCREAMING_SNAKE_CASE__	:							Dict							,    **SCREAMING_SNAKE_CASE__	:							int						)  ->							List[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							Optional[Any]							,    **SCREAMING_SNAKE_CASE__	:							Optional[int]						)  ->							Optional[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							List[Any]							,    **SCREAMING_SNAKE_CASE__	:							List[str]						)  ->							List[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[int]							,    *SCREAMING_SNAKE_CASE__	:							Optional[int]							,    **SCREAMING_SNAKE_CASE__	:							Tuple						)  ->							Optional[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[Any]							,    *SCREAMING_SNAKE_CASE__	:							int							,    **SCREAMING_SNAKE_CASE__	:							Optional[Any]						)  ->							List[str]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Tuple							,    *SCREAMING_SNAKE_CASE__	:							Tuple							,    **SCREAMING_SNAKE_CASE__	:							Any						)  ->							Union[str, Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							Union[str, Any]							,    **SCREAMING_SNAKE_CASE__	:							Tuple						)  ->							int:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[Any]							,    *SCREAMING_SNAKE_CASE__	:							Any							,    **SCREAMING_SNAKE_CASE__	:							str						)  ->							Dict:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)



