


'''simple docstring'''



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      :		List[Any]			=      [
    '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      :		Optional[int]			=      [
    'mlp.dense_4h_to_h.weight',
    'self_attention.dense.weight',
]






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Tuple		, _SCREAMING_SNAKE_CASE :  Optional[int]  ):
					__a     :			List[Any]          =    {
					    '''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
					__a     :			Union[str, Any]          =    int(re.match(r'.*layer_(\d*).*'		, _lowercase  )[1]  )
					layer_number -= 3
					return F"""h.{layer_number}.""" + key






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  int  ):


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






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Optional[int]		, _SCREAMING_SNAKE_CASE :  Tuple		, _SCREAMING_SNAKE_CASE :  Tuple		, _SCREAMING_SNAKE_CASE :  Optional[int]		, _SCREAMING_SNAKE_CASE :  Union[str, Any]  ):


					if bloom_config_file == "":
										__a     :			Tuple          =    BloomConfig()
					else:
										__a     :			Optional[int]          =    BloomConfig.from_json_file(_lowercase  )

					if shard_model:
										__a     :			Any          =    os.listdir(_lowercase  )
										__a     :			Union[str, Any]          =    sorted(filter(lambda _SCREAMING_SNAKE_CASE  : s.startswith('layer'  ) and "model_00" in s		, _lowercase  )  )

										__a     :			Any          =    {'''weight_map''': {}, '''metadata''': {}}
										__a     :			List[str]          =    0

										__a     :			Any          =    None

										__a     :			Optional[int]          =    BloomConfig()

										for j, file in enumerate(_lowercase  ):
															print('Processing file: {}'.format(_lowercase  )  )
															__a     :			Optional[Any]          =    None

															for i in range(_lowercase  ):
																				# load all TP files
																				__a     :			Tuple          =    file.replace('model_00'		, F"""model_0{i}"""  )
																				__a     :			Any          =    torch.load(os.path.join(_lowercase		, _lowercase  )		, map_location='cpu'  )

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

																				if tensors is None:
																									__a     :			Union[str, Any]          =    temp
																				else:
																									for key in tensors.keys():
																														if any(key.endswith(_lowercase  ) 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
																																			__a     :			int          =    1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN  ) else 0
																																			# We concatenate these weights accross TP ranks
																																			__a     :			Tuple          =    torch.cat([tensors[key], temp[key]]		, dim=_lowercase  )

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

															for key in tensors.keys():
																				__a     :			Union[str, Any]          =    tensors[key]
																				total_size += value.numel() * get_dtype_size(value.dtype  )
																				if key not in index_dict["weight_map"]:
																									__a     :			List[str]          =    '''pytorch_model_{}-of-{}.bin'''.format(
																									    str(j + 1  ).zfill(5  )		, str(len(_lowercase  )  ).zfill(5  )  )

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

										__a     :			Tuple          =    os.listdir(_lowercase  )
										__a     :			Union[str, Any]          =    sorted(filter(lambda _SCREAMING_SNAKE_CASE  : s.startswith('layer'  ) and "model_00" in s		, _lowercase  )  )

										__a     :			Any          =    None
										for i, file in enumerate(_lowercase  ):
															__a     :			Optional[Any]          =    None
															for i in range(_lowercase  ):
																				# load all TP files
																				__a     :			List[Any]          =    file.replace('model_00'		, F"""model_0{i}"""  )
																				__a     :			Optional[int]          =    torch.load(os.path.join(_lowercase		, _lowercase  )		, map_location='cpu'  )

																				# Rename keys in the transformers names
																				__a     :			str          =    list(temp.keys()  )
																				for key in keys:
																									__a     :			Dict          =    temp.pop(_lowercase  )

																				if tensors is None:
																									__a     :			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(_lowercase  ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH  ):
																																			tensors[key] += temp[key]
																														else:
																																			# Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel
																																			__a     :			Optional[int]          =    1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN  ) else 0
																																			# We concatenate these weights accross TP ranks
																																			__a     :			List[str]          =    torch.cat([tensors[key], temp[key]]		, dim=_lowercase  )

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

															__a     :			Tuple          =    model.load_state_dict(_lowercase		, strict=_lowercase  )
															assert not other_keys.unexpected_keys, F"""The keys {other_keys.unexpected_keys} are unexpected"""
															if missing_keys is None:
																				__a     :			Union[str, Any]          =    set(other_keys.missing_keys  )
															else:
																				__a     :			Dict          =    missing_keys.intersection(set(other_keys.missing_keys  )  )

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

										# Save pytorch-model
										os.makedirs(_lowercase		, exist_ok=_lowercase  )
										__a     :			str          =    pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
										__a     :			Dict          =    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:
															__a     :			Optional[int]          =    model.to(config.torch_dtype  )
										torch.save(model.state_dict()		, _lowercase  )
										print(F"""Save configuration file to {pytorch_config_dump_path}"""  )
										with open(_lowercase		, 'w'		, encoding='utf-8'  ) as f:
															f.write(config.to_json_string()  )


if __name__ == "__main__":
	__lowercase      :		Optional[int]			=      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      :		List[Any]			=      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,
	)









# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import torch

from ..models.clipseg import CLIPSegForImageSegmentation
from ..utils import is_vision_available, requires_backends
from .base import PipelineTool


if is_vision_available():
     from PIL import Image

class     __a(	_a  ):






    """simple docstring"""





    lowerCAmelCase         = (
        '''This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.'''
        '''It takes two arguments named `image` which should be the original image, and `label` which should be a text '''
        '''describing the elements what should be identified in the segmentation mask. The tool returns the mask.'''
    )
    lowerCAmelCase         = '''CIDAS/clipseg-rd64-refined'''
    lowerCAmelCase         = '''image_segmenter'''
    lowerCAmelCase         = CLIPSegForImageSegmentation

    lowerCAmelCase         = ['''image''', '''text''']
    lowerCAmelCase         = ['''image''']





    def __init__( self  ,*_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )					->						Dict:
         requires_backends(self  ,['''vision''']    )
         super().__init__(*_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )





    def 				a__ ( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )					->						Tuple:
         return self.pre_processor(text=[label]  ,images=[image]  ,padding=_SCREAMING_SNAKE_CASE  ,return_tensors='''pt'''    )





    def 				a__ ( self  ,_SCREAMING_SNAKE_CASE    )					->						str:
         with torch.no_grad():
              UpperCAmelCase_     :				Dict  =     self.model(**_SCREAMING_SNAKE_CASE    ).logits
         return logits





    def 				a__ ( self  ,_SCREAMING_SNAKE_CASE    )					->						Dict:
         UpperCAmelCase_     :				Dict  =     outputs.cpu().detach().numpy()
         UpperCAmelCase_     :				Any  =     0
         UpperCAmelCase_     :				List[Any]  =     1
         return Image.fromarray((array * 255).astype(np.uinta    )    )