





import inspect
import unittest

from transformers import MobileViTConfig
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 MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
		from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import MobileViTImageProcessor

class 		snake_case_   (							UpperCAmelCase_						):







	'''simple docstring'''




	def    UpperCAmelCase			(						self							:     Dict   )       -> List[str]:

			'''simple docstring'''




			__lowercase											=     self.config_class(**self.inputs_dict   )
			self.parent.assertTrue(hasattr(__lowerCamelCase					,      'hidden_sizes'   )   )
			self.parent.assertTrue(hasattr(__lowerCamelCase					,      'neck_hidden_sizes'   )   )
			self.parent.assertTrue(hasattr(__lowerCamelCase					,      'num_attention_heads'   )   )





class 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     Any					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int=13					,      __lowerCamelCase							:     List[str]=32					,      __lowerCamelCase							:     int=2					,      __lowerCamelCase							:     Union[str, Any]=3					,      __lowerCamelCase							:     Optional[int]=640					,      __lowerCamelCase							:     List[str]=4					,      __lowerCamelCase							:     int="silu"					,      __lowerCamelCase							:     Tuple=3					,      __lowerCamelCase							:     Optional[int]=32					,      __lowerCamelCase							:     Optional[Any]=0.1					,      __lowerCamelCase							:     int=0.1					,      __lowerCamelCase							:     Optional[int]=0.1					,      __lowerCamelCase							:     int=0.02					,      __lowerCamelCase							:     Optional[Any]=True					,      __lowerCamelCase							:     Optional[Any]=True					,      __lowerCamelCase							:     str=10					,      __lowerCamelCase							:     List[Any]=None					,      )       -> List[Any]:

			'''simple docstring'''




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




	def    UpperCAmelCase			(						self							:     int   )       -> str:

			'''simple docstring'''




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

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

			__lowercase											=     self.get_config()

			return config, pixel_values, labels, pixel_labels




	def    UpperCAmelCase			(						self							:     int   )       -> Any:

			'''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    UpperCAmelCase			(						self							:     Optional[int]					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     str   )       -> Optional[Any]:

			'''simple docstring'''




			__lowercase											=     MobileViTModel(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     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,
			    )					,      )




	def    UpperCAmelCase			(						self							:     str					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     Any   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     MobileViTForImageClassification(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      labels=__lowerCamelCase   )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)   )




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Optional[int]   )       -> Union[str, Any]:

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     MobileViTForSemanticSegmentation(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     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,
			    )					,      )
			__lowercase											=     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    UpperCAmelCase			(						self							:     Tuple   )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     self.prepare_config_and_inputs()
			__lowercase						,      __lowercase						,      __lowercase						,      __lowercase											=     config_and_inputs
			__lowercase											=     {'pixel_values': pixel_values}
			return config, inputs_dict





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







	'''simple docstring'''
	__UpperCamelCase   =		(
	    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	__UpperCamelCase   =		(
	    {
	        '''feature-extraction''': MobileViTModel,
	        '''image-classification''': MobileViTForImageClassification,
	        '''image-segmentation''': MobileViTForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

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




	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> Optional[Any]:

			'''simple docstring'''




			__lowercase											=     MobileViTModelTester(self   )
			__lowercase											=     MobileViTConfigTester(self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase   )




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

			'''simple docstring'''




			self.config_tester.run_common_tests()




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

			'''simple docstring'''




			pass




	@unittest.skip(reason='MobileViT does not support input and output embeddings'   )
	def    UpperCAmelCase			(						self							:     Any   )       -> Union[str, Any]:

			'''simple docstring'''




			pass




	@unittest.skip(reason='MobileViT does not output attentions'   )
	def    UpperCAmelCase			(						self							:     int   )       -> Union[str, Any]:

			'''simple docstring'''




			pass




	def    UpperCAmelCase			(						self							:     str   )       -> int:

			'''simple docstring'''




			__lowercase						,      __lowercase											=     self.model_tester.prepare_config_and_inputs_for_common()

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

					__lowercase											=     ['pixel_values']
					self.assertListEqual(arg_names[:1]					,      __lowerCamelCase   )




	@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'   )
	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> List[str]:

			'''simple docstring'''




			pass




	def    UpperCAmelCase			(						self							:     Dict   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*__lowerCamelCase   )




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

			'''simple docstring'''




			def check_hidden_states_output(__lowerCamelCase							:     Any					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple   ):
					__lowercase											=     model_class(__lowerCamelCase   )
					model.to(__lowerCamelCase   )
					model.eval()

					with torch.no_grad():
							__lowercase											=     model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase   )   )

					__lowercase											=     outputs.hidden_states

					__lowercase											=     5
					self.assertEqual(len(__lowerCamelCase   )					,      __lowerCamelCase   )

					# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
					# with the width and height being successively divided by 2.
					__lowercase											=     2
					for i in range(len(__lowerCamelCase   )   ):
							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   )

			__lowercase						,      __lowercase											=     self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					__lowercase											=     True
					check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase   )

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

					check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase   )




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

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     int   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCamelCase   )






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

			'''simple docstring'''




			for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__lowercase											=     MobileViTModel.from_pretrained(__lowerCamelCase   )
					self.assertIsNotNone(__lowerCamelCase   )





def  SCREAMING_SNAKE_CASE				(      )		->    Any:
		__lowercase											=     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
		return image


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







	'''simple docstring'''




	@cached_property
	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> Optional[int]:

			'''simple docstring'''




			return MobileViTImageProcessor.from_pretrained('apple/mobilevit-xx-small'   ) if is_vision_available() else None




	@slow
	def    UpperCAmelCase			(						self							:     Dict   )       -> int:

			'''simple docstring'''




			__lowercase											=     MobileViTForImageClassification.from_pretrained('apple/mobilevit-xx-small'   ).to(__lowerCamelCase   )

			__lowercase											=     self.default_image_processor
			__lowercase											=     prepare_img()
			__lowercase											=     image_processor(images=__lowerCamelCase					,      return_tensors='pt'   ).to(__lowerCamelCase   )

			# forward pass
			with torch.no_grad():
					__lowercase											=     model(**__lowerCamelCase   )

			# verify the logits
			__lowercase											=     torch.Size((1, 1_000)   )
			self.assertEqual(outputs.logits.shape					,      __lowerCamelCase   )

			__lowercase											=     torch.tensor([-1.9364, -1.2327, -0.4653]   ).to(__lowerCamelCase   )

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




	@slow
	def    UpperCAmelCase			(						self							:     str   )       -> Any:

			'''simple docstring'''




			__lowercase											=     MobileViTForSemanticSegmentation.from_pretrained('apple/deeplabv3-mobilevit-xx-small'   )
			__lowercase											=     model.to(__lowerCamelCase   )

			__lowercase											=     MobileViTImageProcessor.from_pretrained('apple/deeplabv3-mobilevit-xx-small'   )

			__lowercase											=     prepare_img()
			__lowercase											=     image_processor(images=__lowerCamelCase					,      return_tensors='pt'   ).to(__lowerCamelCase   )

			# forward pass
			with torch.no_grad():
					__lowercase											=     model(**__lowerCamelCase   )
			__lowercase											=     outputs.logits

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

			__lowercase											=     torch.tensor(
			    [
			        [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
			        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
			        [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
			    ]					,      device=__lowerCamelCase					,      )

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






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

			'''simple docstring'''




			__lowercase											=     MobileViTForSemanticSegmentation.from_pretrained('apple/deeplabv3-mobilevit-xx-small'   )
			__lowercase											=     model.to(__lowerCamelCase   )

			__lowercase											=     MobileViTImageProcessor.from_pretrained('apple/deeplabv3-mobilevit-xx-small'   )

			__lowercase											=     prepare_img()
			__lowercase											=     image_processor(images=__lowerCamelCase					,      return_tensors='pt'   ).to(__lowerCamelCase   )

			# forward pass
			with torch.no_grad():
					__lowercase											=     model(**__lowerCamelCase   )

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

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

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











from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_   :      Dict			=   logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_   :      Union[str, Any]			=   {
    '''RWKV/rwkv-4-169m-pile''': '''https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json''',
    '''RWKV/rwkv-4-430m-pile''': '''https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json''',
    '''RWKV/rwkv-4-1b5-pile''': '''https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json''',
    '''RWKV/rwkv-4-3b-pile''': '''https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json''',
    '''RWKV/rwkv-4-7b-pile''': '''https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json''',
    '''RWKV/rwkv-4-14b-pile''': '''https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json''',
    '''RWKV/rwkv-raven-1b5''': '''https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json''',
    '''RWKV/rwkv-raven-3b''': '''https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json''',
    '''RWKV/rwkv-raven-7b''': '''https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json''',
    '''RWKV/rwkv-raven-14b''': '''https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json''',
}

class 		snake_case_   (							UpperCAmelCase_						):







	'''simple docstring'''
	__UpperCamelCase   =		'''rwkv'''
	__UpperCamelCase   =		{'''max_position_embeddings''': '''context_length'''}




	def __init__(						self							:     List[str]					,      __lowerCamelCase							:     Tuple=50_277					,      __lowerCamelCase							:     List[str]=1_024					,      __lowerCamelCase							:     List[Any]=4_096					,      __lowerCamelCase							:     Tuple=32					,      __lowerCamelCase							:     List[str]=None					,      __lowerCamelCase							:     Optional[int]=None					,      __lowerCamelCase							:     Optional[Any]=1E-5					,      __lowerCamelCase							:     Tuple=0					,      __lowerCamelCase							:     int=0					,      __lowerCamelCase							:     Dict=6					,      __lowerCamelCase							:     List[Any]=False					,      __lowerCamelCase							:     Tuple=True					,      **__lowerCamelCase							:     Optional[int]					,      )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     vocab_size
			__lowercase											=     context_length
			__lowercase											=     hidden_size
			__lowercase											=     num_hidden_layers
			__lowercase											=     attention_hidden_size if attention_hidden_size is not None else hidden_size
			__lowercase											=     intermediate_size if intermediate_size is not None else 4 * hidden_size
			__lowercase											=     layer_norm_epsilon
			__lowercase											=     rescale_every
			__lowercase											=     use_cache

			__lowercase											=     bos_token_id
			__lowercase											=     eos_token_id

			super().__init__(
			    tie_word_embeddings=__lowerCamelCase					,      bos_token_id=__lowerCamelCase					,      eos_token_id=__lowerCamelCase					,      **__lowerCamelCase   )




