"""simple docstring"""


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						:	List[str] ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	str=13 ,					__lowerCAmelCase						:	Union[str, Any]=7 ,					__lowerCAmelCase						:	Any=True ,					__lowerCAmelCase						:	str=True ,					__lowerCAmelCase						:	int=True ,					__lowerCAmelCase						:	int=True ,					__lowerCAmelCase						:	List[Any]=99 ,					__lowerCAmelCase						:	str=32 ,					__lowerCAmelCase						:	List[Any]=2 ,					__lowerCAmelCase						:	int=4 ,					__lowerCAmelCase						:	int=37 ,					__lowerCAmelCase						:	Optional[int]="gelu" ,					__lowerCAmelCase						:	Dict=0.1 ,					__lowerCAmelCase						:	Any=0.1 ,					__lowerCAmelCase						:	Optional[int]=512 ,					__lowerCAmelCase						:	Optional[Any]=16 ,					__lowerCAmelCase						:	Any=2 ,					__lowerCAmelCase						:	str=0.02 ,					__lowerCAmelCase						:	List[Any]=3 ,					__lowerCAmelCase						:	int=4 ,					__lowerCAmelCase						:	Optional[int]=None ,					__lowerCAmelCase						:	Optional[int]=0 ,					):
      _UpperCAmelCase    			=   parent
      _UpperCAmelCase    			=   batch_size
      _UpperCAmelCase    			=   seq_length
      _UpperCAmelCase    			=   is_training
      _UpperCAmelCase    			=   use_input_mask
      _UpperCAmelCase    			=   use_token_type_ids
      _UpperCAmelCase    			=   use_labels
      _UpperCAmelCase    			=   vocab_size
      _UpperCAmelCase    			=   hidden_size
      _UpperCAmelCase    			=   num_hidden_layers
      _UpperCAmelCase    			=   num_attention_heads
      _UpperCAmelCase    			=   intermediate_size
      _UpperCAmelCase    			=   hidden_act
      _UpperCAmelCase    			=   hidden_dropout_prob
      _UpperCAmelCase    			=   attention_probs_dropout_prob
      _UpperCAmelCase    			=   max_position_embeddings
      _UpperCAmelCase    			=   type_vocab_size
      _UpperCAmelCase    			=   type_sequence_label_size
      _UpperCAmelCase    			=   initializer_range
      _UpperCAmelCase    			=   num_labels
      _UpperCAmelCase    			=   num_choices
      _UpperCAmelCase    			=   scope
      _UpperCAmelCase    			=   projection_dim


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      _UpperCAmelCase    			=   ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size       )

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

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

      _UpperCAmelCase    			=   None
      _UpperCAmelCase    			=   None
      _UpperCAmelCase    			=   None
      if self.use_labels:
        _UpperCAmelCase    			=   ids_tensor([self.batch_size] ,					self.type_sequence_label_size       )
        _UpperCAmelCase    			=   ids_tensor([self.batch_size, self.seq_length] ,					self.num_labels       )
        _UpperCAmelCase    			=   ids_tensor([self.batch_size] ,					self.num_choices       )

      _UpperCAmelCase    			=   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=__lowerCAmelCase ,					initializer_range=self.initializer_range ,					)
      _UpperCAmelCase    			=   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   lowerCAmelCase_	(       self						:	List[Any] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Dict ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	Any       ):
      _UpperCAmelCase    			=   TFDPRContextEncoder(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase ,					attention_mask=__lowerCAmelCase ,					token_type_ids=__lowerCAmelCase       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase ,					token_type_ids=__lowerCAmelCase       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.pooler_output.shape ,					(self.batch_size, self.projection_dim or self.hidden_size)       )


    def   lowerCAmelCase_	(       self						:	str ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	str ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	Optional[int] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	List[str] ,					__lowerCAmelCase						:	Any       ):
      _UpperCAmelCase    			=   TFDPRQuestionEncoder(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase ,					attention_mask=__lowerCAmelCase ,					token_type_ids=__lowerCAmelCase       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase ,					token_type_ids=__lowerCAmelCase       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )
      self.parent.assertEqual(result.pooler_output.shape ,					(self.batch_size, self.projection_dim or self.hidden_size)       )


    def   lowerCAmelCase_	(       self						:	Dict ,					__lowerCAmelCase						:	int ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	Any ,					__lowerCAmelCase						:	Union[str, Any] ,					__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	Optional[Any]       ):
      _UpperCAmelCase    			=   TFDPRReader(config=__lowerCAmelCase       )
      _UpperCAmelCase    			=   model(__lowerCAmelCase ,					attention_mask=__lowerCAmelCase       )

      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   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   self.prepare_config_and_inputs()
      (
          (
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					(
          _UpperCAmelCase
      )			,					
      )    			=   config_and_inputs
      _UpperCAmelCase    			=   {"""input_ids""": input_ids}
      return config, inputs_dict



@require_tf
class 							a					(		lowerCAmelCase_						,				lowerCAmelCase_						,				unittest.TestCase ):
    _snake_case  :	Tuple  				=				(
        (
            TFDPRContextEncoder,
            TFDPRQuestionEncoder,
            TFDPRReader,
        )
        if is_tf_available()
        else ()
    )
    _snake_case  :	str  				=				{'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

    _snake_case  :	Optional[int]  				=				False
    _snake_case  :	Optional[int]  				=				False
    _snake_case  :	Any  				=				False
    _snake_case  :	Optional[int]  				=				False
    _snake_case  :	Dict  				=				False


    def   lowerCAmelCase_	(       self						:	List[str]       ):
      _UpperCAmelCase    			=   TFDPRModelTester(self       )
      _UpperCAmelCase    			=   ConfigTester(self ,					config_class=__lowerCAmelCase ,					hidden_size=37       )


    def   lowerCAmelCase_	(       self						:	Tuple       ):
      self.config_tester.run_common_tests()


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_context_encoder(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	List[str]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_question_encoder(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Optional[int]       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_dpr_reader(*__lowerCAmelCase       )


    @slow
    def   lowerCAmelCase_	(       self						:	int       ):
      for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase    			=   TFDPRContextEncoder.from_pretrained(__lowerCAmelCase       )
        self.assertIsNotNone(__lowerCAmelCase       )

      for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase    			=   TFDPRContextEncoder.from_pretrained(__lowerCAmelCase       )
        self.assertIsNotNone(__lowerCAmelCase       )

      for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase    			=   TFDPRQuestionEncoder.from_pretrained(__lowerCAmelCase       )
        self.assertIsNotNone(__lowerCAmelCase       )

      for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase    			=   TFDPRReader.from_pretrained(__lowerCAmelCase       )
        self.assertIsNotNone(__lowerCAmelCase       )



@require_tf
class 							a					(		unittest.TestCase ):


    @slow
    def   lowerCAmelCase_	(       self						:	Optional[Any]       ):
      _UpperCAmelCase    			=   TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""       )

      _UpperCAmelCase    			=   tf.constant(
          [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]       )  # [CLS] hello, is my dog cute? [SEP]
      _UpperCAmelCase    			=   model(__lowerCAmelCase       )[0]  # embedding shape = (1, 768)
      # compare the actual values for a slice.
      _UpperCAmelCase    			=   tf.constant(
          [
              [
                  0.03_236_253,
                  0.12_753_335,
                  0.16_818_509,
                  0.00_279_786,
                  0.3_896_933,
                  0.24_264_945,
                  0.2_178_971,
                  -0.02_335_227,
                  -0.08_481_959,
                  -0.14_324_117,
              ]
          ]       )
      self.assertTrue(numpy.allclose(output[:, :10].numpy() ,					expected_slice.numpy() ,					atol=1e-4       )       )

"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


UpperCAmelCase__			=    {
    """configuration_gpt_bigcode""": ["""GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTBigCodeConfig"""],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCAmelCase__			=    [
        """GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """GPTBigCodeForSequenceClassification""",
        """GPTBigCodeForTokenClassification""",
        """GPTBigCodeForCausalLM""",
        """GPTBigCodeModel""",
        """GPTBigCodePreTrainedModel""",
    ]

if TYPE_CHECKING:
    from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_gpt_bigcode import (
            GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST,
            GPTBigCodeForCausalLM,
            GPTBigCodeForSequenceClassification,
            GPTBigCodeForTokenClassification,
            GPTBigCodeModel,
            GPTBigCodePreTrainedModel,
        )


else:
    import sys

    UpperCAmelCase__			=    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
