

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

UpperCAmelCase					 =	logging.get_logger(__name__)

def 					__UpperCamelCase				(						lowercase__    :	Dict,       lowercase__    :	Dict,       lowercase__    :	str,       lowercase__    :	List[str]=None,       lowercase__    :	List[str]=None			):






     '''simple docstring'''





     if "." in tensor_name:
          __lowercase      		=tensor_name.split('.'			)
          for split in splits[:-1]:
               __lowercase      		=getattr(lowercase__,       lowercase__			)
               if new_module is None:
                    raise ValueError(F'''{module} has no attribute {split}.'''			)
               __lowercase      		=new_module
          __lowercase      		=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}.'''			)
     __lowercase      		=tensor_name in module._buffers
     __lowercase      		=getattr(lowercase__,       lowercase__			)

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

     __lowercase      		=False
     __lowercase      		=False
     if is_buffer or not is_bitsandbytes_available():
          __lowercase      		=False
          __lowercase      		=False
     else:
          __lowercase      		=hasattr(bnb.nn,       'Params4bit'			) and isinstance(module._parameters[tensor_name],       bnb.nn.Paramsabit			)
          __lowercase      		=isinstance(module._parameters[tensor_name],       bnb.nn.IntaParams			)

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

               __lowercase      		=old_value.__dict__
               if is_abit:
                    __lowercase      		=bnb.nn.IntaParams(lowercase__,       requires_grad=lowercase__,       **lowercase__			).to(lowercase__			)
               elif is_abit:
                    __lowercase      		=bnb.nn.Paramsabit(lowercase__,       requires_grad=lowercase__,       **lowercase__			).to(lowercase__			)

               __lowercase      		=new_value
               if fpaa_statistics is not None:
                    setattr(module.weight,       'SCB',       fpaa_statistics.to(lowercase__			)			)

     else:
          if value is None:
               __lowercase      		=old_value.to(lowercase__			)
          elif isinstance(lowercase__,       torch.Tensor			):
               __lowercase      		=value.to(lowercase__			)
          else:
               __lowercase      		=torch.tensor(lowercase__,       device=lowercase__			)

          if is_buffer:
               __lowercase      		=new_value
          else:
               __lowercase      		=nn.Parameter(lowercase__,       requires_grad=old_value.requires_grad			)
               __lowercase      		=new_value

def 					__UpperCamelCase				(						lowercase__    :	Optional[Any],       lowercase__    :	List[str]=None,       lowercase__    :	List[Any]=None,       lowercase__    :	Union[str, Any]=None,       lowercase__    :	Optional[int]=False			):






     '''simple docstring'''





     for name, module in model.named_children():
          if current_key_name is None:
               __lowercase      		=[]
          current_key_name.append(lowercase__			)

          if (isinstance(lowercase__,       nn.Linear			) or isinstance(lowercase__,       lowercase__			)) 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(lowercase__			) for key in modules_to_not_convert			):
                    with init_empty_weights():
                         if isinstance(lowercase__,       lowercase__			):
                              __lowercase		,  __lowercase      		=module.weight.shape
                         else:
                              __lowercase      		=module.in_features
                              __lowercase      		=module.out_features

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

def 					__UpperCamelCase				(						lowercase__    :	Tuple,       lowercase__    :	Union[str, Any]=None,       lowercase__    :	Optional[int]=None,       lowercase__    :	Optional[int]=None			):






     '''simple docstring'''





     __lowercase      		=['lm_head'] if modules_to_not_convert is None else modules_to_not_convert
     __lowercase		,  __lowercase      		=_replace_with_bnb_linear(
         lowercase__,       lowercase__,       lowercase__,       lowercase__			)

     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				(						*lowercase__    :	Optional[Any],       **lowercase__    :	str			):






     '''simple docstring'''





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

def 					__UpperCamelCase				(						*lowercase__    :	Union[str, Any],       **lowercase__    :	List[str]			):






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







def 					__UpperCamelCase				(						lowercase__    :	Dict			):






     '''simple docstring'''





     __lowercase      		=deepcopy(lowercase__			)  # this has 0 cost since it is done inside `init_empty_weights` context manager`
     tied_model.tie_weights()

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

     # Check if it is a base model
     __lowercase      		=not hasattr(lowercase__,       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
     __lowercase      		=list(model.named_children()			)
     __lowercase      		=[list_modules[-1][0]]

     # add last module together with tied weights
     __lowercase      		=set(lowercase__			) - set(lowercase__			)
     __lowercase      		=list(set(lowercase__			)			) + list(lowercase__			)

     # remove ".weight" from the keys
     __lowercase      		=['.weight', '.bias']
     __lowercase      		=[]
     for name in list_untouched:
          for name_to_remove in names_to_remove:
               if name_to_remove in name:
                    __lowercase      		=name.replace(lowercase__,       ''			)
          filtered_module_names.append(lowercase__			)

     return filtered_module_names





'''simple docstring'''

import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


UpperCAmelCase					 =	get_tests_dir('''fixtures''')


class 	lowerCAmelCase  (   unittest.TestCase ):







       def       snake_case (				self      :			str   ):







            """simple docstring"""







            __lowercase      		=mock.Mock()
            __lowercase      		=500
            __lowercase      		={}
            __lowercase      		=HTTPError
            __lowercase      		={}

            # Download this model to make sure it's in the cache.
            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'   )
            # Under the mock environment we get a 500 error when trying to reach the model.
            with mock.patch('requests.Session.request'    ,   return_value=__lowercase   ) as mock_head:
                 __lowercase      		=WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'   )
                 # This check we did call the fake head request
                 mock_head.assert_called()







       def       snake_case (				self      :			Dict   ):







            """simple docstring"""







            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained(
                'https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json'   )






@is_staging_test
class 	lowerCAmelCase  (   unittest.TestCase ):







       @classmethod
       def       snake_case (				cls      :			str   ):







            """simple docstring"""







            __lowercase      		=TOKEN
            HfFolder.save_token(__lowercase   )







       @classmethod
       def       snake_case (				cls      :			str   ):







            """simple docstring"""







            try:
                 delete_repo(token=cls._token    ,   repo_id='test-feature-extractor'   )
            except HTTPError:
                 pass

            try:
                 delete_repo(token=cls._token    ,   repo_id='valid_org/test-feature-extractor-org'   )
            except HTTPError:
                 pass

            try:
                 delete_repo(token=cls._token    ,   repo_id='test-dynamic-feature-extractor'   )
            except HTTPError:
                 pass







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained(__lowercase   )
            feature_extractor.push_to_hub('test-feature-extractor'    ,   use_auth_token=self._token   )

            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained(f'''{USER}/test-feature-extractor'''   )
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(__lowercase    ,   getattr(__lowercase    ,   __lowercase   )   )

            # Reset repo
            delete_repo(token=self._token    ,   repo_id='test-feature-extractor'   )

            # Push to hub via save_pretrained
            with tempfile.TemporaryDirectory() as tmp_dir:
                 feature_extractor.save_pretrained(
                     __lowercase    ,   repo_id='test-feature-extractor'    ,   push_to_hub=__lowercase    ,   use_auth_token=self._token   )

            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained(f'''{USER}/test-feature-extractor'''   )
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(__lowercase    ,   getattr(__lowercase    ,   __lowercase   )   )







       def       snake_case (				self      :			List[Any]   ):







            """simple docstring"""







            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained(__lowercase   )
            feature_extractor.push_to_hub('valid_org/test-feature-extractor'    ,   use_auth_token=self._token   )

            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor'   )
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(__lowercase    ,   getattr(__lowercase    ,   __lowercase   )   )

            # Reset repo
            delete_repo(token=self._token    ,   repo_id='valid_org/test-feature-extractor'   )

            # Push to hub via save_pretrained
            with tempfile.TemporaryDirectory() as tmp_dir:
                 feature_extractor.save_pretrained(
                     __lowercase    ,   repo_id='valid_org/test-feature-extractor-org'    ,   push_to_hub=__lowercase    ,   use_auth_token=self._token   )

            __lowercase      		=WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org'   )
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(__lowercase    ,   getattr(__lowercase    ,   __lowercase   )   )







       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







            CustomFeatureExtractor.register_for_auto_class()
            __lowercase      		=CustomFeatureExtractor.from_pretrained(__lowercase   )

            feature_extractor.push_to_hub('test-dynamic-feature-extractor'    ,   use_auth_token=self._token   )

            # This has added the proper auto_map field to the config
            self.assertDictEqual(
                feature_extractor.auto_map    ,   {'AutoFeatureExtractor': 'custom_feature_extraction.CustomFeatureExtractor'}    ,   )

            __lowercase      		=AutoFeatureExtractor.from_pretrained(
                f'''{USER}/test-dynamic-feature-extractor'''    ,   trust_remote_code=__lowercase   )
            # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
            self.assertEqual(new_feature_extractor.__class__.__name__    ,   'CustomFeatureExtractor'   )


