



'''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'''
							)
							__lowerCAmelCase						      =       None
__lowerCAmelCase						      =       {
    '''7B''': 11_008,
    '''13B''': 13_824,
    '''30B''': 17_920,
    '''65B''': 22_016,
    '''70B''': 28_672,
}
__lowerCAmelCase						      =       {
    '''7B''': 1,
    '''7Bf''': 1,
    '''13B''': 2,
    '''13Bf''': 2,
    '''30B''': 4,
    '''65B''': 8,
    '''70B''': 8,
    '''70Bf''': 8,
}



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_=1						,      lowerCAmelCase_=256  )      ->				List[str]:
		return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3  )  ) + multiple_of - 1) // multiple_of)



def 					__lowerCamelCase					(				lowerCAmelCase_  )      ->				Tuple:
		with open(lowerCAmelCase_						,      'r'  ) as f:
				return json.load(lowerCAmelCase_  )



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_  )      ->				List[str]:
		with open(lowerCAmelCase_						,      'w'  ) as f:
				json.dump(lowerCAmelCase_						,      lowerCAmelCase_  )



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_=True  )      ->				str:
		os.makedirs(lowerCAmelCase_						,      exist_ok=lowerCAmelCase_  )
		_a					:				Any				=							os.path.join(lowerCAmelCase_						,      'tmp'  )
		os.makedirs(lowerCAmelCase_						,      exist_ok=lowerCAmelCase_  )

		_a					:				List[str]				=							read_json(os.path.join(lowerCAmelCase_						,      'params.json'  )  )
		_a					:				str				=							NUM_SHARDS[model_size]
		_a					:				int				=							params['n_layers']
		_a					:				Union[str, Any]				=							params['n_heads']
		_a					:				Any				=							n_heads // num_shards
		_a					:				Dict				=							params['dim']
		_a					:				List[str]				=							dim // n_heads
		_a					:				Union[str, Any]				=							10_000.0
		_a					:				str				=							1.0 / (base ** (torch.arange(0						,      lowerCAmelCase_						,      2  ).float() / dims_per_head))

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

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

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

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

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

				_a					:				str				=							inv_freq
				for k, v in state_dict.items():
						_a					:				Tuple				=							filename
						param_count += v.numel()
				torch.save(lowerCAmelCase_						,      os.path.join(lowerCAmelCase_						,      lowerCAmelCase_  )  )

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

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

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

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

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



def 					__lowerCamelCase					(				lowerCAmelCase_						,      lowerCAmelCase_  )      ->				str:
		# Initialize the tokenizer based on the `spm` model
		_a					:				Dict				=							LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast
		print(f"""Saving a {tokenizer_class.__name__} to {tokenizer_path}."""  )
		_a					:				Dict				=							tokenizer_class(lowerCAmelCase_  )
		tokenizer.save_pretrained(lowerCAmelCase_  )



def 					__lowerCamelCase					(				)      ->				Optional[int]:
		_a					:				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=lowerCAmelCase_						,      help='Whether or not to save using `safetensors`.'  )
		_a					:				Tuple				=							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						,      )
		_a					:				List[Any]				=							os.path.join(args.input_dir						,      'tokenizer.model'  )
		write_tokenizer(args.output_dir						,      lowerCAmelCase_  )


if __name__ == "__main__":
							main()









'''simple docstring'''

import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
							import tensorflow as tf

							from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
							from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

__lowerCAmelCase						      =       logging.get_logger(__name__)

class      __magic_name__						(   enum.Enum       ):
							lowerCAmelCase	:   Union[str, Any]           = 0
							lowerCAmelCase	:   Dict           = 1

@add_end_docstrings(_UpperCamelCase       )
class      __magic_name__						(   _UpperCamelCase       ):
							lowerCAmelCase	:   int           = 'generated'







							def __init__(							self :	List[Any]     ,*_UpperCAmelCase :	Any     ,**_UpperCAmelCase :	Optional[int]		):
									super().__init__(*_UpperCAmelCase     ,**_UpperCAmelCase		)

									self.check_model_type(
									    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
									    if self.framework == 'tf'
									    else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING		)







							def 							__lowercase      (							self :	Optional[int]     ,_UpperCAmelCase :	List[Any]=None     ,_UpperCAmelCase :	Tuple=None     ,_UpperCAmelCase :	List[Any]=None     ,_UpperCAmelCase :	Union[str, Any]=None     ,_UpperCAmelCase :	Dict=None     ,_UpperCAmelCase :	Optional[int]=None     ,**_UpperCAmelCase :	List[Any]     ,):
									_a					:				Tuple				=							{}
									if truncation is not None:
											_a					:				Union[str, Any]				=							truncation

									_a					:				List[str]				=							generate_kwargs

									_a					:				Optional[Any]				=							{}
									if return_tensors is not None and return_type is None:
											_a					:				Optional[int]				=							ReturnType.TENSORS if return_tensors else ReturnType.TEXT
									if return_type is not None:
											_a					:				List[Any]				=							return_type

									if clean_up_tokenization_spaces is not None:
											_a					:				Optional[int]				=							clean_up_tokenization_spaces

									if stop_sequence is not None:
											_a					:				List[str]				=							self.tokenizer.encode(_UpperCAmelCase     ,add_special_tokens=_UpperCAmelCase		)
											if len(_UpperCAmelCase		) > 1:
													warnings.warn(
													    'Stopping on a multiple token sequence is not yet supported on transformers. The first token of'
													    ' the stop sequence will be used as the stop sequence string in the interim.'		)
											_a					:				List[Any]				=							stop_sequence_ids[0]

									return preprocess_params, forward_params, postprocess_params







							def 							__lowercase      (							self :	Any     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int		):
									return True







							def 							__lowercase      (							self :	str     ,*_UpperCAmelCase :	Any     ,_UpperCAmelCase :	Union[str, Any]		):
									_a					:				Optional[Any]				=							self.model.config.prefix if self.model.config.prefix is not None else ''
									if isinstance(args[0]     ,_UpperCAmelCase		):
											if self.tokenizer.pad_token_id is None:
													raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input'		)
											_a					:				Optional[Any]				=							([prefix + arg for arg in args[0]],)
											_a					:				Any				=							True

									elif isinstance(args[0]     ,_UpperCAmelCase		):
											_a					:				List[str]				=							(prefix + args[0],)
											_a					:				str				=							False
									else:
											raise ValueError(
											    F""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""		)
									_a					:				Optional[Any]				=							self.tokenizer(*_UpperCAmelCase     ,padding=_UpperCAmelCase     ,truncation=_UpperCAmelCase     ,return_tensors=self.framework		)
									# This is produced by tokenizers but is an invalid generate kwargs
									if "token_type_ids" in inputs:
											del inputs["token_type_ids"]
									return inputs







							def __call__(							self :	Optional[int]     ,*_UpperCAmelCase :	List[Any]     ,**_UpperCAmelCase :	Any		):
									_a					:				Tuple				=							super().__call__(*_UpperCAmelCase     ,**_UpperCAmelCase		)
									if (
									    isinstance(args[0]     ,_UpperCAmelCase		)
									    and all(isinstance(_UpperCAmelCase     ,_UpperCAmelCase		) for el in args[0]		)
									    and all(len(_UpperCAmelCase		) == 1 for res in result		)
									):
											return [res[0] for res in result]
									return result







							def 							__lowercase      (							self :	Tuple     ,_UpperCAmelCase :	Union[str, Any]     ,_UpperCAmelCase :	List[str]=TruncationStrategy.DO_NOT_TRUNCATE     ,**_UpperCAmelCase :	int		):
									_a					:				Dict				=							self._parse_and_tokenize(_UpperCAmelCase     ,truncation=_UpperCAmelCase     ,**_UpperCAmelCase		)
									return inputs







							def 							__lowercase      (							self :	Any     ,_UpperCAmelCase :	str     ,**_UpperCAmelCase :	Tuple		):
									if self.framework == "pt":
											_a	, _a					:				int				=							model_inputs['input_ids'].shape
									elif self.framework == "tf":
											_a	, _a					:				Dict				=							tf.shape(model_inputs['input_ids']		).numpy()

									_a					:				Optional[Any]				=							generate_kwargs.get('min_length'     ,self.model.config.min_length		)
									_a					:				Optional[int]				=							generate_kwargs.get('max_length'     ,self.model.config.max_length		)
									self.check_inputs(_UpperCAmelCase     ,generate_kwargs['min_length']     ,generate_kwargs['max_length']		)
									_a					:				List[str]				=							self.model.generate(**_UpperCAmelCase     ,**_UpperCAmelCase		)
									_a					:				int				=							output_ids.shape[0]
									if self.framework == "pt":
											_a					:				int				=							output_ids.reshape(_UpperCAmelCase     ,out_b // in_b     ,*output_ids.shape[1:]		)
									elif self.framework == "tf":
											_a					:				Any				=							tf.reshape(_UpperCAmelCase     ,(in_b, out_b // in_b, *output_ids.shape[1:])		)
									return {"output_ids": output_ids}







							def 							__lowercase      (							self :	Dict     ,_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Any=ReturnType.TEXT     ,_UpperCAmelCase :	Dict=False		):
									_a					:				Union[str, Any]				=							[]
									for output_ids in model_outputs["output_ids"][0]:
											if return_type == ReturnType.TENSORS:
													_a					:				int				=							{F"""{self.return_name}_token_ids""": output_ids}
											elif return_type == ReturnType.TEXT:
													_a					:				str				=							{
													    F"""{self.return_name}_text""": self.tokenizer.decode(
													        _UpperCAmelCase     ,skip_special_tokens=_UpperCAmelCase     ,clean_up_tokenization_spaces=_UpperCAmelCase     ,)
													}
											records.append(_UpperCAmelCase		)
									return records

@add_end_docstrings(_UpperCamelCase       )
class      __magic_name__						(   _UpperCamelCase       ):
							lowerCAmelCase	:   Any           = 'summary'







							def __call__(							self :	Any     ,*_UpperCAmelCase :	Union[str, Any]     ,**_UpperCAmelCase :	List[str]		):
									return super().__call__(*_UpperCAmelCase     ,**_UpperCAmelCase		)







							def 							__lowercase      (							self :	Optional[Any]     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int		):
									if max_length < min_length:
											logger.warning(F"""Your min_length={min_length} must be inferior than your max_length={max_length}."""		)

									if input_length < max_length:
											logger.warning(
											    F"""Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is """
											    'a summarization task, where outputs shorter than the input are typically wanted, you might '
											    F"""consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})"""		)

@add_end_docstrings(_UpperCamelCase       )
class      __magic_name__						(   _UpperCamelCase       ):
							lowerCAmelCase	:   List[Any]           = 'translation'







							def 							__lowercase      (							self :	Optional[int]     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	int		):
									if input_length > 0.9 * max_length:
											logger.warning(
											    F"""Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider """
											    'increasing your max_length manually, e.g. translator(\'...\', max_length=400)'		)
									return True







							def 							__lowercase      (							self :	Any     ,*_UpperCAmelCase :	Any     ,_UpperCAmelCase :	List[str]=TruncationStrategy.DO_NOT_TRUNCATE     ,_UpperCAmelCase :	Optional[int]=None     ,_UpperCAmelCase :	Any=None		):
									if getattr(self.tokenizer     ,'_build_translation_inputs'     ,_UpperCAmelCase		):
											return self.tokenizer._build_translation_inputs(
											    *_UpperCAmelCase     ,return_tensors=self.framework     ,truncation=_UpperCAmelCase     ,src_lang=_UpperCAmelCase     ,tgt_lang=_UpperCAmelCase		)
									else:
											return super()._parse_and_tokenize(*_UpperCAmelCase     ,truncation=_UpperCAmelCase		)







							def 							__lowercase      (							self :	Tuple     ,_UpperCAmelCase :	List[Any]=None     ,_UpperCAmelCase :	List[str]=None     ,**_UpperCAmelCase :	Dict		):
									_a	, _a	, _a					:				str				=							super()._sanitize_parameters(**_UpperCAmelCase		)
									if src_lang is not None:
											_a					:				Optional[int]				=							src_lang
									if tgt_lang is not None:
											_a					:				List[Any]				=							tgt_lang
									if src_lang is None and tgt_lang is None:
											# Backward compatibility, direct arguments use is preferred.
											_a					:				int				=							kwargs.get('task'     ,self.task		)
											_a					:				int				=							task.split('_'		)
											if task and len(_UpperCAmelCase		) == 4:
													# translation, XX, to YY
													_a					:				List[Any]				=							items[1]
													_a					:				Any				=							items[3]
									return preprocess_params, forward_params, postprocess_params







							def __call__(							self :	Optional[Any]     ,*_UpperCAmelCase :	Tuple     ,**_UpperCAmelCase :	Optional[int]		):
									return super().__call__(*_UpperCAmelCase     ,**_UpperCAmelCase		)




