


'''simple docstring'''






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"
  )
  __A					:  Dict											=      None
__A					:  int											=      {
    "7B": 1_1008,
    "13B": 1_3824,
    "30B": 1_7920,
    "65B": 2_2016,
    "70B": 2_8672,
}
__A					:  List[Any]											=      {
    "7B": 1,
    "7Bf": 1,
    "13B": 2,
    "13Bf": 2,
    "30B": 4,
    "65B": 8,
    "70B": 8,
    "70Bf": 8,
}







def 				UpperCamelCase_			(    A__					:   List[Any]							,    A__					:   int=1							,    A__					:   Dict=2_56					):
  '''simple docstring'''





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







def 				UpperCamelCase_			(    A__					:   Tuple					):
  '''simple docstring'''





  with open(__snake_case							,    """r"""					) as f:
    return json.load(__snake_case					)







def 				UpperCamelCase_			(    A__					:   Optional[Any]							,    A__					:   int					):
  '''simple docstring'''





  with open(__snake_case							,    """w"""					) as f:
    json.dump(__snake_case							,    __snake_case					)







def 				UpperCamelCase_			(    A__					:   List[str]							,    A__					:   Any							,    A__					:   Dict							,    A__					:   int=True					):
  '''simple docstring'''





  os.makedirs(__snake_case							,    exist_ok=__snake_case					)
  lowerCAmelCase_			:      Union[str, Any]			=   os.path.join(__snake_case							,    """tmp"""					)
  os.makedirs(__snake_case							,    exist_ok=__snake_case					)

  lowerCAmelCase_			:      int			=   read_json(os.path.join(__snake_case							,    """params.json"""					)					)
  lowerCAmelCase_			:      int			=   NUM_SHARDS[model_size]
  lowerCAmelCase_			:      List[str]			=   params["""n_layers"""]
  lowerCAmelCase_			:      Optional[int]			=   params["""n_heads"""]
  lowerCAmelCase_			:      Tuple			=   n_heads // num_shards
  lowerCAmelCase_			:      int			=   params["""dim"""]
  lowerCAmelCase_			:      Optional[Any]			=   dim // n_heads
  lowerCAmelCase_			:      Tuple			=   1_00_00.0
  lowerCAmelCase_			:      List[str]			=   1.0 / (base ** (torch.arange(0							,    __snake_case							,    2					).float() / dims_per_head))

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

  # permute for sliced rotary
  def permute(A__					:   int							,    A__					:   List[Any]=n_heads							,    A__					:   Any=dim							,    A__					:   str=dim					):
    return w.view(__snake_case							,    dima // n_heads // 2							,    2							,    __snake_case					).transpose(1							,    2					).reshape(__snake_case							,    __snake_case					)

  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.)
    lowerCAmelCase_			:      List[str]			=   torch.load(os.path.join(__snake_case							,    """consolidated.00.pth"""					)							,    map_location="""cpu"""					)
  else:
    # Sharded
    lowerCAmelCase_			:      Tuple			=   [
        torch.load(os.path.join(__snake_case							,    f'consolidated.{i:02d}.pth'					)							,    map_location="""cpu"""					)
        for i in range(__snake_case					)
    ]
  lowerCAmelCase_			:      int			=   0
  lowerCAmelCase_			:      Any			=   {"""weight_map""": {}}
  for layer_i in range(__snake_case					):
    lowerCAmelCase_			:      Any			=   f'pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin'
    if model_size == "7B":
      # Unsharded
      lowerCAmelCase_			:      Optional[Any]			=   {
          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.

      lowerCAmelCase_			:      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(),
      }
      lowerCAmelCase_			:      Optional[Any]			=   permute(
          torch.cat(
              [
                  loaded[i][f'layers.{layer_i}.attention.wq.weight'].view(__snake_case							,    __snake_case							,    __snake_case					)
                  for i in range(__snake_case					)
              ]							,    dim=0							,    ).reshape(__snake_case							,    __snake_case					)					)
      lowerCAmelCase_			:      Tuple			=   permute(
          torch.cat(
              [
                  loaded[i][f'layers.{layer_i}.attention.wk.weight'].view(
                      __snake_case							,    __snake_case							,    __snake_case					)
                  for i in range(__snake_case					)
              ]							,    dim=0							,    ).reshape(__snake_case							,    __snake_case					)							,    __snake_case							,    __snake_case							,    __snake_case							,    )
      lowerCAmelCase_			:      List[Any]			=   torch.cat(
          [
              loaded[i][f'layers.{layer_i}.attention.wv.weight'].view(
                  __snake_case							,    __snake_case							,    __snake_case					)
              for i in range(__snake_case					)
          ]							,    dim=0							,    ).reshape(__snake_case							,    __snake_case					)

      lowerCAmelCase_			:      Optional[Any]			=   torch.cat(
          [loaded[i][f'layers.{layer_i}.attention.wo.weight'] for i in range(__snake_case					)]							,    dim=1					)
      lowerCAmelCase_			:      List[str]			=   torch.cat(
          [loaded[i][f'layers.{layer_i}.feed_forward.w1.weight'] for i in range(__snake_case					)]							,    dim=0					)
      lowerCAmelCase_			:      List[str]			=   torch.cat(
          [loaded[i][f'layers.{layer_i}.feed_forward.w2.weight'] for i in range(__snake_case					)]							,    dim=1					)
      lowerCAmelCase_			:      Tuple			=   torch.cat(
          [loaded[i][f'layers.{layer_i}.feed_forward.w3.weight'] for i in range(__snake_case					)]							,    dim=0					)

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

  lowerCAmelCase_			:      int			=   f'pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin'
  if model_size == "7B":
    # Unsharded
    lowerCAmelCase_			:      Any			=   {
        """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""],
        """model.norm.weight""": loaded["""norm.weight"""],
        """lm_head.weight""": loaded["""output.weight"""],
    }
  else:
    lowerCAmelCase_			:      List[str]			=   {
        """model.norm.weight""": loaded[0]["""norm.weight"""],
        """model.embed_tokens.weight""": torch.cat(
            [loaded[i]["""tok_embeddings.weight"""] for i in range(__snake_case					)]							,    dim=1					),
        """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(__snake_case					)]							,    dim=0					),
    }

  for k, v in state_dict.items():
    lowerCAmelCase_			:      Union[str, Any]			=   filename
    param_count += v.numel()
  torch.save(__snake_case							,    os.path.join(__snake_case							,    __snake_case					)					)

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

  # 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."""					)
  lowerCAmelCase_			:      List[str]			=   LlamaForCausalLM.from_pretrained(__snake_case							,    torch_dtype=torch.floataa							,    low_cpu_mem_usage=__snake_case					)
  # Avoid saving this as part of the config.
  del model.config._name_or_path

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







def 				UpperCamelCase_			(    A__					:   str							,    A__					:   str					):
  '''simple docstring'''





  lowerCAmelCase_			:      Tuple			=   LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast
  print(f'Saving a {tokenizer_class.__name__} to {tokenizer_path}.'					)
  lowerCAmelCase_			:      List[str]			=   tokenizer_class(__snake_case					)
  tokenizer.save_pretrained(__snake_case					)







def 				UpperCamelCase_			(    ):
  '''simple docstring'''





  lowerCAmelCase_			:      Dict			=   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=__snake_case							,    help="""Whether or not to save using `safetensors`."""					)
  lowerCAmelCase_			:      Union[str, Any]			=   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							,    )
  lowerCAmelCase_			:      Optional[int]			=   os.path.join(args.input_dir							,    """tokenizer.model"""					)
  write_tokenizer(args.output_dir							,    __snake_case					)


if __name__ == "__main__":
  main()







import gc
import unittest

from diffusers import FlaxStableDiffusionInpaintPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
  import jax
  import jax.numpy as jnp
  from flax.jax_utils import replicate
  from flax.training.common_utils import shard







@slow
@require_flax
class       lowerCamelCase__   (       unittest.TestCase):





     def      __A					(self       )     ->   List[Any]:
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()





     def      __A					(self       )     ->   Optional[Any]:
      _lowercase							      =load_image(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
          '''/sd2-inpaint/init_image.png'''       )
      _lowercase							      =load_image(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png'''       )

      _lowercase							      ='''xvjiarui/stable-diffusion-2-inpainting'''
      _lowercase      ,    _lowercase							      =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase		,     safety_checker=UpperCAmelCase       )

      _lowercase							      ='''Face of a yellow cat, high resolution, sitting on a park bench'''

      _lowercase							      =jax.random.PRNGKey(0       )
      _lowercase							      =5_0

      _lowercase							      =jax.device_count()
      _lowercase							      =num_samples * [prompt]
      _lowercase							      =num_samples * [init_image]
      _lowercase							      =num_samples * [mask_image]
      _lowercase      ,    _lowercase      ,    _lowercase							      =pipeline.prepare_inputs(UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase       )

      # shard inputs and rng
      _lowercase							      =replicate(UpperCAmelCase       )
      _lowercase							      =jax.random.split(UpperCAmelCase		,     jax.device_count()       )
      _lowercase							      =shard(UpperCAmelCase       )
      _lowercase							      =shard(UpperCAmelCase       )
      _lowercase							      =shard(UpperCAmelCase       )

      _lowercase							      =pipeline(
          UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     jit=UpperCAmelCase       )

      _lowercase							      =output.images.reshape(UpperCAmelCase		,     5_1_2		,     5_1_2		,     3       )

      _lowercase							      =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

      _lowercase							      =jnp.asarray(jax.device_get(image_slice.flatten()       )       )
      _lowercase							      =jnp.array(
          [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084]       )
      print(f"output_slice: {output_slice}"       )
      assert jnp.abs(output_slice - expected_slice       ).max() < 1e-2

