



"""simple docstring"""







import importlib.metadata
import warnings
from copy import deepcopy

from packaging import version

from ..utils import logging
from .import_utils import is_accelerate_available, is_bitsandbytes_available


if is_bitsandbytes_available():
	import bitsandbytes as bnb
	import torch
	import torch.nn as nn

	from ..pytorch_utils import ConvaD

if is_accelerate_available():
	from accelerate import init_empty_weights
	from accelerate.utils import find_tied_parameters

a_							=    logging.get_logger(__name__)

def     UpperCAmelCase_					(				__a   :			Optional[int]      ,							__a   :			Any      ,							__a   :			Union[str, Any]      ,							__a   :			Optional[int]=None      ,							__a   :			List[str]=None						):




			'''simple docstring'''



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

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

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

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

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

									_lowerCamelCase					:					Dict	       =    new_value
									if fpaa_statistics is not None:
												setattr(module.weight      ,							'SCB'      ,							fpaa_statistics.to(__a						)						)

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

						if is_buffer:
									_lowerCamelCase					:					str	       =    new_value
						else:
									_lowerCamelCase					:					Dict	       =    nn.Parameter(__a      ,							requires_grad=old_value.requires_grad						)
									_lowerCamelCase					:					int	       =    new_value

def     UpperCAmelCase_					(				__a   :			Union[str, Any]      ,							__a   :			str=None      ,							__a   :			List[str]=None      ,							__a   :			str=None      ,							__a   :			Dict=False						):




			'''simple docstring'''



			for name, module in model.named_children():
						if current_key_name is None:
									_lowerCamelCase					:					Tuple	       =    []
						current_key_name.append(__a						)

						if (isinstance(__a      ,							nn.Linear						) or isinstance(__a      ,							__a						)) 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(__a						) for key in modules_to_not_convert						):
												with init_empty_weights():
															if isinstance(__a      ,							__a						):
																		_lowerCamelCase       ,							_lowerCamelCase					:					str	       =    module.weight.shape
															else:
																		_lowerCamelCase					:					Optional[int]	       =    module.in_features
																		_lowerCamelCase					:					int	       =    module.out_features

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

def     UpperCAmelCase_					(				__a   :			Union[str, Any]      ,							__a   :			List[Any]=None      ,							__a   :			Optional[Any]=None      ,							__a   :			str=None						):




			'''simple docstring'''



			_lowerCamelCase					:					Optional[Any]	       =    ['lm_head'] if modules_to_not_convert is None else modules_to_not_convert
			_lowerCamelCase       ,							_lowerCamelCase					:					List[str]	       =    _replace_with_bnb_linear(
			    __a      ,							__a      ,							__a      ,							__a						)

			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_					(				*__a   :			Tuple      ,							**__a   :			Optional[int]						):




			'''simple docstring'''



			warnings.warn(
			    '`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead'      ,							__a      ,							)
			return replace_with_bnb_linear(*__a      ,							**__a						)

def     UpperCAmelCase_					(				*__a   :			List[str]      ,							**__a   :			Tuple						):




			'''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'      ,							__a      ,							)
			return set_module_quantized_tensor_to_device(*__a      ,							**__a						)



def     UpperCAmelCase_					(				__a   :			int						):




			'''simple docstring'''



			_lowerCamelCase					:					Union[str, Any]	       =    deepcopy(__a						)  # this has 0 cost since it is done inside `init_empty_weights` context manager`
			tied_model.tie_weights()

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

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

			# add last module together with tied weights
			_lowerCamelCase					:					Dict	       =    set(__a						) - set(__a						)
			_lowerCamelCase					:					Dict	       =    list(set(__a						)						) + list(__a						)

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

			return filtered_module_names







'''simple docstring'''
from collections import OrderedDict
from typing import Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...feature_extraction_utils import FeatureExtractionMixin
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import TensorType, logging


lowerCAmelCase					:    Any           = logging.get_logger(__name__)

lowerCAmelCase					:    Optional[int]           = {
    'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json',
    # See all Perceiver models at https://huggingface.co/models?filter=perceiver
}


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):
				lowerCAmelCase_     			=							"""perceiver"""







				def __init__(       self						,				A_=256						,				A_=1280						,				A_=768						,				A_=1						,				A_=26						,				A_=8						,				A_=8						,				A_=None						,				A_=None						,				A_="kv"						,				A_=1						,				A_=1						,				A_="gelu"						,				A_=0.1						,				A_=0.02						,				A_=1e-12						,				A_=True						,				A_=262						,				A_=2048						,				A_=56						,				A_=[368, 496]						,				A_=16						,				A_=1920						,				A_=16						,				A_=[1, 16, 224, 224]						,				**A_						,				)->		str:







											'''simple docstring'''






											super().__init__(**A_     )

											UpperCamelCase   			=     num_latents
											UpperCamelCase   			=     d_latents
											UpperCamelCase   			=     d_model
											UpperCamelCase   			=     num_blocks
											UpperCamelCase   			=     num_self_attends_per_block
											UpperCamelCase   			=     num_self_attention_heads
											UpperCamelCase   			=     num_cross_attention_heads
											UpperCamelCase   			=     qk_channels
											UpperCamelCase   			=     v_channels
											UpperCamelCase   			=     cross_attention_shape_for_attention
											UpperCamelCase   			=     self_attention_widening_factor
											UpperCamelCase   			=     cross_attention_widening_factor
											UpperCamelCase   			=     hidden_act
											UpperCamelCase   			=     attention_probs_dropout_prob
											UpperCamelCase   			=     initializer_range
											UpperCamelCase   			=     layer_norm_eps
											UpperCamelCase   			=     use_query_residual
											# masked language modeling attributes
											UpperCamelCase   			=     vocab_size
											UpperCamelCase   			=     max_position_embeddings
											# image classification attributes
											UpperCamelCase   			=     image_size
											# flow attributes
											UpperCamelCase   			=     train_size
											# multimodal autoencoding attributes
											UpperCamelCase   			=     num_frames
											UpperCamelCase   			=     audio_samples_per_frame
											UpperCamelCase   			=     samples_per_patch
											UpperCamelCase   			=     output_shape


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):







				@property
				def 						UpperCAmelCase_    (       self     )->		Mapping[str, Mapping[int, str]]:







											'''simple docstring'''






											if self.task == "multiple-choice":
																		UpperCamelCase   			=     {0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																		UpperCamelCase   			=     {0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('inputs', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ]     )







				@property
				def 						UpperCAmelCase_    (       self     )->		float:







											'''simple docstring'''






											return 1e-4







				def 						UpperCAmelCase_    (       self						,				A_						,				A_ = -1						,				A_ = -1						,				A_ = -1						,				A_ = False						,				A_ = None						,				A_ = 3						,				A_ = 40						,				A_ = 40						,				)->		Mapping[str, Any]:







											'''simple docstring'''






											if isinstance(A_						,				A_     ):
																		# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
																		UpperCamelCase   			=     compute_effective_axis_dimension(
																		    A_						,				fixed_dimension=OnnxConfig.default_fixed_batch						,				num_token_to_add=0     )
																		# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
																		UpperCamelCase   			=     preprocessor.num_special_tokens_to_add(A_     )
																		UpperCamelCase   			=     compute_effective_axis_dimension(
																		    A_						,				fixed_dimension=OnnxConfig.default_fixed_sequence						,				num_token_to_add=A_     )
																		# Generate dummy inputs according to compute batch and sequence
																		UpperCamelCase   			=     [' '.join(['a']     ) * seq_length] * batch_size
																		UpperCamelCase   			=     dict(preprocessor(A_						,				return_tensors=A_     )     )
																		UpperCamelCase   			=     inputs.pop('input_ids'     )
																		return inputs
											elif isinstance(A_						,				A_     ) and preprocessor.model_input_names[0] == "pixel_values":
																		# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
																		UpperCamelCase   			=     compute_effective_axis_dimension(A_						,				fixed_dimension=OnnxConfig.default_fixed_batch     )
																		UpperCamelCase   			=     self._generate_dummy_images(A_						,				A_						,				A_						,				A_     )
																		UpperCamelCase   			=     dict(preprocessor(images=A_						,				return_tensors=A_     )     )
																		UpperCamelCase   			=     inputs.pop('pixel_values'     )
																		return inputs
											else:
																		raise ValueError(
																		    'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.'     )




