


from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCAmelCase     :					Dict       =logging.get_logger(__name__)

_UpperCAmelCase     :					List[Any]       ={
    """google/pegasus-x-base""": """https://huggingface.co/google/pegasus-x-base/resolve/main/config.json""",
    """google/pegasus-x-large""": """https://huggingface.co/google/pegasus-x-large/resolve/main/config.json""",
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}







class 			snake_case__(							UpperCAmelCase__							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__ :					int   =							"""pegasus_x"""
						SCREAMING_SNAKE_CASE__ :					Dict   =							["""past_key_values"""]
						SCREAMING_SNAKE_CASE__ :					Union[str, Any]   =							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}




						def __init__(			self				,  __lowercase=9_6_1_0_3				,  __lowercase=1_6_3_8_4				,  __lowercase=1_6				,  __lowercase=4_0_9_6				,  __lowercase=1_6				,  __lowercase=1_6				,  __lowercase=4_0_9_6				,  __lowercase=1_6				,  __lowercase=0.0				,  __lowercase=0.0				,  __lowercase=True				,  __lowercase=True				,  __lowercase="gelu"				,  __lowercase=1_0_2_4				,  __lowercase=0.1				,  __lowercase=0.0				,  __lowercase=0.0				,  __lowercase=0.02				,  __lowercase=0				,  __lowercase=True				,  __lowercase=0				,  __lowercase=1				,  __lowercase=1				,  __lowercase=3_2				,  __lowercase=5_1_2				,  __lowercase=True				,  **__lowercase				,  )   ->      Tuple:
										lowerCAmelCase_	: Union[str, Any]      			=			vocab_size
										lowerCAmelCase_	: str      			=			max_position_embeddings
										lowerCAmelCase_	: Union[str, Any]      			=			d_model
										lowerCAmelCase_	: Optional[Any]      			=			encoder_ffn_dim
										lowerCAmelCase_	: Any      			=			encoder_layers
										lowerCAmelCase_	: int      			=			encoder_attention_heads
										lowerCAmelCase_	: str      			=			decoder_ffn_dim
										lowerCAmelCase_	: List[str]      			=			decoder_layers
										lowerCAmelCase_	: Any      			=			decoder_attention_heads
										lowerCAmelCase_	: Any      			=			dropout
										lowerCAmelCase_	: Optional[Any]      			=			attention_dropout
										lowerCAmelCase_	: Tuple      			=			activation_dropout
										lowerCAmelCase_	: Dict      			=			activation_function
										lowerCAmelCase_	: Dict      			=			init_std
										lowerCAmelCase_	: Dict      			=			encoder_layerdrop
										lowerCAmelCase_	: List[str]      			=			decoder_layerdrop
										lowerCAmelCase_	: Optional[int]      			=			use_cache
										lowerCAmelCase_	: List[Any]      			=			encoder_layers
										lowerCAmelCase_	: List[Any]      			=			scale_embedding  # scale factor will be sqrt(d_model) if True

										lowerCAmelCase_	: Optional[int]      			=			num_global_tokens
										lowerCAmelCase_	: Tuple      			=			block_size
										lowerCAmelCase_	: Optional[int]      			=			stagger_local_blocks

										super().__init__(
										    pad_token_id=__lowercase				,  eos_token_id=__lowercase				,  is_encoder_decoder=__lowercase				,  decoder_start_token_id=__lowercase				,  forced_eos_token_id=__lowercase				,  **__lowercase				,  )




						@property
						def   lowercase_	(			self							)   ->      int:
										return self.encoder_attention_heads




						@property
						def   lowercase_	(			self							)   ->      int:
										return self.d_model



from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
						from PIL import Image

						from transformers import ConvNextImageProcessor







class 			snake_case__:







						'''simple docstring'''




						def __init__(			self				,  __lowercase				,  __lowercase=1_3				,  __lowercase=3_2				,  __lowercase=3				,  __lowercase=4				,  __lowercase=[1_0, 2_0, 3_0, 4_0]				,  __lowercase=[2, 2, 3, 2]				,  __lowercase=True				,  __lowercase=True				,  __lowercase=3_7				,  __lowercase="gelu"				,  __lowercase=1_0				,  __lowercase=0.02				,  __lowercase=3				,  __lowercase=None				,  )   ->      List[str]:
										lowerCAmelCase_	: List[Any]      			=			parent
										lowerCAmelCase_	: int      			=			batch_size
										lowerCAmelCase_	: Optional[int]      			=			image_size
										lowerCAmelCase_	: Dict      			=			num_channels
										lowerCAmelCase_	: Dict      			=			num_stages
										lowerCAmelCase_	: Union[str, Any]      			=			hidden_sizes
										lowerCAmelCase_	: List[Any]      			=			depths
										lowerCAmelCase_	: str      			=			is_training
										lowerCAmelCase_	: Optional[int]      			=			use_labels
										lowerCAmelCase_	: List[str]      			=			intermediate_size
										lowerCAmelCase_	: int      			=			hidden_act
										lowerCAmelCase_	: Any      			=			type_sequence_label_size
										lowerCAmelCase_	: List[Any]      			=			initializer_range
										lowerCAmelCase_	: Union[str, Any]      			=			scope




						def   lowercase_	(			self							)   ->      List[Any]:
										lowerCAmelCase_	: str      			=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

										lowerCAmelCase_	: Any      			=			None
										if self.use_labels:
														lowerCAmelCase_	: Tuple      			=			ids_tensor([self.batch_size]				,  self.type_sequence_label_size							)

										lowerCAmelCase_	: str      			=			self.get_config()

										return config, pixel_values, labels




						def   lowercase_	(			self							)   ->      str:
										return ConvNextConfig(
										    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=__lowercase				,  initializer_range=self.initializer_range				,  )




						def   lowercase_	(			self				,  __lowercase				,  __lowercase				,  __lowercase							)   ->      str:
										lowerCAmelCase_	: List[str]      			=			TFConvNextModel(config=__lowercase							)
										lowerCAmelCase_	: str      			=			model(__lowercase				,  training=__lowercase							)
										# 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 // 3_2, self.image_size // 3_2)				,  )




						def   lowercase_	(			self				,  __lowercase				,  __lowercase				,  __lowercase							)   ->      Optional[int]:
										lowerCAmelCase_	: int      			=			self.type_sequence_label_size
										lowerCAmelCase_	: Dict      			=			TFConvNextForImageClassification(__lowercase							)
										lowerCAmelCase_	: Tuple      			=			model(__lowercase				,  labels=__lowercase				,  training=__lowercase							)
										self.parent.assertEqual(result.logits.shape				,  (self.batch_size, self.type_sequence_label_size)							)




						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: Optional[Any]      			=			self.prepare_config_and_inputs()
										lowerCAmelCase_			,					lowerCAmelCase_			,					lowerCAmelCase_	: Optional[int]      			=			config_and_inputs
										lowerCAmelCase_	: Optional[int]      			=			{'''pixel_values''': pixel_values}
										return config, inputs_dict







@require_tf
class 			snake_case__(							UpperCAmelCase__,  UpperCAmelCase__,  unittest.TestCase							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__ :					Optional[Any]   =							(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
						SCREAMING_SNAKE_CASE__ :					Dict   =							(
						    {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
						    if is_tf_available()
						    else {}
						)

						SCREAMING_SNAKE_CASE__ :					Optional[int]   =							False
						SCREAMING_SNAKE_CASE__ :					Optional[Any]   =							False
						SCREAMING_SNAKE_CASE__ :					str   =							False
						SCREAMING_SNAKE_CASE__ :					List[Any]   =							False
						SCREAMING_SNAKE_CASE__ :					Optional[Any]   =							False




						def   lowercase_	(			self							)   ->      str:
										lowerCAmelCase_	: Optional[int]      			=			TFConvNextModelTester(self							)
										lowerCAmelCase_	: Dict      			=			ConfigTester(
										    self				,  config_class=__lowercase				,  has_text_modality=__lowercase				,  hidden_size=3_7				,  )




						@unittest.skip(reason='''ConvNext does not use inputs_embeds'''							)
						def   lowercase_	(			self							)   ->      str:
										pass




						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''							)							) == 0				,  reason='''TF does not support backprop for grouped convolutions on CPU.'''				,  )
						@slow
						def   lowercase_	(			self							)   ->      Union[str, Any]:
										super().test_keras_fit()




						@unittest.skip(reason='''ConvNext does not support input and output embeddings'''							)
						def   lowercase_	(			self							)   ->      Any:
										pass




						def   lowercase_	(			self							)   ->      Dict:
										lowerCAmelCase_			,					lowerCAmelCase_	: Dict      			=			self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														lowerCAmelCase_	: List[str]      			=			model_class(__lowercase							)
														lowerCAmelCase_	: Dict      			=			inspect.signature(model.call							)
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														lowerCAmelCase_	: Union[str, Any]      			=			[*signature.parameters.keys()]

														lowerCAmelCase_	: int      			=			['''pixel_values''']
														self.assertListEqual(arg_names[:1]				,  __lowercase							)




						def   lowercase_	(			self							)   ->      List[str]:
										lowerCAmelCase_	: Any      			=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowercase							)




						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''							)							) == 0				,  reason='''TF does not support backprop for grouped convolutions on CPU.'''				,  )
						def   lowercase_	(			self							)   ->      List[Any]:
										super().test_dataset_conversion()




						def   lowercase_	(			self							)   ->      List[Any]:
										def check_hidden_states_output(__lowercase				,  __lowercase				,  __lowercase							):
														lowerCAmelCase_	: Union[str, Any]      			=			model_class(__lowercase							)

														lowerCAmelCase_	: str      			=			model(**self._prepare_for_class(__lowercase				,  __lowercase							)							)
														lowerCAmelCase_	: Optional[int]      			=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

														lowerCAmelCase_	: Any      			=			self.model_tester.num_stages
														self.assertEqual(len(__lowercase							)				,  expected_num_stages + 1							)

														# ConvNext'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_	: List[Any]      			=			self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														lowerCAmelCase_	: Tuple      			=			True
														check_hidden_states_output(__lowercase				,  __lowercase				,  __lowercase							)

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														lowerCAmelCase_	: str      			=			True

														check_hidden_states_output(__lowercase				,  __lowercase				,  __lowercase							)




						def   lowercase_	(			self							)   ->      Optional[int]:
										lowerCAmelCase_			,					lowerCAmelCase_	: str      			=			self.model_tester.prepare_config_and_inputs_for_common()

										def check_equivalence(__lowercase				,  __lowercase				,  __lowercase				,  __lowercase={}							):
														lowerCAmelCase_	: str      			=			model(__lowercase				,  return_dict=__lowercase				,  **__lowercase							)
														lowerCAmelCase_	: int      			=			model(__lowercase				,  return_dict=__lowercase				,  **__lowercase							).to_tuple()

														def recursive_check(__lowercase				,  __lowercase							):
																		if isinstance(__lowercase				,  (List, Tuple)							):
																						for tuple_iterable_value, dict_iterable_value in zip(__lowercase				,  __lowercase							):
																										recursive_check(__lowercase				,  __lowercase							)
																		elif tuple_object is None:
																						return
																		else:
																						self.assertTrue(
																						    all(tf.equal(__lowercase				,  __lowercase							)							)				,  msg=(
																						        '''Tuple and dict output are not equal. Difference:'''
																						        f""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object							)							)}"""
																						    )				,  )

																		recursive_check(__lowercase				,  __lowercase							)

										for model_class in self.all_model_classes:
														lowerCAmelCase_	: Optional[int]      			=			model_class(__lowercase							)

														lowerCAmelCase_	: Optional[int]      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														lowerCAmelCase_	: Tuple      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase							)

														lowerCAmelCase_	: Any      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														lowerCAmelCase_	: List[str]      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase							)

														lowerCAmelCase_	: List[str]      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														lowerCAmelCase_	: int      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase				,  {'''output_hidden_states''': True}							)

														lowerCAmelCase_	: Any      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														lowerCAmelCase_	: Optional[int]      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase				,  {'''output_hidden_states''': True}							)




						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: Dict      			=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__lowercase							)




						@slow
						def   lowercase_	(			self							)   ->      Union[str, Any]:
										lowerCAmelCase_	: Union[str, Any]      			=			TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224'''							)
										self.assertIsNotNone(__lowercase							)






def      lowerCAmelCase	(						)->	Optional[Any]:
				lowerCAmelCase_	: Dict      			=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
				return image







@require_tf
@require_vision
class 			snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						@cached_property
						def   lowercase_	(			self							)   ->      Union[str, Any]:
										return ConvNextImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''							) if is_vision_available() else None




						@slow
						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: Union[str, Any]      			=			TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''							)

										lowerCAmelCase_	: int      			=			self.default_image_processor
										lowerCAmelCase_	: Dict      			=			prepare_img()
										lowerCAmelCase_	: Optional[Any]      			=			image_processor(images=__lowercase				,  return_tensors='''tf'''							)

										# forward pass
										lowerCAmelCase_	: Any      			=			model(**__lowercase							)

										# verify the logits
										lowerCAmelCase_	: Union[str, Any]      			=			tf.TensorShape((1, 1_0_0_0)							)
										self.assertEqual(outputs.logits.shape				,  __lowercase							)

										lowerCAmelCase_	: List[str]      			=			tf.constant([-0.02_60, -0.47_39, 0.19_11]							)

										tf.debugging.assert_near(outputs.logits[0, :3]				,  __lowercase				,  atol=1e-4							)