






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 							SCREAMING_SNAKE_CASE__		(       snake_case__		):





					"""simple docstring"""






					def  _lowercase  (      self  :						str ):
						snake_case__				:   List[str]     =						self.config_class(**self.inputs_dict )
						self.parent.assertTrue(hasattr(_A							,	"width_multiplier" ) )



class 							SCREAMING_SNAKE_CASE__		:





					"""simple docstring"""






					def __init__(      self  :						Optional[int]							,	__A  :						List[Any]							,	__A  :						List[Any]=1_3							,	__A  :						str=6_4							,	__A  :						Dict=2							,	__A  :						Tuple=3							,	__A  :						Optional[Any]="swish"							,	__A  :						Optional[Any]=3							,	__A  :						Optional[int]=3_2							,	__A  :						int=0.1							,	__A  :						str=0.0_2							,	__A  :						str=True							,	__A  :						Any=True							,	__A  :						Any=1_0							,	__A  :						Any=None							,	__A  :						Optional[int]=0.2_5							,	__A  :						int=0.0							,	__A  :						int=0.0							,	):
						snake_case__				:   Optional[Any]     =						parent
						snake_case__				:   Dict     =						batch_size
						snake_case__				:   Union[str, Any]     =						image_size
						snake_case__				:   Dict     =						patch_size
						snake_case__				:   Tuple     =						num_channels
						snake_case__				:   List[Any]     =						make_divisible(5_1_2 * width_multiplier							,	divisor=8 )
						snake_case__				:   List[str]     =						hidden_act
						snake_case__				:   Union[str, Any]     =						conv_kernel_size
						snake_case__				:   Union[str, Any]     =						output_stride
						snake_case__				:   Union[str, Any]     =						classifier_dropout_prob
						snake_case__				:   str     =						use_labels
						snake_case__				:   Optional[int]     =						is_training
						snake_case__				:   Any     =						num_labels
						snake_case__				:   Any     =						initializer_range
						snake_case__				:   Tuple     =						scope
						snake_case__				:   Optional[Any]     =						width_multiplier
						snake_case__				:   str     =						ffn_dropout
						snake_case__				:   Dict     =						attn_dropout



					def  _lowercase  (      self  :						Union[str, Any] ):
						snake_case__				:   List[str]     =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

						snake_case__				:   Any     =						self.get_config()

						return config, pixel_values, labels, pixel_labels



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



					def  _lowercase  (      self  :						List[Any]							,	__A  :						Tuple							,	__A  :						Any							,	__A  :						str							,	__A  :						List[Any] ):
						snake_case__				:   Dict     =						self.num_labels
						snake_case__				:   Dict     =						MobileViTVaForSemanticSegmentation(_A )
						model.to(_A )
						model.eval()
						snake_case__				:   Tuple     =						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,
						    )							,	)
						snake_case__				:   Optional[int]     =						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  _lowercase  (      self  :						List[Any] ):
						snake_case__				:   Optional[Any]     =						self.prepare_config_and_inputs()
						snake_case__				:   Optional[int]     =						config_and_inputs
						snake_case__				:   List[Any]     =						{'pixel_values': pixel_values}
						return config, inputs_dict



@require_torch
class 							SCREAMING_SNAKE_CASE__		(       snake_case__				,     snake_case__				,     unittest.TestCase		):





					"""simple docstring"""






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

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

					a_			=	False
					a_			=	False
					a_			=	False
					a_			=	False



					def  _lowercase  (      self  :						Union[str, Any] ):
						snake_case__				:   Optional[int]     =						MobileViTVaModelTester(self )
						snake_case__				:   int     =						MobileViTVaConfigTester(self							,	config_class=_A							,	has_text_modality=_A )



					def  _lowercase  (      self  :						List[str] ):
						self.config_tester.run_common_tests()



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



					@unittest.skip(reason="MobileViTV2 does not support input and output embeddings" )
					def  _lowercase  (      self  :						Optional[int] ):
						pass



					@unittest.skip(reason="MobileViTV2 does not output attentions" )
					def  _lowercase  (      self  :						int ):
						pass



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



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



					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   int     =						self.model_tester.prepare_config_and_inputs_for_common()

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

							snake_case__				:   Tuple     =						['pixel_values']
							self.assertListEqual(arg_names[:1]							,	_A )



					def  _lowercase  (      self  :						List[Any] ):
						snake_case__				:   int     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_A )



					def  _lowercase  (      self  :						List[str] ):
						def check_hidden_states_output(__A  :						Optional[Any]							,	__A  :						Optional[Any]							,	__A  :						Dict ):
							snake_case__				:   Optional[int]     =						model_class(_A )
							model.to(_A )
							model.eval()

							with torch.no_grad():
								snake_case__				:   Dict     =						model(**self._prepare_for_class(_A							,	_A ) )

							snake_case__				:   Optional[int]     =						outputs.hidden_states

							snake_case__				:   Any     =						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.
							snake_case__				:   Optional[Any]     =						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 )

						snake_case__				:   Optional[Any]     =						self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							snake_case__				:   List[str]     =						True
							check_hidden_states_output(_A							,	_A							,	_A )

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

							check_hidden_states_output(_A							,	_A							,	_A )



					def  _lowercase  (      self  :						str ):
						snake_case__				:   Dict     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*_A )



					def  _lowercase  (      self  :						Optional[int] ):
						snake_case__				:   Optional[int]     =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*_A )






					@slow
					def  _lowercase  (      self  :						Optional[int] ):
						for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							snake_case__				:   List[str]     =						MobileViTVaModel.from_pretrained(_A )
							self.assertIsNotNone(_A )


def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   Tuple     =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
	return image





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





					"""simple docstring"""






					@cached_property
					def  _lowercase  (      self  :						str ):
						return (
						    MobileViTImageProcessor.from_pretrained("apple/mobilevitv2-1.0-imagenet1k-256" )
						    if is_vision_available()
						    else None
						)



					@slow
					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   Dict     =						MobileViTVaForImageClassification.from_pretrained("apple/mobilevitv2-1.0-imagenet1k-256" ).to(
						    _A )

						snake_case__				:   Tuple     =						self.default_image_processor
						snake_case__				:   Union[str, Any]     =						prepare_img()
						snake_case__				:   int     =						image_processor(images=_A							,	return_tensors="pt" ).to(_A )

						# forward pass
						with torch.no_grad():
							snake_case__				:   str     =						model(**_A )

						# verify the logits
						snake_case__				:   Dict     =						torch.Size((1, 1_0_0_0) )
						self.assertEqual(outputs.logits.shape							,	_A )

						snake_case__				:   Dict     =						torch.tensor([-1.6_3_3_6e0_0, -7.3_2_0_4e-0_2, -5.1_8_8_3e-0_1] ).to(_A )

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



					@slow
					def  _lowercase  (      self  :						Tuple ):
						snake_case__				:   List[str]     =						MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3" )
						snake_case__				:   Union[str, Any]     =						model.to(_A )

						snake_case__				:   int     =						MobileViTImageProcessor.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3" )

						snake_case__				:   Union[str, Any]     =						prepare_img()
						snake_case__				:   Dict     =						image_processor(images=_A							,	return_tensors="pt" ).to(_A )

						# forward pass
						with torch.no_grad():
							snake_case__				:   int     =						model(**_A )
						snake_case__				:   Optional[int]     =						outputs.logits

						# verify the logits
						snake_case__				:   List[str]     =						torch.Size((1, 2_1, 3_2, 3_2) )
						self.assertEqual(logits.shape							,	_A )

						snake_case__				:   Dict     =						torch.tensor(
						    [
						        [[7.0_8_6_3, 7.1_5_2_5, 6.8_2_0_1], [6.6_9_3_1, 6.8_7_7_0, 6.8_9_3_3], [6.2_9_7_8, 7.0_3_6_6, 6.9_6_3_6]],
						        [[-3.7_1_3_4, -3.6_7_1_2, -3.6_6_7_5], [-3.5_8_2_5, -3.3_5_4_9, -3.4_7_7_7], [-3.3_4_3_5, -3.3_9_7_9, -3.2_8_5_7]],
						        [[-2.9_3_2_9, -2.8_0_0_3, -2.7_3_6_9], [-3.0_5_6_4, -2.4_7_8_0, -2.0_2_0_7], [-2.6_8_8_9, -1.9_2_9_8, -1.7_6_4_0]],
						    ]							,	device=_A							,	)

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






					@slow
					def  _lowercase  (      self  :						int ):
						snake_case__				:   Tuple     =						MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3" )
						snake_case__				:   str     =						model.to(_A )

						snake_case__				:   Union[str, Any]     =						MobileViTImageProcessor.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3" )

						snake_case__				:   Union[str, Any]     =						prepare_img()
						snake_case__				:   List[Any]     =						image_processor(images=_A							,	return_tensors="pt" ).to(_A )

						# forward pass
						with torch.no_grad():
							snake_case__				:   str     =						model(**_A )

						snake_case__				:   Dict     =						outputs.logits.detach().cpu()

						snake_case__				:   Optional[int]     =						image_processor.post_process_semantic_segmentation(outputs=_A							,	target_sizes=[(5_0, 6_0)] )
						snake_case__				:   Dict     =						torch.Size((5_0, 6_0) )
						self.assertEqual(segmentation[0].shape							,	_A )

						snake_case__				:   List[str]     =						image_processor.post_process_semantic_segmentation(outputs=_A )
						snake_case__				:   str     =						torch.Size((3_2, 3_2) )
						self.assertEqual(segmentation[0].shape							,	_A )











from unittest.mock import patch

import pyspark

from datasets.packaged_modules.spark.spark import (
    Spark,
    SparkExamplesIterable,
    _generate_iterable_examples,
)

from ..utils import (
    require_dill_gt_0_3_2,
    require_not_windows,
)







def   SCREAMING_SNAKE_CASE       (				snake_case_      :							List[Any]  ,						snake_case_      :							Union[str, Any]     ):
	snake_case__				:   Optional[int]     =						[]
	for part_id in partition_order:
		snake_case__				:   List[Any]     =						df.where(F'''SPARK_PARTITION_ID() = {part_id}'''     ).collect()
		for row_idx, row in enumerate(snake_case_     ):
			expected_row_ids_and_row_dicts.append((F'''{part_id}_{row_idx}''', row.asDict())     )
	return expected_row_ids_and_row_dicts







@require_not_windows
@require_dill_gt_0_3_2
def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   Tuple     =						pyspark.sql.SparkSession.builder.master("local[*]"     ).appName("pyspark"     ).getOrCreate()
	snake_case__				:   Union[str, Any]     =						spark.range(100     ).repartition(1     )
	snake_case__				:   Any     =						Spark(snake_case_     )
	# The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means
	# that each partition can hold 2 rows.
	spark_builder._repartition_df_if_needed(max_shard_size=16     )
	# Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions.
	assert spark_builder.df.rdd.getNumPartitions() == 50







@require_not_windows
@require_dill_gt_0_3_2
def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   Dict     =						pyspark.sql.SparkSession.builder.master("local[*]"     ).appName("pyspark"     ).getOrCreate()
	snake_case__				:   Optional[Any]     =						spark.range(10     ).repartition(2     )
	snake_case__				:   Optional[Any]     =						[1, 0]
	snake_case__				:   Dict     =						_generate_iterable_examples(snake_case_  ,						snake_case_     )  # Reverse the partitions.
	snake_case__				:   Tuple     =						_get_expected_row_ids_and_row_dicts_for_partition_order(snake_case_  ,						snake_case_     )

	for i, (row_id, row_dict) in enumerate(generate_fn()     ):
		snake_case__,		snake_case__				:   Tuple     =						expected_row_ids_and_row_dicts[i]
		assert row_id == expected_row_id
		assert row_dict == expected_row_dict







@require_not_windows
@require_dill_gt_0_3_2
def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   Optional[int]     =						pyspark.sql.SparkSession.builder.master("local[*]"     ).appName("pyspark"     ).getOrCreate()
	snake_case__				:   Optional[int]     =						spark.range(10     ).repartition(1     )
	snake_case__				:   Union[str, Any]     =						SparkExamplesIterable(snake_case_     )
	assert it.n_shards == 1
	for i, (row_id, row_dict) in enumerate(snake_case_     ):
		assert row_id == F'''0_{i}'''
		assert row_dict == {"id": i}







@require_not_windows
@require_dill_gt_0_3_2
def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   Optional[int]     =						pyspark.sql.SparkSession.builder.master("local[*]"     ).appName("pyspark"     ).getOrCreate()
	snake_case__				:   str     =						spark.range(30     ).repartition(3     )
	# Mock the generator so that shuffle reverses the partition indices.
	with patch("numpy.random.Generator"     ) as generator_mock:
		snake_case__				:   Union[str, Any]     =						lambda snake_case_     : x.reverse()
		snake_case__				:   Optional[int]     =						_get_expected_row_ids_and_row_dicts_for_partition_order(snake_case_  ,						[2, 1, 0]     )

		snake_case__				:   List[Any]     =						SparkExamplesIterable(snake_case_     ).shuffle_data_sources(snake_case_     )
		assert shuffled_it.n_shards == 3
		for i, (row_id, row_dict) in enumerate(snake_case_     ):
			snake_case__,		snake_case__				:   Optional[Any]     =						expected_row_ids_and_row_dicts[i]
			assert row_id == expected_row_id
			assert row_dict == expected_row_dict







@require_not_windows
@require_dill_gt_0_3_2
def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   Any     =						pyspark.sql.SparkSession.builder.master("local[*]"     ).appName("pyspark"     ).getOrCreate()
	snake_case__				:   Tuple     =						spark.range(20     ).repartition(4     )

	# Partitions 0 and 2
	snake_case__				:   List[Any]     =						SparkExamplesIterable(snake_case_     ).shard_data_sources(worker_id=0  ,						num_workers=2     )
	assert shard_it_a.n_shards == 2
	snake_case__				:   List[str]     =						_get_expected_row_ids_and_row_dicts_for_partition_order(snake_case_  ,						[0, 2]     )
	for i, (row_id, row_dict) in enumerate(snake_case_     ):
		snake_case__,		snake_case__				:   Optional[int]     =						expected_row_ids_and_row_dicts_a[i]
		assert row_id == expected_row_id
		assert row_dict == expected_row_dict

	# Partitions 1 and 3
	snake_case__				:   Any     =						SparkExamplesIterable(snake_case_     ).shard_data_sources(worker_id=1  ,						num_workers=2     )
	assert shard_it_a.n_shards == 2
	snake_case__				:   List[Any]     =						_get_expected_row_ids_and_row_dicts_for_partition_order(snake_case_  ,						[1, 3]     )
	for i, (row_id, row_dict) in enumerate(snake_case_     ):
		snake_case__,		snake_case__				:   Optional[Any]     =						expected_row_ids_and_row_dicts_a[i]
		assert row_id == expected_row_id
		assert row_dict == expected_row_dict







@require_not_windows
@require_dill_gt_0_3_2
def   SCREAMING_SNAKE_CASE       (				):
	snake_case__				:   Dict     =						pyspark.sql.SparkSession.builder.master("local[*]"     ).appName("pyspark"     ).getOrCreate()
	snake_case__				:   Tuple     =						spark.range(100     ).repartition(1     )
	snake_case__				:   Union[str, Any]     =						Spark(snake_case_     )
	# Choose a small max_shard_size for maximum partitioning.
	spark_builder._repartition_df_if_needed(max_shard_size=1     )
	# The new number of partitions should not be greater than the number of rows.
	assert spark_builder.df.rdd.getNumPartitions() == 100



