


'''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

__lowercase      :		Optional[int]			=      logging.get_logger(__name__)






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  List[str]		, _SCREAMING_SNAKE_CASE :  Optional[Any]		, _SCREAMING_SNAKE_CASE :  List[Any]		, _SCREAMING_SNAKE_CASE :  List[Any]=None		, _SCREAMING_SNAKE_CASE :  Union[str, Any]=None  ):







					if "." in tensor_name:
										__a     :			List[str]          =    tensor_name.split('.'  )
										for split in splits[:-1]:
															__a     :			Optional[Any]          =    getattr(UpperCamelCase__		, UpperCamelCase__  )
															if new_module is None:
																				raise ValueError(F"""{module} has no attribute {split}."""  )
															__a     :			Union[str, Any]          =    new_module
										__a     :			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}."""  )
					__a     :			List[str]          =    tensor_name in module._buffers
					__a     :			Optional[int]          =    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}."""  )

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

					if is_abit or is_abit:
										__a     :			int          =    module._parameters[tensor_name]
										if param.device.type != "cuda":
															if value is None:
																				__a     :			List[str]          =    old_value.to(UpperCamelCase__  )
															elif isinstance(UpperCamelCase__		, torch.Tensor  ):
																				__a     :			Tuple          =    value.to('cpu'  )
																				if value.dtype == torch.inta:
																									__a     :			Optional[Any]          =    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:
																				__a     :			int          =    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:
																				__a     :			Any          =    new_value.T

															__a     :			Optional[Any]          =    old_value.__dict__
															if is_abit:
																				__a     :			int          =    bnb.nn.IntaParams(UpperCamelCase__		, requires_grad=UpperCamelCase__		, **UpperCamelCase__  ).to(UpperCamelCase__  )
															elif is_abit:
																				__a     :			Optional[int]          =    bnb.nn.Paramsabit(UpperCamelCase__		, requires_grad=UpperCamelCase__		, **UpperCamelCase__  ).to(UpperCamelCase__  )

															__a     :			Union[str, Any]          =    new_value
															if fpaa_statistics is not None:
																				setattr(module.weight		, 'SCB'		, fpaa_statistics.to(UpperCamelCase__  )  )

					else:
										if value is None:
															__a     :			str          =    old_value.to(UpperCamelCase__  )
										elif isinstance(UpperCamelCase__		, torch.Tensor  ):
															__a     :			Union[str, Any]          =    value.to(UpperCamelCase__  )
										else:
															__a     :			int          =    torch.tensor(UpperCamelCase__		, device=UpperCamelCase__  )

										if is_buffer:
															__a     :			Optional[Any]          =    new_value
										else:
															__a     :			Optional[Any]          =    nn.Parameter(UpperCamelCase__		, requires_grad=old_value.requires_grad  )
															__a     :			List[str]          =    new_value






def     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  List[Any]		, _SCREAMING_SNAKE_CASE :  List[Any]=None		, _SCREAMING_SNAKE_CASE :  int=None		, _SCREAMING_SNAKE_CASE :  str=None		, _SCREAMING_SNAKE_CASE :  Any=False  ):







					for name, module in model.named_children():
										if current_key_name is None:
															__a     :			List[Any]          =    []
										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__  ):
																														__a       ,							__a     :			Optional[int]          =    module.weight.shape
																									else:
																														__a     :			List[str]          =    module.in_features
																														__a     :			Optional[int]          =    module.out_features

																									if quantization_config.quantization_method() == "llm_int8":
																														__a     :			Tuple          =    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		, )
																														__a     :			Tuple          =    True
																									else:
																														if (
																														    quantization_config.llm_inta_skip_modules is not None
																														    and name in quantization_config.llm_inta_skip_modules
																														):
																																			pass
																														else:
																																			__a     :			Optional[int]          =    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		, )
																																			__a     :			Optional[int]          =    True
                    # Store the module class in case we need to transpose the weight later
																									__a     :			Any          =    type(UpperCamelCase__  )
																									# Force requires grad to False to avoid unexpected errors
																									model._modules[name].requires_grad_(UpperCamelCase__  )
										if len(list(module.children()  )  ) > 0:
															__a       ,							__a     :			Union[str, 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     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Optional[int]		, _SCREAMING_SNAKE_CASE :  List[str]=None		, _SCREAMING_SNAKE_CASE :  Union[str, Any]=None		, _SCREAMING_SNAKE_CASE :  Dict=None  ):
					__a     :			Union[str, Any]          =    ['lm_head'] if modules_to_not_convert is None else modules_to_not_convert
					__a       ,							__a     :			List[str]          =    _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     lowerCamelCase				(*_SCREAMING_SNAKE_CASE :  Tuple		, **_SCREAMING_SNAKE_CASE :  Dict  ):
					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     lowerCamelCase				(*_SCREAMING_SNAKE_CASE :  Dict		, **_SCREAMING_SNAKE_CASE :  Optional[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     lowerCamelCase				(_SCREAMING_SNAKE_CASE :  Any  ):
					__a     :			int          =    deepcopy(UpperCamelCase__  )  # this has 0 cost since it is done inside `init_empty_weights` context manager`
					tied_model.tie_weights()

					__a     :			int          =    find_tied_parameters(UpperCamelCase__  )
					# For compatibility with Accelerate < 0.18
					if isinstance(UpperCamelCase__		, UpperCamelCase__  ):
										__a     :			str          =    sum(list(tied_params.values()  )		, []  ) + list(tied_params.keys()  )
					else:
										__a     :			str          =    sum(UpperCamelCase__		, []  )
					__a     :			str          =    len(UpperCamelCase__  ) > 0

					# Check if it is a base model
					__a     :			Dict          =    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
					__a     :			List[Any]          =    list(model.named_children()  )
					__a     :			str          =    [list_modules[-1][0]]

					# add last module together with tied weights
					__a     :			Union[str, Any]          =    set(UpperCamelCase__  ) - set(UpperCamelCase__  )
					__a     :			Tuple          =    list(set(UpperCamelCase__  )  ) + list(UpperCamelCase__  )

					# remove ".weight" from the keys
					__a     :			Tuple          =    ['.weight', '.bias']
					__a     :			Dict          =    []
					for name in list_untouched:
										for name_to_remove in names_to_remove:
															if name_to_remove in name:
																				__a     :			Optional[int]          =    name.replace(UpperCamelCase__		, ''  )
										filtered_module_names.append(UpperCamelCase__  )

					return filtered_module_names







import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
				from .tokenization_albert import AlbertTokenizer
else:
				_lowercase							:   List[str]						=						None

_lowercase							:   List[str]						=						logging.get_logger(__name__)
_lowercase							:   List[Any]						=						{"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"}

_lowercase							:   str						=						{
    "vocab_file": {
        "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/spiece.model",
        "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/spiece.model",
        "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model",
        "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model",
        "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/spiece.model",
        "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/spiece.model",
        "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model",
        "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model",
    },
    "tokenizer_file": {
        "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json",
        "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json",
        "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json",
        "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json",
        "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json",
        "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json",
        "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json",
        "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json",
    },
}

_lowercase							:   List[str]						=						{
    "albert-base-v1": 512,
    "albert-large-v1": 512,
    "albert-xlarge-v1": 512,
    "albert-xxlarge-v1": 512,
    "albert-base-v2": 512,
    "albert-large-v2": 512,
    "albert-xlarge-v2": 512,
    "albert-xxlarge-v2": 512,
}

_lowercase							:   Dict						=						"▁"






class    _UpperCamelCase     (       __snake_case					):

				"""simple docstring"""







				lowerCAmelCase					=			VOCAB_FILES_NAMES
				lowerCAmelCase					=			PRETRAINED_VOCAB_FILES_MAP
				lowerCAmelCase					=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				lowerCAmelCase					=			AlbertTokenizer

				def __init__(    self					, a__=None					, a__=None					, a__=True					, a__=True					, a__=False					, a__="[CLS]"					, a__="[SEP]"					, a__="<unk>"					, a__="[SEP]"					, a__="<pad>"					, a__="[CLS]"					, a__="[MASK]"					, **a__					, )						->					Dict:
					# Mask token behave like a normal word, i.e. include the space before it and
					# is included in the raw text, there should be a match in a non-normalized sentence.
					A             =					(
					    AddedToken(a__					, lstrip=a__					, rstrip=a__					, normalized=a__ )
					    if isinstance(a__					, a__ )
					    else mask_token
					)

					super().__init__(
					    a__					, tokenizer_file=a__					, do_lower_case=a__					, remove_space=a__					, keep_accents=a__					, bos_token=a__					, eos_token=a__					, unk_token=a__					, sep_token=a__					, pad_token=a__					, cls_token=a__					, mask_token=a__					, **a__					, )

					A             =					do_lower_case
					A             =					remove_space
					A             =					keep_accents
					A             =					vocab_file
					A             =					False if not self.vocab_file else True

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

				def   _UpperCAmelCase       (    self					, a__					, a__ = None )						->					List[int]:
					A             =					[self.sep_token_id]
					A             =					[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 ) * [0] + len(token_ids_a + sep ) * [1]

				def   _UpperCAmelCase       (    self					, a__					, a__ = None )						->					Tuple[str]:
					if not self.can_save_slow_tokenizer:
						raise ValueError(
						    """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
						    """tokenizer.""" )

					if not os.path.isdir(a__ ):
						logger.error(f'Vocabulary path ({save_directory}) should be a directory' )
						return
					A             =					os.path.join(
					    a__					, (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] )

					if os.path.abspath(self.vocab_file ) != os.path.abspath(a__ ):
						copyfile(self.vocab_file					, a__ )

					return (out_vocab_file,)



