

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






					"""simple docstring"""



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







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__	:						Optional[Any]  				=	ids_tensor([self.batch_size, self.seq_length]   ,self.vocab_size		)

						lowerCAmelCase__	:						Tuple  				=	None
						if self.use_input_mask:
							lowerCAmelCase__	:						int  				=	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__	:						List[str]  				=	self.get_config()

						return config, input_ids, input_mask, token_labels







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

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







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

						# first forward pass
						lowerCAmelCase__	:						Union[str, Any]  				=	model(
						    lowercase_   ,attention_mask=lowercase_   ,encoder_hidden_states=lowercase_   ,encoder_attention_mask=lowercase_   ,use_cache=lowercase_   ,)
						lowerCAmelCase__	:						Dict  				=	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__	:						Optional[Any]  				=	ids_tensor((self.batch_size, 3)   ,vocab_size=2		)

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

						lowerCAmelCase__	:						Union[str, Any]  				=	model(
						    lowercase_   ,attention_mask=lowercase_   ,encoder_hidden_states=lowercase_   ,encoder_attention_mask=lowercase_   ,output_hidden_states=lowercase_   ,)['''hidden_states'''][0]
						lowerCAmelCase__	:						str  				=	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
						lowerCAmelCase__	:						List[str]  				=	ids_tensor((1,)   ,output_from_past.shape[-1]		).item()
						lowerCAmelCase__	:						List[Any]  				=	output_from_no_past[:, -3:, random_slice_idx].detach()
						lowerCAmelCase__	:						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(lowercase_   ,lowercase_   ,atol=1E-3		)		)







					def        __lowerCAmelCase (       self			:				Any   ,lowercase_			:				List[Any]   ,lowercase_			:				List[str]   ,lowercase_			:				Tuple   ,lowercase_			:				Any   ,*lowercase_			:				List[str]   ,):
						lowerCAmelCase__	:						Optional[Any]  				=	BertGenerationDecoder(lowercase_		)
						model.to(lowercase_		)
						model.eval()
						lowerCAmelCase__	:						int  				=	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]		):
						lowerCAmelCase__				,lowerCAmelCase__				,lowerCAmelCase__				,lowerCAmelCase__	:						Tuple  				=	self.prepare_config_and_inputs()
						lowerCAmelCase__	:						Any  				=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
						return config, inputs_dict







@require_torch
class 				SCREAMING_SNAKE_CASE					(		a_    ,							a_    ,							a_    ,							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			:				str		):
						lowerCAmelCase__	:						Tuple  				=	BertGenerationEncoderTester(self		)
						lowerCAmelCase__	:						List[str]  				=	ConfigTester(self   ,config_class=lowercase_   ,hidden_size=3_7		)







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						self.config_tester.run_common_tests()







					def        __lowerCAmelCase (       self			:				Dict		):
						lowerCAmelCase__	:						List[Any]  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase_		)







					def        __lowerCAmelCase (       self			:				Dict		):
						lowerCAmelCase__				,lowerCAmelCase__				,lowerCAmelCase__				,lowerCAmelCase__	:						str  				=	self.model_tester.prepare_config_and_inputs()
						lowerCAmelCase__	:						Optional[Any]  				=	'''bert'''
						self.model_tester.create_and_check_model(lowercase_   ,lowercase_   ,lowercase_   ,lowercase_		)







					def        __lowerCAmelCase (       self			:				Dict		):
						lowerCAmelCase__	:						Union[str, Any]  				=	self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_model_as_decoder(*lowercase_		)







					def        __lowerCAmelCase (       self			:				str		):
						lowerCAmelCase__	:						Dict  				=	self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_decoder_model_past_large_inputs(*lowercase_		)







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

						lowerCAmelCase__	:						Union[str, Any]  				=	None

						self.model_tester.create_and_check_model_as_decoder(
						    lowercase_   ,lowercase_   ,lowercase_   ,lowercase_   ,lowercase_   ,lowercase_   ,)







					def        __lowerCAmelCase (       self			:				int		):
						lowerCAmelCase__	:						int  				=	self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_for_causal_lm(*lowercase_		)







					@slow
					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__	:						str  				=	BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''		)
						self.assertIsNotNone(lowercase_		)







@require_torch
class 				SCREAMING_SNAKE_CASE					(		unittest.TestCase       ):






					"""simple docstring"""



					@slow
					def        __lowerCAmelCase (       self			:				List[Any]		):
						lowerCAmelCase__	:						List[str]  				=	BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''		)
						lowerCAmelCase__	:						List[Any]  				=	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__	:						List[Any]  				=	model(lowercase_		)[0]
						lowerCAmelCase__	:						Any  				=	torch.Size([1, 8, 1_0_2_4]		)
						self.assertEqual(output.shape   ,lowercase_		)
						lowerCAmelCase__	:						int  				=	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 				SCREAMING_SNAKE_CASE					(		unittest.TestCase       ):






					"""simple docstring"""



					@slow
					def        __lowerCAmelCase (       self			:				Dict		):
						lowerCAmelCase__	:						Optional[Any]  				=	BertGenerationDecoder.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__	:						Optional[Any]  				=	model(lowercase_		)[0]
						lowerCAmelCase__	:						Union[str, Any]  				=	torch.Size([1, 8, 5_0_3_5_8]		)
						self.assertEqual(output.shape   ,lowercase_		)
						lowerCAmelCase__	:						Dict  				=	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		)		)








# flake8: noqa
# Lint as: python3
UpperCAmelCase__   	=							[
    '''VerificationMode''',
    '''Version''',
    '''disable_progress_bar''',
    '''enable_progress_bar''',
    '''is_progress_bar_enabled''',
    '''experimental''',
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental

