'''simple docstring'''




import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel




class    a__				:






  def __init__(			self		:		List[str]  ,    a		:		Optional[Any]  ,    a		:		Optional[int]=13  ,    a		:		List[Any]=7  ,    a		:		Dict=True  ,    a		:		str=True  ,    a		:		Optional[int]=False  ,    a		:		Optional[Any]=True  ,    a		:		int=99  ,    a		:		Optional[int]=32  ,    a		:		Tuple=5  ,    a		:		Union[str, Any]=4  ,    a		:		int=37  ,    a		:		Union[str, Any]="gelu"  ,    a		:		Tuple=0.1  ,    a		:		Optional[Any]=0.1  ,    a		:		Any=5_12  ,    a		:		List[Any]=16  ,    a		:		Dict=2  ,    a		:		Union[str, Any]=0.02  ,    a		:		Dict=3  ,    a		:		Union[str, Any]=4  ,    a		:		int=None  ,    ):





      """simple docstring"""



      __lowerCamelCase          =  parent
      __lowerCamelCase          =  batch_size
      __lowerCamelCase          =  seq_length
      __lowerCamelCase          =  is_training
      __lowerCamelCase          =  use_input_mask
      __lowerCamelCase          =  use_token_type_ids
      __lowerCamelCase          =  use_labels
      __lowerCamelCase          =  vocab_size
      __lowerCamelCase          =  hidden_size
      __lowerCamelCase          =  num_hidden_layers
      __lowerCamelCase          =  num_attention_heads
      __lowerCamelCase          =  intermediate_size
      __lowerCamelCase          =  hidden_act
      __lowerCamelCase          =  hidden_dropout_prob
      __lowerCamelCase          =  attention_probs_dropout_prob
      __lowerCamelCase          =  max_position_embeddings
      __lowerCamelCase          =  type_vocab_size
      __lowerCamelCase          =  type_sequence_label_size
      __lowerCamelCase          =  initializer_range
      __lowerCamelCase          =  num_labels
      __lowerCamelCase          =  num_choices
      __lowerCamelCase          =  scope






  def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]   ):





      """simple docstring"""



      __lowerCamelCase          =  ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size   )

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

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

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

      __lowerCamelCase          =  self.get_config()

      return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






  def        SCREAMING_SNAKE_CASE__       (			self		:		str   ):





      """simple docstring"""



      return OpenLlamaConfig(
          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=a  ,    initializer_range=self.initializer_range  ,    use_stable_embedding=a  ,    )






  def        SCREAMING_SNAKE_CASE__       (			self		:		int  ,    a		:		str  ,    a		:		Union[str, Any]  ,    a		:		Tuple  ,    a		:		List[str]  ,    a		:		str  ,    a		:		Dict  ,    a		:		Tuple   ):





      """simple docstring"""



      __lowerCamelCase          =  OpenLlamaModel(config=a   )
      model.to(a   )
      model.eval()
      __lowerCamelCase          =  model(a  ,    attention_mask=a   )
      __lowerCamelCase          =  model(a   )
      self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		str  ,    a		:		Optional[Any]  ,    a		:		List[str]  ,    a		:		Optional[int]  ,    a		:		Union[str, Any]  ,    a		:		Dict  ,    a		:		Union[str, Any]  ,    a		:		Optional[Any]  ,    a		:		Tuple  ,    a		:		Tuple  ,    ):





      """simple docstring"""



      __lowerCamelCase          =  True
      __lowerCamelCase          =  OpenLlamaModel(a   )
      model.to(a   )
      model.eval()
      __lowerCamelCase          =  model(
          a  ,    attention_mask=a  ,    encoder_hidden_states=a  ,    encoder_attention_mask=a  ,    )
      __lowerCamelCase          =  model(
          a  ,    attention_mask=a  ,    encoder_hidden_states=a  ,    )
      __lowerCamelCase          =  model(a  ,    attention_mask=a   )
      self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		Dict  ,    a		:		int  ,    a		:		Tuple  ,    a		:		Optional[int]  ,    a		:		Optional[int]  ,    a		:		List[str]  ,    a		:		str  ,    a		:		Tuple  ,    a		:		Dict  ,    a		:		Optional[Any]  ,    ):





      """simple docstring"""



      __lowerCamelCase          =  OpenLlamaForCausalLM(config=a   )
      model.to(a   )
      model.eval()
      __lowerCamelCase          =  model(a  ,    attention_mask=a  ,    labels=a   )
      self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.vocab_size)   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		Any  ,    a		:		Union[str, Any]  ,    a		:		Tuple  ,    a		:		List[Any]  ,    a		:		List[Any]  ,    a		:		Any  ,    a		:		List[Any]  ,    a		:		List[Any]  ,    a		:		Union[str, Any]  ,    a		:		Tuple  ,    ):





      """simple docstring"""



      __lowerCamelCase          =  True
      __lowerCamelCase          =  True
      __lowerCamelCase          =  OpenLlamaForCausalLM(config=a   )
      model.to(a   )
      model.eval()

      # first forward pass
      __lowerCamelCase          =  model(
          a  ,    attention_mask=a  ,    encoder_hidden_states=a  ,    encoder_attention_mask=a  ,    use_cache=a  ,    )
      __lowerCamelCase          =  outputs.past_key_values

      # create hypothetical multiple next token and extent to next_input_ids
      __lowerCamelCase          =  ids_tensor((self.batch_size, 3)  ,    config.vocab_size   )
      __lowerCamelCase          =  ids_tensor((self.batch_size, 3)  ,    vocab_size=2   )

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

      __lowerCamelCase          =  model(
          a  ,    attention_mask=a  ,    encoder_hidden_states=a  ,    encoder_attention_mask=a  ,    output_hidden_states=a  ,    )["""hidden_states"""][0]
      __lowerCamelCase          =  model(
          a  ,    attention_mask=a  ,    encoder_hidden_states=a  ,    encoder_attention_mask=a  ,    past_key_values=a  ,    output_hidden_states=a  ,    )["""hidden_states"""][0]

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

      self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

      # test that outputs are equal for slice
      self.parent.assertTrue(torch.allclose(a  ,    a  ,    atol=1e-3   )   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





      """simple docstring"""



      __lowerCamelCase          =  self.prepare_config_and_inputs()
      (
          __lowerCamelCase						
      )          =  config_and_inputs
      __lowerCamelCase          =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
      return config, inputs_dict




@require_torch
class    a__				(       __UpperCamelCase							,       __UpperCamelCase							,       __UpperCamelCase							,       unittest.TestCase			):
  lowerCamelCase  :					List[str]							=(
      (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
  )
  lowerCamelCase  :					Tuple							=(OpenLlamaForCausalLM,) if is_torch_available() else ()
  lowerCamelCase  :					List[str]							=(
      {
          """feature-extraction""": OpenLlamaModel,
          """text-classification""": OpenLlamaForSequenceClassification,
          """text-generation""": OpenLlamaForCausalLM,
          """zero-shot""": OpenLlamaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowerCamelCase  :					Dict							=False
  lowerCamelCase  :					Union[str, Any]							=False






  def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





      """simple docstring"""



      __lowerCamelCase          =  OpenLlamaModelTester(self   )
      __lowerCamelCase          =  ConfigTester(self  ,    config_class=a  ,    hidden_size=37   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





      """simple docstring"""



      self.config_tester.run_common_tests()






  def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





      """simple docstring"""



      __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*a   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		Any   ):





      """simple docstring"""



      __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
          __lowerCamelCase          =  type
          self.model_tester.create_and_check_model(*a   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		Any   ):





      """simple docstring"""



      __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()
      __lowerCamelCase          =  3
      __lowerCamelCase          =  input_dict["""input_ids"""]
      __lowerCamelCase          =  input_ids.ne(1   ).to(a   )
      __lowerCamelCase          =  ids_tensor([self.model_tester.batch_size]  ,    self.model_tester.type_sequence_label_size   )
      __lowerCamelCase          =  OpenLlamaForSequenceClassification(a   )
      model.to(a   )
      model.eval()
      __lowerCamelCase          =  model(a  ,    attention_mask=a  ,    labels=a   )
      self.assertEqual(result.logits.shape  ,    (self.model_tester.batch_size, self.model_tester.num_labels)   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





      """simple docstring"""



      __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()
      __lowerCamelCase          =  3
      __lowerCamelCase          =  """single_label_classification"""
      __lowerCamelCase          =  input_dict["""input_ids"""]
      __lowerCamelCase          =  input_ids.ne(1   ).to(a   )
      __lowerCamelCase          =  ids_tensor([self.model_tester.batch_size]  ,    self.model_tester.type_sequence_label_size   )
      __lowerCamelCase          =  OpenLlamaForSequenceClassification(a   )
      model.to(a   )
      model.eval()
      __lowerCamelCase          =  model(a  ,    attention_mask=a  ,    labels=a   )
      self.assertEqual(result.logits.shape  ,    (self.model_tester.batch_size, self.model_tester.num_labels)   )






  def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]   ):





      """simple docstring"""



      __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()
      __lowerCamelCase          =  3
      __lowerCamelCase          =  """multi_label_classification"""
      __lowerCamelCase          =  input_dict["""input_ids"""]
      __lowerCamelCase          =  input_ids.ne(1   ).to(a   )
      __lowerCamelCase          =  ids_tensor(
          [self.model_tester.batch_size, config.num_labels]  ,    self.model_tester.type_sequence_label_size   ).to(torch.float   )
      __lowerCamelCase          =  OpenLlamaForSequenceClassification(a   )
      model.to(a   )
      model.eval()
      __lowerCamelCase          =  model(a  ,    attention_mask=a  ,    labels=a   )
      self.assertEqual(result.logits.shape  ,    (self.model_tester.batch_size, self.model_tester.num_labels)   )






  @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test'''   )
  def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





      """simple docstring"""



      pass






  @parameterized.expand([('''linear''',), ('''dynamic''',)]   )
  def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]  ,    a		:		Optional[int]   ):





      """simple docstring"""



      __lowerCamelCase          =  self.model_tester.prepare_config_and_inputs_for_common()
      __lowerCamelCase          =  ids_tensor([1, 10]  ,    config.vocab_size   )
      __lowerCamelCase          =  ids_tensor([1, int(config.max_position_embeddings * 1.5   )]  ,    config.vocab_size   )

      set_seed(42   )  # Fixed seed at init time so the two models get the same random weights
      __lowerCamelCase          =  OpenLlamaModel(a   )
      original_model.to(a   )
      original_model.eval()
      __lowerCamelCase          =  original_model(a   ).last_hidden_state
      __lowerCamelCase          =  original_model(a   ).last_hidden_state

      set_seed(42   )  # Fixed seed at init time so the two models get the same random weights
      __lowerCamelCase          =  {"""type""": scaling_type, """factor""": 10.0}
      __lowerCamelCase          =  OpenLlamaModel(a   )
      scaled_model.to(a   )
      scaled_model.eval()
      __lowerCamelCase          =  scaled_model(a   ).last_hidden_state
      __lowerCamelCase          =  scaled_model(a   ).last_hidden_state

      # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
      # maximum sequence length, so the outputs for the short input should match.
      if scaling_type == "dynamic":
          self.assertTrue(torch.allclose(a  ,    a  ,    atol=1e-5   )   )
      else:
          self.assertFalse(torch.allclose(a  ,    a  ,    atol=1e-5   )   )

      # The output should be different for long inputs
      self.assertFalse(torch.allclose(a  ,    a  ,    atol=1e-5   )   )

'''simple docstring'''




__UpperCAmelCase												="ABCDEFGHIJKLMNOPQRSTUVWXYZ"






def __lowerCAmelCase			(							)     ->   None:
    __lowerCamelCase          =  input('''Enter message: '''    )
    __lowerCamelCase          =  input('''Enter key [alphanumeric]: '''    )
    __lowerCamelCase          =  input('''Encrypt/Decrypt [e/d]: '''    )

    if mode.lower().startswith('''e'''    ):
        __lowerCamelCase          =  '''encrypt'''
        __lowerCamelCase          =  encrypt_message(UpperCamelCase__    ,  UpperCamelCase__    )
    elif mode.lower().startswith('''d'''    ):
        __lowerCamelCase          =  '''decrypt'''
        __lowerCamelCase          =  decrypt_message(UpperCamelCase__    ,  UpperCamelCase__    )

    print(f"""\n{mode.title()}ed message:"""    )
    print(UpperCamelCase__    )






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    )     ->   str:
    return translate_message(UpperCamelCase__    ,  UpperCamelCase__    ,  '''encrypt'''    )






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    )     ->   str:
    return translate_message(UpperCamelCase__    ,  UpperCamelCase__    ,  '''decrypt'''    )






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__    )     ->   str:
    __lowerCamelCase          =  []
    __lowerCamelCase          =  0
    __lowerCamelCase          =  key.upper()

    for symbol in message:
        __lowerCamelCase          =  LETTERS.find(symbol.upper()    )
        if num != -1:
            if mode == "encrypt":
                num += LETTERS.find(key[key_index]    )
            elif mode == "decrypt":
                num -= LETTERS.find(key[key_index]    )

            num %= len(UpperCamelCase__    )

            if symbol.isupper():
                translated.append(LETTERS[num]    )
            elif symbol.islower():
                translated.append(LETTERS[num].lower()    )

            key_index += 1
            if key_index == len(UpperCamelCase__    ):
                __lowerCamelCase          =  0
        else:
            translated.append(UpperCamelCase__    )
    return "".join(UpperCamelCase__    )


if __name__ == "__main__":
  main()
