



"""simple docstring"""

import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import MaskFormerSwinBackbone
     from transformers.models.maskformer import MaskFormerSwinModel




class  _UpperCAmelCase :

 def __init__(  self	: str						,  lowercase_	: List[str]						,  lowercase_	: Optional[Any]=13						,  lowercase_	: Optional[Any]=32						,  lowercase_	: Optional[Any]=2						,  lowercase_	: Dict=3						,  lowercase_	: Optional[Any]=16						,  lowercase_	: Any=[1, 2, 1]						,  lowercase_	: List[str]=[2, 2, 4]						,  lowercase_	: Union[str, Any]=2						,  lowercase_	: Any=2.0						,  lowercase_	: List[Any]=True						,  lowercase_	: Any=0.0						,  lowercase_	: Union[str, Any]=0.0						,  lowercase_	: List[Any]=0.1						,  lowercase_	: Dict="gelu"						,  lowercase_	: Union[str, Any]=False						,  lowercase_	: str=True						,  lowercase_	: Tuple=0.02						,  lowercase_	: Dict=1E-5						,  lowercase_	: Union[str, Any]=True						,  lowercase_	: Optional[int]=None						,  lowercase_	: Dict=True						,  lowercase_	: str=10						,  lowercase_	: Union[str, Any]=8						,  lowercase_	: int=["stage1", "stage2", "stage3"]						,  lowercase_	: Optional[int]=[1, 2, 3]						,  ):
     snake_case_				:  str       =			parent
     snake_case_				:  Tuple       =			batch_size
     snake_case_				:  Dict       =			image_size
     snake_case_				:  Dict       =			patch_size
     snake_case_				:  Union[str, Any]       =			num_channels
     snake_case_				:  Any       =			embed_dim
     snake_case_				:  Optional[int]       =			depths
     snake_case_				:  int       =			num_heads
     snake_case_				:  List[str]       =			window_size
     snake_case_				:  Dict       =			mlp_ratio
     snake_case_				:  Any       =			qkv_bias
     snake_case_				:  Union[str, Any]       =			hidden_dropout_prob
     snake_case_				:  Any       =			attention_probs_dropout_prob
     snake_case_				:  int       =			drop_path_rate
     snake_case_				:  Optional[Any]       =			hidden_act
     snake_case_				:  Tuple       =			use_absolute_embeddings
     snake_case_				:  int       =			patch_norm
     snake_case_				:  Any       =			layer_norm_eps
     snake_case_				:  Optional[Any]       =			initializer_range
     snake_case_				:  Optional[Any]       =			is_training
     snake_case_				:  Optional[Any]       =			scope
     snake_case_				:  List[str]       =			use_labels
     snake_case_				:  List[Any]       =			type_sequence_label_size
     snake_case_				:  Dict       =			encoder_stride
     snake_case_				:  Optional[int]       =			out_features
     snake_case_				:  List[str]       =			out_indices

 def 		_snake_case      (  self	: Optional[Any]       ):
     snake_case_				:  str       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

     snake_case_				:  Union[str, Any]       =			None
     if self.use_labels:
         snake_case_				:  Optional[Any]       =			ids_tensor([self.batch_size]						,  self.type_sequence_label_size       )

     snake_case_				:  Optional[int]       =			self.get_config()

     return config, pixel_values, labels

 def 		_snake_case      (  self	: List[str]       ):
     return MaskFormerSwinConfig(
         image_size=self.image_size						,  patch_size=self.patch_size						,  num_channels=self.num_channels						,  embed_dim=self.embed_dim						,  depths=self.depths						,  num_heads=self.num_heads						,  window_size=self.window_size						,  mlp_ratio=self.mlp_ratio						,  qkv_bias=self.qkv_bias						,  hidden_dropout_prob=self.hidden_dropout_prob						,  attention_probs_dropout_prob=self.attention_probs_dropout_prob						,  drop_path_rate=self.drop_path_rate						,  hidden_act=self.hidden_act						,  use_absolute_embeddings=self.use_absolute_embeddings						,  path_norm=self.patch_norm						,  layer_norm_eps=self.layer_norm_eps						,  initializer_range=self.initializer_range						,  encoder_stride=self.encoder_stride						,  out_features=self.out_features						,  out_indices=self.out_indices						,  )

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: List[Any]						,  lowercase_	: Tuple						,  lowercase_	: Any       ):
     snake_case_				:  int       =			MaskFormerSwinModel(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Any       =			model(lowercase_       )

     snake_case_				:  Union[str, Any]       =			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
     snake_case_				:  Dict       =			int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

     self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, expected_seq_len, expected_dim)       )

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: Optional[Any]						,  lowercase_	: List[str]						,  lowercase_	: Any       ):
     snake_case_				:  List[str]       =			MaskFormerSwinBackbone(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Optional[Any]       =			model(lowercase_       )

     # verify feature maps
     self.parent.assertEqual(len(result.feature_maps       )						,  len(config.out_features       )       )
     self.parent.assertListEqual(list(result.feature_maps[0].shape       )						,  [13, 16, 16, 16]       )

     # verify channels
     self.parent.assertEqual(len(model.channels       )						,  len(config.out_features       )       )
     self.parent.assertListEqual(model.channels						,  [16, 32, 64]       )

     # verify ValueError
     with self.parent.assertRaises(lowercase_       ):
         snake_case_				:  int       =			['''stem''']
         snake_case_				:  int       =			MaskFormerSwinBackbone(config=lowercase_       )






 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  Dict       =			self.prepare_config_and_inputs()
     snake_case_				:  Union[str, Any]       =			config_and_inputs
     snake_case_				:  int       =			{'''pixel_values''': pixel_values}
     return config, inputs_dict







@require_torch
class  _UpperCAmelCase (    lowerCAmelCase__					,  lowerCAmelCase__					,  unittest.TestCase):
 _lowerCAmelCase     :    Dict	      =	(
     (
         MaskFormerSwinModel,
         MaskFormerSwinBackbone,
     )
     if is_torch_available()
     else ()
 )
 _lowerCAmelCase     :    Optional[int]	      =	{"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {}
 _lowerCAmelCase     :    Optional[int]	      =	False
 _lowerCAmelCase     :    int	      =	False
 _lowerCAmelCase     :    int	      =	False
 _lowerCAmelCase     :    Union[str, Any]	      =	False
 _lowerCAmelCase     :    Tuple	      =	False

 def 		_snake_case      (  self	: str       ):
     snake_case_				:  Any       =			MaskFormerSwinModelTester(self       )
     snake_case_				:  Any       =			ConfigTester(self						,  config_class=lowercase_						,  embed_dim=37       )

 @require_torch_multi_gpu
 @unittest.skip(
     reason=(
         '''`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with'''
         ''' `nn.DataParallel`'''
     )       )
 def 		_snake_case      (  self	: Optional[int]       ):
     pass

 def 		_snake_case      (  self	: int       ):
     self.create_and_test_config_common_properties()
     self.config_tester.create_and_test_config_to_json_string()
     self.config_tester.create_and_test_config_to_json_file()
     self.config_tester.create_and_test_config_from_and_save_pretrained()
     self.config_tester.create_and_test_config_with_num_labels()
     self.config_tester.check_config_can_be_init_without_params()
     self.config_tester.check_config_arguments_init()

 def 		_snake_case      (  self	: int       ):
     return

 def 		_snake_case      (  self	: List[str]       ):
     snake_case_				:  Union[str, Any]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_       )

 def 		_snake_case      (  self	: int       ):
     snake_case_				:  Tuple       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_backbone(*lowercase_       )

 @unittest.skip('''Swin does not use inputs_embeds'''       )
 def 		_snake_case      (  self	: Dict       ):
     pass

 @unittest.skip('''Swin does not support feedforward chunking'''       )
 def 		_snake_case      (  self	: Dict       ):
     pass

 def 		_snake_case      (  self	: str       ):
     snake_case_				:  str       =			self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         snake_case_				:  Union[str, Any]       =			model_class(lowercase_       )
         self.assertIsInstance(model.get_input_embeddings()						,  (nn.Module)       )
         snake_case_				:  Union[str, Any]       =			model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(lowercase_						,  nn.Linear       )       )

 def 		_snake_case      (  self	: Any       ):
     snake_case_				:  Any       =			self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         snake_case_				:  Any       =			model_class(lowercase_       )
         snake_case_				:  Union[str, Any]       =			inspect.signature(model.forward       )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         snake_case_				:  str       =			[*signature.parameters.keys()]

         snake_case_				:  Any       =			['''pixel_values''']
         self.assertListEqual(arg_names[:1]						,  lowercase_       )

 @unittest.skip(reason='''MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'''       )
 def 		_snake_case      (  self	: Optional[Any]       ):
     pass

 @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone'''       )
 def 		_snake_case      (  self	: List[str]       ):
     pass

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: int						,  lowercase_	: Optional[int]						,  lowercase_	: Dict						,  lowercase_	: Optional[Any]       ):
     snake_case_				:  Union[str, Any]       =			model_class(lowercase_       )
     model.to(lowercase_       )
     model.eval()

     with torch.no_grad():
         snake_case_				:  int       =			model(**self._prepare_for_class(lowercase_						,  lowercase_       )       )

     snake_case_				:  int       =			outputs.hidden_states

     snake_case_				:  List[Any]       =			getattr(
         self.model_tester						,  '''expected_num_hidden_layers'''						,  len(self.model_tester.depths       ) + 1       )
     self.assertEqual(len(lowercase_       )						,  lowercase_       )

     # Swin has a different seq_length
     snake_case_				:  str       =			(
         config.patch_size
         if isinstance(config.patch_size						,  collections.abc.Iterable       )
         else (config.patch_size, config.patch_size)
     )

     snake_case_				:  Union[str, Any]       =			(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

     self.assertListEqual(
         list(hidden_states[0].shape[-2:]       )						,  [num_patches, self.model_tester.embed_dim]						,  )

 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  Optional[Any]       =			self.model_tester.prepare_config_and_inputs_for_common()

     snake_case_				:  str       =			(
         self.model_tester.image_size
         if isinstance(self.model_tester.image_size						,  collections.abc.Iterable       )
         else (self.model_tester.image_size, self.model_tester.image_size)
     )

     for model_class in self.all_model_classes:
         snake_case_				:  Dict       =			True
         self.check_hidden_states_output(lowercase_						,  lowercase_						,  lowercase_						,  lowercase_       )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case_				:  Optional[Any]       =			True

         self.check_hidden_states_output(lowercase_						,  lowercase_						,  lowercase_						,  lowercase_       )

 def 		_snake_case      (  self	: int       ):
     snake_case_				:  Optional[int]       =			self.model_tester.prepare_config_and_inputs_for_common()
     snake_case_				:  Dict       =			3

     snake_case_				:  List[Any]       =			(
         self.model_tester.image_size
         if isinstance(self.model_tester.image_size						,  collections.abc.Iterable       )
         else (self.model_tester.image_size, self.model_tester.image_size)
     )
     snake_case_				:  str       =			(
         config.patch_size
         if isinstance(config.patch_size						,  collections.abc.Iterable       )
         else (config.patch_size, config.patch_size)
     )

     snake_case_				:  str       =			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
     snake_case_				:  Optional[Any]       =			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

     for model_class in self.all_model_classes:
         snake_case_				:  List[str]       =			True
         self.check_hidden_states_output(lowercase_						,  lowercase_						,  lowercase_						,  (padded_height, padded_width)       )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case_				:  Optional[int]       =			True
         self.check_hidden_states_output(lowercase_						,  lowercase_						,  lowercase_						,  (padded_height, padded_width)       )

 @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints'''       )
 def 		_snake_case      (  self	: Optional[int]       ):
     pass

 @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''       )
 def 		_snake_case      (  self	: Tuple       ):
     pass

 @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''       )
 def 		_snake_case      (  self	: Optional[Any]       ):
     pass






 def 		_snake_case      (  self	: Any       ):
     snake_case_				:  Optional[Any]       =			self.model_tester.prepare_config_and_inputs_for_common()

     def set_nan_tensor_to_zero(lowercase_	: List[str]       ):
         snake_case_				:  List[Any]       =			0
         return t

     def check_equivalence(lowercase_	: int						,  lowercase_	: Tuple						,  lowercase_	: str						,  lowercase_	: Optional[int]={}       ):
         with torch.no_grad():
             snake_case_				:  Any       =			model(**lowercase_						,  return_dict=lowercase_						,  **lowercase_       )
             snake_case_				:  str       =			model(**lowercase_						,  return_dict=lowercase_						,  **lowercase_       ).to_tuple()

             def recursive_check(lowercase_	: Dict						,  lowercase_	: Any       ):
                 if isinstance(lowercase_						,  (List, Tuple)       ):
                     for tuple_iterable_value, dict_iterable_value in zip(lowercase_						,  lowercase_       ):
                         recursive_check(lowercase_						,  lowercase_       )
                 elif isinstance(lowercase_						,  lowercase_       ):
                     for tuple_iterable_value, dict_iterable_value in zip(
                         tuple_object.values()						,  dict_object.values()       ):
                         recursive_check(lowercase_						,  lowercase_       )
                 elif tuple_object is None:
                     return
                 else:
                     self.assertTrue(
                         torch.allclose(
                             set_nan_tensor_to_zero(lowercase_       )						,  set_nan_tensor_to_zero(lowercase_       )						,  atol=1E-5       )						,  msg=(
                             '''Tuple and dict output are not equal. Difference:'''
                             f" {torch.max(torch.abs(tuple_object - dict_object       )       )}. Tuple has `nan`:"
                             f" {torch.isnan(lowercase_       ).any()} and `inf`: {torch.isinf(lowercase_       )}. Dict has"
                             f" `nan`: {torch.isnan(lowercase_       ).any()} and `inf`: {torch.isinf(lowercase_       )}."
                         )						,  )

             recursive_check(lowercase_						,  lowercase_       )

     for model_class in self.all_model_classes:
         snake_case_				:  int       =			model_class(lowercase_       )
         model.to(lowercase_       )
         model.eval()

         snake_case_				:  Optional[int]       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         snake_case_				:  Union[str, Any]       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_       )

         snake_case_				:  int       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         snake_case_				:  Any       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_       )

         snake_case_				:  int       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         snake_case_				:  Any       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_						,  {'''output_hidden_states''': True}       )

         snake_case_				:  Union[str, Any]       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         snake_case_				:  int       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_						,  {'''output_hidden_states''': True}       )







@require_torch
class  _UpperCAmelCase (    unittest.TestCase					,  lowerCAmelCase__):
 _lowerCAmelCase     :    Optional[int]	      =	(MaskFormerSwinBackbone,) if is_torch_available() else ()
 _lowerCAmelCase     :    List[Any]	      =	MaskFormerSwinConfig

 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  Tuple       =			MaskFormerSwinModelTester(self       )






 def 		_snake_case      (  self	: int       ):
     snake_case_				:  List[str]       =			self.model_tester.prepare_config_and_inputs_for_common()
     snake_case_				:  Union[str, Any]       =			inputs_dict['''pixel_values'''].shape[0]

     for backbone_class in self.all_model_classes:
         snake_case_				:  Optional[Any]       =			backbone_class(lowercase_       )
         backbone.to(lowercase_       )
         backbone.eval()

         snake_case_				:  Dict       =			backbone(**lowercase_       )

         # Test default outputs and verify feature maps
         self.assertIsInstance(outputs.feature_maps						,  lowercase_       )
         self.assertTrue(len(outputs.feature_maps       ) == len(backbone.channels       )       )
         for feature_map, n_channels in zip(outputs.feature_maps						,  backbone.channels       ):
             self.assertTrue(feature_map.shape[:2]						,  (batch_size, n_channels)       )
         self.assertIsNone(outputs.hidden_states       )
         self.assertIsNone(outputs.attentions       )

         # Test output_hidden_states=True
         snake_case_				:  Tuple       =			backbone(**lowercase_						,  output_hidden_states=lowercase_       )
         self.assertIsNotNone(outputs.hidden_states       )
         self.assertTrue(len(outputs.hidden_states       )						,  len(backbone.stage_names       )       )
         # We skip the stem layer
         for hidden_states, n_channels in zip(outputs.hidden_states[1:]						,  backbone.channels       ):
             for hidden_state in hidden_states:
                 # Hidden states are in the format (batch_size, (height * width), n_channels)
                 snake_case_				:  List[str]       =			hidden_state.shape
                 self.assertTrue((h_batch_size, h_n_channels)						,  (batch_size, n_channels)       )

            # Test output_attentions=True
         if self.has_attentions:
             snake_case_				:  Any       =			backbone(**lowercase_						,  output_attentions=lowercase_       )
             self.assertIsNotNone(outputs.attentions       )










"""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

lowercase__					:						Optional[int]												=			logging.get_logger(__name__)




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

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

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

            snake_case_				:  Tuple       =			old_value.__dict__
            if is_abit:
                snake_case_				:  Tuple       =			bnb.nn.IntaParams(_a	,    requires_grad=_a	,    **_a					).to(_a					)
            elif is_abit:
                snake_case_				:  Any       =			bnb.nn.Paramsabit(_a	,    requires_grad=_a	,    **_a					).to(_a					)

            snake_case_				:  Union[str, Any]       =			new_value
            if fpaa_statistics is not None:
                setattr(module.weight	,    '''SCB'''	,    fpaa_statistics.to(_a					)					)

    else:
        if value is None:
            snake_case_				:  Dict       =			old_value.to(_a					)
        elif isinstance(_a	,    torch.Tensor					):
            snake_case_				:  Dict       =			value.to(_a					)
        else:
            snake_case_				:  str       =			torch.tensor(_a	,    device=_a					)

        if is_buffer:
            snake_case_				:  Optional[int]       =			new_value
        else:
            snake_case_				:  Optional[Any]       =			nn.Parameter(_a	,    requires_grad=old_value.requires_grad					)
            snake_case_				:  List[Any]       =			new_value




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

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

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

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

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

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

    return filtered_module_names





