


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					:	Optional[Any]  ,			_lowerCAmelCase					:	Union[str, Any]  ,			_lowerCAmelCase					:	Optional[int]=9_9  ,			_lowerCAmelCase					:	List[str]=1_3  ,			_lowerCAmelCase					:	Dict=1_6  ,			_lowerCAmelCase					:	int=7  ,			_lowerCAmelCase					:	Tuple=True  ,			_lowerCAmelCase					:	Union[str, Any]=True  ,			_lowerCAmelCase					:	Union[str, Any]=True  ,			_lowerCAmelCase					:	Optional[int]=False  ,			_lowerCAmelCase					:	List[str]=True  ,			_lowerCAmelCase					:	Optional[Any]=2  ,			_lowerCAmelCase					:	Optional[Any]=3_2  ,			_lowerCAmelCase					:	Any=4  ,			_lowerCAmelCase					:	List[str]=4  ,			_lowerCAmelCase					:	Optional[Any]=3_0  ,			_lowerCAmelCase					:	int=0  ,			_lowerCAmelCase					:	List[Any]=1  ,			_lowerCAmelCase					:	Optional[int]=2  ,			_lowerCAmelCase					:	Tuple=None  ,			)       ->				Dict:



											'''simple docstring'''





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

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

											__magic_name__ 		=			None
											__magic_name__ 		=			decoder_seq_length
											__magic_name__ 		=			2
											__magic_name__ 		=			1
					def   a (	self					:	int     )       ->				int:



											'''simple docstring'''





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

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

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

											__magic_name__ 		=			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]  ,			_lowerCAmelCase					:	Optional[Any]  ,			_lowerCAmelCase					:	List[Any]  ,			_lowerCAmelCase					:	int  ,			_lowerCAmelCase					:	Optional[int]  ,			)       ->				Optional[Any]:



											'''simple docstring'''





											__magic_name__ 		=			True
											__magic_name__ 		=			SpeechaTextaDecoder(config=_lowerCAmelCase     ).to(_lowerCAmelCase     ).eval()
											__magic_name__ 		=			input_ids[:2]

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

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

											__magic_name__ 		=			outputs['past_key_values']

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

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

											print(_lowerCAmelCase     )

											__magic_name__ 		=			model(_lowerCAmelCase     )['last_hidden_state']
											__magic_name__ 		=			model(_lowerCAmelCase  ,			past_key_values=_lowerCAmelCase     )['last_hidden_state']

											# select random slice
											__magic_name__ 		=			ids_tensor((1,)  ,			output_from_past.shape[-1]     ).item()
											__magic_name__ 		=			output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
											__magic_name__ 		=			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					:	List[Any]     )       ->				str:



											'''simple docstring'''





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

											__magic_name__ 		=			{
											    'input_ids': input_ids,
											    'attention_mask': attention_mask,
											}
											return config, inputs_dict







@require_torch
class     __snake_case							(      _UpperCAmelCase  ,			_UpperCAmelCase  ,			_UpperCAmelCase  ,			unittest.TestCase):
					SCREAMING_SNAKE_CASE_		:					Any 				= (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE_		:					List[str] 				= (SpeechaTextaForCausalLM,) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE_		:					int 				= {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
					SCREAMING_SNAKE_CASE_		:					Tuple 				= True
					SCREAMING_SNAKE_CASE_		:					int 				= False
					def   a (	self					:	Optional[Any]  ,			)       ->				List[str]:



											'''simple docstring'''





											__magic_name__ 		=			SpeechaTextaStandaloneDecoderModelTester(self  ,			is_training=_lowerCAmelCase     )
											__magic_name__ 		=			ConfigTester(self  ,			config_class=_lowerCAmelCase     )
					def   a (	self					:	Optional[int]     )       ->				List[str]:



											'''simple docstring'''





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



											'''simple docstring'''





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



											'''simple docstring'''





											pass
					def   a (	self					:	Union[str, Any]     )       ->				Tuple:



											'''simple docstring'''





											self.config_tester.run_common_tests()
					def   a (	self					:	List[Any]     )       ->				Union[str, Any]:



											'''simple docstring'''





											__magic_name__ 		=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_decoder_model_past(*_lowerCAmelCase     )







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



											'''simple docstring'''





											return





import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding

class     __snake_case							(      _UpperCAmelCase):
					SCREAMING_SNAKE_CASE_		:					Union[str, Any] 				= ["image_processor", "tokenizer"]
					SCREAMING_SNAKE_CASE_		:					Tuple 				= "AutoImageProcessor"
					SCREAMING_SNAKE_CASE_		:					Dict 				= "AutoTokenizer"
					def __init__(	self					:	List[str]  ,			_lowerCAmelCase					:	str=None  ,			_lowerCAmelCase					:	Dict=None  ,			**_lowerCAmelCase					:	List[str]     )       ->				Optional[int]:



											'''simple docstring'''





											__magic_name__ 		=			None
											if "feature_extractor" in kwargs:
																	warnings.warn(
																	    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
																	    ' instead.'  ,			_lowerCAmelCase  ,			)
																	__magic_name__ 		=			kwargs.pop('feature_extractor'     )

											__magic_name__ 		=			image_processor if image_processor is not None else feature_extractor
											if image_processor is None:
																	raise ValueError('You have to specify an image_processor.'     )
											if tokenizer is None:
																	raise ValueError('You have to specify a tokenizer.'     )

											super().__init__(_lowerCAmelCase  ,			_lowerCAmelCase     )
											__magic_name__ 		=			self.image_processor
					def __call__(	self					:	str  ,			_lowerCAmelCase					:	Any=None  ,			_lowerCAmelCase					:	int=None  ,			_lowerCAmelCase					:	List[str]=None  ,			**_lowerCAmelCase					:	int     )       ->				int:



											'''simple docstring'''





											if text is None and images is None:
																	raise ValueError('You have to specify either text or images. Both cannot be none.'     )

											if text is not None:
																	__magic_name__ 		=			self.tokenizer(_lowerCAmelCase  ,			return_tensors=_lowerCAmelCase  ,			**_lowerCAmelCase     )

											if images is not None:
																	__magic_name__ 		=			self.image_processor(_lowerCAmelCase  ,			return_tensors=_lowerCAmelCase  ,			**_lowerCAmelCase     )

											if text is not None and images is not None:
																	__magic_name__ 		=			image_features.pixel_values
																	return encoding
											elif text is not None:
																	return encoding
											else:
																	return BatchEncoding(data=dict(**_lowerCAmelCase     )  ,			tensor_type=_lowerCAmelCase     )
					def   a (	self					:	Optional[int]  ,			*_lowerCAmelCase					:	Optional[int]  ,			**_lowerCAmelCase					:	Optional[Any]     )       ->				Optional[int]:



											'''simple docstring'''





											return self.tokenizer.batch_decode(*_lowerCAmelCase  ,			**_lowerCAmelCase     )
					def   a (	self					:	Union[str, Any]  ,			*_lowerCAmelCase					:	str  ,			**_lowerCAmelCase					:	Tuple     )       ->				int:



											'''simple docstring'''





											return self.tokenizer.decode(*_lowerCAmelCase  ,			**_lowerCAmelCase     )
					@property
					def   a (	self					:	List[Any]     )       ->				Tuple:



											'''simple docstring'''





											__magic_name__ 		=			self.tokenizer.model_input_names
											__magic_name__ 		=			self.image_processor.model_input_names
											return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )
					@property
					def   a (	self					:	Optional[Any]     )       ->				Dict:



											'''simple docstring'''





											warnings.warn(
											    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'  ,			_lowerCAmelCase  ,			)
											return self.image_processor_class







					@property
					def   a (	self					:	Dict     )       ->				Tuple:



											'''simple docstring'''





											warnings.warn(
											    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'  ,			_lowerCAmelCase  ,			)
											return self.image_processor

