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






			def __init__(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	Any      ,SCREAMING_SNAKE_CASE_  :	List[str]=13      ,SCREAMING_SNAKE_CASE_  :	int=7      ,SCREAMING_SNAKE_CASE_  :	List[str]=True      ,SCREAMING_SNAKE_CASE_  :	Optional[int]=True      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=99      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=32      ,SCREAMING_SNAKE_CASE_  :	Tuple=5      ,SCREAMING_SNAKE_CASE_  :	Any=4      ,SCREAMING_SNAKE_CASE_  :	List[str]=37      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]="gelu"      ,SCREAMING_SNAKE_CASE_  :	List[Any]=0.1      ,SCREAMING_SNAKE_CASE_  :	List[str]=0.1      ,SCREAMING_SNAKE_CASE_  :	List[Any]=50      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=0.02      ,SCREAMING_SNAKE_CASE_  :	Tuple=True      ,SCREAMING_SNAKE_CASE_  :	Optional[int]=None      ,)       ->	List[Any]:





				"""simple docstring"""


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






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





				"""simple docstring"""


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

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

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

				lowerCAmelCase												=  self.get_config()

				return config, input_ids, input_mask, token_labels






			def 					UpperCAmelCase							(self  :	List[Any]			)       ->	int:





				"""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=SCREAMING_SNAKE_CASE_      ,initializer_range=self.initializer_range      ,)






			def 					UpperCAmelCase							(self  :	List[Any]			)       ->	Tuple:





				"""simple docstring"""


				(
				    (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       
				)												=  self.prepare_config_and_inputs()

				lowerCAmelCase												=  True
				lowerCAmelCase												=  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
				lowerCAmelCase												=  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 					UpperCAmelCase							(self  :	Optional[int]      ,SCREAMING_SNAKE_CASE_  :	int      ,SCREAMING_SNAKE_CASE_  :	str      ,SCREAMING_SNAKE_CASE_  :	str      ,SCREAMING_SNAKE_CASE_  :	Any      ,**SCREAMING_SNAKE_CASE_  :	Optional[Any]      ,)       ->	Optional[int]:





				"""simple docstring"""


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






			def 					UpperCAmelCase							(self  :	Dict      ,SCREAMING_SNAKE_CASE_  :	Dict      ,SCREAMING_SNAKE_CASE_  :	Optional[int]      ,SCREAMING_SNAKE_CASE_  :	Tuple      ,SCREAMING_SNAKE_CASE_  :	Any      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	List[str]      ,**SCREAMING_SNAKE_CASE_  :	Any      ,)       ->	Dict:





				"""simple docstring"""


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






			def 					UpperCAmelCase							(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	List[str]      ,SCREAMING_SNAKE_CASE_  :	Any      ,SCREAMING_SNAKE_CASE_  :	Dict      ,SCREAMING_SNAKE_CASE_  :	Tuple      ,SCREAMING_SNAKE_CASE_  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	Any      ,**SCREAMING_SNAKE_CASE_  :	Optional[int]      ,)       ->	Any:





				"""simple docstring"""


				lowerCAmelCase												=  True
				lowerCAmelCase												=  True
				lowerCAmelCase												=  BertGenerationDecoder(config=SCREAMING_SNAKE_CASE_			).to(SCREAMING_SNAKE_CASE_			).eval()

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

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

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

				lowerCAmelCase												=  model(
				    SCREAMING_SNAKE_CASE_      ,attention_mask=SCREAMING_SNAKE_CASE_      ,encoder_hidden_states=SCREAMING_SNAKE_CASE_      ,encoder_attention_mask=SCREAMING_SNAKE_CASE_      ,output_hidden_states=SCREAMING_SNAKE_CASE_      ,)['''hidden_states'''][0]
				lowerCAmelCase												=  model(
				    SCREAMING_SNAKE_CASE_      ,attention_mask=SCREAMING_SNAKE_CASE_      ,encoder_hidden_states=SCREAMING_SNAKE_CASE_      ,encoder_attention_mask=SCREAMING_SNAKE_CASE_      ,past_key_values=SCREAMING_SNAKE_CASE_      ,output_hidden_states=SCREAMING_SNAKE_CASE_      ,)['''hidden_states'''][0]

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






			def 					UpperCAmelCase							(self  :	int      ,SCREAMING_SNAKE_CASE_  :	Dict      ,SCREAMING_SNAKE_CASE_  :	List[str]      ,SCREAMING_SNAKE_CASE_  :	Tuple      ,SCREAMING_SNAKE_CASE_  :	Dict      ,*SCREAMING_SNAKE_CASE_  :	Optional[Any]      ,)       ->	Any:





				"""simple docstring"""


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






			def 					UpperCAmelCase							(self  :	int			)       ->	Tuple:





				"""simple docstring"""


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





@require_torch
class 			lowercase					( lowercase__   ,lowercase__   ,lowercase__   ,unittest.TestCase		):
			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 					UpperCAmelCase							(self  :	Optional[Any]			)       ->	Union[str, Any]:





				"""simple docstring"""


				lowerCAmelCase												=  BertGenerationEncoderTester(self			)
				lowerCAmelCase												=  ConfigTester(self      ,config_class=SCREAMING_SNAKE_CASE_      ,hidden_size=37			)






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





				"""simple docstring"""


				self.config_tester.run_common_tests()






			def 					UpperCAmelCase							(self  :	Optional[int]			)       ->	List[Any]:





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Optional[int]			)       ->	Dict:





				"""simple docstring"""


				lowerCAmelCase							,       lowerCAmelCase							,       lowerCAmelCase							,       lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
				lowerCAmelCase												=  '''bert'''
				self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Tuple			)       ->	Optional[int]:





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_model_as_decoder(*SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Any			)       ->	Any:





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	str			)       ->	Optional[int]:





				"""simple docstring"""


				(
				    (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       (
				    lowerCAmelCase
				)							,       
				)												=  self.model_tester.prepare_config_and_inputs_for_decoder()

				lowerCAmelCase												=  None

				self.model_tester.create_and_check_model_as_decoder(
				    SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,)






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





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_for_causal_lm(*SCREAMING_SNAKE_CASE_			)






			@slow
			def 					UpperCAmelCase							(self  :	str			)       ->	int:





				"""simple docstring"""


				lowerCAmelCase												=  BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''			)
				self.assertIsNotNone(SCREAMING_SNAKE_CASE_			)





@require_torch
class 			lowercase					( unittest.TestCase		):






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





				"""simple docstring"""


				lowerCAmelCase												=  BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''			)
				lowerCAmelCase												=  torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]			)
				with torch.no_grad():
					lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			)[0]
				lowerCAmelCase												=  torch.Size([1, 8, 1_024]			)
				self.assertEqual(output.shape      ,SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  torch.tensor(
				    [[[0.17_75, 0.00_83, -0.03_21], [1.60_02, 0.12_87, 0.39_12], [2.14_73, 0.57_91, 0.60_66]]]			)
				self.assertTrue(torch.allclose(output[:, :3, :3]      ,SCREAMING_SNAKE_CASE_      ,atol=1e-4			)			)





@require_torch
class 			lowercase					( unittest.TestCase		):






			@slow
			def 					UpperCAmelCase							(self  :	Dict			)       ->	Any:





				"""simple docstring"""


				lowerCAmelCase												=  BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''			)
				lowerCAmelCase												=  torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]			)
				with torch.no_grad():
					lowerCAmelCase												=  model(SCREAMING_SNAKE_CASE_			)[0]
				lowerCAmelCase												=  torch.Size([1, 8, 50_358]			)
				self.assertEqual(output.shape      ,SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  torch.tensor(
				    [[[-0.57_88, -2.59_94, -3.70_54], [0.04_38, 4.79_97, 1.87_95], [1.58_62, 6.64_09, 4.46_38]]]			)
				self.assertTrue(torch.allclose(output[:, :3, :3]      ,SCREAMING_SNAKE_CASE_      ,atol=1e-4			)			)




"""simple docstring"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding



class 			lowercase					( lowercase__		):
			lowercase        =     ['''image_processor''', '''tokenizer''']
			lowercase        =     '''CLIPImageProcessor'''
			lowercase        =     ('''XLMRobertaTokenizer''', '''XLMRobertaTokenizerFast''')






			def __init__(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	int=None      ,SCREAMING_SNAKE_CASE_  :	str=None      ,**SCREAMING_SNAKE_CASE_  :	Union[str, Any]			)       ->	int:





				"""simple docstring"""


				lowerCAmelCase												=  None
				if "feature_extractor" in kwargs:
					warnings.warn(
					    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
					    ''' instead.'''      ,SCREAMING_SNAKE_CASE_      ,)
					lowerCAmelCase												=  kwargs.pop('''feature_extractor'''			)

				lowerCAmelCase												=  image_processor if image_processor is not None else feature_extractor
				if image_processor is None:
					raise ValueError('''You need to specify an `image_processor`.'''			)
				if tokenizer is None:
					raise ValueError('''You need to specify a `tokenizer`.'''			)

				super().__init__(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)






			def __call__(self  :	Union[str, Any]      ,SCREAMING_SNAKE_CASE_  :	List[str]=None      ,SCREAMING_SNAKE_CASE_  :	Optional[int]=None      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]=None      ,**SCREAMING_SNAKE_CASE_  :	Any			)       ->	List[Any]:





				"""simple docstring"""


				if text is None and images is None:
					raise ValueError('''You have to specify either text or images. Both cannot be none.'''			)

				if text is not None:
					lowerCAmelCase												=  self.tokenizer(SCREAMING_SNAKE_CASE_      ,return_tensors=SCREAMING_SNAKE_CASE_      ,**SCREAMING_SNAKE_CASE_			)

				if images is not None:
					lowerCAmelCase												=  self.image_processor(SCREAMING_SNAKE_CASE_      ,return_tensors=SCREAMING_SNAKE_CASE_      ,**SCREAMING_SNAKE_CASE_			)

				if text is not None and images is not None:
					lowerCAmelCase												=  image_features.pixel_values
					return encoding
				elif text is not None:
					return encoding
				else:
					return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE_			)      ,tensor_type=SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	List[str]      ,*SCREAMING_SNAKE_CASE_  :	Union[str, Any]      ,**SCREAMING_SNAKE_CASE_  :	Tuple			)       ->	Optional[Any]:





				"""simple docstring"""


				return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_      ,**SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Union[str, Any]      ,*SCREAMING_SNAKE_CASE_  :	int      ,**SCREAMING_SNAKE_CASE_  :	Union[str, Any]			)       ->	Any:





				"""simple docstring"""


				return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_      ,**SCREAMING_SNAKE_CASE_			)






			@property
			def 					UpperCAmelCase							(self  :	List[Any]			)       ->	Dict:





				"""simple docstring"""


				lowerCAmelCase												=  self.tokenizer.model_input_names
				lowerCAmelCase												=  self.image_processor.model_input_names
				return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names			)			)


