





'''simple docstring'''




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   A_	(  lowerCAmelCase_			):




 def      lowercase	(					self       :    Union[str, Any]    ):
      _UpperCAmelCase     							=   self.config_class(**self.inputs_dict    )
      self.parent.assertTrue(hasattr(snake_case_	,			"width_multiplier"    )    )







class   A_	:




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




 def      lowercase	(					self       :    str    ):
      _UpperCAmelCase     							=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

      _UpperCAmelCase     							=   self.get_config()

      return config, pixel_values, labels, pixel_labels




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




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







@require_torch
class   A_	(  lowerCAmelCase_							,						lowerCAmelCase_							,						unittest.TestCase			):
 _lowerCamelCase  : Union[str, Any]           =							(
     (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
     if is_torch_available()
     else ()
 )

 _lowerCamelCase  : List[str]           =							(
     {
         """feature-extraction""": MobileViTVaModel,
         """image-classification""": MobileViTVaForImageClassification,
         """image-segmentation""": MobileViTVaForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 _lowerCamelCase  : List[Any]           =							False
 _lowerCamelCase  : Optional[int]           =							False
 _lowerCamelCase  : List[str]           =							False
 _lowerCamelCase  : str           =							False




 def      lowercase	(					self       :    Optional[int]    ):
      _UpperCAmelCase     							=   MobileViTVaModelTester(self    )
      _UpperCAmelCase     							=   MobileViTVaConfigTester(self	,			config_class=snake_case_	,			has_text_modality=snake_case_    )




 def      lowercase	(					self       :    List[Any]    ):
      self.config_tester.run_common_tests()




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




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




 @unittest.skip(reason="MobileViTV2 does not output attentions"    )
 def      lowercase	(					self       :    Optional[Any]    ):
      pass




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




 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."    )
 def      lowercase	(					self       :    str    ):
      pass




 def      lowercase	(					self       :    List[Any]    ):
      _UpperCAmelCase							,  _UpperCAmelCase     							=   self.model_tester.prepare_config_and_inputs_for_common()

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

           _UpperCAmelCase     							=   ["pixel_values"]
           self.assertListEqual(arg_names[:1]	,			snake_case_    )




 def      lowercase	(					self       :    Tuple    ):
      _UpperCAmelCase     							=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case_    )




 def      lowercase	(					self       :    List[Any]    ):
      def check_hidden_states_output(snake_case_       :    Union[str, Any]	,			snake_case_       :    str	,			snake_case_       :    List[Any]    ):
           _UpperCAmelCase     							=   model_class(snake_case_    )
           model.to(snake_case_    )
           model.eval()

           with torch.no_grad():
                _UpperCAmelCase     							=   model(**self._prepare_for_class(snake_case_	,			snake_case_    )    )

           _UpperCAmelCase     							=   outputs.hidden_states

           _UpperCAmelCase     							=   5
           self.assertEqual(len(snake_case_    )	,			snake_case_    )

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

      _UpperCAmelCase							,  _UpperCAmelCase     							=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           _UpperCAmelCase     							=   True
           check_hidden_states_output(snake_case_	,			snake_case_	,			snake_case_    )

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

           check_hidden_states_output(snake_case_	,			snake_case_	,			snake_case_    )




 def      lowercase	(					self       :    Any    ):
      _UpperCAmelCase     							=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*snake_case_    )




 def      lowercase	(					self       :    Tuple    ):
      _UpperCAmelCase     							=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_semantic_segmentation(*snake_case_    )




 @slow
 def      lowercase	(					self       :    List[Any]    ):
      for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _UpperCAmelCase     							=   MobileViTVaModel.from_pretrained(snake_case_    )
           self.assertIsNotNone(snake_case_    )





def 	UpperCAmelCase_							(					)		->       List[Any]:


     '''simple docstring'''



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







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




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




 @slow
 def      lowercase	(					self       :    Dict    ):
      _UpperCAmelCase     							=   MobileViTVaForImageClassification.from_pretrained("apple/mobilevitv2-1.0-imagenet1k-256"    ).to(
          snake_case_    )

      _UpperCAmelCase     							=   self.default_image_processor
      _UpperCAmelCase     							=   prepare_img()
      _UpperCAmelCase     							=   image_processor(images=snake_case_	,			return_tensors="pt"    ).to(snake_case_    )

      # forward pass
      with torch.no_grad():
           _UpperCAmelCase     							=   model(**snake_case_    )

      # verify the logits
      _UpperCAmelCase     							=   torch.Size((1, 1_0_0_0)    )
      self.assertEqual(outputs.logits.shape	,			snake_case_    )

      _UpperCAmelCase     							=   torch.tensor([-1.6_336e00, -7.3_204e-02, -5.1_883e-01]    ).to(snake_case_    )

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




 @slow
 def      lowercase	(					self       :    Dict    ):
      _UpperCAmelCase     							=   MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"    )
      _UpperCAmelCase     							=   model.to(snake_case_    )

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

      _UpperCAmelCase     							=   prepare_img()
      _UpperCAmelCase     							=   image_processor(images=snake_case_	,			return_tensors="pt"    ).to(snake_case_    )

      # forward pass
      with torch.no_grad():
           _UpperCAmelCase     							=   model(**snake_case_    )
      _UpperCAmelCase     							=   outputs.logits

      # verify the logits
      _UpperCAmelCase     							=   torch.Size((1, 2_1, 3_2, 3_2)    )
      self.assertEqual(logits.shape	,			snake_case_    )

      _UpperCAmelCase     							=   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=snake_case_	,			)

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




 @slow
 def      lowercase	(					self       :    Dict    ):
      _UpperCAmelCase     							=   MobileViTVaForSemanticSegmentation.from_pretrained("shehan97/mobilevitv2-1.0-voc-deeplabv3"    )
      _UpperCAmelCase     							=   model.to(snake_case_    )

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

      _UpperCAmelCase     							=   prepare_img()
      _UpperCAmelCase     							=   image_processor(images=snake_case_	,			return_tensors="pt"    ).to(snake_case_    )

      # forward pass
      with torch.no_grad():
           _UpperCAmelCase     							=   model(**snake_case_    )

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

      _UpperCAmelCase     							=   image_processor.post_process_semantic_segmentation(outputs=snake_case_	,			target_sizes=[(5_0, 6_0)]    )
      _UpperCAmelCase     							=   torch.Size((5_0, 6_0)    )
      self.assertEqual(segmentation[0].shape	,			snake_case_    )

      _UpperCAmelCase     							=   image_processor.post_process_semantic_segmentation(outputs=snake_case_    )
      _UpperCAmelCase     							=   torch.Size((3_2, 3_2)    )
      self.assertEqual(segmentation[0].shape	,			snake_case_    )











'''simple docstring'''




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(__lowercase			,     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 	UpperCAmelCase_							(					__lowercase							:      int			,     __lowercase							:      Dict       )		->       List[Any]:


     '''simple docstring'''



     _UpperCAmelCase     							=   _distribute_shards(**__lowercase       )
     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 	UpperCAmelCase_							(					__lowercase							:      Dict			,     __lowercase							:      Optional[Any]			,     __lowercase							:      int       )		->       str:


     '''simple docstring'''



     _UpperCAmelCase     							=   _split_gen_kwargs(__lowercase			,     __lowercase       )
     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 	UpperCAmelCase_							(					__lowercase							:      Optional[Any]			,     __lowercase							:      List[Any]       )		->       List[Any]:


     '''simple docstring'''



     if expected is RuntimeError:
          with pytest.raises(__lowercase       ):
               _number_of_shards_in_gen_kwargs(__lowercase       )
     else:
          _UpperCAmelCase     							=   _number_of_shards_in_gen_kwargs(__lowercase       )
          assert out == expected




