





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 		__a							(	unittest.TestCase      ):



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



       def  A							(     self	:   Optional[int]			):
           lowerCAmelCase_					:	Tuple						=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

           lowerCAmelCase_					:	int						=   self.get_config()

           return config, pixel_values



       def  A							(     self	:   List[str]			):
           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  A							(     self	:   Optional[Any]					,			UpperCAmelCase	:   Optional[int]					,			UpperCAmelCase	:   str			):
           lowerCAmelCase_					:	Dict						=   FlaxResNetModel(config=UpperCAmelCase			)
           lowerCAmelCase_					:	Tuple						=   model(UpperCAmelCase			)

           # 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  A							(     self	:   int					,			UpperCAmelCase	:   int					,			UpperCAmelCase	:   List[Any]			):
           lowerCAmelCase_					:	str						=   self.num_labels
           lowerCAmelCase_					:	Optional[int]						=   FlaxResNetForImageClassification(config=UpperCAmelCase			)
           lowerCAmelCase_					:	int						=   model(UpperCAmelCase			)
           self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.num_labels)			)



       def  A							(     self	:   Optional[int]			):
           lowerCAmelCase_					:	Dict						=   self.prepare_config_and_inputs()
           lowerCAmelCase_					:	List[Any]						=   config_and_inputs
           lowerCAmelCase_					:	List[Any]						=   {"""pixel_values""": pixel_values}
           return config, inputs_dict






@require_flax
class 		__a							(	__UpperCamelCase			,unittest.TestCase      ):
       __snake_case						:   Optional[int]      =		(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

       __snake_case						:   Union[str, Any]      =		False
       __snake_case						:   str      =		False
       __snake_case						:   Tuple      =		False



       def  A							(     self	:   int			):
           lowerCAmelCase_					:	str						=   FlaxResNetModelTester(self			)
           lowerCAmelCase_					:	Optional[Any]						=   ConfigTester(self					,			config_class=UpperCAmelCase					,			has_text_modality=UpperCAmelCase			)



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



       def  A							(     self	:   Any			):
           lowerCAmelCase_					:	List[Any]						=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase			)



       def  A							(     self	:   Tuple			):
           lowerCAmelCase_					:	str						=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase			)



       @unittest.skip(reason="""ResNet does not use inputs_embeds"""			)
       def  A							(     self	:   Dict			):
           pass



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



       def  A							(     self	:   Optional[Any]			):
           lowerCAmelCase_					:	Union[str, Any]						=   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowerCAmelCase_					:	Tuple						=   model_class(UpperCAmelCase			)
               lowerCAmelCase_					:	Optional[int]						=   inspect.signature(model.__call__			)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               lowerCAmelCase_					:	Tuple						=   [*signature.parameters.keys()]

               lowerCAmelCase_					:	Optional[Any]						=   ["""pixel_values"""]
               self.assertListEqual(arg_names[:1]					,			UpperCAmelCase			)



       def  A							(     self	:   Optional[Any]			):
           def check_hidden_states_output(UpperCAmelCase	:   Tuple					,			UpperCAmelCase	:   Union[str, Any]					,			UpperCAmelCase	:   List[str]			):
               lowerCAmelCase_					:	str						=   model_class(UpperCAmelCase			)

               lowerCAmelCase_					:	Dict						=   model(**self._prepare_for_class(UpperCAmelCase					,			UpperCAmelCase			)			)

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

               lowerCAmelCase_					:	Optional[Any]						=   self.model_tester.num_stages
               self.assertEqual(len(UpperCAmelCase			)					,			expected_num_stages + 1			)



       @unittest.skip(reason="""ResNet does not use feedforward chunking"""			)
       def  A							(     self	:   Any			):
           pass



       def  A							(     self	:   List[Any]			):
           lowerCAmelCase_					:	Any						=   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               with self.subTest(model_class.__name__			):
                   lowerCAmelCase_					:	Optional[Any]						=   self._prepare_for_class(UpperCAmelCase					,			UpperCAmelCase			)
                   lowerCAmelCase_					:	List[str]						=   model_class(UpperCAmelCase			)

                   @jax.jit
                   def model_jitted(UpperCAmelCase	:   Optional[int]					,			**UpperCAmelCase	:   List[Any]			):
                       return model(pixel_values=UpperCAmelCase					,			**UpperCAmelCase			)

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

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

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






def 			__UpperCamelCase				(			)	->      str:
    '''simple docstring'''





    lowerCAmelCase_					:	List[str]						=   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
    return image

@require_flax
class 		__a							(	unittest.TestCase      ):



       @cached_property
       def  A							(     self	:   Dict			):
           return AutoImageProcessor.from_pretrained("""microsoft/resnet-50"""			) if is_vision_available() else None



       @slow
       def  A							(     self	:   int			):
           lowerCAmelCase_					:	List[Any]						=   FlaxResNetForImageClassification.from_pretrained("""microsoft/resnet-50"""			)

           lowerCAmelCase_					:	int						=   self.default_image_processor
           lowerCAmelCase_					:	Any						=   prepare_img()
           lowerCAmelCase_					:	str						=   image_processor(images=UpperCAmelCase					,			return_tensors="""np"""			)

           lowerCAmelCase_					:	Union[str, Any]						=   model(**UpperCAmelCase			)

           # verify the logits
           lowerCAmelCase_					:	Optional[Any]						=   (1, 10_00)
           self.assertEqual(outputs.logits.shape					,			UpperCAmelCase			)

           lowerCAmelCase_					:	Tuple						=   jnp.array([-11.1069, -9.7877, -8.3777]			)

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









import torch
import torch.nn.functional as F

from diffusers import VQDiffusionScheduler

from .test_schedulers import SchedulerCommonTest

class 		__a							(	__UpperCamelCase      ):
       __snake_case						:   Optional[Any]      =		(VQDiffusionScheduler,)



       def  A							(     self	:   Optional[int]					,			**UpperCAmelCase	:   List[Any]			):
           lowerCAmelCase_					:	Any						=   {
               """num_vec_classes""": 40_97,
               """num_train_timesteps""": 1_00,
           }

           config.update(**UpperCAmelCase			)
           return config



       def  A							(     self	:   str					,			UpperCAmelCase	:   List[Any]			):
           lowerCAmelCase_					:	Union[str, Any]						=   4
           lowerCAmelCase_					:	str						=   8
           lowerCAmelCase_					:	Optional[Any]						=   8

           lowerCAmelCase_					:	str						=   torch.randint(0					,			UpperCAmelCase					,			(batch_size, height * width)			)

           return sample



       @property
       def  A							(     self	:   Optional[int]			):
           assert False



       def  A							(     self	:   int					,			UpperCAmelCase	:   Dict			):
           def model(UpperCAmelCase	:   Union[str, Any]					,			UpperCAmelCase	:   List[str]					,			*UpperCAmelCase	:   List[Any]			):
               lowerCAmelCase_		,		lowerCAmelCase_					:	Tuple						=   sample.shape
               lowerCAmelCase_					:	int						=   torch.rand((batch_size, num_vec_classes - 1, num_latent_pixels)			)
               lowerCAmelCase_					:	str						=   F.log_softmax(logits.double()					,			dim=1			).float()
               return return_value

           return model



       def  A							(     self	:   Union[str, Any]			):
           for timesteps in [2, 5, 1_00, 10_00]:
               self.check_over_configs(num_train_timesteps=UpperCAmelCase			)



       def  A							(     self	:   str			):
           for num_vec_classes in [5, 1_00, 10_00, 40_00]:
               self.check_over_configs(num_vec_classes=UpperCAmelCase			)



       def  A							(     self	:   Optional[Any]			):
           for t in [0, 50, 99]:
               self.check_over_forward(time_step=UpperCAmelCase			)



       def  A							(     self	:   int			):
           pass


