





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       :List[str]       =  logging.get_logger(__name__)







def       UpperCAmelCase							(     _lowerCamelCase      :  str			,   _lowerCamelCase      :  Dict			,   _lowerCamelCase      :  str			,   _lowerCamelCase      :  Optional[int]=None			,   _lowerCamelCase      :  Optional[int]=None     ):





						'''simple docstring'''


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

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

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

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

																		SCREAMING_SNAKE_CASE__     :				List[str]   =					new_value
																		if fpaa_statistics is not None:
																								setattr(module.weight			,   "SCB"			,   fpaa_statistics.to(_lowerCamelCase     )     )

						else:
												if value is None:
																		SCREAMING_SNAKE_CASE__     :				List[str]   =					old_value.to(_lowerCamelCase     )
												elif isinstance(_lowerCamelCase			,   torch.Tensor     ):
																		SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					value.to(_lowerCamelCase     )
												else:
																		SCREAMING_SNAKE_CASE__     :				Any   =					torch.tensor(_lowerCamelCase			,   device=_lowerCamelCase     )

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







def       UpperCAmelCase							(     _lowerCamelCase      :  Dict			,   _lowerCamelCase      :  Tuple=None			,   _lowerCamelCase      :  Any=None			,   _lowerCamelCase      :  Tuple=None			,   _lowerCamelCase      :  Union[str, Any]=False     ):





						'''simple docstring'''


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

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





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					["lm_head"] if modules_to_not_convert is None else modules_to_not_convert
						SCREAMING_SNAKE_CASE__,							SCREAMING_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       UpperCAmelCase							(     *_lowerCamelCase      :  Dict			,   **_lowerCamelCase      :  List[Any]     ):





						'''simple docstring'''


						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       UpperCAmelCase							(     *_lowerCamelCase      :  Union[str, Any]			,   **_lowerCamelCase      :  Optional[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"			,   _lowerCamelCase			,   )
						return set_module_quantized_tensor_to_device(*_lowerCamelCase			,   **_lowerCamelCase     )







def       UpperCAmelCase							(     _lowerCamelCase      :  Tuple     ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__     :				int   =					deepcopy(_lowerCamelCase     )  # this has 0 cost since it is done inside `init_empty_weights` context manager`
						tied_model.tie_weights()

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

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

						# add last module together with tied weights
						SCREAMING_SNAKE_CASE__     :				int   =					set(_lowerCamelCase     ) - set(_lowerCamelCase     )
						SCREAMING_SNAKE_CASE__     :				Any   =					list(set(_lowerCamelCase     )     ) + list(_lowerCamelCase     )

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

						return filtered_module_names






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding



class    _a     (				lowercase__							):






							"""simple docstring"""

							snake_case_							 =						["image_processor", "tokenizer"]
							snake_case_							 =						"CLIPImageProcessor"
							snake_case_							 =						("CLIPTokenizer", "CLIPTokenizerFast")

							def __init__(      self				:     Any							,	a				:     List[Any]=None							,	a				:     Any=None							,	**a				:     int    )	->int:
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					None
													if "feature_extractor" in kwargs:
																			warnings.warn(
																			    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
																			    " instead."							,	a							,	)
																			SCREAMING_SNAKE_CASE__     :				List[Any]   =					kwargs.pop("feature_extractor"    )

													SCREAMING_SNAKE_CASE__     :				int   =					image_processor if image_processor is not None else feature_extractor
													if image_processor is None:
																			raise ValueError("You need to specify an `image_processor`."    )
													if tokenizer is None:
																			raise ValueError("You need to specify a `tokenizer`."    )

													super().__init__(a							,	a    )

							def __call__(      self				:     Tuple							,	a				:     Tuple=None							,	a				:     Union[str, Any]=None							,	a				:     List[str]=None							,	**a				:     Optional[Any]    )	->Optional[Any]:

													if text is None and images is None:
																			raise ValueError("You have to specify either text or images. Both cannot be none."    )

													if text is not None:
																			SCREAMING_SNAKE_CASE__     :				str   =					self.tokenizer(a							,	return_tensors=a							,	**a    )

													if images is not None:
																			SCREAMING_SNAKE_CASE__     :				int   =					self.image_processor(a							,	return_tensors=a							,	**a    )

													if text is not None and images is not None:
																			SCREAMING_SNAKE_CASE__     :				Tuple   =					image_features.pixel_values
																			return encoding
													elif text is not None:
																			return encoding
													else:
																			return BatchEncoding(data=dict(**a    )							,	tensor_type=a    )

							def      A_  (      self				:     Optional[int]							,	*a				:     Any							,	**a				:     List[str]    )	->Any:
													return self.tokenizer.batch_decode(*a							,	**a    )

							def      A_  (      self				:     Any							,	*a				:     Optional[int]							,	**a				:     Dict    )	->Any:
													return self.tokenizer.decode(*a							,	**a    )

							@property
							def      A_  (      self				:     List[str]    )	->Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :				Dict   =					self.tokenizer.model_input_names
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					self.image_processor.model_input_names
													return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names    )    )

							@property
							def      A_  (      self				:     Optional[int]    )	->List[Any]:
													warnings.warn(
													    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."							,	a							,	)
													return self.image_processor_class






							@property
							def      A_  (      self				:     Dict    )	->str:
													warnings.warn(
													    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."							,	a							,	)
													return self.image_processor