





'''simple docstring'''





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

import numpy as np
import PIL
import torch
import torch.utils.checkpoint

from ...models import UNetaDModel, VQModel
from ...schedulers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
)
from ...utils import PIL_INTERPOLATION, randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput





def   lowerCAmelCase_				( __a	)  ->	Dict:







  """simple docstring"""





  lowerCamelCase__:   List[Any]    						=image.size
  lowerCamelCase__:   Any    						=(x - x % 32 for x in (w, h))  # resize to integer multiple of 32
  lowerCamelCase__:   str    						=image.resize((w, h)			,					resample=PIL_INTERPOLATION["lanczos"]	)
  lowerCamelCase__:   Optional[int]    						=np.array(__a	).astype(np.floataa	) / 255.0
  lowerCamelCase__:   List[str]    						=image[None].transpose(0			,					3			,					1			,					2	)
  lowerCamelCase__:   Any    						=torch.from_numpy(__a	)
  return 2.0 * image - 1.0






class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''

 def __init__(self       :     Any ,				UpperCAmelCase_       :     VQModel ,				UpperCAmelCase_       :     UNetaDModel ,				UpperCAmelCase_       :     Union[
         DDIMScheduler,
         PNDMScheduler,
         LMSDiscreteScheduler,
         EulerDiscreteScheduler,
         EulerAncestralDiscreteScheduler,
         DPMSolverMultistepScheduler,
     ] ,				) ->Tuple:



   '''simple docstring'''



   super().__init__()
   self.register_modules(vqvae=UpperCAmelCase_ ,				unet=UpperCAmelCase_ ,				scheduler=UpperCAmelCase_)







 @torch.no_grad()
 def __call__(self       :     List[str] ,				UpperCAmelCase_       :     Union[torch.Tensor, PIL.Image.Image] = None ,				UpperCAmelCase_       :     Optional[int] = 1 ,				UpperCAmelCase_       :     Optional[int] = 100 ,				UpperCAmelCase_       :     Optional[float] = 0.0 ,				UpperCAmelCase_       :     Optional[Union[torch.Generator, List[torch.Generator]]] = None ,				UpperCAmelCase_       :     Optional[str] = "pil" ,				UpperCAmelCase_       :     bool = True ,				) ->Union[Tuple, ImagePipelineOutput]:



   '''simple docstring'''



   if isinstance(UpperCAmelCase_ ,				PIL.Image.Image):
     lowerCamelCase__:   str    						=1
   elif isinstance(UpperCAmelCase_ ,				torch.Tensor):
     lowerCamelCase__:   Optional[int]    						=image.shape[0]
   else:
     raise ValueError(F"""`image` has to be of type `PIL.Image.Image` or `torch.Tensor` but is {type(UpperCAmelCase_)}""")

   if isinstance(UpperCAmelCase_ ,				PIL.Image.Image):
     lowerCamelCase__:   Tuple    						=preprocess(UpperCAmelCase_)

   lowerCamelCase__:   Dict    						=image.shape[-2:]

   # in_channels should be 6: 3 for latents, 3 for low resolution image
   lowerCamelCase__:   Union[str, Any]    						=(batch_size, self.unet.config.in_channels // 2, height, width)
   lowerCamelCase__:   int    						=next(self.unet.parameters()).dtype

   lowerCamelCase__:   Optional[Any]    						=randn_tensor(UpperCAmelCase_ ,				generator=UpperCAmelCase_ ,				device=self.device ,				dtype=UpperCAmelCase_)

   lowerCamelCase__:   List[Any]    						=image.to(device=self.device ,				dtype=UpperCAmelCase_)

   # set timesteps and move to the correct device
   self.scheduler.set_timesteps(UpperCAmelCase_ ,				device=self.device)
   lowerCamelCase__:   Dict    						=self.scheduler.timesteps

   # scale the initial noise by the standard deviation required by the scheduler
   lowerCamelCase__:   Tuple    						=latents * self.scheduler.init_noise_sigma

   # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature.
   # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
   # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
   # and should be between [0, 1]
   lowerCamelCase__:   Any    						="eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
   lowerCamelCase__:   int    						={}
   if accepts_eta:
     lowerCamelCase__:   Any    						=eta

   for t in self.progress_bar(UpperCAmelCase_):
     # concat latents and low resolution image in the channel dimension.
     lowerCamelCase__:   List[str]    						=torch.cat([latents, image] ,				dim=1)
     lowerCamelCase__:   List[str]    						=self.scheduler.scale_model_input(UpperCAmelCase_ ,				UpperCAmelCase_)
     # predict the noise residual
     lowerCamelCase__:   Union[str, Any]    						=self.unet(UpperCAmelCase_ ,				UpperCAmelCase_).sample
     # compute the previous noisy sample x_t -> x_t-1
     lowerCamelCase__:   Optional[Any]    						=self.scheduler.step(UpperCAmelCase_ ,				UpperCAmelCase_ ,				UpperCAmelCase_ ,				**UpperCAmelCase_).prev_sample

   # decode the image latents with the VQVAE
   lowerCamelCase__:   List[Any]    						=self.vqvae.decode(UpperCAmelCase_).sample
   lowerCamelCase__:   Optional[Any]    						=torch.clamp(UpperCAmelCase_ ,				-1.0 ,				1.0)
   lowerCamelCase__:   str    						=image / 2 + 0.5
   lowerCamelCase__:   List[Any]    						=image.cpu().permute(0 ,				2 ,				3 ,				1).numpy()

   if output_type == "pil":
     lowerCamelCase__:   Tuple    						=self.numpy_to_pil(UpperCAmelCase_)

   if not return_dict:
     return (image,)

   return ImagePipelineOutput(images=UpperCAmelCase_)








from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A             =       logging.get_logger(__name__)

__A             =       {
    "tiiuae/falcon-40b": "https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json",
    "tiiuae/falcon-7b": "https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json",
}



class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''
 lowercase_	=    "falcon"
 lowercase_	=    ["past_key_values"]

 def __init__(self       :     str ,				UpperCAmelCase_       :     Optional[Any]=65_024 ,				UpperCAmelCase_       :     Union[str, Any]=4_544 ,				UpperCAmelCase_       :     Any=32 ,				UpperCAmelCase_       :     str=71 ,				UpperCAmelCase_       :     int=1E-5 ,				UpperCAmelCase_       :     int=0.02 ,				UpperCAmelCase_       :     int=True ,				UpperCAmelCase_       :     Tuple=0.0 ,				UpperCAmelCase_       :     str=0.0 ,				UpperCAmelCase_       :     List[Any]=None ,				UpperCAmelCase_       :     Any=False ,				UpperCAmelCase_       :     Tuple=False ,				UpperCAmelCase_       :     List[Any]=True ,				UpperCAmelCase_       :     Dict=True ,				UpperCAmelCase_       :     List[str]=False ,				UpperCAmelCase_       :     str=11 ,				UpperCAmelCase_       :     List[Any]=11 ,				**UpperCAmelCase_       :     str ,				) ->Any:



   '''simple docstring'''



   lowerCamelCase__:   Dict    						=vocab_size
   # Backward compatibility with n_embed kwarg
   lowerCamelCase__:   List[Any]    						=kwargs.pop("n_embed" ,				UpperCAmelCase_)
   lowerCamelCase__:   Dict    						=hidden_size if n_embed is None else n_embed
   lowerCamelCase__:   Tuple    						=num_hidden_layers
   lowerCamelCase__:   Dict    						=num_attention_heads
   lowerCamelCase__:   Optional[Any]    						=layer_norm_epsilon
   lowerCamelCase__:   Union[str, Any]    						=initializer_range
   lowerCamelCase__:   Union[str, Any]    						=use_cache
   lowerCamelCase__:   str    						=hidden_dropout
   lowerCamelCase__:   Tuple    						=attention_dropout

   lowerCamelCase__:   Dict    						=bos_token_id
   lowerCamelCase__:   Union[str, Any]    						=eos_token_id
   lowerCamelCase__:   Any    						=num_attention_heads if num_kv_heads is None else num_kv_heads
   lowerCamelCase__:   Dict    						=alibi
   lowerCamelCase__:   Optional[Any]    						=new_decoder_architecture
   lowerCamelCase__:   List[str]    						=multi_query  # Ignored when new_decoder_architecture is True
   lowerCamelCase__:   Tuple    						=parallel_attn
   lowerCamelCase__:   Optional[Any]    						=bias

   super().__init__(bos_token_id=UpperCAmelCase_ ,				eos_token_id=UpperCAmelCase_ ,				**UpperCAmelCase_)

 @property
 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->Dict:



   '''simple docstring'''



   return self.hidden_size // self.num_attention_heads







 @property
 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->List[Any]:



   '''simple docstring'''



   return not self.alibi

