

"""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     _UpperCAmelCase			:
			def __init__(		self      ,		_A      ,		_A=99      ,		_A=13      ,		_A=16      ,		_A=7      ,		_A=True      ,		_A=True      ,		_A=True      ,		_A=False      ,		_A=True      ,		_A=2      ,		_A=32      ,		_A=4      ,		_A=4      ,		_A=30      ,		_A=0      ,		_A=1      ,		_A=2      ,		_A=None      ,		)    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							List[Any]									=					parent
				_UpperCAmelCase    :							List[str]									=					batch_size
				_UpperCAmelCase    :							Optional[Any]									=					decoder_seq_length
				# For common tests
				_UpperCAmelCase    :							List[Any]									=					self.decoder_seq_length
				_UpperCAmelCase    :							str									=					is_training
				_UpperCAmelCase    :							List[str]									=					use_attention_mask
				_UpperCAmelCase    :							List[str]									=					use_labels

				_UpperCAmelCase    :							Dict									=					vocab_size
				_UpperCAmelCase    :							str									=					d_model
				_UpperCAmelCase    :							List[str]									=					d_model
				_UpperCAmelCase    :							str									=					decoder_layers
				_UpperCAmelCase    :							str									=					decoder_layers
				_UpperCAmelCase    :							Union[str, Any]									=					decoder_ffn_dim
				_UpperCAmelCase    :							str									=					decoder_attention_heads
				_UpperCAmelCase    :							Union[str, Any]									=					decoder_attention_heads
				_UpperCAmelCase    :							List[str]									=					eos_token_id
				_UpperCAmelCase    :							Optional[int]									=					bos_token_id
				_UpperCAmelCase    :							int									=					pad_token_id
				_UpperCAmelCase    :							int									=					decoder_start_token_id
				_UpperCAmelCase    :							Optional[Any]									=					use_cache
				_UpperCAmelCase    :							List[str]									=					max_position_embeddings

				_UpperCAmelCase    :							Optional[Any]									=					None
				_UpperCAmelCase    :							Any									=					decoder_seq_length
				_UpperCAmelCase    :							Union[str, Any]									=					2
				_UpperCAmelCase    :							Tuple									=					1
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							List[str]									=					ids_tensor([self.batch_size, self.decoder_seq_length]      ,		self.vocab_size     )

				_UpperCAmelCase    :							Optional[int]									=					None
				if self.use_attention_mask:
					_UpperCAmelCase    :							List[str]									=					ids_tensor([self.batch_size, self.decoder_seq_length]      ,		vocab_size=2     )

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

				_UpperCAmelCase    :							Union[str, Any]									=					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 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		)    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					True
				_UpperCAmelCase    :							Optional[Any]									=					SpeechaTextaDecoder(config=UpperCAmelCase_     ).to(UpperCAmelCase_     ).eval()
				_UpperCAmelCase    :							Union[str, Any]									=					input_ids[:2]

				input_ids[input_ids == 0] += 1
				# first forward pass
				_UpperCAmelCase    :							List[Any]									=					model(UpperCAmelCase_      ,		use_cache=UpperCAmelCase_     )
				_UpperCAmelCase    :							List[Any]									=					model(UpperCAmelCase_     )
				_UpperCAmelCase    :							str									=					model(UpperCAmelCase_      ,		use_cache=UpperCAmelCase_     )

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

				_UpperCAmelCase    :							Tuple									=					outputs['past_key_values']

				# create hypothetical next token and extent to next_input_ids
				_UpperCAmelCase    :							Union[str, Any]									=					ids_tensor((2, 1)      ,		config.vocab_size - 1     ) + 1

				# append to next input_ids and
				_UpperCAmelCase    :							List[str]									=					torch.cat([input_ids, next_tokens]      ,		dim=-1     )

				print(UpperCAmelCase_     )

				_UpperCAmelCase    :							Union[str, Any]									=					model(UpperCAmelCase_     )['last_hidden_state']
				_UpperCAmelCase    :							Optional[int]									=					model(UpperCAmelCase_      ,		past_key_values=UpperCAmelCase_     )['last_hidden_state']

				# select random slice
				_UpperCAmelCase    :							Optional[Any]									=					ids_tensor((1,)      ,		output_from_past.shape[-1]     ).item()
				_UpperCAmelCase    :							List[str]									=					output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
				_UpperCAmelCase    :							int									=					output_from_past[:, 0, random_slice_idx].detach()

				# test that outputs are equal for slice
				assert torch.allclose(UpperCAmelCase_      ,		UpperCAmelCase_      ,		atol=1e-3     )
			def 							__snake_case (		self     )    ->    Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					self.prepare_config_and_inputs()
				(
				    _UpperCAmelCase  
				)    :							Any									=					config_and_inputs

				_UpperCAmelCase    :							Union[str, Any]									=					{
				    'input_ids': input_ids,
				    'attention_mask': attention_mask,
				}
				return config, inputs_dict






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






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[Any]									=					SpeechaTextaStandaloneDecoderModelTester(self      ,		is_training=UpperCAmelCase_     )
				_UpperCAmelCase    :							Dict									=					ConfigTester(self      ,		config_class=UpperCAmelCase_     )
			def 							__snake_case (		self     )    ->    Union[str, Any]:






				'''simple docstring'''







				pass
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				pass
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







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






				'''simple docstring'''







				self.config_tester.run_common_tests()
			def 							__snake_case (		self     )    ->    Any:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase_     )
			def 							__snake_case (		self     )    ->    int:






				'''simple docstring'''







				return



"""simple docstring"""






from __future__ import annotations
def       UpperCamelCase					(			_lowerCAmelCase     :						list,  _lowerCAmelCase     :						list )				->			list:
	_UpperCAmelCase    :							List[Any]									=					[None] * (len(_lowerCAmelCase ) + len(_lowerCAmelCase ))

	_UpperCAmelCase    :							Union[str, Any]									=					0  # pointer to current index for left Half
	_UpperCAmelCase    :							str									=					0  # pointer to current index for the right Half
	_UpperCAmelCase    :							Optional[int]									=					0  # pointer to current index for the sorted array Half

	while pointera < len(_lowerCAmelCase ) and pointera < len(_lowerCAmelCase ):
		if left_half[pointera] < right_half[pointera]:
			_UpperCAmelCase    :							Tuple									=					left_half[pointera]
			pointera += 1
			index += 1
		else:
			_UpperCAmelCase    :							int									=					right_half[pointera]
			pointera += 1
			index += 1
	while pointera < len(_lowerCAmelCase ):
		_UpperCAmelCase    :							Tuple									=					left_half[pointera]
		pointera += 1
		index += 1

	while pointera < len(_lowerCAmelCase ):
		_UpperCAmelCase    :							Union[str, Any]									=					right_half[pointera]
		pointera += 1
		index += 1

	return sorted_array
def       UpperCamelCase					(			_lowerCAmelCase     :						list )				->			list:
	if len(_lowerCAmelCase ) <= 1:
		return array
	# the actual formula to calculate the middle element = left + (right - left) // 2
	# this avoids integer overflow in case of large N
	_UpperCAmelCase    :							Any									=					0 + (len(_lowerCAmelCase ) - 0) // 2

	# Split the array into halves till the array length becomes equal to One
	# merge the arrays of single length returned by mergeSort function and
	# pass them into the merge arrays function which merges the array
	_UpperCAmelCase    :							Optional[Any]									=					array[:middle]
	_UpperCAmelCase    :							Optional[int]									=					array[middle:]

	return merge(merge_sort(_lowerCAmelCase ),  merge_sort(_lowerCAmelCase ) )


if __name__ == "__main__":
						import doctest

						doctest.testmod()





