


"""simple docstring"""


from __future__ import annotations

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.file_utils import is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, slow

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 numpy as np
		import tensorflow as tf

		from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
		from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import MobileViTImageProcessor
class snake_case					(     _UpperCamelCase):







		def        a_       (    self					:				int						)				->  List[str]:





						'''simple docstring'''







						_A         =	self.config_class(**self.inputs_dict						)
						self.parent.assertTrue(hasattr(a__			,      "hidden_sizes"						)						)
						self.parent.assertTrue(hasattr(a__			,      "neck_hidden_sizes"						)						)
						self.parent.assertTrue(hasattr(a__			,      "num_attention_heads"						)						)





class snake_case					:







		def __init__(    self					:				Optional[int]			,      a__					:				str			,      a__					:				Dict=13			,      a__					:				Any=32			,      a__					:				List[Any]=2			,      a__					:				int=3			,      a__					:				List[str]=6_40			,      a__					:				Optional[int]=4			,      a__					:				int="silu"			,      a__					:				Optional[Any]=3			,      a__					:				Any=32			,      a__					:				Union[str, Any]=0.1			,      a__					:				List[Any]=0.1			,      a__					:				List[Any]=0.1			,      a__					:				Any=0.0_2			,      a__					:				Union[str, Any]=True			,      a__					:				Any=True			,      a__					:				str=10			,      a__					:				List[Any]=None			,      )				->  str:





						'''simple docstring'''







						_A         =	parent
						_A         =	batch_size
						_A         =	image_size
						_A         =	patch_size
						_A         =	num_channels
						_A         =	last_hidden_size
						_A         =	num_attention_heads
						_A         =	hidden_act
						_A         =	conv_kernel_size
						_A         =	output_stride
						_A         =	hidden_dropout_prob
						_A         =	attention_probs_dropout_prob
						_A         =	classifier_dropout_prob
						_A         =	use_labels
						_A         =	is_training
						_A         =	num_labels
						_A         =	initializer_range
						_A         =	scope







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





						'''simple docstring'''







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

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

						_A         =	self.get_config()

						return config, pixel_values, labels, pixel_labels







		def        a_       (    self					:				Tuple						)				->  List[str]:





						'''simple docstring'''







						return MobileViTConfig(
						    image_size=self.image_size			,      patch_size=self.patch_size			,      num_channels=self.num_channels			,      num_attention_heads=self.num_attention_heads			,      hidden_act=self.hidden_act			,      conv_kernel_size=self.conv_kernel_size			,      output_stride=self.output_stride			,      hidden_dropout_prob=self.hidden_dropout_prob			,      attention_probs_dropout_prob=self.attention_probs_dropout_prob			,      classifier_dropout_prob=self.classifier_dropout_prob			,      initializer_range=self.initializer_range			,      )







		def        a_       (    self					:				str			,      a__					:				List[str]			,      a__					:				int			,      a__					:				Tuple			,      a__					:				Optional[int]						)				->  Optional[Any]:





						'''simple docstring'''







						_A         =	TFMobileViTModel(config=a__						)
						_A         =	model(a__			,      training=a__						)
						_A         =	_A         =	self.image_size // self.output_stride
						self.parent.assertEqual(
						    result.last_hidden_state.shape			,      (self.batch_size, self.last_hidden_size, expected_height, expected_width)						)







		def        a_       (    self					:				str			,      a__					:				Tuple			,      a__					:				Tuple			,      a__					:				Any			,      a__					:				Optional[int]						)				->  List[str]:





						'''simple docstring'''







						_A         =	self.num_labels
						_A         =	TFMobileViTForImageClassification(a__						)
						_A         =	model(a__			,      labels=a__			,      training=a__						)
						self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.num_labels)						)







		def        a_       (    self					:				Optional[int]			,      a__					:				List[str]			,      a__					:				int			,      a__					:				Tuple			,      a__					:				Any						)				->  Tuple:





						'''simple docstring'''







						_A         =	self.num_labels
						_A         =	TFMobileViTForSemanticSegmentation(a__						)
						_A         =	_A         =	self.image_size // self.output_stride

						_A         =	model(a__			,      training=a__						)
						self.parent.assertEqual(
						    result.logits.shape			,      (self.batch_size, self.num_labels, expected_height, expected_width)						)

						_A         =	model(a__			,      labels=a__			,      training=a__						)
						self.parent.assertEqual(
						    result.logits.shape			,      (self.batch_size, self.num_labels, expected_height, expected_width)						)







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





						'''simple docstring'''







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





@require_tf
class snake_case					(     _UpperCamelCase   , _UpperCamelCase   , unittest.TestCase):
		__UpperCamelCase							=	(
		    (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
		    if is_tf_available()
		    else ()
		)
		__UpperCamelCase							=	(
		    {'feature-extraction': TFMobileViTModel, 'image-classification': TFMobileViTForImageClassification}
		    if is_tf_available()
		    else {}
		)

		__UpperCamelCase							=	False
		__UpperCamelCase							=	False
		__UpperCamelCase							=	False
		__UpperCamelCase							=	False
		__UpperCamelCase							=	False







		def        a_       (    self					:				Union[str, Any]						)				->  Optional[int]:





						'''simple docstring'''







						_A         =	TFMobileViTModelTester(self						)
						_A         =	TFMobileViTConfigTester(self			,      config_class=a__			,      has_text_modality=a__						)







		def        a_       (    self					:				Optional[int]						)				->  str:





						'''simple docstring'''







						self.config_tester.run_common_tests()







		@unittest.skip(reason="MobileViT does not use inputs_embeds"						)
		def        a_       (    self					:				Any						)				->  Optional[Any]:





						'''simple docstring'''







						pass







		@unittest.skip(reason="MobileViT does not support input and output embeddings"						)
		def        a_       (    self					:				Any						)				->  int:





						'''simple docstring'''







						pass







		@unittest.skip(reason="MobileViT does not output attentions"						)
		def        a_       (    self					:				Optional[Any]						)				->  Optional[Any]:





						'''simple docstring'''







						pass







		def        a_       (    self					:				Optional[int]						)				->  Optional[Any]:





						'''simple docstring'''







						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()

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

										_A         =	["pixel_values"]
										self.assertListEqual(arg_names[:1]			,      a__						)







		def        a_       (    self					:				List[str]						)				->  int:





						'''simple docstring'''







						_A         =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a__						)







		def        a_       (    self					:				Optional[int]						)				->  str:





						'''simple docstring'''







						def check_hidden_states_output(a__					:				List[str]			,      a__					:				Optional[Any]			,      a__					:				Any						):
										_A         =	model_class(a__						)

										_A         =	model(**self._prepare_for_class(a__			,      a__						)						)

										_A         =	outputs.hidden_states

										_A         =	5
										self.assertEqual(len(a__						)			,      a__						)

										# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
										# with the width and height being successively divided by 2.
										_A         =	2
										for i in range(len(a__						)						):
														self.assertListEqual(
														    list(hidden_states[i].shape[-2:]						)			,      [self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]			,      )
														divisor *= 2

										self.assertEqual(self.model_tester.output_stride			,      divisor // 2						)

						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										_A         =	True
										check_hidden_states_output(a__			,      a__			,      a__						)

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

										check_hidden_states_output(a__			,      a__			,      a__						)







		def        a_       (    self					:				List[Any]						)				->  Tuple:





						'''simple docstring'''







						_A         =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*a__						)







		def        a_       (    self					:				List[str]						)				->  Optional[Any]:





						'''simple docstring'''







						_A         =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*a__						)







		@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        a_       (    self					:				int						)				->  Optional[Any]:





						'''simple docstring'''







						super().test_dataset_conversion()







		def        a_       (    self					:				List[Any]			,      a__					:				Any			,      a__					:				List[Any]			,      a__					:				str=2E-1			,      a__					:				Optional[int]=2E-1						)				->  Optional[Any]:





						'''simple docstring'''







						self.assertTrue(np.allclose(a__			,      a__			,      atol=a__			,      rtol=a__						)						)







		@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        a_       (    self					:				int						)				->  Union[str, Any]:





						'''simple docstring'''







						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										# Since `TFMobileViTModel` cannot operate with the default `fit()` method.
										if model_class.__name__ != "TFMobileViTModel":
														_A         =	model_class(a__						)
														if getattr(a__			,      "hf_compute_loss"			,      a__						):
																		super().test_keras_fit()







		def        a_       (    self					:				str						)				->  int:





						'''simple docstring'''







						_A     ,						_A         =	self.model_tester.prepare_config_and_inputs_for_common()
						for model_class in self.all_model_classes:
										# set an ignore index to correctly test the masked loss used in
										# `TFMobileViTForSemanticSegmentation`.
										if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
														_A         =	5

										_A         =	model_class(a__						)
										if getattr(a__			,      "hf_compute_loss"			,      a__						):
														# The number of elements in the loss should be the same as the number of elements in the label
														_A         =	self._prepare_for_class(inputs_dict.copy()			,      a__			,      return_labels=a__						)
														_A         =	prepared_for_class[
														    sorted(prepared_for_class.keys() - inputs_dict.keys()			,      reverse=a__						)[0]
														]
														_A         =	added_label.shape.as_list()[:1]

														# Test that model correctly compute the loss with kwargs
														_A         =	self._prepare_for_class(inputs_dict.copy()			,      a__			,      return_labels=a__						)
														_A         =	{"input_ids", "pixel_values", "input_features"}
														_A         =	possible_input_names.intersection(set(a__						)						).pop()
														_A         =	prepared_for_class.pop(a__						)

														_A         =	model(a__			,      **a__						)[0]
														self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)

														# Test that model correctly compute the loss when we mask some positions
														_A         =	self._prepare_for_class(inputs_dict.copy()			,      a__			,      return_labels=a__						)
														_A         =	{"input_ids", "pixel_values", "input_features"}
														_A         =	possible_input_names.intersection(set(a__						)						).pop()
														_A         =	prepared_for_class.pop(a__						)
														if "labels" in prepared_for_class:
																		_A         =	prepared_for_class["labels"].numpy()
																		if len(labels.shape						) > 1 and labels.shape[1] != 1:
																						# labels[0] = -100
																						_A         =	tf.convert_to_tensor(a__						)
																						_A         =	model(a__			,      **a__						)[0]
																						self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)
																						self.assertTrue(not np.any(np.isnan(loss.numpy()						)						)						)

                # Test that model correctly compute the loss with a dict
														_A         =	self._prepare_for_class(inputs_dict.copy()			,      a__			,      return_labels=a__						)
														_A         =	model(a__						)[0]
														self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)

														# Test that model correctly compute the loss with a tuple
														_A         =	self._prepare_for_class(inputs_dict.copy()			,      a__			,      return_labels=a__						)

														# Get keys that were added with the _prepare_for_class function
														_A         =	prepared_for_class.keys() - inputs_dict.keys()
														_A         =	inspect.signature(model.call						).parameters
														_A         =	list(signature.keys()						)

														# Create a dictionary holding the location of the tensors in the tuple
														_A         =	{0: input_name}
														for label_key in label_keys:
																		_A         =	signature_names.index(a__						)
																		_A         =	label_key
														_A         =	sorted(tuple_index_mapping.items()						)
														# Initialize a list with their default values, update the values and convert to a tuple
														_A         =	[]

														for name in signature_names:
																		if name != "kwargs":
																						list_input.append(signature[name].default						)

														for index, value in sorted_tuple_index_mapping:
																		_A         =	prepared_for_class[value]

														_A         =	tuple(a__						)

														# Send to model
														_A         =	model(tuple_input[:-1]						)[0]

														self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]						)







		@slow
		def        a_       (    self					:				List[str]						)				->  str:





						'''simple docstring'''







						for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										_A         =	TFMobileViTModel.from_pretrained(a__						)
										self.assertIsNotNone(a__						)






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




@require_tf
class snake_case					(     unittest.TestCase):







		@slow
		def        a_       (    self					:				Optional[Any]						)				->  Tuple:





						'''simple docstring'''







						_A         =	TFMobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"						)

						_A         =	MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"						)
						_A         =	prepare_img()
						_A         =	image_processor(images=a__			,      return_tensors="tf"						)

						# forward pass
						_A         =	model(**a__			,      training=a__						)

						# verify the logits
						_A         =	tf.TensorShape((1, 10_00)						)
						self.assertEqual(outputs.logits.shape			,      a__						)

						_A         =	tf.constant([-1.9_3_6_4, -1.2_3_2_7, -0.4_6_5_3]						)

						tf.debugging.assert_near(outputs.logits[0, :3]			,      a__			,      atol=1E-4			,      rtol=1E-0_4						)







		@slow
		def        a_       (    self					:				int						)				->  Optional[Any]:





						'''simple docstring'''







						_A         =	TFMobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"						)

						_A         =	MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"						)

						_A         =	prepare_img()
						_A         =	image_processor(images=a__			,      return_tensors="tf"						)

						# forward pass
						_A         =	model(inputs.pixel_values			,      training=a__						)
						_A         =	outputs.logits

						# verify the logits
						_A         =	tf.TensorShape((1, 21, 32, 32)						)
						self.assertEqual(logits.shape			,      a__						)

						_A         =	tf.constant(
						    [
						        [[6.9_7_1_3, 6.9_7_8_6, 7.2_4_2_2], [7.2_8_9_3, 7.2_8_2_5, 7.4_4_4_6], [7.6_5_8_0, 7.8_7_9_7, 7.9_4_2_0]],
						        [[-1_0.6_8_6_9, -1_0.3_2_5_0, -1_0.3_4_7_1], [-1_0.4_2_2_8, -9.9_8_6_8, -9.7_1_3_2], [-1_1.0_4_0_5, -1_1.0_2_2_1, -1_0.7_3_1_8]],
						        [[-3.3_0_8_9, -2.8_5_3_9, -2.6_7_4_0], [-3.2_7_0_6, -2.5_6_2_1, -2.5_1_0_8], [-3.2_5_3_4, -2.6_6_1_5, -2.6_6_5_1]],
						    ]						)

						tf.debugging.assert_near(logits[0, :3, :3, :3]			,      a__			,      rtol=1E-4			,      atol=1E-4						)



"""simple docstring"""


import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
		import torch

		from transformers import CamembertModel
@require_torch
@require_sentencepiece
@require_tokenizers
class snake_case					(     unittest.TestCase):







		@slow
		def        a_       (    self					:				int						)				->  Tuple:





						'''simple docstring'''







						_A         =	CamembertModel.from_pretrained("camembert-base"						)
						model.to(a__						)

						_A         =	torch.tensor(
						    [[5, 1_21, 11, 6_60, 16, 7_30, 2_55_43, 1_10, 83, 6]]			,      device=a__			,      dtype=torch.long			,      )  # J'aime le camembert !
						with torch.no_grad():
										_A         =	model(a__						)["last_hidden_state"]
						_A         =	torch.Size((1, 10, 7_68)						)
						self.assertEqual(output.shape			,      a__						)
						# compare the actual values for a slice.
						_A         =	torch.tensor(
						    [[[-0.0_2_5_4, 0.0_2_3_5, 0.1_0_2_7], [0.0_6_0_6, -0.1_8_1_1, -0.0_4_1_8], [-0.1_5_6_1, -0.1_1_2_7, 0.2_6_8_7]]]			,      device=a__			,      dtype=torch.float			,      )
						# camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
						# camembert.eval()
						# expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

						self.assertTrue(torch.allclose(output[:, :3, :3]			,      a__			,      atol=1E-4						)						)