



'''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        UpperCAmelCase_			:
    """simple docstring"""







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




    def lowerCamelCase			(       self   :	int		):
          snake_case__    :       List[Any]       		= ids_tensor([self.batch_size, self.seq_length]					,	self.vocab_size		)

          snake_case__    :       int       		= None
          if self.use_input_mask:
                snake_case__    :       List[str]       		= random_attention_mask([self.batch_size, self.seq_length]		)

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

          snake_case__    :       Dict       		= self.get_config()

          return config, input_ids, input_mask, token_labels




    def lowerCamelCase			(       self   :	Optional[int]		):
          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 lowerCamelCase			(       self   :	Tuple		):
          (
              (
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							 
          )    :       Optional[Any]       		= self.prepare_config_and_inputs()

          snake_case__    :       Union[str, Any]       		= True
          snake_case__    :       Any       		= floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
          snake_case__    :       Dict       		= 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 lowerCamelCase			(       self   :	Any					,	snake_case_   :	List[str]					,	snake_case_   :	Union[str, Any]					,	snake_case_   :	List[str]					,	snake_case_   :	List[str]					,	**snake_case_   :	Any					,	):
          snake_case__    :       str       		= BertGenerationEncoder(config=__lowercase		)
          model.to(__lowercase		)
          model.eval()
          snake_case__    :       List[Any]       		= model(__lowercase					,	attention_mask=__lowercase		)
          snake_case__    :       Optional[Any]       		= model(__lowercase		)
          self.parent.assertEqual(result.last_hidden_state.shape					,	(self.batch_size, self.seq_length, self.hidden_size)		)




    def lowerCamelCase			(       self   :	Optional[Any]					,	snake_case_   :	Optional[int]					,	snake_case_   :	str					,	snake_case_   :	Dict					,	snake_case_   :	Optional[int]					,	snake_case_   :	Tuple					,	snake_case_   :	str					,	**snake_case_   :	List[Any]					,	):
          snake_case__    :       Optional[int]       		= True
          snake_case__    :       int       		= BertGenerationEncoder(config=__lowercase		)
          model.to(__lowercase		)
          model.eval()
          snake_case__    :       List[str]       		= model(
              __lowercase					,	attention_mask=__lowercase					,	encoder_hidden_states=__lowercase					,	encoder_attention_mask=__lowercase					,	)
          snake_case__    :       List[str]       		= 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 lowerCamelCase			(       self   :	Dict					,	snake_case_   :	Tuple					,	snake_case_   :	Dict					,	snake_case_   :	List[str]					,	snake_case_   :	str					,	snake_case_   :	List[Any]					,	snake_case_   :	Union[str, Any]					,	**snake_case_   :	Dict					,	):
          snake_case__    :       Dict       		= True
          snake_case__    :       List[str]       		= True
          snake_case__    :       Any       		= BertGenerationDecoder(config=__lowercase		).to(__lowercase		).eval()

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

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

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

          snake_case__    :       Union[str, Any]       		= model(
              __lowercase					,	attention_mask=__lowercase					,	encoder_hidden_states=__lowercase					,	encoder_attention_mask=__lowercase					,	output_hidden_states=__lowercase					,	)["""hidden_states"""][0]
          snake_case__    :       List[Any]       		= 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
          snake_case__    :       Dict       		= ids_tensor((1,)					,	output_from_past.shape[-1]		).item()
          snake_case__    :       List[Any]       		= output_from_no_past[:, -3:, random_slice_idx].detach()
          snake_case__    :       Dict       		= 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 lowerCamelCase			(       self   :	Any					,	snake_case_   :	Dict					,	snake_case_   :	str					,	snake_case_   :	Optional[Any]					,	snake_case_   :	int					,	*snake_case_   :	List[str]					,	):
          snake_case__    :       Union[str, Any]       		= BertGenerationDecoder(__lowercase		)
          model.to(__lowercase		)
          model.eval()
          snake_case__    :       Union[str, Any]       		= model(__lowercase					,	attention_mask=__lowercase					,	labels=__lowercase		)
          self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.vocab_size)		)




    def lowerCamelCase			(       self   :	Optional[Any]		):
          snake_case__		,							snake_case__		,							snake_case__		,							snake_case__    :       Optional[Any]       		= self.prepare_config_and_inputs()
          snake_case__    :       Optional[int]       		= {"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_torch
class        UpperCAmelCase_			(			UpperCAmelCase__						,						UpperCAmelCase__						,						UpperCAmelCase__						,						unittest.TestCase      ):
    """simple docstring"""







    lowercase   =						(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
    lowercase   =						(BertGenerationDecoder,) if is_torch_available() else ()
    lowercase   =						(
        {"feature-extraction": BertGenerationEncoder, "text-generation": BertGenerationDecoder}
        if is_torch_available()
        else {}
    )




    def lowerCamelCase			(       self   :	Optional[int]		):
          snake_case__    :       Optional[int]       		= BertGenerationEncoderTester(self		)
          snake_case__    :       Union[str, Any]       		= ConfigTester(self					,	config_class=__lowercase					,	hidden_size=37		)




    def lowerCamelCase			(       self   :	Any		):
          self.config_tester.run_common_tests()




    def lowerCamelCase			(       self   :	Optional[Any]		):
          snake_case__    :       Optional[int]       		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__lowercase		)




    def lowerCamelCase			(       self   :	int		):
          snake_case__		,							snake_case__		,							snake_case__		,							snake_case__    :       Tuple       		= self.model_tester.prepare_config_and_inputs()
          snake_case__    :       int       		= """bert"""
          self.model_tester.create_and_check_model(__lowercase					,	__lowercase					,	__lowercase					,	__lowercase		)




    def lowerCamelCase			(       self   :	List[str]		):
          snake_case__    :       Optional[Any]       		= self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_model_as_decoder(*__lowercase		)




    def lowerCamelCase			(       self   :	Optional[Any]		):
          snake_case__    :       List[str]       		= self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_decoder_model_past_large_inputs(*__lowercase		)




    def lowerCamelCase			(       self   :	List[str]		):
          (
              (
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							(
              snake_case__ 
          )		,							 
          )    :       Optional[Any]       		= self.model_tester.prepare_config_and_inputs_for_decoder()

          snake_case__    :       List[Any]       		= None

          self.model_tester.create_and_check_model_as_decoder(
              __lowercase					,	__lowercase					,	__lowercase					,	__lowercase					,	__lowercase					,	__lowercase					,	)




    def lowerCamelCase			(       self   :	Optional[int]		):
          snake_case__    :       List[Any]       		= self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_for_causal_lm(*__lowercase		)




    @slow
    def lowerCamelCase			(       self   :	int		):
          snake_case__    :       str       		= BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""		)
          self.assertIsNotNone(__lowercase		)







@require_torch
class        UpperCAmelCase_			(			unittest.TestCase      ):
    """simple docstring"""







    @slow
    def lowerCamelCase			(       self   :	Dict		):
          snake_case__    :       str       		= BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""		)
          snake_case__    :       Optional[int]       		= torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]		)
          with torch.no_grad():
                snake_case__    :       str       		= model(__lowercase		)[0]
          snake_case__    :       List[Any]       		= torch.Size([1, 8, 1_024]		)
          self.assertEqual(output.shape					,	__lowercase		)
          snake_case__    :       Any       		= 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        UpperCAmelCase_			(			unittest.TestCase      ):
    """simple docstring"""







    @slow
    def lowerCamelCase			(       self   :	List[str]		):
          snake_case__    :       Tuple       		= BertGenerationDecoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""		)
          snake_case__    :       int       		= torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]		)
          with torch.no_grad():
                snake_case__    :       Dict       		= model(__lowercase		)[0]
          snake_case__    :       Tuple       		= torch.Size([1, 8, 50_358]		)
          self.assertEqual(output.shape					,	__lowercase		)
          snake_case__    :       str       		= 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 		lowerCamelCase__       (						_A	,      _A				):







	'''simple docstring'''





	_validate_point(_A				)
	_validate_point(_A				)
	if len(_A				) != len(_A				):
		raise ValueError("Both points must be in the same n-dimensional space"				)

	return float(sum(abs(a - b				) for a, b in zip(_A	,      _A				)				)				)


def 		lowerCamelCase__       (						_A				):







	'''simple docstring'''





	if point:
		if isinstance(_A	,      _A				):
			for item in point:
				if not isinstance(_A	,      (int, float)				):
					snake_case_						       =	(
					    "Expected a list of numbers as input, found "
					    f"{type(_A				).__name__}"
					)
					raise TypeError(_A				)
		else:
			snake_case_						       =	f"Expected a list of numbers as input, found {type(_A				).__name__}"
			raise TypeError(_A				)
	else:
		raise ValueError("Missing an input"				)


def 		lowerCamelCase__       (						_A	,      _A				):







	'''simple docstring'''





	_validate_point(_A				)
	_validate_point(_A				)
	if len(_A				) != len(_A				):
		raise ValueError("Both points must be in the same n-dimensional space"				)

	return float(sum(abs(x - y				) for x, y in zip(_A	,      _A				)				)				)


if __name__ == "__main__":
		import doctest

		doctest.testmod()
