





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					,			snake_case					,			snake_case=13					,			snake_case=7					,			snake_case=True					,			snake_case=True					,			snake_case=99					,			snake_case=32					,			snake_case=5					,			snake_case=4					,			snake_case=37					,			snake_case="gelu"					,			snake_case=0.1					,			snake_case=0.1					,			snake_case=50					,			snake_case=0.02					,			snake_case=True					,			snake_case=None					,			)				->  List[Any]:
      """simple docstring"""





      a__	:						int      		=  parent
      a__	:						Dict      		=  batch_size
      a__	:						List[str]      		=  seq_length
      a__	:						List[str]      		=  is_training
      a__	:						Optional[int]      		=  use_input_mask
      a__	:						List[str]      		=  vocab_size
      a__	:						Union[str, Any]      		=  hidden_size
      a__	:						Tuple      		=  num_hidden_layers
      a__	:						Tuple      		=  num_attention_heads
      a__	:						str      		=  intermediate_size
      a__	:						Union[str, Any]      		=  hidden_act
      a__	:						Any      		=  hidden_dropout_prob
      a__	:						List[str]      		=  attention_probs_dropout_prob
      a__	:						Dict      		=  max_position_embeddings
      a__	:						Union[str, Any]      		=  initializer_range
      a__	:						Optional[int]      		=  use_labels
      a__	:						Tuple      		=  scope





   def 	_snake_case ( self      )				->  Union[str, Any]:
      """simple docstring"""





      a__	:						Dict      		=  ids_tensor([self.batch_size, self.seq_length]					,			self.vocab_size      )

      a__	:						Optional[Any]      		=  None
      if self.use_input_mask:
         a__	:						str      		=  random_attention_mask([self.batch_size, self.seq_length]      )

      if self.use_labels:
         a__	:						Union[str, Any]      		=  ids_tensor([self.batch_size, self.seq_length]					,			self.vocab_size      )

      a__	:						str      		=  self.get_config()

      return config, input_ids, input_mask, token_labels





   def 	_snake_case ( self      )				->  str:
      """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=snake_case					,			initializer_range=self.initializer_range					,			)





   def 	_snake_case ( self      )				->  Optional[int]:
      """simple docstring"""





      (
          (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     
      )	:						Tuple      		=  self.prepare_config_and_inputs()

      a__	:						int      		=  True
      a__	:						Union[str, Any]      		=  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
      a__	:						int      		=  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					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			**snake_case					,			)				->  List[Any]:
      """simple docstring"""





      a__	:						Tuple      		=  BertGenerationEncoder(config=snake_case      )
      model.to(snake_case      )
      model.eval()
      a__	:						List[Any]      		=  model(snake_case					,			attention_mask=snake_case      )
      a__	:						Union[str, Any]      		=  model(snake_case      )
      self.parent.assertEqual(result.last_hidden_state.shape					,			(self.batch_size, self.seq_length, self.hidden_size)      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			**snake_case					,			)				->  Tuple:
      """simple docstring"""





      a__	:						Optional[int]      		=  True
      a__	:						Any      		=  BertGenerationEncoder(config=snake_case      )
      model.to(snake_case      )
      model.eval()
      a__	:						Union[str, Any]      		=  model(
          snake_case					,			attention_mask=snake_case					,			encoder_hidden_states=snake_case					,			encoder_attention_mask=snake_case					,			)
      a__	:						Optional[Any]      		=  model(
          snake_case					,			attention_mask=snake_case					,			encoder_hidden_states=snake_case					,			)
      self.parent.assertEqual(result.last_hidden_state.shape					,			(self.batch_size, self.seq_length, self.hidden_size)      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			**snake_case					,			)				->  List[Any]:
      """simple docstring"""





      a__	:						Optional[int]      		=  True
      a__	:						List[str]      		=  True
      a__	:						int      		=  BertGenerationDecoder(config=snake_case      ).to(snake_case      ).eval()

      # first forward pass
      a__	:						int      		=  model(
          snake_case					,			attention_mask=snake_case					,			encoder_hidden_states=snake_case					,			encoder_attention_mask=snake_case					,			use_cache=snake_case					,			)
      a__	:						Tuple      		=  outputs.past_key_values

      # create hypothetical multiple next token and extent to next_input_ids
      a__	:						Optional[Any]      		=  ids_tensor((self.batch_size, 3)					,			config.vocab_size      )
      a__	:						List[str]      		=  ids_tensor((self.batch_size, 3)					,			vocab_size=2      )

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

      a__	:						int      		=  model(
          snake_case					,			attention_mask=snake_case					,			encoder_hidden_states=snake_case					,			encoder_attention_mask=snake_case					,			output_hidden_states=snake_case					,			)["hidden_states"][0]
      a__	:						Dict      		=  model(
          snake_case					,			attention_mask=snake_case					,			encoder_hidden_states=snake_case					,			encoder_attention_mask=snake_case					,			past_key_values=snake_case					,			output_hidden_states=snake_case					,			)["hidden_states"][0]

      # select random slice
      a__	:						Any      		=  ids_tensor((1,)					,			output_from_past.shape[-1]      ).item()
      a__	:						Optional[Any]      		=  output_from_no_past[:, -3:, random_slice_idx].detach()
      a__	:						Optional[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(snake_case					,			snake_case					,			atol=1E-3      )      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			*snake_case					,			)				->  int:
      """simple docstring"""





      a__	:						int      		=  BertGenerationDecoder(snake_case      )
      model.to(snake_case      )
      model.eval()
      a__	:						str      		=  model(snake_case					,			attention_mask=snake_case					,			labels=snake_case      )
      self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.seq_length, self.vocab_size)      )





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      a__		,     a__		,     a__		,     a__	:						Optional[Any]      		=  self.prepare_config_and_inputs()
      a__	:						Tuple      		=  {"input_ids": input_ids, "attention_mask": input_mask}
      return config, inputs_dict







@require_torch
class   __lowerCAmelCase			(					_UpperCamelCase		,_UpperCamelCase		,_UpperCamelCase		,unittest.TestCase  ):
   _UpperCamelCase :						Union[str, Any]     		=				(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
   _UpperCamelCase :						Any     		=				(BertGenerationDecoder,) if is_torch_available() else ()
   _UpperCamelCase :						List[Any]     		=				(
       {"""feature-extraction""": BertGenerationEncoder, """text-generation""": BertGenerationDecoder}
       if is_torch_available()
       else {}
   )





   def 	_snake_case ( self      )				->  int:
      """simple docstring"""





      a__	:						Tuple      		=  BertGenerationEncoderTester(self      )
      a__	:						str      		=  ConfigTester(self					,			config_class=snake_case					,			hidden_size=37      )





   def 	_snake_case ( self      )				->  Any:
      """simple docstring"""





      self.config_tester.run_common_tests()





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      a__	:						Optional[Any]      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case      )





   def 	_snake_case ( self      )				->  List[str]:
      """simple docstring"""





      a__		,     a__		,     a__		,     a__	:						List[Any]      		=  self.model_tester.prepare_config_and_inputs()
      a__	:						Union[str, Any]      		=  "bert"
      self.model_tester.create_and_check_model(snake_case					,			snake_case					,			snake_case					,			snake_case      )





   def 	_snake_case ( self      )				->  Union[str, Any]:
      """simple docstring"""





      a__	:						Any      		=  self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(*snake_case      )





   def 	_snake_case ( self      )				->  List[str]:
      """simple docstring"""





      a__	:						int      		=  self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_decoder_model_past_large_inputs(*snake_case      )





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      (
          (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     
      )	:						Optional[int]      		=  self.model_tester.prepare_config_and_inputs_for_decoder()

      a__	:						Tuple      		=  None

      self.model_tester.create_and_check_model_as_decoder(
          snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)





   def 	_snake_case ( self      )				->  List[str]:
      """simple docstring"""





      a__	:						str      		=  self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_for_causal_lm(*snake_case      )





   @slow
   def 	_snake_case ( self      )				->  Union[str, Any]:
      """simple docstring"""





      a__	:						str      		=  BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"      )
      self.assertIsNotNone(snake_case      )







@require_torch
class   __lowerCAmelCase			(					unittest.TestCase  ):





   @slow
   def 	_snake_case ( self      )				->  Any:
      """simple docstring"""





      a__	:						Optional[int]      		=  BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"      )
      a__	:						Dict      		=  torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]      )
      with torch.no_grad():
         a__	:						Any      		=  model(snake_case      )[0]
      a__	:						Union[str, Any]      		=  torch.Size([1, 8, 1_024]      )
      self.assertEqual(output.shape					,			snake_case      )
      a__	:						Optional[Any]      		=  torch.tensor(
          [[[0.1_775, 0.0_083, -0.0_321], [1.6_002, 0.1_287, 0.3_912], [2.1_473, 0.5_791, 0.6_066]]]      )
      self.assertTrue(torch.allclose(output[:, :3, :3]					,			snake_case					,			atol=1E-4      )      )







@require_torch
class   __lowerCAmelCase			(					unittest.TestCase  ):





   @slow
   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      a__	:						str      		=  BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"      )
      a__	:						List[Any]      		=  torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]      )
      with torch.no_grad():
         a__	:						List[Any]      		=  model(snake_case      )[0]
      a__	:						Optional[int]      		=  torch.Size([1, 8, 50_358]      )
      self.assertEqual(output.shape					,			snake_case      )
      a__	:						List[Any]      		=  torch.tensor(
          [[[-0.5_788, -2.5_994, -3.7_054], [0.0_438, 4.7_997, 1.8_795], [1.5_862, 6.6_409, 4.4_638]]]      )
      self.assertTrue(torch.allclose(output[:, :3, :3]					,			snake_case					,			atol=1E-4      )      )








from collections.abc import Callable
from math import pi, sqrt
from random import uniform
from statistics import mean






def       _A  (      lowerCamelCase   ):

   # A local function to see if a dot lands in the circle.
   def is_in_circle(lowerCamelCase							, lowerCamelCase   ) -> bool:
      a__	:						Any      		=  sqrt((x**2) + (y**2)   )
      # Our circle has a radius of 1, so a distance
      # greater than 1 would land outside the circle.
      return distance_from_centre <= 1

   # The proportion of guesses that landed in the circle
   a__	:						Union[str, Any]      		=  mean(
       int(is_in_circle(uniform(-1.0							, 1.0   )							, uniform(-1.0							, 1.0   )   )   )
       for _ in range(lowerCamelCase   )   )
   # The ratio of the area for circle to square is pi/4.
   a__	:						Any      		=  proportion * 4
   print(F"""The estimated value of pi is {pi_estimate}"""   )
   print(F"""The numpy value of pi is {pi}"""   )
   print(F"""The total error is {abs(pi - pi_estimate   )}"""   )






def       _A  (      lowerCamelCase							, lowerCamelCase							, lowerCamelCase = 0.0							, lowerCamelCase = 1.0							, ):
   return mean(
       function_to_integrate(uniform(lowerCamelCase							, lowerCamelCase   )   ) for _ in range(lowerCamelCase   )   ) * (max_value - min_value)






def       _A  (      lowerCamelCase							, lowerCamelCase = 0.0							, lowerCamelCase = 1.0   ):

   def identity_function(lowerCamelCase   ) -> float:
      return x

   a__	:						int      		=  area_under_curve_estimator(
       lowerCamelCase							, lowerCamelCase							, lowerCamelCase							, lowerCamelCase   )
   a__	:						int      		=  (max_value * max_value - min_value * min_value) / 2

   print("******************"   )
   print(F"""Estimating area under y=x where x varies from {min_value} to {max_value}"""   )
   print(F"""Estimated value is {estimated_value}"""   )
   print(F"""Expected value is {expected_value}"""   )
   print(F"""Total error is {abs(estimated_value - expected_value   )}"""   )
   print("******************"   )






def       _A  (      lowerCamelCase   ):

   def function_to_integrate(lowerCamelCase   ) -> float:
      return sqrt(4.0 - x * x   )

   a__	:						int      		=  area_under_curve_estimator(
       lowerCamelCase							, lowerCamelCase							, 0.0							, 2.0   )

   print("******************"   )
   print("Estimating pi using area_under_curve_estimator"   )
   print(F"""Estimated value is {estimated_value}"""   )
   print(F"""Expected value is {pi}"""   )
   print(F"""Total error is {abs(estimated_value - pi   )}"""   )
   print("******************"   )


if __name__ == "__main__":
  import doctest

  doctest.testmod()

