


'''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_  :





    """simple docstring"""






    def __init__(							self      ,					SCREAMING_SNAKE_CASE_      ,					SCREAMING_SNAKE_CASE_=99      ,					SCREAMING_SNAKE_CASE_=13      ,					SCREAMING_SNAKE_CASE_=16      ,					SCREAMING_SNAKE_CASE_=7      ,					SCREAMING_SNAKE_CASE_=True      ,					SCREAMING_SNAKE_CASE_=True      ,					SCREAMING_SNAKE_CASE_=True      ,					SCREAMING_SNAKE_CASE_=False      ,					SCREAMING_SNAKE_CASE_=True      ,					SCREAMING_SNAKE_CASE_=2      ,					SCREAMING_SNAKE_CASE_=32      ,					SCREAMING_SNAKE_CASE_=4      ,					SCREAMING_SNAKE_CASE_=4      ,					SCREAMING_SNAKE_CASE_=30      ,					SCREAMING_SNAKE_CASE_=0      ,					SCREAMING_SNAKE_CASE_=1      ,					SCREAMING_SNAKE_CASE_=2      ,					SCREAMING_SNAKE_CASE_=None      ,					)    ->					Dict:
         __lowerCamelCase       :     List[Any]        =		parent
         __lowerCamelCase       :     Tuple        =		batch_size
         __lowerCamelCase       :     Tuple        =		decoder_seq_length
         # For common tests
         __lowerCamelCase       :     Dict        =		self.decoder_seq_length
         __lowerCamelCase       :     int        =		is_training
         __lowerCamelCase       :     Dict        =		use_attention_mask
         __lowerCamelCase       :     Tuple        =		use_labels

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

         __lowerCamelCase       :     Tuple        =		None
         __lowerCamelCase       :     Tuple        =		decoder_seq_length
         __lowerCamelCase       :     str        =		2
         __lowerCamelCase       :     str        =		1







    def 				lowercase_    (							self     )    ->					str:
         __lowerCamelCase       :     List[str]        =		ids_tensor([self.batch_size, self.decoder_seq_length]      ,					self.vocab_size     )

         __lowerCamelCase       :     List[Any]        =		None
         if self.use_attention_mask:
              __lowerCamelCase       :     Tuple        =		ids_tensor([self.batch_size, self.decoder_seq_length]      ,					vocab_size=2     )

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

         __lowerCamelCase       :     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 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_      ,					SCREAMING_SNAKE_CASE_      ,					SCREAMING_SNAKE_CASE_      ,					SCREAMING_SNAKE_CASE_      ,					)    ->					List[Any]:
         __lowerCamelCase       :     str        =		True
         __lowerCamelCase       :     str        =		SpeechaTextaDecoder(config=SCREAMING_SNAKE_CASE_     ).to(SCREAMING_SNAKE_CASE_     ).eval()
         __lowerCamelCase       :     List[Any]        =		input_ids[:2]

         input_ids[input_ids == 0] += 1
         # first forward pass
         __lowerCamelCase       :     str        =		model(SCREAMING_SNAKE_CASE_      ,					use_cache=SCREAMING_SNAKE_CASE_     )
         __lowerCamelCase       :     Optional[Any]        =		model(SCREAMING_SNAKE_CASE_     )
         __lowerCamelCase       :     str        =		model(SCREAMING_SNAKE_CASE_      ,					use_cache=SCREAMING_SNAKE_CASE_     )

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

         __lowerCamelCase       :     Optional[Any]        =		outputs['past_key_values']

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

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

         print(SCREAMING_SNAKE_CASE_     )

         __lowerCamelCase       :     Any        =		model(SCREAMING_SNAKE_CASE_     )['last_hidden_state']
         __lowerCamelCase       :     Optional[int]        =		model(SCREAMING_SNAKE_CASE_      ,					past_key_values=SCREAMING_SNAKE_CASE_     )['last_hidden_state']

         # select random slice
         __lowerCamelCase       :     Dict        =		ids_tensor((1,)      ,					output_from_past.shape[-1]     ).item()
         __lowerCamelCase       :     Optional[int]        =		output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
         __lowerCamelCase       :     Tuple        =		output_from_past[:, 0, random_slice_idx].detach()

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







    def 				lowercase_    (							self     )    ->					Any:
         __lowerCamelCase       :     Tuple        =		self.prepare_config_and_inputs()
         (
             (
             __lowerCamelCase
         )    ,	(
             __lowerCamelCase
         )    ,	(
             __lowerCamelCase
         )    ,	(
             __lowerCamelCase
         )    ,	
         )       :     str        =		config_and_inputs

         __lowerCamelCase       :     Dict        =		{
             'input_ids': input_ids,
             'attention_mask': attention_mask,
         }
         return config, inputs_dict







@require_torch
class        UpperCAmelCase_  (_UpperCAmelCase ,		_UpperCAmelCase ,		_UpperCAmelCase ,		unittest.TestCase  ):





    """simple docstring"""






    lowerCamelCase  : List[str]												=      (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
    lowerCamelCase  : Union[str, Any]												=      (SpeechaTextaForCausalLM,) if is_torch_available() else ()
    lowerCamelCase  : str												=      {'text-generation': SpeechaTextaForCausalLM} if is_torch_available() else {}
    lowerCamelCase  : Any												=      True
    lowerCamelCase  : Union[str, Any]												=      False







    def 				lowercase_    (							self      ,					)    ->					List[str]:
         __lowerCamelCase       :     Tuple        =		SpeechaTextaStandaloneDecoderModelTester(self      ,					is_training=SCREAMING_SNAKE_CASE_     )
         __lowerCamelCase       :     Tuple        =		ConfigTester(self      ,					config_class=SCREAMING_SNAKE_CASE_     )







    def 				lowercase_    (							self     )    ->					Dict:
         pass







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







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







    def 				lowercase_    (							self     )    ->					Tuple:
         self.config_tester.run_common_tests()







    def 				lowercase_    (							self     )    ->					List[str]:
         __lowerCamelCase       :     List[str]        =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past(*SCREAMING_SNAKE_CASE_     )







    def 				lowercase_    (							self     )    ->					Optional[Any]:
         return










'''simple docstring'''



from typing import Literal

A__		: Tuple												=  """ABCDEFGHIJKLMNOPQRSTUVWXYZ"""



def       UpperCAmelCase__ (     UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    Literal["encrypt", "decrypt"]							)					->		str:
     __lowerCamelCase       :     Dict        =		LETTERS if mode == 'decrypt' else key
     __lowerCamelCase       :     Tuple        =		key if mode == 'decrypt' else LETTERS
     __lowerCamelCase       :     Optional[int]        =		''
     # loop through each symbol in the message
     for symbol in message:
          if symbol.upper() in chars_a:
               # encrypt/decrypt the symbol
               __lowerCamelCase       :     Optional[int]        =		chars_a.find(symbol.upper()							)
               if symbol.isupper():
                    translated += chars_b[sym_index].upper()
               else:
                    translated += chars_b[sym_index].lower()
          else:
               # symbol is not in LETTERS, just add it
               translated += symbol
     return translated



def       UpperCAmelCase__ (     UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    str							)					->		str:
     return translate_message(UpperCAmelCase_				,   UpperCAmelCase_				,   'encrypt'							)



def       UpperCAmelCase__ (     UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    str							)					->		str:
     return translate_message(UpperCAmelCase_				,   UpperCAmelCase_				,   'decrypt'							)



def       UpperCAmelCase__ (     )					->		None:
     __lowerCamelCase       :     Dict        =		'Hello World'
     __lowerCamelCase       :     str        =		'QWERTYUIOPASDFGHJKLZXCVBNM'
     __lowerCamelCase       :     Any        =		'decrypt'  # set to 'encrypt' or 'decrypt'

     if mode == "encrypt":
          __lowerCamelCase       :     Optional[int]        =		encrypt_message(UpperCAmelCase_				,   UpperCAmelCase_							)
     elif mode == "decrypt":
          __lowerCamelCase       :     List[str]        =		decrypt_message(UpperCAmelCase_				,   UpperCAmelCase_							)
     print(F'Using the key {key}, the {mode}ed message is: {translated}'							)


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      main()






