






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					:  int							,		__A					:  str							,		__A					:  str=1_3							,		__A					:  str=7							,		__A					:  str=True							,		__A					:  Union[str, Any]=True							,		__A					:  int=9_9							,		__A					:  Optional[Any]=3_2							,		__A					:  Dict=5							,		__A					:  List[Any]=4							,		__A					:  str=3_7							,		__A					:  Union[str, Any]="gelu"							,		__A					:  Optional[Any]=0.1							,		__A					:  Tuple=0.1							,		__A					:  int=5_0							,		__A					:  Any=0.0_2							,		__A					:  Optional[Any]=True							,		__A					:  Tuple=None							,		):
						snake_case__			:				Optional[Any]       =  parent
						snake_case__			:				List[Any]       =  batch_size
						snake_case__			:				Union[str, Any]       =  seq_length
						snake_case__			:				List[Any]       =  is_training
						snake_case__			:				int       =  use_input_mask
						snake_case__			:				Optional[Any]       =  vocab_size
						snake_case__			:				Optional[int]       =  hidden_size
						snake_case__			:				Dict       =  num_hidden_layers
						snake_case__			:				Dict       =  num_attention_heads
						snake_case__			:				Dict       =  intermediate_size
						snake_case__			:				List[str]       =  hidden_act
						snake_case__			:				Optional[Any]       =  hidden_dropout_prob
						snake_case__			:				Any       =  attention_probs_dropout_prob
						snake_case__			:				int       =  max_position_embeddings
						snake_case__			:				List[Any]       =  initializer_range
						snake_case__			:				Optional[Any]       =  use_labels
						snake_case__			:				Optional[int]       =  scope



					def 	_lowercase					( self					:  Optional[int]	):
						snake_case__			:				List[Any]       =  ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size	)

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

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

						snake_case__			:				Optional[Any]       =  self.get_config()

						return config, input_ids, input_mask, token_labels



					def 	_lowercase					( self					:  List[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=__A							,		initializer_range=self.initializer_range							,		)



					def 	_lowercase					( self					:  Union[str, Any]	):
						(
						    (
						    snake_case__
						),						(
						    snake_case__
						),						(
						    snake_case__
						),						(
						    snake_case__
						),						
						)			:				int       =  self.prepare_config_and_inputs()

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



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

						# first forward pass
						snake_case__			:				List[Any]       =  model(
						    __A							,		attention_mask=__A							,		encoder_hidden_states=__A							,		encoder_attention_mask=__A							,		use_cache=__A							,		)
						snake_case__			:				Optional[int]       =  outputs.past_key_values

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

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

						snake_case__			:				Union[str, Any]       =  model(
						    __A							,		attention_mask=__A							,		encoder_hidden_states=__A							,		encoder_attention_mask=__A							,		output_hidden_states=__A							,		)["hidden_states"][0]
						snake_case__			:				List[Any]       =  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__			:				Optional[Any]       =  ids_tensor((1,)							,		output_from_past.shape[-1]	).item()
						snake_case__			:				Dict       =  output_from_no_past[:, -3:, random_slice_idx].detach()
						snake_case__			:				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(__A							,		__A							,		atol=1e-3	)	)



					def 	_lowercase					( self					:  Any							,		__A					:  List[str]							,		__A					:  Any							,		__A					:  List[str]							,		__A					:  int							,		*__A					:  str							,		):
						snake_case__			:				str       =  BertGenerationDecoder(__A	)
						model.to(__A	)
						model.eval()
						snake_case__			:				List[str]       =  model(__A							,		attention_mask=__A							,		labels=__A	)
						self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.vocab_size)	)






					def 	_lowercase					( self					:  Optional[Any]	):
						snake_case__,						snake_case__,						snake_case__,						snake_case__			:				str       =  self.prepare_config_and_inputs()
						snake_case__			:				Tuple       =  {"input_ids": input_ids, "attention_mask": input_mask}
						return config, inputs_dict



@require_torch
class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_			,       UpperCamelCase_			,       UpperCamelCase_			,       unittest.TestCase						):





					"""simple docstring"""






					a_     =		(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
					a_     =		(BertGenerationDecoder,) if is_torch_available() else ()
					a_     =		(
					    {"feature-extraction": BertGenerationEncoder, "text-generation": BertGenerationDecoder}
					    if is_torch_available()
					    else {}
					)



					def 	_lowercase					( self					:  List[str]	):
						snake_case__			:				Union[str, Any]       =  BertGenerationEncoderTester(self	)
						snake_case__			:				int       =  ConfigTester(self							,		config_class=__A							,		hidden_size=3_7	)



					def 	_lowercase					( self					:  str	):
						self.config_tester.run_common_tests()



					def 	_lowercase					( self					:  Union[str, Any]	):
						snake_case__			:				Dict       =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__A	)



					def 	_lowercase					( self					:  List[str]	):
						snake_case__,						snake_case__,						snake_case__,						snake_case__			:				List[str]       =  self.model_tester.prepare_config_and_inputs()
						snake_case__			:				int       =  "bert"
						self.model_tester.create_and_check_model(__A							,		__A							,		__A							,		__A	)



					def 	_lowercase					( self					:  int	):
						snake_case__			:				Any       =  self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_model_as_decoder(*__A	)



					def 	_lowercase					( self					:  Optional[Any]	):
						snake_case__			:				List[str]       =  self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_decoder_model_past_large_inputs(*__A	)



					def 	_lowercase					( self					:  Tuple	):
						# This regression test was failing with PyTorch < 1.3
						(
						    (
						    snake_case__
						),						(
						    snake_case__
						),						(
						    snake_case__
						),						(
						    snake_case__
						),						(
						    snake_case__
						),						(
						    snake_case__
						),						
						)			:				Tuple       =  self.model_tester.prepare_config_and_inputs_for_decoder()

						snake_case__			:				Dict       =  None

						self.model_tester.create_and_check_model_as_decoder(
						    __A							,		__A							,		__A							,		__A							,		__A							,		__A							,		)



					def 	_lowercase					( self					:  Optional[Any]	):
						snake_case__			:				Union[str, Any]       =  self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_for_causal_lm(*__A	)






					@slow
					def 	_lowercase					( self					:  int	):
						snake_case__			:				Optional[Any]       =  BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"	)
						self.assertIsNotNone(__A	)



@require_torch
class 							SCREAMING_SNAKE_CASE__						(    unittest.TestCase						):





					"""simple docstring"""






					@slow
					def 	_lowercase					( self					:  List[Any]	):
						snake_case__			:				Any       =  BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"	)
						snake_case__			:				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():
							snake_case__			:				Tuple       =  model(__A	)[0]
						snake_case__			:				Union[str, Any]       =  torch.Size([1, 8, 1_0_2_4]	)
						self.assertEqual(output.shape							,		__A	)
						snake_case__			:				str       =  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 							SCREAMING_SNAKE_CASE__						(    unittest.TestCase						):





					"""simple docstring"""






					@slow
					def 	_lowercase					( self					:  Optional[Any]	):
						snake_case__			:				int       =  BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"	)
						snake_case__			:				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():
							snake_case__			:				Optional[int]       =  model(__A	)[0]
						snake_case__			:				int       =  torch.Size([1, 8, 5_0_3_5_8]	)
						self.assertEqual(output.shape							,		__A	)
						snake_case__			:				Optional[int]       =  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	)	)











import warnings
from typing import List, Optional, Union

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



class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_						):





					"""simple docstring"""






					a_     =		["image_processor", "tokenizer"]
					a_     =		"ViltImageProcessor"
					a_     =		("BertTokenizer", "BertTokenizerFast")



					def __init__( self					:  Optional[int]							,		__A					:  Optional[int]=None							,		__A					:  Optional[Any]=None							,		**__A					:  int	):
						snake_case__			:				Union[str, Any]       =  None
						if "feature_extractor" in kwargs:
							warnings.warn(
							    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
							    " instead."							,		__A							,		)
							snake_case__			:				Tuple       =  kwargs.pop("feature_extractor"	)

						snake_case__			:				Any       =  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__(__A							,		__A	)
						snake_case__			:				Tuple       =  self.image_processor



					def __call__( self					:  List[Any]							,		__A					:  int							,		__A					:  Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None							,		__A					:  bool = True							,		__A					:  Union[bool, str, PaddingStrategy] = False							,		__A					:  Union[bool, str, TruncationStrategy] = None							,		__A					:  Optional[int] = None							,		__A					:  int = 0							,		__A					:  Optional[int] = None							,		__A					:  Optional[bool] = None							,		__A					:  Optional[bool] = None							,		__A					:  bool = False							,		__A					:  bool = False							,		__A					:  bool = False							,		__A					:  bool = False							,		__A					:  bool = True							,		__A					:  Optional[Union[str, TensorType]] = None							,		**__A					:  List[Any]							,		):
						snake_case__			:				Optional[int]       =  self.tokenizer(
						    text=__A							,		add_special_tokens=__A							,		padding=__A							,		truncation=__A							,		max_length=__A							,		stride=__A							,		pad_to_multiple_of=__A							,		return_token_type_ids=__A							,		return_attention_mask=__A							,		return_overflowing_tokens=__A							,		return_special_tokens_mask=__A							,		return_offsets_mapping=__A							,		return_length=__A							,		verbose=__A							,		return_tensors=__A							,		**__A							,		)
						# add pixel_values + pixel_mask
						snake_case__			:				Optional[Any]       =  self.image_processor(__A							,		return_tensors=__A	)
						encoding.update(__A	)

						return encoding



					def 	_lowercase					( self					:  Optional[Any]							,		*__A					:  List[str]							,		**__A					:  Optional[int]	):
						return self.tokenizer.batch_decode(*__A							,		**__A	)



					def 	_lowercase					( self					:  Dict							,		*__A					:  str							,		**__A					:  str	):
						return self.tokenizer.decode(*__A							,		**__A	)



					@property
					def 	_lowercase					( self					:  str	):
						snake_case__			:				Optional[Any]       =  self.tokenizer.model_input_names
						snake_case__			:				Any       =  self.image_processor.model_input_names
						return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names	)	)



					@property
					def 	_lowercase					( self					:  List[Any]	):
						warnings.warn(
						    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."							,		__A							,		)
						return self.image_processor_class






					@property
					def 	_lowercase					( self					:  str	):
						warnings.warn(
						    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."							,		__A							,		)
						return self.image_processor



