
"""simple docstring"""



import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


UpperCamelCase__          =	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')
UpperCamelCase__          =	parser.parse_args()


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

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

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

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

# compute
UpperCamelCase__          =	6_66
UpperCamelCase__          =	torch.Generator(device).manual_seed(seed)
UpperCamelCase__          =	{'generator': generator}
if args.steps is not None:
    UpperCamelCase__          =	args.steps

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

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



"""simple docstring"""



import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration
def 					lowerCamelCase   (     _snake_case ):
    UpperCAmelCase__	:      int     							=					[
        'encoder.version',
        'decoder.version',
        'model.encoder.version',
        'model.decoder.version',
        'decoder.output_projection.weight',
        '_float_tensor',
        'encoder.embed_positions._float_tensor',
        'decoder.embed_positions._float_tensor',
    ]
    for k in ignore_keys:
        state_dict.pop(_snake_case    ,_snake_case )
def 					lowerCamelCase   (     _snake_case ):
    UpperCAmelCase__							,							UpperCAmelCase__	:      Optional[Any]     							=					emb.weight.shape
    UpperCAmelCase__	:      List[str]     							=					nn.Linear(_snake_case    ,_snake_case    ,bias=_snake_case )
    UpperCAmelCase__	:      Any     							=					emb.weight.data
    return lin_layer
def 					lowerCamelCase   (     _snake_case ):
    UpperCAmelCase__	:      Optional[int]     							=					torch.load(_snake_case    ,map_location='cpu' )
    UpperCAmelCase__	:      Any     							=					mam_aaa['args'] or mam_aaa['cfg']['model']
    UpperCAmelCase__	:      Optional[int]     							=					mam_aaa['model']
    remove_ignore_keys_(_snake_case )
    UpperCAmelCase__	:      Optional[int]     							=					state_dict['encoder.embed_tokens.weight'].shape[0]

    UpperCAmelCase__	:      List[str]     							=					MaMaaaConfig(
        vocab_size=_snake_case    ,max_position_embeddings=1024    ,encoder_layers=args.encoder_layers    ,decoder_layers=args.decoder_layers    ,encoder_attention_heads=args.encoder_attention_heads    ,decoder_attention_heads=args.decoder_attention_heads    ,encoder_ffn_dim=args.encoder_ffn_embed_dim    ,decoder_ffn_dim=args.decoder_ffn_embed_dim    ,d_model=args.encoder_embed_dim    ,encoder_layerdrop=args.encoder_layerdrop    ,decoder_layerdrop=args.decoder_layerdrop    ,dropout=args.dropout    ,attention_dropout=args.attention_dropout    ,activation_dropout=args.activation_dropout    ,activation_function='relu'    ,)

    UpperCAmelCase__	:      Optional[int]     							=					state_dict['decoder.embed_tokens.weight']
    UpperCAmelCase__	:      Union[str, Any]     							=					MaMaaaForConditionalGeneration(_snake_case )
    model.model.load_state_dict(_snake_case    ,strict=_snake_case )
    UpperCAmelCase__	:      Optional[Any]     							=					make_linear_from_emb(model.model.shared )

    return model


if __name__ == "__main__":
    UpperCamelCase__          =	argparse.ArgumentParser()
    # Required parameters
    parser.add_argument('fairseq_path', type=str, help='path to a model.pt on local filesystem.')
    parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
    UpperCamelCase__          =	parser.parse_args()
    UpperCamelCase__          =	convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
    model.save_pretrained(args.pytorch_dump_folder_path)

