





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, MobileNetVaModel
			from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import MobileNetVaImageProcessor






class       __A					(					__lowerCAmelCase				):

			'''simple docstring'''







			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.config_class(**self.inputs_dict							)
				self.parent.assertTrue(hasattr(_UpperCamelCase ,						'''tf_padding'''							)							)
				self.parent.assertTrue(hasattr(_UpperCamelCase ,						'''depth_multiplier'''							)							)






class       __A					:

			'''simple docstring'''







			def __init__(					self ,						__lowerCAmelCase ,						__lowerCAmelCase=1_3 ,						__lowerCAmelCase=3 ,						__lowerCAmelCase=3_2 ,						__lowerCAmelCase=0.25 ,						__lowerCAmelCase=8 ,						__lowerCAmelCase=True ,						__lowerCAmelCase=1_0_2_4 ,						__lowerCAmelCase=3_2 ,						__lowerCAmelCase="relu6" ,						__lowerCAmelCase=0.1 ,						__lowerCAmelCase=0.02 ,						__lowerCAmelCase=True ,						__lowerCAmelCase=True ,						__lowerCAmelCase=1_0 ,						__lowerCAmelCase=None ,						):



				'''simple docstring'''







				lowerCamelCase__              =	parent
				lowerCamelCase__              =	batch_size
				lowerCamelCase__              =	num_channels
				lowerCamelCase__              =	image_size
				lowerCamelCase__              =	depth_multiplier
				lowerCamelCase__              =	min_depth
				lowerCamelCase__              =	tf_padding
				lowerCamelCase__              =	int(last_hidden_size * depth_multiplier							)
				lowerCamelCase__              =	output_stride
				lowerCamelCase__              =	hidden_act
				lowerCamelCase__              =	classifier_dropout_prob
				lowerCamelCase__              =	use_labels
				lowerCamelCase__              =	is_training
				lowerCamelCase__              =	num_labels
				lowerCamelCase__              =	initializer_range
				lowerCamelCase__              =	scope





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

				lowerCamelCase__              =	self.get_config()

				return config, pixel_values, labels, pixel_labels





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return MobileNetVaConfig(
				    num_channels=self.num_channels ,						image_size=self.image_size ,						depth_multiplier=self.depth_multiplier ,						min_depth=self.min_depth ,						tf_padding=self.tf_padding ,						hidden_act=self.hidden_act ,						classifier_dropout_prob=self.classifier_dropout_prob ,						initializer_range=self.initializer_range ,						)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__              =	MobileNetVaModel(config=_UpperCamelCase							)
				model.to(_UpperCamelCase							)
				model.eval()
				lowerCamelCase__              =	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,
				    ) ,						)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__              =	self.num_labels
				lowerCamelCase__              =	MobileNetVaForImageClassification(_UpperCamelCase							)
				model.to(_UpperCamelCase							)
				model.eval()
				lowerCamelCase__              =	model(_UpperCamelCase ,						labels=_UpperCamelCase							)
				self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.num_labels)							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.prepare_config_and_inputs()
				lowerCamelCase__              =	config_and_inputs
				lowerCamelCase__              =	{"""pixel_values""": pixel_values}
				return config, inputs_dict






@require_torch
class       __A					(					__lowerCAmelCase    ,       __lowerCAmelCase    ,       unittest.TestCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
			lowerCAmelCase_							  =		(
			    {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
			    if is_torch_available()
			    else {}
			)

			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False
			lowerCAmelCase_							  =		False





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	MobileNetVaModelTester(self							)
				lowerCamelCase__              =	MobileNetVaConfigTester(self ,						config_class=_UpperCamelCase ,						has_text_modality=_UpperCamelCase							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				self.config_tester.run_common_tests()





			@unittest.skip(reason='''MobileNetV1 does not use inputs_embeds'''							)
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings'''							)
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			@unittest.skip(reason='''MobileNetV1 does not output attentions'''							)
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				pass





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					lowerCamelCase__              =	model_class(_UpperCamelCase							)
					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] ,						_UpperCamelCase							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_UpperCamelCase							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				def check_hidden_states_output(__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							):
					lowerCamelCase__              =	model_class(_UpperCamelCase							)
					model.to(_UpperCamelCase							)
					model.eval()

					with torch.no_grad():
						lowerCamelCase__              =	model(**self._prepare_for_class(_UpperCamelCase ,						_UpperCamelCase							)							)

					lowerCamelCase__              =	outputs.hidden_states

					lowerCamelCase__              =	2_6
					self.assertEqual(len(_UpperCamelCase							) ,						_UpperCamelCase							)

				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					lowerCamelCase__              =	True
					check_hidden_states_output(_UpperCamelCase ,						_UpperCamelCase ,						_UpperCamelCase							)

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

					check_hidden_states_output(_UpperCamelCase ,						_UpperCamelCase ,						_UpperCamelCase							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase							)





			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCamelCase__              =	MobileNetVaModel.from_pretrained(_UpperCamelCase							)
					self.assertIsNotNone(_UpperCamelCase							)





def lowerCAmelCase__()       ->   Optional[Any]:







	'''simple docstring'''







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






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

			'''simple docstring'''







			@cached_property
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				return (
				    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224'''							) if is_vision_available() else None
				)





			@slow
			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224'''							).to(_UpperCamelCase							)

				lowerCamelCase__              =	self.default_image_processor
				lowerCamelCase__              =	prepare_img()
				lowerCamelCase__              =	image_processor(images=_UpperCamelCase ,						return_tensors='''pt'''							).to(_UpperCamelCase							)

				# forward pass
				with torch.no_grad():
					lowerCamelCase__              =	model(**_UpperCamelCase							)

				# verify the logits
				lowerCamelCase__              =	torch.Size((1, 1_0_0_1)							)
				self.assertEqual(outputs.logits.shape ,						_UpperCamelCase							)

				lowerCamelCase__              =	torch.tensor([-4.1739, -1.1233, 3.1205]							).to(_UpperCamelCase							)

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









from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin






@dataclass
class       __A					(					lowerCAmelCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		42






class       __A					(					lowerCAmelCase    ,       lowerCAmelCase				):

			'''simple docstring'''







			@register_to_config
			def __init__(					self ,						__lowerCAmelCase = 1_6 ,						__lowerCAmelCase = 8_8 ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = 1 ,						__lowerCAmelCase = 0.0 ,						__lowerCAmelCase = 3_2 ,						__lowerCAmelCase = None ,						__lowerCAmelCase = False ,						__lowerCAmelCase = None ,						__lowerCAmelCase = "geglu" ,						__lowerCAmelCase = True ,						__lowerCAmelCase = True ,						):



				'''simple docstring'''







				super().__init__()
				lowerCamelCase__              =	num_attention_heads
				lowerCamelCase__              =	attention_head_dim
				lowerCamelCase__              =	num_attention_heads * attention_head_dim

				lowerCamelCase__              =	in_channels

				lowerCamelCase__              =	torch.nn.GroupNorm(num_groups=__lowerCAmelCase ,						num_channels=__lowerCAmelCase ,						eps=1E-6 ,						affine=__lowerCAmelCase							)
				lowerCamelCase__              =	nn.Linear(__lowerCAmelCase ,						__lowerCAmelCase							)

				# 3. Define transformers blocks
				lowerCamelCase__              =	nn.ModuleList(
				    [
				        BasicTransformerBlock(
				            __lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						dropout=__lowerCAmelCase ,						cross_attention_dim=__lowerCAmelCase ,						activation_fn=__lowerCAmelCase ,						attention_bias=__lowerCAmelCase ,						double_self_attention=__lowerCAmelCase ,						norm_elementwise_affine=__lowerCAmelCase ,						)
				        for d in range(__lowerCAmelCase							)
				    ]							)

				lowerCamelCase__              =	nn.Linear(__lowerCAmelCase ,						__lowerCAmelCase							)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=1 ,						__lowerCAmelCase=None ,						__lowerCAmelCase = True ,						):



				'''simple docstring'''







				lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__              =	hidden_states.shape
				lowerCamelCase__              =	batch_frames // num_frames

				lowerCamelCase__              =	hidden_states

				lowerCamelCase__              =	hidden_states[None, :].reshape(__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							)
				lowerCamelCase__              =	hidden_states.permute(0 ,						2 ,						1 ,						3 ,						4							)

				lowerCamelCase__              =	self.norm(__lowerCAmelCase							)
				lowerCamelCase__              =	hidden_states.permute(0 ,						3 ,						4 ,						2 ,						1							).reshape(batch_size * height * width ,						__lowerCAmelCase ,						__lowerCAmelCase							)

				lowerCamelCase__              =	self.proj_in(__lowerCAmelCase							)

				# 2. Blocks
				for block in self.transformer_blocks:
					lowerCamelCase__              =	block(
					    __lowerCAmelCase ,						encoder_hidden_states=__lowerCAmelCase ,						timestep=__lowerCAmelCase ,						cross_attention_kwargs=__lowerCAmelCase ,						class_labels=__lowerCAmelCase ,						)

				# 3. Output
				lowerCamelCase__              =	self.proj_out(__lowerCAmelCase							)
				lowerCamelCase__              =	(
				    hidden_states[None, None, :]
				    .reshape(__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							)
				    .permute(0 ,						3 ,						4 ,						1 ,						2							)
				    .contiguous()
				)
				lowerCamelCase__              =	hidden_states.reshape(__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							)

				lowerCamelCase__              =	hidden_states + residual

				if not return_dict:
					return (output,)

				return TransformerTemporalModelOutput(sample=__lowerCAmelCase							)



