


import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 EfficientNetForImageClassification, EfficientNetModel
					from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor

class 	__a   :
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=13    ,					_SCREAMING_SNAKE_CASE=32    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=[3, 3, 5]    ,					_SCREAMING_SNAKE_CASE=[32, 16, 24]    ,					_SCREAMING_SNAKE_CASE=[16, 24, 40]    ,					_SCREAMING_SNAKE_CASE=[1, 1, 2]    ,					_SCREAMING_SNAKE_CASE=[1, 1, 2]    ,					_SCREAMING_SNAKE_CASE=[1, 6, 6]    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=37    ,					_SCREAMING_SNAKE_CASE="gelu"    ,					_SCREAMING_SNAKE_CASE=10    ,					)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    parent
									_UpperCAmelCase					=    batch_size
									_UpperCAmelCase					=    image_size
									_UpperCAmelCase					=    num_channels
									_UpperCAmelCase					=    kernel_sizes
									_UpperCAmelCase					=    in_channels
									_UpperCAmelCase					=    out_channels
									_UpperCAmelCase					=    strides
									_UpperCAmelCase					=    num_block_repeats
									_UpperCAmelCase					=    expand_ratios
									_UpperCAmelCase					=    is_training
									_UpperCAmelCase					=    hidden_act
									_UpperCAmelCase					=    num_labels
									_UpperCAmelCase					=    use_labels
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





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

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

									_UpperCAmelCase					=    self.get_config()
									return config, pixel_values, labels
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									return EfficientNetConfig(
									    num_channels=self.num_channels    ,					kernel_sizes=self.kernel_sizes    ,					in_channels=self.in_channels    ,					out_channels=self.out_channels    ,					strides=self.strides    ,					num_block_repeats=self.num_block_repeats    ,					expand_ratios=self.expand_ratios    ,					hidden_act=self.hidden_act    ,					num_labels=self.num_labels    ,					)
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> int:




									"""simple docstring"""





									_UpperCAmelCase					=    EfficientNetModel(config=lowercase_		)
									model.to(lowercase_		)
									model.eval()
									_UpperCAmelCase					=    model(lowercase_		)
									# expected last hidden states: B, C, H // 4, W // 4
									self.parent.assertEqual(
									    result.last_hidden_state.shape    ,					(self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)    ,					)
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase					=    EfficientNetForImageClassification(lowercase_		)
									model.to(lowercase_		)
									model.eval()
									_UpperCAmelCase					=    model(lowercase_    ,					labels=lowercase_		)
									self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)		)






		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									_UpperCAmelCase					=    self.prepare_config_and_inputs()
									_UpperCAmelCase					=    config_and_inputs
									_UpperCAmelCase					=    {'''pixel_values''': pixel_values}
									return config, inputs_dict



@require_torch
class 	__a   (       __UpperCAmelCase					,   __UpperCAmelCase					,   unittest.TestCase    ):
		_a			:						str    			=			(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
		_a			:						Optional[int]    			=			(
		    {'feature-extraction': EfficientNetModel, 'image-classification': EfficientNetForImageClassification}
		    if is_torch_available()
		    else {}
		)

		_a			:						Dict    			=			False
		_a			:						str    			=			False
		_a			:						List[str]    			=			False
		_a			:						Optional[int]    			=			False
		_a			:						Any    			=			False
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase					=    EfficientNetModelTester(self		)
									_UpperCAmelCase					=    ConfigTester(
									    self    ,					config_class=lowercase_    ,					has_text_modality=lowercase_    ,					hidden_size=37		)
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""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 			UpperCAmelCase__       (    self		)				-> Optional[Any]:




									"""simple docstring"""





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




									"""simple docstring"""





									pass
		@unittest.skip(reason='EfficientNet does not support input and output embeddings'		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									pass
		@unittest.skip(reason='EfficientNet does not use feedforward chunking'		)
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									pass
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs_for_common()

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

																_UpperCAmelCase					=    ['''pixel_values''']
																self.assertListEqual(arg_names[:1]    ,					lowercase_		)
		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase_		)
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									def check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    model_class(lowercase_		)
																model.to(lowercase_		)
																model.eval()

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

																_UpperCAmelCase					=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
																_UpperCAmelCase					=    sum(config.num_block_repeats		) * 4
																self.assertEqual(len(lowercase_		)    ,					lowercase_		)

																# EfficientNet'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					=    self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																_UpperCAmelCase					=    True
																check_hidden_states_output(lowercase_    ,					lowercase_    ,					lowercase_		)

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

																check_hidden_states_output(lowercase_    ,					lowercase_    ,					lowercase_		)
		def 			UpperCAmelCase__       (    self		)				-> List[str]:




									"""simple docstring"""





									_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowercase_		)
		@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





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




									"""simple docstring"""





									for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCAmelCase					=    EfficientNetModel.from_pretrained(lowercase_		)
																self.assertIsNotNone(lowercase_		)






		@is_pipeline_test
		@require_vision
		@slow
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									super().test_pipeline_image_classification()



def  lowerCAmelCase__				(    )						->	Optional[Any]:


							'''simple docstring'''





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







@require_torch
@require_vision
class 	__a   (       unittest.TestCase    ):
		@cached_property
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									return AutoImageProcessor.from_pretrained('google/efficientnet-b7'		) if is_vision_available() else None






		@slow
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""simple docstring"""





									_UpperCAmelCase					=    EfficientNetForImageClassification.from_pretrained('google/efficientnet-b7'		).to(lowercase_		)

									_UpperCAmelCase					=    self.default_image_processor
									_UpperCAmelCase					=    prepare_img()
									_UpperCAmelCase					=    image_processor(images=lowercase_    ,					return_tensors='pt'		).to(lowercase_		)

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

									# verify the logits
									_UpperCAmelCase					=    torch.Size((1, 1000)		)
									self.assertEqual(outputs.logits.shape    ,					lowercase_		)

									_UpperCAmelCase					=    torch.tensor([0.0001, 0.0002, 0.0002]		).to(lowercase_		)
									self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,					lowercase_    ,					atol=1e-4		)		)




import tempfile
import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVeScheduler

class 	__a   (       unittest.TestCase    ):
		# TODO adapt with class SchedulerCommonTest (scheduler needs Numpy Integration)
		_a			:						List[Any]    			=			(ScoreSdeVeScheduler,)
		_a			:						Any    			=			()
		@property
		def 			UpperCAmelCase__       (    self		)				-> List[str]:




									"""simple docstring"""





									_UpperCAmelCase					=    4
									_UpperCAmelCase					=    3
									_UpperCAmelCase					=    8
									_UpperCAmelCase					=    8

									_UpperCAmelCase					=    torch.rand((batch_size, num_channels, height, width)		)

									return sample
		@property
		def 			UpperCAmelCase__       (    self		)				-> Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    4
									_UpperCAmelCase					=    3
									_UpperCAmelCase					=    8
									_UpperCAmelCase					=    8

									_UpperCAmelCase					=    batch_size * num_channels * height * width
									_UpperCAmelCase					=    torch.arange(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    sample.reshape(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    sample / num_elems
									_UpperCAmelCase					=    sample.permute(3    ,					0    ,					1    ,					2		)

									return sample
		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





									def model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					*_SCREAMING_SNAKE_CASE		):
																return sample * t / (t + 1)

									return model
		def 			UpperCAmelCase__       (    self    ,					**_SCREAMING_SNAKE_CASE		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    {
									    'num_train_timesteps': 2000,
									    'snr': 0.15,
									    'sigma_min': 0.01,
									    'sigma_max': 1348,
									    'sampling_eps': 1e-5,
									}

									config.update(**_SCREAMING_SNAKE_CASE		)
									return config
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE=0    ,					**_SCREAMING_SNAKE_CASE		)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    dict(self.forward_default_kwargs		)

									for scheduler_class in self.scheduler_classes:
																_UpperCAmelCase					=    self.dummy_sample
																_UpperCAmelCase					=    0.1 * sample

																_UpperCAmelCase					=    self.get_scheduler_config(**_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase					=    scheduler_class(**_SCREAMING_SNAKE_CASE		)

																with tempfile.TemporaryDirectory() as tmpdirname:
																							scheduler.save_config(_SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase					=    scheduler_class.from_pretrained(_SCREAMING_SNAKE_CASE		)

																_UpperCAmelCase					=    scheduler.step_pred(
																    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample
																_UpperCAmelCase					=    new_scheduler.step_pred(
																    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample

																assert torch.sum(torch.abs(output - new_output		)		) < 1e-5, "Scheduler outputs are not identical"

																_UpperCAmelCase					=    scheduler.step_correct(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample
																_UpperCAmelCase					=    new_scheduler.step_correct(
																    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample

																assert torch.sum(torch.abs(output - new_output		)		) < 1e-5, "Scheduler correction are not identical"
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE=0    ,					**_SCREAMING_SNAKE_CASE		)				-> List[str]:




									"""simple docstring"""





									_UpperCAmelCase					=    dict(self.forward_default_kwargs		)
									kwargs.update(_SCREAMING_SNAKE_CASE		)

									for scheduler_class in self.scheduler_classes:
																_UpperCAmelCase					=    self.dummy_sample
																_UpperCAmelCase					=    0.1 * sample

																_UpperCAmelCase					=    self.get_scheduler_config()
																_UpperCAmelCase					=    scheduler_class(**_SCREAMING_SNAKE_CASE		)

																with tempfile.TemporaryDirectory() as tmpdirname:
																							scheduler.save_config(_SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase					=    scheduler_class.from_pretrained(_SCREAMING_SNAKE_CASE		)

																_UpperCAmelCase					=    scheduler.step_pred(
																    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample
																_UpperCAmelCase					=    new_scheduler.step_pred(
																    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample

																assert torch.sum(torch.abs(output - new_output		)		) < 1e-5, "Scheduler outputs are not identical"

																_UpperCAmelCase					=    scheduler.step_correct(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample
																_UpperCAmelCase					=    new_scheduler.step_correct(
																    _SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample

																assert torch.sum(torch.abs(output - new_output		)		) < 1e-5, "Scheduler correction are not identical"
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""simple docstring"""





									for timesteps in [10, 100, 1000]:
																self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									for sigma_min, sigma_max in zip([0.0001, 0.001, 0.01]    ,					[1, 100, 1000]		):
																self.check_over_configs(sigma_min=_SCREAMING_SNAKE_CASE    ,					sigma_max=_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





									for t in [0.1, 0.5, 0.75]:
																self.check_over_forward(time_step=_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    dict(self.forward_default_kwargs		)

									_UpperCAmelCase					=    self.scheduler_classes[0]
									_UpperCAmelCase					=    self.get_scheduler_config()
									_UpperCAmelCase					=    scheduler_class(**_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase					=    3

									_UpperCAmelCase					=    self.dummy_model()
									_UpperCAmelCase					=    self.dummy_sample_deter

									scheduler.set_sigmas(_SCREAMING_SNAKE_CASE		)
									scheduler.set_timesteps(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    torch.manual_seed(0		)

									for i, t in enumerate(scheduler.timesteps		):
																_UpperCAmelCase					=    scheduler.sigmas[i]

																for _ in range(scheduler.config.correct_steps		):
																							with torch.no_grad():
																														_UpperCAmelCase					=    model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase					=    scheduler.step_correct(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE		).prev_sample

																with torch.no_grad():
																							_UpperCAmelCase					=    model(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)

																_UpperCAmelCase					=    scheduler.step_pred(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					generator=_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase	, _UpperCAmelCase					=    output.prev_sample, output.prev_sample_mean

									_UpperCAmelCase					=    torch.sum(torch.abs(_SCREAMING_SNAKE_CASE		)		)
									_UpperCAmelCase					=    torch.mean(torch.abs(_SCREAMING_SNAKE_CASE		)		)

									assert np.isclose(result_sum.item()    ,					14372758528.0		)
									assert np.isclose(result_mean.item()    ,					18714530.0		)






		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=    dict(self.forward_default_kwargs		)

									_UpperCAmelCase					=    kwargs.pop('num_inference_steps'    ,					_SCREAMING_SNAKE_CASE		)

									for scheduler_class in self.scheduler_classes:
																_UpperCAmelCase					=    self.get_scheduler_config()
																_UpperCAmelCase					=    scheduler_class(**_SCREAMING_SNAKE_CASE		)

																_UpperCAmelCase					=    self.dummy_sample
																_UpperCAmelCase					=    0.1 * sample

																if num_inference_steps is not None and hasattr(_SCREAMING_SNAKE_CASE    ,					'set_timesteps'		):
																							scheduler.set_timesteps(_SCREAMING_SNAKE_CASE		)
																elif num_inference_steps is not None and not hasattr(_SCREAMING_SNAKE_CASE    ,					'set_timesteps'		):
																							_UpperCAmelCase					=    num_inference_steps

																_UpperCAmelCase					=    scheduler.step_pred(_SCREAMING_SNAKE_CASE    ,					0    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample
																_UpperCAmelCase					=    scheduler.step_pred(_SCREAMING_SNAKE_CASE    ,					1    ,					_SCREAMING_SNAKE_CASE    ,					generator=torch.manual_seed(0		)    ,					**_SCREAMING_SNAKE_CASE		).prev_sample

																self.assertEqual(output_a.shape    ,					sample.shape		)
																self.assertEqual(output_a.shape    ,					output_a.shape		)
