



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, MobileNetVaModel
							from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import MobileNetVaImageProcessor



class        lowerCamelCase__			(							_UpperCamelCase):




		'''simple docstring'''



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

						"""simple docstring"""






						lowerCAmelCase__     :				Optional[Any]												=     self.config_class(**self.inputs_dict )
						self.parent.assertTrue(hasattr(lowerCAmelCase_		,'''tf_padding''' ) )
						self.parent.assertTrue(hasattr(lowerCAmelCase_		,'''depth_multiplier''' ) )



class        lowerCamelCase__			:




		'''simple docstring'''



		def __init__(self		,__lowerCamelCase		,__lowerCamelCase=13		,__lowerCamelCase=3		,__lowerCamelCase=32		,__lowerCamelCase=0.25		,__lowerCamelCase=8		,__lowerCamelCase=True		,__lowerCamelCase=10_24		,__lowerCamelCase=32		,__lowerCamelCase="relu6"		,__lowerCamelCase=0.1		,__lowerCamelCase=0.02		,__lowerCamelCase=True		,__lowerCamelCase=True		,__lowerCamelCase=10		,__lowerCamelCase=None		,)     ->			Optional[Any]:

						"""simple docstring"""






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



		def 		lowerCAmelCase__      (self )     ->			Tuple:

						"""simple docstring"""






						lowerCAmelCase__     :				Optional[Any]												=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

						lowerCAmelCase__     :				Optional[Any]												=     self.get_config()

						return config, pixel_values, labels, pixel_labels



		def 		lowerCAmelCase__      (self )     ->			Any:

						"""simple docstring"""






						return MobileNetVaConfig(
						    num_channels=self.num_channels		,image_size=self.image_size		,depth_multiplier=self.depth_multiplier		,min_depth=self.min_depth		,tf_padding=self.tf_padding		,hidden_act=self.hidden_act		,classifier_dropout_prob=self.classifier_dropout_prob		,initializer_range=self.initializer_range		,)



		def 		lowerCAmelCase__      (self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase )     ->			str:

						"""simple docstring"""






						lowerCAmelCase__     :				Optional[Any]												=     MobileNetVaModel(config=lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						lowerCAmelCase__     :				List[str]												=     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 		lowerCAmelCase__      (self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase )     ->			Optional[int]:

						"""simple docstring"""






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



		def 		lowerCAmelCase__      (self )     ->			Any:

						"""simple docstring"""






						lowerCAmelCase__     :				List[str]												=     self.prepare_config_and_inputs()
						lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__     :				Optional[Any]												=     config_and_inputs
						lowerCAmelCase__     :				Any												=     {'''pixel_values''': pixel_values}
						return config, inputs_dict



@require_torch
class        lowerCamelCase__			(							_UpperCamelCase      ,				_UpperCamelCase      ,				unittest.TestCase):




		'''simple docstring'''



		snake_case_	=(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
		snake_case_	=(
		    {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
		    if is_torch_available()
		    else {}
		)

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



		def 		lowerCAmelCase__      (self )     ->			Tuple:

						"""simple docstring"""






						lowerCAmelCase__     :				int												=     MobileNetVaModelTester(self )
						lowerCAmelCase__     :				Optional[Any]												=     MobileNetVaConfigTester(self		,config_class=lowerCAmelCase_		,has_text_modality=lowerCAmelCase_ )



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

						"""simple docstring"""






						self.config_tester.run_common_tests()



		@unittest.skip(reason='''MobileNetV1 does not use inputs_embeds''' )
		def 		lowerCAmelCase__      (self )     ->			Tuple:

						"""simple docstring"""






						pass



		@unittest.skip(reason='''MobileNetV1 does not support input and output embeddings''' )
		def 		lowerCAmelCase__      (self )     ->			List[str]:

						"""simple docstring"""






						pass



		@unittest.skip(reason='''MobileNetV1 does not output attentions''' )
		def 		lowerCAmelCase__      (self )     ->			List[Any]:

						"""simple docstring"""






						pass



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

						"""simple docstring"""






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

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

										lowerCAmelCase__     :				Dict												=     ['''pixel_values''']
										self.assertListEqual(arg_names[:1]		,lowerCAmelCase_ )



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

						"""simple docstring"""






						lowerCAmelCase__     :				Any												=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase_ )



		def 		lowerCAmelCase__      (self )     ->			str:

						"""simple docstring"""






						def check_hidden_states_output(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase ):
										lowerCAmelCase__     :				Optional[Any]												=     model_class(lowerCAmelCase_ )
										model.to(lowerCAmelCase_ )
										model.eval()

										with torch.no_grad():
														lowerCAmelCase__     :				Tuple												=     model(**self._prepare_for_class(lowerCAmelCase_		,lowerCAmelCase_ ) )

										lowerCAmelCase__     :				str												=     outputs.hidden_states

										lowerCAmelCase__     :				Tuple												=     26
										self.assertEqual(len(lowerCAmelCase_ )		,lowerCAmelCase_ )

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

						for model_class in self.all_model_classes:
										lowerCAmelCase__     :				str												=     True
										check_hidden_states_output(lowerCAmelCase_		,lowerCAmelCase_		,lowerCAmelCase_ )

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

										check_hidden_states_output(lowerCAmelCase_		,lowerCAmelCase_		,lowerCAmelCase_ )



		def 		lowerCAmelCase__      (self )     ->			int:

						"""simple docstring"""






						lowerCAmelCase__     :				Any												=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_ )



		@slow
		def 		lowerCAmelCase__      (self )     ->			int:

						"""simple docstring"""






						for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__     :				Union[str, Any]												=     MobileNetVaModel.from_pretrained(lowerCAmelCase_ )
										self.assertIsNotNone(lowerCAmelCase_ )






def 	lowerCAmelCase__		(    ):





				'''simple docstring'''

				lowerCAmelCase__     :				Any												=     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image



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




		'''simple docstring'''



		@cached_property
		def 		lowerCAmelCase__      (self )     ->			List[Any]:

						"""simple docstring"""






						return (
						    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v1_1.0_224''' ) if is_vision_available() else None
						)



		@slow
		def 		lowerCAmelCase__      (self )     ->			str:

						"""simple docstring"""






						lowerCAmelCase__     :				str												=     MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v1_1.0_224''' ).to(lowerCAmelCase_ )

						lowerCAmelCase__     :				Optional[int]												=     self.default_image_processor
						lowerCAmelCase__     :				Union[str, Any]												=     prepare_img()
						lowerCAmelCase__     :				List[Any]												=     image_processor(images=lowerCAmelCase_		,return_tensors='''pt''' ).to(lowerCAmelCase_ )

						# forward pass
						with torch.no_grad():
										lowerCAmelCase__     :				Dict												=     model(**lowerCAmelCase_ )

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

						lowerCAmelCase__     :				Tuple												=     torch.tensor([-4.1739, -1.1233, 3.1205] ).to(lowerCAmelCase_ )

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








import pytest

from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs

@pytest.mark.parametrize(
    'kwargs, expected',	[
        ({'num_shards': 0, 'max_num_jobs': 1}, []),
        ({'num_shards': 10, 'max_num_jobs': 1}, [range(10	)]),
        ({'num_shards': 10, 'max_num_jobs': 10}, [range(lowerCAmelCase_,	i + 1	) for i in range(10	)]),
        ({'num_shards': 1, 'max_num_jobs': 10}, [range(1	)]),
        ({'num_shards': 10, 'max_num_jobs': 3}, [range(0,	4	), range(4,	7	), range(7,	10	)]),
        ({'num_shards': 3, 'max_num_jobs': 10}, [range(0,	1	), range(1,	2	), range(2,	3	)]),
    ],	)
def   a_    ( lowerCAmelCase_      :			Optional[Any],	lowerCAmelCase_      :			Any	):
   __lowerCAmelCase								=	_distribute_shards(**lowerCAmelCase_	)
   assert out == expected

@pytest.mark.parametrize(
    'gen_kwargs, max_num_jobs, expected',	[
        ({'foo': 0}, 10, [{'foo': 0}]),
        ({'shards': [0, 1, 2, 3]}, 1, [{'shards': [0, 1, 2, 3]}]),
        ({'shards': [0, 1, 2, 3]}, 4, [{'shards': [0]}, {'shards': [1]}, {'shards': [2]}, {'shards': [3]}]),
        ({'shards': [0, 1]}, 4, [{'shards': [0]}, {'shards': [1]}]),
        ({'shards': [0, 1, 2, 3]}, 2, [{'shards': [0, 1]}, {'shards': [2, 3]}]),
    ],	)
def   a_    ( lowerCAmelCase_      :			Optional[Any],	lowerCAmelCase_      :			List[str],	lowerCAmelCase_      :			Optional[int]	):
   __lowerCAmelCase								=	_split_gen_kwargs(lowerCAmelCase_,	lowerCAmelCase_	)
   assert out == expected




@pytest.mark.parametrize(
    'gen_kwargs, expected',	[
        ({'foo': 0}, 1),
        ({'shards': [0]}, 1),
        ({'shards': [0, 1, 2, 3]}, 4),
        ({'shards': [0, 1, 2, 3], 'foo': 0}, 4),
        ({'shards': [0, 1, 2, 3], 'other': (0, 1)}, 4),
        ({'shards': [0, 1, 2, 3], 'shards2': [0, 1]}, RuntimeError),
    ],	)
def   a_    ( lowerCAmelCase_      :			int,	lowerCAmelCase_      :			Any	):
   if expected is RuntimeError:
      with pytest.raises(lowerCAmelCase_	):
         _number_of_shards_in_gen_kwargs(lowerCAmelCase_	)
   else:
      __lowerCAmelCase								=	_number_of_shards_in_gen_kwargs(lowerCAmelCase_	)
      assert out == expected

