




"""simple docstring"""






import inspect
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
lowercase__			=		"""src/transformers"""


# This is to make sure the transformers module imported is the one in the repo.
lowercase__			=		direct_transformers_import(PATH_TO_TRANSFORMERS)

lowercase__			=		transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
lowercase__			=		re.compile(R"""\[(.+?)\]\((https://huggingface\.co/.+?)\)""")


lowercase__			=		{
    """DecisionTransformerConfig""",
    """EncoderDecoderConfig""",
    """MusicgenConfig""",
    """RagConfig""",
    """SpeechEncoderDecoderConfig""",
    """TimmBackboneConfig""",
    """VisionEncoderDecoderConfig""",
    """VisionTextDualEncoderConfig""",
    """LlamaConfig""",
}







def 	_snake_case     (							lowercase__						):
				_lowerCamelCase	:     Optional[Any]       =       None

				# source code of `config_class`
				_lowerCamelCase	:     int       =       inspect.getsource(lowercase__						)
				_lowerCamelCase	:     List[str]       =       _re_checkpoint.findall(lowercase__						)

				# Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
				# For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
				for ckpt_name, ckpt_link in checkpoints:
								# allow the link to end with `/`
								if ckpt_link.endswith('/'						):
												_lowerCamelCase	:     List[str]       =       ckpt_link[:-1]

								# verify the checkpoint name corresponds to the checkpoint link
								_lowerCamelCase	:     Dict       =       f'''https://huggingface.co/{ckpt_name}'''
								if ckpt_link == ckpt_link_from_name:
												_lowerCamelCase	:     str       =       ckpt_name
												break

				return checkpoint







def 	_snake_case     (							):
				_lowerCamelCase	:     Any       =       []

				for config_class in list(CONFIG_MAPPING.values()						):
								# Skip deprecated models
								if "models.deprecated" in config_class.__module__:
												continue
								_lowerCamelCase	:     int       =       get_checkpoint_from_config_class(lowercase__						)

								_lowerCamelCase	:     str       =       config_class.__name__
								if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
												configs_without_checkpoint.append(lowercase__						)

				if len(lowercase__						) > 0:
								_lowerCamelCase	:     List[str]       =       '\n'.join(sorted(lowercase__						)						)
								raise ValueError(f'''The following configurations don\'t contain any valid checkpoint:\n{message}'''						)


if __name__ == "__main__":
			check_config_docstrings_have_checkpoints()





"""simple docstring"""






import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


lowercase__			=		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__			=		parser.parse_args()


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

lowercase__			=		"""path-to-your-trained-model"""
lowercase__			=		StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
			lowercase__			=		DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
lowercase__			=		pipe.to(device)

# to channels last
lowercase__			=		pipe.unet.to(memory_format=torch.channels_last)
lowercase__			=		pipe.vae.to(memory_format=torch.channels_last)
lowercase__			=		pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
			lowercase__			=		pipe.safety_checker.to(memory_format=torch.channels_last)

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

# compute
lowercase__			=		666
lowercase__			=		torch.Generator(device).manual_seed(seed)
lowercase__			=		{"""generator""": generator}
if args.steps is not None:
			lowercase__			=		args.steps

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

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