

'''simple docstring'''




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



	"""simple docstring"""





	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[int]:




				'''simple docstring'''






				UpperCamelCase				:	Dict    						=					self.config_class(**self.inputs_dict   )
				self.parent.assertTrue(hasattr(lowerCAmelCase__				,			"tf_padding"   )   )
				self.parent.assertTrue(hasattr(lowerCAmelCase__				,			"depth_multiplier"   )   )

class     UpperCAmelCase_ :



	"""simple docstring"""





	def __init__(    self				,			lowerCamelCase				,			lowerCamelCase=13				,			lowerCamelCase=3				,			lowerCamelCase=32				,			lowerCamelCase=0.25				,			lowerCamelCase=8				,			lowerCamelCase=8				,			lowerCamelCase=6				,			lowerCamelCase=32				,			lowerCamelCase=True				,			lowerCamelCase=True				,			lowerCamelCase=True				,			lowerCamelCase="relu6"				,			lowerCamelCase=12_80				,			lowerCamelCase=0.1				,			lowerCamelCase=0.02				,			lowerCamelCase=True				,			lowerCamelCase=True				,			lowerCamelCase=10				,			lowerCamelCase=None				,			)							->     List[str]:




				'''simple docstring'''






				UpperCamelCase				:	Dict    						=					parent
				UpperCamelCase				:	int    						=					batch_size
				UpperCamelCase				:	Dict    						=					num_channels
				UpperCamelCase				:	Tuple    						=					image_size
				UpperCamelCase				:	Optional[int]    						=					depth_multiplier
				UpperCamelCase				:	int    						=					depth_divisible_by
				UpperCamelCase				:	Union[str, Any]    						=					min_depth
				UpperCamelCase				:	Any    						=					expand_ratio
				UpperCamelCase				:	Tuple    						=					tf_padding
				UpperCamelCase				:	List[str]    						=					output_stride
				UpperCamelCase				:	Union[str, Any]    						=					first_layer_is_expansion
				UpperCamelCase				:	Any    						=					finegrained_output
				UpperCamelCase				:	Optional[Any]    						=					hidden_act
				UpperCamelCase				:	Optional[Any]    						=					last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier   )
				UpperCamelCase				:	List[Any]    						=					classifier_dropout_prob
				UpperCamelCase				:	Any    						=					use_labels
				UpperCamelCase				:	List[str]    						=					is_training
				UpperCamelCase				:	List[str]    						=					num_labels
				UpperCamelCase				:	int    						=					initializer_range
				UpperCamelCase				:	List[Any]    						=					scope

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Dict:




				'''simple docstring'''






				UpperCamelCase				:	Dict    						=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

				UpperCamelCase				:	str    						=					self.get_config()

				return config, pixel_values, labels, pixel_labels

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     List[Any]:




				'''simple docstring'''






				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 	SCREAMING_SNAKE_CASE__							(    self				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase   )							->     Any:




				'''simple docstring'''






				UpperCamelCase				:	str    						=					MobileNetVaModel(config=lowerCAmelCase__   )
				model.to(lowerCAmelCase__   )
				model.eval()
				UpperCamelCase				:	Tuple    						=					model(lowerCAmelCase__   )
				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 	SCREAMING_SNAKE_CASE__							(    self				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase   )							->     Union[str, Any]:




				'''simple docstring'''






				UpperCamelCase				:	Any    						=					self.num_labels
				UpperCamelCase				:	Dict    						=					MobileNetVaForImageClassification(lowerCAmelCase__   )
				model.to(lowerCAmelCase__   )
				model.eval()
				UpperCamelCase				:	Any    						=					model(lowerCAmelCase__				,			labels=lowerCAmelCase__   )
				self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.num_labels)   )

	def 	SCREAMING_SNAKE_CASE__							(    self				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase   )							->     Union[str, Any]:




				'''simple docstring'''






				UpperCamelCase				:	Optional[Any]    						=					self.num_labels
				UpperCamelCase				:	Dict    						=					MobileNetVaForSemanticSegmentation(lowerCAmelCase__   )
				model.to(lowerCAmelCase__   )
				model.eval()
				UpperCamelCase				:	str    						=					model(lowerCAmelCase__   )
				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				:	Dict    						=					model(lowerCAmelCase__				,			labels=lowerCAmelCase__   )
				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 	SCREAMING_SNAKE_CASE__							(    self   )							->     int:




				'''simple docstring'''






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

@require_torch
class     UpperCAmelCase_ (	__a				,	__a				,	unittest.TestCase    ):



	"""simple docstring"""





	__SCREAMING_SNAKE_CASE					=       (
	    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	__SCREAMING_SNAKE_CASE					=       (
	    {
	        '''feature-extraction''': MobileNetVaModel,
	        '''image-classification''': MobileNetVaForImageClassification,
	        '''image-segmentation''': MobileNetVaForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	__SCREAMING_SNAKE_CASE					=       False
	__SCREAMING_SNAKE_CASE					=       False
	__SCREAMING_SNAKE_CASE					=       False
	__SCREAMING_SNAKE_CASE					=       False

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Dict:




				'''simple docstring'''






				UpperCamelCase				:	Any    						=					MobileNetVaModelTester(self   )
				UpperCamelCase				:	Any    						=					MobileNetVaConfigTester(self				,			config_class=lowerCAmelCase__				,			has_text_modality=lowerCAmelCase__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Any:




				'''simple docstring'''






				self.config_tester.run_common_tests()

	@unittest.skip(reason="MobileNetV2 does not use inputs_embeds"   )
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[int]:




				'''simple docstring'''






				pass

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




				'''simple docstring'''






				pass

	@unittest.skip(reason="MobileNetV2 does not output attentions"   )
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     str:




				'''simple docstring'''






				pass

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Dict:




				'''simple docstring'''






				UpperCamelCase						,   UpperCamelCase				:	List[str]    						=					self.model_tester.prepare_config_and_inputs_for_common()

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

							UpperCamelCase				:	Optional[int]    						=					["pixel_values"]
							self.assertListEqual(arg_names[:1]				,			lowerCAmelCase__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     List[str]:




				'''simple docstring'''






				UpperCamelCase				:	List[str]    						=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCAmelCase__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Optional[int]:




				'''simple docstring'''






				def check_hidden_states_output(lowerCamelCase				,			lowerCamelCase				,			lowerCamelCase   ):
							UpperCamelCase				:	Dict    						=					model_class(lowerCAmelCase__   )
							model.to(lowerCAmelCase__   )
							model.eval()

							with torch.no_grad():
										UpperCamelCase				:	List[Any]    						=					model(**self._prepare_for_class(lowerCAmelCase__				,			lowerCAmelCase__   )   )

							UpperCamelCase				:	Tuple    						=					outputs.hidden_states

							UpperCamelCase				:	List[str]    						=					16
							self.assertEqual(len(lowerCAmelCase__   )				,			lowerCAmelCase__   )

				UpperCamelCase						,   UpperCamelCase				:	int    						=					self.model_tester.prepare_config_and_inputs_for_common()

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

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

							check_hidden_states_output(lowerCAmelCase__				,			lowerCAmelCase__				,			lowerCAmelCase__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Tuple:




				'''simple docstring'''






				UpperCamelCase				:	Dict    						=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__   )

	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     str:




				'''simple docstring'''






				UpperCamelCase				:	Dict    						=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase__   )



	@slow
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     str:




				'''simple docstring'''






				for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCamelCase				:	str    						=					MobileNetVaModel.from_pretrained(lowerCAmelCase__   )
							self.assertIsNotNone(lowerCAmelCase__   )

def       A__	(							):


			'''simple docstring'''


			UpperCamelCase				:	str    						=					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
			return image




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



	"""simple docstring"""





	@cached_property
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Any:




				'''simple docstring'''






				return (
				    MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224"   ) if is_vision_available() else None
				)

	@slow
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     Union[str, Any]:




				'''simple docstring'''






				UpperCamelCase				:	List[Any]    						=					MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224"   ).to(lowerCAmelCase__   )

				UpperCamelCase				:	List[Any]    						=					self.default_image_processor
				UpperCamelCase				:	Union[str, Any]    						=					prepare_img()
				UpperCamelCase				:	List[str]    						=					image_processor(images=lowerCAmelCase__				,			return_tensors="pt"   ).to(lowerCAmelCase__   )

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

				# verify the logits
				UpperCamelCase				:	int    						=					torch.Size((1, 10_01)   )
				self.assertEqual(outputs.logits.shape				,			lowerCAmelCase__   )

				UpperCamelCase				:	List[Any]    						=					torch.tensor([0.2445, -1.1993, 0.1905]   ).to(lowerCAmelCase__   )

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



	@slow
	def 	SCREAMING_SNAKE_CASE__							(    self   )							->     List[Any]:




				'''simple docstring'''






				UpperCamelCase				:	Any    						=					MobileNetVaForSemanticSegmentation.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"   )
				UpperCamelCase				:	Union[str, Any]    						=					model.to(lowerCAmelCase__   )

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

				UpperCamelCase				:	Optional[int]    						=					prepare_img()
				UpperCamelCase				:	List[Any]    						=					image_processor(images=lowerCAmelCase__				,			return_tensors="pt"   ).to(lowerCAmelCase__   )

				# forward pass
				with torch.no_grad():
							UpperCamelCase				:	Optional[Any]    						=					model(**lowerCAmelCase__   )
				UpperCamelCase				:	Union[str, Any]    						=					outputs.logits

				# verify the logits
				UpperCamelCase				:	List[str]    						=					torch.Size((1, 21, 65, 65)   )
				self.assertEqual(logits.shape				,			lowerCAmelCase__   )

				UpperCamelCase				:	Any    						=					torch.tensor(
				    [
				        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
				        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
				        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
				    ]				,			device=lowerCAmelCase__				,			)

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


# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
						import torch


_snake_case      :							Optional[Any]     	=    logging.get_logger(__name__)





@dataclass
class   UpperCamelCase_      :







						'''simple docstring'''



						def __init__(			self							:str		,    lowerCAmelCase__							:Any=False		,    lowerCAmelCase__							:Tuple=False		,    lowerCAmelCase__							:List[str]=6.0		,    lowerCAmelCase__							:int=None		,    lowerCAmelCase__							:Optional[Any]=False		,    lowerCAmelCase__							:List[Any]=False		,    lowerCAmelCase__							:int=None		,    lowerCAmelCase__							:Tuple="fp4"		,    lowerCAmelCase__							:Union[str, Any]=False		,    **lowerCAmelCase__							:Optional[Any]		,    )  ->Any:
										lowercase         =      load_in_abit
										lowercase         =      load_in_abit
										lowercase         =      llm_inta_threshold
										lowercase         =      llm_inta_skip_modules
										lowercase         =      llm_inta_enable_fpaa_cpu_offload
										lowercase         =      llm_inta_has_fpaa_weight
										lowercase         =      bnb_abit_quant_type
										lowercase         =      bnb_abit_use_double_quant

										if bnb_abit_compute_dtype is None:
														lowercase         =      torch.floataa
										elif isinstance(lowerCAmelCase__		,    lowerCAmelCase__     ):
														lowercase         =      getattr(lowerCAmelCase__		,    lowerCAmelCase__     )
										elif isinstance(lowerCAmelCase__		,    torch.dtype     ):
														lowercase         =      bnb_abit_compute_dtype
										else:
														raise ValueError("bnb_4bit_compute_dtype must be a string or a torch.dtype"     )

										self.post_init()





						def        SCREAMING_SNAKE_CASE(			self							:Any     )  ->str:
										if not isinstance(self.llm_inta_threshold		,    lowerCAmelCase__     ):
														raise ValueError("llm_int8_threshold must be a float"     )

										if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules		,    lowerCAmelCase__     ):
														raise ValueError("llm_int8_skip_modules must be a list of strings"     )
										if not isinstance(self.llm_inta_enable_fpaa_cpu_offload		,    lowerCAmelCase__     ):
														raise ValueError("llm_int8_enable_fp32_cpu_offload must be a boolean"     )

										if not isinstance(self.llm_inta_has_fpaa_weight		,    lowerCAmelCase__     ):
														raise ValueError("llm_int8_has_fp16_weight must be a boolean"     )

										if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype		,    torch.dtype     ):
														raise ValueError("bnb_4bit_compute_dtype must be torch.dtype"     )

										if not isinstance(self.bnb_abit_quant_type		,    lowerCAmelCase__     ):
														raise ValueError("bnb_4bit_quant_type must be a string"     )

										if not isinstance(self.bnb_abit_use_double_quant		,    lowerCAmelCase__     ):
														raise ValueError("bnb_4bit_use_double_quant must be a boolean"     )

										if self.load_in_abit and not version.parse(importlib.metadata.version("bitsandbytes"     )     ) >= version.parse(
										    "0.39.0"     ):
														raise ValueError(
														    "4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version"     )





						def        SCREAMING_SNAKE_CASE(			self							:List[str]     )  ->Tuple:
										return self.load_in_abit or self.load_in_abit





						def        SCREAMING_SNAKE_CASE(			self							:List[str]     )  ->int:
										if self.load_in_abit:
														return "llm_int8"
										elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
														return "fp4"
										elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
														return "nf4"
										else:
														return None





						@classmethod
						def        SCREAMING_SNAKE_CASE(			cls							:Union[str, Any]		,    lowerCAmelCase__							:List[Any]		,    lowerCAmelCase__							:Union[str, Any]		,    **lowerCAmelCase__							:Any     )  ->Optional[Any]:
										lowercase         =      cls(**lowerCAmelCase__     )

										lowercase         =      []
										for key, value in kwargs.items():
														if hasattr(lowerCAmelCase__		,    lowerCAmelCase__     ):
																		setattr(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__     )
																		to_remove.append(lowerCAmelCase__     )
										for key in to_remove:
														kwargs.pop(lowerCAmelCase__		,    lowerCAmelCase__     )

										if return_unused_kwargs:
														return config, kwargs
										else:
														return config





						def        SCREAMING_SNAKE_CASE(			self							:Dict		,    lowerCAmelCase__							:Union[str, os.PathLike]     )  ->Optional[int]:
										with open(lowerCAmelCase__		,    "w"		,    encoding="utf-8"     ) as writer:
														lowercase         =      self.to_dict()
														lowercase         =      json.dumps(lowerCAmelCase__		,    indent=2		,    sort_keys=lowerCAmelCase__     ) + "\n"

														writer.write(lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:Any     )  ->Dict[str, Any]:
										lowercase         =      copy.deepcopy(self.__dict__     )
										lowercase         =      str(output["bnb_4bit_compute_dtype"]     ).split("."     )[1]

										return output





						def __repr__(			self							:int     )  ->Optional[Any]:
										return F'''{self.__class__.__name__} {self.to_json_string()}'''





						def        SCREAMING_SNAKE_CASE(			self							:Optional[Any]		,    lowerCAmelCase__							:bool = True     )  ->str:
										if use_diff is True:
														lowercase         =      self.to_diff_dict()
										else:
														lowercase         =      self.to_dict()
										return json.dumps(lowerCAmelCase__		,    indent=2		,    sort_keys=lowerCAmelCase__     ) + "\n"





						def        SCREAMING_SNAKE_CASE(			self							:int     )  ->Dict[str, Any]:
										lowercase         =      self.to_dict()

										# get the default config dict
										lowercase         =      BitsAndBytesConfig().to_dict()

										lowercase         =      {}

										# only serialize values that differ from the default config
										for key, value in config_dict.items():
														if value != default_config_dict[key]:
																		lowercase         =      value

										return serializable_config_dict
