
import inspect
import unittest

from transformers import MobileViTVaConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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 MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, MobileViTVaModel
					from transformers.models.mobilevitva.modeling_mobilevitva import (
					    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
					    make_divisible,
					)


if is_vision_available():
					from PIL import Image

					from transformers import MobileViTImageProcessor





class 				UpperCAmelCase (    __SCREAMING_SNAKE_CASE    ):




	'''simple docstring'''





	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					__A       =					self.config_class(**self.inputs_dict					)
					self.parent.assertTrue(hasattr(A				,"width_multiplier"					)					)





class 				UpperCAmelCase :




	'''simple docstring'''





	def __init__(			self   :					int				,A   :					Optional[int]				,A   :					int=13				,A   :					int=64				,A   :					List[Any]=2				,A   :					List[Any]=3				,A   :					List[Any]="swish"				,A   :					Any=3				,A   :					Optional[Any]=32				,A   :					Union[str, Any]=0.1				,A   :					Union[str, Any]=0.02				,A   :					Any=True				,A   :					Optional[Any]=True				,A   :					List[Any]=10				,A   :					Union[str, Any]=None				,A   :					Any=0.25				,A   :					int=0.0				,A   :					Tuple=0.0				,):
					__A       =					parent
					__A       =					batch_size
					__A       =					image_size
					__A       =					patch_size
					__A       =					num_channels
					__A       =					make_divisible(5_12 * width_multiplier				,divisor=8					)
					__A       =					hidden_act
					__A       =					conv_kernel_size
					__A       =					output_stride
					__A       =					classifier_dropout_prob
					__A       =					use_labels
					__A       =					is_training
					__A       =					num_labels
					__A       =					initializer_range
					__A       =					scope
					__A       =					width_multiplier
					__A       =					ffn_dropout
					__A       =					attn_dropout






	def 							UpperCamelCase_    (			self   :					List[Any]					):
					__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 							UpperCamelCase_    (			self   :					Tuple					):
					return MobileViTVaConfig(
					    image_size=self.image_size				,patch_size=self.patch_size				,num_channels=self.num_channels				,hidden_act=self.hidden_act				,conv_kernel_size=self.conv_kernel_size				,output_stride=self.output_stride				,classifier_dropout_prob=self.classifier_dropout_prob				,initializer_range=self.initializer_range				,width_multiplier=self.width_multiplier				,ffn_dropout=self.ffn_dropout_prob				,attn_dropout=self.attn_dropout_prob				,)






	def 							UpperCamelCase_    (			self   :					List[str]				,A   :					List[Any]				,A   :					List[str]				,A   :					Optional[Any]				,A   :					List[str]					):
					__A       =					MobileViTVaModel(config=A					)
					model.to(A					)
					model.eval()
					__A       =					model(A					)
					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 							UpperCamelCase_    (			self   :					List[Any]				,A   :					Dict				,A   :					str				,A   :					Any				,A   :					Tuple					):
					__A       =					self.num_labels
					__A       =					MobileViTVaForImageClassification(A					)
					model.to(A					)
					model.eval()
					__A       =					model(A				,labels=A					)
					self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.num_labels)					)






	def 							UpperCamelCase_    (			self   :					Dict				,A   :					str				,A   :					Tuple				,A   :					int				,A   :					Dict					):
					__A       =					self.num_labels
					__A       =					MobileViTVaForSemanticSegmentation(A					)
					model.to(A					)
					model.eval()
					__A       =					model(A					)
					self.parent.assertEqual(
					    result.logits.shape				,(
					        self.batch_size,
					        self.num_labels,
					        self.image_size // self.output_stride,
					        self.image_size // self.output_stride,
					    )				,)
					__A       =					model(A				,labels=A					)
					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 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					self.prepare_config_and_inputs()
					__A     ,				__A     ,				__A     ,				__A       =					config_and_inputs
					__A       =					{"pixel_values": pixel_values}
					return config, inputs_dict





@require_torch
class 				UpperCAmelCase (    __SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE     ,     unittest.TestCase    ):




	'''simple docstring'''





	snake_case_          =	(
	    (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)

	snake_case_          =	(
	    {
	        "feature-extraction": MobileViTVaModel,
	        "image-classification": MobileViTVaForImageClassification,
	        "image-segmentation": MobileViTVaForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	snake_case_          =	False
	snake_case_          =	False
	snake_case_          =	False
	snake_case_          =	False






	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					MobileViTVaModelTester(self					)
					__A       =					MobileViTVaConfigTester(self				,config_class=A				,has_text_modality=A					)






	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					self.config_tester.run_common_tests()






	@unittest.skip(reason="MobileViTV2 does not use inputs_embeds"					)
	def 							UpperCamelCase_    (			self   :					List[Any]					):
					pass






	@unittest.skip(reason="MobileViTV2 does not support input and output embeddings"					)
	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					pass






	@unittest.skip(reason="MobileViTV2 does not output attentions"					)
	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					pass






	@require_torch_multi_gpu
	@unittest.skip(reason="Got `CUDA error: misaligned address` for tests after this one being run."					)
	def 							UpperCamelCase_    (			self   :					Any					):
					pass






	@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."					)
	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					pass






	def 							UpperCamelCase_    (			self   :					Any					):
					__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.forward					)
									# 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 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A					)






	def 							UpperCamelCase_    (			self   :					List[Any]					):
					def check_hidden_states_output(A   :					Union[str, Any]				,A   :					int				,A   :					List[Any]					):
									__A       =					model_class(A					)
									model.to(A					)
									model.eval()

									with torch.no_grad():
													__A       =					model(**self._prepare_for_class(A				,A					)					)

									__A       =					outputs.hidden_states

									__A       =					5
									self.assertEqual(len(A					)				,A					)

									# MobileViTV2'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 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*A					)






	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_semantic_segmentation(*A					)







	@slow
	def 							UpperCamelCase_    (			self   :					List[Any]					):
					for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A       =					MobileViTVaModel.from_pretrained(A					)
									self.assertIsNotNone(A					)





def 		UpperCAmelCase     (      )						->      Optional[int]:







				"""simple docstring"""



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





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




	'''simple docstring'''





	@cached_property
	def 							UpperCamelCase_    (			self   :					List[Any]					):
					return (
					    MobileViTImageProcessor.from_pretrained("apple/mobilevitv2-1.0-imagenet1k-256"					)
					    if is_vision_available()
					    else None
					)






	@slow
	def 							UpperCamelCase_    (			self   :					Any					):
					__A       =					MobileViTVaForImageClassification.from_pretrained("apple/mobilevitv2-1.0-imagenet1k-256"					).to(
					    A					)

					__A       =					self.default_image_processor
					__A       =					prepare_img()
					__A       =					image_processor(images=A				,return_tensors="pt"					).to(A					)

					# forward pass
					with torch.no_grad():
									__A       =					model(**A					)

					# verify the logits
					__A       =					torch.Size((1, 10_00)					)
					self.assertEqual(outputs.logits.shape				,A					)

					__A       =					torch.tensor([-1.6336E00, -7.3204E-02, -5.1883E-01]					).to(A					)

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






	@slow
	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"					)
					__A       =					model.to(A					)

					__A       =					MobileViTImageProcessor.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"					)

					__A       =					prepare_img()
					__A       =					image_processor(images=A				,return_tensors="pt"					).to(A					)

					# forward pass
					with torch.no_grad():
									__A       =					model(**A					)
					__A       =					outputs.logits

					# verify the logits
					__A       =					torch.Size((1, 21, 32, 32)					)
					self.assertEqual(logits.shape				,A					)

					__A       =					torch.tensor(
					    [
					        [[7.08_63, 7.15_25, 6.82_01], [6.69_31, 6.87_70, 6.89_33], [6.29_78, 7.03_66, 6.96_36]],
					        [[-3.71_34, -3.67_12, -3.66_75], [-3.58_25, -3.35_49, -3.47_77], [-3.34_35, -3.39_79, -3.28_57]],
					        [[-2.93_29, -2.80_03, -2.73_69], [-3.05_64, -2.47_80, -2.02_07], [-2.68_89, -1.92_98, -1.76_40]],
					    ]				,device=A				,)

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







	@slow
	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					__A       =					MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"					)
					__A       =					model.to(A					)

					__A       =					MobileViTImageProcessor.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"					)

					__A       =					prepare_img()
					__A       =					image_processor(images=A				,return_tensors="pt"					).to(A					)

					# forward pass
					with torch.no_grad():
									__A       =					model(**A					)

					__A       =					outputs.logits.detach().cpu()

					__A       =					image_processor.post_process_semantic_segmentation(outputs=A				,target_sizes=[(50, 60)]					)
					__A       =					torch.Size((50, 60)					)
					self.assertEqual(segmentation[0].shape				,A					)

					__A       =					image_processor.post_process_semantic_segmentation(outputs=A					)
					__A       =					torch.Size((32, 32)					)
					self.assertEqual(segmentation[0].shape				,A					)







from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


__UpperCamelCase:	Optional[int] = logging.get_logger(__name__)


class       __lowerCAmelCase					(       _UpperCamelCase      ):


	'''simple docstring'''





	_A													=				["audio_values", "audio_mask"]

	def __init__(						self:						Any,					lowerCamelCase_:						int=2048,					lowerCamelCase_:						str=1,					lowerCamelCase_:						str=[16, 16],					lowerCamelCase_:						Union[str, Any]=128,					lowerCamelCase_:						Tuple=44100,					lowerCamelCase_:						Union[str, Any]=86,					lowerCamelCase_:						Optional[Any]=2048,					lowerCamelCase_:						Dict=0.0,					**lowerCamelCase_:						str,					):
			super().__init__(
			    feature_size=lowerCamelCase_,					sampling_rate=lowerCamelCase_,					padding_value=lowerCamelCase_,					**lowerCamelCase_,					)

			lowercase__					:      List[str]      =		spectrogram_length
			lowercase__					:      Union[str, Any]      =		num_channels
			lowercase__					:      int      =		patch_size
			lowercase__					:      Union[str, Any]      =		feature_size // self.patch_size[1]
			lowercase__					:      Optional[int]      =		n_fft
			lowercase__					:      int      =		sampling_rate // hop_length_to_sampling_rate
			lowercase__					:      str      =		sampling_rate
			lowercase__					:      Union[str, Any]      =		padding_value
			lowercase__					:      Any      =		mel_filter_bank(
			    num_frequency_bins=1 + n_fft // 2,					num_mel_filters=lowerCamelCase_,					min_frequency=0.0,					max_frequency=2_2_0_5_0.0,					sampling_rate=lowerCamelCase_,					norm='slaney',					mel_scale='slaney',					).T

	def    snake_case__(						self:						Tuple,					lowerCamelCase_:						np.array			):
			lowercase__					:      Optional[Any]      =		spectrogram(
			    lowerCamelCase_,					window_function(self.n_fft,					'hann'			),					frame_length=self.n_fft,					hop_length=self.hop_length,					power=2.0,					mel_filters=self.mel_filters.T,					log_mel='dB',					db_range=8_0.0,					)
			lowercase__					:      Optional[Any]      =		log_spec[:, :-1]
			lowercase__					:      int      =		log_spec - 2_0.0
			lowercase__					:      Dict      =		np.clip(log_spec / 4_0.0,					-2.0,					0.0			) + 1.0
			return log_spec





	def __call__(						self:						Dict,					lowerCamelCase_:						Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]],					lowerCamelCase_:						Optional[Union[str, TensorType]] = None,					lowerCamelCase_:						Optional[bool] = True,					lowerCamelCase_:						Optional[int] = None,					lowerCamelCase_:						bool = False,					lowerCamelCase_:						bool = False,					**lowerCamelCase_:						str,					):

			if sampling_rate is not None:
					if sampling_rate != self.sampling_rate:
							raise ValueError(
							    'This feature extractor is set to support sampling rate'
							    F""" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled"""
							    F""" with {self.sampling_rate} and not {sampling_rate}."""			)
			else:
					logger.warning(
					    'It is strongly recommended to pass the `sampling_rate` argument to this function. '
					    'Failing to do so can result in silent errors that might be hard to debug.'			)

			lowercase__					:      Any      =		isinstance(lowerCamelCase_,					np.ndarray			) and len(raw_speech.shape			) > 1
			if is_batched_numpy and len(raw_speech.shape			) > 2:
					raise ValueError(F"""Only mono-channel audio is supported for input to {self}"""			)
			lowercase__					:      Optional[Any]      =		is_batched_numpy or (
			    isinstance(lowerCamelCase_,					(list, tuple)			) and (isinstance(raw_speech[0],					(np.ndarray, tuple, list)			))
			)
			if is_batched:
					lowercase__					:      int      =		[np.asarray([speech],					dtype=np.floataa			).T for speech in raw_speech]
			elif not is_batched and not isinstance(lowerCamelCase_,					np.ndarray			):
					lowercase__					:      Optional[int]      =		np.asarray(lowerCamelCase_,					dtype=np.floataa			)
			elif isinstance(lowerCamelCase_,					np.ndarray			) and raw_speech.dtype is np.dtype(np.floataa			):
					lowercase__					:      str      =		raw_speech.astype(np.floataa			)
			# always return batch
			if not is_batched:
					lowercase__					:      Tuple      =		[np.asarray([raw_speech]			).T]

			# Convert audio signals to log mel spectrograms, truncate by time axis
			lowercase__					:      List[str]      =		[
			    self._np_extract_fbank_features(waveform.squeeze()			).T[: self.spectrogram_length] for waveform in raw_speech
			]
			if isinstance(audio_features[0],					lowerCamelCase_			):
					lowercase__					:      str      =		[np.asarray(lowerCamelCase_,					dtype=np.floataa			) for feature in audio_features]

			# Create audio attention mask
			lowercase__					:      Dict      =		max(
			    [ceil(feature.shape[0] / self.patch_size[0]			) * self.freq_len for feature in audio_features]			)  # The maximum number of audio patches in a batch
			if return_attention_mask:
					lowercase__					:      int      =		[
					    (ceil(feature.shape[0] / self.patch_size[0]			) * self.freq_len) * [1]
					    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]			) * self.freq_len) * [0]
					    for feature in audio_features
					]
					lowercase__					:      Any      =		np.array(lowerCamelCase_			).astype(np.floataa			)

			# convert into correct format for padding
			lowercase__					:      Any      =		max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
			lowercase__					:      Tuple      =		np.ones([len(lowerCamelCase_			), 1, max_time_len, self.feature_size]			).astype(np.floataa			)
			lowercase__					:      Dict      =		padded_audio_features * self.padding_value
			for i in range(len(lowerCamelCase_			)			):
					lowercase__					:      Union[str, Any]      =		audio_features[i]
					lowercase__					:      str      =		feature

			# return as BatchFeature
			if return_attention_mask:
					lowercase__					:      Dict      =		{'audio_values': padded_audio_features, 'audio_mask': audio_mask}
			else:
					lowercase__					:      int      =		{'audio_values': padded_audio_features}

			lowercase__					:      List[Any]      =		BatchFeature(data=lowerCamelCase_,					tensor_type=lowerCamelCase_			)
			return encoded_inputs


