





'''simple docstring'''

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 					a_     (				snake_case_       ):

    '''simple docstring'''


    def 			snake_case_(		self       )		->   int:
        _SCREAMING_SNAKE_CASE									=			self.config_class(**self.inputs_dict       )
        self.parent.assertTrue(hasattr(A ,		"""tf_padding"""       )       )
        self.parent.assertTrue(hasattr(A ,		"""depth_multiplier"""       )       )




class 					a_     :

    '''simple docstring'''


    def __init__(		self ,		A ,		A=13 ,		A=3 ,		A=32 ,		A=0.25 ,		A=8 ,		A=True ,		A=1024 ,		A=32 ,		A="relu6" ,		A=0.1 ,		A=0.02 ,		A=True ,		A=True ,		A=10 ,		A=None ,		)		->   List[str]:
        _SCREAMING_SNAKE_CASE									=			parent
        _SCREAMING_SNAKE_CASE									=			batch_size
        _SCREAMING_SNAKE_CASE									=			num_channels
        _SCREAMING_SNAKE_CASE									=			image_size
        _SCREAMING_SNAKE_CASE									=			depth_multiplier
        _SCREAMING_SNAKE_CASE									=			min_depth
        _SCREAMING_SNAKE_CASE									=			tf_padding
        _SCREAMING_SNAKE_CASE									=			int(last_hidden_size * depth_multiplier       )
        _SCREAMING_SNAKE_CASE									=			output_stride
        _SCREAMING_SNAKE_CASE									=			hidden_act
        _SCREAMING_SNAKE_CASE									=			classifier_dropout_prob
        _SCREAMING_SNAKE_CASE									=			use_labels
        _SCREAMING_SNAKE_CASE									=			is_training
        _SCREAMING_SNAKE_CASE									=			num_labels
        _SCREAMING_SNAKE_CASE									=			initializer_range
        _SCREAMING_SNAKE_CASE									=			scope


    def 			snake_case_(		self       )		->   str:
        _SCREAMING_SNAKE_CASE									=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

        _SCREAMING_SNAKE_CASE									=			self.get_config()

        return config, pixel_values, labels, pixel_labels


    def 			snake_case_(		self       )		->   Union[str, Any]:
        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 			snake_case_(		self ,		A ,		A ,		A ,		A       )		->   str:
        _SCREAMING_SNAKE_CASE									=			MobileNetVaModel(config=A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			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 			snake_case_(		self ,		A ,		A ,		A ,		A       )		->   Optional[int]:
        _SCREAMING_SNAKE_CASE									=			self.num_labels
        _SCREAMING_SNAKE_CASE									=			MobileNetVaForImageClassification(A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A ,		labels=A       )
        self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_labels)       )





    def 			snake_case_(		self       )		->   str:
        _SCREAMING_SNAKE_CASE									=			self.prepare_config_and_inputs()
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			config_and_inputs
        _SCREAMING_SNAKE_CASE									=			{"""pixel_values""": pixel_values}
        return config, inputs_dict




@require_torch
class 					a_     (				snake_case_      ,    snake_case_      ,    unittest.TestCase       ):

    '''simple docstring'''
    UpperCamelCase     = (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
    UpperCamelCase     = (
        {'''feature-extraction''': MobileNetVaModel, '''image-classification''': MobileNetVaForImageClassification}
        if is_torch_available()
        else {}
    )

    UpperCamelCase     = False
    UpperCamelCase     = False
    UpperCamelCase     = False
    UpperCamelCase     = False


    def 			snake_case_(		self       )		->   List[str]:
        _SCREAMING_SNAKE_CASE									=			MobileNetVaModelTester(self       )
        _SCREAMING_SNAKE_CASE									=			MobileNetVaConfigTester(self ,		config_class=A ,		has_text_modality=A       )


    def 			snake_case_(		self       )		->   Tuple:
        self.config_tester.run_common_tests()


    @unittest.skip(reason="""MobileNetV1 does not use inputs_embeds"""       )
    def 			snake_case_(		self       )		->   Union[str, Any]:
        pass


    @unittest.skip(reason="""MobileNetV1 does not support input and output embeddings"""       )
    def 			snake_case_(		self       )		->   Union[str, Any]:
        pass


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


    def 			snake_case_(		self       )		->   int:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

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

            _SCREAMING_SNAKE_CASE									=			["""pixel_values"""]
            self.assertListEqual(arg_names[:1] ,		A       )


    def 			snake_case_(		self       )		->   Optional[int]:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A       )


    def 			snake_case_(		self       )		->   Optional[Any]:
        def check_hidden_states_output(A ,		A ,		A       ):
            _SCREAMING_SNAKE_CASE									=			model_class(A       )
            model.to(A       )
            model.eval()

            with torch.no_grad():
                _SCREAMING_SNAKE_CASE									=			model(**self._prepare_for_class(A ,		A       )       )

            _SCREAMING_SNAKE_CASE									=			outputs.hidden_states

            _SCREAMING_SNAKE_CASE									=			26
            self.assertEqual(len(A       ) ,		A       )

        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            _SCREAMING_SNAKE_CASE									=			True
            check_hidden_states_output(A ,		A ,		A       )

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

            check_hidden_states_output(A ,		A ,		A       )


    def 			snake_case_(		self       )		->   str:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*A       )





    @slow
    def 			snake_case_(		self       )		->   str:
        for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE									=			MobileNetVaModel.from_pretrained(A       )
            self.assertIsNotNone(A       )





def lowerCamelCase						(     )						->List[str]:
    _SCREAMING_SNAKE_CASE									=			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
    return image






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

    '''simple docstring'''


    @cached_property
    def 			snake_case_(		self       )		->   str:
        return (
            MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v1_1.0_224"""       ) if is_vision_available() else None
        )





    @slow
    def 			snake_case_(		self       )		->   Dict:
        _SCREAMING_SNAKE_CASE									=			MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v1_1.0_224"""       ).to(A       )

        _SCREAMING_SNAKE_CASE									=			self.default_image_processor
        _SCREAMING_SNAKE_CASE									=			prepare_img()
        _SCREAMING_SNAKE_CASE									=			image_processor(images=A ,		return_tensors="""pt"""       ).to(A       )

        # forward pass
        with torch.no_grad():
            _SCREAMING_SNAKE_CASE									=			model(**A       )

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

        _SCREAMING_SNAKE_CASE									=			torch.tensor([-4.1739, -1.1233, 3.1205]       ).to(A       )

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










'''simple docstring'''

from collections.abc import Sequence





def lowerCamelCase						(     __lowerCamelCase :    Sequence[float]					,       __lowerCamelCase :    bool = False    )						->float:
    if not arr:
        return 0

    _SCREAMING_SNAKE_CASE									=			0 if allow_empty_subarrays else float("""-inf"""    )
    _SCREAMING_SNAKE_CASE									=			0.0
    for num in arr:
        _SCREAMING_SNAKE_CASE									=			max(0 if allow_empty_subarrays else num					,       curr_sum + num    )
        _SCREAMING_SNAKE_CASE									=			max(__lowerCamelCase					,       __lowerCamelCase    )

    return max_sum


if __name__ == "__main__":
      from doctest import testmod

      testmod()

      lowercase_					= [-2, 1, -3, 4, -1, 2, 1, -5, 4]
      print(f"""{max_subarray_sum(nums) = }""")



