


'''simple docstring'''




import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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 (
     SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
     SqueezeBertForMaskedLM,
     SqueezeBertForMultipleChoice,
     SqueezeBertForQuestionAnswering,
     SqueezeBertForSequenceClassification,
     SqueezeBertForTokenClassification,
     SqueezeBertModel,
 )







class 					a			(				snake_case__  ):
 '''simple docstring'''




 def __init__(    self		,      lowerCamelCase_		,      lowerCamelCase_=1_3		,      lowerCamelCase_=7		,      lowerCamelCase_=True		,      lowerCamelCase_=True		,      lowerCamelCase_=False		,      lowerCamelCase_=True		,      lowerCamelCase_=9_9		,      lowerCamelCase_=3_2		,      lowerCamelCase_=5		,      lowerCamelCase_=4		,      lowerCamelCase_=6_4		,      lowerCamelCase_="gelu"		,      lowerCamelCase_=0.1		,      lowerCamelCase_=0.1		,      lowerCamelCase_=5_1_2		,      lowerCamelCase_=1_6		,      lowerCamelCase_=2		,      lowerCamelCase_=0.02		,      lowerCamelCase_=3		,      lowerCamelCase_=4		,      lowerCamelCase_=None		,      lowerCamelCase_=2		,      lowerCamelCase_=2		,      lowerCamelCase_=2		,      lowerCamelCase_=2		,      lowerCamelCase_=4		,      lowerCamelCase_=1		,      )      ->    str:
    _a					:  Optional[int]						=  parent
    _a					:  Union[str, Any]						=  batch_size
    _a					:  Any						=  seq_length
    _a					:  Optional[Any]						=  is_training
    _a					:  List[str]						=  use_input_mask
    _a					:  Dict						=  use_token_type_ids
    _a					:  List[Any]						=  use_labels
    _a					:  Tuple						=  vocab_size
    _a					:  Any						=  hidden_size
    _a					:  List[Any]						=  num_hidden_layers
    _a					:  Tuple						=  num_attention_heads
    _a					:  str						=  intermediate_size
    _a					:  str						=  hidden_act
    _a					:  Union[str, Any]						=  hidden_dropout_prob
    _a					:  int						=  attention_probs_dropout_prob
    _a					:  Tuple						=  max_position_embeddings
    _a					:  Union[str, Any]						=  type_vocab_size
    _a					:  List[str]						=  type_sequence_label_size
    _a					:  Dict						=  initializer_range
    _a					:  str						=  num_labels
    _a					:  Optional[int]						=  num_choices
    _a					:  Dict						=  scope
    _a					:  str						=  q_groups
    _a					:  List[str]						=  k_groups
    _a					:  List[str]						=  v_groups
    _a					:  Dict						=  post_attention_groups
    _a					:  List[str]						=  intermediate_groups
    _a					:  Any						=  output_groups




 def   __UpperCamelCase   (    self      )      ->    str:
    _a					:  List[Any]						=  ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size      )

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

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

    _a					:  Union[str, Any]						=  self.get_config()

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




 def   __UpperCamelCase   (    self      )      ->    Union[str, Any]:
    return SqueezeBertConfig(
        embedding_size=self.hidden_size		,      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		,      attention_probs_dropout_prob=self.hidden_dropout_prob		,      attention_dropout=self.attention_probs_dropout_prob		,      max_position_embeddings=self.max_position_embeddings		,      initializer_range=self.initializer_range		,      q_groups=self.q_groups		,      k_groups=self.k_groups		,      v_groups=self.v_groups		,      post_attention_groups=self.post_attention_groups		,      intermediate_groups=self.intermediate_groups		,      output_groups=self.output_groups		,      )




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    Tuple:
    _a					:  str						=  SqueezeBertModel(config=lowerCamelCase_      )
    model.to(lowerCamelCase_      )
    model.eval()
    _a					:  int						=  model(lowerCamelCase_		,      lowerCamelCase_      )
    _a					:  str						=  model(lowerCamelCase_      )
    self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size)      )




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




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    Union[str, Any]:
    _a					:  str						=  SqueezeBertForQuestionAnswering(config=lowerCamelCase_      )
    model.to(lowerCamelCase_      )
    model.eval()
    _a					:  Tuple						=  model(
        lowerCamelCase_		,      attention_mask=lowerCamelCase_		,      start_positions=lowerCamelCase_		,      end_positions=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)      )




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    Optional[Any]:
    _a					:  Optional[Any]						=  self.num_labels
    _a					:  Union[str, Any]						=  SqueezeBertForSequenceClassification(lowerCamelCase_      )
    model.to(lowerCamelCase_      )
    model.eval()
    _a					:  Tuple						=  model(lowerCamelCase_		,      attention_mask=lowerCamelCase_		,      labels=lowerCamelCase_      )
    self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)      )




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    Dict:
    _a					:  Optional[Any]						=  self.num_labels
    _a					:  Any						=  SqueezeBertForTokenClassification(config=lowerCamelCase_      )
    model.to(lowerCamelCase_      )
    model.eval()

    _a					:  Optional[int]						=  model(lowerCamelCase_		,      attention_mask=lowerCamelCase_		,      labels=lowerCamelCase_      )
    self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.num_labels)      )




 def   __UpperCamelCase   (    self		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_		,      lowerCamelCase_      )      ->    Any:
    _a					:  Optional[int]						=  self.num_choices
    _a					:  List[Any]						=  SqueezeBertForMultipleChoice(config=lowerCamelCase_      )
    model.to(lowerCamelCase_      )
    model.eval()
    _a					:  Optional[int]						=  input_ids.unsqueeze(1      ).expand(-1		,      self.num_choices		,      -1      ).contiguous()
    _a					:  Optional[int]						=  input_mask.unsqueeze(1      ).expand(-1		,      self.num_choices		,      -1      ).contiguous()
    _a					:  List[str]						=  model(
        lowerCamelCase_		,      attention_mask=lowerCamelCase_		,      labels=lowerCamelCase_		,      )
    self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_choices)      )




 def   __UpperCamelCase   (    self      )      ->    int:
    _a					:  str						=  self.prepare_config_and_inputs()
    ((_a)		,						(_a)		,						(_a)		,						(_a)		,						(_a)		,						(_a))					:  Tuple						=  config_and_inputs
    _a					:  Any						=  {'input_ids': input_ids, 'attention_mask': input_mask}
    return config, inputs_dict







@require_torch
class 					a			(				snake_case__			,							snake_case__			,							unittest.TestCase  ):
 '''simple docstring'''




 __lowerCAmelCase						: List[str]							  =		(
     (
         SqueezeBertModel,
         SqueezeBertForMaskedLM,
         SqueezeBertForMultipleChoice,
         SqueezeBertForQuestionAnswering,
         SqueezeBertForSequenceClassification,
         SqueezeBertForTokenClassification,
     )
     if is_torch_available()
     else None
 )
 __lowerCAmelCase						: Dict							  =		(
     {
         """feature-extraction""": SqueezeBertModel,
         """fill-mask""": SqueezeBertForMaskedLM,
         """question-answering""": SqueezeBertForQuestionAnswering,
         """text-classification""": SqueezeBertForSequenceClassification,
         """token-classification""": SqueezeBertForTokenClassification,
         """zero-shot""": SqueezeBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __lowerCAmelCase						: Tuple							  =		False
 __lowerCAmelCase						: List[Any]							  =		True
 __lowerCAmelCase						: Optional[Any]							  =		False




 def   __UpperCamelCase   (    self      )      ->    Optional[Any]:
    _a					:  List[Any]						=  SqueezeBertModelTester(self      )
    _a					:  Any						=  ConfigTester(self		,      config_class=lowerCamelCase_		,      dim=3_7      )




 def   __UpperCamelCase   (    self      )      ->    Union[str, Any]:
    self.config_tester.run_common_tests()




 def   __UpperCamelCase   (    self      )      ->    Optional[int]:
    _a					:  Dict						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_squeezebert_model(*lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    List[Any]:
    _a					:  List[Any]						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    List[str]:
    _a					:  Optional[Any]						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    Union[str, Any]:
    _a					:  Tuple						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    Any:
    _a					:  Union[str, Any]						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    Union[str, Any]:
    _a					:  Any						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCamelCase_      )




 @slow
 def   __UpperCamelCase   (    self      )      ->    str:
    for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _a					:  Optional[int]						=  SqueezeBertModel.from_pretrained(lowerCamelCase_      )
       self.assertIsNotNone(lowerCamelCase_      )







@require_sentencepiece
@require_tokenizers
@require_torch
class 					a			(				unittest.TestCase  ):
 '''simple docstring'''




 @slow
 def   __UpperCamelCase   (    self      )      ->    Optional[int]:
    _a					:  Union[str, Any]						=  SqueezeBertForSequenceClassification.from_pretrained('squeezebert/squeezebert-mnli'      )

    _a					:  Tuple						=  torch.tensor([[1, 2_9_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 1_3, 1_5_8_8, 2]]      )
    _a					:  str						=  model(lowerCamelCase_      )[0]
    _a					:  List[str]						=  torch.Size((1, 3)      )
    self.assertEqual(output.shape		,      lowerCamelCase_      )
    _a					:  Tuple						=  torch.tensor([[0.6401, -0.0349, -0.6041]]      )
    self.assertTrue(torch.allclose(lowerCamelCase_		,      lowerCamelCase_		,      atol=1e-4      )      )






'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_      :		Dict          =		logging.get_logger(__name__)

UpperCAmelCase_      :		List[str]          =		{
    "uclanlp/visualbert-vqa": "https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json",
    "uclanlp/visualbert-vqa-pre": "https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json",
    "uclanlp/visualbert-vqa-coco-pre": (
        "https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json"
    ),
    "uclanlp/visualbert-vcr": "https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json",
    "uclanlp/visualbert-vcr-pre": "https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json",
    "uclanlp/visualbert-vcr-coco-pre": (
        "https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json"
    ),
    "uclanlp/visualbert-nlvr2": "https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json",
    "uclanlp/visualbert-nlvr2-pre": "https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json",
    "uclanlp/visualbert-nlvr2-coco-pre": (
        "https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json"
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}







class 					a			(				snake_case__  ):
 '''simple docstring'''




 __lowerCAmelCase						: int							  =		"""visual_bert"""




 def __init__(    self		,      lowerCamelCase_=3_0_5_2_2		,      lowerCamelCase_=7_6_8		,      lowerCamelCase_=5_1_2		,      lowerCamelCase_=1_2		,      lowerCamelCase_=1_2		,      lowerCamelCase_=3_0_7_2		,      lowerCamelCase_="gelu"		,      lowerCamelCase_=0.1		,      lowerCamelCase_=0.1		,      lowerCamelCase_=5_1_2		,      lowerCamelCase_=2		,      lowerCamelCase_=0.02		,      lowerCamelCase_=1e-12		,      lowerCamelCase_=False		,      lowerCamelCase_=True		,      lowerCamelCase_=1		,      lowerCamelCase_=0		,      lowerCamelCase_=2		,      **lowerCamelCase_		,      )      ->    Optional[Any]:
    super().__init__(pad_token_id=lowerCamelCase_		,      bos_token_id=lowerCamelCase_		,      eos_token_id=lowerCamelCase_		,      **lowerCamelCase_      )

    _a					:  List[str]						=  vocab_size
    _a					:  Union[str, Any]						=  max_position_embeddings
    _a					:  Tuple						=  hidden_size
    _a					:  Tuple						=  visual_embedding_dim
    _a					:  Optional[Any]						=  num_hidden_layers
    _a					:  str						=  num_attention_heads
    _a					:  Union[str, Any]						=  intermediate_size
    _a					:  List[str]						=  hidden_act
    _a					:  str						=  hidden_dropout_prob
    _a					:  List[Any]						=  attention_probs_dropout_prob
    _a					:  Optional[Any]						=  initializer_range
    _a					:  List[Any]						=  type_vocab_size
    _a					:  Any						=  layer_norm_eps
    _a					:  Tuple						=  bypass_transformer
    _a					:  str						=  special_visual_initialize


