



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





     def __init__(							self			:				int  ,	UpperCamelCase			:				Tuple  ,	UpperCamelCase			:				Union[str, Any]=9_9  ,	UpperCamelCase			:				Optional[Any]=1_3  ,	UpperCamelCase			:				Union[str, Any]=1_6  ,	UpperCamelCase			:				Any=7  ,	UpperCamelCase			:				int=True  ,	UpperCamelCase			:				Optional[Any]=True  ,	UpperCamelCase			:				Dict=True  ,	UpperCamelCase			:				Any=False  ,	UpperCamelCase			:				List[str]=True  ,	UpperCamelCase			:				Optional[Any]=2  ,	UpperCamelCase			:				str=3_2  ,	UpperCamelCase			:				int=4  ,	UpperCamelCase			:				Tuple=4  ,	UpperCamelCase			:				int=3_0  ,	UpperCamelCase			:				Tuple=0  ,	UpperCamelCase			:				Tuple=1  ,	UpperCamelCase			:				str=2  ,	UpperCamelCase			:				Any=None  ,	)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     Tuple							=						parent
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						batch_size
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						decoder_seq_length
      # For common tests
      __SCREAMING_SNAKE_CASE							:     int							=						self.decoder_seq_length
      __SCREAMING_SNAKE_CASE							:     Tuple							=						is_training
      __SCREAMING_SNAKE_CASE							:     List[str]							=						use_attention_mask
      __SCREAMING_SNAKE_CASE							:     int							=						use_labels

      __SCREAMING_SNAKE_CASE							:     str							=						vocab_size
      __SCREAMING_SNAKE_CASE							:     List[str]							=						d_model
      __SCREAMING_SNAKE_CASE							:     Optional[Any]							=						d_model
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						decoder_layers
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						decoder_layers
      __SCREAMING_SNAKE_CASE							:     str							=						decoder_ffn_dim
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						decoder_attention_heads
      __SCREAMING_SNAKE_CASE							:     str							=						decoder_attention_heads
      __SCREAMING_SNAKE_CASE							:     int							=						eos_token_id
      __SCREAMING_SNAKE_CASE							:     str							=						bos_token_id
      __SCREAMING_SNAKE_CASE							:     str							=						pad_token_id
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						decoder_start_token_id
      __SCREAMING_SNAKE_CASE							:     Any							=						use_cache
      __SCREAMING_SNAKE_CASE							:     Tuple							=						max_position_embeddings

      __SCREAMING_SNAKE_CASE							:     str							=						None
      __SCREAMING_SNAKE_CASE							:     List[str]							=						decoder_seq_length
      __SCREAMING_SNAKE_CASE							:     Dict							=						2
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						1





     def 						__snake_case     (							self			:				Any					)->Optional[int]:
      __SCREAMING_SNAKE_CASE							:     Dict							=						ids_tensor([self.batch_size, self.decoder_seq_length]  ,	self.vocab_size					)

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

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

      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						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			:				Optional[int]  ,	UpperCamelCase			:				Tuple  ,	UpperCamelCase			:				List[Any]  ,	UpperCamelCase			:				List[str]  ,	UpperCamelCase			:				Union[str, Any]  ,	)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						True
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						SpeechaTextaDecoder(config=UpperCamelCase					).to(UpperCamelCase					).eval()
      __SCREAMING_SNAKE_CASE							:     Any							=						input_ids[:2]

      input_ids[input_ids == 0] += 1
      # first forward pass
      __SCREAMING_SNAKE_CASE							:     int							=						model(UpperCamelCase  ,	use_cache=UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						model(UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     int							=						model(UpperCamelCase  ,	use_cache=UpperCamelCase					)

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

      __SCREAMING_SNAKE_CASE							:     str							=						outputs["past_key_values"]

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

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

      print(UpperCamelCase					)

      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						model(UpperCamelCase					)["last_hidden_state"]
      __SCREAMING_SNAKE_CASE							:     Any							=						model(UpperCamelCase  ,	past_key_values=UpperCamelCase					)["last_hidden_state"]

      # select random slice
      __SCREAMING_SNAKE_CASE							:     int							=						ids_tensor((1,)  ,	output_from_past.shape[-1]					).item()
      __SCREAMING_SNAKE_CASE							:     Dict							=						output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
      __SCREAMING_SNAKE_CASE							:     Any							=						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			:				Optional[int]					)->Optional[int]:
      __SCREAMING_SNAKE_CASE							:     List[Any]							=						self.prepare_config_and_inputs()
      (
          (
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		(
          __SCREAMING_SNAKE_CASE
      ),		
      )							:     Optional[int]							=						config_and_inputs

      __SCREAMING_SNAKE_CASE							:     List[Any]							=						{
          "input_ids": input_ids,
          "attention_mask": attention_mask,
      }
      return config, inputs_dict






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





     def 						__snake_case     (							self			:				List[str]  ,	)->Union[str, Any]:
      __SCREAMING_SNAKE_CASE							:     Optional[int]							=						SpeechaTextaStandaloneDecoderModelTester(self  ,	is_training=UpperCamelCase					)
      __SCREAMING_SNAKE_CASE							:     Any							=						ConfigTester(self  ,	config_class=UpperCamelCase					)





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





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





     def 						__snake_case     (							self			:				Union[str, Any]					)->Optional[Any]:
      pass





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





     def 						__snake_case     (							self			:				List[str]					)->List[Any]:
      __SCREAMING_SNAKE_CASE							:     Union[str, Any]							=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase					)






     def 						__snake_case     (							self			:				Optional[int]					)->str:
      return











def 						_UpperCAmelCase							( snake_case     ):





					"""simple docstring"""

					_lowerCAmelCase         =       str(snake_case     ).strip()
					if not bin_string:
										raise ValueError("""Empty string was passed to the function"""     )
					_lowerCAmelCase         =       bin_string[0] == """-"""
					if is_negative:
										_lowerCAmelCase         =       bin_string[1:]
					if not all(char in """01""" for char in bin_string     ):
										raise ValueError("""Non-binary value was passed to the function"""     )
					_lowerCAmelCase         =       0
					for char in bin_string:
										_lowerCAmelCase         =       2 * decimal_number + int(snake_case     )
					return -decimal_number if is_negative else decimal_number


if __name__ == "__main__":
	from doctest import testmod

	testmod()



