import inspect
import unittest

from transformers import MobileNetVaConfig
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 MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
	from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import MobileNetVaImageProcessor

class        UpperCamelCase						(				__a				):





						def        A_							(self						)				->				Any:
												UpperCamelCase_		:							Any  =		self.config_class(**self.inputs_dict						)
												self.parent.assertTrue(hasattr(__UpperCamelCase			,     """tf_padding"""						)						)
												self.parent.assertTrue(hasattr(__UpperCamelCase			,     """depth_multiplier"""						)						)


class        UpperCamelCase						:





						def __init__(self			,     __UpperCamelCase			,     __UpperCamelCase=13			,     __UpperCamelCase=3			,     __UpperCamelCase=32			,     __UpperCamelCase=0.25			,     __UpperCamelCase=8			,     __UpperCamelCase=8			,     __UpperCamelCase=6			,     __UpperCamelCase=32			,     __UpperCamelCase=True			,     __UpperCamelCase=True			,     __UpperCamelCase=True			,     __UpperCamelCase="relu6"			,     __UpperCamelCase=1_280			,     __UpperCamelCase=0.1			,     __UpperCamelCase=0.02			,     __UpperCamelCase=True			,     __UpperCamelCase=True			,     __UpperCamelCase=10			,     __UpperCamelCase=None			,     )				->				Optional[int]:
												UpperCamelCase_		:							Union[str, Any]  =		parent
												UpperCamelCase_		:							List[str]  =		batch_size
												UpperCamelCase_		:							int  =		num_channels
												UpperCamelCase_		:							Any  =		image_size
												UpperCamelCase_		:							Any  =		depth_multiplier
												UpperCamelCase_		:							str  =		depth_divisible_by
												UpperCamelCase_		:							List[str]  =		min_depth
												UpperCamelCase_		:							List[str]  =		expand_ratio
												UpperCamelCase_		:							Optional[int]  =		tf_padding
												UpperCamelCase_		:							int  =		output_stride
												UpperCamelCase_		:							Union[str, Any]  =		first_layer_is_expansion
												UpperCamelCase_		:							Any  =		finegrained_output
												UpperCamelCase_		:							Tuple  =		hidden_act
												UpperCamelCase_		:							Dict  =		last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier						)
												UpperCamelCase_		:							Tuple  =		classifier_dropout_prob
												UpperCamelCase_		:							Union[str, Any]  =		use_labels
												UpperCamelCase_		:							Union[str, Any]  =		is_training
												UpperCamelCase_		:							int  =		num_labels
												UpperCamelCase_		:							Tuple  =		initializer_range
												UpperCamelCase_		:							List[Any]  =		scope





						def        A_							(self						)				->				Union[str, Any]:
												UpperCamelCase_		:							Optional[Any]  =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

												UpperCamelCase_		:							Optional[Any]  =		None
												UpperCamelCase_		:							Optional[Any]  =		None
												if self.use_labels:
																		UpperCamelCase_		:							List[Any]  =		ids_tensor([self.batch_size]			,     self.num_labels						)
																		UpperCamelCase_		:							str  =		ids_tensor([self.batch_size, self.image_size, self.image_size]			,     self.num_labels						)

												UpperCamelCase_		:							Any  =		self.get_config()

												return config, pixel_values, labels, pixel_labels





						def        A_							(self						)				->				List[str]:
												return MobileNetVaConfig(
												    num_channels=self.num_channels			,     image_size=self.image_size			,     depth_multiplier=self.depth_multiplier			,     depth_divisible_by=self.depth_divisible_by			,     min_depth=self.min_depth			,     expand_ratio=self.expand_ratio			,     output_stride=self.output_stride			,     first_layer_is_expansion=self.first_layer_is_expansion			,     finegrained_output=self.finegrained_output			,     hidden_act=self.hidden_act			,     tf_padding=self.tf_padding			,     classifier_dropout_prob=self.classifier_dropout_prob			,     initializer_range=self.initializer_range			,     )





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				List[Any]:
												UpperCamelCase_		:							Optional[Any]  =		MobileNetVaModel(config=__UpperCamelCase						)
												model.to(__UpperCamelCase						)
												model.eval()
												UpperCamelCase_		:							Union[str, Any]  =		model(__UpperCamelCase						)
												self.parent.assertEqual(
												    result.last_hidden_state.shape			,     (
												        self.batch_size,
												        self.last_hidden_size,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )			,     )
												self.parent.assertEqual(
												    result.pooler_output.shape			,     (self.batch_size, self.last_hidden_size)			,     )





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				Union[str, Any]:
												UpperCamelCase_		:							Tuple  =		self.num_labels
												UpperCamelCase_		:							Tuple  =		MobileNetVaForImageClassification(__UpperCamelCase						)
												model.to(__UpperCamelCase						)
												model.eval()
												UpperCamelCase_		:							Dict  =		model(__UpperCamelCase			,     labels=__UpperCamelCase						)
												self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				int:
												UpperCamelCase_		:							Dict  =		self.num_labels
												UpperCamelCase_		:							List[Any]  =		MobileNetVaForSemanticSegmentation(__UpperCamelCase						)
												model.to(__UpperCamelCase						)
												model.eval()
												UpperCamelCase_		:							Dict  =		model(__UpperCamelCase						)
												self.parent.assertEqual(
												    result.logits.shape			,     (
												        self.batch_size,
												        self.num_labels,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )			,     )
												UpperCamelCase_		:							List[str]  =		model(__UpperCamelCase			,     labels=__UpperCamelCase						)
												self.parent.assertEqual(
												    result.logits.shape			,     (
												        self.batch_size,
												        self.num_labels,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )			,     )





						def        A_							(self						)				->				Any:
												UpperCamelCase_		:							List[Any]  =		self.prepare_config_and_inputs()
												UpperCamelCase_,UpperCamelCase_,UpperCamelCase_,UpperCamelCase_		:							Optional[Any]  =		config_and_inputs
												UpperCamelCase_		:							Tuple  =		{"""pixel_values""": pixel_values}
												return config, inputs_dict


@require_torch
class        UpperCamelCase						(				__a	,	__a	,	unittest.TestCase				):
						a__		:Tuple      =			(
						    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
						    if is_torch_available()
						    else ()
						)
						a__		:Tuple      =			(
						    {
						        '''feature-extraction''': MobileNetVaModel,
						        '''image-classification''': MobileNetVaForImageClassification,
						        '''image-segmentation''': MobileNetVaForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						a__		:Union[str, Any]      =			False
						a__		:List[str]      =			False
						a__		:Dict      =			False
						a__		:List[str]      =			False





						def        A_							(self						)				->				List[Any]:
												UpperCamelCase_		:							List[Any]  =		MobileNetVaModelTester(self						)
												UpperCamelCase_		:							List[Any]  =		MobileNetVaConfigTester(self			,     config_class=__UpperCamelCase			,     has_text_modality=__UpperCamelCase						)





						def        A_							(self						)				->				Optional[Any]:
												self.config_tester.run_common_tests()





						@unittest.skip(reason="""MobileNetV2 does not use inputs_embeds"""						)
						def        A_							(self						)				->				Any:
												pass





						@unittest.skip(reason="""MobileNetV2 does not support input and output embeddings"""						)
						def        A_							(self						)				->				int:
												pass





						@unittest.skip(reason="""MobileNetV2 does not output attentions"""						)
						def        A_							(self						)				->				Any:
												pass





						def        A_							(self						)				->				Tuple:
												UpperCamelCase_,UpperCamelCase_		:							Tuple  =		self.model_tester.prepare_config_and_inputs_for_common()

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

																		UpperCamelCase_		:							List[str]  =		["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]			,     __UpperCamelCase						)





						def        A_							(self						)				->				List[str]:
												UpperCamelCase_		:							int  =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase						)





						def        A_							(self						)				->				Dict:
												def check_hidden_states_output(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						):
																		UpperCamelCase_		:							Dict  =		model_class(__UpperCamelCase						)
																		model.to(__UpperCamelCase						)
																		model.eval()

																		with torch.no_grad():
																								UpperCamelCase_		:							Optional[int]  =		model(**self._prepare_for_class(__UpperCamelCase			,     __UpperCamelCase						)						)

																		UpperCamelCase_		:							Optional[int]  =		outputs.hidden_states

																		UpperCamelCase_		:							Union[str, Any]  =		16
																		self.assertEqual(len(__UpperCamelCase						)			,     __UpperCamelCase						)

												UpperCamelCase_,UpperCamelCase_		:							Optional[Any]  =		self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		UpperCamelCase_		:							int  =		True
																		check_hidden_states_output(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		UpperCamelCase_		:							Optional[int]  =		True

																		check_hidden_states_output(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)





						def        A_							(self						)				->				Optional[Any]:
												UpperCamelCase_		:							Union[str, Any]  =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase						)





						def        A_							(self						)				->				Any:
												UpperCamelCase_		:							Dict  =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCamelCase						)





						@slow
						def        A_							(self						)				->				str:
												for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		UpperCamelCase_		:							List[str]  =		MobileNetVaModel.from_pretrained(__UpperCamelCase						)
																		self.assertIsNotNone(__UpperCamelCase						)






def     lowerCAmelCase_			(   ):
						UpperCamelCase_		:							Union[str, Any]  =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
						return image







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





						@cached_property
						def        A_							(self						)				->				List[str]:
												return (
												    MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""						) if is_vision_available() else None
												)





						@slow
						def        A_							(self						)				->				Optional[int]:
												UpperCamelCase_		:							Union[str, Any]  =		MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""						).to(__UpperCamelCase						)

												UpperCamelCase_		:							int  =		self.default_image_processor
												UpperCamelCase_		:							Optional[int]  =		prepare_img()
												UpperCamelCase_		:							str  =		image_processor(images=__UpperCamelCase			,     return_tensors="""pt"""						).to(__UpperCamelCase						)

												# forward pass
												with torch.no_grad():
																		UpperCamelCase_		:							str  =		model(**__UpperCamelCase						)

												# verify the logits
												UpperCamelCase_		:							Union[str, Any]  =		torch.Size((1, 1_001)						)
												self.assertEqual(outputs.logits.shape			,     __UpperCamelCase						)

												UpperCamelCase_		:							Union[str, Any]  =		torch.tensor([0.2_445, -1.1_993, 0.1_905]						).to(__UpperCamelCase						)

												self.assertTrue(torch.allclose(outputs.logits[0, :3]			,     __UpperCamelCase			,     atol=1E-4						)						)





						@slow
						def        A_							(self						)				->				Union[str, Any]:
												UpperCamelCase_		:							Tuple  =		MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""						)
												UpperCamelCase_		:							Union[str, Any]  =		model.to(__UpperCamelCase						)

												UpperCamelCase_		:							Any  =		MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""						)

												UpperCamelCase_		:							str  =		prepare_img()
												UpperCamelCase_		:							Union[str, Any]  =		image_processor(images=__UpperCamelCase			,     return_tensors="""pt"""						).to(__UpperCamelCase						)

												# forward pass
												with torch.no_grad():
																		UpperCamelCase_		:							List[str]  =		model(**__UpperCamelCase						)
												UpperCamelCase_		:							Tuple  =		outputs.logits

												# verify the logits
												UpperCamelCase_		:							Any  =		torch.Size((1, 21, 65, 65)						)
												self.assertEqual(logits.shape			,     __UpperCamelCase						)

												UpperCamelCase_		:							Union[str, Any]  =		torch.tensor(
												    [
												        [[17.5_790, 17.7_581, 18.3_355], [18.3_257, 18.4_230, 18.8_973], [18.6_169, 18.8_650, 19.2_187]],
												        [[-2.1_595, -2.0_977, -2.3_741], [-2.4_226, -2.3_028, -2.6_835], [-2.7_819, -2.5_991, -2.7_706]],
												        [[4.2_058, 4.8_317, 4.7_638], [4.4_136, 5.0_361, 4.9_383], [4.5_028, 4.9_644, 4.8_734]],
												    ]			,     device=__UpperCamelCase			,     )

												self.assertTrue(torch.allclose(logits[0, :3, :3, :3]			,     __UpperCamelCase			,     atol=1E-4						)						)







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
	from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


SCREAMING_SNAKE_CASE		:     List[str]         =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE		:     str         =	{
    "bigscience/bloom": "https://huggingface.co/bigscience/bloom/resolve/main/config.json",
    "bigscience/bloom-560m": "https://huggingface.co/bigscience/bloom-560m/blob/main/config.json",
    "bigscience/bloom-1b1": "https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json",
    "bigscience/bloom-1b7": "https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json",
    "bigscience/bloom-3b": "https://huggingface.co/bigscience/bloom-3b/blob/main/config.json",
    "bigscience/bloom-7b1": "https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json",
}

class        UpperCamelCase						(				__a				):
						a__		:Any      =			'''bloom'''
						a__		:Optional[int]      =			['''past_key_values''']
						a__		:Tuple      =			{
						    '''num_hidden_layers''': '''n_layer''',
						    '''num_attention_heads''': '''n_head''',
						}





						def __init__(self			,     __UpperCamelCase=250_880			,     __UpperCamelCase=64			,     __UpperCamelCase=2			,     __UpperCamelCase=8			,     __UpperCamelCase=1E-5			,     __UpperCamelCase=0.02			,     __UpperCamelCase=True			,     __UpperCamelCase=1			,     __UpperCamelCase=2			,     __UpperCamelCase=False			,     __UpperCamelCase=0.0			,     __UpperCamelCase=0.0			,     __UpperCamelCase=1			,     __UpperCamelCase=False			,     **__UpperCamelCase			,     )				->				List[Any]:
												UpperCamelCase_		:							Dict  =		vocab_size
												# Backward compatibility with n_embed kwarg
												UpperCamelCase_		:							Optional[Any]  =		kwargs.pop("""n_embed"""			,     __UpperCamelCase						)
												UpperCamelCase_		:							List[Any]  =		hidden_size if n_embed is None else n_embed
												UpperCamelCase_		:							List[Any]  =		n_layer
												UpperCamelCase_		:							Optional[Any]  =		n_head
												UpperCamelCase_		:							List[str]  =		layer_norm_epsilon
												UpperCamelCase_		:							int  =		initializer_range
												UpperCamelCase_		:							List[str]  =		use_cache
												UpperCamelCase_		:							List[Any]  =		pretraining_tp
												UpperCamelCase_		:							Optional[int]  =		apply_residual_connection_post_layernorm
												UpperCamelCase_		:							Any  =		hidden_dropout
												UpperCamelCase_		:							Optional[Any]  =		attention_dropout

												UpperCamelCase_		:							List[str]  =		bos_token_id
												UpperCamelCase_		:							Optional[Any]  =		eos_token_id
												UpperCamelCase_		:							str  =		slow_but_exact

												super().__init__(bos_token_id=__UpperCamelCase			,     eos_token_id=__UpperCamelCase			,     **__UpperCamelCase						)


class        UpperCamelCase						(				__a				):
						a__		:Dict      =			version.parse('''1.12'''				)





						def __init__(self			,     __UpperCamelCase			,     __UpperCamelCase = "default"			,     __UpperCamelCase = None			,     __UpperCamelCase = False			,     )				->				List[str]:
												super().__init__(__UpperCamelCase			,     task=__UpperCamelCase			,     patching_specs=__UpperCamelCase			,     use_past=__UpperCamelCase						)
												if not getattr(self._config			,     """pad_token_id"""			,     __UpperCamelCase						):
																		# TODO: how to do that better?
																		UpperCamelCase_		:							Optional[Any]  =		0





						@property
						def        A_							(self						)				->				Mapping[str, Mapping[int, str]]:
												UpperCamelCase_		:							str  =		OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}						)
												if self.use_past:
																		# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
																		self.fill_with_past_key_values_(__UpperCamelCase			,     direction="""inputs"""			,     inverted_values_shape=__UpperCamelCase						)
																		UpperCamelCase_		:							int  =		{0: """batch""", 1: """past_sequence + sequence"""}
												else:
																		UpperCamelCase_		:							Dict  =		{0: """batch""", 1: """sequence"""}

												return common_inputs





						@property
						def        A_							(self						)				->				int:
												return self._config.n_layer





						@property
						def        A_							(self						)				->				int:
												return self._config.n_head





						@property
						def        A_							(self						)				->				float:
												return 1E-3





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase = -1			,     __UpperCamelCase = -1			,     __UpperCamelCase = False			,     __UpperCamelCase = None			,     )				->				Mapping[str, Any]:
												UpperCamelCase_		:							Union[str, Any]  =		super(__UpperCamelCase			,     self						).generate_dummy_inputs(
												    __UpperCamelCase			,     batch_size=__UpperCamelCase			,     seq_length=__UpperCamelCase			,     is_pair=__UpperCamelCase			,     framework=__UpperCamelCase						)

												# We need to order the input in the way they appears in the forward()
												UpperCamelCase_		:							List[Any]  =		OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}						)

												# Need to add the past_keys
												if self.use_past:
																		if not is_torch_available():
																								raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""						)
																		else:
																								import torch

																								UpperCamelCase_,UpperCamelCase_		:							List[str]  =		common_inputs["""input_ids"""].shape
																								# Not using the same length for past_key_values
																								UpperCamelCase_		:							List[str]  =		seqlen + 2
																								UpperCamelCase_		:							Tuple  =		self._config.hidden_size // self.num_attention_heads
																								UpperCamelCase_		:							List[Any]  =		(
																								    batch * self.num_attention_heads,
																								    head_dim,
																								    past_key_values_length,
																								)
																								UpperCamelCase_		:							str  =		(
																								    batch * self.num_attention_heads,
																								    past_key_values_length,
																								    head_dim,
																								)
																								UpperCamelCase_		:							Any  =		[
																								    (torch.zeros(__UpperCamelCase						), torch.zeros(__UpperCamelCase						)) for _ in range(self.num_layers						)
																								]

												UpperCamelCase_		:							int  =		common_inputs["""attention_mask"""]
												if self.use_past:
																		UpperCamelCase_		:							List[str]  =		ordered_inputs["""attention_mask"""].dtype
																		UpperCamelCase_		:							Any  =		torch.cat(
																		    [ordered_inputs["""attention_mask"""], torch.ones(__UpperCamelCase			,     __UpperCamelCase			,     dtype=__UpperCamelCase						)]			,     dim=1						)

												return ordered_inputs





						@property
						def        A_							(self						)				->				int:
												return 13






