




import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        YosoForMaskedLM,
        YosoForMultipleChoice,
        YosoForQuestionAnswering,
        YosoForSequenceClassification,
        YosoForTokenClassification,
        YosoModel,
    )
    from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST


class        UpperCAmelCase__				:



     '''simple docstring'''
     def __init__(				self ,    _lowerCAmelCase ,    _lowerCAmelCase=13 ,    _lowerCAmelCase=7 ,    _lowerCAmelCase=True ,    _lowerCAmelCase=True ,    _lowerCAmelCase=True ,    _lowerCAmelCase=True ,    _lowerCAmelCase=99 ,    _lowerCAmelCase=32 ,    _lowerCAmelCase=5 ,    _lowerCAmelCase=4 ,    _lowerCAmelCase=37 ,    _lowerCAmelCase="gelu" ,    _lowerCAmelCase=0.1 ,    _lowerCAmelCase=0.1 ,    _lowerCAmelCase=512 ,    _lowerCAmelCase=16 ,    _lowerCAmelCase=2 ,    _lowerCAmelCase=0.02 ,    _lowerCAmelCase=3 ,    _lowerCAmelCase=4 ,    _lowerCAmelCase=None ,    ):
            a			   =parent
            a			   =batch_size
            a			   =seq_length
            a			   =is_training
            a			   =use_input_mask
            a			   =use_token_type_ids
            a			   =use_labels
            a			   =vocab_size
            a			   =hidden_size
            a			   =num_hidden_layers
            a			   =num_attention_heads
            a			   =intermediate_size
            a			   =hidden_act
            a			   =hidden_dropout_prob
            a			   =attention_probs_dropout_prob
            a			   =max_position_embeddings
            a			   =type_vocab_size
            a			   =type_sequence_label_size
            a			   =initializer_range
            a			   =num_labels
            a			   =num_choices
            a			   =scope
     def        lowerCAmelCase__  (				self	):
            a			   =ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size	)

            a			   =None
            if self.use_input_mask:
                   a			   =random_attention_mask([self.batch_size, self.seq_length]	)

            a			   =None
            if self.use_token_type_ids:
                   a			   =ids_tensor([self.batch_size, self.seq_length] ,    self.type_vocab_size	)

            a			   =None
            a			   =None
            a			   =None
            if self.use_labels:
                   a			   =ids_tensor([self.batch_size] ,    self.type_sequence_label_size	)
                   a			   =ids_tensor([self.batch_size, self.seq_length] ,    self.num_labels	)
                   a			   =ids_tensor([self.batch_size] ,    self.num_choices	)

            a			   =self.get_config()

            return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
     def        lowerCAmelCase__  (				self	):
            return YosoConfig(
                vocab_size=self.vocab_size ,    hidden_size=self.hidden_size ,    num_hidden_layers=self.num_hidden_layers ,    num_attention_heads=self.num_attention_heads ,    intermediate_size=self.intermediate_size ,    hidden_act=self.hidden_act ,    hidden_dropout_prob=self.hidden_dropout_prob ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob ,    max_position_embeddings=self.max_position_embeddings ,    type_vocab_size=self.type_vocab_size ,    is_decoder=_lowerCAmelCase ,    initializer_range=self.initializer_range ,    )
     def        lowerCAmelCase__  (				self	):
            a			   =self.get_config()
            a			   =300
            return config
     def        lowerCAmelCase__  (				self	):
            (
                (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      
            )			   =self.prepare_config_and_inputs()

            a			   =True
            a			   =floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
            a			   =ids_tensor([self.batch_size, self.seq_length] ,    vocab_size=2	)

            return (
                config,
                input_ids,
                token_type_ids,
                input_mask,
                sequence_labels,
                token_labels,
                choice_labels,
                encoder_hidden_states,
                encoder_attention_mask,
            )
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            a			   =YosoModel(config=_lowerCAmelCase	)
            model.to(_lowerCAmelCase	)
            model.eval()
            a			   =model(_lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase	)
            a			   =model(_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase	)
            a			   =model(_lowerCAmelCase	)
            self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size)	)
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    ):
            a			   =True
            a			   =YosoModel(_lowerCAmelCase	)
            model.to(_lowerCAmelCase	)
            model.eval()
            a			   =model(
                _lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase ,    encoder_hidden_states=_lowerCAmelCase ,    encoder_attention_mask=_lowerCAmelCase ,    )
            a			   =model(
                _lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase ,    encoder_hidden_states=_lowerCAmelCase ,    )
            a			   =model(_lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase	)
            self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size)	)
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            a			   =YosoForMaskedLM(config=_lowerCAmelCase	)
            model.to(_lowerCAmelCase	)
            model.eval()
            a			   =model(_lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase ,    labels=_lowerCAmelCase	)
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)	)
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            a			   =YosoForQuestionAnswering(config=_lowerCAmelCase	)
            model.to(_lowerCAmelCase	)
            model.eval()
            a			   =model(
                _lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase ,    start_positions=_lowerCAmelCase ,    end_positions=_lowerCAmelCase ,    )
            self.parent.assertEqual(result.start_logits.shape ,    (self.batch_size, self.seq_length)	)
            self.parent.assertEqual(result.end_logits.shape ,    (self.batch_size, self.seq_length)	)
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            a			   =self.num_labels
            a			   =YosoForSequenceClassification(_lowerCAmelCase	)
            model.to(_lowerCAmelCase	)
            model.eval()
            a			   =model(_lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase ,    labels=_lowerCAmelCase	)
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_labels)	)
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            a			   =self.num_labels
            a			   =YosoForTokenClassification(config=_lowerCAmelCase	)
            model.to(_lowerCAmelCase	)
            model.eval()
            a			   =model(_lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase ,    labels=_lowerCAmelCase	)
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.num_labels)	)
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            a			   =self.num_choices
            a			   =YosoForMultipleChoice(config=_lowerCAmelCase	)
            model.to(_lowerCAmelCase	)
            model.eval()
            a			   =input_ids.unsqueeze(1	).expand(-1 ,    self.num_choices ,    -1	).contiguous()
            a			   =token_type_ids.unsqueeze(1	).expand(-1 ,    self.num_choices ,    -1	).contiguous()
            a			   =input_mask.unsqueeze(1	).expand(-1 ,    self.num_choices ,    -1	).contiguous()
            a			   =model(
                _lowerCAmelCase ,    attention_mask=_lowerCAmelCase ,    token_type_ids=_lowerCAmelCase ,    labels=_lowerCAmelCase ,    )
            self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_choices)	)






     def        lowerCAmelCase__  (				self	):
            a			   =self.prepare_config_and_inputs()
            (
                (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      (
                a
            )   ,      
            )			   =config_and_inputs
            a			   ={"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
            return config, inputs_dict



@require_torch
class        UpperCAmelCase__				(							UpperCAmelCase__      ,		UpperCAmelCase__      ,		unittest.TestCase				):



     '''simple docstring'''
     _SCREAMING_SNAKE_CASE      :					List[str]     					=    (
         (
             YosoModel,
             YosoForMaskedLM,
             YosoForMultipleChoice,
             YosoForQuestionAnswering,
             YosoForSequenceClassification,
             YosoForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     _SCREAMING_SNAKE_CASE      :					List[str]     					=    False
     _SCREAMING_SNAKE_CASE      :					Optional[int]     					=    False
     _SCREAMING_SNAKE_CASE      :					List[str]     					=    False

     _SCREAMING_SNAKE_CASE      :					Optional[Any]     					=    ()
     _SCREAMING_SNAKE_CASE      :					Dict     					=    (
         {
             "feature-extraction": YosoModel,
             "fill-mask": YosoForMaskedLM,
             "question-answering": YosoForQuestionAnswering,
             "text-classification": YosoForSequenceClassification,
             "token-classification": YosoForTokenClassification,
             "zero-shot": YosoForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     def        lowerCAmelCase__  (				self	):
            a			   =YosoModelTester(self	)
            a			   =ConfigTester(self ,    config_class=_lowerCAmelCase ,    hidden_size=37	)
     def        lowerCAmelCase__  (				self	):
            self.config_tester.run_common_tests()
     def        lowerCAmelCase__  (				self	):
            a			   =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_lowerCAmelCase	)
     def        lowerCAmelCase__  (				self	):
            a			   =self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                   a			   =type
                   self.model_tester.create_and_check_model(*_lowerCAmelCase	)
     def        lowerCAmelCase__  (				self	):
            a			   =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase	)
     def        lowerCAmelCase__  (				self	):
            a			   =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase	)
     def        lowerCAmelCase__  (				self	):
            a			   =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase	)
     def        lowerCAmelCase__  (				self	):
            a			   =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase	)
     def        lowerCAmelCase__  (				self	):
            a			   =self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase	)
     @slow
     def        lowerCAmelCase__  (				self	):
            for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   a			   =YosoModel.from_pretrained(_lowerCAmelCase	)
                   self.assertIsNotNone(_lowerCAmelCase	)






     def        lowerCAmelCase__  (				self	):
            return



@require_torch
class        UpperCAmelCase__				(							unittest.TestCase				):



     '''simple docstring'''
     @slow
     def        lowerCAmelCase__  (				self	):
            a			   =YosoModel.from_pretrained("""uw-madison/yoso-4096"""	)
            a			   =torch.tensor([[0, 1, 2, 3, 4, 5]]	)

            with torch.no_grad():
                   a			   =model(_lowerCAmelCase	)[0]

            a			   =torch.Size((1, 6, 768)	)
            self.assertEqual(output.shape ,    _lowerCAmelCase	)

            a			   =torch.tensor(
                [[[-0.06_11, 0.12_42, 0.08_40], [0.02_80, -0.00_48, 0.11_25], [0.01_06, 0.02_26, 0.07_51]]]	)

            self.assertTrue(torch.allclose(output[:, :3, :3] ,    _lowerCAmelCase ,    atol=1E-4	)	)
     @slow
     def        lowerCAmelCase__  (				self	):
            a			   =YosoForMaskedLM.from_pretrained("""uw-madison/yoso-4096"""	)
            a			   =torch.tensor([[0, 1, 2, 3, 4, 5]]	)

            with torch.no_grad():
                   a			   =model(_lowerCAmelCase	)[0]

            a			   =50_265

            a			   =torch.Size((1, 6, vocab_size)	)
            self.assertEqual(output.shape ,    _lowerCAmelCase	)

            a			   =torch.tensor(
                [[[-2.13_13, -3.72_85, -2.24_07], [-2.70_47, -3.33_14, -2.64_08], [0.06_29, -2.51_66, -0.33_56]]]	)

            self.assertTrue(torch.allclose(output[:, :3, :3] ,    _lowerCAmelCase ,    atol=1E-4	)	)






     @slow
     def        lowerCAmelCase__  (				self	):
            a			   =YosoForMaskedLM.from_pretrained("""uw-madison/yoso-4096"""	)
            a			   =torch.arange(4_096	).unsqueeze(0	)

            with torch.no_grad():
                   a			   =model(_lowerCAmelCase	)[0]

            a			   =50_265

            a			   =torch.Size((1, 4_096, vocab_size)	)
            self.assertEqual(output.shape ,    _lowerCAmelCase	)

            a			   =torch.tensor(
                [[[-2.39_14, -4.37_42, -5.09_56], [-4.09_88, -4.23_84, -7.04_06], [-3.14_27, -3.71_92, -6.68_00]]]	)

            self.assertTrue(torch.allclose(output[:, :3, :3] ,    _lowerCAmelCase ,    atol=1E-4	)	)






from __future__ import annotations





def 							lowerCamelCase    (							UpperCAmelCase_	:      str   ,       UpperCAmelCase_	:      dict[str, str]      )->			str:

       """simple docstring"""




       a   ,      a   ,      a			   ="""""", """""", """"""
       a			   =[]

       for character in message_part:
              tmp.append(character_to_number[character]      )

       for each in tmp:
              one += each[0]
              two += each[1]
              three += each[2]

       return one + two + three





def 							lowerCamelCase    (							UpperCAmelCase_	:      str   ,       UpperCAmelCase_	:      dict[str, str]      )->			tuple[str, str, str]:

       """simple docstring"""




       a   ,      a			   ="""""", """"""
       a			   =[]

       for character in message_part:
              this_part += character_to_number[character]

       for digit in this_part:
              tmp += digit
              if len(UpperCAmelCase_      ) == len(UpperCAmelCase_      ):
                     result.append(UpperCAmelCase_      )
                     a			   =""""""

       return result[0], result[1], result[2]





def 							lowerCamelCase    (							UpperCAmelCase_	:      str   ,       UpperCAmelCase_	:      str      )->			tuple[str, str, dict[str, str], dict[str, str]]:

       """simple docstring"""




       a			   =alphabet.replace(""" """   ,       """"""      ).upper()
       a			   =message.replace(""" """   ,       """"""      ).upper()

       # Check length and characters
       if len(UpperCAmelCase_      ) != 27:
              raise KeyError("""Length of alphabet has to be 27."""      )
       for each in message:
              if each not in alphabet:
                     raise ValueError("""Each message character has to be included in alphabet!"""      )

    # Generate dictionares
       a			   =(
           """111""",
           """112""",
           """113""",
           """121""",
           """122""",
           """123""",
           """131""",
           """132""",
           """133""",
           """211""",
           """212""",
           """213""",
           """221""",
           """222""",
           """223""",
           """231""",
           """232""",
           """233""",
           """311""",
           """312""",
           """313""",
           """321""",
           """322""",
           """323""",
           """331""",
           """332""",
           """333""",
       )
       a			   ={}
       a			   ={}
       for letter, number in zip(UpperCAmelCase_   ,       UpperCAmelCase_      ):
              a			   =number
              a			   =letter

       return message, alphabet, character_to_number, number_to_character





def 							lowerCamelCase    (							UpperCAmelCase_	:      str   ,       UpperCAmelCase_	:      str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."   ,       UpperCAmelCase_	:      int = 5      )->			str:

       """simple docstring"""




       a   ,      a   ,      a   ,      a			   =__prepare(
           UpperCAmelCase_   ,       UpperCAmelCase_      )
       a   ,      a			   ="""""", """"""

       for i in range(0   ,       len(UpperCAmelCase_      ) + 1   ,       UpperCAmelCase_      ):
              encrypted_numeric += __encrypt_part(
                  message[i : i + period]   ,       UpperCAmelCase_      )

       for i in range(0   ,       len(UpperCAmelCase_      )   ,       3      ):
              encrypted += number_to_character[encrypted_numeric[i : i + 3]]

       return encrypted





def 							lowerCamelCase    (							UpperCAmelCase_	:      str   ,       UpperCAmelCase_	:      str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ."   ,       UpperCAmelCase_	:      int = 5      )->			str:

       """simple docstring"""




       a   ,      a   ,      a   ,      a			   =__prepare(
           UpperCAmelCase_   ,       UpperCAmelCase_      )
       a			   =[]
       a			   =""""""

       for i in range(0   ,       len(UpperCAmelCase_      ) + 1   ,       UpperCAmelCase_      ):
              a   ,      a   ,      a			   =__decrypt_part(message[i : i + period]   ,       UpperCAmelCase_      )

              for j in range(0   ,       len(UpperCAmelCase_      )      ):
                     decrypted_numeric.append(a[j] + b[j] + c[j]      )

       for each in decrypted_numeric:
              decrypted += number_to_character[each]

       return decrypted


if __name__ == "__main__":
    _lowerCamelCase							=      '''DEFEND THE EAST WALL OF THE CASTLE.'''
    _lowerCamelCase							=      encrypt_message(msg, '''EPSDUCVWYM.ZLKXNBTFGORIJHAQ''')
    _lowerCamelCase							=      decrypt_message(encrypted, '''EPSDUCVWYM.ZLKXNBTFGORIJHAQ''')
    print(f"""Encrypted: {encrypted}\nDecrypted: {decrypted}""")
