
'''simple docstring'''





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

 '''simple docstring'''
 def __init__(				self		:     Dict ,							snake_case__		:     str ,							snake_case__		:     Dict=12 ,							snake_case__		:     List[str]=7 ,							snake_case__		:     List[Any]=True ,							snake_case__		:     Union[str, Any]=True ,							snake_case__		:     Optional[Any]=True ,							snake_case__		:     Union[str, Any]=99 ,							snake_case__		:     Tuple=32 ,							snake_case__		:     Dict=32 ,							snake_case__		:     List[Any]=5 ,							snake_case__		:     str=4 ,							snake_case__		:     Any=37 ,							snake_case__		:     str=0.1 ,							snake_case__		:     Optional[Any]=0.1 ,							snake_case__		:     Dict=5_12 ,							snake_case__		:     Dict=0.02 ,							snake_case__		:     str=0 ,							snake_case__		:     Optional[Any]=None ,							):



   '''simple docstring'''




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



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[Any]												=       ids_tensor([self.batch_size, self.seq_length] ,							self.vocab_size   )

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

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

   UpperCAmelCase__			:				List[str]												=       self.get_config()

   return config, input_ids, input_mask
 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''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        UpperCamelCase     (				self		:     Union[str, Any] ,							snake_case__		:     str ,							snake_case__		:     Optional[int] ,							snake_case__		:     int   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Dict												=       BlipTextModel(config=snake_case__   )
   model.to(snake_case__   )
   model.eval()
   with torch.no_grad():
     UpperCAmelCase__			:				int												=       model(snake_case__ ,							attention_mask=snake_case__   )
     UpperCAmelCase__			:				Dict												=       model(snake_case__   )
   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        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Any												=       self.prepare_config_and_inputs()
   UpperCAmelCase__			:				List[Any]												=       config_and_inputs
   UpperCAmelCase__			:				Dict												=       {"input_ids": input_ids, "attention_mask": input_mask}
   return config, inputs_dict






@require_torch
class        UpperCAmelCase_       (						A				,							unittest.TestCase							):

 '''simple docstring'''
 lowercase_				:						Union[str, Any]				=       (BlipTextModel,) if is_torch_available() else ()
 lowercase_				:						List[Any]				=       False
 lowercase_				:						Dict				=       False
 lowercase_				:						Optional[int]				=       False
 def        UpperCamelCase     (				self		:     Optional[Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       BlipTextModelTester(self   )
   UpperCAmelCase__			:				List[Any]												=       ConfigTester(self ,							config_class=snake_case__ ,							hidden_size=37   )
 def        UpperCamelCase     (				self		:     Dict   ):



   '''simple docstring'''




   self.config_tester.run_common_tests()
 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Any												=       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*snake_case__   )
 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   pass
 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   pass
 @unittest.skip(reason="Blip does not use inputs_embeds"   )
 def        UpperCamelCase     (				self		:     int   ):



   '''simple docstring'''




   pass
 @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
 def        UpperCamelCase     (				self		:     str   ):



   '''simple docstring'''




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



   '''simple docstring'''




   pass
 @slow
 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCAmelCase__			:				Optional[Any]												=       BlipTextModel.from_pretrained(snake_case__   )
     self.assertIsNotNone(snake_case__   )







 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case__   )





'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


SCREAMING_SNAKE_CASE    =					{
    """configuration_donut_swin""": ["""DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DonutSwinConfig"""],
    """processing_donut""": ["""DonutProcessor"""],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE    =					[
          """DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """DonutSwinModel""",
          """DonutSwinPreTrainedModel""",
      ]

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE    =					["""DonutFeatureExtractor"""]
      SCREAMING_SNAKE_CASE    =					["""DonutImageProcessor"""]


if TYPE_CHECKING:
      from .configuration_donut_swin import DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, DonutSwinConfig
      from .processing_donut import DonutProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_donut_swin import (
                DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
                DonutSwinModel,
                DonutSwinPreTrainedModel,
            )

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_donut import DonutFeatureExtractor
            from .image_processing_donut import DonutImageProcessor

else:
      import sys

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



