





'''simple docstring'''

from __future__ import annotations

import inspect
import unittest

from transformers import MobileViTConfig
from transformers.file_utils import is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import numpy as np
  import tensorflow as tf

  from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
  from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import MobileViTImageProcessor






class      UpperCAmelCase			( UpperCamelCase__       ):
 def   UpperCAmelCase_							(       self   :List[str]							)->							Optional[int]:
     A__       =    self.config_class(**self.inputs_dict							)
     self.parent.assertTrue(hasattr(lowercase_		, "hidden_sizes"							)							)
     self.parent.assertTrue(hasattr(lowercase_		, "neck_hidden_sizes"							)							)
     self.parent.assertTrue(hasattr(lowercase_		, "num_attention_heads"							)							)






class      UpperCAmelCase			:
 def __init__(       self   :Optional[int]		, lowercase_   :Union[str, Any]		, lowercase_   :Union[str, Any]=13		, lowercase_   :str=32		, lowercase_   :str=2		, lowercase_   :Dict=3		, lowercase_   :Tuple=6_40		, lowercase_   :Optional[Any]=4		, lowercase_   :List[Any]="silu"		, lowercase_   :Optional[Any]=3		, lowercase_   :Union[str, Any]=32		, lowercase_   :List[str]=0.1		, lowercase_   :Union[str, Any]=0.1		, lowercase_   :Optional[Any]=0.1		, lowercase_   :Any=0.0_2		, lowercase_   :Any=True		, lowercase_   :Union[str, Any]=True		, lowercase_   :List[str]=10		, lowercase_   :int=None		, )->							Optional[Any]:
     A__       =    parent
     A__       =    batch_size
     A__       =    image_size
     A__       =    patch_size
     A__       =    num_channels
     A__       =    last_hidden_size
     A__       =    num_attention_heads
     A__       =    hidden_act
     A__       =    conv_kernel_size
     A__       =    output_stride
     A__       =    hidden_dropout_prob
     A__       =    attention_probs_dropout_prob
     A__       =    classifier_dropout_prob
     A__       =    use_labels
     A__       =    is_training
     A__       =    num_labels
     A__       =    initializer_range
     A__       =    scope
 def   UpperCAmelCase_							(       self   :List[Any]							)->							Dict:
     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							)->							List[str]:
     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   :List[Any]		, lowercase_   :List[Any]		, lowercase_   :Optional[Any]		, lowercase_   :str		, lowercase_   :Optional[int]							)->							Optional[Any]:
     A__       =    TFMobileViTModel(config=lowercase_							)
     A__       =    model(lowercase_		, training=lowercase_							)
     A__       =    A__       =    self.image_size // self.output_stride
     self.parent.assertEqual(
         result.last_hidden_state.shape		, (self.batch_size, self.last_hidden_size, expected_height, expected_width)							)
 def   UpperCAmelCase_							(       self   :Any		, lowercase_   :List[Any]		, lowercase_   :List[Any]		, lowercase_   :int		, lowercase_   :Optional[Any]							)->							int:
     A__       =    self.num_labels
     A__       =    TFMobileViTForImageClassification(lowercase_							)
     A__       =    model(lowercase_		, labels=lowercase_		, training=lowercase_							)
     self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.num_labels)							)
 def   UpperCAmelCase_							(       self   :int		, lowercase_   :Any		, lowercase_   :Any		, lowercase_   :Optional[Any]		, lowercase_   :str							)->							str:
     A__       =    self.num_labels
     A__       =    TFMobileViTForSemanticSegmentation(lowercase_							)
     A__       =    A__       =    self.image_size // self.output_stride

     A__       =    model(lowercase_		, training=lowercase_							)
     self.parent.assertEqual(
         result.logits.shape		, (self.batch_size, self.num_labels, expected_height, expected_width)							)

     A__       =    model(lowercase_		, labels=lowercase_		, training=lowercase_							)
     self.parent.assertEqual(
         result.logits.shape		, (self.batch_size, self.num_labels, expected_height, expected_width)							)



 def   UpperCAmelCase_							(       self   :Optional[Any]							)->							int:
     A__       =    self.prepare_config_and_inputs()
     A__,			A__,			A__,			A__       =    config_and_inputs
     A__       =    {"pixel_values": pixel_values}
     return config, inputs_dict






@require_tf
class      UpperCAmelCase			( UpperCamelCase__    , UpperCamelCase__    , unittest.TestCase       ):
 __lowercase     	=       (
     (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
     if is_tf_available()
     else ()
 )
 __lowercase     	=       (
     {"""feature-extraction""": TFMobileViTModel, """image-classification""": TFMobileViTForImageClassification}
     if is_tf_available()
     else {}
 )

 __lowercase     	=       False
 __lowercase     	=       False
 __lowercase     	=       False
 __lowercase     	=       False
 __lowercase     	=       False
 def   UpperCAmelCase_							(       self   :Optional[Any]							)->							int:
     A__       =    TFMobileViTModelTester(self							)
     A__       =    TFMobileViTConfigTester(self		, config_class=lowercase_		, has_text_modality=lowercase_							)
 def   UpperCAmelCase_							(       self   :Union[str, Any]							)->							Dict:
     self.config_tester.run_common_tests()
 @unittest.skip(reason="MobileViT does not use inputs_embeds"							)
 def   UpperCAmelCase_							(       self   :str							)->							List[str]:
     pass
 @unittest.skip(reason="MobileViT does not support input and output embeddings"							)
 def   UpperCAmelCase_							(       self   :Any							)->							List[Any]:
     pass
 @unittest.skip(reason="MobileViT does not output attentions"							)
 def   UpperCAmelCase_							(       self   :str							)->							Tuple:
     pass
 def   UpperCAmelCase_							(       self   :str							)->							Tuple:
     A__,			A__       =    self.model_tester.prepare_config_and_inputs_for_common()

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

         A__       =    ["pixel_values"]
         self.assertListEqual(arg_names[:1]		, lowercase_							)
 def   UpperCAmelCase_							(       self   :List[Any]							)->							Tuple:
     A__       =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_							)
 def   UpperCAmelCase_							(       self   :Optional[Any]							)->							List[str]:
     def check_hidden_states_output(lowercase_   :Union[str, Any]		, lowercase_   :List[Any]		, lowercase_   :Dict							):
         A__       =    model_class(lowercase_							)

         A__       =    model(**self._prepare_for_class(lowercase_		, lowercase_							)							)

         A__       =    outputs.hidden_states

         A__       =    5
         self.assertEqual(len(lowercase_							)		, lowercase_							)

         # MobileViT'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(lowercase_							)							):
             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(lowercase_		, lowercase_		, lowercase_							)

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

         check_hidden_states_output(lowercase_		, lowercase_		, lowercase_							)
 def   UpperCAmelCase_							(       self   :Union[str, Any]							)->							List[Any]:
     A__       =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowercase_							)
 def   UpperCAmelCase_							(       self   :List[str]							)->							int:
     A__       =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_semantic_segmentation(*lowercase_							)
 @unittest.skipIf(
     not is_tf_available() or len(tf.config.list_physical_devices("GPU"							)							) == 0		, reason="TF does not support backprop for grouped convolutions on CPU."		, )
 def   UpperCAmelCase_							(       self   :Optional[int]							)->							List[Any]:
     super().test_dataset_conversion()
 def   UpperCAmelCase_							(       self   :List[str]		, lowercase_   :Union[str, Any]		, lowercase_   :List[str]		, lowercase_   :Any=2E-1		, lowercase_   :Dict=2E-1							)->							List[Any]:
     self.assertTrue(np.allclose(lowercase_		, lowercase_		, atol=lowercase_		, rtol=lowercase_							)							)
 @unittest.skipIf(
     not is_tf_available() or len(tf.config.list_physical_devices("GPU"							)							) == 0		, reason="TF does not support backprop for grouped convolutions on CPU."		, )
 @slow
 def   UpperCAmelCase_							(       self   :Optional[int]							)->							str:
     A__,			A__       =    self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         # Since `TFMobileViTModel` cannot operate with the default `fit()` method.
         if model_class.__name__ != "TFMobileViTModel":
             A__       =    model_class(lowercase_							)
             if getattr(lowercase_		, "hf_compute_loss"		, lowercase_							):
                 super().test_keras_fit()
 def   UpperCAmelCase_							(       self   :Union[str, Any]							)->							Dict:
     A__,			A__       =    self.model_tester.prepare_config_and_inputs_for_common()
     for model_class in self.all_model_classes:
         # set an ignore index to correctly test the masked loss used in
         # `TFMobileViTForSemanticSegmentation`.
         if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
             A__       =    5

         A__       =    model_class(lowercase_							)
         if getattr(lowercase_		, "hf_compute_loss"		, lowercase_							):
             # The number of elements in the loss should be the same as the number of elements in the label
             A__       =    self._prepare_for_class(inputs_dict.copy()		, lowercase_		, return_labels=lowercase_							)
             A__       =    prepared_for_class[
                 sorted(prepared_for_class.keys() - inputs_dict.keys()		, reverse=lowercase_							)[0]
             ]
             A__       =    added_label.shape.as_list()[:1]

             # Test that model correctly compute the loss with kwargs
             A__       =    self._prepare_for_class(inputs_dict.copy()		, lowercase_		, return_labels=lowercase_							)
             A__       =    {"input_ids", "pixel_values", "input_features"}
             A__       =    possible_input_names.intersection(set(lowercase_							)							).pop()
             A__       =    prepared_for_class.pop(lowercase_							)

             A__       =    model(lowercase_		, **lowercase_							)[0]
             self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)

             # Test that model correctly compute the loss when we mask some positions
             A__       =    self._prepare_for_class(inputs_dict.copy()		, lowercase_		, return_labels=lowercase_							)
             A__       =    {"input_ids", "pixel_values", "input_features"}
             A__       =    possible_input_names.intersection(set(lowercase_							)							).pop()
             A__       =    prepared_for_class.pop(lowercase_							)
             if "labels" in prepared_for_class:
                 A__       =    prepared_for_class["labels"].numpy()
                 if len(labels.shape							) > 1 and labels.shape[1] != 1:
                     # labels[0] = -100
                     A__       =    tf.convert_to_tensor(lowercase_							)
                     A__       =    model(lowercase_		, **lowercase_							)[0]
                     self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)
                     self.assertTrue(not np.any(np.isnan(loss.numpy()							)							)							)

                # Test that model correctly compute the loss with a dict
             A__       =    self._prepare_for_class(inputs_dict.copy()		, lowercase_		, return_labels=lowercase_							)
             A__       =    model(lowercase_							)[0]
             self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)

             # Test that model correctly compute the loss with a tuple
             A__       =    self._prepare_for_class(inputs_dict.copy()		, lowercase_		, return_labels=lowercase_							)

             # Get keys that were added with the _prepare_for_class function
             A__       =    prepared_for_class.keys() - inputs_dict.keys()
             A__       =    inspect.signature(model.call							).parameters
             A__       =    list(signature.keys()							)

             # Create a dictionary holding the location of the tensors in the tuple
             A__       =    {0: input_name}
             for label_key in label_keys:
                 A__       =    signature_names.index(lowercase_							)
                 A__       =    label_key
             A__       =    sorted(tuple_index_mapping.items()							)
             # Initialize a list with their default values, update the values and convert to a tuple
             A__       =    []

             for name in signature_names:
                 if name != "kwargs":
                     list_input.append(signature[name].default							)

             for index, value in sorted_tuple_index_mapping:
                 A__       =    prepared_for_class[value]

             A__       =    tuple(lowercase_							)

             # Send to model
             A__       =    model(tuple_input[:-1]							)[0]

             self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)



 @slow
 def   UpperCAmelCase_							(       self   :List[Any]							)->							str:
     for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         A__       =    TFMobileViTModel.from_pretrained(lowercase_							)
         self.assertIsNotNone(lowercase_							)





def 		UpperCamelCase       (							):
    A__       =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
    return image






@require_tf
class      UpperCAmelCase			( unittest.TestCase       ):
 @slow
 def   UpperCAmelCase_							(       self   :Tuple							)->							str:
     A__       =    TFMobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"							)

     A__       =    MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"							)
     A__       =    prepare_img()
     A__       =    image_processor(images=lowercase_		, return_tensors="tf"							)

     # forward pass
     A__       =    model(**lowercase_		, training=lowercase_							)

     # verify the logits
     A__       =    tf.TensorShape((1, 10_00)							)
     self.assertEqual(outputs.logits.shape		, lowercase_							)

     A__       =    tf.constant([-1.9_3_6_4, -1.2_3_2_7, -0.4_6_5_3]							)

     tf.debugging.assert_near(outputs.logits[0, :3]		, lowercase_		, atol=1E-4		, rtol=1E-04							)



 @slow
 def   UpperCAmelCase_							(       self   :Optional[int]							)->							Optional[Any]:
     # `from_pt` will be removed
     A__       =    TFMobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"							)

     A__       =    MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"							)

     A__       =    prepare_img()
     A__       =    image_processor(images=lowercase_		, return_tensors="tf"							)

     # forward pass
     A__       =    model(inputs.pixel_values		, training=lowercase_							)
     A__       =    outputs.logits

     # verify the logits
     A__       =    tf.TensorShape((1, 21, 32, 32)							)
     self.assertEqual(logits.shape		, lowercase_							)

     A__       =    tf.constant(
         [
             [[6.9_7_1_3, 6.9_7_8_6, 7.2_4_2_2], [7.2_8_9_3, 7.2_8_2_5, 7.4_4_4_6], [7.6_5_8_0, 7.8_7_9_7, 7.9_4_2_0]],
             [[-1_0.6_8_6_9, -1_0.3_2_5_0, -1_0.3_4_7_1], [-1_0.4_2_2_8, -9.9_8_6_8, -9.7_1_3_2], [-1_1.0_4_0_5, -1_1.0_2_2_1, -1_0.7_3_1_8]],
             [[-3.3_0_8_9, -2.8_5_3_9, -2.6_7_4_0], [-3.2_7_0_6, -2.5_6_2_1, -2.5_1_0_8], [-3.2_5_3_4, -2.6_6_1_5, -2.6_6_5_1]],
         ]							)

     tf.debugging.assert_near(logits[0, :3, :3, :3]		, lowercase_		, rtol=1E-4		, atol=1E-4							)










'''simple docstring'''

import bisect
import copy
from collections import defaultdict

import numpy as np
from torch.utils.data import BatchSampler, Sampler

from utils import logger





def 		UpperCamelCase       (							_lowerCamelCase      :      str		, _lowerCamelCase      :      Optional[Any] ):
    A__       =    copy.deepcopy(_lowerCamelCase )
    A__       =    sorted(_lowerCamelCase )
    A__       =    [bisect.bisect_right(_lowerCamelCase		, _lowerCamelCase ) for y in x]
    return quantized





def 		UpperCamelCase       (							_lowerCamelCase      :      Optional[int]		, _lowerCamelCase      :      Tuple=0 ):
    A__       =    np.arange(start=3		, stop=_lowerCamelCase		, step=4 ).tolist() if k > 0 else [10]
    A__       =    _quantize(_lowerCamelCase		, _lowerCamelCase )
    # count number of elements per group
    A__       =    np.unique(_lowerCamelCase		, return_counts=_lowerCamelCase )[1]
    A__       =    [0] + bins + [np.inf]
    logger.info("Using {} as bins for aspect lengths quantization".format(_lowerCamelCase ) )
    logger.info("Count of instances per bin: {}".format(_lowerCamelCase ) )
    return groups






class      UpperCAmelCase			( UpperCamelCase__       ):
 def __init__(       self   :Optional[Any]		, lowercase_   :List[Any]		, lowercase_   :Optional[int]		, lowercase_   :str							)->							Dict:
     if not isinstance(lowercase_		, lowercase_							):
         raise ValueError(
             "sampler should be an instance of torch.utils.data.Sampler, but got sampler={}".format(lowercase_							)							)
     A__       =    sampler
     A__       =    group_ids
     A__       =    batch_size
 def __iter__(       self   :int							)->							Tuple:
     A__       =    defaultdict(lowercase_							)
     A__       =    defaultdict(lowercase_							)

     A__       =    0
     for idx in self.sampler:
         A__       =    self.group_ids[idx]
         buffer_per_group[group_id].append(lowercase_							)
         samples_per_group[group_id].append(lowercase_							)
         if len(buffer_per_group[group_id]							) == self.batch_size:
             yield buffer_per_group[group_id]  # TODO
             num_batches += 1
             del buffer_per_group[group_id]
         assert len(buffer_per_group[group_id]							) < self.batch_size

     # now we have run out of elements that satisfy
     # the group criteria, let's return the remaining
     # elements so that the size of the sampler is
     # deterministic
     A__       =    len(self							)
     A__       =    expected_num_batches - num_batches
     if num_remaining > 0:
         # for the remaining batches, group the batches by similar lengths
         A__       =    []
         for group_id, idxs in sorted(buffer_per_group.items()		, key=lambda lowercase_							: x[0]							):
             batch_idx.extend(lowercase_							)
             if len(lowercase_							) >= self.batch_size:
                 yield batch_idx[: self.batch_size]
                 A__       =    batch_idx[self.batch_size :]
                 num_remaining -= 1
         if len(lowercase_							) > 0:
             yield batch_idx
             num_remaining -= 1
     assert num_remaining == 0



 def __len__(       self   :int							)->							Tuple:
     return (len(self.sampler							) + self.batch_size - 1) // self.batch_size



