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						(					_a			):



     def 			a__  (					self       ):
            _A=	self.config_class(**self.inputs_dict       )
            self.parent.assertTrue(hasattr(lowerCAmelCase__				,      'tf_padding'       )       )
            self.parent.assertTrue(hasattr(lowerCAmelCase__				,      'depth_multiplier'       )       )





class      lowerCAmelCase						:



     def __init__(					self				,      lowerCAmelCase__				,      lowerCAmelCase__=13				,      lowerCAmelCase__=3				,      lowerCAmelCase__=32				,      lowerCAmelCase__=0.25				,      lowerCAmelCase__=8				,      lowerCAmelCase__=True				,      lowerCAmelCase__=1024				,      lowerCAmelCase__=32				,      lowerCAmelCase__="relu6"				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.02				,      lowerCAmelCase__=True				,      lowerCAmelCase__=True				,      lowerCAmelCase__=10				,      lowerCAmelCase__=None				,      ):
            _A=	parent
            _A=	batch_size
            _A=	num_channels
            _A=	image_size
            _A=	depth_multiplier
            _A=	min_depth
            _A=	tf_padding
            _A=	int(last_hidden_size * depth_multiplier       )
            _A=	output_stride
            _A=	hidden_act
            _A=	classifier_dropout_prob
            _A=	use_labels
            _A=	is_training
            _A=	num_labels
            _A=	initializer_range
            _A=	scope



     def 			a__  (					self       ):
            _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 			a__  (					self       ):
            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 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	MobileNetVaModel(config=lowerCAmelCase__       )
            model.to(lowerCAmelCase__       )
            model.eval()
            _A=	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 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	self.num_labels
            _A=	MobileNetVaForImageClassification(lowerCAmelCase__       )
            model.to(lowerCAmelCase__       )
            model.eval()
            _A=	model(lowerCAmelCase__				,      labels=lowerCAmelCase__       )
            self.parent.assertEqual(result.logits.shape				,      (self.batch_size, self.num_labels)       )





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





@require_torch
class      lowerCAmelCase						(					_a			,		_a			,		unittest.TestCase			):
     _SCREAMING_SNAKE_CASE     :     Any		=(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
     _SCREAMING_SNAKE_CASE     :     List[Any]		=(
         {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
         if is_torch_available()
         else {}
     )

     _SCREAMING_SNAKE_CASE     :     List[Any]		=False
     _SCREAMING_SNAKE_CASE     :     int		=False
     _SCREAMING_SNAKE_CASE     :     Optional[int]		=False
     _SCREAMING_SNAKE_CASE     :     Optional[int]		=False



     def 			a__  (					self       ):
            _A=	MobileNetVaModelTester(self       )
            _A=	MobileNetVaConfigTester(self				,      config_class=lowerCAmelCase__				,      has_text_modality=lowerCAmelCase__       )



     def 			a__  (					self       ):
            self.config_tester.run_common_tests()



     @unittest.skip(reason='MobileNetV1 does not use inputs_embeds'       )
     def 			a__  (					self       ):
            pass



     @unittest.skip(reason='MobileNetV1 does not support input and output embeddings'       )
     def 			a__  (					self       ):
            pass



     @unittest.skip(reason='MobileNetV1 does not output attentions'       )
     def 			a__  (					self       ):
            pass



     def 			a__  (					self       ):
            _A,			_A=	self.model_tester.prepare_config_and_inputs_for_common()

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

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



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCAmelCase__       )



     def 			a__  (					self       ):
            def check_hidden_states_output(lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
                   _A=	model_class(lowerCAmelCase__       )
                   model.to(lowerCAmelCase__       )
                   model.eval()

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

                   _A=	outputs.hidden_states

                   _A=	26
                   self.assertEqual(len(lowerCAmelCase__       )				,      lowerCAmelCase__       )

            _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(lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       )

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

                   check_hidden_states_output(lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__       )





     @slow
     def 			a__  (					self       ):
            for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A=	MobileNetVaModel.from_pretrained(lowerCAmelCase__       )
                   self.assertIsNotNone(lowerCAmelCase__       )


def 							UpperCamelCase				(    )		->						Optional[Any]:




       '''simple docstring'''






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





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



     @cached_property
     def 			a__  (					self       ):
            return (
                MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v1_1.0_224'       ) if is_vision_available() else None
            )





     @slow
     def 			a__  (					self       ):
            _A=	MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v1_1.0_224'       ).to(lowerCAmelCase__       )

            _A=	self.default_image_processor
            _A=	prepare_img()
            _A=	image_processor(images=lowerCAmelCase__				,      return_tensors='pt'       ).to(lowerCAmelCase__       )

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

            # verify the logits
            _A=	torch.Size((1, 1001)       )
            self.assertEqual(outputs.logits.shape				,      lowerCAmelCase__       )

            _A=	torch.tensor([-4.1739, -1.1233, 3.1205]       ).to(lowerCAmelCase__       )

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





def 							UpperCamelCase				(    )		->						list[list[int]]:




       '''simple docstring'''






       return [list(range(10_00 - i       ,					-10_00 - i       ,					-1   )   ) for i in range(10_00   )]


UpperCAmelCase_								=							generate_large_matrix()
UpperCAmelCase_								=							(
    [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]],
    [[3, 2], [1, 0]],
    [[7, 7, 6]],
    [[7, 7, 6], [-1, -2, -3]],
    grid,
)

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						None:




       '''simple docstring'''






       assert all(row == sorted(lowerCAmelCase_       ,					reverse=lowerCAmelCase_   ) for row in grid   )
       assert all(list(lowerCAmelCase_   ) == sorted(lowerCAmelCase_       ,					reverse=lowerCAmelCase_   ) for col in zip(*lowerCAmelCase_   )   )

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       _A=	0
       _A=	len(lowerCAmelCase_   ) - 1

       # Edge cases such as no values or all numbers are negative.
       if not array or array[0] < 0:
              return 0

       while right + 1 > left:
              _A=	(left + right) // 2
              _A=	array[mid]

              # Num must be negative and the index must be greater than or equal to 0.
              if num < 0 and array[mid - 1] >= 0:
                     return mid

              if num >= 0:
                     _A=	mid + 1
              else:
                     _A=	mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
       return len(lowerCAmelCase_   )

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       _A=	0
       _A=	len(grid[0]   )

       for i in range(len(lowerCAmelCase_   )   ):
              _A=	find_negative_index(grid[i][:bound]   )
              total += bound
       return (len(lowerCAmelCase_   ) * len(grid[0]   )) - total

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       return len([number for row in grid for number in row if number < 0]   )

def 							UpperCamelCase				(    lowerCAmelCase_   )		->						int:




       '''simple docstring'''






       _A=	0
       for row in grid:
              for i, number in enumerate(lowerCAmelCase_   ):
                     if number < 0:
                            total += len(lowerCAmelCase_   ) - i
                            break
       return total

def 							UpperCamelCase				(    )		->						None:




       '''simple docstring'''






       from timeit import timeit

       print('Running benchmarks'   )
       _A=	(
           'from __main__ import count_negatives_binary_search, '
           'count_negatives_brute_force, count_negatives_brute_force_with_break, grid'
       )
       for func in (
           "count_negatives_binary_search",  # took 0.7727 seconds
           "count_negatives_brute_force_with_break",  # took 4.6505 seconds
           "count_negatives_brute_force",  # took 12.8160 seconds
       ):
              _A=	timeit(F"{func}(grid=grid)"       ,					setup=lowerCAmelCase_       ,					number=5_00   )
              print(F"{func}() took {time:0.4f} seconds"   )


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      benchmark()