


import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
   import torch

   from transformers import BlipTextModel
   from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST


class       __lowerCAmelCase       :






  """simple docstring"""





  def __init__(       self    :    Dict      ,			_lowerCAmelCase    :    Union[str, Any]      ,			_lowerCAmelCase    :    List[Any]=1_2      ,			_lowerCAmelCase    :    str=7      ,			_lowerCAmelCase    :    Tuple=True      ,			_lowerCAmelCase    :    str=True      ,			_lowerCAmelCase    :    List[Any]=True      ,			_lowerCAmelCase    :    List[str]=9_9      ,			_lowerCAmelCase    :    Dict=3_2      ,			_lowerCAmelCase    :    Tuple=3_2      ,			_lowerCAmelCase    :    Tuple=5      ,			_lowerCAmelCase    :    str=4      ,			_lowerCAmelCase    :    str=3_7      ,			_lowerCAmelCase    :    Optional[int]=0.1      ,			_lowerCAmelCase    :    Dict=0.1      ,			_lowerCAmelCase    :    List[Any]=5_1_2      ,			_lowerCAmelCase    :    str=0.02      ,			_lowerCAmelCase    :    Optional[int]=0      ,			_lowerCAmelCase    :    Dict=None      ,			)				->	Optional[int]:




     """simple docstring"""
     snake_case_         =   parent
     snake_case_         =   batch_size
     snake_case_         =   seq_length
     snake_case_         =   is_training
     snake_case_         =   use_input_mask
     snake_case_         =   use_labels
     snake_case_         =   vocab_size
     snake_case_         =   hidden_size
     snake_case_         =   projection_dim
     snake_case_         =   num_hidden_layers
     snake_case_         =   num_attention_heads
     snake_case_         =   intermediate_size
     snake_case_         =   dropout
     snake_case_         =   attention_dropout
     snake_case_         =   max_position_embeddings
     snake_case_         =   initializer_range
     snake_case_         =   scope
     snake_case_         =   bos_token_id



  def    lowerCAmelCase__					(       self    :    Dict					)				->	List[Any]:




     """simple docstring"""
     snake_case_         =   ids_tensor([self.batch_size, self.seq_length]      ,			self.vocab_size					)

     snake_case_         =   None
     if self.use_input_mask:
        snake_case_         =   random_attention_mask([self.batch_size, self.seq_length]					)

     if input_mask is not None:
        snake_case_      ,      snake_case_         =   input_mask.shape
        snake_case_         =   np.random.randint(1      ,			seq_length - 1      ,			size=(batch_size,)					)
        for batch_idx, start_index in enumerate(_lowerCAmelCase					):
           snake_case_         =   1
           snake_case_         =   0

     snake_case_         =   self.get_config()

     return config, input_ids, input_mask



  def    lowerCAmelCase__					(       self    :    List[str]					)				->	Dict:




     """simple docstring"""
     return BlipTextConfig(
         vocab_size=self.vocab_size      ,			hidden_size=self.hidden_size      ,			projection_dim=self.projection_dim      ,			num_hidden_layers=self.num_hidden_layers      ,			num_attention_heads=self.num_attention_heads      ,			intermediate_size=self.intermediate_size      ,			dropout=self.dropout      ,			attention_dropout=self.attention_dropout      ,			max_position_embeddings=self.max_position_embeddings      ,			initializer_range=self.initializer_range      ,			bos_token_id=self.bos_token_id      ,			)



  def    lowerCAmelCase__					(       self    :    Dict      ,			_lowerCAmelCase    :    str      ,			_lowerCAmelCase    :    List[Any]      ,			_lowerCAmelCase    :    Dict					)				->	Optional[int]:




     """simple docstring"""
     snake_case_         =   BlipTextModel(config=_lowerCAmelCase					)
     model.to(_lowerCAmelCase					)
     model.eval()
     with torch.no_grad():
        snake_case_         =   model(_lowerCAmelCase      ,			attention_mask=_lowerCAmelCase					)
        snake_case_         =   model(_lowerCAmelCase					)
     self.parent.assertEqual(result.last_hidden_state.shape      ,			(self.batch_size, self.seq_length, self.hidden_size)					)
     self.parent.assertEqual(result.pooler_output.shape      ,			(self.batch_size, self.hidden_size)					)



  def    lowerCAmelCase__					(       self    :    Any					)				->	Optional[int]:




     """simple docstring"""
     snake_case_         =   self.prepare_config_and_inputs()
     snake_case_      ,      snake_case_      ,      snake_case_         =   config_and_inputs
     snake_case_         =   {"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict






@require_torch
class       __lowerCAmelCase       (	a	,					unittest.TestCase   ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE						      =    (BlipTextModel,) if is_torch_available() else ()
  _SCREAMING_SNAKE_CASE						      =    False
  _SCREAMING_SNAKE_CASE						      =    False
  _SCREAMING_SNAKE_CASE						      =    False



  def    lowerCAmelCase__					(       self    :    int					)				->	Optional[Any]:




     """simple docstring"""
     snake_case_         =   BlipTextModelTester(self					)
     snake_case_         =   ConfigTester(self      ,			config_class=_lowerCAmelCase      ,			hidden_size=3_7					)



  def    lowerCAmelCase__					(       self    :    List[Any]					)				->	Optional[Any]:




     """simple docstring"""
     self.config_tester.run_common_tests()



  def    lowerCAmelCase__					(       self    :    List[Any]					)				->	Dict:




     """simple docstring"""
     snake_case_         =   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCAmelCase					)



  def    lowerCAmelCase__					(       self    :    Optional[Any]					)				->	str:




     """simple docstring"""
     pass



  def    lowerCAmelCase__					(       self    :    List[Any]					)				->	List[str]:




     """simple docstring"""
     pass



  @unittest.skip(reason="Blip does not use inputs_embeds"					)
  def    lowerCAmelCase__					(       self    :    List[str]					)				->	Union[str, Any]:




     """simple docstring"""
     pass



  @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"					)
  def    lowerCAmelCase__					(       self    :    List[str]					)				->	List[Any]:




     """simple docstring"""
     pass



  @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"					)
  def    lowerCAmelCase__					(       self    :    Tuple					)				->	int:




     """simple docstring"""
     pass



  @slow
  def    lowerCAmelCase__					(       self    :    Dict					)				->	int:




     """simple docstring"""
     for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        snake_case_         =   BlipTextModel.from_pretrained(_lowerCAmelCase					)
        self.assertIsNotNone(_lowerCAmelCase					)



  def    lowerCAmelCase__					(       self    :    str					)				->	Dict:




     """simple docstring"""
     super().test_pt_tf_model_equivalence(allow_missing_keys=_lowerCAmelCase					)








from __future__ import annotations



def      _lowerCAmelCase  (							lowerCAmelCase_							:int							)->list[int]:
   '''simple docstring'''






   assert (
       isinstance(lowerCAmelCase_						,  lowerCAmelCase_							) and mask > 0
   ), F'''mask needs to be positive integer, your input {mask}'''
   snake_case_         =   []
   snake_case_         =   mask

   while submask:
      all_submasks.append(lowerCAmelCase_							)
      snake_case_         =   (submask - 1) & mask

   return all_submasks


if __name__ == "__main__":
   import doctest

   doctest.testmod()




