

"""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 			__a					:





       """simple docstring"""


       def __init__(   self			,					snake_case			,					snake_case=13			,					snake_case=32			,					snake_case=3			,					snake_case=4			,					snake_case=[10, 20, 30, 40]			,					snake_case=[2, 2, 3, 2]			,					snake_case=True			,					snake_case=True			,					snake_case=37			,					snake_case="gelu"			,					snake_case=10			,					snake_case=0.02			,					snake_case=["stage2", "stage3", "stage4"]			,					snake_case=[2, 3, 4]			,					snake_case=None			,					):





        """simple docstring"""

        lowerCAmelCase__   :				Dict 			=				parent
        lowerCAmelCase__   :				Any 			=				batch_size
        lowerCAmelCase__   :				Union[str, Any] 			=				image_size
        lowerCAmelCase__   :				Union[str, Any] 			=				num_channels
        lowerCAmelCase__   :				Union[str, Any] 			=				num_stages
        lowerCAmelCase__   :				str 			=				hidden_sizes
        lowerCAmelCase__   :				Union[str, Any] 			=				depths
        lowerCAmelCase__   :				Union[str, Any] 			=				is_training
        lowerCAmelCase__   :				List[str] 			=				use_labels
        lowerCAmelCase__   :				Optional[Any] 			=				intermediate_size
        lowerCAmelCase__   :				Union[str, Any] 			=				hidden_act
        lowerCAmelCase__   :				Dict 			=				num_labels
        lowerCAmelCase__   :				Optional[Any] 			=				initializer_range
        lowerCAmelCase__   :				Tuple 			=				out_features
        lowerCAmelCase__   :				Any 			=				out_indices
        lowerCAmelCase__   :				List[Any] 			=				scope






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

        lowerCAmelCase__   :				Union[str, Any] 			=				None
        if self.use_labels:
         lowerCAmelCase__   :				List[Any] 			=				ids_tensor([self.batch_size]			,					self.num_labels   )

        lowerCAmelCase__   :				Optional[int] 			=				self.get_config()
        return config, pixel_values, labels






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        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=snake_case			,					initializer_range=self.initializer_range			,					out_features=self.out_features			,					out_indices=self.out_indices			,					num_labels=self.num_labels			,					)






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[Any] 			=				ConvNextModel(config=snake_case   )
        model.to(snake_case   )
        model.eval()
        lowerCAmelCase__   :				Dict 			=				model(snake_case   )
        # 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 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				ConvNextForImageClassification(snake_case   )
        model.to(snake_case   )
        model.eval()
        lowerCAmelCase__   :				int 			=				model(snake_case			,					labels=snake_case   )
        self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_labels)   )






       def 			SCREAMING_SNAKE_CASE_	(   self			,					snake_case			,					snake_case			,					snake_case   ):





        """simple docstring"""

        lowerCAmelCase__   :				str 			=				ConvNextBackbone(config=snake_case   )
        model.to(snake_case   )
        model.eval()
        lowerCAmelCase__   :				str 			=				model(snake_case   )

        # 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
        lowerCAmelCase__   :				int 			=				None
        lowerCAmelCase__   :				List[Any] 			=				ConvNextBackbone(config=snake_case   )
        model.to(snake_case   )
        model.eval()
        lowerCAmelCase__   :				str 			=				model(snake_case   )

        # 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 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				self.prepare_config_and_inputs()
        lowerCAmelCase__   :				Union[str, Any] 			=				config_and_inputs
        lowerCAmelCase__   :				Any 			=				{"pixel_values": pixel_values}
        return config, inputs_dict






@require_torch
class 			__a					(						lowerCamelCase__       ,	lowerCamelCase__       ,	unittest.TestCase       ):





       """simple docstring"""


       __UpperCamelCase					:      str    =						(
           (
               ConvNextModel,
               ConvNextForImageClassification,
               ConvNextBackbone,
           )
           if is_torch_available()
           else ()
       )
       __UpperCamelCase					:      Any    =						(
           {'feature-extraction': ConvNextModel, 'image-classification': ConvNextForImageClassification}
           if is_torch_available()
           else {}
       )

       __UpperCamelCase					:      List[str]    =						True
       __UpperCamelCase					:      Tuple    =						False
       __UpperCamelCase					:      str    =						False
       __UpperCamelCase					:      List[str]    =						False
       __UpperCamelCase					:      Union[str, Any]    =						False






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				ConvNextModelTester(self   )
        lowerCAmelCase__   :				List[Any] 			=				ConfigTester(self			,					config_class=snake_case			,					has_text_modality=snake_case			,					hidden_size=37   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        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 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        return






       @unittest.skip(reason="ConvNext does not use inputs_embeds"   )
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        pass






       @unittest.skip(reason="ConvNext does not support input and output embeddings"   )
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        pass






       @unittest.skip(reason="ConvNext does not use feedforward chunking"   )
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        pass






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				List[str] 			=				self.model_tester.prepare_config_and_inputs_for_common()

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

         lowerCAmelCase__   :				Dict 			=				["pixel_values"]
         self.assertListEqual(arg_names[:1]			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Optional[int] 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        def check_hidden_states_output(snake_case			,					snake_case			,					snake_case   ):
         lowerCAmelCase__   :				Tuple 			=				model_class(snake_case   )
         model.to(snake_case   )
         model.eval()

         with torch.no_grad():
          lowerCAmelCase__   :				Optional[Any] 			=				model(**self._prepare_for_class(snake_case			,					snake_case   )   )

         lowerCAmelCase__   :				Any 			=				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         lowerCAmelCase__   :				Dict 			=				self.model_tester.num_stages
         self.assertEqual(len(snake_case   )			,					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]			,					)

        lowerCAmelCase__   :				int 			=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         lowerCAmelCase__   :				int 			=				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"]
         lowerCAmelCase__   :				Any 			=				True

         check_hidden_states_output(snake_case			,					snake_case			,					snake_case   )






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				int 			=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*snake_case   )






       @slow
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowerCAmelCase__   :				Any 			=				ConvNextModel.from_pretrained(snake_case   )
         self.assertIsNotNone(snake_case   )




def       SCREAMING_SNAKE_CASE							( )			->						Union[str, Any]:
 lowerCAmelCase__   :				Dict 			=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
 return image






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





       """simple docstring"""


       @cached_property
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        return AutoImageProcessor.from_pretrained("facebook/convnext-tiny-224"   ) if is_vision_available() else None






       @slow
       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				ConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"   ).to(snake_case   )

        lowerCAmelCase__   :				Optional[Any] 			=				self.default_image_processor
        lowerCAmelCase__   :				List[str] 			=				prepare_img()
        lowerCAmelCase__   :				Optional[Any] 			=				image_processor(images=snake_case			,					return_tensors="pt"   ).to(snake_case   )

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

        # verify the logits
        lowerCAmelCase__   :				List[Any] 			=				torch.Size((1, 1_000)   )
        self.assertEqual(outputs.logits.shape			,					snake_case   )

        lowerCAmelCase__   :				List[str] 			=				torch.tensor([-0.0_260, -0.4_739, 0.1_911]   ).to(snake_case   )

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






@require_torch
class 			__a					(						unittest.TestCase       ,	lowerCamelCase__       ):





       """simple docstring"""


       __UpperCamelCase					:      Dict    =						(ConvNextBackbone,) if is_torch_available() else ()
       __UpperCamelCase					:      Tuple    =						ConvNextConfig

       __UpperCamelCase					:      Union[str, Any]    =						False






       def 			SCREAMING_SNAKE_CASE_	(   self   ):





        """simple docstring"""

        lowerCAmelCase__   :				Union[str, Any] 			=				ConvNextModelTester(self   )













from __future__ import annotations

def   __snake_case    (       _lowerCAmelCase			:			list[int]					,					_lowerCAmelCase			:			int						)			->    list[list[int]]:
   A_       :					list[list[int]]        =      []
   A_       :					list[int]        =      []
   A_       :					Dict        =      0
   A_       :					str        =      sum(_lowerCAmelCase						)
   create_state_space_tree(_lowerCAmelCase					,					_lowerCAmelCase					,					_lowerCAmelCase					,					_lowerCAmelCase					,					_lowerCAmelCase					,					_lowerCAmelCase						)
   return result

def   __snake_case    (       _lowerCAmelCase			:			list[int]					,					_lowerCAmelCase			:			int					,					_lowerCAmelCase			:			int					,					_lowerCAmelCase			:			list[int]					,					_lowerCAmelCase			:			list[list[int]]					,					_lowerCAmelCase			:			int					,					)			->    None:
   if sum(_lowerCAmelCase						) > max_sum or (remaining_nums_sum + sum(_lowerCAmelCase						)) < max_sum:
      return
   if sum(_lowerCAmelCase						) == max_sum:
      result.append(_lowerCAmelCase						)
      return
   for index in range(_lowerCAmelCase					,					len(_lowerCAmelCase						)						):
      create_state_space_tree(
          _lowerCAmelCase					,					_lowerCAmelCase					,					index + 1					,					[*path, nums[index]]					,					_lowerCAmelCase					,					remaining_nums_sum - nums[index]					,					)
_lowerCAmelCase				:   List[Any]						       =						[3, 34, 4, 12, 5, 2]
_lowerCAmelCase				:   Dict						       =						9
_lowerCAmelCase				:   Dict						       =						generate_sum_of_subsets_soln(nums, max_sum)





print(*result)
