




'''simple docstring'''




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







			def __init__(			self       :						Union[str, Any]       ,    lowercase_       :						str       ,    lowercase_       :						int=13       ,    lowercase_       :						Union[str, Any]=64       ,    lowercase_       :						Tuple=2       ,    lowercase_       :						Dict=3       ,    lowercase_       :						Union[str, Any]=True       ,    lowercase_       :						List[str]=True       ,    lowercase_       :						Dict=32       ,    lowercase_       :						List[str]=5       ,    lowercase_       :						Union[str, Any]=4       ,    lowercase_       :						Tuple=37       ,    lowercase_       :						Optional[Any]="gelu"       ,    lowercase_       :						List[Any]=0.1       ,    lowercase_       :						List[Any]=0.1       ,    lowercase_       :						Optional[Any]=10       ,    lowercase_       :						int=0.02       ,    lowercase_       :						str=[1, 16, 4, 4]       ,    lowercase_       :						str=None       ,    ):
										lowercase_      :    int       		= parent
										lowercase_      :    str       		= batch_size
										lowercase_      :    Tuple       		= image_size
										lowercase_      :    List[Any]       		= patch_size
										lowercase_      :    Optional[Any]       		= num_channels
										lowercase_      :    List[str]       		= is_training
										lowercase_      :    List[Any]       		= use_labels
										lowercase_      :    Optional[Any]       		= hidden_size
										lowercase_      :    Any       		= num_hidden_layers
										lowercase_      :    List[Any]       		= num_attention_heads
										lowercase_      :    Any       		= intermediate_size
										lowercase_      :    Optional[int]       		= hidden_act
										lowercase_      :    List[str]       		= hidden_dropout_prob
										lowercase_      :    str       		= attention_probs_dropout_prob
										lowercase_      :    int       		= type_sequence_label_size
										lowercase_      :    Dict       		= initializer_range
										lowercase_      :    Tuple       		= scope
										lowercase_      :    Dict       		= 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
										lowercase_      :    Optional[int]       		= (self.image_size // 32) ** 2
										lowercase_      :    List[str]       		= num_patches + 1







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]					):
										lowercase_      :    Optional[Any]       		= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

										lowercase_      :    str       		= None
										if self.use_labels:
																	lowercase_      :    Union[str, Any]       		= ids_tensor([self.batch_size]       ,    self.type_sequence_label_size					)

										lowercase_      :    Dict       		= self.get_config()

										return config, pixel_values, labels







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    Union[str, Any]       		= {
										    """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 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						Dict       ,    lowercase_       :						List[Any]       ,    lowercase_       :						int					):
										lowercase_      :    Optional[int]       		= ViTHybridModel(config=A__					)
										model.to(A__					)
										model.eval()
										lowercase_      :    Tuple       		= model(A__					)
										self.parent.assertEqual(result.last_hidden_state.shape       ,    (self.batch_size, self.seq_length, self.hidden_size)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]       ,    lowercase_       :						List[str]       ,    lowercase_       :						str       ,    lowercase_       :						Optional[int]					):
										lowercase_      :    Dict       		= self.type_sequence_label_size
										lowercase_      :    Dict       		= ViTHybridForImageClassification(A__					)
										model.to(A__					)
										model.eval()
										lowercase_      :    Any       		= model(A__       ,    labels=A__					)
										self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.type_sequence_label_size)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    List[str]       		= self.prepare_config_and_inputs()
										lowercase_      :    Union[str, Any]       		= config_and_inputs
										lowercase_      :    str       		= {"""pixel_values""": pixel_values}
										return config, inputs_dict





@require_torch
class 					__magic_name__		(					_UpperCAmelCase,  _UpperCAmelCase,  unittest.TestCase):
			UpperCamelCase__       = (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
			UpperCamelCase__       = (
			    {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification}
			    if is_torch_available()
			    else {}
			)
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = False







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										lowercase_      :    Union[str, Any]       		= ViTHybridModelTester(self					)
										lowercase_      :    List[Any]       		= ConfigTester(self       ,    config_class=A__       ,    has_text_modality=A__       ,    hidden_size=37					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										self.config_tester.run_common_tests()







			@unittest.skip(reason="""ViT does not use inputs_embeds"""					)
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										pass







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										lowercase_      :    List[str]       		= self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																	lowercase_      :    int       		= model_class(A__					)
																	self.assertIsInstance(model.get_input_embeddings()       ,    (nn.Module)					)
																	lowercase_      :    int       		= model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(A__       ,    nn.Linear					)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple					):
										lowercase_      :    Tuple       		= self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowercase_      :    Union[str, Any]       		= ["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]       ,    A__					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										lowercase_      :    List[str]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*A__					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										lowercase_      :    List[str]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*A__					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]					):
										lowercase_      :    Tuple       		= self.model_tester.prepare_config_and_inputs_for_common()

										lowercase_      :    Any       		= _config_zero_init(A__					)
										for model_class in self.all_model_classes:
																	lowercase_      :    List[str]       		= model_class(config=A__					)
																	# Skip the check for the backbone
																	for name, module in model.named_modules():
																								if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
																															lowercase_      :    Tuple       		= [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 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_      :    Union[str, Any]       		= ViTHybridModel.from_pretrained(A__					)
																	self.assertIsNotNone(A__					)




def 							lowerCamelCase			(   )    ->   Any:
							lowercase_      :    Tuple       		= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""				)
							return image





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







			@cached_property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										return (
										    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)
										    if is_vision_available()
										    else None
										)







			@slow
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    str       		= ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]					).to(
										    A__					)

										lowercase_      :    Optional[Any]       		= self.default_image_processor
										lowercase_      :    Tuple       		= prepare_img()
										lowercase_      :    Optional[int]       		= image_processor(images=A__       ,    return_tensors="""pt"""					).to(A__					)

										# forward pass
										with torch.no_grad():
																	lowercase_      :    List[Any]       		= model(**A__					)

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

										lowercase_      :    List[Any]       		= torch.tensor([-1.90_90, -0.49_93, -0.23_89]					).to(A__					)

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







			@slow
			@require_accelerate
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Tuple					):
										lowercase_      :    str       		= ViTHybridImageProcessor.from_pretrained("""google/vit-hybrid-base-bit-384"""					)
										lowercase_      :    Tuple       		= ViTHybridForImageClassification.from_pretrained("""google/vit-hybrid-base-bit-384"""       ,    device_map="""auto"""					)

										lowercase_      :    Optional[Any]       		= prepare_img()

										lowercase_      :    Union[str, Any]       		= image_processor(images=A__       ,    return_tensors="""pt"""					)
										lowercase_      :    Union[str, Any]       		= model(**A__					)
										lowercase_      :    Tuple       		= outputs.logits
										# model predicts one of the 1000 ImageNet classes
										lowercase_      :    Union[str, Any]       		= logits.argmax(-1					).item()

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






'''simple docstring'''




import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_lowercase		:				Optional[Any]	       =		argparse.ArgumentParser("Stable Diffusion script with intel optimization", add_help=False)
parser.add_argument("--dpm", action="store_true", help="Enable DPMSolver or not")
parser.add_argument("--steps", default=None, type=int, help="Num inference steps")
_lowercase		:				Dict	       =		parser.parse_args()


_lowercase		:				Dict	       =		"cpu"
_lowercase		:				str	       =		"a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings"

_lowercase		:				Any	       =		"path-to-your-trained-model"
_lowercase		:				str	       =		StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
						_lowercase		:				Optional[int]	       =		DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
_lowercase		:				Any	       =		pipe.to(device)

# to channels last
_lowercase		:				Union[str, Any]	       =		pipe.unet.to(memory_format=torch.channels_last)
_lowercase		:				List[Any]	       =		pipe.vae.to(memory_format=torch.channels_last)
_lowercase		:				Union[str, Any]	       =		pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
						_lowercase		:				Tuple	       =		pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
_lowercase		:				int	       =		torch.randn(2, 4, 64, 64)
_lowercase		:				int	       =		torch.rand(1) * 999
_lowercase		:				Union[str, Any]	       =		torch.randn(2, 77, 768)
_lowercase		:				Optional[Any]	       =		(sample, timestep, encoder_hidden_status)
try:
						_lowercase		:				int	       =		ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
						_lowercase		:				Optional[int]	       =		ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
_lowercase		:				List[Any]	       =		ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
_lowercase		:				str	       =		ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
						_lowercase		:				int	       =		ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
_lowercase		:				int	       =		666
_lowercase		:				Any	       =		torch.Generator(device).manual_seed(seed)
_lowercase		:				int	       =		{"generator": generator}
if args.steps is not None:
						_lowercase		:				Optional[int]	       =		args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
						_lowercase		:				List[Any]	       =		pipe(prompt, **generate_kwargs).images[0]

# save image
image.save("generated.png")





