






from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
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 TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor
class 							A_    :






 '''simple docstring'''




 def __init__(	self							,  _A							,  _A=3							,  _A=32							,  _A=3							,  _A=10							,  _A=[10, 20, 30, 40]							,  _A=[1, 1, 2, 1]							,  _A=True							,  _A=True							,  _A="relu"							,  _A=3							,  _A=None							,  ) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				parent
     _UpperCAmelCase				:      List[str]			  =				batch_size
     _UpperCAmelCase				:      Tuple			  =				image_size
     _UpperCAmelCase				:      List[str]			  =				num_channels
     _UpperCAmelCase				:      List[str]			  =				embeddings_size
     _UpperCAmelCase				:      str			  =				hidden_sizes
     _UpperCAmelCase				:      Union[str, Any]			  =				depths
     _UpperCAmelCase				:      List[Any]			  =				is_training
     _UpperCAmelCase				:      str			  =				use_labels
     _UpperCAmelCase				:      Dict			  =				hidden_act
     _UpperCAmelCase				:      Dict			  =				num_labels
     _UpperCAmelCase				:      Dict			  =				scope
     _UpperCAmelCase				:      Tuple			  =				len(_A)


 def  snake_case__				(	self) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

     _UpperCAmelCase				:      int			  =				None
     if self.use_labels:
         _UpperCAmelCase				:      List[Any]			  =				ids_tensor([self.batch_size]							,  self.num_labels)

     _UpperCAmelCase				:      str			  =				self.get_config()
     return config, pixel_values, labels


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

     """simple docstring"""



     return RegNetConfig(
         num_channels=self.num_channels							,  embeddings_size=self.embeddings_size							,  hidden_sizes=self.hidden_sizes							,  depths=self.depths							,  hidden_act=self.hidden_act							,  num_labels=self.num_labels							,  )


 def  snake_case__				(	self							,  _A							,  _A							,  _A) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				TFRegNetModel(config=_A)
     _UpperCAmelCase				:      str			  =				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  snake_case__				(	self							,  _A							,  _A							,  _A) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      List[Any]			  =				self.num_labels
     _UpperCAmelCase				:      List[Any]			  =				TFRegNetForImageClassification(_A)
     _UpperCAmelCase				:      Any			  =				model(_A							,  labels=_A							,  training=_A)
     self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_labels))


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



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



@require_tf
class 							A_    (						__lowercase ,   __lowercase ,   unittest.TestCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Dict =					(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
 _SCREAMING_SNAKE_CASE							:			Optional[int] =					(
     {"feature-extraction": TFRegNetModel, "image-classification": TFRegNetForImageClassification}
     if is_tf_available()
     else {}
 )

 _SCREAMING_SNAKE_CASE							:			int =					False
 _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					False
 _SCREAMING_SNAKE_CASE							:			List[str] =					False
 _SCREAMING_SNAKE_CASE							:			List[str] =					False
 _SCREAMING_SNAKE_CASE							:			List[Any] =					False


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				TFRegNetModelTester(self)
     _UpperCAmelCase				:      Tuple			  =				ConfigTester(self							,  config_class=_A							,  has_text_modality=_A)


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     return


 @unittest.skip(reason='''RegNet does not use inputs_embeds''')
 def  snake_case__				(	self) ->		List[str]:

     """simple docstring"""



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

     """simple docstring"""



     super().test_keras_fit()


 @unittest.skip(reason='''RegNet does not support input and output embeddings''')
 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     pass


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase  , _UpperCAmelCase				:      Optional[Any]			  =				self.model_tester.prepare_config_and_inputs_for_common()

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

         _UpperCAmelCase				:      List[Any]			  =				['''pixel_values''']
         self.assertListEqual(arg_names[:1]							,  _A)


 def  snake_case__				(	self) ->		str:

     """simple docstring"""



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


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

     """simple docstring"""



     def check_hidden_states_output(_A							,  _A							,  _A):
         _UpperCAmelCase				:      Optional[int]			  =				model_class(_A)
         _UpperCAmelCase				:      Any			  =				model(**self._prepare_for_class(_A							,  _A)							,  training=_A)

         _UpperCAmelCase				:      int			  =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         _UpperCAmelCase				:      int			  =				self.model_tester.num_stages
         self.assertEqual(len(_A)							,  expected_num_stages + 1)

         # RegNet'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 // 2, self.model_tester.image_size // 2]							,  )

     _UpperCAmelCase  , _UpperCAmelCase				:      Dict			  =				self.model_tester.prepare_config_and_inputs_for_common()
     _UpperCAmelCase				:      Optional[int]			  =				['''basic''', '''bottleneck''']
     for model_class in self.all_model_classes:
         for layer_type in layers_type:
             _UpperCAmelCase				:      Optional[Any]			  =				layer_type
             _UpperCAmelCase				:      int			  =				True
             check_hidden_states_output(_A							,  _A							,  _A)

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

             check_hidden_states_output(_A							,  _A							,  _A)


 def  snake_case__				(	self) ->		str:

     """simple docstring"""



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

     def check_equivalence(_A							,  _A							,  _A							,  _A={}):
         _UpperCAmelCase				:      Dict			  =				model(_A							,  return_dict=_A							,  **_A)
         _UpperCAmelCase				:      Optional[Any]			  =				model(_A							,  return_dict=_A							,  **_A).to_tuple()

         def recursive_check(_A							,  _A):
             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:
         _UpperCAmelCase				:      str			  =				model_class(_A)

         _UpperCAmelCase				:      Any			  =				self._prepare_for_class(_A							,  _A)
         _UpperCAmelCase				:      Tuple			  =				self._prepare_for_class(_A							,  _A)
         check_equivalence(_A							,  _A							,  _A)

         _UpperCAmelCase				:      str			  =				self._prepare_for_class(_A							,  _A							,  return_labels=_A)
         _UpperCAmelCase				:      Tuple			  =				self._prepare_for_class(_A							,  _A							,  return_labels=_A)
         check_equivalence(_A							,  _A							,  _A)

         _UpperCAmelCase				:      Union[str, Any]			  =				self._prepare_for_class(_A							,  _A)
         _UpperCAmelCase				:      Tuple			  =				self._prepare_for_class(_A							,  _A)
         check_equivalence(_A							,  _A							,  _A							,  {'''output_hidden_states''': True})

         _UpperCAmelCase				:      Union[str, Any]			  =				self._prepare_for_class(_A							,  _A							,  return_labels=_A)
         _UpperCAmelCase				:      List[str]			  =				self._prepare_for_class(_A							,  _A							,  return_labels=_A)
         check_equivalence(_A							,  _A							,  _A							,  {'''output_hidden_states''': True})


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*_A)


 @slow
 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _UpperCAmelCase				:      Dict			  =				TFRegNetModel.from_pretrained(_A)
         self.assertIsNotNone(_A)



def      _lowerCamelCase	(						)      ->					Dict:
    _UpperCAmelCase				:      Optional[Any]			  =				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
    return image


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






 '''simple docstring'''




 @cached_property
 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     return (
         AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])
         if is_vision_available()
         else None
     )


 @slow
 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])

     _UpperCAmelCase				:      Union[str, Any]			  =				self.default_image_processor
     _UpperCAmelCase				:      List[Any]			  =				prepare_img()
     _UpperCAmelCase				:      Optional[int]			  =				image_processor(images=_A							,  return_tensors='''tf''')

     # forward pass
     _UpperCAmelCase				:      int			  =				model(**_A							,  training=_A)

     # verify the logits
     _UpperCAmelCase				:      List[Any]			  =				tf.TensorShape((1, 1000))
     self.assertEqual(outputs.logits.shape							,  _A)

     _UpperCAmelCase				:      Dict			  =				tf.constant([-0.4180, -1.5051, -3.4836])

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












from __future__ import annotations

def      _lowerCamelCase	(						__A    :							int )      ->					list[int]:
    _UpperCAmelCase				:      List[str]			  =				[True] * limit
    _UpperCAmelCase				:      Optional[int]			  =				False
    _UpperCAmelCase				:      Dict			  =				False
    _UpperCAmelCase				:      List[str]			  =				True

    for i in range(3		,		int(limit**0.5 + 1 )		,		2 ):
        _UpperCAmelCase				:      List[str]			  =				i * 2
        while index < limit:
            _UpperCAmelCase				:      Union[str, Any]			  =				False
            _UpperCAmelCase				:      int			  =				index + i

    _UpperCAmelCase				:      Optional[int]			  =				[2]

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

    return primes

def      _lowerCamelCase	(						__A    :							int = 1_000_000 )      ->					int:
    _UpperCAmelCase				:      Any			  =				prime_sieve(__A )
    _UpperCAmelCase				:      Optional[Any]			  =				0
    _UpperCAmelCase				:      Tuple			  =				0

    for i in range(len(__A ) ):
        for j in range(i + length		,		len(__A ) ):
            _UpperCAmelCase				:      List[Any]			  =				sum(primes[i:j] )
            if sol >= ceiling:
                break

            if sol in primes:
                _UpperCAmelCase				:      List[str]			  =				j - i
                _UpperCAmelCase				:      Optional[Any]			  =				sol

    return largest


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




