




import inspect
from typing import Optional, Union

import numpy as np
import PIL
import torch
from torch.nn import functional as F
from torchvision import transforms
from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    DPMSolverMultistepScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput
from diffusers.utils import (
    PIL_INTERPOLATION,
    randn_tensor,
)






def  snake_case(    __magic_name__			,		__magic_name__			,		__magic_name__   )			->							Tuple:





     '''simple docstring'''


     if isinstance(__magic_name__			,		torch.Tensor   ):
          return image
     elif isinstance(__magic_name__			,		PIL.Image.Image   ):
          lowercase :       List[Any]       =			[image]

     if isinstance(image[0]			,		PIL.Image.Image   ):
          lowercase :       str       =			[np.array(i.resize((w, h)			,		resample=PIL_INTERPOLATION['''lanczos''']   )   )[None, :] for i in image]
          lowercase :       List[Any]       =			np.concatenate(__magic_name__			,		axis=0   )
          lowercase :       Tuple       =			np.array(__magic_name__   ).astype(np.floataa   ) / 2_5_5.0
          lowercase :       Optional[int]       =			image.transpose(0			,		3			,		1			,		2   )
          lowercase :       Dict       =			2.0 * image - 1.0
          lowercase :       Dict       =			torch.from_numpy(__magic_name__   )
     elif isinstance(image[0]			,		torch.Tensor   ):
          lowercase :       List[Any]       =			torch.cat(__magic_name__			,		dim=0   )
     return image






def  snake_case(    __magic_name__			,		__magic_name__			,		__magic_name__			,		__magic_name__=0.9_9_9_5   )			->							Union[str, Any]:





     '''simple docstring'''


     if not isinstance(__magic_name__			,		np.ndarray   ):
          lowercase :       Dict       =			True
          lowercase :       Optional[int]       =			va.device
          lowercase :       str       =			va.cpu().numpy()
          lowercase :       int       =			va.cpu().numpy()

     lowercase :       Union[str, Any]       =			np.sum(va * va / (np.linalg.norm(__magic_name__   ) * np.linalg.norm(__magic_name__   ))   )
     if np.abs(__magic_name__   ) > DOT_THRESHOLD:
          lowercase :       Any       =			(1 - t) * va + t * va
     else:
          lowercase :       List[Any]       =			np.arccos(__magic_name__   )
          lowercase :       List[str]       =			np.sin(__magic_name__   )
          lowercase :       List[Any]       =			theta_a * t
          lowercase :       Tuple       =			np.sin(__magic_name__   )
          lowercase :       Union[str, Any]       =			np.sin(theta_a - theta_t   ) / sin_theta_a
          lowercase :       int       =			sin_theta_t / sin_theta_a
          lowercase :       Union[str, Any]       =			sa * va + sa * va

     if inputs_are_torch:
          lowercase :       List[str]       =			torch.from_numpy(__magic_name__   ).to(__magic_name__   )

     return va






def  snake_case(    __magic_name__			,		__magic_name__   )			->							Optional[int]:





     '''simple docstring'''


     lowercase :       int       =			F.normalize(__magic_name__			,		dim=-1   )
     lowercase :       Optional[Any]       =			F.normalize(__magic_name__			,		dim=-1   )
     return (x - y).norm(dim=-1   ).div(2   ).arcsin().pow(2   ).mul(2   )






def  snake_case(    __magic_name__			,		__magic_name__   )			->							Union[str, Any]:





     '''simple docstring'''


     for param in model.parameters():
          lowercase :       Union[str, Any]       =			value



class 						_A (  _lowerCamelCase				):






  def __init__(  self		:     Tuple				,						_A		:     AutoencoderKL				,						_A		:     CLIPTextModel				,						_A		:     CLIPModel				,						_A		:     CLIPTokenizer				,						_A		:     UNetaDConditionModel				,						_A		:     Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler]				,						_A		:     CLIPFeatureExtractor				,						_A		:     Tuple=None				,						_A		:     Union[str, Any]=None				,						_A		:     Dict=None				,						)		->   List[str]:






       """simple docstring"""







       super().__init__()
       self.register_modules(
           vae=_A				,						text_encoder=_A				,						clip_model=_A				,						tokenizer=_A				,						unet=_A				,						scheduler=_A				,						feature_extractor=_A				,						coca_model=_A				,						coca_tokenizer=_A				,						coca_transform=_A				,						)
       lowercase :       Dict       =			(
           feature_extractor.size
           if isinstance(feature_extractor.size				,						_A				)
           else feature_extractor.size['''shortest_edge''']
       )
       lowercase :       Tuple       =			transforms.Normalize(mean=feature_extractor.image_mean				,						std=feature_extractor.image_std				)
       set_requires_grad(self.text_encoder				,						_A				)
       set_requires_grad(self.clip_model				,						_A				)






  def  __a      (  self		:     str				,						_A		:     Optional[Union[str, int]] = "auto"				)		->   List[Any]:






       """simple docstring"""







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






  def  __a      (  self		:     Optional[int]				)		->   Any:






       """simple docstring"""







       self.enable_attention_slicing(_A				)






  def  __a      (  self		:     int				)		->   Tuple:






       """simple docstring"""







       set_requires_grad(self.vae				,						_A				)






  def  __a      (  self		:     List[Any]				)		->   List[str]:






       """simple docstring"""







       set_requires_grad(self.vae				,						_A				)






  def  __a      (  self		:     str				)		->   Optional[int]:






       """simple docstring"""







       set_requires_grad(self.unet				,						_A				)






  def  __a      (  self		:     Optional[Any]				)		->   List[Any]:






       """simple docstring"""







       set_requires_grad(self.unet				,						_A				)






  def  __a      (  self		:     Tuple				,						_A		:     str				,						_A		:     List[str]				,						_A		:     Any				)		->   Dict:






       """simple docstring"""







       lowercase :       Tuple       =			min(int(num_inference_steps * strength				)				,						_A				)

       lowercase :       Tuple       =			max(num_inference_steps - init_timestep				,						0				)
       lowercase :       str       =			self.scheduler.timesteps[t_start:]

       return timesteps, num_inference_steps - t_start






  def  __a      (  self		:     str				,						_A		:     Tuple				,						_A		:     List[str]				,						_A		:     Optional[int]				,						_A		:     int				,						_A		:     List[str]				,						_A		:     Any=None				)		->   Optional[int]:






       """simple docstring"""







       if not isinstance(_A				,						torch.Tensor				):
            raise ValueError(f"""`image` has to be of type `torch.Tensor` but is {type(_A				)}"""				)

       lowercase :       int       =			image.to(device=_A				,						dtype=_A				)

       if isinstance(_A				,						_A				):
            lowercase :       str       =			[
                self.vae.encode(image[i : i + 1]				).latent_dist.sample(generator[i]				) for i in range(_A				)
            ]
            lowercase :       Tuple       =			torch.cat(_A				,						dim=0				)
       else:
            lowercase :       Tuple       =			self.vae.encode(_A				).latent_dist.sample(_A				)

       # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
       lowercase :       Optional[int]       =			0.18_215 * init_latents
       lowercase :       Dict       =			init_latents.repeat_interleave(_A				,						dim=0				)

       lowercase :       Any       =			randn_tensor(init_latents.shape				,						generator=_A				,						device=_A				,						dtype=_A				)

       # get latents
       lowercase :       Dict       =			self.scheduler.add_noise(_A				,						_A				,						_A				)
       lowercase :       Dict       =			init_latents

       return latents






  def  __a      (  self		:     List[Any]				,						_A		:     Union[str, Any]				)		->   Tuple:






       """simple docstring"""







       lowercase :       int       =			self.coca_transform(_A				).unsqueeze(0				)
       with torch.no_grad(), torch.cuda.amp.autocast():
            lowercase :       List[Any]       =			self.coca_model.generate(transformed_image.to(device=self.device				,						dtype=self.coca_model.dtype				)				)
       lowercase :       List[Any]       =			self.coca_tokenizer.decode(generated[0].cpu().numpy()				)
       return generated.split('''<end_of_text>'''				)[0].replace('''<start_of_text>'''				,						''''''				).rstrip(''' .,'''				)






  def  __a      (  self		:     Tuple				,						_A		:     int				,						_A		:     Any				)		->   str:






       """simple docstring"""







       lowercase :       Any       =			self.feature_extractor.preprocess(_A				)
       lowercase :       Union[str, Any]       =			torch.from_numpy(clip_image_input['''pixel_values'''][0]				).unsqueeze(0				).to(self.device				).half()
       lowercase :       int       =			self.clip_model.get_image_features(_A				)
       lowercase :       str       =			image_embeddings_clip / image_embeddings_clip.norm(p=2				,						dim=-1				,						keepdim=_A				)
       lowercase :       Any       =			image_embeddings_clip.repeat_interleave(_A				,						dim=0				)
       return image_embeddings_clip






  @torch.enable_grad()
  def  __a      (  self		:     List[Any]				,						_A		:     Any				,						_A		:     str				,						_A		:     str				,						_A		:     int				,						_A		:     List[str]				,						_A		:     int				,						_A		:     List[str]				,						)		->   Optional[Any]:






       """simple docstring"""







       lowercase :       Any       =			latents.detach().requires_grad_()

       lowercase :       Any       =			self.scheduler.scale_model_input(_A				,						_A				)

       # predict the noise residual
       lowercase :       Tuple       =			self.unet(_A				,						_A				,						encoder_hidden_states=_A				).sample

       if isinstance(self.scheduler				,						(PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler)				):
            lowercase :       Dict       =			self.scheduler.alphas_cumprod[timestep]
            lowercase :       List[str]       =			1 - alpha_prod_t
            # compute predicted original sample from predicted noise also called
            # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
            lowercase :       Union[str, Any]       =			(latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5

            lowercase :       int       =			torch.sqrt(_A				)
            lowercase :       Tuple       =			pred_original_sample * (fac) + latents * (1 - fac)
       elif isinstance(self.scheduler				,						_A				):
            lowercase :       List[str]       =			self.scheduler.sigmas[index]
            lowercase :       str       =			latents - sigma * noise_pred
       else:
            raise ValueError(f"""scheduler type {type(self.scheduler				)} not supported"""				)

       # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
       lowercase :       Dict       =			1 / 0.18_215 * sample
       lowercase :       Dict       =			self.vae.decode(_A				).sample
       lowercase :       List[Any]       =			(image / 2 + 0.5).clamp(0				,						1				)

       lowercase :       Optional[int]       =			transforms.Resize(self.feature_extractor_size				)(_A				)
       lowercase :       Tuple       =			self.normalize(_A				).to(latents.dtype				)

       lowercase :       Dict       =			self.clip_model.get_image_features(_A				)
       lowercase :       Union[str, Any]       =			image_embeddings_clip / image_embeddings_clip.norm(p=2				,						dim=-1				,						keepdim=_A				)

       lowercase :       Union[str, Any]       =			spherical_dist_loss(_A				,						_A				).mean() * clip_guidance_scale

       lowercase :       Dict       =			-torch.autograd.grad(_A				,						_A				)[0]

       if isinstance(self.scheduler				,						_A				):
            lowercase :       Tuple       =			latents.detach() + grads * (sigma**2)
            lowercase :       List[Any]       =			noise_pred_original
       else:
            lowercase :       str       =			noise_pred_original - torch.sqrt(_A				) * grads
       return noise_pred, latents






  @torch.no_grad()
  def __call__(  self		:     Optional[int]				,						_A		:     Union[torch.FloatTensor, PIL.Image.Image]				,						_A		:     Union[torch.FloatTensor, PIL.Image.Image]				,						_A		:     Optional[str] = None				,						_A		:     Optional[str] = None				,						_A		:     Optional[int] = 512				,						_A		:     Optional[int] = 512				,						_A		:     float = 0.6				,						_A		:     Optional[int] = 50				,						_A		:     Optional[float] = 7.5				,						_A		:     Optional[int] = 1				,						_A		:     float = 0.0				,						_A		:     Optional[float] = 100				,						_A		:     Optional[torch.Generator] = None				,						_A		:     Optional[str] = "pil"				,						_A		:     bool = True				,						_A		:     float = 0.8				,						_A		:     float = 0.1				,						_A		:     float = 0.1				,						)		->   int:






       """simple docstring"""







       if isinstance(_A				,						_A				) and len(_A				) != batch_size:
            raise ValueError(f"""You have passed {batch_size} batch_size, but only {len(_A				)} generators."""				)

       if height % 8 != 0 or width % 8 != 0:
            raise ValueError(f"""`height` and `width` have to be divisible by 8 but are {height} and {width}."""				)

       if isinstance(_A				,						torch.Generator				) and batch_size > 1:
            lowercase :       Dict       =			[generator] + [None] * (batch_size - 1)

       lowercase :       Any       =			[
           ('''model''', self.coca_model is None),
           ('''tokenizer''', self.coca_tokenizer is None),
           ('''transform''', self.coca_transform is None),
       ]
       lowercase :       Any       =			[x[0] for x in coca_is_none if x[1]]
       lowercase :       Tuple       =			''', '''.join(_A				)
       # generate prompts with coca model if prompt is None
       if content_prompt is None:
            if len(_A				):
                 raise ValueError(
                     f"""Content prompt is None and CoCa [{coca_is_none_str}] is None."""
                     f"""Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline."""				)
            lowercase :       Dict       =			self.get_image_description(_A				)
       if style_prompt is None:
            if len(_A				):
                 raise ValueError(
                     f"""Style prompt is None and CoCa [{coca_is_none_str}] is None."""
                     f""" Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline."""				)
            lowercase :       Union[str, Any]       =			self.get_image_description(_A				)

       # get prompt text embeddings for content and style
       lowercase :       List[Any]       =			self.tokenizer(
           _A				,						padding='''max_length'''				,						max_length=self.tokenizer.model_max_length				,						truncation=_A				,						return_tensors='''pt'''				,						)
       lowercase :       Tuple       =			self.text_encoder(content_text_input.input_ids.to(self.device				)				)[0]

       lowercase :       Union[str, Any]       =			self.tokenizer(
           _A				,						padding='''max_length'''				,						max_length=self.tokenizer.model_max_length				,						truncation=_A				,						return_tensors='''pt'''				,						)
       lowercase :       str       =			self.text_encoder(style_text_input.input_ids.to(self.device				)				)[0]

       lowercase :       Union[str, Any]       =			slerp(_A				,						_A				,						_A				)

       # duplicate text embeddings for each generation per prompt
       lowercase :       List[Any]       =			text_embeddings.repeat_interleave(_A				,						dim=0				)

       # set timesteps
       lowercase :       List[str]       =			'''offset''' in set(inspect.signature(self.scheduler.set_timesteps				).parameters.keys()				)
       lowercase :       Dict       =			{}
       if accepts_offset:
            lowercase :       Dict       =			1

       self.scheduler.set_timesteps(_A				,						**_A				)
       # Some schedulers like PNDM have timesteps as arrays
       # It's more optimized to move all timesteps to correct device beforehand
       self.scheduler.timesteps.to(self.device				)

       lowercase      ,	lowercase :       List[str]       =			self.get_timesteps(_A				,						_A				,						self.device				)
       lowercase :       Optional[Any]       =			timesteps[:1].repeat(_A				)

       # Preprocess image
       lowercase :       Optional[int]       =			preprocess(_A				,						_A				,						_A				)
       lowercase :       Optional[int]       =			self.prepare_latents(
           _A				,						_A				,						_A				,						text_embeddings.dtype				,						self.device				,						_A				)

       lowercase :       List[str]       =			preprocess(_A				,						_A				,						_A				)
       lowercase :       int       =			self.prepare_latents(
           _A				,						_A				,						_A				,						text_embeddings.dtype				,						self.device				,						_A				)

       lowercase :       List[str]       =			slerp(_A				,						_A				,						_A				)

       if clip_guidance_scale > 0:
            lowercase :       Tuple       =			self.get_clip_image_embeddings(_A				,						_A				)
            lowercase :       Tuple       =			self.get_clip_image_embeddings(_A				,						_A				)
            lowercase :       int       =			slerp(
                _A				,						_A				,						_A				)

       # 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.
       lowercase :       str       =			guidance_scale > 1.0
       # get unconditional embeddings for classifier free guidance
       if do_classifier_free_guidance:
            lowercase :       Optional[Any]       =			content_text_input.input_ids.shape[-1]
            lowercase :       Tuple       =			self.tokenizer(['''''']				,						padding='''max_length'''				,						max_length=_A				,						return_tensors='''pt'''				)
            lowercase :       List[str]       =			self.text_encoder(uncond_input.input_ids.to(self.device				)				)[0]
            # duplicate unconditional embeddings for each generation per prompt
            lowercase :       str       =			uncond_embeddings.repeat_interleave(_A				,						dim=0				)

            # 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 :       Any       =			torch.cat([uncond_embeddings, text_embeddings]				)

       # get the initial random noise unless the user supplied it

       # Unlike in other pipelines, latents need to be generated in the target device
       # for 1-to-1 results reproducibility with the CompVis implementation.
       # However this currently doesn't work in `mps`.
       lowercase :       Any       =			(batch_size, self.unet.config.in_channels, height // 8, width // 8)
       lowercase :       Optional[Any]       =			text_embeddings.dtype
       if latents is None:
            if self.device.type == "mps":
                 # randn does not work reproducibly on mps
                 lowercase :       Tuple       =			torch.randn(_A				,						generator=_A				,						device='''cpu'''				,						dtype=_A				).to(
                     self.device				)
            else:
                 lowercase :       Dict       =			torch.randn(_A				,						generator=_A				,						device=self.device				,						dtype=_A				)
       else:
            if latents.shape != latents_shape:
                 raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}"""				)
            lowercase :       Union[str, Any]       =			latents.to(self.device				)

       # scale the initial noise by the standard deviation required by the scheduler
       lowercase :       Any       =			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]
       lowercase :       str       =			'''eta''' in set(inspect.signature(self.scheduler.step				).parameters.keys()				)
       lowercase :       Optional[Any]       =			{}
       if accepts_eta:
            lowercase :       str       =			eta

       # check if the scheduler accepts generator
       lowercase :       str       =			'''generator''' in set(inspect.signature(self.scheduler.step				).parameters.keys()				)
       if accepts_generator:
            lowercase :       Dict       =			generator

       with self.progress_bar(total=_A				):
            for i, t in enumerate(_A				):
                 # expand the latents if we are doing classifier free guidance
                 lowercase :       Tuple       =			torch.cat([latents] * 2				) if do_classifier_free_guidance else latents
                 lowercase :       List[str]       =			self.scheduler.scale_model_input(_A				,						_A				)

                 # predict the noise residual
                 lowercase :       Optional[Any]       =			self.unet(_A				,						_A				,						encoder_hidden_states=_A				).sample

                 # perform classifier free guidance
                 if do_classifier_free_guidance:
                      lowercase      ,	lowercase :       Any       =			noise_pred.chunk(2				)
                      lowercase :       List[Any]       =			noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

                 # perform clip guidance
                 if clip_guidance_scale > 0:
                      lowercase :       Union[str, Any]       =			(
                          text_embeddings.chunk(2				)[1] if do_classifier_free_guidance else text_embeddings
                      )
                      lowercase      ,	lowercase :       str       =			self.cond_fn(
                          _A				,						_A				,						_A				,						_A				,						_A				,						_A				,						_A				,						)

                 # compute the previous noisy sample x_t -> x_t-1
                 lowercase :       int       =			self.scheduler.step(_A				,						_A				,						_A				,						**_A				).prev_sample

        # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
       lowercase :       Any       =			1 / 0.18_215 * latents
       lowercase :       str       =			self.vae.decode(_A				).sample

       lowercase :       List[Any]       =			(image / 2 + 0.5).clamp(0				,						1				)
       lowercase :       List[str]       =			image.cpu().permute(0				,						2				,						3				,						1				).numpy()

       if output_type == "pil":
            lowercase :       int       =			self.numpy_to_pil(_A				)

       if not return_dict:
            return (image, None)

       return StableDiffusionPipelineOutput(images=_A				,						nsfw_content_detected=_A				)





import argparse
import gc
import json
import os
import shutil
import warnings

import torch

from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer


try:
 from transformers import LlamaTokenizerFast
except ImportError as e:
 warnings.warn(e)
 warnings.warn(
     'The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion'
 )
 lowerCAmelCase_						=			None
lowerCAmelCase_						=			{
    '7B': 1_10_08,
    '13B': 1_38_24,
    '30B': 1_79_20,
    '65B': 2_20_16,
    '70B': 2_86_72,
}
lowerCAmelCase_						=			{
    '7B': 1,
    '7Bf': 1,
    '13B': 2,
    '13Bf': 2,
    '30B': 4,
    '65B': 8,
    '70B': 8,
    '70Bf': 8,
}






def  snake_case(    __magic_name__			,		__magic_name__=1			,		__magic_name__=2_56   )			->							List[Any]:





     '''simple docstring'''


     return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3   )   ) + multiple_of - 1) // multiple_of)






def  snake_case(    __magic_name__   )			->							Union[str, Any]:





     '''simple docstring'''


     with open(__magic_name__			,		'''r'''   ) as f:
          return json.load(__magic_name__   )






def  snake_case(    __magic_name__			,		__magic_name__   )			->							Optional[int]:





     '''simple docstring'''


     with open(__magic_name__			,		'''w'''   ) as f:
          json.dump(__magic_name__			,		__magic_name__   )






def  snake_case(    __magic_name__			,		__magic_name__			,		__magic_name__			,		__magic_name__=True   )			->							Tuple:





     '''simple docstring'''


     os.makedirs(__magic_name__			,		exist_ok=__magic_name__   )
     lowercase :       List[Any]       =			os.path.join(__magic_name__			,		'''tmp'''   )
     os.makedirs(__magic_name__			,		exist_ok=__magic_name__   )

     lowercase :       List[Any]       =			read_json(os.path.join(__magic_name__			,		'''params.json'''   )   )
     lowercase :       int       =			NUM_SHARDS[model_size]
     lowercase :       str       =			params['''n_layers''']
     lowercase :       Optional[int]       =			params['''n_heads''']
     lowercase :       str       =			n_heads // num_shards
     lowercase :       Dict       =			params['''dim''']
     lowercase :       int       =			dim // n_heads
     lowercase :       List[str]       =			1_0_0_0_0.0
     lowercase :       Optional[Any]       =			1.0 / (base ** (torch.arange(0			,		__magic_name__			,		2   ).float() / dims_per_head))

     if "n_kv_heads" in params:
          lowercase :       Optional[Any]       =			params['''n_kv_heads''']  # for GQA / MQA
          lowercase :       Union[str, Any]       =			n_heads_per_shard // num_key_value_heads
          lowercase :       Any       =			dim // num_key_value_heads
     else:  # compatibility with other checkpoints
          lowercase :       Optional[Any]       =			n_heads
          lowercase :       str       =			n_heads_per_shard
          lowercase :       Any       =			dim

     # permute for sliced rotary
     def permute(__magic_name__			,		__magic_name__=n_heads			,		__magic_name__=dim			,		__magic_name__=dim   ):
          return w.view(__magic_name__			,		dima // n_heads // 2			,		2			,		__magic_name__   ).transpose(1			,		2   ).reshape(__magic_name__			,		__magic_name__   )

     print(F"""Fetching all parameters from the checkpoint at {input_base_path}."""   )
     # Load weights
     if model_size == "7B":
          # Not sharded
          # (The sharded implementation would also work, but this is simpler.)
          lowercase :       Tuple       =			torch.load(os.path.join(__magic_name__			,		'''consolidated.00.pth'''   )			,		map_location='''cpu'''   )
     else:
          # Sharded
          lowercase :       str       =			[
              torch.load(os.path.join(__magic_name__			,		F"""consolidated.{i:02d}.pth"""   )			,		map_location='''cpu'''   )
              for i in range(__magic_name__   )
          ]
     lowercase :       Tuple       =			0
     lowercase :       str       =			{'''weight_map''': {}}
     for layer_i in range(__magic_name__   ):
          lowercase :       List[Any]       =			F"""pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin"""
          if model_size == "7B":
               # Unsharded
               lowercase :       int       =			{
                   F"""model.layers.{layer_i}.self_attn.q_proj.weight""": permute(
                       loaded[F"""layers.{layer_i}.attention.wq.weight"""]   ),
                   F"""model.layers.{layer_i}.self_attn.k_proj.weight""": permute(
                       loaded[F"""layers.{layer_i}.attention.wk.weight"""]   ),
                   F"""model.layers.{layer_i}.self_attn.v_proj.weight""": loaded[F"""layers.{layer_i}.attention.wv.weight"""],
                   F"""model.layers.{layer_i}.self_attn.o_proj.weight""": loaded[F"""layers.{layer_i}.attention.wo.weight"""],
                   F"""model.layers.{layer_i}.mlp.gate_proj.weight""": loaded[F"""layers.{layer_i}.feed_forward.w1.weight"""],
                   F"""model.layers.{layer_i}.mlp.down_proj.weight""": loaded[F"""layers.{layer_i}.feed_forward.w2.weight"""],
                   F"""model.layers.{layer_i}.mlp.up_proj.weight""": loaded[F"""layers.{layer_i}.feed_forward.w3.weight"""],
                   F"""model.layers.{layer_i}.input_layernorm.weight""": loaded[F"""layers.{layer_i}.attention_norm.weight"""],
                   F"""model.layers.{layer_i}.post_attention_layernorm.weight""": loaded[F"""layers.{layer_i}.ffn_norm.weight"""],
               }
          else:
               # Sharded
               # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share
               # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is
               # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned.

               lowercase :       List[Any]       =			{
                   F"""model.layers.{layer_i}.input_layernorm.weight""": loaded[0][
                       F"""layers.{layer_i}.attention_norm.weight"""
                   ].clone(),
                   F"""model.layers.{layer_i}.post_attention_layernorm.weight""": loaded[0][
                       F"""layers.{layer_i}.ffn_norm.weight"""
                   ].clone(),
               }
               lowercase :       Tuple       =			permute(
                   torch.cat(
                       [
                           loaded[i][F"""layers.{layer_i}.attention.wq.weight"""].view(__magic_name__			,		__magic_name__			,		__magic_name__   )
                           for i in range(__magic_name__   )
                       ]			,		dim=0			,		).reshape(__magic_name__			,		__magic_name__   )   )
               lowercase :       Any       =			permute(
                   torch.cat(
                       [
                           loaded[i][F"""layers.{layer_i}.attention.wk.weight"""].view(
                               __magic_name__			,		__magic_name__			,		__magic_name__   )
                           for i in range(__magic_name__   )
                       ]			,		dim=0			,		).reshape(__magic_name__			,		__magic_name__   )			,		__magic_name__			,		__magic_name__			,		__magic_name__			,		)
               lowercase :       Any       =			torch.cat(
                   [
                       loaded[i][F"""layers.{layer_i}.attention.wv.weight"""].view(
                           __magic_name__			,		__magic_name__			,		__magic_name__   )
                       for i in range(__magic_name__   )
                   ]			,		dim=0			,		).reshape(__magic_name__			,		__magic_name__   )

               lowercase :       int       =			torch.cat(
                   [loaded[i][F"""layers.{layer_i}.attention.wo.weight"""] for i in range(__magic_name__   )]			,		dim=1   )
               lowercase :       str       =			torch.cat(
                   [loaded[i][F"""layers.{layer_i}.feed_forward.w1.weight"""] for i in range(__magic_name__   )]			,		dim=0   )
               lowercase :       Any       =			torch.cat(
                   [loaded[i][F"""layers.{layer_i}.feed_forward.w2.weight"""] for i in range(__magic_name__   )]			,		dim=1   )
               lowercase :       List[Any]       =			torch.cat(
                   [loaded[i][F"""layers.{layer_i}.feed_forward.w3.weight"""] for i in range(__magic_name__   )]			,		dim=0   )

          lowercase :       List[Any]       =			inv_freq
          for k, v in state_dict.items():
               lowercase :       Tuple       =			filename
               param_count += v.numel()
          torch.save(__magic_name__			,		os.path.join(__magic_name__			,		__magic_name__   )   )

     lowercase :       Tuple       =			F"""pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin"""
     if model_size == "7B":
          # Unsharded
          lowercase :       Optional[int]       =			{
              '''model.embed_tokens.weight''': loaded['''tok_embeddings.weight'''],
              '''model.norm.weight''': loaded['''norm.weight'''],
              '''lm_head.weight''': loaded['''output.weight'''],
          }
     else:
          lowercase :       Tuple       =			{
              '''model.norm.weight''': loaded[0]['''norm.weight'''],
              '''model.embed_tokens.weight''': torch.cat(
                  [loaded[i]['''tok_embeddings.weight'''] for i in range(__magic_name__   )]			,		dim=1   ),
              '''lm_head.weight''': torch.cat([loaded[i]['''output.weight'''] for i in range(__magic_name__   )]			,		dim=0   ),
          }

     for k, v in state_dict.items():
          lowercase :       Tuple       =			filename
          param_count += v.numel()
     torch.save(__magic_name__			,		os.path.join(__magic_name__			,		__magic_name__   )   )

     # Write configs
     lowercase :       Tuple       =			{'''total_size''': param_count * 2}
     write_json(__magic_name__			,		os.path.join(__magic_name__			,		'''pytorch_model.bin.index.json'''   )   )
     lowercase :       Tuple       =			params['''ffn_dim_multiplier'''] if '''ffn_dim_multiplier''' in params else 1
     lowercase :       Tuple       =			params['''multiple_of'''] if '''multiple_of''' in params else 2_56
     lowercase :       List[Any]       =			LlamaConfig(
         hidden_size=__magic_name__			,		intermediate_size=compute_intermediate_size(__magic_name__			,		__magic_name__			,		__magic_name__   )			,		num_attention_heads=params['''n_heads''']			,		num_hidden_layers=params['''n_layers''']			,		rms_norm_eps=params['''norm_eps''']			,		num_key_value_heads=__magic_name__			,		)
     config.save_pretrained(__magic_name__   )

     # Make space so we can load the model properly now.
     del state_dict
     del loaded
     gc.collect()

     print('''Loading the checkpoint in a Llama model.'''   )
     lowercase :       Dict       =			LlamaForCausalLM.from_pretrained(__magic_name__			,		torch_dtype=torch.floataa			,		low_cpu_mem_usage=__magic_name__   )
     # Avoid saving this as part of the config.
     del model.config._name_or_path

     print('''Saving in the Transformers format.'''   )
     model.save_pretrained(__magic_name__			,		safe_serialization=__magic_name__   )
     shutil.rmtree(__magic_name__   )






def  snake_case(    __magic_name__			,		__magic_name__   )			->							Optional[int]:





     '''simple docstring'''


     lowercase :       str       =			LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast
     print(F"""Saving a {tokenizer_class.__name__} to {tokenizer_path}."""   )
     lowercase :       Tuple       =			tokenizer_class(__magic_name__   )
     tokenizer.save_pretrained(__magic_name__   )






def  snake_case(    )			->							Optional[Any]:





     '''simple docstring'''


     lowercase :       List[str]       =			argparse.ArgumentParser()
     parser.add_argument(
         '''--input_dir'''			,		help='''Location of LLaMA weights, which contains tokenizer.model and model folders'''			,		)
     parser.add_argument(
         '''--model_size'''			,		choices=['''7B''', '''7Bf''', '''13B''', '''13Bf''', '''30B''', '''65B''', '''70B''', '''70Bf''', '''tokenizer_only''']			,		)
     parser.add_argument(
         '''--output_dir'''			,		help='''Location to write HF model and tokenizer'''			,		)
     parser.add_argument('''--safe_serialization'''			,		type=__magic_name__			,		help='''Whether or not to save using `safetensors`.'''   )
     lowercase :       List[str]       =			parser.parse_args()
     if args.model_size != "tokenizer_only":
          write_model(
              model_path=args.output_dir			,		input_base_path=os.path.join(args.input_dir			,		args.model_size   )			,		model_size=args.model_size			,		safe_serialization=args.safe_serialization			,		)
     lowercase :       Any       =			os.path.join(args.input_dir			,		'''tokenizer.model'''   )
     write_tokenizer(args.output_dir			,		__magic_name__   )


if __name__ == "__main__":
 main()