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





		def __init__(				self     :						Optional[Any]    ,	lowerCamelCase__     :						Tuple    ,	lowerCamelCase__     :						Dict=1_3    ,	lowerCamelCase__     :						Dict=7    ,	lowerCamelCase__     :						Optional[int]=True    ,	lowerCamelCase__     :						List[Any]=True    ,	lowerCamelCase__     :						Union[str, Any]=9_9    ,	lowerCamelCase__     :						Dict=3_2    ,	lowerCamelCase__     :						Tuple=5    ,	lowerCamelCase__     :						Dict=4    ,	lowerCamelCase__     :						Any=3_7    ,	lowerCamelCase__     :						Optional[int]="gelu"    ,	lowerCamelCase__     :						Any=0.1    ,	lowerCamelCase__     :						Tuple=0.1    ,	lowerCamelCase__     :						Union[str, Any]=5_0    ,	lowerCamelCase__     :						Tuple=0.0_2    ,	lowerCamelCase__     :						Union[str, Any]=True    ,	lowerCamelCase__     :						str=None    ,	):
							lowerCAmelCase		:       int							=  parent
							lowerCAmelCase		:       Dict							=  batch_size
							lowerCAmelCase		:       Any							=  seq_length
							lowerCAmelCase		:       Optional[int]							=  is_training
							lowerCAmelCase		:       Any							=  use_input_mask
							lowerCAmelCase		:       List[Any]							=  vocab_size
							lowerCAmelCase		:       List[str]							=  hidden_size
							lowerCAmelCase		:       List[str]							=  num_hidden_layers
							lowerCAmelCase		:       Optional[Any]							=  num_attention_heads
							lowerCAmelCase		:       Union[str, Any]							=  intermediate_size
							lowerCAmelCase		:       List[str]							=  hidden_act
							lowerCAmelCase		:       int							=  hidden_dropout_prob
							lowerCAmelCase		:       List[str]							=  attention_probs_dropout_prob
							lowerCAmelCase		:       Any							=  max_position_embeddings
							lowerCAmelCase		:       List[Any]							=  initializer_range
							lowerCAmelCase		:       Union[str, Any]							=  use_labels
							lowerCAmelCase		:       str							=  scope





		def    _A				(				self     :						Optional[int]					):
							lowerCAmelCase		:       List[str]							=  ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size					)

							lowerCAmelCase		:       Union[str, Any]							=  None
							if self.use_input_mask:
												lowerCAmelCase		:       List[Any]							=  random_attention_mask([self.batch_size, self.seq_length]					)

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

							lowerCAmelCase		:       Tuple							=  self.get_config()

							return config, input_ids, input_mask, token_labels





		def    _A				(				self     :						Optional[Any]					):
							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    _A				(				self     :						Dict					):
							(
							    (
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,							
							)		:       int							=  self.prepare_config_and_inputs()

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





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

							# first forward pass
							lowerCAmelCase		:       Union[str, Any]							=  model(
							    __UpperCamelCase    ,	attention_mask=__UpperCamelCase    ,	encoder_hidden_states=__UpperCamelCase    ,	encoder_attention_mask=__UpperCamelCase    ,	use_cache=__UpperCamelCase    ,	)
							lowerCAmelCase		:       str							=  outputs.past_key_values

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

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

							lowerCAmelCase		:       List[str]							=  model(
							    __UpperCamelCase    ,	attention_mask=__UpperCamelCase    ,	encoder_hidden_states=__UpperCamelCase    ,	encoder_attention_mask=__UpperCamelCase    ,	output_hidden_states=__UpperCamelCase    ,	)['''hidden_states'''][0]
							lowerCAmelCase		:       List[Any]							=  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
							lowerCAmelCase		:       Optional[Any]							=  ids_tensor((1,)    ,	output_from_past.shape[-1]					).item()
							lowerCAmelCase		:       Any							=  output_from_no_past[:, -3:, random_slice_idx].detach()
							lowerCAmelCase		:       List[Any]							=  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    _A				(				self     :						Optional[Any]    ,	lowerCamelCase__     :						List[str]    ,	lowerCamelCase__     :						Tuple    ,	lowerCamelCase__     :						List[str]    ,	lowerCamelCase__     :						List[Any]    ,	*lowerCamelCase__     :						Tuple    ,	):
							lowerCAmelCase		:       Optional[int]							=  BertGenerationDecoder(__UpperCamelCase					)
							model.to(__UpperCamelCase					)
							model.eval()
							lowerCAmelCase		:       str							=  model(__UpperCamelCase    ,	attention_mask=__UpperCamelCase    ,	labels=__UpperCamelCase					)
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.vocab_size)					)





		def    _A				(				self     :						Optional[int]					):
							lowerCAmelCase						,				lowerCAmelCase						,				lowerCAmelCase						,				lowerCAmelCase		:       List[Any]							=  self.prepare_config_and_inputs()
							lowerCAmelCase		:       List[Any]							=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_torch
class 		__magic_name__ (    a_,					a_,					a_,					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    _A				(				self     :						int					):
							lowerCAmelCase		:       Union[str, Any]							=  BertGenerationEncoderTester(self					)
							lowerCAmelCase		:       int							=  ConfigTester(self    ,	config_class=__UpperCamelCase    ,	hidden_size=3_7					)





		def    _A				(				self     :						int					):
							self.config_tester.run_common_tests()





		def    _A				(				self     :						int					):
							lowerCAmelCase		:       Optional[Any]							=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCamelCase					)





		def    _A				(				self     :						Optional[int]					):
							lowerCAmelCase						,				lowerCAmelCase						,				lowerCAmelCase						,				lowerCAmelCase		:       Any							=  self.model_tester.prepare_config_and_inputs()
							lowerCAmelCase		:       Optional[Any]							=  '''bert'''
							self.model_tester.create_and_check_model(__UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase					)





		def    _A				(				self     :						Tuple					):
							lowerCAmelCase		:       int							=  self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*__UpperCamelCase					)





		def    _A				(				self     :						List[str]					):
							lowerCAmelCase		:       str							=  self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(*__UpperCamelCase					)





		def    _A				(				self     :						Union[str, Any]					):
							# This regression test was failing with PyTorch < 1.3
							(
							    (
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,				(
							    lowerCAmelCase			
							)						,							
							)		:       List[str]							=  self.model_tester.prepare_config_and_inputs_for_decoder()

							lowerCAmelCase		:       List[Any]							=  None

							self.model_tester.create_and_check_model_as_decoder(
							    __UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	__UpperCamelCase    ,	)





		def    _A				(				self     :						Optional[Any]					):
							lowerCAmelCase		:       List[str]							=  self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_for_causal_lm(*__UpperCamelCase					)





		@slow
		def    _A				(				self     :						Any					):
							lowerCAmelCase		:       Any							=  BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''					)
							self.assertIsNotNone(__UpperCamelCase					)



@require_torch
class 		__magic_name__ (    unittest.TestCase     ):





		@slow
		def    _A				(				self     :						List[Any]					):
							lowerCAmelCase		:       List[str]							=  BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''					)
							lowerCAmelCase		:       Optional[int]							=  torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]]					)
							with torch.no_grad():
												lowerCAmelCase		:       Tuple							=  model(__UpperCamelCase					)[0]
							lowerCAmelCase		:       int							=  torch.Size([1, 8, 1_0_2_4]					)
							self.assertEqual(output.shape    ,	__UpperCamelCase					)
							lowerCAmelCase		:       List[Any]							=  torch.tensor(
							    [[[0.1_7_7_5, 0.0_0_8_3, -0.0_3_2_1], [1.6_0_0_2, 0.1_2_8_7, 0.3_9_1_2], [2.1_4_7_3, 0.5_7_9_1, 0.6_0_6_6]]]					)
							self.assertTrue(torch.allclose(output[:, :3, :3]    ,	__UpperCamelCase    ,	atol=1E-4					)					)



@require_torch
class 		__magic_name__ (    unittest.TestCase     ):





		@slow
		def    _A				(				self     :						Union[str, Any]					):
							lowerCAmelCase		:       Tuple							=  BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''					)
							lowerCAmelCase		:       Dict							=  torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]]					)
							with torch.no_grad():
												lowerCAmelCase		:       int							=  model(__UpperCamelCase					)[0]
							lowerCAmelCase		:       Optional[int]							=  torch.Size([1, 8, 5_0_3_5_8]					)
							self.assertEqual(output.shape    ,	__UpperCamelCase					)
							lowerCAmelCase		:       Any							=  torch.tensor(
							    [[[-0.5_7_8_8, -2.5_9_9_4, -3.7_0_5_4], [0.0_4_3_8, 4.7_9_9_7, 1.8_7_9_5], [1.5_8_6_2, 6.6_4_0_9, 4.4_6_3_8]]]					)
							self.assertTrue(torch.allclose(output[:, :3, :3]    ,	__UpperCamelCase    ,	atol=1E-4					)					)








from math import pow, sqrt





def    __lowerCAmelCase   (    *__snake_case							):
     __lowerCAmelCase             =       len(__snake_case							) > 0 and all(value > 0.0 for value in values							)
     return result





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case							):
     return (
         round(sqrt(molar_mass_a / molar_mass_a							)       ,			6							)
         if validate(__snake_case       ,			__snake_case							)
         else ValueError("Input Error: Molar mass values must greater than 0."							)
     )





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case       ,			__snake_case							):
     return (
         round(effusion_rate * sqrt(molar_mass_a / molar_mass_a							)       ,			6							)
         if validate(__snake_case       ,			__snake_case       ,			__snake_case							)
         else ValueError(
             "Input Error: Molar mass and effusion rate values must greater than 0."							)
     )





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case       ,			__snake_case							):
     return (
         round(effusion_rate / sqrt(molar_mass_a / molar_mass_a							)       ,			6							)
         if validate(__snake_case       ,			__snake_case       ,			__snake_case							)
         else ValueError(
             "Input Error: Molar mass and effusion rate values must greater than 0."							)
     )





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case       ,			__snake_case							):
     return (
         round(molar_mass / pow(effusion_rate_a / effusion_rate_a       ,			2							)       ,			6							)
         if validate(__snake_case       ,			__snake_case       ,			__snake_case							)
         else ValueError(
             "Input Error: Molar mass and effusion rate values must greater than 0."							)
     )





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case       ,			__snake_case							):
     return (
         round(pow(effusion_rate_a / effusion_rate_a       ,			2							) / molar_mass       ,			6							)
         if validate(__snake_case       ,			__snake_case       ,			__snake_case							)
         else ValueError(
             "Input Error: Molar mass and effusion rate values must greater than 0."							)
     )






