# 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


UpperCAmelCase_           =				logging.get_logger(__name__)




@dataclass
class 			__magic_name__	:





   """simple docstring"""




   def __init__(						self       :	Optional[Any]		,	_lowercase       :	List[str]=False		,	_lowercase       :	Optional[int]=False		,	_lowercase       :	List[str]=6.0		,	_lowercase       :	Optional[Any]=None		,	_lowercase       :	List[str]=False		,	_lowercase       :	Tuple=False		,	_lowercase       :	Tuple=None		,	_lowercase       :	Optional[int]="fp4"		,	_lowercase       :	List[str]=False		,	**_lowercase       :	str		,	):



    """simple docstring"""



    _UpperCamelCase:  List[Any]    						=			load_in_abit
    _UpperCamelCase:  Tuple    						=			load_in_abit
    _UpperCamelCase:  str    						=			llm_inta_threshold
    _UpperCamelCase:  List[Any]    						=			llm_inta_skip_modules
    _UpperCamelCase:  str    						=			llm_inta_enable_fpaa_cpu_offload
    _UpperCamelCase:  Optional[Any]    						=			llm_inta_has_fpaa_weight
    _UpperCamelCase:  Union[str, Any]    						=			bnb_abit_quant_type
    _UpperCamelCase:  Union[str, Any]    						=			bnb_abit_use_double_quant

    if bnb_abit_compute_dtype is None:
     _UpperCamelCase:  Optional[int]    						=			torch.floataa
    elif isinstance(_lowercase		,	_lowercase				):
     _UpperCamelCase:  Dict    						=			getattr(_lowercase		,	_lowercase				)
    elif isinstance(_lowercase		,	torch.dtype				):
     _UpperCamelCase:  Union[str, Any]    						=			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       :	Union[str, Any]				):



    """simple docstring"""



    if not isinstance(self.llm_inta_threshold		,	_lowercase				):
     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		,	_lowercase				):
     raise ValueError('''llm_int8_skip_modules must be a list of strings'''				)
    if not isinstance(self.llm_inta_enable_fpaa_cpu_offload		,	_lowercase				):
     raise ValueError('''llm_int8_enable_fp32_cpu_offload must be a boolean'''				)

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

    if not isinstance(self.bnb_abit_use_double_quant		,	_lowercase				):
     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[Any]				):



    """simple docstring"""



    return self.load_in_abit or self.load_in_abit



   def 	lowerCAmelCase    (						self       :	Optional[Any]				):



    """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 	lowerCAmelCase    (						cls       :	str		,	_lowercase       :	List[Any]		,	_lowercase       :	Tuple		,	**_lowercase       :	Tuple				):



    """simple docstring"""



    _UpperCamelCase:  int    						=			cls(**_lowercase				)

    _UpperCamelCase:  Optional[int]    						=			[]
    for key, value in kwargs.items():
     if hasattr(_lowercase		,	_lowercase				):
      setattr(_lowercase		,	_lowercase		,	_lowercase				)
      to_remove.append(_lowercase				)
    for key in to_remove:
     kwargs.pop(_lowercase		,	_lowercase				)

    if return_unused_kwargs:
     return config, kwargs
    else:
     return config



   def 	lowerCAmelCase    (						self       :	List[Any]		,	_lowercase       :	Union[str, os.PathLike]				):



    """simple docstring"""



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

     writer.write(_lowercase				)



   def 	lowerCAmelCase    (						self       :	List[str]				):



    """simple docstring"""



    _UpperCamelCase:  Optional[int]    						=			copy.deepcopy(self.__dict__				)
    _UpperCamelCase:  Any    						=			str(output['''bnb_4bit_compute_dtype''']				).split('''.'''				)[1]

    return output



   def __repr__(						self       :	Union[str, Any]				):



    """simple docstring"""



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



   def 	lowerCAmelCase    (						self       :	Dict		,	_lowercase       :	bool = True				):



    """simple docstring"""



    if use_diff is True:
     _UpperCamelCase:  Optional[Any]    						=			self.to_diff_dict()
    else:
     _UpperCamelCase:  Dict    						=			self.to_dict()
    return json.dumps(_lowercase		,	indent=2		,	sort_keys=_lowercase				) + "\n"



   def 	lowerCAmelCase    (						self       :	int				):



    """simple docstring"""



    _UpperCamelCase:  Dict    						=			self.to_dict()

    # get the default config dict
    _UpperCamelCase:  str    						=			BitsAndBytesConfig().to_dict()

    _UpperCamelCase:  List[str]    						=			{}

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

    return serializable_config_dict
import unittest

import numpy as np

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
  from transformers.models.bert.modeling_flax_bert import (
      FlaxBertForMaskedLM,
      FlaxBertForMultipleChoice,
      FlaxBertForNextSentencePrediction,
      FlaxBertForPreTraining,
      FlaxBertForQuestionAnswering,
      FlaxBertForSequenceClassification,
      FlaxBertForTokenClassification,
      FlaxBertModel,
  )




class 			__magic_name__	(		unittest.TestCase     ):





   """simple docstring"""




   def __init__(						self       :	Tuple		,	_lowercase       :	List[str]		,	_lowercase       :	Dict=13		,	_lowercase       :	Any=7		,	_lowercase       :	List[str]=True		,	_lowercase       :	Any=True		,	_lowercase       :	Optional[int]=True		,	_lowercase       :	int=True		,	_lowercase       :	str=99		,	_lowercase       :	Union[str, Any]=32		,	_lowercase       :	Any=5		,	_lowercase       :	Dict=4		,	_lowercase       :	Tuple=37		,	_lowercase       :	Optional[Any]="gelu"		,	_lowercase       :	Optional[int]=0.1		,	_lowercase       :	Any=0.1		,	_lowercase       :	Union[str, Any]=512		,	_lowercase       :	Dict=16		,	_lowercase       :	int=2		,	_lowercase       :	Union[str, Any]=0.02		,	_lowercase       :	Dict=4		,	):



    """simple docstring"""



    _UpperCamelCase:  List[str]    						=			parent
    _UpperCamelCase:  Optional[int]    						=			batch_size
    _UpperCamelCase:  Any    						=			seq_length
    _UpperCamelCase:  List[str]    						=			is_training
    _UpperCamelCase:  Union[str, Any]    						=			use_attention_mask
    _UpperCamelCase:  Union[str, Any]    						=			use_token_type_ids
    _UpperCamelCase:  str    						=			use_labels
    _UpperCamelCase:  List[str]    						=			vocab_size
    _UpperCamelCase:  Dict    						=			hidden_size
    _UpperCamelCase:  List[Any]    						=			num_hidden_layers
    _UpperCamelCase:  List[Any]    						=			num_attention_heads
    _UpperCamelCase:  Tuple    						=			intermediate_size
    _UpperCamelCase:  Optional[Any]    						=			hidden_act
    _UpperCamelCase:  Any    						=			hidden_dropout_prob
    _UpperCamelCase:  int    						=			attention_probs_dropout_prob
    _UpperCamelCase:  Optional[int]    						=			max_position_embeddings
    _UpperCamelCase:  List[Any]    						=			type_vocab_size
    _UpperCamelCase:  List[Any]    						=			type_sequence_label_size
    _UpperCamelCase:  List[Any]    						=			initializer_range
    _UpperCamelCase:  Optional[int]    						=			num_choices



   def 	lowerCAmelCase    (						self       :	Optional[Any]				):



    """simple docstring"""



    _UpperCamelCase:  int    						=			ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size				)

    _UpperCamelCase:  Optional[int]    						=			None
    if self.use_attention_mask:
     _UpperCamelCase:  List[str]    						=			random_attention_mask([self.batch_size, self.seq_length]				)

    _UpperCamelCase:  Optional[int]    						=			None
    if self.use_token_type_ids:
     _UpperCamelCase:  Tuple    						=			ids_tensor([self.batch_size, self.seq_length]		,	self.type_vocab_size				)

    _UpperCamelCase:  str    						=			BertConfig(
        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=_lowercase		,	initializer_range=self.initializer_range		,	)

    return config, input_ids, token_type_ids, attention_mask



   def 	lowerCAmelCase    (						self       :	Optional[Any]				):



    """simple docstring"""



    _UpperCamelCase:  Optional[Any]    						=			self.prepare_config_and_inputs()
    _UpperCamelCase						,				_UpperCamelCase						,				_UpperCamelCase						,				_UpperCamelCase:  Any    						=			config_and_inputs
    _UpperCamelCase:  List[Any]    						=			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
    return config, inputs_dict



   def 	lowerCAmelCase    (						self       :	Dict				):



    """simple docstring"""



    _UpperCamelCase:  Any    						=			self.prepare_config_and_inputs()
    _UpperCamelCase						,				_UpperCamelCase						,				_UpperCamelCase						,				_UpperCamelCase:  str    						=			config_and_inputs

    _UpperCamelCase:  int    						=			True
    _UpperCamelCase:  Optional[Any]    						=			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]				)
    _UpperCamelCase:  Optional[Any]    						=			ids_tensor([self.batch_size, self.seq_length]		,	vocab_size=2				)

    return (
        config,
        input_ids,
        attention_mask,
        encoder_hidden_states,
        encoder_attention_mask,
    )




@require_flax
class 			__magic_name__	(		__a   ,						unittest.TestCase     ):





   """simple docstring"""




   lowerCAmelCase   :     Dict	      = True

   lowerCAmelCase   :     List[Any]	      = (
       (
           FlaxBertModel,
           FlaxBertForPreTraining,
           FlaxBertForMaskedLM,
           FlaxBertForMultipleChoice,
           FlaxBertForQuestionAnswering,
           FlaxBertForNextSentencePrediction,
           FlaxBertForSequenceClassification,
           FlaxBertForTokenClassification,
           FlaxBertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )



   def 	lowerCAmelCase    (						self       :	Dict				):



    """simple docstring"""



    _UpperCamelCase:  Dict    						=			FlaxBertModelTester(self				)



   @slow
   def 	lowerCAmelCase    (						self       :	Tuple				):



    """simple docstring"""



    _UpperCamelCase:  Dict    						=			FlaxBertModel.from_pretrained('''bert-base-cased'''				)
    _UpperCamelCase:  int    						=			model(np.ones((1, 1)				)				)
    self.assertIsNotNone(_lowercase				)