
'''simple docstring'''





import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
     import jax
     import jax.numpy as jnp

     from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor



class 			lowerCAmelCase__ (							unittest.TestCase							):



  '''simple docstring'''






  def __init__(self	:			List[Any]						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			int=3						,			lowerCAmelCase_	:			List[Any]=32						,			lowerCAmelCase_	:			str=3						,			lowerCAmelCase_	:			Dict=10						,			lowerCAmelCase_	:			Dict=[10, 20, 30, 40]						,			lowerCAmelCase_	:			Any=[1, 1, 2, 1]						,			lowerCAmelCase_	:			List[Any]=True						,			lowerCAmelCase_	:			str=True						,			lowerCAmelCase_	:			List[Any]="relu"						,			lowerCAmelCase_	:			List[str]=3						,			lowerCAmelCase_	:			List[Any]=None						,			) ->							Dict:
        snake_case_    							=				parent
        snake_case_    							=				batch_size
        snake_case_    							=				image_size
        snake_case_    							=				num_channels
        snake_case_    							=				embeddings_size
        snake_case_    							=				hidden_sizes
        snake_case_    							=				depths
        snake_case_    							=				is_training
        snake_case_    							=				use_labels
        snake_case_    							=				hidden_act
        snake_case_    							=				num_labels
        snake_case_    							=				scope
        snake_case_    							=				len(lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							int:
        snake_case_    							=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

        snake_case_    							=				self.get_config()

        return config, pixel_values

  def 		_lowerCamelCase						(self	:			List[str]     ) ->							Tuple:
        return ResNetConfig(
            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						,			image_size=self.image_size						,			)

  def 		_lowerCamelCase						(self	:			int						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Dict     ) ->							Optional[int]:
        snake_case_    							=				FlaxResNetModel(config=lowerCAmelCase_     )
        snake_case_    							=				model(lowerCAmelCase_     )

        # Output shape (b, c, h, w)
        self.parent.assertEqual(
            result.last_hidden_state.shape						,			(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)						,			)

  def 		_lowerCamelCase						(self	:			str						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Any     ) ->							List[Any]:
        snake_case_    							=				self.num_labels
        snake_case_    							=				FlaxResNetForImageClassification(config=lowerCAmelCase_     )
        snake_case_    							=				model(lowerCAmelCase_     )
        self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)     )


  def 		_lowerCamelCase						(self	:			int     ) ->							List[str]:
        snake_case_    							=				self.prepare_config_and_inputs()
        snake_case_							,						snake_case_    							=				config_and_inputs
        snake_case_    							=				{"pixel_values": pixel_values}
        return config, inputs_dict



@require_flax
class 			lowerCAmelCase__ (							UpperCamelCase       ,				unittest.TestCase							):



  '''simple docstring'''






  _UpperCamelCase							=			(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

  _UpperCamelCase							=			False
  _UpperCamelCase							=			False
  _UpperCamelCase							=			False

  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							None:
        snake_case_    							=				FlaxResNetModelTester(self     )
        snake_case_    							=				ConfigTester(self						,			config_class=lowerCAmelCase_						,			has_text_modality=lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							int:
        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     ) ->							Optional[int]:
        return

  def 		_lowerCamelCase						(self	:			List[Any]     ) ->							Dict:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			List[Any]     ) ->							str:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_     )

  @unittest.skip(reason="ResNet does not use inputs_embeds"     )
  def 		_lowerCamelCase						(self	:			int     ) ->							Optional[int]:
        pass

  @unittest.skip(reason="ResNet does not support input and output embeddings"     )
  def 		_lowerCamelCase						(self	:			str     ) ->							Any:
        pass

  def 		_lowerCamelCase						(self	:			Union[str, Any]     ) ->							Union[str, Any]:
        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(lowerCAmelCase_     )
              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]						,			lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Dict     ) ->							List[Any]:
        def check_hidden_states_output(lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			Optional[int]     ):
              snake_case_    							=				model_class(lowerCAmelCase_     )

              snake_case_    							=				model(**self._prepare_for_class(lowerCAmelCase_						,			lowerCAmelCase_     )     )

              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(lowerCAmelCase_     )						,			expected_num_stages + 1     )

  @unittest.skip(reason="ResNet does not use feedforward chunking"     )
  def 		_lowerCamelCase						(self	:			Tuple     ) ->							Tuple:
        pass


  def 		_lowerCamelCase						(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:
              with self.subTest(model_class.__name__     ):
                    snake_case_    							=				self._prepare_for_class(lowerCAmelCase_						,			lowerCAmelCase_     )
                    snake_case_    							=				model_class(lowerCAmelCase_     )

                    @jax.jit
                    def model_jitted(lowerCAmelCase_	:			int						,			**lowerCAmelCase_	:			Any     ):
                          return model(pixel_values=lowerCAmelCase_						,			**lowerCAmelCase_     )

                    with self.subTest("JIT Enabled"     ):
                          snake_case_    							=				model_jitted(**lowerCAmelCase_     ).to_tuple()

                    with self.subTest("JIT Disabled"     ):
                          with jax.disable_jit():
                                snake_case_    							=				model_jitted(**lowerCAmelCase_     ).to_tuple()

                    self.assertEqual(len(lowerCAmelCase_     )						,			len(lowerCAmelCase_     )     )
                    for jitted_output, output in zip(lowerCAmelCase_						,			lowerCAmelCase_     ):
                          self.assertEqual(jitted_output.shape						,			output.shape     )

def 	snake_case_							(				):
      snake_case_    							=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
      return image




@require_flax
class 			lowerCAmelCase__ (							unittest.TestCase							):



  '''simple docstring'''






  @cached_property
  def 		_lowerCamelCase						(self	:			int     ) ->							str:
        return AutoImageProcessor.from_pretrained("microsoft/resnet-50"     ) if is_vision_available() else None


  @slow
  def 		_lowerCamelCase						(self	:			List[Any]     ) ->							Union[str, Any]:
        snake_case_    							=				FlaxResNetForImageClassification.from_pretrained("microsoft/resnet-50"     )

        snake_case_    							=				self.default_image_processor
        snake_case_    							=				prepare_img()
        snake_case_    							=				image_processor(images=lowerCAmelCase_						,			return_tensors="np"     )

        snake_case_    							=				model(**lowerCAmelCase_     )

        # verify the logits
        snake_case_    							=				(1, 10_00)
        self.assertEqual(outputs.logits.shape						,			lowerCAmelCase_     )

        snake_case_    							=				jnp.array([-11.1_069, -9.7_877, -8.3_777]     )

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






"""simple docstring"""







import unittest
from timeit import timeit

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  lowerCAmelCase_  :							Any								=					first_num if first_num >= second_num else second_num
  lowerCAmelCase_  :							Union[str, Any]								=					max_num
  while (common_mult % first_num > 0) or (common_mult % second_num > 0):
    common_mult += max_num
  return common_mult

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  return b if a == 0 else greatest_common_divisor(b % a       ,  lowerCAmelCase__		)

def 							UpperCamelCase_       (       lowerCAmelCase__ :							int       ,  lowerCAmelCase__ :							int		)		->  int:

  """simple docstring"""




  return first_num // greatest_common_divisor(lowerCAmelCase__       ,  lowerCAmelCase__		) * second_num

def 							UpperCamelCase_       (       )		->  Dict:

  """simple docstring"""




  lowerCAmelCase_  :							str								=					(
      'from __main__ import least_common_multiple_slow, least_common_multiple_fast'
  )
  print(
      'least_common_multiple_slow():'       ,  timeit('least_common_multiple_slow(1000, 999)'       ,  setup=lowerCAmelCase__		)       ,  )
  print(
      'least_common_multiple_fast():'       ,  timeit('least_common_multiple_fast(1000, 999)'       ,  setup=lowerCAmelCase__		)       ,  )







class  UpperCamelCase__  (      unittest.TestCase    ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE    							=	(
          (10, 20),
          (13, 15),
          (4, 31),
          (10, 42),
          (43, 34),
          (5, 12),
          (12, 25),
          (10, 25),
          (6, 9),
      )
      _SCREAMING_SNAKE_CASE    							=	(20, 195, 124, 210, 1462, 60, 300, 50, 18)



      def 	SCREAMING_SNAKE_CASE__					( self							: List[str]			):
        for i, (first_num, second_num) in enumerate(self.test_inputs			):
          lowerCAmelCase_  :							Union[str, Any]								=					least_common_multiple_slow(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)
          lowerCAmelCase_  :							Any								=					least_common_multiple_fast(SCREAMING_SNAKE_CASE_       ,     SCREAMING_SNAKE_CASE_			)
          with self.subTest(i=SCREAMING_SNAKE_CASE_			):
            self.assertEqual(SCREAMING_SNAKE_CASE_       ,     self.expected_results[i]			)
            self.assertEqual(SCREAMING_SNAKE_CASE_       ,     self.expected_results[i]			)


if __name__ == "__main__":
     benchmark()
     unittest.main()





