

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     a__    :







  """simple docstring"""




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







        SCREAMING_SNAKE_CASE	: Any					       =				parent
        SCREAMING_SNAKE_CASE	: str					       =				batch_size
        SCREAMING_SNAKE_CASE	: Dict					       =				seq_length
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				is_training
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				use_input_mask
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				use_labels
        SCREAMING_SNAKE_CASE	: Dict					       =				vocab_size
        SCREAMING_SNAKE_CASE	: List[str]					       =				hidden_size
        SCREAMING_SNAKE_CASE	: List[str]					       =				projection_dim
        SCREAMING_SNAKE_CASE	: Any					       =				num_hidden_layers
        SCREAMING_SNAKE_CASE	: int					       =				num_attention_heads
        SCREAMING_SNAKE_CASE	: str					       =				intermediate_size
        SCREAMING_SNAKE_CASE	: List[Any]					       =				dropout
        SCREAMING_SNAKE_CASE	: List[str]					       =				attention_dropout
        SCREAMING_SNAKE_CASE	: int					       =				max_position_embeddings
        SCREAMING_SNAKE_CASE	: List[Any]					       =				initializer_range
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				scope
        SCREAMING_SNAKE_CASE	: str					       =				bos_token_id


  def 	_lowercase (     self      :	Dict						)   ->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: List[str]					       =				ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size						)

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

        if input_mask is not None:
              SCREAMING_SNAKE_CASE       ,		SCREAMING_SNAKE_CASE	: int					       =				input_mask.shape
              SCREAMING_SNAKE_CASE	: Dict					       =				np.random.randint(1			,			seq_length - 1			,			size=(batch_size,)						)
              for batch_idx, start_index in enumerate(UpperCAmelCase__						):
                    SCREAMING_SNAKE_CASE	: List[Any]					       =				1
                    SCREAMING_SNAKE_CASE	: List[Any]					       =				0

        SCREAMING_SNAKE_CASE	: List[Any]					       =				self.get_config()

        return config, input_ids, input_mask


  def 	_lowercase (     self      :	Dict						)   ->Optional[int]:
        """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 	_lowercase (     self      :	Any			,			UpperCAmelCase__      :	Union[str, Any]			,			UpperCAmelCase__      :	Optional[int]			,			UpperCAmelCase__      :	Dict						)   ->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[int]					       =				BlipTextModel(config=UpperCAmelCase__						)
        model.to(UpperCAmelCase__						)
        model.eval()
        with torch.no_grad():
              SCREAMING_SNAKE_CASE	: Dict					       =				model(UpperCAmelCase__			,			attention_mask=UpperCAmelCase__						)
              SCREAMING_SNAKE_CASE	: Dict					       =				model(UpperCAmelCase__						)
        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 	_lowercase (     self      :	Union[str, Any]						)   ->int:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Any					       =				self.prepare_config_and_inputs()
        SCREAMING_SNAKE_CASE       ,		SCREAMING_SNAKE_CASE       ,		SCREAMING_SNAKE_CASE	: Optional[int]					       =				config_and_inputs
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				{"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_torch
class     a__    (				UpperCAmelCase		,     unittest.TestCase    ):







  """simple docstring"""




  UpperCAmelCase__ :	Union[str, Any]         =(BlipTextModel,) if is_torch_available() else ()
  UpperCAmelCase__ :	str         =False
  UpperCAmelCase__ :	Any         =False
  UpperCAmelCase__ :	Any         =False


  def 	_lowercase (     self      :	List[str]						)   ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				BlipTextModelTester(self						)
        SCREAMING_SNAKE_CASE	: str					       =				ConfigTester(self			,			config_class=UpperCAmelCase__			,			hidden_size=3_7						)


  def 	_lowercase (     self      :	str						)   ->int:
        """simple docstring"""







        self.config_tester.run_common_tests()


  def 	_lowercase (     self      :	str						)   ->str:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Any					       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCAmelCase__						)


  def 	_lowercase (     self      :	Tuple						)   ->Tuple:
        """simple docstring"""







        pass


  def 	_lowercase (     self      :	List[Any]						)   ->List[Any]:
        """simple docstring"""







        pass


  @unittest.skip(reason="""Blip does not use inputs_embeds"""						)
  def 	_lowercase (     self      :	List[str]						)   ->Tuple:
        """simple docstring"""







        pass


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







        pass


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







        pass


  @slow
  def 	_lowercase (     self      :	int						)   ->Optional[Any]:
        """simple docstring"""







        for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE	: Dict					       =				BlipTextModel.from_pretrained(UpperCAmelCase__						)
              self.assertIsNotNone(UpperCAmelCase__						)




  def 	_lowercase (     self      :	Optional[int]						)   ->Optional[Any]:
        """simple docstring"""







        super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCAmelCase__						)







from typing import TYPE_CHECKING

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


UpperCAmelCase__	:       int			      =      {
    """configuration_speech_to_text_2""": ["""SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2Text2Config"""],
    """processing_speech_to_text_2""": ["""Speech2Text2Processor"""],
    """tokenization_speech_to_text_2""": ["""Speech2Text2Tokenizer"""],
}


try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__	:       int			      =      [
      """SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """Speech2Text2ForCausalLM""",
      """Speech2Text2PreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_speech_to_text_a import SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextaConfig
  from .processing_speech_to_text_a import SpeechaTextaProcessor
  from .tokenization_speech_to_text_a import SpeechaTextaTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_speech_to_text_a import (
        SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST,
        SpeechaTextaForCausalLM,
        SpeechaTextaPreTrainedModel,
    )

else:
  import sys

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




