


'''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    __magic_name__				:






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




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






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




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

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

          _UpperCAmelCase  :  Optional[Any]				       =self.get_config()
          return config, pixel_values, labels






       def      lowerCAmelCase						(     self)		->    Dict:




          '''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      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =ConvNextModel(config=snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  str				       =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 // 3_2, self.image_size // 3_2)    ,				)






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    int:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =ConvNextForImageClassification(snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  Dict				       =model(snake_case    ,				labels=snake_case)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_labels))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =ConvNextBackbone(config=snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  Dict				       =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
          _UpperCAmelCase  :  List[Any]				       =None
          _UpperCAmelCase  :  Dict				       =ConvNextBackbone(config=snake_case)
          model.to(snake_case)
          model.eval()
          _UpperCAmelCase  :  int				       =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      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =self.prepare_config_and_inputs()
          _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase  :  List[Any]				       =config_and_inputs
          _UpperCAmelCase  :  int				       ={'pixel_values': pixel_values}
          return config, inputs_dict






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

       UpperCAmelCase		 =True
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =ConvNextModelTester(self)
          _UpperCAmelCase  :  List[str]				       =ConfigTester(self    ,				config_class=snake_case    ,				has_text_modality=snake_case    ,				hidden_size=3_7)






       def      lowerCAmelCase						(     self)		->    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      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          return






       @unittest.skip(reason='ConvNext does not use inputs_embeds')
       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          pass






       @unittest.skip(reason='ConvNext does not support input and output embeddings')
       def      lowerCAmelCase						(     self)		->    int:




          '''simple docstring'''
          pass






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




          '''simple docstring'''
          pass






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase       ,			_UpperCAmelCase  :  Optional[int]				       =self.model_tester.prepare_config_and_inputs_for_common()

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

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






       def      lowerCAmelCase						(     self)		->    Tuple:




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case)






       def      lowerCAmelCase						(     self)		->    int:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_backbone(*snake_case)






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          def check_hidden_states_output(snake_case    ,				snake_case    ,				snake_case):
             _UpperCAmelCase  :  Dict				       =model_class(snake_case)
             model.to(snake_case)
             model.eval()

             with torch.no_grad():
                _UpperCAmelCase  :  Tuple				       =model(**self._prepare_for_class(snake_case    ,				snake_case))

             _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(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]    ,				)

          _UpperCAmelCase       ,			_UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _UpperCAmelCase  :  Tuple				       =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"]
             _UpperCAmelCase  :  Union[str, Any]				       =True

             check_hidden_states_output(snake_case    ,				snake_case    ,				snake_case)






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case)






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




          '''simple docstring'''
          for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  List[str]				       =ConvNextModel.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)






def lowerCamelCase__     (		):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[int]				       =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
   return image






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






       @cached_property
       def      lowerCAmelCase						(     self)		->    Tuple:




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






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




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224').to(snake_case)

          _UpperCAmelCase  :  List[Any]				       =self.default_image_processor
          _UpperCAmelCase  :  Optional[Any]				       =prepare_img()
          _UpperCAmelCase  :  int				       =image_processor(images=snake_case    ,				return_tensors='pt').to(snake_case)

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

          # verify the logits
          _UpperCAmelCase  :  Union[str, Any]				       =torch.Size((1, 1_0_0_0))
          self.assertEqual(outputs.logits.shape    ,				snake_case)

          _UpperCAmelCase  :  List[Any]				       =torch.tensor([-0.02_60, -0.47_39, 0.19_11]).to(snake_case)

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






@require_torch
class    __magic_name__				(						unittest.TestCase   ,lowerCAmelCase							):
       UpperCAmelCase		 =(ConvNextBackbone,) if is_torch_available() else ()
       UpperCAmelCase		 =ConvNextConfig

       UpperCAmelCase		 =False






       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =ConvNextModelTester(self)





'''simple docstring'''






from __future__ import annotations

from fractions import Fraction





def lowerCamelCase__     (		__lowerCamelCase			:				int			,       __lowerCamelCase			:				int				):







   '''simple docstring'''




   return (
       num != den and num % 1_0 == den // 1_0 and (num // 1_0) / (den % 1_0) == num / den
   )





def lowerCamelCase__     (		__lowerCamelCase			:				int				):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[int]				       =[]
   _UpperCAmelCase  :  Union[str, Any]				       =1_1
   _UpperCAmelCase  :  Union[str, Any]				       =int('1' + '0' * digit_len				)
   for num in range(__lowerCamelCase			,       __lowerCamelCase				):
      while den <= 9_9:
         if (num != den) and (num % 1_0 == den // 1_0) and (den % 1_0 != 0):
            if is_digit_cancelling(__lowerCamelCase			,       __lowerCamelCase				):
               solutions.append(f"{num}/{den}"				)
         den += 1
      num += 1
      _UpperCAmelCase  :  Optional[int]				       =1_0
   return solutions





def lowerCamelCase__     (		__lowerCamelCase			:				int = 2				):







   '''simple docstring'''




   _UpperCAmelCase  :  Any				       =1.0
   for fraction in fraction_list(__lowerCamelCase				):
      _UpperCAmelCase  :  List[str]				       =Fraction(__lowerCamelCase				)
      result *= frac.denominator / frac.numerator
   return int(__lowerCamelCase				)


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

