

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

_lowerCAmelCase         =						logging.get_logger(__name__)




def 						UpperCamelCase	(			a		,						a		,						a		,						a=None		,						a=None			)				->     Tuple:






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

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

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

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

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

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

          if is_buffer:
               __magic_name__						       =    new_value
          else:
               __magic_name__						       =    nn.Parameter(a		,						requires_grad=old_value.requires_grad			)
               __magic_name__						       =    new_value




def 						UpperCamelCase	(			a		,						a=None		,						a=None		,						a=None		,						a=False			)				->     List[str]:






     '''simple docstring'''
     for name, module in model.named_children():
          if current_key_name is None:
               __magic_name__						       =    []
          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			):
                              __magic_name__       ,  __magic_name__						       =    module.weight.shape
                         else:
                              __magic_name__						       =    module.in_features
                              __magic_name__						       =    module.out_features

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






     '''simple docstring'''
     __magic_name__						       =    ['''lm_head'''] if modules_to_not_convert is None else modules_to_not_convert
     __magic_name__       ,  __magic_name__						       =    _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		,						**a			)				->     Any:






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




def 						UpperCamelCase	(			a			)				->     Optional[Any]:






     '''simple docstring'''
     __magic_name__						       =    deepcopy(a			)  # this has 0 cost since it is done inside `init_empty_weights` context manager`
     tied_model.tie_weights()

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

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

     # add last module together with tied weights
     __magic_name__						       =    set(a			) - set(a			)
     __magic_name__						       =    list(set(a			)			) + list(a			)

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

     return filtered_module_names









'''simple docstring'''




from __future__ import annotations

from fractions import Fraction




def 						UpperCamelCase	(			a		,						a			)				->     bool:






     '''simple docstring'''
     return (
         num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den
     )




def 						UpperCamelCase	(			a			)				->     list[str]:






     '''simple docstring'''
     __magic_name__						       =    []
     __magic_name__						       =    11
     __magic_name__						       =    int('''1''' + '''0''' * digit_len			)
     for num in range(a		,						a			):
          while den <= 99:
               if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
                    if is_digit_cancelling(a		,						a			):
                         solutions.append(F'''{num}/{den}'''			)
               den += 1
          num += 1
          __magic_name__						       =    10
     return solutions




def 						UpperCamelCase	(			a = 2			)				->     int:






     '''simple docstring'''
     __magic_name__						       =    1.0
     for fraction in fraction_list(a			):
          __magic_name__						       =    Fraction(a			)
          result *= frac.denominator / frac.numerator
     return int(a			)


if __name__ == "__main__":
      print(solution())






