






import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
			from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor
class  lowerCAmelCase_       :




							def __init__(   self     :   int ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Any=13 ,							SCREAMING_SNAKE_CASE_     :   List[str]=32 ,							SCREAMING_SNAKE_CASE_     :   int=3 ,							SCREAMING_SNAKE_CASE_     :   Tuple=4 ,							SCREAMING_SNAKE_CASE_     :   Tuple=[10, 20, 30, 40] ,							SCREAMING_SNAKE_CASE_     :   List[Any]=[2, 2, 3, 2] ,							SCREAMING_SNAKE_CASE_     :   Dict=True ,							SCREAMING_SNAKE_CASE_     :   List[str]=True ,							SCREAMING_SNAKE_CASE_     :   Dict=37 ,							SCREAMING_SNAKE_CASE_     :   Tuple="gelu" ,							SCREAMING_SNAKE_CASE_     :   Dict=10 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=0.02 ,							SCREAMING_SNAKE_CASE_     :   Tuple=["stage2", "stage3", "stage4"] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=[2, 3, 4] ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=None ,							):
											lowerCAmelCase__      			=						parent
											lowerCAmelCase__      			=						batch_size
											lowerCAmelCase__      			=						image_size
											lowerCAmelCase__      			=						num_channels
											lowerCAmelCase__      			=						num_stages
											lowerCAmelCase__      			=						hidden_sizes
											lowerCAmelCase__      			=						depths
											lowerCAmelCase__      			=						is_training
											lowerCAmelCase__      			=						use_labels
											lowerCAmelCase__      			=						intermediate_size
											lowerCAmelCase__      			=						hidden_act
											lowerCAmelCase__      			=						num_labels
											lowerCAmelCase__      			=						initializer_range
											lowerCAmelCase__      			=						out_features
											lowerCAmelCase__      			=						out_indices
											lowerCAmelCase__      			=						scope




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

											lowerCAmelCase__      			=						None
											if self.use_labels:
															lowerCAmelCase__      			=						ids_tensor([self.batch_size] ,							self.num_labels	)

											lowerCAmelCase__      			=						self.get_config()

											return config, pixel_values, labels




							def 						__snake_case					(   self     :   Dict	):
											return ConvNextVaConfig(
											    num_channels=self.num_channels ,							hidden_sizes=self.hidden_sizes ,							depths=self.depths ,							num_stages=self.num_stages ,							hidden_act=self.hidden_act ,							is_decoder=SCREAMING_SNAKE_CASE_ ,							initializer_range=self.initializer_range ,							out_features=self.out_features ,							out_indices=self.out_indices ,							num_labels=self.num_labels ,							)




							def 						__snake_case					(   self     :   str ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   List[Any]	):
											lowerCAmelCase__      			=						ConvNextVaModel(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											# expected last hidden states: B, C, H // 32, W // 32
											self.parent.assertEqual(
											    result.last_hidden_state.shape ,							(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) ,							)




							def 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   str	):
											lowerCAmelCase__      			=						ConvNextVaForImageClassification(SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							labels=SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)	)




							def 						__snake_case					(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   str	):
											lowerCAmelCase__      			=						ConvNextVaBackbone(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)

											# verify hidden states
											self.parent.assertEqual(len(result.feature_maps	) ,							len(config.out_features	)	)
											self.parent.assertListEqual(list(result.feature_maps[0].shape	) ,							[self.batch_size, self.hidden_sizes[1], 4, 4]	)

											# verify channels
											self.parent.assertEqual(len(model.channels	) ,							len(config.out_features	)	)
											self.parent.assertListEqual(model.channels ,							config.hidden_sizes[1:]	)

											# verify backbone works with out_features=None
											lowerCAmelCase__      			=						None
											lowerCAmelCase__      			=						ConvNextVaBackbone(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)

											# verify feature maps
											self.parent.assertEqual(len(result.feature_maps	) ,							1	)
											self.parent.assertListEqual(list(result.feature_maps[0].shape	) ,							[self.batch_size, self.hidden_sizes[-1], 1, 1]	)

											# verify channels
											self.parent.assertEqual(len(model.channels	) ,							1	)
											self.parent.assertListEqual(model.channels ,							[config.hidden_sizes[-1]]	)




							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						config_and_inputs
											lowerCAmelCase__      			=						{'''pixel_values''': pixel_values}
											return config, inputs_dict




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						config_and_inputs
											lowerCAmelCase__      			=						{'''pixel_values''': pixel_values, '''labels''': labels}
											return config, inputs_dict



@require_torch
class  lowerCAmelCase_       (				snake_case__     ,    snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:List[str]		      =    (
							    (
							        ConvNextVaModel,
							        ConvNextVaForImageClassification,
							        ConvNextVaBackbone,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCamelCase_		:List[str]		      =    (
							    {'feature-extraction': ConvNextVaModel, 'image-classification': ConvNextVaForImageClassification}
							    if is_torch_available()
							    else {}
							)

							UpperCamelCase_		:Optional[int]		      =    False
							UpperCamelCase_		:str		      =    False
							UpperCamelCase_		:str		      =    False
							UpperCamelCase_		:Dict		      =    False
							UpperCamelCase_		:Dict		      =    False




							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__      			=						ConvNextVaModelTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=SCREAMING_SNAKE_CASE_ ,							has_text_modality=SCREAMING_SNAKE_CASE_ ,							hidden_size=37	)




							def 						__snake_case					(   self     :   int	):
											self.create_and_test_config_common_properties()
											self.config_tester.create_and_test_config_to_json_string()
											self.config_tester.create_and_test_config_to_json_file()
											self.config_tester.create_and_test_config_from_and_save_pretrained()
											self.config_tester.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()




							def 						__snake_case					(   self     :   Any	):
											return




							@unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''	)
							def 						__snake_case					(   self     :   List[Any]	):
											pass




							@unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''	)
							def 						__snake_case					(   self     :   Union[str, Any]	):
											pass




							@unittest.skip(reason='''ConvNextV2 does not use feedforward chunking'''	)
							def 						__snake_case					(   self     :   Optional[Any]	):
											pass




							def 						__snake_case					(   self     :   Dict	):
											if not self.model_tester.is_training:
															return

											for model_class in self.all_model_classes:
															lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_with_labels()
															lowerCAmelCase__      			=						True

															if model_class.__name__ in [
															    *get_values(SCREAMING_SNAKE_CASE_	),
															    *get_values(SCREAMING_SNAKE_CASE_	),
															]:
																			continue

															lowerCAmelCase__      			=						model_class(SCREAMING_SNAKE_CASE_	)
															model.to(SCREAMING_SNAKE_CASE_	)
															model.train()
															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							return_labels=SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						model(**SCREAMING_SNAKE_CASE_	).loss
															loss.backward()




							def 						__snake_case					(   self     :   Union[str, Any]	):
											if not self.model_tester.is_training:
															return

											for model_class in self.all_model_classes:
															lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_with_labels()
															lowerCAmelCase__      			=						False
															lowerCAmelCase__      			=						True

															if (
															    model_class.__name__
															    in [*get_values(SCREAMING_SNAKE_CASE_	), *get_values(SCREAMING_SNAKE_CASE_	)]
															    or not model_class.supports_gradient_checkpointing
															):
																			continue

															lowerCAmelCase__      			=						model_class(SCREAMING_SNAKE_CASE_	)
															model.to(SCREAMING_SNAKE_CASE_	)
															model.gradient_checkpointing_enable()
															model.train()
															lowerCAmelCase__      			=						self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							return_labels=SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						model(**SCREAMING_SNAKE_CASE_	).loss
															loss.backward()




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															lowerCAmelCase__      			=						model_class(SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						inspect.signature(model.forward	)
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															lowerCAmelCase__      			=						[*signature.parameters.keys()]

															lowerCAmelCase__      			=						['''pixel_values''']
															self.assertListEqual(arg_names[:1] ,							SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Optional[int]	):
											def check_hidden_states_output(SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   List[Any]	):
															lowerCAmelCase__      			=						model_class(SCREAMING_SNAKE_CASE_	)
															model.to(SCREAMING_SNAKE_CASE_	)
															model.eval()

															with torch.no_grad():
																			lowerCAmelCase__      			=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)	)

															lowerCAmelCase__      			=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															lowerCAmelCase__      			=						self.model_tester.num_stages
															self.assertEqual(len(SCREAMING_SNAKE_CASE_	) ,							expected_num_stages + 1	)

															# ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
															self.assertListEqual(
															    list(hidden_states[0].shape[-2:]	) ,							[self.model_tester.image_size // 4, self.model_tester.image_size // 4] ,							)

											lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															lowerCAmelCase__      			=						True
															check_hidden_states_output(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															lowerCAmelCase__      			=						True

															check_hidden_states_output(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_	)




							@slow
							def 						__snake_case					(   self     :   Any	):
											for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCAmelCase__      			=						ConvNextVaModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
															self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)





def 	lowerCAmelCase_		()		->    Tuple:


				'''simple docstring'''




				lowerCAmelCase__      			=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
				return image




@require_torch
@require_vision
class  lowerCAmelCase_       (				unittest.TestCase					):




							@cached_property
							def 						__snake_case					(   self     :   Union[str, Any]	):
											return AutoImageProcessor.from_pretrained('''facebook/convnextv2-tiny-1k-224'''	) if is_vision_available() else None




							@slow
							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''	).to(SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						self.default_image_processor
											lowerCAmelCase__      			=						prepare_img()
											lowerCAmelCase__      			=						preprocessor(images=SCREAMING_SNAKE_CASE_ ,							return_tensors='''pt'''	).to(SCREAMING_SNAKE_CASE_	)

											# forward pass
											with torch.no_grad():
															lowerCAmelCase__      			=						model(**SCREAMING_SNAKE_CASE_	)

											# verify the logits
											lowerCAmelCase__      			=						torch.Size((1, 1_000)	)
											self.assertEqual(outputs.logits.shape ,							SCREAMING_SNAKE_CASE_	)

											lowerCAmelCase__      			=						torch.tensor([0.9_996, 0.1_966, -0.4_386]	).to(SCREAMING_SNAKE_CASE_	)
											self.assertTrue(torch.allclose(outputs.logits[0, :3] ,							SCREAMING_SNAKE_CASE_ ,							atol=1e-4	)	)













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




							def __init__(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=13 ,							SCREAMING_SNAKE_CASE_     :   Dict=7 ,							SCREAMING_SNAKE_CASE_     :   Dict=True ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=True ,							SCREAMING_SNAKE_CASE_     :   List[str]=99 ,							SCREAMING_SNAKE_CASE_     :   str=32 ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=5 ,							SCREAMING_SNAKE_CASE_     :   str=4 ,							SCREAMING_SNAKE_CASE_     :   Tuple=37 ,							SCREAMING_SNAKE_CASE_     :   List[Any]="gelu" ,							SCREAMING_SNAKE_CASE_     :   Tuple=0.1 ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=0.1 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=50 ,							SCREAMING_SNAKE_CASE_     :   List[str]=0.02 ,							SCREAMING_SNAKE_CASE_     :   List[str]=True ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=None ,							):
											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 						__snake_case					(   self     :   Union[str, Any]	):
											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 						__snake_case					(   self     :   List[str]	):
											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 						__snake_case					(   self     :   str	):
											(
											    (
											    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 						__snake_case					(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							**SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							):
											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 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							**SCREAMING_SNAKE_CASE_     :   str ,							):
											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 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							**SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							):
											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 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							*SCREAMING_SNAKE_CASE_     :   Tuple ,							):
											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 						__snake_case					(   self     :   List[Any]	):
											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  lowerCAmelCase_       (				snake_case__     ,    snake_case__     ,    snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Dict		      =    (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
							UpperCamelCase_		:str		      =    (BertGenerationDecoder,) if is_torch_available() else ()
							UpperCamelCase_		:List[str]		      =    (
							    {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
							    if is_torch_available()
							    else {}
							)




							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						BertGenerationEncoderTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=SCREAMING_SNAKE_CASE_ ,							hidden_size=37	)




							def 						__snake_case					(   self     :   Optional[Any]	):
											self.config_tester.run_common_tests()




							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   str	):
											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 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   List[Any]	):
											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 						__snake_case					(   self     :   Any	):
											# This regression test was failing with PyTorch < 1.3
											(
											    (
											    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 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_for_causal_lm(*SCREAMING_SNAKE_CASE_	)




							@slow
							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''	)
											self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)



@require_torch
class  lowerCAmelCase_       (				unittest.TestCase					):




							@slow
							def 						__snake_case					(   self     :   Tuple	):
											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.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] ,							SCREAMING_SNAKE_CASE_ ,							atol=1e-4	)	)



@require_torch
class  lowerCAmelCase_       (				unittest.TestCase					):




							@slow
							def 						__snake_case					(   self     :   Any	):
											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.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] ,							SCREAMING_SNAKE_CASE_ ,							atol=1e-4	)	)





