





import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
						from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image
class SCREAMING_SNAKE_CASE_						:






						def __init__(      self      :      List[str]		,   _A      :      List[Any]		,   _A      :      str=13		,   _A      :      List[Any]=64		,   _A      :      Optional[int]=2		,   _A      :      int=3		,   _A      :      Tuple=True		,   _A      :      Union[str, Any]=True		,   _A      :      Tuple=32		,   _A      :      int=5		,   _A      :      Dict=4		,   _A      :      Dict=37		,   _A      :      Union[str, Any]="gelu"		,   _A      :      Optional[int]=0.1		,   _A      :      List[Any]=0.1		,   _A      :      Any=10		,   _A      :      Tuple=0.0_2		,   _A      :      Tuple=[1, 16, 4, 4]		,   _A      :      Tuple=None		,   )							->    List[str]:







							"""simple docstring"""


							snake_case_		:			Tuple											=	parent
							snake_case_		:			str											=	batch_size
							snake_case_		:			Union[str, Any]											=	image_size
							snake_case_		:			Optional[Any]											=	patch_size
							snake_case_		:			Optional[int]											=	num_channels
							snake_case_		:			Any											=	is_training
							snake_case_		:			Optional[int]											=	use_labels
							snake_case_		:			str											=	hidden_size
							snake_case_		:			Optional[int]											=	num_hidden_layers
							snake_case_		:			Any											=	num_attention_heads
							snake_case_		:			List[Any]											=	intermediate_size
							snake_case_		:			int											=	hidden_act
							snake_case_		:			List[str]											=	hidden_dropout_prob
							snake_case_		:			Tuple											=	attention_probs_dropout_prob
							snake_case_		:			Optional[Any]											=	type_sequence_label_size
							snake_case_		:			Optional[int]											=	initializer_range
							snake_case_		:			Optional[int]											=	scope
							snake_case_		:			Optional[Any]											=	backbone_featmap_shape

							# in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
							# the number of patches is based on the feature map of the backbone, which by default uses an output stride
							# of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
							snake_case_		:			Optional[Any]											=	(self.image_size // 32) ** 2
							snake_case_		:			Optional[int]											=	num_patches + 1






						def UpperCAmelCase_				(      self      :      Optional[Any]	)							->    str:







							"""simple docstring"""


							snake_case_		:			str											=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

							snake_case_		:			List[Any]											=	self.get_config()

							return config, pixel_values, labels






						def UpperCAmelCase_				(      self      :      Union[str, Any]	)							->    List[Any]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	{
							    'global_padding': 'same',
							    'layer_type': 'bottleneck',
							    'depths': [3, 4, 9],
							    'out_features': ['stage1', 'stage2', 'stage3'],
							    'embedding_dynamic_padding': True,
							    'hidden_sizes': [4, 8, 16, 32],
							    'num_groups': 2,
							}

							return ViTHybridConfig(
							    image_size=self.image_size		,   patch_size=self.patch_size		,   num_channels=self.num_channels		,   hidden_size=self.hidden_size		,   num_hidden_layers=self.num_hidden_layers		,   num_attention_heads=self.num_attention_heads		,   intermediate_size=self.intermediate_size		,   hidden_act=self.hidden_act		,   hidden_dropout_prob=self.hidden_dropout_prob		,   attention_probs_dropout_prob=self.attention_probs_dropout_prob		,   is_decoder=_A		,   initializer_range=self.initializer_range		,   backbone_featmap_shape=self.backbone_featmap_shape		,   backbone_config=_A		,   )






						def UpperCAmelCase_				(      self      :      Optional[Any]		,   _A      :      Optional[int]		,   _A      :      Any		,   _A      :      List[Any]	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	ViTHybridModel(config=_A	)
							model.to(_A	)
							model.eval()
							snake_case_		:			str											=	model(_A	)
							self.parent.assertEqual(result.last_hidden_state.shape		,   (self.batch_size, self.seq_length, self.hidden_size)	)






						def UpperCAmelCase_				(      self      :      List[Any]		,   _A      :      Optional[Any]		,   _A      :      Union[str, Any]		,   _A      :      int	)							->    str:







							"""simple docstring"""


							snake_case_		:			Union[str, Any]											=	self.type_sequence_label_size
							snake_case_		:			Tuple											=	ViTHybridForImageClassification(_A	)
							model.to(_A	)
							model.eval()
							snake_case_		:			Dict											=	model(_A		,   labels=_A	)
							self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.type_sequence_label_size)	)






						def UpperCAmelCase_				(      self      :      Any	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	self.prepare_config_and_inputs()
							snake_case_						,snake_case_						,snake_case_		:			str											=	config_and_inputs
							snake_case_		:			int											=	{'pixel_values': pixel_values}
							return config, inputs_dict
@require_torch
class SCREAMING_SNAKE_CASE_						(					snake_case_       ,				snake_case_       ,				unittest.TestCase					):
						__magic_name__:			Dict       =       (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
						__magic_name__:			Tuple       =       (
						    {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
						    if is_torch_available()
						    else {}
						)
						__magic_name__:			Dict       =       False
						__magic_name__:			Dict       =       False
						__magic_name__:			Optional[Any]       =       False






						def UpperCAmelCase_				(      self      :      Tuple	)							->    Any:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	ViTHybridModelTester(self	)
							snake_case_		:			Optional[int]											=	ConfigTester(self		,   config_class=_A		,   has_text_modality=_A		,   hidden_size=37	)






						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    List[str]:







							"""simple docstring"""


							self.config_tester.run_common_tests()






						@unittest.skip(reason='ViT does not use inputs_embeds'	)
						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Optional[int]:







							"""simple docstring"""


							pass






						def UpperCAmelCase_				(      self      :      Any	)							->    Optional[int]:







							"""simple docstring"""


							snake_case_						,snake_case_		:			Optional[int]											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		:			Optional[Any]											=	model_class(_A	)
								self.assertIsInstance(model.get_input_embeddings()		,   (nn.Module)	)
								snake_case_		:			int											=	model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(_A		,   nn.Linear	)	)






						def UpperCAmelCase_				(      self      :      int	)							->    Any:







							"""simple docstring"""


							snake_case_						,snake_case_		:			Optional[int]											=	self.model_tester.prepare_config_and_inputs_for_common()

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

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






						def UpperCAmelCase_				(      self      :      str	)							->    List[Any]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A	)






						def UpperCAmelCase_				(      self      :      int	)							->    Dict:







							"""simple docstring"""


							snake_case_		:			int											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_A	)






						def UpperCAmelCase_				(      self      :      str	)							->    str:







							"""simple docstring"""


							snake_case_						,snake_case_		:			Optional[int]											=	self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_		:			Optional[Any]											=	_config_zero_init(_A	)
							for model_class in self.all_model_classes:
								snake_case_		:			Optional[int]											=	model_class(config=_A	)
								# Skip the check for the backbone
								for name, module in model.named_modules():
									if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
										snake_case_		:			Any											=	[F"""{name}.{key}""" for key in module.state_dict().keys()]
										break

								for name, param in model.named_parameters():
									if param.requires_grad:
										if name in backbone_params:
											continue
										self.assertIn(
										    ((param.data.mean() * 1E9).round() / 1E9).item()		,   [0.0, 1.0]		,   msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""		,   )






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







							"""simple docstring"""


							for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								snake_case_		:			List[Any]											=	ViTHybridModel.from_pretrained(_A	)
								self.assertIsNotNone(_A	)


def 							SCREAMING_SNAKE_CASE__						(    ):
	snake_case_		:			Union[str, Any]											=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
	return image


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






						@cached_property
						def UpperCAmelCase_				(      self      :      str	)							->    List[Any]:







							"""simple docstring"""


							return (
							    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
							    if is_vision_available()
							    else None
							)






						@slow
						def UpperCAmelCase_				(      self      :      int	)							->    Optional[int]:







							"""simple docstring"""


							snake_case_		:			str											=	ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(
							    _A	)

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

							# forward pass
							with torch.no_grad():
								snake_case_		:			Tuple											=	model(**_A	)

							# verify the logits
							snake_case_		:			Optional[int]											=	torch.Size((1, 1000)	)
							self.assertEqual(outputs.logits.shape		,   _A	)

							snake_case_		:			Union[str, Any]											=	torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]	).to(_A	)

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






						@slow
						@require_accelerate
						def UpperCAmelCase_				(      self      :      Dict	)							->    int:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	ViTHybridImageProcessor.from_pretrained('google/vit-hybrid-base-bit-384'	)
							snake_case_		:			Union[str, Any]											=	ViTHybridForImageClassification.from_pretrained('google/vit-hybrid-base-bit-384'		,   device_map='auto'	)

							snake_case_		:			str											=	prepare_img()

							snake_case_		:			str											=	image_processor(images=_A		,   return_tensors='pt'	)
							snake_case_		:			Union[str, Any]											=	model(**_A	)
							snake_case_		:			str											=	outputs.logits
							# model predicts one of the 1000 ImageNet classes
							snake_case_		:			List[Any]											=	logits.argmax(-1	).item()

							self.assertTrue(model.config.idalabel[predicted_class_idx]		,   'tabby, tabby cat'	)








import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






						@property
						def UpperCAmelCase_				(      self      :      Any	)							->    Optional[Any]:







							"""simple docstring"""


							torch.manual_seed(0	)
							snake_case_		:			List[Any]											=	UNetaDModel(
							    block_out_channels=(32, 64)		,   layers_per_block=2		,   sample_size=32		,   in_channels=3		,   out_channels=3		,   down_block_types=('DownBlock2D', 'AttnDownBlock2D')		,   up_block_types=('AttnUpBlock2D', 'UpBlock2D')		,   )
							return model






						def UpperCAmelCase_				(      self      :      Optional[Any]	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			List[Any]											=	self.dummy_uncond_unet
							snake_case_		:			Optional[Any]											=	ScoreSdeVeScheduler()

							snake_case_		:			Tuple											=	ScoreSdeVePipeline(unet=_A		,   scheduler=_A	)
							sde_ve.to(_A	)
							sde_ve.set_progress_bar_config(disable=_A	)

							snake_case_		:			Optional[Any]											=	torch.manual_seed(0	)
							snake_case_		:			Tuple											=	sde_ve(num_inference_steps=2		,   output_type='numpy'		,   generator=_A	).images

							snake_case_		:			int											=	torch.manual_seed(0	)
							snake_case_		:			Optional[int]											=	sde_ve(num_inference_steps=2		,   output_type='numpy'		,   generator=_A		,   return_dict=_A	)[
							    0
							]

							snake_case_		:			List[str]											=	image[0, -3:, -3:, -1]
							snake_case_		:			Dict											=	image_from_tuple[0, -3:, -3:, -1]

							assert image.shape == (1, 32, 32, 3)
							snake_case_		:			List[Any]											=	np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]	)

							assert np.abs(image_slice.flatten() - expected_slice	).max() < 1E-2
							assert np.abs(image_from_tuple_slice.flatten() - expected_slice	).max() < 1E-2
@slow
@require_torch
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






						def UpperCAmelCase_				(      self      :      Dict	)							->    Dict:







							"""simple docstring"""


							snake_case_		:			Dict											=	'google/ncsnpp-church-256'
							snake_case_		:			List[Any]											=	UNetaDModel.from_pretrained(_A	)

							snake_case_		:			str											=	ScoreSdeVeScheduler.from_pretrained(_A	)

							snake_case_		:			Optional[Any]											=	ScoreSdeVePipeline(unet=_A		,   scheduler=_A	)
							sde_ve.to(_A	)
							sde_ve.set_progress_bar_config(disable=_A	)

							snake_case_		:			Any											=	torch.manual_seed(0	)
							snake_case_		:			Optional[int]											=	sde_ve(num_inference_steps=10		,   output_type='numpy'		,   generator=_A	).images

							snake_case_		:			Union[str, Any]											=	image[0, -3:, -3:, -1]

							assert image.shape == (1, 256, 256, 3)

							snake_case_		:			Dict											=	np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]	)

							assert np.abs(image_slice.flatten() - expected_slice	).max() < 1E-2

