


"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import DistilBertConfig, 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 tensorflow as tf

       from transformers.models.distilbert.modeling_tf_distilbert import (
           TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
           TFDistilBertForMaskedLM,
           TFDistilBertForMultipleChoice,
           TFDistilBertForQuestionAnswering,
           TFDistilBertForSequenceClassification,
           TFDistilBertForTokenClassification,
           TFDistilBertModel,
       )


class 	A_ :




      '''simple docstring'''


      def __init__(    self:	Optional[Any]						,				a:	Dict						,				):
       __lowerCamelCase		:							Optional[Any]		 =						parent
       __lowerCamelCase		:							Dict		 =						13
       __lowerCamelCase		:							Dict		 =						7
       __lowerCamelCase		:							str		 =						True
       __lowerCamelCase		:							Any		 =						True
       __lowerCamelCase		:							List[Any]		 =						False
       __lowerCamelCase		:							int		 =						True
       __lowerCamelCase		:							Optional[int]		 =						99
       __lowerCamelCase		:							Any		 =						32
       __lowerCamelCase		:							Optional[int]		 =						2
       __lowerCamelCase		:							str		 =						4
       __lowerCamelCase		:							Any		 =						37
       __lowerCamelCase		:							Union[str, Any]		 =						'''gelu'''
       __lowerCamelCase		:							Tuple		 =						0.1
       __lowerCamelCase		:							Dict		 =						0.1
       __lowerCamelCase		:							List[Any]		 =						512
       __lowerCamelCase		:							Optional[Any]		 =						16
       __lowerCamelCase		:							List[Any]		 =						2
       __lowerCamelCase		:							Optional[int]		 =						0.0_2
       __lowerCamelCase		:							Optional[Any]		 =						3
       __lowerCamelCase		:							Optional[int]		 =						4
       __lowerCamelCase		:							int		 =						None




      def 					_snake_case     (    self:	int    ):
       __lowerCamelCase		:							Optional[Any]		 =						ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size    )

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

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

       __lowerCamelCase		:							Tuple		 =						DistilBertConfig(
           vocab_size=self.vocab_size						,				dim=self.hidden_size						,				n_layers=self.num_hidden_layers						,				n_heads=self.num_attention_heads						,				hidden_dim=self.intermediate_size						,				hidden_act=self.hidden_act						,				dropout=self.hidden_dropout_prob						,				attention_dropout=self.attention_probs_dropout_prob						,				max_position_embeddings=self.max_position_embeddings						,				initializer_range=self.initializer_range						,				)

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




      def 					_snake_case     (    self:	Dict						,				a:	Tuple						,				a:	Tuple						,				a:	str						,				a:	List[Any]						,				a:	List[Any]						,				a:	Union[str, Any]    ):
       __lowerCamelCase		:							List[str]		 =						TFDistilBertModel(config=snake_case__    )
       __lowerCamelCase		:							Optional[int]		 =						{'''input_ids''': input_ids, '''attention_mask''': input_mask}

       __lowerCamelCase		:							Optional[Any]		 =						model(snake_case__    )

       __lowerCamelCase		:							Union[str, Any]		 =						[input_ids, input_mask]

       __lowerCamelCase		:							Any		 =						model(snake_case__    )

       self.parent.assertEqual(result.last_hidden_state.shape						,				(self.batch_size, self.seq_length, self.hidden_size)    )




      def 					_snake_case     (    self:	Optional[int]						,				a:	List[Any]						,				a:	str						,				a:	Union[str, Any]						,				a:	Any						,				a:	Tuple						,				a:	Tuple    ):
       __lowerCamelCase		:							List[Any]		 =						TFDistilBertForMaskedLM(config=snake_case__    )
       __lowerCamelCase		:							Optional[int]		 =						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       __lowerCamelCase		:							int		 =						model(snake_case__    )
       self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.vocab_size)    )




      def 					_snake_case     (    self:	int						,				a:	List[Any]						,				a:	Dict						,				a:	str						,				a:	Optional[int]						,				a:	Tuple						,				a:	Dict    ):
       __lowerCamelCase		:							Any		 =						TFDistilBertForQuestionAnswering(config=snake_case__    )
       __lowerCamelCase		:							str		 =						{
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
       }
       __lowerCamelCase		:							str		 =						model(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 					_snake_case     (    self:	List[str]						,				a:	str						,				a:	Tuple						,				a:	Dict						,				a:	Any						,				a:	Any						,				a:	str    ):
       __lowerCamelCase		:							List[Any]		 =						self.num_labels
       __lowerCamelCase		:							Dict		 =						TFDistilBertForSequenceClassification(snake_case__    )
       __lowerCamelCase		:							List[str]		 =						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       __lowerCamelCase		:							int		 =						model(snake_case__    )
       self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels)    )




      def 					_snake_case     (    self:	Optional[int]						,				a:	int						,				a:	str						,				a:	str						,				a:	List[str]						,				a:	Optional[int]						,				a:	str    ):
       __lowerCamelCase		:							Any		 =						self.num_choices
       __lowerCamelCase		:							Union[str, Any]		 =						TFDistilBertForMultipleChoice(snake_case__    )
       __lowerCamelCase		:							Union[str, Any]		 =						tf.tile(tf.expand_dims(snake_case__						,				1    )						,				(1, self.num_choices, 1)    )
       __lowerCamelCase		:							str		 =						tf.tile(tf.expand_dims(snake_case__						,				1    )						,				(1, self.num_choices, 1)    )
       __lowerCamelCase		:							str		 =						{
           '''input_ids''': multiple_choice_inputs_ids,
           '''attention_mask''': multiple_choice_input_mask,
       }
       __lowerCamelCase		:							Any		 =						model(snake_case__    )
       self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_choices)    )




      def 					_snake_case     (    self:	List[str]						,				a:	List[str]						,				a:	Optional[int]						,				a:	List[str]						,				a:	str						,				a:	Dict						,				a:	Any    ):
       __lowerCamelCase		:							Optional[int]		 =						self.num_labels
       __lowerCamelCase		:							Union[str, Any]		 =						TFDistilBertForTokenClassification(snake_case__    )
       __lowerCamelCase		:							Dict		 =						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       __lowerCamelCase		:							Any		 =						model(snake_case__    )
       self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.num_labels)    )




      def 					_snake_case     (    self:	Any    ):
       __lowerCamelCase		:							Tuple		 =						self.prepare_config_and_inputs()
       (__lowerCamelCase)		:							Optional[Any]		 =						config_and_inputs
       __lowerCamelCase		:							List[str]		 =						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict



@require_tf
class 	A_ (	lowerCamelCase__ ,				lowerCamelCase__ ,				unittest.TestCase   ):




      '''simple docstring'''


      __snake_case 	=       (
          (
              TFDistilBertModel,
              TFDistilBertForMaskedLM,
              TFDistilBertForQuestionAnswering,
              TFDistilBertForSequenceClassification,
              TFDistilBertForTokenClassification,
              TFDistilBertForMultipleChoice,
          )
          if is_tf_available()
          else None
      )
      __snake_case 	=       (
          {
              """feature-extraction""": TFDistilBertModel,
              """fill-mask""": TFDistilBertForMaskedLM,
              """question-answering""": TFDistilBertForQuestionAnswering,
              """text-classification""": TFDistilBertForSequenceClassification,
              """token-classification""": TFDistilBertForTokenClassification,
              """zero-shot""": TFDistilBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      __snake_case 	=       False
      __snake_case 	=       False




      def 					_snake_case     (    self:	Any    ):
       __lowerCamelCase		:							Union[str, Any]		 =						TFDistilBertModelTester(self    )
       __lowerCamelCase		:							Dict		 =						ConfigTester(self						,				config_class=snake_case__						,				dim=37    )




      def 					_snake_case     (    self:	Optional[Any]    ):
       self.config_tester.run_common_tests()




      def 					_snake_case     (    self:	List[str]    ):
       __lowerCamelCase		:							Optional[int]		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*snake_case__    )




      def 					_snake_case     (    self:	int    ):
       __lowerCamelCase		:							Tuple		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*snake_case__    )




      def 					_snake_case     (    self:	Any    ):
       __lowerCamelCase		:							Tuple		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*snake_case__    )




      def 					_snake_case     (    self:	Tuple    ):
       __lowerCamelCase		:							Dict		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*snake_case__    )




      def 					_snake_case     (    self:	Dict    ):
       __lowerCamelCase		:							str		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*snake_case__    )




      def 					_snake_case     (    self:	str    ):
       __lowerCamelCase		:							str		 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*snake_case__    )




      @slow
      def 					_snake_case     (    self:	Optional[int]    ):





       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]    ):
        __lowerCamelCase		:							Any		 =						TFDistilBertModel.from_pretrained(snake_case__    )
        self.assertIsNotNone(snake_case__    )



@require_tf
class 	A_ (	unittest.TestCase   ):




      '''simple docstring'''


      @slow
      def 					_snake_case     (    self:	Any    ):
       __lowerCamelCase		:							Optional[Any]		 =						TFDistilBertModel.from_pretrained('distilbert-base-uncased'    )
       __lowerCamelCase		:							Optional[Any]		 =						tf.constant([[0, 1, 2, 3, 4, 5]]    )
       __lowerCamelCase		:							List[Any]		 =						model(snake_case__    )[0]

       __lowerCamelCase		:							List[Any]		 =						[1, 6, 768]
       self.assertEqual(output.shape						,				snake_case__    )

       __lowerCamelCase		:							int		 =						tf.constant(
           [
               [
                   [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9],
                   [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4],
                   [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7],
               ]
           ]    )
       tf.debugging.assert_near(output[:, :3, :3]						,				snake_case__						,				atol=1e-4    )





import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
       from ...processing_utils import ProcessorMixin
       from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase_	   =     logging.get_logger(__name__)

lowercase_	   =     {
    'google/owlvit-base-patch32': 'https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json',
    'google/owlvit-base-patch16': 'https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json',
    'google/owlvit-large-patch14': 'https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json',
}


class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit_text_model"""




      def __init__(    self:	Optional[int]						,				a:	Dict=4_9408						,				a:	Optional[Any]=512						,				a:	Dict=2048						,				a:	Optional[Any]=12						,				a:	Tuple=8						,				a:	Union[str, Any]=16						,				a:	str="quick_gelu"						,				a:	List[Any]=1e-5						,				a:	Dict=0.0						,				a:	Optional[int]=0.0_2						,				a:	Dict=1.0						,				a:	Any=0						,				a:	Union[str, Any]=4_9406						,				a:	Any=4_9407						,				**a:	Dict						,				):
       super().__init__(pad_token_id=a						,				bos_token_id=a						,				eos_token_id=a						,				**a    )

       __lowerCamelCase		:							List[Any]		 =						vocab_size
       __lowerCamelCase		:							int		 =						hidden_size
       __lowerCamelCase		:							Optional[Any]		 =						intermediate_size
       __lowerCamelCase		:							List[Any]		 =						num_hidden_layers
       __lowerCamelCase		:							Any		 =						num_attention_heads
       __lowerCamelCase		:							Union[str, Any]		 =						max_position_embeddings
       __lowerCamelCase		:							List[Any]		 =						hidden_act
       __lowerCamelCase		:							List[str]		 =						layer_norm_eps
       __lowerCamelCase		:							Tuple		 =						attention_dropout
       __lowerCamelCase		:							Optional[int]		 =						initializer_range
       __lowerCamelCase		:							Tuple		 =						initializer_factor




      @classmethod
      def 					_snake_case     (    cls:	Dict						,				a:	Union[str, os.PathLike]						,				**a:	Optional[int]    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						cls.get_config_dict(a						,				**a    )

       # get the text config dict if we are loading from OwlViTConfig
       if config_dict.get('model_type'    ) == "owlvit":
        __lowerCamelCase		:							Optional[Any]		 =						config_dict['text_config']

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit_vision_model"""




      def __init__(    self:	int						,				a:	Tuple=768						,				a:	int=3072						,				a:	List[str]=12						,				a:	Optional[Any]=12						,				a:	Optional[int]=3						,				a:	Optional[int]=768						,				a:	Optional[Any]=32						,				a:	Optional[int]="quick_gelu"						,				a:	Union[str, Any]=1e-5						,				a:	Union[str, Any]=0.0						,				a:	Union[str, Any]=0.0_2						,				a:	int=1.0						,				**a:	Union[str, Any]						,				):
       super().__init__(**a    )

       __lowerCamelCase		:							str		 =						hidden_size
       __lowerCamelCase		:							Tuple		 =						intermediate_size
       __lowerCamelCase		:							Dict		 =						num_hidden_layers
       __lowerCamelCase		:							Optional[Any]		 =						num_attention_heads
       __lowerCamelCase		:							int		 =						num_channels
       __lowerCamelCase		:							Optional[Any]		 =						image_size
       __lowerCamelCase		:							Tuple		 =						patch_size
       __lowerCamelCase		:							List[str]		 =						hidden_act
       __lowerCamelCase		:							Tuple		 =						layer_norm_eps
       __lowerCamelCase		:							List[Any]		 =						attention_dropout
       __lowerCamelCase		:							Tuple		 =						initializer_range
       __lowerCamelCase		:							List[Any]		 =						initializer_factor




      @classmethod
      def 					_snake_case     (    cls:	Optional[int]						,				a:	Union[str, os.PathLike]						,				**a:	int    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						cls.get_config_dict(a						,				**a    )

       # get the vision config dict if we are loading from OwlViTConfig
       if config_dict.get('model_type'    ) == "owlvit":
        __lowerCamelCase		:							Any		 =						config_dict['vision_config']

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit"""
      __snake_case 	=       True




      def __init__(    self:	Dict						,				a:	int=None						,				a:	str=None						,				a:	Tuple=512						,				a:	Tuple=2.6_5_9_2						,				a:	int=True						,				**a:	int						,				):
       super().__init__(**a    )

       if text_config is None:
        __lowerCamelCase		:							List[str]		 =						{}
        logger.info('text_config is None. Initializing the OwlViTTextConfig with default values.'    )

       if vision_config is None:
        __lowerCamelCase		:							str		 =						{}
        logger.info('vision_config is None. initializing the OwlViTVisionConfig with default values.'    )

       __lowerCamelCase		:							List[Any]		 =						OwlViTTextConfig(**a    )
       __lowerCamelCase		:							str		 =						OwlViTVisionConfig(**a    )

       __lowerCamelCase		:							Union[str, Any]		 =						projection_dim
       __lowerCamelCase		:							Tuple		 =						logit_scale_init_value
       __lowerCamelCase		:							Dict		 =						return_dict
       __lowerCamelCase		:							Tuple		 =						1.0




      @classmethod
      def 					_snake_case     (    cls:	str						,				a:	Union[str, os.PathLike]						,				**a:	List[Any]    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							List[Any]		 =						cls.get_config_dict(a						,				**a    )

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )




      @classmethod
      def 					_snake_case     (    cls:	Tuple						,				a:	Dict						,				a:	Dict						,				**a:	str    ):
       __lowerCamelCase		:							List[str]		 =						{}
       __lowerCamelCase		:							List[str]		 =						text_config
       __lowerCamelCase		:							Optional[int]		 =						vision_config

       return cls.from_dict(a						,				**a    )




      def 					_snake_case     (    self:	Optional[int]    ):
       __lowerCamelCase		:							Optional[Any]		 =						copy.deepcopy(self.__dict__    )
       __lowerCamelCase		:							List[Any]		 =						self.text_config.to_dict()
       __lowerCamelCase		:							List[str]		 =						self.vision_config.to_dict()
       __lowerCamelCase		:							Optional[Any]		 =						self.__class__.model_type
       return output



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      @property
      def 					_snake_case     (    self:	str    ):
       return OrderedDict(
           [
               ('input_ids', {0: 'batch', 1: 'sequence'}),
               ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
               ('attention_mask', {0: 'batch', 1: 'sequence'}),
           ]    )




      @property
      def 					_snake_case     (    self:	Dict    ):
       return OrderedDict(
           [
               ('logits_per_image', {0: 'batch'}),
               ('logits_per_text', {0: 'batch'}),
               ('text_embeds', {0: 'batch'}),
               ('image_embeds', {0: 'batch'}),
           ]    )




      @property
      def 					_snake_case     (    self:	int    ):
       return 1e-4




      def 					_snake_case     (    self:	Any						,				a:	"ProcessorMixin"						,				a:	int = -1						,				a:	int = -1						,				a:	Optional["TensorType"] = None						,				):
       __lowerCamelCase		:							List[str]		 =						super().generate_dummy_inputs(
           processor.tokenizer						,				batch_size=a						,				seq_length=a						,				framework=a    )
       __lowerCamelCase		:							int		 =						super().generate_dummy_inputs(
           processor.image_processor						,				batch_size=a						,				framework=a    )
       return {**text_input_dict, **image_input_dict}




      @property
      def 					_snake_case     (    self:	int    ):
       return 14

