


import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          FalconForCausalLM,
          FalconForQuestionAnswering,
          FalconForSequenceClassification,
          FalconForTokenClassification,
          FalconModel,
      )





class   SCREAMING_SNAKE_CASE_			:




       '''simple docstring'''



       def __init__(						self				:				Optional[Any]		,				__a				:				Any		,				__a				:				List[Any]=3		,				__a				:				Any=7		,				__a				:				Optional[Any]=True		,				__a				:				Union[str, Any]=True		,				__a				:				int=False		,				__a				:				int=True		,				__a				:				Tuple=99		,				__a				:				Any=32		,				__a				:				Tuple=5		,				__a				:				Union[str, Any]=4		,				__a				:				Tuple=37		,				__a				:				str="gelu"		,				__a				:				Optional[Any]=0.1		,				__a				:				List[str]=0.1		,				__a				:				List[str]=512		,				__a				:				str=16		,				__a				:				List[str]=2		,				__a				:				Dict=0.02		,				__a				:				Optional[Any]=3		,				__a				:				Any=4		,				__a				:				str=None		,				)					->int:
          lowerCamelCase_						:   List[str]		= parent
          lowerCamelCase_						:   Any		= batch_size
          lowerCamelCase_						:   Dict		= seq_length
          lowerCamelCase_						:   str		= is_training
          lowerCamelCase_						:   Optional[int]		= use_input_mask
          lowerCamelCase_						:   List[Any]		= use_token_type_ids
          lowerCamelCase_						:   Optional[int]		= use_labels
          lowerCamelCase_						:   Union[str, Any]		= vocab_size
          lowerCamelCase_						:   List[str]		= hidden_size
          lowerCamelCase_						:   List[str]		= num_hidden_layers
          lowerCamelCase_						:   List[Any]		= num_attention_heads
          lowerCamelCase_						:   Dict		= intermediate_size
          lowerCamelCase_						:   Any		= hidden_act
          lowerCamelCase_						:   Dict		= hidden_dropout_prob
          lowerCamelCase_						:   int		= attention_probs_dropout_prob
          lowerCamelCase_						:   int		= max_position_embeddings
          lowerCamelCase_						:   Any		= type_vocab_size
          lowerCamelCase_						:   Optional[int]		= type_sequence_label_size
          lowerCamelCase_						:   List[Any]		= initializer_range
          lowerCamelCase_						:   Tuple		= num_labels
          lowerCamelCase_						:   str		= num_choices
          lowerCamelCase_						:   Optional[int]		= scope



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->Optional[Any]:
          lowerCamelCase_						:   Tuple		= ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size							)

          lowerCamelCase_						:   List[str]		= None
          if self.use_input_mask:
             lowerCamelCase_						:   int		= random_attention_mask([self.batch_size, self.seq_length]							)

          lowerCamelCase_						:   List[str]		= None

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

          lowerCamelCase_						:   int		= self.get_config()

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



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Dict:
          return FalconConfig(
              vocab_size=self.vocab_size		,				hidden_size=self.hidden_size		,				num_hidden_layers=self.num_hidden_layers		,				num_attention_heads=self.num_attention_heads		,				intermediate_size=self.intermediate_size		,				hidden_act=self.hidden_act		,				hidden_dropout_prob=self.hidden_dropout_prob		,				attention_probs_dropout_prob=self.attention_probs_dropout_prob		,				max_position_embeddings=self.max_position_embeddings		,				type_vocab_size=self.type_vocab_size		,				is_decoder=__a		,				initializer_range=self.initializer_range		,				pad_token_id=1		,				new_decoder_architecture=__a		,				)



       def 				_lowerCAmelCase							(						self				:				Optional[int]		,				__a				:				Optional[int]		,				__a				:				int		,				__a				:				str		,				__a				:				List[Any]		,				__a				:				Any		,				__a				:				Union[str, Any]		,				__a				:				Any							)					->Any:
          lowerCamelCase_						:   Union[str, Any]		= FalconModel(config=__a							)
          model.to(__a							)
          model.eval()
          lowerCamelCase_						:   Any		= model(__a		,				attention_mask=__a							)
          lowerCamelCase_						:   Union[str, Any]		= model(__a							)
          self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size)							)



       def 				_lowerCAmelCase							(						self				:				Any		,				__a				:				Tuple		,				__a				:				str		,				__a				:				int		,				__a				:				int		,				__a				:				int		,				__a				:				Union[str, Any]		,				__a				:				Any		,				__a				:				int		,				__a				:				Union[str, Any]		,				)					->int:
          lowerCamelCase_						:   Union[str, Any]		= True
          lowerCamelCase_						:   Optional[int]		= FalconModel(__a							)
          model.to(__a							)
          model.eval()
          lowerCamelCase_						:   str		= model(
              __a		,				attention_mask=__a		,				encoder_hidden_states=__a		,				encoder_attention_mask=__a		,				)
          lowerCamelCase_						:   Optional[int]		= model(
              __a		,				attention_mask=__a		,				encoder_hidden_states=__a		,				)
          lowerCamelCase_						:   List[str]		= model(__a		,				attention_mask=__a							)
          self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size)							)



       def 				_lowerCAmelCase							(						self				:				Tuple		,				__a				:				Optional[Any]		,				__a				:				str		,				__a				:				Optional[int]		,				__a				:				Optional[int]		,				__a				:				List[str]		,				__a				:				Union[str, Any]		,				__a				:				Dict		,				__a				:				Optional[Any]		,				__a				:				List[str]		,				)					->Optional[Any]:
          lowerCamelCase_						:   Tuple		= FalconForCausalLM(config=__a							)
          model.to(__a							)
          model.eval()
          lowerCamelCase_						:   str		= model(__a		,				attention_mask=__a		,				labels=__a							)
          self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.vocab_size)							)



       def 				_lowerCAmelCase							(						self				:				Optional[int]		,				__a				:				Dict		,				__a				:				Tuple		,				__a				:				Dict		,				__a				:				Dict		,				__a				:				Tuple		,				__a				:				List[Any]		,				__a				:				Optional[Any]		,				__a				:				Dict		,				__a				:				Tuple		,				)					->List[Any]:
          lowerCamelCase_						:   Union[str, Any]		= True
          lowerCamelCase_						:   List[Any]		= True
          lowerCamelCase_						:   Dict		= FalconForCausalLM(config=__a							)
          model.to(__a							)
          model.eval()

          # first forward pass
          lowerCamelCase_						:   List[Any]		= model(
              __a		,				attention_mask=__a		,				encoder_hidden_states=__a		,				encoder_attention_mask=__a		,				use_cache=__a		,				)
          lowerCamelCase_						:   Optional[int]		= outputs.past_key_values

          # create hypothetical multiple next token and extent to next_input_ids
          lowerCamelCase_						:   Any		= ids_tensor((self.batch_size, 3)		,				config.vocab_size							)
          lowerCamelCase_						:   Dict		= ids_tensor((self.batch_size, 3)		,				vocab_size=2							)

          # append to next input_ids and
          lowerCamelCase_						:   List[Any]		= torch.cat([input_ids, next_tokens]		,				dim=-1							)
          lowerCamelCase_						:   Union[str, Any]		= torch.cat([input_mask, next_mask]		,				dim=-1							)

          lowerCamelCase_						:   Any		= model(
              __a		,				attention_mask=__a		,				encoder_hidden_states=__a		,				encoder_attention_mask=__a		,				output_hidden_states=__a		,				)["""hidden_states"""][0]
          lowerCamelCase_						:   Optional[Any]		= model(
              __a		,				attention_mask=__a		,				encoder_hidden_states=__a		,				encoder_attention_mask=__a		,				past_key_values=__a		,				output_hidden_states=__a		,				)["""hidden_states"""][0]

          # select random slice
          lowerCamelCase_						:   Tuple		= ids_tensor((1,)		,				output_from_past.shape[-1]							).item()
          lowerCamelCase_						:   str		= output_from_no_past[:, -3:, random_slice_idx].detach()
          lowerCamelCase_						:   str		= output_from_past[:, :, random_slice_idx].detach()

          self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]							)

          # test that outputs are equal for slice
          self.parent.assertTrue(torch.allclose(__a		,				__a		,				atol=1e-3							)							)




       def 				_lowerCAmelCase							(						self				:				Optional[int]							)					->Optional[Any]:
          lowerCamelCase_						:   List[str]		= self.prepare_config_and_inputs()
          (
              (
              lowerCamelCase_
          ),	(
              lowerCamelCase_
          ),	(
              lowerCamelCase_
          ),	(
              lowerCamelCase_
          ),	(
              lowerCamelCase_
          ),	(
              lowerCamelCase_
          ),	(
              lowerCamelCase_
          ),	
          )						:   Any		= config_and_inputs
          lowerCamelCase_						:   List[str]		= {"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_torch
class   SCREAMING_SNAKE_CASE_			(a__						,       a__						,       a__						,       unittest.TestCase						):




       '''simple docstring'''



       _a   							=							(
           (
               FalconModel,
               FalconForCausalLM,
               FalconForSequenceClassification,
               FalconForTokenClassification,
               FalconForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       _a   							=							(FalconForCausalLM,) if is_torch_available() else ()
       _a   							=							(
           {
               "feature-extraction": FalconModel,
               "text-classification": FalconForSequenceClassification,
               "text-generation": FalconForCausalLM,
               "question-answering": FalconForQuestionAnswering,
               "token-classification": FalconForTokenClassification,
               "zero-shot": FalconForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       _a   							=							False
       _a   							=							False



       def 				_lowerCAmelCase							(						self				:				Dict							)					->Optional[Any]:
          lowerCamelCase_						:   Dict		= FalconModelTester(self							)
          lowerCamelCase_						:   Optional[int]		= ConfigTester(self		,				config_class=__a		,				hidden_size=37							)



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Dict:
          self.config_tester.run_common_tests()



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->int:
          lowerCamelCase_						:   Union[str, Any]		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__a							)



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->Optional[int]:
          lowerCamelCase_,	*lowerCamelCase_						:   Optional[Any]		= self.model_tester.prepare_config_and_inputs()
          for alibi in [True, False]:
             lowerCamelCase_						:   List[str]		= alibi
             self.model_tester.create_and_check_model(__a		,				*__a							)



       def 				_lowerCAmelCase							(						self				:				Dict							)					->List[Any]:
          lowerCamelCase_,	lowerCamelCase_						:   Optional[Any]		= self.model_tester.prepare_config_and_inputs_for_common()
          lowerCamelCase_						:   str		= 3
          lowerCamelCase_						:   Union[str, Any]		= input_dict["""input_ids"""]
          lowerCamelCase_						:   Optional[Any]		= input_ids.ne(1							).to(__a							)
          lowerCamelCase_						:   Optional[int]		= ids_tensor([self.model_tester.batch_size]		,				self.model_tester.type_sequence_label_size							)
          lowerCamelCase_						:   List[str]		= FalconForSequenceClassification(__a							)
          model.to(__a							)
          model.eval()
          lowerCamelCase_						:   int		= model(__a		,				attention_mask=__a		,				labels=__a							)
          self.assertEqual(result.logits.shape		,				(self.model_tester.batch_size, self.model_tester.num_labels)							)



       def 				_lowerCAmelCase							(						self				:				List[str]							)					->Tuple:
          lowerCamelCase_,	lowerCamelCase_						:   Any		= self.model_tester.prepare_config_and_inputs_for_common()
          lowerCamelCase_						:   Tuple		= 3
          lowerCamelCase_						:   str		= """single_label_classification"""
          lowerCamelCase_						:   str		= input_dict["""input_ids"""]
          lowerCamelCase_						:   int		= input_ids.ne(1							).to(__a							)
          lowerCamelCase_						:   Union[str, Any]		= ids_tensor([self.model_tester.batch_size]		,				self.model_tester.type_sequence_label_size							)
          lowerCamelCase_						:   List[Any]		= FalconForSequenceClassification(__a							)
          model.to(__a							)
          model.eval()
          lowerCamelCase_						:   Union[str, Any]		= model(__a		,				attention_mask=__a		,				labels=__a							)
          self.assertEqual(result.logits.shape		,				(self.model_tester.batch_size, self.model_tester.num_labels)							)



       def 				_lowerCAmelCase							(						self				:				List[str]							)					->List[str]:
          lowerCamelCase_,	lowerCamelCase_						:   Union[str, Any]		= self.model_tester.prepare_config_and_inputs_for_common()
          lowerCamelCase_						:   Optional[Any]		= input_dict["""input_ids"""]
          lowerCamelCase_						:   Optional[int]		= FalconForCausalLM(__a							)
          model.to(__a							)
          model.eval()
          lowerCamelCase_						:   List[str]		= model(__a		,				use_cache=__a							)
          lowerCamelCase_						:   Tuple		= input_ids.shape[0]
          lowerCamelCase_						:   List[str]		= model._convert_to_rw_cache(result.past_key_values							)
          lowerCamelCase_						:   Optional[Any]		= model._convert_cache_to_standard_format(__a		,				__a							)
          for layer in range(len(__a							)							):
             for tensor_idx in range(2							):
                self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3							)
                self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4							)
                self.assertTrue(
                    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]							)							)



       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->Dict:
          lowerCamelCase_,	lowerCamelCase_						:   List[str]		= self.model_tester.prepare_config_and_inputs_for_common()
          lowerCamelCase_						:   List[Any]		= 3
          lowerCamelCase_						:   Any		= """multi_label_classification"""
          lowerCamelCase_						:   Any		= input_dict["""input_ids"""]
          lowerCamelCase_						:   Any		= input_ids.ne(1							).to(__a							)
          lowerCamelCase_						:   int		= ids_tensor(
              [self.model_tester.batch_size, config.num_labels]		,				self.model_tester.type_sequence_label_size							).to(torch.float							)
          lowerCamelCase_						:   Dict		= FalconForSequenceClassification(__a							)
          model.to(__a							)
          model.eval()
          lowerCamelCase_						:   List[str]		= model(__a		,				attention_mask=__a		,				labels=__a							)
          self.assertEqual(result.logits.shape		,				(self.model_tester.batch_size, self.model_tester.num_labels)							)




       def 				_lowerCAmelCase							(						self				:				Optional[int]							)					->str:
          # Falcon can have different numbers of KV-heads than the number of query heads, so we need
          # to override this test to use the right head counts.
          for model_class in self.all_generative_model_classes:
             lowerCamelCase_,	lowerCamelCase_						:   int		= self.model_tester.prepare_config_and_inputs_for_common()

             # If it doesn't support cache, pass the test
             if not hasattr(__a		,				"""use_cache"""							):
                return

             lowerCamelCase_						:   Dict		= model_class(__a							).to(__a							)
             if "use_cache" not in inputs:
                lowerCamelCase_						:   Optional[int]		= True
             lowerCamelCase_						:   str		= model(**__a							)

             # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
             if "past_key_values" not in outputs:
                return

             lowerCamelCase_						:   List[str]		= (
                 getattr(__a		,				"""decoder_layers"""		,				__a							)
                 or getattr(__a		,				"""num_decoder_layers"""		,				__a							)
                 or config.num_hidden_layers
             )
             lowerCamelCase_						:   List[Any]		= getattr(__a		,				"""num_kv_heads"""		,				config.num_attention_heads							)
             lowerCamelCase_						:   List[str]		= getattr(__a		,				"""d_model"""		,				config.hidden_size							)
             lowerCamelCase_						:   str		= embed_dim // num_attention_heads

             lowerCamelCase_						:   List[Any]		= outputs["""past_key_values"""]
             self.assertEqual(len(__a							)		,				__a							)

             lowerCamelCase_,	lowerCamelCase_						:   Optional[Any]		= inputs["""input_ids"""].shape
             for i in range(__a							):
                if config.new_decoder_architecture:
                   lowerCamelCase_						:   Tuple		= config.num_attention_heads
                elif config.multi_query:
                   lowerCamelCase_						:   Optional[Any]		= 1
                self.assertEqual(len(past_kv[0]							)		,				2							)  # K V for the decoder = 2
                self.assertEqual(
                    past_kv[i][0].shape		,				(batch_size, num_attention_heads, seq_length, per_head_embed_dim)							)
                self.assertEqual(
                    past_kv[i][1].shape		,				(batch_size, num_attention_heads, seq_length, per_head_embed_dim)							)







@require_torch
class   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       @slow
       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->Any:
          lowerCamelCase_						:   List[str]		= AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""							)
          lowerCamelCase_						:   Optional[int]		= FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""							)
          model.eval()
          model.to(__a							)
          lowerCamelCase_						:   Tuple		= tokenizer("""My favorite food is"""		,				return_tensors="""pt"""							).to(__a							)

          lowerCamelCase_						:   Tuple		= (
              """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
          )

          lowerCamelCase_						:   int		= model.generate(**__a		,				do_sample=__a		,				max_new_tokens=19							)
          lowerCamelCase_						:   Any		= tokenizer.batch_decode(__a							)[0]

          self.assertEqual(__a		,				__a							)



       @slow
       def 				_lowerCAmelCase							(						self				:				Dict							)					->List[Any]:
          # The big models are way too big for the CI, so we use tiny random models that resemble their
          # architectures but with much smaller and fewer layers
          for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
             lowerCamelCase_						:   Dict		= AutoTokenizer.from_pretrained(__a							)
             lowerCamelCase_						:   Optional[Any]		= FalconForCausalLM.from_pretrained(__a							)
             model.eval()
             model.to(__a							)
             lowerCamelCase_						:   str		= tokenizer("""My favorite food is"""		,				return_tensors="""pt"""							).to(__a							)

             # We just test that these run without errors - the models are randomly initialized
             # and so the actual text outputs will be garbage
             model.generate(**__a		,				do_sample=__a		,				max_new_tokens=4							)
             model.generate(**__a		,				do_sample=__a		,				max_new_tokens=4							)
             model.generate(**__a		,				num_beams=2		,				max_new_tokens=4							)




       @slow
       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->Tuple:
          # The big models are way too big for the CI, so we use tiny random models that resemble their
          # architectures but with much smaller and fewer layers
          with torch.no_grad():
             for repo in [
                 "Rocketknight1/falcon-rw-1b",
                 "Rocketknight1/tiny-random-falcon-7b",
                 "Rocketknight1/tiny-random-falcon-40b",
             ]:
                lowerCamelCase_						:   str		= AutoTokenizer.from_pretrained(__a							)
                lowerCamelCase_						:   Tuple		= FalconForCausalLM.from_pretrained(__a							)
                model.eval()
                model.to(device=__a							)
                lowerCamelCase_						:   Dict		= tokenizer("""My favorite food is"""		,				return_tensors="""pt"""							).to(__a							)

                # Test results are the same with and without cache
                lowerCamelCase_						:   int		= model.generate(**__a		,				do_sample=__a		,				max_new_tokens=20		,				use_cache=__a							)
                lowerCamelCase_						:   str		= model.generate(**__a		,				do_sample=__a		,				max_new_tokens=20		,				use_cache=__a							)
                self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0							)









# 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


snake_case__				:	int					       =					logging.get_logger(__name__)





@dataclass
class   SCREAMING_SNAKE_CASE_			:




       '''simple docstring'''



       def __init__(						self				:				int		,				__a				:				Optional[Any]=False		,				__a				:				Tuple=False		,				__a				:				Tuple=6.0		,				__a				:				str=None		,				__a				:				List[str]=False		,				__a				:				str=False		,				__a				:				Optional[Any]=None		,				__a				:				Dict="fp4"		,				__a				:				Optional[int]=False		,				**__a				:				Dict		,				)					->str:
          lowerCamelCase_						:   str		= load_in_abit
          lowerCamelCase_						:   Union[str, Any]		= load_in_abit
          lowerCamelCase_						:   Tuple		= llm_inta_threshold
          lowerCamelCase_						:   int		= llm_inta_skip_modules
          lowerCamelCase_						:   List[str]		= llm_inta_enable_fpaa_cpu_offload
          lowerCamelCase_						:   Optional[Any]		= llm_inta_has_fpaa_weight
          lowerCamelCase_						:   Dict		= bnb_abit_quant_type
          lowerCamelCase_						:   int		= bnb_abit_use_double_quant

          if bnb_abit_compute_dtype is None:
             lowerCamelCase_						:   int		= torch.floataa
          elif isinstance(__a		,				__a							):
             lowerCamelCase_						:   Tuple		= getattr(__a		,				__a							)
          elif isinstance(__a		,				torch.dtype							):
             lowerCamelCase_						:   List[str]		= bnb_abit_compute_dtype
          else:
             raise ValueError("""bnb_4bit_compute_dtype must be a string or a torch.dtype"""							)

          self.post_init()



       def 				_lowerCAmelCase							(						self				:				Any							)					->Union[str, Any]:
          if not isinstance(self.llm_inta_threshold		,				__a							):
             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		,				__a							):
             raise ValueError("""llm_int8_skip_modules must be a list of strings"""							)
          if not isinstance(self.llm_inta_enable_fpaa_cpu_offload		,				__a							):
             raise ValueError("""llm_int8_enable_fp32_cpu_offload must be a boolean"""							)

          if not isinstance(self.llm_inta_has_fpaa_weight		,				__a							):
             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		,				__a							):
             raise ValueError("""bnb_4bit_quant_type must be a string"""							)

          if not isinstance(self.bnb_abit_use_double_quant		,				__a							):
             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 				_lowerCAmelCase							(						self				:				Optional[int]							)					->int:
          return self.load_in_abit or self.load_in_abit



       def 				_lowerCAmelCase							(						self				:				List[str]							)					->List[Any]:
          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 				_lowerCAmelCase							(						cls				:				int		,				__a				:				Dict		,				__a				:				str		,				**__a				:				Any							)					->Optional[Any]:
          lowerCamelCase_						:   Optional[Any]		= cls(**__a							)

          lowerCamelCase_						:   List[Any]		= []
          for key, value in kwargs.items():
             if hasattr(__a		,				__a							):
                setattr(__a		,				__a		,				__a							)
                to_remove.append(__a							)
          for key in to_remove:
             kwargs.pop(__a		,				__a							)

          if return_unused_kwargs:
             return config, kwargs
          else:
             return config



       def 				_lowerCAmelCase							(						self				:				Optional[Any]		,				__a				:				Union[str, os.PathLike]							)					->Any:
          with open(__a		,				"""w"""		,				encoding="""utf-8"""							) as writer:
             lowerCamelCase_						:   List[Any]		= self.to_dict()
             lowerCamelCase_						:   Dict		= json.dumps(__a		,				indent=2		,				sort_keys=__a							) + """\n"""

             writer.write(__a							)



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Dict[str, Any]:
          lowerCamelCase_						:   Dict		= copy.deepcopy(self.__dict__							)
          lowerCamelCase_						:   Tuple		= str(output["""bnb_4bit_compute_dtype"""]							).split("""."""							)[1]

          return output



       def __repr__(						self				:				List[str]							)					->Any:
          return F'''{self.__class__.__name__} {self.to_json_string()}'''



       def 				_lowerCAmelCase							(						self				:				Optional[int]		,				__a				:				bool = True							)					->str:
          if use_diff is True:
             lowerCamelCase_						:   Dict		= self.to_diff_dict()
          else:
             lowerCamelCase_						:   Any		= self.to_dict()
          return json.dumps(__a		,				indent=2		,				sort_keys=__a							) + "\n"




       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->Dict[str, Any]:
          lowerCamelCase_						:   Any		= self.to_dict()

          # get the default config dict
          lowerCamelCase_						:   List[str]		= BitsAndBytesConfig().to_dict()

          lowerCamelCase_						:   str		= {}

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

          return serializable_config_dict





