





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowerCAmelCase				:   str						       =						logging.get_logger(__name__)


class        __magic_name__	(				lowerCamelCase__     ,					lowerCamelCase__				):





     """simple docstring"""



     __UpperCamelCase            =  '''maskformer-swin'''

     __UpperCamelCase            =  {
         '''num_attention_heads''': '''num_heads''',
         '''num_hidden_layers''': '''num_layers''',
     }



     def __init__(				self							:Union[str, Any]      ,   snake_case							:int=224      ,   snake_case							:Union[str, Any]=4      ,   snake_case							:List[str]=3      ,   snake_case							:Optional[int]=96      ,   snake_case							:Any=[2, 2, 6, 2]      ,   snake_case							:List[str]=[3, 6, 12, 24]      ,   snake_case							:List[str]=7      ,   snake_case							:Optional[Any]=4.0      ,   snake_case							:Optional[int]=True      ,   snake_case							:int=0.0      ,   snake_case							:Union[str, Any]=0.0      ,   snake_case							:Tuple=0.1      ,   snake_case							:Any="gelu"      ,   snake_case							:Optional[Any]=False      ,   snake_case							:Tuple=0.02      ,   snake_case							:str=1e-5      ,   snake_case							:Tuple=None      ,   snake_case							:Optional[Any]=None      ,   **snake_case							:List[str]      ,   ):





        '''simple docstring'''




        super().__init__(**snake_case	)

        A_       :					Any        =      image_size
        A_       :					Union[str, Any]        =      patch_size
        A_       :					Union[str, Any]        =      num_channels
        A_       :					Optional[Any]        =      embed_dim
        A_       :					Optional[Any]        =      depths
        A_       :					List[Any]        =      len(snake_case	)
        A_       :					str        =      num_heads
        A_       :					Union[str, Any]        =      window_size
        A_       :					Dict        =      mlp_ratio
        A_       :					str        =      qkv_bias
        A_       :					Tuple        =      hidden_dropout_prob
        A_       :					Union[str, Any]        =      attention_probs_dropout_prob
        A_       :					Dict        =      drop_path_rate
        A_       :					int        =      hidden_act
        A_       :					int        =      use_absolute_embeddings
        A_       :					Tuple        =      layer_norm_eps
        A_       :					Optional[Any]        =      initializer_range
        # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
        # this indicates the channel dimension after the last stage of the model
        A_       :					Dict        =      int(embed_dim * 2 ** (len(snake_case	) - 1)	)
        A_       :					Any        =      ["stem"] + [f"stage{idx}" for idx in range(1      ,   len(snake_case	) + 1	)]
        A_   ,    A_       :					Dict        =      get_aligned_output_features_output_indices(
            out_features=snake_case      ,   out_indices=snake_case      ,   stage_names=self.stage_names	)







import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_lowerCAmelCase				:   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''')
_lowerCAmelCase				:   Tuple						       =						parser.parse_args()


_lowerCAmelCase				:   Any						       =						'''cpu'''
_lowerCAmelCase				:   Dict						       =						'''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'''

_lowerCAmelCase				:   Union[str, Any]						       =						'''path-to-your-trained-model'''
_lowerCAmelCase				:   Tuple						       =						StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
 _lowerCAmelCase				:   List[str]						       =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
_lowerCAmelCase				:   List[Any]						       =						pipe.to(device)

# to channels last
_lowerCAmelCase				:   Any						       =						pipe.unet.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   int						       =						pipe.vae.to(memory_format=torch.channels_last)
_lowerCAmelCase				:   Optional[Any]						       =						pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
 _lowerCAmelCase				:   Optional[int]						       =						pipe.safety_checker.to(memory_format=torch.channels_last)

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

# compute
_lowerCAmelCase				:   Dict						       =						666
_lowerCAmelCase				:   Tuple						       =						torch.Generator(device).manual_seed(seed)
_lowerCAmelCase				:   Tuple						       =						{'''generator''': generator}
if args.steps is not None:
 _lowerCAmelCase				:   Tuple						       =						args.steps

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

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