"""simple docstring"""







from typing import Callable, List, Optional, Tuple, Union

import torch
from transformers import CLIPTextModel, CLIPTokenizer

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin, TransformeraDModel, VQModel
from ...schedulers import VQDiffusionScheduler
from ...utils import logging
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


__SCREAMING_SNAKE_CASE    	=logging.get_logger(__name__)  # pylint: disable=invalid-name







class        UpperCamelCase		(			UpperCamelCase__						,      UpperCamelCase__  ):
   @register_to_config
   def __init__(			self     ,__UpperCamelCase     ,__UpperCamelCase = None     ,__UpperCamelCase = None			)	->      Optional[int]:


        '''simple docstring'''


        super().__init__()

        lowercase_	:				Optional[Any]													=	learnable

        if self.learnable:
             assert hidden_size is not None, "learnable=True requires `hidden_size` to be set"
             assert length is not None, "learnable=True requires `length` to be set"

             lowercase_	:				int													=	torch.zeros(lowercase_     ,lowercase_			)
        else:
             lowercase_	:				Any													=	None

        lowercase_	:				Optional[int]													=	torch.nn.Parameter(lowercase_			)







class        UpperCamelCase		(			UpperCamelCase__  ):
   lowercase							=  4_2
   lowercase							=  4_2
   lowercase							=  4_2
   lowercase							=  4_2
   lowercase							=  4_2
   lowercase							=  4_2
   def __init__(			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,)	->      Tuple:


        '''simple docstring'''


        super().__init__()

        self.register_modules(
            vqvae=lowercase_     ,transformer=lowercase_     ,text_encoder=lowercase_     ,tokenizer=lowercase_     ,scheduler=lowercase_     ,learned_classifier_free_sampling_embeddings=lowercase_     ,)
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Dict:


        '''simple docstring'''


        lowercase_	:				Dict													=	len(lowercase_			) if isinstance(lowercase_     ,lowercase_			) else 1

        # get prompt text embeddings
        lowercase_	:				Union[str, Any]													=	self.tokenizer(
            lowercase_     ,padding='max_length'     ,max_length=self.tokenizer.model_max_length     ,return_tensors='pt'     ,)
        lowercase_	:				Dict													=	text_inputs.input_ids

        if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
             lowercase_	:				List[str]													=	self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]			)
             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}'''			)
             lowercase_	:				Optional[int]													=	text_input_ids[:, : self.tokenizer.model_max_length]
        lowercase_	:				Union[str, Any]													=	self.text_encoder(text_input_ids.to(self.device			)			)[0]

        # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion.
        # While CLIP does normalize the pooled output of the text transformer when combining
        # the image and text embeddings, CLIP does not directly normalize the last hidden state.
        #
        # CLIP normalizing the pooled output.
        # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053
        lowercase_	:				str													=	prompt_embeds / prompt_embeds.norm(dim=-1     ,keepdim=lowercase_			)

        # duplicate text embeddings for each generation per prompt
        lowercase_	:				Tuple													=	prompt_embeds.repeat_interleave(lowercase_     ,dim=0			)

        if do_classifier_free_guidance:
             if self.learned_classifier_free_sampling_embeddings.learnable:
                  lowercase_	:				Tuple													=	self.learned_classifier_free_sampling_embeddings.embeddings
                  lowercase_	:				Union[str, Any]													=	negative_prompt_embeds.unsqueeze(0			).repeat(lowercase_     ,1     ,1			)
             else:
                  lowercase_	:				Optional[Any]													=	[''] * batch_size

                  lowercase_	:				str													=	text_input_ids.shape[-1]
                  lowercase_	:				List[Any]													=	self.tokenizer(
                      lowercase_     ,padding='max_length'     ,max_length=lowercase_     ,truncation=lowercase_     ,return_tensors='pt'     ,)
                  lowercase_	:				Optional[int]													=	self.text_encoder(uncond_input.input_ids.to(self.device			)			)[0]
                  # See comment for normalizing text embeddings
                  lowercase_	:				str													=	negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1     ,keepdim=lowercase_			)

             # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
             lowercase_	:				List[Any]													=	negative_prompt_embeds.shape[1]
             lowercase_	:				int													=	negative_prompt_embeds.repeat(1     ,lowercase_     ,1			)
             lowercase_	:				List[str]													=	negative_prompt_embeds.view(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
             lowercase_	:				Optional[Any]													=	torch.cat([negative_prompt_embeds, prompt_embeds]			)

        return prompt_embeds
   @torch.no_grad()
   def __call__(			self     ,__UpperCamelCase     ,__UpperCamelCase = 100     ,__UpperCamelCase = 5.0     ,__UpperCamelCase = 1.0     ,__UpperCamelCase = 1     ,__UpperCamelCase = None     ,__UpperCamelCase = None     ,__UpperCamelCase = "pil"     ,__UpperCamelCase = True     ,__UpperCamelCase = None     ,__UpperCamelCase = 1     ,)	->      Union[ImagePipelineOutput, Tuple]:


        '''simple docstring'''


        if isinstance(lowercase_     ,lowercase_			):
             lowercase_	:				Optional[Any]													=	1
        elif isinstance(lowercase_     ,lowercase_			):
             lowercase_	:				str													=	len(lowercase_			)
        else:
             raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(lowercase_			)}'''			)

        lowercase_	:				Optional[int]													=	batch_size * num_images_per_prompt

        lowercase_	:				Tuple													=	guidance_scale > 1.0

        lowercase_	:				Dict													=	self._encode_prompt(lowercase_     ,lowercase_     ,lowercase_			)

        if (callback_steps is None) or (
            callback_steps is not None and (not isinstance(lowercase_     ,lowercase_			) or callback_steps <= 0)
        ):
             raise ValueError(
                 f'''`callback_steps` has to be a positive integer but is {callback_steps} of type'''
                 f''' {type(lowercase_			)}.'''			)

        # get the initial completely masked latents unless the user supplied it

        lowercase_	:				int													=	(batch_size, self.transformer.num_latent_pixels)
        if latents is None:
             lowercase_	:				List[str]													=	self.transformer.num_vector_embeds - 1
             lowercase_	:				Dict													=	torch.full(lowercase_     ,lowercase_			).to(self.device			)
        else:
             if latents.shape != latents_shape:
                  raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}'''			)
             if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any():
                  raise ValueError(
                      'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,'
                      f''' {self.transformer.num_vector_embeds - 1} (inclusive).'''			)
             lowercase_	:				Union[str, Any]													=	latents.to(self.device			)

        # set timesteps
        self.scheduler.set_timesteps(lowercase_     ,device=self.device			)

        lowercase_	:				Optional[Any]													=	self.scheduler.timesteps.to(self.device			)

        lowercase_	:				List[Any]													=	latents

        for i, t in enumerate(self.progress_bar(lowercase_			)			):
             # expand the sample if we are doing classifier free guidance
             lowercase_	:				Union[str, Any]													=	torch.cat([sample] * 2			) if do_classifier_free_guidance else sample

             # predict the un-noised image
             # model_output == `log_p_x_0`
             lowercase_	:				Union[str, Any]													=	self.transformer(lowercase_     ,encoder_hidden_states=lowercase_     ,timestep=lowercase_			).sample

             if do_classifier_free_guidance:
                  lowercase_							,  lowercase_	:				int													=	model_output.chunk(2			)
                  lowercase_	:				Tuple													=	model_output_uncond + guidance_scale * (model_output_text - model_output_uncond)
                  model_output -= torch.logsumexp(lowercase_     ,dim=1     ,keepdim=lowercase_			)

             lowercase_	:				Any													=	self.truncate(lowercase_     ,lowercase_			)

             # remove `log(0)`'s (`-inf`s)
             lowercase_	:				Dict													=	model_output.clamp(-70			)

             # compute the previous noisy sample x_t -> x_t-1
             lowercase_	:				int													=	self.scheduler.step(lowercase_     ,timestep=lowercase_     ,sample=lowercase_     ,generator=lowercase_			).prev_sample

             # call the callback, if provided
             if callback is not None and i % callback_steps == 0:
                  callback(lowercase_     ,lowercase_     ,lowercase_			)

        lowercase_	:				Any													=	self.vqvae.config.vq_embed_dim
        lowercase_	:				int													=	(batch_size, self.transformer.height, self.transformer.width, embedding_channels)
        lowercase_	:				Any													=	self.vqvae.quantize.get_codebook_entry(lowercase_     ,shape=lowercase_			)
        lowercase_	:				int													=	self.vqvae.decode(lowercase_     ,force_not_quantize=lowercase_			).sample

        lowercase_	:				Dict													=	(image / 2 + 0.5).clamp(0     ,1			)
        lowercase_	:				Optional[Any]													=	image.cpu().permute(0     ,2     ,3     ,1			).numpy()

        if output_type == "pil":
             lowercase_	:				str													=	self.numpy_to_pil(lowercase_			)

        if not return_dict:
             return (image,)

        return ImagePipelineOutput(images=lowercase_			)







   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      torch.FloatTensor:


        '''simple docstring'''


        lowercase_							,  lowercase_	:				Dict													=	torch.sort(lowercase_     ,1     ,descending=lowercase_			)
        lowercase_	:				List[str]													=	torch.exp(lowercase_			)
        lowercase_	:				str													=	sorted_p_x_0.cumsum(dim=1			) < truncation_rate

        # Ensure that at least the largest probability is not zeroed out
        lowercase_	:				Tuple													=	torch.full_like(keep_mask[:, 0:1, :]     ,lowercase_			)
        lowercase_	:				str													=	torch.cat((all_true, keep_mask)     ,dim=1			)
        lowercase_	:				Dict													=	keep_mask[:, :-1, :]

        lowercase_	:				Tuple													=	keep_mask.gather(1     ,indices.argsort(1			)			)

        lowercase_	:				Union[str, Any]													=	log_p_x_0.clone()

        lowercase_	:				Optional[Any]													=	-torch.inf  # -inf = log(0)

        return rv








'''simple docstring'''

import inspect
import tempfile
from collections import OrderedDict, UserDict
from collections.abc import MutableMapping
from contextlib import ExitStack, contextmanager
from dataclasses import fields
from enum import Enum
from typing import Any, ContextManager, List, Tuple

import numpy as np

from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy


if is_flax_available():
  import jax.numpy as jnp






class    UpperCAmelCase	(       UpperCamelCase__				):
 def __get__(				self      :Optional[int]						,       lowercase_      :Tuple						,       lowercase_      :Tuple=None							)->							Optional[Any]:
     # See docs.python.org/3/howto/descriptor.html#properties
     if obj is None:
         return self
     if self.fget is None:
         raise AttributeError("unreadable attribute"							)
     A__				=			"__cached_" + self.fget.__name__
     A__				=			getattr(lowercase_						,       lowercase_						,       lowercase_							)
     if cached is None:
         A__				=			self.fget(lowercase_							)
         setattr(lowercase_						,       lowercase_						,       lowercase_							)
     return cached





def 						UpperCamelCase				(							_lowerCamelCase					:					Dict       ):
    A__				=			val.lower()
    if val in {"y", "yes", "t", "true", "on", "1"}:
        return 1
    if val in {"n", "no", "f", "false", "off", "0"}:
        return 0
    raise ValueError(F"invalid truth value {val!r}"       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Any       ):
    if is_torch_fx_proxy(_lowerCamelCase       ):
        return True
    if is_torch_available():
        import torch

        if isinstance(_lowerCamelCase						,       torch.Tensor       ):
            return True
    if is_tf_available():
        import tensorflow as tf

        if isinstance(_lowerCamelCase						,       tf.Tensor       ):
            return True

    if is_flax_available():
        import jax.numpy as jnp
        from jax.core import Tracer

        if isinstance(_lowerCamelCase						,       (jnp.ndarray, Tracer)       ):
            return True

    return isinstance(_lowerCamelCase						,       np.ndarray       )





def 						UpperCamelCase				(							_lowerCamelCase					:					str       ):
    return isinstance(_lowerCamelCase						,       np.ndarray       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Union[str, Any]       ):
    return _is_numpy(_lowerCamelCase       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Dict       ):
    import torch

    return isinstance(_lowerCamelCase						,       torch.Tensor       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Union[str, Any]       ):
    return False if not is_torch_available() else _is_torch(_lowerCamelCase       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Any       ):
    import torch

    return isinstance(_lowerCamelCase						,       torch.device       )





def 						UpperCamelCase				(							_lowerCamelCase					:					int       ):
    return False if not is_torch_available() else _is_torch_device(_lowerCamelCase       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Optional[Any]       ):
    import torch

    if isinstance(_lowerCamelCase						,       _lowerCamelCase       ):
        if hasattr(_lowerCamelCase						,       _lowerCamelCase       ):
            A__				=			getattr(_lowerCamelCase						,       _lowerCamelCase       )
        else:
            return False
    return isinstance(_lowerCamelCase						,       torch.dtype       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Any       ):
    return False if not is_torch_available() else _is_torch_dtype(_lowerCamelCase       )





def 						UpperCamelCase				(							_lowerCamelCase					:					List[Any]       ):
    import tensorflow as tf

    return isinstance(_lowerCamelCase						,       tf.Tensor       )





def 						UpperCamelCase				(							_lowerCamelCase					:					List[str]       ):
    return False if not is_tf_available() else _is_tensorflow(_lowerCamelCase       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Union[str, Any]       ):
    import tensorflow as tf

    # the `is_symbolic_tensor` predicate is only available starting with TF 2.14
    if hasattr(_lowerCamelCase						,       "is_symbolic_tensor"       ):
        return tf.is_symbolic_tensor(_lowerCamelCase       )
    return type(_lowerCamelCase       ) == tf.Tensor





def 						UpperCamelCase				(							_lowerCamelCase					:					str       ):
    return False if not is_tf_available() else _is_tf_symbolic_tensor(_lowerCamelCase       )





def 						UpperCamelCase				(							_lowerCamelCase					:					str       ):
    import jax.numpy as jnp  # noqa: F811

    return isinstance(_lowerCamelCase						,       jnp.ndarray       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Tuple       ):
    return False if not is_flax_available() else _is_jax(_lowerCamelCase       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Optional[int]       ):
    if isinstance(_lowerCamelCase						,       (dict, UserDict)       ):
        return {k: to_py_obj(_lowerCamelCase       ) for k, v in obj.items()}
    elif isinstance(_lowerCamelCase						,       (list, tuple)       ):
        return [to_py_obj(_lowerCamelCase       ) for o in obj]
    elif is_tf_tensor(_lowerCamelCase       ):
        return obj.numpy().tolist()
    elif is_torch_tensor(_lowerCamelCase       ):
        return obj.detach().cpu().tolist()
    elif is_jax_tensor(_lowerCamelCase       ):
        return np.asarray(_lowerCamelCase       ).tolist()
    elif isinstance(_lowerCamelCase						,       (np.ndarray, np.number)       ):  # tolist also works on 0d np arrays
        return obj.tolist()
    else:
        return obj





def 						UpperCamelCase				(							_lowerCamelCase					:					int       ):
    if isinstance(_lowerCamelCase						,       (dict, UserDict)       ):
        return {k: to_numpy(_lowerCamelCase       ) for k, v in obj.items()}
    elif isinstance(_lowerCamelCase						,       (list, tuple)       ):
        return np.array(_lowerCamelCase       )
    elif is_tf_tensor(_lowerCamelCase       ):
        return obj.numpy()
    elif is_torch_tensor(_lowerCamelCase       ):
        return obj.detach().cpu().numpy()
    elif is_jax_tensor(_lowerCamelCase       ):
        return np.asarray(_lowerCamelCase       )
    else:
        return obj






class    UpperCAmelCase	(       UpperCamelCase__				):
 def      UpperCAmelCase_							(				self      :int							)->							Any:
     A__				=			fields(self							)

     # Safety and consistency checks
     if not len(lowercase_							):
         raise ValueError(F"{self.__class__.__name__} has no fields."							)
     if not all(field.default is None for field in class_fields[1:]							):
         raise ValueError(F"{self.__class__.__name__} should not have more than one required field."							)

     A__				=			getattr(self						,       class_fields[0].name							)
     A__				=			all(getattr(self						,       field.name							) is None for field in class_fields[1:]							)

     if other_fields_are_none and not is_tensor(lowercase_							):
         if isinstance(lowercase_						,       lowercase_							):
             A__				=			first_field.items()
             A__				=			True
         else:
             try:
                 A__				=			iter(lowercase_							)
                 A__				=			True
             except TypeError:
                 A__				=			False

            # if we provided an iterator as first field and the iterator is a (key, value) iterator
            # set the associated fields
         if first_field_iterator:
             for idx, element in enumerate(lowercase_							):
                 if (
                     not isinstance(lowercase_						,       (list, tuple)							)
                     or not len(lowercase_							) == 2
                     or not isinstance(element[0]						,       lowercase_							)
                 ):
                     if idx == 0:
                         # If we do not have an iterator of key/values, set it as attribute
                         A__				=			first_field
                     else:
                         # If we have a mixed iterator, raise an error
                         raise ValueError(
                             F"Cannot set key/value for {element}. It needs to be a tuple (key, value)."							)
                     break
                 setattr(self						,       element[0]						,       element[1]							)
                 if element[1] is not None:
                     A__				=			element[1]
         elif first_field is not None:
             A__				=			first_field
     else:
         for field in class_fields:
             A__				=			getattr(self						,       field.name							)
             if v is not None:
                 A__				=			v
 def __delitem__(				self      :List[Any]						,       *lowercase_      :List[Any]						,       **lowercase_      :Optional[Any]							)->							Union[str, Any]:
     raise Exception(F"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."							)
 def      UpperCAmelCase_							(				self      :Tuple						,       *lowercase_      :int						,       **lowercase_      :int							)->							Union[str, Any]:
     raise Exception(F"You cannot use ``setdefault`` on a {self.__class__.__name__} instance."							)
 def      UpperCAmelCase_							(				self      :List[Any]						,       *lowercase_      :Optional[int]						,       **lowercase_      :Tuple							)->							List[Any]:
     raise Exception(F"You cannot use ``pop`` on a {self.__class__.__name__} instance."							)
 def      UpperCAmelCase_							(				self      :Dict						,       *lowercase_      :Optional[int]						,       **lowercase_      :Any							)->							Any:
     raise Exception(F"You cannot use ``update`` on a {self.__class__.__name__} instance."							)
 def __getitem__(				self      :Optional[Any]						,       lowercase_      :Optional[Any]							)->							Any:
     if isinstance(lowercase_						,       lowercase_							):
         A__				=			dict(self.items()							)
         return inner_dict[k]
     else:
         return self.to_tuple()[k]
 def __setattr__(				self      :Optional[Any]						,       lowercase_      :Union[str, Any]						,       lowercase_      :Union[str, Any]							)->							Tuple:
     if name in self.keys() and value is not None:
         # Don't call self.__setitem__ to avoid recursion errors
         super().__setitem__(lowercase_						,       lowercase_							)
     super().__setattr__(lowercase_						,       lowercase_							)
 def __setitem__(				self      :Tuple						,       lowercase_      :Optional[int]						,       lowercase_      :Tuple							)->							List[Any]:
     # Will raise a KeyException if needed
     super().__setitem__(lowercase_						,       lowercase_							)
     # Don't call self.__setattr__ to avoid recursion errors
     super().__setattr__(lowercase_						,       lowercase_							)



 def      UpperCAmelCase_							(				self      :List[Any]							)->							Tuple[Any]:
     return tuple(self[k] for k in self.keys()							)






class    UpperCAmelCase	(       UpperCamelCase__			,       UpperCamelCase__				):
 @classmethod
 def      UpperCAmelCase_							(				cls      :Any						,       lowercase_      :int							)->							List[str]:
     raise ValueError(
         F"{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys()							)}"							)






class    UpperCAmelCase	(       UpperCamelCase__				):
 __lowercase   		=				"""longest"""
 __lowercase   		=				"""max_length"""
 __lowercase   		=				"""do_not_pad"""






class    UpperCAmelCase	(       UpperCamelCase__				):
 __lowercase   		=				"""pt"""
 __lowercase   		=				"""tf"""
 __lowercase   		=				"""np"""
 __lowercase   		=				"""jax"""






class    UpperCAmelCase	:
 def __init__(				self      :List[str]						,       lowercase_      :List[ContextManager]							)->							str:
     A__				=			context_managers
     A__				=			ExitStack()
 def __enter__(				self      :Dict							)->							Any:
     for context_manager in self.context_managers:
         self.stack.enter_context(lowercase_							)



 def __exit__(				self      :List[Any]						,       *lowercase_      :Optional[Any]						,       **lowercase_      :str							)->							Union[str, Any]:
     self.stack.__exit__(*lowercase_						,       **lowercase_							)





def 						UpperCamelCase				(							_lowerCamelCase					:					Dict       ):
    A__				=			infer_framework(_lowerCamelCase       )
    if framework == "tf":
        A__				=			inspect.signature(model_class.call       )  # TensorFlow models
    elif framework == "pt":
        A__				=			inspect.signature(model_class.forward       )  # PyTorch models
    else:
        A__				=			inspect.signature(model_class.__call__       )  # Flax models

    for p in signature.parameters:
        if p == "return_loss" and signature.parameters[p].default is True:
            return True

    return False





def 						UpperCamelCase				(							_lowerCamelCase					:					List[str]       ):
    A__				=			model_class.__name__
    A__				=			infer_framework(_lowerCamelCase       )
    if framework == "tf":
        A__				=			inspect.signature(model_class.call       )  # TensorFlow models
    elif framework == "pt":
        A__				=			inspect.signature(model_class.forward       )  # PyTorch models
    else:
        A__				=			inspect.signature(model_class.__call__       )  # Flax models

    if "QuestionAnswering" in model_name:
        return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")]
    else:
        return [p for p in signature.parameters if "label" in p]





def 						UpperCamelCase				(							_lowerCamelCase					:					MutableMapping						,       _lowerCamelCase					:					str = ""						,       _lowerCamelCase					:					str = "."       ):

    def _flatten_dict(_lowerCamelCase					:					List[Any]						,       _lowerCamelCase					:					int=""						,       _lowerCamelCase					:					Any="."       ):
        for k, v in d.items():
            A__				=			str(_lowerCamelCase       ) + delimiter + str(_lowerCamelCase       ) if parent_key else k
            if v and isinstance(_lowerCamelCase						,       _lowerCamelCase       ):
                yield from flatten_dict(_lowerCamelCase						,       _lowerCamelCase						,       delimiter=_lowerCamelCase       ).items()
            else:
                yield key, v

    return dict(_flatten_dict(_lowerCamelCase						,       _lowerCamelCase						,       _lowerCamelCase       )       )





@contextmanager
def 						UpperCamelCase				(							_lowerCamelCase					:					Union[str, Any]						,       _lowerCamelCase					:					bool = False       ):
    if use_temp_dir:
        with tempfile.TemporaryDirectory() as tmp_dir:
            yield tmp_dir
    else:
        yield working_dir





def 						UpperCamelCase				(							_lowerCamelCase					:					int						,       _lowerCamelCase					:					Optional[Any]=None       ):
    if is_numpy_array(_lowerCamelCase       ):
        return np.transpose(_lowerCamelCase						,       axes=_lowerCamelCase       )
    elif is_torch_tensor(_lowerCamelCase       ):
        return array.T if axes is None else array.permute(*_lowerCamelCase       )
    elif is_tf_tensor(_lowerCamelCase       ):
        import tensorflow as tf

        return tf.transpose(_lowerCamelCase						,       perm=_lowerCamelCase       )
    elif is_jax_tensor(_lowerCamelCase       ):
        return jnp.transpose(_lowerCamelCase						,       axes=_lowerCamelCase       )
    else:
        raise ValueError(F"Type not supported for transpose: {type(_lowerCamelCase       )}."       )





def 						UpperCamelCase				(							_lowerCamelCase					:					Dict						,       _lowerCamelCase					:					Any       ):
    if is_numpy_array(_lowerCamelCase       ):
        return np.reshape(_lowerCamelCase						,       _lowerCamelCase       )
    elif is_torch_tensor(_lowerCamelCase       ):
        return array.reshape(*_lowerCamelCase       )
    elif is_tf_tensor(_lowerCamelCase       ):
        import tensorflow as tf

        return tf.reshape(_lowerCamelCase						,       _lowerCamelCase       )
    elif is_jax_tensor(_lowerCamelCase       ):
        return jnp.reshape(_lowerCamelCase						,       _lowerCamelCase       )
    else:
        raise ValueError(F"Type not supported for reshape: {type(_lowerCamelCase       )}."       )





def 						UpperCamelCase				(							_lowerCamelCase					:					List[str]						,       _lowerCamelCase					:					Optional[Any]=None       ):
    if is_numpy_array(_lowerCamelCase       ):
        return np.squeeze(_lowerCamelCase						,       axis=_lowerCamelCase       )
    elif is_torch_tensor(_lowerCamelCase       ):
        return array.squeeze() if axis is None else array.squeeze(dim=_lowerCamelCase       )
    elif is_tf_tensor(_lowerCamelCase       ):
        import tensorflow as tf

        return tf.squeeze(_lowerCamelCase						,       axis=_lowerCamelCase       )
    elif is_jax_tensor(_lowerCamelCase       ):
        return jnp.squeeze(_lowerCamelCase						,       axis=_lowerCamelCase       )
    else:
        raise ValueError(F"Type not supported for squeeze: {type(_lowerCamelCase       )}."       )





def 						UpperCamelCase				(							_lowerCamelCase					:					List[str]						,       _lowerCamelCase					:					Dict       ):
    if is_numpy_array(_lowerCamelCase       ):
        return np.expand_dims(_lowerCamelCase						,       _lowerCamelCase       )
    elif is_torch_tensor(_lowerCamelCase       ):
        return array.unsqueeze(dim=_lowerCamelCase       )
    elif is_tf_tensor(_lowerCamelCase       ):
        import tensorflow as tf

        return tf.expand_dims(_lowerCamelCase						,       axis=_lowerCamelCase       )
    elif is_jax_tensor(_lowerCamelCase       ):
        return jnp.expand_dims(_lowerCamelCase						,       axis=_lowerCamelCase       )
    else:
        raise ValueError(F"Type not supported for expand_dims: {type(_lowerCamelCase       )}."       )





def 						UpperCamelCase				(							_lowerCamelCase					:					List[str]       ):
    if is_numpy_array(_lowerCamelCase       ):
        return np.size(_lowerCamelCase       )
    elif is_torch_tensor(_lowerCamelCase       ):
        return array.numel()
    elif is_tf_tensor(_lowerCamelCase       ):
        import tensorflow as tf

        return tf.size(_lowerCamelCase       )
    elif is_jax_tensor(_lowerCamelCase       ):
        return array.size
    else:
        raise ValueError(F"Type not supported for expand_dims: {type(_lowerCamelCase       )}."       )





def 						UpperCamelCase				(							_lowerCamelCase					:					List[str]						,       _lowerCamelCase					:					Optional[Any]       ):
    for key, value in auto_map.items():
        if isinstance(_lowerCamelCase						,       (tuple, list)       ):
            A__				=			[F"{repo_id}--{v}" if (v is not None and "--" not in v) else v for v in value]
        elif value is not None and "--" not in value:
            A__				=			F"{repo_id}--{value}"

    return auto_map





def 						UpperCamelCase				(							_lowerCamelCase					:					Dict       ):
    for base_class in inspect.getmro(_lowerCamelCase       ):
        A__				=			base_class.__module__
        A__				=			base_class.__name__
        if module.startswith("tensorflow"       ) or module.startswith("keras"       ) or name == "TFPreTrainedModel":
            return "tf"
        elif module.startswith("torch"       ) or name == "PreTrainedModel":
            return "pt"
        elif module.startswith("flax"       ) or module.startswith("jax"       ) or name == "FlaxPreTrainedModel":
            return "flax"
    else:
        raise TypeError(F"Could not infer framework from class {model_class}."       )



