

from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import (
        TFFunnelBaseModel,
        TFFunnelForMaskedLM,
        TFFunnelForMultipleChoice,
        TFFunnelForPreTraining,
        TFFunnelForQuestionAnswering,
        TFFunnelForSequenceClassification,
        TFFunnelForTokenClassification,
        TFFunnelModel,
    )
class  _A		:

  def __init__(				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE=13 ,					_SCREAMING_SNAKE_CASE=7 ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=99 ,					_SCREAMING_SNAKE_CASE=[1, 1, 2] ,					_SCREAMING_SNAKE_CASE=1 ,					_SCREAMING_SNAKE_CASE=32 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=8 ,					_SCREAMING_SNAKE_CASE=37 ,					_SCREAMING_SNAKE_CASE="gelu_new" ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=0.0 ,					_SCREAMING_SNAKE_CASE=512 ,					_SCREAMING_SNAKE_CASE=3 ,					_SCREAMING_SNAKE_CASE=0.02 ,					_SCREAMING_SNAKE_CASE=3 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE=False ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     parent
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     batch_size
        SCREAMING_SNAKE_CASE_				:  Any           =     seq_length
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     is_training
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     use_input_mask
        SCREAMING_SNAKE_CASE_				:  Dict           =     use_token_type_ids
        SCREAMING_SNAKE_CASE_				:  int           =     use_labels
        SCREAMING_SNAKE_CASE_				:  Any           =     vocab_size
        SCREAMING_SNAKE_CASE_				:  Dict           =     block_sizes
        SCREAMING_SNAKE_CASE_				:  Tuple           =     num_decoder_layers
        SCREAMING_SNAKE_CASE_				:  Any           =     d_model
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     n_head
        SCREAMING_SNAKE_CASE_				:  int           =     d_head
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     d_inner
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     hidden_act
        SCREAMING_SNAKE_CASE_				:  Dict           =     hidden_dropout
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     attention_dropout
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     activation_dropout
        SCREAMING_SNAKE_CASE_				:  Any           =     max_position_embeddings
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     type_vocab_size
        SCREAMING_SNAKE_CASE_				:  Dict           =     2
        SCREAMING_SNAKE_CASE_				:  List[str]           =     num_labels
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     num_choices
        SCREAMING_SNAKE_CASE_				:  List[str]           =     scope
        SCREAMING_SNAKE_CASE_				:  Any           =     initializer_std

        # Used in the tests to check the size of the first attention layer
        SCREAMING_SNAKE_CASE_				:  Any           =     n_head
        # Used in the tests to check the size of the first hidden state
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     self.d_model
        # Used in the tests to check the number of output hidden states/attentions
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     sum(self.block_sizes					) + (0 if base else self.num_decoder_layers)
        # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
        # the last hidden state of the first block (which is the first hidden state of the decoder).
        if not base:
              SCREAMING_SNAKE_CASE_				:  Any           =     self.num_hidden_layers + 2

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size					)

        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     None
        if self.use_input_mask:
              SCREAMING_SNAKE_CASE_				:  List[Any]           =     random_attention_mask([self.batch_size, self.seq_length]					)

        SCREAMING_SNAKE_CASE_				:  List[str]           =     None
        if self.use_token_type_ids:
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     ids_tensor([self.batch_size, self.seq_length] ,					self.type_vocab_size					)

        SCREAMING_SNAKE_CASE_				:  int           =     None
        SCREAMING_SNAKE_CASE_				:  Tuple           =     None
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     None
        if self.use_labels:
              SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     ids_tensor([self.batch_size] ,					self.type_sequence_label_size					)
              SCREAMING_SNAKE_CASE_				:  Any           =     ids_tensor([self.batch_size, self.seq_length] ,					self.num_labels					)
              SCREAMING_SNAKE_CASE_				:  Dict           =     ids_tensor([self.batch_size] ,					self.num_choices					)

        SCREAMING_SNAKE_CASE_				:  Dict           =     FunnelConfig(
            vocab_size=self.vocab_size ,					block_sizes=self.block_sizes ,					num_decoder_layers=self.num_decoder_layers ,					d_model=self.d_model ,					n_head=self.n_head ,					d_head=self.d_head ,					d_inner=self.d_inner ,					hidden_act=self.hidden_act ,					hidden_dropout=self.hidden_dropout ,					attention_dropout=self.attention_dropout ,					activation_dropout=self.activation_dropout ,					max_position_embeddings=self.max_position_embeddings ,					type_vocab_size=self.type_vocab_size ,					initializer_std=self.initializer_std ,					)

        return (
            config,
            input_ids,
            token_type_ids,
            input_mask,
            sequence_labels,
            token_labels,
            choice_labels,
        )

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Any           =     TFFunnelModel(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     model(_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     [input_ids, input_mask]
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     model(_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  List[str]           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.d_model)					)

        SCREAMING_SNAKE_CASE_				:  Any           =     False
        SCREAMING_SNAKE_CASE_				:  Any           =     TFFunnelModel(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.d_model)					)

        SCREAMING_SNAKE_CASE_				:  str           =     False
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     TFFunnelModel(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.d_model)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     TFFunnelBaseModel(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     model(_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  str           =     [input_ids, input_mask]
        SCREAMING_SNAKE_CASE_				:  Dict           =     model(_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  int           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, 2, self.d_model)					)

        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     False
        SCREAMING_SNAKE_CASE_				:  Tuple           =     TFFunnelBaseModel(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, 3, self.d_model)					)

        SCREAMING_SNAKE_CASE_				:  Dict           =     False
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     TFFunnelBaseModel(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Any           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, 2, self.d_model)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     TFFunnelForPreTraining(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  str           =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        SCREAMING_SNAKE_CASE_				:  Tuple           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Any           =     TFFunnelForMaskedLM(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  str           =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        SCREAMING_SNAKE_CASE_				:  str           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     self.num_labels
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     TFFunnelForSequenceClassification(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        SCREAMING_SNAKE_CASE_				:  int           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     self.num_choices
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     TFFunnelForMultipleChoice(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE ,					1					) ,					(1, self.num_choices, 1)					)
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE ,					1					) ,					(1, self.num_choices, 1)					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE ,					1					) ,					(1, self.num_choices, 1)					)
        SCREAMING_SNAKE_CASE_				:  Any           =     {
            'input_ids': multiple_choice_inputs_ids,
            'attention_mask': multiple_choice_input_mask,
            'token_type_ids': multiple_choice_token_type_ids,
        }
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_choices)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     self.num_labels
        SCREAMING_SNAKE_CASE_				:  int           =     TFFunnelForTokenClassification(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  str           =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        SCREAMING_SNAKE_CASE_				:  int           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     TFFunnelForQuestionAnswering(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        SCREAMING_SNAKE_CASE_				:  str           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.start_logits.shape ,					(self.batch_size, self.seq_length)					)
        self.parent.assertEqual(result.end_logits.shape ,					(self.batch_size, self.seq_length)					)



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Any           =     self.prepare_config_and_inputs()
        (
            SCREAMING_SNAKE_CASE_						
        )				:  str           =     config_and_inputs
        SCREAMING_SNAKE_CASE_				:  int           =     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict

@require_tf
class  _A		(  __magic_name__   ,     __magic_name__   ,     unittest.TestCase):
  SCREAMING_SNAKE_CASE    :      str       =  (
      (
          TFFunnelModel,
          TFFunnelForMaskedLM,
          TFFunnelForPreTraining,
          TFFunnelForQuestionAnswering,
          TFFunnelForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  SCREAMING_SNAKE_CASE    :      str       =  (
      {
          '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
          '''fill-mask''': TFFunnelForMaskedLM,
          '''question-answering''': TFFunnelForQuestionAnswering,
          '''text-classification''': TFFunnelForSequenceClassification,
          '''token-classification''': TFFunnelForTokenClassification,
          '''zero-shot''': TFFunnelForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  SCREAMING_SNAKE_CASE    :      Union[str, Any]       =  False
  SCREAMING_SNAKE_CASE    :      Any       =  False

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Any           =     TFFunnelModelTester(self					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     ConfigTester(self ,					config_class=_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        self.config_tester.run_common_tests()

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE					)



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE					)

@require_tf
class  _A		(  __magic_name__   ,     unittest.TestCase):
  SCREAMING_SNAKE_CASE    :      Union[str, Any]       =  (
      (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
  )
  SCREAMING_SNAKE_CASE    :      Union[str, Any]       =  False
  SCREAMING_SNAKE_CASE    :      Dict       =  False

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Any           =     TFFunnelModelTester(self ,					base=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  int           =     ConfigTester(self ,					config_class=_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        self.config_tester.run_common_tests()

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_base_model(*_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE					)



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Tuple           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE					)






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
    import torch


lowerCAmelCase     :   Optional[int]						     =  logging.get_logger(__name__)
@dataclass
class  _A		:

  def __init__(				self ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=6.0 ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE="fp4" ,					_SCREAMING_SNAKE_CASE=False ,					**_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     load_in_abit
        SCREAMING_SNAKE_CASE_				:  Tuple           =     load_in_abit
        SCREAMING_SNAKE_CASE_				:  Dict           =     llm_inta_threshold
        SCREAMING_SNAKE_CASE_				:  Tuple           =     llm_inta_skip_modules
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     llm_inta_enable_fpaa_cpu_offload
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     llm_inta_has_fpaa_weight
        SCREAMING_SNAKE_CASE_				:  List[str]           =     bnb_abit_quant_type
        SCREAMING_SNAKE_CASE_				:  int           =     bnb_abit_use_double_quant

        if bnb_abit_compute_dtype is None:
              SCREAMING_SNAKE_CASE_				:  List[Any]           =     torch.floataa
        elif isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
              SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     getattr(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)
        elif isinstance(_SCREAMING_SNAKE_CASE ,					torch.dtype					):
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     bnb_abit_compute_dtype
        else:
              raise ValueError('bnb_4bit_compute_dtype must be a string or a torch.dtype'					)

        self.post_init()

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        if not isinstance(self.llm_inta_threshold ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_threshold must be a float'					)

        if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_skip_modules must be a list of strings'					)
        if not isinstance(self.llm_inta_enable_fpaa_cpu_offload ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_enable_fp32_cpu_offload must be a boolean'					)

        if not isinstance(self.llm_inta_has_fpaa_weight ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('llm_int8_has_fp16_weight must be a boolean'					)

        if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype ,					torch.dtype					):
              raise ValueError('bnb_4bit_compute_dtype must be torch.dtype'					)

        if not isinstance(self.bnb_abit_quant_type ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('bnb_4bit_quant_type must be a string'					)

        if not isinstance(self.bnb_abit_use_double_quant ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError('bnb_4bit_use_double_quant must be a boolean'					)

        if self.load_in_abit and not version.parse(importlib.metadata.version('bitsandbytes'					)					) >= version.parse(
            '0.39.0'					):
              raise ValueError(
                  '4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version'					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        return self.load_in_abit or self.load_in_abit

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        if self.load_in_abit:
              return "llm_int8"
        elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
              return "fp4"
        elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
              return "nf4"
        else:
              return None

  @classmethod
  def  UpperCAmelCase    (				cls ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     cls(**_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     []
        for key, value in kwargs.items():
              if hasattr(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
                    setattr(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)
                    to_remove.append(_SCREAMING_SNAKE_CASE					)
        for key in to_remove:
              kwargs.pop(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)

        if return_unused_kwargs:
              return config, kwargs
        else:
              return config

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        with open(_SCREAMING_SNAKE_CASE ,					'w' ,					encoding='utf-8'					) as writer:
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.to_dict()
              SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     json.dumps(_SCREAMING_SNAKE_CASE ,					indent=2 ,					sort_keys=_SCREAMING_SNAKE_CASE					) + '\n'

              writer.write(_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     copy.deepcopy(self.__dict__					)
        SCREAMING_SNAKE_CASE_				:  str           =     str(output['bnb_4bit_compute_dtype']					).split('.'					)[1]

        return output

  def __repr__(				self					):





        """simple docstring"""







        return f"{self.__class__.__name__} {self.to_json_string()}"

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE = True					):





        """simple docstring"""







        if use_diff is True:
              SCREAMING_SNAKE_CASE_				:  int           =     self.to_diff_dict()
        else:
              SCREAMING_SNAKE_CASE_				:  List[str]           =     self.to_dict()
        return json.dumps(_SCREAMING_SNAKE_CASE ,					indent=2 ,					sort_keys=_SCREAMING_SNAKE_CASE					) + "\n"



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     self.to_dict()

        # get the default config dict
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     BitsAndBytesConfig().to_dict()

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     {}

        # only serialize values that differ from the default config
        for key, value in config_dict.items():
              if value != default_config_dict[key]:
                    SCREAMING_SNAKE_CASE_				:  int           =     value

        return serializable_config_dict






