





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






				def __init__(			self		:   List[str],			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Optional[int]=1_3,			lowerCAmelCase__		:   List[Any]=7,			lowerCAmelCase__		:   Optional[int]=True,			lowerCAmelCase__		:   Tuple=True,			lowerCAmelCase__		:   List[str]=9_9,			lowerCAmelCase__		:   Optional[Any]=3_2,			lowerCAmelCase__		:   List[Any]=5,			lowerCAmelCase__		:   Optional[int]=4,			lowerCAmelCase__		:   Tuple=3_7,			lowerCAmelCase__		:   Any="gelu",			lowerCAmelCase__		:   str=0.1,			lowerCAmelCase__		:   Union[str, Any]=0.1,			lowerCAmelCase__		:   Optional[Any]=5_0,			lowerCAmelCase__		:   List[Any]=0.02,			lowerCAmelCase__		:   List[str]=True,			lowerCAmelCase__		:   int=None,			)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   parent
										_UpperCamelCase      : int         =   batch_size
										_UpperCamelCase      : List[Any]         =   seq_length
										_UpperCamelCase      : List[str]         =   is_training
										_UpperCamelCase      : Union[str, Any]         =   use_input_mask
										_UpperCamelCase      : Dict         =   vocab_size
										_UpperCamelCase      : str         =   hidden_size
										_UpperCamelCase      : Optional[int]         =   num_hidden_layers
										_UpperCamelCase      : Optional[Any]         =   num_attention_heads
										_UpperCamelCase      : Any         =   intermediate_size
										_UpperCamelCase      : Tuple         =   hidden_act
										_UpperCamelCase      : Any         =   hidden_dropout_prob
										_UpperCamelCase      : int         =   attention_probs_dropout_prob
										_UpperCamelCase      : List[str]         =   max_position_embeddings
										_UpperCamelCase      : str         =   initializer_range
										_UpperCamelCase      : Optional[int]         =   use_labels
										_UpperCamelCase      : int         =   scope






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




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   ids_tensor([self.batch_size, self.seq_length],			self.vocab_size							)

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

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

										_UpperCamelCase      : Optional[int]         =   self.get_config()

										return config, input_ids, input_mask, token_labels






				def     snake_case     (			self		:   str							)     ->				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=lowerCAmelCase__,			initializer_range=self.initializer_range,			)






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




										'''simple docstring'''



										(
										    _UpperCamelCase							
										)      : Optional[Any]         =   self.prepare_config_and_inputs()

										_UpperCamelCase      : List[Any]         =   True
										_UpperCamelCase      : Optional[Any]         =   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
										_UpperCamelCase      : 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     snake_case     (			self		:   List[str],			lowerCAmelCase__		:   Dict,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   Optional[Any],			**lowerCAmelCase__		:   Dict,			)     ->				Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   BertGenerationEncoder(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										_UpperCamelCase      : List[Any]         =   model(lowerCAmelCase__,			attention_mask=lowerCAmelCase__							)
										_UpperCamelCase      : Dict         =   model(lowerCAmelCase__							)
										self.parent.assertEqual(result.last_hidden_state.shape,			(self.batch_size, self.seq_length, self.hidden_size)							)






				def     snake_case     (			self		:   List[Any],			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   Union[str, Any],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Optional[Any],			**lowerCAmelCase__		:   int,			)     ->				Optional[int]:




										'''simple docstring'''



										_UpperCamelCase      : str         =   True
										_UpperCamelCase      : Union[str, Any]         =   BertGenerationEncoder(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										_UpperCamelCase      : List[str]         =   model(
										    lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			encoder_hidden_states=lowerCAmelCase__,			encoder_attention_mask=lowerCAmelCase__,			)
										_UpperCamelCase      : Optional[Any]         =   model(
										    lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			encoder_hidden_states=lowerCAmelCase__,			)
										self.parent.assertEqual(result.last_hidden_state.shape,			(self.batch_size, self.seq_length, self.hidden_size)							)






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




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   True
										_UpperCamelCase      : List[Any]         =   True
										_UpperCamelCase      : str         =   BertGenerationDecoder(config=lowerCAmelCase__							).to(lowerCAmelCase__							).eval()

										# first forward pass
										_UpperCamelCase      : List[Any]         =   model(
										    lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			encoder_hidden_states=lowerCAmelCase__,			encoder_attention_mask=lowerCAmelCase__,			use_cache=lowerCAmelCase__,			)
										_UpperCamelCase      : str         =   outputs.past_key_values

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

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

										_UpperCamelCase      : Optional[Any]         =   model(
										    lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			encoder_hidden_states=lowerCAmelCase__,			encoder_attention_mask=lowerCAmelCase__,			output_hidden_states=lowerCAmelCase__,			)['''hidden_states'''][0]
										_UpperCamelCase      : Any         =   model(
										    lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			encoder_hidden_states=lowerCAmelCase__,			encoder_attention_mask=lowerCAmelCase__,			past_key_values=lowerCAmelCase__,			output_hidden_states=lowerCAmelCase__,			)['''hidden_states'''][0]

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






				def     snake_case     (			self		:   Optional[int],			lowerCAmelCase__		:   int,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Dict,			*lowerCAmelCase__		:   Dict,			)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : Tuple         =   BertGenerationDecoder(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()
										_UpperCamelCase      : Optional[Any]         =   model(lowerCAmelCase__,			attention_mask=lowerCAmelCase__,			labels=lowerCAmelCase__							)
										self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.seq_length, self.vocab_size)							)






				def     snake_case     (			self		:   str							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.prepare_config_and_inputs()
										_UpperCamelCase      : int         =   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
										return config, inputs_dict




@require_torch
class 			_a						(				_lowerCAmelCase     ,    _lowerCAmelCase     ,    _lowerCAmelCase     ,    unittest.TestCase    ):
				UpperCamelCase								=   (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
				UpperCamelCase								=   (BertGenerationDecoder,) if is_torch_available() else ()
				UpperCamelCase								=   (
				    {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
				    if is_torch_available()
				    else {}
				)






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




										'''simple docstring'''



										_UpperCamelCase      : Tuple         =   BertGenerationEncoderTester(self							)
										_UpperCamelCase      : str         =   ConfigTester(self,			config_class=lowerCAmelCase__,			hidden_size=3_7							)






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




										'''simple docstring'''



										self.config_tester.run_common_tests()






				def     snake_case     (			self		:   int							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__							)






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




										'''simple docstring'''



										_UpperCamelCase      : str         =   self.model_tester.prepare_config_and_inputs()
										_UpperCamelCase      : Tuple         =   '''bert'''
										self.model_tester.create_and_check_model(lowerCAmelCase__,			lowerCAmelCase__,			lowerCAmelCase__,			lowerCAmelCase__							)






				def     snake_case     (			self		:   Any							)     ->				List[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Optional[int]         =   self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_model_as_decoder(*lowerCAmelCase__							)






				def     snake_case     (			self		:   Dict							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_decoder_model_past_large_inputs(*lowerCAmelCase__							)






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




										'''simple docstring'''



										(
										    _UpperCamelCase							
										)      : Union[str, Any]         =   self.model_tester.prepare_config_and_inputs_for_decoder()

										_UpperCamelCase      : Union[str, Any]         =   None

										self.model_tester.create_and_check_model_as_decoder(
										    lowerCAmelCase__,			lowerCAmelCase__,			lowerCAmelCase__,			lowerCAmelCase__,			lowerCAmelCase__,			lowerCAmelCase__,			)






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




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase__							)






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




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
										self.assertIsNotNone(lowerCAmelCase__							)




@require_torch
class 			_a						(				unittest.TestCase    ):






				@slow
				def     snake_case     (			self		:   Tuple							)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
										_UpperCamelCase      : 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():
																_UpperCamelCase      : Optional[Any]         =   model(lowerCAmelCase__							)[0]
										_UpperCamelCase      : int         =   torch.Size([1, 8, 1_0_2_4]							)
										self.assertEqual(output.shape,			lowerCAmelCase__							)
										_UpperCamelCase      : Any         =   torch.tensor(
										    [[[0.1_775, 0.0_083, -0.0_321], [1.6_002, 0.1_287, 0.3_912], [2.1_473, 0.5_791, 0.6_066]]]							)
										self.assertTrue(torch.allclose(output[:, :3, :3],			lowerCAmelCase__,			atol=1e-4							)							)




@require_torch
class 			_a						(				unittest.TestCase    ):






				@slow
				def     snake_case     (			self		:   Dict							)     ->				Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase      : int         =   BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
										_UpperCamelCase      : Union[str, 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():
																_UpperCamelCase      : Tuple         =   model(lowerCAmelCase__							)[0]
										_UpperCamelCase      : Any         =   torch.Size([1, 8, 5_0_3_5_8]							)
										self.assertEqual(output.shape,			lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   torch.tensor(
										    [[[-0.5_788, -2.5_994, -3.7_054], [0.0_438, 4.7_997, 1.8_795], [1.5_862, 6.6_409, 4.4_638]]]							)
										self.assertTrue(torch.allclose(output[:, :3, :3],			lowerCAmelCase__,			atol=1e-4							)							)










"""simple docstring"""




from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class 			_a						(				_lowerCAmelCase    ):
				UpperCamelCase								=   ['''image_processor''', '''tokenizer''']
				UpperCamelCase								=   '''BlipImageProcessor'''
				UpperCamelCase								=   '''AutoTokenizer'''






				def __init__(			self		:   List[str],			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   Optional[int]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   False
										super().__init__(lowerCAmelCase__,			lowerCAmelCase__							)
										_UpperCamelCase      : Tuple         =   self.image_processor






				def __call__(			self		:   str,			lowerCAmelCase__		:   ImageInput = None,			lowerCAmelCase__		:   Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,			lowerCAmelCase__		:   bool = True,			lowerCAmelCase__		:   Union[bool, str, PaddingStrategy] = False,			lowerCAmelCase__		:   Union[bool, str, TruncationStrategy] = None,			lowerCAmelCase__		:   Optional[int] = None,			lowerCAmelCase__		:   int = 0,			lowerCAmelCase__		:   Optional[int] = None,			lowerCAmelCase__		:   Optional[bool] = None,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = True,			lowerCAmelCase__		:   Optional[Union[str, TensorType]] = None,			**lowerCAmelCase__		:   Optional[Any],			)     ->				BatchEncoding:




										'''simple docstring'''



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

										# Get only text
										if images is None:
																_UpperCamelCase      : int         =   self.tokenizer
																_UpperCamelCase      : List[str]         =   self.tokenizer(
																    text=lowerCAmelCase__,			add_special_tokens=lowerCAmelCase__,			padding=lowerCAmelCase__,			truncation=lowerCAmelCase__,			max_length=lowerCAmelCase__,			stride=lowerCAmelCase__,			pad_to_multiple_of=lowerCAmelCase__,			return_attention_mask=lowerCAmelCase__,			return_overflowing_tokens=lowerCAmelCase__,			return_special_tokens_mask=lowerCAmelCase__,			return_offsets_mapping=lowerCAmelCase__,			return_token_type_ids=lowerCAmelCase__,			return_length=lowerCAmelCase__,			verbose=lowerCAmelCase__,			return_tensors=lowerCAmelCase__,			**lowerCAmelCase__,			)
																return text_encoding

										# add pixel_values
										_UpperCamelCase      : List[str]         =   self.image_processor(lowerCAmelCase__,			return_tensors=lowerCAmelCase__							)

										if text is not None:
																_UpperCamelCase      : Any         =   self.tokenizer(
																    text=lowerCAmelCase__,			add_special_tokens=lowerCAmelCase__,			padding=lowerCAmelCase__,			truncation=lowerCAmelCase__,			max_length=lowerCAmelCase__,			stride=lowerCAmelCase__,			pad_to_multiple_of=lowerCAmelCase__,			return_attention_mask=lowerCAmelCase__,			return_overflowing_tokens=lowerCAmelCase__,			return_special_tokens_mask=lowerCAmelCase__,			return_offsets_mapping=lowerCAmelCase__,			return_token_type_ids=lowerCAmelCase__,			return_length=lowerCAmelCase__,			verbose=lowerCAmelCase__,			return_tensors=lowerCAmelCase__,			**lowerCAmelCase__,			)
										else:
																_UpperCamelCase      : List[Any]         =   None

										if text_encoding is not None:
																encoding_image_processor.update(lowerCAmelCase__							)

										return encoding_image_processor






				def     snake_case     (			self		:   List[Any],			*lowerCAmelCase__		:   List[str],			**lowerCAmelCase__		:   str							)     ->				Optional[int]:




										'''simple docstring'''



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






				def     snake_case     (			self		:   List[Any],			*lowerCAmelCase__		:   Dict,			**lowerCAmelCase__		:   Any							)     ->				List[str]:




										'''simple docstring'''



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






				@property
				# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
				def     snake_case     (			self		:   Any							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.tokenizer.model_input_names
										_UpperCamelCase      : Union[str, Any]         =   self.image_processor.model_input_names
										return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)



