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





     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      ,	):

      '''simple docstring'''




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

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

      __UpperCamelCase							    =			None
      __UpperCamelCase							    =			decoder_seq_length
      __UpperCamelCase							    =			2
      __UpperCamelCase							    =			1





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




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

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

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

      __UpperCamelCase							    =			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      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			True
      __UpperCamelCase							    =			SpeechaTextaDecoder(config=UpperCAmelCase__ ).to(UpperCAmelCase__ ).eval()
      __UpperCamelCase							    =			input_ids[:2]

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

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

      __UpperCamelCase							    =			outputs['past_key_values']

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

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

      print(UpperCAmelCase__ )

      __UpperCamelCase							    =			model(UpperCAmelCase__ )['last_hidden_state']
      __UpperCamelCase							    =			model(UpperCAmelCase__      ,	past_key_values=UpperCAmelCase__ )['last_hidden_state']

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

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







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




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

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


@require_torch
class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ,    __SCREAMING_SNAKE_CASE     ,    __SCREAMING_SNAKE_CASE     ,    unittest.TestCase     ):
     lowercase		  =		(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
     lowercase		  =		(SpeechaTextaForCausalLM,) if is_torch_available() else ()
     lowercase		  =		{"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
     lowercase		  =		True
     lowercase		  =		False





     def 		UpperCAmelCase   (      self      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			SpeechaTextaStandaloneDecoderModelTester(self      ,	is_training=UpperCAmelCase__ )
      __UpperCamelCase							    =			ConfigTester(self      ,	config_class=UpperCAmelCase__ )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      pass





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      pass





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      pass





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self.config_tester.run_common_tests()





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase__ )







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      return







"""simple docstring"""






def __lowercase  (   lowerCamelCase_	:   list	):
				if not isinstance(lowerCamelCase_    ,       lowerCamelCase_	):
								raise ValueError("Input series is not valid, valid series - [2, 4, 8]"	)
				if len(lowerCamelCase_	) == 0:
								raise ValueError("Input list must be a non empty list"	)
				if len(lowerCamelCase_	) == 1:
								return True
				try:
								SCREAMING_SNAKE_CASE__  				=					series[1] / series[0]
								for index in range(len(lowerCamelCase_	) - 1	):
												if series[index + 1] / series[index] != common_ratio:
																return False
				except ZeroDivisionError:
								return False
				return True


def __lowercase  (   lowerCamelCase_	:   list	):
				if not isinstance(lowerCamelCase_    ,       lowerCamelCase_	):
								raise ValueError("Input series is not valid, valid series - [2, 4, 8]"	)
				if len(lowerCamelCase_	) == 0:
								raise ValueError("Input list must be a non empty list"	)
				SCREAMING_SNAKE_CASE__  				=					1
				for value in series:
								answer *= value
				return pow(lowerCamelCase_    ,       1 / len(lowerCamelCase_	)	)


if __name__ == "__main__":
	import doctest

	doctest.testmod()
