






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







		'''simple docstring'''



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



		def 					__UpperCAmelCase			(							self   )	->							Optional[int]:
							UpperCAmelCase_    :      Tuple            =						ids_tensor([self.batch_size, self.seq_length]				,		self.vocab_size   )

							UpperCAmelCase_    :      Optional[Any]            =						None
							if self.use_input_mask:
												UpperCAmelCase_    :      Dict            =						random_attention_mask([self.batch_size, self.seq_length]   )

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

							UpperCAmelCase_    :      List[Any]            =						self.get_config()

							return config, input_ids, input_mask, token_labels



		def 					__UpperCAmelCase			(							self   )	->							Dict:
							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=_UpperCamelCase				,		initializer_range=self.initializer_range				,		)



		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							(
							    (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  
							)    :      Union[str, Any]            =						self.prepare_config_and_inputs()

							UpperCAmelCase_    :      Optional[Any]            =						True
							UpperCAmelCase_    :      Optional[int]            =						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 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		**_UpperCamelCase				,		)	->							Optional[Any]:
							UpperCAmelCase_    :      List[Any]            =						BertGenerationEncoder(config=_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      Tuple            =						model(_UpperCamelCase				,		attention_mask=_UpperCamelCase   )
							UpperCAmelCase_    :      str            =						model(_UpperCamelCase   )
							self.parent.assertEqual(result.last_hidden_state.shape				,		(self.batch_size, self.seq_length, self.hidden_size)   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		**_UpperCamelCase				,		)	->							Tuple:
							UpperCAmelCase_    :      Union[str, Any]            =						True
							UpperCAmelCase_    :      Optional[Any]            =						BertGenerationEncoder(config=_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      Tuple            =						model(
							    _UpperCamelCase				,		attention_mask=_UpperCamelCase				,		encoder_hidden_states=_UpperCamelCase				,		encoder_attention_mask=_UpperCamelCase				,		)
							UpperCAmelCase_    :      str            =						model(
							    _UpperCamelCase				,		attention_mask=_UpperCamelCase				,		encoder_hidden_states=_UpperCamelCase				,		)
							self.parent.assertEqual(result.last_hidden_state.shape				,		(self.batch_size, self.seq_length, self.hidden_size)   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		**_UpperCamelCase				,		)	->							Any:
							UpperCAmelCase_    :      List[str]            =						True
							UpperCAmelCase_    :      Union[str, Any]            =						True
							UpperCAmelCase_    :      Optional[int]            =						BertGenerationDecoder(config=_UpperCamelCase   ).to(_UpperCamelCase   ).eval()

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

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

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

							UpperCAmelCase_    :      List[str]            =						model(
							    _UpperCamelCase				,		attention_mask=_UpperCamelCase				,		encoder_hidden_states=_UpperCamelCase				,		encoder_attention_mask=_UpperCamelCase				,		output_hidden_states=_UpperCamelCase				,		)['hidden_states'][0]
							UpperCAmelCase_    :      List[Any]            =						model(
							    _UpperCamelCase				,		attention_mask=_UpperCamelCase				,		encoder_hidden_states=_UpperCamelCase				,		encoder_attention_mask=_UpperCamelCase				,		past_key_values=_UpperCamelCase				,		output_hidden_states=_UpperCamelCase				,		)['hidden_states'][0]

							# select random slice
							UpperCAmelCase_    :      Union[str, Any]            =						ids_tensor((1,)				,		output_from_past.shape[-1]   ).item()
							UpperCAmelCase_    :      Optional[Any]            =						output_from_no_past[:, -3:, random_slice_idx].detach()
							UpperCAmelCase_    :      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(_UpperCamelCase				,		_UpperCamelCase				,		atol=1E-3   )   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		*_UpperCamelCase				,		)	->							Union[str, Any]:
							UpperCAmelCase_    :      int            =						BertGenerationDecoder(_UpperCamelCase   )
							model.to(_UpperCamelCase   )
							model.eval()
							UpperCAmelCase_    :      Tuple            =						model(_UpperCamelCase				,		attention_mask=_UpperCamelCase				,		labels=_UpperCamelCase   )
							self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.seq_length, self.vocab_size)   )



		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							UpperCAmelCase_       ,  UpperCAmelCase_       ,  UpperCAmelCase_       ,  UpperCAmelCase_    :      Optional[Any]            =						self.prepare_config_and_inputs()
							UpperCAmelCase_    :      List[str]            =						{'input_ids': input_ids, 'attention_mask': input_mask}
							return config, inputs_dict





@require_torch
class   lowerCamelCase      (_snake_case ,  _snake_case ,  _snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    str       						=  (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
		_snake_case    :    List[Any]       						=  (BertGenerationDecoder,) if is_torch_available() else ()
		_snake_case    :    int       						=  (
		    {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
		    if is_torch_available()
		    else {}
		)



		def 					__UpperCAmelCase			(							self   )	->							Dict:
							UpperCAmelCase_    :      Tuple            =						BertGenerationEncoderTester(self   )
							UpperCAmelCase_    :      Tuple            =						ConfigTester(self				,		config_class=_UpperCamelCase				,		hidden_size=3_7   )



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							self.config_tester.run_common_tests()



		def 					__UpperCAmelCase			(							self   )	->							List[str]:
							UpperCAmelCase_    :      List[str]            =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							UpperCAmelCase_       ,  UpperCAmelCase_       ,  UpperCAmelCase_       ,  UpperCAmelCase_    :      List[Any]            =						self.model_tester.prepare_config_and_inputs()
							UpperCAmelCase_    :      Tuple            =						'bert'
							self.model_tester.create_and_check_model(_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							UpperCAmelCase_    :      int            =						self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							UpperCAmelCase_    :      int            =						self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(*_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							# This regression test was failing with PyTorch < 1.3
							(
							    (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  (
							    UpperCAmelCase_
							)       ,  
							)    :      Union[str, Any]            =						self.model_tester.prepare_config_and_inputs_for_decoder()

							UpperCAmelCase_    :      str            =						None

							self.model_tester.create_and_check_model_as_decoder(
							    _UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		)



		def 					__UpperCAmelCase			(							self   )	->							int:
							UpperCAmelCase_    :      Optional[int]            =						self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_for_causal_lm(*_UpperCamelCase   )



		@slow
		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							UpperCAmelCase_    :      Tuple            =						BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'   )
							self.assertIsNotNone(_UpperCamelCase   )





@require_torch
class   lowerCamelCase      (unittest.TestCase			):







		'''simple docstring'''



		@slow
		def 					__UpperCAmelCase			(							self   )	->							List[str]:
							UpperCAmelCase_    :      Any            =						BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'   )
							UpperCAmelCase_    :      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():
												UpperCAmelCase_    :      Dict            =						model(_UpperCamelCase   )[0]
							UpperCAmelCase_    :      Dict            =						torch.Size([1, 8, 1_0_2_4]   )
							self.assertEqual(output.shape				,		_UpperCamelCase   )
							UpperCAmelCase_    :      Dict            =						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]				,		_UpperCamelCase				,		atol=1E-4   )   )





@require_torch
class   lowerCamelCase      (unittest.TestCase			):







		'''simple docstring'''



		@slow
		def 					__UpperCAmelCase			(							self   )	->							Optional[int]:
							UpperCAmelCase_    :      str            =						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_    :      str            =						model(_UpperCamelCase   )[0]
							UpperCAmelCase_    :      str            =						torch.Size([1, 8, 5_0_3_5_8]   )
							self.assertEqual(output.shape				,		_UpperCamelCase   )
							UpperCAmelCase_    :      Dict            =						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]				,		_UpperCamelCase				,		atol=1E-4   )   )














import warnings

from ...utils import logging
from .image_processing_imagegpt import ImageGPTImageProcessor


__UpperCAmelCase								=						logging.get_logger(__name__)



class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		def __init__(							self				,		*_UpperCamelCase				,		**_UpperCamelCase   )	->							None:
							warnings.warn(
							    'The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
							    ' Please use ImageGPTImageProcessor instead.'				,		_UpperCamelCase				,		)
							super().__init__(*_UpperCamelCase				,		**_UpperCamelCase   )






