






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





  '''simple docstring'''



  def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__=13 ,    UpperCamelCase__=7 ,    UpperCamelCase__=True ,    UpperCamelCase__=True ,    UpperCamelCase__=99 ,    UpperCamelCase__=32 ,    UpperCamelCase__=5 ,    UpperCamelCase__=4 ,    UpperCamelCase__=37 ,    UpperCamelCase__="gelu" ,    UpperCamelCase__=0.1 ,    UpperCamelCase__=0.1 ,    UpperCamelCase__=50 ,    UpperCamelCase__=0.02 ,    UpperCamelCase__=True ,    UpperCamelCase__=None ,    ):
      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   parent
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   batch_size
      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   seq_length
      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   is_training
      SCREAMING_SNAKE_CASE_   :	int													=   use_input_mask
      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   vocab_size
      SCREAMING_SNAKE_CASE_   :	int													=   hidden_size
      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   num_hidden_layers
      SCREAMING_SNAKE_CASE_   :	int													=   num_attention_heads
      SCREAMING_SNAKE_CASE_   :	Dict													=   intermediate_size
      SCREAMING_SNAKE_CASE_   :	List[str]													=   hidden_act
      SCREAMING_SNAKE_CASE_   :	int													=   hidden_dropout_prob
      SCREAMING_SNAKE_CASE_   :	str													=   attention_probs_dropout_prob
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   max_position_embeddings
      SCREAMING_SNAKE_CASE_   :	List[str]													=   initializer_range
      SCREAMING_SNAKE_CASE_   :	int													=   use_labels
      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   scope


  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size       )

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

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

      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.get_config()

      return config, input_ids, input_mask, token_labels


  def   __snake_case			(						self       ):
      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=UpperCamelCase__ ,    initializer_range=self.initializer_range ,    )


  def   __snake_case			(						self       ):
      (
          SCREAMING_SNAKE_CASE_       
      )   :	int													=   self.prepare_config_and_inputs()

      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   True
      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
      SCREAMING_SNAKE_CASE_   :	str													=   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   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    **UpperCamelCase__ ,    ):
      SCREAMING_SNAKE_CASE_   :	Optional[int]													=   BertGenerationEncoder(config=UpperCamelCase__       )
      model.to(UpperCamelCase__       )
      model.eval()
      SCREAMING_SNAKE_CASE_   :	List[str]													=   model(UpperCamelCase__ ,    attention_mask=UpperCamelCase__       )
      SCREAMING_SNAKE_CASE_   :	List[str]													=   model(UpperCamelCase__       )
      self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size)       )


  def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    **UpperCamelCase__ ,    ):
      SCREAMING_SNAKE_CASE_   :	List[str]													=   True
      SCREAMING_SNAKE_CASE_   :	str													=   BertGenerationEncoder(config=UpperCamelCase__       )
      model.to(UpperCamelCase__       )
      model.eval()
      SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   model(
          UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    encoder_hidden_states=UpperCamelCase__ ,    encoder_attention_mask=UpperCamelCase__ ,    )
      SCREAMING_SNAKE_CASE_   :	Dict													=   model(
          UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    encoder_hidden_states=UpperCamelCase__ ,    )
      self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size)       )


  def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    **UpperCamelCase__ ,    ):
      SCREAMING_SNAKE_CASE_   :	List[str]													=   True
      SCREAMING_SNAKE_CASE_   :	Tuple													=   True
      SCREAMING_SNAKE_CASE_   :	Dict													=   BertGenerationDecoder(config=UpperCamelCase__       ).to(UpperCamelCase__       ).eval()

      # first forward pass
      SCREAMING_SNAKE_CASE_   :	int													=   model(
          UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    encoder_hidden_states=UpperCamelCase__ ,    encoder_attention_mask=UpperCamelCase__ ,    use_cache=UpperCamelCase__ ,    )
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   outputs.past_key_values

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

      # append to next input_ids and
      SCREAMING_SNAKE_CASE_   :	Tuple													=   torch.cat([input_ids, next_tokens] ,    dim=-1       )
      SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   torch.cat([input_mask, next_mask] ,    dim=-1       )

      SCREAMING_SNAKE_CASE_   :	int													=   model(
          UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    encoder_hidden_states=UpperCamelCase__ ,    encoder_attention_mask=UpperCamelCase__ ,    output_hidden_states=UpperCamelCase__ ,    )['hidden_states'][0]
      SCREAMING_SNAKE_CASE_   :	List[str]													=   model(
          UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    encoder_hidden_states=UpperCamelCase__ ,    encoder_attention_mask=UpperCamelCase__ ,    past_key_values=UpperCamelCase__ ,    output_hidden_states=UpperCamelCase__ ,    )['hidden_states'][0]

      # select random slice
      SCREAMING_SNAKE_CASE_   :	Tuple													=   ids_tensor((1,) ,    output_from_past.shape[-1]       ).item()
      SCREAMING_SNAKE_CASE_   :	List[str]													=   output_from_no_past[:, -3:, random_slice_idx].detach()
      SCREAMING_SNAKE_CASE_   :	int													=   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(UpperCamelCase__ ,    UpperCamelCase__ ,    atol=1E-3       )       )


  def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    *UpperCamelCase__ ,    ):
      SCREAMING_SNAKE_CASE_   :	Dict													=   BertGenerationDecoder(UpperCamelCase__       )
      model.to(UpperCamelCase__       )
      model.eval()
      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   model(UpperCamelCase__ ,    attention_mask=UpperCamelCase__ ,    labels=UpperCamelCase__       )
      self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)       )






  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	List[str]													=   self.prepare_config_and_inputs()
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   {'input_ids': input_ids, 'attention_mask': input_mask}
      return config, inputs_dict






@require_torch
class 							__a							(  __A ,	__A ,	__A ,	unittest.TestCase						):





  '''simple docstring'''



  UpperCAmelCase__      :				List[str]				    =						(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
  UpperCAmelCase__      :				Dict				    =						(BertGenerationDecoder,) if is_torch_available() else ()
  UpperCAmelCase__      :				Dict				    =						(
      {"""feature-extraction""": BertGenerationEncoder, """text-generation""": BertGenerationDecoder}
      if is_torch_available()
      else {}
  )


  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   BertGenerationEncoderTester(self       )
      SCREAMING_SNAKE_CASE_   :	Any													=   ConfigTester(self ,    config_class=UpperCamelCase__ ,    hidden_size=37       )


  def   __snake_case			(						self       ):
      self.config_tester.run_common_tests()


  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Tuple													=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCamelCase__       )


  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.model_tester.prepare_config_and_inputs()
      SCREAMING_SNAKE_CASE_   :	List[str]													=   'bert'
      self.model_tester.create_and_check_model(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       )


  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(*UpperCamelCase__       )


  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_decoder_model_past_large_inputs(*UpperCamelCase__       )


  def   __snake_case			(						self       ):
      # This regression test was failing with PyTorch < 1.3
      (
          SCREAMING_SNAKE_CASE_       
      )   :	List[str]													=   self.model_tester.prepare_config_and_inputs_for_decoder()

      SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   None

      self.model_tester.create_and_check_model_as_decoder(
          UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    )


  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Any													=   self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase__       )






  @slow
  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Any													=   BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'       )
      self.assertIsNotNone(UpperCamelCase__       )






@require_torch
class 							__a							(  unittest.TestCase						):





  '''simple docstring'''



  @slow
  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'       )
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]       )
      with torch.no_grad():
          SCREAMING_SNAKE_CASE_   :	List[Any]													=   model(UpperCamelCase__       )[0]
      SCREAMING_SNAKE_CASE_   :	List[str]													=   torch.Size([1, 8, 1024]       )
      self.assertEqual(output.shape ,    UpperCamelCase__       )
      SCREAMING_SNAKE_CASE_   :	int													=   torch.tensor(
          [[[0.17_75, 0.00_83, -0.03_21], [1.60_02, 0.12_87, 0.39_12], [2.14_73, 0.57_91, 0.60_66]]]       )
      self.assertTrue(torch.allclose(output[:, :3, :3] ,    UpperCamelCase__ ,    atol=1E-4       )       )






@require_torch
class 							__a							(  unittest.TestCase						):





  '''simple docstring'''



  @slow
  def   __snake_case			(						self       ):
      SCREAMING_SNAKE_CASE_   :	Dict													=   BertGenerationDecoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'       )
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]       )
      with torch.no_grad():
          SCREAMING_SNAKE_CASE_   :	int													=   model(UpperCamelCase__       )[0]
      SCREAMING_SNAKE_CASE_   :	int													=   torch.Size([1, 8, 50358]       )
      self.assertEqual(output.shape ,    UpperCamelCase__       )
      SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.tensor(
          [[[-0.57_88, -2.59_94, -3.70_54], [0.04_38, 4.79_97, 1.87_95], [1.58_62, 6.64_09, 4.46_38]]]       )
      self.assertTrue(torch.allclose(output[:, :3, :3] ,    UpperCamelCase__ ,    atol=1E-4       )       )







from timeit import timeit



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				if number < 0:
								raise ValueError('the value of input must not be negative'      )
				SCREAMING_SNAKE_CASE_   :	Tuple													=   0
				while number:
								number &= number - 1
								result += 1
				return result



def 					_lowerCamelCase(			lowerCAmelCase__				:		int      ):





				'''simple docstring'''







				if number < 0:
								raise ValueError('the value of input must not be negative'      )
				SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   0
				while number:
								if number % 2 == 1:
												result += 1
								number >>= 1
				return result



def 					_lowerCamelCase(			):





				'''simple docstring'''







				def do_benchmark(lowerCAmelCase__				:		int      ) -> None:
								SCREAMING_SNAKE_CASE_   :	int													=   'import __main__ as z'
								print(F'''Benchmark when {number = }:'''      )
								print(F'''{get_set_bits_count_using_modulo_operator(lowerCAmelCase__      ) = }'''      )
								SCREAMING_SNAKE_CASE_   :	int													=   timeit('z.get_set_bits_count_using_modulo_operator(25)'    ,    setup=lowerCAmelCase__      )
								print(F'''timeit() runs in {timing} seconds'''      )
								print(F'''{get_set_bits_count_using_brian_kernighans_algorithm(lowerCAmelCase__      ) = }'''      )
								SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   timeit(
								    'z.get_set_bits_count_using_brian_kernighans_algorithm(25)'    ,    setup=lowerCAmelCase__    ,    )
								print(F'''timeit() runs in {timing} seconds'''      )

				for number in (25, 37, 58, 0):
								do_benchmark(lowerCAmelCase__      )
								print()


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				benchmark()