'''simple docstring'''




from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


snake_case	=      '''CompVis/stable-diffusion-v1-1'''
snake_case	=      '''CompVis/stable-diffusion-v1-2'''
snake_case	=      '''CompVis/stable-diffusion-v1-3'''
snake_case	=      '''CompVis/stable-diffusion-v1-4'''





class 							SCREAMING_SNAKE_CASE (					UpperCAmelCase_			):
  """simple docstring"""







  def __init__(	self :       List[str]       ,       __lowerCAmelCase :       AutoencoderKL       ,       __lowerCAmelCase :       CLIPTextModel       ,       __lowerCAmelCase :       CLIPTokenizer       ,       __lowerCAmelCase :       UNetaDConditionModel       ,       __lowerCAmelCase :       Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]       ,       __lowerCAmelCase :       StableDiffusionSafetyChecker       ,       __lowerCAmelCase :       CLIPImageProcessor       ,       __lowerCAmelCase :       bool = True       ,       ):


        """simple docstring"""



        super()._init_()

        _lowerCAmelCase		 = StableDiffusionPipeline.from_pretrained(lowerCamelCase__       )
        _lowerCAmelCase		 = StableDiffusionPipeline.from_pretrained(lowerCamelCase__       )
        _lowerCAmelCase		 = StableDiffusionPipeline.from_pretrained(lowerCamelCase__       )
        _lowerCAmelCase		 = StableDiffusionPipeline(
            vae=lowerCamelCase__       ,       text_encoder=lowerCamelCase__       ,       tokenizer=lowerCamelCase__       ,       unet=lowerCamelCase__       ,       scheduler=lowerCamelCase__       ,       safety_checker=lowerCamelCase__       ,       feature_extractor=lowerCamelCase__       ,       requires_safety_checker=lowerCamelCase__       ,       )

        self.register_modules(pipelinea=self.pipea       ,       pipelinea=self.pipea       ,       pipelinea=self.pipea       ,       pipelinea=self.pipea       )



  @property
  def 	a (	self :       Optional[Any]       ):


        """simple docstring"""



        return {k: getattr(self       ,       lowerCamelCase__       ) for k in self.config.keys() if not k.startswith('_'       )}



  def 	a (	self :       List[str]       ,       __lowerCAmelCase :       Optional[Union[str, int]] = "auto"       ):


        """simple docstring"""



        if slice_size == "auto":
              # half the attention head size is usually a good trade-off between
              # speed and memory
              _lowerCAmelCase		 = self.unet.config.attention_head_dim // 2
        self.unet.set_attention_slice(lowerCamelCase__       )



  def 	a (	self :       str       ):


        """simple docstring"""



        self.enable_attention_slicing(lowerCamelCase__       )



  @torch.no_grad()
  def 	a (	self :       Any       ,       __lowerCAmelCase :       Union[str, List[str]]       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 50       ,       __lowerCAmelCase :       float = 7.5       ,       __lowerCAmelCase :       Optional[Union[str, List[str]]] = None       ,       __lowerCAmelCase :       Optional[int] = 1       ,       __lowerCAmelCase :       float = 0.0       ,       __lowerCAmelCase :       Optional[torch.Generator] = None       ,       __lowerCAmelCase :       Optional[torch.FloatTensor] = None       ,       __lowerCAmelCase :       Optional[str] = "pil"       ,       __lowerCAmelCase :       bool = True       ,       __lowerCAmelCase :       Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,       __lowerCAmelCase :       int = 1       ,       **__lowerCAmelCase :       str       ,       ):


        """simple docstring"""



        return self.pipea(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )



  @torch.no_grad()
  def 	a (	self :       Union[str, Any]       ,       __lowerCAmelCase :       Union[str, List[str]]       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 50       ,       __lowerCAmelCase :       float = 7.5       ,       __lowerCAmelCase :       Optional[Union[str, List[str]]] = None       ,       __lowerCAmelCase :       Optional[int] = 1       ,       __lowerCAmelCase :       float = 0.0       ,       __lowerCAmelCase :       Optional[torch.Generator] = None       ,       __lowerCAmelCase :       Optional[torch.FloatTensor] = None       ,       __lowerCAmelCase :       Optional[str] = "pil"       ,       __lowerCAmelCase :       bool = True       ,       __lowerCAmelCase :       Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,       __lowerCAmelCase :       int = 1       ,       **__lowerCAmelCase :       Dict       ,       ):


        """simple docstring"""



        return self.pipea(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )



  @torch.no_grad()
  def 	a (	self :       Union[str, Any]       ,       __lowerCAmelCase :       Union[str, List[str]]       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 50       ,       __lowerCAmelCase :       float = 7.5       ,       __lowerCAmelCase :       Optional[Union[str, List[str]]] = None       ,       __lowerCAmelCase :       Optional[int] = 1       ,       __lowerCAmelCase :       float = 0.0       ,       __lowerCAmelCase :       Optional[torch.Generator] = None       ,       __lowerCAmelCase :       Optional[torch.FloatTensor] = None       ,       __lowerCAmelCase :       Optional[str] = "pil"       ,       __lowerCAmelCase :       bool = True       ,       __lowerCAmelCase :       Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,       __lowerCAmelCase :       int = 1       ,       **__lowerCAmelCase :       Any       ,       ):


        """simple docstring"""



        return self.pipea(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )



  @torch.no_grad()
  def 	a (	self :       Dict       ,       __lowerCAmelCase :       Union[str, List[str]]       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 50       ,       __lowerCAmelCase :       float = 7.5       ,       __lowerCAmelCase :       Optional[Union[str, List[str]]] = None       ,       __lowerCAmelCase :       Optional[int] = 1       ,       __lowerCAmelCase :       float = 0.0       ,       __lowerCAmelCase :       Optional[torch.Generator] = None       ,       __lowerCAmelCase :       Optional[torch.FloatTensor] = None       ,       __lowerCAmelCase :       Optional[str] = "pil"       ,       __lowerCAmelCase :       bool = True       ,       __lowerCAmelCase :       Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,       __lowerCAmelCase :       int = 1       ,       **__lowerCAmelCase :       List[Any]       ,       ):


        """simple docstring"""



        return self.pipea(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )



  @torch.no_grad()
  def 	a (	self :       Optional[Any]       ,       __lowerCAmelCase :       Union[str, List[str]]       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 512       ,       __lowerCAmelCase :       int = 50       ,       __lowerCAmelCase :       float = 7.5       ,       __lowerCAmelCase :       Optional[Union[str, List[str]]] = None       ,       __lowerCAmelCase :       Optional[int] = 1       ,       __lowerCAmelCase :       float = 0.0       ,       __lowerCAmelCase :       Optional[torch.Generator] = None       ,       __lowerCAmelCase :       Optional[torch.FloatTensor] = None       ,       __lowerCAmelCase :       Optional[str] = "pil"       ,       __lowerCAmelCase :       bool = True       ,       __lowerCAmelCase :       Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,       __lowerCAmelCase :       int = 1       ,       **__lowerCAmelCase :       Dict       ,       ):


        """simple docstring"""



        _lowerCAmelCase		 = """cuda""" if torch.cuda.is_available() else """cpu"""
        self.to(lowerCamelCase__       )

        # Checks if the height and width are divisible by 8 or not
        if height % 8 != 0 or width % 8 != 0:
              raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}."       )

        # Get first result from Stable Diffusion Checkpoint v1.1
        _lowerCAmelCase		 = self.textaimg_sda_a(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )

        # Get first result from Stable Diffusion Checkpoint v1.2
        _lowerCAmelCase		 = self.textaimg_sda_a(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )

        # Get first result from Stable Diffusion Checkpoint v1.3
        _lowerCAmelCase		 = self.textaimg_sda_a(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )

        # Get first result from Stable Diffusion Checkpoint v1.4
        _lowerCAmelCase		 = self.textaimg_sda_a(
            prompt=lowerCamelCase__       ,       height=lowerCamelCase__       ,       width=lowerCamelCase__       ,       num_inference_steps=lowerCamelCase__       ,       guidance_scale=lowerCamelCase__       ,       negative_prompt=lowerCamelCase__       ,       num_images_per_prompt=lowerCamelCase__       ,       eta=lowerCamelCase__       ,       generator=lowerCamelCase__       ,       latents=lowerCamelCase__       ,       output_type=lowerCamelCase__       ,       return_dict=lowerCamelCase__       ,       callback=lowerCamelCase__       ,       callback_steps=lowerCamelCase__       ,       **lowerCamelCase__       ,       )

        # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
        return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]       )













import os
from pickle import UnpicklingError
from typing import Dict, Tuple

import jax
import jax.numpy as jnp
import numpy as np
from flax.serialization import from_bytes
from flax.traverse_util import flatten_dict, unflatten_dict

import transformers

from .utils import logging


UpperCamelCase    							=      logging.get_logger(__name__)


def 				__lowerCamelCase     (							__lowerCAmelCase							:					Any					,							__lowerCAmelCase							:					Any					,							__lowerCAmelCase							:					Any					,							__lowerCAmelCase							:					List[Any]=False							)     ->  Union[str, Any]:
   try:
      import torch  # noqa: F401
   except ImportError:
      logger.error(
          """Loading a PyTorch model in Flax, requires both PyTorch and Flax to be installed. Please see"""
          """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation"""
          """ instructions."""							)
      raise

   if not is_sharded:
      __UpperCamelCase	:			Tuple					=					os.path.abspath(__lowerCAmelCase							)
      logger.info(f'Loading PyTorch weights from {pt_path}'							)

      __UpperCamelCase	:			int					=					torch.load(__lowerCAmelCase					,							map_location="""cpu"""							)
      logger.info(f'PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values()							):,} parameters.'							)

      __UpperCamelCase	:			List[Any]					=					convert_pytorch_state_dict_to_flax(__lowerCAmelCase					,							__lowerCAmelCase							)
   else:
      # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files
      __UpperCamelCase	:			List[str]					=					convert_pytorch_sharded_state_dict_to_flax(__lowerCAmelCase					,							__lowerCAmelCase							)
   return flax_state_dict


def 				__lowerCamelCase     (							__lowerCAmelCase							:					Tuple[str]					,							__lowerCAmelCase							:					np.ndarray					,							__lowerCAmelCase							:					Dict[str, jnp.ndarray]					,							__lowerCAmelCase							:					str					,							)     ->  (Tuple[str], np.ndarray):

   def is_key_or_prefix_key_in_dict(__lowerCAmelCase							:					Tuple[str]							) -> bool:
      return len(set(__lowerCAmelCase							) & {key, (model_prefix,) + key}							) > 0

   # layer norm
   __UpperCamelCase	:			Optional[int]					=					pt_tuple_key[:-1] + ("""scale""",)
   if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(__lowerCAmelCase							):
      return renamed_pt_tuple_key, pt_tensor

   # batch norm layer mean
   __UpperCamelCase	:			Optional[Any]					=					pt_tuple_key[:-1] + ("""mean""",)
   if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(__lowerCAmelCase							):
      return renamed_pt_tuple_key, pt_tensor

   # batch norm layer var
   __UpperCamelCase	:			Optional[Any]					=					pt_tuple_key[:-1] + ("""var""",)
   if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(__lowerCAmelCase							):
      return renamed_pt_tuple_key, pt_tensor

   # embedding
   __UpperCamelCase	:			Union[str, Any]					=					pt_tuple_key[:-1] + ("""embedding""",)
   if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(__lowerCAmelCase							):
      return renamed_pt_tuple_key, pt_tensor

   # conv layer
   __UpperCamelCase	:			Tuple					=					pt_tuple_key[:-1] + ("""kernel""",)
   if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(__lowerCAmelCase							):
      __UpperCamelCase	:			Optional[int]					=					pt_tensor.transpose(2					,							3					,							1					,							0							)
      return renamed_pt_tuple_key, pt_tensor

   # linear layer
   __UpperCamelCase	:			int					=					pt_tuple_key[:-1] + ("""kernel""",)
   if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(__lowerCAmelCase							):
      __UpperCamelCase	:			Optional[int]					=					pt_tensor.T
      return renamed_pt_tuple_key, pt_tensor

   # old PyTorch layer norm weight
   __UpperCamelCase	:			Tuple					=					pt_tuple_key[:-1] + ("""weight""",)
   if pt_tuple_key[-1] == "gamma":
      return renamed_pt_tuple_key, pt_tensor

   # old PyTorch layer norm bias
   __UpperCamelCase	:			str					=					pt_tuple_key[:-1] + ("""bias""",)
   if pt_tuple_key[-1] == "beta":
      return renamed_pt_tuple_key, pt_tensor

   # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030
   __UpperCamelCase	:			Optional[int]					=					None
   if pt_tuple_key[-3::2] == ("parametrizations", "original0"):
      __UpperCamelCase	:			Tuple					=					pt_tuple_key[-2] + """_g"""
   elif pt_tuple_key[-3::2] == ("parametrizations", "original1"):
      __UpperCamelCase	:			Dict					=					pt_tuple_key[-2] + """_v"""
   if name is not None:
      __UpperCamelCase	:			Optional[Any]					=					pt_tuple_key[:-3] + (name,)
      return renamed_pt_tuple_key, pt_tensor

   return pt_tuple_key, pt_tensor


def 				__lowerCamelCase     (							__lowerCAmelCase							:					int					,							__lowerCAmelCase							:					int							)     ->  Union[str, Any]:
   # convert pytorch tensor to numpy
   __UpperCamelCase	:			Optional[Any]					=					{k: v.numpy() for k, v in pt_state_dict.items()}

   __UpperCamelCase	:			List[Any]					=					flax_model.base_model_prefix

   # use params dict if the model contains batch norm layers
   if "params" in flax_model.params:
      __UpperCamelCase	:			Union[str, Any]					=					flax_model.params["""params"""]
   else:
      __UpperCamelCase	:			List[Any]					=					flax_model.params
   __UpperCamelCase	:			List[Any]					=					flatten_dict(__lowerCAmelCase							)

   # add batch_stats keys,values to dict
   if "batch_stats" in flax_model.params:
      __UpperCamelCase	:			List[str]					=					flatten_dict(flax_model.params["""batch_stats"""]							)
      random_flax_state_dict.update(__lowerCAmelCase							)

   __UpperCamelCase	:			List[Any]					=					{}

   __UpperCamelCase	:			int					=					(model_prefix not in flax_model_params) and (
       model_prefix in {k.split("""."""							)[0] for k in pt_state_dict.keys()}
   )
   __UpperCamelCase	:			int					=					(model_prefix in flax_model_params) and (
       model_prefix not in {k.split("""."""							)[0] for k in pt_state_dict.keys()}
   )

   # Need to change some parameters name to match Flax names
   for pt_key, pt_tensor in pt_state_dict.items():
      __UpperCamelCase	:			List[str]					=					tuple(pt_key.split("""."""							)							)

      # remove base model prefix if necessary
      __UpperCamelCase	:			Optional[Any]					=					pt_tuple_key[0] == model_prefix
      if load_model_with_head_into_base_model and has_base_model_prefix:
         __UpperCamelCase	:			List[str]					=					pt_tuple_key[1:]

      # Correctly rename weight parameters
      __UpperCamelCase	,     __UpperCamelCase	:			Optional[int]					=					rename_key_and_reshape_tensor(
          __lowerCAmelCase					,							__lowerCAmelCase					,							__lowerCAmelCase					,							__lowerCAmelCase							)

      # add model prefix if necessary
      __UpperCamelCase	:			Optional[Any]					=					(model_prefix,) + flax_key in random_flax_state_dict
      if load_base_model_into_model_with_head and require_base_model_prefix:
         __UpperCamelCase	:			Dict					=					(model_prefix,) + flax_key

      if flax_key in random_flax_state_dict:
         if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
            raise ValueError(
                f'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape '
                f'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.'							)

        # add batch stats if the model contains batchnorm layers
      if "batch_stats" in flax_model.params:
         if "mean" in flax_key[-1] or "var" in flax_key[-1]:
            __UpperCamelCase	:			int					=					jnp.asarray(__lowerCAmelCase							)
            continue
         # remove num_batches_tracked key
         if "num_batches_tracked" in flax_key[-1]:
            flax_state_dict.pop(__lowerCAmelCase					,							__lowerCAmelCase							)
            continue

         # also add unexpected weight so that warning is thrown
         __UpperCamelCase	:			Optional[Any]					=					jnp.asarray(__lowerCAmelCase							)

      else:
         # also add unexpected weight so that warning is thrown
         __UpperCamelCase	:			Optional[int]					=					jnp.asarray(__lowerCAmelCase							)

   return unflatten_dict(__lowerCAmelCase							)


def 				__lowerCamelCase     (							__lowerCAmelCase							:					List[str]					,							__lowerCAmelCase							:					int							)     ->  Dict:
   import torch

   # Load the index
   __UpperCamelCase	:			List[str]					=					{}
   for shard_file in shard_filenames:
      # load using msgpack utils
      __UpperCamelCase	:			List[str]					=					torch.load(__lowerCAmelCase							)
      __UpperCamelCase	:			int					=					{k: v.numpy() for k, v in pt_state_dict.items()}

      __UpperCamelCase	:			List[str]					=					flax_model.base_model_prefix

      # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict
      if "batch_stats" in flax_model.params:
         __UpperCamelCase	:			Optional[Any]					=					flax_model.params["""params"""]

         __UpperCamelCase	:			int					=					flatten_dict(__lowerCAmelCase							)
         random_flax_state_dict.update(flatten_dict(flax_model.params["""batch_stats"""]							)							)
      else:
         __UpperCamelCase	:			Dict					=					flax_model.params
         __UpperCamelCase	:			int					=					flatten_dict(__lowerCAmelCase							)

      __UpperCamelCase	:			Union[str, Any]					=					(model_prefix not in flax_model_params) and (
          model_prefix in {k.split("""."""							)[0] for k in pt_state_dict.keys()}
      )
      __UpperCamelCase	:			Any					=					(model_prefix in flax_model_params) and (
          model_prefix not in {k.split("""."""							)[0] for k in pt_state_dict.keys()}
      )
      # Need to change some parameters name to match Flax names
      for pt_key, pt_tensor in pt_state_dict.items():
         __UpperCamelCase	:			Any					=					tuple(pt_key.split("""."""							)							)

         # remove base model prefix if necessary
         __UpperCamelCase	:			Dict					=					pt_tuple_key[0] == model_prefix
         if load_model_with_head_into_base_model and has_base_model_prefix:
            __UpperCamelCase	:			str					=					pt_tuple_key[1:]

         # Correctly rename weight parameters
         __UpperCamelCase	,     __UpperCamelCase	:			Optional[Any]					=					rename_key_and_reshape_tensor(
             __lowerCAmelCase					,							__lowerCAmelCase					,							__lowerCAmelCase					,							__lowerCAmelCase							)
         # add model prefix if necessary
         __UpperCamelCase	:			Tuple					=					(model_prefix,) + flax_key in random_flax_state_dict
         if load_base_model_into_model_with_head and require_base_model_prefix:
            __UpperCamelCase	:			Optional[Any]					=					(model_prefix,) + flax_key

         if flax_key in random_flax_state_dict:
            if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
               raise ValueError(
                   f'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape '
                   f'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.'							)

            # add batch stats if the model contains batchnorm layers
         if "batch_stats" in flax_model.params:
            if "mean" in flax_key[-1]:
               __UpperCamelCase	:			List[Any]					=					jnp.asarray(__lowerCAmelCase							)
               continue
            if "var" in flax_key[-1]:
               __UpperCamelCase	:			Optional[Any]					=					jnp.asarray(__lowerCAmelCase							)
               continue
            # remove num_batches_tracked key
            if "num_batches_tracked" in flax_key[-1]:
               flax_state_dict.pop(__lowerCAmelCase					,							__lowerCAmelCase							)
               continue

            # also add unexpected weight so that warning is thrown
            __UpperCamelCase	:			int					=					jnp.asarray(__lowerCAmelCase							)

         else:
            # also add unexpected weight so that warning is thrown
            __UpperCamelCase	:			int					=					jnp.asarray(__lowerCAmelCase							)
   return unflatten_dict(__lowerCAmelCase							)


def 				__lowerCamelCase     (							__lowerCAmelCase							:					Any					,							__lowerCAmelCase							:					Union[str, Any]							)     ->  Union[str, Any]:
   __UpperCamelCase	:			List[Any]					=					os.path.abspath(__lowerCAmelCase							)
   logger.info(f'Loading Flax weights from {flax_checkpoint_path}'							)

   # import correct flax class
   __UpperCamelCase	:			str					=					getattr(__lowerCAmelCase					,							"""Flax""" + model.__class__.__name__							)

   # load flax weight dict
   with open(__lowerCAmelCase					,							"""rb"""							) as state_f:
      try:
         __UpperCamelCase	:			Optional[int]					=					from_bytes(__lowerCAmelCase					,							state_f.read()							)
      except UnpicklingError:
         raise EnvironmentError(f'Unable to convert {flax_checkpoint_path} to Flax deserializable object. '							)

   return load_flax_weights_in_pytorch_model(__lowerCAmelCase					,							__lowerCAmelCase							)



def 				__lowerCamelCase     (							__lowerCAmelCase							:					Optional[int]					,							__lowerCAmelCase							:					Optional[int]							)     ->  List[Any]:

   try:
      import torch  # noqa: F401
   except ImportError:
      logger.error(
          """Loading a Flax weights in PyTorch, requires both PyTorch and Flax to be installed. Please see"""
          """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation"""
          """ instructions."""							)
      raise

   # check if we have bf16 weights
   __UpperCamelCase	:			Tuple					=					flatten_dict(jax.tree_util.tree_map(lambda __lowerCAmelCase							: x.dtype == jnp.bfloataa					,							__lowerCAmelCase							)							).values()
   if any(__lowerCAmelCase							):
      # convert all weights to fp32 if the are bf16 since torch.from_numpy can-not handle bf16
      # and bf16 is not fully supported in PT yet.
      logger.warning(
          """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """
          """before loading those in PyTorch model."""							)
      __UpperCamelCase	:			List[Any]					=					jax.tree_util.tree_map(
          lambda __lowerCAmelCase							: params.astype(np.floataa							) if params.dtype == jnp.bfloataa else params					,							__lowerCAmelCase							)

   __UpperCamelCase	:			Tuple					=					flatten_dict(__lowerCAmelCase							)
   __UpperCamelCase	:			int					=					pt_model.state_dict()

   __UpperCamelCase	:			Tuple					=					(pt_model.base_model_prefix in flax_state) and (
       pt_model.base_model_prefix not in {k.split("""."""							)[0] for k in pt_model_dict.keys()}
   )
   __UpperCamelCase	:			Any					=					(pt_model.base_model_prefix not in flax_state) and (
       pt_model.base_model_prefix in {k.split("""."""							)[0] for k in pt_model_dict.keys()}
   )

   # keep track of unexpected & missing keys
   __UpperCamelCase	:			Dict					=					[]
   __UpperCamelCase	:			List[str]					=					set(pt_model_dict.keys()							)

   for flax_key_tuple, flax_tensor in flax_state_dict.items():
      __UpperCamelCase	:			Union[str, Any]					=					flax_key_tuple[0] == pt_model.base_model_prefix
      __UpperCamelCase	:			Optional[Any]					=					""".""".join((pt_model.base_model_prefix,) + flax_key_tuple							) in pt_model_dict

      # adapt flax_key to prepare for loading from/to base model only
      if load_model_with_head_into_base_model and has_base_model_prefix:
         __UpperCamelCase	:			List[Any]					=					flax_key_tuple[1:]
      elif load_base_model_into_model_with_head and require_base_model_prefix:
         __UpperCamelCase	:			Tuple					=					(pt_model.base_model_prefix,) + flax_key_tuple

      # rename flax weights to PyTorch format
      if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(__lowerCAmelCase							) not in pt_model_dict:
         # conv layer
         __UpperCamelCase	:			Optional[int]					=					flax_key_tuple[:-1] + ("""weight""",)
         __UpperCamelCase	:			Dict					=					jnp.transpose(__lowerCAmelCase					,							(3, 2, 0, 1)							)
      elif flax_key_tuple[-1] == "kernel" and ".".join(__lowerCAmelCase							) not in pt_model_dict:
         # linear layer
         __UpperCamelCase	:			Optional[int]					=					flax_key_tuple[:-1] + ("""weight""",)
         __UpperCamelCase	:			List[Any]					=					flax_tensor.T
      elif flax_key_tuple[-1] in ["scale", "embedding"]:
         __UpperCamelCase	:			Union[str, Any]					=					flax_key_tuple[:-1] + ("""weight""",)

      # adding batch stats from flax batch norm to pt
      elif "mean" in flax_key_tuple[-1]:
         __UpperCamelCase	:			List[str]					=					flax_key_tuple[:-1] + ("""running_mean""",)
      elif "var" in flax_key_tuple[-1]:
         __UpperCamelCase	:			Tuple					=					flax_key_tuple[:-1] + ("""running_var""",)

      if "batch_stats" in flax_state:
         __UpperCamelCase	:			Optional[int]					=					""".""".join(flax_key_tuple[1:]							)  # Remove the params/batch_stats header
      else:
         __UpperCamelCase	:			Optional[int]					=					""".""".join(__lowerCAmelCase							)

      # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation.
      __UpperCamelCase	:			Union[str, Any]					=					{}
      # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030
      for key in pt_model_dict:
         __UpperCamelCase	:			int					=					key.split("""."""							)
         __UpperCamelCase	:			str					=					None
         if key_components[-3::2] == ["parametrizations", "original0"]:
            __UpperCamelCase	:			Union[str, Any]					=					key_components[-2] + """_g"""
         elif key_components[-3::2] == ["parametrizations", "original1"]:
            __UpperCamelCase	:			Tuple					=					key_components[-2] + """_v"""
         if name is not None:
            __UpperCamelCase	:			Optional[Any]					=					key_components[:-3] + [name]
            __UpperCamelCase	:			int					=					""".""".join(__lowerCAmelCase							)
            __UpperCamelCase	:			List[str]					=					key

      if flax_key in special_pt_names:
         __UpperCamelCase	:			Any					=					special_pt_names[flax_key]

      if flax_key in pt_model_dict:
         if flax_tensor.shape != pt_model_dict[flax_key].shape:
            raise ValueError(
                f'Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected '
                f'to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.'							)
         else:
            # add weight to pytorch dict
            __UpperCamelCase	:			str					=					np.asarray(__lowerCAmelCase							) if not isinstance(__lowerCAmelCase					,							np.ndarray							) else flax_tensor
            __UpperCamelCase	:			Optional[int]					=					torch.from_numpy(__lowerCAmelCase							)
            # remove from missing keys
            missing_keys.remove(__lowerCAmelCase							)
      else:
         # weight is not expected by PyTorch model
         unexpected_keys.append(__lowerCAmelCase							)

   pt_model.load_state_dict(__lowerCAmelCase							)

   # re-transform missing_keys to list
   __UpperCamelCase	:			Union[str, Any]					=					list(__lowerCAmelCase							)

   if len(__lowerCAmelCase							) > 0:
      logger.warning(
          """Some weights of the Flax model were not used when initializing the PyTorch model"""
          f' {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing'
          f' {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture'
          """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This"""
          f' IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect'
          """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a"""
          """ FlaxBertForSequenceClassification model)."""							)
   else:
      logger.warning(f'All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n'							)
   if len(__lowerCAmelCase							) > 0:
      logger.warning(
          f'Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly'
          f' initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to'
          """ use it for predictions and inference."""							)
   else:
      logger.warning(
          f'All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n'
          """If your task is similar to the task the model of the checkpoint was trained on, """
          f'you can already use {pt_model.__class__.__name__} for predictions without further training.'							)

   return pt_model



