

'''simple docstring'''


import json
import os
from functools import lru_cache
from typing import Dict, List, Optional, Tuple, Union

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...tokenization_utils_base import BatchEncoding, EncodedInput
from ...utils import PaddingStrategy, logging


lowerCAmelCase__										= logging.get_logger(__name__)


lowerCAmelCase__										= {"vocab_file": "vocab.json", "merges_file": "merges.txt"}

# See all LED models at https://huggingface.co/models?filter=LED
lowerCAmelCase__										= {
    "vocab_file": {
        "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json",
    },
    "merges_file": {
        "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt",
    },
    "tokenizer_file": {
        "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json",
    },
}

lowerCAmelCase__										= {
    "allenai/led-base-16384": 16384,
}







@lru_cache()
# Copied from transformers.models.bart.tokenization_bart.bytes_to_unicode
def 						SCREAMING_SNAKE_CASE(  )    ->    str:
	UpperCAmelCase_		:      Optional[Any]       	=					(
	    list(range(ord('!'  )   ,ord('~'  ) + 1  )  ) + list(range(ord('¡'  )   ,ord('¬'  ) + 1  )  ) + list(range(ord('®'  )   ,ord('ÿ'  ) + 1  )  )
	)
	UpperCAmelCase_		:      List[Any]       	=					bs[:]
	UpperCAmelCase_		:      str       	=					0
	for b in range(2**8  ):
		if b not in bs:
			bs.append(UpperCamelCase  )
			cs.append(2**8 + n  )
			n += 1
	UpperCAmelCase_		:      Any       	=					[chr(UpperCamelCase  ) for n in cs]
	return dict(zip(UpperCamelCase   ,UpperCamelCase  )  )







def 						SCREAMING_SNAKE_CASE(  UpperCamelCase  )    ->    Dict:
	UpperCAmelCase_		:      List[Any]       	=					set()
	UpperCAmelCase_		:      List[Any]       	=					word[0]
	for char in word[1:]:
		pairs.add((prev_char, char)  )
		UpperCAmelCase_		:      List[str]       	=					char
	return pairs







class 					lowercase						(  a_ ):
							_lowerCamelCase		:      Any=   VOCAB_FILES_NAMES
							_lowerCamelCase		:      Tuple=   PRETRAINED_VOCAB_FILES_MAP
							_lowerCamelCase		:      Optional[int]=   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							_lowerCamelCase		:      Dict=   ["input_ids", "attention_mask"]


							def __init__(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case="replace"    ,			_snake_case="<s>"    ,			_snake_case="</s>"    ,			_snake_case="</s>"    ,			_snake_case="<s>"    ,			_snake_case="<unk>"    ,			_snake_case="<pad>"    ,			_snake_case="<mask>"    ,			_snake_case=False    ,			**_snake_case    ,			)							->				Any:
								UpperCAmelCase_		:      Dict       	=					AddedToken(_snake_case    ,			lstrip=_snake_case    ,			rstrip=_snake_case) if isinstance(_snake_case    ,			_snake_case) else bos_token
								UpperCAmelCase_		:      Union[str, Any]       	=					AddedToken(_snake_case    ,			lstrip=_snake_case    ,			rstrip=_snake_case) if isinstance(_snake_case    ,			_snake_case) else eos_token
								UpperCAmelCase_		:      Any       	=					AddedToken(_snake_case    ,			lstrip=_snake_case    ,			rstrip=_snake_case) if isinstance(_snake_case    ,			_snake_case) else sep_token
								UpperCAmelCase_		:      Dict       	=					AddedToken(_snake_case    ,			lstrip=_snake_case    ,			rstrip=_snake_case) if isinstance(_snake_case    ,			_snake_case) else cls_token
								UpperCAmelCase_		:      Any       	=					AddedToken(_snake_case    ,			lstrip=_snake_case    ,			rstrip=_snake_case) if isinstance(_snake_case    ,			_snake_case) else unk_token
								UpperCAmelCase_		:      Tuple       	=					AddedToken(_snake_case    ,			lstrip=_snake_case    ,			rstrip=_snake_case) if isinstance(_snake_case    ,			_snake_case) else pad_token

								# Mask token behave like a normal word, i.e. include the space before it
								UpperCAmelCase_		:      Union[str, Any]       	=					AddedToken(_snake_case    ,			lstrip=_snake_case    ,			rstrip=_snake_case) if isinstance(_snake_case    ,			_snake_case) else mask_token

								super().__init__(
								    errors=_snake_case    ,			bos_token=_snake_case    ,			eos_token=_snake_case    ,			unk_token=_snake_case    ,			sep_token=_snake_case    ,			cls_token=_snake_case    ,			pad_token=_snake_case    ,			mask_token=_snake_case    ,			add_prefix_space=_snake_case    ,			**_snake_case    ,			)

								with open(_snake_case    ,			encoding='utf-8') as vocab_handle:
									UpperCAmelCase_		:      Tuple       	=					json.load(_snake_case)
								UpperCAmelCase_		:      Any       	=					{v: k for k, v in self.encoder.items()}
								UpperCAmelCase_		:      List[str]       	=					errors  # how to handle errors in decoding
								UpperCAmelCase_		:      Union[str, Any]       	=					bytes_to_unicode()
								UpperCAmelCase_		:      int       	=					{v: k for k, v in self.byte_encoder.items()}
								with open(_snake_case    ,			encoding='utf-8') as merges_handle:
									UpperCAmelCase_		:      Tuple       	=					merges_handle.read().split('\n')[1:-1]
								UpperCAmelCase_		:      str       	=					[tuple(merge.split()) for merge in bpe_merges]
								UpperCAmelCase_		:      Optional[Any]       	=					dict(zip(_snake_case    ,			range(len(_snake_case))))
								UpperCAmelCase_		:      str       	=					{}
								UpperCAmelCase_		:      Dict       	=					add_prefix_space

								# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
								UpperCAmelCase_		:      Any       	=					re.compile(r'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+')


							@property
							# Copied from transformers.models.bart.tokenization_bart.BartTokenizer.vocab_size
							def 						_snake_case							(			self)							->				Optional[int]:
								return len(self.encoder)


							def 						_snake_case							(			self)							->				Optional[Any]:
								return dict(self.encoder    ,			**self.added_tokens_encoder)


							def 						_snake_case							(			self    ,			_snake_case)							->				Optional[int]:
								if token in self.cache:
									return self.cache[token]
								UpperCAmelCase_		:      Union[str, Any]       	=					tuple(_snake_case)
								UpperCAmelCase_		:      Any       	=					get_pairs(_snake_case)

								if not pairs:
									return token

								while True:
									UpperCAmelCase_		:      List[str]       	=					min(_snake_case    ,			key=lambda _snake_case: self.bpe_ranks.get(_snake_case    ,			float('inf')))
									if bigram not in self.bpe_ranks:
										break
									UpperCAmelCase_	,    UpperCAmelCase_		:      int       	=					bigram
									UpperCAmelCase_		:      Tuple       	=					[]
									UpperCAmelCase_		:      List[str]       	=					0
									while i < len(_snake_case):
										try:
											UpperCAmelCase_		:      List[str]       	=					word.index(_snake_case    ,			_snake_case)
										except ValueError:
											new_word.extend(word[i:])
											break
										else:
											new_word.extend(word[i:j])
											UpperCAmelCase_		:      Union[str, Any]       	=					j

										if word[i] == first and i < len(_snake_case) - 1 and word[i + 1] == second:
											new_word.append(first + second)
											i += 2
										else:
											new_word.append(word[i])
											i += 1
									UpperCAmelCase_		:      str       	=					tuple(_snake_case)
									UpperCAmelCase_		:      Dict       	=					new_word
									if len(_snake_case) == 1:
										break
									else:
										UpperCAmelCase_		:      int       	=					get_pairs(_snake_case)
								UpperCAmelCase_		:      Tuple       	=					' '.join(_snake_case)
								UpperCAmelCase_		:      Optional[int]       	=					word
								return word


							def 						_snake_case							(			self    ,			_snake_case)							->				int:
								UpperCAmelCase_		:      str       	=					[]
								for token in re.findall(self.pat    ,			_snake_case):
									UpperCAmelCase_		:      Tuple       	=					''.join(
									    self.byte_encoder[b] for b in token.encode('utf-8'))  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
									bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_snake_case).split(' '))
								return bpe_tokens


							def 						_snake_case							(			self    ,			_snake_case)							->				Union[str, Any]:
								return self.encoder.get(_snake_case    ,			self.encoder.get(self.unk_token))


							def 						_snake_case							(			self    ,			_snake_case)							->				Optional[int]:
								return self.decoder.get(_snake_case)


							def 						_snake_case							(			self    ,			_snake_case)							->				Optional[int]:
								UpperCAmelCase_		:      List[str]       	=					''.join(_snake_case)
								UpperCAmelCase_		:      List[str]       	=					bytearray([self.byte_decoder[c] for c in text]).decode('utf-8'    ,			errors=self.errors)
								return text


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case = None)							->				Tuple[str]:
								if not os.path.isdir(_snake_case):
									logger.error(F"""Vocabulary path ({save_directory}) should be a directory""")
									return
								UpperCAmelCase_		:      Optional[Any]       	=					os.path.join(
								    _snake_case    ,			(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])
								UpperCAmelCase_		:      Dict       	=					os.path.join(
								    _snake_case    ,			(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'])

								with open(_snake_case    ,			'w'    ,			encoding='utf-8') as f:
									f.write(json.dumps(self.encoder    ,			indent=2    ,			sort_keys=_snake_case    ,			ensure_ascii=_snake_case) + '\n')

								UpperCAmelCase_		:      Any       	=					0
								with open(_snake_case    ,			'w'    ,			encoding='utf-8') as writer:
									writer.write('#version: 0.2\n')
									for bpe_tokens, token_index in sorted(self.bpe_ranks.items()    ,			key=lambda _snake_case: kv[1]):
										if index != token_index:
											logger.warning(
											    F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."""
											    ' Please check that the tokenizer is not corrupted!')
											UpperCAmelCase_		:      Union[str, Any]       	=					token_index
										writer.write(' '.join(_snake_case) + '\n')
										index += 1

								return vocab_file, merge_file


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case = None)							->				List[int]:
								if token_ids_a is None:
									return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
								UpperCAmelCase_		:      str       	=					[self.cls_token_id]
								UpperCAmelCase_		:      str       	=					[self.sep_token_id]
								return cls + token_ids_a + sep + sep + token_ids_a + sep


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case = None    ,			_snake_case = False)							->				List[int]:
								if already_has_special_tokens:
									return super().get_special_tokens_mask(
									    token_ids_a=_snake_case    ,			token_ids_a=_snake_case    ,			already_has_special_tokens=_snake_case)

								if token_ids_a is None:
									return [1] + ([0] * len(_snake_case)) + [1]
								return [1] + ([0] * len(_snake_case)) + [1, 1] + ([0] * len(_snake_case)) + [1]


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case = None)							->				List[int]:
								UpperCAmelCase_		:      List[str]       	=					[self.sep_token_id]
								UpperCAmelCase_		:      Dict       	=					[self.cls_token_id]

								if token_ids_a is None:
									return len(cls + token_ids_a + sep) * [0]
								return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0]


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case=False    ,			**_snake_case)							->				int:
								UpperCAmelCase_		:      List[Any]       	=					kwargs.pop('add_prefix_space'    ,			self.add_prefix_space)
								if (is_split_into_words or add_prefix_space) and (len(_snake_case) > 0 and not text[0].isspace()):
									UpperCAmelCase_		:      Dict       	=					' ' + text
								return (text, kwargs)





							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case = None    ,			_snake_case = PaddingStrategy.DO_NOT_PAD    ,			_snake_case = None    ,			_snake_case = None    ,			)							->				dict:
								UpperCAmelCase_		:      int       	=					super()._pad(
								    encoded_inputs=_snake_case    ,			max_length=_snake_case    ,			padding_strategy=_snake_case    ,			pad_to_multiple_of=_snake_case    ,			return_attention_mask=_snake_case    ,			)

								# Load from model defaults
								if return_attention_mask is None:
									UpperCAmelCase_		:      Union[str, Any]       	=					'attention_mask' in self.model_input_names

								if return_attention_mask and "global_attention_mask" in encoded_inputs:
									UpperCAmelCase_		:      int       	=					encoded_inputs[self.model_input_names[0]]
									# `global_attention_mask` need to have the same length as other (sequential) inputs.
									UpperCAmelCase_		:      List[Any]       	=					len(encoded_inputs['global_attention_mask']) != len(_snake_case)

									if needs_to_be_padded:
										UpperCAmelCase_		:      Union[str, Any]       	=					len(_snake_case) - len(encoded_inputs['global_attention_mask'])

										if self.padding_side == "right":
											# Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend`
											UpperCAmelCase_		:      Optional[int]       	=					(
											    encoded_inputs['global_attention_mask'] + [-1] * difference
											)
										elif self.padding_side == "left":
											UpperCAmelCase_		:      List[Any]       	=					[-1] * difference + encoded_inputs[
											    'global_attention_mask'
											]
										else:
											raise ValueError('Invalid padding strategy:' + str(self.padding_side))

								return encoded_inputs









'''simple docstring'''


import importlib.metadata
import warnings
from copy import deepcopy

from packaging import version

from ..utils import logging
from .import_utils import is_accelerate_available, is_bitsandbytes_available


if is_bitsandbytes_available():
						import bitsandbytes as bnb
						import torch
						import torch.nn as nn

						from ..pytorch_utils import ConvaD

if is_accelerate_available():
						from accelerate import init_empty_weights
						from accelerate.utils import find_tied_parameters

lowerCAmelCase__										= logging.get_logger(__name__)







def 						SCREAMING_SNAKE_CASE(  UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase=None   ,UpperCamelCase=None  )    ->    Any:
	# Recurse if needed
	if "." in tensor_name:
		UpperCAmelCase_		:      Tuple       	=					tensor_name.split('.'  )
		for split in splits[:-1]:
			UpperCAmelCase_		:      Optional[int]       	=					getattr(UpperCamelCase   ,UpperCamelCase  )
			if new_module is None:
				raise ValueError(f"""{module} has no attribute {split}."""  )
			UpperCAmelCase_		:      Union[str, Any]       	=					new_module
		UpperCAmelCase_		:      int       	=					splits[-1]

	if tensor_name not in module._parameters and tensor_name not in module._buffers:
		raise ValueError(f"""{module} does not have a parameter or a buffer named {tensor_name}."""  )
	UpperCAmelCase_		:      Tuple       	=					tensor_name in module._buffers
	UpperCAmelCase_		:      Any       	=					getattr(UpperCamelCase   ,UpperCamelCase  )

	if old_value.device == torch.device('meta'  ) and device not in ["meta", torch.device('meta'  )] and value is None:
		raise ValueError(f"""{tensor_name} is on the meta device, we need a `value` to put in on {device}."""  )

	UpperCAmelCase_		:      Any       	=					False
	UpperCAmelCase_		:      Any       	=					False
	if is_buffer or not is_bitsandbytes_available():
		UpperCAmelCase_		:      Dict       	=					False
		UpperCAmelCase_		:      Tuple       	=					False
	else:
		UpperCAmelCase_		:      List[str]       	=					hasattr(bnb.nn   ,'Params4bit'  ) and isinstance(module._parameters[tensor_name]   ,bnb.nn.Paramsabit  )
		UpperCAmelCase_		:      str       	=					isinstance(module._parameters[tensor_name]   ,bnb.nn.IntaParams  )

	if is_abit or is_abit:
		UpperCAmelCase_		:      Union[str, Any]       	=					module._parameters[tensor_name]
		if param.device.type != "cuda":
			if value is None:
				UpperCAmelCase_		:      List[str]       	=					old_value.to(UpperCamelCase  )
			elif isinstance(UpperCamelCase   ,torch.Tensor  ):
				UpperCAmelCase_		:      Any       	=					value.to('cpu'  )
				if value.dtype == torch.inta:
					UpperCAmelCase_		:      Dict       	=					version.parse(importlib.metadata.version('bitsandbytes'  )  ) > version.parse(
					    '0.37.2'  )
					if not is_abit_serializable:
						raise ValueError(
						    'Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. '
						    'Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`.'  )
			else:
				UpperCAmelCase_		:      List[Any]       	=					torch.tensor(UpperCamelCase   ,device='cpu'  )

			# Support models using `Conv1D` in place of `nn.Linear` (e.g. gpt2) by transposing the weight matrix prior to quantization.
			# Since weights are saved in the correct "orientation", we skip transposing when loading.
			if issubclass(module.source_cls   ,UpperCamelCase  ) and fpaa_statistics is None:
				UpperCAmelCase_		:      Dict       	=					new_value.T

			UpperCAmelCase_		:      Tuple       	=					old_value.__dict__
			if is_abit:
				UpperCAmelCase_		:      Any       	=					bnb.nn.IntaParams(UpperCamelCase   ,requires_grad=UpperCamelCase   ,**UpperCamelCase  ).to(UpperCamelCase  )
			elif is_abit:
				UpperCAmelCase_		:      Optional[Any]       	=					bnb.nn.Paramsabit(UpperCamelCase   ,requires_grad=UpperCamelCase   ,**UpperCamelCase  ).to(UpperCamelCase  )

			UpperCAmelCase_		:      Optional[Any]       	=					new_value
			if fpaa_statistics is not None:
				setattr(module.weight   ,'SCB'   ,fpaa_statistics.to(UpperCamelCase  )  )

	else:
		if value is None:
			UpperCAmelCase_		:      str       	=					old_value.to(UpperCamelCase  )
		elif isinstance(UpperCamelCase   ,torch.Tensor  ):
			UpperCAmelCase_		:      Tuple       	=					value.to(UpperCamelCase  )
		else:
			UpperCAmelCase_		:      str       	=					torch.tensor(UpperCamelCase   ,device=UpperCamelCase  )

		if is_buffer:
			UpperCAmelCase_		:      Dict       	=					new_value
		else:
			UpperCAmelCase_		:      Optional[int]       	=					nn.Parameter(UpperCamelCase   ,requires_grad=old_value.requires_grad  )
			UpperCAmelCase_		:      Optional[int]       	=					new_value







def 						SCREAMING_SNAKE_CASE(  UpperCamelCase   ,UpperCamelCase=None   ,UpperCamelCase=None   ,UpperCamelCase=None   ,UpperCamelCase=False  )    ->    Tuple:
	for name, module in model.named_children():
		if current_key_name is None:
			UpperCAmelCase_		:      Optional[int]       	=					[]
		current_key_name.append(UpperCamelCase  )

		if (isinstance(UpperCamelCase   ,nn.Linear  ) or isinstance(UpperCamelCase   ,UpperCamelCase  )) and name not in modules_to_not_convert:
			# Check if the current key is not in the `modules_to_not_convert`
			if not any(key in '.'.join(UpperCamelCase  ) for key in modules_to_not_convert  ):
				with init_empty_weights():
					if isinstance(UpperCamelCase   ,UpperCamelCase  ):
						UpperCAmelCase_	,    UpperCAmelCase_		:      Optional[Any]       	=					module.weight.shape
					else:
						UpperCAmelCase_		:      Dict       	=					module.in_features
						UpperCAmelCase_		:      Any       	=					module.out_features

					if quantization_config.quantization_method() == "llm_int8":
						UpperCAmelCase_		:      Optional[int]       	=					bnb.nn.LinearabitLt(
						    UpperCamelCase   ,UpperCamelCase   ,module.bias is not None   ,has_fpaa_weights=quantization_config.llm_inta_has_fpaa_weight   ,threshold=quantization_config.llm_inta_threshold   ,)
						UpperCAmelCase_		:      List[Any]       	=					True
					else:
						if (
						    quantization_config.llm_inta_skip_modules is not None
						    and name in quantization_config.llm_inta_skip_modules
						):
							pass
						else:
							UpperCAmelCase_		:      Union[str, Any]       	=					bnb.nn.Linearabit(
							    UpperCamelCase   ,UpperCamelCase   ,module.bias is not None   ,quantization_config.bnb_abit_compute_dtype   ,compress_statistics=quantization_config.bnb_abit_use_double_quant   ,quant_type=quantization_config.bnb_abit_quant_type   ,)
							UpperCAmelCase_		:      Dict       	=					True
                    # Store the module class in case we need to transpose the weight later
					UpperCAmelCase_		:      List[str]       	=					type(UpperCamelCase  )
					# Force requires grad to False to avoid unexpected errors
					model._modules[name].requires_grad_(UpperCamelCase  )
		if len(list(module.children()  )  ) > 0:
			UpperCAmelCase_	,    UpperCAmelCase_		:      Any       	=					_replace_with_bnb_linear(
			    UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase   ,has_been_replaced=UpperCamelCase   ,)
		# Remove the last key for recursion
		current_key_name.pop(-1  )
	return model, has_been_replaced







def 						SCREAMING_SNAKE_CASE(  UpperCamelCase   ,UpperCamelCase=None   ,UpperCamelCase=None   ,UpperCamelCase=None  )    ->    Optional[int]:
	UpperCAmelCase_		:      Union[str, Any]       	=					['lm_head'] if modules_to_not_convert is None else modules_to_not_convert
	UpperCAmelCase_	,    UpperCAmelCase_		:      Any       	=					_replace_with_bnb_linear(
	    UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase  )

	if not has_been_replaced:
		logger.warning(
		    'You are loading your model in 8bit or 4bit but no linear modules were found in your model.'
		    ' Please double check your model architecture, or submit an issue on github if you think this is'
		    ' a bug.'  )

	return model







def 						SCREAMING_SNAKE_CASE(  *UpperCamelCase   ,**UpperCamelCase  )    ->    Tuple:
	warnings.warn(
	    '`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead'   ,UpperCamelCase   ,)
	return replace_with_bnb_linear(*UpperCamelCase   ,**UpperCamelCase  )







def 						SCREAMING_SNAKE_CASE(  *UpperCamelCase   ,**UpperCamelCase  )    ->    List[Any]:
	warnings.warn(
	    '`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead'   ,UpperCamelCase   ,)
	return set_module_quantized_tensor_to_device(*UpperCamelCase   ,**UpperCamelCase  )







def 						SCREAMING_SNAKE_CASE(  UpperCamelCase  )    ->    int:
	UpperCAmelCase_		:      Union[str, Any]       	=					deepcopy(UpperCamelCase  )  # this has 0 cost since it is done inside `init_empty_weights` context manager`
	tied_model.tie_weights()

	UpperCAmelCase_		:      Tuple       	=					find_tied_parameters(UpperCamelCase  )
	# For compatibility with Accelerate < 0.18
	if isinstance(UpperCamelCase   ,UpperCamelCase  ):
		UpperCAmelCase_		:      Dict       	=					sum(list(tied_params.values()  )   ,[]  ) + list(tied_params.keys()  )
	else:
		UpperCAmelCase_		:      Union[str, Any]       	=					sum(UpperCamelCase   ,[]  )
	UpperCAmelCase_		:      Union[str, Any]       	=					len(UpperCamelCase  ) > 0

	# Check if it is a base model
	UpperCAmelCase_		:      List[Any]       	=					not hasattr(UpperCamelCase   ,model.base_model_prefix  )

	# Ignore this for base models (BertModel, GPT2Model, etc.)
	if (not has_tied_params) and is_base_model:
		return []

	# otherwise they have an attached head
	UpperCAmelCase_		:      str       	=					list(model.named_children()  )
	UpperCAmelCase_		:      Optional[int]       	=					[list_modules[-1][0]]

	# add last module together with tied weights
	UpperCAmelCase_		:      Any       	=					set(UpperCamelCase  ) - set(UpperCamelCase  )
	UpperCAmelCase_		:      Optional[int]       	=					list(set(UpperCamelCase  )  ) + list(UpperCamelCase  )

	# remove ".weight" from the keys
	UpperCAmelCase_		:      Optional[int]       	=					['.weight', '.bias']
	UpperCAmelCase_		:      Tuple       	=					[]
	for name in list_untouched:
		for name_to_remove in names_to_remove:
			if name_to_remove in name:
				UpperCAmelCase_		:      Union[str, Any]       	=					name.replace(UpperCamelCase   ,''  )
		filtered_module_names.append(UpperCamelCase  )

	return filtered_module_names






