





import argparse
import json
import os

import fairseq
import torch
from torch import nn

from transformers import (
    SpeechaTextaConfig,
    SpeechaTextaForCausalLM,
    SpeechaTextaTokenizer,
    SpeechEncoderDecoderConfig,
    SpeechEncoderDecoderModel,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaModel,
    logging,
)


logging.set_verbosity_info()
UpperCAmelCase_					=					logging.get_logger(__name__)

UpperCAmelCase_					=					{
    """post_extract_proj""": """feature_projection.projection""",
    """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""",
    """self_attn.k_proj""": """encoder.layers.*.attention.k_proj""",
    """self_attn.v_proj""": """encoder.layers.*.attention.v_proj""",
    """self_attn.q_proj""": """encoder.layers.*.attention.q_proj""",
    """self_attn.out_proj""": """encoder.layers.*.attention.out_proj""",
    """self_attn_layer_norm""": """encoder.layers.*.layer_norm""",
    """fc1""": """encoder.layers.*.feed_forward.intermediate_dense""",
    """fc2""": """encoder.layers.*.feed_forward.output_dense""",
    """final_layer_norm""": """encoder.layers.*.final_layer_norm""",
    """encoder.layer_norm""": """encoder.layer_norm""",
    """w2v_model.layer_norm""": """feature_projection.layer_norm""",
    """quantizer.weight_proj""": """quantizer.weight_proj""",
    """quantizer.vars""": """quantizer.codevectors""",
    """project_q""": """project_q""",
    """final_proj""": """project_hid""",
    """w2v_encoder.proj""": """lm_head""",
    """mask_emb""": """masked_spec_embed""",
}
UpperCAmelCase_					=					[
    """lm_head""",
    """quantizer.weight_proj""",
    """quantizer.codevectors""",
    """project_q""",
    """project_hid""",
]




def 						lowerCAmelCase_       (    __UpperCAmelCase:     List[Any]						,  __UpperCAmelCase:     List[Any]						,  __UpperCAmelCase:     List[str]						,  __UpperCAmelCase:     Tuple						,  __UpperCAmelCase:     Optional[int] ) ->      int:
    for attribute in key.split('''.''' ):
        UpperCamelCase__				:							Dict			  = getattr(_lowercase						,  _lowercase )

    if weight_type is not None:
        UpperCamelCase__				:							List[str]			  = getattr(_lowercase						,  _lowercase ).shape
    else:
        UpperCamelCase__				:							Optional[Any]			  = hf_pointer.shape

    assert hf_shape == value.shape, (
        f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be"
        f" {value.shape} for {full_name}"
    )

    if weight_type == "weight":
        UpperCamelCase__				:							Union[str, Any]			  = value
    elif weight_type == "weight_g":
        UpperCamelCase__				:							Dict			  = value
    elif weight_type == "weight_v":
        UpperCamelCase__				:							Tuple			  = value
    elif weight_type == "bias":
        UpperCamelCase__				:							Union[str, Any]			  = value
    else:
        UpperCamelCase__				:							Dict			  = value

    logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." )




def 						lowerCAmelCase_       (    __UpperCAmelCase:     Optional[Any]						,  __UpperCAmelCase:     Dict ) ->      Tuple:
    UpperCamelCase__				:							Any			  = []
    UpperCamelCase__				:							int			  = fairseq_model.state_dict()

    UpperCamelCase__				:							Tuple			  = hf_model.feature_extractor

    # if encoder has different dim to decoder -> use proj_weight
    UpperCamelCase__				:							Union[str, Any]			  = None

    for name, value in fairseq_dict.items():
        UpperCamelCase__				:							List[str]			  = False
        if "conv_layers" in name:
            load_conv_layer(
                _lowercase						,  _lowercase						,  _lowercase						,  _lowercase						,  hf_model.config.feat_extract_norm == '''group'''						,  )
            UpperCamelCase__				:							List[Any]			  = True
        elif name.split('''.''' )[0] == "proj":
            UpperCamelCase__				:							Tuple			  = fairseq_model.proj
            UpperCamelCase__				:							Union[str, Any]			  = True
        else:
            for key, mapped_key in MAPPING.items():
                if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]:
                    UpperCamelCase__				:							Optional[int]			  = True
                    if "*" in mapped_key:
                        UpperCamelCase__				:							Optional[int]			  = name.split(_lowercase )[0].split('''.''' )[-2]
                        UpperCamelCase__				:							Any			  = mapped_key.replace('''*'''						,  _lowercase )
                    if "weight_g" in name:
                        UpperCamelCase__				:							List[str]			  = 'weight_g'
                    elif "weight_v" in name:
                        UpperCamelCase__				:							int			  = 'weight_v'
                    elif "bias" in name:
                        UpperCamelCase__				:							List[str]			  = 'bias'
                    elif "weight" in name:
                        UpperCamelCase__				:							Tuple			  = 'weight'
                    else:
                        UpperCamelCase__				:							Optional[int]			  = None
                    set_recursively(_lowercase						,  _lowercase						,  _lowercase						,  _lowercase						,  _lowercase )
                continue
        if not is_used:
            unused_weights.append(_lowercase )

    logger.warning(f"Unused weights: {unused_weights}" )

    return proj_weight




def 						lowerCAmelCase_       (    __UpperCAmelCase:     List[str]						,  __UpperCAmelCase:     str						,  __UpperCAmelCase:     str						,  __UpperCAmelCase:     str						,  __UpperCAmelCase:     Any ) ->      str:
    UpperCamelCase__				:							Optional[int]			  = full_name.split('''conv_layers.''' )[-1]
    UpperCamelCase__				:							int			  = name.split('''.''' )
    UpperCamelCase__				:							int			  = int(items[0] )
    UpperCamelCase__				:							str			  = int(items[1] )

    if type_id == 0:
        if "bias" in name:
            assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
                f"{full_name} has size {value.shape}, but"
                f" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."
            )
            UpperCamelCase__				:							Tuple			  = value
            logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." )
        elif "weight" in name:
            assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
                f"{full_name} has size {value.shape}, but"
                f" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."
            )
            UpperCamelCase__				:							Tuple			  = value
            logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}." )
    elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
        if "bias" in name:
            assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
                f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was"
                " found."
            )
            UpperCamelCase__				:							str			  = value
            logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." )
        elif "weight" in name:
            assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
                f"{full_name} has size {value.shape}, but"
                f" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found."
            )
            UpperCamelCase__				:							str			  = value
            logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." )
    else:
        unused_weights.append(_lowercase )




def 						lowerCAmelCase_       (    __UpperCAmelCase:     Any ) ->      List[Any]:
    UpperCamelCase__				:							Tuple			  = emb.weight.shape
    UpperCamelCase__				:							Tuple			  = nn.Linear(_lowercase						,  _lowercase						,  bias=_lowercase )
    UpperCamelCase__				:							Optional[int]			  = emb.weight.data
    return lin_layer




def 						lowerCAmelCase_       (    __UpperCAmelCase:     Tuple ) ->      Optional[Any]:
    with open(_lowercase						,  '''r'''						,  encoding='''utf-8''' ) as f:
        UpperCamelCase__				:							Optional[Any]			  = f.readlines()
        UpperCamelCase__				:							str			  = [line.split(''' ''' )[0] for line in lines]

    UpperCamelCase__				:							List[str]			  = len(_lowercase )

    UpperCamelCase__				:							Union[str, Any]			  = {
        '<s>': 0,
        '<pad>': 1,
        '</s>': 2,
        '<unk>': 3,
    }

    vocab_dict.update(dict(zip(_lowercase						,  range(4						,  num_words + 4 ) ) ) )
    return vocab_dict




@torch.no_grad()
def 						lowerCAmelCase_       (    __UpperCAmelCase:     Dict						,  __UpperCAmelCase:     Optional[Any]						,  __UpperCAmelCase:     Tuple						,  __UpperCAmelCase:     Dict						,  __UpperCAmelCase:     Any						,  __UpperCAmelCase:     Optional[Any]						,  __UpperCAmelCase:     Optional[int]						,  ) ->      Union[str, Any]:
    UpperCamelCase__				:							List[str]			  = WavaVecaConfig.from_pretrained(_lowercase )
    UpperCamelCase__				:							List[str]			  = SpeechaTextaConfig.from_pretrained(
        _lowercase						,  vocab_size=_lowercase						,  decoder_layers=_lowercase						,  do_stable_layer_norm=_lowercase )

    UpperCamelCase__				:							List[str]			  = WavaVecaFeatureExtractor(
        feature_size=1						,  sampling_rate=1_6000						,  padding_value=0						,  do_normalize=_lowercase						,  return_attention_mask=_lowercase						,  )

    UpperCamelCase__				:							Optional[Any]			  = fairseq.checkpoint_utils.load_model_ensemble_and_task(
        [checkpoint_path]						,  arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} )
    UpperCamelCase__				:							Tuple			  = model[0].eval()

    # set weights for wav2vec2 encoder
    UpperCamelCase__				:							int			  = WavaVecaModel(_lowercase )
    UpperCamelCase__				:							Tuple			  = recursively_load_weights_wavaveca(model.encoder						,  _lowercase )

    UpperCamelCase__				:							Tuple			  = SpeechaTextaForCausalLM(_lowercase )
    UpperCamelCase__				:							Tuple			  = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict()						,  strict=_lowercase )

    # set output linear layer
    unexpected_keys.remove('''embed_out''' )
    UpperCamelCase__				:							Dict			  = nn.Parameter(model.decoder.embed_out.detach() )

    # layer norm is init to identity matrix so leaving it is fine
    logger.warning(f"The following keys are missing when loading the decoder weights: {missing_keys}" )
    logger.warning(f"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" )

    UpperCamelCase__				:							Tuple			  = SpeechEncoderDecoderModel(encoder=_lowercase						,  decoder=_lowercase )
    UpperCamelCase__				:							Any			  = False

    # add projection layer
    UpperCamelCase__				:							Union[str, Any]			  = nn.Parameter(projection_layer.weight )
    UpperCamelCase__				:							List[str]			  = nn.Parameter(projection_layer.bias )

    UpperCamelCase__				:							Dict			  = create_vocab_dict(_lowercase )

    with open(os.path.join(_lowercase						,  '''vocab.json''' )						,  '''w''' ) as fp:
        json.dump(_lowercase						,  _lowercase )

    UpperCamelCase__				:							int			  = SpeechaTextaTokenizer(os.path.join(_lowercase						,  '''vocab.json''' ) )
    tokenizer.save_pretrained(_lowercase )

    UpperCamelCase__				:							Optional[int]			  = hf_wavavec.config.to_dict()
    UpperCamelCase__				:							str			  = tokenizer.pad_token_id
    UpperCamelCase__				:							Any			  = tokenizer.bos_token_id
    UpperCamelCase__				:							Union[str, Any]			  = tokenizer.eos_token_id
    UpperCamelCase__				:							Dict			  = 'speech_to_text_2'
    UpperCamelCase__				:							List[Any]			  = 'wav2vec2'

    UpperCamelCase__				:							Dict			  = SpeechEncoderDecoderConfig.from_dict(_lowercase )

    hf_wavavec.save_pretrained(_lowercase )
    feature_extractor.save_pretrained(_lowercase )


if __name__ == "__main__":
  UpperCAmelCase_					=					argparse.ArgumentParser()
  parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
  parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint')
  parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model')
  parser.add_argument(
      '--encoder_config_path',
      default='facebook/wav2vec2-large-lv60',
      type=str,
      help='Path to hf encoder wav2vec2 checkpoint config',
  )
  parser.add_argument(
      '--decoder_config_path',
      default='facebook/s2t-small-mustc-en-fr-st',
      type=str,
      help='Path to hf decoder s2t checkpoint config',
  )
  parser.add_argument('--vocab_size', default=1_0224, type=int, help='Vocab size of decoder')
  parser.add_argument('--num_decoder_layers', default=7, type=int, help='Number of decoder layers')

  UpperCAmelCase_					=					parser.parse_args()
  convert_wavaveca_checkpoint(
      args.checkpoint_path,
      args.pytorch_dump_folder_path,
      args.dict_path,
      encoder_config_path=args.encoder_config_path,
      decoder_config_path=args.decoder_config_path,
      vocab_size=args.vocab_size,
      num_decoder_layers=args.num_decoder_layers,
  )








import argparse
import math
import os

import torch
from neural_compressor.utils.pytorch import load
from PIL import Image
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel


def       SCREAMING_SNAKE_CASE__  (       )   ->							int:




		'''simple docstring'''
		lowercase__					:      str      =		argparse.ArgumentParser()
		parser.add_argument(
		    '-m'   ,    '--pretrained_model_name_or_path'   ,    type=_lowercase   ,    default=_lowercase   ,    required=_lowercase   ,    help='Path to pretrained model or model identifier from huggingface.co/models.'   ,    )
		parser.add_argument(
		    '-c'   ,    '--caption'   ,    type=_lowercase   ,    default='robotic cat with wings'   ,    help='Text used to generate images.'   ,    )
		parser.add_argument(
		    '-n'   ,    '--images_num'   ,    type=_lowercase   ,    default=4   ,    help='How much images to generate.'   ,    )
		parser.add_argument(
		    '-s'   ,    '--seed'   ,    type=_lowercase   ,    default=42   ,    help='Seed for random process.'   ,    )
		parser.add_argument(
		    '-ci'   ,    '--cuda_id'   ,    type=_lowercase   ,    default=0   ,    help='cuda_id.'   ,    )
		lowercase__					:      Any      =		parser.parse_args()
		return args


def       SCREAMING_SNAKE_CASE__  (       _lowercase     : Union[str, Any]   ,    _lowercase     : Optional[int]   ,    _lowercase     : Union[str, Any]			)   ->							str:




		'''simple docstring'''
		if not len(_lowercase			) == rows * cols:
				raise ValueError('The specified number of rows and columns are not correct.'			)

		lowercase__   ,				lowercase__					:      Any      =		imgs[0].size
		lowercase__					:      int      =		Image.new('RGB'   ,    size=(cols * w, rows * h)			)
		lowercase__   ,				lowercase__					:      Any      =		grid.size

		for i, img in enumerate(_lowercase			):
				grid.paste(_lowercase   ,    box=(i % cols * w, i // cols * h)			)
		return grid


def       SCREAMING_SNAKE_CASE__  (       _lowercase     : Optional[int]   ,    _lowercase     : Tuple="robotic cat with wings"   ,    _lowercase     : Optional[Any]=7.5   ,    _lowercase     : int=50   ,    _lowercase     : str=1   ,    _lowercase     : Any=42   ,    )   ->							str:




		'''simple docstring'''
		lowercase__					:      int      =		torch.Generator(pipeline.device			).manual_seed(_lowercase			)
		lowercase__					:      Any      =		pipeline(
		    _lowercase   ,    guidance_scale=_lowercase   ,    num_inference_steps=_lowercase   ,    generator=_lowercase   ,    num_images_per_prompt=_lowercase   ,    ).images
		lowercase__					:      Dict      =		int(math.sqrt(_lowercase			)			)
		lowercase__					:      int      =		image_grid(_lowercase   ,    rows=_rows   ,    cols=num_images_per_prompt // _rows			)
		return grid, images


__UpperCamelCase:	int = parse_args()
# Load models and create wrapper for stable diffusion
__UpperCamelCase:	Optional[int] = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="""tokenizer""")
__UpperCamelCase:	List[str] = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="""text_encoder""")
__UpperCamelCase:	int = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder="""vae""")
__UpperCamelCase:	int = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="""unet""")

__UpperCamelCase:	Tuple = StableDiffusionPipeline.from_pretrained(
    args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer
)
__UpperCamelCase:	List[str] = lambda images, clip_input: (images, False)
if os.path.exists(os.path.join(args.pretrained_model_name_or_path, """best_model.pt""")):
			__UpperCamelCase:	Dict = load(args.pretrained_model_name_or_path, model=unet)
			unet.eval()
			setattr(pipeline, """unet""", unet)
else:
			__UpperCamelCase:	Dict = unet.to(torch.device("""cuda""", args.cuda_id))
__UpperCamelCase:	Optional[int] = pipeline.to(unet.device)
__UpperCamelCase,	__UpperCamelCase:	Union[str, Any] = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed)
grid.save(os.path.join(args.pretrained_model_name_or_path, """{}.png""".format("""_""".join(args.caption.split()))))
__UpperCamelCase:	List[str] = os.path.join(args.pretrained_model_name_or_path, """_""".join(args.caption.split()))
os.makedirs(dirname, exist_ok=True)
for idx, image in enumerate(images):
			image.save(os.path.join(dirname, """{}.png""".format(idx + 1)))


