

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
 from PIL import Image

 from transformers import ConvNextImageProcessor



class     lowercase_       :



      def __init__(     self :							List[str]     ,   A__ :							Optional[Any]     ,   A__ :							Dict=13     ,   A__ :							List[str]=32     ,   A__ :							Optional[Any]=3     ,   A__ :							str=4     ,   A__ :							List[str]=[10, 20, 30, 40]     ,   A__ :							str=[2, 2, 3, 2]     ,   A__ :							Dict=True     ,   A__ :							Optional[int]=True     ,   A__ :							List[Any]=37     ,   A__ :							int="gelu"     ,   A__ :							Union[str, Any]=10     ,   A__ :							Any=0.02     ,   A__ :							Dict=3     ,   A__ :							List[Any]=None     ,   )       ->	Optional[Any]:
        _snake_case		=				parent
        _snake_case		=				batch_size
        _snake_case		=				image_size
        _snake_case		=				num_channels
        _snake_case		=				num_stages
        _snake_case		=				hidden_sizes
        _snake_case		=				depths
        _snake_case		=				is_training
        _snake_case		=				use_labels
        _snake_case		=				intermediate_size
        _snake_case		=				hidden_act
        _snake_case		=				type_sequence_label_size
        _snake_case		=				initializer_range
        _snake_case		=				scope



      def 		UpperCamelCase_						(     self :							str						)       ->	Union[str, Any]:
        _snake_case		=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        _snake_case		=				None
        if self.use_labels:
          _snake_case		=				ids_tensor([self.batch_size]     ,   self.type_sequence_label_size						)

        _snake_case		=				self.get_config()

        return config, pixel_values, labels



      def 		UpperCamelCase_						(     self :							List[Any]						)       ->	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=A__     ,   initializer_range=self.initializer_range     ,   )



      def 		UpperCamelCase_						(     self :							List[str]     ,   A__ :							Union[str, Any]     ,   A__ :							List[Any]     ,   A__ :							Optional[Any]						)       ->	Dict:
        _snake_case		=				TFConvNextModel(config=A__						)
        _snake_case		=				model(A__     ,   training=A__						)
        # 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 		UpperCamelCase_						(     self :							Any     ,   A__ :							Tuple     ,   A__ :							Any     ,   A__ :							Union[str, Any]						)       ->	Union[str, Any]:
        _snake_case		=				self.type_sequence_label_size
        _snake_case		=				TFConvNextForImageClassification(A__						)
        _snake_case		=				model(A__     ,   labels=A__     ,   training=A__						)
        self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.type_sequence_label_size)						)







      def 		UpperCamelCase_						(     self :							str						)       ->	List[Any]:
        _snake_case		=				self.prepare_config_and_inputs()
        _snake_case,	_snake_case,	_snake_case		=				config_and_inputs
        _snake_case		=				{'''pixel_values''': pixel_values}
        return config, inputs_dict



@require_tf
class     lowercase_       (    __lowercase  ,				__lowercase  ,				unittest.TestCase	):
      UpperCamelCase_     :						Optional[int]		 =							(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
      UpperCamelCase_     :						str		 =							(
          {"feature-extraction": TFConvNextModel, "image-classification": TFConvNextForImageClassification}
          if is_tf_available()
          else {}
      )

      UpperCamelCase_     :						Any		 =							False
      UpperCamelCase_     :						Optional[Any]		 =							False
      UpperCamelCase_     :						Tuple		 =							False
      UpperCamelCase_     :						Tuple		 =							False
      UpperCamelCase_     :						List[Any]		 =							False



      def 		UpperCamelCase_						(     self :							List[str]						)       ->	Optional[int]:
        _snake_case		=				TFConvNextModelTester(self						)
        _snake_case		=				ConfigTester(
            self     ,   config_class=A__     ,   has_text_modality=A__     ,   hidden_size=37     ,   )



      @unittest.skip(reason='''ConvNext does not use inputs_embeds'''						)
      def 		UpperCamelCase_						(     self :							List[str]						)       ->	Optional[int]:
        pass



      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''						)						) == 0     ,   reason='''TF does not support backprop for grouped convolutions on CPU.'''     ,   )
      @slow
      def 		UpperCamelCase_						(     self :							Union[str, Any]						)       ->	Union[str, Any]:
        super().test_keras_fit()



      @unittest.skip(reason='''ConvNext does not support input and output embeddings'''						)
      def 		UpperCamelCase_						(     self :							Dict						)       ->	str:
        pass



      def 		UpperCamelCase_						(     self :							Any						)       ->	Optional[int]:
        _snake_case,	_snake_case		=				self.model_tester.prepare_config_and_inputs_for_common()

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

          _snake_case		=				['''pixel_values''']
          self.assertListEqual(arg_names[:1]     ,   A__						)



      def 		UpperCamelCase_						(     self :							List[str]						)       ->	Union[str, Any]:
        _snake_case		=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A__						)



      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''						)						) == 0     ,   reason='''TF does not support backprop for grouped convolutions on CPU.'''     ,   )
      def 		UpperCamelCase_						(     self :							int						)       ->	Tuple:
        super().test_dataset_conversion()



      def 		UpperCamelCase_						(     self :							Dict						)       ->	Any:
        def check_hidden_states_output(A__ :							str     ,   A__ :							Union[str, Any]     ,   A__ :							Optional[int]						):
          _snake_case		=				model_class(A__						)

          _snake_case		=				model(**self._prepare_for_class(A__     ,   A__						)						)
          _snake_case		=				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          _snake_case		=				self.model_tester.num_stages
          self.assertEqual(len(A__						)     ,   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]     ,   )

        _snake_case,	_snake_case		=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          _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"]
          _snake_case		=				True

          check_hidden_states_output(A__     ,   A__     ,   A__						)



      def 		UpperCamelCase_						(     self :							Any						)       ->	str:
        _snake_case,	_snake_case		=				self.model_tester.prepare_config_and_inputs_for_common()

        def check_equivalence(A__ :							str     ,   A__ :							int     ,   A__ :							int     ,   A__ :							str={}						):
          _snake_case		=				model(A__     ,   return_dict=A__     ,   **A__						)
          _snake_case		=				model(A__     ,   return_dict=A__     ,   **A__						).to_tuple()

          def recursive_check(A__ :							Any     ,   A__ :							List[Any]						):
            if isinstance(A__     ,   (List, Tuple)						):
              for tuple_iterable_value, dict_iterable_value in zip(A__     ,   A__						):
                recursive_check(A__     ,   A__						)
            elif tuple_object is None:
              return
            else:
              self.assertTrue(
                  all(tf.equal(A__     ,   A__						)						)     ,   msg=(
                      '''Tuple and dict output are not equal. Difference:'''
                      f""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object						)						)}"""
                  )     ,   )

            recursive_check(A__     ,   A__						)

        for model_class in self.all_model_classes:
          _snake_case		=				model_class(A__						)

          _snake_case		=				self._prepare_for_class(A__     ,   A__						)
          _snake_case		=				self._prepare_for_class(A__     ,   A__						)
          check_equivalence(A__     ,   A__     ,   A__						)

          _snake_case		=				self._prepare_for_class(A__     ,   A__     ,   return_labels=A__						)
          _snake_case		=				self._prepare_for_class(A__     ,   A__     ,   return_labels=A__						)
          check_equivalence(A__     ,   A__     ,   A__						)

          _snake_case		=				self._prepare_for_class(A__     ,   A__						)
          _snake_case		=				self._prepare_for_class(A__     ,   A__						)
          check_equivalence(A__     ,   A__     ,   A__     ,   {'''output_hidden_states''': True}						)

          _snake_case		=				self._prepare_for_class(A__     ,   A__     ,   return_labels=A__						)
          _snake_case		=				self._prepare_for_class(A__     ,   A__     ,   return_labels=A__						)
          check_equivalence(A__     ,   A__     ,   A__     ,   {'''output_hidden_states''': True}						)



      def 		UpperCamelCase_						(     self :							str						)       ->	Any:
        _snake_case		=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*A__						)







      @slow
      def 		UpperCamelCase_						(     self :							Any						)       ->	Optional[Any]:
        _snake_case		=				TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224'''						)
        self.assertIsNotNone(A__						)



def  snake_case_()				->      int:



  """simple docstring"""






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



@require_tf
@require_vision
class     lowercase_       (    unittest.TestCase	):



      @cached_property
      def 		UpperCamelCase_						(     self :							List[Any]						)       ->	List[Any]:
        return ConvNextImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''						) if is_vision_available() else None







      @slow
      def 		UpperCamelCase_						(     self :							Optional[int]						)       ->	Tuple:
        _snake_case		=				TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''						)

        _snake_case		=				self.default_image_processor
        _snake_case		=				prepare_img()
        _snake_case		=				image_processor(images=A__     ,   return_tensors='''tf'''						)

        # forward pass
        _snake_case		=				model(**A__						)

        # verify the logits
        _snake_case		=				tf.TensorShape((1, 1000)						)
        self.assertEqual(outputs.logits.shape     ,   A__						)

        _snake_case		=				tf.constant([-0.0260, -0.4739, 0.1911]						)

        tf.debugging.assert_near(outputs.logits[0, :3]     ,   A__     ,   atol=1e-4						)




from __future__ import annotations

from collections import Counter



def  snake_case_(_UpperCamelCase	)				->      list[int]:



  """simple docstring"""






  _snake_case		=				[True] * n
  _snake_case		=				False
  _snake_case		=				False
  _snake_case		=				True

  for i in range(3					,			int(n**0.5 + 1	)					,			2	):
    _snake_case		=				i * 2
    while index < n:
      _snake_case		=				False
      _snake_case		=				index + i

  _snake_case		=				[2]

  for i in range(3					,			_UpperCamelCase					,			2	):
    if is_prime[i]:
      primes.append(_UpperCamelCase	)

  return primes



def  snake_case_(_UpperCamelCase	)				->      list[list[int]]:



  """simple docstring"""






  _snake_case		=				str(_UpperCamelCase	)
  _snake_case		=				[]
  _snake_case		=				['''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''']

  for duplicate in Counter(_UpperCamelCase	) - Counter(set(_UpperCamelCase	)	):
    _snake_case		=				[int(number_str.replace(_UpperCamelCase					,			_UpperCamelCase	)	) for digit in digits]
    replacements.append(_UpperCamelCase	)

  return replacements



def  snake_case_(_UpperCamelCase = 8	)				->      int:



  """simple docstring"""






  _snake_case		=				set()

  # Filter primes with less than 3 replaceable digits
  _snake_case		=				{
      x for x in set(prime_sieve(1_000_000	)	) if len(str(_UpperCamelCase	)	) - len(set(str(_UpperCamelCase	)	)	) >= 3
  }

  for prime in primes:
    if prime in numbers_checked:
      continue

    _snake_case		=				digit_replacements(_UpperCamelCase	)

    for family in replacements:
      numbers_checked.update(_UpperCamelCase	)
      _snake_case		=				primes.intersection(_UpperCamelCase	)

      if len(_UpperCamelCase	) != family_length:
        continue

      return min(_UpperCamelCase	)

  return -1


if __name__ == "__main__":
 print(solution())

