



"""simple docstring"""





import argparse

import torch

from transformers import BertForMaskedLM


if __name__ == "__main__":
					__lowerCAmelCase       :					int      					=					argparse.ArgumentParser(
					    description=(
					        '''Extraction some layers of the full BertForMaskedLM or RObertaForMaskedLM for Transfer Learned'''
					        ''' Distillation'''
					    )
					)
					parser.add_argument('''--model_type''', default='''bert''', choices=['''bert'''])
					parser.add_argument('''--model_name''', default='''bert-base-uncased''', type=str)
					parser.add_argument('''--dump_checkpoint''', default='''serialization_dir/tf_bert-base-uncased_0247911.pth''', type=str)
					parser.add_argument('''--vocab_transform''', action='''store_true''')
					__lowerCAmelCase       :					List[str]      					=					parser.parse_args()

					if args.model_type == "bert":
										__lowerCAmelCase       :					Tuple      					=					BertForMaskedLM.from_pretrained(args.model_name)
										__lowerCAmelCase       :					Optional[Any]      					=					'''bert'''
					else:
										raise ValueError('''args.model_type should be "bert".''')

					__lowerCAmelCase       :					List[str]      					=					model.state_dict()
					__lowerCAmelCase       :					Any      					=					{}

					for w in ["word_embeddings", "position_embeddings"]:
										__lowerCAmelCase       :					List[Any]      					=					state_dict[F'''{prefix}.embeddings.{w}.weight''']
					for w in ["weight", "bias"]:
										__lowerCAmelCase       :					Any      					=					state_dict[F'''{prefix}.embeddings.LayerNorm.{w}''']

					__lowerCAmelCase       :					Dict      					=					0
					for teacher_idx in [0, 2, 4, 7, 9, 11]:
										for w in ["weight", "bias"]:
															__lowerCAmelCase       :					int      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.attention.self.query.{w}'''
															]
															__lowerCAmelCase       :					List[str]      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.attention.self.key.{w}'''
															]
															__lowerCAmelCase       :					Optional[Any]      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.attention.self.value.{w}'''
															]

															__lowerCAmelCase       :					str      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.attention.output.dense.{w}'''
															]
															__lowerCAmelCase       :					Tuple      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.attention.output.LayerNorm.{w}'''
															]

															__lowerCAmelCase       :					Optional[Any]      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.intermediate.dense.{w}'''
															]
															__lowerCAmelCase       :					Tuple      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.output.dense.{w}'''
															]
															__lowerCAmelCase       :					Any      					=					state_dict[
															    F'''{prefix}.encoder.layer.{teacher_idx}.output.LayerNorm.{w}'''
															]
										std_idx += 1

					__lowerCAmelCase       :					List[str]      					=					state_dict['''cls.predictions.decoder.weight''']
					__lowerCAmelCase       :					Tuple      					=					state_dict['''cls.predictions.bias''']
					if args.vocab_transform:
										for w in ["weight", "bias"]:
															__lowerCAmelCase       :					List[Any]      					=					state_dict[F'''cls.predictions.transform.dense.{w}''']
															__lowerCAmelCase       :					List[str]      					=					state_dict[F'''cls.predictions.transform.LayerNorm.{w}''']

					print(F'''N layers selected for distillation: {std_idx}''')
					print(F'''Number of params transferred for distillation: {len(compressed_sd.keys())}''')

					print(F'''Save transferred checkpoint to {args.dump_checkpoint}.''')
					torch.save(compressed_sd, args.dump_checkpoint)






"""simple docstring"""





import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


__lowerCAmelCase       :					Optional[int]      					=					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       :					Optional[Any]      					=					parser.parse_args()


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

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

# to channels last
__lowerCAmelCase       :					Optional[Any]      					=					pipe.unet.to(memory_format=torch.channels_last)
__lowerCAmelCase       :					List[str]      					=					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       :					Dict      					=					pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
__lowerCAmelCase       :					Tuple      					=					torch.randn(2, 4, 64, 64)
__lowerCAmelCase       :					Any      					=					torch.rand(1) * 999
__lowerCAmelCase       :					List[str]      					=					torch.randn(2, 77, 768)
__lowerCAmelCase       :					Optional[int]      					=					(sample, timestep, encoder_hidden_status)
try:
					__lowerCAmelCase       :					List[Any]      					=					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       :					Any      					=					ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
__lowerCAmelCase       :					int      					=					ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
					__lowerCAmelCase       :					Union[str, Any]      					=					ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

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

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

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

