


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





				"""simple docstring"""







				def __init__(    self							,		UpperCAmelCase__							,		UpperCAmelCase__=99							,		UpperCAmelCase__=13							,		UpperCAmelCase__=16							,		UpperCAmelCase__=7							,		UpperCAmelCase__=True							,		UpperCAmelCase__=True							,		UpperCAmelCase__=True							,		UpperCAmelCase__=False							,		UpperCAmelCase__=True							,		UpperCAmelCase__=2							,		UpperCAmelCase__=32							,		UpperCAmelCase__=4							,		UpperCAmelCase__=4							,		UpperCAmelCase__=30							,		UpperCAmelCase__=0							,		UpperCAmelCase__=1							,		UpperCAmelCase__=2							,		UpperCAmelCase__=None							,		):
								SCREAMING_SNAKE_CASE__  				=					parent
								SCREAMING_SNAKE_CASE__  				=					batch_size
								SCREAMING_SNAKE_CASE__  				=					decoder_seq_length
								# For common tests
								SCREAMING_SNAKE_CASE__  				=					self.decoder_seq_length
								SCREAMING_SNAKE_CASE__  				=					is_training
								SCREAMING_SNAKE_CASE__  				=					use_attention_mask
								SCREAMING_SNAKE_CASE__  				=					use_labels

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

								SCREAMING_SNAKE_CASE__  				=					None
								SCREAMING_SNAKE_CASE__  				=					decoder_seq_length
								SCREAMING_SNAKE_CASE__  				=					2
								SCREAMING_SNAKE_CASE__  				=					1



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					ids_tensor([self.batch_size, self.decoder_seq_length]							,		self.vocab_size  )

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

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

								SCREAMING_SNAKE_CASE__  				=					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 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		):
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					SpeechaTextaDecoder(config=UpperCAmelCase__  ).to(UpperCAmelCase__  ).eval()
								SCREAMING_SNAKE_CASE__  				=					input_ids[:2]

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

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

								SCREAMING_SNAKE_CASE__  				=					outputs["past_key_values"]

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

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

								print(UpperCAmelCase__  )

								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )["last_hidden_state"]
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__							,		past_key_values=UpperCAmelCase__  )["last_hidden_state"]

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

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






				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.prepare_config_and_inputs()
								(
								    (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   
								)  				=					config_and_inputs

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


@require_torch
class       lowerCamelCase_		(      lowercase							,	lowercase							,	lowercase							,	unittest.TestCase ):





				"""simple docstring"""







				_lowerCAmelCase   :   str   				=       (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
				_lowerCAmelCase   :   List[Any]   				=       (SpeechaTextaForCausalLM,) if is_torch_available() else ()
				_lowerCAmelCase   :   str   				=       {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
				_lowerCAmelCase   :   int   				=       True
				_lowerCAmelCase   :   Optional[int]   				=       False



				def 	lowerCAmelCase__		(    self							,		):
								SCREAMING_SNAKE_CASE__  				=					SpeechaTextaStandaloneDecoderModelTester(self							,		is_training=UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					ConfigTester(self							,		config_class=UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self  ):
								pass



				def 	lowerCAmelCase__		(    self  ):
								pass



				def 	lowerCAmelCase__		(    self  ):
								pass



				def 	lowerCAmelCase__		(    self  ):
								self.config_tester.run_common_tests()



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase__  )






				def 	lowerCAmelCase__		(    self  ):
								return




"""simple docstring"""






class       lowerCamelCase_		:





				"""simple docstring"""







				def __init__(    self  ):
								SCREAMING_SNAKE_CASE__  				=					[0]
								SCREAMING_SNAKE_CASE__  				=					0



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					self.__heap[i]
								while i // 2 > 0:
												if self.__heap[i] > self.__heap[i // 2]:
																SCREAMING_SNAKE_CASE__  				=					self.__heap[i // 2]
																SCREAMING_SNAKE_CASE__  				=					temporary
												i //= 2



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__  ):
								self.__heap.append(UpperCAmelCase__  )
								self.__size += 1
								self.__swap_up(self.__size  )



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__  ):
								while self.__size >= 2 * i:
												if 2 * i + 1 > self.__size:
																SCREAMING_SNAKE_CASE__  				=					2 * i
												else:
																if self.__heap[2 * i] > self.__heap[2 * i + 1]:
																				SCREAMING_SNAKE_CASE__  				=					2 * i
																else:
																				SCREAMING_SNAKE_CASE__  				=					2 * i + 1
												SCREAMING_SNAKE_CASE__  				=					self.__heap[i]
												if self.__heap[i] < self.__heap[bigger_child]:
																SCREAMING_SNAKE_CASE__  				=					self.__heap[bigger_child]
																SCREAMING_SNAKE_CASE__  				=					temporary
												SCREAMING_SNAKE_CASE__  				=					bigger_child



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.__heap[1]
								SCREAMING_SNAKE_CASE__  				=					self.__heap[self.__size]
								self.__size -= 1
								self.__heap.pop()
								self.__swap_down(1  )
								return max_value



				@property
				def 	lowerCAmelCase__		(    self  ):
								return self.__heap[1:]






				def __len__(    self  ):
								return self.__size


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	# create an instance of BinaryHeap
	_lowerCamelCase  =	BinaryHeap()
	binary_heap.insert(6)
	binary_heap.insert(10)
	binary_heap.insert(15)
	binary_heap.insert(12)
	# pop root(max-values because it is max heap)
	print(binary_heap.pop())  # 15
	print(binary_heap.pop())  # 12
	# get the list and size after operations
	print(binary_heap.get_list)
	print(len(binary_heap))
