




'''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 			_snake_case		:




			def __init__( self    ,							a__    ,							a__=13    ,							a__=7    ,							a__=True    ,							a__=True    ,							a__=99    ,							a__=32    ,							a__=5    ,							a__=4    ,							a__=37    ,							a__="gelu"    ,							a__=0.1    ,							a__=0.1    ,							a__=50    ,							a__=0.0_2    ,							a__=True    ,							a__=None    ,							)    ->    int:

										'''simple docstring'''




										snake_case_      =				parent
										snake_case_      =				batch_size
										snake_case_      =				seq_length
										snake_case_      =				is_training
										snake_case_      =				use_input_mask
										snake_case_      =				vocab_size
										snake_case_      =				hidden_size
										snake_case_      =				num_hidden_layers
										snake_case_      =				num_attention_heads
										snake_case_      =				intermediate_size
										snake_case_      =				hidden_act
										snake_case_      =				hidden_dropout_prob
										snake_case_      =				attention_probs_dropout_prob
										snake_case_      =				max_position_embeddings
										snake_case_      =				initializer_range
										snake_case_      =				use_labels
										snake_case_      =				scope




			def        lowerCAmelCase__    ( self							)    ->    Tuple:

										'''simple docstring'''




										snake_case_      =				ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size							)

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

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

										snake_case_      =				self.get_config()

										return config, input_ids, input_mask, token_labels




			def        lowerCAmelCase__    ( self							)    ->    List[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=_a    ,							initializer_range=self.initializer_range    ,							)




			def        lowerCAmelCase__    ( self							)    ->    int:

										'''simple docstring'''




										(
										    snake_case_  
										)      =				self.prepare_config_and_inputs()

										snake_case_      =				True
										snake_case_      =				floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
										snake_case_      =				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    ,							a__    ,							a__    ,							a__    ,							a__    ,							**a__    ,							)    ->    Optional[Any]:

										'''simple docstring'''




										snake_case_      =				BertGenerationEncoder(config=_a							)
										model.to(_a							)
										model.eval()
										snake_case_      =				model(_a    ,							attention_mask=_a							)
										snake_case_      =				model(_a							)
										self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size)							)




			def        lowerCAmelCase__    ( self    ,							a__    ,							a__    ,							a__    ,							a__    ,							a__    ,							a__    ,							**a__    ,							)    ->    Optional[Any]:

										'''simple docstring'''




										snake_case_      =				True
										snake_case_      =				BertGenerationEncoder(config=_a							)
										model.to(_a							)
										model.eval()
										snake_case_      =				model(
										    _a    ,							attention_mask=_a    ,							encoder_hidden_states=_a    ,							encoder_attention_mask=_a    ,							)
										snake_case_      =				model(
										    _a    ,							attention_mask=_a    ,							encoder_hidden_states=_a    ,							)
										self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size)							)




			def        lowerCAmelCase__    ( self    ,							a__    ,							a__    ,							a__    ,							a__    ,							a__    ,							a__    ,							**a__    ,							)    ->    List[Any]:

										'''simple docstring'''




										snake_case_      =				True
										snake_case_      =				True
										snake_case_      =				BertGenerationDecoder(config=_a							).to(_a							).eval()

										# first forward pass
										snake_case_      =				model(
										    _a    ,							attention_mask=_a    ,							encoder_hidden_states=_a    ,							encoder_attention_mask=_a    ,							use_cache=_a    ,							)
										snake_case_      =				outputs.past_key_values

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

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

										snake_case_      =				model(
										    _a    ,							attention_mask=_a    ,							encoder_hidden_states=_a    ,							encoder_attention_mask=_a    ,							output_hidden_states=_a    ,							)['hidden_states'][0]
										snake_case_      =				model(
										    _a    ,							attention_mask=_a    ,							encoder_hidden_states=_a    ,							encoder_attention_mask=_a    ,							past_key_values=_a    ,							output_hidden_states=_a    ,							)['hidden_states'][0]

										# select random slice
										snake_case_      =				ids_tensor((1,)    ,							output_from_past.shape[-1]							).item()
										snake_case_      =				output_from_no_past[:, -3:, random_slice_idx].detach()
										snake_case_      =				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(_a    ,							_a    ,							atol=1e-3							)							)




			def        lowerCAmelCase__    ( self    ,							a__    ,							a__    ,							a__    ,							a__    ,							*a__    ,							)    ->    Union[str, Any]:

										'''simple docstring'''




										snake_case_      =				BertGenerationDecoder(_a							)
										model.to(_a							)
										model.eval()
										snake_case_      =				model(_a    ,							attention_mask=_a    ,							labels=_a							)
										self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.vocab_size)							)




			def        lowerCAmelCase__    ( self							)    ->    Dict:

										'''simple docstring'''




										snake_case_      =				self.prepare_config_and_inputs()
										snake_case_      =				{'input_ids': input_ids, 'attention_mask': input_mask}
										return config, inputs_dict




@require_torch
class 			_snake_case		(     lowercase__						,						lowercase__						,						lowercase__						,						unittest.TestCase							):
			lowerCAmelCase_					:				Tuple							=   (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
			lowerCAmelCase_					:				Tuple							=   (BertGenerationDecoder,) if is_torch_available() else ()
			lowerCAmelCase_					:				List[str]							=   (
			    {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
			    if is_torch_available()
			    else {}
			)




			def        lowerCAmelCase__    ( self							)    ->    List[str]:

										'''simple docstring'''




										snake_case_      =				BertGenerationEncoderTester(self							)
										snake_case_      =				ConfigTester(self    ,							config_class=_a    ,							hidden_size=37							)




			def        lowerCAmelCase__    ( self							)    ->    Dict:

										'''simple docstring'''




										self.config_tester.run_common_tests()




			def        lowerCAmelCase__    ( self							)    ->    Optional[Any]:

										'''simple docstring'''




										snake_case_      =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_a							)




			def        lowerCAmelCase__    ( self							)    ->    Tuple:

										'''simple docstring'''




										snake_case_      =				self.model_tester.prepare_config_and_inputs()
										snake_case_      =				'bert'
										self.model_tester.create_and_check_model(_a    ,							_a    ,							_a    ,							_a							)




			def        lowerCAmelCase__    ( self							)    ->    Union[str, Any]:

										'''simple docstring'''




										snake_case_      =				self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_model_as_decoder(*_a							)




			def        lowerCAmelCase__    ( self							)    ->    str:

										'''simple docstring'''




										snake_case_      =				self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_decoder_model_past_large_inputs(*_a							)




			def        lowerCAmelCase__    ( self							)    ->    Union[str, Any]:

										'''simple docstring'''




										(
										    snake_case_  
										)      =				self.model_tester.prepare_config_and_inputs_for_decoder()

										snake_case_      =				None

										self.model_tester.create_and_check_model_as_decoder(
										    _a    ,							_a    ,							_a    ,							_a    ,							_a    ,							_a    ,							)




			def        lowerCAmelCase__    ( self							)    ->    int:

										'''simple docstring'''




										snake_case_      =				self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_for_causal_lm(*_a							)




			@slow
			def        lowerCAmelCase__    ( self							)    ->    Any:

										'''simple docstring'''




										snake_case_      =				BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
										self.assertIsNotNone(_a							)




@require_torch
class 			_snake_case		(     unittest.TestCase							):




			@slow
			def        lowerCAmelCase__    ( self							)    ->    List[Any]:

										'''simple docstring'''




										snake_case_      =				BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
										snake_case_      =				torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]							)
										with torch.no_grad():
																	snake_case_      =				model(_a							)[0]
										snake_case_      =				torch.Size([1, 8, 1_024]							)
										self.assertEqual(output.shape    ,							_a							)
										snake_case_      =				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]    ,							_a    ,							atol=1e-4							)							)




@require_torch
class 			_snake_case		(     unittest.TestCase							):




			@slow
			def        lowerCAmelCase__    ( self							)    ->    Optional[int]:

										'''simple docstring'''




										snake_case_      =				BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
										snake_case_      =				torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]							)
										with torch.no_grad():
																	snake_case_      =				model(_a							)[0]
										snake_case_      =				torch.Size([1, 8, 50_358]							)
										self.assertEqual(output.shape    ,							_a							)
										snake_case_      =				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]    ,							_a    ,							atol=1e-4							)							)










'''simple docstring'''





from ..utils import DummyObject, requires_backends




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Dict							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    List[Any]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    List[Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    str:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Dict							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Dict:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    List[str]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Optional[Any]							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    str:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Optional[int]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    List[str]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Optional[Any]							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Union[str, Any]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    List[str]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Dict:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				List[str]							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Optional[int]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				int							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    List[str]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    List[Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Any:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Optional[Any]							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Any:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    List[Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    int:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Dict							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Optional[int]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    List[Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				List[str]							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Dict:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Union[str, Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				int							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Dict:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Union[str, Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    int:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Any							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Tuple:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    str:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				str							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    List[str]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Union[str, Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Union[str, Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




class 			_snake_case		(     metaclass=lowercase_							):
			lowerCAmelCase_					:				Tuple							=   ["flax"]




			def __init__( self    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(self    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    int:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




			@classmethod
			def        lowerCAmelCase__    ( cls    ,							*a__    ,							**a__							)    ->    Optional[Any]:

										'''simple docstring'''




										requires_backends(cls    ,							["flax"]							)




