


'''simple docstring'''







import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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 ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
   from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor







class A__       :






      def __init__( self			:Tuple     ,						SCREAMING_SNAKE_CASE			:str     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]=1_3     ,						SCREAMING_SNAKE_CASE			:List[Any]=3_2     ,						SCREAMING_SNAKE_CASE			:List[Any]=3     ,						SCREAMING_SNAKE_CASE			:Optional[int]=4     ,						SCREAMING_SNAKE_CASE			:Any=[1_0, 2_0, 3_0, 4_0]     ,						SCREAMING_SNAKE_CASE			:str=[2, 2, 3, 2]     ,						SCREAMING_SNAKE_CASE			:List[str]=True     ,						SCREAMING_SNAKE_CASE			:Optional[Any]=True     ,						SCREAMING_SNAKE_CASE			:str=3_7     ,						SCREAMING_SNAKE_CASE			:List[Any]="gelu"     ,						SCREAMING_SNAKE_CASE			:List[Any]=1_0     ,						SCREAMING_SNAKE_CASE			:List[str]=0.02     ,						SCREAMING_SNAKE_CASE			:List[str]=["stage2", "stage3", "stage4"]     ,						SCREAMING_SNAKE_CASE			:Optional[Any]=[2, 3, 4]     ,						SCREAMING_SNAKE_CASE			:Tuple=None     ,						)       ->					str:




             '''simple docstring'''
             _a    : Any     					=parent
             _a    : Union[str, Any]     					=batch_size
             _a    : int     					=image_size
             _a    : Optional[Any]     					=num_channels
             _a    : Tuple     					=num_stages
             _a    : List[Any]     					=hidden_sizes
             _a    : Union[str, Any]     					=depths
             _a    : List[str]     					=is_training
             _a    : str     					=use_labels
             _a    : Optional[Any]     					=intermediate_size
             _a    : Optional[Any]     					=hidden_act
             _a    : Any     					=num_labels
             _a    : str     					=initializer_range
             _a    : Optional[Any]     					=out_features
             _a    : Any     					=out_indices
             _a    : List[Any]     					=scope






      def __UpperCAmelCase       ( self			:Any	)       ->					List[str]:




             '''simple docstring'''
             _a    : List[str]     					=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

             _a    : Any     					=None
             if self.use_labels:
                    _a    : Optional[Any]     					=ids_tensor([self.batch_size]     ,						self.num_labels	)

             _a    : Optional[int]     					=self.get_config()

             return config, pixel_values, labels






      def __UpperCAmelCase       ( self			:Optional[int]	)       ->					Optional[Any]:




             '''simple docstring'''
             return ConvNextVaConfig(
                 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=SCREAMING_SNAKE_CASE     ,						initializer_range=self.initializer_range     ,						out_features=self.out_features     ,						out_indices=self.out_indices     ,						num_labels=self.num_labels     ,						)






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:str	)       ->					List[Any]:




             '''simple docstring'''
             _a    : Tuple     					=ConvNextVaModel(config=SCREAMING_SNAKE_CASE	)
             model.to(SCREAMING_SNAKE_CASE	)
             model.eval()
             _a    : List[str]     					=model(SCREAMING_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 // 3_2, self.image_size // 3_2)     ,						)






      def __UpperCAmelCase       ( self			:str     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:Dict     ,						SCREAMING_SNAKE_CASE			:int	)       ->					List[str]:




             '''simple docstring'''
             _a    : int     					=ConvNextVaForImageClassification(SCREAMING_SNAKE_CASE	)
             model.to(SCREAMING_SNAKE_CASE	)
             model.eval()
             _a    : Optional[Any]     					=model(SCREAMING_SNAKE_CASE     ,						labels=SCREAMING_SNAKE_CASE	)
             self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_labels)	)






      def __UpperCAmelCase       ( self			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:int     ,						SCREAMING_SNAKE_CASE			:Optional[Any]     ,						SCREAMING_SNAKE_CASE			:str	)       ->					Any:




             '''simple docstring'''
             _a    : Union[str, Any]     					=ConvNextVaBackbone(config=SCREAMING_SNAKE_CASE	)
             model.to(SCREAMING_SNAKE_CASE	)
             model.eval()
             _a    : Tuple     					=model(SCREAMING_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
             _a    : Union[str, Any]     					=None
             _a    : Optional[Any]     					=ConvNextVaBackbone(config=SCREAMING_SNAKE_CASE	)
             model.to(SCREAMING_SNAKE_CASE	)
             model.eval()
             _a    : Optional[int]     					=model(SCREAMING_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 __UpperCAmelCase       ( self			:Tuple	)       ->					Dict:




             '''simple docstring'''
             _a    : Dict     					=self.prepare_config_and_inputs()
             _a					,    _a					,    _a    : Any     					=config_and_inputs
             _a    : Union[str, Any]     					={"""pixel_values""": pixel_values}
             return config, inputs_dict






      def __UpperCAmelCase       ( self			:Optional[int]	)       ->					int:




             '''simple docstring'''
             _a    : Tuple     					=self.prepare_config_and_inputs()
             _a					,    _a					,    _a    : List[Any]     					=config_and_inputs
             _a    : Tuple     					={"""pixel_values""": pixel_values, """labels""": labels}
             return config, inputs_dict







@require_torch
class A__       (						UpperCAmelCase__ ,     UpperCAmelCase__ ,     unittest.TestCase      ):
      __UpperCamelCase       :  Optional[int]	       =							(
          (
              ConvNextVaModel,
              ConvNextVaForImageClassification,
              ConvNextVaBackbone,
          )
          if is_torch_available()
          else ()
      )
      __UpperCamelCase       :  Tuple	       =							(
          {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
          if is_torch_available()
          else {}
      )

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






      def __UpperCAmelCase       ( self			:Optional[Any]	)       ->					str:




             '''simple docstring'''
             _a    : Optional[int]     					=ConvNextVaModelTester(self	)
             _a    : List[str]     					=ConfigTester(self     ,						config_class=SCREAMING_SNAKE_CASE     ,						has_text_modality=SCREAMING_SNAKE_CASE     ,						hidden_size=3_7	)






      def __UpperCAmelCase       ( self			:List[str]	)       ->					Tuple:




             '''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 __UpperCAmelCase       ( self			:List[str]	)       ->					Any:




             '''simple docstring'''
             return






      @unittest.skip(reason="""ConvNextV2 does not use inputs_embeds"""	)
      def __UpperCAmelCase       ( self			:str	)       ->					Optional[int]:




             '''simple docstring'''
             pass






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




             '''simple docstring'''
             pass






      @unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""	)
      def __UpperCAmelCase       ( self			:Any	)       ->					List[Any]:




             '''simple docstring'''
             pass






      def __UpperCAmelCase       ( self			:List[Any]	)       ->					List[str]:




             '''simple docstring'''
             if not self.model_tester.is_training:
                    return

             for model_class in self.all_model_classes:
                    _a					,    _a    : Any     					=self.model_tester.prepare_config_and_inputs_with_labels()
                    _a    : Dict     					=True

                    if model_class.__name__ in [
                        *get_values(SCREAMING_SNAKE_CASE	),
                        *get_values(SCREAMING_SNAKE_CASE	),
                    ]:
                           continue

                    _a    : Union[str, Any]     					=model_class(SCREAMING_SNAKE_CASE	)
                    model.to(SCREAMING_SNAKE_CASE	)
                    model.train()
                    _a    : Optional[Any]     					=self._prepare_for_class(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						return_labels=SCREAMING_SNAKE_CASE	)
                    _a    : List[Any]     					=model(**SCREAMING_SNAKE_CASE	).loss
                    loss.backward()






      def __UpperCAmelCase       ( self			:List[Any]	)       ->					Optional[int]:




             '''simple docstring'''
             if not self.model_tester.is_training:
                    return

             for model_class in self.all_model_classes:
                    _a					,    _a    : Any     					=self.model_tester.prepare_config_and_inputs_with_labels()
                    _a    : List[str]     					=False
                    _a    : Any     					=True

                    if (
                        model_class.__name__
                        in [*get_values(SCREAMING_SNAKE_CASE	), *get_values(SCREAMING_SNAKE_CASE	)]
                        or not model_class.supports_gradient_checkpointing
                    ):
                           continue

                    _a    : List[str]     					=model_class(SCREAMING_SNAKE_CASE	)
                    model.to(SCREAMING_SNAKE_CASE	)
                    model.gradient_checkpointing_enable()
                    model.train()
                    _a    : Union[str, Any]     					=self._prepare_for_class(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						return_labels=SCREAMING_SNAKE_CASE	)
                    _a    : Any     					=model(**SCREAMING_SNAKE_CASE	).loss
                    loss.backward()






      def __UpperCAmelCase       ( self			:List[str]	)       ->					Optional[int]:




             '''simple docstring'''
             _a					,    _a    : Union[str, Any]     					=self.model_tester.prepare_config_and_inputs_for_common()

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

                    _a    : List[str]     					=["""pixel_values"""]
                    self.assertListEqual(arg_names[:1]     ,						SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Union[str, Any]	)       ->					Any:




             '''simple docstring'''
             _a    : List[str]     					=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Dict	)       ->					Union[str, Any]:




             '''simple docstring'''
             def check_hidden_states_output(SCREAMING_SNAKE_CASE			:Union[str, Any]     ,						SCREAMING_SNAKE_CASE			:List[Any]     ,						SCREAMING_SNAKE_CASE			:Tuple	):
                    _a    : str     					=model_class(SCREAMING_SNAKE_CASE	)
                    model.to(SCREAMING_SNAKE_CASE	)
                    model.eval()

                    with torch.no_grad():
                           _a    : Tuple     					=model(**self._prepare_for_class(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)	)

                    _a    : Optional[Any]     					=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                    _a    : str     					=self.model_tester.num_stages
                    self.assertEqual(len(SCREAMING_SNAKE_CASE	)     ,						expected_num_stages + 1	)

                    # ConvNextV2'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]     ,						)

             _a					,    _a    : Tuple     					=self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _a    : List[Any]     					=True
                    check_hidden_states_output(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)

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

                    check_hidden_states_output(SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE     ,						SCREAMING_SNAKE_CASE	)






      def __UpperCAmelCase       ( self			:Any	)       ->					List[str]:




             '''simple docstring'''
             _a    : str     					=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE	)






      @slow
      def __UpperCAmelCase       ( self			:Optional[Any]	)       ->					Any:




             '''simple docstring'''
             for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _a    : Dict     					=ConvNextVaModel.from_pretrained(SCREAMING_SNAKE_CASE	)
                    self.assertIsNotNone(SCREAMING_SNAKE_CASE	)


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







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






      @cached_property
      def __UpperCAmelCase       ( self			:List[Any]	)       ->					Dict:




             '''simple docstring'''
             return AutoImageProcessor.from_pretrained("""facebook/convnextv2-tiny-1k-224"""	) if is_vision_available() else None






      @slow
      def __UpperCAmelCase       ( self			:int	)       ->					Union[str, Any]:




             '''simple docstring'''
             _a    : Any     					=ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""	).to(SCREAMING_SNAKE_CASE	)

             _a    : Tuple     					=self.default_image_processor
             _a    : Optional[int]     					=prepare_img()
             _a    : List[Any]     					=preprocessor(images=SCREAMING_SNAKE_CASE     ,						return_tensors="""pt"""	).to(SCREAMING_SNAKE_CASE	)

             # forward pass
             with torch.no_grad():
                    _a    : List[Any]     					=model(**SCREAMING_SNAKE_CASE	)

             # verify the logits
             _a    : Any     					=torch.Size((1, 1_0_0_0)	)
             self.assertEqual(outputs.logits.shape     ,						SCREAMING_SNAKE_CASE	)

             _a    : Optional[Any]     					=torch.tensor([0.9_996, 0.1_966, -0.4_386]	).to(SCREAMING_SNAKE_CASE	)
             self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,						SCREAMING_SNAKE_CASE     ,						atol=1e-4	)	)






'''simple docstring'''







from math import isqrt


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	int  )  ->			bool:
       return all(number % divisor != 0 for divisor in range(2    ,isqrt(_UpperCAmelCase  ) + 1  )  )


def 	SCREAMING_SNAKE_CASE_					(  _UpperCAmelCase	:	int = 10**6  )  ->			int:
       _a    : List[Any]     					=0
       _a    : str     					=1
       _a    : Optional[Any]     					=7
       while prime_candidate < max_prime:
              primes_count += is_prime(_UpperCAmelCase  )

              cube_index += 1
              prime_candidate += 6 * cube_index

       return primes_count


if __name__ == "__main__":
   print(F"{solution() = }")


