






'''simple docstring'''
import inspect
import unittest

from transformers import ConvNextConfig
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_backbone_common import BackboneTesterMixin
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 ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
  from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor






class    __SCREAMING_SNAKE_CASE					:




 def __init__(					self		:							str    ,					__lowercase		:							Union[str, Any]    ,					__lowercase		:							Optional[int]=13    ,					__lowercase		:							List[str]=32    ,					__lowercase		:							List[Any]=3    ,					__lowercase		:							Dict=4    ,					__lowercase		:							Union[str, Any]=[10, 20, 30, 40]    ,					__lowercase		:							Union[str, Any]=[2, 2, 3, 2]    ,					__lowercase		:							List[Any]=True    ,					__lowercase		:							Optional[int]=True    ,					__lowercase		:							str=37    ,					__lowercase		:							Optional[int]="gelu"    ,					__lowercase		:							Union[str, Any]=10    ,					__lowercase		:							List[Any]=0.02    ,					__lowercase		:							Any=["stage2", "stage3", "stage4"]    ,					__lowercase		:							int=[2, 3, 4]    ,					__lowercase		:							int=None    ,					)		->  Union[str, Any]:
       SCREAMING_SNAKE_CASE__   : str													=parent
       SCREAMING_SNAKE_CASE__   : List[str]													=batch_size
       SCREAMING_SNAKE_CASE__   : Optional[int]													=image_size
       SCREAMING_SNAKE_CASE__   : str													=num_channels
       SCREAMING_SNAKE_CASE__   : Dict													=num_stages
       SCREAMING_SNAKE_CASE__   : List[str]													=hidden_sizes
       SCREAMING_SNAKE_CASE__   : Tuple													=depths
       SCREAMING_SNAKE_CASE__   : str													=is_training
       SCREAMING_SNAKE_CASE__   : Tuple													=use_labels
       SCREAMING_SNAKE_CASE__   : Optional[int]													=intermediate_size
       SCREAMING_SNAKE_CASE__   : Dict													=hidden_act
       SCREAMING_SNAKE_CASE__   : Any													=num_labels
       SCREAMING_SNAKE_CASE__   : Optional[int]													=initializer_range
       SCREAMING_SNAKE_CASE__   : List[str]													=out_features
       SCREAMING_SNAKE_CASE__   : str													=out_indices
       SCREAMING_SNAKE_CASE__   : Optional[int]													=scope




 def    __magic_name__		(					self		:							List[Any]					)		->  Union[str, Any]:
       SCREAMING_SNAKE_CASE__   : List[str]													=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

       SCREAMING_SNAKE_CASE__   : Optional[int]													=None
       if self.use_labels:
             SCREAMING_SNAKE_CASE__   : Union[str, Any]													=ids_tensor([self.batch_size]    ,					self.num_labels					)

       SCREAMING_SNAKE_CASE__   : List[str]													=self.get_config()
       return config, pixel_values, labels




 def    __magic_name__		(					self		:							str					)		->  Any:
       return ConvNextConfig(
           num_channels=self.num_channels    ,					hidden_sizes=self.hidden_sizes    ,					depths=self.depths    ,					num_stages=self.num_stages    ,					hidden_act=self.hidden_act    ,					is_decoder=_UpperCAmelCase    ,					initializer_range=self.initializer_range    ,					out_features=self.out_features    ,					out_indices=self.out_indices    ,					num_labels=self.num_labels    ,					)




 def    __magic_name__		(					self		:							str    ,					__lowercase		:							Union[str, Any]    ,					__lowercase		:							str    ,					__lowercase		:							int					)		->  Dict:
       SCREAMING_SNAKE_CASE__   : Dict													=ConvNextModel(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       SCREAMING_SNAKE_CASE__   : Tuple													=model(_UpperCAmelCase					)
       # expected last hidden states: B, C, H // 32, W // 32
       self.parent.assertEqual(
           result.last_hidden_state.shape    ,					(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,					)




 def    __magic_name__		(					self		:							Union[str, Any]    ,					__lowercase		:							Union[str, Any]    ,					__lowercase		:							Optional[int]    ,					__lowercase		:							Dict					)		->  List[str]:
       SCREAMING_SNAKE_CASE__   : Any													=ConvNextForImageClassification(_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       SCREAMING_SNAKE_CASE__   : str													=model(_UpperCAmelCase    ,					labels=_UpperCAmelCase					)
       self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)					)




 def    __magic_name__		(					self		:							str    ,					__lowercase		:							Optional[Any]    ,					__lowercase		:							Any    ,					__lowercase		:							Tuple					)		->  List[Any]:
       SCREAMING_SNAKE_CASE__   : Any													=ConvNextBackbone(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       SCREAMING_SNAKE_CASE__   : Dict													=model(_UpperCAmelCase					)

       # verify hidden states
       self.parent.assertEqual(len(result.feature_maps					)    ,					len(config.out_features					)					)
       self.parent.assertListEqual(list(result.feature_maps[0].shape					)    ,					[self.batch_size, self.hidden_sizes[1], 4, 4]					)

       # verify channels
       self.parent.assertEqual(len(model.channels					)    ,					len(config.out_features					)					)
       self.parent.assertListEqual(model.channels    ,					config.hidden_sizes[1:]					)

       # verify backbone works with out_features=None
       SCREAMING_SNAKE_CASE__   : List[Any]													=None
       SCREAMING_SNAKE_CASE__   : Any													=ConvNextBackbone(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=model(_UpperCAmelCase					)

       # verify feature maps
       self.parent.assertEqual(len(result.feature_maps					)    ,					1					)
       self.parent.assertListEqual(list(result.feature_maps[0].shape					)    ,					[self.batch_size, self.hidden_sizes[-1], 1, 1]					)

       # verify channels
       self.parent.assertEqual(len(model.channels					)    ,					1					)
       self.parent.assertListEqual(model.channels    ,					[config.hidden_sizes[-1]]					)




 def    __magic_name__		(					self		:							str					)		->  Optional[int]:
       SCREAMING_SNAKE_CASE__   : int													=self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=config_and_inputs
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													={'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_torch
class    __SCREAMING_SNAKE_CASE					(	_UpperCAmelCase		, _UpperCAmelCase		, unittest.TestCase ):
 snake_case_     							=							(
     (
         ConvNextModel,
         ConvNextForImageClassification,
         ConvNextBackbone,
     )
     if is_torch_available()
     else ()
 )
 snake_case_     							=							(
     {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
     if is_torch_available()
     else {}
 )

 snake_case_     							=							True
 snake_case_     							=							False
 snake_case_     							=							False
 snake_case_     							=							False
 snake_case_     							=							False




 def    __magic_name__		(					self		:							Optional[Any]					)		->  List[str]:
       SCREAMING_SNAKE_CASE__   : Any													=ConvNextModelTester(self					)
       SCREAMING_SNAKE_CASE__   : Dict													=ConfigTester(self    ,					config_class=_UpperCAmelCase    ,					has_text_modality=_UpperCAmelCase    ,					hidden_size=37					)




 def    __magic_name__		(					self		:							Dict					)		->  List[Any]:
       self.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()




 def    __magic_name__		(					self		:							Dict					)		->  List[Any]:
       return




 @unittest.skip(reason='''ConvNext does not use inputs_embeds'''					)
 def    __magic_name__		(					self		:							int					)		->  str:
       pass




 @unittest.skip(reason='''ConvNext does not support input and output embeddings'''					)
 def    __magic_name__		(					self		:							Dict					)		->  Optional[Any]:
       pass




 @unittest.skip(reason='''ConvNext does not use feedforward chunking'''					)
 def    __magic_name__		(					self		:							List[Any]					)		->  Optional[int]:
       pass




 def    __magic_name__		(					self		:							Optional[int]					)		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__   : int													=self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE__   : int													=['''pixel_values''']
             self.assertListEqual(arg_names[:1]    ,					_UpperCAmelCase					)




 def    __magic_name__		(					self		:							Optional[int]					)		->  str:
       SCREAMING_SNAKE_CASE__   : str													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase					)




 def    __magic_name__		(					self		:							Optional[int]					)		->  List[str]:
       SCREAMING_SNAKE_CASE__   : List[str]													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_backbone(*_UpperCAmelCase					)




 def    __magic_name__		(					self		:							Dict					)		->  int:
       def check_hidden_states_output(__lowercase		:							Any    ,					__lowercase		:							str    ,					__lowercase		:							Union[str, Any]					):
             SCREAMING_SNAKE_CASE__   : Union[str, Any]													=model_class(_UpperCAmelCase					)
             model.to(_UpperCAmelCase					)
             model.eval()

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

             SCREAMING_SNAKE_CASE__   : str													=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             SCREAMING_SNAKE_CASE__   : List[str]													=self.model_tester.num_stages
             self.assertEqual(len(_UpperCAmelCase					)    ,					expected_num_stages + 1					)

             # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]					)    ,					[self.model_tester.image_size // 4, self.model_tester.image_size // 4]    ,					)

       SCREAMING_SNAKE_CASE__   : Optional[int]													=self.model_tester.prepare_config_and_inputs_for_common()

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

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

             check_hidden_states_output(_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase					)




 def    __magic_name__		(					self		:							Any					)		->  List[Any]:
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase					)




 @slow
 def    __magic_name__		(					self		:							List[Any]					)		->  List[Any]:
       for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__   : Dict													=ConvNextModel.from_pretrained(_UpperCAmelCase					)
             self.assertIsNotNone(_UpperCAmelCase					)





def        _a(     ):




      '''simple docstring'''






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






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




 @cached_property
 def    __magic_name__		(					self		:							Optional[int]					)		->  Optional[Any]:
       return AutoImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''					) if is_vision_available() else None




 @slow
 def    __magic_name__		(					self		:							Optional[Any]					)		->  Tuple:
       SCREAMING_SNAKE_CASE__   : str													=ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''					).to(_UpperCAmelCase					)

       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=self.default_image_processor
       SCREAMING_SNAKE_CASE__   : Any													=prepare_img()
       SCREAMING_SNAKE_CASE__   : Any													=image_processor(images=_UpperCAmelCase    ,					return_tensors='''pt'''					).to(_UpperCAmelCase					)

       # forward pass
       with torch.no_grad():
             SCREAMING_SNAKE_CASE__   : Optional[Any]													=model(**_UpperCAmelCase					)

       # verify the logits
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=torch.Size((1, 10_00)					)
       self.assertEqual(outputs.logits.shape    ,					_UpperCAmelCase					)

       SCREAMING_SNAKE_CASE__   : Tuple													=torch.tensor([-0.0260, -0.4739, 0.1911]					).to(_UpperCAmelCase					)

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






@require_torch
class    __SCREAMING_SNAKE_CASE					(	unittest.TestCase		, _UpperCAmelCase ):
 snake_case_     							=							(ConvNextBackbone,) if is_torch_available() else ()
 snake_case_     							=							ConvNextConfig

 snake_case_     							=							False




 def    __magic_name__		(					self		:							Union[str, Any]					)		->  int:
       SCREAMING_SNAKE_CASE__   : Dict													=ConvNextModelTester(self					)
"""simple docstring"""






from collections.abc import Sequence



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase = False )  ->  float:
   if not arr:
      return 0

   lowercase__:   Any					=      0 if allow_empty_subarrays else float('''-inf''' )
   lowercase__:   Union[str, Any]					=      0.0
   for num in arr:
      lowercase__:   List[str]					=      max(0 if allow_empty_subarrays else num       ,    curr_sum + num )
      lowercase__:   int					=      max(__UpperCAmelCase       ,    __UpperCAmelCase )

   return max_sum


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

       testmod()

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