code: 
  
  
  
  '''simple docstring'''
  
  
  
  from logging import getLogger
  from typing import Any, Callable, List, Optional, Union
  
  import numpy as np
  import PIL
  import torch
  
  from ...schedulers import DDPMScheduler
  from ..onnx_utils import ORT_TO_NP_TYPE, OnnxRuntimeModel
  from ..pipeline_utils import ImagePipelineOutput
  from . import StableDiffusionUpscalePipeline
  
  
  a__						:			int										=			getLogger(__name__)
  
  
  a__						:			Union[str, Any]										=			4
  a__						:			str										=			7
  
  a__						:			Optional[Any]										=			{
      'float16': torch.floataa,
      'float32': torch.floataa,
  }
  
  
  
  
  
  
  
  def _lowercase   (  __A       ):
  
  
  
   '''simple docstring'''
  
  
  
   if isinstance(__A							,torch.Tensor       ):
    return image
   elif isinstance(__A							,PIL.Image.Image       ):
    __UpperCamelCase				      =  [image]
  
   if isinstance(image[0]							,PIL.Image.Image       ):
    __UpperCamelCase      ,		__UpperCamelCase				      =  image[0].size
    __UpperCamelCase      ,		__UpperCamelCase				      =  (x - x % 64 for x in (w, h))  # resize to integer multiple of 32
  
    __UpperCamelCase				      =  [np.array(i.resize((w, h)       )       )[None, :] for i in image]
    __UpperCamelCase				      =  np.concatenate(__A							,axis=0       )
    __UpperCamelCase				      =  np.array(__A       ).astype(np.floataa       ) / 255.0
    __UpperCamelCase				      =  image.transpose(0							,3							,1							,2       )
    __UpperCamelCase				      =  2.0 * image - 1.0
    __UpperCamelCase				      =  torch.from_numpy(__A       )
   elif isinstance(image[0]							,torch.Tensor       ):
    __UpperCamelCase				      =  torch.cat(__A							,dim=0       )
  
   return image
  
  
  
  
  
  
  class        UpperCAmelCase__  (    UpperCAmelCase_):
  
  
  
      def __init__(  self		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase = 3_5_0		,	)  ->							Any:
       super().__init__(
           vae=lowercase		,	text_encoder=lowercase		,	tokenizer=lowercase		,	unet=lowercase		,	low_res_scheduler=lowercase		,	scheduler=lowercase		,	safety_checker=lowercase		,	feature_extractor=lowercase		,	watermarker=lowercase		,	max_noise_level=lowercase		,	)
  
  
  
      def __call__(  self		,	lowercase		,	lowercase		,	lowercase = 7_5		,	lowercase = 9.0		,	lowercase = 2_0		,	lowercase = None		,	lowercase = 1		,	lowercase = 0.0		,	lowercase = None		,	lowercase = None		,	lowercase = None		,	lowercase = None		,	lowercase = "pil"		,	lowercase = True		,	lowercase = None		,	lowercase = 1		,	)  ->							Dict:
       self.check_inputs(lowercase		,	lowercase		,	lowercase		,	lowercase			)
  
       # 2. Define call parameters
       if prompt is not None and isinstance(lowercase		,	lowercase			):
        __UpperCamelCase				      =  1
       elif prompt is not None and isinstance(lowercase		,	lowercase			):
        __UpperCamelCase				      =  len(lowercase			)
       else:
        __UpperCamelCase				      =  prompt_embeds.shape[0]
  
       __UpperCamelCase				      =  self._execution_device
       # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
       # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
       # corresponds to doing no classifier free guidance.
       __UpperCamelCase				      =  guidance_scale > 1.0
  
       # 3. Encode input prompt
       __UpperCamelCase				      =  self._encode_prompt(
           lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	prompt_embeds=lowercase		,	negative_prompt_embeds=lowercase		,	)
  
       __UpperCamelCase				      =  ORT_TO_PT_TYPE[str(text_embeddings.dtype			)]
  
       # 4. Preprocess image
       __UpperCamelCase				      =  preprocess(lowercase			)
       __UpperCamelCase				      =  image.cpu()
  
       # 5. set timesteps
       self.scheduler.set_timesteps(lowercase		,	device=lowercase			)
       __UpperCamelCase				      =  self.scheduler.timesteps
  
       # 5. Add noise to image
       __UpperCamelCase				      =  torch.tensor([noise_level]		,	dtype=torch.long		,	device=lowercase			)
       __UpperCamelCase				      =  torch.randn(image.shape		,	generator=lowercase		,	device=lowercase		,	dtype=lowercase			)
       __UpperCamelCase				      =  self.low_res_scheduler.add_noise(lowercase		,	lowercase		,	lowercase			)
  
       __UpperCamelCase				      =  2 if do_classifier_free_guidance else 1
       __UpperCamelCase				      =  np.concatenate([image] * batch_multiplier * num_images_per_prompt			)
       __UpperCamelCase				      =  np.concatenate([noise_level] * image.shape[0]			)
  
       # 6. Prepare latent variables
       __UpperCamelCase      ,		__UpperCamelCase				      =  image.shape[2:]
       __UpperCamelCase				      =  self.prepare_latents(
           batch_size * num_images_per_prompt		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	)
  
       # 7. Check that sizes of image and latents match
       __UpperCamelCase				      =  image.shape[1]
       if NUM_LATENT_CHANNELS + num_channels_image != NUM_UNET_INPUT_CHANNELS:
        raise ValueError(
            """Incorrect configuration settings! The config of `pipeline.unet` expects"""
            f" {NUM_UNET_INPUT_CHANNELS} but received `num_channels_latents`: {NUM_LATENT_CHANNELS} +"
            f" `num_channels_image`: {num_channels_image} "
            f" = {NUM_LATENT_CHANNELS+num_channels_image}. Please verify the config of"
            """ `pipeline.unet` or your `image` input."""			)
  
       # 8. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
       __UpperCamelCase				      =  self.prepare_extra_step_kwargs(lowercase		,	lowercase			)
  
       __UpperCamelCase				      =  next(
           (input.type for input in self.unet.model.get_inputs() if input.name == """timestep""")		,	"""tensor(float)"""			)
       __UpperCamelCase				      =  ORT_TO_NP_TYPE[timestep_dtype]
  
       # 9. Denoising loop
       __UpperCamelCase				      =  len(lowercase			) - num_inference_steps * self.scheduler.order
       with self.progress_bar(total=lowercase			) as progress_bar:
        for i, t in enumerate(lowercase			):
         # expand the latents if we are doing classifier free guidance
         __UpperCamelCase				      =  np.concatenate([latents] * 2			) if do_classifier_free_guidance else latents
  
         # concat latents, mask, masked_image_latents in the channel dimension
         __UpperCamelCase				      =  self.scheduler.scale_model_input(lowercase		,	lowercase			)
         __UpperCamelCase				      =  np.concatenate([latent_model_input, image]		,	axis=1			)
  
         # timestep to tensor
         __UpperCamelCase				      =  np.array([t]		,	dtype=lowercase			)
  
         # predict the noise residual
         __UpperCamelCase				      =  self.unet(
             sample=lowercase		,	timestep=lowercase		,	encoder_hidden_states=lowercase		,	class_labels=noise_level.astype(np.intaa			)		,	)[0]
  
         # perform guidance
         if do_classifier_free_guidance:
          __UpperCamelCase      ,		__UpperCamelCase				      =  np.split(lowercase		,	2			)
         __UpperCamelCase				      =  noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
  
         # compute the previous noisy sample x_t -> x_t-1
         __UpperCamelCase				      =  self.scheduler.step(
             torch.from_numpy(lowercase			)		,	lowercase		,	lowercase		,	**lowercase			).prev_sample
  
         # call the callback, if provided
         if i == len(lowercase			) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
          progress_bar.update()
          if callback is not None and i % callback_steps == 0:
           callback(lowercase		,	lowercase		,	lowercase			)
  
             # 10. Post-processing
       __UpperCamelCase				      =  self.decode_latents(latents.float()			)
  
       # 11. Convert to PIL
       if output_type == "pil":
        __UpperCamelCase				      =  self.numpy_to_pil(lowercase			)
  
       if not return_dict:
        return (image,)
  
       return ImagePipelineOutput(images=lowercase			)
  
  
  
      def    __lowerCamelCase				(  self		,	lowercase			)  ->							Any:
       __UpperCamelCase				      =  1 / 0.08_333 * latents
       __UpperCamelCase				      =  self.vae(latent_sample=lowercase			)[0]
       __UpperCamelCase				      =  np.clip(image / 2 + 0.5		,	0		,	1			)
       __UpperCamelCase				      =  image.transpose((0, 2, 3, 1)			)
       return image
  
  
  
  
      def    __lowerCamelCase				(  self		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase		,	lowercase = None		,	lowercase = None		,	)  ->							Dict:
       if prompt is not None and isinstance(lowercase		,	lowercase			):
        __UpperCamelCase				      =  1
       elif prompt is not None and isinstance(lowercase		,	lowercase			):
        __UpperCamelCase				      =  len(lowercase			)
       else:
        __UpperCamelCase				      =  prompt_embeds.shape[0]
  
       if prompt_embeds is None:
        __UpperCamelCase				      =  self.tokenizer(
            lowercase		,	padding="""max_length"""		,	max_length=self.tokenizer.model_max_length		,	truncation=lowercase		,	return_tensors="""pt"""		,	)
        __UpperCamelCase				      =  text_inputs.input_ids
        __UpperCamelCase				      =  self.tokenizer(lowercase		,	padding="""longest"""		,	return_tensors="""pt"""			).input_ids
  
        if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
            lowercase		,	lowercase			):
         __UpperCamelCase				      =  self.tokenizer.batch_decode(
             untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]			)
         logger.warning(
             """The following part of your input was truncated because CLIP can only handle sequences up to"""
             f" {self.tokenizer.model_max_length} tokens: {removed_text}"			)
  
        # no positional arguments to text_encoder
        __UpperCamelCase				      =  self.text_encoder(
            input_ids=text_input_ids.int().to(lowercase			)		,	)
        __UpperCamelCase				      =  prompt_embeds[0]
  
       __UpperCamelCase      ,		__UpperCamelCase      ,		__UpperCamelCase				      =  prompt_embeds.shape
       # duplicate text embeddings for each generation per prompt, using mps friendly method
       __UpperCamelCase				      =  prompt_embeds.repeat(1		,	lowercase			)
       __UpperCamelCase				      =  prompt_embeds.reshape(bs_embed * num_images_per_prompt		,	lowercase		,	-1			)
  
       # get unconditional embeddings for classifier free guidance
       if do_classifier_free_guidance and negative_prompt_embeds is None:
        __UpperCamelCase				      =  4_2
        if negative_prompt is None:
         __UpperCamelCase				      =  [""""""] * batch_size
        elif type(lowercase			) is not type(lowercase			):
         raise TypeError(
             f"`negative_prompt` should be the same type to `prompt`, but got {type(lowercase			)} !="
             f" {type(lowercase			)}."			)
        elif isinstance(lowercase		,	lowercase			):
         __UpperCamelCase				      =  [negative_prompt]
        elif batch_size != len(lowercase			):
         raise ValueError(
             f"`negative_prompt`: {negative_prompt} has batch size {len(lowercase			)}, but `prompt`:"
             f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
             """ the batch size of `prompt`."""			)
        else:
         __UpperCamelCase				      =  negative_prompt
  
        __UpperCamelCase				      =  text_input_ids.shape[-1]
        __UpperCamelCase				      =  self.tokenizer(
            lowercase		,	padding="""max_length"""		,	max_length=lowercase		,	truncation=lowercase		,	return_tensors="""pt"""		,	)
  
        # if hasattr(uncond_input, "attention_mask"):
        #     attention_mask = uncond_input.attention_mask.to(device)
        # else:
        #     attention_mask = None
  
        __UpperCamelCase				      =  self.text_encoder(
            input_ids=uncond_input.input_ids.int().to(lowercase			)		,	)
        __UpperCamelCase				      =  uncond_embeddings[0]
  
       if do_classifier_free_guidance:
        __UpperCamelCase				      =  uncond_embeddings.shape[1]
        # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
        __UpperCamelCase				      =  uncond_embeddings.repeat(1		,	lowercase			)
        __UpperCamelCase				      =  uncond_embeddings.reshape(batch_size * num_images_per_prompt		,	lowercase		,	-1			)
  
        # For classifier free guidance, we need to do two forward passes.
        # Here we concatenate the unconditional and text embeddings into a single batch
        # to avoid doing two forward passes
        __UpperCamelCase				      =  np.concatenate([uncond_embeddings, prompt_embeds]			)
  
       return prompt_embeds
  
  
  
  
  
  
style_context: 
  
  
  
  '''simple docstring'''
  
  
  
  import argparse
  
  import torch
  from saprl.hub import distilhubert
  
  from transformers import HubertConfig, HubertModel, WavaVecaFeatureExtractor, logging
  
  
  logging.set_verbosity_info()
  a__						:			str										=			logging.get_logger(__name__)
  
  a__						:			Optional[int]										=			{
      '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',
      'mask_emb': 'masked_spec_embed',
  }
  
  
  
  
  
  
  
  def _lowercase   (  __A							,__A							,__A							,__A							,__A       ):
  
  
  
   '''simple docstring'''
  
  
  
   for attribute in key.split("""."""       ):
    __UpperCamelCase				      =  getattr(__A							,__A       )
  
   if weight_type is not None:
    __UpperCamelCase				      =  getattr(__A							,__A       ).shape
   else:
    __UpperCamelCase				      =  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				      =  value
   elif weight_type == "weight_g":
    __UpperCamelCase				      =  value
   elif weight_type == "weight_v":
    __UpperCamelCase				      =  value
   elif weight_type == "bias":
    __UpperCamelCase				      =  value
   else:
    __UpperCamelCase				      =  value
  
   logger.info(f"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}."       )
  
  
  
  
  
  
  
  def _lowercase   (  __A							,__A       ):
  
  
  
   '''simple docstring'''
  
  
  
   __UpperCamelCase				      =  []
   __UpperCamelCase				      =  fairseq_model.state_dict()
  
   __UpperCamelCase				      =  hf_model.feature_extractor
  
   for name, value in fairseq_dict.items():
    __UpperCamelCase				      =  False
    if "conv_layers" in name:
     load_conv_layer(
         __A							,__A							,__A							,__A							,hf_model.config.feat_extract_norm == """group"""							,)
     __UpperCamelCase				      =  True
    else:
     for key, mapped_key in MAPPING.items():
      __UpperCamelCase				      =  mapped_key
  
      if key in name:
       __UpperCamelCase				      =  True
       if "*" in mapped_key:
        __UpperCamelCase				      =  name.split(__A       )[0].split("""."""       )[-2]
        __UpperCamelCase				      =  mapped_key.replace("""*"""							,__A       )
       if "weight_g" in name:
        __UpperCamelCase				      =  """weight_g"""
       elif "weight_v" in name:
        __UpperCamelCase				      =  """weight_v"""
       elif "weight" in name:
        __UpperCamelCase				      =  """weight"""
       elif "bias" in name:
        __UpperCamelCase				      =  """bias"""
       else:
        __UpperCamelCase				      =  None
       set_recursively(__A							,__A							,__A							,__A							,__A       )
      continue
    if not is_used:
     unused_weights.append(__A       )
  
   logger.warning(f"Unused weights: {unused_weights}"       )
  
  
  
  
  
  
  
  def _lowercase   (  __A							,__A							,__A							,__A							,__A       ):
  
  
  
   '''simple docstring'''
  
  
  
   __UpperCamelCase				      =  full_name.split("""conv_layers."""       )[-1]
   __UpperCamelCase				      =  name.split("""."""       )
   __UpperCamelCase				      =  int(items[0]       )
   __UpperCamelCase				      =  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				      =  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				      =  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				      =  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				      =  value
     logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."       )
   else:
    unused_weights.append(__A       )
  
  
  
  
  
  
  
  def _lowercase   (  __A       ):
  
  
  
   '''simple docstring'''
  
  
  
   __UpperCamelCase				      =  HubertConfig()
   __UpperCamelCase				      =  model.config
  
   __UpperCamelCase				      =  fs_config.activation_dropout
   __UpperCamelCase				      =  False
   __UpperCamelCase				      =  fs_config.attention_dropout
   __UpperCamelCase				      =  False
   __UpperCamelCase				      =  eval(fs_config.extractor_conv_feature_layers       )
   __UpperCamelCase				      =  [x[0] for x in conv_layers]
   __UpperCamelCase				      =  [x[1] for x in conv_layers]
   __UpperCamelCase				      =  [x[2] for x in conv_layers]
   __UpperCamelCase				      =  """gelu"""
   __UpperCamelCase				      =  """layer""" if fs_config.extractor_mode == """layer_norm""" else """group"""
   __UpperCamelCase				      =  False
   __UpperCamelCase				      =  0.0
   __UpperCamelCase				      =  0.0
   __UpperCamelCase				      =  fs_config.activation_fn
   __UpperCamelCase				      =  fs_config.dropout
   __UpperCamelCase				      =  fs_config.encoder_embed_dim
   __UpperCamelCase				      =  0.02
   __UpperCamelCase				      =  fs_config.encoder_ffn_embed_dim
   __UpperCamelCase				      =  1E-5
   __UpperCamelCase				      =  0.0
   __UpperCamelCase				      =  fs_config.encoder_attention_heads
   __UpperCamelCase				      =  fs_config.conv_pos_groups
   __UpperCamelCase				      =  fs_config.conv_pos
   __UpperCamelCase				      =  len(__A       )
   __UpperCamelCase				      =  fs_config.encoder_layers
  
   return config
  
  
  
  
  
  
  
  @torch.no_grad()
  def _lowercase   (  __A							,__A=None       ):
  
  
  
   '''simple docstring'''
  
  
  
   __UpperCamelCase				      =  distilhubert().model.model
  
   if config_path is not None:
    __UpperCamelCase				      =  HubertConfig.from_pretrained(__A       )
   else:
    __UpperCamelCase				      =  convert_config(__A       )
   __UpperCamelCase				      =  model.eval()
  
   __UpperCamelCase				      =  WavaVecaFeatureExtractor(
       feature_size=1							,sampling_rate=16_000							,padding_value=0							,do_normalize=__A							,return_attention_mask=__A							,)
   __UpperCamelCase				      =  HubertModel(__A       )
  
   recursively_load_weights(__A							,__A       )
  
   feature_extractor.save_pretrained(__A       )
   hf_model.save_pretrained(__A       )
  
  
  if __name__ == "__main__":
   a__						:			List[str]										=			argparse.ArgumentParser()
   parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
   parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
   a__						:			Optional[Any]										=			parser.parse_args()
   convert_hubert_checkpoint(args.pytorch_dump_folder_path, args.config_path)
  
  
  
  
  
  