

from __future__ import annotations

import unittest

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

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 numpy
    import tensorflow as tf

    from transformers import (
        TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        BertConfig,
        DPRConfig,
        TFDPRContextEncoder,
        TFDPRQuestionEncoder,
        TFDPRReader,
    )
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=32 ,					_SCREAMING_SNAKE_CASE=2 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=37 ,					_SCREAMING_SNAKE_CASE="gelu" ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=512 ,					_SCREAMING_SNAKE_CASE=16 ,					_SCREAMING_SNAKE_CASE=2 ,					_SCREAMING_SNAKE_CASE=0.02 ,					_SCREAMING_SNAKE_CASE=3 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE=0 ,					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     parent
        SCREAMING_SNAKE_CASE_				:  Any           =     batch_size
        SCREAMING_SNAKE_CASE_				:  str           =     seq_length
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     is_training
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     use_input_mask
        SCREAMING_SNAKE_CASE_				:  Any           =     use_token_type_ids
        SCREAMING_SNAKE_CASE_				:  List[str]           =     use_labels
        SCREAMING_SNAKE_CASE_				:  Dict           =     vocab_size
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     hidden_size
        SCREAMING_SNAKE_CASE_				:  str           =     num_hidden_layers
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     num_attention_heads
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     intermediate_size
        SCREAMING_SNAKE_CASE_				:  Dict           =     hidden_act
        SCREAMING_SNAKE_CASE_				:  Dict           =     hidden_dropout_prob
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE_				:  int           =     max_position_embeddings
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     type_vocab_size
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     type_sequence_label_size
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     initializer_range
        SCREAMING_SNAKE_CASE_				:  List[str]           =     num_labels
        SCREAMING_SNAKE_CASE_				:  str           =     num_choices
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     scope
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     projection_dim

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







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

        SCREAMING_SNAKE_CASE_				:  int           =     None
        if self.use_input_mask:
              # follow test_modeling_tf_ctrl.py
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     random_attention_mask([self.batch_size, self.seq_length]					)

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

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

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     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=_SCREAMING_SNAKE_CASE ,					initializer_range=self.initializer_range ,					)
        SCREAMING_SNAKE_CASE_				:  Tuple           =     DPRConfig(projection_dim=self.projection_dim ,					**config.to_dict()					)

        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_				:  str           =     TFDPRContextEncoder(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     model(_SCREAMING_SNAKE_CASE ,					attention_mask=_SCREAMING_SNAKE_CASE ,					token_type_ids=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     model(_SCREAMING_SNAKE_CASE ,					token_type_ids=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.pooler_output.shape ,					(self.batch_size, self.projection_dim or self.hidden_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_				:  str           =     TFDPRQuestionEncoder(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     model(_SCREAMING_SNAKE_CASE ,					attention_mask=_SCREAMING_SNAKE_CASE ,					token_type_ids=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Any           =     model(_SCREAMING_SNAKE_CASE ,					token_type_ids=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  int           =     model(_SCREAMING_SNAKE_CASE					)
        self.parent.assertEqual(result.pooler_output.shape ,					(self.batch_size, self.projection_dim or self.hidden_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_				:  Any           =     TFDPRReader(config=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     model(_SCREAMING_SNAKE_CASE ,					attention_mask=_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)					)
        self.parent.assertEqual(result.relevance_logits.shape ,					(self.batch_size,)					)



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Any           =     self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE_
        )       ,						(
            SCREAMING_SNAKE_CASE_
        )       ,						(
            SCREAMING_SNAKE_CASE_
        )       ,						(
            SCREAMING_SNAKE_CASE_
        )       ,						(
            SCREAMING_SNAKE_CASE_
        )       ,						(
            SCREAMING_SNAKE_CASE_
        )       ,						(
            SCREAMING_SNAKE_CASE_
        )       ,						
        )				:  Tuple           =     config_and_inputs
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     {'input_ids': input_ids}
        return config, inputs_dict

@require_tf
class  _A		(  __magic_name__   ,     __magic_name__   ,     unittest.TestCase):
  SCREAMING_SNAKE_CASE    :      str       =  (
      (
          TFDPRContextEncoder,
          TFDPRQuestionEncoder,
          TFDPRReader,
      )
      if is_tf_available()
      else ()
  )
  SCREAMING_SNAKE_CASE    :      List[Any]       =  {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

  SCREAMING_SNAKE_CASE    :      Dict       =  False
  SCREAMING_SNAKE_CASE    :      int       =  False
  SCREAMING_SNAKE_CASE    :      str       =  False
  SCREAMING_SNAKE_CASE    :      Tuple       =  False
  SCREAMING_SNAKE_CASE    :      Any       =  False

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Tuple           =     TFDPRModelTester(self					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     ConfigTester(self ,					config_class=_SCREAMING_SNAKE_CASE ,					hidden_size=37					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        self.config_tester.run_common_tests()

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_context_encoder(*_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_question_encoder(*_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_reader(*_SCREAMING_SNAKE_CASE					)



  @slow
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_				:  Tuple           =     TFDPRContextEncoder.from_pretrained(_SCREAMING_SNAKE_CASE					)
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE					)

        for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_				:  List[str]           =     TFDPRContextEncoder.from_pretrained(_SCREAMING_SNAKE_CASE					)
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE					)

        for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_				:  str           =     TFDPRQuestionEncoder.from_pretrained(_SCREAMING_SNAKE_CASE					)
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE					)

        for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_				:  Dict           =     TFDPRReader.from_pretrained(_SCREAMING_SNAKE_CASE					)
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE					)

@require_tf
class  _A		(  unittest.TestCase):

  @slow
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'					)

        SCREAMING_SNAKE_CASE_				:  List[str]           =     tf.constant(
            [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]					)  # [CLS] hello, is my dog cute? [SEP]
        SCREAMING_SNAKE_CASE_				:  str           =     model(_SCREAMING_SNAKE_CASE					)[0]  # embedding shape = (1, 768)
        # compare the actual values for a slice.
        SCREAMING_SNAKE_CASE_				:  List[str]           =     tf.constant(
            [
                [
                    0.03236253,
                    0.12753335,
                    0.16818509,
                    0.00279786,
                    0.3896933,
                    0.24264945,
                    0.2178971,
                    -0.02335227,
                    -0.08481959,
                    -0.14324117,
                ]
            ]					)
        self.assertTrue(numpy.allclose(output[:, :10].numpy() ,					expected_slice.numpy() ,					atol=1e-4					)					)









# 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






