

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







						def __init__(self  ,				UpperCAmelCase_  ,				UpperCAmelCase_=9_9  ,				UpperCAmelCase_=1_3  ,				UpperCAmelCase_=1_6  ,				UpperCAmelCase_=7  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=False  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=2  ,				UpperCAmelCase_=3_2  ,				UpperCAmelCase_=4  ,				UpperCAmelCase_=4  ,				UpperCAmelCase_=3_0  ,				UpperCAmelCase_=0  ,				UpperCAmelCase_=1  ,				UpperCAmelCase_=2  ,				UpperCAmelCase_=None  ,				)      ->    Any:
											__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				)      ->    Union[str, Any]:
											__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  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				)      ->    Tuple:
											__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				)      ->    Union[str, Any]:
											__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       _snake_case  (    lowerCAmelCase__    ,      lowerCAmelCase__    ,      lowerCAmelCase__    ,      unittest.TestCase							):
						__snake_case			      =  (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
						__snake_case			      =  (SpeechaTextaForCausalLM,) if is_torch_available() else ()
						__snake_case			      =  {'text-generation': SpeechaTextaForCausalLM} if is_torch_available() else {}
						__snake_case			      =  True
						__snake_case			      =  False







						def 							_UpperCamelCase     (self  ,				)      ->    Union[str, Any]:
											__lowercase        =      SpeechaTextaStandaloneDecoderModelTester(self  ,				is_training=_lowercase				)
											__lowercase        =      ConfigTester(self  ,				config_class=_lowercase				)







						def 							_UpperCamelCase     (self				)      ->    int:
											pass







						def 							_UpperCamelCase     (self				)      ->    int:
											pass







						def 							_UpperCamelCase     (self				)      ->    Tuple:
											pass







						def 							_UpperCamelCase     (self				)      ->    List[Any]:
											self.config_tester.run_common_tests()







						def 							_UpperCamelCase     (self				)      ->    List[str]:
											__lowercase        =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_decoder_model_past(*_lowercase				)







						def 							_UpperCamelCase     (self				)      ->    Tuple:
											return








from ..utils import DummyObject, requires_backends

class 		lowercase_       (	metaclass=lowerCAmelCase__				):
					__UpperCamelCase     	= ["torch", "transformers", "k_diffusion"]




					def __init__(   self:		Tuple,     *_lowercase:		Dict,     **_lowercase:		Optional[int]):


									'''simple docstring'''


									requires_backends(self,     ["""torch""", """transformers""", """k_diffusion"""])




					@classmethod
					def     _lowercase						(   cls:		Union[str, Any],     *_lowercase:		Union[str, Any],     **_lowercase:		Optional[int]):


									'''simple docstring'''


									requires_backends(cls,     ["""torch""", """transformers""", """k_diffusion"""])







					@classmethod
					def     _lowercase						(   cls:		List[str],     *_lowercase:		int,     **_lowercase:		List[Any]):


									'''simple docstring'''


									requires_backends(cls,     ["""torch""", """transformers""", """k_diffusion"""])





