



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_						:     Union[str, Any]	     =		None
a_						:     Any	     =		{
    '7B': 1_1_0_0_8,
    '13B': 1_3_8_2_4,
    '30B': 1_7_9_2_0,
    '65B': 2_2_0_1_6,
    '70B': 2_8_6_7_2,
}
a_						:     List[str]	     =		{
    '7B': 1,
    '7Bf': 1,
    '13B': 2,
    '13Bf': 2,
    '30B': 4,
    '65B': 8,
    '70B': 8,
    '70Bf': 8,
}

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__=1   ,   UpperCAmelCase__=256 ):


      """simple docstring"""



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

def     __lowercase(	UpperCAmelCase__ ):


      """simple docstring"""



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

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__ ):


      """simple docstring"""



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

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__=True ):


      """simple docstring"""



      os.makedirs(UpperCAmelCase__   ,   exist_ok=UpperCAmelCase__ )
      lowerCamelCase								=		os.path.join(UpperCAmelCase__   ,   "tmp" )
      os.makedirs(UpperCAmelCase__   ,   exist_ok=UpperCAmelCase__ )

      lowerCamelCase								=		read_json(os.path.join(UpperCAmelCase__   ,   "params.json" ) )
      lowerCamelCase								=		NUM_SHARDS[model_size]
      lowerCamelCase								=		params["n_layers"]
      lowerCamelCase								=		params["n_heads"]
      lowerCamelCase								=		n_heads // num_shards
      lowerCamelCase								=		params["dim"]
      lowerCamelCase								=		dim // n_heads
      lowerCamelCase								=		1_0_0_0_0.0
      lowerCamelCase								=		1.0 / (base ** (torch.arange(0   ,   UpperCAmelCase__   ,   2 ).float() / dims_per_head))

      if "n_kv_heads" in params:
            lowerCamelCase								=		params["n_kv_heads"]  # for GQA / MQA
            lowerCamelCase								=		n_heads_per_shard // num_key_value_heads
            lowerCamelCase								=		dim // num_key_value_heads
      else:  # compatibility with other checkpoints
            lowerCamelCase								=		n_heads
            lowerCamelCase								=		n_heads_per_shard
            lowerCamelCase								=		dim

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

      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								=		torch.load(os.path.join(UpperCAmelCase__   ,   "consolidated.00.pth" )   ,   map_location="cpu" )
      else:
            # Sharded
            lowerCamelCase								=		[
                torch.load(os.path.join(UpperCAmelCase__   ,   F"""consolidated.{i:02d}.pth""" )   ,   map_location="cpu" )
                for i in range(UpperCAmelCase__ )
            ]
      lowerCamelCase								=		0
      lowerCamelCase								=		{"weight_map": {}}
      for layer_i in range(UpperCAmelCase__ ):
            lowerCamelCase								=		F"""pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin"""
            if model_size == "7B":
                  # Unsharded
                  lowerCamelCase								=		{
                      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								=		{
                      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								=		permute(
                      torch.cat(
                          [
                              loaded[i][F"""layers.{layer_i}.attention.wq.weight"""].view(UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__ )
                              for i in range(UpperCAmelCase__ )
                          ]   ,   dim=0   ,   ).reshape(UpperCAmelCase__   ,   UpperCAmelCase__ ) )
                  lowerCamelCase								=		permute(
                      torch.cat(
                          [
                              loaded[i][F"""layers.{layer_i}.attention.wk.weight"""].view(
                                  UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__ )
                              for i in range(UpperCAmelCase__ )
                          ]   ,   dim=0   ,   ).reshape(UpperCAmelCase__   ,   UpperCAmelCase__ )   ,   UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__   ,   )
                  lowerCamelCase								=		torch.cat(
                      [
                          loaded[i][F"""layers.{layer_i}.attention.wv.weight"""].view(
                              UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__ )
                          for i in range(UpperCAmelCase__ )
                      ]   ,   dim=0   ,   ).reshape(UpperCAmelCase__   ,   UpperCAmelCase__ )

                  lowerCamelCase								=		torch.cat(
                      [loaded[i][F"""layers.{layer_i}.attention.wo.weight"""] for i in range(UpperCAmelCase__ )]   ,   dim=1 )
                  lowerCamelCase								=		torch.cat(
                      [loaded[i][F"""layers.{layer_i}.feed_forward.w1.weight"""] for i in range(UpperCAmelCase__ )]   ,   dim=0 )
                  lowerCamelCase								=		torch.cat(
                      [loaded[i][F"""layers.{layer_i}.feed_forward.w2.weight"""] for i in range(UpperCAmelCase__ )]   ,   dim=1 )
                  lowerCamelCase								=		torch.cat(
                      [loaded[i][F"""layers.{layer_i}.feed_forward.w3.weight"""] for i in range(UpperCAmelCase__ )]   ,   dim=0 )

            lowerCamelCase								=		inv_freq
            for k, v in state_dict.items():
                  lowerCamelCase								=		filename
                  param_count += v.numel()
            torch.save(UpperCAmelCase__   ,   os.path.join(UpperCAmelCase__   ,   UpperCAmelCase__ ) )

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

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

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

      # 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								=		LlamaForCausalLM.from_pretrained(UpperCAmelCase__   ,   torch_dtype=torch.floataa   ,   low_cpu_mem_usage=UpperCAmelCase__ )
      # Avoid saving this as part of the config.
      del model.config._name_or_path

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

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__ ):


      """simple docstring"""



      lowerCamelCase								=		LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast
      print(F"""Saving a {tokenizer_class.__name__} to {tokenizer_path}.""" )
      lowerCamelCase								=		tokenizer_class(UpperCAmelCase__ )
      tokenizer.save_pretrained(UpperCAmelCase__ )

def     __lowercase(	):


      """simple docstring"""



      lowerCamelCase								=		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=UpperCAmelCase__   ,   help="Whether or not to save using `safetensors`." )
      lowerCamelCase								=		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								=		os.path.join(args.input_dir   ,   "tokenizer.model" )
      write_tokenizer(args.output_dir   ,   UpperCAmelCase__ )


if __name__ == "__main__":
     main()




import argparse
import os

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_task_guides.py
a_						:     Tuple	     =		'src/transformers'
a_						:     Any	     =		'docs/source/en/tasks'

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__ ):


      """simple docstring"""



      with open(UpperCAmelCase__   ,   "r"   ,   encoding="utf-8"   ,   newline="\n" ) as f:
            lowerCamelCase								=		f.readlines()
      # Find the start prompt.
      lowerCamelCase								=		0
      while not lines[start_index].startswith(UpperCAmelCase__ ):
            start_index += 1
      start_index += 1

      lowerCamelCase								=		start_index
      while not lines[end_index].startswith(UpperCAmelCase__ ):
            end_index += 1
      end_index -= 1

      while len(lines[start_index] ) <= 1:
            start_index += 1
      while len(lines[end_index] ) <= 1:
            end_index -= 1
      end_index += 1
      return "".join(lines[start_index:end_index] ), start_index, end_index, lines


# This is to make sure the transformers module imported is the one in the repo.
a_						:     Union[str, Any]	     =		direct_transformers_import(TRANSFORMERS_PATH)

a_						:     List[Any]	     =		{
    'asr.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES,
    'audio_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES,
    'language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
    'image_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES,
    'masked_language_modeling.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES,
    'multiple_choice.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES,
    'object_detection.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES,
    'question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES,
    'semantic_segmentation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES,
    'sequence_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES,
    'summarization.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    'token_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES,
    'translation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    'video_classification.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES,
    'document_question_answering.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES,
    'monocular_depth_estimation.md': transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES,
}

# This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any
# `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`).
a_						:     Union[str, Any]	     =		{
    'summarization.md': ('nllb',),
    'translation.md': ('nllb',),
}

def     __lowercase(	UpperCAmelCase__ ):


      """simple docstring"""



      lowerCamelCase								=		TASK_GUIDE_TO_MODELS[task_guide]
      lowerCamelCase								=		SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(UpperCAmelCase__   ,   set() )
      lowerCamelCase								=		{
          code: name
          for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
          if (code in model_maping_names or code in special_model_types)
      }
      return ", ".join([F"""[{name}](../model_doc/{code})""" for code, name in model_names.items()] ) + "\n"

def     __lowercase(	UpperCAmelCase__   ,   UpperCAmelCase__=False ):


      """simple docstring"""



      lowerCamelCase			,							lowerCamelCase			,							lowerCamelCase			,							lowerCamelCase								=		_find_text_in_file(
          filename=os.path.join(UpperCAmelCase__   ,   UpperCAmelCase__ )   ,   start_prompt="<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->"   ,   end_prompt="<!--End of the generated tip-->"   ,   )

      lowerCamelCase								=		get_model_list_for_task(UpperCAmelCase__ )

      if current_list != new_list:
            if overwrite:
                  with open(os.path.join(UpperCAmelCase__   ,   UpperCAmelCase__ )   ,   "w"   ,   encoding="utf-8"   ,   newline="\n" ) as f:
                        f.writelines(lines[:start_index] + [new_list] + lines[end_index:] )
            else:
                  raise ValueError(
                      F"""The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`"""
                      " to fix this." )


if __name__ == "__main__":
     a_						:     str	     =		argparse.ArgumentParser()
     parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
     a_						:     str	     =		parser.parse_args()

     for task_guide in TASK_GUIDE_TO_MODELS.keys():
          check_model_list_for_task(task_guide, args.fix_and_overwrite)