



import argparse
import json
import os
import re

import torch

from transformers import BloomConfig, BloomModel
from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME
from transformers.utils import logging


logging.set_verbosity_info()

_lowercase     							=						[
    '''word_embeddings_layernorm.weight''',
    '''word_embeddings_layernorm.bias''',
    '''input_layernorm.weight''',
    '''input_layernorm.bias''',
    '''post_attention_layernorm.weight''',
    '''post_attention_layernorm.bias''',
    '''self_attention.dense.bias''',
    '''mlp.dense_4h_to_h.bias''',
    '''ln_f.weight''',
    '''ln_f.bias''',
]

_lowercase     							=						[
    '''mlp.dense_4h_to_h.weight''',
    '''self_attention.dense.weight''',
]






def      _A				(UpperCamelCase		:      int      ,			UpperCamelCase		:      List[str] )  ->int:



			'''simple docstring'''







			lowerCamelCase__ :      Optional[int]								=		{
			    """word_embeddings.weight""": """word_embeddings.weight""",
			    """word_embeddings.norm.weight""": """word_embeddings_layernorm.weight""",
			    """word_embeddings.norm.bias""": """word_embeddings_layernorm.bias""",
			    """weight""": """ln_f.weight""",
			    """bias""": """ln_f.bias""",
			}

			if key in layer_rename_map:
						return layer_rename_map[key]

			# Handle transformer blocks
			lowerCamelCase__ :      str								=		int(re.match(r""".*layer_(\d*).*"""      ,			UpperCamelCase )[1] )
			layer_number -= 3
			return f"h.{layer_number}." + key






def      _A				(UpperCamelCase		:      Optional[int] )  ->List[Any]:



			'''simple docstring'''







			if dtype == torch.bool:
						return 1 / 8
			lowerCamelCase__ :      Union[str, Any]								=		re.search(r"""[^\d](\d+)$"""      ,			str(UpperCamelCase ) )
			if bit_search is None:
						raise ValueError(f"`dtype` is not a valid dtype: {dtype}." )
			lowerCamelCase__ :      Dict								=		int(bit_search.groups()[0] )
			return bit_size // 8






def      _A				(UpperCamelCase		:      Tuple      ,			UpperCamelCase		:      Optional[int]      ,			UpperCamelCase		:      Optional[int]      ,			UpperCamelCase		:      int      ,			UpperCamelCase		:      Optional[Any] )  ->Dict:



			'''simple docstring'''







			if bloom_config_file == "":
						lowerCamelCase__ :      Union[str, Any]								=		BloomConfig()
			else:
						lowerCamelCase__ :      Any								=		BloomConfig.from_json_file(UpperCamelCase )

			if shard_model:
						lowerCamelCase__ :      Dict								=		os.listdir(UpperCamelCase )
						lowerCamelCase__ :      List[str]								=		sorted(filter(lambda UpperCamelCase : s.startswith("""layer""" ) and "model_00" in s      ,			UpperCamelCase ) )

						lowerCamelCase__ :      Optional[int]								=		{"""weight_map""": {}, """metadata""": {}}
						lowerCamelCase__ :      Dict								=		0

						lowerCamelCase__ :      Dict								=		None

						lowerCamelCase__ :      Union[str, Any]								=		BloomConfig()

						for j, file in enumerate(UpperCamelCase ):
									print("""Processing file: {}""".format(UpperCamelCase ) )
									lowerCamelCase__ :      List[Any]								=		None

									for i in range(UpperCamelCase ):
												# load all TP files
												lowerCamelCase__ :      Optional[int]								=		file.replace("""model_00"""      ,			f"model_0{i}" )
												lowerCamelCase__ :      Any								=		torch.load(os.path.join(UpperCamelCase      ,			UpperCamelCase )      ,			map_location="""cpu""" )

												# Rename keys in the transformers names
												lowerCamelCase__ :      Dict								=		list(temp.keys() )
												for key in keys:
															lowerCamelCase__ :      Union[str, Any]								=		temp.pop(UpperCamelCase )

												if tensors is None:
															lowerCamelCase__ :      List[Any]								=		temp
												else:
															for key in tensors.keys():
																		if any(key.endswith(UpperCamelCase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ):
																					# We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425)
																					tensors[key] += temp[key]
																		else:
																					# Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel
																					lowerCamelCase__ :      Tuple								=		1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0
																					# We concatenate these weights accross TP ranks
																					lowerCamelCase__ :      Optional[Any]								=		torch.cat([tensors[key], temp[key]]      ,			dim=UpperCamelCase )

            # Divide by the number of TP the weights we want to average
									for key in tensors.keys():
												if any(key.endswith(UpperCamelCase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ):
															lowerCamelCase__ :      List[str]								=		tensors[key] / pretraining_tp
									torch.save(
									    UpperCamelCase      ,			os.path.join(
									        UpperCamelCase      ,			"""pytorch_model_{}-of-{}.bin""".format(str(j + 1 ).zfill(5 )      ,			str(len(UpperCamelCase ) ).zfill(5 ) )      ,			)      ,			)

									for key in tensors.keys():
												lowerCamelCase__ :      Optional[int]								=		tensors[key]
												total_size += value.numel() * get_dtype_size(value.dtype )
												if key not in index_dict["weight_map"]:
															lowerCamelCase__ :      int								=		"""pytorch_model_{}-of-{}.bin""".format(
															    str(j + 1 ).zfill(5 )      ,			str(len(UpperCamelCase ) ).zfill(5 ) )

						lowerCamelCase__ :      Tuple								=		BloomConfig()
						lowerCamelCase__ :      Optional[int]								=		pytorch_dump_folder_path + """/""" + CONFIG_NAME
						lowerCamelCase__ :      Dict								=		total_size
						with open(UpperCamelCase      ,			"""w"""      ,			encoding="""utf-8""" ) as f:
									f.write(config.to_json_string() )
						with open(os.path.join(UpperCamelCase      ,			WEIGHTS_NAME + """.index.json""" )      ,			"""w"""      ,			encoding="""utf-8""" ) as f:
									lowerCamelCase__ :      Union[str, Any]								=		json.dumps(UpperCamelCase      ,			indent=2      ,			sort_keys=UpperCamelCase ) + """\n"""
									f.write(UpperCamelCase )
			else:
						lowerCamelCase__ :      List[str]								=		BloomModel(UpperCamelCase )

						lowerCamelCase__ :      int								=		os.listdir(UpperCamelCase )
						lowerCamelCase__ :      int								=		sorted(filter(lambda UpperCamelCase : s.startswith("""layer""" ) and "model_00" in s      ,			UpperCamelCase ) )

						lowerCamelCase__ :      int								=		None
						for i, file in enumerate(UpperCamelCase ):
									lowerCamelCase__ :      Any								=		None
									for i in range(UpperCamelCase ):
												# load all TP files
												lowerCamelCase__ :      int								=		file.replace("""model_00"""      ,			f"model_0{i}" )
												lowerCamelCase__ :      Dict								=		torch.load(os.path.join(UpperCamelCase      ,			UpperCamelCase )      ,			map_location="""cpu""" )

												# Rename keys in the transformers names
												lowerCamelCase__ :      Any								=		list(temp.keys() )
												for key in keys:
															lowerCamelCase__ :      Optional[int]								=		temp.pop(UpperCamelCase )

												if tensors is None:
															lowerCamelCase__ :      int								=		temp
												else:
															for key in tensors.keys():
																		# We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425)
																		if any(key.endswith(UpperCamelCase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ):
																					tensors[key] += temp[key]
																		else:
																					# Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel
																					lowerCamelCase__ :      Optional[Any]								=		1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0
																					# We concatenate these weights accross TP ranks
																					lowerCamelCase__ :      List[Any]								=		torch.cat([tensors[key], temp[key]]      ,			dim=UpperCamelCase )

            # Divide by the number of TP the weights we want to average
									for key in tensors.keys():
												if any(key.endswith(UpperCamelCase ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ):
															lowerCamelCase__ :      Dict								=		tensors[key] / pretraining_tp

									lowerCamelCase__ :      List[Any]								=		model.load_state_dict(UpperCamelCase      ,			strict=UpperCamelCase )
									assert not other_keys.unexpected_keys, f"The keys {other_keys.unexpected_keys} are unexpected"
									if missing_keys is None:
												lowerCamelCase__ :      Optional[Any]								=		set(other_keys.missing_keys )
									else:
												lowerCamelCase__ :      str								=		missing_keys.intersection(set(other_keys.missing_keys ) )

						assert not missing_keys, f"The keys {missing_keys} are missing"

						# Save pytorch-model
						os.makedirs(UpperCamelCase      ,			exist_ok=UpperCamelCase )
						lowerCamelCase__ :      Optional[int]								=		pytorch_dump_folder_path + """/""" + WEIGHTS_NAME
						lowerCamelCase__ :      Optional[int]								=		pytorch_dump_folder_path + """/""" + CONFIG_NAME
						print(f"Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}" )
						if config.torch_dtype is not None:
									lowerCamelCase__ :      Optional[Any]								=		model.to(config.torch_dtype )
						torch.save(model.state_dict()      ,			UpperCamelCase )
						print(f"Save configuration file to {pytorch_config_dump_path}" )
						with open(UpperCamelCase      ,			"""w"""      ,			encoding="""utf-8""" ) as f:
									f.write(config.to_json_string() )


if __name__ == "__main__":
				_lowercase     							=						argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    '''--bloom_checkpoint_path''',
				    default=None,
				    type=str,
				    required=True,
				    help='''Path to the Megatron-LM checkpoint path.''',
				)
				parser.add_argument(
				    '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
				)
				parser.add_argument(
				    '''--bloom_config_file''',
				    default='''''',
				    type=str,
				    help=(
				        '''An optional config json file corresponding to the pre-trained model. \n'''
				        '''This specifies the model architecture.'''
				    ),
				)
				parser.add_argument(
				    '''--shard_model''',
				    action='''store_true''',
				    help='''An optional setting to shard the output model \nThis enables sharding the converted checkpoint''',
				)
				parser.add_argument(
				    '''--pretraining_tp''',
				    default=4,
				    type=int,
				    help='''Pretraining TP rank that has been used when training the model in Megatron-LM \n''',
				)
				_lowercase     							=						parser.parse_args()
				convert_bloom_checkpoint_to_pytorch(
				    args.bloom_checkpoint_path,
				    args.bloom_config_file,
				    args.pytorch_dump_folder_path,
				    args.shard_model,
				    args.pretraining_tp,
				)








import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


_lowercase     							=						version.parse(importlib_metadata.version('''nltk'''))
if NLTK_VERSION >= version.Version('''3.6.4'''):
				from nltk import word_tokenize


_lowercase     							=						'''\
@inproceedings{banarjee2005,
  title     = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},
  author    = {Banerjee, Satanjeev  and Lavie, Alon},
  booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},
  month     = jun,
  year      = {2005},
  address   = {Ann Arbor, Michigan},
  publisher = {Association for Computational Linguistics},
  url       = {https://www.aclweb.org/anthology/W05-0909},
  pages     = {65--72},
}
'''

_lowercase     							=						'''\
METEOR, an automatic metric for machine translation evaluation
that is based on a generalized concept of unigram matching between the
machine-produced translation and human-produced reference translations.
Unigrams can be matched based on their surface forms, stemmed forms,
and meanings; furthermore, METEOR can be easily extended to include more
advanced matching strategies. Once all generalized unigram matches
between the two strings have been found, METEOR computes a score for
this matching using a combination of unigram-precision, unigram-recall, and
a measure of fragmentation that is designed to directly capture how
well-ordered the matched words in the machine translation are in relation
to the reference.

METEOR gets an R correlation value of 0.347 with human evaluation on the Arabic
data and 0.331 on the Chinese data. This is shown to be an improvement on
using simply unigram-precision, unigram-recall and their harmonic F1
combination.
'''

_lowercase     							=						'''
Computes METEOR score of translated segments against one or more references.
Args:
    predictions: list of predictions to score. Each prediction
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.
    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9
    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3
    gamma: Relative weight assigned to fragmentation penalty. default: 0.5
Returns:
    \'meteor\': meteor score.
Examples:

    >>> meteor = datasets.load_metric(\'meteor\')
    >>> predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party"]
    >>> references = ["It is a guide to action that ensures that the military will forever heed Party commands"]
    >>> results = meteor.compute(predictions=predictions, references=references)
    >>> print(round(results["meteor"], 4))
    0.6944
'''
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION					, _KWARGS_DESCRIPTION      )
class 				__A					( datasets.Metric      ):





			def        _snake_case    (self				):
						return datasets.MetricInfo(
						    description=_DESCRIPTION							,    citation=_CITATION							,    inputs_description=_KWARGS_DESCRIPTION							,    features=datasets.Features(
						        {
						            """predictions""": datasets.Value("""string"""							,    id="""sequence"""				),
						            """references""": datasets.Value("""string"""							,    id="""sequence"""				),
						        }				)							,    codebase_urls=["""https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"""]							,    reference_urls=[
						        """https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score""",
						        """https://en.wikipedia.org/wiki/METEOR""",
						    ]							,    )





			def        _snake_case    (self							,    __magic_name__				):
						import nltk

						nltk.download("""wordnet"""				)
						if NLTK_VERSION >= version.Version("""3.6.5"""				):
									nltk.download("""punkt"""				)
						if NLTK_VERSION >= version.Version("""3.6.6"""				):
									nltk.download("""omw-1.4"""				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__=0.9							,    __magic_name__=3							,    __magic_name__=0.5				):
						if NLTK_VERSION >= version.Version("""3.6.5"""				):
									lowerCamelCase__ :      List[str]								=		[
									    meteor_score.single_meteor_score(
									        word_tokenize(__magic_name__				)							,    word_tokenize(__magic_name__				)							,    alpha=__magic_name__							,    beta=__magic_name__							,    gamma=__magic_name__				)
									    for ref, pred in zip(__magic_name__							,    __magic_name__				)
									]
						else:
									lowerCamelCase__ :      int								=		[
									    meteor_score.single_meteor_score(__magic_name__							,    __magic_name__							,    alpha=__magic_name__							,    beta=__magic_name__							,    gamma=__magic_name__				)
									    for ref, pred in zip(__magic_name__							,    __magic_name__				)
									]

						return {"meteor": np.mean(__magic_name__				)}



