





import unittest

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

from ...generation.test_utils import GenerationTesterMixin
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 BertGenerationDecoder, BertGenerationEncoder





class  lowerCAmelCase							:







  def __init__(      self  :Dict   ,							_lowercase  :Optional[Any]   ,							_lowercase  :Optional[Any]=13   ,							_lowercase  :Optional[int]=7   ,							_lowercase  :Tuple=True   ,							_lowercase  :int=True   ,							_lowercase  :List[str]=99   ,							_lowercase  :Union[str, Any]=32   ,							_lowercase  :Tuple=5   ,							_lowercase  :List[Any]=4   ,							_lowercase  :Optional[int]=37   ,							_lowercase  :int="gelu"   ,							_lowercase  :List[str]=0.1   ,							_lowercase  :Any=0.1   ,							_lowercase  :Dict=50   ,							_lowercase  :Dict=0.02   ,							_lowercase  :int=True   ,							_lowercase  :int=None   ,							):






     '''simple docstring'''





     lowercase__			       =  parent
     lowercase__			       =  batch_size
     lowercase__			       =  seq_length
     lowercase__			       =  is_training
     lowercase__			       =  use_input_mask
     lowercase__			       =  vocab_size
     lowercase__			       =  hidden_size
     lowercase__			       =  num_hidden_layers
     lowercase__			       =  num_attention_heads
     lowercase__			       =  intermediate_size
     lowercase__			       =  hidden_act
     lowercase__			       =  hidden_dropout_prob
     lowercase__			       =  attention_probs_dropout_prob
     lowercase__			       =  max_position_embeddings
     lowercase__			       =  initializer_range
     lowercase__			       =  use_labels
     lowercase__			       =  scope







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





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

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

     if self.use_labels:
        lowercase__			       =  ids_tensor([self.batch_size, self.seq_length]   ,							self.vocab_size )

     lowercase__			       =  self.get_config()

     return config, input_ids, input_mask, token_labels







  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     return BertGenerationConfig(
         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   ,							is_decoder=_lowercase   ,							initializer_range=self.initializer_range   ,							)







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     (
         (
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							
     )			       =  self.prepare_config_and_inputs()

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

     return (
         config,
         input_ids,
         input_mask,
         token_labels,
         encoder_hidden_states,
         encoder_attention_mask,
     )







  def   UpperCAmelCase					(      self  :str   ,							_lowercase  :List[Any]   ,							_lowercase  :Tuple   ,							_lowercase  :Dict   ,							_lowercase  :Optional[int]   ,							**_lowercase  :List[Any]   ,							):






     '''simple docstring'''





     lowercase__			       =  BertGenerationEncoder(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(_lowercase   ,							attention_mask=_lowercase )
     lowercase__			       =  model(_lowercase )
     self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size) )







  def   UpperCAmelCase					(      self  :Optional[Any]   ,							_lowercase  :int   ,							_lowercase  :List[Any]   ,							_lowercase  :int   ,							_lowercase  :Optional[int]   ,							_lowercase  :List[str]   ,							_lowercase  :Optional[int]   ,							**_lowercase  :str   ,							):






     '''simple docstring'''





     lowercase__			       =  True
     lowercase__			       =  BertGenerationEncoder(config=_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(
         _lowercase   ,							attention_mask=_lowercase   ,							encoder_hidden_states=_lowercase   ,							encoder_attention_mask=_lowercase   ,							)
     lowercase__			       =  model(
         _lowercase   ,							attention_mask=_lowercase   ,							encoder_hidden_states=_lowercase   ,							)
     self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size) )







  def   UpperCAmelCase					(      self  :Any   ,							_lowercase  :List[Any]   ,							_lowercase  :int   ,							_lowercase  :Tuple   ,							_lowercase  :List[str]   ,							_lowercase  :Tuple   ,							_lowercase  :Optional[int]   ,							**_lowercase  :List[str]   ,							):






     '''simple docstring'''





     lowercase__			       =  True
     lowercase__			       =  True
     lowercase__			       =  BertGenerationDecoder(config=_lowercase ).to(_lowercase ).eval()

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

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

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

     lowercase__			       =  model(
         _lowercase   ,							attention_mask=_lowercase   ,							encoder_hidden_states=_lowercase   ,							encoder_attention_mask=_lowercase   ,							output_hidden_states=_lowercase   ,							)["hidden_states"][0]
     lowercase__			       =  model(
         _lowercase   ,							attention_mask=_lowercase   ,							encoder_hidden_states=_lowercase   ,							encoder_attention_mask=_lowercase   ,							past_key_values=_lowercase   ,							output_hidden_states=_lowercase   ,							)["hidden_states"][0]

     # select random slice
     lowercase__			       =  ids_tensor((1,)   ,							output_from_past.shape[-1] ).item()
     lowercase__			       =  output_from_no_past[:, -3:, random_slice_idx].detach()
     lowercase__			       =  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(_lowercase   ,							_lowercase   ,							atol=1e-3 ) )







  def   UpperCAmelCase					(      self  :List[Any]   ,							_lowercase  :Dict   ,							_lowercase  :Any   ,							_lowercase  :Optional[int]   ,							_lowercase  :Any   ,							*_lowercase  :Dict   ,							):






     '''simple docstring'''





     lowercase__			       =  BertGenerationDecoder(_lowercase )
     model.to(_lowercase )
     model.eval()
     lowercase__			       =  model(_lowercase   ,							attention_mask=_lowercase   ,							labels=_lowercase )
     self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length, self.vocab_size) )







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





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





@require_torch
class  lowerCAmelCase							(       lowercase_							,							lowercase_							,							lowercase_							,							unittest.TestCase ):
  __lowerCamelCase		=      (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
  __lowerCamelCase		=      (BertGenerationDecoder,) if is_torch_available() else ()
  __lowerCamelCase		=      (
      {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
      if is_torch_available()
      else {}
  )







  def   UpperCAmelCase					(      self  :Tuple ):






     '''simple docstring'''





     lowercase__			       =  BertGenerationEncoderTester(self )
     lowercase__			       =  ConfigTester(self   ,							config_class=_lowercase   ,							hidden_size=37 )







  def   UpperCAmelCase					(      self  :Any ):






     '''simple docstring'''





     self.config_tester.run_common_tests()







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowercase )







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     lowercase__       ,							lowercase__       ,							lowercase__       ,							lowercase__			       =  self.model_tester.prepare_config_and_inputs()
     lowercase__			       =  "bert"
     self.model_tester.create_and_check_model(_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase )







  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(*_lowercase )







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_decoder_model_past_large_inputs(*_lowercase )







  def   UpperCAmelCase					(      self  :Tuple ):






     '''simple docstring'''





     (
         (
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							(
         lowercase__
     )       ,							
     )			       =  self.model_tester.prepare_config_and_inputs_for_decoder()

     lowercase__			       =  None

     self.model_tester.create_and_check_model_as_decoder(
         _lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							_lowercase   ,							)







  def   UpperCAmelCase					(      self  :Optional[int] ):






     '''simple docstring'''





     lowercase__			       =  self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_for_causal_lm(*_lowercase )







  @slow
  def   UpperCAmelCase					(      self  :List[str] ):






     '''simple docstring'''





     lowercase__			       =  BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" )
     self.assertIsNotNone(_lowercase )





@require_torch
class  lowerCAmelCase							(       unittest.TestCase ):







  @slow
  def   UpperCAmelCase					(      self  :Optional[int] ):






     '''simple docstring'''





     lowercase__			       =  BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" )
     lowercase__			       =  torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]] )
     with torch.no_grad():
        lowercase__			       =  model(_lowercase )[0]
     lowercase__			       =  torch.Size([1, 8, 10_24] )
     self.assertEqual(output.shape   ,							_lowercase )
     lowercase__			       =  torch.tensor(
         [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]] )
     self.assertTrue(torch.allclose(output[:, :3, :3]   ,							_lowercase   ,							atol=1e-4 ) )





@require_torch
class  lowerCAmelCase							(       unittest.TestCase ):







  @slow
  def   UpperCAmelCase					(      self  :Tuple ):






     '''simple docstring'''





     lowercase__			       =  BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" )
     lowercase__			       =  torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]] )
     with torch.no_grad():
        lowercase__			       =  model(_lowercase )[0]
     lowercase__			       =  torch.Size([1, 8, 5_03_58] )
     self.assertEqual(output.shape   ,							_lowercase )
     lowercase__			       =  torch.tensor(
         [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]] )
     self.assertTrue(torch.allclose(output[:, :3, :3]   ,							_lowercase   ,							atol=1e-4 ) )













def _A       (   __magic_name__	):
   lowercase__			       =  ""
   for ch in key:
      if ch == " " or ch not in key_no_dups and ch.isalpha():
         key_no_dups += ch
   return key_no_dups


def _A       (   __magic_name__	):
   lowercase__			       =  [chr(i + 65	) for i in range(26	)]
   # Remove duplicate characters from key
   lowercase__			       =  remove_duplicates(key.upper()	)
   lowercase__			       =  len(__magic_name__	)
   # First fill cipher with key characters
   lowercase__			       =  {alphabet[i]: char for i, char in enumerate(__magic_name__	)}
   # Then map remaining characters in alphabet to
   # the alphabet from the beginning
   for i in range(len(__magic_name__	)  , 26	):
      lowercase__			       =  alphabet[i - offset]
      # Ensure we are not mapping letters to letters previously mapped
      while char in key:
         offset -= 1
         lowercase__			       =  alphabet[i - offset]
      lowercase__			       =  char
   return cipher_alphabet


def _A       (   __magic_name__  , __magic_name__	):
   return "".join(cipher_map.get(__magic_name__  , __magic_name__	) for ch in message.upper()	)


def _A       (   __magic_name__  , __magic_name__	):
   lowercase__			       =  {v: k for k, v in cipher_map.items()}
   return "".join(rev_cipher_map.get(__magic_name__  , __magic_name__	) for ch in message.upper()	)


def _A       (   ):
   lowercase__			       =  input("Enter message to encode or decode: "	).strip()
   lowercase__			       =  input("Enter keyword: "	).strip()
   lowercase__			       =  input("Encipher or decipher? E/D:"	).strip()[0].lower()
   try:
      lowercase__			       =  {"e": encipher, "d": decipher}[option]
   except KeyError:
      raise KeyError("invalid input option"	)
   lowercase__			       =  create_cipher_map(__magic_name__	)
   print(func(__magic_name__  , __magic_name__	)	)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  main()






