

import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.bert.modeling_bert import (
    BERT_INPUTS_DOCSTRING,
    BERT_START_DOCSTRING,
    BertEmbeddings,
    BertLayer,
    BertPooler,
    BertPreTrainedModel,
)







def        lowerCAmelCase_  (      __lowerCamelCase			):
							__snake_case	:    Optional[Any]											=     torch.exp(__snake_case			)
							__snake_case	:    List[Any]											=     torch.sum(__snake_case							,			dim=1			)  # sum of exp(x_i)
							__snake_case	:    Any											=     torch.sum(x * exp_x							,			dim=1			)  # sum of x_i * exp(x_i)
							return torch.log(__snake_case			) - B / A

class        a	(nn.Module   ):




							"""simple docstring"""


							def __init__(							self :						int					,			lowerCamelCase :						Tuple       )			->					Any:
														super().__init__()
														__snake_case	:    Any											=     config.output_attentions
														__snake_case	:    str											=     config.output_hidden_states
														__snake_case	:    int											=     nn.ModuleList([BertLayer(__lowerCAmelCase       ) for _ in range(config.num_hidden_layers       )]       )
														__snake_case	:    Optional[int]											=     nn.ModuleList([BertHighway(__lowerCAmelCase       ) for _ in range(config.num_hidden_layers       )]       )

														__snake_case	:    List[str]											=     [-1 for _ in range(config.num_hidden_layers       )]

							def 					__snake_case      (							self :						Dict					,			lowerCamelCase :						List[str]       )			->					List[Any]:







														if (type(__lowerCAmelCase       ) is float) or (type(__lowerCAmelCase       ) is int):
																					for i in range(len(self.early_exit_entropy       )       ):
																												__snake_case	:    Tuple											=     x
														else:
																					__snake_case	:    Optional[Any]											=     x

							def 					__snake_case      (							self :						Optional[int]					,			lowerCamelCase :						str       )			->					Optional[int]:
														__snake_case	:    Dict											=     pooler.state_dict()
														for highway in self.highway:
																					for name, param in highway.pooler.state_dict().items():
																												param.copy_(loaded_model[name]       )






							def 					__snake_case      (							self :						Dict					,			lowerCamelCase :						List[str]					,			lowerCamelCase :						Tuple=None					,			lowerCamelCase :						Dict=None					,			lowerCamelCase :						Union[str, Any]=None					,			lowerCamelCase :						Any=None					,			)			->					Optional[int]:
														__snake_case	:    Optional[int]											=     ()
														__snake_case	:    List[str]											=     ()
														__snake_case	:    List[Any]											=     ()
														for i, layer_module in enumerate(self.layer       ):
																					if self.output_hidden_states:
																												__snake_case	:    Optional[Any]											=     all_hidden_states + (hidden_states,)

																					__snake_case	:    List[Any]											=     layer_module(
																					    __lowerCAmelCase					,			__lowerCAmelCase					,			head_mask[i]					,			__lowerCAmelCase					,			__lowerCAmelCase       )
																					__snake_case	:    str											=     layer_outputs[0]

																					if self.output_attentions:
																												__snake_case	:    Union[str, Any]											=     all_attentions + (layer_outputs[1],)

																					__snake_case	:    List[str]											=     (hidden_states,)
																					if self.output_hidden_states:
																												__snake_case	:    Any											=     current_outputs + (all_hidden_states,)
																					if self.output_attentions:
																												__snake_case	:    Union[str, Any]											=     current_outputs + (all_attentions,)

																					__snake_case	:    Optional[int]											=     self.highway[i](__lowerCAmelCase       )
																					# logits, pooled_output

																					if not self.training:
																												__snake_case	:    Tuple											=     highway_exit[0]
																												__snake_case	:    int											=     entropy(__lowerCAmelCase       )
																												__snake_case	:    Dict											=     highway_exit + (highway_entropy,)  # logits, hidden_states(?), entropy
																												__snake_case	:    Union[str, Any]											=     all_highway_exits + (highway_exit,)

																												if highway_entropy < self.early_exit_entropy[i]:
																																			__snake_case	:    Dict											=     (highway_logits,) + current_outputs[1:] + (all_highway_exits,)
																																			raise HighwayException(__lowerCAmelCase					,			i + 1       )
																					else:
																												__snake_case	:    List[Any]											=     all_highway_exits + (highway_exit,)

        # Add last layer
														if self.output_hidden_states:
																					__snake_case	:    Any											=     all_hidden_states + (hidden_states,)

														__snake_case	:    Any											=     (hidden_states,)
														if self.output_hidden_states:
																					__snake_case	:    Optional[Any]											=     outputs + (all_hidden_states,)
														if self.output_attentions:
																					__snake_case	:    List[str]											=     outputs + (all_attentions,)

														__snake_case	:    List[str]											=     outputs + (all_highway_exits,)
														return outputs  # last-layer hidden state, (all hidden states), (all attentions), all highway exits

@add_start_docstrings(
    "The Bert Model transformer with early exiting (DeeBERT). "  ,       _lowerCAmelCase  ,       )
class        a	(_lowerCAmelCase   ):




							"""simple docstring"""


							def __init__(							self :						List[str]					,			lowerCamelCase :						List[str]       )			->					List[Any]:
														super().__init__(__lowerCAmelCase       )
														__snake_case	:    List[str]											=     config

														__snake_case	:    Any											=     BertEmbeddings(__lowerCAmelCase       )
														__snake_case	:    List[Any]											=     DeeBertEncoder(__lowerCAmelCase       )
														__snake_case	:    Union[str, Any]											=     BertPooler(__lowerCAmelCase       )

														self.init_weights()

							def 					__snake_case      (							self :						int       )			->					Tuple:
														self.encoder.init_highway_pooler(self.pooler       )

							def 					__snake_case      (							self :						Union[str, Any]       )			->					List[str]:
														return self.embeddings.word_embeddings

							def 					__snake_case      (							self :						str					,			lowerCamelCase :						List[Any]       )			->					List[str]:
														__snake_case	:    List[str]											=     value

							def 					__snake_case      (							self :						Optional[int]					,			lowerCamelCase :						Any       )			->					List[str]:







														for layer, heads in heads_to_prune.items():
																					self.encoder.layer[layer].attention.prune_heads(__lowerCAmelCase       )






							@add_start_docstrings_to_model_forward(__lowerCAmelCase       )
							def 					__snake_case      (							self :						List[str]					,			lowerCamelCase :						int=None					,			lowerCamelCase :						Optional[int]=None					,			lowerCamelCase :						Dict=None					,			lowerCamelCase :						List[str]=None					,			lowerCamelCase :						List[str]=None					,			lowerCamelCase :						Union[str, Any]=None					,			lowerCamelCase :						int=None					,			lowerCamelCase :						List[str]=None					,			)			->					Any:







														if input_ids is not None and inputs_embeds is not None:
																					raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time"       )
														elif input_ids is not None:
																					__snake_case	:    Optional[Any]											=     input_ids.size()
														elif inputs_embeds is not None:
																					__snake_case	:    Optional[int]											=     inputs_embeds.size()[:-1]
														else:
																					raise ValueError("You have to specify either input_ids or inputs_embeds"       )

														__snake_case	:    Tuple											=     input_ids.device if input_ids is not None else inputs_embeds.device

														if attention_mask is None:
																					__snake_case	:    List[Any]											=     torch.ones(__lowerCAmelCase					,			device=__lowerCAmelCase       )
														if encoder_attention_mask is None:
																					__snake_case	:    str											=     torch.ones(__lowerCAmelCase					,			device=__lowerCAmelCase       )
														if token_type_ids is None:
																					__snake_case	:    int											=     torch.zeros(__lowerCAmelCase					,			dtype=torch.long					,			device=__lowerCAmelCase       )

														# We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
														# ourselves in which case we just need to make it broadcastable to all heads.
														__snake_case	:    Dict											=     self.get_extended_attention_mask(__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase       )

														# If a 2D ou 3D attention mask is provided for the cross-attention
														# we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
														if encoder_attention_mask.dim() == 3:
																					__snake_case	:    List[Any]											=     encoder_attention_mask[:, None, :, :]
														if encoder_attention_mask.dim() == 2:
																					__snake_case	:    Dict											=     encoder_attention_mask[:, None, None, :]

														__snake_case	:    Optional[int]											=     encoder_extended_attention_mask.to(
														    dtype=next(self.parameters()       ).dtype       )  # fp16 compatibility
														__snake_case	:    List[str]											=     (1.0 - encoder_extended_attention_mask) * -10000.0

														# Prepare head mask if needed
														# 1.0 in head_mask indicate we keep the head
														# attention_probs has shape bsz x n_heads x N x N
														# input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
														# and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
														__snake_case	:    Optional[int]											=     self.get_head_mask(__lowerCAmelCase					,			self.config.num_hidden_layers       )

														__snake_case	:    Optional[int]											=     self.embeddings(
														    input_ids=__lowerCAmelCase					,			position_ids=__lowerCAmelCase					,			token_type_ids=__lowerCAmelCase					,			inputs_embeds=__lowerCAmelCase       )
														__snake_case	:    Any											=     self.encoder(
														    __lowerCAmelCase					,			attention_mask=__lowerCAmelCase					,			head_mask=__lowerCAmelCase					,			encoder_hidden_states=__lowerCAmelCase					,			encoder_attention_mask=__lowerCAmelCase					,			)
														__snake_case	:    Optional[Any]											=     encoder_outputs[0]
														__snake_case	:    Dict											=     self.pooler(__lowerCAmelCase       )

														__snake_case	:    List[str]											=     (
														    sequence_output,
														    pooled_output,
														) + encoder_outputs[
														    1:
														]  # add hidden_states and attentions if they are here
														return outputs  # sequence_output, pooled_output, (hidden_states), (attentions), highway exits

class        a	(_lowerCAmelCase   ):




							"""simple docstring"""


							def __init__(							self :						List[str]					,			lowerCamelCase :						Any					,			lowerCamelCase :						Union[str, Any]       )			->					int:
														__snake_case	:    List[str]											=     message
														__snake_case	:    List[str]											=     exit_layer  # start from 1!

class        a	(nn.Module   ):




							"""simple docstring"""


							def __init__(							self :						Dict					,			lowerCamelCase :						List[Any]       )			->					Optional[Any]:
														super().__init__()
														__snake_case	:    Optional[int]											=     BertPooler(__lowerCAmelCase       )
														__snake_case	:    Tuple											=     nn.Dropout(config.hidden_dropout_prob       )
														__snake_case	:    List[Any]											=     nn.Linear(config.hidden_size					,			config.num_labels       )






							def 					__snake_case      (							self :						str					,			lowerCamelCase :						int       )			->					Optional[int]:
														__snake_case	:    int											=     encoder_outputs[0]
														__snake_case	:    Any											=     self.pooler(__lowerCAmelCase       )
														# "return" pooler_output

														# BertModel
														__snake_case	:    int											=     (pooler_input, pooler_output) + encoder_outputs[1:]
														# "return" bmodel_output

														# Dropout and classification
														__snake_case	:    Any											=     bmodel_output[1]

														__snake_case	:    Dict											=     self.dropout(__lowerCAmelCase       )
														__snake_case	:    Optional[Any]											=     self.classifier(__lowerCAmelCase       )

														return logits, pooled_output

@add_start_docstrings(
    "Bert Model (with early exiting - DeeBERT) with a classifier on top,\n    also takes care of multi-layer training. "  ,       _lowerCAmelCase  ,       )
class        a	(_lowerCAmelCase   ):




							"""simple docstring"""


							def __init__(							self :						int					,			lowerCamelCase :						Union[str, Any]       )			->					Union[str, Any]:
														super().__init__(__lowerCAmelCase       )
														__snake_case	:    Dict											=     config.num_labels
														__snake_case	:    str											=     config.num_hidden_layers

														__snake_case	:    Optional[Any]											=     DeeBertModel(__lowerCAmelCase       )
														__snake_case	:    Union[str, Any]											=     nn.Dropout(config.hidden_dropout_prob       )
														__snake_case	:    int											=     nn.Linear(config.hidden_size					,			self.config.num_labels       )

														self.init_weights()






							@add_start_docstrings_to_model_forward(__lowerCAmelCase       )
							def 					__snake_case      (							self :						List[str]					,			lowerCamelCase :						List[str]=None					,			lowerCamelCase :						Any=None					,			lowerCamelCase :						List[str]=None					,			lowerCamelCase :						str=None					,			lowerCamelCase :						Union[str, Any]=None					,			lowerCamelCase :						int=None					,			lowerCamelCase :						Any=None					,			lowerCamelCase :						List[str]=-1					,			lowerCamelCase :						Union[str, Any]=False					,			)			->					str:
														__snake_case	:    Optional[int]											=     self.num_layers
														try:
																					__snake_case	:    Union[str, Any]											=     self.bert(
																					    __lowerCAmelCase					,			attention_mask=__lowerCAmelCase					,			token_type_ids=__lowerCAmelCase					,			position_ids=__lowerCAmelCase					,			head_mask=__lowerCAmelCase					,			inputs_embeds=__lowerCAmelCase					,			)
																					# sequence_output, pooled_output, (hidden_states), (attentions), highway exits

																					__snake_case	:    List[str]											=     outputs[1]

																					__snake_case	:    Union[str, Any]											=     self.dropout(__lowerCAmelCase       )
																					__snake_case	:    Tuple											=     self.classifier(__lowerCAmelCase       )
																					__snake_case	:    Any											=     (logits,) + outputs[2:]  # add hidden states and attention if they are here
														except HighwayException as e:
																					__snake_case	:    Union[str, Any]											=     e.message
																					__snake_case	:    Union[str, Any]											=     e.exit_layer
																					__snake_case	:    Any											=     outputs[0]

														if not self.training:
																					__snake_case	:    Dict											=     entropy(__lowerCAmelCase       )
																					__snake_case	:    Optional[Any]											=     []
																					__snake_case	:    Dict											=     []
														if labels is not None:
																					if self.num_labels == 1:
																												#  We are doing regression
																												__snake_case	:    Any											=     MSELoss()
																												__snake_case	:    Union[str, Any]											=     loss_fct(logits.view(-1       )					,			labels.view(-1       )       )
																					else:
																												__snake_case	:    Any											=     CrossEntropyLoss()
																												__snake_case	:    Any											=     loss_fct(logits.view(-1					,			self.num_labels       )					,			labels.view(-1       )       )

																					# work with highway exits
																					__snake_case	:    Union[str, Any]											=     []
																					for highway_exit in outputs[-1]:
																												__snake_case	:    Dict											=     highway_exit[0]
																												if not self.training:
																																			highway_logits_all.append(__lowerCAmelCase       )
																																			highway_entropy.append(highway_exit[2]       )
																												if self.num_labels == 1:
																																			#  We are doing regression
																																			__snake_case	:    Any											=     MSELoss()
																																			__snake_case	:    int											=     loss_fct(highway_logits.view(-1       )					,			labels.view(-1       )       )
																												else:
																																			__snake_case	:    str											=     CrossEntropyLoss()
																																			__snake_case	:    Dict											=     loss_fct(highway_logits.view(-1					,			self.num_labels       )					,			labels.view(-1       )       )
																												highway_losses.append(__lowerCAmelCase       )

																					if train_highway:
																												__snake_case	:    Union[str, Any]											=     (sum(highway_losses[:-1]       ),) + outputs
																												# exclude the final highway, of course
																					else:
																												__snake_case	:    str											=     (loss,) + outputs
														if not self.training:
																					__snake_case	:    List[Any]											=     outputs + ((original_entropy, highway_entropy), exit_layer)
																					if output_layer >= 0:
																												__snake_case	:    Optional[int]											=     (
																												    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
																												)  # use the highway of the last layer

														return outputs  # (loss), logits, (hidden_states), (attentions), (highway_exits)





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

_snake_case  :	Any			      =       logging.get_logger(__name__)







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

							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}.'			)

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

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

																					__snake_case	:    Tuple											=     old_value.__dict__
																					if is_abit:
																												__snake_case	:    List[str]											=     bnb.nn.IntaParams(__lowerCamelCase							,			requires_grad=__lowerCamelCase							,			**__lowerCamelCase			).to(__lowerCamelCase			)
																					elif is_abit:
																												__snake_case	:    Optional[int]											=     bnb.nn.Paramsabit(__lowerCamelCase							,			requires_grad=__lowerCamelCase							,			**__lowerCamelCase			).to(__lowerCamelCase			)

																					__snake_case	:    Dict											=     new_value
																					if fpaa_statistics is not None:
																												setattr(module.weight							,			"SCB"							,			fpaa_statistics.to(__lowerCamelCase			)			)

							else:
														if value is None:
																					__snake_case	:    Tuple											=     old_value.to(__lowerCamelCase			)
														elif isinstance(__lowerCamelCase							,			torch.Tensor			):
																					__snake_case	:    Dict											=     value.to(__lowerCamelCase			)
														else:
																					__snake_case	:    List[Any]											=     torch.tensor(__lowerCamelCase							,			device=__lowerCamelCase			)

														if is_buffer:
																					__snake_case	:    Optional[Any]											=     new_value
														else:
																					__snake_case	:    int											=     nn.Parameter(__lowerCamelCase							,			requires_grad=old_value.requires_grad			)
																					__snake_case	:    Tuple											=     new_value







def        lowerCAmelCase_  (      __lowerCamelCase							,			__lowerCamelCase=None							,			__lowerCamelCase=None							,			__lowerCamelCase=None							,			__lowerCamelCase=False			):
							for name, module in model.named_children():
														if current_key_name is None:
																					__snake_case	:    List[Any]											=     []
														current_key_name.append(__lowerCamelCase			)

														if (isinstance(__lowerCamelCase							,			nn.Linear			) or isinstance(__lowerCamelCase							,			__lowerCamelCase			)) 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(__lowerCamelCase			) for key in modules_to_not_convert			):
																												with init_empty_weights():
																																			if isinstance(__lowerCamelCase							,			__lowerCamelCase			):
																																										__snake_case				,    __snake_case	:    Union[str, Any]											=     module.weight.shape
																																			else:
																																										__snake_case	:    List[Any]											=     module.in_features
																																										__snake_case	:    Tuple											=     module.out_features

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







def        lowerCAmelCase_  (      __lowerCamelCase							,			__lowerCamelCase=None							,			__lowerCamelCase=None							,			__lowerCamelCase=None			):
							__snake_case	:    List[str]											=     ["lm_head"] if modules_to_not_convert is None else modules_to_not_convert
							__snake_case				,    __snake_case	:    Any											=     _replace_with_bnb_linear(
							    __lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase			)

							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_  (      *__lowerCamelCase							,			**__lowerCamelCase			):
							warnings.warn(
							    "`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead"							,			__lowerCamelCase							,			)
							return replace_with_bnb_linear(*__lowerCamelCase							,			**__lowerCamelCase			)







def        lowerCAmelCase_  (      *__lowerCamelCase							,			**__lowerCamelCase			):
							warnings.warn(
							    "`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead"							,			__lowerCamelCase							,			)
							return set_module_quantized_tensor_to_device(*__lowerCamelCase							,			**__lowerCamelCase			)







def        lowerCAmelCase_  (      __lowerCamelCase			):
							__snake_case	:    Optional[int]											=     deepcopy(__lowerCamelCase			)  # this has 0 cost since it is done inside `init_empty_weights` context manager`
							tied_model.tie_weights()

							__snake_case	:    Any											=     find_tied_parameters(__lowerCamelCase			)
							# For compatibility with Accelerate < 0.18
							if isinstance(__lowerCamelCase							,			__lowerCamelCase			):
														__snake_case	:    Dict											=     sum(list(tied_params.values()			)							,			[]			) + list(tied_params.keys()			)
							else:
														__snake_case	:    Any											=     sum(__lowerCamelCase							,			[]			)
							__snake_case	:    Optional[int]											=     len(__lowerCamelCase			) > 0

							# Check if it is a base model
							__snake_case	:    Dict											=     not hasattr(__lowerCamelCase							,			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
							__snake_case	:    Optional[int]											=     list(model.named_children()			)
							__snake_case	:    List[str]											=     [list_modules[-1][0]]

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

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

							return filtered_module_names

