
import shutil
import tempfile
import unittest

from transformers import (
    SPIECE_UNDERLINE,
    AddedToken,
    BatchEncoding,
    NllbTokenizer,
    NllbTokenizerFast,
    is_torch_available,
)
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


UpperCamelCase_						:     List[str]	       =							get_tests_dir("""fixtures/test_sentencepiece.model""")


if is_torch_available():
							from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right

UpperCamelCase_						:     Tuple	       =							25_6047
UpperCamelCase_						:     Dict	       =							25_6145




@require_sentencepiece
@require_tokenizers
class   __lowercase						(						__snake_case   , unittest.TestCase       ):
	_A       =   NllbTokenizer
	_A       =   NllbTokenizerFast
	_A       =   True
	_A       =   True
	_A       =   {}






	def       _a(self				: Union[str, Any]    ) ->			Optional[Any]:
							super().setUp()

							# We have a SentencePiece fixture for testing
							_lowercase       :	Optional[int]           =   NllbTokenizer(snake_case      ,	keep_accents=snake_case    )
							tokenizer.save_pretrained(self.tmpdirname    )






	def       _a(self				: int    ) ->			List[Any]:
							_lowercase       :	Optional[Any]           =   NllbTokenizer(snake_case      ,	keep_accents=snake_case    )

							_lowercase       :	List[str]           =   tokenizer.tokenize("This is a test"    )
							self.assertListEqual(snake_case      ,	["▁This", "▁is", "▁a", "▁t", "est"]    )

							self.assertListEqual(
							    tokenizer.convert_tokens_to_ids(snake_case    )      ,	[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]      ,	)

							_lowercase       :	Union[str, Any]           =   tokenizer.tokenize("I was born in 92000, and this is falsé."    )
							self.assertListEqual(
							    snake_case      ,	[
							        SPIECE_UNDERLINE + "I",
							        SPIECE_UNDERLINE + "was",
							        SPIECE_UNDERLINE + "b",
							        "or",
							        "n",
							        SPIECE_UNDERLINE + "in",
							        SPIECE_UNDERLINE + "",
							        "9",
							        "2",
							        "0",
							        "0",
							        "0",
							        ",",
							        SPIECE_UNDERLINE + "and",
							        SPIECE_UNDERLINE + "this",
							        SPIECE_UNDERLINE + "is",
							        SPIECE_UNDERLINE + "f",
							        "al",
							        "s",
							        "é",
							        ".",
							    ]      ,	)
							_lowercase       :	Any           =   tokenizer.convert_tokens_to_ids(snake_case    )
							self.assertListEqual(
							    snake_case      ,	[
							        value + tokenizer.fairseq_offset
							        for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
							    ]      ,	)

							_lowercase       :	List[Any]           =   tokenizer.convert_ids_to_tokens(snake_case    )
							self.assertListEqual(
							    snake_case      ,	[
							        SPIECE_UNDERLINE + "I",
							        SPIECE_UNDERLINE + "was",
							        SPIECE_UNDERLINE + "b",
							        "or",
							        "n",
							        SPIECE_UNDERLINE + "in",
							        SPIECE_UNDERLINE + "",
							        "<unk>",
							        "2",
							        "0",
							        "0",
							        "0",
							        ",",
							        SPIECE_UNDERLINE + "and",
							        SPIECE_UNDERLINE + "this",
							        SPIECE_UNDERLINE + "is",
							        SPIECE_UNDERLINE + "f",
							        "al",
							        "s",
							        "<unk>",
							        ".",
							    ]      ,	)






	def       _a(self				: str    ) ->			Tuple:
							_lowercase       :	Any           =   (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-nllb", {})
							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""    ):
																			_lowercase       :	Tuple           =   self.rust_tokenizer_class.from_pretrained(snake_case      ,	**snake_case    )
																			_lowercase       :	Union[str, Any]           =   self.tokenizer_class.from_pretrained(snake_case      ,	**snake_case    )

																			_lowercase       :	Tuple           =   tempfile.mkdtemp()

																			_lowercase       :	Dict           =   tokenizer_r.save_pretrained(snake_case    )
																			_lowercase       :	List[str]           =   tokenizer_p.save_pretrained(snake_case    )

																			# Checks it save with the same files + the tokenizer.json file for the fast one
																			self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files    )    )
																			_lowercase       :	List[Any]           =   tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f    )
																			self.assertSequenceEqual(snake_case      ,	snake_case    )

																			# Checks everything loads correctly in the same way
																			_lowercase       :	Dict           =   tokenizer_r.from_pretrained(snake_case    )
																			_lowercase       :	Dict           =   tokenizer_p.from_pretrained(snake_case    )

																			# Check special tokens are set accordingly on Rust and Python
																			for key in tokenizer_pp.special_tokens_map:
																									self.assertTrue(hasattr(snake_case      ,	snake_case    )    )

																			shutil.rmtree(snake_case    )

																			# Save tokenizer rust, legacy_format=True
																			_lowercase       :	str           =   tempfile.mkdtemp()

																			_lowercase       :	Any           =   tokenizer_r.save_pretrained(snake_case      ,	legacy_format=snake_case    )
																			_lowercase       :	str           =   tokenizer_p.save_pretrained(snake_case    )

																			# Checks it save with the same files
																			self.assertSequenceEqual(snake_case      ,	snake_case    )

																			# Checks everything loads correctly in the same way
																			_lowercase       :	int           =   tokenizer_r.from_pretrained(snake_case    )
																			_lowercase       :	List[str]           =   tokenizer_p.from_pretrained(snake_case    )

																			# Check special tokens are set accordingly on Rust and Python
																			for key in tokenizer_pp.special_tokens_map:
																									self.assertTrue(hasattr(snake_case      ,	snake_case    )    )

																			shutil.rmtree(snake_case    )

																			# Save tokenizer rust, legacy_format=False
																			_lowercase       :	str           =   tempfile.mkdtemp()

																			_lowercase       :	int           =   tokenizer_r.save_pretrained(snake_case      ,	legacy_format=snake_case    )
																			_lowercase       :	Dict           =   tokenizer_p.save_pretrained(snake_case    )

																			# Checks it saved the tokenizer.json file
																			self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files    )    )

																			# Checks everything loads correctly in the same way
																			_lowercase       :	Tuple           =   tokenizer_r.from_pretrained(snake_case    )
																			_lowercase       :	List[Any]           =   tokenizer_p.from_pretrained(snake_case    )

																			# Check special tokens are set accordingly on Rust and Python
																			for key in tokenizer_pp.special_tokens_map:
																									self.assertTrue(hasattr(snake_case      ,	snake_case    )    )

																			shutil.rmtree(snake_case    )






	@require_torch
	def       _a(self				: Optional[Any]    ) ->			List[Any]:
							if not self.test_seqaseq:
													return

							_lowercase       :	Dict           =   self.get_tokenizers()
							for tokenizer in tokenizers:
													with self.subTest(F"""{tokenizer.__class__.__name__}"""    ):
																			# Longer text that will definitely require truncation.
																			_lowercase       :	Optional[int]           =   [
																			    " UN Chief Says There Is No Military Solution in Syria",
																			    " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for"
																			    " Syria is that 'there is no military solution' to the nearly five-year conflict and more weapons"
																			    " will only worsen the violence and misery for millions of people.",
																			]
																			_lowercase       :	Dict           =   [
																			    "Şeful ONU declară că nu există o soluţie militară în Siria",
																			    "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al"
																			    " Rusiei pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi"
																			    " că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.",
																			]
																			try:
																									_lowercase       :	Dict           =   tokenizer.prepare_seqaseq_batch(
																									    src_texts=snake_case      ,	tgt_texts=snake_case      ,	max_length=3      ,	max_target_length=10      ,	return_tensors="pt"      ,	src_lang="eng_Latn"      ,	tgt_lang="ron_Latn"      ,	)
																			except NotImplementedError:
																									return
																			self.assertEqual(batch.input_ids.shape[1]      ,	3    )
																			self.assertEqual(batch.labels.shape[1]      ,	10    )
																			# max_target_length will default to max_length if not specified
																			_lowercase       :	Dict           =   tokenizer.prepare_seqaseq_batch(
																			    snake_case      ,	tgt_texts=snake_case      ,	max_length=3      ,	return_tensors="pt"    )
																			self.assertEqual(batch.input_ids.shape[1]      ,	3    )
																			self.assertEqual(batch.labels.shape[1]      ,	3    )

																			_lowercase       :	Dict           =   tokenizer.prepare_seqaseq_batch(
																			    src_texts=snake_case      ,	max_length=3      ,	max_target_length=10      ,	return_tensors="pt"    )
																			self.assertEqual(batch_encoder_only.input_ids.shape[1]      ,	3    )
																			self.assertEqual(batch_encoder_only.attention_mask.shape[1]      ,	3    )
																			self.assertNotIn("decoder_input_ids"      ,	snake_case    )






	@unittest.skip("Unfortunately way too slow to build a BPE with SentencePiece."    )
	def       _a(self				: Optional[int]    ) ->			Tuple:
							pass






	def       _a(self				: Optional[int]    ) ->			Optional[int]:
							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""    ):
																			_lowercase       :	Any           =   [AddedToken("<special>"      ,	lstrip=snake_case    )]

																			_lowercase       :	List[str]           =   self.rust_tokenizer_class.from_pretrained(
																			    snake_case      ,	additional_special_tokens=snake_case      ,	**snake_case    )
																			_lowercase       :	Optional[Any]           =   tokenizer_r.encode("Hey this is a <special> token"    )

																			_lowercase       :	List[str]           =   tokenizer_r.encode("<special>"      ,	add_special_tokens=snake_case    )[0]

																			self.assertTrue(special_token_id in r_output    )

																			if self.test_slow_tokenizer:
																									_lowercase       :	int           =   self.rust_tokenizer_class.from_pretrained(
																									    snake_case      ,	additional_special_tokens=snake_case      ,	**snake_case      ,	)
																									_lowercase       :	int           =   self.tokenizer_class.from_pretrained(
																									    snake_case      ,	additional_special_tokens=snake_case      ,	**snake_case    )

																									_lowercase       :	Optional[Any]           =   tokenizer_p.encode("Hey this is a <special> token"    )

																									_lowercase       :	int           =   tokenizer_cr.encode("Hey this is a <special> token"    )

																									self.assertEqual(snake_case      ,	snake_case    )
																									self.assertEqual(snake_case      ,	snake_case    )
																									self.assertTrue(special_token_id in p_output    )
																									self.assertTrue(special_token_id in cr_output    )




@require_torch
@require_sentencepiece
@require_tokenizers
class   __lowercase						(						unittest.TestCase       ):
	_A       =   "facebook/nllb-200-distilled-600M"
	_A       =   [
	    " UN Chief Says There Is No Military Solution in Syria",
	    " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.",
	]
	_A       =   [
	    "Şeful ONU declară că nu există o soluţie militară în Siria",
	    "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei"
	    " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor"
	    " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.",
	]
	_A       =   [
	    256047,
	    16297,
	    134408,
	    8165,
	    248066,
	    14734,
	    950,
	    1135,
	    105721,
	    3573,
	    83,
	    27352,
	    108,
	    49486,
	    2,
	]






	@classmethod
	def       _a(cls				: List[str]    ) ->			Tuple:
							_lowercase       :	NllbTokenizer           =   NllbTokenizer.from_pretrained(
							    cls.checkpoint_name      ,	src_lang="eng_Latn"      ,	tgt_lang="ron_Latn"    )
							_lowercase       :	List[str]           =   1
							return cls






	def       _a(self				: Tuple    ) ->			List[Any]:
							self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Arab"]      ,	25_6001    )
							self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ace_Latn"]      ,	25_6002    )
							self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["fra_Latn"]      ,	25_6057    )






	def       _a(self				: Dict    ) ->			str:
							_lowercase       :	int           =   self.tokenizer.batch_encode_plus(self.src_text    ).input_ids[0]
							self.assertListEqual(self.expected_src_tokens      ,	snake_case    )






	def       _a(self				: Dict    ) ->			str:
							self.assertIn(snake_case      ,	self.tokenizer.all_special_ids    )
							# fmt: off
							_lowercase       :	Optional[Any]           =   [RO_CODE, 4254, 9_8068, 11_2923, 3_9072, 3909, 713, 10_2767, 26, 1_7314, 3_5642, 1_4683, 3_3118, 2022, 6_6987, 2, 25_6047]
							# fmt: on

							_lowercase       :	Optional[Any]           =   self.tokenizer.decode(snake_case      ,	skip_special_tokens=snake_case    )
							_lowercase       :	str           =   self.tokenizer.decode(generated_ids[1:]      ,	skip_special_tokens=snake_case    )
							self.assertEqual(snake_case      ,	snake_case    )
							self.assertNotIn(self.tokenizer.eos_token      ,	snake_case    )






	def       _a(self				: str    ) ->			Union[str, Any]:
							_lowercase       :	Tuple           =   ["this is gunna be a long sentence " * 20]
							assert isinstance(src_text[0]      ,	snake_case    )
							_lowercase       :	Optional[Any]           =   10
							_lowercase       :	str           =   self.tokenizer(snake_case      ,	max_length=snake_case      ,	truncation=snake_case    ).input_ids[0]
							self.assertEqual(ids[-1]      ,	2    )
							self.assertEqual(ids[0]      ,	snake_case    )
							self.assertEqual(len(snake_case    )      ,	snake_case    )






	def       _a(self				: str    ) ->			str:
							self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"]    )      ,	[25_6203, 3]    )






	def       _a(self				: Dict    ) ->			Optional[int]:
							_lowercase       :	str           =   tempfile.mkdtemp()
							_lowercase       :	int           =   self.tokenizer.fairseq_tokens_to_ids
							self.tokenizer.save_pretrained(snake_case    )
							_lowercase       :	Any           =   NllbTokenizer.from_pretrained(snake_case    )
							self.assertDictEqual(new_tok.fairseq_tokens_to_ids      ,	snake_case    )






	@require_torch
	def       _a(self				: Optional[Any]    ) ->			Optional[Any]:
							_lowercase       :	int           =   self.tokenizer(
							    self.src_text      ,	text_target=self.tgt_text      ,	padding=snake_case      ,	truncation=snake_case      ,	max_length=len(self.expected_src_tokens    )      ,	return_tensors="pt"      ,	)
							_lowercase       :	Optional[Any]           =   shift_tokens_right(
							    batch["labels"]      ,	self.tokenizer.pad_token_id      ,	self.tokenizer.lang_code_to_id["ron_Latn"]    )

							self.assertIsInstance(snake_case      ,	snake_case    )

							self.assertEqual((2, 15)      ,	batch.input_ids.shape    )
							self.assertEqual((2, 15)      ,	batch.attention_mask.shape    )
							_lowercase       :	Any           =   batch.input_ids.tolist()[0]
							self.assertListEqual(self.expected_src_tokens      ,	snake_case    )
							self.assertEqual(snake_case      ,	batch.decoder_input_ids[0, 0]    )  # EOS
							# Test that special tokens are reset
							self.assertEqual(self.tokenizer.prefix_tokens      ,	[EN_CODE]    )
							self.assertEqual(self.tokenizer.suffix_tokens      ,	[self.tokenizer.eos_token_id]    )






	def       _a(self				: Any    ) ->			Tuple:
							_lowercase       :	Optional[Any]           =   self.tokenizer(self.src_text      ,	padding=snake_case      ,	truncation=snake_case      ,	max_length=3      ,	return_tensors="pt"    )
							_lowercase       :	List[str]           =   self.tokenizer(
							    text_target=self.tgt_text      ,	padding=snake_case      ,	truncation=snake_case      ,	max_length=10      ,	return_tensors="pt"    )
							_lowercase       :	int           =   targets["input_ids"]
							_lowercase       :	Optional[int]           =   shift_tokens_right(
							    snake_case      ,	self.tokenizer.pad_token_id      ,	decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang]      ,	)

							self.assertEqual(batch.input_ids.shape[1]      ,	3    )
							self.assertEqual(batch.decoder_input_ids.shape[1]      ,	10    )






	@require_torch
	def       _a(self				: Optional[Any]    ) ->			Dict:
							_lowercase       :	int           =   self.tokenizer._build_translation_inputs(
							    "A test"      ,	return_tensors="pt"      ,	src_lang="eng_Latn"      ,	tgt_lang="fra_Latn"    )

							self.assertEqual(
							    nested_simplify(snake_case    )      ,	{
							        # A, test, EOS, en_XX
							        "input_ids": [[25_6047, 70, 7356, 2]],
							        "attention_mask": [[1, 1, 1, 1]],
							        # ar_AR
							        "forced_bos_token_id": 25_6057,
							    }      ,	)






	@require_torch
	def       _a(self				: int    ) ->			Dict:
							_lowercase       :	Union[str, Any]           =   True
							_lowercase       :	List[Any]           =   self.tokenizer(
							    "UN Chief says there is no military solution in Syria"      ,	src_lang="eng_Latn"      ,	tgt_lang="fra_Latn"    )
							self.assertEqual(
							    inputs.input_ids      ,	[1_6297, 13_4408, 2_5653, 6370, 248, 254, 10_3929, 9_4995, 108, 4_9486, 2, 25_6047]    )

							_lowercase       :	Dict           =   False
							_lowercase       :	Any           =   self.tokenizer(
							    "UN Chief says there is no military solution in Syria"      ,	src_lang="eng_Latn"      ,	tgt_lang="fra_Latn"    )
							self.assertEqual(
							    inputs.input_ids      ,	[25_6047, 1_6297, 13_4408, 2_5653, 6370, 248, 254, 10_3929, 9_4995, 108, 4_9486, 2]    )






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

UpperCamelCase_						:     int	       =							logging.get_logger(__name__)



def 		UpperCamelCase     (       _UpperCAmelCase  :   Optional[Any] ,			_UpperCAmelCase  :   Optional[Any] ,			_UpperCAmelCase  :   Optional[Any] ,			_UpperCAmelCase  :   Dict=None ,			_UpperCAmelCase  :   Dict=None  )   ->			Tuple:






						'''simple docstring'''

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

						_lowercase       :	int           =   False
						_lowercase       :	str           =   False
						if is_buffer or not is_bitsandbytes_available():
												_lowercase       :	int           =   False
												_lowercase       :	List[str]           =   False
						else:
												_lowercase       :	List[str]           =   hasattr(bnb.nn ,			"Params4bit"  ) and isinstance(module._parameters[tensor_name] ,			bnb.nn.Paramsabit  )
												_lowercase       :	int           =   isinstance(module._parameters[tensor_name] ,			bnb.nn.IntaParams  )

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

																		_lowercase       :	List[str]           =   old_value.__dict__
																		if is_abit:
																								_lowercase       :	Optional[Any]           =   bnb.nn.IntaParams(_UpperCAmelCase ,			requires_grad=_UpperCAmelCase ,			**_UpperCAmelCase  ).to(_UpperCAmelCase  )
																		elif is_abit:
																								_lowercase       :	Dict           =   bnb.nn.Paramsabit(_UpperCAmelCase ,			requires_grad=_UpperCAmelCase ,			**_UpperCAmelCase  ).to(_UpperCAmelCase  )

																		_lowercase       :	str           =   new_value
																		if fpaa_statistics is not None:
																								setattr(module.weight ,			"SCB" ,			fpaa_statistics.to(_UpperCAmelCase  )  )

						else:
												if value is None:
																		_lowercase       :	Optional[Any]           =   old_value.to(_UpperCAmelCase  )
												elif isinstance(_UpperCAmelCase ,			torch.Tensor  ):
																		_lowercase       :	Tuple           =   value.to(_UpperCAmelCase  )
												else:
																		_lowercase       :	Optional[Any]           =   torch.tensor(_UpperCAmelCase ,			device=_UpperCAmelCase  )

												if is_buffer:
																		_lowercase       :	str           =   new_value
												else:
																		_lowercase       :	int           =   nn.Parameter(_UpperCAmelCase ,			requires_grad=old_value.requires_grad  )
																		_lowercase       :	str           =   new_value



def 		UpperCamelCase     (       _UpperCAmelCase  :   Any ,			_UpperCAmelCase  :   str=None ,			_UpperCAmelCase  :   List[str]=None ,			_UpperCAmelCase  :   Tuple=None ,			_UpperCAmelCase  :   Tuple=False  )   ->			int:






						'''simple docstring'''

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

																														if quantization_config.quantization_method() == "llm_int8":
																																				_lowercase       :	Union[str, Any]           =   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 ,			)
																																				_lowercase       :	Tuple           =   True
																														else:
																																				if (
																																				    quantization_config.llm_inta_skip_modules is not None
																																				    and name in quantization_config.llm_inta_skip_modules
																																				):
																																										pass
																																				else:
																																										_lowercase       :	List[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 ,			)
																																										_lowercase       :	Optional[int]           =   True
                    # Store the module class in case we need to transpose the weight later
																														_lowercase       :	str           =   type(_UpperCAmelCase  )
																														# Force requires grad to False to avoid unexpected errors
																														model._modules[name].requires_grad_(_UpperCAmelCase  )
												if len(list(module.children()  )  ) > 0:
																		_lowercase      ,    _lowercase       :	List[str]           =   _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 		UpperCamelCase     (       _UpperCAmelCase  :   Optional[int] ,			_UpperCAmelCase  :   Tuple=None ,			_UpperCAmelCase  :   Optional[int]=None ,			_UpperCAmelCase  :   int=None  )   ->			Optional[int]:






						'''simple docstring'''

						_lowercase       :	str           =   ["lm_head"] if modules_to_not_convert is None else modules_to_not_convert
						_lowercase      ,    _lowercase       :	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 		UpperCamelCase     (       *_UpperCAmelCase  :   Optional[Any] ,			**_UpperCAmelCase  :   Dict  )   ->			Tuple:






						'''simple docstring'''

						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 		UpperCamelCase     (       *_UpperCAmelCase  :   Dict ,			**_UpperCAmelCase  :   Dict  )   ->			int:






						'''simple docstring'''

						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 		UpperCamelCase     (       _UpperCAmelCase  :   List[Any]  )   ->			Tuple:






						'''simple docstring'''

						_lowercase       :	Tuple           =   deepcopy(_UpperCAmelCase  )  # this has 0 cost since it is done inside `init_empty_weights` context manager`
						tied_model.tie_weights()

						_lowercase       :	List[Any]           =   find_tied_parameters(_UpperCAmelCase  )
						# For compatibility with Accelerate < 0.18
						if isinstance(_UpperCAmelCase ,			_UpperCAmelCase  ):
												_lowercase       :	Any           =   sum(list(tied_params.values()  ) ,			[]  ) + list(tied_params.keys()  )
						else:
												_lowercase       :	Tuple           =   sum(_UpperCAmelCase ,			[]  )
						_lowercase       :	List[Any]           =   len(_UpperCAmelCase  ) > 0

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

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

						# remove ".weight" from the keys
						_lowercase       :	str           =   [".weight", ".bias"]
						_lowercase       :	Optional[Any]           =   []
						for name in list_untouched:
												for name_to_remove in names_to_remove:
																		if name_to_remove in name:
																								_lowercase       :	Optional[Any]           =   name.replace(_UpperCAmelCase ,			""  )
												filtered_module_names.append(_UpperCAmelCase  )

						return filtered_module_names




