




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

SCREAMING_SNAKE_CASE_            = logging.get_logger(__name__)

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

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

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

												_UpperCAmelCase    :						Union[str, Any]							=      old_value.__dict__
												if is_abit:
																_UpperCAmelCase    :						Union[str, Any]							=      bnb.nn.IntaParams(lowerCAmelCase					, requires_grad=lowerCAmelCase					, **lowerCAmelCase	).to(lowerCAmelCase	)
												elif is_abit:
																_UpperCAmelCase    :						Union[str, Any]							=      bnb.nn.Paramsabit(lowerCAmelCase					, requires_grad=lowerCAmelCase					, **lowerCAmelCase	).to(lowerCAmelCase	)

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

				else:
								if value is None:
												_UpperCAmelCase    :						int							=      old_value.to(lowerCAmelCase	)
								elif isinstance(lowerCAmelCase					, torch.Tensor	):
												_UpperCAmelCase    :						Optional[int]							=      value.to(lowerCAmelCase	)
								else:
												_UpperCAmelCase    :						Dict							=      torch.tensor(lowerCAmelCase					, device=lowerCAmelCase	)

								if is_buffer:
												_UpperCAmelCase    :						Union[str, Any]							=      new_value
								else:
												_UpperCAmelCase    :						Dict							=      nn.Parameter(lowerCAmelCase					, requires_grad=old_value.requires_grad	)
												_UpperCAmelCase    :						Tuple							=      new_value

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					Tuple					, lowerCAmelCase:					int=None					, lowerCAmelCase:					Union[str, Any]=None					, lowerCAmelCase:					str=None					, lowerCAmelCase:					str=False	)				->    List[str]:
				for name, module in model.named_children():
								if current_key_name is None:
												_UpperCAmelCase    :						Optional[int]							=      []
								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	):
																								_UpperCAmelCase						,							_UpperCAmelCase    :						List[Any]							=      module.weight.shape
																				else:
																								_UpperCAmelCase    :						Dict							=      module.in_features
																								_UpperCAmelCase    :						Union[str, Any]							=      module.out_features

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

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

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

				# add last module together with tied weights
				_UpperCAmelCase    :						Tuple							=      set(lowerCAmelCase	) - set(lowerCAmelCase	)
				_UpperCAmelCase    :						int							=      list(set(lowerCAmelCase	)	) + list(lowerCAmelCase	)

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

				return filtered_module_names





from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload

class 			a					:


						def __init__(    self  ,   A_ = None		):







										'''simple docstring'''


										if components is None:
														_UpperCAmelCase    :						Dict							=      []
										_UpperCAmelCase    :						Dict							=      list(A_		)


						def __len__(    self		):







										'''simple docstring'''


										return len(self.__components		)


						def __str__(    self		):







										'''simple docstring'''


										return "(" + ",".join(map(A_  ,   self.__components		)		) + ")"


						def __add__(    self  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						int							=      len(self		)
										if size == len(A_		):
														_UpperCAmelCase    :						Union[str, Any]							=      [self.__components[i] + other.component(A_		) for i in range(A_		)]
														return Vector(A_		)
										else:
														raise Exception("must have the same size"		)


						def __sub__(    self  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[str]							=      len(self		)
										if size == len(A_		):
														_UpperCAmelCase    :						Optional[Any]							=      [self.__components[i] - other.component(A_		) for i in range(A_		)]
														return Vector(A_		)
										else:  # error case
														raise Exception("must have the same size"		)


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										if isinstance(A_  ,   (float, int)		):
														_UpperCAmelCase    :						str							=      [c * other for c in self.__components]
														return Vector(A_		)
										elif isinstance(A_  ,   A_		) and len(self		) == len(A_		):
														_UpperCAmelCase    :						int							=      len(self		)
														_UpperCAmelCase    :						Any							=      [self.__components[i] * other.component(A_		) for i in range(A_		)]
														return sum(A_		)
										else:  # error case
														raise Exception("invalid operand!"		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return Vector(self.__components		)


						def     _UpperCAmelCase			(    self  ,   A_		):







										'''simple docstring'''


										if isinstance(A_  ,   A_		) and -len(self.__components		) <= i < len(self.__components		):
														return self.__components[i]
										else:
														raise Exception("index out of range"		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										assert -len(self.__components		) <= pos < len(self.__components		)
										_UpperCAmelCase    :						Any							=      value


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										if len(self.__components		) == 0:
														raise Exception("Vector is empty"		)
										_UpperCAmelCase    :						Dict							=      [c**2 for c in self.__components]
										return math.sqrt(sum(A_		)		)





						def     _UpperCAmelCase			(    self  ,   A_  ,   A_ = False		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      self * other
										_UpperCAmelCase    :						str							=      self.euclidean_length() * other.euclidean_length()
										if deg:
														return math.degrees(math.acos(num / den		)		)
										else:
														return math.acos(num / den		)







def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int	)				->    Vector:
				assert isinstance(lowerCAmelCase					, lowerCAmelCase	)
				return Vector([0] * dimension	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int					, lowerCAmelCase:					int	)				->    Vector:
				assert isinstance(lowerCAmelCase					, lowerCAmelCase	) and (isinstance(lowerCAmelCase					, lowerCAmelCase	))
				_UpperCAmelCase    :						Tuple							=      [0] * dimension
				_UpperCAmelCase    :						Optional[Any]							=      1
				return Vector(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					float					, lowerCAmelCase:					Vector					, lowerCAmelCase:					Vector	)				->    Vector:
				assert (
				    isinstance(lowerCAmelCase					, lowerCAmelCase	)
				    and isinstance(lowerCAmelCase					, lowerCAmelCase	)
				    and (isinstance(lowerCAmelCase					, (int, float)	))
				)
				return x * scalar + y

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int					, lowerCAmelCase:					int					, lowerCAmelCase:					int	)				->    Vector:
				random.seed(lowerCAmelCase	)
				_UpperCAmelCase    :						Optional[int]							=      [random.randint(lowerCAmelCase					, lowerCAmelCase	) for _ in range(lowerCAmelCase	)]
				return Vector(lowerCAmelCase	)

class 			a					:


						def __init__(    self  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      matrix
										_UpperCAmelCase    :						Union[str, Any]							=      w
										_UpperCAmelCase    :						str							=      h


						def __str__(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[int]							=      ""
										for i in range(self.__height		):
														ans += "|"
														for j in range(self.__width		):
																		if j < self.__width - 1:
																						ans += str(self.__matrix[i][j]		) + ","
																		else:
																						ans += str(self.__matrix[i][j]		) + "|\n"
										return ans


						def __add__(    self  ,   A_		):







										'''simple docstring'''


										if self.__width == other.width() and self.__height == other.height():
														_UpperCAmelCase    :						Optional[int]							=      []
														for i in range(self.__height		):
																		_UpperCAmelCase    :						List[str]							=      [
																		    self.__matrix[i][j] + other.component(A_  ,   A_		)
																		    for j in range(self.__width		)
																		]
																		matrix.append(A_		)
														return Matrix(A_  ,   self.__width  ,   self.__height		)
										else:
														raise Exception("matrix must have the same dimension!"		)


						def __sub__(    self  ,   A_		):







										'''simple docstring'''


										if self.__width == other.width() and self.__height == other.height():
														_UpperCAmelCase    :						Optional[Any]							=      []
														for i in range(self.__height		):
																		_UpperCAmelCase    :						Tuple							=      [
																		    self.__matrix[i][j] - other.component(A_  ,   A_		)
																		    for j in range(self.__width		)
																		]
																		matrix.append(A_		)
														return Matrix(A_  ,   self.__width  ,   self.__height		)
										else:
														raise Exception("matrices must have the same dimension!"		)


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										if isinstance(A_  ,   A_		):  # matrix-vector
														if len(A_		) == self.__width:
																		_UpperCAmelCase    :						Union[str, Any]							=      zero_vector(self.__height		)
																		for i in range(self.__height		):
																						_UpperCAmelCase    :						List[Any]							=      [
																						    self.__matrix[i][j] * other.component(A_		)
																						    for j in range(self.__width		)
																						]
																						ans.change_component(A_  ,   sum(A_		)		)
																		return ans
														else:
																		raise Exception(
																		    "vector must have the same size as the "
																		    "number of columns of the matrix!"		)
										elif isinstance(A_  ,   (int, float)		):  # matrix-scalar
														_UpperCAmelCase    :						Any							=      [
														    [self.__matrix[i][j] * other for j in range(self.__width		)]
														    for i in range(self.__height		)
														]
														return Matrix(A_  ,   self.__width  ,   self.__height		)
										return None


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return self.__height


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return self.__width


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										if 0 <= x < self.__height and 0 <= y < self.__width:
														return self.__matrix[x][y]
										else:
														raise Exception("change_component: indices out of bounds"		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										if 0 <= x < self.__height and 0 <= y < self.__width:
														_UpperCAmelCase    :						Tuple							=      value
										else:
														raise Exception("change_component: indices out of bounds"		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										if self.__height != self.__width:
														raise Exception("Matrix is not square"		)
										_UpperCAmelCase    :						Union[str, Any]							=      self.__matrix[:x] + self.__matrix[x + 1 :]
										for i in range(len(A_		)		):
														_UpperCAmelCase    :						Tuple							=      minor[i][:y] + minor[i][y + 1 :]
										return Matrix(A_  ,   self.__width - 1  ,   self.__height - 1		).determinant()


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										if self.__height != self.__width:
														raise Exception("Matrix is not square"		)
										if 0 <= x < self.__height and 0 <= y < self.__width:
														return (-1) ** (x + y) * self.minor(A_  ,   A_		)
										else:
														raise Exception("Indices out of bounds"		)





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										if self.__height != self.__width:
														raise Exception("Matrix is not square"		)
										if self.__height < 1:
														raise Exception("Matrix has no element"		)
										elif self.__height == 1:
														return self.__matrix[0][0]
										elif self.__height == 2:
														return (
														    self.__matrix[0][0] * self.__matrix[1][1]
														    - self.__matrix[0][1] * self.__matrix[1][0]
														)
										else:
														_UpperCAmelCase    :						Dict							=      [
														    self.__matrix[0][y] * self.cofactor(0  ,   A_		) for y in range(self.__width		)
														]
														return sum(A_		)







def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int	)				->    Matrix:
				_UpperCAmelCase    :						list[list[float]]							=      [[0] * n for _ in range(lowerCAmelCase	)]
				return Matrix(lowerCAmelCase					, lowerCAmelCase					, lowerCAmelCase	)





def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int					, lowerCAmelCase:					int					, lowerCAmelCase:					int					, lowerCAmelCase:					int	)				->    Matrix:
				random.seed(lowerCAmelCase	)
				_UpperCAmelCase    :						list[list[float]]							=      [
				    [random.randint(lowerCAmelCase					, lowerCAmelCase	) for _ in range(lowerCAmelCase	)] for _ in range(lowerCAmelCase	)
				]
				return Matrix(lowerCAmelCase					, lowerCAmelCase					, lowerCAmelCase	)


