






'''simple docstring'''



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




      def __init__(	self			, lowerCamelCase			, lowerCamelCase=13			, lowerCamelCase=7			, lowerCamelCase=True			, lowerCamelCase=True			, lowerCamelCase=99			, lowerCamelCase=32			, lowerCamelCase=5			, lowerCamelCase=4			, lowerCamelCase=37			, lowerCamelCase="gelu"			, lowerCamelCase=0.1			, lowerCamelCase=0.1			, lowerCamelCase=50			, lowerCamelCase=0.02			, lowerCamelCase=True			, lowerCamelCase=None			, )				->Union[str, Any]:



         '''simple docstring'''
         __a					  =     parent
         __a					  =     batch_size
         __a					  =     seq_length
         __a					  =     is_training
         __a					  =     use_input_mask
         __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					  =     initializer_range
         __a					  =     use_labels
         __a					  =     scope




      def 			__UpperCamelCase				(	self   )				->Any:



         '''simple docstring'''
         __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]   )

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

         __a					  =     self.get_config()

         return config, input_ids, input_mask, token_labels




      def 			__UpperCamelCase				(	self   )				->Tuple:



         '''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=lowerCamelCase			, initializer_range=self.initializer_range			, )




      def 			__UpperCamelCase				(	self   )				->int:



         '''simple docstring'''
         (
             (
             __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,
             input_mask,
             token_labels,
             encoder_hidden_states,
             encoder_attention_mask,
         )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, **lowerCamelCase			, )				->Optional[Any]:



         '''simple docstring'''
         __a					  =     BertGenerationEncoder(config=lowerCamelCase   )
         model.to(lowerCamelCase   )
         model.eval()
         __a					  =     model(lowerCamelCase			, attention_mask=lowerCamelCase   )
         __a					  =     model(lowerCamelCase   )
         self.parent.assertEqual(result.last_hidden_state.shape			, (self.batch_size, self.seq_length, self.hidden_size)   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, **lowerCamelCase			, )				->List[Any]:



         '''simple docstring'''
         __a					  =     True
         __a					  =     BertGenerationEncoder(config=lowerCamelCase   )
         model.to(lowerCamelCase   )
         model.eval()
         __a					  =     model(
             lowerCamelCase			, attention_mask=lowerCamelCase			, encoder_hidden_states=lowerCamelCase			, encoder_attention_mask=lowerCamelCase			, )
         __a					  =     model(
             lowerCamelCase			, attention_mask=lowerCamelCase			, encoder_hidden_states=lowerCamelCase			, )
         self.parent.assertEqual(result.last_hidden_state.shape			, (self.batch_size, self.seq_length, self.hidden_size)   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, **lowerCamelCase			, )				->Union[str, Any]:



         '''simple docstring'''
         __a					  =     True
         __a					  =     True
         __a					  =     BertGenerationDecoder(config=lowerCamelCase   ).to(lowerCamelCase   ).eval()

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

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

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

         __a					  =     model(
             lowerCamelCase			, attention_mask=lowerCamelCase			, encoder_hidden_states=lowerCamelCase			, encoder_attention_mask=lowerCamelCase			, output_hidden_states=lowerCamelCase			, )['hidden_states'][0]
         __a					  =     model(
             lowerCamelCase			, attention_mask=lowerCamelCase			, encoder_hidden_states=lowerCamelCase			, encoder_attention_mask=lowerCamelCase			, past_key_values=lowerCamelCase			, output_hidden_states=lowerCamelCase			, )['hidden_states'][0]

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




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, *lowerCamelCase			, )				->str:



         '''simple docstring'''
         __a					  =     BertGenerationDecoder(lowerCamelCase   )
         model.to(lowerCamelCase   )
         model.eval()
         __a					  =     model(lowerCamelCase			, attention_mask=lowerCamelCase			, labels=lowerCamelCase   )
         self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.seq_length, self.vocab_size)   )






      def 			__UpperCamelCase				(	self   )				->int:



         '''simple docstring'''
         __a  ,      __a  ,      __a  ,      __a					  =     self.prepare_config_and_inputs()
         __a					  =     {'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict







@require_torch
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase   ,   _lowerCAmelCase   ,   _lowerCAmelCase   ,   unittest.TestCase  ):
      __a	       =(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
      __a	       =(BertGenerationDecoder,) if is_torch_available() else ()
      __a	       =(
          {"feature-extraction": BertGenerationEncoder, "text-generation": BertGenerationDecoder}
          if is_torch_available()
          else {}
      )




      def 			__UpperCamelCase				(	self   )				->List[Any]:



         '''simple docstring'''
         __a					  =     BertGenerationEncoderTester(self   )
         __a					  =     ConfigTester(self			, config_class=lowerCamelCase			, hidden_size=37   )




      def 			__UpperCamelCase				(	self   )				->Tuple:



         '''simple docstring'''
         self.config_tester.run_common_tests()




      def 			__UpperCamelCase				(	self   )				->Optional[int]:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowerCamelCase   )




      def 			__UpperCamelCase				(	self   )				->Any:



         '''simple docstring'''
         __a  ,      __a  ,      __a  ,      __a					  =     self.model_tester.prepare_config_and_inputs()
         __a					  =     'bert'
         self.model_tester.create_and_check_model(lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )




      def 			__UpperCamelCase				(	self   )				->int:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(*lowerCamelCase   )




      def 			__UpperCamelCase				(	self   )				->List[Any]:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(*lowerCamelCase   )




      def 			__UpperCamelCase				(	self   )				->Tuple:



         '''simple docstring'''
         # This regression test was failing with PyTorch < 1.3
         (
             (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      (
             __a
         )  ,      
         )					  =     self.model_tester.prepare_config_and_inputs_for_decoder()

         __a					  =     None

         self.model_tester.create_and_check_model_as_decoder(
             lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, )




      def 			__UpperCamelCase				(	self   )				->Optional[int]:



         '''simple docstring'''
         __a					  =     self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase   )






      @slow
      def 			__UpperCamelCase				(	self   )				->Optional[Any]:



         '''simple docstring'''
         __a					  =     BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'   )
         self.assertIsNotNone(lowerCamelCase   )







@require_torch
class 				__SCREAMING_SNAKE_CASE							(				unittest.TestCase  ):




      @slow
      def 			__UpperCamelCase				(	self   )				->Optional[int]:



         '''simple docstring'''
         __a					  =     BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'   )
         __a					  =     torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]   )
         with torch.no_grad():
            __a					  =     model(lowerCamelCase   )[0]
         __a					  =     torch.Size([1, 8, 1024]   )
         self.assertEqual(output.shape			, lowerCamelCase   )
         __a					  =     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]			, lowerCamelCase			, atol=1e-4   )   )







@require_torch
class 				__SCREAMING_SNAKE_CASE							(				unittest.TestCase  ):




      @slow
      def 			__UpperCamelCase				(	self   )				->str:



         '''simple docstring'''
         __a					  =     BertGenerationDecoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'   )
         __a					  =     torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]   )
         with torch.no_grad():
            __a					  =     model(lowerCamelCase   )[0]
         __a					  =     torch.Size([1, 8, 5_0358]   )
         self.assertEqual(output.shape			, lowerCamelCase   )
         __a					  =     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]			, lowerCamelCase			, atol=1e-4   )   )







'''simple docstring'''



from timeit import timeit

__UpperCamelCase   :							int         =   {
    """MALAYALAM""": True,
    """String""": False,
    """rotor""": True,
    """level""": True,
    """A""": True,
    """BB""": True,
    """ABC""": False,
    """amanaplanacanalpanama""": True,  # "a man a plan a canal panama"
}
# Ensure our test data is valid
assert all((key == key[::-1]) is value for key, value in test_data.items())



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						str     )	->      bool:

   """simple docstring"""
   __a					  =     0
   __a					  =     len(SCREAMING_SNAKE_CASE__     ) - 1
   while start_i < end_i:
      if s[start_i] == s[end_i]:
         start_i += 1
         end_i -= 1
      else:
         return False
   return True



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						str     )	->      bool:

   """simple docstring"""
   __a					  =     len(SCREAMING_SNAKE_CASE__     ) // 2
   __a					  =     len(SCREAMING_SNAKE_CASE__     )

   # We need to traverse till half of the length of string
   # as we can get access of the i'th last element from
   # i'th index.
   # eg: [0,1,2,3,4,5] => 4th index can be accessed
   # with the help of 1st index (i==n-i-1)
   # where n is length of string
   return all(s[i] == s[n - i - 1] for i in range(SCREAMING_SNAKE_CASE__     )     )



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						str     )	->      bool:

   """simple docstring"""
   if len(SCREAMING_SNAKE_CASE__     ) <= 2:
      return True
   if s[0] == s[len(SCREAMING_SNAKE_CASE__     ) - 1]:
      return is_palindrome_recursive(s[1:-1]     )
   else:
      return False



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						str     )	->      bool:

   """simple docstring"""
   return s == s[::-1]



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						str     )	->      None:

   """simple docstring"""
   __a					  =     f"""all({name}(key) is value for key, value in test_data.items())"""
   __a					  =     f"""from __main__ import test_data, {name}"""
   __a					  =     500000
   __a					  =     timeit(stmt=SCREAMING_SNAKE_CASE__,       setup=SCREAMING_SNAKE_CASE__,       number=SCREAMING_SNAKE_CASE__     )
   print(f"""{name:<35} finished {number:,} runs in {result:.5f} seconds"""     )


if __name__ == "__main__":
 for key, value in test_data.items():
  assert is_palindrome(key) is is_palindrome_recursive(key)
  assert is_palindrome(key) is is_palindrome_slice(key)
  print(f"""{key:21} {value}""")
 print("""a man a plan a canal panama""")

 # finished 500,000 runs in 0.46793 seconds
 benchmark_function("""is_palindrome_slice""")
 # finished 500,000 runs in 0.85234 seconds
 benchmark_function("""is_palindrome""")
 # finished 500,000 runs in 1.32028 seconds
 benchmark_function("""is_palindrome_recursive""")
 # finished 500,000 runs in 2.08679 seconds
 benchmark_function("""is_palindrome_traversal""")